jsmpg.js 65KB

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