jsmpg.js 78KB

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