Blame view

node_modules/mux.js/lib/utils/exp-golomb.js 3.74 KB
2a09d1a4   liuqimichale   添加宜春 天水 宣化
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
  'use strict';
  
  var ExpGolomb;
  
  /**
   * Parser for exponential Golomb codes, a variable-bitwidth number encoding
   * scheme used by h264.
   */
  ExpGolomb = function(workingData) {
    var
      // the number of bytes left to examine in workingData
      workingBytesAvailable = workingData.byteLength,
  
      // the current word being examined
      workingWord = 0, // :uint
  
      // the number of bits left to examine in the current word
      workingBitsAvailable = 0; // :uint;
  
    // ():uint
    this.length = function() {
      return (8 * workingBytesAvailable);
    };
  
    // ():uint
    this.bitsAvailable = function() {
      return (8 * workingBytesAvailable) + workingBitsAvailable;
    };
  
    // ():void
    this.loadWord = function() {
      var
        position = workingData.byteLength - workingBytesAvailable,
        workingBytes = new Uint8Array(4),
        availableBytes = Math.min(4, workingBytesAvailable);
  
      if (availableBytes === 0) {
        throw new Error('no bytes available');
      }
  
      workingBytes.set(workingData.subarray(position,
                                            position + availableBytes));
      workingWord = new DataView(workingBytes.buffer).getUint32(0);
  
      // track the amount of workingData that has been processed
      workingBitsAvailable = availableBytes * 8;
      workingBytesAvailable -= availableBytes;
    };
  
    // (count:int):void
    this.skipBits = function(count) {
      var skipBytes; // :int
      if (workingBitsAvailable > count) {
        workingWord          <<= count;
        workingBitsAvailable -= count;
      } else {
        count -= workingBitsAvailable;
        skipBytes = Math.floor(count / 8);
  
        count -= (skipBytes * 8);
        workingBytesAvailable -= skipBytes;
  
        this.loadWord();
  
        workingWord <<= count;
        workingBitsAvailable -= count;
      }
    };
  
    // (size:int):uint
    this.readBits = function(size) {
      var
        bits = Math.min(workingBitsAvailable, size), // :uint
        valu = workingWord >>> (32 - bits); // :uint
      // if size > 31, handle error
      workingBitsAvailable -= bits;
      if (workingBitsAvailable > 0) {
        workingWord <<= bits;
      } else if (workingBytesAvailable > 0) {
        this.loadWord();
      }
  
      bits = size - bits;
      if (bits > 0) {
        return valu << bits | this.readBits(bits);
      }
      return valu;
    };
  
    // ():uint
    this.skipLeadingZeros = function() {
      var leadingZeroCount; // :uint
      for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
        if ((workingWord & (0x80000000 >>> leadingZeroCount)) !== 0) {
          // the first bit of working word is 1
          workingWord <<= leadingZeroCount;
          workingBitsAvailable -= leadingZeroCount;
          return leadingZeroCount;
        }
      }
  
      // we exhausted workingWord and still have not found a 1
      this.loadWord();
      return leadingZeroCount + this.skipLeadingZeros();
    };
  
    // ():void
    this.skipUnsignedExpGolomb = function() {
      this.skipBits(1 + this.skipLeadingZeros());
    };
  
    // ():void
    this.skipExpGolomb = function() {
      this.skipBits(1 + this.skipLeadingZeros());
    };
  
    // ():uint
    this.readUnsignedExpGolomb = function() {
      var clz = this.skipLeadingZeros(); // :uint
      return this.readBits(clz + 1) - 1;
    };
  
    // ():int
    this.readExpGolomb = function() {
      var valu = this.readUnsignedExpGolomb(); // :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
      }
      return -1 * (valu >>> 1); // divide by two then make it negative
    };
  
    // Some convenience functions
    // :Boolean
    this.readBoolean = function() {
      return this.readBits(1) === 1;
    };
  
    // ():int
    this.readUnsignedByte = function() {
      return this.readBits(8);
    };
  
    this.loadWord();
  };
  
  module.exports = ExpGolomb;