daa419b4 by David LaPalomento

Tweak the segment parser to avoid shifts

The difference isn't particularly significant but we can get away with a couple fewer shifts when parsing TS packets. Also, avoid an additional branch by default the dts value to the calculated pts.
1 parent cfb6c419
...@@ -289,7 +289,7 @@ ...@@ -289,7 +289,7 @@
289 // var sid:int = data[offset+3]; // StreamID 289 // var sid:int = data[offset+3]; // StreamID
290 pesPacketSize = (data[offset + 4] << 8) | data[offset + 5]; 290 pesPacketSize = (data[offset + 4] << 8) | data[offset + 5];
291 dataAlignmentIndicator = (data[offset + 6] & 0x04) !== 0; 291 dataAlignmentIndicator = (data[offset + 6] & 0x04) !== 0;
292 ptsDtsIndicator = (data[offset + 7] & 0xC0) >>> 6; 292 ptsDtsIndicator = data[offset + 7];
293 pesHeaderLength = data[offset + 8]; // TODO sanity check header length 293 pesHeaderLength = data[offset + 8]; // TODO sanity check header length
294 offset += 9; // Skip past PES header 294 offset += 9; // Skip past PES header
295 295
...@@ -299,22 +299,24 @@ ...@@ -299,22 +299,24 @@
299 // so what we are going to do instead, is drop the least 299 // so what we are going to do instead, is drop the least
300 // significant bit (the same as dividing by two) then we can 300 // significant bit (the same as dividing by two) then we can
301 // divide by 45 (45 * 2 = 90) to get ms. 301 // divide by 45 (45 * 2 = 90) to get ms.
302 if (ptsDtsIndicator & 0x03) { 302 if (ptsDtsIndicator & 0xC0) {
303 // the PTS and DTS are not written out directly. For information on
304 // how they are encoded, see
305 // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
303 pts = (data[offset + 0] & 0x0E) << 28 306 pts = (data[offset + 0] & 0x0E) << 28
304 | (data[offset + 1] & 0xFF) << 21 307 | (data[offset + 1] & 0xFF) << 21
305 | (data[offset + 2] & 0xFE) << 13 308 | (data[offset + 2] & 0xFE) << 13
306 | (data[offset + 3] & 0xFF) << 6 309 | (data[offset + 3] & 0xFF) << 6
307 | (data[offset + 4] & 0xFE) >>> 2; 310 | (data[offset + 4] & 0xFE) >>> 2;
308 pts /= 45; 311 pts /= 45;
309 if (ptsDtsIndicator & 0x01) {// DTS 312 dts = pts;
313 if (ptsDtsIndicator & 0x40) {// DTS
310 dts = (data[offset + 5] & 0x0E ) << 28 314 dts = (data[offset + 5] & 0x0E ) << 28
311 | (data[offset + 6] & 0xFF ) << 21 315 | (data[offset + 6] & 0xFF ) << 21
312 | (data[offset + 7] & 0xFE ) << 13 316 | (data[offset + 7] & 0xFE ) << 13
313 | (data[offset + 8] & 0xFF ) << 6 317 | (data[offset + 8] & 0xFF ) << 6
314 | (data[offset + 9] & 0xFE ) >>> 2; 318 | (data[offset + 9] & 0xFE ) >>> 2;
315 dts /= 45; 319 dts /= 45;
316 } else {
317 dts = pts;
318 } 320 }
319 } 321 }
320 // Skip past "optional" portion of PTS header 322 // Skip past "optional" portion of PTS header
...@@ -397,4 +399,4 @@ ...@@ -397,4 +399,4 @@
397 } 399 }
398 }; 400 };
399 }; 401 };
400 })(this); 402 })(window);
......