sd_ass: initialize structs for external tracks properly
[mplayer.git] / libmpdemux / demux_ty.c
blob527d350bc615e9757cbf831a20fc82612fe90613
1 /*
2 * tivo@wingert.org, February 2003
4 * Copyright (C) 2003 Christopher R. Wingert
6 * The license covers the portions of this file regarding TiVo additions.
8 * Olaf Beck and Tridge (indirectly) were essential at providing
9 * information regarding the format of the TiVo streams.
11 * However, no code in the following subsection is directly copied from
12 * either author.
14 * This file is part of MPlayer.
16 * MPlayer is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * MPlayer is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <stdarg.h>
38 #include <libavutil/avstring.h>
39 #include <libavutil/intreadwrite.h>
41 #include "config.h"
42 #include "mp_msg.h"
44 #include "libmpcodecs/dec_audio.h"
45 #include "stream/stream.h"
46 #include "demuxer.h"
47 #ifdef DEMUX_TY_OSD
48 #include "demux_ty_osd.h"
49 #endif
50 #include "parse_es.h"
51 #include "stheader.h"
52 #include "sub/sub_cc.h"
53 #include "sub/sub.h"
55 // 2/c0: audio data
56 // 3/c0: audio packet header (PES header)
57 // 4/c0: audio data (S/A only?)
58 // 9/c0: audio packet header, AC-3 audio
59 // 2/e0: video data
60 // 6/e0: video packet header (PES header)
61 // 7/e0: video sequence header start
62 // 8/e0: video I-frame header start
63 // a/e0: video P-frame header start
64 // b/e0: video B-frame header start
65 // c/e0: video GOP header start
66 // e/01: closed-caption data
67 // e/02: Extended data services data
70 #define TIVO_PES_FILEID 0xf5467abd
71 #define TIVO_PART_LENGTH 0x20000000
73 #define CHUNKSIZE ( 128 * 1024 )
74 #define MAX_AUDIO_BUFFER ( 16 * 1024 )
76 #define TY_V 1
77 #define TY_A 2
79 typedef struct
81 off_t startOffset;
82 off_t fileSize;
83 int chunks;
84 } tmf_fileParts;
86 #define MAX_TMF_PARTS 16
88 typedef struct
90 int whichChunk;
91 unsigned char chunk[ CHUNKSIZE ];
93 unsigned char lastAudio[ MAX_AUDIO_BUFFER ];
94 int lastAudioEnd;
96 int tivoType; // 1 = SA, 2 = DTiVo
98 int64_t lastAudioPTS;
99 int64_t lastVideoPTS;
101 off_t size;
102 int readHeader;
104 int tmf;
105 tmf_fileParts tmfparts[ MAX_TMF_PARTS ];
106 int tmf_totalparts;
107 } TiVoInfo;
109 // ===========================================================================
110 #define TMF_SIG "showing.xml"
112 // ===========================================================================
113 static int ty_tmf_filetoparts( demuxer_t *demux, TiVoInfo *tivo )
115 int parts = 0;
117 stream_seek(demux->stream, 0);
119 mp_msg( MSGT_DEMUX, MSGL_DBG3, "Dumping tar contents\n" );
120 while (!demux->stream->eof)
122 char header[ 512 ];
123 char *name;
124 char *extension;
125 char *sizestr;
126 int size;
127 off_t skip;
128 if (stream_read(demux->stream, header, 512) < 512)
130 mp_msg( MSGT_DEMUX, MSGL_DBG3, "Read bad\n" );
131 break;
133 name = header;
134 name[99] = 0;
135 sizestr = &header[124];
136 sizestr[11] = 0;
137 size = strtol(sizestr, NULL, 8);
139 mp_msg( MSGT_DEMUX, MSGL_DBG3, "name %-20.20s size %-12.12s %d\n",
140 name, sizestr, size );
142 extension = strrchr(name, '.');
143 if (extension && strcmp(extension, ".ty") == 0)
145 if ( parts >= MAX_TMF_PARTS ) {
146 mp_msg( MSGT_DEMUX, MSGL_ERR, "ty:tmf too big\n" );
147 break;
149 tivo->tmfparts[ parts ].fileSize = size;
150 tivo->tmfparts[ parts ].startOffset = stream_tell(demux->stream);
151 tivo->tmfparts[ parts ].chunks = size / CHUNKSIZE;
152 mp_msg(MSGT_DEMUX, MSGL_DBG3,
153 "tmf_filetoparts(): index %d, chunks %d\n"
154 "tmf_filetoparts(): size %"PRId64"\n"
155 "tmf_filetoparts(): startOffset %"PRId64"\n",
156 parts, tivo->tmfparts[ parts ].chunks,
157 tivo->tmfparts[ parts ].fileSize, tivo->tmfparts[ parts ].startOffset
159 parts++;
162 // size rounded up to blocks
163 skip = (size + 511) & ~511;
164 stream_skip(demux->stream, skip);
166 stream_reset(demux->stream);
167 tivo->tmf_totalparts = parts;
168 mp_msg( MSGT_DEMUX, MSGL_DBG3,
169 "tmf_filetoparts(): No More Part Files %d\n", parts );
171 return 1;
175 // ===========================================================================
176 static off_t tmf_filetooffset(TiVoInfo *tivo, int chunk)
178 int i;
179 for (i = 0; i < tivo->tmf_totalparts; i++) {
180 if (chunk < tivo->tmfparts[i].chunks)
181 return tivo->tmfparts[i].startOffset + chunk * CHUNKSIZE;
182 chunk -= tivo->tmfparts[i].chunks;
184 return -1;
188 // ===========================================================================
189 static int tmf_load_chunk( demuxer_t *demux, TiVoInfo *tivo,
190 unsigned char *buff, int readChunk )
192 off_t fileoffset;
193 int count;
195 mp_msg( MSGT_DEMUX, MSGL_DBG3, "\ntmf_load_chunk() begin %d\n",
196 readChunk );
198 fileoffset = tmf_filetooffset(tivo, readChunk);
200 if (fileoffset == -1 || !stream_seek(demux->stream, fileoffset)) {
201 mp_msg( MSGT_DEMUX, MSGL_ERR, "Read past EOF()\n" );
202 return 0;
204 count = stream_read( demux->stream, buff, CHUNKSIZE );
205 demux->filepos = stream_tell( demux->stream );
207 mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() count %x\n",
208 count );
210 mp_msg( MSGT_DEMUX, MSGL_DBG3,
211 "tmf_load_chunk() bytes %x %x %x %x %x %x %x %x\n",
212 buff[ 0 ], buff[ 1 ], buff[ 2 ], buff[ 3 ],
213 buff[ 4 ], buff[ 5 ], buff[ 6 ], buff[ 7 ] );
215 mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() end\n" );
217 return count;
220 // ===========================================================================
222 // DTiVo MPEG 336, 480, 576, 768
223 // SA TiVo 864
224 // DTiVo AC-3 1550
226 #define SERIES1_PTS_LENGTH 11
227 #define SERIES1_PTS_OFFSET 6
228 #define SERIES2_PTS_LENGTH 16
229 #define SERIES2_PTS_OFFSET 9
230 #define AC3_PTS_LENGTH 16
231 #define AC3_PTS_OFFSET 9
233 static int IsValidAudioPacket( int size, int *ptsOffset, int *ptsLen )
235 // AC-3
236 if ( size == 1550 || size == 1552 )
238 *ptsOffset = AC3_PTS_OFFSET;
239 *ptsLen = AC3_PTS_LENGTH;
240 return 1;
243 // MPEG
244 if ( (size & 15) == (SERIES1_PTS_LENGTH & 15) )
246 *ptsOffset = SERIES1_PTS_OFFSET;
247 *ptsLen = SERIES1_PTS_LENGTH;
248 return 1;
250 if ( (size & 15) == (SERIES2_PTS_LENGTH & 15) )
252 *ptsOffset = SERIES2_PTS_OFFSET;
253 *ptsLen = SERIES2_PTS_LENGTH;
254 return 1;
256 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Tossing Audio Packet Size %d\n",
257 size );
258 return 0;
262 static int64_t get_ty_pts( unsigned char *buf )
264 int a = buf[0] & 0xe;
265 int b = AV_RB16(buf + 1);
266 int c = AV_RB16(buf + 3);
268 if (!(1 & a & b & c)) // invalid MPEG timestamp
269 return MP_NOPTS_VALUE;
270 a >>= 1; b >>= 1; c >>= 1;
271 return (((uint64_t)a) << 30) | (b << 15) | c;
274 static void demux_ty_AddToAudioBuffer( TiVoInfo *tivo, unsigned char *buffer,
275 int size )
277 if ( tivo->lastAudioEnd + size < MAX_AUDIO_BUFFER )
279 memcpy( &tivo->lastAudio[ tivo->lastAudioEnd ],
280 buffer, size );
281 tivo->lastAudioEnd += size;
283 else
284 mp_msg( MSGT_DEMUX, MSGL_ERR,
285 "ty:WARNING - Would have blown my audio buffer\n" );
288 static void demux_ty_CopyToDemuxPacket( demux_stream_t *ds,
289 unsigned char *buffer, int size, off_t pos, int64_t pts )
291 demux_packet_t *dp = new_demux_packet( size );
292 memcpy( dp->buffer, buffer, size );
293 if (pts != MP_NOPTS_VALUE)
294 dp->pts = pts / 90000.0;
295 dp->pos = pos;
296 ds_add_packet( ds, dp );
299 static int demux_ty_FindESHeader( uint8_t nal,
300 unsigned char *buffer, int bufferSize )
302 uint32_t search = 0x00000100 | nal;
303 uint32_t found = -1;
304 uint8_t *p = buffer;
305 uint8_t *end = p + bufferSize;
306 while (p < end) {
307 found <<= 8;
308 found |= *p++;
309 if (found == search)
310 return p - buffer - 4;
312 return -1;
315 static void demux_ty_FindESPacket( uint8_t nal,
316 unsigned char *buffer, int bufferSize, int *esOffset1, int *esOffset2 )
318 *esOffset1 = demux_ty_FindESHeader(nal, buffer, bufferSize);
319 if (*esOffset1 == -1) {
320 *esOffset2 = -1;
321 return;
323 buffer += *esOffset1 + 1;
324 bufferSize -= *esOffset1 + 1;
325 *esOffset2 = demux_ty_FindESHeader(nal, buffer, bufferSize);
326 if (*esOffset2 != -1)
327 *esOffset2 += *esOffset1 + 1;
330 #define VIDEO_NAL 0xe0
331 #define AUDIO_NAL 0xc0
332 #define AC3_NAL 0xbd
334 static int demux_ty_fill_buffer( demuxer_t *demux, demux_stream_t *dsds )
336 int invalidType = 0;
337 int errorHeader = 0;
338 int recordsDecoded = 0;
340 int readSize;
342 int numberRecs;
343 unsigned char *recPtr;
344 int offset;
346 int counter;
348 int aid;
350 TiVoInfo *tivo = demux->priv;
351 unsigned char *chunk = tivo->chunk;
353 if ( demux->stream->type == STREAMTYPE_DVD )
354 return 0;
356 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty processing\n" );
358 if( demux->stream->eof ) return 0;
360 // ======================================================================
361 // If we haven't figured out the size of the stream, let's do so
362 // ======================================================================
363 if ( demux->stream->type == STREAMTYPE_VSTREAM )
365 // The vstream code figures out the exact size of the stream
366 demux->movi_start = 0;
367 demux->movi_end = demux->stream->end_pos;
368 tivo->size = demux->stream->end_pos;
370 else
372 // If its a local file, try to find the Part Headers, so we can
373 // calculate the ACTUAL stream size
374 // If we can't find it, go off with the file size and hope the
375 // extract program did the "right thing"
376 if ( tivo->readHeader == 0 )
378 off_t filePos;
379 tivo->readHeader = 1;
381 filePos = demux->filepos;
382 stream_seek( demux->stream, 0 );
384 readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
386 if ( memcmp( chunk, TMF_SIG, sizeof( TMF_SIG ) ) == 0 )
388 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Detected a tmf\n" );
389 tivo->tmf = 1;
390 ty_tmf_filetoparts( demux, tivo );
391 readSize = tmf_load_chunk( demux, tivo, chunk, 0 );
394 if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID )
396 off_t numberParts;
398 readSize = 0;
400 if ( tivo->tmf != 1 )
402 off_t offset;
404 numberParts = demux->stream->end_pos / TIVO_PART_LENGTH;
405 offset = numberParts * TIVO_PART_LENGTH;
407 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty/ty+Number Parts %"PRId64"\n",
408 (int64_t)numberParts );
410 if ( offset + CHUNKSIZE < demux->stream->end_pos )
412 stream_seek( demux->stream, offset );
413 readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
416 else
418 numberParts = tivo->tmf_totalparts;
419 offset = numberParts * TIVO_PART_LENGTH;
420 readSize = tmf_load_chunk( demux, tivo, chunk,
421 numberParts * ( TIVO_PART_LENGTH - CHUNKSIZE ) /
422 CHUNKSIZE );
425 if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID )
427 int size = AV_RB24(chunk + 12);
428 size -= 4;
429 size *= CHUNKSIZE;
430 tivo->size = numberParts * TIVO_PART_LENGTH;
431 tivo->size += size;
432 mp_msg( MSGT_DEMUX, MSGL_DBG3,
433 "ty:Header Calc Stream Size %"PRId64"\n", tivo->size );
437 if ( demux->stream->start_pos > 0 )
438 filePos = demux->stream->start_pos;
439 stream_seek( demux->stream, filePos );
440 demux->filepos = stream_tell( demux->stream );
441 tivo->whichChunk = filePos / CHUNKSIZE;
443 demux->movi_start = 0;
444 demux->movi_end = tivo->size;
447 // ======================================================================
448 // Give a clue as to where we are in the stream
449 // ======================================================================
450 mp_msg( MSGT_DEMUX, MSGL_DBG3,
451 "ty:ty header size %"PRIx64"\n", (int64_t)tivo->size );
452 mp_msg( MSGT_DEMUX, MSGL_DBG3,
453 "ty:ty which Chunk %d\n", tivo->whichChunk );
454 mp_msg( MSGT_DEMUX, MSGL_DBG3,
455 "ty:file end_pos %"PRIx64"\n", (int64_t)demux->stream->end_pos );
456 mp_msg( MSGT_DEMUX, MSGL_DBG3,
457 "\nty:wanted current offset %"PRIx64"\n", (int64_t)stream_tell( demux->stream ) );
459 if ( tivo->size > 0 && stream_tell( demux->stream ) > tivo->size )
461 demux->stream->eof = 1;
462 return 0;
465 do {
466 if ( tivo->tmf != 1 )
468 // Make sure we are on a 128k boundary
469 if ( demux->filepos % CHUNKSIZE != 0 )
471 int whichChunk = demux->filepos / CHUNKSIZE;
472 if ( demux->filepos % CHUNKSIZE > CHUNKSIZE / 2 )
473 whichChunk++;
474 stream_seek( demux->stream, whichChunk * CHUNKSIZE );
477 demux->filepos = stream_tell( demux->stream );
478 tivo->whichChunk = demux->filepos / CHUNKSIZE;
479 readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
480 if ( readSize != CHUNKSIZE )
481 return 0;
483 else
485 readSize = tmf_load_chunk( demux, tivo, chunk, tivo->whichChunk );
486 if ( readSize != CHUNKSIZE )
487 return 0;
488 tivo->whichChunk++;
490 if (AV_RB32(chunk) == TIVO_PES_FILEID)
491 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Skipping PART Header\n" );
492 } while (AV_RB32(chunk) == TIVO_PES_FILEID);
494 mp_msg( MSGT_DEMUX, MSGL_DBG3,
495 "\nty:actual current offset %"PRIx64"\n", stream_tell( demux->stream ) -
496 CHUNKSIZE );
499 // Let's make a Video Demux Stream for MPlayer
500 aid = 0x0;
501 if( !demux->v_streams[ aid ] ) new_sh_video( demux, aid );
502 if( demux->video->id == -1 ) demux->video->id = aid;
503 if( demux->video->id == aid )
505 demux_stream_t *ds = demux->video;
506 if( !ds->sh ) ds->sh = demux->v_streams[ aid ];
509 // ======================================================================
510 // Finally, we get to actually parse the chunk
511 // ======================================================================
512 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty parsing a chunk\n" );
513 numberRecs = chunk[ 0 ];
514 recPtr = &chunk[ 4 ];
515 offset = numberRecs * 16 + 4;
516 for ( counter = 0 ; counter < numberRecs ; counter++ )
518 int size = AV_RB24(recPtr) >> 4;
519 int type = recPtr[ 3 ];
520 int nybbleType = recPtr[ 2 ] & 0x0f;
521 recordsDecoded++;
523 mp_msg( MSGT_DEMUX, MSGL_DBG3,
524 "ty:Record Type %x/%x %d\n", nybbleType, type, size );
526 // ================================================================
527 // Video Parsing
528 // ================================================================
529 if ( type == 0xe0 )
531 if ( size > 0 && size + offset <= CHUNKSIZE )
533 int esOffset1 = demux_ty_FindESHeader( VIDEO_NAL, &chunk[ offset ],
534 size);
535 if ( esOffset1 != -1 )
536 tivo->lastVideoPTS = get_ty_pts(
537 &chunk[ offset + esOffset1 + 9 ] );
539 // Do NOT Pass the PES Header onto the MPEG2 Decode
540 if( nybbleType != 0x06 )
541 demux_ty_CopyToDemuxPacket( demux->video,
542 &chunk[ offset ], size, demux->filepos + offset,
543 tivo->lastVideoPTS );
544 offset += size;
546 else
547 errorHeader++;
549 // ================================================================
550 // Audio Parsing
551 // ================================================================
552 else if ( type == 0xc0 )
554 if ( size > 0 && size + offset <= CHUNKSIZE )
556 if( demux->audio->id == -1 )
558 if ( nybbleType == 0x02 )
559 continue; // DTiVo inconclusive, wait for more
560 else if ( nybbleType == 0x09 )
562 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting AC-3 Audio\n" );
563 aid = 0x80; // AC-3
565 else
567 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting MPEG Audio\n" );
568 aid = 0x0; // MPEG Audio
571 demux->audio->id = aid;
572 if( !demux->a_streams[ aid ] ) new_sh_audio( demux, aid );
573 if( demux->audio->id == aid )
575 demux_stream_t *ds = demux->audio;
576 if( !ds->sh ) {
577 sh_audio_t* sh_a;
578 ds->sh = demux->a_streams[ aid ];
579 sh_a = (sh_audio_t*)ds->sh;
580 switch(aid & 0xE0){ // 1110 0000 b (high 3 bit: type low 5: id)
581 case 0x00: sh_a->format=0x50;break; // mpeg
582 case 0xA0: sh_a->format=0x10001;break; // dvd pcm
583 case 0x80: if((aid & 0xF8) == 0x88) sh_a->format=0x2001;//dts
584 else sh_a->format=0x2000;break; // ac3
590 aid = demux->audio->id;
593 // SA DTiVo Audio Data, no PES
594 // ================================================
595 if ( nybbleType == 0x02 || nybbleType == 0x04 )
597 if ( nybbleType == 0x02 && tivo->tivoType == 2 )
598 demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
599 else
602 mp_msg( MSGT_DEMUX, MSGL_DBG3,
603 "ty:Adding Audio Packet Size %d\n", size );
604 demux_ty_CopyToDemuxPacket( demux->audio,
605 &chunk[ offset ], size, ( demux->filepos + offset ),
606 tivo->lastAudioPTS );
610 // 3 - MPEG Audio with PES Header, either SA or DTiVo
611 // 9 - DTiVo AC3 Audio Data with PES Header
612 // ================================================
613 if ( nybbleType == 0x03 || nybbleType == 0x09 )
615 int esOffset1, esOffset2;
616 if ( nybbleType == 0x03 )
617 esOffset1 = demux_ty_FindESHeader( AUDIO_NAL, &chunk[ offset ],
618 size);
620 // SA PES Header, No Audio Data
621 // ================================================
622 if ( nybbleType == 0x03 && esOffset1 == 0 && size == 16 )
624 tivo->tivoType = 1;
625 tivo->lastAudioPTS = get_ty_pts( &chunk[ offset +
626 SERIES2_PTS_OFFSET ] );
628 else
629 // DTiVo Audio with PES Header
630 // ================================================
632 tivo->tivoType = 2;
634 demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
635 demux_ty_FindESPacket( nybbleType == 9 ? AC3_NAL : AUDIO_NAL,
636 tivo->lastAudio, tivo->lastAudioEnd, &esOffset1,
637 &esOffset2 );
639 if ( esOffset1 != -1 && esOffset2 != -1 )
641 int packetSize = esOffset2 - esOffset1;
642 int headerSize;
643 int ptsOffset;
645 if ( IsValidAudioPacket( packetSize, &ptsOffset,
646 &headerSize ) )
648 mp_msg( MSGT_DEMUX, MSGL_DBG3,
649 "ty:Adding DTiVo Audio Packet Size %d\n",
650 packetSize );
652 tivo->lastAudioPTS = get_ty_pts(
653 &tivo->lastAudio[ esOffset1 + ptsOffset ] );
655 if (nybbleType == 9) headerSize = 0;
656 demux_ty_CopyToDemuxPacket
658 demux->audio,
659 &tivo->lastAudio[ esOffset1 + headerSize ],
660 packetSize - headerSize,
661 demux->filepos + offset,
662 tivo->lastAudioPTS
667 // Collapse the Audio Buffer
668 tivo->lastAudioEnd -= esOffset2;
669 memmove( &tivo->lastAudio[ 0 ],
670 &tivo->lastAudio[ esOffset2 ],
671 tivo->lastAudioEnd );
676 offset += size;
678 else
679 errorHeader++;
681 // ================================================================
682 // 1 = Closed Caption
683 // 2 = Extended Data Services
684 // ================================================================
685 else if ( type == 0x01 || type == 0x02 )
687 unsigned char lastXDS[ 16 ];
688 int b = AV_RB24(recPtr) >> 4;
689 b &= 0x7f7f;
691 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:%s %04x\n", type == 1 ? "CC" : "XDS", b);
693 lastXDS[ 0x00 ] = 0x00;
694 lastXDS[ 0x01 ] = 0x00;
695 lastXDS[ 0x02 ] = 0x01;
696 lastXDS[ 0x03 ] = 0xb2;
697 lastXDS[ 0x04 ] = 'T';
698 lastXDS[ 0x05 ] = 'Y';
699 lastXDS[ 0x06 ] = type;
700 lastXDS[ 0x07 ] = b >> 8;
701 lastXDS[ 0x08 ] = b;
702 if ( subcc_enabled )
703 demux_ty_CopyToDemuxPacket( demux->video, lastXDS, 0x09,
704 demux->filepos + offset, tivo->lastVideoPTS );
706 // ================================================================
707 // Unknown
708 // ================================================================
709 else
711 if ( size > 0 && size + offset <= CHUNKSIZE )
712 offset += size;
713 if (type != 3 && type != 5 && (type != 0 || size > 0)) {
714 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Invalid Type %x\n", type );
715 invalidType++;
718 recPtr += 16;
721 if ( errorHeader > 0 || invalidType > 0 )
723 mp_msg( MSGT_DEMUX, MSGL_DBG3,
724 "ty:Error Check - Records %d, Parsed %d, Errors %d + %d\n",
725 numberRecs, recordsDecoded, errorHeader, invalidType );
727 // Invalid MPEG ES Size Check
728 if ( errorHeader > numberRecs / 2 )
729 return 0;
731 // Invalid MPEG Stream Type Check
732 if ( invalidType > numberRecs / 2 )
733 return 0;
736 demux->filepos = stream_tell( demux->stream );
738 return 1;
741 static void demux_seek_ty( demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags )
743 demux_stream_t *d_audio = demuxer->audio;
744 demux_stream_t *d_video = demuxer->video;
745 sh_audio_t *sh_audio = d_audio->sh;
746 sh_video_t *sh_video = d_video->sh;
747 off_t newpos;
748 off_t res;
749 TiVoInfo *tivo = demuxer->priv;
751 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Seeking to %7.1f\n", rel_seek_secs );
753 tivo->lastAudioEnd = 0;
754 tivo->lastAudioPTS = MP_NOPTS_VALUE;
755 tivo->lastVideoPTS = MP_NOPTS_VALUE;
757 //================= seek in MPEG ==========================
758 demuxer->filepos = stream_tell( demuxer->stream );
760 newpos = ( flags & SEEK_ABSOLUTE ) ? demuxer->movi_start : demuxer->filepos;
762 if( flags & SEEK_FACTOR )
763 // float seek 0..1
764 newpos += ( demuxer->movi_end - demuxer->movi_start ) * rel_seek_secs;
765 else
767 // time seek (secs)
768 if( ! sh_video->i_bps ) // unspecified or VBR
769 newpos += 2324 * 75 * rel_seek_secs; // 174.3 kbyte/sec
770 else
771 newpos += sh_video->i_bps * rel_seek_secs;
774 if ( newpos < demuxer->movi_start )
776 if( demuxer->stream->type != STREAMTYPE_VCD ) demuxer->movi_start = 0;
777 if( newpos < demuxer->movi_start ) newpos = demuxer->movi_start;
780 res = newpos / CHUNKSIZE;
781 if ( rel_seek_secs >= 0 )
782 newpos = ( res + 1 ) * CHUNKSIZE;
783 else
784 newpos = res * CHUNKSIZE;
786 if ( newpos < 0 )
787 newpos = 0;
789 tivo->whichChunk = newpos / CHUNKSIZE;
791 stream_seek( demuxer->stream, newpos );
793 // re-sync video:
794 videobuf_code_len = 0; // reset ES stream buffer
796 ds_fill_buffer( d_video );
797 if( sh_audio )
798 ds_fill_buffer( d_audio );
800 while( 1 )
802 int i;
803 if( sh_audio && !d_audio->eof && d_video->pts && d_audio->pts )
805 float a_pts = d_audio->pts;
806 a_pts += ( ds_tell_pts( d_audio ) - sh_audio->a_in_buffer_len ) /
807 (float)sh_audio->i_bps;
808 if( d_video->pts > a_pts )
810 skip_audio_frame( sh_audio ); // sync audio
811 continue;
814 i = sync_video_packet( d_video );
815 if( i == 0x1B3 || i == 0x1B8 ) break; // found it!
816 if( !i || !skip_video_packet( d_video ) ) break; // EOF?
818 #ifdef DEMUX_TY_OSD
819 if ( subcc_enabled )
820 ty_ClearOSD( 0 );
821 #endif
824 static int demux_ty_control( demuxer_t *demuxer,int cmd, void *arg )
826 demux_stream_t *d_video = demuxer->video;
827 sh_video_t *sh_video = d_video->sh;
829 switch(cmd)
831 case DEMUXER_CTRL_GET_TIME_LENGTH:
832 if(!sh_video->i_bps) // unspecified or VBR
833 return DEMUXER_CTRL_DONTKNOW;
834 *(double *)arg=
835 (double)demuxer->movi_end-demuxer->movi_start/sh_video->i_bps;
836 return DEMUXER_CTRL_GUESS;
838 case DEMUXER_CTRL_GET_PERCENT_POS:
839 return DEMUXER_CTRL_DONTKNOW;
840 default:
841 return DEMUXER_CTRL_NOTIMPL;
846 static void demux_close_ty( demuxer_t *demux )
848 TiVoInfo *tivo = demux->priv;
850 free( tivo );
851 sub_justify = 0;
855 static int ty_check_file(demuxer_t* demuxer)
857 demuxer->filepos = 0;
858 TiVoInfo *tivo = calloc(1, sizeof(TiVoInfo));
859 demuxer->priv = tivo;
860 return ds_fill_buffer(demuxer->video) ? DEMUXER_TYPE_MPEG_TY : 0;
864 static demuxer_t* demux_open_ty(demuxer_t* demuxer)
866 sh_audio_t *sh_audio=NULL;
867 sh_video_t *sh_video=NULL;
869 sh_video=demuxer->video->sh;sh_video->ds=demuxer->video;
871 if(demuxer->audio->id!=-2) {
872 if(!ds_fill_buffer(demuxer->audio)){
873 mp_msg(MSGT_DEMUXER, MSGL_INFO, "MPEG: %s",
874 mp_gtext("No audio stream found -> no sound.\n"));
875 demuxer->audio->sh=NULL;
876 } else {
877 sh_audio=demuxer->audio->sh;sh_audio->ds=demuxer->audio;
881 return demuxer;
885 const demuxer_desc_t demuxer_desc_mpeg_ty = {
886 "TiVo demuxer",
887 "tivo",
888 "TiVo",
889 "Christopher R. Wingert",
890 "Demux streams from TiVo",
891 DEMUXER_TYPE_MPEG_TY,
892 0, // unsafe autodetect
893 ty_check_file,
894 demux_ty_fill_buffer,
895 demux_open_ty,
896 demux_close_ty,
897 demux_seek_ty,
898 demux_ty_control