+ ByteIOContext pb;
+ int tag;
+ unsigned len;
+
+ init_put_byte(&pb, buf, size, 0, NULL, NULL, NULL, NULL);
+
+ len = ff_mp4_read_descr(s, &pb, &tag);
+ if (tag == MP4IODescrTag) {
+ get_be16(&pb); // ID
+ get_byte(&pb);
+ get_byte(&pb);
+ get_byte(&pb);
+ get_byte(&pb);
+ get_byte(&pb);
+ len = ff_mp4_read_descr(s, &pb, &tag);
+ if (tag == MP4ESDescrTag) {
+ *es_id = get_be16(&pb); /* ES_ID */
+ dprintf(s, "ES_ID %#x\n", *es_id);
+ get_byte(&pb); /* priority */
+ len = ff_mp4_read_descr(s, &pb, &tag);
+ if (tag == MP4DecConfigDescrTag) {
+ *dec_config_descr = av_malloc(len);
+ if (!*dec_config_descr)
+ return AVERROR(ENOMEM);
+ *dec_config_descr_size = len;
+ get_buffer(&pb, *dec_config_descr, len);
+ }
+ }
+ }
+ return 0;
+}
+
+int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
+ const uint8_t **pp, const uint8_t *desc_list_end,
+ int mp4_dec_config_descr_len, int mp4_es_id, int pid,
+ uint8_t *mp4_dec_config_descr)
+{
+ const uint8_t *desc_end;
+ int desc_len, desc_tag;
+ char language[4];
+
+ desc_tag = get8(pp, desc_list_end);
+ if (desc_tag < 0)
+ return -1;
+ desc_len = get8(pp, desc_list_end);
+ if (desc_len < 0)
+ return -1;
+ desc_end = *pp + desc_len;
+ if (desc_end > desc_list_end)
+ return -1;
+
+ dprintf(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
+
+ if (st->codec->codec_id == CODEC_ID_NONE &&
+ stream_type == STREAM_TYPE_PRIVATE_DATA)
+ mpegts_find_stream_type(st, desc_tag, DESC_types);
+
+ switch(desc_tag) {
+ case 0x1F: /* FMC descriptor */
+ get16(pp, desc_end);
+ if (st->codec->codec_id == CODEC_ID_AAC_LATM &&
+ mp4_dec_config_descr_len && mp4_es_id == pid) {
+ ByteIOContext pb;
+ init_put_byte(&pb, mp4_dec_config_descr,
+ mp4_dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
+ ff_mp4_read_dec_config_descr(fc, st, &pb);
+ if (st->codec->codec_id == CODEC_ID_AAC &&
+ st->codec->extradata_size > 0)
+ st->need_parsing = 0;
+ }
+ break;
+ case 0x56: /* DVB teletext descriptor */
+ language[0] = get8(pp, desc_end);
+ language[1] = get8(pp, desc_end);
+ language[2] = get8(pp, desc_end);
+ language[3] = 0;
+ av_metadata_set2(&st->metadata, "language", language, 0);
+ break;
+ case 0x59: /* subtitling descriptor */
+ language[0] = get8(pp, desc_end);
+ language[1] = get8(pp, desc_end);
+ language[2] = get8(pp, desc_end);
+ language[3] = 0;
+ get8(pp, desc_end);
+ if (st->codec->extradata) {
+ if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
+ av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
+ } else {
+ st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (st->codec->extradata) {
+ st->codec->extradata_size = 4;
+ memcpy(st->codec->extradata, *pp, 4);
+ }
+ }
+ *pp += 4;
+ av_metadata_set2(&st->metadata, "language", language, 0);
+ break;
+ case 0x0a: /* ISO 639 language descriptor */
+ language[0] = get8(pp, desc_end);
+ language[1] = get8(pp, desc_end);
+ language[2] = get8(pp, desc_end);
+ language[3] = 0;
+ av_metadata_set2(&st->metadata, "language", language, 0);
+ break;
+ case 0x05: /* registration descriptor */
+ st->codec->codec_tag = bytestream_get_le32(pp);
+ dprintf(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
+ if (st->codec->codec_id == CODEC_ID_NONE &&
+ stream_type == STREAM_TYPE_PRIVATE_DATA)
+ mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
+ break;
+ default:
+ break;
+ }
+ *pp = desc_end;
+ return 0;
+}
+
+static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
+{
+ MpegTSContext *ts = filter->u.section_filter.opaque;
+ SectionHeader h1, *h = &h1;
+ PESContext *pes;
+ AVStream *st;
+ const uint8_t *p, *p_end, *desc_list_end;
+ int program_info_length, pcr_pid, pid, stream_type;
+ int desc_list_len;
+ uint32_t prog_reg_desc = 0; /* registration descriptor */
+ uint8_t *mp4_dec_config_descr = NULL;
+ int mp4_dec_config_descr_len = 0;
+ int mp4_es_id = 0;
+
+#ifdef DEBUG
+ dprintf(ts->stream, "PMT: len %i\n", section_len);
+ av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
+#endif
+
+ p_end = section + section_len - 4;
+ p = section;
+ if (parse_section_header(h, &p, p_end) < 0)
+ return;
+
+ dprintf(ts->stream, "sid=0x%x sec_num=%d/%d\n",
+ h->id, h->sec_num, h->last_sec_num);
+
+ if (h->tid != PMT_TID)
+ return;
+
+ clear_program(ts, h->id);
+ pcr_pid = get16(&p, p_end) & 0x1fff;
+ if (pcr_pid < 0)
+ return;
+ add_pid_to_pmt(ts, h->id, pcr_pid);
+
+ dprintf(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
+
+ program_info_length = get16(&p, p_end) & 0xfff;
+ if (program_info_length < 0)
+ return;
+ while(program_info_length >= 2) {
+ uint8_t tag, len;
+ tag = get8(&p, p_end);
+ len = get8(&p, p_end);
+
+ dprintf(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
+
+ if(len > program_info_length - 2)
+ //something else is broken, exit the program_descriptors_loop
+ break;
+ program_info_length -= len + 2;
+ if (tag == 0x1d) { // IOD descriptor
+ get8(&p, p_end); // scope
+ get8(&p, p_end); // label
+ len -= 2;
+ mp4_read_iods(ts->stream, p, len, &mp4_es_id,
+ &mp4_dec_config_descr, &mp4_dec_config_descr_len);
+ } else if (tag == 0x05 && len >= 4) { // registration descriptor
+ prog_reg_desc = bytestream_get_le32(&p);
+ len -= 4;
+ }
+ p += len;
+ }
+ p += program_info_length;
+ if (p >= p_end)
+ goto out;
+
+ // stop parsing after pmt, we found header
+ if (!ts->stream->nb_streams)
+ ts->stop_parse = 1;
+
+ for(;;) {
+ st = 0;
+ stream_type = get8(&p, p_end);
+ if (stream_type < 0)
+ break;
+ pid = get16(&p, p_end) & 0x1fff;
+ if (pid < 0)
+ break;
+
+ /* now create ffmpeg stream */
+ if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
+ pes = ts->pids[pid]->u.pes_filter.opaque;
+ if (!pes->st)
+ pes->st = av_new_stream(pes->stream, pes->pid);
+ st = pes->st;
+ } else {
+ if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
+ pes = add_pes_stream(ts, pid, pcr_pid);
+ if (pes)
+ st = av_new_stream(pes->stream, pes->pid);
+ }
+
+ if (!st)
+ goto out;
+
+ if (!pes->stream_type)
+ mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
+
+ add_pid_to_pmt(ts, h->id, pid);
+
+ ff_program_add_stream_index(ts->stream, h->id, st->index);
+
+ desc_list_len = get16(&p, p_end) & 0xfff;
+ if (desc_list_len < 0)
+ break;
+ desc_list_end = p + desc_list_len;
+ if (desc_list_end > p_end)
+ break;
+ for(;;) {
+ if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
+ mp4_dec_config_descr_len, mp4_es_id, pid, mp4_dec_config_descr) < 0)
+ break;
+
+ if (prog_reg_desc == AV_RL32("HDMV") && 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;
+ }
+ }
+ p = desc_list_end;
+ }
+
+ out:
+ av_free(mp4_dec_config_descr);
+}
+
+static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
+{
+ MpegTSContext *ts = filter->u.section_filter.opaque;
+ SectionHeader h1, *h = &h1;
+ const uint8_t *p, *p_end;
+ int sid, pmt_pid;
+
+#ifdef DEBUG
+ dprintf(ts->stream, "PAT:\n");
+ av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
+#endif
+ p_end = section + section_len - 4;
+ p = section;
+ if (parse_section_header(h, &p, p_end) < 0)
+ return;
+ if (h->tid != PAT_TID)
+ return;
+
+ clear_programs(ts);
+ for(;;) {
+ sid = get16(&p, p_end);
+ if (sid < 0)
+ break;
+ pmt_pid = get16(&p, p_end) & 0x1fff;
+ if (pmt_pid < 0)
+ break;
+
+ dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
+
+ if (sid == 0x0000) {
+ /* NIT info */
+ } else {
+ av_new_program(ts->stream, sid);
+ if (ts->pids[pmt_pid])
+ mpegts_close_filter(ts, ts->pids[pmt_pid]);
+ mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
+ add_pat_entry(ts, sid);
+ add_pid_to_pmt(ts, sid, 0); //add pat pid to program
+ add_pid_to_pmt(ts, sid, pmt_pid);
+ }
+ }
+}
+
+static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
+{
+ MpegTSContext *ts = filter->u.section_filter.opaque;
+ SectionHeader h1, *h = &h1;
+ const uint8_t *p, *p_end, *desc_list_end, *desc_end;
+ int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
+ char *name, *provider_name;
+
+#ifdef DEBUG
+ dprintf(ts->stream, "SDT:\n");
+ av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
+#endif
+
+ p_end = section + section_len - 4;
+ p = section;
+ if (parse_section_header(h, &p, p_end) < 0)
+ return;
+ if (h->tid != SDT_TID)
+ return;
+ onid = get16(&p, p_end);
+ if (onid < 0)
+ return;
+ val = get8(&p, p_end);
+ if (val < 0)
+ return;
+ for(;;) {
+ sid = get16(&p, p_end);
+ if (sid < 0)
+ break;
+ val = get8(&p, p_end);
+ if (val < 0)
+ break;
+ desc_list_len = get16(&p, p_end) & 0xfff;
+ if (desc_list_len < 0)
+ break;
+ desc_list_end = p + desc_list_len;
+ if (desc_list_end > p_end)
+ break;
+ for(;;) {
+ desc_tag = get8(&p, desc_list_end);
+ if (desc_tag < 0)
+ break;
+ desc_len = get8(&p, desc_list_end);
+ desc_end = p + desc_len;
+ if (desc_end > desc_list_end)
+ break;
+
+ dprintf(ts->stream, "tag: 0x%02x len=%d\n",
+ desc_tag, desc_len);
+
+ switch(desc_tag) {
+ case 0x48:
+ service_type = get8(&p, p_end);
+ if (service_type < 0)
+ break;
+ provider_name = getstr8(&p, p_end);
+ if (!provider_name)
+ break;
+ name = getstr8(&p, p_end);
+ if (name) {
+ AVProgram *program = av_new_program(ts->stream, sid);
+ if(program) {
+ av_metadata_set2(&program->metadata, "service_name", name, 0);
+ av_metadata_set2(&program->metadata, "service_provider", provider_name, 0);
+ }
+ }
+ av_free(name);
+ av_free(provider_name);
+ break;
+ default:
+ break;
+ }
+ p = desc_end;
+ }
+ p = desc_list_end;
+ }
+}
+
+/* handle one TS packet */
+static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
+{
+ AVFormatContext *s = ts->stream;
+ MpegTSFilter *tss;