demux, vd_ffmpeg: fix demux keyframe flag, set AV_PKT_FLAG_KEY
[mplayer.git] / libmpdemux / demux_ty.c
blobcc9bb8f9020b2f11c1fd3cb60d3fe9fc4be59d77
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"
54 extern int sub_justify;
56 // 2/c0: audio data
57 // 3/c0: audio packet header (PES header)
58 // 4/c0: audio data (S/A only?)
59 // 9/c0: audio packet header, AC-3 audio
60 // 2/e0: video data
61 // 6/e0: video packet header (PES header)
62 // 7/e0: video sequence header start
63 // 8/e0: video I-frame header start
64 // a/e0: video P-frame header start
65 // b/e0: video B-frame header start
66 // c/e0: video GOP header start
67 // e/01: closed-caption data
68 // e/02: Extended data services data
71 #define TIVO_PES_FILEID 0xf5467abd
72 #define TIVO_PART_LENGTH 0x20000000
74 #define CHUNKSIZE ( 128 * 1024 )
75 #define MAX_AUDIO_BUFFER ( 16 * 1024 )
77 #define TY_V 1
78 #define TY_A 2
80 typedef struct
82 off_t startOffset;
83 off_t fileSize;
84 int chunks;
85 } tmf_fileParts;
87 #define MAX_TMF_PARTS 16
89 typedef struct
91 int whichChunk;
92 unsigned char chunk[ CHUNKSIZE ];
94 unsigned char lastAudio[ MAX_AUDIO_BUFFER ];
95 int lastAudioEnd;
97 int tivoType; // 1 = SA, 2 = DTiVo
99 int64_t lastAudioPTS;
100 int64_t lastVideoPTS;
102 off_t size;
103 int readHeader;
105 int tmf;
106 tmf_fileParts tmfparts[ MAX_TMF_PARTS ];
107 int tmf_totalparts;
108 } TiVoInfo;
110 // ===========================================================================
111 #define TMF_SIG "showing.xml"
113 // ===========================================================================
114 static int ty_tmf_filetoparts( demuxer_t *demux, TiVoInfo *tivo )
116 int parts = 0;
118 stream_seek(demux->stream, 0);
120 mp_msg( MSGT_DEMUX, MSGL_DBG3, "Dumping tar contents\n" );
121 while (!demux->stream->eof)
123 char header[ 512 ];
124 char *name;
125 char *extension;
126 char *sizestr;
127 int size;
128 off_t skip;
129 if (stream_read(demux->stream, header, 512) < 512)
131 mp_msg( MSGT_DEMUX, MSGL_DBG3, "Read bad\n" );
132 break;
134 name = header;
135 name[99] = 0;
136 sizestr = &header[124];
137 sizestr[11] = 0;
138 size = strtol(sizestr, NULL, 8);
140 mp_msg( MSGT_DEMUX, MSGL_DBG3, "name %-20.20s size %-12.12s %d\n",
141 name, sizestr, size );
143 extension = strrchr(name, '.');
144 if (extension && strcmp(extension, ".ty") == 0)
146 if ( parts >= MAX_TMF_PARTS ) {
147 mp_msg( MSGT_DEMUX, MSGL_ERR, "ty:tmf too big\n" );
148 break;
150 tivo->tmfparts[ parts ].fileSize = size;
151 tivo->tmfparts[ parts ].startOffset = stream_tell(demux->stream);
152 tivo->tmfparts[ parts ].chunks = size / CHUNKSIZE;
153 mp_msg(MSGT_DEMUX, MSGL_DBG3,
154 "tmf_filetoparts(): index %d, chunks %d\n"
155 "tmf_filetoparts(): size %"PRId64"\n"
156 "tmf_filetoparts(): startOffset %"PRId64"\n",
157 parts, tivo->tmfparts[ parts ].chunks,
158 tivo->tmfparts[ parts ].fileSize, tivo->tmfparts[ parts ].startOffset
160 parts++;
163 // size rounded up to blocks
164 skip = (size + 511) & ~511;
165 stream_skip(demux->stream, skip);
167 stream_reset(demux->stream);
168 tivo->tmf_totalparts = parts;
169 mp_msg( MSGT_DEMUX, MSGL_DBG3,
170 "tmf_filetoparts(): No More Part Files %d\n", parts );
172 return 1;
176 // ===========================================================================
177 static off_t tmf_filetooffset(TiVoInfo *tivo, int chunk)
179 int i;
180 for (i = 0; i < tivo->tmf_totalparts; i++) {
181 if (chunk < tivo->tmfparts[i].chunks)
182 return tivo->tmfparts[i].startOffset + chunk * CHUNKSIZE;
183 chunk -= tivo->tmfparts[i].chunks;
185 return -1;
189 // ===========================================================================
190 static int tmf_load_chunk( demuxer_t *demux, TiVoInfo *tivo,
191 unsigned char *buff, int readChunk )
193 off_t fileoffset;
194 int count;
196 mp_msg( MSGT_DEMUX, MSGL_DBG3, "\ntmf_load_chunk() begin %d\n",
197 readChunk );
199 fileoffset = tmf_filetooffset(tivo, readChunk);
201 if (fileoffset == -1 || !stream_seek(demux->stream, fileoffset)) {
202 mp_msg( MSGT_DEMUX, MSGL_ERR, "Read past EOF()\n" );
203 return 0;
205 count = stream_read( demux->stream, buff, CHUNKSIZE );
206 demux->filepos = stream_tell( demux->stream );
208 mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() count %x\n",
209 count );
211 mp_msg( MSGT_DEMUX, MSGL_DBG3,
212 "tmf_load_chunk() bytes %x %x %x %x %x %x %x %x\n",
213 buff[ 0 ], buff[ 1 ], buff[ 2 ], buff[ 3 ],
214 buff[ 4 ], buff[ 5 ], buff[ 6 ], buff[ 7 ] );
216 mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() end\n" );
218 return count;
221 // ===========================================================================
223 // DTiVo MPEG 336, 480, 576, 768
224 // SA TiVo 864
225 // DTiVo AC-3 1550
227 #define SERIES1_PTS_LENGTH 11
228 #define SERIES1_PTS_OFFSET 6
229 #define SERIES2_PTS_LENGTH 16
230 #define SERIES2_PTS_OFFSET 9
231 #define AC3_PTS_LENGTH 16
232 #define AC3_PTS_OFFSET 9
234 static int IsValidAudioPacket( int size, int *ptsOffset, int *ptsLen )
236 // AC-3
237 if ( size == 1550 || size == 1552 )
239 *ptsOffset = AC3_PTS_OFFSET;
240 *ptsLen = AC3_PTS_LENGTH;
241 return 1;
244 // MPEG
245 if ( (size & 15) == (SERIES1_PTS_LENGTH & 15) )
247 *ptsOffset = SERIES1_PTS_OFFSET;
248 *ptsLen = SERIES1_PTS_LENGTH;
249 return 1;
251 if ( (size & 15) == (SERIES2_PTS_LENGTH & 15) )
253 *ptsOffset = SERIES2_PTS_OFFSET;
254 *ptsLen = SERIES2_PTS_LENGTH;
255 return 1;
257 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Tossing Audio Packet Size %d\n",
258 size );
259 return 0;
263 static int64_t get_ty_pts( unsigned char *buf )
265 int a = buf[0] & 0xe;
266 int b = AV_RB16(buf + 1);
267 int c = AV_RB16(buf + 3);
269 if (!(1 & a & b & c)) // invalid MPEG timestamp
270 return MP_NOPTS_VALUE;
271 a >>= 1; b >>= 1; c >>= 1;
272 return (((uint64_t)a) << 30) | (b << 15) | c;
275 static void demux_ty_AddToAudioBuffer( TiVoInfo *tivo, unsigned char *buffer,
276 int size )
278 if ( tivo->lastAudioEnd + size < MAX_AUDIO_BUFFER )
280 memcpy( &tivo->lastAudio[ tivo->lastAudioEnd ],
281 buffer, size );
282 tivo->lastAudioEnd += size;
284 else
285 mp_msg( MSGT_DEMUX, MSGL_ERR,
286 "ty:WARNING - Would have blown my audio buffer\n" );
289 static void demux_ty_CopyToDemuxPacket( demux_stream_t *ds,
290 unsigned char *buffer, int size, off_t pos, int64_t pts )
292 demux_packet_t *dp = new_demux_packet( size );
293 memcpy( dp->buffer, buffer, size );
294 if (pts != MP_NOPTS_VALUE)
295 dp->pts = pts / 90000.0;
296 dp->pos = pos;
297 ds_add_packet( ds, dp );
300 static int demux_ty_FindESHeader( uint8_t nal,
301 unsigned char *buffer, int bufferSize )
303 uint32_t search = 0x00000100 | nal;
304 uint32_t found = -1;
305 uint8_t *p = buffer;
306 uint8_t *end = p + bufferSize;
307 while (p < end) {
308 found <<= 8;
309 found |= *p++;
310 if (found == search)
311 return p - buffer - 4;
313 return -1;
316 static void demux_ty_FindESPacket( uint8_t nal,
317 unsigned char *buffer, int bufferSize, int *esOffset1, int *esOffset2 )
319 *esOffset1 = demux_ty_FindESHeader(nal, buffer, bufferSize);
320 if (*esOffset1 == -1) {
321 *esOffset2 = -1;
322 return;
324 buffer += *esOffset1 + 1;
325 bufferSize -= *esOffset1 + 1;
326 *esOffset2 = demux_ty_FindESHeader(nal, buffer, bufferSize);
327 if (*esOffset2 != -1)
328 *esOffset2 += *esOffset1 + 1;
331 #define VIDEO_NAL 0xe0
332 #define AUDIO_NAL 0xc0
333 #define AC3_NAL 0xbd
335 static int demux_ty_fill_buffer( demuxer_t *demux, demux_stream_t *dsds )
337 int invalidType = 0;
338 int errorHeader = 0;
339 int recordsDecoded = 0;
341 int readSize;
343 int numberRecs;
344 unsigned char *recPtr;
345 int offset;
347 int counter;
349 int aid;
351 TiVoInfo *tivo = demux->priv;
352 unsigned char *chunk = tivo->chunk;
354 if ( demux->stream->type == STREAMTYPE_DVD )
355 return 0;
357 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty processing\n" );
359 if( demux->stream->eof ) return 0;
361 // ======================================================================
362 // If we haven't figured out the size of the stream, let's do so
363 // ======================================================================
364 if ( demux->stream->type == STREAMTYPE_VSTREAM )
366 // The vstream code figures out the exact size of the stream
367 demux->movi_start = 0;
368 demux->movi_end = demux->stream->end_pos;
369 tivo->size = demux->stream->end_pos;
371 else
373 // If its a local file, try to find the Part Headers, so we can
374 // calculate the ACTUAL stream size
375 // If we can't find it, go off with the file size and hope the
376 // extract program did the "right thing"
377 if ( tivo->readHeader == 0 )
379 off_t filePos;
380 tivo->readHeader = 1;
382 filePos = demux->filepos;
383 stream_seek( demux->stream, 0 );
385 readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
387 if ( memcmp( chunk, TMF_SIG, sizeof( TMF_SIG ) ) == 0 )
389 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Detected a tmf\n" );
390 tivo->tmf = 1;
391 ty_tmf_filetoparts( demux, tivo );
392 readSize = tmf_load_chunk( demux, tivo, chunk, 0 );
395 if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID )
397 off_t numberParts;
399 readSize = 0;
401 if ( tivo->tmf != 1 )
403 off_t offset;
405 numberParts = demux->stream->end_pos / TIVO_PART_LENGTH;
406 offset = numberParts * TIVO_PART_LENGTH;
408 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty/ty+Number Parts %"PRId64"\n",
409 (int64_t)numberParts );
411 if ( offset + CHUNKSIZE < demux->stream->end_pos )
413 stream_seek( demux->stream, offset );
414 readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
417 else
419 numberParts = tivo->tmf_totalparts;
420 offset = numberParts * TIVO_PART_LENGTH;
421 readSize = tmf_load_chunk( demux, tivo, chunk,
422 numberParts * ( TIVO_PART_LENGTH - CHUNKSIZE ) /
423 CHUNKSIZE );
426 if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID )
428 int size = AV_RB24(chunk + 12);
429 size -= 4;
430 size *= CHUNKSIZE;
431 tivo->size = numberParts * TIVO_PART_LENGTH;
432 tivo->size += size;
433 mp_msg( MSGT_DEMUX, MSGL_DBG3,
434 "ty:Header Calc Stream Size %"PRId64"\n", tivo->size );
438 if ( demux->stream->start_pos > 0 )
439 filePos = demux->stream->start_pos;
440 stream_seek( demux->stream, filePos );
441 demux->filepos = stream_tell( demux->stream );
442 tivo->whichChunk = filePos / CHUNKSIZE;
444 demux->movi_start = 0;
445 demux->movi_end = tivo->size;
448 // ======================================================================
449 // Give a clue as to where we are in the stream
450 // ======================================================================
451 mp_msg( MSGT_DEMUX, MSGL_DBG3,
452 "ty:ty header size %"PRIx64"\n", (int64_t)tivo->size );
453 mp_msg( MSGT_DEMUX, MSGL_DBG3,
454 "ty:ty which Chunk %d\n", tivo->whichChunk );
455 mp_msg( MSGT_DEMUX, MSGL_DBG3,
456 "ty:file end_pos %"PRIx64"\n", (int64_t)demux->stream->end_pos );
457 mp_msg( MSGT_DEMUX, MSGL_DBG3,
458 "\nty:wanted current offset %"PRIx64"\n", (int64_t)stream_tell( demux->stream ) );
460 if ( tivo->size > 0 && stream_tell( demux->stream ) > tivo->size )
462 demux->stream->eof = 1;
463 return 0;
466 do {
467 if ( tivo->tmf != 1 )
469 // Make sure we are on a 128k boundary
470 if ( demux->filepos % CHUNKSIZE != 0 )
472 int whichChunk = demux->filepos / CHUNKSIZE;
473 if ( demux->filepos % CHUNKSIZE > CHUNKSIZE / 2 )
474 whichChunk++;
475 stream_seek( demux->stream, whichChunk * CHUNKSIZE );
478 demux->filepos = stream_tell( demux->stream );
479 tivo->whichChunk = demux->filepos / CHUNKSIZE;
480 readSize = stream_read( demux->stream, chunk, CHUNKSIZE );
481 if ( readSize != CHUNKSIZE )
482 return 0;
484 else
486 readSize = tmf_load_chunk( demux, tivo, chunk, tivo->whichChunk );
487 if ( readSize != CHUNKSIZE )
488 return 0;
489 tivo->whichChunk++;
491 if (AV_RB32(chunk) == TIVO_PES_FILEID)
492 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Skipping PART Header\n" );
493 } while (AV_RB32(chunk) == TIVO_PES_FILEID);
495 mp_msg( MSGT_DEMUX, MSGL_DBG3,
496 "\nty:actual current offset %"PRIx64"\n", stream_tell( demux->stream ) -
497 CHUNKSIZE );
500 // Let's make a Video Demux Stream for MPlayer
501 aid = 0x0;
502 if( !demux->v_streams[ aid ] ) new_sh_video( demux, aid );
503 if( demux->video->id == -1 ) demux->video->id = aid;
504 if( demux->video->id == aid )
506 demux_stream_t *ds = demux->video;
507 if( !ds->sh ) ds->sh = demux->v_streams[ aid ];
510 // ======================================================================
511 // Finally, we get to actually parse the chunk
512 // ======================================================================
513 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty parsing a chunk\n" );
514 numberRecs = chunk[ 0 ];
515 recPtr = &chunk[ 4 ];
516 offset = numberRecs * 16 + 4;
517 for ( counter = 0 ; counter < numberRecs ; counter++ )
519 int size = AV_RB24(recPtr) >> 4;
520 int type = recPtr[ 3 ];
521 int nybbleType = recPtr[ 2 ] & 0x0f;
522 recordsDecoded++;
524 mp_msg( MSGT_DEMUX, MSGL_DBG3,
525 "ty:Record Type %x/%x %d\n", nybbleType, type, size );
527 // ================================================================
528 // Video Parsing
529 // ================================================================
530 if ( type == 0xe0 )
532 if ( size > 0 && size + offset <= CHUNKSIZE )
534 int esOffset1 = demux_ty_FindESHeader( VIDEO_NAL, &chunk[ offset ],
535 size);
536 if ( esOffset1 != -1 )
537 tivo->lastVideoPTS = get_ty_pts(
538 &chunk[ offset + esOffset1 + 9 ] );
540 // Do NOT Pass the PES Header onto the MPEG2 Decode
541 if( nybbleType != 0x06 )
542 demux_ty_CopyToDemuxPacket( demux->video,
543 &chunk[ offset ], size, demux->filepos + offset,
544 tivo->lastVideoPTS );
545 offset += size;
547 else
548 errorHeader++;
550 // ================================================================
551 // Audio Parsing
552 // ================================================================
553 else if ( type == 0xc0 )
555 if ( size > 0 && size + offset <= CHUNKSIZE )
557 if( demux->audio->id == -1 )
559 if ( nybbleType == 0x02 )
560 continue; // DTiVo inconclusive, wait for more
561 else if ( nybbleType == 0x09 )
563 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting AC-3 Audio\n" );
564 aid = 0x80; // AC-3
566 else
568 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting MPEG Audio\n" );
569 aid = 0x0; // MPEG Audio
572 demux->audio->id = aid;
573 if( !demux->a_streams[ aid ] ) new_sh_audio( demux, aid );
574 if( demux->audio->id == aid )
576 demux_stream_t *ds = demux->audio;
577 if( !ds->sh ) {
578 sh_audio_t* sh_a;
579 ds->sh = demux->a_streams[ aid ];
580 sh_a = (sh_audio_t*)ds->sh;
581 switch(aid & 0xE0){ // 1110 0000 b (high 3 bit: type low 5: id)
582 case 0x00: sh_a->format=0x50;break; // mpeg
583 case 0xA0: sh_a->format=0x10001;break; // dvd pcm
584 case 0x80: if((aid & 0xF8) == 0x88) sh_a->format=0x2001;//dts
585 else sh_a->format=0x2000;break; // ac3
591 aid = demux->audio->id;
594 // SA DTiVo Audio Data, no PES
595 // ================================================
596 if ( nybbleType == 0x02 || nybbleType == 0x04 )
598 if ( nybbleType == 0x02 && tivo->tivoType == 2 )
599 demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
600 else
603 mp_msg( MSGT_DEMUX, MSGL_DBG3,
604 "ty:Adding Audio Packet Size %d\n", size );
605 demux_ty_CopyToDemuxPacket( demux->audio,
606 &chunk[ offset ], size, ( demux->filepos + offset ),
607 tivo->lastAudioPTS );
611 // 3 - MPEG Audio with PES Header, either SA or DTiVo
612 // 9 - DTiVo AC3 Audio Data with PES Header
613 // ================================================
614 if ( nybbleType == 0x03 || nybbleType == 0x09 )
616 int esOffset1, esOffset2;
617 if ( nybbleType == 0x03 )
618 esOffset1 = demux_ty_FindESHeader( AUDIO_NAL, &chunk[ offset ],
619 size);
621 // SA PES Header, No Audio Data
622 // ================================================
623 if ( nybbleType == 0x03 && esOffset1 == 0 && size == 16 )
625 tivo->tivoType = 1;
626 tivo->lastAudioPTS = get_ty_pts( &chunk[ offset +
627 SERIES2_PTS_OFFSET ] );
629 else
630 // DTiVo Audio with PES Header
631 // ================================================
633 tivo->tivoType = 2;
635 demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size );
636 demux_ty_FindESPacket( nybbleType == 9 ? AC3_NAL : AUDIO_NAL,
637 tivo->lastAudio, tivo->lastAudioEnd, &esOffset1,
638 &esOffset2 );
640 if ( esOffset1 != -1 && esOffset2 != -1 )
642 int packetSize = esOffset2 - esOffset1;
643 int headerSize;
644 int ptsOffset;
646 if ( IsValidAudioPacket( packetSize, &ptsOffset,
647 &headerSize ) )
649 mp_msg( MSGT_DEMUX, MSGL_DBG3,
650 "ty:Adding DTiVo Audio Packet Size %d\n",
651 packetSize );
653 tivo->lastAudioPTS = get_ty_pts(
654 &tivo->lastAudio[ esOffset1 + ptsOffset ] );
656 if (nybbleType == 9) headerSize = 0;
657 demux_ty_CopyToDemuxPacket
659 demux->audio,
660 &tivo->lastAudio[ esOffset1 + headerSize ],
661 packetSize - headerSize,
662 demux->filepos + offset,
663 tivo->lastAudioPTS
668 // Collapse the Audio Buffer
669 tivo->lastAudioEnd -= esOffset2;
670 memmove( &tivo->lastAudio[ 0 ],
671 &tivo->lastAudio[ esOffset2 ],
672 tivo->lastAudioEnd );
677 offset += size;
679 else
680 errorHeader++;
682 // ================================================================
683 // 1 = Closed Caption
684 // 2 = Extended Data Services
685 // ================================================================
686 else if ( type == 0x01 || type == 0x02 )
688 unsigned char lastXDS[ 16 ];
689 int b = AV_RB24(recPtr) >> 4;
690 b &= 0x7f7f;
692 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:%s %04x\n", type == 1 ? "CC" : "XDS", b);
694 lastXDS[ 0x00 ] = 0x00;
695 lastXDS[ 0x01 ] = 0x00;
696 lastXDS[ 0x02 ] = 0x01;
697 lastXDS[ 0x03 ] = 0xb2;
698 lastXDS[ 0x04 ] = 'T';
699 lastXDS[ 0x05 ] = 'Y';
700 lastXDS[ 0x06 ] = type;
701 lastXDS[ 0x07 ] = b >> 8;
702 lastXDS[ 0x08 ] = b;
703 if ( subcc_enabled )
704 demux_ty_CopyToDemuxPacket( demux->video, lastXDS, 0x09,
705 demux->filepos + offset, tivo->lastVideoPTS );
707 // ================================================================
708 // Unknown
709 // ================================================================
710 else
712 if ( size > 0 && size + offset <= CHUNKSIZE )
713 offset += size;
714 if (type != 3 && type != 5 && (type != 0 || size > 0)) {
715 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Invalid Type %x\n", type );
716 invalidType++;
719 recPtr += 16;
722 if ( errorHeader > 0 || invalidType > 0 )
724 mp_msg( MSGT_DEMUX, MSGL_DBG3,
725 "ty:Error Check - Records %d, Parsed %d, Errors %d + %d\n",
726 numberRecs, recordsDecoded, errorHeader, invalidType );
728 // Invalid MPEG ES Size Check
729 if ( errorHeader > numberRecs / 2 )
730 return 0;
732 // Invalid MPEG Stream Type Check
733 if ( invalidType > numberRecs / 2 )
734 return 0;
737 demux->filepos = stream_tell( demux->stream );
739 return 1;
742 static void demux_seek_ty( demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags )
744 demux_stream_t *d_audio = demuxer->audio;
745 demux_stream_t *d_video = demuxer->video;
746 sh_audio_t *sh_audio = d_audio->sh;
747 sh_video_t *sh_video = d_video->sh;
748 off_t newpos;
749 off_t res;
750 TiVoInfo *tivo = demuxer->priv;
752 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Seeking to %7.1f\n", rel_seek_secs );
754 tivo->lastAudioEnd = 0;
755 tivo->lastAudioPTS = MP_NOPTS_VALUE;
756 tivo->lastVideoPTS = MP_NOPTS_VALUE;
758 //================= seek in MPEG ==========================
759 demuxer->filepos = stream_tell( demuxer->stream );
761 newpos = ( flags & SEEK_ABSOLUTE ) ? demuxer->movi_start : demuxer->filepos;
763 if( flags & SEEK_FACTOR )
764 // float seek 0..1
765 newpos += ( demuxer->movi_end - demuxer->movi_start ) * rel_seek_secs;
766 else
768 // time seek (secs)
769 if( ! sh_video->i_bps ) // unspecified or VBR
770 newpos += 2324 * 75 * rel_seek_secs; // 174.3 kbyte/sec
771 else
772 newpos += sh_video->i_bps * rel_seek_secs;
775 if ( newpos < demuxer->movi_start )
777 if( demuxer->stream->type != STREAMTYPE_VCD ) demuxer->movi_start = 0;
778 if( newpos < demuxer->movi_start ) newpos = demuxer->movi_start;
781 res = newpos / CHUNKSIZE;
782 if ( rel_seek_secs >= 0 )
783 newpos = ( res + 1 ) * CHUNKSIZE;
784 else
785 newpos = res * CHUNKSIZE;
787 if ( newpos < 0 )
788 newpos = 0;
790 tivo->whichChunk = newpos / CHUNKSIZE;
792 stream_seek( demuxer->stream, newpos );
794 // re-sync video:
795 videobuf_code_len = 0; // reset ES stream buffer
797 ds_fill_buffer( d_video );
798 if( sh_audio )
799 ds_fill_buffer( d_audio );
801 while( 1 )
803 int i;
804 if( sh_audio && !d_audio->eof && d_video->pts && d_audio->pts )
806 float a_pts = d_audio->pts;
807 a_pts += ( ds_tell_pts( d_audio ) - sh_audio->a_in_buffer_len ) /
808 (float)sh_audio->i_bps;
809 if( d_video->pts > a_pts )
811 skip_audio_frame( sh_audio ); // sync audio
812 continue;
815 i = sync_video_packet( d_video );
816 if( i == 0x1B3 || i == 0x1B8 ) break; // found it!
817 if( !i || !skip_video_packet( d_video ) ) break; // EOF?
819 #ifdef DEMUX_TY_OSD
820 if ( subcc_enabled )
821 ty_ClearOSD( 0 );
822 #endif
825 static int demux_ty_control( demuxer_t *demuxer,int cmd, void *arg )
827 demux_stream_t *d_video = demuxer->video;
828 sh_video_t *sh_video = d_video->sh;
830 switch(cmd)
832 case DEMUXER_CTRL_GET_TIME_LENGTH:
833 if(!sh_video->i_bps) // unspecified or VBR
834 return DEMUXER_CTRL_DONTKNOW;
835 *(double *)arg=
836 (double)demuxer->movi_end-demuxer->movi_start/sh_video->i_bps;
837 return DEMUXER_CTRL_GUESS;
839 case DEMUXER_CTRL_GET_PERCENT_POS:
840 return DEMUXER_CTRL_DONTKNOW;
841 default:
842 return DEMUXER_CTRL_NOTIMPL;
847 static void demux_close_ty( demuxer_t *demux )
849 TiVoInfo *tivo = demux->priv;
851 free( tivo );
852 sub_justify = 0;
856 static int ty_check_file(demuxer_t* demuxer)
858 demuxer->filepos = 0;
859 TiVoInfo *tivo = calloc(1, sizeof(TiVoInfo));
860 demuxer->priv = tivo;
861 return ds_fill_buffer(demuxer->video) ? DEMUXER_TYPE_MPEG_TY : 0;
865 static demuxer_t* demux_open_ty(demuxer_t* demuxer)
867 sh_audio_t *sh_audio=NULL;
868 sh_video_t *sh_video=NULL;
870 sh_video=demuxer->video->sh;sh_video->ds=demuxer->video;
872 if(demuxer->audio->id!=-2) {
873 if(!ds_fill_buffer(demuxer->audio)){
874 mp_msg(MSGT_DEMUXER, MSGL_INFO, "MPEG: %s",
875 mp_gtext("No audio stream found -> no sound.\n"));
876 demuxer->audio->sh=NULL;
877 } else {
878 sh_audio=demuxer->audio->sh;sh_audio->ds=demuxer->audio;
882 return demuxer;
886 const demuxer_desc_t demuxer_desc_mpeg_ty = {
887 "TiVo demuxer",
888 "tivo",
889 "TiVo",
890 "Christopher R. Wingert",
891 "Demux streams from TiVo",
892 DEMUXER_TYPE_MPEG_TY,
893 0, // unsafe autodetect
894 ty_check_file,
895 demux_ty_fill_buffer,
896 demux_open_ty,
897 demux_close_ty,
898 demux_seek_ty,
899 demux_ty_control