jsmpg.js 76KB

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