jsmpg.js 65KB

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