jsmpg.js 74KB

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