jsmpg.js 65KB

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