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