Mercurial > mplayer.hg
annotate libmpdemux/demux_ty.c @ 24501:0d9578ee65cf
Simpler and more robust tar parsing
author | reimar |
---|---|
date | Sat, 15 Sep 2007 11:33:31 +0000 |
parents | dcb02dadd06c |
children | 0266e095b2d7 |
rev | line source |
---|---|
10263 | 1 /* |
2 * tivo@wingert.org, February 2003 | |
3 * | |
4 * Copyright (C) 2003 Christopher R. Wingert | |
5 * | |
6 * The license covers the portions of this file regarding TiVo additions. | |
7 * | |
24487 | 8 * Olaf Beck and Tridge (indirectly) were essential at providing |
9 * information regarding the format of the TiVo streams. | |
10263 | 10 * |
24487 | 11 * However, no code in the following subsection is directly copied from |
10263 | 12 * either author. |
13 * | |
14 * | |
15 * This program is free software; you can redistribute it and/or | |
16 * modify it under the terms of the GNU General Public License | |
17 * as published by the Free Software Foundation; either version 2 | |
18 * of the License, or (at your option) any later version. | |
19 * | |
20 * This program is distributed in the hope that it will be useful, | |
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 * GNU General Public License for more details. | |
24 * | |
25 * You should have received a copy of the GNU General Public License | |
26 * along with this program; if not, write to the Free Software | |
19614 | 27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
10263 | 28 * |
29 */ | |
30 | |
31 | |
32 #include <stdio.h> | |
33 #include <stdlib.h> | |
34 #include <unistd.h> | |
35 #include <time.h> | |
36 #include <stdarg.h> | |
37 | |
38 #include "config.h" | |
39 #include "mp_msg.h" | |
40 #include "help_mp.h" | |
41 | |
22605
4d81dbdf46b9
Add explicit location for headers from the stream/ directory.
diego
parents:
20141
diff
changeset
|
42 #include "stream/stream.h" |
10263 | 43 #include "demuxer.h" |
44 #include "parse_es.h" | |
45 #include "stheader.h" | |
17012 | 46 #include "sub_cc.h" |
23703
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23381
diff
changeset
|
47 #include "libavutil/avstring.h" |
24461
921de822048c
Fix completely broken get_ty_pts (it's an ordinary MPEG timestamp)
reimar
parents:
24460
diff
changeset
|
48 #include "libavutil/intreadwrite.h" |
10263 | 49 |
50 extern void skip_audio_frame( sh_audio_t *sh_audio ); | |
51 extern int sub_justify; | |
52 | |
53 // 2/c0: audio data | |
54 // 3/c0: audio packet header (PES header) | |
55 // 4/c0: audio data (S/A only?) | |
56 // 9/c0: audio packet header, AC-3 audio | |
57 // 2/e0: video data | |
58 // 6/e0: video packet header (PES header) | |
59 // 7/e0: video sequence header start | |
60 // 8/e0: video I-frame header start | |
61 // a/e0: video P-frame header start | |
62 // b/e0: video B-frame header start | |
63 // c/e0: video GOP header start | |
64 // e/01: closed-caption data | |
24487 | 65 // e/02: Extended data services data |
10263 | 66 |
67 | |
24446 | 68 #define TIVO_PES_FILEID 0xf5467abd |
69 #define TIVO_PART_LENGTH 0x20000000 | |
10263 | 70 |
71 #define CHUNKSIZE ( 128 * 1024 ) | |
72 #define MAX_AUDIO_BUFFER ( 16 * 1024 ) | |
73 | |
24446 | 74 #define TY_V 1 |
75 #define TY_A 2 | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
76 |
24499 | 77 typedef struct |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
78 { |
24498
7d955b1de13c
Remove another variable and reorder to avoid wasting space due to alignment
reimar
parents:
24497
diff
changeset
|
79 off_t startOffset; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
80 off_t fileSize; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
81 int chunks; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
82 } tmf_fileParts; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
83 |
24446 | 84 #define MAX_TMF_PARTS 16 |
10263 | 85 |
24499 | 86 typedef struct |
10263 | 87 { |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
88 int whichChunk; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
89 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
90 unsigned char lastAudio[ MAX_AUDIO_BUFFER ]; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
91 int lastAudioEnd; |
10263 | 92 |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
93 int tivoType; // 1 = SA, 2 = DTiVo |
10263 | 94 |
24463 | 95 int64_t lastAudioPTS; |
96 int64_t lastVideoPTS; | |
10263 | 97 |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
98 off_t size; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
99 int readHeader; |
24487 | 100 |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
101 int tmf; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
102 tmf_fileParts tmfparts[ MAX_TMF_PARTS ]; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
103 int tmf_totalparts; |
10263 | 104 } TiVoInfo; |
105 | |
106 off_t vstream_streamsize( ); | |
107 void ty_ClearOSD( int start ); | |
108 | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
109 // =========================================================================== |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
110 #define TMF_SIG "showing.xml" |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
111 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
112 // =========================================================================== |
24443 | 113 static int ty_tmf_filetoparts( demuxer_t *demux, TiVoInfo *tivo ) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
114 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
115 int parts = 0; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
116 |
24501 | 117 stream_seek(demux->stream, 0); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
118 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
119 mp_msg( MSGT_DEMUX, MSGL_DBG3, "Dumping tar contents\n" ); |
24501 | 120 while (!demux->stream->eof) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
121 { |
24488 | 122 char header[ 512 ]; |
123 char *name; | |
24500 | 124 char *extension; |
24488 | 125 char *sizestr; |
126 int size; | |
127 off_t skip; | |
24457 | 128 if (stream_read(demux->stream, header, 512) < 512) |
24487 | 129 { |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
130 mp_msg( MSGT_DEMUX, MSGL_DBG3, "Read bad\n" ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
131 break; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
132 } |
24448
044d3ec97bf7
Avoid strlcpy, tar headers already have space to ensure 0-termination
reimar
parents:
24447
diff
changeset
|
133 name = header; |
044d3ec97bf7
Avoid strlcpy, tar headers already have space to ensure 0-termination
reimar
parents:
24447
diff
changeset
|
134 name[99] = 0; |
044d3ec97bf7
Avoid strlcpy, tar headers already have space to ensure 0-termination
reimar
parents:
24447
diff
changeset
|
135 sizestr = &header[124]; |
24449 | 136 sizestr[11] = 0; |
24442
9fc610537539
Use strtol instead of horribly suboptimal ty_octaltodecimal
reimar
parents:
24423
diff
changeset
|
137 size = strtol(sizestr, NULL, 8); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
138 |
24501 | 139 mp_msg( MSGT_DEMUX, MSGL_DBG3, "name %-20.20s size %-12.12s %d\n", |
140 name, sizestr, size ); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
141 |
24500 | 142 extension = strrchr(name, '.'); |
24501 | 143 if (extension && strcmp(extension, ".ty") == 0) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
144 { |
24455 | 145 if ( parts >= MAX_TMF_PARTS ) { |
146 mp_msg( MSGT_DEMUX, MSGL_ERR, "ty:tmf too big\n" ); | |
147 break; | |
148 } | |
24447
c6253f3b5f47
Do not ignore last chunk in .tmf files, it will cause part of the file to be
reimar
parents:
24446
diff
changeset
|
149 tivo->tmfparts[ parts ].fileSize = size; |
24501 | 150 tivo->tmfparts[ parts ].startOffset = stream_tell(demux->stream); |
24450 | 151 tivo->tmfparts[ parts ].chunks = size / CHUNKSIZE; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
152 mp_msg |
24487 | 153 ( |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
154 MSGT_DEMUX, MSGL_DBG3, |
24498
7d955b1de13c
Remove another variable and reorder to avoid wasting space due to alignment
reimar
parents:
24497
diff
changeset
|
155 "tmf_filetoparts(): index %d, chunks %d\n", |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
156 parts, |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
157 tivo->tmfparts[ parts ].chunks |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
158 ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
159 mp_msg |
24487 | 160 ( |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
161 MSGT_DEMUX, MSGL_DBG3, |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16346
diff
changeset
|
162 "tmf_filetoparts(): size %"PRId64"\n", |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
163 tivo->tmfparts[ parts ].fileSize |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
164 ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
165 mp_msg |
24487 | 166 ( |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
167 MSGT_DEMUX, MSGL_DBG3, |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16346
diff
changeset
|
168 "tmf_filetoparts(): startOffset %"PRId64"\n", |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
169 tivo->tmfparts[ parts ].startOffset |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
170 ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
171 parts++; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
172 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
173 |
24501 | 174 // size rounded up to blocks |
175 skip = (size + 511) & ~511; | |
176 stream_skip(demux->stream, skip); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
177 } |
24501 | 178 stream_reset(demux->stream); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
179 tivo->tmf_totalparts = parts; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
180 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
181 "tmf_filetoparts(): No More Part Files %d\n", parts ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
182 |
24446 | 183 return 1; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
184 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
185 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
186 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
187 // =========================================================================== |
24451 | 188 static off_t tmf_filetooffset(TiVoInfo *tivo, int chunk) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
189 { |
24451 | 190 int i; |
191 for (i = 0; i < tivo->tmf_totalparts; i++) { | |
192 if (chunk < tivo->tmfparts[i].chunks) | |
193 return tivo->tmfparts[i].startOffset + chunk * CHUNKSIZE; | |
194 chunk -= tivo->tmfparts[i].chunks; | |
195 } | |
196 return -1; | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
197 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
198 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
199 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
200 // =========================================================================== |
24487 | 201 static int tmf_load_chunk( demuxer_t *demux, TiVoInfo *tivo, |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
202 unsigned char *buff, int size, int readChunk ) |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
203 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
204 off_t fileoffset; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
205 int count; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
206 |
24487 | 207 mp_msg( MSGT_DEMUX, MSGL_DBG3, "\ntmf_load_chunk() begin %d\n", |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
208 readChunk ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
209 |
24452 | 210 fileoffset = tmf_filetooffset(tivo, readChunk); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
211 |
24452 | 212 if (fileoffset == -1) { |
213 mp_msg(MSGT_DEMUX, MSGL_ERR, "Read past EOF()\n"); | |
214 return 0; | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
215 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
216 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
217 if ( stream_seek( demux->stream, fileoffset ) != 1 ) |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
218 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
219 mp_msg( MSGT_DEMUX, MSGL_ERR, "Read past EOF()\n" ); |
24464 | 220 return 0; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
221 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
222 count = stream_read( demux->stream, buff, size ); |
24487 | 223 demux->filepos = stream_tell( demux->stream ); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
224 |
24487 | 225 mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() count %x\n", |
226 count ); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
227 |
24487 | 228 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
229 "tmf_load_chunk() bytes %x %x %x %x %x %x %x %x\n", | |
230 buff[ 0 ], buff[ 1 ], buff[ 2 ], buff[ 3 ], | |
231 buff[ 4 ], buff[ 5 ], buff[ 6 ], buff[ 7 ] ); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
232 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
233 mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() end\n" ); |
24487 | 234 |
24446 | 235 return count; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
236 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
237 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
238 // =========================================================================== |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
239 |
10263 | 240 // DTiVo MPEG 336, 480, 576, 768 |
241 // SA TiVo 864 | |
242 // DTiVo AC-3 1550 | |
243 // | |
24446 | 244 #define SERIES1_PTS_LENGTH 11 |
245 #define SERIES1_PTS_OFFSET 6 | |
246 #define SERIES2_PTS_LENGTH 16 | |
247 #define SERIES2_PTS_OFFSET 9 | |
248 #define AC3_PTS_LENGTH 16 | |
249 #define AC3_PTS_OFFSET 9 | |
10263 | 250 |
251 static int IsValidAudioPacket( int size, int *ptsOffset, int *ptsLen ) | |
252 { | |
253 *ptsOffset = 0; | |
254 *ptsLen = 0; | |
255 | |
256 // AC-3 | |
24446 | 257 if ( size == 1550 || size == 1552 ) |
10263 | 258 { |
259 *ptsOffset = AC3_PTS_OFFSET; | |
260 *ptsLen = AC3_PTS_LENGTH; | |
24446 | 261 return 1; |
10263 | 262 } |
263 | |
264 // MPEG | |
24471
516c6a2277b1
Greatly simplify IsValidAudioPacket, though this might break something
reimar
parents:
24470
diff
changeset
|
265 if ( (size & 15) == (SERIES1_PTS_LENGTH & 15) ) |
10263 | 266 { |
267 *ptsOffset = SERIES1_PTS_OFFSET; | |
268 *ptsLen = SERIES1_PTS_LENGTH; | |
24470 | 269 return 1; |
10263 | 270 } |
24471
516c6a2277b1
Greatly simplify IsValidAudioPacket, though this might break something
reimar
parents:
24470
diff
changeset
|
271 if ( (size & 15) == (SERIES2_PTS_LENGTH & 15) ) |
10263 | 272 { |
273 *ptsOffset = SERIES2_PTS_OFFSET; | |
274 *ptsLen = SERIES2_PTS_LENGTH; | |
24470 | 275 return 1; |
10263 | 276 } |
24487 | 277 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Tossing Audio Packet Size %d\n", |
10263 | 278 size ); |
24446 | 279 return 0; |
10263 | 280 } |
281 | |
282 | |
24463 | 283 static int64_t get_ty_pts( unsigned char *buf ) |
10263 | 284 { |
24461
921de822048c
Fix completely broken get_ty_pts (it's an ordinary MPEG timestamp)
reimar
parents:
24460
diff
changeset
|
285 int a = buf[0] & 0xe; |
921de822048c
Fix completely broken get_ty_pts (it's an ordinary MPEG timestamp)
reimar
parents:
24460
diff
changeset
|
286 int b = AV_RB16(buf + 1); |
921de822048c
Fix completely broken get_ty_pts (it's an ordinary MPEG timestamp)
reimar
parents:
24460
diff
changeset
|
287 int c = AV_RB16(buf + 3); |
10263 | 288 |
24461
921de822048c
Fix completely broken get_ty_pts (it's an ordinary MPEG timestamp)
reimar
parents:
24460
diff
changeset
|
289 if (!(1 & a & b & c)) // invalid MPEG timestamp |
24463 | 290 return MP_NOPTS_VALUE; |
24461
921de822048c
Fix completely broken get_ty_pts (it's an ordinary MPEG timestamp)
reimar
parents:
24460
diff
changeset
|
291 a >>= 1; b >>= 1; c >>= 1; |
24463 | 292 return (((uint64_t)a) << 30) | (b << 15) | c; |
10263 | 293 } |
294 | |
24487 | 295 static void demux_ty_AddToAudioBuffer( TiVoInfo *tivo, unsigned char *buffer, |
10263 | 296 int size ) |
297 { | |
24446 | 298 if ( tivo->lastAudioEnd + size < MAX_AUDIO_BUFFER ) |
10263 | 299 { |
24487 | 300 memcpy( &tivo->lastAudio[ tivo->lastAudioEnd ], |
10263 | 301 buffer, size ); |
302 tivo->lastAudioEnd += size; | |
303 } | |
304 else | |
305 mp_msg( MSGT_DEMUX, MSGL_ERR, | |
306 "ty:WARNING - Would have blown my audio buffer\n" ); | |
307 } | |
308 | |
24496
42693ad6dd30
Remove now useless parameters from demux_ty_CopyToDemuxPacket
reimar
parents:
24495
diff
changeset
|
309 static void demux_ty_CopyToDemuxPacket( demux_stream_t *ds, |
24497
713ad2d3878d
PTS should be passed as int64_t to demux_ty_CopyToDemuxPacket
reimar
parents:
24496
diff
changeset
|
310 unsigned char *buffer, int size, off_t pos, int64_t pts ) |
10263 | 311 { |
24482 | 312 demux_packet_t *dp = new_demux_packet( size ); |
10263 | 313 memcpy( dp->buffer, buffer, size ); |
24463 | 314 if (pts != MP_NOPTS_VALUE) |
315 dp->pts = pts / 90000.0; | |
10263 | 316 dp->pos = pos; |
317 dp->flags = 0; | |
318 ds_add_packet( ds, dp ); | |
319 } | |
320 | |
24484 | 321 static int demux_ty_FindESHeader( uint8_t nal, |
24478 | 322 unsigned char *buffer, int bufferSize ) |
10263 | 323 { |
24484 | 324 uint32_t search = 0x00000100 | nal; |
24483 | 325 uint32_t found = -1; |
326 uint8_t *p = buffer; | |
327 uint8_t *end = p + bufferSize; | |
328 while (p < end) { | |
329 found <<= 8; | |
330 found |= *p++; | |
331 if (found == search) | |
332 return p - buffer - 4; | |
10263 | 333 } |
24446 | 334 return -1; |
10263 | 335 } |
336 | |
24487 | 337 static void demux_ty_FindESPacket( uint8_t nal, |
10263 | 338 unsigned char *buffer, int bufferSize, int *esOffset1, int *esOffset2 ) |
339 { | |
24484 | 340 *esOffset1 = demux_ty_FindESHeader(nal, buffer, bufferSize); |
24480
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
341 if (*esOffset1 == -1) { |
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
342 *esOffset2 = -1; |
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
343 return; |
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
344 } |
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
345 buffer += *esOffset1 + 1; |
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
346 bufferSize -= *esOffset1 + 1; |
24484 | 347 *esOffset2 = demux_ty_FindESHeader(nal, buffer, bufferSize); |
24480
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
348 if (*esOffset2 != -1) |
a365d70938b3
Simplify demux_ty_FindESPacket by reusing demux_ty_FindESHeader
reimar
parents:
24479
diff
changeset
|
349 *esOffset2 += *esOffset1 + 1; |
10263 | 350 } |
351 | |
24484 | 352 #define VIDEO_NAL 0xe0 |
353 #define AUDIO_NAL 0xc0 | |
354 #define AC3_NAL 0xbd | |
10263 | 355 |
16175 | 356 static int demux_ty_fill_buffer( demuxer_t *demux, demux_stream_t *dsds ) |
10263 | 357 { |
358 int invalidType = 0; | |
359 int errorHeader = 0; | |
360 int recordsDecoded = 0; | |
361 | |
362 unsigned char chunk[ CHUNKSIZE ]; | |
363 int readSize; | |
364 | |
365 int numberRecs; | |
366 unsigned char *recPtr; | |
367 int offset; | |
368 | |
369 int counter; | |
370 | |
371 int aid; | |
24487 | 372 |
24469
afbab72dbf2c
Do not misuse a_streams for private info, demuxer->priv is for that!
reimar
parents:
24468
diff
changeset
|
373 TiVoInfo *tivo = demux->priv; |
10263 | 374 |
375 if ( demux->stream->type == STREAMTYPE_DVD ) | |
24487 | 376 return 0; |
10263 | 377 |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
378 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty processing\n" ); |
10263 | 379 |
380 if( demux->stream->eof ) return 0; | |
24487 | 381 |
10263 | 382 // ====================================================================== |
11000 | 383 // If we haven't figured out the size of the stream, let's do so |
10263 | 384 // ====================================================================== |
385 #ifdef STREAMTYPE_STREAM_TY | |
386 if ( demux->stream->type == STREAMTYPE_STREAM_TY ) | |
387 { | |
388 // The vstream code figures out the exact size of the stream | |
389 demux->movi_start = 0; | |
390 demux->movi_end = vstream_streamsize(); | |
391 tivo->size = vstream_streamsize(); | |
392 } | |
393 else | |
394 #endif | |
395 { | |
396 // If its a local file, try to find the Part Headers, so we can | |
397 // calculate the ACTUAL stream size | |
398 // If we can't find it, go off with the file size and hope the | |
399 // extract program did the "right thing" | |
400 if ( tivo->readHeader == 0 ) | |
401 { | |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
402 off_t filePos; |
10263 | 403 tivo->readHeader = 1; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
404 tivo->size = demux->stream->end_pos; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
405 |
10263 | 406 filePos = demux->filepos; |
407 stream_seek( demux->stream, 0 ); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
408 |
10263 | 409 readSize = stream_read( demux->stream, chunk, CHUNKSIZE ); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
410 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
411 if ( memcmp( chunk, TMF_SIG, sizeof( TMF_SIG ) ) == 0 ) |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
412 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
413 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Detected a tmf\n" ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
414 tivo->tmf = 1; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
415 ty_tmf_filetoparts( demux, tivo ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
416 readSize = tmf_load_chunk( demux, tivo, chunk, CHUNKSIZE, 0 ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
417 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
418 |
24467 | 419 if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID ) |
10263 | 420 { |
421 off_t numberParts; | |
422 | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
423 readSize = 0; |
24487 | 424 |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
425 if ( tivo->tmf != 1 ) |
10263 | 426 { |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
427 off_t offset; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
428 |
10263 | 429 numberParts = demux->stream->end_pos / TIVO_PART_LENGTH; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
430 offset = numberParts * TIVO_PART_LENGTH; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
431 |
17366 | 432 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty/ty+Number Parts %"PRId64"\n", |
433 (int64_t)numberParts ); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
434 |
24446 | 435 if ( offset + CHUNKSIZE < demux->stream->end_pos ) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
436 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
437 stream_seek( demux->stream, offset ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
438 readSize = stream_read( demux->stream, chunk, CHUNKSIZE ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
439 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
440 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
441 else |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
442 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
443 numberParts = tivo->tmf_totalparts; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
444 offset = numberParts * TIVO_PART_LENGTH; |
24487 | 445 readSize = tmf_load_chunk( demux, tivo, chunk, CHUNKSIZE, |
446 numberParts * ( TIVO_PART_LENGTH - CHUNKSIZE ) / | |
24446 | 447 CHUNKSIZE ); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
448 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
449 |
24467 | 450 if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID ) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
451 { |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
452 int size = AV_RB24(chunk + 12); |
10263 | 453 size -= 4; |
454 size *= CHUNKSIZE; | |
455 tivo->size = numberParts * TIVO_PART_LENGTH; | |
456 tivo->size += size; | |
24487 | 457 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
16750
0a31740dd5e6
Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents:
16346
diff
changeset
|
458 "ty:Header Calc Stream Size %"PRId64"\n", tivo->size ); |
10263 | 459 } |
460 } | |
461 if ( tivo->size > demux->stream->end_pos ) | |
462 tivo->size = demux->stream->end_pos; | |
463 | |
24487 | 464 if ( demux->stream->start_pos > 0 ) |
465 filePos = demux->stream->start_pos; | |
10263 | 466 stream_seek( demux->stream, filePos ); |
24487 | 467 demux->filepos = stream_tell( demux->stream ); |
24446 | 468 tivo->whichChunk = filePos / CHUNKSIZE; |
10263 | 469 } |
470 demux->movi_start = 0; | |
471 demux->movi_end = tivo->size; | |
472 } | |
473 | |
474 // ====================================================================== | |
475 // Give a clue as to where we are in the stream | |
476 // ====================================================================== | |
477 mp_msg( MSGT_DEMUX, MSGL_DBG3, | |
17366 | 478 "ty:ty header size %"PRIx64"\n", (int64_t)tivo->size ); |
10263 | 479 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
17366 | 480 "ty:ty which Chunk %d\n", tivo->whichChunk ); |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
481 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
17366 | 482 "ty:file end_pos %"PRIx64"\n", (int64_t)demux->stream->end_pos ); |
10263 | 483 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
17366 | 484 "\nty:wanted current offset %"PRIx64"\n", (int64_t)stream_tell( demux->stream ) ); |
10263 | 485 |
24464 | 486 if ( tivo->size > 0 && stream_tell( demux->stream ) > tivo->size ) |
10263 | 487 { |
488 demux->stream->eof = 1; | |
24464 | 489 return 0; |
10263 | 490 } |
491 | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
492 if ( tivo->tmf != 1 ) |
10263 | 493 { |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
494 // Make sure we are on a 128k boundary |
24446 | 495 if ( demux->filepos % CHUNKSIZE != 0 ) |
10263 | 496 { |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
497 int whichChunk = demux->filepos / CHUNKSIZE; |
24446 | 498 if ( demux->filepos % CHUNKSIZE > CHUNKSIZE / 2 ) |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
499 whichChunk++; |
24446 | 500 stream_seek( demux->stream, whichChunk * CHUNKSIZE ); |
10263 | 501 } |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
502 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
503 demux->filepos = stream_tell( demux->stream ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
504 tivo->whichChunk = demux->filepos / CHUNKSIZE; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
505 readSize = stream_read( demux->stream, chunk, CHUNKSIZE ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
506 if ( readSize != CHUNKSIZE ) |
24464 | 507 return 0; |
10263 | 508 } |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
509 else |
10263 | 510 { |
24487 | 511 readSize = tmf_load_chunk( demux, tivo, chunk, CHUNKSIZE, |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
512 tivo->whichChunk ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
513 if ( readSize != CHUNKSIZE ) |
24446 | 514 return 0; |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
515 tivo->whichChunk++; |
10263 | 516 } |
517 | |
518 // We found a part header, skip it | |
24467 | 519 if( AV_RB32(chunk) == TIVO_PES_FILEID ) |
10263 | 520 { |
521 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Skipping PART Header\n" ); | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
522 if ( tivo->tmf != 1 ) |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
523 { |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
524 demux->filepos = stream_tell( demux->stream ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
525 readSize = stream_read( demux->stream, chunk, CHUNKSIZE ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
526 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
527 else |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
528 { |
24487 | 529 readSize = tmf_load_chunk( demux, tivo, chunk, CHUNKSIZE, |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
530 tivo->whichChunk ); |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
531 tivo->whichChunk++; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
532 } |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
533 |
10263 | 534 if ( readSize != CHUNKSIZE ) |
24446 | 535 return 0; |
24487 | 536 } |
10263 | 537 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
24487 | 538 "\nty:actual current offset %"PRIx64"\n", stream_tell( demux->stream ) - |
539 CHUNKSIZE ); | |
10263 | 540 |
541 | |
12860 | 542 // Let's make a Video Demux Stream for MPlayer |
10263 | 543 aid = 0x0; |
544 if( !demux->v_streams[ aid ] ) new_sh_video( demux, aid ); | |
545 if( demux->video->id == -1 ) demux->video->id = aid; | |
546 if( demux->video->id == aid ) | |
547 { | |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
548 demux_stream_t *ds = demux->video; |
10263 | 549 if( !ds->sh ) ds->sh = demux->v_streams[ aid ]; |
550 } | |
551 | |
552 // ====================================================================== | |
553 // Finally, we get to actually parse the chunk | |
554 // ====================================================================== | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
555 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty parsing a chunk\n" ); |
10263 | 556 numberRecs = chunk[ 0 ]; |
557 recPtr = &chunk[ 4 ]; | |
24446 | 558 offset = numberRecs * 16 + 4; |
10263 | 559 for ( counter = 0 ; counter < numberRecs ; counter++ ) |
560 { | |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
561 int size = AV_RB24(recPtr) >> 4; |
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
562 int type = recPtr[ 3 ]; |
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
563 int nybbleType = recPtr[ 2 ] & 0x0f; |
10263 | 564 recordsDecoded++; |
565 | |
566 mp_msg( MSGT_DEMUX, MSGL_DBG3, | |
567 "ty:Record Type %x/%x %d\n", nybbleType, type, size ); | |
568 | |
569 // ================================================================ | |
570 // Video Parsing | |
571 // ================================================================ | |
572 if ( type == 0xe0 ) | |
573 { | |
24446 | 574 if ( size > 0 && size + offset <= CHUNKSIZE ) |
10263 | 575 { |
24487 | 576 int esOffset1 = demux_ty_FindESHeader( VIDEO_NAL, &chunk[ offset ], |
24478 | 577 size); |
10263 | 578 if ( esOffset1 != -1 ) |
24487 | 579 tivo->lastVideoPTS = get_ty_pts( |
10263 | 580 &chunk[ offset + esOffset1 + 9 ] ); |
581 | |
582 // Do NOT Pass the PES Header onto the MPEG2 Decode | |
583 if( nybbleType != 0x06 ) | |
24496
42693ad6dd30
Remove now useless parameters from demux_ty_CopyToDemuxPacket
reimar
parents:
24495
diff
changeset
|
584 demux_ty_CopyToDemuxPacket( demux->video, |
24487 | 585 &chunk[ offset ], size, demux->filepos + offset, |
586 tivo->lastVideoPTS ); | |
10263 | 587 offset += size; |
588 } | |
24487 | 589 else |
590 errorHeader++; | |
10263 | 591 } |
592 // ================================================================ | |
593 // Audio Parsing | |
594 // ================================================================ | |
24487 | 595 else if ( type == 0xc0 ) |
10263 | 596 { |
24446 | 597 if ( size > 0 && size + offset <= CHUNKSIZE ) |
10263 | 598 { |
599 if( demux->audio->id == -1 ) | |
600 { | |
601 if ( nybbleType == 0x02 ) | |
602 continue; // DTiVo inconclusive, wait for more | |
603 else if ( nybbleType == 0x09 ) | |
604 { | |
605 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting AC-3 Audio\n" ); | |
606 aid = 0x80; // AC-3 | |
607 } | |
608 else | |
609 { | |
610 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Setting MPEG Audio\n" ); | |
611 aid = 0x0; // MPEG Audio | |
612 } | |
613 | |
614 demux->audio->id = aid; | |
615 if( !demux->a_streams[ aid ] ) new_sh_audio( demux, aid ); | |
616 if( demux->audio->id == aid ) | |
617 { | |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
618 demux_stream_t *ds = demux->audio; |
15580 | 619 if( !ds->sh ) { |
620 sh_audio_t* sh_a; | |
621 ds->sh = demux->a_streams[ aid ]; | |
622 sh_a = (sh_audio_t*)ds->sh; | |
623 switch(aid & 0xE0){ // 1110 0000 b (high 3 bit: type low 5: id) | |
624 case 0x00: sh_a->format=0x50;break; // mpeg | |
625 case 0xA0: sh_a->format=0x10001;break; // dvd pcm | |
626 case 0x80: if((aid & 0xF8) == 0x88) sh_a->format=0x2001;//dts | |
627 else sh_a->format=0x2000;break; // ac3 | |
628 } | |
629 } | |
10263 | 630 } |
631 } | |
632 | |
633 aid = demux->audio->id; | |
634 | |
635 | |
636 // SA DTiVo Audio Data, no PES | |
637 // ================================================ | |
24476 | 638 if ( nybbleType == 0x02 || nybbleType == 0x04 ) |
10263 | 639 { |
24476 | 640 if ( nybbleType == 0x02 && tivo->tivoType == 2 ) |
10263 | 641 demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size ); |
642 else | |
643 { | |
644 | |
645 mp_msg( MSGT_DEMUX, MSGL_DBG3, | |
646 "ty:Adding Audio Packet Size %d\n", size ); | |
24496
42693ad6dd30
Remove now useless parameters from demux_ty_CopyToDemuxPacket
reimar
parents:
24495
diff
changeset
|
647 demux_ty_CopyToDemuxPacket( demux->audio, |
24487 | 648 &chunk[ offset ], size, ( demux->filepos + offset ), |
649 tivo->lastAudioPTS ); | |
10263 | 650 } |
651 } | |
652 | |
24475 | 653 // 3 - MPEG Audio with PES Header, either SA or DTiVo |
654 // 9 - DTiVo AC3 Audio Data with PES Header | |
10263 | 655 // ================================================ |
24475 | 656 if ( nybbleType == 0x03 || nybbleType == 0x09 ) |
10263 | 657 { |
24477
8c9d86adb28e
Move variable declarations into the block where they are used
reimar
parents:
24476
diff
changeset
|
658 int esOffset1, esOffset2; |
24475 | 659 if ( nybbleType == 0x03 ) |
24487 | 660 esOffset1 = demux_ty_FindESHeader( AUDIO_NAL, &chunk[ offset ], |
24478 | 661 size); |
10263 | 662 |
663 // SA PES Header, No Audio Data | |
664 // ================================================ | |
24475 | 665 if ( nybbleType == 0x03 && esOffset1 == 0 && size == 16 ) |
10263 | 666 { |
667 tivo->tivoType = 1; | |
24487 | 668 tivo->lastAudioPTS = get_ty_pts( &chunk[ offset + |
10263 | 669 SERIES2_PTS_OFFSET ] ); |
670 } | |
671 else | |
672 // DTiVo Audio with PES Header | |
673 // ================================================ | |
674 { | |
675 tivo->tivoType = 2; | |
676 | |
677 demux_ty_AddToAudioBuffer( tivo, &chunk[ offset ], size ); | |
24484 | 678 demux_ty_FindESPacket( nybbleType == 9 ? AC3_NAL : AUDIO_NAL, |
10263 | 679 tivo->lastAudio, tivo->lastAudioEnd, &esOffset1, |
680 &esOffset2 ); | |
681 | |
24446 | 682 if ( esOffset1 != -1 && esOffset2 != -1 ) |
10263 | 683 { |
684 int packetSize = esOffset2 - esOffset1; | |
685 int headerSize; | |
686 int ptsOffset; | |
687 | |
24487 | 688 if ( IsValidAudioPacket( packetSize, &ptsOffset, |
10263 | 689 &headerSize ) ) |
690 { | |
691 mp_msg( MSGT_DEMUX, MSGL_DBG3, | |
24487 | 692 "ty:Adding DTiVo Audio Packet Size %d\n", |
10263 | 693 packetSize ); |
694 | |
24487 | 695 tivo->lastAudioPTS = get_ty_pts( |
10263 | 696 &tivo->lastAudio[ esOffset1 + ptsOffset ] ); |
697 | |
24475 | 698 if (nybbleType == 9) headerSize = 0; |
10263 | 699 demux_ty_CopyToDemuxPacket |
24487 | 700 ( |
701 demux->audio, | |
24486 | 702 &tivo->lastAudio[ esOffset1 + headerSize ], |
703 packetSize - headerSize, | |
704 demux->filepos + offset, | |
24487 | 705 tivo->lastAudioPTS |
10263 | 706 ); |
707 | |
708 } | |
709 | |
710 // Collapse the Audio Buffer | |
24485 | 711 tivo->lastAudioEnd -= esOffset2; |
24487 | 712 memmove( &tivo->lastAudio[ 0 ], |
713 &tivo->lastAudio[ esOffset2 ], | |
24485 | 714 tivo->lastAudioEnd ); |
10263 | 715 } |
716 } | |
717 } | |
718 | |
719 offset += size; | |
720 } | |
24487 | 721 else |
722 errorHeader++; | |
723 } | |
10263 | 724 // ================================================================ |
24474 | 725 // 1 = Closed Caption |
726 // 2 = Extended Data Services | |
10263 | 727 // ================================================================ |
24487 | 728 else if ( type == 0x01 || type == 0x02 ) |
729 { | |
24474 | 730 unsigned char lastXDS[ 16 ]; |
24487 | 731 int b = AV_RB24(recPtr) >> 4; |
732 b &= 0x7f7f; | |
10263 | 733 |
24474 | 734 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:%s %04x\n", type == 1 ? "CC" : "XDS", b); |
10263 | 735 |
24487 | 736 lastXDS[ 0x00 ] = 0x00; |
737 lastXDS[ 0x01 ] = 0x00; | |
738 lastXDS[ 0x02 ] = 0x01; | |
739 lastXDS[ 0x03 ] = 0xb2; | |
740 lastXDS[ 0x04 ] = 'T'; | |
741 lastXDS[ 0x05 ] = 'Y'; | |
742 lastXDS[ 0x06 ] = type; | |
743 lastXDS[ 0x07 ] = b >> 8; | |
744 lastXDS[ 0x08 ] = b; | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
745 if ( subcc_enabled ) |
24496
42693ad6dd30
Remove now useless parameters from demux_ty_CopyToDemuxPacket
reimar
parents:
24495
diff
changeset
|
746 demux_ty_CopyToDemuxPacket( demux->video, lastXDS, 0x09, |
24487 | 747 demux->filepos + offset, tivo->lastVideoPTS ); |
748 } | |
10263 | 749 // ================================================================ |
750 // Unknown | |
751 // ================================================================ | |
24487 | 752 else |
753 { | |
24446 | 754 if ( size > 0 && size + offset <= CHUNKSIZE ) |
10263 | 755 offset += size; |
24489
97eba82233f5
live recordings can contain 0-size type 0 chunks, ignore them instead
reimar
parents:
24488
diff
changeset
|
756 if (type != 3 && type != 5 && (type != 0 || size > 0)) { |
10263 | 757 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Invalid Type %x\n", type ); |
24487 | 758 invalidType++; |
24472 | 759 } |
24487 | 760 } |
10263 | 761 recPtr += 16; |
762 } | |
763 | |
20141
bd3afd5a2e48
Fix misdetection of http://samples.mplayerhq.hu/tta/tivo_misdetect.tta as TiVo file
reimar
parents:
19614
diff
changeset
|
764 if ( errorHeader > 0 || invalidType > 0 ) |
10263 | 765 { |
24487 | 766 mp_msg( MSGT_DEMUX, MSGL_DBG3, |
20141
bd3afd5a2e48
Fix misdetection of http://samples.mplayerhq.hu/tta/tivo_misdetect.tta as TiVo file
reimar
parents:
19614
diff
changeset
|
767 "ty:Error Check - Records %d, Parsed %d, Errors %d + %d\n", |
bd3afd5a2e48
Fix misdetection of http://samples.mplayerhq.hu/tta/tivo_misdetect.tta as TiVo file
reimar
parents:
19614
diff
changeset
|
768 numberRecs, recordsDecoded, errorHeader, invalidType ); |
10263 | 769 |
770 // Invalid MPEG ES Size Check | |
24446 | 771 if ( errorHeader > numberRecs / 2 ) |
772 return 0; | |
10263 | 773 |
774 // Invalid MPEG Stream Type Check | |
24446 | 775 if ( invalidType > numberRecs / 2 ) |
776 return 0; | |
10263 | 777 } |
778 | |
779 demux->filepos = stream_tell( demux->stream ); | |
780 | |
24446 | 781 return 1; |
10263 | 782 } |
783 | |
17636 | 784 static void demux_seek_ty( demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags ) |
10263 | 785 { |
786 demux_stream_t *d_audio = demuxer->audio; | |
787 demux_stream_t *d_video = demuxer->video; | |
788 sh_audio_t *sh_audio = d_audio->sh; | |
789 sh_video_t *sh_video = d_video->sh; | |
790 off_t newpos; | |
791 off_t res; | |
24469
afbab72dbf2c
Do not misuse a_streams for private info, demuxer->priv is for that!
reimar
parents:
24468
diff
changeset
|
792 TiVoInfo *tivo = demuxer->priv; |
10263 | 793 |
794 mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Seeking to %7.1f\n", rel_seek_secs ); | |
795 | |
796 tivo->lastAudioEnd = 0; | |
24463 | 797 tivo->lastAudioPTS = MP_NOPTS_VALUE; |
798 tivo->lastVideoPTS = MP_NOPTS_VALUE; | |
10263 | 799 // |
800 //================= seek in MPEG ========================== | |
801 demuxer->filepos = stream_tell( demuxer->stream ); | |
802 | |
803 newpos = ( flags & 1 ) ? demuxer->movi_start : demuxer->filepos; | |
24487 | 804 |
10263 | 805 if( flags & 2 ) |
24487 | 806 // float seek 0..1 |
807 newpos += ( demuxer->movi_end - demuxer->movi_start ) * rel_seek_secs; | |
808 else | |
10263 | 809 { |
24487 | 810 // time seek (secs) |
10263 | 811 if( ! sh_video->i_bps ) // unspecified or VBR |
812 newpos += 2324 * 75 * rel_seek_secs; // 174.3 kbyte/sec | |
813 else | |
814 newpos += sh_video->i_bps * rel_seek_secs; | |
815 } | |
816 | |
817 if ( newpos < demuxer->movi_start ) | |
818 { | |
24487 | 819 if( demuxer->stream->type != STREAMTYPE_VCD ) demuxer->movi_start = 0; |
820 if( newpos < demuxer->movi_start ) newpos = demuxer->movi_start; | |
821 } | |
10263 | 822 |
823 res = newpos / CHUNKSIZE; | |
824 if ( rel_seek_secs >= 0 ) | |
825 newpos = ( res + 1 ) * CHUNKSIZE; | |
826 else | |
827 newpos = res * CHUNKSIZE; | |
828 | |
829 if ( newpos < 0 ) | |
830 newpos = 0; | |
15581
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
831 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
832 tivo->whichChunk = newpos / CHUNKSIZE; |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
833 |
2e621c99354d
a cleaned-up version of ty demuxer improvements found in tivo-mplayer fork.
joey
parents:
15580
diff
changeset
|
834 stream_seek( demuxer->stream, newpos ); |
10263 | 835 |
836 // re-sync video: | |
837 videobuf_code_len = 0; // reset ES stream buffer | |
838 | |
24487 | 839 ds_fill_buffer( d_video ); |
840 if( sh_audio ) | |
841 ds_fill_buffer( d_audio ); | |
10263 | 842 |
24487 | 843 while( 1 ) |
10263 | 844 { |
24487 | 845 int i; |
10263 | 846 if( sh_audio && !d_audio->eof && d_video->pts && d_audio->pts ) |
847 { | |
24487 | 848 float a_pts = d_audio->pts; |
10263 | 849 a_pts += ( ds_tell_pts( d_audio ) - sh_audio->a_in_buffer_len ) / |
850 (float)sh_audio->i_bps; | |
24487 | 851 if( d_video->pts > a_pts ) |
10263 | 852 { |
24487 | 853 skip_audio_frame( sh_audio ); // sync audio |
854 continue; | |
855 } | |
10263 | 856 } |
857 i = sync_video_packet( d_video ); | |
858 if( i == 0x1B3 || i == 0x1B8 ) break; // found it! | |
859 if( !i || !skip_video_packet( d_video ) ) break; // EOF? | |
860 } | |
24487 | 861 if ( subcc_enabled ) |
862 ty_ClearOSD( 0 ); | |
10263 | 863 } |
864 | |
24443 | 865 static int demux_ty_control( demuxer_t *demuxer,int cmd, void *arg ) |
10263 | 866 { |
867 demux_stream_t *d_video = demuxer->video; | |
868 sh_video_t *sh_video = d_video->sh; | |
869 | |
24487 | 870 switch(cmd) |
10263 | 871 { |
24487 | 872 case DEMUXER_CTRL_GET_TIME_LENGTH: |
873 if(!sh_video->i_bps) // unspecified or VBR | |
874 return DEMUXER_CTRL_DONTKNOW; | |
875 *(double *)arg= | |
24446 | 876 (double)demuxer->movi_end-demuxer->movi_start/sh_video->i_bps; |
24487 | 877 return DEMUXER_CTRL_GUESS; |
10263 | 878 |
24487 | 879 case DEMUXER_CTRL_GET_PERCENT_POS: |
880 return DEMUXER_CTRL_DONTKNOW; | |
881 default: | |
882 return DEMUXER_CTRL_NOTIMPL; | |
10263 | 883 } |
884 } | |
885 | |
886 | |
17174
83a8c738be89
make demuxer seek and close functions return void, patch by Dominik Mierzejewski
wanderer
parents:
17012
diff
changeset
|
887 static void demux_close_ty( demuxer_t *demux ) |
10263 | 888 { |
24469
afbab72dbf2c
Do not misuse a_streams for private info, demuxer->priv is for that!
reimar
parents:
24468
diff
changeset
|
889 TiVoInfo *tivo = demux->priv; |
10263 | 890 |
891 free( tivo ); | |
24487 | 892 sub_justify = 0; |
10263 | 893 } |
894 | |
895 | |
16175 | 896 static int ty_check_file(demuxer_t* demuxer) |
897 { | |
24469
afbab72dbf2c
Do not misuse a_streams for private info, demuxer->priv is for that!
reimar
parents:
24468
diff
changeset
|
898 TiVoInfo *tivo = calloc(1, sizeof(TiVoInfo)); |
afbab72dbf2c
Do not misuse a_streams for private info, demuxer->priv is for that!
reimar
parents:
24468
diff
changeset
|
899 demuxer->priv = tivo; |
16175 | 900 return ds_fill_buffer(demuxer->video) ? DEMUXER_TYPE_MPEG_TY : 0; |
901 } | |
902 | |
903 | |
904 static demuxer_t* demux_open_ty(demuxer_t* demuxer) | |
905 { | |
906 sh_audio_t *sh_audio=NULL; | |
907 sh_video_t *sh_video=NULL; | |
908 | |
909 sh_video=demuxer->video->sh;sh_video->ds=demuxer->video; | |
910 | |
911 if(demuxer->audio->id!=-2) { | |
912 if(!ds_fill_buffer(demuxer->audio)){ | |
913 mp_msg(MSGT_DEMUXER,MSGL_INFO,"MPEG: " MSGTR_MissingAudioStream); | |
914 demuxer->audio->sh=NULL; | |
915 } else { | |
916 sh_audio=demuxer->audio->sh;sh_audio->ds=demuxer->audio; | |
917 } | |
918 } | |
919 | |
920 return demuxer; | |
921 } | |
922 | |
923 | |
924 demuxer_desc_t demuxer_desc_mpeg_ty = { | |
925 "TiVo demuxer", | |
926 "tivo", | |
927 "TiVo", | |
928 "Christopher R. Wingert", | |
929 "Demux streams from TiVo", | |
930 DEMUXER_TYPE_MPEG_TY, | |
931 0, // unsafe autodetect | |
932 ty_check_file, | |
933 demux_ty_fill_buffer, | |
934 demux_open_ty, | |
935 demux_close_ty, | |
936 demux_seek_ty, | |
937 demux_ty_control | |
938 }; |