PageRenderTime 132ms CodeModel.GetById 98ms app.highlight 26ms RepoModel.GetById 4ms app.codeStats 0ms

/Play/common.js

http://github.com/mbebenita/Broadway
JavaScript | 238 lines | 140 code | 28 blank | 70 comment | 20 complexity | f641ccb60db9267ed074c9ab6e9b0745 MD5 | raw file
  1var mapLev2Idx = new Uint8Array([
  2    255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 0, 1, 2, 3, 255, 255, 255, 255, 255,
  3    255, 4, 5, 6, 255, 255, 255, 255, 255, 255, 255, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 255, 255,
  4    255, 255, 255, 255, 255, 13, 14, 255, 255, 255, 255, 255, 255, 255, 255
  5]);
  6
  7/**
  8 * Zigzag scan from 1-D to 2-D.
  9 */
 10var ZZ_SCAN = new Uint8Array([
 11    0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
 12]);
 13
 14/* Zigzag scan from 1-D to 2-D output to block[24][16]. */
 15var ZZ_SCAN_BLOCK = new Uint8Array([
 16    0, 1, 16, 32, 17, 2, 3, 18, 33, 48, 49, 34, 19, 35, 50, 51
 17]);
 18
 19/**
 20 * From zigzag to raster for luma DC value
 21 */
 22var ZIGZAG2RASTERDC = new Uint8Array([
 23    0, 4, 64, 128, 68, 8, 12, 72, 132, 192, 196, 136, 76, 140, 200, 204
 24]);
 25
 26/**
 27 * Mapping from coding scan block indx to raster scan block index
 28 */
 29var blkIdx2blkX = new Int32Array([
 30    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
 31]);
 32
 33var blkIdx2blkY = new Int32Array([
 34    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
 35]);
 36
 37function convertTo2DUint8Array(array) {
 38    for (var i = 0; i < array.length; i++) {
 39        array[i] = new Uint8Array(array[i]);
 40    }
 41}
 42
 43function convertTo2DInt32Array(array) {
 44    for (var i = 0; i < array.length; i++) {
 45        array[i] = new Int32Array(array[i]);
 46    }
 47}
 48
 49/**
 50 * from [blk8indx][blk4indx] to raster scan index
 51 */
 52var blkIdx2blkXY = convertTo2DInt32Array([
 53    [0, 1, 4, 5], [2, 3, 6, 7], [8, 9, 12, 13], [10, 11, 14, 15]
 54]);
 55
 56/*
 57 * Availability of the neighboring top-right block relative to the current block.
 58 */
 59var BlkTopRight = new Int32Array([
 60    2, 2, 2, 3, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0
 61]);
 62
 63/**
 64 * Table 8-13 Specification of QPc as a function of qPI.
 65 */
 66var mapQPi2QPc = new Uint8Array([
 67    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
 68    24, 25, 26, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39
 69]);
 70
 71/**
 72 * See 8.5.5 equation (8-252 and 8-253) the definition of v matrix.
 73 * in zigzag scan
 74 */
 75var dequant_coefres = convertTo2DInt32Array([
 76    [10, 13, 13, 10, 16, 10, 13, 13, 13, 13, 16, 10, 16, 13, 13, 16],
 77    [11, 14, 14, 11, 18, 11, 14, 14, 14, 14, 18, 11, 18, 14, 14, 18],
 78    [13, 16, 16, 13, 20, 13, 16, 16, 16, 16, 20, 13, 20, 16, 16, 20],
 79    [14, 18, 18, 14, 23, 14, 18, 18, 18, 18, 23, 14, 23, 18, 18, 23],
 80    [16, 20, 20, 16, 25, 16, 20, 20, 20, 20, 25, 16, 25, 20, 20, 25],
 81    [18, 23, 23, 18, 29, 18, 23, 23, 23, 23, 29, 18, 29, 23, 23, 29]
 82]);
 83
 84/**
 85 * From jm7.6 block.c. (in zigzag scan)
 86 */
 87var quant_coef = convertTo2DInt32Array([
 88    [13107, 8066, 8066, 13107, 5243, 13107, 8066, 8066, 8066, 8066, 5243, 13107, 5243, 8066, 8066, 5243],
 89    [11916, 7490, 7490, 11916, 4660, 11916, 7490, 7490, 7490, 7490, 4660, 11916, 4660, 7490, 7490, 4660],
 90    [10082, 6554, 6554, 10082, 4194, 10082, 6554, 6554, 6554, 6554, 4194, 10082, 4194, 6554, 6554, 4194],
 91    [9362, 5825, 5825, 9362, 3647, 9362, 5825, 5825, 5825, 5825, 3647, 9362, 3647, 5825, 5825, 3647],
 92    [8192, 5243, 5243, 8192, 3355, 8192, 5243, 5243, 5243, 5243, 3355, 8192, 3355, 5243, 5243, 3355],
 93    [7282, 4559, 4559, 7282, 2893, 7282, 4559, 4559, 4559, 4559, 2893, 7282, 2893, 4559, 4559, 2893]
 94]);
 95
 96/**
 97 * Convert scan from raster scan order to block decoding order and from block decoding order to raster scan order. Same
 98 * table!!!
 99 */
100var ras2dec = new Uint8Array([
101    0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15
102]);
103
104/**
105 * mapping from level_idc to index map
106 */
107var mapLev2Idx = new Uint8Array([
108    255, 255, 255, 255, 255, 255, 255, 255, 255, 1, 0, 1, 2, 3, 255, 255, 255, 255, 255,
109    255, 4, 5, 6, 255, 255, 255, 255, 255, 255, 255, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 255, 255,
110    255, 255, 255, 255, 255, 13, 14, 255, 255, 255, 255, 255, 255, 255, 255
111]);
112
113/**
114 * map back from index to Level IDC
115 */
116var mapIdx2Lev = new Uint8Array([
117    10, 11, 12, 13, 20, 21, 22, 30, 31, 32, 40, 41, 42, 50, 51
118]);
119
120/**
121 * from the index map to the MaxDPB value times 2
122 */
123var MaxDPBX2 = new Int32Array([
124    297, 675, 1782, 1782, 1782, 3564, 6075, 6075, 13500, 15360, 24576, 24576, 24576, 82620, 138240
125]);
126
127/**
128 * map index to the max frame size
129 */
130var MaxFS = new Int32Array([
131    99, 396, 396, 396, 396, 792, 1620, 1620, 3600, 5120, 8192, 8192, 8192, 22080, 36864
132]);
133
134/**
135 * map index to max MB processing rate
136 */
137var MaxMBPS = new Int32Array([
138    1485, 3000, 6000, 11880, 11880, 19800, 20250, 40500, 108000, 216000, 245760, 245760, 491520, 589824, 983040
139]);
140
141/**
142 * map index to max video bit rate
143 */
144var MaxBR = new Int32Array([
145    64, 192, 384, 768, 2000, 4000, 4000, 10000, 14000, 20000, 20000, 50000, 50000, 135000, 240000
146]);
147
148/**
149 * map index to max CPB size
150 */
151var MaxCPB = new Int32Array([
152    175, 500, 1000, 2000, 2000, 4000, 4000, 10000, 14000, 20000, 25000, 62500, 62500, 135000, 240000
153]);
154
155/**
156 * map index to max vertical MV range
157 */
158var MaxVmvR = new Int32Array([
159    64, 128, 128, 128, 128, 256, 256, 256, 512, 512, 512, 512, 512, 512, 512
160]);
161
162const MAX_NUM_SLICE_GROUP = 8;
163
164const MAX_DEC_REF_PIC_MARKING = 64;
165
166/**
167 * Counts the number of leading zeros in a 32 bit number. This using a kind of
168 * binary search. At each level, if the upper half is zero then we count the
169 * number of bits in the upper half and shift the lower half of the bits in the
170 * upper half, otherwise we just split the upper half again and so on.
171 * 
172 * @param {Number} x A 32 bit number.
173 */
174function countLeadingZeros32(x) {
175    if (x == 0) {
176        return 32;
177    }
178    var n = 1;
179    if (x >>> 16 == 0) {
180        n += 16;
181        x <<= 16;
182    }
183    if (x >>> 24 == 0) {
184        n += 8;
185        x <<= 8;
186    }
187    if (x >>> 28 == 0) {
188        n += 4;
189        x <<= 4;
190    }
191    if (x >>> 30 == 0) {
192        n += 2;
193        x <<= 2;
194    }
195    n -= x >>> 31;
196    return n;
197}
198
199/**
200 * Counts the number of leading zeros in a 16 bit number.
201 * @see countLeadingZeros32
202 * @param {Number} x A 32 bit number.
203 */
204function countLeadingZeros16(x) {
205    if (x == 0) {
206        return 16;
207    }
208    var n = 1;
209    if (x >>> 8 == 0) {
210        n += 8;
211        x <<= 8;
212    }
213    if (x >>> 12 == 0) {
214        n += 4;
215        x <<= 4;
216    }
217    if (x >>> 14 == 0) {
218        n += 2;
219        x <<= 2;
220    }
221    n -= x >>> 15;
222    return n;
223}
224
225function max(x, y) {
226    return x > y ? x : y;
227}
228
229function min(x, y) {
230    return x < y ? x : y;
231}
232
233/**
234 * Clips x between a and b.
235 */
236function clip(a, b, x) {
237    return max(a, min(x, b));
238}