jsmpg.js 52KB

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