jsmpg.js 73KB

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