3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
30 /*********************************************/
33 #define MAX_SYNC_SIZE 100000
35 static int cdxa_probe(AVProbeData *p)
37 /* check file header */
38 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
39 p->buf[2] == 'F' && p->buf[3] == 'F' &&
40 p->buf[8] == 'C' && p->buf[9] == 'D' &&
41 p->buf[10] == 'X' && p->buf[11] == 'A')
42 return AVPROBE_SCORE_MAX;
47 static int mpegps_probe(AVProbeData *p)
50 int sys=0, pspack=0, priv1=0, vid=0, audio=0;
54 score = cdxa_probe(p);
55 if (score > 0) return score;
57 /* Search for MPEG stream */
58 for(i=0; i<p->buf_size; i++){
59 code = (code<<8) + p->buf[i];
60 if ((code & 0xffffff00) == 0x100) {
61 if(code == SYSTEM_HEADER_START_CODE) sys++;
62 else if(code == PRIVATE_STREAM_1) priv1++;
63 else if(code == PACK_START_CODE) pspack++;
64 else if((code & 0xf0) == VIDEO_ID) vid++;
65 else if((code & 0xe0) == AUDIO_ID) audio++;
69 if(vid || audio) /* invalid VDR files nd short PES streams */
70 score= AVPROBE_SCORE_MAX/4;
72 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d\n", sys, priv1, pspack,vid, audio);
73 if(sys && sys*9 <= pspack*10)
74 return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
75 if((priv1 || vid || audio) && (priv1+vid+audio)*9 <= pspack*10)
76 return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
77 if((!!vid ^ !!audio) && (audio+vid > 1) && !sys && !pspack) /* PES stream */
78 return AVPROBE_SCORE_MAX/2+2;
80 //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
85 typedef struct MpegDemuxContext {
87 unsigned char psm_es_type[256];
90 static int mpegps_read_header(AVFormatContext *s,
91 AVFormatParameters *ap)
93 MpegDemuxContext *m = s->priv_data;
94 m->header_state = 0xff;
95 s->ctx_flags |= AVFMTCTX_NOHEADER;
97 /* no need to do more */
101 static int64_t get_pts(ByteIOContext *pb, int c)
108 pts = (int64_t)((c >> 1) & 0x07) << 30;
110 pts |= (int64_t)(val >> 1) << 15;
112 pts |= (int64_t)(val >> 1);
116 static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
117 int32_t *header_state)
119 unsigned int state, v;
122 state = *header_state;
129 if (state == 0x000001) {
130 state = ((state << 8) | v) & 0xffffff;
134 state = ((state << 8) | v) & 0xffffff;
138 *header_state = state;
143 #if 0 /* unused, remove? */
145 static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
147 int64_t pos, pos_start;
148 int max_size, start_code;
150 max_size = *size_ptr;
151 pos_start = url_ftell(pb);
153 /* in order to go faster, we fill the buffer */
154 pos = pos_start - 16386;
157 url_fseek(pb, pos, SEEK_SET);
163 if (pos < 0 || (pos_start - pos) >= max_size) {
167 url_fseek(pb, pos, SEEK_SET);
168 start_code = get_be32(pb);
169 if ((start_code & 0xffffff00) == 0x100)
173 *size_ptr = pos_start - pos;
179 * Extracts stream types from a program stream map
180 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
182 * @return number of bytes occupied by PSM in the bitstream
184 static long mpegps_psm_parse(MpegDemuxContext *m, ByteIOContext *pb)
186 int psm_length, ps_info_length, es_map_length;
188 psm_length = get_be16(pb);
191 ps_info_length = get_be16(pb);
193 /* skip program_stream_info */
194 url_fskip(pb, ps_info_length);
195 es_map_length = get_be16(pb);
197 /* at least one es available? */
198 while (es_map_length >= 4){
199 unsigned char type = get_byte(pb);
200 unsigned char es_id = get_byte(pb);
201 uint16_t es_info_length = get_be16(pb);
202 /* remember mapping from stream id to stream type */
203 m->psm_es_type[es_id] = type;
204 /* skip program_stream_info */
205 url_fskip(pb, es_info_length);
206 es_map_length -= 4 + es_info_length;
208 get_be32(pb); /* crc32 */
209 return 2 + psm_length;
212 /* read the next PES header. Return its position in ppos
213 (if not NULL), and its start code, pts and dts.
215 static int mpegps_read_pes_header(AVFormatContext *s,
216 int64_t *ppos, int *pstart_code,
217 int64_t *ppts, int64_t *pdts)
219 MpegDemuxContext *m = s->priv_data;
220 int len, size, startcode, c, flags, header_len;
221 int pes_ext, ext2_len, id_ext, skip;
223 int64_t last_sync= url_ftell(&s->pb);
226 url_fseek(&s->pb, last_sync, SEEK_SET);
228 /* next start code (should be immediately after) */
229 m->header_state = 0xff;
230 size = MAX_SYNC_SIZE;
231 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
232 last_sync = url_ftell(&s->pb);
233 //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(&s->pb));
236 if (startcode == PACK_START_CODE)
238 if (startcode == SYSTEM_HEADER_START_CODE)
240 if (startcode == PADDING_STREAM ||
241 startcode == PRIVATE_STREAM_2) {
243 len = get_be16(&s->pb);
244 url_fskip(&s->pb, len);
247 if (startcode == PROGRAM_STREAM_MAP) {
248 mpegps_psm_parse(m, &s->pb);
252 /* find matching stream */
253 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
254 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
255 (startcode == 0x1bd) || (startcode == 0x1fd)))
258 *ppos = url_ftell(&s->pb) - 4;
260 len = get_be16(&s->pb);
262 dts = AV_NOPTS_VALUE;
267 c = get_byte(&s->pb);
269 /* XXX: for mpeg1, should test only bit 7 */
273 if ((c & 0xc0) == 0x40) {
274 /* buffer scale & size */
276 c = get_byte(&s->pb);
279 if ((c & 0xe0) == 0x20) {
280 dts = pts = get_pts(&s->pb, c);
283 dts = get_pts(&s->pb, -1);
286 } else if ((c & 0xc0) == 0x80) {
288 #if 0 /* some streams have this field set for no apparent reason */
289 if ((c & 0x30) != 0) {
290 /* Encrypted multiplex not handled */
294 flags = get_byte(&s->pb);
295 header_len = get_byte(&s->pb);
297 if (header_len > len)
301 dts = pts = get_pts(&s->pb, -1);
304 dts = get_pts(&s->pb, -1);
308 if (flags & 0x01) { /* PES extension */
309 pes_ext = get_byte(&s->pb);
311 if (pes_ext & 0x40) { /* pack header - should be zero in PS */
314 /* Skip PES private data, program packet sequence counter and P-STD buffer */
315 skip = (pes_ext >> 4) & 0xb;
317 url_fskip(&s->pb, skip);
320 if (pes_ext & 0x01) { /* PES extension 2 */
321 ext2_len = get_byte(&s->pb);
323 if ((ext2_len & 0x7f) > 0) {
324 id_ext = get_byte(&s->pb);
325 if ((id_ext & 0x80) == 0)
326 startcode = ((startcode & 0xff) << 8) | id_ext;
333 url_fskip(&s->pb, header_len);
338 if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
339 startcode = get_byte(&s->pb);
341 if (startcode >= 0x80 && startcode <= 0xcf) {
342 /* audio: skip header */
347 if (startcode >= 0xb0 && startcode <= 0xbf) {
348 /* MLP/TrueHD audio has a 4-byte header */
356 if(dts != AV_NOPTS_VALUE && ppos){
358 for(i=0; i<s->nb_streams; i++){
359 if(startcode == s->streams[i]->id) {
360 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
365 *pstart_code = startcode;
371 static int mpegps_read_packet(AVFormatContext *s,
374 MpegDemuxContext *m = s->priv_data;
376 int len, startcode, i, type, codec_id = 0, es_type;
377 int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
380 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
384 /* now find stream */
385 for(i=0;i<s->nb_streams;i++) {
387 if (st->id == startcode)
391 es_type = m->psm_es_type[startcode & 0xff];
393 if(es_type == STREAM_TYPE_VIDEO_MPEG1){
394 codec_id = CODEC_ID_MPEG2VIDEO;
395 type = CODEC_TYPE_VIDEO;
396 } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
397 codec_id = CODEC_ID_MPEG2VIDEO;
398 type = CODEC_TYPE_VIDEO;
399 } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
400 es_type == STREAM_TYPE_AUDIO_MPEG2){
401 codec_id = CODEC_ID_MP3;
402 type = CODEC_TYPE_AUDIO;
403 } else if(es_type == STREAM_TYPE_AUDIO_AAC){
404 codec_id = CODEC_ID_AAC;
405 type = CODEC_TYPE_AUDIO;
406 } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
407 codec_id = CODEC_ID_MPEG4;
408 type = CODEC_TYPE_VIDEO;
409 } else if(es_type == STREAM_TYPE_VIDEO_H264){
410 codec_id = CODEC_ID_H264;
411 type = CODEC_TYPE_VIDEO;
412 } else if(es_type == STREAM_TYPE_AUDIO_AC3){
413 codec_id = CODEC_ID_AC3;
414 type = CODEC_TYPE_AUDIO;
418 } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
419 static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
420 unsigned char buf[8];
421 get_buffer(&s->pb, buf, 8);
422 url_fseek(&s->pb, -8, SEEK_CUR);
423 if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
424 codec_id = CODEC_ID_CAVS;
426 codec_id = CODEC_ID_MPEG2VIDEO;
427 type = CODEC_TYPE_VIDEO;
428 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
429 type = CODEC_TYPE_AUDIO;
430 codec_id = CODEC_ID_MP2;
431 } else if (startcode >= 0x80 && startcode <= 0x87) {
432 type = CODEC_TYPE_AUDIO;
433 codec_id = CODEC_ID_AC3;
434 } else if ((startcode >= 0x88 && startcode <= 0x8f)
435 ||( startcode >= 0x98 && startcode <= 0x9f)) {
436 /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
437 type = CODEC_TYPE_AUDIO;
438 codec_id = CODEC_ID_DTS;
439 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
440 type = CODEC_TYPE_AUDIO;
441 codec_id = CODEC_ID_PCM_S16BE;
442 } else if (startcode >= 0xb0 && startcode <= 0xbf) {
443 type = CODEC_TYPE_AUDIO;
444 codec_id = CODEC_ID_MLP;
445 } else if (startcode >= 0xc0 && startcode <= 0xcf) {
446 /* Used for both AC-3 and E-AC-3 in EVOB files */
447 type = CODEC_TYPE_AUDIO;
448 codec_id = CODEC_ID_AC3;
449 } else if (startcode >= 0x20 && startcode <= 0x3f) {
450 type = CODEC_TYPE_SUBTITLE;
451 codec_id = CODEC_ID_DVD_SUBTITLE;
452 } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
453 type = CODEC_TYPE_VIDEO;
454 codec_id = CODEC_ID_VC1;
458 url_fskip(&s->pb, len);
461 /* no stream found: add a new stream */
462 st = av_new_stream(s, startcode);
465 st->codec->codec_type = type;
466 st->codec->codec_id = codec_id;
467 if (codec_id != CODEC_ID_PCM_S16BE)
468 st->need_parsing = AVSTREAM_PARSE_FULL;
470 if(st->discard >= AVDISCARD_ALL)
472 if (startcode >= 0xa0 && startcode <= 0xaf) {
475 /* for LPCM, we just skip the header and consider it is raw
479 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
480 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
481 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
483 freq = (b1 >> 4) & 3;
484 st->codec->sample_rate = lpcm_freq_tab[freq];
485 st->codec->channels = 1 + (b1 & 7);
486 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * 2;
488 av_new_packet(pkt, len);
489 get_buffer(&s->pb, pkt->data, pkt->size);
492 pkt->stream_index = st->index;
494 av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
495 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
501 static int mpegps_read_close(AVFormatContext *s)
506 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
507 int64_t *ppos, int64_t pos_limit)
510 int64_t pos, pts, dts;
514 printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
516 url_fseek(&s->pb, pos, SEEK_SET);
518 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
521 printf("none (ret=%d)\n", len);
523 return AV_NOPTS_VALUE;
525 if (startcode == s->streams[stream_index]->id &&
526 dts != AV_NOPTS_VALUE) {
529 url_fskip(&s->pb, len);
532 printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
538 #ifdef CONFIG_MPEGPS_DEMUXER
539 AVInputFormat mpegps_demuxer = {
542 sizeof(MpegDemuxContext),
547 NULL, //mpegps_read_seek,
549 .flags = AVFMT_SHOW_IDS,