jsmpg.js 71KB

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