jsmpg.js 78KB

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