jsmpg.js 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. (function(window){ "use strict";
  2. // jsmpeg by Dominic Szablewski - phoboslab.org, github.com/phoboslab
  3. //
  4. // Consider this to be under MIT license. It's largely based an an Open Source
  5. // Decoder for Java under GPL, while I looked at another Decoder from Nokia
  6. // (under no particular license?) for certain aspects.
  7. // I'm not sure if this work is "derivative" enough to have a different license
  8. // but then again, who still cares about MPEG1?
  9. //
  10. // Based on "Java MPEG-1 Video Decoder and Player" by Korandi Zoltan:
  11. // http://sourceforge.net/projects/javampeg1video/
  12. //
  13. // Inspired by "MPEG Decoder in Java ME" by Nokia:
  14. // http://www.developer.nokia.com/Community/Wiki/MPEG_decoder_in_Java_ME
  15. var requestAnimFrame = (function(){
  16. return window.requestAnimationFrame ||
  17. window.webkitRequestAnimationFrame ||
  18. window.mozRequestAnimationFrame ||
  19. function( callback ){
  20. window.setTimeout(callback, 1000 / 60);
  21. };
  22. })();
  23. var jsmpeg = window.jsmpeg = function( url, opts ) {
  24. opts = opts || {};
  25. this.canvas = opts.canvas || document.createElement('canvas');
  26. this.autoplay = !!opts.autoplay;
  27. this.loop = !!opts.loop;
  28. this.externalLoadCallback = opts.onload || null;
  29. this.customIntraQuantMatrix = new Uint8Array(64);
  30. this.customNonIntraQuantMatrix = new Uint8Array(64);
  31. this.blockData = new Int32Array(64);
  32. this.canvasContext = this.canvas.getContext('2d');
  33. this.load(url);
  34. };
  35. jsmpeg.prototype.load = function( url ) {
  36. this.url = url;
  37. var request = new XMLHttpRequest();
  38. var that = this;
  39. request.onreadystatechange = function() {
  40. if( request.readyState == request.DONE && request.status == 200 ) {
  41. that.loadCallback(request.response);
  42. }
  43. };
  44. request.onprogress = this.updateLoader.bind(this);
  45. request.open('GET', url);
  46. request.responseType = "arraybuffer";
  47. request.send();
  48. };
  49. jsmpeg.prototype.updateLoader = function( ev ) {
  50. var
  51. p = ev.loaded / ev.total,
  52. w = this.canvas.width,
  53. h = this.canvas.height,
  54. ctx = this.canvasContext;
  55. ctx.fillStyle = '#222';
  56. ctx.fillRect(0, 0, w, h);
  57. ctx.fillStyle = '#fff';
  58. ctx.fillRect(0, h - h*p, w, h*p);
  59. };
  60. jsmpeg.prototype.loadCallback = function(file) {
  61. var time = Date.now();
  62. this.buffer = new BitReader(file);
  63. this.findStartCode(START_SEQUENCE);
  64. this.firstSequenceHeader = this.buffer.index;
  65. this.decodeSequenceHeader();
  66. // Load the first frame
  67. this.nextFrame();
  68. if( this.autoplay ) {
  69. this.play();
  70. }
  71. if( this.externalLoadCallback ) {
  72. this.externalLoadCallback(this);
  73. }
  74. };
  75. jsmpeg.prototype.play = function(file) {
  76. if( this.playing ) { return; }
  77. this.playing = true;
  78. this.scheduleNextFrame();
  79. };
  80. jsmpeg.prototype.pause = function(file) {
  81. this.playing = false;
  82. };
  83. jsmpeg.prototype.stop = function(file) {
  84. this.buffer.index = this.firstSequenceHeader;
  85. this.playing = false;
  86. };
  87. jsmpeg.prototype.readCode = function(codeTable) {
  88. var state = 0;
  89. do {
  90. state = codeTable[state + this.buffer.getBits(1)];
  91. } while( state >= 0 && codeTable[state] != 0 );
  92. return codeTable[state+2];
  93. };
  94. jsmpeg.prototype.findStartCode = function( code ) {
  95. var current = 0;
  96. while( true ) {
  97. current = this.buffer.findNextMPEGStartCode();
  98. if( current == code || current == BitReader.NOT_FOUND ) {
  99. return current;
  100. }
  101. }
  102. return BitReader.NOT_FOUND;
  103. };
  104. jsmpeg.prototype.fillArray = function(a, value) {
  105. for( var i = 0, length = a.length; i < length; i++ ) {
  106. a[i] = value;
  107. }
  108. };
  109. // ----------------------------------------------------------------------------
  110. // Sequence Layer
  111. jsmpeg.prototype.pictureRate = 30;
  112. jsmpeg.prototype.lateTime = 0;
  113. jsmpeg.prototype.firstSequenceHeader = 0;
  114. jsmpeg.prototype.targetTime = 0;
  115. jsmpeg.prototype.nextFrame = function() {
  116. while(true) {
  117. var code = this.buffer.findNextMPEGStartCode();
  118. if( code == START_SEQUENCE ) {
  119. this.decodeSequenceHeader();
  120. }
  121. else if( code == START_PICTURE ) {
  122. if( this.playing ) {
  123. this.scheduleNextFrame();
  124. }
  125. this.decodePicture();
  126. return this.canvas;
  127. }
  128. else if( code == BitReader.NOT_FOUND ) {
  129. this.stop(); // Jump back to the beginning
  130. // Only loop if we found a sequence header
  131. if( this.loop && this.sequenceStarted ) {
  132. this.play();
  133. }
  134. return null;
  135. }
  136. else {
  137. // ignore (GROUP, USER_DATA, EXTENSION, SLICES...)
  138. }
  139. }
  140. };
  141. jsmpeg.prototype.scheduleNextFrame = function() {
  142. var wait = (1000/this.pictureRate) - this.lateTime;
  143. this.targetTime = Date.now() + wait;
  144. if( wait < 18 ) {
  145. this.scheduleAnimation();
  146. }
  147. else {
  148. setTimeout( this.scheduleAnimation.bind(this), wait );
  149. }
  150. };
  151. jsmpeg.prototype.scheduleAnimation = function() {
  152. requestAnimFrame( this.nextFrame.bind(this), this.canvas );
  153. };
  154. jsmpeg.prototype.decodeSequenceHeader = function() {
  155. this.width = this.buffer.getBits(12);
  156. this.height = this.buffer.getBits(12);
  157. this.buffer.advance(4); // skip pixel aspect ratio
  158. this.pictureRate = PICTURE_RATE[this.buffer.getBits(4)];
  159. this.buffer.advance(18 + 1 + 10 + 1); // skip bitRate, marker, bufferSize and constrained bit
  160. this.intraQuantMatrix = DEFAULT_INTRA_QUANT_MATRIX;
  161. this.nonIntraQuantMatrix = DEFAULT_NON_INTRA_QUANT_MATRIX;
  162. this.mbWidth = (this.width + 15) >> 4;
  163. this.mbHeight = (this.height + 15) >> 4;
  164. this.mbSize = this.mbWidth * this.mbHeight;
  165. this.codedWidth = this.mbWidth << 4;
  166. this.codedHeight = this.mbHeight << 4;
  167. this.codedSize = this.codedWidth * this.codedHeight;
  168. this.halfWidth = this.mbWidth << 3;
  169. this.halfHeight = this.mbHeight << 3;
  170. this.quarterSize = this.codedSize >> 2;
  171. if( this.buffer.getBits(1) ) { // load custom intra quant matrix?
  172. for( var i = 0; i < 64; i++ ) {
  173. this.customIntraQuantMatrix[ZIG_ZAG[i]] = this.buffer.getBits(8);
  174. }
  175. this.intraQuantMatrix = this.customIntraQuantMatrix;
  176. }
  177. if( this.buffer.getBits(1) ) { // load custom non intra quant matrix?
  178. for( var i = 0; i < 64; i++ ) {
  179. this.customNonIntraQuantMatrix[ZIG_ZAG[i]] = this.buffer.getBits(8);
  180. }
  181. this.nonIntraQuantMatrix = this.customNonIntraQuantMatrix;
  182. }
  183. // Sequence already started? Don't allocate buffers again
  184. if( this.sequenceStarted ) { return; }
  185. this.sequenceStarted = true;
  186. // Allocated buffers and resize the canvas
  187. this.currentY = new Uint8ClampedArray(this.codedSize);
  188. this.currentY32 = new Uint32Array(this.currentY.buffer);
  189. this.currentCr = new Uint8ClampedArray(this.codedSize >> 2);
  190. this.currentCr32 = new Uint32Array(this.currentCr.buffer);
  191. this.currentCb = new Uint8ClampedArray(this.codedSize >> 2);
  192. this.currentCb32 = new Uint32Array(this.currentCb.buffer);
  193. this.forwardY = new Uint8ClampedArray(this.codedSize);
  194. this.forwardY32 = new Uint32Array(this.forwardY.buffer);
  195. this.forwardCr = new Uint8ClampedArray(this.codedSize >> 2);
  196. this.forwardCr32 = new Uint32Array(this.forwardCr.buffer);
  197. this.forwardCb = new Uint8ClampedArray(this.codedSize >> 2);
  198. this.forwardCb32 = new Uint32Array(this.forwardCb.buffer);
  199. this.canvas.width = this.width;
  200. this.canvas.height = this.height;
  201. this.currentRGBA = this.canvasContext.getImageData(0, 0, this.width, this.height);
  202. this.fillArray(this.currentRGBA.data, 255);
  203. };
  204. // ----------------------------------------------------------------------------
  205. // Picture Layer
  206. jsmpeg.prototype.currentY = null;
  207. jsmpeg.prototype.currentCr = null;
  208. jsmpeg.prototype.currentCb = null;
  209. jsmpeg.prototype.currentRGBA = null;
  210. jsmpeg.prototype.pictureCodingType = 0;
  211. // Buffers for motion compensation
  212. jsmpeg.prototype.forwardY = null;
  213. jsmpeg.prototype.forwardCr = null;
  214. jsmpeg.prototype.forwardCb = null;
  215. jsmpeg.prototype.fullPelForward = false;
  216. jsmpeg.prototype.forwardFCode = 0;
  217. jsmpeg.prototype.forwardRSize = 0;
  218. jsmpeg.prototype.forwardF = 0;
  219. jsmpeg.prototype.decodePicture = function() {
  220. this.buffer.advance(10); // skip temporalReference
  221. this.pictureCodingType = this.buffer.getBits(3);
  222. this.buffer.advance(16); // skip vbv_delay
  223. // Skip B and D frames or unknown coding type
  224. if( this.pictureCodingType <= 0 || this.pictureCodingType >= PICTURE_TYPE_B ) {
  225. return;
  226. }
  227. // full_pel_forward, forward_f_code
  228. if( this.pictureCodingType == PICTURE_TYPE_P ) {
  229. this.fullPelForward = this.buffer.getBits(1);
  230. this.forwardFCode = this.buffer.getBits(3);
  231. if( this.forwardFCode == 0 ) {
  232. // Ignore picture with zero forward_f_code
  233. return;
  234. }
  235. this.forwardRSize = this.forwardFCode - 1;
  236. this.forwardF = 1 << this.forwardRSize;
  237. }
  238. var code = 0;
  239. do {
  240. code = this.buffer.findNextMPEGStartCode();
  241. } while( code == START_EXTENSION || code == START_USER_DATA );
  242. while( code >= START_SLICE_FIRST && code <= START_SLICE_LAST ) {
  243. this.decodeSlice( (code & 0x000000FF) );
  244. code = this.buffer.findNextMPEGStartCode();
  245. }
  246. // We found the next start code; rewind 32bits and let the main loop handle it.
  247. this.buffer.rewind(32);
  248. this.YCbCrToRGBA();
  249. this.canvasContext.putImageData(this.currentRGBA, 0, 0);
  250. // If this is a reference picutre then rotate the prediction pointers
  251. if( this.pictureCodingType == PICTURE_TYPE_I || this.pictureCodingType == PICTURE_TYPE_P ) {
  252. var
  253. tmpY = this.forwardY,
  254. tmpY32 = this.forwardY32,
  255. tmpCr = this.forwardCr,
  256. tmpCr32 = this.forwardCr32,
  257. tmpCb = this.forwardCb,
  258. tmpCb32 = this.forwardCb32;
  259. this.forwardY = this.currentY;
  260. this.forwardY32 = this.currentY32;
  261. this.forwardCr = this.currentCr;
  262. this.forwardCr32 = this.currentCr32;
  263. this.forwardCb = this.currentCb;
  264. this.forwardCb32 = this.currentCb32;
  265. this.currentY = tmpY;
  266. this.currentY32 = tmpY32;
  267. this.currentCr = tmpCr;
  268. this.currentCr32 = tmpCr32;
  269. this.currentCb = tmpCb;
  270. this.currentCb32 = tmpCb32;
  271. }
  272. };
  273. jsmpeg.prototype.YCbCrToRGBA = function() {
  274. var pY = this.currentY;
  275. var pCb = this.currentCb;
  276. var pCr = this.currentCr;
  277. var pRGBA = this.currentRGBA.data;
  278. // Chroma values are the same for each block of 4 pixels, so we proccess
  279. // 2 lines at a time, 2 neighboring pixels each.
  280. var yIndex1 = 0;
  281. var yIndex2 = this.codedWidth;
  282. var yNext2Lines = this.codedWidth + (this.codedWidth - this.width);
  283. var cIndex = 0;
  284. var cNextLine = this.halfWidth - (this.width >> 1);
  285. var rgbaIndex1 = 0;
  286. var rgbaIndex2 = this.width * 4;
  287. var rgbaNext2Lines = this.width * 4;
  288. var cols = this.width >> 1;
  289. var rows = this.height >> 1;
  290. var y, cb, cr, r, g, b;
  291. for( var row = 0; row < rows; row++ ) {
  292. for( var col = 0; col < cols; col++ ) {
  293. cb = pCb[cIndex];
  294. cr = pCr[cIndex];
  295. cIndex++;
  296. r = (cr + ((cr * 103) >> 8)) - 179;
  297. g = ((cb * 88) >> 8) - 44 + ((cr * 183) >> 8) - 91;
  298. b = (cb + ((cb * 198) >> 8)) - 227;
  299. // Line 1
  300. y = pY[yIndex1++];
  301. pRGBA[rgbaIndex1] = y + r;
  302. pRGBA[rgbaIndex1+1] = y - g;
  303. pRGBA[rgbaIndex1+2] = y + b;
  304. rgbaIndex1 += 4;
  305. y = pY[yIndex1++];
  306. pRGBA[rgbaIndex1] = y + r;
  307. pRGBA[rgbaIndex1+1] = y - g;
  308. pRGBA[rgbaIndex1+2] = y + b;
  309. rgbaIndex1 += 4;
  310. // Line 2
  311. y = pY[yIndex2++];
  312. pRGBA[rgbaIndex2] = y + r;
  313. pRGBA[rgbaIndex2+1] = y - g;
  314. pRGBA[rgbaIndex2+2] = y + b;
  315. rgbaIndex2 += 4;
  316. y = pY[yIndex2++];
  317. pRGBA[rgbaIndex2] = y + r;
  318. pRGBA[rgbaIndex2+1] = y - g;
  319. pRGBA[rgbaIndex2+2] = y + b;
  320. rgbaIndex2 += 4;
  321. }
  322. yIndex1 += yNext2Lines;
  323. yIndex2 += yNext2Lines;
  324. rgbaIndex1 += rgbaNext2Lines;
  325. rgbaIndex2 += rgbaNext2Lines;
  326. cIndex += cNextLine;
  327. }
  328. };
  329. // ----------------------------------------------------------------------------
  330. // Slice Layer
  331. jsmpeg.prototype.quantizerScale = 0;
  332. jsmpeg.prototype.sliceBegin = false;
  333. jsmpeg.prototype.decodeSlice = function(slice) {
  334. this.sliceBegin = true;
  335. this.macroblockAddress = (slice - 1) * this.mbWidth - 1;
  336. // Reset motion vectors and DC predictors
  337. this.motionFwH = this.motionFwHPrev = 0;
  338. this.motionFwV = this.motionFwVPrev = 0;
  339. this.dcPredictorY = 128;
  340. this.dcPredictorCr = 128;
  341. this.dcPredictorCb = 128;
  342. this.quantizerScale = this.buffer.getBits(5);
  343. // skip extra bits
  344. while( this.buffer.getBits(1) ) {
  345. this.buffer.advance(8);
  346. }
  347. do {
  348. this.decodeMacroblock();
  349. // We may have to ignore Video Stream Start Codes here (0xE0)!?
  350. } while( !this.buffer.nextBytesAreStartCode() );
  351. }
  352. // ----------------------------------------------------------------------------
  353. // Macroblock Layer
  354. jsmpeg.prototype.macroblockAddress = 0;
  355. jsmpeg.prototype.mbRow = 0;
  356. jsmpeg.prototype.mbCol = 0;
  357. jsmpeg.prototype.macroblockType = 0;
  358. jsmpeg.prototype.macroblockIntra = false;
  359. jsmpeg.prototype.macroblockMotFw = false;
  360. jsmpeg.prototype.motionFwH = 0;
  361. jsmpeg.prototype.motionFwV = 0;
  362. jsmpeg.prototype.motionFwHPrev = 0;
  363. jsmpeg.prototype.motionFwVPrev = 0;
  364. jsmpeg.prototype.decodeMacroblock = function() {
  365. // Decode macroblock_address_increment
  366. var
  367. increment = 0,
  368. t = this.readCode(MACROBLOCK_ADDRESS_INCREMENT);
  369. while( t == 34 ) {
  370. // macroblock_stuffing
  371. t = this.readCode(MACROBLOCK_ADDRESS_INCREMENT);
  372. }
  373. while( t == 35 ) {
  374. // macroblock_escape
  375. increment += 33;
  376. t = this.readCode(MACROBLOCK_ADDRESS_INCREMENT);
  377. }
  378. increment += t;
  379. // Process any skipped macroblocks
  380. if( this.sliceBegin ) {
  381. // The first macroblock_address_increment of each slice is relative
  382. // to beginning of the preverious row, not the preverious macroblock
  383. this.sliceBegin = false;
  384. this.macroblockAddress += increment;
  385. }
  386. else {
  387. if( this.macroblockAddress + increment >= this.mbSize ) {
  388. // Illegal (too large) macroblock_address_increment
  389. return;
  390. }
  391. if( increment > 1 ) {
  392. // Skipped macroblocks reset DC predictors
  393. this.dcPredictorY = 128;
  394. this.dcPredictorCr = 128;
  395. this.dcPredictorCb = 128;
  396. // Skipped macroblocks in P-pictures reset motion vectors
  397. if( this.pictureCodingType == PICTURE_TYPE_P ) {
  398. this.motionFwH = this.motionFwHPrev = 0;
  399. this.motionFwV = this.motionFwVPrev = 0;
  400. }
  401. }
  402. // Predict skipped macroblocks
  403. while( increment > 1) {
  404. this.macroblockAddress++;
  405. this.mbRow = (this.macroblockAddress / this.mbWidth)|0;
  406. this.mbCol = this.macroblockAddress % this.mbWidth;
  407. this.copyMacroblock(this.motionFwH, this.motionFwV, this.forwardY, this.forwardCr, this.forwardCb);
  408. increment--;
  409. }
  410. this.macroblockAddress++;
  411. }
  412. this.mbRow = (this.macroblockAddress / this.mbWidth)|0;
  413. this.mbCol = this.macroblockAddress % this.mbWidth;
  414. // Process the current macroblock
  415. this.macroblockType = this.readCode(MACROBLOCK_TYPE_TABLES[this.pictureCodingType]);
  416. this.macroblockIntra = (this.macroblockType & 0x01);
  417. this.macroblockMotFw = (this.macroblockType & 0x08);
  418. // Quantizer scale
  419. if( (this.macroblockType & 0x10) != 0 ) {
  420. this.quantizerScale = this.buffer.getBits(5);
  421. }
  422. if( this.macroblockIntra ) {
  423. // Intra-coded macroblocks reset motion vectors
  424. this.motionFwH = this.motionFwHPrev = 0;
  425. this.motionFwV = this.motionFwVPrev = 0;
  426. }
  427. else {
  428. // Non-intra macroblocks reset DC predictors
  429. this.dcPredictorY = 128;
  430. this.dcPredictorCr = 128;
  431. this.dcPredictorCb = 128;
  432. this.decodeMotionVectors();
  433. this.copyMacroblock(this.motionFwH, this.motionFwV, this.forwardY, this.forwardCr, this.forwardCb);
  434. }
  435. // Decode blocks
  436. var cbp = ((this.macroblockType & 0x02) != 0)
  437. ? this.readCode(CODE_BLOCK_PATTERN)
  438. : (this.macroblockIntra ? 0x3f : 0);
  439. for( var block = 0, mask = 0x20; block < 6; block++ ) {
  440. if( (cbp & mask) != 0 ) {
  441. this.decodeBlock(block);
  442. }
  443. mask >>= 1;
  444. }
  445. };
  446. jsmpeg.prototype.decodeMotionVectors = function() {
  447. var code, d, r = 0;
  448. // Forward
  449. if( this.macroblockMotFw ) {
  450. // Horizontal forward
  451. code = this.readCode(MOTION);
  452. if( (code != 0) && (this.forwardF != 1) ) {
  453. r = this.buffer.getBits(this.forwardRSize);
  454. d = ((Math.abs(code) - 1) << this.forwardRSize) + r + 1;
  455. if( code < 0 ) {
  456. d = -d;
  457. }
  458. }
  459. else {
  460. d = code;
  461. }
  462. this.motionFwHPrev += d;
  463. if( this.motionFwHPrev > (this.forwardF << 4) - 1 ) {
  464. this.motionFwHPrev -= this.forwardF << 5;
  465. }
  466. else if( this.motionFwHPrev < ((-this.forwardF) << 4) ) {
  467. this.motionFwHPrev += this.forwardF << 5;
  468. }
  469. this.motionFwH = this.motionFwHPrev;
  470. if( this.fullPelForward ) {
  471. this.motionFwH <<= 1;
  472. }
  473. // Vertical forward
  474. code = this.readCode(MOTION);
  475. if( (code != 0) && (this.forwardF != 1) ) {
  476. r = this.buffer.getBits(this.forwardRSize);
  477. d = ((Math.abs(code) - 1) << this.forwardRSize) + r + 1;
  478. if( code < 0 ) {
  479. d = -d;
  480. }
  481. }
  482. else {
  483. d = code;
  484. }
  485. this.motionFwVPrev += d;
  486. if( this.motionFwVPrev > (this.forwardF << 4) - 1 ) {
  487. this.motionFwVPrev -= this.forwardF << 5;
  488. }
  489. else if( this.motionFwVPrev < ((-this.forwardF) << 4) ) {
  490. this.motionFwVPrev += this.forwardF << 5;
  491. }
  492. this.motionFwV = this.motionFwVPrev;
  493. if( this.fullPelForward ) {
  494. this.motionFwV <<= 1;
  495. }
  496. }
  497. else if( this.pictureCodingType == PICTURE_TYPE_P ) {
  498. // No motion information in P-picture, reset vectors
  499. this.motionFwH = this.motionFwHPrev = 0;
  500. this.motionFwV = this.motionFwVPrev = 0;
  501. }
  502. };
  503. jsmpeg.prototype.copyMacroblock = function(motionH, motionV, sY, sCr, sCb ) {
  504. var
  505. width, scan,
  506. H, V, oddH, oddV,
  507. src, dest, last;
  508. var dY = this.currentY32;
  509. var dCb = this.currentCb32;
  510. var dCr = this.currentCr32;
  511. // Luminance
  512. width = this.codedWidth;
  513. scan = width - 16;
  514. H = motionH >> 1;
  515. V = motionV >> 1;
  516. oddH = (motionH & 1) == 1;
  517. oddV = (motionV & 1) == 1;
  518. src = ((this.mbRow << 4) + V) * width + (this.mbCol << 4) + H;
  519. dest = (this.mbRow * width + this.mbCol) << 2;
  520. last = dest + (width << 2);
  521. var y11, y21, y12, y22, y;
  522. if( oddH ) {
  523. if( oddV ) {
  524. while( dest < last ) {
  525. y21 = sY[src]; y22 = sY[src+width]; src++;
  526. for( var x = 0; x < 4; x++ ) {
  527. y11 = y21; y12 = y22; y21 = sY[src]; y22 = sY[src+width]; src++;
  528. y = (((y11 + y21 + y12 + y22 + 2) >> 2) & 0xff);
  529. y11 = y21; y12 = y22; y21 = sY[src]; y22 = sY[src+width]; src++;
  530. y |= (((y11 + y21 + y12 + y22 + 2) << 6) & 0xff00);
  531. y11 = y21; y12 = y22; y21 = sY[src]; y22 = sY[src+width]; src++;
  532. y |= (((y11 + y21 + y12 + y22 + 2) << 14) & 0xff0000);
  533. y11 = y21; y12 = y22; y21 = sY[src]; y22 = sY[src+width]; src++;
  534. y |= (((y11 + y21 + y12 + y22 + 2) << 22) & 0xff000000);
  535. dY[dest++] = y;
  536. }
  537. dest += scan >> 2; src += scan-1;
  538. }
  539. }
  540. else {
  541. while( dest < last ) {
  542. y21 = sY[src]; src++;
  543. for( var x = 0; x < 4; x++ ) {
  544. y11 = y21; y21 = sY[src]; src++;
  545. y = (((y11 + y21 + 1) >> 1) & 0xff);
  546. y11 = y21; y21 = sY[src]; src++;
  547. y |= (((y11 + y21 + 1) << 7) & 0xff00);
  548. y11 = y21; y21 = sY[src]; src++;
  549. y |= (((y11 + y21 + 1) << 15) & 0xff0000);
  550. y11 = y21; y21 = sY[src]; src++;
  551. y |= (((y11 + y21 + 1) << 23) & 0xff000000);
  552. dY[dest++] = y;
  553. }
  554. dest += scan >> 2; src += scan-1;
  555. }
  556. }
  557. }
  558. else {
  559. if( oddV ) {
  560. while( dest < last ) {
  561. for( var x = 0; x < 4; x++ ) {
  562. y = (((sY[src] + sY[src+width] + 1) >> 1) & 0xff); src++;
  563. y |= (((sY[src] + sY[src+width] + 1) << 7) & 0xff00); src++;
  564. y |= (((sY[src] + sY[src+width] + 1) << 15) & 0xff0000); src++;
  565. y |= (((sY[src] + sY[src+width] + 1) << 23) & 0xff000000); src++;
  566. dY[dest++] = y;
  567. }
  568. dest += scan >> 2; src += scan;
  569. }
  570. }
  571. else {
  572. while( dest < last ) {
  573. for( var x = 0; x < 4; x++ ) {
  574. y = sY[src]; src++;
  575. y |= sY[src] << 8; src++;
  576. y |= sY[src] << 16; src++;
  577. y |= sY[src] << 24; src++;
  578. dY[dest++] = y;
  579. }
  580. dest += scan >> 2; src += scan;
  581. }
  582. }
  583. }
  584. // Chrominance
  585. width = this.halfWidth;
  586. scan = width - 8;
  587. H = (motionH/2) >> 1;
  588. V = (motionV/2) >> 1;
  589. oddH = ((motionH/2) & 1) == 1;
  590. oddV = ((motionV/2) & 1) == 1;
  591. src = ((this.mbRow << 3) + V) * width + (this.mbCol << 3) + H;
  592. dest = (this.mbRow * width + this.mbCol) << 1;
  593. last = dest + (width << 1);
  594. var cr11, cr21, cr12, cr22, cr;
  595. var cb11, cb21, cb12, cb22, cb;
  596. if( oddH ) {
  597. if( oddV ) {
  598. while( dest < last ) {
  599. cr21 = sCr[src]; cr22 = sCr[src+width];
  600. cb21 = sCb[src]; cb22 = sCb[src+width];
  601. src++;
  602. for( var x = 0; x < 2; x++ ) {
  603. cr11 = cr21; cr12 = cr22; cr21 = sCr[src]; cr22 = sCr[src+width];
  604. cb11 = cb21; cb12 = cb22; cb21 = sCb[src]; cb22 = sCb[src+width]; src++;
  605. cr = (((cr11 + cr21 + cr12 + cr22 + 2) >> 2) & 0xff);
  606. cb = (((cb11 + cb21 + cb12 + cb22 + 2) >> 2) & 0xff);
  607. cr11 = cr21; cr12 = cr22; cr21 = sCr[src]; cr22 = sCr[src+width];
  608. cb11 = cb21; cb12 = cb22; cb21 = sCb[src]; cb22 = sCb[src+width]; src++;
  609. cr |= (((cr11 + cr21 + cr12 + cr22 + 2) << 6) & 0xff00);
  610. cb |= (((cb11 + cb21 + cb12 + cb22 + 2) << 6) & 0xff00);
  611. cr11 = cr21; cr12 = cr22; cr21 = sCr[src]; cr22 = sCr[src+width];
  612. cb11 = cb21; cb12 = cb22; cb21 = sCb[src]; cb22 = sCb[src+width]; src++;
  613. cr |= (((cr11 + cr21 + cr12 + cr22 + 2) << 14) & 0xff0000);
  614. cb |= (((cb11 + cb21 + cb12 + cb22 + 2) << 14) & 0xff0000);
  615. cr11 = cr21; cr12 = cr22; cr21 = sCr[src]; cr22 = sCr[src+width];
  616. cb11 = cb21; cb12 = cb22; cb21 = sCb[src]; cb22 = sCb[src+width]; src++;
  617. cr |= (((cr11 + cr21 + cr12 + cr22 + 2) << 22) & 0xff000000);
  618. cb |= (((cb11 + cb21 + cb12 + cb22 + 2) << 22) & 0xff000000);
  619. dCr[dest] = cr;
  620. dCb[dest] = cb;
  621. dest++;
  622. }
  623. dest += scan >> 2; src += scan-1;
  624. }
  625. }
  626. else {
  627. while( dest < last ) {
  628. cr21 = sCr[src];
  629. cb21 = sCb[src];
  630. src++;
  631. for( var x = 0; x < 2; x++ ) {
  632. cr11 = cr21; cr21 = sCr[src];
  633. cb11 = cb21; cb21 = sCb[src]; src++;
  634. cr = (((cr11 + cr21 + 1) >> 1) & 0xff);
  635. cb = (((cb11 + cb21 + 1) >> 1) & 0xff);
  636. cr11 = cr21; cr21 = sCr[src];
  637. cb11 = cb21; cb21 = sCb[src]; src++;
  638. cr |= (((cr11 + cr21 + 1) << 7) & 0xff00);
  639. cb |= (((cb11 + cb21 + 1) << 7) & 0xff00);
  640. cr11 = cr21; cr21 = sCr[src];
  641. cb11 = cb21; cb21 = sCb[src]; src++;
  642. cr |= (((cr11 + cr21 + 1) << 15) & 0xff0000);
  643. cb |= (((cb11 + cb21 + 1) << 15) & 0xff0000);
  644. cr11 = cr21; cr21 = sCr[src];
  645. cb11 = cb21; cb21 = sCb[src]; src++;
  646. cr |= (((cr11 + cr21 + 1) << 23) & 0xff000000);
  647. cb |= (((cb11 + cb21 + 1) << 23) & 0xff000000);
  648. dCr[dest] = cr;
  649. dCb[dest] = cb;
  650. dest++;
  651. }
  652. dest += scan >> 2; src += scan-1;
  653. }
  654. }
  655. }
  656. else {
  657. if( oddV ) {
  658. while( dest < last ) {
  659. for( var x = 0; x < 2; x++ ) {
  660. cr = (((sCr[src] + sCr[src+width] + 1) >> 1) & 0xff);
  661. cb = (((sCb[src] + sCb[src+width] + 1) >> 1) & 0xff); src++;
  662. cr |= (((sCr[src] + sCr[src+width] + 1) << 7) & 0xff00);
  663. cb |= (((sCb[src] + sCb[src+width] + 1) << 7) & 0xff00); src++;
  664. cr |= (((sCr[src] + sCr[src+width] + 1) << 15) & 0xff0000);
  665. cb |= (((sCb[src] + sCb[src+width] + 1) << 15) & 0xff0000); src++;
  666. cr |= (((sCr[src] + sCr[src+width] + 1) << 23) & 0xff000000);
  667. cb |= (((sCb[src] + sCb[src+width] + 1) << 23) & 0xff000000); src++;
  668. dCr[dest] = cr;
  669. dCb[dest] = cb;
  670. dest++;
  671. }
  672. dest += scan >> 2; src += scan;
  673. }
  674. }
  675. else {
  676. while( dest < last ) {
  677. for( var x = 0; x < 2; x++ ) {
  678. cr = sCr[src];
  679. cb = sCb[src]; src++;
  680. cr |= sCr[src] << 8;
  681. cb |= sCb[src] << 8; src++;
  682. cr |= sCr[src] << 16;
  683. cb |= sCb[src] << 16; src++;
  684. cr |= sCr[src] << 24;
  685. cb |= sCb[src] << 24; src++;
  686. dCr[dest] = cr;
  687. dCb[dest] = cb;
  688. dest++;
  689. }
  690. dest += scan >> 2; src += scan;
  691. }
  692. }
  693. }
  694. };
  695. // ----------------------------------------------------------------------------
  696. // Block layer
  697. jsmpeg.prototype.dcPredictorY;
  698. jsmpeg.prototype.dcPredictorCr;
  699. jsmpeg.prototype.dcPredictorCb;
  700. jsmpeg.prototype.blockData = null;
  701. jsmpeg.prototype.decodeBlock = function(block) {
  702. var
  703. n = 0,
  704. quantMatrix;
  705. // Clear preverious data
  706. this.fillArray(this.blockData, 0);
  707. // Decode DC coefficient of intra-coded blocks
  708. if( this.macroblockIntra ) {
  709. var
  710. predictor,
  711. dctSize;
  712. // DC prediction
  713. if( block < 4 ) {
  714. predictor = this.dcPredictorY;
  715. dctSize = this.readCode(DCT_DC_SIZE_LUMINANCE);
  716. }
  717. else {
  718. predictor = (block == 4 ? this.dcPredictorCr : this.dcPredictorCb);
  719. dctSize = this.readCode(DCT_DC_SIZE_CHROMINANCE);
  720. }
  721. // Read DC coeff
  722. if( dctSize > 0 ) {
  723. var differential = this.buffer.getBits(dctSize);
  724. if( (differential & (1 << (dctSize - 1))) != 0 ) {
  725. this.blockData[0] = predictor + differential;
  726. }
  727. else {
  728. this.blockData[0] = predictor + ((-1 << dctSize)|(differential+1));
  729. }
  730. }
  731. else {
  732. this.blockData[0] = predictor;
  733. }
  734. // Save predictor value
  735. if( block < 4 ) {
  736. this.dcPredictorY = this.blockData[0];
  737. }
  738. else if( block == 4 ) {
  739. this.dcPredictorCr = this.blockData[0];
  740. }
  741. else {
  742. this.dcPredictorCb = this.blockData[0];
  743. }
  744. // Dequantize + premultiply
  745. this.blockData[0] <<= (3 + 5);
  746. quantMatrix = this.intraQuantMatrix;
  747. n = 1;
  748. }
  749. else {
  750. quantMatrix = this.nonIntraQuantMatrix;
  751. }
  752. // Decode AC coefficients (+DC for non-intra)
  753. var level = 0;
  754. while( true ) {
  755. var
  756. run = 0,
  757. coeff = this.readCode(DCT_COEFF);
  758. if( (coeff == 0x0001) && (n > 0) && (this.buffer.getBits(1) == 0) ) {
  759. // end_of_block
  760. break;
  761. }
  762. if( coeff == 0xffff ) {
  763. // escape
  764. run = this.buffer.getBits(6);
  765. level = this.buffer.getBits(8);
  766. if( level == 0 ) {
  767. level = this.buffer.getBits(8);
  768. }
  769. else if( level == 128 ) {
  770. level = this.buffer.getBits(8) - 256;
  771. }
  772. else if( level > 128 ) {
  773. level = level - 256;
  774. }
  775. }
  776. else {
  777. run = coeff >> 8;
  778. level = coeff & 0xff;
  779. if( this.buffer.getBits(1) ) {
  780. level = -level;
  781. }
  782. }
  783. n += run;
  784. var dezigZagged = ZIG_ZAG[n];
  785. n++;
  786. // Dequantize, oddify, clip
  787. level <<= 1;
  788. if( !this.macroblockIntra ) {
  789. level += (level < 0 ? -1 : 1);
  790. }
  791. level = (level * this.quantizerScale * quantMatrix[dezigZagged]) >> 4;
  792. if( (level & 1) == 0 ) {
  793. level -= level > 0 ? 1 : -1;
  794. }
  795. if( level > 2047 ) {
  796. level = 2047;
  797. }
  798. else if( level < -2048 ) {
  799. level = -2048;
  800. }
  801. // Save premultiplied coefficient
  802. this.blockData[dezigZagged] = level * PREMULTIPLIER_MATRIX[dezigZagged];
  803. };
  804. // Transform block data to the spatial domain
  805. if( n == 1 ) {
  806. // Only DC coeff., no IDCT needed
  807. this.fillArray(this.blockData, (this.blockData[0] + 128) >> 8);
  808. }
  809. else {
  810. this.IDCT();
  811. }
  812. // Move block to its place
  813. var
  814. destArray,
  815. destIndex,
  816. scan;
  817. if( block < 4 ) {
  818. destArray = this.currentY;
  819. scan = this.codedWidth - 8;
  820. destIndex = (this.mbRow * this.codedWidth + this.mbCol) << 4;
  821. if( (block & 1) != 0 ) {
  822. destIndex += 8;
  823. }
  824. if( (block & 2) != 0 ) {
  825. destIndex += this.codedWidth << 3;
  826. }
  827. }
  828. else {
  829. destArray = (block == 4) ? this.currentCb : this.currentCr;
  830. scan = (this.codedWidth >> 1) - 8;
  831. destIndex = ((this.mbRow * this.codedWidth) << 2) + (this.mbCol << 3);
  832. }
  833. n = 0;
  834. var blockData = this.blockData;
  835. if( this.macroblockIntra ) {
  836. // Overwrite (no prediction)
  837. for( var i = 0; i < 8; i++ ) {
  838. for( var j = 0; j < 8; j++ ) {
  839. destArray[destIndex++] = blockData[n++];
  840. }
  841. destIndex += scan;
  842. }
  843. }
  844. else {
  845. // Add data to the predicted macroblock
  846. for( var i = 0; i < 8; i++ ) {
  847. for( var j = 0; j < 8; j++ ) {
  848. destArray[destIndex++] += blockData[n++];
  849. }
  850. destIndex += scan;
  851. }
  852. }
  853. };
  854. jsmpeg.prototype.IDCT = function() {
  855. // See http://vsr.informatik.tu-chemnitz.de/~jan/MPEG/HTML/IDCT.html
  856. // for more info.
  857. var
  858. b1, b3, b4, b6, b7, tmp1, tmp2, m0,
  859. x0, x1, x2, x3, x4, y3, y4, y5, y6, y7,
  860. i,
  861. blockData = this.blockData;
  862. // Transform columns
  863. for( i = 0; i < 8; ++i ) {
  864. b1 = blockData[4*8+i];
  865. b3 = blockData[2*8+i] + blockData[6*8+i];
  866. b4 = blockData[5*8+i] - blockData[3*8+i];
  867. tmp1 = blockData[1*8+i] + blockData[7*8+i];
  868. tmp2 = blockData[3*8+i] + blockData[5*8+i];
  869. b6 = blockData[1*8+i] - blockData[7*8+i];
  870. b7 = tmp1 + tmp2;
  871. m0 = blockData[0*8+i];
  872. x4 = ((b6*473 - b4*196 + 128) >> 8) - b7;
  873. x0 = x4 - (((tmp1 - tmp2)*362 + 128) >> 8);
  874. x1 = m0 - b1;
  875. x2 = (((blockData[2*8+i] - blockData[6*8+i])*362 + 128) >> 8) - b3;
  876. x3 = m0 + b1;
  877. y3 = x1 + x2;
  878. y4 = x3 + b3;
  879. y5 = x1 - x2;
  880. y6 = x3 - b3;
  881. y7 = -x0 - ((b4*473 + b6*196 + 128) >> 8);
  882. blockData[0*8+i] = b7 + y4;
  883. blockData[1*8+i] = x4 + y3;
  884. blockData[2*8+i] = y5 - x0;
  885. blockData[3*8+i] = y6 - y7;
  886. blockData[4*8+i] = y6 + y7;
  887. blockData[5*8+i] = x0 + y5;
  888. blockData[6*8+i] = y3 - x4;
  889. blockData[7*8+i] = y4 - b7;
  890. }
  891. // Transform rows
  892. for( i = 0; i < 64; i += 8 ) {
  893. b1 = blockData[4+i];
  894. b3 = blockData[2+i] + blockData[6+i];
  895. b4 = blockData[5+i] - blockData[3+i];
  896. tmp1 = blockData[1+i] + blockData[7+i];
  897. tmp2 = blockData[3+i] + blockData[5+i];
  898. b6 = blockData[1+i] - blockData[7+i];
  899. b7 = tmp1 + tmp2;
  900. m0 = blockData[0+i];
  901. x4 = ((b6*473 - b4*196 + 128) >> 8) - b7;
  902. x0 = x4 - (((tmp1 - tmp2)*362 + 128) >> 8);
  903. x1 = m0 - b1;
  904. x2 = (((blockData[2+i] - blockData[6+i])*362 + 128) >> 8) - b3;
  905. x3 = m0 + b1;
  906. y3 = x1 + x2;
  907. y4 = x3 + b3;
  908. y5 = x1 - x2;
  909. y6 = x3 - b3;
  910. y7 = -x0 - ((b4*473 + b6*196 + 128) >> 8);
  911. blockData[0+i] = (b7 + y4 + 128) >> 8;
  912. blockData[1+i] = (x4 + y3 + 128) >> 8;
  913. blockData[2+i] = (y5 - x0 + 128) >> 8;
  914. blockData[3+i] = (y6 - y7 + 128) >> 8;
  915. blockData[4+i] = (y6 + y7 + 128) >> 8;
  916. blockData[5+i] = (x0 + y5 + 128) >> 8;
  917. blockData[6+i] = (y3 - x4 + 128) >> 8;
  918. blockData[7+i] = (y4 - b7 + 128) >> 8;
  919. }
  920. };
  921. // ----------------------------------------------------------------------------
  922. // VLC Tables and Constants
  923. var
  924. PICTURE_RATE = [
  925. 0.000, 23.976, 24.000, 25.000, 29.970, 30.000, 50.000, 59.940,
  926. 60.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
  927. ],
  928. ZIG_ZAG = new Uint8Array([
  929. 0, 1, 8, 16, 9, 2, 3, 10,
  930. 17, 24, 32, 25, 18, 11, 4, 5,
  931. 12, 19, 26, 33, 40, 48, 41, 34,
  932. 27, 20, 13, 6, 7, 14, 21, 28,
  933. 35, 42, 49, 56, 57, 50, 43, 36,
  934. 29, 22, 15, 23, 30, 37, 44, 51,
  935. 58, 59, 52, 45, 38, 31, 39, 46,
  936. 53, 60, 61, 54, 47, 55, 62, 63
  937. ]),
  938. DEFAULT_INTRA_QUANT_MATRIX = new Uint8Array([
  939. 8, 16, 19, 22, 26, 27, 29, 34,
  940. 16, 16, 22, 24, 27, 29, 34, 37,
  941. 19, 22, 26, 27, 29, 34, 34, 38,
  942. 22, 22, 26, 27, 29, 34, 37, 40,
  943. 22, 26, 27, 29, 32, 35, 40, 48,
  944. 26, 27, 29, 32, 35, 40, 48, 58,
  945. 26, 27, 29, 34, 38, 46, 56, 69,
  946. 27, 29, 35, 38, 46, 56, 69, 83
  947. ]),
  948. DEFAULT_NON_INTRA_QUANT_MATRIX = new Uint8Array([
  949. 16, 16, 16, 16, 16, 16, 16, 16,
  950. 16, 16, 16, 16, 16, 16, 16, 16,
  951. 16, 16, 16, 16, 16, 16, 16, 16,
  952. 16, 16, 16, 16, 16, 16, 16, 16,
  953. 16, 16, 16, 16, 16, 16, 16, 16,
  954. 16, 16, 16, 16, 16, 16, 16, 16,
  955. 16, 16, 16, 16, 16, 16, 16, 16,
  956. 16, 16, 16, 16, 16, 16, 16, 16
  957. ]),
  958. PREMULTIPLIER_MATRIX = new Uint8Array([
  959. 32, 44, 42, 38, 32, 25, 17, 9,
  960. 44, 62, 58, 52, 44, 35, 24, 12,
  961. 42, 58, 55, 49, 42, 33, 23, 12,
  962. 38, 52, 49, 44, 38, 30, 20, 10,
  963. 32, 44, 42, 38, 32, 25, 17, 9,
  964. 25, 35, 33, 30, 25, 20, 14, 7,
  965. 17, 24, 23, 20, 17, 14, 9, 5,
  966. 9, 12, 12, 10, 9, 7, 5, 2
  967. ]),
  968. // MPEG-1 VLC
  969. // macroblock_stuffing decodes as 34.
  970. // macroblock_escape decodes as 35.
  971. MACROBLOCK_ADDRESS_INCREMENT = new Int16Array([
  972. 1*3, 2*3, 0, // 0
  973. 3*3, 4*3, 0, // 1 0
  974. 0, 0, 1, // 2 1.
  975. 5*3, 6*3, 0, // 3 00
  976. 7*3, 8*3, 0, // 4 01
  977. 9*3, 10*3, 0, // 5 000
  978. 11*3, 12*3, 0, // 6 001
  979. 0, 0, 3, // 7 010.
  980. 0, 0, 2, // 8 011.
  981. 13*3, 14*3, 0, // 9 0000
  982. 15*3, 16*3, 0, // 10 0001
  983. 0, 0, 5, // 11 0010.
  984. 0, 0, 4, // 12 0011.
  985. 17*3, 18*3, 0, // 13 0000 0
  986. 19*3, 20*3, 0, // 14 0000 1
  987. 0, 0, 7, // 15 0001 0.
  988. 0, 0, 6, // 16 0001 1.
  989. 21*3, 22*3, 0, // 17 0000 00
  990. 23*3, 24*3, 0, // 18 0000 01
  991. 25*3, 26*3, 0, // 19 0000 10
  992. 27*3, 28*3, 0, // 20 0000 11
  993. -1, 29*3, 0, // 21 0000 000
  994. -1, 30*3, 0, // 22 0000 001
  995. 31*3, 32*3, 0, // 23 0000 010
  996. 33*3, 34*3, 0, // 24 0000 011
  997. 35*3, 36*3, 0, // 25 0000 100
  998. 37*3, 38*3, 0, // 26 0000 101
  999. 0, 0, 9, // 27 0000 110.
  1000. 0, 0, 8, // 28 0000 111.
  1001. 39*3, 40*3, 0, // 29 0000 0001
  1002. 41*3, 42*3, 0, // 30 0000 0011
  1003. 43*3, 44*3, 0, // 31 0000 0100
  1004. 45*3, 46*3, 0, // 32 0000 0101
  1005. 0, 0, 15, // 33 0000 0110.
  1006. 0, 0, 14, // 34 0000 0111.
  1007. 0, 0, 13, // 35 0000 1000.
  1008. 0, 0, 12, // 36 0000 1001.
  1009. 0, 0, 11, // 37 0000 1010.
  1010. 0, 0, 10, // 38 0000 1011.
  1011. 47*3, -1, 0, // 39 0000 0001 0
  1012. -1, 48*3, 0, // 40 0000 0001 1
  1013. 49*3, 50*3, 0, // 41 0000 0011 0
  1014. 51*3, 52*3, 0, // 42 0000 0011 1
  1015. 53*3, 54*3, 0, // 43 0000 0100 0
  1016. 55*3, 56*3, 0, // 44 0000 0100 1
  1017. 57*3, 58*3, 0, // 45 0000 0101 0
  1018. 59*3, 60*3, 0, // 46 0000 0101 1
  1019. 61*3, -1, 0, // 47 0000 0001 00
  1020. -1, 62*3, 0, // 48 0000 0001 11
  1021. 63*3, 64*3, 0, // 49 0000 0011 00
  1022. 65*3, 66*3, 0, // 50 0000 0011 01
  1023. 67*3, 68*3, 0, // 51 0000 0011 10
  1024. 69*3, 70*3, 0, // 52 0000 0011 11
  1025. 71*3, 72*3, 0, // 53 0000 0100 00
  1026. 73*3, 74*3, 0, // 54 0000 0100 01
  1027. 0, 0, 21, // 55 0000 0100 10.
  1028. 0, 0, 20, // 56 0000 0100 11.
  1029. 0, 0, 19, // 57 0000 0101 00.
  1030. 0, 0, 18, // 58 0000 0101 01.
  1031. 0, 0, 17, // 59 0000 0101 10.
  1032. 0, 0, 16, // 60 0000 0101 11.
  1033. 0, 0, 35, // 61 0000 0001 000. -- macroblock_escape
  1034. 0, 0, 34, // 62 0000 0001 111. -- macroblock_stuffing
  1035. 0, 0, 33, // 63 0000 0011 000.
  1036. 0, 0, 32, // 64 0000 0011 001.
  1037. 0, 0, 31, // 65 0000 0011 010.
  1038. 0, 0, 30, // 66 0000 0011 011.
  1039. 0, 0, 29, // 67 0000 0011 100.
  1040. 0, 0, 28, // 68 0000 0011 101.
  1041. 0, 0, 27, // 69 0000 0011 110.
  1042. 0, 0, 26, // 70 0000 0011 111.
  1043. 0, 0, 25, // 71 0000 0100 000.
  1044. 0, 0, 24, // 72 0000 0100 001.
  1045. 0, 0, 23, // 73 0000 0100 010.
  1046. 0, 0, 22 // 74 0000 0100 011.
  1047. ]),
  1048. // macroblock_type bitmap:
  1049. // 0x10 macroblock_quant
  1050. // 0x08 macroblock_motion_forward
  1051. // 0x04 macroblock_motion_backward
  1052. // 0x02 macrobkock_pattern
  1053. // 0x01 macroblock_intra
  1054. //
  1055. MACROBLOCK_TYPE_I = new Int8Array([
  1056. 1*3, 2*3, 0, // 0
  1057. -1, 3*3, 0, // 1 0
  1058. 0, 0, 0x01, // 2 1.
  1059. 0, 0, 0x11 // 3 01.
  1060. ]),
  1061. MACROBLOCK_TYPE_P = new Int8Array([
  1062. 1*3, 2*3, 0, // 0
  1063. 3*3, 4*3, 0, // 1 0
  1064. 0, 0, 0x0a, // 2 1.
  1065. 5*3, 6*3, 0, // 3 00
  1066. 0, 0, 0x02, // 4 01.
  1067. 7*3, 8*3, 0, // 5 000
  1068. 0, 0, 0x08, // 6 001.
  1069. 9*3, 10*3, 0, // 7 0000
  1070. 11*3, 12*3, 0, // 8 0001
  1071. -1, 13*3, 0, // 9 00000
  1072. 0, 0, 0x12, // 10 00001.
  1073. 0, 0, 0x1a, // 11 00010.
  1074. 0, 0, 0x01, // 12 00011.
  1075. 0, 0, 0x11 // 13 000001.
  1076. ]),
  1077. MACROBLOCK_TYPE_B = new Int8Array([
  1078. 1*3, 2*3, 0, // 0
  1079. 3*3, 5*3, 0, // 1 0
  1080. 4*3, 6*3, 0, // 2 1
  1081. 8*3, 7*3, 0, // 3 00
  1082. 0, 0, 0x0c, // 4 10.
  1083. 9*3, 10*3, 0, // 5 01
  1084. 0, 0, 0x0e, // 6 11.
  1085. 13*3, 14*3, 0, // 7 001
  1086. 12*3, 11*3, 0, // 8 000
  1087. 0, 0, 0x04, // 9 010.
  1088. 0, 0, 0x06, // 10 011.
  1089. 18*3, 16*3, 0, // 11 0001
  1090. 15*3, 17*3, 0, // 12 0000
  1091. 0, 0, 0x08, // 13 0010.
  1092. 0, 0, 0x0a, // 14 0011.
  1093. -1, 19*3, 0, // 15 00000
  1094. 0, 0, 0x01, // 16 00011.
  1095. 20*3, 21*3, 0, // 17 00001
  1096. 0, 0, 0x1e, // 18 00010.
  1097. 0, 0, 0x11, // 19 000001.
  1098. 0, 0, 0x16, // 20 000010.
  1099. 0, 0, 0x1a // 21 000011.
  1100. ]),
  1101. CODE_BLOCK_PATTERN = new Int16Array([
  1102. 2*3, 1*3, 0, // 0
  1103. 3*3, 6*3, 0, // 1 1
  1104. 4*3, 5*3, 0, // 2 0
  1105. 8*3, 11*3, 0, // 3 10
  1106. 12*3, 13*3, 0, // 4 00
  1107. 9*3, 7*3, 0, // 5 01
  1108. 10*3, 14*3, 0, // 6 11
  1109. 20*3, 19*3, 0, // 7 011
  1110. 18*3, 16*3, 0, // 8 100
  1111. 23*3, 17*3, 0, // 9 010
  1112. 27*3, 25*3, 0, // 10 110
  1113. 21*3, 28*3, 0, // 11 101
  1114. 15*3, 22*3, 0, // 12 000
  1115. 24*3, 26*3, 0, // 13 001
  1116. 0, 0, 60, // 14 111.
  1117. 35*3, 40*3, 0, // 15 0000
  1118. 44*3, 48*3, 0, // 16 1001
  1119. 38*3, 36*3, 0, // 17 0101
  1120. 42*3, 47*3, 0, // 18 1000
  1121. 29*3, 31*3, 0, // 19 0111
  1122. 39*3, 32*3, 0, // 20 0110
  1123. 0, 0, 32, // 21 1010.
  1124. 45*3, 46*3, 0, // 22 0001
  1125. 33*3, 41*3, 0, // 23 0100
  1126. 43*3, 34*3, 0, // 24 0010
  1127. 0, 0, 4, // 25 1101.
  1128. 30*3, 37*3, 0, // 26 0011
  1129. 0, 0, 8, // 27 1100.
  1130. 0, 0, 16, // 28 1011.
  1131. 0, 0, 44, // 29 0111 0.
  1132. 50*3, 56*3, 0, // 30 0011 0
  1133. 0, 0, 28, // 31 0111 1.
  1134. 0, 0, 52, // 32 0110 1.
  1135. 0, 0, 62, // 33 0100 0.
  1136. 61*3, 59*3, 0, // 34 0010 1
  1137. 52*3, 60*3, 0, // 35 0000 0
  1138. 0, 0, 1, // 36 0101 1.
  1139. 55*3, 54*3, 0, // 37 0011 1
  1140. 0, 0, 61, // 38 0101 0.
  1141. 0, 0, 56, // 39 0110 0.
  1142. 57*3, 58*3, 0, // 40 0000 1
  1143. 0, 0, 2, // 41 0100 1.
  1144. 0, 0, 40, // 42 1000 0.
  1145. 51*3, 62*3, 0, // 43 0010 0
  1146. 0, 0, 48, // 44 1001 0.
  1147. 64*3, 63*3, 0, // 45 0001 0
  1148. 49*3, 53*3, 0, // 46 0001 1
  1149. 0, 0, 20, // 47 1000 1.
  1150. 0, 0, 12, // 48 1001 1.
  1151. 80*3, 83*3, 0, // 49 0001 10
  1152. 0, 0, 63, // 50 0011 00.
  1153. 77*3, 75*3, 0, // 51 0010 00
  1154. 65*3, 73*3, 0, // 52 0000 00
  1155. 84*3, 66*3, 0, // 53 0001 11
  1156. 0, 0, 24, // 54 0011 11.
  1157. 0, 0, 36, // 55 0011 10.
  1158. 0, 0, 3, // 56 0011 01.
  1159. 69*3, 87*3, 0, // 57 0000 10
  1160. 81*3, 79*3, 0, // 58 0000 11
  1161. 68*3, 71*3, 0, // 59 0010 11
  1162. 70*3, 78*3, 0, // 60 0000 01
  1163. 67*3, 76*3, 0, // 61 0010 10
  1164. 72*3, 74*3, 0, // 62 0010 01
  1165. 86*3, 85*3, 0, // 63 0001 01
  1166. 88*3, 82*3, 0, // 64 0001 00
  1167. -1, 94*3, 0, // 65 0000 000
  1168. 95*3, 97*3, 0, // 66 0001 111
  1169. 0, 0, 33, // 67 0010 100.
  1170. 0, 0, 9, // 68 0010 110.
  1171. 106*3, 110*3, 0, // 69 0000 100
  1172. 102*3, 116*3, 0, // 70 0000 010
  1173. 0, 0, 5, // 71 0010 111.
  1174. 0, 0, 10, // 72 0010 010.
  1175. 93*3, 89*3, 0, // 73 0000 001
  1176. 0, 0, 6, // 74 0010 011.
  1177. 0, 0, 18, // 75 0010 001.
  1178. 0, 0, 17, // 76 0010 101.
  1179. 0, 0, 34, // 77 0010 000.
  1180. 113*3, 119*3, 0, // 78 0000 011
  1181. 103*3, 104*3, 0, // 79 0000 111
  1182. 90*3, 92*3, 0, // 80 0001 100
  1183. 109*3, 107*3, 0, // 81 0000 110
  1184. 117*3, 118*3, 0, // 82 0001 001
  1185. 101*3, 99*3, 0, // 83 0001 101
  1186. 98*3, 96*3, 0, // 84 0001 110
  1187. 100*3, 91*3, 0, // 85 0001 011
  1188. 114*3, 115*3, 0, // 86 0001 010
  1189. 105*3, 108*3, 0, // 87 0000 101
  1190. 112*3, 111*3, 0, // 88 0001 000
  1191. 121*3, 125*3, 0, // 89 0000 0011
  1192. 0, 0, 41, // 90 0001 1000.
  1193. 0, 0, 14, // 91 0001 0111.
  1194. 0, 0, 21, // 92 0001 1001.
  1195. 124*3, 122*3, 0, // 93 0000 0010
  1196. 120*3, 123*3, 0, // 94 0000 0001
  1197. 0, 0, 11, // 95 0001 1110.
  1198. 0, 0, 19, // 96 0001 1101.
  1199. 0, 0, 7, // 97 0001 1111.
  1200. 0, 0, 35, // 98 0001 1100.
  1201. 0, 0, 13, // 99 0001 1011.
  1202. 0, 0, 50, // 100 0001 0110.
  1203. 0, 0, 49, // 101 0001 1010.
  1204. 0, 0, 58, // 102 0000 0100.
  1205. 0, 0, 37, // 103 0000 1110.
  1206. 0, 0, 25, // 104 0000 1111.
  1207. 0, 0, 45, // 105 0000 1010.
  1208. 0, 0, 57, // 106 0000 1000.
  1209. 0, 0, 26, // 107 0000 1101.
  1210. 0, 0, 29, // 108 0000 1011.
  1211. 0, 0, 38, // 109 0000 1100.
  1212. 0, 0, 53, // 110 0000 1001.
  1213. 0, 0, 23, // 111 0001 0001.
  1214. 0, 0, 43, // 112 0001 0000.
  1215. 0, 0, 46, // 113 0000 0110.
  1216. 0, 0, 42, // 114 0001 0100.
  1217. 0, 0, 22, // 115 0001 0101.
  1218. 0, 0, 54, // 116 0000 0101.
  1219. 0, 0, 51, // 117 0001 0010.
  1220. 0, 0, 15, // 118 0001 0011.
  1221. 0, 0, 30, // 119 0000 0111.
  1222. 0, 0, 39, // 120 0000 0001 0.
  1223. 0, 0, 47, // 121 0000 0011 0.
  1224. 0, 0, 55, // 122 0000 0010 1.
  1225. 0, 0, 27, // 123 0000 0001 1.
  1226. 0, 0, 59, // 124 0000 0010 0.
  1227. 0, 0, 31 // 125 0000 0011 1.
  1228. ]),
  1229. MOTION = new Int16Array([
  1230. 1*3, 2*3, 0, // 0
  1231. 4*3, 3*3, 0, // 1 0
  1232. 0, 0, 0, // 2 1.
  1233. 6*3, 5*3, 0, // 3 01
  1234. 8*3, 7*3, 0, // 4 00
  1235. 0, 0, -1, // 5 011.
  1236. 0, 0, 1, // 6 010.
  1237. 9*3, 10*3, 0, // 7 001
  1238. 12*3, 11*3, 0, // 8 000
  1239. 0, 0, 2, // 9 0010.
  1240. 0, 0, -2, // 10 0011.
  1241. 14*3, 15*3, 0, // 11 0001
  1242. 16*3, 13*3, 0, // 12 0000
  1243. 20*3, 18*3, 0, // 13 0000 1
  1244. 0, 0, 3, // 14 0001 0.
  1245. 0, 0, -3, // 15 0001 1.
  1246. 17*3, 19*3, 0, // 16 0000 0
  1247. -1, 23*3, 0, // 17 0000 00
  1248. 27*3, 25*3, 0, // 18 0000 11
  1249. 26*3, 21*3, 0, // 19 0000 01
  1250. 24*3, 22*3, 0, // 20 0000 10
  1251. 32*3, 28*3, 0, // 21 0000 011
  1252. 29*3, 31*3, 0, // 22 0000 101
  1253. -1, 33*3, 0, // 23 0000 001
  1254. 36*3, 35*3, 0, // 24 0000 100
  1255. 0, 0, -4, // 25 0000 111.
  1256. 30*3, 34*3, 0, // 26 0000 010
  1257. 0, 0, 4, // 27 0000 110.
  1258. 0, 0, -7, // 28 0000 0111.
  1259. 0, 0, 5, // 29 0000 1010.
  1260. 37*3, 41*3, 0, // 30 0000 0100
  1261. 0, 0, -5, // 31 0000 1011.
  1262. 0, 0, 7, // 32 0000 0110.
  1263. 38*3, 40*3, 0, // 33 0000 0011
  1264. 42*3, 39*3, 0, // 34 0000 0101
  1265. 0, 0, -6, // 35 0000 1001.
  1266. 0, 0, 6, // 36 0000 1000.
  1267. 51*3, 54*3, 0, // 37 0000 0100 0
  1268. 50*3, 49*3, 0, // 38 0000 0011 0
  1269. 45*3, 46*3, 0, // 39 0000 0101 1
  1270. 52*3, 47*3, 0, // 40 0000 0011 1
  1271. 43*3, 53*3, 0, // 41 0000 0100 1
  1272. 44*3, 48*3, 0, // 42 0000 0101 0
  1273. 0, 0, 10, // 43 0000 0100 10.
  1274. 0, 0, 9, // 44 0000 0101 00.
  1275. 0, 0, 8, // 45 0000 0101 10.
  1276. 0, 0, -8, // 46 0000 0101 11.
  1277. 57*3, 66*3, 0, // 47 0000 0011 11
  1278. 0, 0, -9, // 48 0000 0101 01.
  1279. 60*3, 64*3, 0, // 49 0000 0011 01
  1280. 56*3, 61*3, 0, // 50 0000 0011 00
  1281. 55*3, 62*3, 0, // 51 0000 0100 00
  1282. 58*3, 63*3, 0, // 52 0000 0011 10
  1283. 0, 0, -10, // 53 0000 0100 11.
  1284. 59*3, 65*3, 0, // 54 0000 0100 01
  1285. 0, 0, 12, // 55 0000 0100 000.
  1286. 0, 0, 16, // 56 0000 0011 000.
  1287. 0, 0, 13, // 57 0000 0011 110.
  1288. 0, 0, 14, // 58 0000 0011 100.
  1289. 0, 0, 11, // 59 0000 0100 010.
  1290. 0, 0, 15, // 60 0000 0011 010.
  1291. 0, 0, -16, // 61 0000 0011 001.
  1292. 0, 0, -12, // 62 0000 0100 001.
  1293. 0, 0, -14, // 63 0000 0011 101.
  1294. 0, 0, -15, // 64 0000 0011 011.
  1295. 0, 0, -11, // 65 0000 0100 011.
  1296. 0, 0, -13 // 66 0000 0011 111.
  1297. ]),
  1298. DCT_DC_SIZE_LUMINANCE = new Int8Array([
  1299. 2*3, 1*3, 0, // 0
  1300. 6*3, 5*3, 0, // 1 1
  1301. 3*3, 4*3, 0, // 2 0
  1302. 0, 0, 1, // 3 00.
  1303. 0, 0, 2, // 4 01.
  1304. 9*3, 8*3, 0, // 5 11
  1305. 7*3, 10*3, 0, // 6 10
  1306. 0, 0, 0, // 7 100.
  1307. 12*3, 11*3, 0, // 8 111
  1308. 0, 0, 4, // 9 110.
  1309. 0, 0, 3, // 10 101.
  1310. 13*3, 14*3, 0, // 11 1111
  1311. 0, 0, 5, // 12 1110.
  1312. 0, 0, 6, // 13 1111 0.
  1313. 16*3, 15*3, 0, // 14 1111 1
  1314. 17*3, -1, 0, // 15 1111 11
  1315. 0, 0, 7, // 16 1111 10.
  1316. 0, 0, 8 // 17 1111 110.
  1317. ]),
  1318. DCT_DC_SIZE_CHROMINANCE = new Int8Array([
  1319. 2*3, 1*3, 0, // 0
  1320. 4*3, 3*3, 0, // 1 1
  1321. 6*3, 5*3, 0, // 2 0
  1322. 8*3, 7*3, 0, // 3 11
  1323. 0, 0, 2, // 4 10.
  1324. 0, 0, 1, // 5 01.
  1325. 0, 0, 0, // 6 00.
  1326. 10*3, 9*3, 0, // 7 111
  1327. 0, 0, 3, // 8 110.
  1328. 12*3, 11*3, 0, // 9 1111
  1329. 0, 0, 4, // 10 1110.
  1330. 14*3, 13*3, 0, // 11 1111 1
  1331. 0, 0, 5, // 12 1111 0.
  1332. 16*3, 15*3, 0, // 13 1111 11
  1333. 0, 0, 6, // 14 1111 10.
  1334. 17*3, -1, 0, // 15 1111 111
  1335. 0, 0, 7, // 16 1111 110.
  1336. 0, 0, 8 // 17 1111 1110.
  1337. ]),
  1338. // dct_coeff bitmap:
  1339. // 0xff00 run
  1340. // 0x00ff level
  1341. // Decoded values are unsigned. Sign bit follows in the stream.
  1342. // Interpretation of the value 0x0001
  1343. // for dc_coeff_first: run=0, level=1
  1344. // for dc_coeff_next: If the next bit is 1: run=0, level=1
  1345. // If the next bit is 0: end_of_block
  1346. // escape decodes as 0xffff.
  1347. DCT_COEFF = new Int32Array([
  1348. 1*3, 2*3, 0, // 0
  1349. 4*3, 3*3, 0, // 1 0
  1350. 0, 0, 0x0001, // 2 1.
  1351. 7*3, 8*3, 0, // 3 01
  1352. 6*3, 5*3, 0, // 4 00
  1353. 13*3, 9*3, 0, // 5 001
  1354. 11*3, 10*3, 0, // 6 000
  1355. 14*3, 12*3, 0, // 7 010
  1356. 0, 0, 0x0101, // 8 011.
  1357. 20*3, 22*3, 0, // 9 0011
  1358. 18*3, 21*3, 0, // 10 0001
  1359. 16*3, 19*3, 0, // 11 0000
  1360. 0, 0, 0x0201, // 12 0101.
  1361. 17*3, 15*3, 0, // 13 0010
  1362. 0, 0, 0x0002, // 14 0100.
  1363. 0, 0, 0x0003, // 15 0010 1.
  1364. 27*3, 25*3, 0, // 16 0000 0
  1365. 29*3, 31*3, 0, // 17 0010 0
  1366. 24*3, 26*3, 0, // 18 0001 0
  1367. 32*3, 30*3, 0, // 19 0000 1
  1368. 0, 0, 0x0401, // 20 0011 0.
  1369. 23*3, 28*3, 0, // 21 0001 1
  1370. 0, 0, 0x0301, // 22 0011 1.
  1371. 0, 0, 0x0102, // 23 0001 10.
  1372. 0, 0, 0x0701, // 24 0001 00.
  1373. 0, 0, 0xffff, // 25 0000 01. -- escape
  1374. 0, 0, 0x0601, // 26 0001 01.
  1375. 37*3, 36*3, 0, // 27 0000 00
  1376. 0, 0, 0x0501, // 28 0001 11.
  1377. 35*3, 34*3, 0, // 29 0010 00
  1378. 39*3, 38*3, 0, // 30 0000 11
  1379. 33*3, 42*3, 0, // 31 0010 01
  1380. 40*3, 41*3, 0, // 32 0000 10
  1381. 52*3, 50*3, 0, // 33 0010 010
  1382. 54*3, 53*3, 0, // 34 0010 001
  1383. 48*3, 49*3, 0, // 35 0010 000
  1384. 43*3, 45*3, 0, // 36 0000 001
  1385. 46*3, 44*3, 0, // 37 0000 000
  1386. 0, 0, 0x0801, // 38 0000 111.
  1387. 0, 0, 0x0004, // 39 0000 110.
  1388. 0, 0, 0x0202, // 40 0000 100.
  1389. 0, 0, 0x0901, // 41 0000 101.
  1390. 51*3, 47*3, 0, // 42 0010 011
  1391. 55*3, 57*3, 0, // 43 0000 0010
  1392. 60*3, 56*3, 0, // 44 0000 0001
  1393. 59*3, 58*3, 0, // 45 0000 0011
  1394. 61*3, 62*3, 0, // 46 0000 0000
  1395. 0, 0, 0x0a01, // 47 0010 0111.
  1396. 0, 0, 0x0d01, // 48 0010 0000.
  1397. 0, 0, 0x0006, // 49 0010 0001.
  1398. 0, 0, 0x0103, // 50 0010 0101.
  1399. 0, 0, 0x0005, // 51 0010 0110.
  1400. 0, 0, 0x0302, // 52 0010 0100.
  1401. 0, 0, 0x0b01, // 53 0010 0011.
  1402. 0, 0, 0x0c01, // 54 0010 0010.
  1403. 76*3, 75*3, 0, // 55 0000 0010 0
  1404. 67*3, 70*3, 0, // 56 0000 0001 1
  1405. 73*3, 71*3, 0, // 57 0000 0010 1
  1406. 78*3, 74*3, 0, // 58 0000 0011 1
  1407. 72*3, 77*3, 0, // 59 0000 0011 0
  1408. 69*3, 64*3, 0, // 60 0000 0001 0
  1409. 68*3, 63*3, 0, // 61 0000 0000 0
  1410. 66*3, 65*3, 0, // 62 0000 0000 1
  1411. 81*3, 87*3, 0, // 63 0000 0000 01
  1412. 91*3, 80*3, 0, // 64 0000 0001 01
  1413. 82*3, 79*3, 0, // 65 0000 0000 11
  1414. 83*3, 86*3, 0, // 66 0000 0000 10
  1415. 93*3, 92*3, 0, // 67 0000 0001 10
  1416. 84*3, 85*3, 0, // 68 0000 0000 00
  1417. 90*3, 94*3, 0, // 69 0000 0001 00
  1418. 88*3, 89*3, 0, // 70 0000 0001 11
  1419. 0, 0, 0x0203, // 71 0000 0010 11.
  1420. 0, 0, 0x0104, // 72 0000 0011 00.
  1421. 0, 0, 0x0007, // 73 0000 0010 10.
  1422. 0, 0, 0x0402, // 74 0000 0011 11.
  1423. 0, 0, 0x0502, // 75 0000 0010 01.
  1424. 0, 0, 0x1001, // 76 0000 0010 00.
  1425. 0, 0, 0x0f01, // 77 0000 0011 01.
  1426. 0, 0, 0x0e01, // 78 0000 0011 10.
  1427. 105*3, 107*3, 0, // 79 0000 0000 111
  1428. 111*3, 114*3, 0, // 80 0000 0001 011
  1429. 104*3, 97*3, 0, // 81 0000 0000 010
  1430. 125*3, 119*3, 0, // 82 0000 0000 110
  1431. 96*3, 98*3, 0, // 83 0000 0000 100
  1432. -1, 123*3, 0, // 84 0000 0000 000
  1433. 95*3, 101*3, 0, // 85 0000 0000 001
  1434. 106*3, 121*3, 0, // 86 0000 0000 101
  1435. 99*3, 102*3, 0, // 87 0000 0000 011
  1436. 113*3, 103*3, 0, // 88 0000 0001 110
  1437. 112*3, 116*3, 0, // 89 0000 0001 111
  1438. 110*3, 100*3, 0, // 90 0000 0001 000
  1439. 124*3, 115*3, 0, // 91 0000 0001 010
  1440. 117*3, 122*3, 0, // 92 0000 0001 101
  1441. 109*3, 118*3, 0, // 93 0000 0001 100
  1442. 120*3, 108*3, 0, // 94 0000 0001 001
  1443. 127*3, 136*3, 0, // 95 0000 0000 0010
  1444. 139*3, 140*3, 0, // 96 0000 0000 1000
  1445. 130*3, 126*3, 0, // 97 0000 0000 0101
  1446. 145*3, 146*3, 0, // 98 0000 0000 1001
  1447. 128*3, 129*3, 0, // 99 0000 0000 0110
  1448. 0, 0, 0x0802, // 100 0000 0001 0001.
  1449. 132*3, 134*3, 0, // 101 0000 0000 0011
  1450. 155*3, 154*3, 0, // 102 0000 0000 0111
  1451. 0, 0, 0x0008, // 103 0000 0001 1101.
  1452. 137*3, 133*3, 0, // 104 0000 0000 0100
  1453. 143*3, 144*3, 0, // 105 0000 0000 1110
  1454. 151*3, 138*3, 0, // 106 0000 0000 1010
  1455. 142*3, 141*3, 0, // 107 0000 0000 1111
  1456. 0, 0, 0x000a, // 108 0000 0001 0011.
  1457. 0, 0, 0x0009, // 109 0000 0001 1000.
  1458. 0, 0, 0x000b, // 110 0000 0001 0000.
  1459. 0, 0, 0x1501, // 111 0000 0001 0110.
  1460. 0, 0, 0x0602, // 112 0000 0001 1110.
  1461. 0, 0, 0x0303, // 113 0000 0001 1100.
  1462. 0, 0, 0x1401, // 114 0000 0001 0111.
  1463. 0, 0, 0x0702, // 115 0000 0001 0101.
  1464. 0, 0, 0x1101, // 116 0000 0001 1111.
  1465. 0, 0, 0x1201, // 117 0000 0001 1010.
  1466. 0, 0, 0x1301, // 118 0000 0001 1001.
  1467. 148*3, 152*3, 0, // 119 0000 0000 1101
  1468. 0, 0, 0x0403, // 120 0000 0001 0010.
  1469. 153*3, 150*3, 0, // 121 0000 0000 1011
  1470. 0, 0, 0x0105, // 122 0000 0001 1011.
  1471. 131*3, 135*3, 0, // 123 0000 0000 0001
  1472. 0, 0, 0x0204, // 124 0000 0001 0100.
  1473. 149*3, 147*3, 0, // 125 0000 0000 1100
  1474. 172*3, 173*3, 0, // 126 0000 0000 0101 1
  1475. 162*3, 158*3, 0, // 127 0000 0000 0010 0
  1476. 170*3, 161*3, 0, // 128 0000 0000 0110 0
  1477. 168*3, 166*3, 0, // 129 0000 0000 0110 1
  1478. 157*3, 179*3, 0, // 130 0000 0000 0101 0
  1479. 169*3, 167*3, 0, // 131 0000 0000 0001 0
  1480. 174*3, 171*3, 0, // 132 0000 0000 0011 0
  1481. 178*3, 177*3, 0, // 133 0000 0000 0100 1
  1482. 156*3, 159*3, 0, // 134 0000 0000 0011 1
  1483. 164*3, 165*3, 0, // 135 0000 0000 0001 1
  1484. 183*3, 182*3, 0, // 136 0000 0000 0010 1
  1485. 175*3, 176*3, 0, // 137 0000 0000 0100 0
  1486. 0, 0, 0x0107, // 138 0000 0000 1010 1.
  1487. 0, 0, 0x0a02, // 139 0000 0000 1000 0.
  1488. 0, 0, 0x0902, // 140 0000 0000 1000 1.
  1489. 0, 0, 0x1601, // 141 0000 0000 1111 1.
  1490. 0, 0, 0x1701, // 142 0000 0000 1111 0.
  1491. 0, 0, 0x1901, // 143 0000 0000 1110 0.
  1492. 0, 0, 0x1801, // 144 0000 0000 1110 1.
  1493. 0, 0, 0x0503, // 145 0000 0000 1001 0.
  1494. 0, 0, 0x0304, // 146 0000 0000 1001 1.
  1495. 0, 0, 0x000d, // 147 0000 0000 1100 1.
  1496. 0, 0, 0x000c, // 148 0000 0000 1101 0.
  1497. 0, 0, 0x000e, // 149 0000 0000 1100 0.
  1498. 0, 0, 0x000f, // 150 0000 0000 1011 1.
  1499. 0, 0, 0x0205, // 151 0000 0000 1010 0.
  1500. 0, 0, 0x1a01, // 152 0000 0000 1101 1.
  1501. 0, 0, 0x0106, // 153 0000 0000 1011 0.
  1502. 180*3, 181*3, 0, // 154 0000 0000 0111 1
  1503. 160*3, 163*3, 0, // 155 0000 0000 0111 0
  1504. 196*3, 199*3, 0, // 156 0000 0000 0011 10
  1505. 0, 0, 0x001b, // 157 0000 0000 0101 00.
  1506. 203*3, 185*3, 0, // 158 0000 0000 0010 01
  1507. 202*3, 201*3, 0, // 159 0000 0000 0011 11
  1508. 0, 0, 0x0013, // 160 0000 0000 0111 00.
  1509. 0, 0, 0x0016, // 161 0000 0000 0110 01.
  1510. 197*3, 207*3, 0, // 162 0000 0000 0010 00
  1511. 0, 0, 0x0012, // 163 0000 0000 0111 01.
  1512. 191*3, 192*3, 0, // 164 0000 0000 0001 10
  1513. 188*3, 190*3, 0, // 165 0000 0000 0001 11
  1514. 0, 0, 0x0014, // 166 0000 0000 0110 11.
  1515. 184*3, 194*3, 0, // 167 0000 0000 0001 01
  1516. 0, 0, 0x0015, // 168 0000 0000 0110 10.
  1517. 186*3, 193*3, 0, // 169 0000 0000 0001 00
  1518. 0, 0, 0x0017, // 170 0000 0000 0110 00.
  1519. 204*3, 198*3, 0, // 171 0000 0000 0011 01
  1520. 0, 0, 0x0019, // 172 0000 0000 0101 10.
  1521. 0, 0, 0x0018, // 173 0000 0000 0101 11.
  1522. 200*3, 205*3, 0, // 174 0000 0000 0011 00
  1523. 0, 0, 0x001f, // 175 0000 0000 0100 00.
  1524. 0, 0, 0x001e, // 176 0000 0000 0100 01.
  1525. 0, 0, 0x001c, // 177 0000 0000 0100 11.
  1526. 0, 0, 0x001d, // 178 0000 0000 0100 10.
  1527. 0, 0, 0x001a, // 179 0000 0000 0101 01.
  1528. 0, 0, 0x0011, // 180 0000 0000 0111 10.
  1529. 0, 0, 0x0010, // 181 0000 0000 0111 11.
  1530. 189*3, 206*3, 0, // 182 0000 0000 0010 11
  1531. 187*3, 195*3, 0, // 183 0000 0000 0010 10
  1532. 218*3, 211*3, 0, // 184 0000 0000 0001 010
  1533. 0, 0, 0x0025, // 185 0000 0000 0010 011.
  1534. 215*3, 216*3, 0, // 186 0000 0000 0001 000
  1535. 0, 0, 0x0024, // 187 0000 0000 0010 100.
  1536. 210*3, 212*3, 0, // 188 0000 0000 0001 110
  1537. 0, 0, 0x0022, // 189 0000 0000 0010 110.
  1538. 213*3, 209*3, 0, // 190 0000 0000 0001 111
  1539. 221*3, 222*3, 0, // 191 0000 0000 0001 100
  1540. 219*3, 208*3, 0, // 192 0000 0000 0001 101
  1541. 217*3, 214*3, 0, // 193 0000 0000 0001 001
  1542. 223*3, 220*3, 0, // 194 0000 0000 0001 011
  1543. 0, 0, 0x0023, // 195 0000 0000 0010 101.
  1544. 0, 0, 0x010b, // 196 0000 0000 0011 100.
  1545. 0, 0, 0x0028, // 197 0000 0000 0010 000.
  1546. 0, 0, 0x010c, // 198 0000 0000 0011 011.
  1547. 0, 0, 0x010a, // 199 0000 0000 0011 101.
  1548. 0, 0, 0x0020, // 200 0000 0000 0011 000.
  1549. 0, 0, 0x0108, // 201 0000 0000 0011 111.
  1550. 0, 0, 0x0109, // 202 0000 0000 0011 110.
  1551. 0, 0, 0x0026, // 203 0000 0000 0010 010.
  1552. 0, 0, 0x010d, // 204 0000 0000 0011 010.
  1553. 0, 0, 0x010e, // 205 0000 0000 0011 001.
  1554. 0, 0, 0x0021, // 206 0000 0000 0010 111.
  1555. 0, 0, 0x0027, // 207 0000 0000 0010 001.
  1556. 0, 0, 0x1f01, // 208 0000 0000 0001 1011.
  1557. 0, 0, 0x1b01, // 209 0000 0000 0001 1111.
  1558. 0, 0, 0x1e01, // 210 0000 0000 0001 1100.
  1559. 0, 0, 0x1002, // 211 0000 0000 0001 0101.
  1560. 0, 0, 0x1d01, // 212 0000 0000 0001 1101.
  1561. 0, 0, 0x1c01, // 213 0000 0000 0001 1110.
  1562. 0, 0, 0x010f, // 214 0000 0000 0001 0011.
  1563. 0, 0, 0x0112, // 215 0000 0000 0001 0000.
  1564. 0, 0, 0x0111, // 216 0000 0000 0001 0001.
  1565. 0, 0, 0x0110, // 217 0000 0000 0001 0010.
  1566. 0, 0, 0x0603, // 218 0000 0000 0001 0100.
  1567. 0, 0, 0x0b02, // 219 0000 0000 0001 1010.
  1568. 0, 0, 0x0e02, // 220 0000 0000 0001 0111.
  1569. 0, 0, 0x0d02, // 221 0000 0000 0001 1000.
  1570. 0, 0, 0x0c02, // 222 0000 0000 0001 1001.
  1571. 0, 0, 0x0f02 // 223 0000 0000 0001 0110.
  1572. ]),
  1573. PICTURE_TYPE_I = 1,
  1574. PICTURE_TYPE_P = 2,
  1575. PICTURE_TYPE_B = 3,
  1576. PICTURE_TYPE_D = 4,
  1577. START_SEQUENCE = 0xB3,
  1578. START_SLICE_FIRST = 0x01,
  1579. START_SLICE_LAST = 0xAF,
  1580. START_PICTURE = 0x00,
  1581. START_EXTENSION = 0xB5,
  1582. START_USER_DATA = 0xB2;
  1583. var MACROBLOCK_TYPE_TABLES = [
  1584. null,
  1585. MACROBLOCK_TYPE_I,
  1586. MACROBLOCK_TYPE_P,
  1587. MACROBLOCK_TYPE_B
  1588. ];
  1589. // ----------------------------------------------------------------------------
  1590. // Bit Reader
  1591. var BitReader = function(arrayBuffer) {
  1592. this.bytes = new Uint8Array(arrayBuffer);
  1593. this.length = this.bytes.length;
  1594. this.index = 0;
  1595. };
  1596. BitReader.NOT_FOUND = -1;
  1597. BitReader.prototype.findNextMPEGStartCode = function() {
  1598. for( var i = (this.index+7 >> 3); i < this.length; i++ ) {
  1599. if(
  1600. this.bytes[i] == 0x00 &&
  1601. this.bytes[i+1] == 0x00 &&
  1602. this.bytes[i+2] == 0x01
  1603. ) {
  1604. this.index = (i+4) << 3;
  1605. return this.bytes[i+3];
  1606. }
  1607. }
  1608. this.index = (this.length << 3);
  1609. return BitReader.NOT_FOUND;
  1610. };
  1611. BitReader.prototype.nextBytesAreStartCode = function() {
  1612. var i = (this.index+7 >> 3);
  1613. return (
  1614. i >= this.length || (
  1615. this.bytes[i] == 0x00 &&
  1616. this.bytes[i+1] == 0x00 &&
  1617. this.bytes[i+2] == 0x01
  1618. )
  1619. );
  1620. };
  1621. BitReader.prototype.nextBits = function(count) {
  1622. var
  1623. byteOffset = this.index >> 3,
  1624. room = (8 - this.index % 8);
  1625. if( room >= count ) {
  1626. return (this.bytes[byteOffset] >> (room - count)) & (0xff >> (8-count));
  1627. }
  1628. var
  1629. leftover = (this.index + count) % 8, // Leftover bits in last byte
  1630. end = (this.index + count -1) >> 3,
  1631. value = this.bytes[byteOffset] & (0xff >> (8-room)); // Fill out first byte
  1632. for( byteOffset++; byteOffset < end; byteOffset++ ) {
  1633. value <<= 8; // Shift and
  1634. value |= this.bytes[byteOffset]; // Put next byte
  1635. }
  1636. if (leftover > 0) {
  1637. value <<= leftover; // Make room for remaining bits
  1638. value |= (this.bytes[byteOffset] >> (8 - leftover));
  1639. }
  1640. else {
  1641. value <<= 8;
  1642. value |= this.bytes[byteOffset];
  1643. }
  1644. return value;
  1645. };
  1646. BitReader.prototype.getBits = function(count) {
  1647. var value = this.nextBits(count);
  1648. this.index += count;
  1649. return value;
  1650. };
  1651. BitReader.prototype.advance = function(count) {
  1652. return (this.index += count);
  1653. };
  1654. BitReader.prototype.rewind = function(count) {
  1655. return (this.index -= count);
  1656. };
  1657. })(window);