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