634
650
pkt->dts = pes->dts;
635
651
/* store position of first TS packet of this PES packet */
636
652
pkt->pos = pes->ts_packet_pos;
653
pkt->flags = pes->flags;
638
655
/* reset pts values */
639
656
pes->pts = AV_NOPTS_VALUE;
640
657
pes->dts = AV_NOPTS_VALUE;
641
658
pes->buffer = NULL;
642
659
pes->data_index = 0;
663
static uint64_t get_bits64(GetBitContext *gb, int bits)
668
ret |= get_bits(gb, 17);
672
ret |= get_bits(gb, bits);
676
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
679
int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
680
int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
681
int dts_flag = -1, cts_flag = -1;
682
int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
683
init_get_bits(&gb, buf, buf_size*8);
685
if (sl->use_au_start)
686
au_start_flag = get_bits1(&gb);
688
au_end_flag = get_bits1(&gb);
689
if (!sl->use_au_start && !sl->use_au_end)
690
au_start_flag = au_end_flag = 1;
692
ocr_flag = get_bits1(&gb);
694
idle_flag = get_bits1(&gb);
696
padding_flag = get_bits1(&gb);
698
padding_bits = get_bits(&gb, 3);
700
if (!idle_flag && (!padding_flag || padding_bits != 0)) {
701
if (sl->packet_seq_num_len)
702
skip_bits_long(&gb, sl->packet_seq_num_len);
703
if (sl->degr_prior_len)
705
skip_bits(&gb, sl->degr_prior_len);
707
skip_bits_long(&gb, sl->ocr_len);
709
if (sl->use_rand_acc_pt)
711
if (sl->au_seq_num_len > 0)
712
skip_bits_long(&gb, sl->au_seq_num_len);
713
if (sl->use_timestamps) {
714
dts_flag = get_bits1(&gb);
715
cts_flag = get_bits1(&gb);
718
if (sl->inst_bitrate_len)
719
inst_bitrate_flag = get_bits1(&gb);
721
dts = get_bits64(&gb, sl->timestamp_len);
723
cts = get_bits64(&gb, sl->timestamp_len);
725
skip_bits_long(&gb, sl->au_len);
726
if (inst_bitrate_flag)
727
skip_bits_long(&gb, sl->inst_bitrate_len);
730
if (dts != AV_NOPTS_VALUE)
732
if (cts != AV_NOPTS_VALUE)
735
if (sl->timestamp_len && sl->timestamp_res)
736
avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
738
return (get_bits_count(&gb) + 7) >> 3;
645
741
/* return non zero if a packet could be constructed */
967
Mp4Descr *active_descr;
971
} MP4DescrParseContext;
973
static int init_MP4DescrParseContext(
974
MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
975
unsigned size, Mp4Descr *descr, int max_descr_count)
979
return AVERROR_INVALIDDATA;
981
if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
982
NULL, NULL, NULL, NULL)) < 0)
989
d->active_descr = NULL;
990
d->max_descr_count = max_descr_count;
995
static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
996
int64_t new_off = avio_tell(pb);
997
(*len) -= new_off - *off;
1001
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1004
static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
1007
if (parse_mp4_descr(d, off, len, 0) < 0)
1009
update_offsets(&d->pb, &off, &len);
1014
static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1016
avio_rb16(&d->pb); // ID
1022
update_offsets(&d->pb, &off, &len);
1023
return parse_mp4_descr_arr(d, off, len);
1026
static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1031
id_flags = avio_rb16(&d->pb);
1032
if (!(id_flags & 0x0020)) { //URL_Flag
1033
update_offsets(&d->pb, &off, &len);
1034
return parse_mp4_descr_arr(d, off, len); //ES_Descriptor[]
1040
static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1043
if (d->descr_count >= d->max_descr_count)
1045
ff_mp4_parse_es_descr(&d->pb, &es_id);
1046
d->active_descr = d->descr + (d->descr_count++);
1048
d->active_descr->es_id = es_id;
1049
update_offsets(&d->pb, &off, &len);
1050
parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
1051
update_offsets(&d->pb, &off, &len);
1053
parse_mp4_descr(d, off, len, MP4SLDescrTag);
1054
d->active_descr = NULL;
1058
static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1060
Mp4Descr *descr = d->active_descr;
1063
d->active_descr->dec_config_descr = av_malloc(len);
1064
if (!descr->dec_config_descr)
1065
return AVERROR(ENOMEM);
1066
descr->dec_config_descr_len = len;
1067
avio_read(&d->pb, descr->dec_config_descr, len);
1071
static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1073
Mp4Descr *descr = d->active_descr;
1078
predefined = avio_r8(&d->pb);
1081
int flags = avio_r8(&d->pb);
1082
descr->sl.use_au_start = !!(flags & 0x80);
1083
descr->sl.use_au_end = !!(flags & 0x40);
1084
descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1085
descr->sl.use_padding = !!(flags & 0x08);
1086
descr->sl.use_timestamps = !!(flags & 0x04);
1087
descr->sl.use_idle = !!(flags & 0x02);
1088
descr->sl.timestamp_res = avio_rb32(&d->pb);
1090
descr->sl.timestamp_len = avio_r8(&d->pb);
1091
descr->sl.ocr_len = avio_r8(&d->pb);
1092
descr->sl.au_len = avio_r8(&d->pb);
1093
descr->sl.inst_bitrate_len = avio_r8(&d->pb);
1094
lengths = avio_rb16(&d->pb);
1095
descr->sl.degr_prior_len = lengths >> 12;
1096
descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1097
descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1099
av_log_missing_feature(d->s, "Predefined SLConfigDescriptor\n", 0);
1104
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1107
int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
1108
update_offsets(&d->pb, &off, &len);
1109
if (len < 0 || len1 > len || len1 <= 0) {
1110
av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
1114
if (d->level++ >= MAX_LEVEL) {
1115
av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1119
if (target_tag && tag != target_tag) {
1120
av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
1126
parse_MP4IODescrTag(d, off, len1);
1129
parse_MP4ODescrTag(d, off, len1);
1132
parse_MP4ESDescrTag(d, off, len1);
1134
case MP4DecConfigDescrTag:
1135
parse_MP4DecConfigDescrTag(d, off, len1);
1138
parse_MP4SLDescrTag(d, off, len1);
1144
avio_seek(&d->pb, off + len1, SEEK_SET);
859
1148
static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
860
int *es_id, uint8_t **dec_config_descr,
861
int *dec_config_descr_size)
1149
Mp4Descr *descr, int *descr_count, int max_descr_count)
1151
MP4DescrParseContext d;
1152
if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
1155
parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
1157
*descr_count = d.descr_count;
1161
static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1162
Mp4Descr *descr, int *descr_count, int max_descr_count)
1164
MP4DescrParseContext d;
1165
if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
1168
parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
1170
*descr_count = d.descr_count;
1174
static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1176
MpegTSContext *ts = filter->u.section_filter.opaque;
1178
const uint8_t *p, *p_end;
867
ffio_init_context(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
869
len = ff_mp4_read_descr(s, &pb, &tag);
870
if (tag == MP4IODescrTag) {
871
avio_rb16(&pb); // ID
877
len = ff_mp4_read_descr(s, &pb, &tag);
878
if (tag == MP4ESDescrTag) {
879
*es_id = avio_rb16(&pb); /* ES_ID */
880
av_dlog(s, "ES_ID %#x\n", *es_id);
881
avio_r8(&pb); /* priority */
882
len = ff_mp4_read_descr(s, &pb, &tag);
883
if (tag == MP4DecConfigDescrTag) {
884
*dec_config_descr = av_malloc(len);
885
if (!*dec_config_descr)
886
return AVERROR(ENOMEM);
887
*dec_config_descr_size = len;
888
avio_read(&pb, *dec_config_descr, len);
1180
Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
1181
int mp4_descr_count = 0;
1183
AVFormatContext *s = ts->stream;
1185
p_end = section + section_len - 4;
1187
if (parse_section_header(&h, &p, p_end) < 0)
1189
if (h.tid != M4OD_TID)
1192
mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
1194
for (pid = 0; pid < NB_PID_MAX; pid++) {
1197
for (i = 0; i < mp4_descr_count; i++) {
1200
if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1202
if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
1203
av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1206
pes = ts->pids[pid]->u.pes_filter.opaque;
1212
pes->sl = mp4_descr[i].sl;
1214
ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1215
mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1216
ff_mp4_read_dec_config_descr(s, st, &pb);
1217
if (st->codec->codec_id == CODEC_ID_AAC &&
1218
st->codec->extradata_size > 0)
1219
st->need_parsing = 0;
1220
if (st->codec->codec_id == CODEC_ID_H264 &&
1221
st->codec->extradata_size > 0)
1222
st->need_parsing = 0;
1224
if (st->codec->codec_id <= CODEC_ID_NONE) {
1225
} else if (st->codec->codec_id < CODEC_ID_FIRST_AUDIO) {
1226
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1227
} else if (st->codec->codec_id < CODEC_ID_FIRST_SUBTITLE) {
1228
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1229
} else if (st->codec->codec_id < CODEC_ID_FIRST_UNKNOWN) {
1230
st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1234
for (i = 0; i < mp4_descr_count; i++)
1235
av_free(mp4_descr[i].dec_config_descr);
895
1238
int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
896
1239
const uint8_t **pp, const uint8_t *desc_list_end,
897
int mp4_dec_config_descr_len, int mp4_es_id, int pid,
898
uint8_t *mp4_dec_config_descr)
1240
Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
900
1243
const uint8_t *desc_end;
901
int desc_len, desc_tag;
1244
int desc_len, desc_tag, desc_es_id;
902
1245
char language[252];
919
1262
mpegts_find_stream_type(st, desc_tag, DESC_types);
921
1264
switch(desc_tag) {
1265
case 0x1E: /* SL descriptor */
1266
desc_es_id = get16(pp, desc_end);
1267
if (ts && ts->pids[pid])
1268
ts->pids[pid]->es_id = desc_es_id;
1269
for (i = 0; i < mp4_descr_count; i++)
1270
if (mp4_descr[i].dec_config_descr_len &&
1271
mp4_descr[i].es_id == desc_es_id) {
1273
ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1274
mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1275
ff_mp4_read_dec_config_descr(fc, st, &pb);
1276
if (st->codec->codec_id == CODEC_ID_AAC &&
1277
st->codec->extradata_size > 0)
1278
st->need_parsing = 0;
1279
if (st->codec->codec_id == CODEC_ID_MPEG4SYSTEMS)
1280
mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
922
1283
case 0x1F: /* FMC descriptor */
923
1284
get16(pp, desc_end);
924
if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
925
mp4_dec_config_descr_len && mp4_es_id == pid) {
1285
if (mp4_descr_count > 0 && st->codec->codec_id == CODEC_ID_AAC_LATM &&
1286
mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
927
ffio_init_context(&pb, mp4_dec_config_descr,
928
mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1288
ffio_init_context(&pb, mp4_descr->dec_config_descr,
1289
mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
929
1290
ff_mp4_read_dec_config_descr(fc, st, &pb);
930
1291
if (st->codec->codec_id == CODEC_ID_AAC &&
931
1292
st->codec->extradata_size > 0)