jsmpg.js 59KB

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