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