yading@11: /* yading@11: * MPEG2 transport stream (aka DVB) demuxer yading@11: * Copyright (c) 2002-2003 Fabrice Bellard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavutil/buffer.h" yading@11: #include "libavutil/crc.h" yading@11: #include "libavutil/intreadwrite.h" yading@11: #include "libavutil/log.h" yading@11: #include "libavutil/dict.h" yading@11: #include "libavutil/mathematics.h" yading@11: #include "libavutil/opt.h" yading@11: #include "libavutil/avassert.h" yading@11: #include "libavcodec/bytestream.h" yading@11: #include "libavcodec/get_bits.h" yading@11: #include "avformat.h" yading@11: #include "mpegts.h" yading@11: #include "internal.h" yading@11: #include "avio_internal.h" yading@11: #include "seek.h" yading@11: #include "mpeg.h" yading@11: #include "isom.h" yading@11: yading@11: /* maximum size in which we look for synchronisation if yading@11: synchronisation is lost */ yading@11: #define MAX_RESYNC_SIZE 65536 yading@11: yading@11: #define MAX_PES_PAYLOAD 200*1024 yading@11: yading@11: #define MAX_MP4_DESCR_COUNT 16 yading@11: yading@11: enum MpegTSFilterType { yading@11: MPEGTS_PES, yading@11: MPEGTS_SECTION, yading@11: }; yading@11: yading@11: typedef struct MpegTSFilter MpegTSFilter; yading@11: yading@11: typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos); yading@11: yading@11: typedef struct MpegTSPESFilter { yading@11: PESCallback *pes_cb; yading@11: void *opaque; yading@11: } MpegTSPESFilter; yading@11: yading@11: typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len); yading@11: yading@11: typedef void SetServiceCallback(void *opaque, int ret); yading@11: yading@11: typedef struct MpegTSSectionFilter { yading@11: int section_index; yading@11: int section_h_size; yading@11: uint8_t *section_buf; yading@11: unsigned int check_crc:1; yading@11: unsigned int end_of_section_reached:1; yading@11: SectionCallback *section_cb; yading@11: void *opaque; yading@11: } MpegTSSectionFilter; yading@11: yading@11: struct MpegTSFilter { yading@11: int pid; yading@11: int es_id; yading@11: int last_cc; /* last cc code (-1 if first packet) */ yading@11: enum MpegTSFilterType type; yading@11: union { yading@11: MpegTSPESFilter pes_filter; yading@11: MpegTSSectionFilter section_filter; yading@11: } u; yading@11: }; yading@11: yading@11: #define MAX_PIDS_PER_PROGRAM 64 yading@11: struct Program { yading@11: unsigned int id; //program id/service id yading@11: unsigned int nb_pids; yading@11: unsigned int pids[MAX_PIDS_PER_PROGRAM]; yading@11: }; yading@11: yading@11: struct MpegTSContext { yading@11: const AVClass *class; yading@11: /* user data */ yading@11: AVFormatContext *stream; yading@11: /** raw packet size, including FEC if present */ yading@11: int raw_packet_size; yading@11: yading@11: int pos47; yading@11: yading@11: /** if true, all pids are analyzed to find streams */ yading@11: int auto_guess; yading@11: yading@11: /** compute exact PCR for each transport stream packet */ yading@11: int mpeg2ts_compute_pcr; yading@11: yading@11: int64_t cur_pcr; /**< used to estimate the exact PCR */ yading@11: int pcr_incr; /**< used to estimate the exact PCR */ yading@11: yading@11: /* data needed to handle file based ts */ yading@11: /** stop parsing loop */ yading@11: int stop_parse; yading@11: /** packet containing Audio/Video data */ yading@11: AVPacket *pkt; yading@11: /** to detect seek */ yading@11: int64_t last_pos; yading@11: yading@11: /******************************************/ yading@11: /* private mpegts data */ yading@11: /* scan context */ yading@11: /** structure to keep track of Program->pids mapping */ yading@11: unsigned int nb_prg; yading@11: struct Program *prg; yading@11: yading@11: int8_t crc_validity[NB_PID_MAX]; yading@11: yading@11: /** filters for various streams specified by PMT + for the PAT and PMT */ yading@11: MpegTSFilter *pids[NB_PID_MAX]; yading@11: int current_pid; yading@11: }; yading@11: yading@11: static const AVOption options[] = { yading@11: {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT, yading@11: {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM }, yading@11: { NULL }, yading@11: }; yading@11: yading@11: static const AVClass mpegtsraw_class = { yading@11: .class_name = "mpegtsraw demuxer", yading@11: .item_name = av_default_item_name, yading@11: .option = options, yading@11: .version = LIBAVUTIL_VERSION_INT, yading@11: }; yading@11: yading@11: /* TS stream handling */ yading@11: yading@11: enum MpegTSState { yading@11: MPEGTS_HEADER = 0, yading@11: MPEGTS_PESHEADER, yading@11: MPEGTS_PESHEADER_FILL, yading@11: MPEGTS_PAYLOAD, yading@11: MPEGTS_SKIP, yading@11: }; yading@11: yading@11: /* enough for PES header + length */ yading@11: #define PES_START_SIZE 6 yading@11: #define PES_HEADER_SIZE 9 yading@11: #define MAX_PES_HEADER_SIZE (9 + 255) yading@11: yading@11: typedef struct PESContext { yading@11: int pid; yading@11: int pcr_pid; /**< if -1 then all packets containing PCR are considered */ yading@11: int stream_type; yading@11: MpegTSContext *ts; yading@11: AVFormatContext *stream; yading@11: AVStream *st; yading@11: AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */ yading@11: enum MpegTSState state; yading@11: /* used to get the format */ yading@11: int data_index; yading@11: int flags; /**< copied to the AVPacket flags */ yading@11: int total_size; yading@11: int pes_header_size; yading@11: int extended_stream_id; yading@11: int64_t pts, dts; yading@11: int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */ yading@11: uint8_t header[MAX_PES_HEADER_SIZE]; yading@11: AVBufferRef *buffer; yading@11: SLConfigDescr sl; yading@11: } PESContext; yading@11: yading@11: extern AVInputFormat ff_mpegts_demuxer; yading@11: yading@11: static void clear_avprogram(MpegTSContext *ts, unsigned int programid) yading@11: { yading@11: AVProgram *prg = NULL; yading@11: int i; yading@11: for(i=0; istream->nb_programs; i++) yading@11: if(ts->stream->programs[i]->id == programid){ yading@11: prg = ts->stream->programs[i]; yading@11: break; yading@11: } yading@11: if (!prg) yading@11: return; yading@11: prg->nb_stream_indexes = 0; yading@11: } yading@11: yading@11: static void clear_program(MpegTSContext *ts, unsigned int programid) yading@11: { yading@11: int i; yading@11: yading@11: clear_avprogram(ts, programid); yading@11: for(i=0; inb_prg; i++) yading@11: if(ts->prg[i].id == programid) yading@11: ts->prg[i].nb_pids = 0; yading@11: } yading@11: yading@11: static void clear_programs(MpegTSContext *ts) yading@11: { yading@11: av_freep(&ts->prg); yading@11: ts->nb_prg=0; yading@11: } yading@11: yading@11: static void add_pat_entry(MpegTSContext *ts, unsigned int programid) yading@11: { yading@11: struct Program *p; yading@11: void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program)); yading@11: if(!tmp) yading@11: return; yading@11: ts->prg = tmp; yading@11: p = &ts->prg[ts->nb_prg]; yading@11: p->id = programid; yading@11: p->nb_pids = 0; yading@11: ts->nb_prg++; yading@11: } yading@11: yading@11: static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid) yading@11: { yading@11: int i; yading@11: struct Program *p = NULL; yading@11: for(i=0; inb_prg; i++) { yading@11: if(ts->prg[i].id == programid) { yading@11: p = &ts->prg[i]; yading@11: break; yading@11: } yading@11: } yading@11: if(!p) yading@11: return; yading@11: yading@11: if(p->nb_pids >= MAX_PIDS_PER_PROGRAM) yading@11: return; yading@11: p->pids[p->nb_pids++] = pid; yading@11: } yading@11: yading@11: static void set_pcr_pid(AVFormatContext *s, unsigned int programid, unsigned int pid) yading@11: { yading@11: int i; yading@11: for(i=0; inb_programs; i++) { yading@11: if(s->programs[i]->id == programid) { yading@11: s->programs[i]->pcr_pid = pid; yading@11: break; yading@11: } yading@11: } yading@11: } yading@11: yading@11: /** yading@11: * @brief discard_pid() decides if the pid is to be discarded according yading@11: * to caller's programs selection yading@11: * @param ts : - TS context yading@11: * @param pid : - pid yading@11: * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL yading@11: * 0 otherwise yading@11: */ yading@11: static int discard_pid(MpegTSContext *ts, unsigned int pid) yading@11: { yading@11: int i, j, k; yading@11: int used = 0, discarded = 0; yading@11: struct Program *p; yading@11: for(i=0; inb_prg; i++) { yading@11: p = &ts->prg[i]; yading@11: for(j=0; jnb_pids; j++) { yading@11: if(p->pids[j] != pid) yading@11: continue; yading@11: //is program with id p->id set to be discarded? yading@11: for(k=0; kstream->nb_programs; k++) { yading@11: if(ts->stream->programs[k]->id == p->id) { yading@11: if(ts->stream->programs[k]->discard == AVDISCARD_ALL) yading@11: discarded++; yading@11: else yading@11: used++; yading@11: } yading@11: } yading@11: } yading@11: } yading@11: yading@11: return !used && discarded; yading@11: } yading@11: yading@11: /** yading@11: * Assemble PES packets out of TS packets, and then call the "section_cb" yading@11: * function when they are complete. yading@11: */ yading@11: static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1, yading@11: const uint8_t *buf, int buf_size, int is_start) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: MpegTSSectionFilter *tss = &tss1->u.section_filter; yading@11: int len; yading@11: yading@11: if (is_start) { yading@11: memcpy(tss->section_buf, buf, buf_size); yading@11: tss->section_index = buf_size; yading@11: tss->section_h_size = -1; yading@11: tss->end_of_section_reached = 0; yading@11: } else { yading@11: if (tss->end_of_section_reached) yading@11: return; yading@11: len = 4096 - tss->section_index; yading@11: if (buf_size < len) yading@11: len = buf_size; yading@11: memcpy(tss->section_buf + tss->section_index, buf, len); yading@11: tss->section_index += len; yading@11: } yading@11: yading@11: /* compute section length if possible */ yading@11: if (tss->section_h_size == -1 && tss->section_index >= 3) { yading@11: len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3; yading@11: if (len > 4096) yading@11: return; yading@11: tss->section_h_size = len; yading@11: } yading@11: yading@11: if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) { yading@11: int crc_valid = 1; yading@11: tss->end_of_section_reached = 1; yading@11: yading@11: if (tss->check_crc){ yading@11: crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size); yading@11: if (crc_valid){ yading@11: ts->crc_validity[ tss1->pid ] = 100; yading@11: }else if(ts->crc_validity[ tss1->pid ] > -10){ yading@11: ts->crc_validity[ tss1->pid ]--; yading@11: }else yading@11: crc_valid = 2; yading@11: } yading@11: if (crc_valid) yading@11: tss->section_cb(tss1, tss->section_buf, tss->section_h_size); yading@11: } yading@11: } yading@11: yading@11: static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid, yading@11: SectionCallback *section_cb, void *opaque, yading@11: int check_crc) yading@11: yading@11: { yading@11: MpegTSFilter *filter; yading@11: MpegTSSectionFilter *sec; yading@11: yading@11: av_dlog(ts->stream, "Filter: pid=0x%x\n", pid); yading@11: yading@11: if (pid >= NB_PID_MAX || ts->pids[pid]) yading@11: return NULL; yading@11: filter = av_mallocz(sizeof(MpegTSFilter)); yading@11: if (!filter) yading@11: return NULL; yading@11: ts->pids[pid] = filter; yading@11: filter->type = MPEGTS_SECTION; yading@11: filter->pid = pid; yading@11: filter->es_id = -1; yading@11: filter->last_cc = -1; yading@11: sec = &filter->u.section_filter; yading@11: sec->section_cb = section_cb; yading@11: sec->opaque = opaque; yading@11: sec->section_buf = av_malloc(MAX_SECTION_SIZE); yading@11: sec->check_crc = check_crc; yading@11: if (!sec->section_buf) { yading@11: av_free(filter); yading@11: return NULL; yading@11: } yading@11: return filter; yading@11: } yading@11: yading@11: static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, yading@11: PESCallback *pes_cb, yading@11: void *opaque) yading@11: { yading@11: MpegTSFilter *filter; yading@11: MpegTSPESFilter *pes; yading@11: yading@11: if (pid >= NB_PID_MAX || ts->pids[pid]) yading@11: return NULL; yading@11: filter = av_mallocz(sizeof(MpegTSFilter)); yading@11: if (!filter) yading@11: return NULL; yading@11: ts->pids[pid] = filter; yading@11: filter->type = MPEGTS_PES; yading@11: filter->pid = pid; yading@11: filter->es_id = -1; yading@11: filter->last_cc = -1; yading@11: pes = &filter->u.pes_filter; yading@11: pes->pes_cb = pes_cb; yading@11: pes->opaque = opaque; yading@11: return filter; yading@11: } yading@11: yading@11: static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter) yading@11: { yading@11: int pid; yading@11: yading@11: pid = filter->pid; yading@11: if (filter->type == MPEGTS_SECTION) yading@11: av_freep(&filter->u.section_filter.section_buf); yading@11: else if (filter->type == MPEGTS_PES) { yading@11: PESContext *pes = filter->u.pes_filter.opaque; yading@11: av_buffer_unref(&pes->buffer); yading@11: /* referenced private data will be freed later in yading@11: * avformat_close_input */ yading@11: if (!((PESContext *)filter->u.pes_filter.opaque)->st) { yading@11: av_freep(&filter->u.pes_filter.opaque); yading@11: } yading@11: } yading@11: yading@11: av_free(filter); yading@11: ts->pids[pid] = NULL; yading@11: } yading@11: yading@11: static int analyze(const uint8_t *buf, int size, int packet_size, int *index){ yading@11: int stat[TS_MAX_PACKET_SIZE]; yading@11: int i; yading@11: int x=0; yading@11: int best_score=0; yading@11: yading@11: memset(stat, 0, packet_size*sizeof(int)); yading@11: yading@11: for(x=i=0; i best_score){ yading@11: best_score= stat[x]; yading@11: if(index) *index= x; yading@11: } yading@11: } yading@11: yading@11: x++; yading@11: if(x == packet_size) x= 0; yading@11: } yading@11: yading@11: return best_score; yading@11: } yading@11: yading@11: /* autodetect fec presence. Must have at least 1024 bytes */ yading@11: static int get_packet_size(const uint8_t *buf, int size) yading@11: { yading@11: int score, fec_score, dvhs_score; yading@11: yading@11: if (size < (TS_FEC_PACKET_SIZE * 5 + 1)) yading@11: return -1; yading@11: yading@11: score = analyze(buf, size, TS_PACKET_SIZE, NULL); yading@11: dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL); yading@11: fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL); yading@11: av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n", yading@11: score, dvhs_score, fec_score); yading@11: yading@11: if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE; yading@11: else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE; yading@11: else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE; yading@11: else return -1; yading@11: } yading@11: yading@11: typedef struct SectionHeader { yading@11: uint8_t tid; yading@11: uint16_t id; yading@11: uint8_t version; yading@11: uint8_t sec_num; yading@11: uint8_t last_sec_num; yading@11: } SectionHeader; yading@11: yading@11: static inline int get8(const uint8_t **pp, const uint8_t *p_end) yading@11: { yading@11: const uint8_t *p; yading@11: int c; yading@11: yading@11: p = *pp; yading@11: if (p >= p_end) yading@11: return -1; yading@11: c = *p++; yading@11: *pp = p; yading@11: return c; yading@11: } yading@11: yading@11: static inline int get16(const uint8_t **pp, const uint8_t *p_end) yading@11: { yading@11: const uint8_t *p; yading@11: int c; yading@11: yading@11: p = *pp; yading@11: if ((p + 1) >= p_end) yading@11: return -1; yading@11: c = AV_RB16(p); yading@11: p += 2; yading@11: *pp = p; yading@11: return c; yading@11: } yading@11: yading@11: /* read and allocate a DVB string preceded by its length */ yading@11: static char *getstr8(const uint8_t **pp, const uint8_t *p_end) yading@11: { yading@11: int len; yading@11: const uint8_t *p; yading@11: char *str; yading@11: yading@11: p = *pp; yading@11: len = get8(&p, p_end); yading@11: if (len < 0) yading@11: return NULL; yading@11: if ((p + len) > p_end) yading@11: return NULL; yading@11: str = av_malloc(len + 1); yading@11: if (!str) yading@11: return NULL; yading@11: memcpy(str, p, len); yading@11: str[len] = '\0'; yading@11: p += len; yading@11: *pp = p; yading@11: return str; yading@11: } yading@11: yading@11: static int parse_section_header(SectionHeader *h, yading@11: const uint8_t **pp, const uint8_t *p_end) yading@11: { yading@11: int val; yading@11: yading@11: val = get8(pp, p_end); yading@11: if (val < 0) yading@11: return -1; yading@11: h->tid = val; yading@11: *pp += 2; yading@11: val = get16(pp, p_end); yading@11: if (val < 0) yading@11: return -1; yading@11: h->id = val; yading@11: val = get8(pp, p_end); yading@11: if (val < 0) yading@11: return -1; yading@11: h->version = (val >> 1) & 0x1f; yading@11: val = get8(pp, p_end); yading@11: if (val < 0) yading@11: return -1; yading@11: h->sec_num = val; yading@11: val = get8(pp, p_end); yading@11: if (val < 0) yading@11: return -1; yading@11: h->last_sec_num = val; yading@11: return 0; yading@11: } yading@11: yading@11: typedef struct { yading@11: uint32_t stream_type; yading@11: enum AVMediaType codec_type; yading@11: enum AVCodecID codec_id; yading@11: } StreamType; yading@11: yading@11: static const StreamType ISO_types[] = { yading@11: { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO }, yading@11: { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO }, yading@11: { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 }, yading@11: { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 }, yading@11: { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC }, yading@11: { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 }, yading@11: /* Makito encoder sets stream type 0x11 for AAC, yading@11: * so auto-detect LOAS/LATM instead of hardcoding it. */ yading@11: #if !CONFIG_LOAS_DEMUXER yading@11: { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */ yading@11: #endif yading@11: { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 }, yading@11: { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS }, yading@11: { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC }, yading@11: { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 }, yading@11: { 0 }, yading@11: }; yading@11: yading@11: static const StreamType HDMV_types[] = { yading@11: { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY }, yading@11: { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, yading@11: { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, yading@11: { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD }, yading@11: { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, yading@11: { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */ yading@11: { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/ yading@11: { 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */ yading@11: { 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */ yading@11: { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE }, yading@11: { 0 }, yading@11: }; yading@11: yading@11: /* ATSC ? */ yading@11: static const StreamType MISC_types[] = { yading@11: { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, yading@11: { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, yading@11: { 0 }, yading@11: }; yading@11: yading@11: static const StreamType REGD_types[] = { yading@11: { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC }, yading@11: { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, yading@11: { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M }, yading@11: { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, yading@11: { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, yading@11: { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, yading@11: { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV }, yading@11: { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 }, yading@11: { 0 }, yading@11: }; yading@11: yading@11: /* descriptor present */ yading@11: static const StreamType DESC_types[] = { yading@11: { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */ yading@11: { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */ yading@11: { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, yading@11: { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT }, yading@11: { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */ yading@11: { 0 }, yading@11: }; yading@11: yading@11: static void mpegts_find_stream_type(AVStream *st, yading@11: uint32_t stream_type, const StreamType *types) yading@11: { yading@11: if (avcodec_is_open(st->codec)) { yading@11: av_log(NULL, AV_LOG_DEBUG, "cannot set stream info, codec is open\n"); yading@11: return; yading@11: } yading@11: yading@11: for (; types->stream_type; types++) { yading@11: if (stream_type == types->stream_type) { yading@11: st->codec->codec_type = types->codec_type; yading@11: st->codec->codec_id = types->codec_id; yading@11: st->request_probe = 0; yading@11: return; yading@11: } yading@11: } yading@11: } yading@11: yading@11: static int mpegts_set_stream_info(AVStream *st, PESContext *pes, yading@11: uint32_t stream_type, uint32_t prog_reg_desc) yading@11: { yading@11: int old_codec_type= st->codec->codec_type; yading@11: int old_codec_id = st->codec->codec_id; yading@11: yading@11: if (avcodec_is_open(st->codec)) { yading@11: av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, codec is open\n"); yading@11: return 0; yading@11: } yading@11: yading@11: avpriv_set_pts_info(st, 33, 1, 90000); yading@11: st->priv_data = pes; yading@11: st->codec->codec_type = AVMEDIA_TYPE_DATA; yading@11: st->codec->codec_id = AV_CODEC_ID_NONE; yading@11: st->need_parsing = AVSTREAM_PARSE_FULL; yading@11: pes->st = st; yading@11: pes->stream_type = stream_type; yading@11: yading@11: av_log(pes->stream, AV_LOG_DEBUG, yading@11: "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n", yading@11: st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc); yading@11: yading@11: st->codec->codec_tag = pes->stream_type; yading@11: yading@11: mpegts_find_stream_type(st, pes->stream_type, ISO_types); yading@11: if ((prog_reg_desc == AV_RL32("HDMV") || yading@11: prog_reg_desc == AV_RL32("HDPR")) && yading@11: st->codec->codec_id == AV_CODEC_ID_NONE) { yading@11: mpegts_find_stream_type(st, pes->stream_type, HDMV_types); yading@11: if (pes->stream_type == 0x83) { yading@11: // HDMV TrueHD streams also contain an AC3 coded version of the yading@11: // audio track - add a second stream for this yading@11: AVStream *sub_st; yading@11: // priv_data cannot be shared between streams yading@11: PESContext *sub_pes = av_malloc(sizeof(*sub_pes)); yading@11: if (!sub_pes) yading@11: return AVERROR(ENOMEM); yading@11: memcpy(sub_pes, pes, sizeof(*sub_pes)); yading@11: yading@11: sub_st = avformat_new_stream(pes->stream, NULL); yading@11: if (!sub_st) { yading@11: av_free(sub_pes); yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: yading@11: sub_st->id = pes->pid; yading@11: avpriv_set_pts_info(sub_st, 33, 1, 90000); yading@11: sub_st->priv_data = sub_pes; yading@11: sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: sub_st->codec->codec_id = AV_CODEC_ID_AC3; yading@11: sub_st->need_parsing = AVSTREAM_PARSE_FULL; yading@11: sub_pes->sub_st = pes->sub_st = sub_st; yading@11: } yading@11: } yading@11: if (st->codec->codec_id == AV_CODEC_ID_NONE) yading@11: mpegts_find_stream_type(st, pes->stream_type, MISC_types); yading@11: if (st->codec->codec_id == AV_CODEC_ID_NONE){ yading@11: st->codec->codec_id = old_codec_id; yading@11: st->codec->codec_type= old_codec_type; yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static void new_pes_packet(PESContext *pes, AVPacket *pkt) yading@11: { yading@11: av_init_packet(pkt); yading@11: yading@11: pkt->buf = pes->buffer; yading@11: pkt->data = pes->buffer->data; yading@11: pkt->size = pes->data_index; yading@11: yading@11: if(pes->total_size != MAX_PES_PAYLOAD && yading@11: pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) { yading@11: av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n"); yading@11: pes->flags |= AV_PKT_FLAG_CORRUPT; yading@11: } yading@11: memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); yading@11: yading@11: // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID yading@11: if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76) yading@11: pkt->stream_index = pes->sub_st->index; yading@11: else yading@11: pkt->stream_index = pes->st->index; yading@11: pkt->pts = pes->pts; yading@11: pkt->dts = pes->dts; yading@11: /* store position of first TS packet of this PES packet */ yading@11: pkt->pos = pes->ts_packet_pos; yading@11: pkt->flags = pes->flags; yading@11: yading@11: /* reset pts values */ yading@11: pes->pts = AV_NOPTS_VALUE; yading@11: pes->dts = AV_NOPTS_VALUE; yading@11: pes->buffer = NULL; yading@11: pes->data_index = 0; yading@11: pes->flags = 0; yading@11: } yading@11: yading@11: static uint64_t get_ts64(GetBitContext *gb, int bits) yading@11: { yading@11: if (get_bits_left(gb) < bits) yading@11: return AV_NOPTS_VALUE; yading@11: return get_bits64(gb, bits); yading@11: } yading@11: yading@11: static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size) yading@11: { yading@11: GetBitContext gb; yading@11: int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0; yading@11: int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0; yading@11: int dts_flag = -1, cts_flag = -1; yading@11: int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE; yading@11: yading@11: init_get_bits(&gb, buf, buf_size*8); yading@11: yading@11: if (sl->use_au_start) yading@11: au_start_flag = get_bits1(&gb); yading@11: if (sl->use_au_end) yading@11: au_end_flag = get_bits1(&gb); yading@11: if (!sl->use_au_start && !sl->use_au_end) yading@11: au_start_flag = au_end_flag = 1; yading@11: if (sl->ocr_len > 0) yading@11: ocr_flag = get_bits1(&gb); yading@11: if (sl->use_idle) yading@11: idle_flag = get_bits1(&gb); yading@11: if (sl->use_padding) yading@11: padding_flag = get_bits1(&gb); yading@11: if (padding_flag) yading@11: padding_bits = get_bits(&gb, 3); yading@11: yading@11: if (!idle_flag && (!padding_flag || padding_bits != 0)) { yading@11: if (sl->packet_seq_num_len) yading@11: skip_bits_long(&gb, sl->packet_seq_num_len); yading@11: if (sl->degr_prior_len) yading@11: if (get_bits1(&gb)) yading@11: skip_bits(&gb, sl->degr_prior_len); yading@11: if (ocr_flag) yading@11: skip_bits_long(&gb, sl->ocr_len); yading@11: if (au_start_flag) { yading@11: if (sl->use_rand_acc_pt) yading@11: get_bits1(&gb); yading@11: if (sl->au_seq_num_len > 0) yading@11: skip_bits_long(&gb, sl->au_seq_num_len); yading@11: if (sl->use_timestamps) { yading@11: dts_flag = get_bits1(&gb); yading@11: cts_flag = get_bits1(&gb); yading@11: } yading@11: } yading@11: if (sl->inst_bitrate_len) yading@11: inst_bitrate_flag = get_bits1(&gb); yading@11: if (dts_flag == 1) yading@11: dts = get_ts64(&gb, sl->timestamp_len); yading@11: if (cts_flag == 1) yading@11: cts = get_ts64(&gb, sl->timestamp_len); yading@11: if (sl->au_len > 0) yading@11: skip_bits_long(&gb, sl->au_len); yading@11: if (inst_bitrate_flag) yading@11: skip_bits_long(&gb, sl->inst_bitrate_len); yading@11: } yading@11: yading@11: if (dts != AV_NOPTS_VALUE) yading@11: pes->dts = dts; yading@11: if (cts != AV_NOPTS_VALUE) yading@11: pes->pts = cts; yading@11: yading@11: if (sl->timestamp_len && sl->timestamp_res) yading@11: avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res); yading@11: yading@11: return (get_bits_count(&gb) + 7) >> 3; yading@11: } yading@11: yading@11: /* return non zero if a packet could be constructed */ yading@11: static int mpegts_push_data(MpegTSFilter *filter, yading@11: const uint8_t *buf, int buf_size, int is_start, yading@11: int64_t pos) yading@11: { yading@11: PESContext *pes = filter->u.pes_filter.opaque; yading@11: MpegTSContext *ts = pes->ts; yading@11: const uint8_t *p; yading@11: int len, code; yading@11: yading@11: if(!ts->pkt) yading@11: return 0; yading@11: yading@11: if (is_start) { yading@11: if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { yading@11: new_pes_packet(pes, ts->pkt); yading@11: ts->stop_parse = 1; yading@11: } yading@11: pes->state = MPEGTS_HEADER; yading@11: pes->data_index = 0; yading@11: pes->ts_packet_pos = pos; yading@11: } yading@11: p = buf; yading@11: while (buf_size > 0) { yading@11: switch(pes->state) { yading@11: case MPEGTS_HEADER: yading@11: len = PES_START_SIZE - pes->data_index; yading@11: if (len > buf_size) yading@11: len = buf_size; yading@11: memcpy(pes->header + pes->data_index, p, len); yading@11: pes->data_index += len; yading@11: p += len; yading@11: buf_size -= len; yading@11: if (pes->data_index == PES_START_SIZE) { yading@11: /* we got all the PES or section header. We can now yading@11: decide */ yading@11: if (pes->header[0] == 0x00 && pes->header[1] == 0x00 && yading@11: pes->header[2] == 0x01) { yading@11: /* it must be an mpeg2 PES stream */ yading@11: code = pes->header[3] | 0x100; yading@11: av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code); yading@11: yading@11: if ((pes->st && pes->st->discard == AVDISCARD_ALL && yading@11: (!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) || yading@11: code == 0x1be) /* padding_stream */ yading@11: goto skip; yading@11: yading@11: /* stream not present in PMT */ yading@11: if (!pes->st) { yading@11: pes->st = avformat_new_stream(ts->stream, NULL); yading@11: if (!pes->st) yading@11: return AVERROR(ENOMEM); yading@11: pes->st->id = pes->pid; yading@11: mpegts_set_stream_info(pes->st, pes, 0, 0); yading@11: } yading@11: yading@11: pes->total_size = AV_RB16(pes->header + 4); yading@11: /* NOTE: a zero total size means the PES size is yading@11: unbounded */ yading@11: if (!pes->total_size) yading@11: pes->total_size = MAX_PES_PAYLOAD; yading@11: yading@11: /* allocate pes buffer */ yading@11: pes->buffer = av_buffer_alloc(pes->total_size + yading@11: FF_INPUT_BUFFER_PADDING_SIZE); yading@11: if (!pes->buffer) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */ yading@11: code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */ yading@11: code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */ yading@11: code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */ yading@11: pes->state = MPEGTS_PESHEADER; yading@11: if (pes->st->codec->codec_id == AV_CODEC_ID_NONE && !pes->st->request_probe) { yading@11: av_dlog(pes->stream, "pid=%x stream_type=%x probing\n", yading@11: pes->pid, pes->stream_type); yading@11: pes->st->request_probe= 1; yading@11: } yading@11: } else { yading@11: pes->state = MPEGTS_PAYLOAD; yading@11: pes->data_index = 0; yading@11: } yading@11: } else { yading@11: /* otherwise, it should be a table */ yading@11: /* skip packet */ yading@11: skip: yading@11: pes->state = MPEGTS_SKIP; yading@11: continue; yading@11: } yading@11: } yading@11: break; yading@11: /**********************************************/ yading@11: /* PES packing parsing */ yading@11: case MPEGTS_PESHEADER: yading@11: len = PES_HEADER_SIZE - pes->data_index; yading@11: if (len < 0) yading@11: return -1; yading@11: if (len > buf_size) yading@11: len = buf_size; yading@11: memcpy(pes->header + pes->data_index, p, len); yading@11: pes->data_index += len; yading@11: p += len; yading@11: buf_size -= len; yading@11: if (pes->data_index == PES_HEADER_SIZE) { yading@11: pes->pes_header_size = pes->header[8] + 9; yading@11: pes->state = MPEGTS_PESHEADER_FILL; yading@11: } yading@11: break; yading@11: case MPEGTS_PESHEADER_FILL: yading@11: len = pes->pes_header_size - pes->data_index; yading@11: if (len < 0) yading@11: return -1; yading@11: if (len > buf_size) yading@11: len = buf_size; yading@11: memcpy(pes->header + pes->data_index, p, len); yading@11: pes->data_index += len; yading@11: p += len; yading@11: buf_size -= len; yading@11: if (pes->data_index == pes->pes_header_size) { yading@11: const uint8_t *r; yading@11: unsigned int flags, pes_ext, skip; yading@11: yading@11: flags = pes->header[7]; yading@11: r = pes->header + 9; yading@11: pes->pts = AV_NOPTS_VALUE; yading@11: pes->dts = AV_NOPTS_VALUE; yading@11: if ((flags & 0xc0) == 0x80) { yading@11: pes->dts = pes->pts = ff_parse_pes_pts(r); yading@11: r += 5; yading@11: } else if ((flags & 0xc0) == 0xc0) { yading@11: pes->pts = ff_parse_pes_pts(r); yading@11: r += 5; yading@11: pes->dts = ff_parse_pes_pts(r); yading@11: r += 5; yading@11: } yading@11: pes->extended_stream_id = -1; yading@11: if (flags & 0x01) { /* PES extension */ yading@11: pes_ext = *r++; yading@11: /* Skip PES private data, program packet sequence counter and P-STD buffer */ yading@11: skip = (pes_ext >> 4) & 0xb; yading@11: skip += skip & 0x9; yading@11: r += skip; yading@11: if ((pes_ext & 0x41) == 0x01 && yading@11: (r + 2) <= (pes->header + pes->pes_header_size)) { yading@11: /* PES extension 2 */ yading@11: if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0) yading@11: pes->extended_stream_id = r[1]; yading@11: } yading@11: } yading@11: yading@11: /* we got the full header. We parse it and get the payload */ yading@11: pes->state = MPEGTS_PAYLOAD; yading@11: pes->data_index = 0; yading@11: if (pes->stream_type == 0x12 && buf_size > 0) { yading@11: int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size); yading@11: pes->pes_header_size += sl_header_bytes; yading@11: p += sl_header_bytes; yading@11: buf_size -= sl_header_bytes; yading@11: } yading@11: } yading@11: break; yading@11: case MPEGTS_PAYLOAD: yading@11: if (buf_size > 0 && pes->buffer) { yading@11: if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) { yading@11: new_pes_packet(pes, ts->pkt); yading@11: pes->total_size = MAX_PES_PAYLOAD; yading@11: pes->buffer = av_buffer_alloc(pes->total_size + FF_INPUT_BUFFER_PADDING_SIZE); yading@11: if (!pes->buffer) yading@11: return AVERROR(ENOMEM); yading@11: ts->stop_parse = 1; yading@11: } else if (pes->data_index == 0 && buf_size > pes->total_size) { yading@11: // pes packet size is < ts size packet and pes data is padded with 0xff yading@11: // not sure if this is legal in ts but see issue #2392 yading@11: buf_size = pes->total_size; yading@11: } yading@11: memcpy(pes->buffer->data + pes->data_index, p, buf_size); yading@11: pes->data_index += buf_size; yading@11: } yading@11: buf_size = 0; yading@11: /* emit complete packets with known packet size yading@11: * decreases demuxer delay for infrequent packets like subtitles from yading@11: * a couple of seconds to milliseconds for properly muxed files. yading@11: * total_size is the number of bytes following pes_packet_length yading@11: * in the pes header, i.e. not counting the first PES_START_SIZE bytes */ yading@11: if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD && yading@11: pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) { yading@11: ts->stop_parse = 1; yading@11: new_pes_packet(pes, ts->pkt); yading@11: } yading@11: break; yading@11: case MPEGTS_SKIP: yading@11: buf_size = 0; yading@11: break; yading@11: } yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid) yading@11: { yading@11: MpegTSFilter *tss; yading@11: PESContext *pes; yading@11: yading@11: /* if no pid found, then add a pid context */ yading@11: pes = av_mallocz(sizeof(PESContext)); yading@11: if (!pes) yading@11: return 0; yading@11: pes->ts = ts; yading@11: pes->stream = ts->stream; yading@11: pes->pid = pid; yading@11: pes->pcr_pid = pcr_pid; yading@11: pes->state = MPEGTS_SKIP; yading@11: pes->pts = AV_NOPTS_VALUE; yading@11: pes->dts = AV_NOPTS_VALUE; yading@11: tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes); yading@11: if (!tss) { yading@11: av_free(pes); yading@11: return 0; yading@11: } yading@11: return pes; yading@11: } yading@11: yading@11: #define MAX_LEVEL 4 yading@11: typedef struct { yading@11: AVFormatContext *s; yading@11: AVIOContext pb; yading@11: Mp4Descr *descr; yading@11: Mp4Descr *active_descr; yading@11: int descr_count; yading@11: int max_descr_count; yading@11: int level; yading@11: } MP4DescrParseContext; yading@11: yading@11: static int init_MP4DescrParseContext( yading@11: MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf, yading@11: unsigned size, Mp4Descr *descr, int max_descr_count) yading@11: { yading@11: int ret; yading@11: if (size > (1<<30)) yading@11: return AVERROR_INVALIDDATA; yading@11: yading@11: if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0, yading@11: NULL, NULL, NULL, NULL)) < 0) yading@11: return ret; yading@11: yading@11: d->s = s; yading@11: d->level = 0; yading@11: d->descr_count = 0; yading@11: d->descr = descr; yading@11: d->active_descr = NULL; yading@11: d->max_descr_count = max_descr_count; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static void update_offsets(AVIOContext *pb, int64_t *off, int *len) { yading@11: int64_t new_off = avio_tell(pb); yading@11: (*len) -= new_off - *off; yading@11: *off = new_off; yading@11: } yading@11: yading@11: static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, yading@11: int target_tag); yading@11: yading@11: static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len) yading@11: { yading@11: while (len > 0) { yading@11: if (parse_mp4_descr(d, off, len, 0) < 0) yading@11: return -1; yading@11: update_offsets(&d->pb, &off, &len); yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len) yading@11: { yading@11: avio_rb16(&d->pb); // ID yading@11: avio_r8(&d->pb); yading@11: avio_r8(&d->pb); yading@11: avio_r8(&d->pb); yading@11: avio_r8(&d->pb); yading@11: avio_r8(&d->pb); yading@11: update_offsets(&d->pb, &off, &len); yading@11: return parse_mp4_descr_arr(d, off, len); yading@11: } yading@11: yading@11: static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len) yading@11: { yading@11: int id_flags; yading@11: if (len < 2) yading@11: return 0; yading@11: id_flags = avio_rb16(&d->pb); yading@11: if (!(id_flags & 0x0020)) { //URL_Flag yading@11: update_offsets(&d->pb, &off, &len); yading@11: return parse_mp4_descr_arr(d, off, len); //ES_Descriptor[] yading@11: } else { yading@11: return 0; yading@11: } yading@11: } yading@11: yading@11: static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len) yading@11: { yading@11: int es_id = 0; yading@11: if (d->descr_count >= d->max_descr_count) yading@11: return -1; yading@11: ff_mp4_parse_es_descr(&d->pb, &es_id); yading@11: d->active_descr = d->descr + (d->descr_count++); yading@11: yading@11: d->active_descr->es_id = es_id; yading@11: update_offsets(&d->pb, &off, &len); yading@11: parse_mp4_descr(d, off, len, MP4DecConfigDescrTag); yading@11: update_offsets(&d->pb, &off, &len); yading@11: if (len > 0) yading@11: parse_mp4_descr(d, off, len, MP4SLDescrTag); yading@11: d->active_descr = NULL; yading@11: return 0; yading@11: } yading@11: yading@11: static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len) yading@11: { yading@11: Mp4Descr *descr = d->active_descr; yading@11: if (!descr) yading@11: return -1; yading@11: d->active_descr->dec_config_descr = av_malloc(len); yading@11: if (!descr->dec_config_descr) yading@11: return AVERROR(ENOMEM); yading@11: descr->dec_config_descr_len = len; yading@11: avio_read(&d->pb, descr->dec_config_descr, len); yading@11: return 0; yading@11: } yading@11: yading@11: static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len) yading@11: { yading@11: Mp4Descr *descr = d->active_descr; yading@11: int predefined; yading@11: if (!descr) yading@11: return -1; yading@11: yading@11: predefined = avio_r8(&d->pb); yading@11: if (!predefined) { yading@11: int lengths; yading@11: int flags = avio_r8(&d->pb); yading@11: descr->sl.use_au_start = !!(flags & 0x80); yading@11: descr->sl.use_au_end = !!(flags & 0x40); yading@11: descr->sl.use_rand_acc_pt = !!(flags & 0x20); yading@11: descr->sl.use_padding = !!(flags & 0x08); yading@11: descr->sl.use_timestamps = !!(flags & 0x04); yading@11: descr->sl.use_idle = !!(flags & 0x02); yading@11: descr->sl.timestamp_res = avio_rb32(&d->pb); yading@11: avio_rb32(&d->pb); yading@11: descr->sl.timestamp_len = avio_r8(&d->pb); yading@11: descr->sl.ocr_len = avio_r8(&d->pb); yading@11: descr->sl.au_len = avio_r8(&d->pb); yading@11: descr->sl.inst_bitrate_len = avio_r8(&d->pb); yading@11: lengths = avio_rb16(&d->pb); yading@11: descr->sl.degr_prior_len = lengths >> 12; yading@11: descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f; yading@11: descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f; yading@11: } else { yading@11: avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor"); yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, yading@11: int target_tag) { yading@11: int tag; yading@11: int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag); yading@11: update_offsets(&d->pb, &off, &len); yading@11: if (len < 0 || len1 > len || len1 <= 0) { yading@11: av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len); yading@11: return -1; yading@11: } yading@11: yading@11: if (d->level++ >= MAX_LEVEL) { yading@11: av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n"); yading@11: goto done; yading@11: } yading@11: yading@11: if (target_tag && tag != target_tag) { yading@11: av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag); yading@11: goto done; yading@11: } yading@11: yading@11: switch (tag) { yading@11: case MP4IODescrTag: yading@11: parse_MP4IODescrTag(d, off, len1); yading@11: break; yading@11: case MP4ODescrTag: yading@11: parse_MP4ODescrTag(d, off, len1); yading@11: break; yading@11: case MP4ESDescrTag: yading@11: parse_MP4ESDescrTag(d, off, len1); yading@11: break; yading@11: case MP4DecConfigDescrTag: yading@11: parse_MP4DecConfigDescrTag(d, off, len1); yading@11: break; yading@11: case MP4SLDescrTag: yading@11: parse_MP4SLDescrTag(d, off, len1); yading@11: break; yading@11: } yading@11: yading@11: done: yading@11: d->level--; yading@11: avio_seek(&d->pb, off + len1, SEEK_SET); yading@11: return 0; yading@11: } yading@11: yading@11: static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size, yading@11: Mp4Descr *descr, int *descr_count, int max_descr_count) yading@11: { yading@11: MP4DescrParseContext d; yading@11: if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0) yading@11: return -1; yading@11: yading@11: parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag); yading@11: yading@11: *descr_count = d.descr_count; yading@11: return 0; yading@11: } yading@11: yading@11: static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size, yading@11: Mp4Descr *descr, int *descr_count, int max_descr_count) yading@11: { yading@11: MP4DescrParseContext d; yading@11: if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0) yading@11: return -1; yading@11: yading@11: parse_mp4_descr_arr(&d, avio_tell(&d.pb), size); yading@11: yading@11: *descr_count = d.descr_count; yading@11: return 0; yading@11: } yading@11: yading@11: static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) yading@11: { yading@11: MpegTSContext *ts = filter->u.section_filter.opaque; yading@11: SectionHeader h; yading@11: const uint8_t *p, *p_end; yading@11: AVIOContext pb; yading@11: Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }}; yading@11: int mp4_descr_count = 0; yading@11: int i, pid; yading@11: AVFormatContext *s = ts->stream; yading@11: yading@11: p_end = section + section_len - 4; yading@11: p = section; yading@11: if (parse_section_header(&h, &p, p_end) < 0) yading@11: return; yading@11: if (h.tid != M4OD_TID) yading@11: return; yading@11: yading@11: mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT); yading@11: yading@11: for (pid = 0; pid < NB_PID_MAX; pid++) { yading@11: if (!ts->pids[pid]) yading@11: continue; yading@11: for (i = 0; i < mp4_descr_count; i++) { yading@11: PESContext *pes; yading@11: AVStream *st; yading@11: if (ts->pids[pid]->es_id != mp4_descr[i].es_id) yading@11: continue; yading@11: if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) { yading@11: av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid); yading@11: continue; yading@11: } yading@11: pes = ts->pids[pid]->u.pes_filter.opaque; yading@11: st = pes->st; yading@11: if (!st) { yading@11: continue; yading@11: } yading@11: yading@11: pes->sl = mp4_descr[i].sl; yading@11: yading@11: ffio_init_context(&pb, mp4_descr[i].dec_config_descr, yading@11: mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL); yading@11: ff_mp4_read_dec_config_descr(s, st, &pb); yading@11: if (st->codec->codec_id == AV_CODEC_ID_AAC && yading@11: st->codec->extradata_size > 0) yading@11: st->need_parsing = 0; yading@11: if (st->codec->codec_id == AV_CODEC_ID_H264 && yading@11: st->codec->extradata_size > 0) yading@11: st->need_parsing = 0; yading@11: yading@11: if (st->codec->codec_id <= AV_CODEC_ID_NONE) { yading@11: } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) { yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) { yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) { yading@11: st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; yading@11: } yading@11: } yading@11: } yading@11: for (i = 0; i < mp4_descr_count; i++) yading@11: av_free(mp4_descr[i].dec_config_descr); yading@11: } yading@11: yading@11: int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type, yading@11: const uint8_t **pp, const uint8_t *desc_list_end, yading@11: Mp4Descr *mp4_descr, int mp4_descr_count, int pid, yading@11: MpegTSContext *ts) yading@11: { yading@11: const uint8_t *desc_end; yading@11: int desc_len, desc_tag, desc_es_id; yading@11: char language[252]; yading@11: int i; yading@11: yading@11: desc_tag = get8(pp, desc_list_end); yading@11: if (desc_tag < 0) yading@11: return -1; yading@11: desc_len = get8(pp, desc_list_end); yading@11: if (desc_len < 0) yading@11: return -1; yading@11: desc_end = *pp + desc_len; yading@11: if (desc_end > desc_list_end) yading@11: return -1; yading@11: yading@11: av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len); yading@11: yading@11: if (st->codec->codec_id == AV_CODEC_ID_NONE && yading@11: stream_type == STREAM_TYPE_PRIVATE_DATA) yading@11: mpegts_find_stream_type(st, desc_tag, DESC_types); yading@11: yading@11: switch(desc_tag) { yading@11: case 0x1E: /* SL descriptor */ yading@11: desc_es_id = get16(pp, desc_end); yading@11: if (ts && ts->pids[pid]) yading@11: ts->pids[pid]->es_id = desc_es_id; yading@11: for (i = 0; i < mp4_descr_count; i++) yading@11: if (mp4_descr[i].dec_config_descr_len && yading@11: mp4_descr[i].es_id == desc_es_id) { yading@11: AVIOContext pb; yading@11: ffio_init_context(&pb, mp4_descr[i].dec_config_descr, yading@11: mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL); yading@11: ff_mp4_read_dec_config_descr(fc, st, &pb); yading@11: if (st->codec->codec_id == AV_CODEC_ID_AAC && yading@11: st->codec->extradata_size > 0) yading@11: st->need_parsing = 0; yading@11: if (st->codec->codec_id == AV_CODEC_ID_MPEG4SYSTEMS) yading@11: mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1); yading@11: } yading@11: break; yading@11: case 0x1F: /* FMC descriptor */ yading@11: get16(pp, desc_end); yading@11: if (mp4_descr_count > 0 && (st->codec->codec_id == AV_CODEC_ID_AAC_LATM || st->request_probe>0) && yading@11: mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) { yading@11: AVIOContext pb; yading@11: ffio_init_context(&pb, mp4_descr->dec_config_descr, yading@11: mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL); yading@11: ff_mp4_read_dec_config_descr(fc, st, &pb); yading@11: if (st->codec->codec_id == AV_CODEC_ID_AAC && yading@11: st->codec->extradata_size > 0){ yading@11: st->request_probe= st->need_parsing = 0; yading@11: st->codec->codec_type= AVMEDIA_TYPE_AUDIO; yading@11: } yading@11: } yading@11: break; yading@11: case 0x56: /* DVB teletext descriptor */ yading@11: language[0] = get8(pp, desc_end); yading@11: language[1] = get8(pp, desc_end); yading@11: language[2] = get8(pp, desc_end); yading@11: language[3] = 0; yading@11: av_dict_set(&st->metadata, "language", language, 0); yading@11: break; yading@11: case 0x59: /* subtitling descriptor */ yading@11: language[0] = get8(pp, desc_end); yading@11: language[1] = get8(pp, desc_end); yading@11: language[2] = get8(pp, desc_end); yading@11: language[3] = 0; yading@11: /* hearing impaired subtitles detection */ yading@11: switch(get8(pp, desc_end)) { yading@11: case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */ yading@11: case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */ yading@11: case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */ yading@11: case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */ yading@11: case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */ yading@11: case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */ yading@11: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; yading@11: break; yading@11: } yading@11: if (st->codec->extradata) { yading@11: if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4)) yading@11: avpriv_request_sample(fc, "DVB sub with multiple IDs"); yading@11: } else { yading@11: st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE); yading@11: if (st->codec->extradata) { yading@11: st->codec->extradata_size = 4; yading@11: memcpy(st->codec->extradata, *pp, 4); yading@11: } yading@11: } yading@11: *pp += 4; yading@11: av_dict_set(&st->metadata, "language", language, 0); yading@11: break; yading@11: case 0x0a: /* ISO 639 language descriptor */ yading@11: for (i = 0; i + 4 <= desc_len; i += 4) { yading@11: language[i + 0] = get8(pp, desc_end); yading@11: language[i + 1] = get8(pp, desc_end); yading@11: language[i + 2] = get8(pp, desc_end); yading@11: language[i + 3] = ','; yading@11: switch (get8(pp, desc_end)) { yading@11: case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break; yading@11: case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break; yading@11: case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break; yading@11: } yading@11: } yading@11: if (i) { yading@11: language[i - 1] = 0; yading@11: av_dict_set(&st->metadata, "language", language, 0); yading@11: } yading@11: break; yading@11: case 0x05: /* registration descriptor */ yading@11: st->codec->codec_tag = bytestream_get_le32(pp); yading@11: av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag); yading@11: if (st->codec->codec_id == AV_CODEC_ID_NONE) yading@11: mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types); yading@11: break; yading@11: case 0x52: /* stream identifier descriptor */ yading@11: st->stream_identifier = 1 + get8(pp, desc_end); yading@11: break; yading@11: default: yading@11: break; yading@11: } yading@11: *pp = desc_end; yading@11: return 0; yading@11: } yading@11: yading@11: static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) yading@11: { yading@11: MpegTSContext *ts = filter->u.section_filter.opaque; yading@11: SectionHeader h1, *h = &h1; yading@11: PESContext *pes; yading@11: AVStream *st; yading@11: const uint8_t *p, *p_end, *desc_list_end; yading@11: int program_info_length, pcr_pid, pid, stream_type; yading@11: int desc_list_len; yading@11: uint32_t prog_reg_desc = 0; /* registration descriptor */ yading@11: yading@11: Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }}; yading@11: int mp4_descr_count = 0; yading@11: int i; yading@11: yading@11: av_dlog(ts->stream, "PMT: len %i\n", section_len); yading@11: hex_dump_debug(ts->stream, section, section_len); yading@11: yading@11: p_end = section + section_len - 4; yading@11: p = section; yading@11: if (parse_section_header(h, &p, p_end) < 0) yading@11: return; yading@11: yading@11: av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n", yading@11: h->id, h->sec_num, h->last_sec_num); yading@11: yading@11: if (h->tid != PMT_TID) yading@11: return; yading@11: yading@11: clear_program(ts, h->id); yading@11: pcr_pid = get16(&p, p_end); yading@11: if (pcr_pid < 0) yading@11: return; yading@11: pcr_pid &= 0x1fff; yading@11: add_pid_to_pmt(ts, h->id, pcr_pid); yading@11: set_pcr_pid(ts->stream, h->id, pcr_pid); yading@11: yading@11: av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid); yading@11: yading@11: program_info_length = get16(&p, p_end); yading@11: if (program_info_length < 0) yading@11: return; yading@11: program_info_length &= 0xfff; yading@11: while(program_info_length >= 2) { yading@11: uint8_t tag, len; yading@11: tag = get8(&p, p_end); yading@11: len = get8(&p, p_end); yading@11: yading@11: av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len); yading@11: yading@11: if(len > program_info_length - 2) yading@11: //something else is broken, exit the program_descriptors_loop yading@11: break; yading@11: program_info_length -= len + 2; yading@11: if (tag == 0x1d) { // IOD descriptor yading@11: get8(&p, p_end); // scope yading@11: get8(&p, p_end); // label yading@11: len -= 2; yading@11: mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count, yading@11: &mp4_descr_count, MAX_MP4_DESCR_COUNT); yading@11: } else if (tag == 0x05 && len >= 4) { // registration descriptor yading@11: prog_reg_desc = bytestream_get_le32(&p); yading@11: len -= 4; yading@11: } yading@11: p += len; yading@11: } yading@11: p += program_info_length; yading@11: if (p >= p_end) yading@11: goto out; yading@11: yading@11: // stop parsing after pmt, we found header yading@11: if (!ts->stream->nb_streams) yading@11: ts->stop_parse = 2; yading@11: yading@11: for(;;) { yading@11: st = 0; yading@11: pes = NULL; yading@11: stream_type = get8(&p, p_end); yading@11: if (stream_type < 0) yading@11: break; yading@11: pid = get16(&p, p_end); yading@11: if (pid < 0) yading@11: break; yading@11: pid &= 0x1fff; yading@11: if (pid == ts->current_pid) yading@11: break; yading@11: yading@11: /* now create stream */ yading@11: if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) { yading@11: pes = ts->pids[pid]->u.pes_filter.opaque; yading@11: if (!pes->st) { yading@11: pes->st = avformat_new_stream(pes->stream, NULL); yading@11: if (!pes->st) yading@11: goto out; yading@11: pes->st->id = pes->pid; yading@11: } yading@11: st = pes->st; yading@11: } else if (stream_type != 0x13) { yading@11: if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably yading@11: pes = add_pes_stream(ts, pid, pcr_pid); yading@11: if (pes) { yading@11: st = avformat_new_stream(pes->stream, NULL); yading@11: if (!st) yading@11: goto out; yading@11: st->id = pes->pid; yading@11: } yading@11: } else { yading@11: int idx = ff_find_stream_index(ts->stream, pid); yading@11: if (idx >= 0) { yading@11: st = ts->stream->streams[idx]; yading@11: } else { yading@11: st = avformat_new_stream(ts->stream, NULL); yading@11: if (!st) yading@11: goto out; yading@11: st->id = pid; yading@11: st->codec->codec_type = AVMEDIA_TYPE_DATA; yading@11: } yading@11: } yading@11: yading@11: if (!st) yading@11: goto out; yading@11: yading@11: if (pes && !pes->stream_type) yading@11: mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc); yading@11: yading@11: add_pid_to_pmt(ts, h->id, pid); yading@11: yading@11: ff_program_add_stream_index(ts->stream, h->id, st->index); yading@11: yading@11: desc_list_len = get16(&p, p_end); yading@11: if (desc_list_len < 0) yading@11: break; yading@11: desc_list_len &= 0xfff; yading@11: desc_list_end = p + desc_list_len; yading@11: if (desc_list_end > p_end) yading@11: break; yading@11: for(;;) { yading@11: if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end, yading@11: mp4_descr, mp4_descr_count, pid, ts) < 0) yading@11: break; yading@11: yading@11: if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) { yading@11: ff_program_add_stream_index(ts->stream, h->id, pes->sub_st->index); yading@11: pes->sub_st->codec->codec_tag = st->codec->codec_tag; yading@11: } yading@11: } yading@11: p = desc_list_end; yading@11: } yading@11: yading@11: out: yading@11: for (i = 0; i < mp4_descr_count; i++) yading@11: av_free(mp4_descr[i].dec_config_descr); yading@11: } yading@11: yading@11: static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) yading@11: { yading@11: MpegTSContext *ts = filter->u.section_filter.opaque; yading@11: SectionHeader h1, *h = &h1; yading@11: const uint8_t *p, *p_end; yading@11: int sid, pmt_pid; yading@11: AVProgram *program; yading@11: yading@11: av_dlog(ts->stream, "PAT:\n"); yading@11: hex_dump_debug(ts->stream, section, section_len); yading@11: yading@11: p_end = section + section_len - 4; yading@11: p = section; yading@11: if (parse_section_header(h, &p, p_end) < 0) yading@11: return; yading@11: if (h->tid != PAT_TID) yading@11: return; yading@11: yading@11: ts->stream->ts_id = h->id; yading@11: yading@11: clear_programs(ts); yading@11: for(;;) { yading@11: sid = get16(&p, p_end); yading@11: if (sid < 0) yading@11: break; yading@11: pmt_pid = get16(&p, p_end); yading@11: if (pmt_pid < 0) yading@11: break; yading@11: pmt_pid &= 0x1fff; yading@11: yading@11: if (pmt_pid == ts->current_pid) yading@11: break; yading@11: yading@11: av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid); yading@11: yading@11: if (sid == 0x0000) { yading@11: /* NIT info */ yading@11: } else { yading@11: program = av_new_program(ts->stream, sid); yading@11: program->program_num = sid; yading@11: program->pmt_pid = pmt_pid; yading@11: if (ts->pids[pmt_pid]) yading@11: mpegts_close_filter(ts, ts->pids[pmt_pid]); yading@11: mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1); yading@11: add_pat_entry(ts, sid); yading@11: add_pid_to_pmt(ts, sid, 0); //add pat pid to program yading@11: add_pid_to_pmt(ts, sid, pmt_pid); yading@11: } yading@11: } yading@11: yading@11: if (sid < 0) { yading@11: int i,j; yading@11: for (j=0; jstream->nb_programs; j++) { yading@11: for (i=0; inb_prg; i++) yading@11: if (ts->prg[i].id == ts->stream->programs[j]->id) yading@11: break; yading@11: if (i==ts->nb_prg) yading@11: clear_avprogram(ts, ts->stream->programs[j]->id); yading@11: } yading@11: } yading@11: } yading@11: yading@11: static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len) yading@11: { yading@11: MpegTSContext *ts = filter->u.section_filter.opaque; yading@11: SectionHeader h1, *h = &h1; yading@11: const uint8_t *p, *p_end, *desc_list_end, *desc_end; yading@11: int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type; yading@11: char *name, *provider_name; yading@11: yading@11: av_dlog(ts->stream, "SDT:\n"); yading@11: hex_dump_debug(ts->stream, section, section_len); yading@11: yading@11: p_end = section + section_len - 4; yading@11: p = section; yading@11: if (parse_section_header(h, &p, p_end) < 0) yading@11: return; yading@11: if (h->tid != SDT_TID) yading@11: return; yading@11: onid = get16(&p, p_end); yading@11: if (onid < 0) yading@11: return; yading@11: val = get8(&p, p_end); yading@11: if (val < 0) yading@11: return; yading@11: for(;;) { yading@11: sid = get16(&p, p_end); yading@11: if (sid < 0) yading@11: break; yading@11: val = get8(&p, p_end); yading@11: if (val < 0) yading@11: break; yading@11: desc_list_len = get16(&p, p_end); yading@11: if (desc_list_len < 0) yading@11: break; yading@11: desc_list_len &= 0xfff; yading@11: desc_list_end = p + desc_list_len; yading@11: if (desc_list_end > p_end) yading@11: break; yading@11: for(;;) { yading@11: desc_tag = get8(&p, desc_list_end); yading@11: if (desc_tag < 0) yading@11: break; yading@11: desc_len = get8(&p, desc_list_end); yading@11: desc_end = p + desc_len; yading@11: if (desc_end > desc_list_end) yading@11: break; yading@11: yading@11: av_dlog(ts->stream, "tag: 0x%02x len=%d\n", yading@11: desc_tag, desc_len); yading@11: yading@11: switch(desc_tag) { yading@11: case 0x48: yading@11: service_type = get8(&p, p_end); yading@11: if (service_type < 0) yading@11: break; yading@11: provider_name = getstr8(&p, p_end); yading@11: if (!provider_name) yading@11: break; yading@11: name = getstr8(&p, p_end); yading@11: if (name) { yading@11: AVProgram *program = av_new_program(ts->stream, sid); yading@11: if(program) { yading@11: av_dict_set(&program->metadata, "service_name", name, 0); yading@11: av_dict_set(&program->metadata, "service_provider", provider_name, 0); yading@11: } yading@11: } yading@11: av_free(name); yading@11: av_free(provider_name); yading@11: break; yading@11: default: yading@11: break; yading@11: } yading@11: p = desc_end; yading@11: } yading@11: p = desc_list_end; yading@11: } yading@11: } yading@11: yading@11: /* handle one TS packet */ yading@11: static int handle_packet(MpegTSContext *ts, const uint8_t *packet) yading@11: { yading@11: AVFormatContext *s = ts->stream; yading@11: MpegTSFilter *tss; yading@11: int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity, yading@11: has_adaptation, has_payload; yading@11: const uint8_t *p, *p_end; yading@11: int64_t pos; yading@11: yading@11: pid = AV_RB16(packet + 1) & 0x1fff; yading@11: if(pid && discard_pid(ts, pid)) yading@11: return 0; yading@11: is_start = packet[1] & 0x40; yading@11: tss = ts->pids[pid]; yading@11: if (ts->auto_guess && tss == NULL && is_start) { yading@11: add_pes_stream(ts, pid, -1); yading@11: tss = ts->pids[pid]; yading@11: } yading@11: if (!tss) yading@11: return 0; yading@11: ts->current_pid = pid; yading@11: yading@11: afc = (packet[3] >> 4) & 3; yading@11: if (afc == 0) /* reserved value */ yading@11: return 0; yading@11: has_adaptation = afc & 2; yading@11: has_payload = afc & 1; yading@11: is_discontinuity = has_adaptation yading@11: && packet[4] != 0 /* with length > 0 */ yading@11: && (packet[5] & 0x80); /* and discontinuity indicated */ yading@11: yading@11: /* continuity check (currently not used) */ yading@11: cc = (packet[3] & 0xf); yading@11: expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc; yading@11: cc_ok = pid == 0x1FFF // null packet PID yading@11: || is_discontinuity yading@11: || tss->last_cc < 0 yading@11: || expected_cc == cc; yading@11: yading@11: tss->last_cc = cc; yading@11: if (!cc_ok) { yading@11: av_log(ts->stream, AV_LOG_DEBUG, yading@11: "Continuity check failed for pid %d expected %d got %d\n", yading@11: pid, expected_cc, cc); yading@11: if(tss->type == MPEGTS_PES) { yading@11: PESContext *pc = tss->u.pes_filter.opaque; yading@11: pc->flags |= AV_PKT_FLAG_CORRUPT; yading@11: } yading@11: } yading@11: yading@11: if (!has_payload) yading@11: return 0; yading@11: p = packet + 4; yading@11: if (has_adaptation) { yading@11: /* skip adaptation field */ yading@11: p += p[0] + 1; yading@11: } yading@11: /* if past the end of packet, ignore */ yading@11: p_end = packet + TS_PACKET_SIZE; yading@11: if (p >= p_end) yading@11: return 0; yading@11: yading@11: pos = avio_tell(ts->stream->pb); yading@11: ts->pos47= pos % ts->raw_packet_size; yading@11: yading@11: if (tss->type == MPEGTS_SECTION) { yading@11: if (is_start) { yading@11: /* pointer field present */ yading@11: len = *p++; yading@11: if (p + len > p_end) yading@11: return 0; yading@11: if (len && cc_ok) { yading@11: /* write remaining section bytes */ yading@11: write_section_data(s, tss, yading@11: p, len, 0); yading@11: /* check whether filter has been closed */ yading@11: if (!ts->pids[pid]) yading@11: return 0; yading@11: } yading@11: p += len; yading@11: if (p < p_end) { yading@11: write_section_data(s, tss, yading@11: p, p_end - p, 1); yading@11: } yading@11: } else { yading@11: if (cc_ok) { yading@11: write_section_data(s, tss, yading@11: p, p_end - p, 0); yading@11: } yading@11: } yading@11: } else { yading@11: int ret; yading@11: // Note: The position here points actually behind the current packet. yading@11: if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start, yading@11: pos - ts->raw_packet_size)) < 0) yading@11: return ret; yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: /* XXX: try to find a better synchro over several packets (use yading@11: get_packet_size() ?) */ yading@11: static int mpegts_resync(AVFormatContext *s) yading@11: { yading@11: AVIOContext *pb = s->pb; yading@11: int c, i; yading@11: yading@11: for(i = 0;i < MAX_RESYNC_SIZE; i++) { yading@11: c = avio_r8(pb); yading@11: if (url_feof(pb)) yading@11: return -1; yading@11: if (c == 0x47) { yading@11: avio_seek(pb, -1, SEEK_CUR); yading@11: return 0; yading@11: } yading@11: } yading@11: av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n"); yading@11: /* no sync found */ yading@11: return -1; yading@11: } yading@11: yading@11: /* return -1 if error or EOF. Return 0 if OK. */ yading@11: static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size) yading@11: { yading@11: AVIOContext *pb = s->pb; yading@11: int skip, len; yading@11: yading@11: for(;;) { yading@11: len = avio_read(pb, buf, TS_PACKET_SIZE); yading@11: if (len != TS_PACKET_SIZE) yading@11: return len < 0 ? len : AVERROR_EOF; yading@11: /* check packet sync byte */ yading@11: if (buf[0] != 0x47) { yading@11: /* find a new packet start */ yading@11: avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR); yading@11: if (mpegts_resync(s) < 0) yading@11: return AVERROR(EAGAIN); yading@11: else yading@11: continue; yading@11: } else { yading@11: skip = raw_packet_size - TS_PACKET_SIZE; yading@11: if (skip > 0) yading@11: avio_skip(pb, skip); yading@11: break; yading@11: } yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static int handle_packets(MpegTSContext *ts, int nb_packets) yading@11: { yading@11: AVFormatContext *s = ts->stream; yading@11: uint8_t packet[TS_PACKET_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; yading@11: int packet_num, ret = 0; yading@11: yading@11: if (avio_tell(s->pb) != ts->last_pos) { yading@11: int i; yading@11: av_dlog(ts->stream, "Skipping after seek\n"); yading@11: /* seek detected, flush pes buffer */ yading@11: for (i = 0; i < NB_PID_MAX; i++) { yading@11: if (ts->pids[i]) { yading@11: if (ts->pids[i]->type == MPEGTS_PES) { yading@11: PESContext *pes = ts->pids[i]->u.pes_filter.opaque; yading@11: av_buffer_unref(&pes->buffer); yading@11: pes->data_index = 0; yading@11: pes->state = MPEGTS_SKIP; /* skip until pes header */ yading@11: } yading@11: ts->pids[i]->last_cc = -1; yading@11: } yading@11: } yading@11: } yading@11: yading@11: ts->stop_parse = 0; yading@11: packet_num = 0; yading@11: memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE); yading@11: for(;;) { yading@11: packet_num++; yading@11: if (nb_packets != 0 && packet_num >= nb_packets || yading@11: ts->stop_parse > 1) { yading@11: ret = AVERROR(EAGAIN); yading@11: break; yading@11: } yading@11: if (ts->stop_parse > 0) yading@11: break; yading@11: yading@11: ret = read_packet(s, packet, ts->raw_packet_size); yading@11: if (ret != 0) yading@11: break; yading@11: ret = handle_packet(ts, packet); yading@11: if (ret != 0) yading@11: break; yading@11: } yading@11: ts->last_pos = avio_tell(s->pb); yading@11: return ret; yading@11: } yading@11: yading@11: static int mpegts_probe(AVProbeData *p) yading@11: { yading@11: const int size= p->buf_size; yading@11: int maxscore=0; yading@11: int sumscore=0; yading@11: int i; yading@11: int check_count= size / TS_FEC_PACKET_SIZE; yading@11: #define CHECK_COUNT 10 yading@11: #define CHECK_BLOCK 100 yading@11: yading@11: if (check_count < CHECK_COUNT) yading@11: return -1; yading@11: yading@11: for (i=0; ibuf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , NULL); yading@11: int dvhs_score= analyze(p->buf + TS_DVHS_PACKET_SIZE*i, TS_DVHS_PACKET_SIZE*left, TS_DVHS_PACKET_SIZE, NULL); yading@11: int fec_score = analyze(p->buf + TS_FEC_PACKET_SIZE *i, TS_FEC_PACKET_SIZE *left, TS_FEC_PACKET_SIZE , NULL); yading@11: score = FFMAX3(score, dvhs_score, fec_score); yading@11: sumscore += score; yading@11: maxscore = FFMAX(maxscore, score); yading@11: } yading@11: yading@11: sumscore = sumscore*CHECK_COUNT/check_count; yading@11: maxscore = maxscore*CHECK_COUNT/CHECK_BLOCK; yading@11: yading@11: av_dlog(0, "TS score: %d %d\n", sumscore, maxscore); yading@11: yading@11: if (sumscore > 6) return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT; yading@11: else if (maxscore > 6) return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT; yading@11: else return -1; yading@11: } yading@11: yading@11: /* return the 90kHz PCR and the extension for the 27MHz PCR. return yading@11: (-1) if not available */ yading@11: static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, yading@11: const uint8_t *packet) yading@11: { yading@11: int afc, len, flags; yading@11: const uint8_t *p; yading@11: unsigned int v; yading@11: yading@11: afc = (packet[3] >> 4) & 3; yading@11: if (afc <= 1) yading@11: return -1; yading@11: p = packet + 4; yading@11: len = p[0]; yading@11: p++; yading@11: if (len == 0) yading@11: return -1; yading@11: flags = *p++; yading@11: len--; yading@11: if (!(flags & 0x10)) yading@11: return -1; yading@11: if (len < 6) yading@11: return -1; yading@11: v = AV_RB32(p); yading@11: *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7); yading@11: *ppcr_low = ((p[4] & 1) << 8) | p[5]; yading@11: return 0; yading@11: } yading@11: yading@11: static int mpegts_read_header(AVFormatContext *s) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: AVIOContext *pb = s->pb; yading@11: uint8_t buf[8*1024]={0}; yading@11: int len; yading@11: int64_t pos; yading@11: yading@11: /* read the first 8192 bytes to get packet size */ yading@11: pos = avio_tell(pb); yading@11: len = avio_read(pb, buf, sizeof(buf)); yading@11: ts->raw_packet_size = get_packet_size(buf, len); yading@11: if (ts->raw_packet_size <= 0) { yading@11: av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n"); yading@11: ts->raw_packet_size = TS_PACKET_SIZE; yading@11: } yading@11: ts->stream = s; yading@11: ts->auto_guess = 0; yading@11: yading@11: if (s->iformat == &ff_mpegts_demuxer) { yading@11: /* normal demux */ yading@11: yading@11: /* first do a scan to get all the services */ yading@11: /* NOTE: We attempt to seek on non-seekable files as well, as the yading@11: * probe buffer usually is big enough. Only warn if the seek failed yading@11: * on files where the seek should work. */ yading@11: if (avio_seek(pb, pos, SEEK_SET) < 0) yading@11: av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n"); yading@11: yading@11: mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1); yading@11: yading@11: mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); yading@11: yading@11: handle_packets(ts, s->probesize / ts->raw_packet_size); yading@11: /* if could not find service, enable auto_guess */ yading@11: yading@11: ts->auto_guess = 1; yading@11: yading@11: av_dlog(ts->stream, "tuning done\n"); yading@11: yading@11: s->ctx_flags |= AVFMTCTX_NOHEADER; yading@11: } else { yading@11: AVStream *st; yading@11: int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l; yading@11: int64_t pcrs[2], pcr_h; yading@11: int packet_count[2]; yading@11: uint8_t packet[TS_PACKET_SIZE]; yading@11: yading@11: /* only read packets */ yading@11: yading@11: st = avformat_new_stream(s, NULL); yading@11: if (!st) yading@11: goto fail; yading@11: avpriv_set_pts_info(st, 60, 1, 27000000); yading@11: st->codec->codec_type = AVMEDIA_TYPE_DATA; yading@11: st->codec->codec_id = AV_CODEC_ID_MPEG2TS; yading@11: yading@11: /* we iterate until we find two PCRs to estimate the bitrate */ yading@11: pcr_pid = -1; yading@11: nb_pcrs = 0; yading@11: nb_packets = 0; yading@11: for(;;) { yading@11: ret = read_packet(s, packet, ts->raw_packet_size); yading@11: if (ret < 0) yading@11: return -1; yading@11: pid = AV_RB16(packet + 1) & 0x1fff; yading@11: if ((pcr_pid == -1 || pcr_pid == pid) && yading@11: parse_pcr(&pcr_h, &pcr_l, packet) == 0) { yading@11: pcr_pid = pid; yading@11: packet_count[nb_pcrs] = nb_packets; yading@11: pcrs[nb_pcrs] = pcr_h * 300 + pcr_l; yading@11: nb_pcrs++; yading@11: if (nb_pcrs >= 2) yading@11: break; yading@11: } yading@11: nb_packets++; yading@11: } yading@11: yading@11: /* NOTE1: the bitrate is computed without the FEC */ yading@11: /* NOTE2: it is only the bitrate of the start of the stream */ yading@11: ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]); yading@11: ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0]; yading@11: s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr; yading@11: st->codec->bit_rate = s->bit_rate; yading@11: st->start_time = ts->cur_pcr; yading@11: av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n", yading@11: st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr); yading@11: } yading@11: yading@11: avio_seek(pb, pos, SEEK_SET); yading@11: return 0; yading@11: fail: yading@11: return -1; yading@11: } yading@11: yading@11: #define MAX_PACKET_READAHEAD ((128 * 1024) / 188) yading@11: yading@11: static int mpegts_raw_read_packet(AVFormatContext *s, yading@11: AVPacket *pkt) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: int ret, i; yading@11: int64_t pcr_h, next_pcr_h, pos; yading@11: int pcr_l, next_pcr_l; yading@11: uint8_t pcr_buf[12]; yading@11: yading@11: if (av_new_packet(pkt, TS_PACKET_SIZE) < 0) yading@11: return AVERROR(ENOMEM); yading@11: pkt->pos= avio_tell(s->pb); yading@11: ret = read_packet(s, pkt->data, ts->raw_packet_size); yading@11: if (ret < 0) { yading@11: av_free_packet(pkt); yading@11: return ret; yading@11: } yading@11: if (ts->mpeg2ts_compute_pcr) { yading@11: /* compute exact PCR for each packet */ yading@11: if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) { yading@11: /* we read the next PCR (XXX: optimize it by using a bigger buffer */ yading@11: pos = avio_tell(s->pb); yading@11: for(i = 0; i < MAX_PACKET_READAHEAD; i++) { yading@11: avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET); yading@11: avio_read(s->pb, pcr_buf, 12); yading@11: if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) { yading@11: /* XXX: not precise enough */ yading@11: ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) / yading@11: (i + 1); yading@11: break; yading@11: } yading@11: } yading@11: avio_seek(s->pb, pos, SEEK_SET); yading@11: /* no next PCR found: we use previous increment */ yading@11: ts->cur_pcr = pcr_h * 300 + pcr_l; yading@11: } yading@11: pkt->pts = ts->cur_pcr; yading@11: pkt->duration = ts->pcr_incr; yading@11: ts->cur_pcr += ts->pcr_incr; yading@11: } yading@11: pkt->stream_index = 0; yading@11: return 0; yading@11: } yading@11: yading@11: static int mpegts_read_packet(AVFormatContext *s, yading@11: AVPacket *pkt) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: int ret, i; yading@11: yading@11: pkt->size = -1; yading@11: ts->pkt = pkt; yading@11: ret = handle_packets(ts, 0); yading@11: if (ret < 0) { yading@11: av_free_packet(ts->pkt); yading@11: /* flush pes data left */ yading@11: for (i = 0; i < NB_PID_MAX; i++) { yading@11: if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) { yading@11: PESContext *pes = ts->pids[i]->u.pes_filter.opaque; yading@11: if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { yading@11: new_pes_packet(pes, pkt); yading@11: pes->state = MPEGTS_SKIP; yading@11: ret = 0; yading@11: break; yading@11: } yading@11: } yading@11: } yading@11: } yading@11: yading@11: if (!ret && pkt->size < 0) yading@11: ret = AVERROR(EINTR); yading@11: return ret; yading@11: } yading@11: yading@11: static void mpegts_free(MpegTSContext *ts) yading@11: { yading@11: int i; yading@11: yading@11: clear_programs(ts); yading@11: yading@11: for(i=0;ipids[i]) mpegts_close_filter(ts, ts->pids[i]); yading@11: } yading@11: yading@11: static int mpegts_read_close(AVFormatContext *s) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: mpegts_free(ts); yading@11: return 0; yading@11: } yading@11: yading@11: static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index, yading@11: int64_t *ppos, int64_t pos_limit) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: int64_t pos, timestamp; yading@11: uint8_t buf[TS_PACKET_SIZE]; yading@11: int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid; yading@11: pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47; yading@11: while(pos < pos_limit) { yading@11: if (avio_seek(s->pb, pos, SEEK_SET) < 0) yading@11: return AV_NOPTS_VALUE; yading@11: if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE) yading@11: return AV_NOPTS_VALUE; yading@11: if (buf[0] != 0x47) { yading@11: if (mpegts_resync(s) < 0) yading@11: return AV_NOPTS_VALUE; yading@11: pos = avio_tell(s->pb); yading@11: continue; yading@11: } yading@11: if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) && yading@11: parse_pcr(×tamp, &pcr_l, buf) == 0) { yading@11: *ppos = pos; yading@11: return timestamp; yading@11: } yading@11: pos += ts->raw_packet_size; yading@11: } yading@11: yading@11: return AV_NOPTS_VALUE; yading@11: } yading@11: yading@11: static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index, yading@11: int64_t *ppos, int64_t pos_limit) yading@11: { yading@11: MpegTSContext *ts = s->priv_data; yading@11: int64_t pos; yading@11: pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47; yading@11: ff_read_frame_flush(s); yading@11: if (avio_seek(s->pb, pos, SEEK_SET) < 0) yading@11: return AV_NOPTS_VALUE; yading@11: while(pos < pos_limit) { yading@11: int ret; yading@11: AVPacket pkt; yading@11: av_init_packet(&pkt); yading@11: ret= av_read_frame(s, &pkt); yading@11: if(ret < 0) yading@11: return AV_NOPTS_VALUE; yading@11: av_free_packet(&pkt); yading@11: if(pkt.dts != AV_NOPTS_VALUE && pkt.pos >= 0){ yading@11: ff_reduce_index(s, pkt.stream_index); yading@11: av_add_index_entry(s->streams[pkt.stream_index], pkt.pos, pkt.dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */); yading@11: if(pkt.stream_index == stream_index){ yading@11: *ppos= pkt.pos; yading@11: return pkt.dts; yading@11: } yading@11: } yading@11: pos = pkt.pos; yading@11: } yading@11: yading@11: return AV_NOPTS_VALUE; yading@11: } yading@11: yading@11: /**************************************************************/ yading@11: /* parsing functions - called from other demuxers such as RTP */ yading@11: yading@11: MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s) yading@11: { yading@11: MpegTSContext *ts; yading@11: yading@11: ts = av_mallocz(sizeof(MpegTSContext)); yading@11: if (!ts) yading@11: return NULL; yading@11: /* no stream case, currently used by RTP */ yading@11: ts->raw_packet_size = TS_PACKET_SIZE; yading@11: ts->stream = s; yading@11: ts->auto_guess = 1; yading@11: mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1); yading@11: mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); yading@11: yading@11: return ts; yading@11: } yading@11: yading@11: /* return the consumed length if a packet was output, or -1 if no yading@11: packet is output */ yading@11: int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt, yading@11: const uint8_t *buf, int len) yading@11: { yading@11: int len1; yading@11: yading@11: len1 = len; yading@11: ts->pkt = pkt; yading@11: for(;;) { yading@11: ts->stop_parse = 0; yading@11: if (len < TS_PACKET_SIZE) yading@11: return -1; yading@11: if (buf[0] != 0x47) { yading@11: buf++; yading@11: len--; yading@11: } else { yading@11: handle_packet(ts, buf); yading@11: buf += TS_PACKET_SIZE; yading@11: len -= TS_PACKET_SIZE; yading@11: if (ts->stop_parse == 1) yading@11: break; yading@11: } yading@11: } yading@11: return len1 - len; yading@11: } yading@11: yading@11: void ff_mpegts_parse_close(MpegTSContext *ts) yading@11: { yading@11: mpegts_free(ts); yading@11: av_free(ts); yading@11: } yading@11: yading@11: AVInputFormat ff_mpegts_demuxer = { yading@11: .name = "mpegts", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"), yading@11: .priv_data_size = sizeof(MpegTSContext), yading@11: .read_probe = mpegts_probe, yading@11: .read_header = mpegts_read_header, yading@11: .read_packet = mpegts_read_packet, yading@11: .read_close = mpegts_read_close, yading@11: .read_timestamp = mpegts_get_dts, yading@11: .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT, yading@11: }; yading@11: yading@11: AVInputFormat ff_mpegtsraw_demuxer = { yading@11: .name = "mpegtsraw", yading@11: .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"), yading@11: .priv_data_size = sizeof(MpegTSContext), yading@11: .read_header = mpegts_read_header, yading@11: .read_packet = mpegts_raw_read_packet, yading@11: .read_close = mpegts_read_close, yading@11: .read_timestamp = mpegts_get_dts, yading@11: .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT, yading@11: .priv_class = &mpegtsraw_class, yading@11: };