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