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