ffmpeg / libavformat / mpeg.c @ 66e5b1df
History | View | Annotate | Download (19.4 KB)
1 |
/*
|
---|---|
2 |
* MPEG1/2 demuxer
|
3 |
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
4 |
*
|
5 |
* This file is part of FFmpeg.
|
6 |
*
|
7 |
* FFmpeg is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU Lesser General Public
|
9 |
* License as published by the Free Software Foundation; either
|
10 |
* version 2.1 of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* FFmpeg is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 |
* Lesser General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU Lesser General Public
|
18 |
* License along with FFmpeg; if not, write to the Free Software
|
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 |
*/
|
21 |
|
22 |
#include "avformat.h" |
23 |
#include "internal.h" |
24 |
#include "mpeg.h" |
25 |
|
26 |
//#define DEBUG_SEEK
|
27 |
|
28 |
#undef NDEBUG
|
29 |
#include <assert.h> |
30 |
|
31 |
/*********************************************/
|
32 |
/* demux code */
|
33 |
|
34 |
#define MAX_SYNC_SIZE 100000 |
35 |
|
36 |
static int check_pes(uint8_t *p, uint8_t *end){ |
37 |
int pes1;
|
38 |
int pes2= (p[3] & 0xC0) == 0x80 |
39 |
&& (p[4] & 0xC0) != 0x40 |
40 |
&&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0)); |
41 |
|
42 |
for(p+=3; p<end && *p == 0xFF; p++); |
43 |
if((*p&0xC0) == 0x40) p+=2; |
44 |
if((*p&0xF0) == 0x20){ |
45 |
pes1= p[0]&p[2]&p[4]&1; |
46 |
}else if((*p&0xF0) == 0x30){ |
47 |
pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1; |
48 |
}else
|
49 |
pes1 = *p == 0x0F;
|
50 |
|
51 |
return pes1||pes2;
|
52 |
} |
53 |
|
54 |
static int mpegps_probe(AVProbeData *p) |
55 |
{ |
56 |
uint32_t code= -1;
|
57 |
int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0; |
58 |
int i;
|
59 |
int score=0; |
60 |
|
61 |
for(i=0; i<p->buf_size; i++){ |
62 |
code = (code<<8) + p->buf[i];
|
63 |
if ((code & 0xffffff00) == 0x100) { |
64 |
int len= p->buf[i+1] << 8 | p->buf[i+2]; |
65 |
int pes= check_pes(p->buf+i, p->buf+p->buf_size);
|
66 |
|
67 |
if(code == SYSTEM_HEADER_START_CODE) sys++;
|
68 |
else if(code == PACK_START_CODE) pspack++; |
69 |
else if((code & 0xf0) == VIDEO_ID && pes) vid++; |
70 |
// skip pes payload to avoid start code emulation for private
|
71 |
// and audio streams
|
72 |
else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;} |
73 |
else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;} |
74 |
|
75 |
else if((code & 0xf0) == VIDEO_ID && !pes) invalid++; |
76 |
else if((code & 0xe0) == AUDIO_ID && !pes) invalid++; |
77 |
else if(code == PRIVATE_STREAM_1 && !pes) invalid++; |
78 |
} |
79 |
} |
80 |
|
81 |
if(vid+audio > invalid) /* invalid VDR files nd short PES streams */ |
82 |
score= AVPROBE_SCORE_MAX/4;
|
83 |
|
84 |
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, invalid, p->buf_size);
|
85 |
if(sys>invalid && sys*9 <= pspack*10) |
86 |
return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg |
87 |
if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9) |
88 |
return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg |
89 |
if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */ |
90 |
return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; |
91 |
|
92 |
//02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
|
93 |
//mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
|
94 |
return score;
|
95 |
} |
96 |
|
97 |
|
98 |
typedef struct MpegDemuxContext { |
99 |
int32_t header_state; |
100 |
unsigned char psm_es_type[256]; |
101 |
int sofdec;
|
102 |
} MpegDemuxContext; |
103 |
|
104 |
static int mpegps_read_header(AVFormatContext *s, |
105 |
AVFormatParameters *ap) |
106 |
{ |
107 |
MpegDemuxContext *m = s->priv_data; |
108 |
const char *sofdec = "Sofdec"; |
109 |
int v, i = 0; |
110 |
|
111 |
m->header_state = 0xff;
|
112 |
s->ctx_flags |= AVFMTCTX_NOHEADER; |
113 |
|
114 |
m->sofdec = -1;
|
115 |
do {
|
116 |
v = avio_r8(s->pb); |
117 |
m->header_state = m->header_state << 8 | v;
|
118 |
m->sofdec++; |
119 |
} while (v == sofdec[i] && i++ < 6); |
120 |
|
121 |
m->sofdec = (m->sofdec == 6) ? 1 : 0; |
122 |
|
123 |
/* no need to do more */
|
124 |
return 0; |
125 |
} |
126 |
|
127 |
static int64_t get_pts(AVIOContext *pb, int c) |
128 |
{ |
129 |
uint8_t buf[5];
|
130 |
|
131 |
buf[0] = c<0 ? avio_r8(pb) : c; |
132 |
avio_read(pb, buf+1, 4); |
133 |
|
134 |
return ff_parse_pes_pts(buf);
|
135 |
} |
136 |
|
137 |
static int find_next_start_code(AVIOContext *pb, int *size_ptr, |
138 |
int32_t *header_state) |
139 |
{ |
140 |
unsigned int state, v; |
141 |
int val, n;
|
142 |
|
143 |
state = *header_state; |
144 |
n = *size_ptr; |
145 |
while (n > 0) { |
146 |
if (pb->eof_reached)
|
147 |
break;
|
148 |
v = avio_r8(pb); |
149 |
n--; |
150 |
if (state == 0x000001) { |
151 |
state = ((state << 8) | v) & 0xffffff; |
152 |
val = state; |
153 |
goto found;
|
154 |
} |
155 |
state = ((state << 8) | v) & 0xffffff; |
156 |
} |
157 |
val = -1;
|
158 |
found:
|
159 |
*header_state = state; |
160 |
*size_ptr = n; |
161 |
return val;
|
162 |
} |
163 |
|
164 |
#if 0 /* unused, remove? */
|
165 |
/* XXX: optimize */
|
166 |
static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
|
167 |
{
|
168 |
int64_t pos, pos_start;
|
169 |
int max_size, start_code;
|
170 |
|
171 |
max_size = *size_ptr;
|
172 |
pos_start = avio_tell(pb);
|
173 |
|
174 |
/* in order to go faster, we fill the buffer */
|
175 |
pos = pos_start - 16386;
|
176 |
if (pos < 0)
|
177 |
pos = 0;
|
178 |
avio_seek(pb, pos, SEEK_SET);
|
179 |
avio_r8(pb);
|
180 |
|
181 |
pos = pos_start;
|
182 |
for(;;) {
|
183 |
pos--;
|
184 |
if (pos < 0 || (pos_start - pos) >= max_size) {
|
185 |
start_code = -1;
|
186 |
goto the_end;
|
187 |
}
|
188 |
avio_seek(pb, pos, SEEK_SET);
|
189 |
start_code = avio_rb32(pb);
|
190 |
if ((start_code & 0xffffff00) == 0x100)
|
191 |
break;
|
192 |
}
|
193 |
the_end:
|
194 |
*size_ptr = pos_start - pos;
|
195 |
return start_code;
|
196 |
}
|
197 |
#endif
|
198 |
|
199 |
/**
|
200 |
* Extract stream types from a program stream map
|
201 |
* According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
|
202 |
*
|
203 |
* @return number of bytes occupied by PSM in the bitstream
|
204 |
*/
|
205 |
static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb) |
206 |
{ |
207 |
int psm_length, ps_info_length, es_map_length;
|
208 |
|
209 |
psm_length = avio_rb16(pb); |
210 |
avio_r8(pb); |
211 |
avio_r8(pb); |
212 |
ps_info_length = avio_rb16(pb); |
213 |
|
214 |
/* skip program_stream_info */
|
215 |
avio_seek(pb, ps_info_length, SEEK_CUR); |
216 |
es_map_length = avio_rb16(pb); |
217 |
|
218 |
/* at least one es available? */
|
219 |
while (es_map_length >= 4){ |
220 |
unsigned char type = avio_r8(pb); |
221 |
unsigned char es_id = avio_r8(pb); |
222 |
uint16_t es_info_length = avio_rb16(pb); |
223 |
/* remember mapping from stream id to stream type */
|
224 |
m->psm_es_type[es_id] = type; |
225 |
/* skip program_stream_info */
|
226 |
avio_seek(pb, es_info_length, SEEK_CUR); |
227 |
es_map_length -= 4 + es_info_length;
|
228 |
} |
229 |
avio_rb32(pb); /* crc32 */
|
230 |
return 2 + psm_length; |
231 |
} |
232 |
|
233 |
/* read the next PES header. Return its position in ppos
|
234 |
(if not NULL), and its start code, pts and dts.
|
235 |
*/
|
236 |
static int mpegps_read_pes_header(AVFormatContext *s, |
237 |
int64_t *ppos, int *pstart_code,
|
238 |
int64_t *ppts, int64_t *pdts) |
239 |
{ |
240 |
MpegDemuxContext *m = s->priv_data; |
241 |
int len, size, startcode, c, flags, header_len;
|
242 |
int pes_ext, ext2_len, id_ext, skip;
|
243 |
int64_t pts, dts; |
244 |
int64_t last_sync= avio_tell(s->pb); |
245 |
|
246 |
error_redo:
|
247 |
avio_seek(s->pb, last_sync, SEEK_SET); |
248 |
redo:
|
249 |
/* next start code (should be immediately after) */
|
250 |
m->header_state = 0xff;
|
251 |
size = MAX_SYNC_SIZE; |
252 |
startcode = find_next_start_code(s->pb, &size, &m->header_state); |
253 |
last_sync = avio_tell(s->pb); |
254 |
//printf("startcode=%x pos=0x%"PRIx64"\n", startcode, avio_tell(s->pb));
|
255 |
if (startcode < 0){ |
256 |
if(s->pb->eof_reached)
|
257 |
return AVERROR_EOF;
|
258 |
//FIXME we should remember header_state
|
259 |
return AVERROR(EAGAIN);
|
260 |
} |
261 |
|
262 |
if (startcode == PACK_START_CODE)
|
263 |
goto redo;
|
264 |
if (startcode == SYSTEM_HEADER_START_CODE)
|
265 |
goto redo;
|
266 |
if (startcode == PADDING_STREAM) {
|
267 |
avio_seek(s->pb, avio_rb16(s->pb), SEEK_CUR); |
268 |
goto redo;
|
269 |
} |
270 |
if (startcode == PRIVATE_STREAM_2) {
|
271 |
len = avio_rb16(s->pb); |
272 |
if (!m->sofdec) {
|
273 |
while (len-- >= 6) { |
274 |
if (avio_r8(s->pb) == 'S') { |
275 |
uint8_t buf[5];
|
276 |
avio_read(s->pb, buf, sizeof(buf));
|
277 |
m->sofdec = !memcmp(buf, "ofdec", 5); |
278 |
len -= sizeof(buf);
|
279 |
break;
|
280 |
} |
281 |
} |
282 |
m->sofdec -= !m->sofdec; |
283 |
} |
284 |
avio_seek(s->pb, len, SEEK_CUR); |
285 |
goto redo;
|
286 |
} |
287 |
if (startcode == PROGRAM_STREAM_MAP) {
|
288 |
mpegps_psm_parse(m, s->pb); |
289 |
goto redo;
|
290 |
} |
291 |
|
292 |
/* find matching stream */
|
293 |
if (!((startcode >= 0x1c0 && startcode <= 0x1df) || |
294 |
(startcode >= 0x1e0 && startcode <= 0x1ef) || |
295 |
(startcode == 0x1bd) || (startcode == 0x1fd))) |
296 |
goto redo;
|
297 |
if (ppos) {
|
298 |
*ppos = avio_tell(s->pb) - 4;
|
299 |
} |
300 |
len = avio_rb16(s->pb); |
301 |
pts = |
302 |
dts = AV_NOPTS_VALUE; |
303 |
/* stuffing */
|
304 |
for(;;) {
|
305 |
if (len < 1) |
306 |
goto error_redo;
|
307 |
c = avio_r8(s->pb); |
308 |
len--; |
309 |
/* XXX: for mpeg1, should test only bit 7 */
|
310 |
if (c != 0xff) |
311 |
break;
|
312 |
} |
313 |
if ((c & 0xc0) == 0x40) { |
314 |
/* buffer scale & size */
|
315 |
avio_r8(s->pb); |
316 |
c = avio_r8(s->pb); |
317 |
len -= 2;
|
318 |
} |
319 |
if ((c & 0xe0) == 0x20) { |
320 |
dts = pts = get_pts(s->pb, c); |
321 |
len -= 4;
|
322 |
if (c & 0x10){ |
323 |
dts = get_pts(s->pb, -1);
|
324 |
len -= 5;
|
325 |
} |
326 |
} else if ((c & 0xc0) == 0x80) { |
327 |
/* mpeg 2 PES */
|
328 |
#if 0 /* some streams have this field set for no apparent reason */
|
329 |
if ((c & 0x30) != 0) {
|
330 |
/* Encrypted multiplex not handled */
|
331 |
goto redo;
|
332 |
}
|
333 |
#endif
|
334 |
flags = avio_r8(s->pb); |
335 |
header_len = avio_r8(s->pb); |
336 |
len -= 2;
|
337 |
if (header_len > len)
|
338 |
goto error_redo;
|
339 |
len -= header_len; |
340 |
if (flags & 0x80) { |
341 |
dts = pts = get_pts(s->pb, -1);
|
342 |
header_len -= 5;
|
343 |
if (flags & 0x40) { |
344 |
dts = get_pts(s->pb, -1);
|
345 |
header_len -= 5;
|
346 |
} |
347 |
} |
348 |
if (flags & 0x3f && header_len == 0){ |
349 |
flags &= 0xC0;
|
350 |
av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
|
351 |
} |
352 |
if (flags & 0x01) { /* PES extension */ |
353 |
pes_ext = avio_r8(s->pb); |
354 |
header_len--; |
355 |
/* Skip PES private data, program packet sequence counter and P-STD buffer */
|
356 |
skip = (pes_ext >> 4) & 0xb; |
357 |
skip += skip & 0x9;
|
358 |
if (pes_ext & 0x40 || skip > header_len){ |
359 |
av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
|
360 |
pes_ext=skip=0;
|
361 |
} |
362 |
avio_seek(s->pb, skip, SEEK_CUR); |
363 |
header_len -= skip; |
364 |
|
365 |
if (pes_ext & 0x01) { /* PES extension 2 */ |
366 |
ext2_len = avio_r8(s->pb); |
367 |
header_len--; |
368 |
if ((ext2_len & 0x7f) > 0) { |
369 |
id_ext = avio_r8(s->pb); |
370 |
if ((id_ext & 0x80) == 0) |
371 |
startcode = ((startcode & 0xff) << 8) | id_ext; |
372 |
header_len--; |
373 |
} |
374 |
} |
375 |
} |
376 |
if(header_len < 0) |
377 |
goto error_redo;
|
378 |
avio_seek(s->pb, header_len, SEEK_CUR); |
379 |
} |
380 |
else if( c!= 0xf ) |
381 |
goto redo;
|
382 |
|
383 |
if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) { |
384 |
startcode = avio_r8(s->pb); |
385 |
len--; |
386 |
if (startcode >= 0x80 && startcode <= 0xcf) { |
387 |
/* audio: skip header */
|
388 |
avio_r8(s->pb); |
389 |
avio_r8(s->pb); |
390 |
avio_r8(s->pb); |
391 |
len -= 3;
|
392 |
if (startcode >= 0xb0 && startcode <= 0xbf) { |
393 |
/* MLP/TrueHD audio has a 4-byte header */
|
394 |
avio_r8(s->pb); |
395 |
len--; |
396 |
} |
397 |
} |
398 |
} |
399 |
if(len<0) |
400 |
goto error_redo;
|
401 |
if(dts != AV_NOPTS_VALUE && ppos){
|
402 |
int i;
|
403 |
for(i=0; i<s->nb_streams; i++){ |
404 |
if(startcode == s->streams[i]->id &&
|
405 |
!url_is_streamed(s->pb) /* index useless on streams anyway */) {
|
406 |
ff_reduce_index(s, i); |
407 |
av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */); |
408 |
} |
409 |
} |
410 |
} |
411 |
|
412 |
*pstart_code = startcode; |
413 |
*ppts = pts; |
414 |
*pdts = dts; |
415 |
return len;
|
416 |
} |
417 |
|
418 |
static int mpegps_read_packet(AVFormatContext *s, |
419 |
AVPacket *pkt) |
420 |
{ |
421 |
MpegDemuxContext *m = s->priv_data; |
422 |
AVStream *st; |
423 |
int len, startcode, i, es_type;
|
424 |
enum CodecID codec_id = CODEC_ID_NONE;
|
425 |
enum AVMediaType type;
|
426 |
int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
|
427 |
uint8_t av_uninit(dvdaudio_substream_type); |
428 |
|
429 |
redo:
|
430 |
len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts); |
431 |
if (len < 0) |
432 |
return len;
|
433 |
|
434 |
if(startcode == 0x1bd) { |
435 |
dvdaudio_substream_type = avio_r8(s->pb); |
436 |
avio_seek(s->pb, 3, SEEK_CUR);
|
437 |
len -= 4;
|
438 |
} |
439 |
|
440 |
/* now find stream */
|
441 |
for(i=0;i<s->nb_streams;i++) { |
442 |
st = s->streams[i]; |
443 |
if (st->id == startcode)
|
444 |
goto found;
|
445 |
} |
446 |
|
447 |
es_type = m->psm_es_type[startcode & 0xff];
|
448 |
if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){ |
449 |
if(es_type == STREAM_TYPE_VIDEO_MPEG1){
|
450 |
codec_id = CODEC_ID_MPEG2VIDEO; |
451 |
type = AVMEDIA_TYPE_VIDEO; |
452 |
} else if(es_type == STREAM_TYPE_VIDEO_MPEG2){ |
453 |
codec_id = CODEC_ID_MPEG2VIDEO; |
454 |
type = AVMEDIA_TYPE_VIDEO; |
455 |
} else if(es_type == STREAM_TYPE_AUDIO_MPEG1 || |
456 |
es_type == STREAM_TYPE_AUDIO_MPEG2){ |
457 |
codec_id = CODEC_ID_MP3; |
458 |
type = AVMEDIA_TYPE_AUDIO; |
459 |
} else if(es_type == STREAM_TYPE_AUDIO_AAC){ |
460 |
codec_id = CODEC_ID_AAC; |
461 |
type = AVMEDIA_TYPE_AUDIO; |
462 |
} else if(es_type == STREAM_TYPE_VIDEO_MPEG4){ |
463 |
codec_id = CODEC_ID_MPEG4; |
464 |
type = AVMEDIA_TYPE_VIDEO; |
465 |
} else if(es_type == STREAM_TYPE_VIDEO_H264){ |
466 |
codec_id = CODEC_ID_H264; |
467 |
type = AVMEDIA_TYPE_VIDEO; |
468 |
} else if(es_type == STREAM_TYPE_AUDIO_AC3){ |
469 |
codec_id = CODEC_ID_AC3; |
470 |
type = AVMEDIA_TYPE_AUDIO; |
471 |
} else {
|
472 |
goto skip;
|
473 |
} |
474 |
} else if (startcode >= 0x1e0 && startcode <= 0x1ef) { |
475 |
static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 }; |
476 |
unsigned char buf[8]; |
477 |
avio_read(s->pb, buf, 8);
|
478 |
avio_seek(s->pb, -8, SEEK_CUR);
|
479 |
if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1)) |
480 |
codec_id = CODEC_ID_CAVS; |
481 |
else
|
482 |
codec_id = CODEC_ID_PROBE; |
483 |
type = AVMEDIA_TYPE_VIDEO; |
484 |
} else if (startcode >= 0x1c0 && startcode <= 0x1df) { |
485 |
type = AVMEDIA_TYPE_AUDIO; |
486 |
codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
|
487 |
} else if (startcode >= 0x80 && startcode <= 0x87) { |
488 |
type = AVMEDIA_TYPE_AUDIO; |
489 |
codec_id = CODEC_ID_AC3; |
490 |
} else if ( ( startcode >= 0x88 && startcode <= 0x8f) |
491 |
||( startcode >= 0x98 && startcode <= 0x9f)) { |
492 |
/* 0x90 - 0x97 is reserved for SDDS in DVD specs */
|
493 |
type = AVMEDIA_TYPE_AUDIO; |
494 |
codec_id = CODEC_ID_DTS; |
495 |
} else if (startcode >= 0xa0 && startcode <= 0xaf) { |
496 |
type = AVMEDIA_TYPE_AUDIO; |
497 |
/* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
|
498 |
codec_id = CODEC_ID_PCM_DVD; |
499 |
} else if (startcode >= 0xb0 && startcode <= 0xbf) { |
500 |
type = AVMEDIA_TYPE_AUDIO; |
501 |
codec_id = CODEC_ID_TRUEHD; |
502 |
} else if (startcode >= 0xc0 && startcode <= 0xcf) { |
503 |
/* Used for both AC-3 and E-AC-3 in EVOB files */
|
504 |
type = AVMEDIA_TYPE_AUDIO; |
505 |
codec_id = CODEC_ID_AC3; |
506 |
} else if (startcode >= 0x20 && startcode <= 0x3f) { |
507 |
type = AVMEDIA_TYPE_SUBTITLE; |
508 |
codec_id = CODEC_ID_DVD_SUBTITLE; |
509 |
} else if (startcode >= 0xfd55 && startcode <= 0xfd5f) { |
510 |
type = AVMEDIA_TYPE_VIDEO; |
511 |
codec_id = CODEC_ID_VC1; |
512 |
} else if (startcode == 0x1bd) { |
513 |
// check dvd audio substream type
|
514 |
type = AVMEDIA_TYPE_AUDIO; |
515 |
switch(dvdaudio_substream_type & 0xe0) { |
516 |
case 0xa0: codec_id = CODEC_ID_PCM_DVD; |
517 |
break;
|
518 |
case 0x80: if((dvdaudio_substream_type & 0xf8) == 0x88) |
519 |
codec_id = CODEC_ID_DTS; |
520 |
else codec_id = CODEC_ID_AC3;
|
521 |
break;
|
522 |
default: av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n"); |
523 |
goto skip;
|
524 |
} |
525 |
} else {
|
526 |
skip:
|
527 |
/* skip packet */
|
528 |
avio_seek(s->pb, len, SEEK_CUR); |
529 |
goto redo;
|
530 |
} |
531 |
/* no stream found: add a new stream */
|
532 |
st = av_new_stream(s, startcode); |
533 |
if (!st)
|
534 |
goto skip;
|
535 |
st->codec->codec_type = type; |
536 |
st->codec->codec_id = codec_id; |
537 |
if (codec_id != CODEC_ID_PCM_S16BE)
|
538 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
539 |
found:
|
540 |
if(st->discard >= AVDISCARD_ALL)
|
541 |
goto skip;
|
542 |
if ((startcode >= 0xa0 && startcode <= 0xaf) || |
543 |
(startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) { |
544 |
int b1, freq;
|
545 |
|
546 |
/* for LPCM, we just skip the header and consider it is raw
|
547 |
audio data */
|
548 |
if (len <= 3) |
549 |
goto skip;
|
550 |
avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
|
551 |
b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
|
552 |
avio_r8(s->pb); /* dynamic range control (0x80 = off) */
|
553 |
len -= 3;
|
554 |
freq = (b1 >> 4) & 3; |
555 |
st->codec->sample_rate = lpcm_freq_tab[freq]; |
556 |
st->codec->channels = 1 + (b1 & 7); |
557 |
st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4; |
558 |
st->codec->bit_rate = st->codec->channels * |
559 |
st->codec->sample_rate * |
560 |
st->codec->bits_per_coded_sample; |
561 |
if (st->codec->bits_per_coded_sample == 16) |
562 |
st->codec->codec_id = CODEC_ID_PCM_S16BE; |
563 |
else if (st->codec->bits_per_coded_sample == 28) |
564 |
return AVERROR(EINVAL);
|
565 |
} |
566 |
av_new_packet(pkt, len); |
567 |
avio_read(s->pb, pkt->data, pkt->size); |
568 |
pkt->pts = pts; |
569 |
pkt->dts = dts; |
570 |
pkt->pos = dummy_pos; |
571 |
pkt->stream_index = st->index; |
572 |
#if 0
|
573 |
av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
|
574 |
pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
|
575 |
#endif
|
576 |
|
577 |
return 0; |
578 |
} |
579 |
|
580 |
static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index, |
581 |
int64_t *ppos, int64_t pos_limit) |
582 |
{ |
583 |
int len, startcode;
|
584 |
int64_t pos, pts, dts; |
585 |
|
586 |
pos = *ppos; |
587 |
#ifdef DEBUG_SEEK
|
588 |
printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next); |
589 |
#endif
|
590 |
if (avio_seek(s->pb, pos, SEEK_SET) < 0) |
591 |
return AV_NOPTS_VALUE;
|
592 |
|
593 |
for(;;) {
|
594 |
len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts); |
595 |
if (len < 0) { |
596 |
#ifdef DEBUG_SEEK
|
597 |
printf("none (ret=%d)\n", len);
|
598 |
#endif
|
599 |
return AV_NOPTS_VALUE;
|
600 |
} |
601 |
if (startcode == s->streams[stream_index]->id &&
|
602 |
dts != AV_NOPTS_VALUE) { |
603 |
break;
|
604 |
} |
605 |
avio_seek(s->pb, len, SEEK_CUR); |
606 |
} |
607 |
#ifdef DEBUG_SEEK
|
608 |
printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0); |
609 |
#endif
|
610 |
*ppos = pos; |
611 |
return dts;
|
612 |
} |
613 |
|
614 |
AVInputFormat ff_mpegps_demuxer = { |
615 |
"mpeg",
|
616 |
NULL_IF_CONFIG_SMALL("MPEG-PS format"),
|
617 |
sizeof(MpegDemuxContext),
|
618 |
mpegps_probe, |
619 |
mpegps_read_header, |
620 |
mpegps_read_packet, |
621 |
NULL,
|
622 |
NULL, //mpegps_read_seek, |
623 |
mpegps_read_dts, |
624 |
.flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT, |
625 |
}; |