jsmpg.js 79KB

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