jsmpg.js 68KB

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