mpegts: K&R formatting cosmetics

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
This commit is contained in:
Keiji Costantini 2014-03-17 17:03:07 +00:00 committed by Vittorio Giovara
parent 75177b2f5e
commit 86ba2327e1
1 changed files with 391 additions and 329 deletions

View File

@ -37,7 +37,7 @@
#include "isom.h" #include "isom.h"
/* maximum size in which we look for synchronisation if /* maximum size in which we look for synchronisation if
synchronisation is lost */ * synchronisation is lost */
#define MAX_RESYNC_SIZE 65536 #define MAX_RESYNC_SIZE 65536
#define MAX_PES_PAYLOAD 200 * 1024 #define MAX_PES_PAYLOAD 200 * 1024
@ -58,7 +58,8 @@ enum MpegTSFilterType {
typedef struct MpegTSFilter MpegTSFilter; typedef struct MpegTSFilter MpegTSFilter;
typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos); typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len,
int is_start, int64_t pos);
typedef struct MpegTSPESFilter { typedef struct MpegTSPESFilter {
PESCallback *pes_cb; PESCallback *pes_cb;
@ -132,16 +133,18 @@ struct MpegTSContext {
unsigned int nb_prg; unsigned int nb_prg;
struct Program *prg; struct Program *prg;
/** filters for various streams specified by PMT + for the PAT and PMT */ /** filters for various streams specified by PMT + for the PAT and PMT */
MpegTSFilter *pids[NB_PID_MAX]; MpegTSFilter *pids[NB_PID_MAX];
}; };
static const AVOption options[] = { static const AVOption options[] = {
{"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT, { "compute_pcr", "Compute exact PCR for each transport stream packet.",
offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
{ .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM }, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
{"ts_packetsize", "Output option carrying the raw packet size.", offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT, { "ts_packetsize", "Output option carrying the raw packet size.",
{.i64 = 0}, 0, 0, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY }, offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
{ .i64 = 0 }, 0, 0,
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
{ NULL }, { NULL },
}; };
@ -219,7 +222,8 @@ static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
ts->nb_prg++; ts->nb_prg++;
} }
static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid) static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid,
unsigned int pid)
{ {
int i; int i;
struct Program *p = NULL; struct Program *p = NULL;
@ -252,12 +256,10 @@ static int discard_pid(MpegTSContext *ts, unsigned int pid)
struct Program *p; struct Program *p;
/* If none of the programs have .discard=AVDISCARD_ALL then there's /* If none of the programs have .discard=AVDISCARD_ALL then there's
* no way we have to discard this packet * no way we have to discard this packet */
*/ for (k = 0; k < ts->stream->nb_programs; k++)
for (k = 0; k < ts->stream->nb_programs; k++) {
if (ts->stream->programs[k]->discard == AVDISCARD_ALL) if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
break; break;
}
if (k == ts->stream->nb_programs) if (k == ts->stream->nb_programs)
return 0; return 0;
@ -314,7 +316,8 @@ static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
tss->section_h_size = len; tss->section_h_size = len;
} }
if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) { if (tss->section_h_size != -1 &&
tss->section_index >= tss->section_h_size) {
tss->end_of_section_reached = 1; tss->end_of_section_reached = 1;
if (!tss->check_crc || if (!tss->check_crc ||
av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
@ -323,10 +326,11 @@ static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
} }
} }
static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid, static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts,
SectionCallback *section_cb, void *opaque, unsigned int pid,
SectionCallback *section_cb,
void *opaque,
int check_crc) int check_crc)
{ {
MpegTSFilter *filter; MpegTSFilter *filter;
MpegTSSectionFilter *sec; MpegTSSectionFilter *sec;
@ -339,10 +343,12 @@ static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int
if (!filter) if (!filter)
return NULL; return NULL;
ts->pids[pid] = filter; ts->pids[pid] = filter;
filter->type = MPEGTS_SECTION; filter->type = MPEGTS_SECTION;
filter->pid = pid; filter->pid = pid;
filter->es_id = -1; filter->es_id = -1;
filter->last_cc = -1; filter->last_cc = -1;
sec = &filter->u.section_filter; sec = &filter->u.section_filter;
sec->section_cb = section_cb; sec->section_cb = section_cb;
sec->opaque = opaque; sec->opaque = opaque;
@ -367,11 +373,13 @@ static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
filter = av_mallocz(sizeof(MpegTSFilter)); filter = av_mallocz(sizeof(MpegTSFilter));
if (!filter) if (!filter)
return NULL; return NULL;
ts->pids[pid] = filter; ts->pids[pid] = filter;
filter->type = MPEGTS_PES; filter->type = MPEGTS_PES;
filter->pid = pid; filter->pid = pid;
filter->es_id = -1; filter->es_id = -1;
filter->last_cc = -1; filter->last_cc = -1;
pes = &filter->u.pes_filter; pes = &filter->u.pes_filter;
pes->pes_cb = pes_cb; pes->pes_cb = pes_cb;
pes->opaque = opaque; pes->opaque = opaque;
@ -399,7 +407,8 @@ static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
ts->pids[pid] = NULL; ts->pids[pid] = NULL;
} }
static int analyze(const uint8_t *buf, int size, int packet_size, int *index){ static int analyze(const uint8_t *buf, int size, int packet_size, int *index)
{
int stat[TS_MAX_PACKET_SIZE]; int stat[TS_MAX_PACKET_SIZE];
int i; int i;
int x = 0; int x = 0;
@ -412,12 +421,14 @@ static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
stat[x]++; stat[x]++;
if (stat[x] > best_score) { if (stat[x] > best_score) {
best_score = stat[x]; best_score = stat[x];
if(index) *index= x; if (index)
*index = x;
} }
} }
x++; x++;
if(x == packet_size) x= 0; if (x == packet_size)
x = 0;
} }
return best_score; return best_score;
@ -437,10 +448,14 @@ static int get_packet_size(const uint8_t *buf, int size)
av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n", av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
score, dvhs_score, fec_score); score, dvhs_score, fec_score);
if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE; if (score > fec_score && score > dvhs_score)
else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE; return TS_PACKET_SIZE;
else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE; else if (dvhs_score > score && dvhs_score > fec_score)
else return -1; return TS_DVHS_PACKET_SIZE;
else if (score < fec_score && dvhs_score < fec_score)
return TS_FEC_PACKET_SIZE;
else
return -1;
} }
typedef struct SectionHeader { typedef struct SectionHeader {
@ -594,16 +609,16 @@ static const StreamType DESC_types[] = {
}; };
static void mpegts_find_stream_type(AVStream *st, static void mpegts_find_stream_type(AVStream *st,
uint32_t stream_type, const StreamType *types) uint32_t stream_type,
const StreamType *types)
{ {
for (; types->stream_type; types++) { for (; types->stream_type; types++)
if (stream_type == types->stream_type) { if (stream_type == types->stream_type) {
st->codec->codec_type = types->codec_type; st->codec->codec_type = types->codec_type;
st->codec->codec_id = types->codec_id; st->codec->codec_id = types->codec_id;
return; return;
} }
} }
}
static int mpegts_set_stream_info(AVStream *st, PESContext *pes, static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
uint32_t stream_type, uint32_t prog_reg_desc) uint32_t stream_type, uint32_t prog_reg_desc)
@ -666,7 +681,8 @@ static void new_pes_packet(PESContext *pes, AVPacket *pkt)
pkt->size = pes->data_index; pkt->size = pes->data_index;
if (pes->total_size != MAX_PES_PAYLOAD && if (pes->total_size != MAX_PES_PAYLOAD &&
pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) { pes->pes_header_size + pes->data_index != pes->total_size +
PES_START_SIZE) {
av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n"); av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
pes->flags |= AV_PKT_FLAG_CORRUPT; pes->flags |= AV_PKT_FLAG_CORRUPT;
} }
@ -691,7 +707,8 @@ static void new_pes_packet(PESContext *pes, AVPacket *pkt)
pes->flags = 0; pes->flags = 0;
} }
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size) static int read_sl_header(PESContext *pes, SLConfigDescr *sl,
const uint8_t *buf, int buf_size)
{ {
GetBitContext gb; GetBitContext gb;
int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0; int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
@ -791,15 +808,17 @@ static int mpegts_push_data(MpegTSFilter *filter,
buf_size -= len; buf_size -= len;
if (pes->data_index == PES_START_SIZE) { if (pes->data_index == PES_START_SIZE) {
/* we got all the PES or section header. We can now /* we got all the PES or section header. We can now
decide */ * decide */
if (pes->header[0] == 0x00 && pes->header[1] == 0x00 && if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
pes->header[2] == 0x01) { pes->header[2] == 0x01) {
/* it must be an mpeg2 PES stream */ /* it must be an mpeg2 PES stream */
code = pes->header[3] | 0x100; code = pes->header[3] | 0x100;
av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code); av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid,
code);
if ((pes->st && pes->st->discard == AVDISCARD_ALL && if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
(!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) || (!pes->sub_st ||
pes->sub_st->discard == AVDISCARD_ALL)) ||
code == 0x1be) /* padding_stream */ code == 0x1be) /* padding_stream */
goto skip; goto skip;
@ -814,7 +833,7 @@ static int mpegts_push_data(MpegTSFilter *filter,
pes->total_size = AV_RB16(pes->header + 4); pes->total_size = AV_RB16(pes->header + 4);
/* NOTE: a zero total size means the PES size is /* NOTE: a zero total size means the PES size is
unbounded */ * unbounded */
if (!pes->total_size) if (!pes->total_size)
pes->total_size = MAX_PES_PAYLOAD; pes->total_size = MAX_PES_PAYLOAD;
@ -830,8 +849,10 @@ static int mpegts_push_data(MpegTSFilter *filter,
code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */ code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
pes->state = MPEGTS_PESHEADER; pes->state = MPEGTS_PESHEADER;
if (pes->st->codec->codec_id == AV_CODEC_ID_NONE) { if (pes->st->codec->codec_id == AV_CODEC_ID_NONE) {
av_dlog(pes->stream, "pid=%x stream_type=%x probing\n", av_dlog(pes->stream,
pes->pid, pes->stream_type); "pid=%x stream_type=%x probing\n",
pes->pid,
pes->stream_type);
pes->st->codec->codec_id = AV_CODEC_ID_PROBE; pes->st->codec->codec_id = AV_CODEC_ID_PROBE;
} }
} else { } else {
@ -910,7 +931,8 @@ static int mpegts_push_data(MpegTSFilter *filter,
pes->state = MPEGTS_PAYLOAD; pes->state = MPEGTS_PAYLOAD;
pes->data_index = 0; pes->data_index = 0;
if (pes->stream_type == 0x12 && buf_size > 0) { if (pes->stream_type == 0x12 && buf_size > 0) {
int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size); int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
buf_size);
pes->pes_header_size += sl_header_bytes; pes->pes_header_size += sl_header_bytes;
p += sl_header_bytes; p += sl_header_bytes;
buf_size -= sl_header_bytes; buf_size -= sl_header_bytes;
@ -919,14 +941,17 @@ static int mpegts_push_data(MpegTSFilter *filter,
break; break;
case MPEGTS_PAYLOAD: case MPEGTS_PAYLOAD:
if (buf_size > 0 && pes->buffer) { if (buf_size > 0 && pes->buffer) {
if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) { if (pes->data_index > 0 &&
pes->data_index + buf_size > pes->total_size) {
new_pes_packet(pes, ts->pkt); new_pes_packet(pes, ts->pkt);
pes->total_size = MAX_PES_PAYLOAD; pes->total_size = MAX_PES_PAYLOAD;
pes->buffer = av_buffer_alloc(pes->total_size + FF_INPUT_BUFFER_PADDING_SIZE); pes->buffer = av_buffer_alloc(pes->total_size +
FF_INPUT_BUFFER_PADDING_SIZE);
if (!pes->buffer) if (!pes->buffer)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
ts->stop_parse = 1; ts->stop_parse = 1;
} else if (pes->data_index == 0 && buf_size > pes->total_size) { } else if (pes->data_index == 0 &&
buf_size > pes->total_size) {
// pes packet size is < ts size packet and pes data is padded with 0xff // pes packet size is < ts size packet and pes data is padded with 0xff
// not sure if this is legal in ts but see issue #2392 // not sure if this is legal in ts but see issue #2392
buf_size = pes->total_size; buf_size = pes->total_size;
@ -990,9 +1015,9 @@ typedef struct {
int level; int level;
} MP4DescrParseContext; } MP4DescrParseContext;
static int init_MP4DescrParseContext( static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s,
MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf, const uint8_t *buf, unsigned size,
unsigned size, Mp4Descr *descr, int max_descr_count) Mp4Descr *descr, int max_descr_count)
{ {
int ret; int ret;
if (size > (1 << 30)) if (size > (1 << 30))
@ -1012,7 +1037,8 @@ static int init_MP4DescrParseContext(
return 0; return 0;
} }
static void update_offsets(AVIOContext *pb, int64_t *off, int *len) { static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
{
int64_t new_off = avio_tell(pb); int64_t new_off = avio_tell(pb);
(*len) -= new_off - *off; (*len) -= new_off - *off;
*off = new_off; *off = new_off;
@ -1075,7 +1101,8 @@ static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
return 0; return 0;
} }
static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len) static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off,
int len)
{ {
Mp4Descr *descr = d->active_descr; Mp4Descr *descr = d->active_descr;
if (!descr) if (!descr)
@ -1122,12 +1149,15 @@ static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
} }
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
int target_tag) { int target_tag)
{
int tag; int tag;
int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag); int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
update_offsets(&d->pb, &off, &len); update_offsets(&d->pb, &off, &len);
if (len < 0 || len1 > len || len1 <= 0) { if (len < 0 || len1 > len || len1 <= 0) {
av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len); av_log(d->s, AV_LOG_ERROR,
"Tag %x length violation new length %d bytes remaining %d\n",
tag, len1, len);
return -1; return -1;
} }
@ -1137,7 +1167,8 @@ static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
} }
if (target_tag && tag != target_tag) { if (target_tag && tag != target_tag) {
av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag); av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
target_tag);
goto done; goto done;
} }
@ -1191,14 +1222,15 @@ static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
return 0; return 0;
} }
static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
int section_len)
{ {
MpegTSContext *ts = filter->u.section_filter.opaque; MpegTSContext *ts = filter->u.section_filter.opaque;
SectionHeader h; SectionHeader h;
const uint8_t *p, *p_end; const uint8_t *p, *p_end;
AVIOContext pb; AVIOContext pb;
Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
int mp4_descr_count = 0; int mp4_descr_count = 0;
Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
int i, pid; int i, pid;
AVFormatContext *s = ts->stream; AVFormatContext *s = ts->stream;
@ -1209,7 +1241,8 @@ static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_le
if (h.tid != M4OD_TID) if (h.tid != M4OD_TID)
return; return;
mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT); mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
MAX_MP4_DESCR_COUNT);
for (pid = 0; pid < NB_PID_MAX; pid++) { for (pid = 0; pid < NB_PID_MAX; pid++) {
if (!ts->pids[pid]) if (!ts->pids[pid])
@ -1225,14 +1258,14 @@ static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_le
} }
pes = ts->pids[pid]->u.pes_filter.opaque; pes = ts->pids[pid]->u.pes_filter.opaque;
st = pes->st; st = pes->st;
if (!st) { if (!st)
continue; continue;
}
pes->sl = mp4_descr[i].sl; pes->sl = mp4_descr[i].sl;
ffio_init_context(&pb, mp4_descr[i].dec_config_descr, ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL); mp4_descr[i].dec_config_descr_len, 0,
NULL, NULL, NULL, NULL);
ff_mp4_read_dec_config_descr(s, st, &pb); ff_mp4_read_dec_config_descr(s, st, &pb);
if (st->codec->codec_id == AV_CODEC_ID_AAC && if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size > 0) st->codec->extradata_size > 0)
@ -1242,15 +1275,15 @@ static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_le
st->need_parsing = 0; st->need_parsing = 0;
if (st->codec->codec_id <= AV_CODEC_ID_NONE) { if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
} else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) { // do nothing
} else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO)
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
} else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) { else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
} else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) { else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
} }
} }
}
for (i = 0; i < mp4_descr_count; i++) for (i = 0; i < mp4_descr_count; i++)
av_free(mp4_descr[i].dec_config_descr); av_free(mp4_descr[i].dec_config_descr);
} }
@ -1291,7 +1324,8 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
mp4_descr[i].es_id == desc_es_id) { mp4_descr[i].es_id == desc_es_id) {
AVIOContext pb; AVIOContext pb;
ffio_init_context(&pb, mp4_descr[i].dec_config_descr, ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL); mp4_descr[i].dec_config_descr_len, 0,
NULL, NULL, NULL, NULL);
ff_mp4_read_dec_config_descr(fc, st, &pb); ff_mp4_read_dec_config_descr(fc, st, &pb);
if (st->codec->codec_id == AV_CODEC_ID_AAC && if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size > 0) st->codec->extradata_size > 0)
@ -1302,11 +1336,13 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
break; break;
case 0x1F: /* FMC descriptor */ case 0x1F: /* FMC descriptor */
get16(pp, desc_end); get16(pp, desc_end);
if (mp4_descr_count > 0 && st->codec->codec_id == AV_CODEC_ID_AAC_LATM && if (mp4_descr_count > 0 &&
st->codec->codec_id == AV_CODEC_ID_AAC_LATM &&
mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) { mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
AVIOContext pb; AVIOContext pb;
ffio_init_context(&pb, mp4_descr->dec_config_descr, ffio_init_context(&pb, mp4_descr->dec_config_descr,
mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL); mp4_descr->dec_config_descr_len, 0,
NULL, NULL, NULL, NULL);
ff_mp4_read_dec_config_descr(fc, st, &pb); ff_mp4_read_dec_config_descr(fc, st, &pb);
if (st->codec->codec_id == AV_CODEC_ID_AAC && if (st->codec->codec_id == AV_CODEC_ID_AAC &&
st->codec->extradata_size > 0) st->codec->extradata_size > 0)
@ -1337,7 +1373,8 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
break; break;
} }
if (st->codec->extradata) { if (st->codec->extradata) {
if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4)) if (st->codec->extradata_size == 4 &&
memcmp(st->codec->extradata, *pp, 4))
avpriv_request_sample(fc, "DVB sub with multiple IDs"); avpriv_request_sample(fc, "DVB sub with multiple IDs");
} else { } else {
st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE); st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
@ -1356,9 +1393,15 @@ int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type
language[i + 2] = get8(pp, desc_end); language[i + 2] = get8(pp, desc_end);
language[i + 3] = ','; language[i + 3] = ',';
switch (get8(pp, desc_end)) { switch (get8(pp, desc_end)) {
case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break; case 0x01:
case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break; st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS;
case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break; break;
case 0x02:
st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
break;
case 0x03:
st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
break;
} }
} }
if (i) { if (i) {
@ -1390,8 +1433,8 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
int desc_list_len; int desc_list_len;
uint32_t prog_reg_desc = 0; /* registration descriptor */ uint32_t prog_reg_desc = 0; /* registration descriptor */
Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
int mp4_descr_count = 0; int mp4_descr_count = 0;
Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
int i; int i;
av_dlog(ts->stream, "PMT: len %i\n", section_len); av_dlog(ts->stream, "PMT: len %i\n", section_len);
@ -1452,6 +1495,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
if (!ts->stream->nb_streams) if (!ts->stream->nb_streams)
ts->stop_parse = 1; ts->stop_parse = 1;
for (;;) { for (;;) {
st = 0; st = 0;
pes = NULL; pes = NULL;
@ -1472,7 +1516,8 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
} }
st = pes->st; st = pes->st;
} else if (stream_type != 0x13) { } else if (stream_type != 0x13) {
if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably if (ts->pids[pid])
mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
pes = add_pes_stream(ts, pid, pcr_pid); pes = add_pes_stream(ts, pid, pcr_pid);
if (pes) { if (pes) {
st = avformat_new_stream(pes->stream, NULL); st = avformat_new_stream(pes->stream, NULL);
@ -1507,12 +1552,15 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
if (desc_list_end > p_end) if (desc_list_end > p_end)
break; break;
for (;;) { for (;;) {
if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end, if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p,
mp4_descr, mp4_descr_count, pid, ts) < 0) desc_list_end, mp4_descr,
mp4_descr_count, pid, ts) < 0)
break; break;
if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) { if (pes && prog_reg_desc == AV_RL32("HDMV") &&
ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index); stream_type == 0x83 && pes->sub_st) {
ff_program_add_stream_index(ts->stream, h->id,
pes->sub_st->index);
pes->sub_st->codec->codec_tag = st->codec->codec_tag; pes->sub_st->codec->codec_tag = st->codec->codec_tag;
} }
} }
@ -1629,7 +1677,8 @@ static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
AVProgram *program = av_new_program(ts->stream, sid); AVProgram *program = av_new_program(ts->stream, sid);
if (program) { if (program) {
av_dict_set(&program->metadata, "service_name", name, 0); av_dict_set(&program->metadata, "service_name", name, 0);
av_dict_set(&program->metadata, "service_provider", provider_name, 0); av_dict_set(&program->metadata, "service_provider",
provider_name, 0);
} }
} }
av_free(name); av_free(name);
@ -1671,17 +1720,17 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
return 0; return 0;
has_adaptation = afc & 2; has_adaptation = afc & 2;
has_payload = afc & 1; has_payload = afc & 1;
is_discontinuity = has_adaptation is_discontinuity = has_adaptation &&
&& packet[4] != 0 /* with length > 0 */ packet[4] != 0 && /* with length > 0 */
&& (packet[5] & 0x80); /* and discontinuity indicated */ (packet[5] & 0x80); /* and discontinuity indicated */
/* continuity check (currently not used) */ /* continuity check (currently not used) */
cc = (packet[3] & 0xf); cc = (packet[3] & 0xf);
expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc; expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
cc_ok = pid == 0x1FFF // null packet PID cc_ok = pid == 0x1FFF || // null packet PID
|| is_discontinuity is_discontinuity ||
|| tss->last_cc < 0 tss->last_cc < 0 ||
|| expected_cc == cc; expected_cc == cc;
tss->last_cc = cc; tss->last_cc = cc;
if (!cc_ok) { if (!cc_ok) {
@ -1746,7 +1795,7 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
} }
/* XXX: try to find a better synchro over several packets (use /* XXX: try to find a better synchro over several packets (use
get_packet_size() ?) */ * get_packet_size() ?) */
static int mpegts_resync(AVFormatContext *s) static int mpegts_resync(AVFormatContext *s)
{ {
AVIOContext *pb = s->pb; AVIOContext *pb = s->pb;
@ -1761,13 +1810,15 @@ static int mpegts_resync(AVFormatContext *s)
return 0; return 0;
} }
} }
av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n"); av_log(s, AV_LOG_ERROR,
"max resync size reached, could not find sync byte\n");
/* no sync found */ /* no sync found */
return -1; return -1;
} }
/* return -1 if error or EOF. Return 0 if OK. */ /* return -1 if error or EOF. Return 0 if OK. */
static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data) static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
const uint8_t **data)
{ {
AVIOContext *pb = s->pb; AVIOContext *pb = s->pb;
int len; int len;
@ -1854,23 +1905,30 @@ static int mpegts_probe(AVProbeData *p)
if (check_count < CHECK_COUNT) if (check_count < CHECK_COUNT)
return -1; return -1;
score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count; score = analyze(p->buf, TS_PACKET_SIZE * check_count,
dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count; TS_PACKET_SIZE, NULL) * CHECK_COUNT / check_count;
fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count; dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * check_count,
TS_DVHS_PACKET_SIZE, NULL) * CHECK_COUNT / check_count;
fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE * check_count,
TS_FEC_PACKET_SIZE, NULL) * CHECK_COUNT / check_count;
av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n", av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
score, dvhs_score, fec_score); score, dvhs_score, fec_score);
// we need a clear definition for the returned score otherwise things will become messy sooner or later /* we need a clear definition for the returned score otherwise
if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT; * things will become messy sooner or later */
else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT; if (score > fec_score && score > dvhs_score && score > 6)
else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT; return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
else return -1; else if (dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6)
return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
else if (fec_score > 6)
return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
else
return -1;
} }
/* return the 90kHz PCR and the extension for the 27MHz PCR. return /* return the 90kHz PCR and the extension for the 27MHz PCR. return
(-1) if not available */ * (-1) if not available */
static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
const uint8_t *packet)
{ {
int afc, len, flags; int afc, len, flags;
const uint8_t *p; const uint8_t *p;
@ -1979,7 +2037,7 @@ static int mpegts_read_header(AVFormatContext *s)
/* NOTE2: it is only the bitrate of the start of the stream */ /* NOTE2: it is only the bitrate of the start of the stream */
ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]); ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0]; ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr; s->bit_rate = TS_PACKET_SIZE * 8 * 27e6 / ts->pcr_incr;
st->codec->bit_rate = s->bit_rate; st->codec->bit_rate = s->bit_rate;
st->start_time = ts->cur_pcr; st->start_time = ts->cur_pcr;
av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n", av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
@ -1994,8 +2052,7 @@ static int mpegts_read_header(AVFormatContext *s)
#define MAX_PACKET_READAHEAD ((128 * 1024) / 188) #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
static int mpegts_raw_read_packet(AVFormatContext *s, static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
AVPacket *pkt)
{ {
MpegTSContext *ts = s->priv_data; MpegTSContext *ts = s->priv_data;
int ret, i; int ret, i;
@ -2006,8 +2063,8 @@ static int mpegts_raw_read_packet(AVFormatContext *s,
if (av_new_packet(pkt, TS_PACKET_SIZE) < 0) if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
pkt->pos= avio_tell(s->pb);
ret = read_packet(s, pkt->data, ts->raw_packet_size, &data); ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
pkt->pos = avio_tell(s->pb);
if (ret < 0) { if (ret < 0) {
av_free_packet(pkt); av_free_packet(pkt);
return ret; return ret;
@ -2025,7 +2082,8 @@ static int mpegts_raw_read_packet(AVFormatContext *s,
avio_read(s->pb, pcr_buf, 12); avio_read(s->pb, pcr_buf, 12);
if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) { if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
/* XXX: not precise enough */ /* XXX: not precise enough */
ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) / ts->pcr_incr =
((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
(i + 1); (i + 1);
break; break;
} }
@ -2042,8 +2100,7 @@ static int mpegts_raw_read_packet(AVFormatContext *s,
return 0; return 0;
} }
static int mpegts_read_packet(AVFormatContext *s, static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
AVPacket *pkt)
{ {
MpegTSContext *ts = s->priv_data; MpegTSContext *ts = s->priv_data;
int ret, i; int ret, i;
@ -2053,7 +2110,7 @@ static int mpegts_read_packet(AVFormatContext *s,
ret = handle_packets(ts, 0); ret = handle_packets(ts, 0);
if (ret < 0) { if (ret < 0) {
/* flush pes data left */ /* flush pes data left */
for (i = 0; i < NB_PID_MAX; i++) { for (i = 0; i < NB_PID_MAX; i++)
if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) { if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
PESContext *pes = ts->pids[i]->u.pes_filter.opaque; PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
@ -2064,7 +2121,6 @@ static int mpegts_read_packet(AVFormatContext *s,
} }
} }
} }
}
if (!ret && pkt->size < 0) if (!ret && pkt->size < 0)
ret = AVERROR(EINTR); ret = AVERROR(EINTR);
@ -2078,7 +2134,8 @@ static void mpegts_free(MpegTSContext *ts)
clear_programs(ts); clear_programs(ts);
for (i = 0; i < NB_PID_MAX; i++) for (i = 0; i < NB_PID_MAX; i++)
if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]); if (ts->pids[i])
mpegts_close_filter(ts, ts->pids[i]);
} }
static int mpegts_read_close(AVFormatContext *s) static int mpegts_read_close(AVFormatContext *s)
@ -2094,9 +2151,12 @@ static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
MpegTSContext *ts = s->priv_data; MpegTSContext *ts = s->priv_data;
int64_t pos, timestamp; int64_t pos, timestamp;
uint8_t buf[TS_PACKET_SIZE]; uint8_t buf[TS_PACKET_SIZE];
int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid; int pcr_l, pcr_pid =
((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
const int find_next = 1; const int find_next = 1;
pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47; pos =
((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) *
ts->raw_packet_size + ts->pos47;
if (find_next) { if (find_next) {
for (;;) { for (;;) {
avio_seek(s->pb, pos, SEEK_SET); avio_seek(s->pb, pos, SEEK_SET);
@ -2127,7 +2187,8 @@ static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
return timestamp; return timestamp;
} }
static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){ static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
{
MpegTSContext *ts = s->priv_data; MpegTSContext *ts = s->priv_data;
uint8_t buf[TS_PACKET_SIZE]; uint8_t buf[TS_PACKET_SIZE];
int64_t pos; int64_t pos;
@ -2142,7 +2203,8 @@ static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, in
if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE) if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
return -1; return -1;
// pid = AV_RB16(buf + 1) & 0x1fff; // pid = AV_RB16(buf + 1) & 0x1fff;
if(buf[1] & 0x40) break; if (buf[1] & 0x40)
break;
pos += ts->raw_packet_size; pos += ts->raw_packet_size;
} }
avio_seek(s->pb, pos, SEEK_SET); avio_seek(s->pb, pos, SEEK_SET);
@ -2168,7 +2230,7 @@ MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
} }
/* return the consumed length if a packet was output, or -1 if no /* return the consumed length if a packet was output, or -1 if no
packet is output */ * packet is output */
int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt, int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
const uint8_t *buf, int len) const uint8_t *buf, int len)
{ {