jsmpg.js 77KB

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