jsmpg.js 78KB

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