jsmpg.js 59KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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.YCbCrToRGBA();
  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.YCbCrToRGBA = function() {
  285. var pY = this.currentY;
  286. var pCb = this.currentCb;
  287. var pCr = this.currentCr;
  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, sY, sCr, sCb ) {
  578. var
  579. width, scan,
  580. H, V, oddH, oddV,
  581. src, dest, last;
  582. var dY = this.currentY;
  583. var dCb = this.currentCb;
  584. var dCr = this.currentCr;
  585. // Luminance
  586. width = this.codedWidth;
  587. scan = width - 16;
  588. H = motionH >> 1;
  589. V = motionV >> 1;
  590. oddH = (motionH & 1) == 1;
  591. oddV = (motionV & 1) == 1;
  592. src = ((this.mbRow << 4) + V) * width + (this.mbCol << 4) + H;
  593. dest = (this.mbRow * width + this.mbCol) << 4;
  594. last = dest + (width << 4);
  595. if( oddH ) {
  596. if( oddV ) {
  597. while( dest < last ) {
  598. for( var x = 0; x < 16; x++ ) {
  599. dY[dest] = (sY[src] + sY[src+1] + sY[src+width] + sY[src+width+1] + 2) >> 2;
  600. dest++; src++;
  601. }
  602. dest += scan; src += scan;
  603. }
  604. }
  605. else {
  606. while( dest < last ) {
  607. for( var x = 0; x < 16; x++ ) {
  608. dY[dest] = (sY[src] + sY[src+1] + 1) >> 1;
  609. dest++; src++;
  610. }
  611. dest += scan; src += scan;
  612. }
  613. }
  614. }
  615. else {
  616. if( oddV ) {
  617. while( dest < last ) {
  618. for( var x = 0; x < 16; x++ ) {
  619. dY[dest] = (sY[src] + sY[src+width] + 1) >> 1;
  620. dest++; src++;
  621. }
  622. dest += scan; src += scan;
  623. }
  624. }
  625. else {
  626. while( dest < last ) {
  627. for( var x = 0; x < 16; x++ ) {
  628. dY[dest] = sY[src];
  629. dest++; src++;
  630. }
  631. dest += scan; src += scan;
  632. }
  633. }
  634. }
  635. // Chrominance
  636. width = this.halfWidth;
  637. scan = width - 8;
  638. H = (motionH/2) >> 1;
  639. V = (motionV/2) >> 1;
  640. oddH = ((motionH/2) & 1) == 1;
  641. oddV = ((motionV/2) & 1) == 1;
  642. src = ((this.mbRow << 3) + V) * width + (this.mbCol << 3) + H;
  643. dest = (this.mbRow * width + this.mbCol) << 3;
  644. last = dest + (width << 3);
  645. if( oddH ) {
  646. if( oddV ) {
  647. while( dest < last ) {
  648. for( var x = 0; x < 8; x++ ) {
  649. dCr[dest] = (sCr[src] + sCr[src+1] + sCr[src+width] + sCr[src+width+1] + 2) >> 2;
  650. dCb[dest] = (sCb[src] + sCb[src+1] + sCb[src+width] + sCb[src+width+1] + 2) >> 2;
  651. dest++; src++;
  652. }
  653. dest += scan; src += scan;
  654. }
  655. }
  656. else {
  657. while( dest < last ) {
  658. for( var x = 0; x < 8; x++ ) {
  659. dCr[dest] = (sCr[src] + sCr[src+1] + 1) >> 1;
  660. dCb[dest] = (sCb[src] + sCb[src+1] + 1) >> 1;
  661. dest++; src++;
  662. }
  663. dest += scan; src += scan;
  664. }
  665. }
  666. }
  667. else {
  668. if( oddV ) {
  669. while( dest < last ) {
  670. for( var x = 0; x < 8; x++ ) {
  671. dCr[dest] = (sCr[src] + sCr[src+width] + 1) >> 1;
  672. dCb[dest] = (sCb[src] + sCb[src+width] + 1) >> 1;
  673. dest++; src++;
  674. }
  675. dest += scan; src += scan;
  676. }
  677. }
  678. else {
  679. while( dest < last ) {
  680. for( var x = 0; x < 8; x++ ) {
  681. dCr[dest] = sCr[src];
  682. dCb[dest] = sCb[src];
  683. dest++; src++;
  684. }
  685. dest += scan; src += scan;
  686. }
  687. }
  688. }
  689. };
  690. jsmpeg.prototype.interpolateMacroblock = function() {
  691. var
  692. width, scan,
  693. H, V, oddH, oddV,
  694. src, dest, last;
  695. // Luminance
  696. width = this.codedWidth;
  697. scan = width - 16;
  698. H = this.motionBwH >> 1;
  699. V = this.motionBwV >> 1;
  700. oddH = (this.motionBwH & 1) == 1;
  701. oddV = (this.motionBwV & 1) == 1;
  702. src = ((this.mbRow << 4) + V) * width + (this.mbCol << 4) + H;
  703. dest = (this.mbRow * width + this.mbCol) << 4;
  704. last = dest + (width << 4);
  705. if( oddH ) {
  706. if( oddV ) {
  707. while( dest < last ) {
  708. for( var x = 0; x < 16; x++ ) {
  709. this.currentY[dest] += ((this.backwardY[src] + this.backwardY[src+1] +
  710. this.backwardY[src+width] + this.backwardY[src+width+1] + 2) >> 2) + 1;
  711. this.currentY[dest] >>= 1;
  712. dest++; src++;
  713. }
  714. dest += scan; src += scan;
  715. }
  716. }
  717. else {
  718. while( dest < last ) {
  719. for( var x = 0; x < 16; x++ ) {
  720. this.currentY[dest] += ((this.backwardY[src] + this.backwardY[src+1] + 1) >> 1) + 1;
  721. this.currentY[dest] >>= 1;
  722. dest++; src++;
  723. }
  724. dest += scan; src += scan;
  725. }
  726. }
  727. }
  728. else {
  729. if( oddV ) {
  730. while( dest < last ) {
  731. for( var x = 0; x < 16; x++ ) {
  732. this.currentY[dest] += ((this.backwardY[src] + this.backwardY[src+width] + 1) >> 1) + 1;
  733. this.currentY[dest] >>= 1;
  734. dest++; src++;
  735. }
  736. dest += scan; src += scan;
  737. }
  738. }
  739. else {
  740. while( dest < last ) {
  741. for( var x = 0; x < 16; x++ ) {
  742. this.currentY[dest] += this.backwardY[src] + 1;
  743. this.currentY[dest] >>= 1;
  744. dest++; src++;
  745. }
  746. dest += scan; src += scan;
  747. }
  748. }
  749. }
  750. // Chrominance
  751. width = this.halfWidth;
  752. scan = width - 8;
  753. H = (this.motionBwH/2) >> 1;
  754. V = (this.motionBwV/2) >> 1;
  755. oddH = ((this.motionBwH/2) & 1) == 1;
  756. oddV = ((this.motionBwV/2) & 1) == 1;
  757. src = ((this.mbRow << 3) + V) * width + (this.mbCol << 3) + H;
  758. dest = (this.mbRow * width + this.mbCol) << 3;
  759. last = dest + (width << 3);
  760. if( oddH ) {
  761. if( oddV ) {
  762. while( dest < last ) {
  763. for( var x = 0; x < 8; x++ ) {
  764. this.currentCr[dest] += ((this.backwardCr[src] + this.backwardCr[src+1] +
  765. this.backwardCr[src+width] + this.backwardCr[src+width+1] + 2) >> 2) + 1;
  766. this.currentCb[dest] += ((this.backwardCb[src] + this.backwardCb[src+1] +
  767. this.backwardCb[src+width] + this.backwardCb[src+width+1] + 2) >> 2) + 1;
  768. this.currentCr[dest] >>= 1;
  769. this.currentCb[dest] >>= 1;
  770. dest++; src++;
  771. }
  772. dest += scan; src += scan;
  773. }
  774. }
  775. else {
  776. while( dest < last ) {
  777. for( var x = 0; x < 8; x++ ) {
  778. this.currentCr[dest] += ((this.backwardCr[src] + this.backwardCr[src+1] + 1) >> 1) + 1;
  779. this.currentCb[dest] += ((this.backwardCb[src] + this.backwardCb[src+1] + 1) >> 1) + 1;
  780. this.currentCr[dest] >>= 1;
  781. this.currentCb[dest] >>= 1;
  782. dest++; src++;
  783. }
  784. dest += scan; src += scan;
  785. }
  786. }
  787. }
  788. else {
  789. if( oddV ) {
  790. while( dest < last ) {
  791. for( var x = 0; x < 8; x++ ) {
  792. this.currentCr[dest] += ((this.backwardCr[src] + this.backwardCr[src+width] + 1) >> 1) + 1;
  793. this.currentCb[dest] += ((this.backwardCb[src] + this.backwardCb[src+width] + 1) >> 1) + 1;
  794. this.currentCr[dest] >>= 1;
  795. this.currentCb[dest] >>= 1;
  796. dest++; src++;
  797. }
  798. dest += scan; src += scan;
  799. }
  800. }
  801. else {
  802. while( dest < last ) {
  803. for( var x = 0; x < 8; x++ ) {
  804. this.currentCr[dest] += this.backwardCr[src] + 1;
  805. this.currentCb[dest] += this.backwardCb[src] + 1;
  806. this.currentCr[dest] >>= 1;
  807. this.currentCb[dest] >>= 1;
  808. dest++; src++;
  809. }
  810. dest += scan; src += scan;
  811. }
  812. }
  813. }
  814. };
  815. jsmpeg.prototype.predictMacroblock = function() {
  816. if( this.pictureCodingType == PICTURE_TYPE_B ) {
  817. if( this.macroblockMotFw ) {
  818. this.copyMacroblock(this.motionFwH, this.motionFwV, this.forwardY, this.forwardCr, this.forwardCb);
  819. if( this.macroblockMotBw ) {
  820. this.interpolateMacroblock();
  821. }
  822. }
  823. else {
  824. this.copyMacroblock(this.motionBwH, this.motionBwV, this.backwardY, this.backwardCr, this.backwardCb);
  825. }
  826. }
  827. else {
  828. this.copyMacroblock(this.motionFwH, this.motionFwV, this.forwardY, this.forwardCr, this.forwardCb);
  829. }
  830. // Ignore error on motion vectors pointing outside the bitmap
  831. };
  832. // ----------------------------------------------------------------------------
  833. // Block layer
  834. jsmpeg.prototype.dcPredictorY;
  835. jsmpeg.prototype.dcPredictorCr;
  836. jsmpeg.prototype.dcPredictorCb;
  837. jsmpeg.prototype.blockData = null;
  838. jsmpeg.prototype.decodeBlock = function(block) {
  839. var
  840. n = 0,
  841. quantMatrix;
  842. // Clear preverious data
  843. this.fillArray(this.blockData, 0);
  844. // Decode DC coefficient of intra-coded blocks
  845. if( this.macroblockIntra ) {
  846. var
  847. predictor,
  848. dctSize;
  849. // DC prediction
  850. if( block < 4 ) {
  851. predictor = this.dcPredictorY;
  852. dctSize = this.readCode(DCT_DC_SIZE_LUMINANCE);
  853. }
  854. else {
  855. predictor = (block == 4 ? this.dcPredictorCr : this.dcPredictorCb);
  856. dctSize = this.readCode(DCT_DC_SIZE_CHROMINANCE);
  857. }
  858. // Read DC coeff
  859. if( dctSize > 0 ) {
  860. var differential = this.buffer.getBits(dctSize);
  861. if( (differential & (1 << (dctSize - 1))) != 0 ) {
  862. this.blockData[0] = predictor + differential;
  863. }
  864. else {
  865. this.blockData[0] = predictor + ((-1 << dctSize)|(differential+1));
  866. }
  867. }
  868. else {
  869. this.blockData[0] = predictor;
  870. }
  871. // Save predictor value
  872. if( block < 4 ) {
  873. this.dcPredictorY = this.blockData[0];
  874. }
  875. else if( block == 4 ) {
  876. this.dcPredictorCr = this.blockData[0];
  877. }
  878. else {
  879. this.dcPredictorCb = this.blockData[0];
  880. }
  881. // Dequantize + premultiply
  882. this.blockData[0] <<= (3 + 5);
  883. quantMatrix = this.intraQuantMatrix;
  884. n = 1;
  885. }
  886. else {
  887. quantMatrix = this.nonIntraQuantMatrix;
  888. }
  889. // Decode AC coefficients (+DC for non-intra)
  890. var level = 0;
  891. while( true ) {
  892. var
  893. run = 0,
  894. coeff = this.readCode(DCT_COEFF);
  895. if( (coeff == 0x0001) && (n > 0) && (this.buffer.getBits(1) == 0) ) {
  896. // end_of_block
  897. break;
  898. }
  899. if( coeff == 0xffff ) {
  900. // escape
  901. run = this.buffer.getBits(6);
  902. level = this.buffer.getBits(8);
  903. if( level == 0 ) {
  904. level = this.buffer.getBits(8);
  905. }
  906. else if( level == 128 ) {
  907. level = this.buffer.getBits(8) - 256;
  908. }
  909. else if( level > 128 ) {
  910. level = level - 256;
  911. }
  912. }
  913. else {
  914. run = coeff >> 8;
  915. level = coeff & 0xff;
  916. if( this.buffer.getBits(1) ) {
  917. level = -level;
  918. }
  919. }
  920. n += run;
  921. var dezigZagged = ZIG_ZAG[n];
  922. n++;
  923. // Dequantize, oddify, clip
  924. level <<= 1;
  925. if( !this.macroblockIntra ) {
  926. level += (level < 0 ? -1 : 1);
  927. }
  928. level = (level * this.quantizerScale * quantMatrix[dezigZagged]) >> 4;
  929. if( (level & 1) == 0 ) {
  930. level -= level > 0 ? 1 : -1;
  931. }
  932. if( level > 2047 ) {
  933. level = 2047;
  934. }
  935. else if( level < -2048 ) {
  936. level = -2048;
  937. }
  938. // Save premultiplied coefficient
  939. this.blockData[dezigZagged] = level * PREMULTIPLIER_MATRIX[dezigZagged];
  940. };
  941. // Transform block data to the spatial domain
  942. if( n == 1 ) {
  943. // Only DC coeff., no IDCT needed
  944. this.fillArray(this.blockData, (this.blockData[0] + 128) >> 8);
  945. }
  946. else {
  947. this.IDCT();
  948. }
  949. // Move block to its place
  950. var
  951. destArray,
  952. destIndex,
  953. scan;
  954. if( block < 4 ) {
  955. destArray = this.currentY;
  956. scan = this.codedWidth - 8;
  957. destIndex = (this.mbRow * this.codedWidth + this.mbCol) << 4;
  958. if( (block & 1) != 0 ) {
  959. destIndex += 8;
  960. }
  961. if( (block & 2) != 0 ) {
  962. destIndex += this.codedWidth << 3;
  963. }
  964. }
  965. else {
  966. destArray = (block == 4) ? this.currentCb : this.currentCr;
  967. scan = (this.codedWidth >> 1) - 8;
  968. destIndex = ((this.mbRow * this.codedWidth) << 2) + (this.mbCol << 3);
  969. }
  970. n = 0;
  971. if( this.macroblockIntra ) {
  972. // Overwrite (no prediction)
  973. for( var i = 0; i < 8; i++ ) {
  974. for( var j = 0; j < 8; j++ ) {
  975. var value = this.blockData[n];
  976. n++;
  977. destArray[destIndex] = value;
  978. destIndex++;
  979. }
  980. destIndex += scan;
  981. }
  982. }
  983. else {
  984. // Add data to the predicted macroblock
  985. for( var i = 0; i < 8; i++ ) {
  986. for( var j = 0; j < 8; j++ ) {
  987. var value = this.blockData[n] + destArray[destIndex];
  988. n++;
  989. destArray[destIndex] = value;
  990. destIndex++;
  991. }
  992. destIndex += scan;
  993. }
  994. }
  995. };
  996. jsmpeg.prototype.IDCT = function() {
  997. // See http://vsr.informatik.tu-chemnitz.de/~jan/MPEG/HTML/IDCT.html
  998. // for more info.
  999. var
  1000. b1, b3, b4, b6, b7, tmp1, tmp2, m0,
  1001. x0, x1, x2, x3, x4, y3, y4, y5, y6, y7,
  1002. i,
  1003. blockData = this.blockData;
  1004. // Transform columns
  1005. for( i = 0; i < 8; ++i ) {
  1006. b1 = blockData[4*8+i];
  1007. b3 = blockData[2*8+i] + blockData[6*8+i];
  1008. b4 = blockData[5*8+i] - blockData[3*8+i];
  1009. tmp1 = blockData[1*8+i] + blockData[7*8+i];
  1010. tmp2 = blockData[3*8+i] + blockData[5*8+i];
  1011. b6 = blockData[1*8+i] - blockData[7*8+i];
  1012. b7 = tmp1 + tmp2;
  1013. m0 = blockData[0*8+i];
  1014. x4 = ((b6*473 - b4*196 + 128) >> 8) - b7;
  1015. x0 = x4 - (((tmp1 - tmp2)*362 + 128) >> 8);
  1016. x1 = m0 - b1;
  1017. x2 = (((blockData[2*8+i] - blockData[6*8+i])*362 + 128) >> 8) - b3;
  1018. x3 = m0 + b1;
  1019. y3 = x1 + x2;
  1020. y4 = x3 + b3;
  1021. y5 = x1 - x2;
  1022. y6 = x3 - b3;
  1023. y7 = -x0 - ((b4*473 + b6*196 + 128) >> 8);
  1024. blockData[0*8+i] = b7 + y4;
  1025. blockData[1*8+i] = x4 + y3;
  1026. blockData[2*8+i] = y5 - x0;
  1027. blockData[3*8+i] = y6 - y7;
  1028. blockData[4*8+i] = y6 + y7;
  1029. blockData[5*8+i] = x0 + y5;
  1030. blockData[6*8+i] = y3 - x4;
  1031. blockData[7*8+i] = y4 - b7;
  1032. }
  1033. // Transform rows
  1034. for( i = 0; i < 64; i += 8 ) {
  1035. b1 = blockData[4+i];
  1036. b3 = blockData[2+i] + blockData[6+i];
  1037. b4 = blockData[5+i] - blockData[3+i];
  1038. tmp1 = blockData[1+i] + blockData[7+i];
  1039. tmp2 = blockData[3+i] + blockData[5+i];
  1040. b6 = blockData[1+i] - blockData[7+i];
  1041. b7 = tmp1 + tmp2;
  1042. m0 = blockData[0+i];
  1043. x4 = ((b6*473 - b4*196 + 128) >> 8) - b7;
  1044. x0 = x4 - (((tmp1 - tmp2)*362 + 128) >> 8);
  1045. x1 = m0 - b1;
  1046. x2 = (((blockData[2+i] - blockData[6+i])*362 + 128) >> 8) - b3;
  1047. x3 = m0 + b1;
  1048. y3 = x1 + x2;
  1049. y4 = x3 + b3;
  1050. y5 = x1 - x2;
  1051. y6 = x3 - b3;
  1052. y7 = -x0 - ((b4*473 + b6*196 + 128) >> 8);
  1053. blockData[0+i] = (b7 + y4 + 128) >> 8;
  1054. blockData[1+i] = (x4 + y3 + 128) >> 8;
  1055. blockData[2+i] = (y5 - x0 + 128) >> 8;
  1056. blockData[3+i] = (y6 - y7 + 128) >> 8;
  1057. blockData[4+i] = (y6 + y7 + 128) >> 8;
  1058. blockData[5+i] = (x0 + y5 + 128) >> 8;
  1059. blockData[6+i] = (y3 - x4 + 128) >> 8;
  1060. blockData[7+i] = (y4 - b7 + 128) >> 8;
  1061. }
  1062. };
  1063. // ----------------------------------------------------------------------------
  1064. // VLC Tables and Constants
  1065. var
  1066. PICTURE_RATE = [
  1067. 0.000, 23.976, 24.000, 25.000, 29.970, 30.000, 50.000, 59.940,
  1068. 60.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
  1069. ],
  1070. ZIG_ZAG = new Uint8Array([
  1071. 0, 1, 8, 16, 9, 2, 3, 10,
  1072. 17, 24, 32, 25, 18, 11, 4, 5,
  1073. 12, 19, 26, 33, 40, 48, 41, 34,
  1074. 27, 20, 13, 6, 7, 14, 21, 28,
  1075. 35, 42, 49, 56, 57, 50, 43, 36,
  1076. 29, 22, 15, 23, 30, 37, 44, 51,
  1077. 58, 59, 52, 45, 38, 31, 39, 46,
  1078. 53, 60, 61, 54, 47, 55, 62, 63
  1079. ]),
  1080. DEFAULT_INTRA_QUANT_MATRIX = new Uint8Array([
  1081. 8, 16, 19, 22, 26, 27, 29, 34,
  1082. 16, 16, 22, 24, 27, 29, 34, 37,
  1083. 19, 22, 26, 27, 29, 34, 34, 38,
  1084. 22, 22, 26, 27, 29, 34, 37, 40,
  1085. 22, 26, 27, 29, 32, 35, 40, 48,
  1086. 26, 27, 29, 32, 35, 40, 48, 58,
  1087. 26, 27, 29, 34, 38, 46, 56, 69,
  1088. 27, 29, 35, 38, 46, 56, 69, 83
  1089. ]),
  1090. DEFAULT_NON_INTRA_QUANT_MATRIX = new Uint8Array([
  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. 16, 16, 16, 16, 16, 16, 16, 16,
  1097. 16, 16, 16, 16, 16, 16, 16, 16,
  1098. 16, 16, 16, 16, 16, 16, 16, 16
  1099. ]),
  1100. PREMULTIPLIER_MATRIX = new Uint8Array([
  1101. 32, 44, 42, 38, 32, 25, 17, 9,
  1102. 44, 62, 58, 52, 44, 35, 24, 12,
  1103. 42, 58, 55, 49, 42, 33, 23, 12,
  1104. 38, 52, 49, 44, 38, 30, 20, 10,
  1105. 32, 44, 42, 38, 32, 25, 17, 9,
  1106. 25, 35, 33, 30, 25, 20, 14, 7,
  1107. 17, 24, 23, 20, 17, 14, 9, 5,
  1108. 9, 12, 12, 10, 9, 7, 5, 2
  1109. ]),
  1110. // MPEG-1 VLC
  1111. // macroblock_stuffing decodes as 34.
  1112. // macroblock_escape decodes as 35.
  1113. MACROBLOCK_ADDRESS_INCREMENT = new Int16Array([
  1114. 1*3, 2*3, 0, // 0
  1115. 3*3, 4*3, 0, // 1 0
  1116. 0, 0, 1, // 2 1.
  1117. 5*3, 6*3, 0, // 3 00
  1118. 7*3, 8*3, 0, // 4 01
  1119. 9*3, 10*3, 0, // 5 000
  1120. 11*3, 12*3, 0, // 6 001
  1121. 0, 0, 3, // 7 010.
  1122. 0, 0, 2, // 8 011.
  1123. 13*3, 14*3, 0, // 9 0000
  1124. 15*3, 16*3, 0, // 10 0001
  1125. 0, 0, 5, // 11 0010.
  1126. 0, 0, 4, // 12 0011.
  1127. 17*3, 18*3, 0, // 13 0000 0
  1128. 19*3, 20*3, 0, // 14 0000 1
  1129. 0, 0, 7, // 15 0001 0.
  1130. 0, 0, 6, // 16 0001 1.
  1131. 21*3, 22*3, 0, // 17 0000 00
  1132. 23*3, 24*3, 0, // 18 0000 01
  1133. 25*3, 26*3, 0, // 19 0000 10
  1134. 27*3, 28*3, 0, // 20 0000 11
  1135. -1, 29*3, 0, // 21 0000 000
  1136. -1, 30*3, 0, // 22 0000 001
  1137. 31*3, 32*3, 0, // 23 0000 010
  1138. 33*3, 34*3, 0, // 24 0000 011
  1139. 35*3, 36*3, 0, // 25 0000 100
  1140. 37*3, 38*3, 0, // 26 0000 101
  1141. 0, 0, 9, // 27 0000 110.
  1142. 0, 0, 8, // 28 0000 111.
  1143. 39*3, 40*3, 0, // 29 0000 0001
  1144. 41*3, 42*3, 0, // 30 0000 0011
  1145. 43*3, 44*3, 0, // 31 0000 0100
  1146. 45*3, 46*3, 0, // 32 0000 0101
  1147. 0, 0, 15, // 33 0000 0110.
  1148. 0, 0, 14, // 34 0000 0111.
  1149. 0, 0, 13, // 35 0000 1000.
  1150. 0, 0, 12, // 36 0000 1001.
  1151. 0, 0, 11, // 37 0000 1010.
  1152. 0, 0, 10, // 38 0000 1011.
  1153. 47*3, -1, 0, // 39 0000 0001 0
  1154. -1, 48*3, 0, // 40 0000 0001 1
  1155. 49*3, 50*3, 0, // 41 0000 0011 0
  1156. 51*3, 52*3, 0, // 42 0000 0011 1
  1157. 53*3, 54*3, 0, // 43 0000 0100 0
  1158. 55*3, 56*3, 0, // 44 0000 0100 1
  1159. 57*3, 58*3, 0, // 45 0000 0101 0
  1160. 59*3, 60*3, 0, // 46 0000 0101 1
  1161. 61*3, -1, 0, // 47 0000 0001 00
  1162. -1, 62*3, 0, // 48 0000 0001 11
  1163. 63*3, 64*3, 0, // 49 0000 0011 00
  1164. 65*3, 66*3, 0, // 50 0000 0011 01
  1165. 67*3, 68*3, 0, // 51 0000 0011 10
  1166. 69*3, 70*3, 0, // 52 0000 0011 11
  1167. 71*3, 72*3, 0, // 53 0000 0100 00
  1168. 73*3, 74*3, 0, // 54 0000 0100 01
  1169. 0, 0, 21, // 55 0000 0100 10.
  1170. 0, 0, 20, // 56 0000 0100 11.
  1171. 0, 0, 19, // 57 0000 0101 00.
  1172. 0, 0, 18, // 58 0000 0101 01.
  1173. 0, 0, 17, // 59 0000 0101 10.
  1174. 0, 0, 16, // 60 0000 0101 11.
  1175. 0, 0, 35, // 61 0000 0001 000. -- macroblock_escape
  1176. 0, 0, 34, // 62 0000 0001 111. -- macroblock_stuffing
  1177. 0, 0, 33, // 63 0000 0011 000.
  1178. 0, 0, 32, // 64 0000 0011 001.
  1179. 0, 0, 31, // 65 0000 0011 010.
  1180. 0, 0, 30, // 66 0000 0011 011.
  1181. 0, 0, 29, // 67 0000 0011 100.
  1182. 0, 0, 28, // 68 0000 0011 101.
  1183. 0, 0, 27, // 69 0000 0011 110.
  1184. 0, 0, 26, // 70 0000 0011 111.
  1185. 0, 0, 25, // 71 0000 0100 000.
  1186. 0, 0, 24, // 72 0000 0100 001.
  1187. 0, 0, 23, // 73 0000 0100 010.
  1188. 0, 0, 22 // 74 0000 0100 011.
  1189. ]),
  1190. // macroblock_type bitmap:
  1191. // 0x10 macroblock_quant
  1192. // 0x08 macroblock_motion_forward
  1193. // 0x04 macroblock_motion_backward
  1194. // 0x02 macrobkock_pattern
  1195. // 0x01 macroblock_intra
  1196. //
  1197. MACROBLOCK_TYPE_I = new Int8Array([
  1198. 1*3, 2*3, 0, // 0
  1199. -1, 3*3, 0, // 1 0
  1200. 0, 0, 0x01, // 2 1.
  1201. 0, 0, 0x11 // 3 01.
  1202. ]),
  1203. MACROBLOCK_TYPE_P = new Int8Array([
  1204. 1*3, 2*3, 0, // 0
  1205. 3*3, 4*3, 0, // 1 0
  1206. 0, 0, 0x0a, // 2 1.
  1207. 5*3, 6*3, 0, // 3 00
  1208. 0, 0, 0x02, // 4 01.
  1209. 7*3, 8*3, 0, // 5 000
  1210. 0, 0, 0x08, // 6 001.
  1211. 9*3, 10*3, 0, // 7 0000
  1212. 11*3, 12*3, 0, // 8 0001
  1213. -1, 13*3, 0, // 9 00000
  1214. 0, 0, 0x12, // 10 00001.
  1215. 0, 0, 0x1a, // 11 00010.
  1216. 0, 0, 0x01, // 12 00011.
  1217. 0, 0, 0x11 // 13 000001.
  1218. ]),
  1219. MACROBLOCK_TYPE_B = new Int8Array([
  1220. 1*3, 2*3, 0, // 0
  1221. 3*3, 5*3, 0, // 1 0
  1222. 4*3, 6*3, 0, // 2 1
  1223. 8*3, 7*3, 0, // 3 00
  1224. 0, 0, 0x0c, // 4 10.
  1225. 9*3, 10*3, 0, // 5 01
  1226. 0, 0, 0x0e, // 6 11.
  1227. 13*3, 14*3, 0, // 7 001
  1228. 12*3, 11*3, 0, // 8 000
  1229. 0, 0, 0x04, // 9 010.
  1230. 0, 0, 0x06, // 10 011.
  1231. 18*3, 16*3, 0, // 11 0001
  1232. 15*3, 17*3, 0, // 12 0000
  1233. 0, 0, 0x08, // 13 0010.
  1234. 0, 0, 0x0a, // 14 0011.
  1235. -1, 19*3, 0, // 15 00000
  1236. 0, 0, 0x01, // 16 00011.
  1237. 20*3, 21*3, 0, // 17 00001
  1238. 0, 0, 0x1e, // 18 00010.
  1239. 0, 0, 0x11, // 19 000001.
  1240. 0, 0, 0x16, // 20 000010.
  1241. 0, 0, 0x1a // 21 000011.
  1242. ]),
  1243. CODE_BLOCK_PATTERN = new Int16Array([
  1244. 2*3, 1*3, 0, // 0
  1245. 3*3, 6*3, 0, // 1 1
  1246. 4*3, 5*3, 0, // 2 0
  1247. 8*3, 11*3, 0, // 3 10
  1248. 12*3, 13*3, 0, // 4 00
  1249. 9*3, 7*3, 0, // 5 01
  1250. 10*3, 14*3, 0, // 6 11
  1251. 20*3, 19*3, 0, // 7 011
  1252. 18*3, 16*3, 0, // 8 100
  1253. 23*3, 17*3, 0, // 9 010
  1254. 27*3, 25*3, 0, // 10 110
  1255. 21*3, 28*3, 0, // 11 101
  1256. 15*3, 22*3, 0, // 12 000
  1257. 24*3, 26*3, 0, // 13 001
  1258. 0, 0, 60, // 14 111.
  1259. 35*3, 40*3, 0, // 15 0000
  1260. 44*3, 48*3, 0, // 16 1001
  1261. 38*3, 36*3, 0, // 17 0101
  1262. 42*3, 47*3, 0, // 18 1000
  1263. 29*3, 31*3, 0, // 19 0111
  1264. 39*3, 32*3, 0, // 20 0110
  1265. 0, 0, 32, // 21 1010.
  1266. 45*3, 46*3, 0, // 22 0001
  1267. 33*3, 41*3, 0, // 23 0100
  1268. 43*3, 34*3, 0, // 24 0010
  1269. 0, 0, 4, // 25 1101.
  1270. 30*3, 37*3, 0, // 26 0011
  1271. 0, 0, 8, // 27 1100.
  1272. 0, 0, 16, // 28 1011.
  1273. 0, 0, 44, // 29 0111 0.
  1274. 50*3, 56*3, 0, // 30 0011 0
  1275. 0, 0, 28, // 31 0111 1.
  1276. 0, 0, 52, // 32 0110 1.
  1277. 0, 0, 62, // 33 0100 0.
  1278. 61*3, 59*3, 0, // 34 0010 1
  1279. 52*3, 60*3, 0, // 35 0000 0
  1280. 0, 0, 1, // 36 0101 1.
  1281. 55*3, 54*3, 0, // 37 0011 1
  1282. 0, 0, 61, // 38 0101 0.
  1283. 0, 0, 56, // 39 0110 0.
  1284. 57*3, 58*3, 0, // 40 0000 1
  1285. 0, 0, 2, // 41 0100 1.
  1286. 0, 0, 40, // 42 1000 0.
  1287. 51*3, 62*3, 0, // 43 0010 0
  1288. 0, 0, 48, // 44 1001 0.
  1289. 64*3, 63*3, 0, // 45 0001 0
  1290. 49*3, 53*3, 0, // 46 0001 1
  1291. 0, 0, 20, // 47 1000 1.
  1292. 0, 0, 12, // 48 1001 1.
  1293. 80*3, 83*3, 0, // 49 0001 10
  1294. 0, 0, 63, // 50 0011 00.
  1295. 77*3, 75*3, 0, // 51 0010 00
  1296. 65*3, 73*3, 0, // 52 0000 00
  1297. 84*3, 66*3, 0, // 53 0001 11
  1298. 0, 0, 24, // 54 0011 11.
  1299. 0, 0, 36, // 55 0011 10.
  1300. 0, 0, 3, // 56 0011 01.
  1301. 69*3, 87*3, 0, // 57 0000 10
  1302. 81*3, 79*3, 0, // 58 0000 11
  1303. 68*3, 71*3, 0, // 59 0010 11
  1304. 70*3, 78*3, 0, // 60 0000 01
  1305. 67*3, 76*3, 0, // 61 0010 10
  1306. 72*3, 74*3, 0, // 62 0010 01
  1307. 86*3, 85*3, 0, // 63 0001 01
  1308. 88*3, 82*3, 0, // 64 0001 00
  1309. -1, 94*3, 0, // 65 0000 000
  1310. 95*3, 97*3, 0, // 66 0001 111
  1311. 0, 0, 33, // 67 0010 100.
  1312. 0, 0, 9, // 68 0010 110.
  1313. 106*3, 110*3, 0, // 69 0000 100
  1314. 102*3, 116*3, 0, // 70 0000 010
  1315. 0, 0, 5, // 71 0010 111.
  1316. 0, 0, 10, // 72 0010 010.
  1317. 93*3, 89*3, 0, // 73 0000 001
  1318. 0, 0, 6, // 74 0010 011.
  1319. 0, 0, 18, // 75 0010 001.
  1320. 0, 0, 17, // 76 0010 101.
  1321. 0, 0, 34, // 77 0010 000.
  1322. 113*3, 119*3, 0, // 78 0000 011
  1323. 103*3, 104*3, 0, // 79 0000 111
  1324. 90*3, 92*3, 0, // 80 0001 100
  1325. 109*3, 107*3, 0, // 81 0000 110
  1326. 117*3, 118*3, 0, // 82 0001 001
  1327. 101*3, 99*3, 0, // 83 0001 101
  1328. 98*3, 96*3, 0, // 84 0001 110
  1329. 100*3, 91*3, 0, // 85 0001 011
  1330. 114*3, 115*3, 0, // 86 0001 010
  1331. 105*3, 108*3, 0, // 87 0000 101
  1332. 112*3, 111*3, 0, // 88 0001 000
  1333. 121*3, 125*3, 0, // 89 0000 0011
  1334. 0, 0, 41, // 90 0001 1000.
  1335. 0, 0, 14, // 91 0001 0111.
  1336. 0, 0, 21, // 92 0001 1001.
  1337. 124*3, 122*3, 0, // 93 0000 0010
  1338. 120*3, 123*3, 0, // 94 0000 0001
  1339. 0, 0, 11, // 95 0001 1110.
  1340. 0, 0, 19, // 96 0001 1101.
  1341. 0, 0, 7, // 97 0001 1111.
  1342. 0, 0, 35, // 98 0001 1100.
  1343. 0, 0, 13, // 99 0001 1011.
  1344. 0, 0, 50, // 100 0001 0110.
  1345. 0, 0, 49, // 101 0001 1010.
  1346. 0, 0, 58, // 102 0000 0100.
  1347. 0, 0, 37, // 103 0000 1110.
  1348. 0, 0, 25, // 104 0000 1111.
  1349. 0, 0, 45, // 105 0000 1010.
  1350. 0, 0, 57, // 106 0000 1000.
  1351. 0, 0, 26, // 107 0000 1101.
  1352. 0, 0, 29, // 108 0000 1011.
  1353. 0, 0, 38, // 109 0000 1100.
  1354. 0, 0, 53, // 110 0000 1001.
  1355. 0, 0, 23, // 111 0001 0001.
  1356. 0, 0, 43, // 112 0001 0000.
  1357. 0, 0, 46, // 113 0000 0110.
  1358. 0, 0, 42, // 114 0001 0100.
  1359. 0, 0, 22, // 115 0001 0101.
  1360. 0, 0, 54, // 116 0000 0101.
  1361. 0, 0, 51, // 117 0001 0010.
  1362. 0, 0, 15, // 118 0001 0011.
  1363. 0, 0, 30, // 119 0000 0111.
  1364. 0, 0, 39, // 120 0000 0001 0.
  1365. 0, 0, 47, // 121 0000 0011 0.
  1366. 0, 0, 55, // 122 0000 0010 1.
  1367. 0, 0, 27, // 123 0000 0001 1.
  1368. 0, 0, 59, // 124 0000 0010 0.
  1369. 0, 0, 31 // 125 0000 0011 1.
  1370. ]),
  1371. MOTION = new Int16Array([
  1372. 1*3, 2*3, 0, // 0
  1373. 4*3, 3*3, 0, // 1 0
  1374. 0, 0, 0, // 2 1.
  1375. 6*3, 5*3, 0, // 3 01
  1376. 8*3, 7*3, 0, // 4 00
  1377. 0, 0, -1, // 5 011.
  1378. 0, 0, 1, // 6 010.
  1379. 9*3, 10*3, 0, // 7 001
  1380. 12*3, 11*3, 0, // 8 000
  1381. 0, 0, 2, // 9 0010.
  1382. 0, 0, -2, // 10 0011.
  1383. 14*3, 15*3, 0, // 11 0001
  1384. 16*3, 13*3, 0, // 12 0000
  1385. 20*3, 18*3, 0, // 13 0000 1
  1386. 0, 0, 3, // 14 0001 0.
  1387. 0, 0, -3, // 15 0001 1.
  1388. 17*3, 19*3, 0, // 16 0000 0
  1389. -1, 23*3, 0, // 17 0000 00
  1390. 27*3, 25*3, 0, // 18 0000 11
  1391. 26*3, 21*3, 0, // 19 0000 01
  1392. 24*3, 22*3, 0, // 20 0000 10
  1393. 32*3, 28*3, 0, // 21 0000 011
  1394. 29*3, 31*3, 0, // 22 0000 101
  1395. -1, 33*3, 0, // 23 0000 001
  1396. 36*3, 35*3, 0, // 24 0000 100
  1397. 0, 0, -4, // 25 0000 111.
  1398. 30*3, 34*3, 0, // 26 0000 010
  1399. 0, 0, 4, // 27 0000 110.
  1400. 0, 0, -7, // 28 0000 0111.
  1401. 0, 0, 5, // 29 0000 1010.
  1402. 37*3, 41*3, 0, // 30 0000 0100
  1403. 0, 0, -5, // 31 0000 1011.
  1404. 0, 0, 7, // 32 0000 0110.
  1405. 38*3, 40*3, 0, // 33 0000 0011
  1406. 42*3, 39*3, 0, // 34 0000 0101
  1407. 0, 0, -6, // 35 0000 1001.
  1408. 0, 0, 6, // 36 0000 1000.
  1409. 51*3, 54*3, 0, // 37 0000 0100 0
  1410. 50*3, 49*3, 0, // 38 0000 0011 0
  1411. 45*3, 46*3, 0, // 39 0000 0101 1
  1412. 52*3, 47*3, 0, // 40 0000 0011 1
  1413. 43*3, 53*3, 0, // 41 0000 0100 1
  1414. 44*3, 48*3, 0, // 42 0000 0101 0
  1415. 0, 0, 10, // 43 0000 0100 10.
  1416. 0, 0, 9, // 44 0000 0101 00.
  1417. 0, 0, 8, // 45 0000 0101 10.
  1418. 0, 0, -8, // 46 0000 0101 11.
  1419. 57*3, 66*3, 0, // 47 0000 0011 11
  1420. 0, 0, -9, // 48 0000 0101 01.
  1421. 60*3, 64*3, 0, // 49 0000 0011 01
  1422. 56*3, 61*3, 0, // 50 0000 0011 00
  1423. 55*3, 62*3, 0, // 51 0000 0100 00
  1424. 58*3, 63*3, 0, // 52 0000 0011 10
  1425. 0, 0, -10, // 53 0000 0100 11.
  1426. 59*3, 65*3, 0, // 54 0000 0100 01
  1427. 0, 0, 12, // 55 0000 0100 000.
  1428. 0, 0, 16, // 56 0000 0011 000.
  1429. 0, 0, 13, // 57 0000 0011 110.
  1430. 0, 0, 14, // 58 0000 0011 100.
  1431. 0, 0, 11, // 59 0000 0100 010.
  1432. 0, 0, 15, // 60 0000 0011 010.
  1433. 0, 0, -16, // 61 0000 0011 001.
  1434. 0, 0, -12, // 62 0000 0100 001.
  1435. 0, 0, -14, // 63 0000 0011 101.
  1436. 0, 0, -15, // 64 0000 0011 011.
  1437. 0, 0, -11, // 65 0000 0100 011.
  1438. 0, 0, -13 // 66 0000 0011 111.
  1439. ]),
  1440. DCT_DC_SIZE_LUMINANCE = new Int8Array([
  1441. 2*3, 1*3, 0, // 0
  1442. 6*3, 5*3, 0, // 1 1
  1443. 3*3, 4*3, 0, // 2 0
  1444. 0, 0, 1, // 3 00.
  1445. 0, 0, 2, // 4 01.
  1446. 9*3, 8*3, 0, // 5 11
  1447. 7*3, 10*3, 0, // 6 10
  1448. 0, 0, 0, // 7 100.
  1449. 12*3, 11*3, 0, // 8 111
  1450. 0, 0, 4, // 9 110.
  1451. 0, 0, 3, // 10 101.
  1452. 13*3, 14*3, 0, // 11 1111
  1453. 0, 0, 5, // 12 1110.
  1454. 0, 0, 6, // 13 1111 0.
  1455. 16*3, 15*3, 0, // 14 1111 1
  1456. 17*3, -1, 0, // 15 1111 11
  1457. 0, 0, 7, // 16 1111 10.
  1458. 0, 0, 8 // 17 1111 110.
  1459. ]),
  1460. DCT_DC_SIZE_CHROMINANCE = new Int8Array([
  1461. 2*3, 1*3, 0, // 0
  1462. 4*3, 3*3, 0, // 1 1
  1463. 6*3, 5*3, 0, // 2 0
  1464. 8*3, 7*3, 0, // 3 11
  1465. 0, 0, 2, // 4 10.
  1466. 0, 0, 1, // 5 01.
  1467. 0, 0, 0, // 6 00.
  1468. 10*3, 9*3, 0, // 7 111
  1469. 0, 0, 3, // 8 110.
  1470. 12*3, 11*3, 0, // 9 1111
  1471. 0, 0, 4, // 10 1110.
  1472. 14*3, 13*3, 0, // 11 1111 1
  1473. 0, 0, 5, // 12 1111 0.
  1474. 16*3, 15*3, 0, // 13 1111 11
  1475. 0, 0, 6, // 14 1111 10.
  1476. 17*3, -1, 0, // 15 1111 111
  1477. 0, 0, 7, // 16 1111 110.
  1478. 0, 0, 8 // 17 1111 1110.
  1479. ]),
  1480. // dct_coeff bitmap:
  1481. // 0xff00 run
  1482. // 0x00ff level
  1483. // Decoded values are unsigned. Sign bit follows in the stream.
  1484. // Interpretation of the value 0x0001
  1485. // for dc_coeff_first: run=0, level=1
  1486. // for dc_coeff_next: If the next bit is 1: run=0, level=1
  1487. // If the next bit is 0: end_of_block
  1488. // escape decodes as 0xffff.
  1489. DCT_COEFF = new Int32Array([
  1490. 1*3, 2*3, 0, // 0
  1491. 4*3, 3*3, 0, // 1 0
  1492. 0, 0, 0x0001, // 2 1.
  1493. 7*3, 8*3, 0, // 3 01
  1494. 6*3, 5*3, 0, // 4 00
  1495. 13*3, 9*3, 0, // 5 001
  1496. 11*3, 10*3, 0, // 6 000
  1497. 14*3, 12*3, 0, // 7 010
  1498. 0, 0, 0x0101, // 8 011.
  1499. 20*3, 22*3, 0, // 9 0011
  1500. 18*3, 21*3, 0, // 10 0001
  1501. 16*3, 19*3, 0, // 11 0000
  1502. 0, 0, 0x0201, // 12 0101.
  1503. 17*3, 15*3, 0, // 13 0010
  1504. 0, 0, 0x0002, // 14 0100.
  1505. 0, 0, 0x0003, // 15 0010 1.
  1506. 27*3, 25*3, 0, // 16 0000 0
  1507. 29*3, 31*3, 0, // 17 0010 0
  1508. 24*3, 26*3, 0, // 18 0001 0
  1509. 32*3, 30*3, 0, // 19 0000 1
  1510. 0, 0, 0x0401, // 20 0011 0.
  1511. 23*3, 28*3, 0, // 21 0001 1
  1512. 0, 0, 0x0301, // 22 0011 1.
  1513. 0, 0, 0x0102, // 23 0001 10.
  1514. 0, 0, 0x0701, // 24 0001 00.
  1515. 0, 0, 0xffff, // 25 0000 01. -- escape
  1516. 0, 0, 0x0601, // 26 0001 01.
  1517. 37*3, 36*3, 0, // 27 0000 00
  1518. 0, 0, 0x0501, // 28 0001 11.
  1519. 35*3, 34*3, 0, // 29 0010 00
  1520. 39*3, 38*3, 0, // 30 0000 11
  1521. 33*3, 42*3, 0, // 31 0010 01
  1522. 40*3, 41*3, 0, // 32 0000 10
  1523. 52*3, 50*3, 0, // 33 0010 010
  1524. 54*3, 53*3, 0, // 34 0010 001
  1525. 48*3, 49*3, 0, // 35 0010 000
  1526. 43*3, 45*3, 0, // 36 0000 001
  1527. 46*3, 44*3, 0, // 37 0000 000
  1528. 0, 0, 0x0801, // 38 0000 111.
  1529. 0, 0, 0x0004, // 39 0000 110.
  1530. 0, 0, 0x0202, // 40 0000 100.
  1531. 0, 0, 0x0901, // 41 0000 101.
  1532. 51*3, 47*3, 0, // 42 0010 011
  1533. 55*3, 57*3, 0, // 43 0000 0010
  1534. 60*3, 56*3, 0, // 44 0000 0001
  1535. 59*3, 58*3, 0, // 45 0000 0011
  1536. 61*3, 62*3, 0, // 46 0000 0000
  1537. 0, 0, 0x0a01, // 47 0010 0111.
  1538. 0, 0, 0x0d01, // 48 0010 0000.
  1539. 0, 0, 0x0006, // 49 0010 0001.
  1540. 0, 0, 0x0103, // 50 0010 0101.
  1541. 0, 0, 0x0005, // 51 0010 0110.
  1542. 0, 0, 0x0302, // 52 0010 0100.
  1543. 0, 0, 0x0b01, // 53 0010 0011.
  1544. 0, 0, 0x0c01, // 54 0010 0010.
  1545. 76*3, 75*3, 0, // 55 0000 0010 0
  1546. 67*3, 70*3, 0, // 56 0000 0001 1
  1547. 73*3, 71*3, 0, // 57 0000 0010 1
  1548. 78*3, 74*3, 0, // 58 0000 0011 1
  1549. 72*3, 77*3, 0, // 59 0000 0011 0
  1550. 69*3, 64*3, 0, // 60 0000 0001 0
  1551. 68*3, 63*3, 0, // 61 0000 0000 0
  1552. 66*3, 65*3, 0, // 62 0000 0000 1
  1553. 81*3, 87*3, 0, // 63 0000 0000 01
  1554. 91*3, 80*3, 0, // 64 0000 0001 01
  1555. 82*3, 79*3, 0, // 65 0000 0000 11
  1556. 83*3, 86*3, 0, // 66 0000 0000 10
  1557. 93*3, 92*3, 0, // 67 0000 0001 10
  1558. 84*3, 85*3, 0, // 68 0000 0000 00
  1559. 90*3, 94*3, 0, // 69 0000 0001 00
  1560. 88*3, 89*3, 0, // 70 0000 0001 11
  1561. 0, 0, 0x0203, // 71 0000 0010 11.
  1562. 0, 0, 0x0104, // 72 0000 0011 00.
  1563. 0, 0, 0x0007, // 73 0000 0010 10.
  1564. 0, 0, 0x0402, // 74 0000 0011 11.
  1565. 0, 0, 0x0502, // 75 0000 0010 01.
  1566. 0, 0, 0x1001, // 76 0000 0010 00.
  1567. 0, 0, 0x0f01, // 77 0000 0011 01.
  1568. 0, 0, 0x0e01, // 78 0000 0011 10.
  1569. 105*3, 107*3, 0, // 79 0000 0000 111
  1570. 111*3, 114*3, 0, // 80 0000 0001 011
  1571. 104*3, 97*3, 0, // 81 0000 0000 010
  1572. 125*3, 119*3, 0, // 82 0000 0000 110
  1573. 96*3, 98*3, 0, // 83 0000 0000 100
  1574. -1, 123*3, 0, // 84 0000 0000 000
  1575. 95*3, 101*3, 0, // 85 0000 0000 001
  1576. 106*3, 121*3, 0, // 86 0000 0000 101
  1577. 99*3, 102*3, 0, // 87 0000 0000 011
  1578. 113*3, 103*3, 0, // 88 0000 0001 110
  1579. 112*3, 116*3, 0, // 89 0000 0001 111
  1580. 110*3, 100*3, 0, // 90 0000 0001 000
  1581. 124*3, 115*3, 0, // 91 0000 0001 010
  1582. 117*3, 122*3, 0, // 92 0000 0001 101
  1583. 109*3, 118*3, 0, // 93 0000 0001 100
  1584. 120*3, 108*3, 0, // 94 0000 0001 001
  1585. 127*3, 136*3, 0, // 95 0000 0000 0010
  1586. 139*3, 140*3, 0, // 96 0000 0000 1000
  1587. 130*3, 126*3, 0, // 97 0000 0000 0101
  1588. 145*3, 146*3, 0, // 98 0000 0000 1001
  1589. 128*3, 129*3, 0, // 99 0000 0000 0110
  1590. 0, 0, 0x0802, // 100 0000 0001 0001.
  1591. 132*3, 134*3, 0, // 101 0000 0000 0011
  1592. 155*3, 154*3, 0, // 102 0000 0000 0111
  1593. 0, 0, 0x0008, // 103 0000 0001 1101.
  1594. 137*3, 133*3, 0, // 104 0000 0000 0100
  1595. 143*3, 144*3, 0, // 105 0000 0000 1110
  1596. 151*3, 138*3, 0, // 106 0000 0000 1010
  1597. 142*3, 141*3, 0, // 107 0000 0000 1111
  1598. 0, 0, 0x000a, // 108 0000 0001 0011.
  1599. 0, 0, 0x0009, // 109 0000 0001 1000.
  1600. 0, 0, 0x000b, // 110 0000 0001 0000.
  1601. 0, 0, 0x1501, // 111 0000 0001 0110.
  1602. 0, 0, 0x0602, // 112 0000 0001 1110.
  1603. 0, 0, 0x0303, // 113 0000 0001 1100.
  1604. 0, 0, 0x1401, // 114 0000 0001 0111.
  1605. 0, 0, 0x0702, // 115 0000 0001 0101.
  1606. 0, 0, 0x1101, // 116 0000 0001 1111.
  1607. 0, 0, 0x1201, // 117 0000 0001 1010.
  1608. 0, 0, 0x1301, // 118 0000 0001 1001.
  1609. 148*3, 152*3, 0, // 119 0000 0000 1101
  1610. 0, 0, 0x0403, // 120 0000 0001 0010.
  1611. 153*3, 150*3, 0, // 121 0000 0000 1011
  1612. 0, 0, 0x0105, // 122 0000 0001 1011.
  1613. 131*3, 135*3, 0, // 123 0000 0000 0001
  1614. 0, 0, 0x0204, // 124 0000 0001 0100.
  1615. 149*3, 147*3, 0, // 125 0000 0000 1100
  1616. 172*3, 173*3, 0, // 126 0000 0000 0101 1
  1617. 162*3, 158*3, 0, // 127 0000 0000 0010 0
  1618. 170*3, 161*3, 0, // 128 0000 0000 0110 0
  1619. 168*3, 166*3, 0, // 129 0000 0000 0110 1
  1620. 157*3, 179*3, 0, // 130 0000 0000 0101 0
  1621. 169*3, 167*3, 0, // 131 0000 0000 0001 0
  1622. 174*3, 171*3, 0, // 132 0000 0000 0011 0
  1623. 178*3, 177*3, 0, // 133 0000 0000 0100 1
  1624. 156*3, 159*3, 0, // 134 0000 0000 0011 1
  1625. 164*3, 165*3, 0, // 135 0000 0000 0001 1
  1626. 183*3, 182*3, 0, // 136 0000 0000 0010 1
  1627. 175*3, 176*3, 0, // 137 0000 0000 0100 0
  1628. 0, 0, 0x0107, // 138 0000 0000 1010 1.
  1629. 0, 0, 0x0a02, // 139 0000 0000 1000 0.
  1630. 0, 0, 0x0902, // 140 0000 0000 1000 1.
  1631. 0, 0, 0x1601, // 141 0000 0000 1111 1.
  1632. 0, 0, 0x1701, // 142 0000 0000 1111 0.
  1633. 0, 0, 0x1901, // 143 0000 0000 1110 0.
  1634. 0, 0, 0x1801, // 144 0000 0000 1110 1.
  1635. 0, 0, 0x0503, // 145 0000 0000 1001 0.
  1636. 0, 0, 0x0304, // 146 0000 0000 1001 1.
  1637. 0, 0, 0x000d, // 147 0000 0000 1100 1.
  1638. 0, 0, 0x000c, // 148 0000 0000 1101 0.
  1639. 0, 0, 0x000e, // 149 0000 0000 1100 0.
  1640. 0, 0, 0x000f, // 150 0000 0000 1011 1.
  1641. 0, 0, 0x0205, // 151 0000 0000 1010 0.
  1642. 0, 0, 0x1a01, // 152 0000 0000 1101 1.
  1643. 0, 0, 0x0106, // 153 0000 0000 1011 0.
  1644. 180*3, 181*3, 0, // 154 0000 0000 0111 1
  1645. 160*3, 163*3, 0, // 155 0000 0000 0111 0
  1646. 196*3, 199*3, 0, // 156 0000 0000 0011 10
  1647. 0, 0, 0x001b, // 157 0000 0000 0101 00.
  1648. 203*3, 185*3, 0, // 158 0000 0000 0010 01
  1649. 202*3, 201*3, 0, // 159 0000 0000 0011 11
  1650. 0, 0, 0x0013, // 160 0000 0000 0111 00.
  1651. 0, 0, 0x0016, // 161 0000 0000 0110 01.
  1652. 197*3, 207*3, 0, // 162 0000 0000 0010 00
  1653. 0, 0, 0x0012, // 163 0000 0000 0111 01.
  1654. 191*3, 192*3, 0, // 164 0000 0000 0001 10
  1655. 188*3, 190*3, 0, // 165 0000 0000 0001 11
  1656. 0, 0, 0x0014, // 166 0000 0000 0110 11.
  1657. 184*3, 194*3, 0, // 167 0000 0000 0001 01
  1658. 0, 0, 0x0015, // 168 0000 0000 0110 10.
  1659. 186*3, 193*3, 0, // 169 0000 0000 0001 00
  1660. 0, 0, 0x0017, // 170 0000 0000 0110 00.
  1661. 204*3, 198*3, 0, // 171 0000 0000 0011 01
  1662. 0, 0, 0x0019, // 172 0000 0000 0101 10.
  1663. 0, 0, 0x0018, // 173 0000 0000 0101 11.
  1664. 200*3, 205*3, 0, // 174 0000 0000 0011 00
  1665. 0, 0, 0x001f, // 175 0000 0000 0100 00.
  1666. 0, 0, 0x001e, // 176 0000 0000 0100 01.
  1667. 0, 0, 0x001c, // 177 0000 0000 0100 11.
  1668. 0, 0, 0x001d, // 178 0000 0000 0100 10.
  1669. 0, 0, 0x001a, // 179 0000 0000 0101 01.
  1670. 0, 0, 0x0011, // 180 0000 0000 0111 10.
  1671. 0, 0, 0x0010, // 181 0000 0000 0111 11.
  1672. 189*3, 206*3, 0, // 182 0000 0000 0010 11
  1673. 187*3, 195*3, 0, // 183 0000 0000 0010 10
  1674. 218*3, 211*3, 0, // 184 0000 0000 0001 010
  1675. 0, 0, 0x0025, // 185 0000 0000 0010 011.
  1676. 215*3, 216*3, 0, // 186 0000 0000 0001 000
  1677. 0, 0, 0x0024, // 187 0000 0000 0010 100.
  1678. 210*3, 212*3, 0, // 188 0000 0000 0001 110
  1679. 0, 0, 0x0022, // 189 0000 0000 0010 110.
  1680. 213*3, 209*3, 0, // 190 0000 0000 0001 111
  1681. 221*3, 222*3, 0, // 191 0000 0000 0001 100
  1682. 219*3, 208*3, 0, // 192 0000 0000 0001 101
  1683. 217*3, 214*3, 0, // 193 0000 0000 0001 001
  1684. 223*3, 220*3, 0, // 194 0000 0000 0001 011
  1685. 0, 0, 0x0023, // 195 0000 0000 0010 101.
  1686. 0, 0, 0x010b, // 196 0000 0000 0011 100.
  1687. 0, 0, 0x0028, // 197 0000 0000 0010 000.
  1688. 0, 0, 0x010c, // 198 0000 0000 0011 011.
  1689. 0, 0, 0x010a, // 199 0000 0000 0011 101.
  1690. 0, 0, 0x0020, // 200 0000 0000 0011 000.
  1691. 0, 0, 0x0108, // 201 0000 0000 0011 111.
  1692. 0, 0, 0x0109, // 202 0000 0000 0011 110.
  1693. 0, 0, 0x0026, // 203 0000 0000 0010 010.
  1694. 0, 0, 0x010d, // 204 0000 0000 0011 010.
  1695. 0, 0, 0x010e, // 205 0000 0000 0011 001.
  1696. 0, 0, 0x0021, // 206 0000 0000 0010 111.
  1697. 0, 0, 0x0027, // 207 0000 0000 0010 001.
  1698. 0, 0, 0x1f01, // 208 0000 0000 0001 1011.
  1699. 0, 0, 0x1b01, // 209 0000 0000 0001 1111.
  1700. 0, 0, 0x1e01, // 210 0000 0000 0001 1100.
  1701. 0, 0, 0x1002, // 211 0000 0000 0001 0101.
  1702. 0, 0, 0x1d01, // 212 0000 0000 0001 1101.
  1703. 0, 0, 0x1c01, // 213 0000 0000 0001 1110.
  1704. 0, 0, 0x010f, // 214 0000 0000 0001 0011.
  1705. 0, 0, 0x0112, // 215 0000 0000 0001 0000.
  1706. 0, 0, 0x0111, // 216 0000 0000 0001 0001.
  1707. 0, 0, 0x0110, // 217 0000 0000 0001 0010.
  1708. 0, 0, 0x0603, // 218 0000 0000 0001 0100.
  1709. 0, 0, 0x0b02, // 219 0000 0000 0001 1010.
  1710. 0, 0, 0x0e02, // 220 0000 0000 0001 0111.
  1711. 0, 0, 0x0d02, // 221 0000 0000 0001 1000.
  1712. 0, 0, 0x0c02, // 222 0000 0000 0001 1001.
  1713. 0, 0, 0x0f02 // 223 0000 0000 0001 0110.
  1714. ]),
  1715. PICTURE_TYPE_I = 1,
  1716. PICTURE_TYPE_P = 2,
  1717. PICTURE_TYPE_B = 3,
  1718. PICTURE_TYPE_D = 4,
  1719. START_SEQUENCE = 0xB3,
  1720. START_SLICE_FIRST = 0x01,
  1721. START_SLICE_LAST = 0xAF,
  1722. START_PICTURE = 0x00,
  1723. START_EXTENSION = 0xB5,
  1724. START_USER_DATA = 0xB2;
  1725. var MACROBLOCK_TYPE_TABLES = [
  1726. null,
  1727. MACROBLOCK_TYPE_I,
  1728. MACROBLOCK_TYPE_P,
  1729. MACROBLOCK_TYPE_B
  1730. ];
  1731. // ----------------------------------------------------------------------------
  1732. // Bit Reader
  1733. var BIT_MASK = new Uint32Array([
  1734. 0x00000000, 0x00000001, 0x00000003, 0x00000007,
  1735. 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
  1736. 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
  1737. 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
  1738. 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
  1739. 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
  1740. 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
  1741. 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
  1742. 0xffffffff
  1743. ]);
  1744. var BitReader = function(arrayBuffer) {
  1745. this.bytes = new Uint8Array(arrayBuffer);
  1746. this.length = this.bytes.length;
  1747. this.index = 0;
  1748. };
  1749. BitReader.NOT_FOUND = -1;
  1750. BitReader.prototype.findNextMPEGStartCode = function() {
  1751. for( var i = (this.index+7 >> 3); i < this.length; i++ ) {
  1752. if(
  1753. this.bytes[i] == 0x00 &&
  1754. this.bytes[i+1] == 0x00 &&
  1755. this.bytes[i+2] == 0x01
  1756. ) {
  1757. this.index = (i+4) << 3;
  1758. return this.bytes[i+3];
  1759. }
  1760. }
  1761. this.index = (this.length << 3);
  1762. return BitReader.NOT_FOUND;
  1763. };
  1764. BitReader.prototype.nextBytesAreStartCode = function() {
  1765. var i = (this.index+7 >> 3);
  1766. return (
  1767. i >= this.length || (
  1768. this.bytes[i] == 0x00 &&
  1769. this.bytes[i+1] == 0x00 &&
  1770. this.bytes[i+2] == 0x01
  1771. )
  1772. );
  1773. };
  1774. BitReader.prototype.nextBits = function(count) {
  1775. var
  1776. byteOffset = this.index >> 3,
  1777. room = (8 - this.index % 8);
  1778. if( room >= count ) {
  1779. return (this.bytes[byteOffset] >> (room - count)) & BIT_MASK[count];
  1780. }
  1781. var
  1782. leftover = (this.index + count) % 8, // Leftover bits in last byte
  1783. end = (this.index + count -1) >> 3,
  1784. value = this.bytes[byteOffset] & BIT_MASK[room]; // Fill out first byte
  1785. for( byteOffset++; byteOffset < end; byteOffset++ ) {
  1786. value <<= 8; // Shift and
  1787. value |= this.bytes[byteOffset] & BIT_MASK[8]; // Put next byte
  1788. }
  1789. if (leftover > 0) {
  1790. value <<= leftover; // Make room for remaining bits
  1791. value |= (this.bytes[byteOffset] >> (8 - leftover)) & BIT_MASK[leftover];
  1792. }
  1793. else {
  1794. value <<= 8;
  1795. value |= this.bytes[byteOffset] & BIT_MASK[8];
  1796. }
  1797. return value;
  1798. };
  1799. BitReader.prototype.getBits = function(count) {
  1800. var value = this.nextBits(count);
  1801. this.index += count;
  1802. return value;
  1803. };
  1804. BitReader.prototype.advance = function(count) {
  1805. return (this.index += count);
  1806. };
  1807. BitReader.prototype.rewind = function(count) {
  1808. return (this.index -= count);
  1809. };
  1810. })(window);