jsmpg.js 76KB

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