jsmpg.js 73KB

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