jsmpg.js 74KB

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