1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233

media / test / data / blackwhite.html [blame]

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        color: white;
        background-color: black;
      }
    </style>
  </head>
  <body onload="main()">
    <div id="buttons"></div>
    <table>
      <tr>
        <td>Image</td>
        <td id="video_header"></td>
        <td>Absolute Diff</td>
        <td>Different Pixels</td>
      </tr>
      <tr>
        <td><img src="blackwhite.png"></div>
        <td><video autoplay></video></div>
        <td><canvas id="diff"></canvas></td>
        <td><canvas id="mask"></canvas></td>
      </tr>
    </div>

    <p id="result"></p>

    <script>
      function log(str) {
        document.getElementById('result').textContent = str;
        console.log(str);
      }

      function loadVideo(name) {
        var videoElem = document.querySelector('video');
        document.getElementById('video_header').textContent = name;
        videoElem.src = 'blackwhite_' + name;
      }

      function onVideoFrame(e) {
        document.title = verifyVideo() ? 'ENDED' : 'FAILED';
      }

      function onVideoError(e) {
        document.title = 'ERROR';
        document.getElementById('diff').style.visibility = 'hidden';
        document.getElementById('mask').style.visibility = 'hidden';
        log('Error playing video: ' + e.target.error.code + '.');
      }

      function main() {
        // Programmatically create buttons for each clip for manual testing.
        var buttonsElem = document.getElementById('buttons');

        function createButton(name) {
          var buttonElem = document.createElement('button');
          buttonElem.textContent = name;
          buttonElem.addEventListener('click', function() {
            loadVideo(name);
          });
          buttonsElem.appendChild(buttonElem);
        }

        var VIDEOS = [
          'yuv420p.ogv',
          'yuv422p.ogv',
          'yuv444p.ogv',
          'yuv420p.webm',
          'yuv444p.webm',
          'yuv420p.mp4',
          'yuv420p_rec709.mp4',
          'yuvj420p.mp4',
          'yuv422p.mp4',
          'yuv444p.mp4',
          'yuv420p_hi10p.mp4'
        ];

        for (var i = 0; i < VIDEOS.length; ++i) {
          createButton(VIDEOS[i]);
        }

        // Video event handlers.
        var videoElem = document.querySelector('video');

        // Check if a query parameter was provided for automated tests.
        if (window.location.search.length > 1) {
          videoElem.addEventListener('error', onVideoError);
          videoElem.requestVideoFrameCallback(onVideoFrame);
          loadVideo(window.location.search.substr(1));
        } else {
          // If we're not an automated test, compute some pretty diffs.
          document.querySelector('video').addEventListener('ended',
                                                           computeDiffs);
        }
      }

      function getCanvasPixels(canvas) {
        try {
          return canvas.getContext('2d')
              .getImageData(0, 0, canvas.width, canvas.height)
              .data;
        } catch(e) {
          var message = 'ERROR: ' + e;
          if (e.name == 'SecurityError') {
            message += ' Couldn\'t get image pixels, try running with ' +
                       '--allow-file-access-from-files.';
          }
          log(message);
        }
      }

      function verifyVideo() {
        var videoElem = document.querySelector('video');
        var offscreen = document.createElement('canvas');
        offscreen.width = videoElem.videoWidth;
        offscreen.height = videoElem.videoHeight;
        offscreen.getContext('2d').drawImage(videoElem, 0, 0, offscreen.width,
                                             offscreen.height);

        videoData = getCanvasPixels(offscreen);
        if (!videoData)
          return false;

        // Check the color of a givel pixel |x,y| in |imgData| against an
        // expected value, |expected|, with up to |allowedError| difference.
        function checkColor(imgData, x, y, stride, expected, allowedError) {
          for (var i = 0; i < 3; ++i) {
            var actual = imgData[(x + y * stride) * 4 + i];
            if (Math.abs(actual - expected) > allowedError) {
              log('Color didn\'t match at (' + x + ', ' + y + '). Expected: ' +
                  expected + ', actual: ' + actual);
              return false;
            }
          }
          return true;
        }

        // Check one pixel in each quadrant (in the upper left, away from
        // boundaries and the text, to avoid compression artifacts).
        // Also allow a small error, for the same reason.

        // TODO(mtomasz): Once code.google.com/p/libyuv/issues/detail?id=324 is
        // fixed, the allowedError should be decreased to 1.
        var allowedError = 2;

        return checkColor(videoData, 30, 30, videoElem.videoWidth, 0xff,
                          allowedError) &&
               checkColor(videoData, 150, 30, videoElem.videoWidth, 0x00,
                          allowedError) &&
               checkColor(videoData, 30, 150, videoElem.videoWidth, 0x10,
                          allowedError) &&
               checkColor(videoData, 150, 150, videoElem.videoWidth, 0xef,
                          allowedError);
      }

      // Compute a standard diff image, plus a high-contrast mask that shows
      // each differing pixel more visibly.
      function computeDiffs() {
        var diffElem = document.getElementById('diff');
        var maskElem = document.getElementById('mask');
        var videoElem = document.querySelector('video');
        var imgElem = document.querySelector('img');

        var width = imgElem.width;
        var height = imgElem.height;

        if (videoElem.videoWidth != width || videoElem.videoHeight != height) {
          log('ERROR: video dimensions don\'t match reference image ' +
              'dimensions');
          return;
        }

        // Make an offscreen canvas to dump reference image pixels into.
        var offscreen = document.createElement('canvas');
        offscreen.width = width;
        offscreen.height = height;

        offscreen.getContext('2d').drawImage(imgElem, 0, 0, width, height);
        imgData = getCanvasPixels(offscreen);
        if (!imgData)
          return;

        // Scale and clear diff canvases.
        diffElem.width = maskElem.width = width;
        diffElem.height = maskElem.height = height;
        var diffCtx = diffElem.getContext('2d');
        var maskCtx = maskElem.getContext('2d');
        maskCtx.clearRect(0, 0, width, height);
        diffCtx.clearRect(0, 0, width, height);

        // Copy video pixels into diff.
        diffCtx.drawImage(videoElem, 0, 0, width, height);

        var diffIData = diffCtx.getImageData(0, 0, width, height);
        var diffData = diffIData.data;
        var maskIData = maskCtx.getImageData(0, 0, width, height);
        var maskData = maskIData.data;

        // Make diffs and collect stats.
        var meanSquaredError = 0;
        for (var i = 0; i < imgData.length; i += 4) {
          var difference = 0;
          for (var j = 0; j < 3; ++j) {
            diffData[i + j] = Math.abs(diffData[i + j] - imgData[i + j]);
            meanSquaredError += diffData[i + j] * diffData[i + j];
            if (diffData[i + j] != 0) {
              difference += diffData[i + j];
            }
          }
          if (difference > 0) {
            if (difference <= 3) {
              // If we're only off by a bit per channel or so, use darker red.
              maskData[i] = 128;
            } else {
              // Bright red to indicate a different pixel.
              maskData[i] = 255;
            }
            maskData[i+3] = 255;
          }
        }

        meanSquaredError /= width * height;
        log('Mean squared error: ' + meanSquaredError);
        diffCtx.putImageData(diffIData, 0, 0);
        maskCtx.putImageData(maskIData, 0, 0);
        document.getElementById('diff').style.visibility = 'visible';
        document.getElementById('mask').style.visibility = 'visible';
      }
    </script>
  </body>
</html>