00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "mpeg.h"
00025
00026
00027
00028 #undef NDEBUG
00029 #include <assert.h>
00030
00031
00032
00033
00034 #define MAX_SYNC_SIZE 100000
00035
00036 static int check_pes(uint8_t *p, uint8_t *end){
00037 int pes1;
00038 int pes2= (p[3] & 0xC0) == 0x80
00039 && (p[4] & 0xC0) != 0x40
00040 &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
00041
00042 for(p+=3; p<end && *p == 0xFF; p++);
00043 if((*p&0xC0) == 0x40) p+=2;
00044 if((*p&0xF0) == 0x20){
00045 pes1= p[0]&p[2]&p[4]&1;
00046 }else if((*p&0xF0) == 0x30){
00047 pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
00048 }else
00049 pes1 = *p == 0x0F;
00050
00051 return pes1||pes2;
00052 }
00053
00054 static int mpegps_probe(AVProbeData *p)
00055 {
00056 uint32_t code= -1;
00057 int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
00058 int i;
00059 int score=0;
00060
00061 for(i=0; i<p->buf_size; i++){
00062 code = (code<<8) + p->buf[i];
00063 if ((code & 0xffffff00) == 0x100) {
00064 int len= p->buf[i+1] << 8 | p->buf[i+2];
00065 int pes= check_pes(p->buf+i, p->buf+p->buf_size);
00066
00067 if(code == SYSTEM_HEADER_START_CODE) sys++;
00068 else if(code == PACK_START_CODE) pspack++;
00069 else if((code & 0xf0) == VIDEO_ID && pes) vid++;
00070
00071
00072 else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;}
00073 else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;}
00074
00075 else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
00076 else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
00077 else if(code == PRIVATE_STREAM_1 && !pes) invalid++;
00078 }
00079 }
00080
00081 if(vid+audio > invalid)
00082 score= AVPROBE_SCORE_MAX/4;
00083
00084
00085 if(sys>invalid && sys*9 <= pspack*10)
00086 return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
00087 if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
00088 return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
00089 if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid)
00090 return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
00091
00092
00093
00094 return score;
00095 }
00096
00097
00098 typedef struct MpegDemuxContext {
00099 int32_t header_state;
00100 unsigned char psm_es_type[256];
00101 int sofdec;
00102 } MpegDemuxContext;
00103
00104 static int mpegps_read_header(AVFormatContext *s,
00105 AVFormatParameters *ap)
00106 {
00107 MpegDemuxContext *m = s->priv_data;
00108 const char *sofdec = "Sofdec";
00109 int v, i = 0;
00110
00111 m->header_state = 0xff;
00112 s->ctx_flags |= AVFMTCTX_NOHEADER;
00113
00114 m->sofdec = -1;
00115 do {
00116 v = avio_r8(s->pb);
00117 m->header_state = m->header_state << 8 | v;
00118 m->sofdec++;
00119 } while (v == sofdec[i] && i++ < 6);
00120
00121 m->sofdec = (m->sofdec == 6) ? 1 : 0;
00122
00123
00124 return 0;
00125 }
00126
00127 static int64_t get_pts(AVIOContext *pb, int c)
00128 {
00129 uint8_t buf[5];
00130
00131 buf[0] = c<0 ? avio_r8(pb) : c;
00132 avio_read(pb, buf+1, 4);
00133
00134 return ff_parse_pes_pts(buf);
00135 }
00136
00137 static int find_next_start_code(AVIOContext *pb, int *size_ptr,
00138 int32_t *header_state)
00139 {
00140 unsigned int state, v;
00141 int val, n;
00142
00143 state = *header_state;
00144 n = *size_ptr;
00145 while (n > 0) {
00146 if (url_feof(pb))
00147 break;
00148 v = avio_r8(pb);
00149 n--;
00150 if (state == 0x000001) {
00151 state = ((state << 8) | v) & 0xffffff;
00152 val = state;
00153 goto found;
00154 }
00155 state = ((state << 8) | v) & 0xffffff;
00156 }
00157 val = -1;
00158 found:
00159 *header_state = state;
00160 *size_ptr = n;
00161 return val;
00162 }
00163
00164 #if 0
00165
00166 static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
00167 {
00168 int64_t pos, pos_start;
00169 int max_size, start_code;
00170
00171 max_size = *size_ptr;
00172 pos_start = avio_tell(pb);
00173
00174
00175 pos = pos_start - 16386;
00176 if (pos < 0)
00177 pos = 0;
00178 avio_seek(pb, pos, SEEK_SET);
00179 avio_r8(pb);
00180
00181 pos = pos_start;
00182 for(;;) {
00183 pos--;
00184 if (pos < 0 || (pos_start - pos) >= max_size) {
00185 start_code = -1;
00186 goto the_end;
00187 }
00188 avio_seek(pb, pos, SEEK_SET);
00189 start_code = avio_rb32(pb);
00190 if ((start_code & 0xffffff00) == 0x100)
00191 break;
00192 }
00193 the_end:
00194 *size_ptr = pos_start - pos;
00195 return start_code;
00196 }
00197 #endif
00198
00205 static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
00206 {
00207 int psm_length, ps_info_length, es_map_length;
00208
00209 psm_length = avio_rb16(pb);
00210 avio_r8(pb);
00211 avio_r8(pb);
00212 ps_info_length = avio_rb16(pb);
00213
00214
00215 avio_seek(pb, ps_info_length, SEEK_CUR);
00216 es_map_length = avio_rb16(pb);
00217
00218
00219 while (es_map_length >= 4){
00220 unsigned char type = avio_r8(pb);
00221 unsigned char es_id = avio_r8(pb);
00222 uint16_t es_info_length = avio_rb16(pb);
00223
00224 m->psm_es_type[es_id] = type;
00225
00226 avio_seek(pb, es_info_length, SEEK_CUR);
00227 es_map_length -= 4 + es_info_length;
00228 }
00229 avio_rb32(pb);
00230 return 2 + psm_length;
00231 }
00232
00233
00234
00235
00236 static int mpegps_read_pes_header(AVFormatContext *s,
00237 int64_t *ppos, int *pstart_code,
00238 int64_t *ppts, int64_t *pdts)
00239 {
00240 MpegDemuxContext *m = s->priv_data;
00241 int len, size, startcode, c, flags, header_len;
00242 int pes_ext, ext2_len, id_ext, skip;
00243 int64_t pts, dts;
00244 int64_t last_sync= avio_tell(s->pb);
00245
00246 error_redo:
00247 avio_seek(s->pb, last_sync, SEEK_SET);
00248 redo:
00249
00250 m->header_state = 0xff;
00251 size = MAX_SYNC_SIZE;
00252 startcode = find_next_start_code(s->pb, &size, &m->header_state);
00253 last_sync = avio_tell(s->pb);
00254
00255 if (startcode < 0){
00256 if(url_feof(s->pb))
00257 return AVERROR_EOF;
00258
00259 return AVERROR(EAGAIN);
00260 }
00261
00262 if (startcode == PACK_START_CODE)
00263 goto redo;
00264 if (startcode == SYSTEM_HEADER_START_CODE)
00265 goto redo;
00266 if (startcode == PADDING_STREAM) {
00267 avio_seek(s->pb, avio_rb16(s->pb), SEEK_CUR);
00268 goto redo;
00269 }
00270 if (startcode == PRIVATE_STREAM_2) {
00271 len = avio_rb16(s->pb);
00272 if (!m->sofdec) {
00273 while (len-- >= 6) {
00274 if (avio_r8(s->pb) == 'S') {
00275 uint8_t buf[5];
00276 avio_read(s->pb, buf, sizeof(buf));
00277 m->sofdec = !memcmp(buf, "ofdec", 5);
00278 len -= sizeof(buf);
00279 break;
00280 }
00281 }
00282 m->sofdec -= !m->sofdec;
00283 }
00284 avio_seek(s->pb, len, SEEK_CUR);
00285 goto redo;
00286 }
00287 if (startcode == PROGRAM_STREAM_MAP) {
00288 mpegps_psm_parse(m, s->pb);
00289 goto redo;
00290 }
00291
00292
00293 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
00294 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
00295 (startcode == 0x1bd) || (startcode == 0x1fd)))
00296 goto redo;
00297 if (ppos) {
00298 *ppos = avio_tell(s->pb) - 4;
00299 }
00300 len = avio_rb16(s->pb);
00301 pts =
00302 dts = AV_NOPTS_VALUE;
00303
00304 for(;;) {
00305 if (len < 1)
00306 goto error_redo;
00307 c = avio_r8(s->pb);
00308 len--;
00309
00310 if (c != 0xff)
00311 break;
00312 }
00313 if ((c & 0xc0) == 0x40) {
00314
00315 avio_r8(s->pb);
00316 c = avio_r8(s->pb);
00317 len -= 2;
00318 }
00319 if ((c & 0xe0) == 0x20) {
00320 dts = pts = get_pts(s->pb, c);
00321 len -= 4;
00322 if (c & 0x10){
00323 dts = get_pts(s->pb, -1);
00324 len -= 5;
00325 }
00326 } else if ((c & 0xc0) == 0x80) {
00327
00328 #if 0
00329 if ((c & 0x30) != 0) {
00330
00331 goto redo;
00332 }
00333 #endif
00334 flags = avio_r8(s->pb);
00335 header_len = avio_r8(s->pb);
00336 len -= 2;
00337 if (header_len > len)
00338 goto error_redo;
00339 len -= header_len;
00340 if (flags & 0x80) {
00341 dts = pts = get_pts(s->pb, -1);
00342 header_len -= 5;
00343 if (flags & 0x40) {
00344 dts = get_pts(s->pb, -1);
00345 header_len -= 5;
00346 }
00347 }
00348 if (flags & 0x3f && header_len == 0){
00349 flags &= 0xC0;
00350 av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
00351 }
00352 if (flags & 0x01) {
00353 pes_ext = avio_r8(s->pb);
00354 header_len--;
00355
00356 skip = (pes_ext >> 4) & 0xb;
00357 skip += skip & 0x9;
00358 if (pes_ext & 0x40 || skip > header_len){
00359 av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
00360 pes_ext=skip=0;
00361 }
00362 avio_seek(s->pb, skip, SEEK_CUR);
00363 header_len -= skip;
00364
00365 if (pes_ext & 0x01) {
00366 ext2_len = avio_r8(s->pb);
00367 header_len--;
00368 if ((ext2_len & 0x7f) > 0) {
00369 id_ext = avio_r8(s->pb);
00370 if ((id_ext & 0x80) == 0)
00371 startcode = ((startcode & 0xff) << 8) | id_ext;
00372 header_len--;
00373 }
00374 }
00375 }
00376 if(header_len < 0)
00377 goto error_redo;
00378 avio_seek(s->pb, header_len, SEEK_CUR);
00379 }
00380 else if( c!= 0xf )
00381 goto redo;
00382
00383 if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
00384 startcode = avio_r8(s->pb);
00385 len--;
00386 if (startcode >= 0x80 && startcode <= 0xcf) {
00387
00388 avio_r8(s->pb);
00389 avio_r8(s->pb);
00390 avio_r8(s->pb);
00391 len -= 3;
00392 if (startcode >= 0xb0 && startcode <= 0xbf) {
00393
00394 avio_r8(s->pb);
00395 len--;
00396 }
00397 }
00398 }
00399 if(len<0)
00400 goto error_redo;
00401 if(dts != AV_NOPTS_VALUE && ppos){
00402 int i;
00403 for(i=0; i<s->nb_streams; i++){
00404 if(startcode == s->streams[i]->id &&
00405 !url_is_streamed(s->pb) ) {
00406 ff_reduce_index(s, i);
00407 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME );
00408 }
00409 }
00410 }
00411
00412 *pstart_code = startcode;
00413 *ppts = pts;
00414 *pdts = dts;
00415 return len;
00416 }
00417
00418 static int mpegps_read_packet(AVFormatContext *s,
00419 AVPacket *pkt)
00420 {
00421 MpegDemuxContext *m = s->priv_data;
00422 AVStream *st;
00423 int len, startcode, i, es_type;
00424 enum CodecID codec_id = CODEC_ID_NONE;
00425 enum AVMediaType type;
00426 int64_t pts, dts, dummy_pos;
00427 uint8_t av_uninit(dvdaudio_substream_type);
00428
00429 redo:
00430 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
00431 if (len < 0)
00432 return len;
00433
00434 if(startcode == 0x1bd) {
00435 dvdaudio_substream_type = avio_r8(s->pb);
00436 avio_seek(s->pb, 3, SEEK_CUR);
00437 len -= 4;
00438 }
00439
00440
00441 for(i=0;i<s->nb_streams;i++) {
00442 st = s->streams[i];
00443 if (st->id == startcode)
00444 goto found;
00445 }
00446
00447 es_type = m->psm_es_type[startcode & 0xff];
00448 if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
00449 if(es_type == STREAM_TYPE_VIDEO_MPEG1){
00450 codec_id = CODEC_ID_MPEG2VIDEO;
00451 type = AVMEDIA_TYPE_VIDEO;
00452 } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
00453 codec_id = CODEC_ID_MPEG2VIDEO;
00454 type = AVMEDIA_TYPE_VIDEO;
00455 } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
00456 es_type == STREAM_TYPE_AUDIO_MPEG2){
00457 codec_id = CODEC_ID_MP3;
00458 type = AVMEDIA_TYPE_AUDIO;
00459 } else if(es_type == STREAM_TYPE_AUDIO_AAC){
00460 codec_id = CODEC_ID_AAC;
00461 type = AVMEDIA_TYPE_AUDIO;
00462 } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
00463 codec_id = CODEC_ID_MPEG4;
00464 type = AVMEDIA_TYPE_VIDEO;
00465 } else if(es_type == STREAM_TYPE_VIDEO_H264){
00466 codec_id = CODEC_ID_H264;
00467 type = AVMEDIA_TYPE_VIDEO;
00468 } else if(es_type == STREAM_TYPE_AUDIO_AC3){
00469 codec_id = CODEC_ID_AC3;
00470 type = AVMEDIA_TYPE_AUDIO;
00471 } else {
00472 goto skip;
00473 }
00474 } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
00475 static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
00476 unsigned char buf[8];
00477 avio_read(s->pb, buf, 8);
00478 avio_seek(s->pb, -8, SEEK_CUR);
00479 if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
00480 codec_id = CODEC_ID_CAVS;
00481 else
00482 codec_id = CODEC_ID_PROBE;
00483 type = AVMEDIA_TYPE_VIDEO;
00484 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
00485 type = AVMEDIA_TYPE_AUDIO;
00486 codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
00487 } else if (startcode >= 0x80 && startcode <= 0x87) {
00488 type = AVMEDIA_TYPE_AUDIO;
00489 codec_id = CODEC_ID_AC3;
00490 } else if ( ( startcode >= 0x88 && startcode <= 0x8f)
00491 ||( startcode >= 0x98 && startcode <= 0x9f)) {
00492
00493 type = AVMEDIA_TYPE_AUDIO;
00494 codec_id = CODEC_ID_DTS;
00495 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
00496 type = AVMEDIA_TYPE_AUDIO;
00497
00498 codec_id = CODEC_ID_PCM_DVD;
00499 } else if (startcode >= 0xb0 && startcode <= 0xbf) {
00500 type = AVMEDIA_TYPE_AUDIO;
00501 codec_id = CODEC_ID_TRUEHD;
00502 } else if (startcode >= 0xc0 && startcode <= 0xcf) {
00503
00504 type = AVMEDIA_TYPE_AUDIO;
00505 codec_id = CODEC_ID_AC3;
00506 } else if (startcode >= 0x20 && startcode <= 0x3f) {
00507 type = AVMEDIA_TYPE_SUBTITLE;
00508 codec_id = CODEC_ID_DVD_SUBTITLE;
00509 } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
00510 type = AVMEDIA_TYPE_VIDEO;
00511 codec_id = CODEC_ID_VC1;
00512 } else if (startcode == 0x1bd) {
00513
00514 type = AVMEDIA_TYPE_AUDIO;
00515 switch(dvdaudio_substream_type & 0xe0) {
00516 case 0xa0: codec_id = CODEC_ID_PCM_DVD;
00517 break;
00518 case 0x80: if((dvdaudio_substream_type & 0xf8) == 0x88)
00519 codec_id = CODEC_ID_DTS;
00520 else codec_id = CODEC_ID_AC3;
00521 break;
00522 default: av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
00523 goto skip;
00524 }
00525 } else {
00526 skip:
00527
00528 avio_seek(s->pb, len, SEEK_CUR);
00529 goto redo;
00530 }
00531
00532 st = av_new_stream(s, startcode);
00533 if (!st)
00534 goto skip;
00535 st->codec->codec_type = type;
00536 st->codec->codec_id = codec_id;
00537 if (codec_id != CODEC_ID_PCM_S16BE)
00538 st->need_parsing = AVSTREAM_PARSE_FULL;
00539 found:
00540 if(st->discard >= AVDISCARD_ALL)
00541 goto skip;
00542 if ((startcode >= 0xa0 && startcode <= 0xaf) ||
00543 (startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
00544 int b1, freq;
00545
00546
00547
00548 if (len <= 3)
00549 goto skip;
00550 avio_r8(s->pb);
00551 b1 = avio_r8(s->pb);
00552 avio_r8(s->pb);
00553 len -= 3;
00554 freq = (b1 >> 4) & 3;
00555 st->codec->sample_rate = lpcm_freq_tab[freq];
00556 st->codec->channels = 1 + (b1 & 7);
00557 st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
00558 st->codec->bit_rate = st->codec->channels *
00559 st->codec->sample_rate *
00560 st->codec->bits_per_coded_sample;
00561 if (st->codec->bits_per_coded_sample == 16)
00562 st->codec->codec_id = CODEC_ID_PCM_S16BE;
00563 else if (st->codec->bits_per_coded_sample == 28)
00564 return AVERROR(EINVAL);
00565 }
00566 av_new_packet(pkt, len);
00567 avio_read(s->pb, pkt->data, pkt->size);
00568 pkt->pts = pts;
00569 pkt->dts = dts;
00570 pkt->pos = dummy_pos;
00571 pkt->stream_index = st->index;
00572 #if 0
00573 av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
00574 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
00575 #endif
00576
00577 return 0;
00578 }
00579
00580 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
00581 int64_t *ppos, int64_t pos_limit)
00582 {
00583 int len, startcode;
00584 int64_t pos, pts, dts;
00585
00586 pos = *ppos;
00587 #ifdef DEBUG_SEEK
00588 printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
00589 #endif
00590 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
00591 return AV_NOPTS_VALUE;
00592
00593 for(;;) {
00594 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
00595 if (len < 0) {
00596 #ifdef DEBUG_SEEK
00597 printf("none (ret=%d)\n", len);
00598 #endif
00599 return AV_NOPTS_VALUE;
00600 }
00601 if (startcode == s->streams[stream_index]->id &&
00602 dts != AV_NOPTS_VALUE) {
00603 break;
00604 }
00605 avio_seek(s->pb, len, SEEK_CUR);
00606 }
00607 #ifdef DEBUG_SEEK
00608 printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
00609 #endif
00610 *ppos = pos;
00611 return dts;
00612 }
00613
00614 AVInputFormat ff_mpegps_demuxer = {
00615 "mpeg",
00616 NULL_IF_CONFIG_SMALL("MPEG-PS format"),
00617 sizeof(MpegDemuxContext),
00618 mpegps_probe,
00619 mpegps_read_header,
00620 mpegps_read_packet,
00621 NULL,
00622 NULL,
00623 mpegps_read_dts,
00624 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
00625 };