jsmpg.js 65KB

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