1 /*****************************************************************************
2 * ty.c - TiVo ty stream video demuxer for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * Copyright (C) 2005 by Neal Symms (tivo@freakinzoo.com) - February 2005
6 * based on code by Christopher Wingert for tivo-mplayer
7 * tivo(at)wingert.org, February 2003
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 * v1.0.0 - 24-Feb-2005 - Initial release - Series 1 support ONLY!
27 * v1.0.1 - 25-Feb-2005 - Added fix for bad GOP headers - Neal
28 * v1.0.2 - 26-Feb-2005 - No longer require "seekable" input stream - Neal
29 * v2.0.0 - 21-Mar-2005 - Series 2 support! No AC-3 on S2 DTivo yet.
30 * v2.1.0 - 22-Mar-2005 - Support for AC-3 on S2 DTivo (long ac3 packets)
31 * v3.0.0 - 14-Jul-2005 - Support for skipping fwd/back via VLC hotkeys
32 *****************************************************************************/
34 /*****************************************************************************
36 *****************************************************************************/
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_demux.h>
45 #include "vlc_codec.h"
47 #include "vlc_input.h"
48 #include "../codec/cc.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
59 set_shortname( N_("TY") )
60 set_description(N_("TY Stream audio/video demux"))
61 set_category( CAT_INPUT )
62 set_subcategory( SUBCAT_INPUT_DEMUX )
63 set_capability("demux", 6)
64 /* FIXME: there seems to be a segfault when using PVR access
65 * and TY demux has a bigger priority than PS
66 * Something must be wrong.
68 set_callbacks( Open, Close )
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Demux ( demux_t * );
77 static int Control( demux_t *, int, va_list );
79 #define SERIES1_PES_LENGTH (11) /* length of audio PES hdr on S1 */
80 #define SERIES2_PES_LENGTH (16) /* length of audio PES hdr on S2 */
81 #define AC3_PES_LENGTH (14) /* length of audio PES hdr for AC3 */
82 #define VIDEO_PES_LENGTH (16) /* length of video PES header */
83 #define DTIVO_PTS_OFFSET (6) /* offs into PES for MPEG PTS on DTivo */
84 #define SA_PTS_OFFSET (9) /* offset into PES for MPEG PTS on SA */
85 #define AC3_PTS_OFFSET (9) /* offset into PES for AC3 PTS on DTivo */
86 #define VIDEO_PTS_OFFSET (9) /* offset into PES for video PTS on all */
87 #define AC3_PKT_LENGTH (1536) /* size of TiVo AC3 pkts (w/o PES hdr) */
88 static const uint8_t ty_VideoPacket[] = { 0x00, 0x00, 0x01, 0xe0 };
89 static const uint8_t ty_MPEGAudioPacket[] = { 0x00, 0x00, 0x01, 0xc0 };
90 static const uint8_t ty_AC3AudioPacket[] = { 0x00, 0x00, 0x01, 0xbd };
92 #define CHUNK_PEEK_COUNT (3) /* number of chunks to probe */
94 /* packet types for reference:
95 2/c0: audio data continued
96 3/c0: audio packet header (PES header)
97 4/c0: audio data (S/A only?)
98 9/c0: audio packet header, AC-3 audio
99 2/e0: video data continued
100 6/e0: video packet header (PES header)
101 7/e0: video sequence header start
102 8/e0: video I-frame header start
103 a/e0: video P-frame header start
104 b/e0: video B-frame header start
105 c/e0: video GOP header start
106 e/01: closed-caption data
107 e/02: Extended data services data
108 e/03: ipreview data ("thumbs up to record" signal)
112 #define TIVO_PES_FILEID ( 0xf5467abd )
113 #define TIVO_PART_LENGTH ( 0x20000000 ) /* 536,870,912 bytes */
114 #define CHUNK_SIZE ( 128 * 1024 )
123 uint64_t l_ty_pts; /* TY PTS in the record header */
128 uint64_t l_timestamp;
129 uint8_t chunk_bitmask[8];
153 #define XDS_MAX_DATA_SIZE (32)
156 XDS_CLASS_CURRENT = 0,
157 XDS_CLASS_FUTURE = 1,
158 XDS_CLASS_CHANNEL = 2,
159 XDS_CLASS_MISCELLANEOUS = 3,
160 XDS_CLASS_PUBLIC_SERVICE = 4,
161 XDS_CLASS_RESERVED = 5,
162 XDS_CLASS_UNDEFINED = 6,
171 uint8_t p_data[XDS_MAX_DATA_SIZE];
176 XDS_META_PROGRAM_RATING_NONE,
177 XDS_META_PROGRAM_RATING_MPAA,
178 XDS_META_PROGRAM_RATING_TPG,
179 /* TODO add CA/CE rating */
180 } xds_meta_program_rating_t;
184 xds_meta_program_rating_t rating;
186 /* Add the other fields once I have the samples */
187 } xds_meta_program_t;
190 char *psz_channel_name;
191 char *psz_channel_call_letter;
192 char *psz_channel_number;
194 xds_meta_program_t current;
195 xds_meta_program_t future;
199 /* Are we in XDS mode */
202 /* Current class type */
208 xds_packet_t pkt[XDS_MAX_CLASS_COUNT][128]; /* XXX it is way too much, but simpler */
218 es_out_id_t *p_video; /* ptr to video codec */
219 es_out_id_t *p_audio; /* holds either ac3 or mpeg codec ptr */
222 es_out_id_t *p_cc[4];
228 size_t i_stream_size; /* size of input stream (if known) */
229 //uint64_t l_program_len; /* length of this stream in msec */
230 bool b_seekable; /* is this stream seekable? */
231 bool b_have_master; /* are master chunks present? */
232 tivo_type_t tivo_type; /* tivo type (SA / DTiVo) */
233 tivo_series_t tivo_series; /* Series1 or Series2 */
234 tivo_audio_t audio_type; /* AC3 or MPEG */
235 int i_Pes_Length; /* Length of Audio PES header */
236 int i_Pts_Offset; /* offset into audio PES of PTS */
237 uint8_t pes_buffer[20]; /* holds incomplete pes headers */
238 int i_pes_buf_cnt; /* how many bytes in our buffer */
239 size_t l_ac3_pkt_size; /* len of ac3 pkt we've seen so far */
240 uint64_t l_last_ty_pts; /* last TY timestamp we've seen */
241 //mtime_t l_last_ty_pts_sync; /* audio PTS at time of last TY PTS */
242 uint64_t l_first_ty_pts; /* first TY PTS in this master chunk */
243 uint64_t l_final_ty_pts; /* final TY PTS in this master chunk */
244 unsigned i_seq_table_size; /* number of entries in SEQ table */
245 unsigned i_bits_per_seq_entry; /* # of bits in SEQ table bitmask */
247 mtime_t firstAudioPTS;
248 mtime_t lastAudioPTS;
249 mtime_t lastVideoPTS;
251 ty_rec_hdr_t *rec_hdrs; /* record headers array */
252 int i_cur_rec; /* current record in this chunk */
253 int i_num_recs; /* number of recs in this chunk */
254 int i_seq_rec; /* record number where seq start is */
255 ty_seq_table_t *seq_table; /* table of SEQ entries from mstr chk */
260 static int get_chunk_header(demux_t *);
261 static mtime_t get_pts( const uint8_t *buf );
262 static int find_es_header( const uint8_t *header,
263 const uint8_t *buffer, int i_search_len );
264 static int ty_stream_seek_pct(demux_t *p_demux, double seek_pct);
265 static int ty_stream_seek_time(demux_t *, uint64_t);
267 static ty_rec_hdr_t *parse_chunk_headers( const uint8_t *p_buf,
268 int i_num_recs, int *pi_payload_size);
269 static int probe_stream(demux_t *p_demux);
270 static void analyze_chunk(demux_t *p_demux, const uint8_t *p_chunk);
271 static void parse_master(demux_t *p_demux);
273 static int DemuxRecVideo( demux_t *p_demux, ty_rec_hdr_t *rec_hdr, block_t *p_block_in );
274 static int DemuxRecAudio( demux_t *p_demux, ty_rec_hdr_t *rec_hdr, block_t *p_block_in );
275 static int DemuxRecCc( demux_t *p_demux, ty_rec_hdr_t *rec_hdr, block_t *p_block_in );
277 static void DemuxDecodeXds( demux_t *p_demux, uint8_t d1, uint8_t d2 );
279 static void XdsInit( xds_t * );
280 static void XdsExit( xds_t * );
282 #define TY_ES_GROUP (1)
285 * Open: check file and initialize demux structures
288 * 1. peek at the first 12 bytes of the stream for the
289 * magic TiVo PART header & stream type & chunk size
290 * 2. if it's not there, error with VLC_EGENERIC
291 * 3. set up video (mpgv) codec
292 * 4. return VLC_SUCCESS
294 static int Open(vlc_object_t *p_this)
296 demux_t *p_demux = (demux_t *)p_this;
299 const uint8_t *p_peek;
302 /* peek at the first 12 bytes. */
303 /* for TY streams, they're always the same */
304 if( stream_Peek( p_demux->s, &p_peek, 12 ) < 12 )
307 if ( U32_AT(p_peek) != TIVO_PES_FILEID ||
308 U32_AT(&p_peek[4]) != 0x02 ||
309 U32_AT(&p_peek[8]) != CHUNK_SIZE )
311 if( !p_demux->b_force &&
312 !demux_IsPathExtension( p_demux, ".ty" ) &&
313 !demux_IsPathExtension( p_demux, ".ty+" ) )
315 msg_Warn( p_demux, "this does not look like a TY file, "
316 "continuing anyway..." );
319 /* at this point, we assume we have a valid TY stream */
320 msg_Dbg( p_demux, "valid TY stream detected" );
322 /* Set exported functions */
323 p_demux->pf_demux = Demux;
324 p_demux->pf_control = Control;
326 /* create our structure that will hold all data */
327 p_demux->p_sys = p_sys = malloc(sizeof(demux_sys_t));
328 memset(p_sys, 0, sizeof(demux_sys_t));
330 /* set up our struct (most were zero'd out with the memset above) */
331 p_sys->b_first_chunk = true;
332 p_sys->b_have_master = (U32_AT(p_peek) == TIVO_PES_FILEID);
333 p_sys->firstAudioPTS = -1;
334 p_sys->i_stream_size = stream_Size(p_demux->s);
335 p_sys->tivo_type = TIVO_TYPE_UNKNOWN;
336 p_sys->audio_type = TIVO_AUDIO_UNKNOWN;
337 p_sys->tivo_series = TIVO_SERIES_UNKNOWN;
338 p_sys->i_Pes_Length = 0;
339 p_sys->i_Pts_Offset = 0;
340 p_sys->l_ac3_pkt_size = 0;
342 /* see if this stream is seekable */
343 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
345 if (probe_stream(p_demux) != VLC_SUCCESS) {
350 if (!p_sys->b_have_master)
351 msg_Warn(p_demux, "No master chunk found; seeking will be limited.");
353 /* register the proper audio codec */
354 if (p_sys->audio_type == TIVO_AUDIO_MPEG) {
355 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
357 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
359 fmt.i_group = TY_ES_GROUP;
360 p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
362 /* register the video stream */
363 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
364 fmt.i_group = TY_ES_GROUP;
365 p_sys->p_video = es_out_Add( p_demux->out, &fmt );
368 for( i = 0; i < 4; i++ )
369 p_sys->p_cc[i] = NULL;
370 cc_Init( &p_sys->cc );
372 XdsInit( &p_sys->xds );
377 /* =========================================================================== */
378 /* Demux: Read & Demux one record from the chunk
380 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
382 * NOTE: I think we can return the number of packets sent instead of just 1.
383 * that means we can demux an entire chunk and shoot it back (may be more efficient)
384 * -- should try that some day :) --
386 static int Demux( demux_t *p_demux )
388 demux_sys_t *p_sys = p_demux->p_sys;
390 block_t *p_block_in = NULL;
392 /*msg_Dbg(p_demux, "ty demux processing" );*/
394 /* did we hit EOF earlier? */
399 * what we do (1 record now.. maybe more later):
400 * - use stream_Read() to read the chunk header & record headers
401 * - discard entire chunk if it is a PART header chunk
402 * - parse all the headers into record header array
403 * - keep a pointer of which record we're on
404 * - use stream_Block() to fetch each record
405 * - parse out PTS from PES headers
406 * - set PTS for data packets
407 * - pass the data on to the proper codec via es_out_Send()
409 * if this is the first time or
410 * if we're at the end of this chunk, start a new one
412 /* parse the next chunk's record headers */
413 if( p_sys->b_first_chunk || p_sys->i_cur_rec >= p_sys->i_num_recs )
415 if( get_chunk_header(p_demux) == 0 )
419 /*======================================================================
420 * parse & send one record of the chunk
421 *====================================================================== */
422 p_rec = &p_sys->rec_hdrs[p_sys->i_cur_rec];
426 const long l_rec_size = p_rec->l_rec_size;
427 /*msg_Dbg(p_demux, "Record Type 0x%x/%02x %ld bytes",
428 subrec_type, p_rec->rec_type, l_rec_size );*/
430 /* some normal records are 0 length, so check for that... */
431 if( l_rec_size <= 0 )
433 /* no data in payload; we're done */
438 /* read in this record's payload */
439 if( !( p_block_in = stream_Block( p_demux->s, l_rec_size ) ) )
442 /* set these as 'unknown' for now */
444 p_block_in->i_dts = 0;
448 -- don't read any data from the stream, data was in the record header --
450 "Record Type 0x%02x/%02x, ext data = %02x, %02x", subrec_type,
451 p_rec->rec_type, p_rec->ex1, p_rec->ex2);
454 if( p_rec->rec_type == 0xe0 )
457 DemuxRecVideo( p_demux, p_rec, p_block_in );
459 else if ( p_rec->rec_type == 0xc0 )
462 DemuxRecAudio( p_demux, p_rec, p_block_in );
464 else if( p_rec->rec_type == 0x01 || p_rec->rec_type == 0x02 )
466 /* Closed Captions/XDS */
467 DemuxRecCc( p_demux, p_rec, p_block_in );
469 else if ( p_rec->rec_type == 0x03 )
471 /* Tivo data services (e.g. "thumbs-up to record!") useless for us */
473 block_Release(p_block_in);
475 else if ( p_rec->rec_type == 0x05 )
477 /* Unknown, but seen regularly */
479 block_Release(p_block_in);
483 msg_Dbg(p_demux, "Invalid record type 0x%02x", p_rec->rec_type );
485 block_Release(p_block_in);
494 static int Control(demux_t *p_demux, int i_query, va_list args)
496 demux_sys_t *p_sys = p_demux->p_sys;
500 /*msg_Info(p_demux, "control cmd %d", i_query);*/
503 case DEMUX_GET_POSITION:
504 /* arg is 0.0 - 1.0 percent of overall file position */
505 if( ( i64 = p_sys->i_stream_size ) > 0 )
507 pf = (double*) va_arg( args, double* );
508 *pf = ((double)1.0) * stream_Tell( p_demux->s ) / (double) i64;
513 case DEMUX_SET_POSITION:
514 /* arg is 0.0 - 1.0 percent of overall file position */
515 f = (double) va_arg( args, double );
516 /* msg_Dbg(p_demux, "Control - set position to %2.3f", f); */
517 if ((i64 = p_sys->i_stream_size) > 0)
518 return ty_stream_seek_pct(p_demux, f);
521 /* return TiVo timestamp */
522 p_i64 = (int64_t *) va_arg(args, int64_t *);
523 //*p_i64 = p_sys->lastAudioPTS - p_sys->firstAudioPTS;
524 //*p_i64 = (p_sys->l_last_ty_pts / 1000) + (p_sys->lastAudioPTS -
525 // p_sys->l_last_ty_pts_sync);
526 *p_i64 = (p_sys->l_last_ty_pts / 1000);
528 case DEMUX_GET_LENGTH: /* length of program in microseconds, 0 if unk */
530 p_i64 = (int64_t *) va_arg(args, int64_t *);
533 case DEMUX_SET_TIME: /* arg is time in microsecs */
534 i64 = (int64_t) va_arg( args, int64_t );
535 return ty_stream_seek_time(p_demux, i64 * 1000);
543 static void Close( vlc_object_t *p_this )
545 demux_t *p_demux = (demux_t*)p_this;
546 demux_sys_t *p_sys = p_demux->p_sys;
548 XdsExit( &p_sys->xds );
549 cc_Exit( &p_sys->cc );
550 free( p_sys->rec_hdrs );
551 free( p_sys->seq_table );
556 /* =========================================================================== */
557 /* Compute Presentation Time Stamp (PTS)
558 * Assume buf points to beginning of PTS */
559 static mtime_t get_pts( const uint8_t *buf )
563 i_pts = ((mtime_t)(buf[0]&0x0e ) << 29)|
564 (mtime_t)(buf[1] << 22)|
565 ((mtime_t)(buf[2]&0xfe) << 14)|
566 (mtime_t)(buf[3] << 7)|
567 (mtime_t)(buf[4] >> 1);
568 i_pts *= 100 / 9; /* convert PTS (90Khz clock) to microseconds */
573 /* =========================================================================== */
574 static int find_es_header( const uint8_t *header,
575 const uint8_t *buffer, int i_search_len )
579 for( count = 0; count < i_search_len; count++ )
581 if( !memcmp( &buffer[count], header, 4 ) )
588 /* =========================================================================== */
589 /* check if we have a full PES header, if not, then save what we have.
590 * this is called when audio-start packets are encountered.
592 * 1 partial PES hdr found, some audio data found (buffer adjusted),
593 * -1 partial PES hdr found, no audio data found
594 * 0 otherwise (complete PES found, pts extracted, pts set, buffer adjusted) */
595 /* TODO: HD support -- nothing known about those streams */
596 static int check_sync_pes( demux_t *p_demux, block_t *p_block,
597 int32_t offset, int32_t rec_len )
599 demux_sys_t *p_sys = p_demux->p_sys;
601 if ( offset < 0 || offset + p_sys->i_Pes_Length > rec_len )
603 /* entire PES header not present */
604 msg_Dbg( p_demux, "PES header at %d not complete in record. storing.",
606 /* save the partial pes header */
609 /* no header found, fake some 00's (this works, believe me) */
610 memset( p_sys->pes_buffer, 4, 0 );
611 p_sys->i_pes_buf_cnt = 4;
613 msg_Err( p_demux, "PES header not found in record of %d bytes!",
617 /* copy the partial pes header we found */
618 memcpy( p_sys->pes_buffer, p_block->p_buffer + offset,
620 p_sys->i_pes_buf_cnt = rec_len - offset;
624 /* PES Header was found, but not complete, so trim the end of this record */
625 p_block->i_buffer -= rec_len - offset;
628 return -1; /* partial PES, no audio data */
630 /* full PES header present, extract PTS */
631 p_sys->lastAudioPTS = get_pts( &p_block->p_buffer[ offset +
632 p_sys->i_Pts_Offset ] );
633 if (p_sys->firstAudioPTS < 0)
634 p_sys->firstAudioPTS = p_sys->lastAudioPTS;
635 p_block->i_pts = p_sys->lastAudioPTS;
636 /*msg_Dbg(p_demux, "Audio PTS %lld", p_sys->lastAudioPTS );*/
637 /* adjust audio record to remove PES header */
638 memmove(p_block->p_buffer + offset, p_block->p_buffer + offset +
639 p_sys->i_Pes_Length, rec_len - p_sys->i_Pes_Length);
640 p_block->i_buffer -= p_sys->i_Pes_Length;
642 msg_Dbg(p_demux, "pes hdr removed; buffer len=%d and has "
643 "%02x %02x %02x %02x %02x %02x %02x %02x "
644 "%02x %02x %02x %02x %02x %02x %02x %02x", p_block->i_buffer,
645 p_block->p_buffer[0], p_block->p_buffer[1],
646 p_block->p_buffer[2], p_block->p_buffer[3],
647 p_block->p_buffer[4], p_block->p_buffer[5],
648 p_block->p_buffer[6], p_block->p_buffer[7],
649 p_block->p_buffer[8], p_block->p_buffer[9],
650 p_block->p_buffer[10], p_block->p_buffer[11],
651 p_block->p_buffer[12], p_block->p_buffer[13],
652 p_block->p_buffer[14], p_block->p_buffer[15]);
657 static int DemuxRecVideo( demux_t *p_demux, ty_rec_hdr_t *rec_hdr, block_t *p_block_in )
659 demux_sys_t *p_sys = p_demux->p_sys;
660 const int subrec_type = rec_hdr->subrec_type;
661 const long l_rec_size = rec_hdr->l_rec_size; // p_block_in->i_buffer might be better
665 assert( rec_hdr->rec_type == 0xe0 );
670 msg_Dbg(p_demux, "packet buffer has "
671 "%02x %02x %02x %02x %02x %02x %02x %02x "
672 "%02x %02x %02x %02x %02x %02x %02x %02x",
673 p_block_in->p_buffer[0], p_block_in->p_buffer[1],
674 p_block_in->p_buffer[2], p_block_in->p_buffer[3],
675 p_block_in->p_buffer[4], p_block_in->p_buffer[5],
676 p_block_in->p_buffer[6], p_block_in->p_buffer[7],
677 p_block_in->p_buffer[8], p_block_in->p_buffer[9],
678 p_block_in->p_buffer[10], p_block_in->p_buffer[11],
679 p_block_in->p_buffer[12], p_block_in->p_buffer[13],
680 p_block_in->p_buffer[14], p_block_in->p_buffer[15]);
682 //if( subrec_type == 0x06 || subrec_type == 0x07 )
683 if( subrec_type != 0x02 && subrec_type != 0x0c &&
684 subrec_type != 0x08 && l_rec_size > 4 )
686 /* get the PTS from this packet if it has one.
687 * on S1, only 0x06 has PES. On S2, however, most all do.
688 * Do NOT Pass the PES Header to the MPEG2 codec */
689 esOffset1 = find_es_header( ty_VideoPacket, p_block_in->p_buffer, 5 );
690 if( esOffset1 != -1 )
692 //msg_Dbg(p_demux, "Video PES hdr in pkt type 0x%02x at offset %d",
693 //subrec_type, esOffset1);
694 p_sys->lastVideoPTS = get_pts(
695 &p_block_in->p_buffer[ esOffset1 + VIDEO_PTS_OFFSET ] );
696 /*msg_Dbg(p_demux, "Video rec %d PTS %"PRId64, p_sys->i_cur_rec,
697 p_sys->lastVideoPTS );*/
698 if (subrec_type != 0x06) {
699 /* if we found a PES, and it's not type 6, then we're S2 */
700 /* The packet will have video data (& other headers) so we
701 * chop out the PES header and send the rest */
702 if (l_rec_size >= VIDEO_PES_LENGTH) {
703 p_block_in->p_buffer += VIDEO_PES_LENGTH + esOffset1;
704 p_block_in->i_buffer -= VIDEO_PES_LENGTH + esOffset1;
706 msg_Dbg(p_demux, "video rec type 0x%02x has short PES"
707 " (%ld bytes)", subrec_type, l_rec_size);
708 /* nuke this block; it's too short, but has PES marker */
709 p_block_in->i_buffer = 0;
713 msg_Dbg(p_demux, "No Video PES hdr in pkt type 0x%02x",
717 if(subrec_type == 0x06 )
719 /* type 6 (S1 DTivo) has no data, so we're done */
720 block_Release(p_block_in);
724 /* if it's not a continue blk, then set PTS */
725 if( subrec_type != 0x02 )
727 /*msg_Dbg(p_demux, "Video rec %d type 0x%02X", p_sys->i_cur_rec,
729 /* if it's a GOP header, make sure it's legal
730 * (if we have enough data) */
731 /* Some ty files don't have this bit set
732 * and it causes problems */
733 if (subrec_type == 0x0c && l_rec_size >= 6)
734 p_block_in->p_buffer[5] |= 0x08;
735 /* store the TY PTS if there is one */
736 if (subrec_type == 0x07) {
737 p_sys->l_last_ty_pts = rec_hdr->l_ty_pts;
738 /* should we use audio or video PTS? */
739 //p_sys->l_last_ty_pts_sync = p_sys->lastAudioPTS;
741 /* yes I know this is a cheap hack. It's the timestamp
742 used for display and skipping fwd/back, so it
743 doesn't have to be accurate to the millisecond.
744 I adjust it here by roughly one 1/30 sec. Yes it
745 will be slightly off for UK streams, but it's OK.
747 p_sys->l_last_ty_pts += 35000000;
748 //p_sys->l_last_ty_pts += 33366667;
750 /* set PTS for this block before we send */
751 if (p_sys->lastVideoPTS > 0)
753 p_block_in->i_pts = p_sys->lastVideoPTS;
754 /* PTS gets used ONCE.
755 * Any subsequent frames we get BEFORE next PES
756 * header will have their PTS computed in the codec */
757 p_sys->lastVideoPTS = 0;
761 /* Register the CC decoders when needed */
762 for( i = 0; i < 4; i++ )
764 static const vlc_fourcc_t fcc[4] = {
765 VLC_FOURCC('c', 'c', '1', ' '),
766 VLC_FOURCC('c', 'c', '2', ' '),
767 VLC_FOURCC('c', 'c', '3', ' '),
768 VLC_FOURCC('c', 'c', '4', ' ')
770 static const char *ppsz_description[4] = {
771 N_("Closed captions 1"),
772 N_("Closed captions 2"),
773 N_("Closed captions 3"),
774 N_("Closed captions 4"),
779 if( !p_sys->cc.pb_present[i] || p_sys->p_cc[i] )
782 es_format_Init( &fmt, SPU_ES, fcc[i] );
783 fmt.psz_description = strdup( _(ppsz_description[i]) );
784 fmt.i_group = TY_ES_GROUP;
785 p_sys->p_cc[i] = es_out_Add( p_demux->out, &fmt );
786 es_format_Clean( &fmt );
789 /* Send the CC data */
790 if( p_block_in->i_pts > 0 && p_sys->cc.i_data > 0 )
792 for( i = 0; i < 4; i++ )
796 block_t *p_cc = block_New( p_demux, p_sys->cc.i_data );
797 p_cc->i_flags |= BLOCK_FLAG_TYPE_I;
798 p_cc->i_pts = p_block_in->i_pts;
799 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
801 es_out_Send( p_demux->out, p_sys->p_cc[i], p_cc );
804 cc_Flush( &p_sys->cc );
807 //msg_Dbg(p_demux, "sending rec %d as video type 0x%02x",
808 //p_sys->i_cur_rec, subrec_type);
809 es_out_Send(p_demux->out, p_sys->p_video, p_block_in);
812 static int DemuxRecAudio( demux_t *p_demux, ty_rec_hdr_t *rec_hdr, block_t *p_block_in )
814 demux_sys_t *p_sys = p_demux->p_sys;
815 const int subrec_type = rec_hdr->subrec_type;
816 const long l_rec_size = rec_hdr->l_rec_size;
819 assert( rec_hdr->rec_type == 0xc0 );
824 fprintf( stderr, "Audio Packet Header " );
825 for( i = 0 ; i < 24 ; i++ )
826 fprintf( stderr, "%2.2x ", p_block_in->p_buffer[i] );
827 fprintf( stderr, "\n" );
830 if( subrec_type == 2 )
832 /* SA or DTiVo Audio Data, no PES (continued block)
833 * ================================================
836 /* continue PES if previous was incomplete */
837 if (p_sys->i_pes_buf_cnt > 0)
839 const int i_need = p_sys->i_Pes_Length - p_sys->i_pes_buf_cnt;
841 msg_Dbg(p_demux, "continuing PES header");
842 /* do we have enough data to complete? */
843 if (i_need >= l_rec_size)
845 /* don't have complete PES hdr; save what we have and return */
846 memcpy(&p_sys->pes_buffer[p_sys->i_pes_buf_cnt],
847 p_block_in->p_buffer, l_rec_size);
848 p_sys->i_pes_buf_cnt += l_rec_size;
850 block_Release(p_block_in);
854 /* we have enough; reconstruct this p_frame with the new hdr */
855 memcpy(&p_sys->pes_buffer[p_sys->i_pes_buf_cnt],
856 p_block_in->p_buffer, i_need);
857 /* advance the block past the PES header (don't want to send it) */
858 p_block_in->p_buffer += i_need;
859 p_block_in->i_buffer -= i_need;
860 /* get the PTS out of this PES header (MPEG or AC3) */
861 if (p_sys->audio_type == TIVO_AUDIO_MPEG)
862 esOffset1 = find_es_header(ty_MPEGAudioPacket,
863 p_sys->pes_buffer, 5);
865 esOffset1 = find_es_header(ty_AC3AudioPacket,
866 p_sys->pes_buffer, 5);
869 /* god help us; something's really wrong */
870 msg_Err(p_demux, "can't find audio PES header in packet");
874 p_sys->lastAudioPTS = get_pts(
875 &p_sys->pes_buffer[ esOffset1 + p_sys->i_Pts_Offset ] );
876 p_block_in->i_pts = p_sys->lastAudioPTS;
878 p_sys->i_pes_buf_cnt = 0;
880 /* S2 DTivo has AC3 packets with 2 padding bytes at end. This is
881 * not allowed in the AC3 spec and will cause problems. So here
882 * we try to trim things. */
883 /* Also, S1 DTivo has alternating short / long AC3 packets. That
884 * is, one packet is short (incomplete) and the next packet has
885 * the first one's missing data, plus all of its own. Strange. */
886 if (p_sys->audio_type == TIVO_AUDIO_AC3 &&
887 p_sys->tivo_series == TIVO_SERIES2) {
888 if (p_sys->l_ac3_pkt_size + p_block_in->i_buffer >
890 p_block_in->i_buffer -= 2;
891 p_sys->l_ac3_pkt_size = 0;
893 p_sys->l_ac3_pkt_size += p_block_in->i_buffer;
897 else if( subrec_type == 0x03 )
899 /* MPEG Audio with PES Header, either SA or DTiVo */
900 /* ================================================ */
901 esOffset1 = find_es_header( ty_MPEGAudioPacket,
902 p_block_in->p_buffer, 5 );
904 /*msg_Dbg(p_demux, "buffer has %#02x %#02x %#02x %#02x",
905 p_block_in->p_buffer[0], p_block_in->p_buffer[1],
906 p_block_in->p_buffer[2], p_block_in->p_buffer[3]);
907 msg_Dbg(p_demux, "audio ES hdr at offset %d", esOffset1);*/
909 /* SA PES Header, No Audio Data */
910 /* ================================================ */
911 if ( ( esOffset1 == 0 ) && ( l_rec_size == 16 ) )
913 p_sys->lastAudioPTS = get_pts( &p_block_in->p_buffer[
915 if (p_sys->firstAudioPTS < 0)
916 p_sys->firstAudioPTS = p_sys->lastAudioPTS;
918 block_Release(p_block_in);
920 /*msg_Dbg(p_demux, "SA Audio PTS %lld",
921 p_sys->lastAudioPTS );*/
923 /* DTiVo Audio with PES Header */
924 /* ================================================ */
926 /* Check for complete PES */
927 if (check_sync_pes(p_demux, p_block_in, esOffset1,
930 /* partial PES header found, nothing else.
932 block_Release(p_block_in);
936 msg_Dbg(p_demux, "packet buffer has "
937 "%02x %02x %02x %02x %02x %02x %02x %02x "
938 "%02x %02x %02x %02x %02x %02x %02x %02x",
939 p_block_in->p_buffer[0], p_block_in->p_buffer[1],
940 p_block_in->p_buffer[2], p_block_in->p_buffer[3],
941 p_block_in->p_buffer[4], p_block_in->p_buffer[5],
942 p_block_in->p_buffer[6], p_block_in->p_buffer[7],
943 p_block_in->p_buffer[8], p_block_in->p_buffer[9],
944 p_block_in->p_buffer[10], p_block_in->p_buffer[11],
945 p_block_in->p_buffer[12], p_block_in->p_buffer[13],
946 p_block_in->p_buffer[14], p_block_in->p_buffer[15]);
949 else if( subrec_type == 0x04 )
951 /* SA Audio with no PES Header */
952 /* ================================================ */
954 "Adding SA Audio Packet Size %ld", l_rec_size ); */
956 if (p_sys->lastAudioPTS > 0)
957 p_block_in->i_pts = p_sys->lastAudioPTS;
959 else if( subrec_type == 0x09 )
961 /* DTiVo AC3 Audio Data with PES Header */
962 /* ================================================ */
963 esOffset1 = find_es_header( ty_AC3AudioPacket,
964 p_block_in->p_buffer, 5 );
967 msg_Dbg(p_demux, "buffer has "
968 "%02x %02x %02x %02x %02x %02x %02x %02x "
969 "%02x %02x %02x %02x %02x %02x %02x %02x",
970 p_block_in->p_buffer[0], p_block_in->p_buffer[1],
971 p_block_in->p_buffer[2], p_block_in->p_buffer[3],
972 p_block_in->p_buffer[4], p_block_in->p_buffer[5],
973 p_block_in->p_buffer[6], p_block_in->p_buffer[7],
974 p_block_in->p_buffer[8], p_block_in->p_buffer[9],
975 p_block_in->p_buffer[10], p_block_in->p_buffer[11],
976 p_block_in->p_buffer[12], p_block_in->p_buffer[13],
977 p_block_in->p_buffer[14], p_block_in->p_buffer[15]);
978 msg_Dbg(p_demux, "audio ES AC3 hdr at offset %d", esOffset1);
981 /* Check for complete PES */
982 if (check_sync_pes(p_demux, p_block_in, esOffset1,
985 /* partial PES header found, nothing else. we're done. */
986 block_Release(p_block_in);
989 /* S2 DTivo has invalid long AC3 packets */
990 if (p_sys->tivo_series == TIVO_SERIES2) {
991 if (p_block_in->i_buffer > AC3_PKT_LENGTH) {
992 p_block_in->i_buffer -= 2;
993 p_sys->l_ac3_pkt_size = 0;
995 p_sys->l_ac3_pkt_size = p_block_in->i_buffer;
1001 /* Unsupported/Unknown */
1002 block_Release(p_block_in);
1006 /* set PCR before we send (if PTS found) */
1007 if( p_block_in->i_pts > 0 )
1008 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
1009 p_block_in->i_pts );
1011 es_out_Send( p_demux->out, p_sys->p_audio, p_block_in );
1015 static int DemuxRecCc( demux_t *p_demux, ty_rec_hdr_t *rec_hdr, block_t *p_block_in )
1017 demux_sys_t *p_sys = p_demux->p_sys;
1022 block_Release(p_block_in);
1024 if( rec_hdr->rec_type == 0x01 )
1026 else if( rec_hdr->rec_type == 0x02 )
1031 /* XDS data (extract programs infos) transmitted on field 2 only */
1033 DemuxDecodeXds( p_demux, rec_hdr->ex1, rec_hdr->ex2 );
1035 if( p_sys->cc.i_data + 3 > CC_MAX_DATA_SIZE )
1038 p_sys->cc.p_data[p_sys->cc.i_data+0] = i_field;
1039 p_sys->cc.p_data[p_sys->cc.i_data+1] = rec_hdr->ex1;
1040 p_sys->cc.p_data[p_sys->cc.i_data+2] = rec_hdr->ex2;
1041 p_sys->cc.i_data += 3;
1043 i_channel = cc_Channel( i_field, &p_sys->cc.p_data[p_sys->cc.i_data-3 + 1] );
1044 if( i_channel >= 0 && i_channel < 4 )
1045 p_sys->cc.pb_present[i_channel] = true;
1049 /* seek to a position within the stream, if possible */
1050 static int ty_stream_seek_pct(demux_t *p_demux, double seek_pct)
1052 demux_sys_t *p_sys = p_demux->p_sys;
1053 int64_t seek_pos = p_sys->i_stream_size * seek_pct;
1056 unsigned i_cur_part;
1058 /* if we're not seekable, there's nothing to do */
1059 if (!p_sys->b_seekable)
1060 return VLC_EGENERIC;
1062 /* figure out which part & chunk we want & go there */
1063 i_cur_part = seek_pos / TIVO_PART_LENGTH;
1064 p_sys->i_cur_chunk = seek_pos / CHUNK_SIZE;
1066 /* try to read the part header (master chunk) if it's there */
1067 if ( stream_Seek( p_demux->s, i_cur_part * TIVO_PART_LENGTH ))
1069 /* can't seek stream */
1070 return VLC_EGENERIC;
1072 parse_master(p_demux);
1074 /* now for the actual chunk */
1075 if ( stream_Seek( p_demux->s, p_sys->i_cur_chunk * CHUNK_SIZE))
1077 /* can't seek stream */
1078 return VLC_EGENERIC;
1080 /* load the chunk */
1081 p_sys->i_stuff_cnt = 0;
1082 get_chunk_header(p_demux);
1084 /* seek within the chunk to get roughly to where we want */
1085 p_sys->i_cur_rec = (int)
1086 ((double) ((seek_pos % CHUNK_SIZE) / (double) (CHUNK_SIZE)) * p_sys->i_num_recs);
1087 msg_Dbg(p_demux, "Seeked to file pos %"PRId64, seek_pos);
1088 msg_Dbg(p_demux, " (chunk %d, record %d)",
1089 p_sys->i_cur_chunk - 1, p_sys->i_cur_rec);
1091 /* seek to the start of this record's data.
1092 * to do that, we have to skip past all prior records */
1094 for (i=0; i<p_sys->i_cur_rec; i++)
1095 l_skip_amt += p_sys->rec_hdrs[i].l_rec_size;
1096 stream_Seek(p_demux->s, ((p_sys->i_cur_chunk-1) * CHUNK_SIZE) +
1097 (p_sys->i_num_recs * 16) + l_skip_amt + 4);
1099 /* to hell with syncing any audio or video, just start reading records... :) */
1100 /*p_sys->lastAudioPTS = p_sys->lastVideoPTS = 0;*/
1105 //#define TY_XDS_DEBUG
1106 static void XdsInit( xds_t *h )
1111 h->i_class = XDS_MAX_CLASS_COUNT;
1113 h->b_future = false;
1114 for( i = 0; i < XDS_MAX_CLASS_COUNT; i++ )
1116 for( j = 0; j < 128; j++ )
1117 h->pkt[i][j].b_started = false;
1119 h->b_meta_changed = false;
1120 memset( &h->meta, 0, sizeof(h->meta) );
1122 static void XdsExit( xds_t *h )
1125 free( h->meta.psz_channel_name );
1126 free( h->meta.psz_channel_call_letter );
1127 free( h->meta.psz_channel_number );
1130 free( h->meta.current.psz_name );
1131 free( h->meta.current.psz_rating );
1133 free( h->meta.future.psz_name );
1134 free( h->meta.future.psz_rating );
1136 static void XdsStringUtf8( char dst[2*32+1], const uint8_t *p_src, int i_src )
1141 for( i = 0, i_dst = 0; i < i_src; i++ )
1145 #define E2( c, u1, u2 ) case c: dst[i_dst++] = u1; dst[i_dst++] = u2; break
1146 E2( 0x2a, 0xc3,0xa1); // lowercase a, acute accent
1147 E2( 0x5c, 0xc3,0xa9); // lowercase e, acute accent
1148 E2( 0x5e, 0xc3,0xad); // lowercase i, acute accent
1149 E2( 0x5f, 0xc3,0xb3); // lowercase o, acute accent
1150 E2( 0x60, 0xc3,0xba); // lowercase u, acute accent
1151 E2( 0x7b, 0xc3,0xa7); // lowercase c with cedilla
1152 E2( 0x7c, 0xc3,0xb7); // division symbol
1153 E2( 0x7d, 0xc3,0x91); // uppercase N tilde
1154 E2( 0x7e, 0xc3,0xb1); // lowercase n tilde
1157 dst[i_dst++] = p_src[i];
1161 dst[i_dst++] = '\0';
1163 static bool XdsChangeString( xds_t *h, char **ppsz_dst, const char *psz_new )
1165 if( *ppsz_dst && psz_new && !strcmp( *ppsz_dst, psz_new ) )
1167 if( *ppsz_dst == NULL && psz_new == NULL )
1172 *ppsz_dst = strdup( psz_new );
1176 h->b_meta_changed = true;
1180 static void XdsDecodeCurrentFuture( xds_t *h, xds_packet_t *pk )
1182 xds_meta_program_t *p_prg = h->b_future ? &h->meta.future : &h->meta.current;
1189 XdsStringUtf8( name, pk->p_data, pk->i_data );
1190 if( XdsChangeString( h, &p_prg->psz_name, name ) )
1192 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Current/Future (Program Name) %d'\n", pk->i_data );
1193 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> program name %s\n", name );
1197 i_rating = (pk->p_data[0] & 0x18);
1198 if( i_rating == 0x08 )
1201 static const char *pppsz_ratings[8][2] = {
1202 { "None", "No rating (no content advisory)" },
1203 { "TV-Y", "All Children (no content advisory)" },
1204 { "TV-Y7", "Directed to Older Children (V = Fantasy Violence)" },
1205 { "TV-G", "General Audience (no content advisory)" },
1206 { "TV-PG", "Parental Guidance Suggested" },
1207 { "TV-14", "Parents Strongly Cautioned" },
1208 { "TV-MA", "Mature Audience Only" },
1209 { "None", "No rating (no content advisory)" }
1211 p_prg->rating = XDS_META_PROGRAM_RATING_TPG;
1212 if( XdsChangeString( h, &p_prg->psz_rating, pppsz_ratings[pk->p_data[1]&0x07][0] ) )
1214 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Current/Future (Rating) %d'\n", pk->i_data );
1215 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> TPG Rating %s (%s)\n",
1216 // pppsz_ratings[pk->p_data[1]&0x07][0], pppsz_ratings[pk->p_data[1]&0x07][1] );
1219 else if( i_rating == 0x00 || i_rating == 0x10 )
1222 static const char *pppsz_ratings[8][2] = {
1224 { "G", "General Audiences" },
1225 { "PG", "Parental Guidance Suggested" },
1226 { "PG-13", "Parents Strongly Cautioned" },
1227 { "R", "Restricted" },
1228 { "NC-17", "No one 17 and under admitted" },
1229 { "X", "No one under 17 admitted" },
1230 { "NR", "Not Rated" },
1232 p_prg->rating = XDS_META_PROGRAM_RATING_MPAA;
1233 if( XdsChangeString( h, &p_prg->psz_rating, pppsz_ratings[pk->p_data[0]&0x07][0] ) )
1235 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Current/Future (Rating) %d'\n", pk->i_data );
1236 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> TPG Rating %s (%s)\n",
1237 // pppsz_ratings[pk->p_data[0]&0x07][0], pppsz_ratings[pk->p_data[0]&0x07][1] );
1242 /* Non US Rating TODO */
1243 assert( i_rating == 0x18 ); // only left value possible */
1244 p_prg->rating = XDS_META_PROGRAM_RATING_NONE;
1245 if( XdsChangeString( h, &p_prg->psz_rating, NULL ) )
1247 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Current/Future (Rating) %d'\n", pk->i_data );
1248 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> 0x%2.2x 0x%2.2x\n", pk->p_data[0], pk->p_data[1] );
1255 fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Current/Future (Unknown 0x%x)'\n", h->i_type );
1261 static void XdsDecodeChannel( xds_t *h, xds_packet_t *pk )
1269 if( pk->i_data < 2 )
1271 XdsStringUtf8( name, pk->p_data, pk->i_data );
1272 if( XdsChangeString( h, &h->meta.psz_channel_name, name ) )
1274 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Network Name) %d'\n", pk->i_data );
1275 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> %s\n", name );
1280 if( pk->i_data < 4 )
1283 XdsStringUtf8( name, pk->p_data, 4 );
1284 if( XdsChangeString( h, &h->meta.psz_channel_call_letter, name ) )
1286 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Network Call Letter)' %d\n", pk->i_data );
1287 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> call letter %s\n", name );
1289 if( pk->i_data >= 6 )
1291 XdsStringUtf8( chan, &pk->p_data[4], 2 );
1292 if( XdsChangeString( h, &h->meta.psz_channel_number, chan ) )
1294 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Network Call Letter)' %d\n", pk->i_data );
1295 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> channel number %s\n", chan );
1300 if( XdsChangeString( h, &h->meta.psz_channel_number, NULL ) )
1302 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Network Call Letter)' %d\n", pk->i_data );
1303 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: ====> no channel number letter anymore\n" );
1308 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Channel Tape Delay)'\n" );
1311 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Transmission Signal Identifier)'\n" );
1315 fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Channel (Unknown 0x%x)'\n", h->i_type );
1321 static void XdsDecode( xds_t *h, xds_packet_t *pk )
1323 switch( h->i_class )
1325 case XDS_CLASS_CURRENT:
1326 case XDS_CLASS_FUTURE:
1327 XdsDecodeCurrentFuture( h, pk );
1329 case XDS_CLASS_CHANNEL:
1330 XdsDecodeChannel( h, pk );
1332 case XDS_CLASS_MISCELLANEOUS:
1334 fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Miscellaneous'\n" );
1337 case XDS_CLASS_PUBLIC_SERVICE:
1339 fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: class 'Public Service'\n" );
1343 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS XdsDecode: unknown class\n" );
1348 static void XdsParse( xds_t *h, uint8_t d1, uint8_t d2 )
1350 /* TODO check parity */
1355 if( d1 >= 0x01 && d1 <= 0x0e )
1357 const xds_class_t i_class = ( d1 - 1 ) >> 1;
1358 const int i_type = d2;
1359 const bool b_start = d1 & 0x01;
1360 xds_packet_t *pk = &h->pkt[i_class][i_type];
1362 if( !b_start && !pk->b_started )
1364 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS Continuying a non started packet, ignoring\n" );
1370 h->i_class = i_class;
1372 h->b_future = !b_start;
1373 pk->b_started = true;
1377 pk->i_sum = d1 + d2;
1380 else if( d1 == 0x0f && h->b_xds )
1382 xds_packet_t *pk = &h->pkt[h->i_class][h->i_type];
1384 /* TODO checksum and decode */
1385 pk->i_sum += d1 + d2;
1386 if( pk->i_sum & 0x7f )
1388 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS invalid checksum, ignoring ---------------------------------\n" );
1389 pk->b_started = false;
1392 if( pk->i_data <= 0 )
1394 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS empty packet, ignoring ---------------------------------\n" );
1395 pk->b_started = false;
1399 //if( pk->p_data[pk->i_data-1] == 0x40 ) /* Padding byte */
1404 pk->b_started = false;
1406 else if( d1 >= 0x20 && h->b_xds )
1408 xds_packet_t *pk = &h->pkt[h->i_class][h->i_type];
1410 if( pk->i_data+2 > XDS_MAX_DATA_SIZE )
1412 /* Broken -> reinit */
1413 //fprintf( stderr, "xxxxxxxxxxxxxxxXDS broken, reset\n" );
1415 pk->b_started = false;
1418 /* TODO check parity bit */
1419 pk->p_data[pk->i_data++] = d1 & 0x7f;
1420 pk->p_data[pk->i_data++] = d2 & 0x7f;
1429 static void DemuxDecodeXds( demux_t *p_demux, uint8_t d1, uint8_t d2 )
1431 demux_sys_t *p_sys = p_demux->p_sys;
1433 XdsParse( &p_demux->p_sys->xds, d1, d2 );
1434 if( p_demux->p_sys->xds.b_meta_changed )
1436 xds_meta_t *m = &p_sys->xds.meta;
1440 /* Channel meta data */
1441 p_meta = vlc_meta_New();
1442 if( m->psz_channel_name )
1443 vlc_meta_SetPublisher( p_meta, m->psz_channel_name );
1444 if( m->psz_channel_call_letter )
1445 vlc_meta_SetTitle( p_meta, m->psz_channel_call_letter );
1446 if( m->psz_channel_number )
1447 vlc_meta_AddExtra( p_meta, "Channel number", m->psz_channel_number );
1448 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META, TY_ES_GROUP, p_meta );
1449 vlc_meta_Delete( p_meta );
1451 /* Event meta data (current/future) */
1452 p_epg = vlc_epg_New( NULL );
1453 if( m->current.psz_name )
1455 vlc_epg_AddEvent( p_epg, 0, 0, m->current.psz_name, NULL, NULL );
1456 //if( m->current.psz_rating )
1457 // TODO but VLC cannot yet handle rating per epg event
1458 vlc_epg_SetCurrent( p_epg, 0 );
1460 if( m->future.psz_name )
1463 if( p_epg->i_event > 0 )
1464 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, TY_ES_GROUP, p_epg );
1465 vlc_epg_Delete( p_epg );
1467 p_demux->p_sys->xds.b_meta_changed = false;
1470 /* seek to an exact time position within the stream, if possible.
1471 * l_seek_time is in nanoseconds, the TIVO time standard.
1473 static int ty_stream_seek_time(demux_t *p_demux, uint64_t l_seek_time)
1475 demux_sys_t *p_sys = p_demux->p_sys;
1476 int i_seq_entry = 0;
1479 long l_cur_pos = stream_Tell(p_demux->s);
1480 unsigned i_cur_part = l_cur_pos / TIVO_PART_LENGTH;
1481 long l_seek_secs = l_seek_time / 1000000000;
1482 uint64_t l_fwd_stamp = 1;
1484 /* if we're not seekable, there's nothing to do */
1485 if (!p_sys->b_seekable || !p_sys->b_have_master)
1486 return VLC_EGENERIC;
1488 msg_Dbg(p_demux, "Skipping to time %02ld:%02ld:%02ld",
1489 l_seek_secs / 3600, (l_seek_secs / 60) % 60, l_seek_secs % 60);
1491 /* seek to the proper segment if necessary */
1492 /* first see if we need to go back */
1493 while (l_seek_time < p_sys->l_first_ty_pts) {
1494 msg_Dbg(p_demux, "skipping to prior segment.");
1495 /* load previous part */
1496 if (i_cur_part == 0) {
1497 stream_Seek(p_demux->s, l_cur_pos);
1498 msg_Err(p_demux, "Attempt to seek past BOF");
1499 return VLC_EGENERIC;
1501 stream_Seek(p_demux->s, (i_cur_part - 1) * TIVO_PART_LENGTH);
1503 parse_master(p_demux);
1505 /* maybe we need to go forward */
1506 while (l_seek_time > p_sys->l_final_ty_pts) {
1507 msg_Dbg(p_demux, "skipping to next segment.");
1508 /* load next part */
1509 if ((i_cur_part + 1) * TIVO_PART_LENGTH > p_sys->i_stream_size) {
1510 /* error; restore previous file position */
1511 stream_Seek(p_demux->s, l_cur_pos);
1512 msg_Err(p_demux, "seek error");
1513 return VLC_EGENERIC;
1515 stream_Seek(p_demux->s, (i_cur_part + 1) * TIVO_PART_LENGTH);
1517 parse_master(p_demux);
1520 /* our target is somewhere within this part;
1521 find the proper chunk using seq_table */
1522 for (i=1; i<p_sys->i_seq_table_size; i++) {
1523 if (p_sys->seq_table[i].l_timestamp > l_seek_time) {
1524 /* i-1 is the section we want; remember the next timestamp in case
1525 we have to use it (this section may not have a proper SEQ hdr
1526 for the time we're seeking) */
1527 msg_Dbg(p_demux, "stopping at seq entry %d.", i);
1528 l_fwd_stamp = p_sys->seq_table[i].l_timestamp;
1534 /* if we went through the entire last loop and didn't find our target,
1535 then we skip to the next part. What has happened is that the actual
1536 time we're seeking is within this part, but there isn't a SEQ hdr
1537 for it here. So we skip to the next part */
1538 if (i == p_sys->i_seq_table_size) {
1539 if ((i_cur_part + 1) * TIVO_PART_LENGTH > p_sys->i_stream_size) {
1540 /* error; restore previous file position */
1541 stream_Seek(p_demux->s, l_cur_pos);
1542 msg_Err(p_demux, "seek error");
1543 return VLC_EGENERIC;
1545 stream_Seek(p_demux->s, (i_cur_part + 1) * TIVO_PART_LENGTH);
1547 parse_master(p_demux);
1551 /* determine which chunk has our seek_time */
1552 for (i=0; i<p_sys->i_bits_per_seq_entry; i++) {
1553 long l_chunk_nr = i_seq_entry * p_sys->i_bits_per_seq_entry + i;
1554 long l_chunk_offset = (l_chunk_nr + 1) * CHUNK_SIZE;
1555 msg_Dbg(p_demux, "testing part %d chunk %ld mask 0x%02X bit %d",
1556 i_cur_part, l_chunk_nr,
1557 p_sys->seq_table[i_seq_entry].chunk_bitmask[i/8], i%8);
1558 if (p_sys->seq_table[i_seq_entry].chunk_bitmask[i/8] & (1 << (i%8))) {
1559 /* check this chunk's SEQ header timestamp */
1560 msg_Dbg(p_demux, "has SEQ. seeking to chunk at 0x%lX",
1561 (i_cur_part * TIVO_PART_LENGTH) + l_chunk_offset);
1562 stream_Seek(p_demux->s, (i_cur_part * TIVO_PART_LENGTH) +
1564 // TODO: we don't have to parse the full header set;
1565 // just test the seq_rec entry for its timestamp
1566 p_sys->i_stuff_cnt = 0;
1567 get_chunk_header(p_demux);
1568 // check ty PTS for the SEQ entry in this chunk
1569 if (p_sys->i_seq_rec < 0 || p_sys->i_seq_rec > p_sys->i_num_recs) {
1570 msg_Err(p_demux, "no SEQ hdr in chunk; table had one.");
1571 /* Seek to beginning of original chunk & reload it */
1572 stream_Seek(p_demux->s, (l_cur_pos / CHUNK_SIZE) * CHUNK_SIZE);
1573 p_sys->i_stuff_cnt = 0;
1574 get_chunk_header(p_demux);
1575 return VLC_EGENERIC;
1577 l_seek_secs = p_sys->rec_hdrs[p_sys->i_seq_rec].l_ty_pts /
1579 msg_Dbg(p_demux, "found SEQ hdr for timestamp %02ld:%02ld:%02ld",
1581 (l_seek_secs / 60) % 60, l_seek_secs % 60);
1582 if (p_sys->rec_hdrs[p_sys->i_seq_rec].l_ty_pts >= l_seek_time) {
1583 // keep this one? go back?
1584 /* for now, we take this one. it's the first SEQ hdr AFTER
1585 the time we were searching for. */
1586 msg_Dbg(p_demux, "seek target found.");
1589 msg_Dbg(p_demux, "timestamp too early. still scanning.");
1592 /* if we made it through this entire loop without finding our target,
1593 then we skip to the next section. What has happened is that the actual
1594 time we're seeking is within this section, but there isn't a SEQ hdr
1595 for it here. So we skip to the next closest one (l_fwd_stamp) */
1596 if (i == p_sys->i_bits_per_seq_entry)
1597 return ty_stream_seek_time(p_demux, l_fwd_stamp);
1599 /* current stream ptr is at beginning of data for this chunk,
1600 so we need to skip past any stream data prior to the seq_rec
1603 for (int j=0; j<p_sys->i_seq_rec; j++)
1604 i_skip_cnt += p_sys->rec_hdrs[j].l_rec_size;
1605 stream_Read(p_demux->s, NULL, i_skip_cnt);
1606 p_sys->i_cur_rec = p_sys->i_seq_rec;
1607 //p_sys->l_last_ty_pts = p_sys->rec_hdrs[p_sys->i_seq_rec].l_ty_pts;
1608 //p_sys->l_last_ty_pts_sync = p_sys->lastAudioPTS;
1614 /* parse a master chunk, filling the SEQ table and other variables.
1615 * We assume the stream is currently pointing to it.
1617 static void parse_master(demux_t *p_demux)
1619 demux_sys_t *p_sys = p_demux->p_sys;
1620 uint8_t mst_buf[32];
1621 uint32_t i, i_map_size;
1622 int64_t i_save_pos = stream_Tell(p_demux->s);
1625 /* Note that the entries in the SEQ table in the stream may have
1626 different sizes depending on the bits per entry. We store them
1627 all in the same size structure, so we have to parse them out one
1628 by one. If we had a dynamic structure, we could simply read the
1629 entire table directly from the stream into memory in place. */
1631 /* clear the SEQ table */
1632 free(p_sys->seq_table);
1634 /* parse header info */
1635 stream_Read(p_demux->s, mst_buf, 32);
1636 i_map_size = U32_AT(&mst_buf[20]); /* size of bitmask, in bytes */
1637 p_sys->i_bits_per_seq_entry = i_map_size * 8;
1638 i = U32_AT(&mst_buf[28]); /* size of SEQ table, in bytes */
1639 p_sys->i_seq_table_size = i / (8 + i_map_size);
1641 /* parse all the entries */
1642 p_sys->seq_table = calloc(p_sys->i_seq_table_size, sizeof(ty_seq_table_t));
1643 if (p_sys->seq_table == NULL)
1645 p_sys->i_seq_table_size = 0;
1648 for (unsigned i=0; i<p_sys->i_seq_table_size; i++) {
1649 stream_Read(p_demux->s, mst_buf, 8);
1650 p_sys->seq_table[i].l_timestamp = U64_AT(&mst_buf[0]);
1651 if (i_map_size > 8) {
1652 msg_Err(p_demux, "Unsupported SEQ bitmap size in master chunk");
1653 stream_Read(p_demux->s, NULL, i_map_size);
1655 stream_Read(p_demux->s, mst_buf + 8, i_map_size);
1656 memcpy(p_sys->seq_table[i].chunk_bitmask, &mst_buf[8], i_map_size);
1660 /* set up a few of our variables */
1661 p_sys->l_first_ty_pts = p_sys->seq_table[0].l_timestamp;
1662 p_sys->l_final_ty_pts =
1663 p_sys->seq_table[p_sys->i_seq_table_size - 1].l_timestamp;
1664 p_sys->b_have_master = true;
1666 i_pts_secs = p_sys->l_first_ty_pts / 1000000000;
1668 "first TY pts in master is %02"PRId64":%02"PRId64":%02"PRId64,
1669 i_pts_secs / 3600, (i_pts_secs / 60) % 60, i_pts_secs % 60 );
1670 i_pts_secs = p_sys->l_final_ty_pts / 1000000000;
1672 "final TY pts in master is %02"PRId64":%02"PRId64":%02"PRId64,
1673 i_pts_secs / 3600, (i_pts_secs / 60) % 60, i_pts_secs % 60 );
1675 /* seek past this chunk */
1676 stream_Seek(p_demux->s, i_save_pos + CHUNK_SIZE);
1680 /* ======================================================================== */
1681 /* "Peek" at some chunks. Skip over the Part header if we find it.
1682 * We parse the peeked data and determine audio type,
1683 * SA vs. DTivo, & Tivo Series.
1684 * Set global vars i_Pes_Length, i_Pts_Offset,
1685 * p_sys->tivo_series, p_sys->tivo_type, p_sys->audio_type */
1686 static int probe_stream(demux_t *p_demux)
1688 demux_sys_t *p_sys = p_demux->p_sys;
1689 const uint8_t *p_buf;
1691 bool b_probe_error = false;
1693 /* we need CHUNK_PEEK_COUNT chunks of data, first one might be a Part header, so ... */
1694 if (stream_Peek( p_demux->s, &p_buf, CHUNK_PEEK_COUNT * CHUNK_SIZE ) <
1695 CHUNK_PEEK_COUNT * CHUNK_SIZE) {
1696 msg_Err(p_demux, "Can't peek %d chunks", CHUNK_PEEK_COUNT);
1697 /* TODO: if seekable, then loop reading chunks into a temp buffer */
1698 return VLC_EGENERIC;
1701 /* the real work: analyze this chunk */
1702 for (i = 0; i < CHUNK_PEEK_COUNT; i++) {
1703 analyze_chunk(p_demux, p_buf);
1704 if (p_sys->tivo_series != TIVO_SERIES_UNKNOWN &&
1705 p_sys->audio_type != TIVO_AUDIO_UNKNOWN &&
1706 p_sys->tivo_type != TIVO_TYPE_UNKNOWN)
1708 p_buf += CHUNK_SIZE;
1711 /* the final tally */
1712 if (p_sys->tivo_series == TIVO_SERIES_UNKNOWN) {
1713 msg_Err(p_demux, "Can't determine Tivo Series.");
1714 b_probe_error = true;
1716 if (p_sys->audio_type == TIVO_AUDIO_UNKNOWN) {
1717 msg_Err(p_demux, "Can't determine Tivo Audio Type.");
1718 b_probe_error = true;
1720 if (p_sys->tivo_type == TIVO_TYPE_UNKNOWN) {
1721 msg_Err(p_demux, "Can't determine Tivo Type (SA/DTivo).");
1722 b_probe_error = true;
1724 return b_probe_error?VLC_EGENERIC:VLC_SUCCESS;
1728 /* ======================================================================== */
1729 /* gather statistics for this chunk & set our tivo-type vars accordingly */
1730 static void analyze_chunk(demux_t *p_demux, const uint8_t *p_chunk)
1732 demux_sys_t *p_sys = p_demux->p_sys;
1734 ty_rec_hdr_t *p_hdrs;
1735 int i_num_6e0, i_num_be0, i_num_9c0, i_num_3c0;
1738 /* skip if it's a Part header */
1739 if( U32_AT( &p_chunk[ 0 ] ) == TIVO_PES_FILEID )
1742 /* number of records in chunk (we ignore high order byte;
1743 * rarely are there > 256 chunks & we don't need that many anyway) */
1744 i_num_recs = p_chunk[0];
1745 if (i_num_recs < 5) {
1746 /* try again with the next chunk. Sometimes there are dead ones */
1750 p_chunk += 4; /* skip past rec count & SEQ bytes */
1751 //msg_Dbg(p_demux, "probe: chunk has %d recs", i_num_recs);
1752 p_hdrs = parse_chunk_headers(p_chunk, i_num_recs, &i_payload_size);
1754 * 1. check video packets. Presence of 0x6e0 means S1.
1755 * No 6e0 but have be0 means S2.
1756 * 2. probe for audio 0x9c0 vs 0x3c0 (AC3 vs Mpeg)
1757 * If AC-3, then we have DTivo.
1758 * If MPEG, search for PTS offset. This will determine SA vs. DTivo.
1760 i_num_6e0 = i_num_be0 = i_num_9c0 = i_num_3c0 = 0;
1761 for (i=0; i<i_num_recs; i++) {
1762 //msg_Dbg(p_demux, "probe: rec is %d/%d = 0x%04x", p_hdrs[i].subrec_type,
1763 //p_hdrs[i].rec_type,
1764 //p_hdrs[i].subrec_type << 8 | p_hdrs[i].rec_type);
1765 switch (p_hdrs[i].subrec_type << 8 | p_hdrs[i].rec_type) {
1780 msg_Dbg(p_demux, "probe: chunk has %d 0x6e0 recs, %d 0xbe0 recs.",
1781 i_num_6e0, i_num_be0);
1783 /* set up our variables */
1784 if (i_num_6e0 > 0) {
1785 msg_Dbg(p_demux, "detected Series 1 Tivo");
1786 p_sys->tivo_series = TIVO_SERIES1;
1787 p_sys->i_Pes_Length = SERIES1_PES_LENGTH;
1788 } else if (i_num_be0 > 0) {
1789 msg_Dbg(p_demux, "detected Series 2 Tivo");
1790 p_sys->tivo_series = TIVO_SERIES2;
1791 p_sys->i_Pes_Length = SERIES2_PES_LENGTH;
1793 if (i_num_9c0 > 0) {
1794 msg_Dbg(p_demux, "detected AC-3 Audio (DTivo)" );
1795 p_sys->audio_type = TIVO_AUDIO_AC3;
1796 p_sys->tivo_type = TIVO_TYPE_DTIVO;
1797 p_sys->i_Pts_Offset = AC3_PTS_OFFSET;
1798 p_sys->i_Pes_Length = AC3_PES_LENGTH;
1799 } else if (i_num_3c0 > 0) {
1800 p_sys->audio_type = TIVO_AUDIO_MPEG;
1801 msg_Dbg(p_demux, "detected MPEG Audio" );
1804 /* if tivo_type still unknown, we can check PTS location
1805 * in MPEG packets to determine tivo_type */
1806 if (p_sys->tivo_type == TIVO_TYPE_UNKNOWN) {
1807 uint32_t i_data_offset = (16 * i_num_recs);
1808 for (i=0; i<i_num_recs; i++) {
1809 if ((p_hdrs[i].subrec_type << 0x08 | p_hdrs[i].rec_type) == 0x3c0 &&
1810 p_hdrs[i].l_rec_size > 15) {
1811 /* first make sure we're aligned */
1812 int i_pes_offset = find_es_header(ty_MPEGAudioPacket,
1813 &p_chunk[i_data_offset], 5);
1814 if (i_pes_offset >= 0) {
1815 /* pes found. on SA, PES has hdr data at offset 6, not PTS. */
1816 //msg_Dbg(p_demux, "probe: mpeg es header found in rec %d at offset %d",
1818 if ((p_chunk[i_data_offset + 6 + i_pes_offset] & 0x80) == 0x80) {
1819 /* S1SA or S2(any) Mpeg Audio (PES hdr, not a PTS start) */
1820 if (p_sys->tivo_series == TIVO_SERIES1)
1821 msg_Dbg(p_demux, "detected Stand-Alone Tivo" );
1822 p_sys->tivo_type = TIVO_TYPE_SA;
1823 p_sys->i_Pts_Offset = SA_PTS_OFFSET;
1825 if (p_sys->tivo_series == TIVO_SERIES1)
1826 msg_Dbg(p_demux, "detected DirecTV Tivo" );
1827 p_sys->tivo_type = TIVO_TYPE_DTIVO;
1828 p_sys->i_Pts_Offset = DTIVO_PTS_OFFSET;
1833 i_data_offset += p_hdrs[i].l_rec_size;
1840 /* =========================================================================== */
1841 static int get_chunk_header(demux_t *p_demux)
1843 int i_readSize, i_num_recs;
1845 const uint8_t *p_peek;
1846 demux_sys_t *p_sys = p_demux->p_sys;
1847 int i_payload_size; /* sum of all records' sizes */
1849 msg_Dbg(p_demux, "parsing ty chunk #%d", p_sys->i_cur_chunk );
1851 /* if we have left-over filler space from the last chunk, get that */
1852 if (p_sys->i_stuff_cnt > 0) {
1853 stream_Read( p_demux->s, NULL, p_sys->i_stuff_cnt);
1854 p_sys->i_stuff_cnt = 0;
1857 /* read the TY packet header */
1858 i_readSize = stream_Peek( p_demux->s, &p_peek, 4 );
1859 p_sys->i_cur_chunk++;
1861 if ( (i_readSize < 4) || ( U32_AT(&p_peek[ 0 ] ) == 0 ))
1868 /* check if it's a PART Header */
1869 if( U32_AT( &p_peek[ 0 ] ) == TIVO_PES_FILEID )
1871 /* parse master chunk */
1872 parse_master(p_demux);
1873 return get_chunk_header(p_demux);
1876 /* number of records in chunk (8- or 16-bit number) */
1877 if (p_peek[3] & 0x80)
1879 /* 16 bit rec cnt */
1880 p_sys->i_num_recs = i_num_recs = (p_peek[1] << 8) + p_peek[0];
1881 p_sys->i_seq_rec = (p_peek[3] << 8) + p_peek[2];
1882 if (p_sys->i_seq_rec != 0xffff)
1884 p_sys->i_seq_rec &= ~0x8000;
1889 /* 8 bit reclen - tivo 1.3 format */
1890 p_sys->i_num_recs = i_num_recs = p_peek[0];
1891 p_sys->i_seq_rec = p_peek[1];
1893 p_sys->i_cur_rec = 0;
1894 p_sys->b_first_chunk = false;
1896 /*msg_Dbg( p_demux, "chunk has %d records", i_num_recs );*/
1898 free(p_sys->rec_hdrs);
1900 /* skip past the 4 bytes we "peeked" earlier */
1901 stream_Read( p_demux->s, NULL, 4 );
1903 /* read the record headers into a temp buffer */
1904 p_hdr_buf = malloc(i_num_recs * 16);
1905 if (stream_Read(p_demux->s, p_hdr_buf, i_num_recs * 16) < i_num_recs * 16) {
1911 p_sys->rec_hdrs = parse_chunk_headers(p_hdr_buf, i_num_recs,
1915 p_sys->i_stuff_cnt = CHUNK_SIZE - 4 -
1916 (p_sys->i_num_recs * 16) - i_payload_size;
1917 if (p_sys->i_stuff_cnt > 0)
1918 msg_Dbg( p_demux, "chunk has %d stuff bytes at end",
1919 p_sys->i_stuff_cnt );
1924 static ty_rec_hdr_t *parse_chunk_headers( const uint8_t *p_buf,
1925 int i_num_recs, int *pi_payload_size)
1928 ty_rec_hdr_t *p_hdrs, *p_rec_hdr;
1930 *pi_payload_size = 0;
1931 p_hdrs = malloc(i_num_recs * sizeof(ty_rec_hdr_t));
1933 for (i = 0; i < i_num_recs; i++)
1935 const uint8_t *record_header = p_buf + (i * 16);
1936 p_rec_hdr = &p_hdrs[i]; /* for brevity */
1937 p_rec_hdr->rec_type = record_header[3];
1938 p_rec_hdr->subrec_type = record_header[2] & 0x0f;
1939 if ((record_header[ 0 ] & 0x80) == 0x80)
1942 /* marker bit 2 set, so read extended data */
1943 b1 = ( ( ( record_header[ 0 ] & 0x0f ) << 4 ) |
1944 ( ( record_header[ 1 ] & 0xf0 ) >> 4 ) );
1946 b2 = ( ( ( record_header[ 1 ] & 0x0f ) << 4 ) |
1947 ( ( record_header[ 2 ] & 0xf0 ) >> 4 ) );
1950 p_rec_hdr->ex1 = b1;
1951 p_rec_hdr->ex2 = b2;
1952 p_rec_hdr->l_rec_size = 0;
1953 p_rec_hdr->l_ty_pts = 0;
1954 p_rec_hdr->b_ext = true;
1958 p_rec_hdr->l_rec_size = ( record_header[ 0 ] << 8 |
1959 record_header[ 1 ] ) << 4 | ( record_header[ 2 ] >> 4 );
1960 *pi_payload_size += p_rec_hdr->l_rec_size;
1961 p_rec_hdr->b_ext = false;
1962 p_rec_hdr->l_ty_pts = U64_AT( &record_header[ 8 ] );
1964 //fprintf( stderr, "parse_chunk_headers[%d] t=0x%x s=%d\n", i, p_rec_hdr->rec_type, p_rec_hdr->subrec_type );
1965 } /* end of record-header loop */