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