jsmpg.js 76KB

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