2001-07-22 17:18:56 +03:00
|
|
|
/*
|
2007-06-21 12:39:29 +03:00
|
|
|
* MPEG1/2 demuxer
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2006-10-07 18:30:46 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2002-05-26 01:34:32 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2001-07-22 17:18:56 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 01:34:32 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 17:18:56 +03:00
|
|
|
*
|
2002-05-26 01:34:32 +03:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 17:18:56 +03:00
|
|
|
*/
|
2007-06-21 12:39:29 +03:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
#include "avformat.h"
|
2011-02-06 16:38:55 +02:00
|
|
|
#include "internal.h"
|
2007-06-21 12:39:29 +03:00
|
|
|
#include "mpeg.h"
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2004-01-14 00:02:49 +02:00
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/*********************************************/
|
|
|
|
/* demux code */
|
|
|
|
|
|
|
|
#define MAX_SYNC_SIZE 100000
|
|
|
|
|
2007-11-03 15:48:30 +02: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;
|
|
|
|
}else if((*p&0xF0) == 0x30){
|
|
|
|
pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
|
|
|
|
}else
|
|
|
|
pes1 = *p == 0x0F;
|
|
|
|
|
|
|
|
return pes1||pes2;
|
|
|
|
}
|
|
|
|
|
2011-09-29 01:43:24 +03:00
|
|
|
static int check_pack_header(const uint8_t *buf) {
|
|
|
|
return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
|
|
|
|
}
|
|
|
|
|
2002-05-20 19:29:40 +03:00
|
|
|
static int mpegps_probe(AVProbeData *p)
|
|
|
|
{
|
2006-02-08 19:35:44 +02:00
|
|
|
uint32_t code= -1;
|
2007-12-03 11:37:06 +02:00
|
|
|
int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
|
2004-10-01 19:00:00 +03:00
|
|
|
int i;
|
2006-08-19 11:39:00 +03:00
|
|
|
int score=0;
|
2002-05-20 19:29:40 +03:00
|
|
|
|
2006-02-08 19:35:44 +02:00
|
|
|
for(i=0; i<p->buf_size; i++){
|
|
|
|
code = (code<<8) + p->buf[i];
|
2003-07-10 12:04:04 +03:00
|
|
|
if ((code & 0xffffff00) == 0x100) {
|
2010-05-24 15:32:13 +03:00
|
|
|
int len= p->buf[i+1] << 8 | p->buf[i+2];
|
2007-11-03 16:57:26 +02:00
|
|
|
int pes= check_pes(p->buf+i, p->buf+p->buf_size);
|
2011-09-29 01:43:24 +03:00
|
|
|
int pack = check_pack_header(p->buf+i);
|
2007-11-03 15:48:30 +02:00
|
|
|
|
2006-06-29 22:03:53 +03:00
|
|
|
if(code == SYSTEM_HEADER_START_CODE) sys++;
|
2011-09-29 01:43:24 +03:00
|
|
|
else if(code == PACK_START_CODE && pack) pspack++;
|
2008-08-26 04:29:43 +03:00
|
|
|
else if((code & 0xf0) == VIDEO_ID && pes) vid++;
|
2010-05-24 15:32:13 +03:00
|
|
|
// skip pes payload to avoid start code emulation for private
|
|
|
|
// and audio streams
|
|
|
|
else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;}
|
|
|
|
else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;}
|
2007-12-03 11:37:06 +02:00
|
|
|
|
|
|
|
else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
|
|
|
|
else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
|
2010-05-24 15:32:13 +03:00
|
|
|
else if(code == PRIVATE_STREAM_1 && !pes) invalid++;
|
2003-07-10 12:04:04 +03:00
|
|
|
}
|
2002-05-20 19:29:40 +03:00
|
|
|
}
|
2006-08-19 11:39:00 +03:00
|
|
|
|
2007-12-03 11:37:06 +02:00
|
|
|
if(vid+audio > invalid) /* invalid VDR files nd short PES streams */
|
2006-08-19 11:39:00 +03:00
|
|
|
score= AVPROBE_SCORE_MAX/4;
|
|
|
|
|
2007-12-03 11:37:06 +02:00
|
|
|
if(sys>invalid && sys*9 <= pspack*10)
|
2009-09-15 15:20:03 +03:00
|
|
|
return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
|
2009-12-08 14:19:30 +02:00
|
|
|
if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
|
2009-09-15 15:20:03 +03:00
|
|
|
return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
|
|
|
|
if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
|
2009-09-15 13:01:25 +03:00
|
|
|
return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
|
2006-08-19 11:39:00 +03:00
|
|
|
|
|
|
|
//02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
|
2009-09-15 13:01:25 +03:00
|
|
|
//mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
|
2006-08-19 11:39:00 +03:00
|
|
|
return score;
|
2002-05-20 19:29:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
typedef struct MpegDemuxContext {
|
2006-09-27 22:47:39 +03:00
|
|
|
int32_t header_state;
|
2005-03-28 20:33:21 +03:00
|
|
|
unsigned char psm_es_type[256];
|
2007-11-08 01:56:00 +02:00
|
|
|
int sofdec;
|
2001-07-22 17:18:56 +03:00
|
|
|
} MpegDemuxContext;
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int mpegps_read_header(AVFormatContext *s)
|
2003-11-10 20:47:52 +02:00
|
|
|
{
|
|
|
|
MpegDemuxContext *m = s->priv_data;
|
2007-11-08 23:27:37 +02:00
|
|
|
const char *sofdec = "Sofdec";
|
|
|
|
int v, i = 0;
|
2007-11-08 01:56:00 +02:00
|
|
|
|
2003-11-10 20:47:52 +02:00
|
|
|
m->header_state = 0xff;
|
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
|
2007-11-08 23:27:37 +02:00
|
|
|
m->sofdec = -1;
|
|
|
|
do {
|
2011-02-21 17:43:01 +02:00
|
|
|
v = avio_r8(s->pb);
|
2007-11-08 23:27:37 +02:00
|
|
|
m->header_state = m->header_state << 8 | v;
|
|
|
|
m->sofdec++;
|
|
|
|
} while (v == sofdec[i] && i++ < 6);
|
2007-11-08 01:56:00 +02:00
|
|
|
|
2009-10-20 18:27:05 +03:00
|
|
|
m->sofdec = (m->sofdec == 6) ? 1 : 0;
|
|
|
|
|
2003-11-10 20:47:52 +02:00
|
|
|
/* no need to do more */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int64_t get_pts(AVIOContext *pb, int c)
|
2003-11-10 20:47:52 +02:00
|
|
|
{
|
2008-01-08 01:32:57 +02:00
|
|
|
uint8_t buf[5];
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
buf[0] = c<0 ? avio_r8(pb) : c;
|
|
|
|
avio_read(pb, buf+1, 4);
|
2008-01-08 01:32:57 +02:00
|
|
|
|
|
|
|
return ff_parse_pes_pts(buf);
|
2003-11-10 20:47:52 +02:00
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static int find_next_start_code(AVIOContext *pb, int *size_ptr,
|
2006-09-27 22:47:39 +03:00
|
|
|
int32_t *header_state)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
unsigned int state, v;
|
|
|
|
int val, n;
|
|
|
|
|
|
|
|
state = *header_state;
|
|
|
|
n = *size_ptr;
|
|
|
|
while (n > 0) {
|
2011-03-07 22:50:25 +02:00
|
|
|
if (pb->eof_reached)
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
2011-02-21 17:43:01 +02:00
|
|
|
v = avio_r8(pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
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-03-28 20:33:21 +03:00
|
|
|
/**
|
2010-06-30 18:38:06 +03:00
|
|
|
* Extract stream types from a program stream map
|
2005-03-28 20:33:21 +03:00
|
|
|
* According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
|
2005-12-17 20:14:38 +02:00
|
|
|
*
|
2005-03-28 20:33:21 +03:00
|
|
|
* @return number of bytes occupied by PSM in the bitstream
|
|
|
|
*/
|
2011-02-20 12:04:12 +02:00
|
|
|
static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
|
2005-03-28 20:33:21 +03:00
|
|
|
{
|
|
|
|
int psm_length, ps_info_length, es_map_length;
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
psm_length = avio_rb16(pb);
|
|
|
|
avio_r8(pb);
|
|
|
|
avio_r8(pb);
|
|
|
|
ps_info_length = avio_rb16(pb);
|
2005-03-28 20:33:21 +03:00
|
|
|
|
|
|
|
/* skip program_stream_info */
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, ps_info_length);
|
2011-02-21 17:43:01 +02:00
|
|
|
es_map_length = avio_rb16(pb);
|
2005-03-28 20:33:21 +03:00
|
|
|
|
|
|
|
/* at least one es available? */
|
|
|
|
while (es_map_length >= 4){
|
2011-02-21 17:43:01 +02:00
|
|
|
unsigned char type = avio_r8(pb);
|
|
|
|
unsigned char es_id = avio_r8(pb);
|
|
|
|
uint16_t es_info_length = avio_rb16(pb);
|
2005-03-28 20:33:21 +03:00
|
|
|
/* remember mapping from stream id to stream type */
|
|
|
|
m->psm_es_type[es_id] = type;
|
|
|
|
/* skip program_stream_info */
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, es_info_length);
|
2005-03-28 20:33:21 +03:00
|
|
|
es_map_length -= 4 + es_info_length;
|
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_rb32(pb); /* crc32 */
|
2005-03-28 20:33:21 +03:00
|
|
|
return 2 + psm_length;
|
|
|
|
}
|
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
/* read the next PES header. Return its position in ppos
|
2003-11-10 20:47:52 +02:00
|
|
|
(if not NULL), and its start code, pts and dts.
|
|
|
|
*/
|
|
|
|
static int mpegps_read_pes_header(AVFormatContext *s,
|
2005-12-17 20:14:38 +02:00
|
|
|
int64_t *ppos, int *pstart_code,
|
2004-04-12 19:50:03 +03:00
|
|
|
int64_t *ppts, int64_t *pdts)
|
2001-07-22 17:18:56 +03:00
|
|
|
{
|
|
|
|
MpegDemuxContext *m = s->priv_data;
|
2003-11-10 20:47:52 +02:00
|
|
|
int len, size, startcode, c, flags, header_len;
|
2007-02-06 21:14:16 +02:00
|
|
|
int pes_ext, ext2_len, id_ext, skip;
|
2007-01-17 12:19:10 +02:00
|
|
|
int64_t pts, dts;
|
2011-03-03 21:11:45 +02:00
|
|
|
int64_t last_sync= avio_tell(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2007-01-17 12:19:10 +02:00
|
|
|
error_redo:
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(s->pb, last_sync, SEEK_SET);
|
2001-07-22 17:18:56 +03:00
|
|
|
redo:
|
2003-11-10 20:47:52 +02:00
|
|
|
/* next start code (should be immediately after) */
|
|
|
|
m->header_state = 0xff;
|
|
|
|
size = MAX_SYNC_SIZE;
|
2007-11-21 09:41:00 +02:00
|
|
|
startcode = find_next_start_code(s->pb, &size, &m->header_state);
|
2011-03-03 21:11:45 +02:00
|
|
|
last_sync = avio_tell(s->pb);
|
2010-02-10 16:25:57 +02:00
|
|
|
if (startcode < 0){
|
2011-03-07 22:50:25 +02:00
|
|
|
if(s->pb->eof_reached)
|
2010-02-10 16:25:57 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
//FIXME we should remember header_state
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
if (startcode == PACK_START_CODE)
|
|
|
|
goto redo;
|
|
|
|
if (startcode == SYSTEM_HEADER_START_CODE)
|
|
|
|
goto redo;
|
2007-11-08 23:27:37 +02:00
|
|
|
if (startcode == PADDING_STREAM) {
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, avio_rb16(s->pb));
|
2007-11-08 23:27:37 +02:00
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
if (startcode == PRIVATE_STREAM_2) {
|
2011-02-21 17:43:01 +02:00
|
|
|
len = avio_rb16(s->pb);
|
2007-11-08 23:27:37 +02:00
|
|
|
if (!m->sofdec) {
|
|
|
|
while (len-- >= 6) {
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(s->pb) == 'S') {
|
2007-11-08 23:27:37 +02:00
|
|
|
uint8_t buf[5];
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_read(s->pb, buf, sizeof(buf));
|
2007-11-08 23:27:37 +02:00
|
|
|
m->sofdec = !memcmp(buf, "ofdec", 5);
|
|
|
|
len -= sizeof(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m->sofdec -= !m->sofdec;
|
|
|
|
}
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, len);
|
2001-07-22 17:18:56 +03:00
|
|
|
goto redo;
|
|
|
|
}
|
2005-03-28 20:33:21 +03:00
|
|
|
if (startcode == PROGRAM_STREAM_MAP) {
|
2007-11-21 09:41:00 +02:00
|
|
|
mpegps_psm_parse(m, s->pb);
|
2005-03-28 20:33:21 +03:00
|
|
|
goto redo;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2001-07-22 17:18:56 +03:00
|
|
|
/* find matching stream */
|
|
|
|
if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
|
|
|
|
(startcode >= 0x1e0 && startcode <= 0x1ef) ||
|
2007-02-06 21:14:16 +02:00
|
|
|
(startcode == 0x1bd) || (startcode == 0x1fd)))
|
2001-07-22 17:18:56 +03:00
|
|
|
goto redo;
|
2003-11-10 20:47:52 +02:00
|
|
|
if (ppos) {
|
2011-03-03 21:11:45 +02:00
|
|
|
*ppos = avio_tell(s->pb) - 4;
|
2003-11-10 20:47:52 +02:00
|
|
|
}
|
2011-02-21 17:43:01 +02:00
|
|
|
len = avio_rb16(s->pb);
|
2007-01-17 12:45:59 +02:00
|
|
|
pts =
|
2002-10-21 18:57:21 +03:00
|
|
|
dts = AV_NOPTS_VALUE;
|
2001-07-22 17:18:56 +03:00
|
|
|
/* stuffing */
|
|
|
|
for(;;) {
|
2003-11-10 20:47:52 +02:00
|
|
|
if (len < 1)
|
2007-01-17 12:19:10 +02:00
|
|
|
goto error_redo;
|
2011-02-21 17:43:01 +02:00
|
|
|
c = avio_r8(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
len--;
|
|
|
|
/* XXX: for mpeg1, should test only bit 7 */
|
2005-12-17 20:14:38 +02:00
|
|
|
if (c != 0xff)
|
2001-07-22 17:18:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((c & 0xc0) == 0x40) {
|
|
|
|
/* buffer scale & size */
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(s->pb);
|
|
|
|
c = avio_r8(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
len -= 2;
|
|
|
|
}
|
2007-01-17 12:55:01 +02:00
|
|
|
if ((c & 0xe0) == 0x20) {
|
2007-11-21 09:41:00 +02:00
|
|
|
dts = pts = get_pts(s->pb, c);
|
2001-07-22 17:18:56 +03:00
|
|
|
len -= 4;
|
2007-01-17 12:55:01 +02:00
|
|
|
if (c & 0x10){
|
2007-11-21 09:41:00 +02:00
|
|
|
dts = get_pts(s->pb, -1);
|
2007-01-17 12:55:01 +02:00
|
|
|
len -= 5;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
} else if ((c & 0xc0) == 0x80) {
|
|
|
|
/* mpeg 2 PES */
|
2011-02-21 17:43:01 +02:00
|
|
|
flags = avio_r8(s->pb);
|
|
|
|
header_len = avio_r8(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
len -= 2;
|
|
|
|
if (header_len > len)
|
2007-01-17 12:19:10 +02:00
|
|
|
goto error_redo;
|
2007-01-17 14:06:31 +02:00
|
|
|
len -= header_len;
|
2007-01-17 12:55:01 +02:00
|
|
|
if (flags & 0x80) {
|
2007-11-21 09:41:00 +02:00
|
|
|
dts = pts = get_pts(s->pb, -1);
|
2001-07-22 17:18:56 +03:00
|
|
|
header_len -= 5;
|
2007-01-17 12:55:01 +02:00
|
|
|
if (flags & 0x40) {
|
2007-11-21 09:41:00 +02:00
|
|
|
dts = get_pts(s->pb, -1);
|
2007-01-17 12:55:01 +02:00
|
|
|
header_len -= 5;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2008-03-04 03:31:15 +02: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 21:14:16 +02:00
|
|
|
if (flags & 0x01) { /* PES extension */
|
2011-02-21 17:43:01 +02:00
|
|
|
pes_ext = avio_r8(s->pb);
|
2007-02-06 21:14:16 +02: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 03:12:49 +03: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;
|
|
|
|
}
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, skip);
|
2007-02-06 21:14:16 +02:00
|
|
|
header_len -= skip;
|
|
|
|
|
|
|
|
if (pes_ext & 0x01) { /* PES extension 2 */
|
2011-02-21 17:43:01 +02:00
|
|
|
ext2_len = avio_r8(s->pb);
|
2007-02-06 21:14:16 +02:00
|
|
|
header_len--;
|
|
|
|
if ((ext2_len & 0x7f) > 0) {
|
2011-02-21 17:43:01 +02:00
|
|
|
id_ext = avio_r8(s->pb);
|
2007-02-06 21:14:16 +02:00
|
|
|
if ((id_ext & 0x80) == 0)
|
|
|
|
startcode = ((startcode & 0xff) << 8) | id_ext;
|
|
|
|
header_len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-17 14:06:31 +02:00
|
|
|
if(header_len < 0)
|
|
|
|
goto error_redo;
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, header_len);
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
2004-04-24 00:02:01 +03:00
|
|
|
else if( c!= 0xf )
|
|
|
|
goto redo;
|
|
|
|
|
2005-03-28 20:33:21 +03:00
|
|
|
if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
|
2011-02-21 17:43:01 +02:00
|
|
|
startcode = avio_r8(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
len--;
|
2007-02-06 21:14:16 +02:00
|
|
|
if (startcode >= 0x80 && startcode <= 0xcf) {
|
2001-07-22 17:18:56 +03:00
|
|
|
/* audio: skip header */
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(s->pb);
|
|
|
|
avio_r8(s->pb);
|
|
|
|
avio_r8(s->pb);
|
2001-07-22 17:18:56 +03:00
|
|
|
len -= 3;
|
2007-02-06 21:14:16 +02:00
|
|
|
if (startcode >= 0xb0 && startcode <= 0xbf) {
|
|
|
|
/* MLP/TrueHD audio has a 4-byte header */
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(s->pb);
|
2007-02-06 21:14:16 +02:00
|
|
|
len--;
|
|
|
|
}
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
}
|
2007-01-17 12:44:57 +02:00
|
|
|
if(len<0)
|
|
|
|
goto error_redo;
|
2004-01-14 00:02:49 +02:00
|
|
|
if(dts != AV_NOPTS_VALUE && ppos){
|
|
|
|
int i;
|
|
|
|
for(i=0; i<s->nb_streams; i++){
|
2008-01-14 18:11:08 +02:00
|
|
|
if(startcode == s->streams[i]->id &&
|
2011-03-05 22:06:46 +02:00
|
|
|
s->pb->seekable /* index useless on streams anyway */) {
|
2008-01-13 15:33:37 +02:00
|
|
|
ff_reduce_index(s, i);
|
2006-03-01 13:29:55 +02:00
|
|
|
av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
|
2004-01-14 00:02:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2003-11-10 20:47:52 +02:00
|
|
|
*pstart_code = startcode;
|
|
|
|
*ppts = pts;
|
|
|
|
*pdts = dts;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mpegps_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2005-03-28 20:33:21 +03:00
|
|
|
MpegDemuxContext *m = s->priv_data;
|
2003-11-10 20:47:52 +02:00
|
|
|
AVStream *st;
|
2011-09-24 02:28:23 +03:00
|
|
|
int len, startcode, i, es_type, ret;
|
2012-08-05 12:11:04 +03:00
|
|
|
enum AVCodecID codec_id = AV_CODEC_ID_NONE;
|
2010-03-31 02:30:55 +03:00
|
|
|
enum AVMediaType type;
|
2004-01-14 00:02:49 +02:00
|
|
|
int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
|
2009-03-05 21:13:12 +02:00
|
|
|
uint8_t av_uninit(dvdaudio_substream_type);
|
2003-11-10 20:47:52 +02:00
|
|
|
|
|
|
|
redo:
|
2004-04-12 19:50:03 +03:00
|
|
|
len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
|
2003-11-10 20:47:52 +02:00
|
|
|
if (len < 0)
|
|
|
|
return len;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2009-02-11 13:09:36 +02:00
|
|
|
if(startcode == 0x1bd) {
|
2011-02-21 17:43:01 +02:00
|
|
|
dvdaudio_substream_type = avio_r8(s->pb);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, 3);
|
2009-02-11 13:09:36 +02:00
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
|
2001-07-22 17:18:56 +03: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 20:33:21 +03:00
|
|
|
|
|
|
|
es_type = m->psm_es_type[startcode & 0xff];
|
2009-02-11 13:09:36 +02:00
|
|
|
if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
|
2005-03-28 20:33:21 +03:00
|
|
|
if(es_type == STREAM_TYPE_VIDEO_MPEG1){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_MPEG2VIDEO;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_VIDEO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_MPEG2VIDEO;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_VIDEO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
|
|
|
|
es_type == STREAM_TYPE_AUDIO_MPEG2){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_MP3;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else if(es_type == STREAM_TYPE_AUDIO_AAC){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_AAC;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_MPEG4;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_VIDEO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else if(es_type == STREAM_TYPE_VIDEO_H264){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_H264;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_VIDEO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else if(es_type == STREAM_TYPE_AUDIO_AC3){
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_AC3;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2005-03-28 20:33:21 +03:00
|
|
|
} else {
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
} else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
|
2006-07-04 00:40:01 +03:00
|
|
|
static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
|
|
|
|
unsigned char buf[8];
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_read(s->pb, buf, 8);
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(s->pb, -8, SEEK_CUR);
|
2006-07-04 00:40:01 +03:00
|
|
|
if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_CAVS;
|
2006-07-04 00:40:01 +03:00
|
|
|
else
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_PROBE;
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_VIDEO;
|
2002-05-20 19:29:40 +03:00
|
|
|
} else if (startcode >= 0x1c0 && startcode <= 0x1df) {
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = m->sofdec > 0 ? AV_CODEC_ID_ADPCM_ADX : AV_CODEC_ID_MP2;
|
2005-05-20 16:10:09 +03:00
|
|
|
} else if (startcode >= 0x80 && startcode <= 0x87) {
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_AC3;
|
2008-08-26 04:29:43 +03:00
|
|
|
} else if ( ( startcode >= 0x88 && startcode <= 0x8f)
|
2007-02-06 21:14:16 +02:00
|
|
|
||( startcode >= 0x98 && startcode <= 0x9f)) {
|
|
|
|
/* 0x90 - 0x97 is reserved for SDDS in DVD specs */
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_DTS;
|
2007-02-06 21:14:16 +02:00
|
|
|
} else if (startcode >= 0xa0 && startcode <= 0xaf) {
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 12:11:04 +03:00
|
|
|
/* 16 bit form will be handled as AV_CODEC_ID_PCM_S16BE */
|
|
|
|
codec_id = AV_CODEC_ID_PCM_DVD;
|
2007-02-06 21:14:16 +02:00
|
|
|
} else if (startcode >= 0xb0 && startcode <= 0xbf) {
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_TRUEHD;
|
2007-02-06 21:14:16 +02:00
|
|
|
} else if (startcode >= 0xc0 && startcode <= 0xcf) {
|
|
|
|
/* Used for both AC-3 and E-AC-3 in EVOB files */
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_AC3;
|
2005-06-03 17:01:49 +03:00
|
|
|
} else if (startcode >= 0x20 && startcode <= 0x3f) {
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_SUBTITLE;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_DVD_SUBTITLE;
|
2007-02-06 21:14:16 +02:00
|
|
|
} else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_VIDEO;
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_VC1;
|
2009-02-11 13:09:36 +02:00
|
|
|
} else if (startcode == 0x1bd) {
|
|
|
|
// check dvd audio substream type
|
2010-03-31 02:30:55 +03:00
|
|
|
type = AVMEDIA_TYPE_AUDIO;
|
2009-02-11 13:09:36 +02:00
|
|
|
switch(dvdaudio_substream_type & 0xe0) {
|
2012-08-05 12:11:04 +03:00
|
|
|
case 0xa0: codec_id = AV_CODEC_ID_PCM_DVD;
|
2009-02-11 13:09:36 +02:00
|
|
|
break;
|
|
|
|
case 0x80: if((dvdaudio_substream_type & 0xf8) == 0x88)
|
2012-08-05 12:11:04 +03:00
|
|
|
codec_id = AV_CODEC_ID_DTS;
|
|
|
|
else codec_id = AV_CODEC_ID_AC3;
|
2009-02-11 13:09:36 +02:00
|
|
|
break;
|
|
|
|
default: av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
|
|
|
|
goto skip;
|
|
|
|
}
|
2002-05-20 19:29:40 +03:00
|
|
|
} else {
|
|
|
|
skip:
|
|
|
|
/* skip packet */
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, len);
|
2002-05-20 19:29:40 +03:00
|
|
|
goto redo;
|
|
|
|
}
|
2002-10-04 18:46:59 +03:00
|
|
|
/* no stream found: add a new stream */
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2005-12-17 20:14:38 +02:00
|
|
|
if (!st)
|
2002-10-04 18:46:59 +03:00
|
|
|
goto skip;
|
2011-06-18 12:43:24 +03:00
|
|
|
st->id = startcode;
|
2005-07-18 01:24:36 +03:00
|
|
|
st->codec->codec_type = type;
|
|
|
|
st->codec->codec_id = codec_id;
|
2012-08-05 12:11:04 +03:00
|
|
|
if (codec_id != AV_CODEC_ID_PCM_S16BE)
|
2007-04-15 16:51:57 +03:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_FULL;
|
2001-07-22 17:18:56 +03:00
|
|
|
found:
|
2005-03-17 03:25:01 +02:00
|
|
|
if(st->discard >= AVDISCARD_ALL)
|
2005-01-22 15:36:02 +02:00
|
|
|
goto skip;
|
2009-02-11 13:09:36 +02:00
|
|
|
if ((startcode >= 0xa0 && startcode <= 0xaf) ||
|
|
|
|
(startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
|
2003-01-31 19:04:46 +02:00
|
|
|
int b1, freq;
|
|
|
|
|
|
|
|
/* for LPCM, we just skip the header and consider it is raw
|
|
|
|
audio data */
|
|
|
|
if (len <= 3)
|
|
|
|
goto skip;
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
|
|
|
|
b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
|
|
|
|
avio_r8(s->pb); /* dynamic range control (0x80 = off) */
|
2003-01-31 19:04:46 +02:00
|
|
|
len -= 3;
|
|
|
|
freq = (b1 >> 4) & 3;
|
2005-07-18 01:24:36 +03:00
|
|
|
st->codec->sample_rate = lpcm_freq_tab[freq];
|
|
|
|
st->codec->channels = 1 + (b1 & 7);
|
2008-09-08 17:24:59 +03:00
|
|
|
st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
|
2008-05-21 05:26:42 +03:00
|
|
|
st->codec->bit_rate = st->codec->channels *
|
|
|
|
st->codec->sample_rate *
|
2008-09-08 17:24:59 +03:00
|
|
|
st->codec->bits_per_coded_sample;
|
|
|
|
if (st->codec->bits_per_coded_sample == 16)
|
2012-08-05 12:11:04 +03:00
|
|
|
st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
|
2008-09-08 17:24:59 +03:00
|
|
|
else if (st->codec->bits_per_coded_sample == 28)
|
2008-05-21 05:26:42 +03:00
|
|
|
return AVERROR(EINVAL);
|
2003-01-31 19:04:46 +02:00
|
|
|
}
|
2011-10-08 03:02:36 +03:00
|
|
|
ret = av_get_packet(s->pb, pkt, len);
|
2001-07-22 17:18:56 +03:00
|
|
|
pkt->pts = pts;
|
2003-11-10 20:47:52 +02:00
|
|
|
pkt->dts = dts;
|
2010-02-02 18:47:51 +02:00
|
|
|
pkt->pos = dummy_pos;
|
2002-05-20 19:29:40 +03:00
|
|
|
pkt->stream_index = st->index;
|
2011-06-02 21:40:09 +03:00
|
|
|
av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
|
2011-04-29 18:27:01 +03:00
|
|
|
pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
|
|
|
|
pkt->size);
|
2004-06-19 06:59:34 +03:00
|
|
|
|
2011-09-24 02:28:23 +03:00
|
|
|
return (ret < 0) ? ret : 0;
|
2001-07-22 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
2005-12-17 20:14:38 +02:00
|
|
|
static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
|
2004-04-12 19:50:03 +03:00
|
|
|
int64_t *ppos, int64_t pos_limit)
|
2003-11-10 20:47:52 +02:00
|
|
|
{
|
|
|
|
int len, startcode;
|
|
|
|
int64_t pos, pts, dts;
|
|
|
|
|
|
|
|
pos = *ppos;
|
2011-02-28 15:57:54 +02:00
|
|
|
if (avio_seek(s->pb, pos, SEEK_SET) < 0)
|
2008-05-29 12:50:17 +03:00
|
|
|
return AV_NOPTS_VALUE;
|
|
|
|
|
2003-11-10 20:47:52 +02:00
|
|
|
for(;;) {
|
2004-04-12 19:50:03 +03:00
|
|
|
len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
|
2003-11-10 20:47:52 +02:00
|
|
|
if (len < 0) {
|
2011-06-07 14:18:12 +03:00
|
|
|
av_dlog(s, "none (ret=%d)\n", len);
|
2003-11-10 20:47:52 +02:00
|
|
|
return AV_NOPTS_VALUE;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
if (startcode == s->streams[stream_index]->id &&
|
2003-11-10 20:47:52 +02:00
|
|
|
dts != AV_NOPTS_VALUE) {
|
|
|
|
break;
|
|
|
|
}
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(s->pb, len);
|
2003-11-10 20:47:52 +02:00
|
|
|
}
|
2011-06-07 14:18:12 +03:00
|
|
|
av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
|
|
|
|
pos, dts, dts / 90000.0);
|
2003-11-10 20:47:52 +02:00
|
|
|
*ppos = pos;
|
2004-05-23 19:26:12 +03:00
|
|
|
return dts;
|
2003-11-10 20:47:52 +02:00
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_mpegps_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "mpeg",
|
2012-07-24 04:23:48 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("MPEG-PS (MPEG-2 Program Stream)"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.priv_data_size = sizeof(MpegDemuxContext),
|
|
|
|
.read_probe = mpegps_probe,
|
|
|
|
.read_header = mpegps_read_header,
|
|
|
|
.read_packet = mpegps_read_packet,
|
|
|
|
.read_timestamp = mpegps_read_dts,
|
2012-04-06 17:50:48 +03:00
|
|
|
.flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
|
2001-07-22 17:18:56 +03:00
|
|
|
};
|