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