File: videoFrame-orientation.any.js

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (96 lines) | stat: -rw-r--r-- 3,116 bytes parent folder | download | duplicates (12)
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
// META: global=window,dedicatedworker

function make4x2VideoFrame(rotation, flip) {
  // y y r r
  // b b g g
  let data = new Uint8Array([
    255, 255, 0, 255,
    255, 255, 0, 255,
    255, 0, 0, 255,
    255, 0, 0, 255,
    0, 0, 255, 255,
    0, 0, 255, 255,
    0, 255, 0, 255,
    0, 255, 0, 255,
  ]);
  return new VideoFrame(data, {
    format: "RGBX",
    codedWidth: 4,
    codedHeight: 2,
    timestamp: 0,
    rotation,
    flip
  });
}

// Verifies various properties about a frame created frommake4x2VideoFrame()
// with flip=true and a 90 degree rotation.
function validateOrientedFrame(frame) {
  assert_equals(frame.visibleRect.width, 4, 'visibleRect.width');
  assert_equals(frame.visibleRect.height, 2, 'visibleRect.height');
  assert_equals(frame.rotation, 90, 'rotation');
  assert_equals(frame.flip, true, 'flip');
  assert_equals(frame.displayWidth, 2, 'displayWidth');
  assert_equals(frame.displayHeight, 4, 'displayHeight');

  let width = frame.displayWidth;
  let height = frame.displayHeight;
  let canvas = new OffscreenCanvas(width, height);
  let ctx = canvas.getContext('2d');
  ctx.drawImage(frame, 0, 0);
  frame.close();
  let data = ctx.getImageData(0, 0, width, height).data;

  function check_pixel(x, y, expected_color) {
    let tolerance = 2;
    assert_approx_equals(data[4*(width*y+x)], expected_color[0], tolerance);
    assert_approx_equals(data[4*(width*y+x)+1], expected_color[1], tolerance);
    assert_approx_equals(data[4*(width*y+x)+2], expected_color[2], tolerance);
  }

  // Expected rendering:
  //   y b
  //   y b
  //   r g
  //   r g
  check_pixel(0, 0, [255, 255, 0]);
  check_pixel(1, 0, [0, 0, 255]);
  check_pixel(0, 3, [255, 0, 0]);
  check_pixel(1, 3, [0, 255, 0]);
}

test(_ => {
  let frame = make4x2VideoFrame(-315, true);
  validateOrientedFrame(frame);
}, 'Test oriented VideoFrame from ArrayBuffer');

test(_ => {
  for (let baseRotation = 0; baseRotation < 360; baseRotation += 90) {
    for (let baseFlip = 0; baseFlip < 2; ++baseFlip) {
      let baseFrame = make4x2VideoFrame(baseRotation, !!baseFlip);
      for (let deltaRotation = 0; deltaRotation < 360; deltaRotation += 90) {
        for (let deltaFlip = 0; deltaFlip < 2; ++deltaFlip) {
          let deltaFrame = new VideoFrame(baseFrame, {
              rotation: deltaRotation,
              flip: !!deltaFlip
          });
          let appliedRotation = !!baseFlip ? 360 - deltaRotation
                                           : deltaRotation;
          let expectedRotation = (baseRotation + appliedRotation) % 360;
          let expectedFlip = !!(baseFlip ^ deltaFlip);
          assert_equals(deltaFrame.rotation, expectedRotation, 'rotation');
          assert_equals(deltaFrame.flip, expectedFlip, 'flip');
          deltaFrame.close();
        }
      }
      baseFrame.close();
    }
  }
}, 'Test combinations of rotation and flip');

test(_ => {
  let orig_frame = make4x2VideoFrame(0, false);
  let frame = new VideoFrame(orig_frame, {rotation: -315, flip: true});
  orig_frame.close();
  validateOrientedFrame(frame);
}, 'Test orientation of wrapped VideoFrame');