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 (audio > 12 || vid > 3 || 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 + 2*invalid) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
00095
00096
00097
00098
00099 return score;
00100 }
00101
00102
00103 typedef struct MpegDemuxContext {
00104 int32_t header_state;
00105 unsigned char psm_es_type[256];
00106 int sofdec;
00107 } MpegDemuxContext;
00108
00109 static int mpegps_read_header(AVFormatContext *s)
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
00177 static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
00178 {
00179 int psm_length, ps_info_length, es_map_length;
00180
00181 psm_length = avio_rb16(pb);
00182 avio_r8(pb);
00183 avio_r8(pb);
00184 ps_info_length = avio_rb16(pb);
00185
00186
00187 avio_skip(pb, ps_info_length);
00188 es_map_length = avio_rb16(pb);
00189
00190
00191 while (es_map_length >= 4){
00192 unsigned char type = avio_r8(pb);
00193 unsigned char es_id = avio_r8(pb);
00194 uint16_t es_info_length = avio_rb16(pb);
00195
00196 m->psm_es_type[es_id] = type;
00197
00198 avio_skip(pb, es_info_length);
00199 es_map_length -= 4 + es_info_length;
00200 }
00201 avio_rb32(pb);
00202 return 2 + psm_length;
00203 }
00204
00205
00206
00207
00208 static int mpegps_read_pes_header(AVFormatContext *s,
00209 int64_t *ppos, int *pstart_code,
00210 int64_t *ppts, int64_t *pdts)
00211 {
00212 MpegDemuxContext *m = s->priv_data;
00213 int len, size, startcode, c, flags, header_len;
00214 int pes_ext, ext2_len, id_ext, skip;
00215 int64_t pts, dts;
00216 int64_t last_sync= avio_tell(s->pb);
00217
00218 error_redo:
00219 avio_seek(s->pb, last_sync, SEEK_SET);
00220 redo:
00221
00222 m->header_state = 0xff;
00223 size = MAX_SYNC_SIZE;
00224 startcode = find_next_start_code(s->pb, &size, &m->header_state);
00225 last_sync = avio_tell(s->pb);
00226
00227 if (startcode < 0){
00228 if(url_feof(s->pb))
00229 return AVERROR_EOF;
00230
00231 return AVERROR(EAGAIN);
00232 }
00233
00234 if (startcode == PACK_START_CODE)
00235 goto redo;
00236 if (startcode == SYSTEM_HEADER_START_CODE)
00237 goto redo;
00238 if (startcode == PADDING_STREAM) {
00239 avio_skip(s->pb, avio_rb16(s->pb));
00240 goto redo;
00241 }
00242 if (startcode == PRIVATE_STREAM_2) {
00243 len = avio_rb16(s->pb);
00244 if (!m->sofdec) {
00245 while (len-- >= 6) {
00246 if (avio_r8(s->pb) == 'S') {
00247 uint8_t buf[5];
00248 avio_read(s->pb, buf, sizeof(buf));
00249 m->sofdec = !memcmp(buf, "ofdec", 5);
00250 len -= sizeof(buf);
00251 break;
00252 }
00253 }
00254 m->sofdec -= !m->sofdec;
00255 }
00256 avio_skip(s->pb, len);
00257 goto redo;
00258 }
00259 if (startcode == PROGRAM_STREAM_MAP) {
00260 mpegps_psm_parse(m, s->pb);
00261 goto redo;
00262 }
00263
00264
00265 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
00266 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
00267 (startcode == 0x1bd) || (startcode == 0x1fd)))
00268 goto redo;
00269 if (ppos) {
00270 *ppos = avio_tell(s->pb) - 4;
00271 }
00272 len = avio_rb16(s->pb);
00273 pts =
00274 dts = AV_NOPTS_VALUE;
00275
00276 for(;;) {
00277 if (len < 1)
00278 goto error_redo;
00279 c = avio_r8(s->pb);
00280 len--;
00281
00282 if (c != 0xff)
00283 break;
00284 }
00285 if ((c & 0xc0) == 0x40) {
00286
00287 avio_r8(s->pb);
00288 c = avio_r8(s->pb);
00289 len -= 2;
00290 }
00291 if ((c & 0xe0) == 0x20) {
00292 dts = pts = get_pts(s->pb, c);
00293 len -= 4;
00294 if (c & 0x10){
00295 dts = get_pts(s->pb, -1);
00296 len -= 5;
00297 }
00298 } else if ((c & 0xc0) == 0x80) {
00299
00300 flags = avio_r8(s->pb);
00301 header_len = avio_r8(s->pb);
00302 len -= 2;
00303 if (header_len > len)
00304 goto error_redo;
00305 len -= header_len;
00306 if (flags & 0x80) {
00307 dts = pts = get_pts(s->pb, -1);
00308 header_len -= 5;
00309 if (flags & 0x40) {
00310 dts = get_pts(s->pb, -1);
00311 header_len -= 5;
00312 }
00313 }
00314 if (flags & 0x3f && header_len == 0){
00315 flags &= 0xC0;
00316 av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
00317 }
00318 if (flags & 0x01) {
00319 pes_ext = avio_r8(s->pb);
00320 header_len--;
00321
00322 skip = (pes_ext >> 4) & 0xb;
00323 skip += skip & 0x9;
00324 if (pes_ext & 0x40 || skip > header_len){
00325 av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
00326 pes_ext=skip=0;
00327 }
00328 avio_skip(s->pb, skip);
00329 header_len -= skip;
00330
00331 if (pes_ext & 0x01) {
00332 ext2_len = avio_r8(s->pb);
00333 header_len--;
00334 if ((ext2_len & 0x7f) > 0) {
00335 id_ext = avio_r8(s->pb);
00336 if ((id_ext & 0x80) == 0)
00337 startcode = ((startcode & 0xff) << 8) | id_ext;
00338 header_len--;
00339 }
00340 }
00341 }
00342 if(header_len < 0)
00343 goto error_redo;
00344 avio_skip(s->pb, header_len);
00345 }
00346 else if( c!= 0xf )
00347 goto redo;
00348
00349 if (startcode == PRIVATE_STREAM_1) {
00350 startcode = avio_r8(s->pb);
00351 len--;
00352 }
00353 if(len<0)
00354 goto error_redo;
00355 if(dts != AV_NOPTS_VALUE && ppos){
00356 int i;
00357 for(i=0; i<s->nb_streams; i++){
00358 if(startcode == s->streams[i]->id &&
00359 s->pb->seekable ) {
00360 ff_reduce_index(s, i);
00361 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME );
00362 }
00363 }
00364 }
00365
00366 *pstart_code = startcode;
00367 *ppts = pts;
00368 *pdts = dts;
00369 return len;
00370 }
00371
00372 static int mpegps_read_packet(AVFormatContext *s,
00373 AVPacket *pkt)
00374 {
00375 MpegDemuxContext *m = s->priv_data;
00376 AVStream *st;
00377 int len, startcode, i, es_type, ret;
00378 int lpcm_header_len;
00379 int request_probe= 0;
00380 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
00381 enum AVMediaType type;
00382 int64_t pts, dts, dummy_pos;
00383
00384 redo:
00385 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
00386 if (len < 0)
00387 return len;
00388
00389 if (startcode >= 0x80 && startcode <= 0xcf) {
00390 if(len < 4)
00391 goto skip;
00392
00393
00394 avio_r8(s->pb);
00395 lpcm_header_len = avio_rb16(s->pb);
00396 len -= 3;
00397 if (startcode >= 0xb0 && startcode <= 0xbf) {
00398
00399 avio_r8(s->pb);
00400 len--;
00401 }
00402 }
00403
00404
00405 for(i=0;i<s->nb_streams;i++) {
00406 st = s->streams[i];
00407 if (st->id == startcode)
00408 goto found;
00409 }
00410
00411 es_type = m->psm_es_type[startcode & 0xff];
00412 if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
00413 if(es_type == STREAM_TYPE_VIDEO_MPEG1){
00414 codec_id = AV_CODEC_ID_MPEG2VIDEO;
00415 type = AVMEDIA_TYPE_VIDEO;
00416 } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
00417 codec_id = AV_CODEC_ID_MPEG2VIDEO;
00418 type = AVMEDIA_TYPE_VIDEO;
00419 } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
00420 es_type == STREAM_TYPE_AUDIO_MPEG2){
00421 codec_id = AV_CODEC_ID_MP3;
00422 type = AVMEDIA_TYPE_AUDIO;
00423 } else if(es_type == STREAM_TYPE_AUDIO_AAC){
00424 codec_id = AV_CODEC_ID_AAC;
00425 type = AVMEDIA_TYPE_AUDIO;
00426 } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
00427 codec_id = AV_CODEC_ID_MPEG4;
00428 type = AVMEDIA_TYPE_VIDEO;
00429 } else if(es_type == STREAM_TYPE_VIDEO_H264){
00430 codec_id = AV_CODEC_ID_H264;
00431 type = AVMEDIA_TYPE_VIDEO;
00432 } else if(es_type == STREAM_TYPE_AUDIO_AC3){
00433 codec_id = AV_CODEC_ID_AC3;
00434 type = AVMEDIA_TYPE_AUDIO;
00435 } else {
00436 goto skip;
00437 }
00438 } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
00439 static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
00440 unsigned char buf[8];
00441 avio_read(s->pb, buf, 8);
00442 avio_seek(s->pb, -8, SEEK_CUR);
00443 if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
00444 codec_id = AV_CODEC_ID_CAVS;
00445 else
00446 request_probe= 1;
00447 type = AVMEDIA_TYPE_VIDEO;
00448 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
00449 type = AVMEDIA_TYPE_AUDIO;
00450 codec_id = m->sofdec > 0 ? AV_CODEC_ID_ADPCM_ADX : AV_CODEC_ID_MP2;
00451 } else if (startcode >= 0x80 && startcode <= 0x87) {
00452 type = AVMEDIA_TYPE_AUDIO;
00453 codec_id = AV_CODEC_ID_AC3;
00454 } else if ( ( startcode >= 0x88 && startcode <= 0x8f)
00455 ||( startcode >= 0x98 && startcode <= 0x9f)) {
00456
00457 type = AVMEDIA_TYPE_AUDIO;
00458 codec_id = AV_CODEC_ID_DTS;
00459 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
00460 type = AVMEDIA_TYPE_AUDIO;
00461 if(lpcm_header_len == 6) {
00462 codec_id = AV_CODEC_ID_MLP;
00463 } else {
00464
00465 codec_id = AV_CODEC_ID_PCM_DVD;
00466 }
00467 } else if (startcode >= 0xb0 && startcode <= 0xbf) {
00468 type = AVMEDIA_TYPE_AUDIO;
00469 codec_id = AV_CODEC_ID_TRUEHD;
00470 } else if (startcode >= 0xc0 && startcode <= 0xcf) {
00471
00472 type = AVMEDIA_TYPE_AUDIO;
00473 codec_id = AV_CODEC_ID_AC3;
00474 } else if (startcode >= 0x20 && startcode <= 0x3f) {
00475 type = AVMEDIA_TYPE_SUBTITLE;
00476 codec_id = AV_CODEC_ID_DVD_SUBTITLE;
00477 } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
00478 type = AVMEDIA_TYPE_VIDEO;
00479 codec_id = AV_CODEC_ID_VC1;
00480 } else {
00481 skip:
00482
00483 avio_skip(s->pb, len);
00484 goto redo;
00485 }
00486
00487 st = avformat_new_stream(s, NULL);
00488 if (!st)
00489 goto skip;
00490 st->id = startcode;
00491 st->codec->codec_type = type;
00492 st->codec->codec_id = codec_id;
00493 st->request_probe = request_probe;
00494 if (codec_id != AV_CODEC_ID_PCM_S16BE)
00495 st->need_parsing = AVSTREAM_PARSE_FULL;
00496 found:
00497 if(st->discard >= AVDISCARD_ALL)
00498 goto skip;
00499 if (startcode >= 0xa0 && startcode <= 0xaf) {
00500 if (lpcm_header_len == 6) {
00501 if (len < 6)
00502 goto skip;
00503 avio_skip(s->pb, 6);
00504 len -=6;
00505 } else {
00506 int b1, freq;
00507
00508
00509
00510 if (len <= 3)
00511 goto skip;
00512 avio_r8(s->pb);
00513 b1 = avio_r8(s->pb);
00514 avio_r8(s->pb);
00515 len -= 3;
00516 freq = (b1 >> 4) & 3;
00517 st->codec->sample_rate = lpcm_freq_tab[freq];
00518 st->codec->channels = 1 + (b1 & 7);
00519 st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
00520 st->codec->bit_rate = st->codec->channels *
00521 st->codec->sample_rate *
00522 st->codec->bits_per_coded_sample;
00523 if (st->codec->bits_per_coded_sample == 16)
00524 st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
00525 else if (st->codec->bits_per_coded_sample == 28)
00526 return AVERROR(EINVAL);
00527 }
00528 }
00529 ret = av_get_packet(s->pb, pkt, len);
00530 pkt->pts = pts;
00531 pkt->dts = dts;
00532 pkt->pos = dummy_pos;
00533 pkt->stream_index = st->index;
00534 av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
00535 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
00536 pkt->size);
00537
00538 return (ret < 0) ? ret : 0;
00539 }
00540
00541 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
00542 int64_t *ppos, int64_t pos_limit)
00543 {
00544 int len, startcode;
00545 int64_t pos, pts, dts;
00546
00547 pos = *ppos;
00548 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
00549 return AV_NOPTS_VALUE;
00550
00551 for(;;) {
00552 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
00553 if (len < 0) {
00554 av_dlog(s, "none (ret=%d)\n", len);
00555 return AV_NOPTS_VALUE;
00556 }
00557 if (startcode == s->streams[stream_index]->id &&
00558 dts != AV_NOPTS_VALUE) {
00559 break;
00560 }
00561 avio_skip(s->pb, len);
00562 }
00563 av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
00564 pos, dts, dts / 90000.0);
00565 *ppos = pos;
00566 return dts;
00567 }
00568
00569 AVInputFormat ff_mpegps_demuxer = {
00570 .name = "mpeg",
00571 .long_name = NULL_IF_CONFIG_SMALL("MPEG-PS (MPEG-2 Program Stream)"),
00572 .priv_data_size = sizeof(MpegDemuxContext),
00573 .read_probe = mpegps_probe,
00574 .read_header = mpegps_read_header,
00575 .read_packet = mpegps_read_packet,
00576 .read_timestamp = mpegps_read_dts,
00577 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
00578 };