2001-07-22 16:18:56 +02:00
|
|
|
/*
|
2007-06-21 11:39:29 +02:00
|
|
|
* MPEG1/2 demuxer
|
2002-05-26 00:34:32 +02:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 00:34:32 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 16:18:56 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 00:34:32 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2007-06-21 11:39:29 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
#include "avformat.h"
|
2007-06-21 11:39:29 +02:00
|
|
|
#include "mpeg.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
//#define DEBUG_SEEK
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2004-01-13 23:02:49 +01:00
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/*********************************************/
|
|
|
|
/* demux code */
|
|
|
|
|
|
|
|
#define MAX_SYNC_SIZE 100000
|
|
|
|
|
2007-11-03 14:48:30 +01:00
|
|
|
static int check_pes(uint8_t *p, uint8_t *end){
|
|
|
|
int pes1;
|
|
|
|
int pes2= (p[3] & 0xC0) == 0x80
|
|
|
|
&& (p[4] & 0xC0) != 0x40
|
|
|
|
&&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
|
|
|
|
|
|
|
|
for(p+=3; p<end && *p == 0xFF; p++);
|
|
|
|
if((*p&0xC0) == 0x40) p+=2;
|
|
|
|
if((*p&0xF0) == 0x20){
|
|
|
|
pes1= p[0]&p[2]&p[4]&1;
|
|
|
|
p+=5;
|
|
|
|
}else if((*p&0xF0) == 0x30){
|
|
|
|
pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
|
|
|
|
p+=10;
|
|
|
|
}else
|
|
|
|
pes1 = *p == 0x0F;
|
|
|
|
|
|
|
|
return pes1||pes2;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:29:40 +02:00
|
|
|
static int mpegps_probe(AVProbeData *p)
|
|
|
|
{
|
2006-02-08 18:35:44 +01:00
|
|
|
uint32_t code= -1;
|
2007-12-03 10:37:06 +01:00
|
|
|
int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
|
2004-10-01 18:00:00 +02:00
|
|
|
int i;
|
2006-08-19 10:39:00 +02:00
|
|
|
int score=0;
|
2002-05-20 18:29:40 +02:00
|
|
|
|
2006-02-08 18:35:44 +01:00
|
|
|
for(i=0; i<p->buf_size; i++){
|
|
|
|
code = (code<<8) + p->buf[i];
|
2003-07-10 11:04:04 +02:00
|
|
|
if ((code & 0xffffff00) == 0x100) {
|
2007-11-03 15:57:26 +01:00
|
|
|
int pes= check_pes(p->buf+i, p->buf+p->buf_size);
|
2007-11-03 14:48:30 +01:00
|
|
|
|
2006-06-29 21:03:53 +02:00
|
|
|
if(code == SYSTEM_HEADER_START_CODE) sys++;
|
|
|
|
else if(code == PRIVATE_STREAM_1) priv1++;
|
|
|
|
else if(code == PACK_START_CODE) pspack++;
|
2007-11-03 14:48:30 +01:00
|
|
|
else if((code & 0xf0) == VIDEO_ID && pes) vid++;
|
|
|
|
else if((code & 0xe0) == AUDIO_ID && pes) audio++;
|
2007-12-03 10:37:06 +01:00
|
|
|
|
|
|
|
else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
|
|
|
|
else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
|
2003-07-10 11:04:04 +02:00
|
|
|
}
|
2002-05-20 18:29:40 +02:00
|
|
|
}
|
2006-08-19 10:39:00 +02:00
|
|
|
|
2007-12-03 10:37:06 +01:00
|
|
|
if(vid+audio > invalid) /* invalid VDR files nd short PES streams */
|
2006-08-19 10:39:00 +02:00
|
|
|
score= AVPROBE_SCORE_MAX/4;
|
|
|
|
|
2007-08-05 04:15:46 +02:00
|
|
|
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, p->buf_size);
|
2007-12-03 10:37:06 +01:00
|
|
|
if(sys>invalid && sys*9 <= pspack*10)
|
2006-02-08 18:35:44 +01:00
|
|
|
return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
|
2007-12-03 10:37:06 +01:00
|
|
|
if(priv1 + vid + audio > invalid && (priv1+vid+audio)*9 <= pspack*10)
|
2006-02-11 10:27:00 +01:00
|
|
|
return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
|
2007-08-05 04:17:06 +02:00
|
|
|
if((!!vid ^ !!audio) && (audio+vid > 1) && !sys && !pspack && p->buf_size>2048) /* PES stream */
|
2006-06-29 21:03:53 +02:00
|
|
|
return AVPROBE_SCORE_MAX/2+2;
|
2006-08-19 10:39:00 +02:00
|
|
|
|
|
|
|
//02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
|
|
|
|
return score;
|
2002-05-20 18:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
typedef struct MpegDemuxContext {
|
2006-09-27 21:47:39 +02:00
|
|
|
int32_t header_state;
|
2005-03-28 19:33:21 +02:00
|
|
|
unsigned char psm_es_type[256];
|
2007-11-08 00:56:00 +01:00
|
|
|
int sofdec;
|
2001-07-22 16:18:56 +02:00
|
|
|
} MpegDemuxContext;
|
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
static int mpegps_read_header(AVFormatContext *s,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
|
|
|
MpegDemuxContext *m = s->priv_data;
|
2007-11-08 22:27:37 +01:00
|
|
|
const char *sofdec = "Sofdec";
|
|
|
|
int v, i = 0;
|
2007-11-08 00:56:00 +01:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
m->header_state = 0xff;
|
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
|
2007-11-08 22:27:37 +01:00
|
|
|
m->sofdec = -1;
|
|
|
|
do {
|
2007-11-21 08:41:00 +01:00
|
|
|
v = get_byte(s->pb);
|
2007-11-08 22:27:37 +01:00
|
|
|
m->header_state = m->header_state << 8 | v;
|
|
|
|
m->sofdec++;
|
|
|
|
} while (v == sofdec[i] && i++ < 6);
|
2007-11-08 00:56:00 +01:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
/* no need to do more */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t get_pts(ByteIOContext *pb, int c)
|
|
|
|
{
|
2008-01-08 00:32:57 +01:00
|
|
|
uint8_t buf[5];
|
|
|
|
|
|
|
|
buf[0] = c<0 ? get_byte(pb) : c;
|
|
|
|
get_buffer(pb, buf+1, 4);
|
|
|
|
|
|
|
|
return ff_parse_pes_pts(buf);
|
2003-11-10 19:47:52 +01:00
|
|
|
}
|
|
|
|
|
2005-12-17 19:14:38 +01:00
|
|
|
static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
|
2006-09-27 21:47:39 +02:00
|
|
|
int32_t *header_state)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
unsigned int state, v;
|
|
|
|
int val, n;
|
|
|
|
|
|
|
|
state = *header_state;
|
|
|
|
n = *size_ptr;
|
|
|
|
while (n > 0) {
|
|
|
|
if (url_feof(pb))
|
|
|
|
break;
|
|
|
|
v = get_byte(pb);
|
|
|
|
n--;
|
|
|
|
if (state == 0x000001) {
|
|
|
|
state = ((state << 8) | v) & 0xffffff;
|
|
|
|
val = state;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
state = ((state << 8) | v) & 0xffffff;
|
|
|
|
}
|
|
|
|
val = -1;
|
|
|
|
found:
|
|
|
|
*header_state = state;
|
|
|
|
*size_ptr = n;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2005-02-24 20:08:50 +01:00
|
|
|
#if 0 /* unused, remove? */
|
2003-11-10 19:47:52 +01:00
|
|
|
/* XXX: optimize */
|
|
|
|
static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2003-11-10 19:47:52 +01:00
|
|
|
int64_t pos, pos_start;
|
|
|
|
int max_size, start_code;
|
2003-10-29 15:20:56 +01:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
max_size = *size_ptr;
|
|
|
|
pos_start = url_ftell(pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
/* in order to go faster, we fill the buffer */
|
|
|
|
pos = pos_start - 16386;
|
|
|
|
if (pos < 0)
|
|
|
|
pos = 0;
|
|
|
|
url_fseek(pb, pos, SEEK_SET);
|
|
|
|
get_byte(pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
pos = pos_start;
|
|
|
|
for(;;) {
|
|
|
|
pos--;
|
|
|
|
if (pos < 0 || (pos_start - pos) >= max_size) {
|
|
|
|
start_code = -1;
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
url_fseek(pb, pos, SEEK_SET);
|
|
|
|
start_code = get_be32(pb);
|
|
|
|
if ((start_code & 0xffffff00) == 0x100)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
the_end:
|
|
|
|
*size_ptr = pos_start - pos;
|
|
|
|
return start_code;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2005-02-24 20:08:50 +01:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2005-03-28 19:33:21 +02:00
|
|
|
/**
|
|
|
|
* Extracts stream types from a program stream map
|
|
|
|
* According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
|
2005-12-17 19:14:38 +01:00
|
|
|
*
|
2005-03-28 19:33:21 +02:00
|
|
|
* @return number of bytes occupied by PSM in the bitstream
|
|
|
|
*/
|
|
|
|
static long mpegps_psm_parse(MpegDemuxContext *m, ByteIOContext *pb)
|
|
|
|
{
|
|
|
|
int psm_length, ps_info_length, es_map_length;
|
|
|
|
|
|
|
|
psm_length = get_be16(pb);
|
|
|
|
get_byte(pb);
|
|
|
|
get_byte(pb);
|
|
|
|
ps_info_length = get_be16(pb);
|
|
|
|
|
|
|
|
/* skip program_stream_info */
|
|
|
|
url_fskip(pb, ps_info_length);
|
|
|
|
es_map_length = get_be16(pb);
|
|
|
|
|
|
|
|
/* at least one es available? */
|
|
|
|
while (es_map_length >= 4){
|
|
|
|
unsigned char type = get_byte(pb);
|
|
|
|
unsigned char es_id = get_byte(pb);
|
|
|
|
uint16_t es_info_length = get_be16(pb);
|
|
|
|
/* remember mapping from stream id to stream type */
|
|
|
|
m->psm_es_type[es_id] = type;
|
|
|
|
/* skip program_stream_info */
|
|
|
|
url_fskip(pb, es_info_length);
|
|
|
|
es_map_length -= 4 + es_info_length;
|
|
|
|
}
|
|
|
|
get_be32(pb); /* crc32 */
|
|
|
|
return 2 + psm_length;
|
|
|
|
}
|
|
|
|
|
2005-12-17 19:14:38 +01:00
|
|
|
/* read the next PES header. Return its position in ppos
|
2003-11-10 19:47:52 +01:00
|
|
|
(if not NULL), and its start code, pts and dts.
|
|
|
|
*/
|
|
|
|
static int mpegps_read_pes_header(AVFormatContext *s,
|
2005-12-17 19:14:38 +01:00
|
|
|
int64_t *ppos, int *pstart_code,
|
2004-04-12 18:50:03 +02:00
|
|
|
int64_t *ppts, int64_t *pdts)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
MpegDemuxContext *m = s->priv_data;
|
2003-11-10 19:47:52 +01:00
|
|
|
int len, size, startcode, c, flags, header_len;
|
2007-02-06 20:14:16 +01:00
|
|
|
int pes_ext, ext2_len, id_ext, skip;
|
2007-01-17 11:19:10 +01:00
|
|
|
int64_t pts, dts;
|
2007-11-21 08:41:00 +01:00
|
|
|
int64_t last_sync= url_ftell(s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2007-01-17 11:19:10 +01:00
|
|
|
error_redo:
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fseek(s->pb, last_sync, SEEK_SET);
|
2001-07-22 16:18:56 +02:00
|
|
|
redo:
|
2003-11-10 19:47:52 +01:00
|
|
|
/* next start code (should be immediately after) */
|
|
|
|
m->header_state = 0xff;
|
|
|
|
size = MAX_SYNC_SIZE;
|
2007-11-21 08:41:00 +01:00
|
|
|
startcode = find_next_start_code(s->pb, &size, &m->header_state);
|
|
|
|
last_sync = url_ftell(s->pb);
|
|
|
|
//printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(s->pb));
|
2001-07-22 16:18:56 +02:00
|
|
|
if (startcode < 0)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2001-07-22 16:18:56 +02:00
|
|
|
if (startcode == PACK_START_CODE)
|
|
|
|
goto redo;
|
|
|
|
if (startcode == SYSTEM_HEADER_START_CODE)
|
|
|
|
goto redo;
|
2007-11-08 22:27:37 +01:00
|
|
|
if (startcode == PADDING_STREAM) {
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fskip(s->pb, get_be16(s->pb));
|
2007-11-08 22:27:37 +01:00
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
if (startcode == PRIVATE_STREAM_2) {
|
2007-11-21 08:41:00 +01:00
|
|
|
len = get_be16(s->pb);
|
2007-11-08 22:27:37 +01:00
|
|
|
if (!m->sofdec) {
|
|
|
|
while (len-- >= 6) {
|
2007-11-21 08:41:00 +01:00
|
|
|
if (get_byte(s->pb) == 'S') {
|
2007-11-08 22:27:37 +01:00
|
|
|
uint8_t buf[5];
|
2007-11-21 08:41:00 +01:00
|
|
|
get_buffer(s->pb, buf, sizeof(buf));
|
2007-11-08 22:27:37 +01:00
|
|
|
m->sofdec = !memcmp(buf, "ofdec", 5);
|
|
|
|
len -= sizeof(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m->sofdec -= !m->sofdec;
|
|
|
|
}
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fskip(s->pb, len);
|
2001-07-22 16:18:56 +02:00
|
|
|
goto redo;
|
|
|
|
}
|
2005-03-28 19:33:21 +02:00
|
|
|
if (startcode == PROGRAM_STREAM_MAP) {
|
2007-11-21 08:41:00 +01:00
|
|
|
mpegps_psm_parse(m, s->pb);
|
2005-03-28 19:33:21 +02:00
|
|
|
goto redo;
|
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* find matching stream */
|
|
|
|
if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
|
|
|
|
(startcode >= 0x1e0 && startcode <= 0x1ef) ||
|
2007-02-06 20:14:16 +01:00
|
|
|
(startcode == 0x1bd) || (startcode == 0x1fd)))
|
2001-07-22 16:18:56 +02:00
|
|
|
goto redo;
|
2003-11-10 19:47:52 +01:00
|
|
|
if (ppos) {
|
2007-11-21 08:41:00 +01:00
|
|
|
*ppos = url_ftell(s->pb) - 4;
|
2003-11-10 19:47:52 +01:00
|
|
|
}
|
2007-11-21 08:41:00 +01:00
|
|
|
len = get_be16(s->pb);
|
2007-01-17 11:45:59 +01:00
|
|
|
pts =
|
2002-10-21 17:57:21 +02:00
|
|
|
dts = AV_NOPTS_VALUE;
|
2001-07-22 16:18:56 +02:00
|
|
|
/* stuffing */
|
|
|
|
for(;;) {
|
2003-11-10 19:47:52 +01:00
|
|
|
if (len < 1)
|
2007-01-17 11:19:10 +01:00
|
|
|
goto error_redo;
|
2007-11-21 08:41:00 +01:00
|
|
|
c = get_byte(s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
len--;
|
|
|
|
/* XXX: for mpeg1, should test only bit 7 */
|
2005-12-17 19:14:38 +01:00
|
|
|
if (c != 0xff)
|
2001-07-22 16:18:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((c & 0xc0) == 0x40) {
|
|
|
|
/* buffer scale & size */
|
2007-11-21 08:41:00 +01:00
|
|
|
get_byte(s->pb);
|
|
|
|
c = get_byte(s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
len -= 2;
|
|
|
|
}
|
2007-01-17 11:55:01 +01:00
|
|
|
if ((c & 0xe0) == 0x20) {
|
2007-11-21 08:41:00 +01:00
|
|
|
dts = pts = get_pts(s->pb, c);
|
2001-07-22 16:18:56 +02:00
|
|
|
len -= 4;
|
2007-01-17 11:55:01 +01:00
|
|
|
if (c & 0x10){
|
2007-11-21 08:41:00 +01:00
|
|
|
dts = get_pts(s->pb, -1);
|
2007-01-17 11:55:01 +01:00
|
|
|
len -= 5;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
} else if ((c & 0xc0) == 0x80) {
|
|
|
|
/* mpeg 2 PES */
|
2006-06-20 00:20:38 +02:00
|
|
|
#if 0 /* some streams have this field set for no apparent reason */
|
2001-07-22 16:18:56 +02:00
|
|
|
if ((c & 0x30) != 0) {
|
2003-11-10 19:47:52 +01:00
|
|
|
/* Encrypted multiplex not handled */
|
|
|
|
goto redo;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2006-06-20 00:20:38 +02:00
|
|
|
#endif
|
2007-11-21 08:41:00 +01:00
|
|
|
flags = get_byte(s->pb);
|
|
|
|
header_len = get_byte(s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
len -= 2;
|
|
|
|
if (header_len > len)
|
2007-01-17 11:19:10 +01:00
|
|
|
goto error_redo;
|
2007-01-17 13:06:31 +01:00
|
|
|
len -= header_len;
|
2007-01-17 11:55:01 +01:00
|
|
|
if (flags & 0x80) {
|
2007-11-21 08:41:00 +01:00
|
|
|
dts = pts = get_pts(s->pb, -1);
|
2001-07-22 16:18:56 +02:00
|
|
|
header_len -= 5;
|
2007-01-17 11:55:01 +01:00
|
|
|
if (flags & 0x40) {
|
2007-11-21 08:41:00 +01:00
|
|
|
dts = get_pts(s->pb, -1);
|
2007-01-17 11:55:01 +01:00
|
|
|
header_len -= 5;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2008-03-04 02:31:15 +01:00
|
|
|
if (flags & 0x3f && header_len == 0){
|
|
|
|
flags &= 0xC0;
|
|
|
|
av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
|
|
|
|
}
|
2007-02-06 20:14:16 +01:00
|
|
|
if (flags & 0x01) { /* PES extension */
|
2007-11-21 08:41:00 +01:00
|
|
|
pes_ext = get_byte(s->pb);
|
2007-02-06 20:14:16 +01:00
|
|
|
header_len--;
|
|
|
|
/* Skip PES private data, program packet sequence counter and P-STD buffer */
|
|
|
|
skip = (pes_ext >> 4) & 0xb;
|
|
|
|
skip += skip & 0x9;
|
2008-04-29 02:12:49 +02:00
|
|
|
if (pes_ext & 0x40 || skip > header_len){
|
|
|
|
av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
|
|
|
|
pes_ext=skip=0;
|
|
|
|
}
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fskip(s->pb, skip);
|
2007-02-06 20:14:16 +01:00
|
|
|
header_len -= skip;
|
|
|
|
|
|
|
|
if (pes_ext & 0x01) { /* PES extension 2 */
|
2007-11-21 08:41:00 +01:00
|
|
|
ext2_len = get_byte(s->pb);
|
2007-02-06 20:14:16 +01:00
|
|
|
header_len--;
|
|
|
|
if ((ext2_len & 0x7f) > 0) {
|
2007-11-21 08:41:00 +01:00
|
|
|
id_ext = get_byte(s->pb);
|
2007-02-06 20:14:16 +01:00
|
|
|
if ((id_ext & 0x80) == 0)
|
|
|
|
startcode = ((startcode & 0xff) << 8) | id_ext;
|
|
|
|
header_len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-17 13:06:31 +01:00
|
|
|
if(header_len < 0)
|
|
|
|
goto error_redo;
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fskip(s->pb, header_len);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
2004-04-23 23:02:01 +02:00
|
|
|
else if( c!= 0xf )
|
|
|
|
goto redo;
|
|
|
|
|
2005-03-28 19:33:21 +02:00
|
|
|
if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
|
2007-11-21 08:41:00 +01:00
|
|
|
startcode = get_byte(s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
len--;
|
2007-02-06 20:14:16 +01:00
|
|
|
if (startcode >= 0x80 && startcode <= 0xcf) {
|
2001-07-22 16:18:56 +02:00
|
|
|
/* audio: skip header */
|
2007-11-21 08:41:00 +01:00
|
|
|
get_byte(s->pb);
|
|
|
|
get_byte(s->pb);
|
|
|
|
get_byte(s->pb);
|
2001-07-22 16:18:56 +02:00
|
|
|
len -= 3;
|
2007-02-06 20:14:16 +01:00
|
|
|
if (startcode >= 0xb0 && startcode <= 0xbf) {
|
|
|
|
/* MLP/TrueHD audio has a 4-byte header */
|
2007-11-21 08:41:00 +01:00
|
|
|
get_byte(s->pb);
|
2007-02-06 20:14:16 +01:00
|
|
|
len--;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2007-01-17 11:44:57 +01:00
|
|
|
if(len<0)
|
|
|
|
goto error_redo;
|
2004-01-13 23:02:49 +01:00
|
|
|
if(dts != AV_NOPTS_VALUE && ppos){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<s->nb_streams; i++){
|
2008-01-14 17:11:08 +01:00
|
|
|
if(startcode == s->streams[i]->id &&
|
|
|
|
!url_is_streamed(s->pb) /* index useless on streams anyway */) {
|
2008-01-13 14:33:37 +01:00
|
|
|
ff_reduce_index(s, i);
|
2006-03-01 12:29:55 +01:00
|
|
|
av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
|
2004-01-13 23:02:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2003-11-10 19:47:52 +01:00
|
|
|
*pstart_code = startcode;
|
|
|
|
*ppts = pts;
|
|
|
|
*pdts = dts;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpegps_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2005-03-28 19:33:21 +02:00
|
|
|
MpegDemuxContext *m = s->priv_data;
|
2003-11-10 19:47:52 +01:00
|
|
|
AVStream *st;
|
2005-03-28 19:33:21 +02:00
|
|
|
int len, startcode, i, type, codec_id = 0, es_type;
|
2004-01-13 23:02:49 +01:00
|
|
|
int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
|
2003-11-10 19:47:52 +01:00
|
|
|
|
|
|
|
redo:
|
2004-04-12 18:50:03 +02:00
|
|
|
len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
|
2003-11-10 19:47:52 +01:00
|
|
|
if (len < 0)
|
|
|
|
return len;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
/* now find stream */
|
|
|
|
for(i=0;i<s->nb_streams;i++) {
|
|
|
|
st = s->streams[i];
|
|
|
|
if (st->id == startcode)
|
|
|
|
goto found;
|
|
|
|
}
|
2005-03-28 19:33:21 +02:00
|
|
|
|
|
|
|
es_type = m->psm_es_type[startcode & 0xff];
|
|
|
|
if(es_type > 0){
|
|
|
|
if(es_type == STREAM_TYPE_VIDEO_MPEG1){
|
|
|
|
codec_id = CODEC_ID_MPEG2VIDEO;
|
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
} else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
|
|
|
|
codec_id = CODEC_ID_MPEG2VIDEO;
|
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
} else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
|
|
|
|
es_type == STREAM_TYPE_AUDIO_MPEG2){
|
|
|
|
codec_id = CODEC_ID_MP3;
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
} else if(es_type == STREAM_TYPE_AUDIO_AAC){
|
|
|
|
codec_id = CODEC_ID_AAC;
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
} else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
|
|
|
|
codec_id = CODEC_ID_MPEG4;
|
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
} else if(es_type == STREAM_TYPE_VIDEO_H264){
|
|
|
|
codec_id = CODEC_ID_H264;
|
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
} else if(es_type == STREAM_TYPE_AUDIO_AC3){
|
|
|
|
codec_id = CODEC_ID_AC3;
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
} else {
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
} else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
|
2006-07-03 23:40:01 +02:00
|
|
|
static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
|
|
|
|
unsigned char buf[8];
|
2007-11-21 08:41:00 +01:00
|
|
|
get_buffer(s->pb, buf, 8);
|
|
|
|
url_fseek(s->pb, -8, SEEK_CUR);
|
2006-07-03 23:40:01 +02:00
|
|
|
if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
|
|
|
|
codec_id = CODEC_ID_CAVS;
|
|
|
|
else
|
|
|
|
codec_id = CODEC_ID_MPEG2VIDEO;
|
2002-05-20 18:29:40 +02:00
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
} else if (startcode >= 0x1c0 && startcode <= 0x1df) {
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
2007-11-08 22:27:37 +01:00
|
|
|
codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
|
2005-05-20 15:10:09 +02:00
|
|
|
} else if (startcode >= 0x80 && startcode <= 0x87) {
|
2002-05-20 18:29:40 +02:00
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
codec_id = CODEC_ID_AC3;
|
2007-02-06 20:14:16 +01:00
|
|
|
} else if ((startcode >= 0x88 && startcode <= 0x8f)
|
|
|
|
||( startcode >= 0x98 && startcode <= 0x9f)) {
|
|
|
|
/* 0x90 - 0x97 is reserved for SDDS in DVD specs */
|
2004-07-14 03:32:14 +02:00
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
codec_id = CODEC_ID_DTS;
|
2007-02-06 20:14:16 +01:00
|
|
|
} else if (startcode >= 0xa0 && startcode <= 0xaf) {
|
2003-01-31 18:04:46 +01:00
|
|
|
type = CODEC_TYPE_AUDIO;
|
2008-05-21 04:26:42 +02:00
|
|
|
/* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
|
|
|
|
codec_id = CODEC_ID_PCM_DVD;
|
2007-02-06 20:14:16 +01:00
|
|
|
} else if (startcode >= 0xb0 && startcode <= 0xbf) {
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
codec_id = CODEC_ID_MLP;
|
|
|
|
} else if (startcode >= 0xc0 && startcode <= 0xcf) {
|
|
|
|
/* Used for both AC-3 and E-AC-3 in EVOB files */
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
codec_id = CODEC_ID_AC3;
|
2005-06-03 16:01:49 +02:00
|
|
|
} else if (startcode >= 0x20 && startcode <= 0x3f) {
|
|
|
|
type = CODEC_TYPE_SUBTITLE;
|
|
|
|
codec_id = CODEC_ID_DVD_SUBTITLE;
|
2007-02-06 20:14:16 +01:00
|
|
|
} else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
|
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
codec_id = CODEC_ID_VC1;
|
2002-05-20 18:29:40 +02:00
|
|
|
} else {
|
|
|
|
skip:
|
|
|
|
/* skip packet */
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fskip(s->pb, len);
|
2002-05-20 18:29:40 +02:00
|
|
|
goto redo;
|
|
|
|
}
|
2002-10-04 17:46:59 +02:00
|
|
|
/* no stream found: add a new stream */
|
|
|
|
st = av_new_stream(s, startcode);
|
2005-12-17 19:14:38 +01:00
|
|
|
if (!st)
|
2002-10-04 17:46:59 +02:00
|
|
|
goto skip;
|
2005-07-18 00:24:36 +02:00
|
|
|
st->codec->codec_type = type;
|
|
|
|
st->codec->codec_id = codec_id;
|
2003-11-10 19:47:52 +01:00
|
|
|
if (codec_id != CODEC_ID_PCM_S16BE)
|
2007-04-15 15:51:57 +02:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_FULL;
|
2001-07-22 16:18:56 +02:00
|
|
|
found:
|
2005-03-17 02:25:01 +01:00
|
|
|
if(st->discard >= AVDISCARD_ALL)
|
2005-01-22 14:36:02 +01:00
|
|
|
goto skip;
|
2007-02-06 20:14:16 +01:00
|
|
|
if (startcode >= 0xa0 && startcode <= 0xaf) {
|
2003-01-31 18:04:46 +01:00
|
|
|
int b1, freq;
|
|
|
|
|
|
|
|
/* for LPCM, we just skip the header and consider it is raw
|
|
|
|
audio data */
|
|
|
|
if (len <= 3)
|
|
|
|
goto skip;
|
2007-11-21 08:41:00 +01:00
|
|
|
get_byte(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
|
|
|
|
b1 = get_byte(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
|
|
|
|
get_byte(s->pb); /* dynamic range control (0x80 = off) */
|
2003-01-31 18:04:46 +01:00
|
|
|
len -= 3;
|
|
|
|
freq = (b1 >> 4) & 3;
|
2005-07-18 00:24:36 +02:00
|
|
|
st->codec->sample_rate = lpcm_freq_tab[freq];
|
|
|
|
st->codec->channels = 1 + (b1 & 7);
|
2008-05-21 04:26:42 +02:00
|
|
|
st->codec->bits_per_sample = 16 + ((b1 >> 6) & 3) * 4;
|
|
|
|
st->codec->bit_rate = st->codec->channels *
|
|
|
|
st->codec->sample_rate *
|
|
|
|
st->codec->bits_per_sample;
|
|
|
|
if (st->codec->bits_per_sample == 16)
|
|
|
|
st->codec->codec_id = CODEC_ID_PCM_S16BE;
|
|
|
|
else if (st->codec->bits_per_sample == 28)
|
|
|
|
return AVERROR(EINVAL);
|
2003-01-31 18:04:46 +01:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
av_new_packet(pkt, len);
|
2007-11-21 08:41:00 +01:00
|
|
|
get_buffer(s->pb, pkt->data, pkt->size);
|
2001-07-22 16:18:56 +02:00
|
|
|
pkt->pts = pts;
|
2003-11-10 19:47:52 +01:00
|
|
|
pkt->dts = dts;
|
2002-05-20 18:29:40 +02:00
|
|
|
pkt->stream_index = st->index;
|
2003-12-09 19:06:18 +01:00
|
|
|
#if 0
|
2005-01-22 14:36:02 +01:00
|
|
|
av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
|
|
|
|
pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
|
2003-12-09 19:06:18 +01:00
|
|
|
#endif
|
2004-06-19 05:59:34 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-20 18:29:40 +02:00
|
|
|
static int mpegps_read_close(AVFormatContext *s)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-17 19:14:38 +01:00
|
|
|
static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
|
2004-04-12 18:50:03 +02:00
|
|
|
int64_t *ppos, int64_t pos_limit)
|
2003-11-10 19:47:52 +01:00
|
|
|
{
|
|
|
|
int len, startcode;
|
|
|
|
int64_t pos, pts, dts;
|
|
|
|
|
|
|
|
pos = *ppos;
|
|
|
|
#ifdef DEBUG_SEEK
|
2006-11-01 23:39:58 +01:00
|
|
|
printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
|
2003-11-10 19:47:52 +01:00
|
|
|
#endif
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fseek(s->pb, pos, SEEK_SET);
|
2003-11-10 19:47:52 +01:00
|
|
|
for(;;) {
|
2004-04-12 18:50:03 +02:00
|
|
|
len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
|
2003-11-10 19:47:52 +01:00
|
|
|
if (len < 0) {
|
|
|
|
#ifdef DEBUG_SEEK
|
|
|
|
printf("none (ret=%d)\n", len);
|
|
|
|
#endif
|
|
|
|
return AV_NOPTS_VALUE;
|
|
|
|
}
|
2005-12-17 19:14:38 +01:00
|
|
|
if (startcode == s->streams[stream_index]->id &&
|
2003-11-10 19:47:52 +01:00
|
|
|
dts != AV_NOPTS_VALUE) {
|
|
|
|
break;
|
|
|
|
}
|
2007-11-21 08:41:00 +01:00
|
|
|
url_fskip(s->pb, len);
|
2003-11-10 19:47:52 +01:00
|
|
|
}
|
|
|
|
#ifdef DEBUG_SEEK
|
2006-11-01 23:39:58 +01:00
|
|
|
printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
|
2003-11-10 19:47:52 +01:00
|
|
|
#endif
|
|
|
|
*ppos = pos;
|
2004-05-23 18:26:12 +02:00
|
|
|
return dts;
|
2003-11-10 19:47:52 +01:00
|
|
|
}
|
|
|
|
|
2006-07-10 01:40:53 +02:00
|
|
|
AVInputFormat mpegps_demuxer = {
|
2002-05-20 18:29:40 +02:00
|
|
|
"mpeg",
|
|
|
|
"MPEG PS format",
|
|
|
|
sizeof(MpegDemuxContext),
|
|
|
|
mpegps_probe,
|
|
|
|
mpegps_read_header,
|
|
|
|
mpegps_read_packet,
|
|
|
|
mpegps_read_close,
|
2004-04-12 18:50:03 +02:00
|
|
|
NULL, //mpegps_read_seek,
|
|
|
|
mpegps_read_dts,
|
2005-06-03 16:01:49 +02:00
|
|
|
.flags = AVFMT_SHOW_IDS,
|
2001-07-22 16:18:56 +02:00
|
|
|
};
|