jsmpg.js 74KB

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