jsmpg.js 64KB

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