# All files / src/demux exp-golomb.js

0% Statements 0/163
0% Branches 0/74
0% Functions 0/16
0% Lines 0/147

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307

     /**
     * Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
    */

    import { logger } from '../utils/logger';

    class ExpGolomb {
      constructor (data) {
        this.data = data;
        // the number of bytes left to examine in this.data
        this.bytesAvailable = data.byteLength;
        // the current word being examined
        this.word = 0; // :uint
        // the number of bits left to examine in the current word
        this.bitsAvailable = 0; // :uint
      }

      // ():void
      loadWord () {
        let
          data = this.data,
          bytesAvailable = this.bytesAvailable,
          position = data.byteLength - bytesAvailable,
          workingBytes = new Uint8Array(4),
          availableBytes = Math.min(4, bytesAvailable);
        if (availableBytes === 0) {
          throw new Error('no bytes available');
        }

        workingBytes.set(data.subarray(position, position + availableBytes));
        this.word = new DataView(workingBytes.buffer).getUint32(0);
        // track the amount of this.data that has been processed
        this.bitsAvailable = availableBytes * 8;
        this.bytesAvailable -= availableBytes;
      }

      // (count:int):void
      skipBits (count) {
        let skipBytes; // :int
        if (this.bitsAvailable > count) {
          this.word <<= count;
          this.bitsAvailable -= count;
        } else {
          count -= this.bitsAvailable;
          skipBytes = count >> 3;
          count -= (skipBytes >> 3);
          this.bytesAvailable -= skipBytes;
          this.loadWord();
          this.word <<= count;
          this.bitsAvailable -= count;
        }
      }

      // (size:int):uint
      readBits (size) {
        let
          bits = Math.min(this.bitsAvailable, size), // :uint
          valu = this.word >>> (32 - bits); // :uint
        if (size > 32) {
          logger.error('Cannot read more than 32 bits at a time');
        }

        this.bitsAvailable -= bits;
        if (this.bitsAvailable > 0) {
          this.word <<= bits;
        } else if (this.bytesAvailable > 0) {
          this.loadWord();
        }

        bits = size - bits;
        if (bits > 0 && this.bitsAvailable) {
          return valu << bits | this.readBits(bits);
        } else {
          return valu;
        }
      }

      // ():uint
      skipLZ () {
        let leadingZeroCount; // :uint
        for (leadingZeroCount = 0; leadingZeroCount < this.bitsAvailable; ++leadingZeroCount) {
          if ((this.word & (0x80000000 >>> leadingZeroCount)) !== 0) {
            // the first bit of working word is 1
            this.word <<= leadingZeroCount;
            this.bitsAvailable -= leadingZeroCount;
            return leadingZeroCount;
          }
        }
        // we exhausted word and still have not found a 1
        this.loadWord();
        return leadingZeroCount + this.skipLZ();
      }

      // ():void
      skipUEG () {
        this.skipBits(1 + this.skipLZ());
      }

      // ():void
      skipEG () {
        this.skipBits(1 + this.skipLZ());
      }

      // ():uint
      readUEG () {
        let clz = this.skipLZ(); // :uint
        return this.readBits(clz + 1) - 1;
      }

      // ():int
      readEG () {
        let valu = this.readUEG(); // :int
        if (0x01 & valu) {
          // the number is odd if the low order bit is set
          return (1 + valu) >>> 1; // add 1 to make it even, and divide by 2
        } else {
          return -1 * (valu >>> 1); // divide by two then make it negative
        }
      }

      // Some convenience functions
      // :Boolean
      readBoolean () {
        return this.readBits(1) === 1;
      }

      // ():int
      readUByte () {
        return this.readBits(8);
      }

      // ():int
      readUShort () {
        return this.readBits(16);
      }
      // ():int
      readUInt () {
        return this.readBits(32);
      }

      /**
       * Advance the ExpGolomb decoder past a scaling list. The scaling
       * list is optionally transmitted as part of a sequence parameter
       * set and is not relevant to transmuxing.
       * @param count {number} the number of entries in this scaling list
       * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
       */
      skipScalingList (count) {
        let
          lastScale = 8,
          nextScale = 8,
          j,
          deltaScale;
        for (j = 0; j < count; j++) {
          if (nextScale !== 0) {
            deltaScale = this.readEG();
            nextScale = (lastScale + deltaScale + 256) % 256;
          }
          lastScale = (nextScale === 0) ? lastScale : nextScale;
        }
      }

      /**
       * Read a sequence parameter set and return some interesting video
       * properties. A sequence parameter set is the H264 metadata that
       * describes the properties of upcoming video frames.
       * @param data {Uint8Array} the bytes of a sequence parameter set
       * @return {object} an object with configuration parsed from the
       * sequence parameter set, including the dimensions of the
       * associated video frames.
       */
      readSPS () {
        let
          frameCropLeftOffset = 0,
          frameCropRightOffset = 0,
          frameCropTopOffset = 0,
          frameCropBottomOffset = 0,
          profileIdc, profileCompat, levelIdc,
          numRefFramesInPicOrderCntCycle, picWidthInMbsMinus1,
          picHeightInMapUnitsMinus1,
          frameMbsOnlyFlag,
          scalingListCount,
          i,
          readUByte = this.readUByte.bind(this),
          readBits = this.readBits.bind(this),
          readUEG = this.readUEG.bind(this),
          readBoolean = this.readBoolean.bind(this),
          skipBits = this.skipBits.bind(this),
          skipEG = this.skipEG.bind(this),
          skipUEG = this.skipUEG.bind(this),
          skipScalingList = this.skipScalingList.bind(this);

        readUByte();
        profileIdc = readUByte(); // profile_idc
        profileCompat = readBits(5); // constraint_set[0-4]_flag, u(5)
        skipBits(3); // reserved_zero_3bits u(3),
        levelIdc = readUByte(); // level_idc u(8)
        skipUEG(); // seq_parameter_set_id
        // some profiles have more optional data we don't need
        if (profileIdc === 100 ||
            profileIdc === 110 ||
            profileIdc === 122 ||
            profileIdc === 244 ||
            profileIdc === 44 ||
            profileIdc === 83 ||
            profileIdc === 86 ||
            profileIdc === 118 ||
            profileIdc === 128) {
          let chromaFormatIdc = readUEG();
          if (chromaFormatIdc === 3) {
            skipBits(1);
          } // separate_colour_plane_flag

          skipUEG(); // bit_depth_luma_minus8
          skipUEG(); // bit_depth_chroma_minus8
          skipBits(1); // qpprime_y_zero_transform_bypass_flag
          if (readBoolean()) { // seq_scaling_matrix_present_flag
            scalingListCount = (chromaFormatIdc !== 3) ? 8 : 12;
            for (i = 0; i < scalingListCount; i++) {
              if (readBoolean()) { // seq_scaling_list_present_flag[ i ]
                if (i < 6) {
                  skipScalingList(16);
                } else {
                  skipScalingList(64);
                }
              }
            }
          }
        }
        skipUEG(); // log2_max_frame_num_minus4
        let picOrderCntType = readUEG();
        if (picOrderCntType === 0) {
          readUEG(); // log2_max_pic_order_cnt_lsb_minus4
        } else if (picOrderCntType === 1) {
          skipBits(1); // delta_pic_order_always_zero_flag
          skipEG(); // offset_for_non_ref_pic
          skipEG(); // offset_for_top_to_bottom_field
          numRefFramesInPicOrderCntCycle = readUEG();
          for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
            skipEG();
          } // offset_for_ref_frame[ i ]
        }
        skipUEG(); // max_num_ref_frames
        skipBits(1); // gaps_in_frame_num_value_allowed_flag
        picWidthInMbsMinus1 = readUEG();
        picHeightInMapUnitsMinus1 = readUEG();
        frameMbsOnlyFlag = readBits(1);
        if (frameMbsOnlyFlag === 0) {
          skipBits(1);
        } // mb_adaptive_frame_field_flag

        skipBits(1); // direct_8x8_inference_flag
        if (readBoolean()) { // frame_cropping_flag
          frameCropLeftOffset = readUEG();
          frameCropRightOffset = readUEG();
          frameCropTopOffset = readUEG();
          frameCropBottomOffset = readUEG();
        }
        let pixelRatio = [1, 1];
        if (readBoolean()) {
          // vui_parameters_present_flag
          if (readBoolean()) {
            // aspect_ratio_info_present_flag
            const aspectRatioIdc = readUByte();
            switch (aspectRatioIdc) {
            case 1: pixelRatio = [1, 1]; break;
            case 2: pixelRatio = [12, 11]; break;
            case 3: pixelRatio = [10, 11]; break;
            case 4: pixelRatio = [16, 11]; break;
            case 5: pixelRatio = [40, 33]; break;
            case 6: pixelRatio = [24, 11]; break;
            case 7: pixelRatio = [20, 11]; break;
            case 8: pixelRatio = [32, 11]; break;
            case 9: pixelRatio = [80, 33]; break;
            case 10: pixelRatio = [18, 11]; break;
            case 11: pixelRatio = [15, 11]; break;
            case 12: pixelRatio = [64, 33]; break;
            case 13: pixelRatio = [160, 99]; break;
            case 14: pixelRatio = [4, 3]; break;
            case 15: pixelRatio = [3, 2]; break;
            case 16: pixelRatio = [2, 1]; break;
            case 255: {
              pixelRatio = [readUByte() << 8 | readUByte(), readUByte() << 8 | readUByte()];
              break;
            }
            }
          }
        }
        return {
          width: Math.ceil((((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 2)),
          height: ((2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16) - ((frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)),
          pixelRatio: pixelRatio
        };
      }

      readSliceType () {
        // skip NALu type
        this.readUByte();
        // discard first_mb_in_slice
        this.readUEG();
        // return slice_type
        return this.readUEG();
      }
    }

    export default ExpGolomb;