jsmpg.js 76KB

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