Ticket #6753: dvb_subtiles_workaround.patch

File dvb_subtiles_workaround.patch, 49.1 KB (added by anonymous, 3 years ago)

A temporary solution is to revert to an older version of mpegts.{h,c}. Attaced patch works for me.

  • libs/libavformat/mpegts.c

     
    11/* 
    22 * MPEG2 transport stream (aka DVB) demuxer 
    3  * Copyright (c) 2002-2003 Fabrice Bellard 
     3 * Copyright (c) 2002-2003 Fabrice Bellard. 
    44 * 
    55 * This file is part of FFmpeg. 
    66 * 
     
    1919 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
    2020 */ 
    2121 
    22 //#define DEBUG 
    23  
    2422#include "libavutil/crc.h" 
    25 #include "libavutil/intreadwrite.h" 
    26 #include "libavcodec/bytestream.h" 
    2723#include "avformat.h" 
    28 #include "libmythdb/compat.h"  // for uint on MinGW (Windows) 
    2924#include <pthread.h> 
     25#include "libmythdb/compat.h" 
    3026#include "mpegts.h" 
    31 #include "internal.h" 
    3227 
     28//#define DEBUG_SI 
     29//#define DEBUG_SEEK 
     30 
    3331/* 1.0 second at 24Mbit/s */ 
    3432#define MAX_SCAN_PACKETS 32000 
    3533 
     
    3735   synchronisation is lost */ 
    3836#define MAX_RESYNC_SIZE 4096 
    3937 
    40 #define MAX_PES_PAYLOAD 200*1024 
    4138#define PMT_NOT_YET_FOUND 0 
    4239#define PMT_NOT_IN_PAT    1 
    4340#define PMT_FOUND         2 
     
    4542typedef struct PESContext PESContext; 
    4643typedef struct SectionContext SectionContext; 
    4744 
    48 static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type); 
     45static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int stream_type); 
     46static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code); 
    4947static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code); 
    5048static SectionContext *add_section_stream(MpegTSContext *ts, int pid, int stream_type); 
    5149static void mpegts_cleanup_streams(MpegTSContext *ts); 
    5250static int is_desired_stream(int type); 
    5351static int find_in_list(const int *pids, int pid); 
    54 static void mpegts_push_section(void *opaque, const uint8_t *section, int section_len); 
    5552 
    5653enum MpegTSFilterType { 
    5754    MPEGTS_PES, 
     
    8582static int is_pat_same(MpegTSContext *mpegts_ctx, 
    8683                       int *pmt_pnums, int *pmts_pids, uint pmt_count); 
    8784 
    88 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item, uint32_t prog_reg_desc, int pcr_pid); 
     85static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item); 
    8986static int is_pmt_same(MpegTSContext *mpegts_ctx, pmt_entry_t* items, 
    9087                       int item_cnt); 
    9188 
    92 typedef int PESCallback(void *opaque, const uint8_t *buf, int len,  
    93                         int is_start, int64_t startpos); 
     89typedef void PESCallback(void *opaque, const uint8_t *buf, int len,  
     90                         int is_start, int64_t startpos); 
    9491 
    9592typedef struct MpegTSPESFilter { 
    9693    PESCallback *pes_cb; 
     
    172169 
    173170    /** packet containing Audio/Video data                   */ 
    174171    AVPacket *pkt; 
    175     /** to detect seek                                       */ 
    176     int64_t last_pos; 
    177172 
    178173    /******************************************/ 
    179174    /* private mpegts data */ 
     
    214209 
    215210enum MpegTSState { 
    216211    MPEGTS_HEADER = 0, 
    217     MPEGTS_PESHEADER, 
    218212    MPEGTS_PESHEADER_FILL, 
    219213    MPEGTS_PAYLOAD, 
    220214    MPEGTS_SKIP, 
    221215}; 
    222216 
    223217/* enough for PES header + length */ 
    224 #define PES_START_SIZE  6 
    225 #define PES_HEADER_SIZE 9 
     218#define PES_START_SIZE 9 
    226219#define MAX_PES_HEADER_SIZE (9 + 255) 
    227220 
    228221struct PESContext { 
    229222    int pid; 
    230     int pcr_pid; /**< if -1 then all packets containing PCR are considered */  
    231223    int stream_type; 
    232224    MpegTSContext *ts; 
    233225    AVFormatContext *stream; 
     
    238230    int total_size; 
    239231    int pes_header_size; 
    240232    int64_t pts, dts; 
    241     int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */ 
    242233    uint8_t header[MAX_PES_HEADER_SIZE]; 
    243     uint8_t *buffer; 
     234    int64_t startpos; 
    244235}; 
    245236 
    246237extern AVInputFormat mpegts_demuxer; 
     
    249240struct SectionContext { 
    250241    int pid; 
    251242    int stream_type; 
    252     int new_packet; 
    253243    MpegTSContext *ts; 
    254244    AVFormatContext *stream; 
    255245    AVStream *st; 
     
    284274        tss->section_index += len; 
    285275    } 
    286276 
    287     if (tss->section_cb == mpegts_push_section) { 
    288         SectionContext *sect = tss->opaque; 
    289         sect->new_packet = 1; 
    290     } 
    291     while (!tss->end_of_section_reached) { 
     277    while (1) { /* There may be several tables in this data. */ 
    292278        /* compute section length if possible */ 
    293279        if (tss->section_h_size == -1 && tss->section_index >= 3) { 
    294             len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3; 
     280            len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3; 
    295281            if (len > 4096) 
    296                 return; 
     282                break; 
    297283            tss->section_h_size = len; 
    298284        } 
    299285 
    300286        if (tss->section_h_size == -1 || tss->section_index < tss->section_h_size) 
    301287            break; 
    302288 
    303         if (!tss->check_crc || 
    304             av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, 
    305                    tss->section_buf, tss->section_h_size) == 0) 
     289        if (!tss->check_crc || av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, tss->section_buf, tss->section_h_size) == 0) 
    306290            tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size); 
    307291 
    308292        if (tss->section_index > tss->section_h_size) { 
     
    312296            tss->section_h_size = -1; 
    313297        } else { 
    314298            tss->end_of_section_reached = 1; 
     299            break; 
    315300        } 
    316301    } 
    317302} 
     
    324309    MpegTSFilter *filter = ts->pids[pid]; 
    325310    MpegTSSectionFilter *sec; 
    326311     
    327     dprintf(ts->stream, "Filter: pid=0x%x\n", pid); 
     312#ifdef DEBUG_SI 
     313    av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid); 
     314#endif 
    328315 
    329  
    330316    if (NULL!=filter) { 
    331 #ifdef DEBUG 
     317#ifdef DEBUG_SI 
    332318        av_log(ts->stream, AV_LOG_DEBUG, "Filter Already Exists\n"); 
    333319#endif 
    334320        mpegts_close_filter(ts, filter); 
     
    355341    return filter; 
    356342} 
    357343 
    358 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, 
     344MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, 
    359345                                     PESCallback *pes_cb, 
    360346                                     void *opaque) 
    361347{ 
     
    377363    return filter; 
    378364} 
    379365 
    380 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter) 
     366void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter) 
    381367{ 
    382368    int pid; 
    383369 
     
    386372 
    387373    pid = filter->pid; 
    388374 
    389 #ifdef DEBUG 
     375#ifdef DEBUG_SI 
    390376    av_log(NULL, AV_LOG_DEBUG, "Closing Filter: pid=0x%x\n", pid); 
    391377#endif 
    392378    if (filter == ts->pmt_filter) 
     
    402388 
    403389    if (filter->type == MPEGTS_SECTION) 
    404390        av_freep(&filter->u.section_filter.section_buf); 
    405     else if (filter->type == MPEGTS_PES) { 
    406         PESContext *pes = filter->u.pes_filter.opaque; 
    407         av_freep(&pes->buffer); 
    408         /* referenced private data will be freed later in 
    409          * av_close_input_stream */ 
    410         if (!((PESContext *)filter->u.pes_filter.opaque)->st) { 
    411             av_freep(&filter->u.pes_filter.opaque); 
    412         } 
    413     } 
    414391 
    415392    av_free(filter); 
    416393    ts->pids[pid] = NULL; 
     
    451428    score    = analyze(buf, size, TS_PACKET_SIZE, NULL); 
    452429    dvhs_score    = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL); 
    453430    fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL); 
    454 //    av_log(NULL, AV_LOG_ERROR, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score); 
    455  
     431/*  av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", 
     432           score, dvhs_score, fec_score);*/ 
     433     
    456434    if     (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE; 
    457435    else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE; 
    458436    else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE; 
     
    544522        return -1; 
    545523    h->last_sec_num = val; 
    546524 
    547 #ifdef DEBUG 
     525#ifdef DEBUG_SI 
    548526    av_log(NULL, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n", 
    549527           h->id, h->sec_num, h->last_sec_num); 
    550528#endif 
    551529    return 0; 
    552530} 
    553531 
    554 typedef struct { 
    555     uint32_t stream_type; 
    556     enum CodecType codec_type; 
    557     enum CodecID codec_id; 
    558 } StreamType; 
    559  
    560 static const StreamType ISO_types[] = { 
    561     { 0x01, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO }, 
    562     { 0x02, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO }, 
    563     { 0x03, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 }, 
    564     { 0x04, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 }, 
    565     { 0x0f, CODEC_TYPE_AUDIO,        CODEC_ID_AAC }, 
    566     { 0x10, CODEC_TYPE_VIDEO,      CODEC_ID_MPEG4 }, 
    567     { 0x11, CODEC_TYPE_AUDIO,   CODEC_ID_AAC_LATM }, 
    568     { 0x1b, CODEC_TYPE_VIDEO,       CODEC_ID_H264 }, 
    569     { 0xd1, CODEC_TYPE_VIDEO,      CODEC_ID_DIRAC }, 
    570     { 0xea, CODEC_TYPE_VIDEO,        CODEC_ID_VC1 }, 
    571     { 0 }, 
    572 }; 
    573  
    574 static const StreamType HDMV_types[] = { 
    575     { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 }, 
    576     { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS }, 
    577     { 0x83, CODEC_TYPE_AUDIO, CODEC_ID_TRUEHD }, 
    578     { 0x84, CODEC_TYPE_AUDIO, CODEC_ID_AC3 }, 
    579     { 0x85, CODEC_TYPE_AUDIO, CODEC_ID_DTS }, 
    580     { 0x86, CODEC_TYPE_AUDIO, CODEC_ID_DTS }, 
    581     { 0 }, 
    582 }; 
    583  
    584 /* ATSC ? */ 
    585 static const StreamType MISC_types[] = { 
    586     { 0x81, CODEC_TYPE_AUDIO,   CODEC_ID_AC3 }, 
    587     { 0x8a, CODEC_TYPE_AUDIO,   CODEC_ID_DTS }, 
    588     { 0x100, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, 
    589     { 0x101, CODEC_TYPE_DATA,     CODEC_ID_DVB_VBI }, 
    590     { 0 }, 
    591 }; 
    592  
    593 static const StreamType REGD_types[] = { 
    594     { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC }, 
    595     { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO,   CODEC_ID_AC3 }, 
    596     { 0 }, 
    597 }; 
    598  
    599 /* descriptor present */ 
    600 static const StreamType DESC_types[] = { 
    601     { 0x6a, CODEC_TYPE_AUDIO,             CODEC_ID_AC3 }, /* AC-3 descriptor */ 
    602     { 0x7a, CODEC_TYPE_AUDIO,            CODEC_ID_EAC3 }, /* E-AC-3 descriptor */ 
    603     { 0x7b, CODEC_TYPE_AUDIO,             CODEC_ID_DTS }, 
    604     { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */ 
    605     { 0 }, 
    606 }; 
    607  
    608 static void mpegts_find_stream_type(AVStream *st, 
    609                                     uint32_t stream_type, const StreamType *types) 
    610 { 
    611     for (; types->stream_type; types++) { 
    612         if (stream_type == types->stream_type) { 
    613             st->codec->codec_type = types->codec_type; 
    614             st->codec->codec_id   = types->codec_id; 
    615             return; 
    616         } 
    617     } 
    618 } 
    619  
    620 static AVStream *new_pes_av_stream(PESContext *pes, uint32_t prog_reg_desc, uint32_t code) 
    621 { 
    622     AVStream *st = av_new_stream(pes->stream, pes->pid); 
    623  
    624     if (!st) 
    625         return NULL; 
    626  
    627     av_set_pts_info(st, 33, 1, 90000); 
    628     st->priv_data = pes; 
    629     st->codec->codec_type = CODEC_TYPE_DATA; 
    630     st->codec->codec_id   = CODEC_ID_NONE; 
    631     st->need_parsing = AVSTREAM_PARSE_FULL; 
    632     pes->st = st; 
    633  
    634     dprintf(pes->stream, "stream_type=%x pid=%x prog_reg_desc=%.4s\n", 
    635             pes->stream_type, pes->pid, (char*)&prog_reg_desc); 
    636  
    637     st->codec->codec_tag = pes->stream_type; 
    638  
    639     mpegts_find_stream_type(st, pes->stream_type, ISO_types); 
    640     if (prog_reg_desc == AV_RL32("HDMV") && 
    641         st->codec->codec_id == CODEC_ID_NONE) 
    642         mpegts_find_stream_type(st, pes->stream_type, HDMV_types); 
    643     if (st->codec->codec_id == CODEC_ID_NONE) 
    644         mpegts_find_stream_type(st, pes->stream_type, MISC_types); 
    645  
    646     /* stream was not present in PMT, guess based on PES start code */ 
    647     if (st->codec->codec_id == CODEC_ID_NONE) { 
    648         if (code >= 0x1c0 && code <= 0x1df) { 
    649             st->codec->codec_type = CODEC_TYPE_AUDIO; 
    650             st->codec->codec_id = CODEC_ID_MP2; 
    651         } else if (code == 0x1bd) { 
    652             st->codec->codec_type = CODEC_TYPE_AUDIO; 
    653             st->codec->codec_id = CODEC_ID_AC3; 
    654         } 
    655     } 
    656  
    657     return st; 
    658 } 
    659  
    660532static MpegTSService *new_service(MpegTSContext *ts, int sid, int pid, 
    661533                                  char *provider_name, char *name) 
    662534{ 
    663535    MpegTSService *service; 
    664536 
    665 #ifdef DEBUG 
     537#ifdef DEBUG_SI 
    666538    av_log(ts->stream, AV_LOG_DEBUG, "new_service: " 
    667539           "sid=0x%04x provider='%s' name='%s'\n", 
    668540           sid, provider_name, name); 
     
    679551    return service; 
    680552} 
    681553 
     554static int mpegts_parse_program_info_length(uint8_t **p, uint8_t *p_end) 
     555{ 
     556    int program_info_length = get16(p, p_end); 
     557    if (program_info_length < 0) 
     558        return -1; 
     559    program_info_length &= 0xfff; 
     560    *p += program_info_length; 
     561    if (*p >= p_end) 
     562        return -1; 
     563    return program_info_length; 
     564} 
     565 
    682566static int find_in_list(const int *pids, int pid) { 
    683567    int i; 
    684568    for (i=0; i<PMT_PIDS_MAX; i++) 
     
    695579        return -1; 
    696580    pcr_pid &= 0x1fff; 
    697581    mpegts_ctx->pcr_pid = pcr_pid; 
    698 #ifdef DEBUG 
     582#ifdef DEBUG_SI 
    699583    av_log(NULL, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid); 
    700584#endif 
    701585    return pcr_pid; 
     
    716600 
    717601    int last_item = 0; 
    718602    int desc_count = 0; 
    719     int program_info_length, pcr_pid, pid, stream_type; 
    720     uint32_t prog_reg_desc = 0; /* registration descriptor */ 
    721603    pmt_entry_t items[PMT_PIDS_MAX]; 
    722604    bzero(&items, sizeof(pmt_entry_t) * PMT_PIDS_MAX); 
    723605 
    724606    mpegts_cleanup_streams(mpegts_ctx); /* in case someone else removed streams.. */ 
    725607 
    726 #ifdef DEBUG 
     608#ifdef DEBUG_SI 
    727609    av_log(mpegts_ctx->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len); 
    728610    av_hex_dump_log(mpegts_ctx->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 
    729611#endif 
     
    734616    /* if we require a specific PMT, and this isn't it return silently */ 
    735617    if (mpegts_ctx->req_sid >= 0 && header.id != mpegts_ctx->req_sid) 
    736618    { 
    737 #ifdef DEBUG 
     619#ifdef DEBUG_SI 
    738620        av_log(NULL, AV_LOG_DEBUG, "We are looking for program 0x%x, not 0x%x", 
    739621               mpegts_ctx->req_sid, header.id); 
    740622#endif 
     
    748630    /* Extract the Program Clock Reference PID */ 
    749631    if (mpegts_parse_pcrpid(mpegts_ctx, &p, p_end) < 0) 
    750632        HANDLE_PMT_PARSE_ERROR("PCR PID"); 
    751     pcr_pid = mpegts_ctx->pcr_pid; 
    752633 
    753     program_info_length = get16(&p, p_end) & 0xfff; 
    754     if (program_info_length < 0) 
    755         return; 
    756     while(program_info_length >= 2) { 
    757         uint8_t tag, len; 
    758         tag = get8(&p, p_end); 
    759         len = get8(&p, p_end); 
    760         if(len > program_info_length - 2) 
    761             //something else is broken, exit the program_descriptors_loop 
    762             break; 
    763         program_info_length -= len + 2; 
    764         if(tag == 0x05 && len >= 4) { // registration descriptor 
    765             prog_reg_desc = bytestream_get_le32(&p); 
    766             len -= 4; 
    767         } 
    768         p += len; 
    769     } 
    770     p += program_info_length; 
    771     if (p >= p_end) 
    772         return; 
     634    /* Extract program info length, just so we can skip it */ 
     635    if (mpegts_parse_program_info_length(&p, p_end) < 0) 
     636        HANDLE_PMT_PARSE_ERROR("program info"); 
    773637 
    774638    /* parse new streams */ 
    775639    while (p < p_end) 
    776640    { 
    777641        dvb_caption_info_t dvbci; 
    778         stream_type = get8(&p, p_end); 
    779         pid = get16(&p, p_end); 
     642        int stream_type = get8(&p, p_end); 
     643        int pid = get16(&p, p_end); 
    780644        int desc_ok = mpegts_parse_desc(&dvbci, &p, p_end, &stream_type); 
    781645        if ((stream_type < 0) || (pid < 0) || (desc_ok < 0)) 
    782646        { 
     
    807671                stream_type = STREAM_TYPE_VIDEO_MPEG2; 
    808672        } 
    809673 
    810 #ifdef DEBUG 
     674#ifdef DEBUG_SI 
    811675    av_log(mpegts_ctx->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", mpegts_ctx->pcr_pid); 
    812676#endif 
    813677 
     
    847711 
    848712        /* create new streams */ 
    849713        for (idx = 0; idx < last_item; idx++) 
    850             mpegts_add_stream(mpegts_ctx, &items[idx], prog_reg_desc, pcr_pid); 
     714            mpegts_add_stream(mpegts_ctx, &items[idx]); 
    851715 
    852716        /* cache pmt */ 
    853717        void *tmp0 = avctx->cur_pmt_sect; 
     
    896760    int idx; 
    897761    if (mpegts_ctx->pid_cnt != item_cnt) 
    898762    { 
    899 #ifdef DEBUG 
     763#ifdef DEBUG_SI 
    900764        av_log(NULL, AV_LOG_DEBUG, "mpegts_ctx->pid_cnt=%d != item_cnt=%d\n", 
    901765               mpegts_ctx->pid_cnt, item_cnt); 
    902766#endif 
     
    908772        int loc = find_in_list(mpegts_ctx->pmt_pids, items[idx].pid); 
    909773        if (loc < 0) 
    910774        { 
    911 #ifdef DEBUG 
     775#ifdef DEBUG_SI 
    912776            av_log(NULL, AV_LOG_DEBUG, 
    913777                   "find_in_list(..,[%d].pid=%d) => -1\n" 
    914778                   "is_pmt_same() => false\n", 
     
    921785        MpegTSFilter *tss = mpegts_ctx->pids[items[idx].pid]; 
    922786        if (!tss) 
    923787        { 
    924 #ifdef DEBUG 
     788#ifdef DEBUG_SI 
    925789            av_log(NULL, AV_LOG_DEBUG, 
    926790                   "mpegts_ctx->pids[items[%d].pid=%d] => null\n" 
    927791                   "is_pmt_same() => false\n", 
     
    934798            PESContext *pes = (PESContext*) tss->u.pes_filter.opaque; 
    935799            if (!pes) 
    936800            { 
    937 #ifdef DEBUG 
     801#ifdef DEBUG_SI 
    938802                av_log(NULL, AV_LOG_DEBUG, "pes == null, where idx %d\n" 
    939803                       "is_pmt_same() => false\n", idx); 
    940804#endif 
     
    942806            } 
    943807            if (pes->stream_type != items[idx].type) 
    944808            { 
    945 #ifdef DEBUG 
     809#ifdef DEBUG_SI 
    946810                av_log(NULL, AV_LOG_DEBUG, 
    947811                       "pes->stream_type != items[%d].type\n" 
    948812                       "is_pmt_same() => false\n", idx); 
     
    955819            SectionContext *sect = (SectionContext*) tss->u.section_filter.opaque; 
    956820            if (!sect) 
    957821            { 
    958 #ifdef DEBUG 
     822#ifdef DEBUG_SI 
    959823                av_log(NULL, AV_LOG_DEBUG, "sect == null, where idx %d\n" 
    960824                       "is_pmt_same() => false\n", idx); 
    961825#endif 
     
    963827            } 
    964828            if (sect->stream_type != items[idx].type) 
    965829            { 
    966 #ifdef DEBUG 
     830#ifdef DEBUG_SI 
    967831                av_log(NULL, AV_LOG_DEBUG, 
    968832                       "sect->stream_type != items[%d].type\n" 
    969833                       "is_pmt_same() => false\n", idx); 
     
    973837        } 
    974838        else 
    975839        { 
    976 #ifdef DEBUG 
     840#ifdef DEBUG_SI 
    977841            av_log(NULL, AV_LOG_DEBUG, 
    978842                   "tss->type != MPEGTS_PES, where idx %d\n" 
    979843                   "is_pmt_same() => false\n", idx); 
     
    981845            return 0; 
    982846        } 
    983847    } 
    984 #ifdef DEBUG 
     848#ifdef DEBUG_SI 
    985849    av_log(NULL, AV_LOG_DEBUG, "is_pmt_same() => true\n", idx); 
    986850#endif 
    987851    return 1; 
     
    1003867        case STREAM_TYPE_AUDIO_AAC_LATM: 
    1004868        case STREAM_TYPE_AUDIO_AC3: 
    1005869        case STREAM_TYPE_AUDIO_DTS: 
    1006         case STREAM_TYPE_AUDIO_HDMV_AC3_PLUS: 
    1007         case STREAM_TYPE_AUDIO_HDMV_AC3_TRUE_HD: 
    1008         case STREAM_TYPE_AUDIO_HDMV_DTS: 
    1009         case STREAM_TYPE_AUDIO_HDMV_DTS_HD: 
    1010         case STREAM_TYPE_AUDIO_HDMV_DTS_HD_MASTER: 
    1011870            //case STREAM_TYPE_PRIVATE_DATA: 
    1012871        case STREAM_TYPE_VBI_DVB: 
    1013872        case STREAM_TYPE_SUBTITLE_DVB: 
     
    1055914        desc_end = *p + desc_len; 
    1056915        if (desc_end > desc_list_end) 
    1057916            break; 
    1058  
    1059 /*             dprintf(ts->stream, "tag: 0x%02x len=%d\n", */ 
    1060 /*                    desc_tag, desc_len); */ 
    1061  
    1062             switch(desc_tag) { 
     917#ifdef DEBUG_SI 
     918        av_log(NULL, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n", desc_tag, desc_len); 
     919#endif 
     920        switch (desc_tag) 
     921        { 
    1063922            case DVB_SUBT_DESCID: 
    1064923                dvbci->language[0] = get8(p, desc_end); 
    1065924                dvbci->language[1] = get8(p, desc_end); 
     
    1130989    } 
    1131990} 
    1132991 
    1133 static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item, uint32_t prog_reg_desc, int pcr_pid) 
     992static void mpegts_add_stream(MpegTSContext *ts, pmt_entry_t* item) 
    1134993{ 
    1135994 
    1136995    av_log(NULL, AV_LOG_DEBUG, 
     
    11741033        } else { 
    11751034            PESContext *pes = NULL; 
    11761035            AVStream *st = NULL; 
    1177             pes = add_pes_stream(ts, item->pid, pcr_pid, item->type); 
     1036            pes = add_pes_stream(ts, item->pid, item->type); 
    11781037            if (!pes) 
    11791038            { 
    11801039                av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: " 
     
    11841043            } 
    11851044 
    11861045            /* Pretend it's audio if we have a language. */ 
    1187             st = new_pes_av_stream(pes, prog_reg_desc, 0); 
     1046            st = new_pes_av_stream(pes, item->dvbci.language[0] ? 0x1c0 : 0); 
    11881047            if (!st) 
    11891048            { 
    11901049                av_log(NULL, AV_LOG_ERROR, "mpegts_add_stream: " 
     
    12511110    uint pmt_count = 0; 
    12521111    int i; 
    12531112 
    1254 #ifdef DEBUG 
     1113#ifdef DEBUG_SI 
    12551114    av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n"); 
    12561115    av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 
    12571116#endif 
     
    12821141 
    12831142        pmt_count++; 
    12841143 
    1285 #ifdef DEBUG 
     1144#ifdef DEBUG_SI 
    12861145        av_log(ts->stream, AV_LOG_DEBUG, 
    12871146               "MPEG Program Number=0x%x pid=0x%x req_sid=0x%x\n", 
    12881147               pmt_pnums[i], pmt_pids[i], ts->req_sid); 
     
    12911150 
    12921151    if (!is_pat_same(ts, pmt_pnums, pmt_pids, pmt_count)) 
    12931152    { 
    1294 #ifdef DEBUG 
     1153#ifdef DEBUG_SI 
    12951154        av_log(NULL, AV_LOG_DEBUG, "New PAT!\n"); 
    12961155#endif 
    12971156        /* if there were services, get rid of them */ 
     
    13241183         * add a filter for the PMT. */ 
    13251184        if (ts->req_sid == pmt_pnums[i]) 
    13261185        { 
    1327 #ifdef DEBUG 
     1186#ifdef DEBUG_SI 
    13281187            av_log(NULL, AV_LOG_DEBUG, "Found program number!\n"); 
    13291188#endif 
    13301189            /* close old filter if it doesn't match */ 
     
    13591218     * tell parser it is safe to quit. */ 
    13601219    if (ts->req_sid < 0 && ts->scanning) 
    13611220    { 
    1362 #ifdef DEBUG 
     1221#ifdef DEBUG_SI 
    13631222        av_log(NULL, AV_LOG_DEBUG, "Found PAT, ending scan\n"); 
    13641223#endif 
    13651224        ts->stop_parse = 1; 
     
    13701229     * and tell parser it is safe to quit. */  
    13711230    if (ts->req_sid >= 0 && !found) 
    13721231    { 
    1373 #ifdef DEBUG 
     1232#ifdef DEBUG_SI 
    13741233        av_log(NULL, AV_LOG_DEBUG, "Program 0x%x is not in PAT, ending scan\n", 
    13751234               ts->req_sid); 
    13761235#endif 
     
    13901249    char *provider_name, *name; 
    13911250    char buf[256]; 
    13921251 
    1393 #ifdef DEBUG 
    1394     dprintf(ts->stream, "PAT:\n"); 
     1252#ifdef DEBUG_SI 
     1253    av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n"); 
    13951254    av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 
    13961255#endif 
    13971256    p_end = section + section_len - 4; 
     
    14081267        pmt_pid = get16(&p, p_end) & 0x1fff; 
    14091268        if (pmt_pid < 0) 
    14101269            break; 
    1411  
    1412         dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid); 
    1413  
     1270#ifdef DEBUG_SI 
     1271        av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid); 
     1272#endif 
    14141273        if (sid == 0x0000) { 
    14151274            /* NIT info */ 
    14161275        } else { 
     
    14311290    /* remove filter */ 
    14321291    mpegts_close_filter(ts, ts->pat_filter); 
    14331292    ts->pat_filter = NULL; 
    1434 #ifdef DEBUG 
     1293#ifdef DEBUG_SI 
    14351294    av_log(NULL, AV_LOG_DEBUG, "end of scan PAT\n"); 
    14361295#endif     
    14371296} 
     
    14461305    int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type; 
    14471306    char *name, *provider_name; 
    14481307 
    1449 #ifdef DEBUG 
    1450     dprintf(ts->stream, "SDT:\n"); 
     1308#ifdef DEBUG_SI 
     1309    av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n"); 
    14511310    av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len); 
    14521311#endif 
    14531312 
     
    14841343            desc_end = p + desc_len; 
    14851344            if (desc_end > desc_list_end) 
    14861345                break; 
    1487  
    1488             dprintf(ts->stream, "tag: 0x%02x len=%d\n", 
     1346#ifdef DEBUG_SI 
     1347            av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n", 
    14891348                   desc_tag, desc_len); 
    1490  
     1349#endif 
    14911350            switch(desc_tag) { 
    14921351            case 0x48: 
    14931352                service_type = get8(&p, p_end); 
     
    15181377} 
    15191378#endif 
    15201379 
    1521 static int64_t get_pts(const uint8_t *p) 
     1380#if 0 
     1381/* scan services in a transport stream by looking at the SDT */ 
     1382void mpegts_scan_sdt(MpegTSContext *ts) 
    15221383{ 
    1523     int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30; 
    1524     pts |= (AV_RB16(p + 1) >> 1) << 15; 
    1525     pts |=  AV_RB16(p + 3) >> 1; 
    1526     return pts; 
     1384    ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID, 
     1385                                                sdt_cb, ts, 1); 
    15271386} 
     1387#endif 
    15281388 
    1529 static void new_pes_packet(PESContext *pes, AVPacket *pkt) 
     1389#if 0 
     1390/* scan services in a transport stream by looking at the PAT (better 
     1391   than nothing !) */ 
     1392void mpegts_scan_pat(MpegTSContext *ts) 
    15301393{ 
    1531     av_init_packet(pkt); 
     1394    ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID, 
     1395                                                pat_scan_cb, ts, 1); 
     1396} 
     1397#endif 
    15321398 
    1533     pkt->destruct = av_destruct_packet; 
    1534     pkt->data = pes->buffer; 
    1535     pkt->size = pes->data_index; 
    1536     if (pkt->data && pkt->size) 
    1537         memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 
     1399static int64_t get_pts(const uint8_t *p) 
     1400{ 
     1401    int64_t pts; 
     1402    int val; 
    15381403 
    1539     pkt->stream_index = pes->st->index; 
    1540     pkt->pts = pes->pts; 
    1541     pkt->dts = pes->dts; 
    1542     /* store position of first TS packet of this PES packet */ 
    1543     pkt->pos = pes->ts_packet_pos; 
    1544  
    1545     /* reset pts values */ 
    1546     pes->pts = AV_NOPTS_VALUE; 
    1547     pes->dts = AV_NOPTS_VALUE; 
    1548     pes->buffer = NULL; 
    1549     pes->data_index = 0; 
    1550     pes->total_size = 0; 
     1404    pts = (int64_t)((p[0] >> 1) & 0x07) << 30; 
     1405    val = (p[1] << 8) | p[2]; 
     1406    pts |= (int64_t)(val >> 1) << 15; 
     1407    val = (p[3] << 8) | p[4]; 
     1408    pts |= (int64_t)(val >> 1); 
     1409    return pts; 
    15511410} 
    15521411 
     1412 
    15531413static void init_stream(AVStream *st, int stream_type, int code) 
    15541414{ 
    15551415    int codec_type=-1, codec_id=-1; 
     
    15891449            codec_id = CODEC_ID_AC3; 
    15901450            break; 
    15911451        case STREAM_TYPE_AUDIO_DTS: 
    1592            codec_type = CODEC_TYPE_AUDIO; 
    1593            codec_id = CODEC_ID_DTS; 
     1452            codec_type = CODEC_TYPE_AUDIO; 
     1453            codec_id = CODEC_ID_DTS; 
    15941454            break; 
    15951455        case STREAM_TYPE_VBI_DVB: 
    15961456            codec_type = CODEC_TYPE_DATA; 
     
    16231483    av_set_pts_info(st, 33, 1, 90000); 
    16241484} 
    16251485 
     1486static AVStream *new_pes_av_stream(PESContext *pes, uint32_t code) 
     1487{ 
     1488    CHECKED_ALLOCZ(pes->st, sizeof(AVStream)); 
     1489    pes->st->codec = avcodec_alloc_context(); 
     1490    init_stream(pes->st, pes->stream_type, code);  /* sets codec type and id */ 
     1491    pes->st->priv_data = pes; 
     1492    pes->st->need_parsing = AVSTREAM_PARSE_FULL; 
     1493 
     1494    pes->st = av_add_stream(pes->stream, pes->st, pes->pid); 
     1495fail: /*for the CHECKED_ALLOCZ macro*/ 
     1496    return pes->st; 
     1497} 
     1498 
    16261499static AVStream *new_section_av_stream(SectionContext *sect, uint32_t code) 
    16271500{ 
    16281501    CHECKED_ALLOCZ(sect->st, sizeof(AVStream)); 
     
    16381511 
    16391512 
    16401513/* return non zero if a packet could be constructed */ 
    1641 static int mpegts_push_data(void *opaque, 
    1642                             const uint8_t *buf, int buf_size, int is_start, 
    1643                             int64_t pos) 
     1514static void mpegts_push_data(void *opaque, 
     1515                             const uint8_t *buf, int buf_size, int is_start, 
     1516                             int64_t position) 
    16441517{ 
    16451518    PESContext *pes = opaque; 
    16461519    MpegTSContext *ts = pes->ts; 
    16471520    const uint8_t *p; 
    16481521    int len, code; 
    16491522 
    1650     if(!ts->pkt) 
    1651         return 0; 
    1652  
    16531523    if (is_start) { 
    1654         if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { 
    1655             new_pes_packet(pes, ts->pkt); 
    1656             ts->stop_parse = 1; 
    1657         } 
     1524        pes->startpos = position; 
    16581525        pes->state = MPEGTS_HEADER; 
    16591526        pes->data_index = 0; 
    1660         pes->ts_packet_pos = pos; 
    16611527    } 
    16621528    p = buf; 
    16631529    while (buf_size > 0) { 
     
    16801546                    pes->header[2] == 0x01) { 
    16811547                    /* it must be an mpeg2 PES stream */ 
    16821548                    code = pes->header[3] | 0x100; 
    1683                     dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code); 
    1684  
    1685                     if ((!pes->st && pes->stream->nb_streams == MAX_STREAMS) || 
    1686                         (pes->st && pes->st->discard == AVDISCARD_ALL) || 
    1687 /*                         !((code >= 0x1c0 && code <= 0x1df) || */ 
    1688 /*                           (code >= 0x1e0 && code <= 0x1ef) || */ 
    1689 /*                           (code == 0x1bd) || (code == 0x1fd)) || */ 
    1690                         code == 0x1be) /* padding_stream */ 
     1549                    if (!((code >= 0x1c0 && code <= 0x1df) || 
     1550                          (code >= 0x1e0 && code <= 0x1ef) || 
     1551                          (code == 0x1bd) || (code == 0x1fd))) 
    16911552                        goto skip; 
    1692  
    1693                     /* stream not present in PMT */ 
    1694                     if (!pes->st) 
    1695                         pes->st = new_pes_av_stream(pes, 0, code); 
    1696                     if (!pes->st) 
    1697                         return AVERROR(ENOMEM); 
    1698  
    1699                     pes->total_size = AV_RB16(pes->header + 4); 
     1553                    if (!pes->st && 0 == new_pes_av_stream(pes, code)) { 
     1554                        av_log(NULL, AV_LOG_ERROR, 
     1555                               "Error: new_pes_av_stream() " 
     1556                               "failed in mpegts_push_data\n"); 
     1557                        goto skip; 
     1558                    } 
     1559                    pes->state = MPEGTS_PESHEADER_FILL; 
     1560                    pes->total_size = (pes->header[4] << 8) | pes->header[5]; 
    17001561                    /* NOTE: a zero total size means the PES size is 
    17011562                       unbounded */ 
    1702                     if (!pes->total_size) 
    1703                         pes->total_size = MAX_PES_PAYLOAD; 
    1704  
    1705                     /* allocate pes buffer */ 
    1706                     pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE); 
    1707                     if (!pes->buffer) 
    1708                         return AVERROR(ENOMEM); 
    1709  
    1710                     if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */ 
    1711                         code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */ 
    1712                         code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */ 
    1713                         code != 0x1f8) {                  /* ITU-T Rec. H.222.1 type E stream */ 
    1714                         pes->state = MPEGTS_PESHEADER; 
    1715                         if (pes->st->codec->codec_id == CODEC_ID_NONE) { 
    1716                             dprintf(pes->stream, "pid=%x stream_type=%x probing\n", 
    1717                                     pes->pid, pes->stream_type); 
    1718                             pes->st->codec->codec_id = CODEC_ID_PROBE; 
    1719                         } 
    1720                     } else { 
    1721                         pes->state = MPEGTS_PAYLOAD; 
    1722                         pes->data_index = 0; 
    1723                     } 
     1563                    if (pes->total_size) 
     1564                        pes->total_size += 6; 
     1565                    pes->pes_header_size = pes->header[8] + 9; 
    17241566                } else { 
    17251567                    /* otherwise, it should be a table */ 
    17261568                    /* skip packet */ 
     
    17321574            break; 
    17331575            /**********************************************/ 
    17341576            /* PES packing parsing */ 
    1735         case MPEGTS_PESHEADER: 
    1736             len = PES_HEADER_SIZE - pes->data_index; 
    1737             if (len < 0) 
    1738                 return -1; 
    1739             if (len > buf_size) 
    1740                 len = buf_size; 
    1741             memcpy(pes->header + pes->data_index, p, len); 
    1742             pes->data_index += len; 
    1743             p += len; 
    1744             buf_size -= len; 
    1745             if (pes->data_index == PES_HEADER_SIZE) { 
    1746                 pes->pes_header_size = pes->header[8] + 9; 
    1747                 pes->state = MPEGTS_PESHEADER_FILL; 
    1748             } 
    1749             break; 
    17501577        case MPEGTS_PESHEADER_FILL: 
    17511578            len = pes->pes_header_size - pes->data_index; 
    1752             if (len < 0) 
    1753                 return -1; 
    17541579            if (len > buf_size) 
    17551580                len = buf_size; 
    17561581            memcpy(pes->header + pes->data_index, p, len); 
     
    17661591                pes->pts = AV_NOPTS_VALUE; 
    17671592                pes->dts = AV_NOPTS_VALUE; 
    17681593                if ((flags & 0xc0) == 0x80) { 
    1769                     pes->dts = pes->pts = get_pts(r); 
     1594                    pes->pts = get_pts(r); 
    17701595                    r += 5; 
    17711596                } else if ((flags & 0xc0) == 0xc0) { 
    17721597                    pes->pts = get_pts(r); 
     
    17741599                    pes->dts = get_pts(r); 
    17751600                    r += 5; 
    17761601                } 
    1777  
    17781602                /* we got the full header. We parse it and get the payload */ 
    17791603                pes->state = MPEGTS_PAYLOAD; 
    1780                 pes->data_index = 0; 
    17811604            } 
    17821605            break; 
    17831606        case MPEGTS_PAYLOAD: 
    1784             if (buf_size > 0) { 
    1785                 if (pes->data_index+buf_size > pes->total_size) { 
    1786                     new_pes_packet(pes, ts->pkt); 
    1787                     pes->total_size = MAX_PES_PAYLOAD; 
    1788                     pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE); 
    1789                     if (!pes->buffer) 
    1790                         return AVERROR(ENOMEM); 
     1607            if (pes->total_size) { 
     1608                len = pes->total_size - pes->data_index; 
     1609                if (len > buf_size) 
     1610                    len = buf_size; 
     1611            } else { 
     1612                len = buf_size; 
     1613            } 
     1614            if (len > 0) { 
     1615                AVPacket *pkt = ts->pkt; 
     1616                if (pkt && pes->st && av_new_packet(pkt, len) == 0) { 
     1617                    memcpy(pkt->data, p, len); 
     1618                    pkt->stream_index = pes->st->index; 
     1619                    pkt->pts = pes->pts; 
     1620                    pkt->dts = pes->dts; 
     1621                    pkt->pos = pes->startpos; 
     1622                    /* reset pts values */ 
     1623                    pes->pts = AV_NOPTS_VALUE; 
     1624                    pes->dts = AV_NOPTS_VALUE; 
    17911625                    ts->stop_parse = 1; 
     1626                    return; 
    17921627                } 
    1793                 memcpy(pes->buffer+pes->data_index, p, buf_size); 
    1794                 pes->data_index += buf_size; 
    17951628            } 
    17961629            buf_size = 0; 
    17971630            break; 
     
    18001633            break; 
    18011634        } 
    18021635    } 
    1803  
    1804     return 0; 
    18051636} 
    18061637 
    1807 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type) 
     1638static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int stream_type) 
    18081639{ 
    18091640    MpegTSFilter *tss = ts->pids[pid]; 
    18101641    PESContext *pes = 0; 
     
    18261657    pes->ts = ts; 
    18271658    pes->stream = ts->stream; 
    18281659    pes->pid = pid; 
    1829     pes->pcr_pid = pcr_pid; 
    18301660    pes->stream_type = stream_type; 
    1831     pes->state = MPEGTS_SKIP; 
    18321661    tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes); 
    18331662    if (!tss) { 
    18341663        av_free(pes); 
     
    18521681    SectionHeader header; 
    18531682    AVPacket *pkt = ts->pkt; 
    18541683    const uint8_t *p = section, *p_end = section + section_len - 4; 
    1855  
    18561684    if (parse_section_header(&header, &p, p_end) < 0) 
    18571685    { 
    18581686        av_log(NULL, AV_LOG_DEBUG, "Unable to parse header\n"); 
    18591687        return; 
    18601688    } 
    1861  
    1862     if (sect->new_packet && pkt && sect->st) { 
    1863         int pktLen = section_len + 184; /* Add enough for a complete TS payload. */ 
    1864         sect->new_packet = 0; 
    1865         if (av_new_packet(pkt, pktLen) == 0) { 
    1866             memcpy(pkt->data, section, section_len); 
    1867             memset(pkt->data+section_len, 0xff, pktLen-section_len); 
    1868             pkt->stream_index = sect->st->index; 
    1869             ts->stop_parse = 1; 
    1870         } 
    1871     } else if (pkt->data) { /* We've already added at least one table. */ 
     1689    if (pkt->data) { /* We've already added at least one table. */ 
    18721690        uint8_t *data = pkt->data; 
    18731691        int space = pkt->size; 
    18741692        int table_size = 0; 
     
    18841702            return; 
    18851703        } 
    18861704        memcpy(data, section, section_len); 
     1705    } else if (pkt && sect->st) { 
     1706        int pktLen = section_len + 184; /* Add enough for a complete TS payload. */ 
     1707        if (av_new_packet(pkt, pktLen) == 0) { 
     1708            memcpy(pkt->data, section, section_len); 
     1709            memset(pkt->data+section_len, 0xff, pktLen-section_len); 
     1710            pkt->stream_index = sect->st->index; 
     1711            pkt->pts = AV_NOPTS_VALUE; 
     1712            pkt->dts = AV_NOPTS_VALUE; 
     1713            pkt->pos = 0; 
     1714            ts->stop_parse = 1; 
     1715        } 
    18871716   } 
    18881717} 
    18891718 
     
    19231752 
    19241753 
    19251754/* handle one TS packet */ 
    1926 static int handle_packet(MpegTSContext *ts, const uint8_t *packet) 
     1755static void handle_packet(MpegTSContext *ts, const uint8_t *packet, 
     1756                          int64_t position) 
    19271757{ 
    19281758    AVFormatContext *s = ts->stream; 
    19291759    MpegTSFilter *tss; 
    19301760    int len, pid, cc, cc_ok, afc, is_start; 
    19311761    const uint8_t *p, *p_end; 
    1932     int64_t pos; 
    19331762 
    1934     pid = AV_RB16(packet + 1) & 0x1fff; 
    1935  
    19361763    if (!ts->pids[0]) { 
    19371764        /* make sure we're always scanning for new PAT's */ 
    19381765        ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); 
    19391766    } 
    19401767 
     1768    pid = ((packet[1] & 0x1f) << 8) | packet[2]; 
    19411769    is_start = packet[1] & 0x40; 
    1942  
    19431770    tss = ts->pids[pid]; 
    19441771    if (ts->auto_guess && tss == NULL && is_start) { 
    1945         add_pes_stream(ts, pid, -1, 0); 
     1772        add_pes_stream(ts, pid, 0); 
    19461773        tss = ts->pids[pid]; 
    19471774    } 
    19481775    if (!tss) 
    1949         return 0; 
     1776        return; 
    19501777 
    19511778    /* continuity check (currently not used) */ 
    19521779    cc = (packet[3] & 0xf); 
     
    19571784    afc = (packet[3] >> 4) & 3; 
    19581785    p = packet + 4; 
    19591786    if (afc == 0) /* reserved value */ 
    1960         return 0; 
     1787        return; 
    19611788    if (afc == 2) /* adaptation field only */ 
    1962         return 0; 
     1789        return; 
    19631790    if (afc == 3) { 
    19641791        /* skip adapation field */ 
    19651792        p += p[0] + 1; 
     
    19671794    /* if past the end of packet, ignore */ 
    19681795    p_end = packet + TS_PACKET_SIZE; 
    19691796    if (p >= p_end) 
    1970         return 0; 
     1797        return; 
    19711798 
    1972     pos = url_ftell(ts->stream->pb); 
    1973     ts->pos47= pos % ts->raw_packet_size; 
     1799    ts->pos47= url_ftell(ts->stream->pb) % ts->raw_packet_size; 
    19741800 
    19751801    if (tss->type == MPEGTS_SECTION) { 
    19761802        if (is_start) { 
    19771803            /* pointer field present */ 
    19781804            len = *p++; 
    19791805            if (p + len > p_end) 
    1980                 return 0; 
     1806                return; 
    19811807            if (len && cc_ok) { 
    19821808                /* write remaining section bytes */ 
    19831809                write_section_data(s, tss, 
    19841810                                   p, len, 0); 
    19851811                /* check whether filter has been closed */ 
    19861812                if (!ts->pids[pid]) 
    1987                     return 0; 
     1813                    return; 
    19881814            } 
    19891815            p += len; 
    19901816            if (p < p_end) { 
     
    19981824            } 
    19991825        } 
    20001826    } else { 
    2001         int ret; 
    2002         // Note: The position here points actually behind the current packet. 
    2003         if ((ret = tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,  
    2004                                             p, p_end - p, is_start, 
    2005                                             pos - ts->raw_packet_size)) < 0) 
    2006             return ret; 
     1827        tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,  
     1828                                 p, p_end - p, is_start, position); 
    20071829    } 
    2008  
    2009     return 0; 
    20101830} 
    20111831 
    20121832/* XXX: try to find a better synchro over several packets (use 
     
    20291849} 
    20301850 
    20311851/* return -1 if error or EOF. Return 0 if OK. */ 
    2032 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size) 
     1852static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size, 
     1853                       int64_t *position) 
    20331854{ 
    20341855    int skip, len; 
    20351856 
    20361857    for(;;) { 
     1858        *position = url_ftell(pb); 
    20371859        len = get_buffer(pb, buf, TS_PACKET_SIZE); 
    20381860        if (len != TS_PACKET_SIZE) 
    20391861            return AVERROR(EIO); 
     
    20611883    ByteIOContext *pb = s->pb; 
    20621884    uint8_t packet[TS_PACKET_SIZE]; 
    20631885    int packet_num, ret; 
     1886    int64_t pos; 
    20641887 
    20651888    ts->stop_parse = 0; 
    20661889    packet_num = 0; 
    20671890    for(;;) { 
    2068         if (ts->stop_parse>0) 
     1891        if (ts->stop_parse) 
    20691892            break; 
    20701893        packet_num++; 
    2071         if (nb_packets != 0 && packet_num * ts->raw_packet_size >= nb_packets) 
     1894        if (nb_packets != 0 && packet_num >= nb_packets) 
    20721895            break; 
    2073         ret = read_packet(pb, packet, ts->raw_packet_size); 
     1896        ret = read_packet(pb, packet, ts->raw_packet_size, &pos); 
    20741897        if (ret != 0) 
    20751898            return ret; 
    2076         ret = handle_packet(ts, packet); 
    2077         if (ret != 0) 
    2078             return ret; 
     1899        handle_packet(ts, packet, pos); 
    20791900    } 
    20801901    return 0; 
    20811902} 
     
    20851906#if 1 
    20861907    const int size= p->buf_size; 
    20871908    int score, fec_score, dvhs_score; 
    2088     int check_count= size / TS_FEC_PACKET_SIZE; 
    20891909#define CHECK_COUNT 10 
    20901910 
    2091     if (check_count < CHECK_COUNT) 
     1911    if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT)) 
    20921912        return -1; 
    20931913 
    2094     score     = analyze(p->buf, TS_PACKET_SIZE     *check_count, TS_PACKET_SIZE     , NULL)*CHECK_COUNT/check_count; 
    2095     dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count; 
    2096     fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count; 
    2097 //    av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score); 
    2098  
    2099 // we need a clear definition for the returned score otherwise things will become messy sooner or later 
     1914    score    = analyze(p->buf, TS_PACKET_SIZE    *CHECK_COUNT, TS_PACKET_SIZE, NULL); 
     1915    dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * CHECK_COUNT, 
     1916                         TS_DVHS_PACKET_SIZE, NULL); 
     1917    fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL); 
     1918#if 0 
     1919    av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", 
     1920           score, fec_score); 
     1921#endif 
     1922  
     1923    /* we need a clear definition for the returned score  
     1924       otherwise things will become messy sooner or later */ 
    21001925    if     (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score     - CHECK_COUNT; 
    21011926    else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score  - CHECK_COUNT; 
    21021927    else if(                 fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT; 
     
    21101935#endif 
    21111936} 
    21121937 
    2113 /* return the 90kHz PCR and the extension for the 27MHz PCR. return 
     1938#if 0 
     1939static void set_service_cb(void *opaque, int ret) 
     1940{ 
     1941    MpegTSContext *ts = opaque; 
     1942    ts->set_service_ret = ret; 
     1943    ts->stop_parse = 1; 
     1944} 
     1945#endif 
     1946 
     1947/* return the 90 kHz PCR and the extension for the 27 MHz PCR. return 
    21141948   (-1) if not available */ 
    21151949static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, 
    21161950                     const uint8_t *packet) 
     
    21441978{ 
    21451979    MpegTSContext *ts = s->priv_data; 
    21461980    ByteIOContext *pb = s->pb; 
    2147     uint8_t buf[5*1024]; 
     1981    uint8_t buf[1024]; 
    21481982    int len, sid, i; 
    21491983    int64_t pos; 
    21501984    MpegTSService *service; 
    21511985 
    21521986    if (ap) { 
     1987        ts->mpeg2ts_raw = ap->mpeg2ts_raw; 
    21531988        ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr; 
    2154         if(ap->mpeg2ts_raw){ 
    2155             av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n"); 
    2156             return -1; 
    2157         } 
    21581989    } 
    21591990 
    21601991    /* read the first 1024 bytes to get packet size */ 
    21611992    pos = url_ftell(pb); 
    21621993    len = get_buffer(pb, buf, sizeof(buf)); 
    2163     if (len != sizeof(buf)) 
    2164     { 
     1994    if (len != sizeof(buf)) { 
    21651995        av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 
    21661996               "unable to read first 1024 bytes\n"); 
    21671997        goto fail; 
    21681998    } 
    21691999    ts->raw_packet_size = get_packet_size(buf, sizeof(buf)); 
    2170     av_log(NULL, AV_LOG_DEBUG, "mpegts_read_header: TS packet size = %d\n", ts->raw_packet_size); 
    2171     if (ts->raw_packet_size <= 0) 
    2172     { 
     2000    if (ts->raw_packet_size <= 0) { 
    21732001        av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 
    21742002               "packet size incorrect\n"); 
    21752003        goto fail; 
     
    21772005    ts->stream = s; 
    21782006    ts->auto_guess = 0; 
    21792007 
    2180     if (s->iformat == &mpegts_demuxer) { 
     2008    if (!ts->mpeg2ts_raw) { 
    21812009        /* normal demux */ 
    21822010 
    21832011        if (!ts->auto_guess) { 
    2184             /* first do a scaning to get all the services */ 
    2185             url_fseek(pb, pos, SEEK_SET); 
    2186  
    21872012            /* SDT Scan Removed here. It caused startup delays in TS files 
    2188                SDT will not exist in a stripped TS file created by myth. 
    2189         mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1); 
    2190             */ 
     2013               SDT will not exist in a stripped TS file created by myth. */ 
    21912014 
    21922015            /* we don't want any PMT pid filters created on first pass */ 
    21932016            ts->req_sid = -1; 
    21942017 
    21952018            ts->scanning = 1; 
    2196             ts->pat_filter = 
    2197         mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1); 
    2198  
    2199         handle_packets(ts, s->probesize); 
     2019            ts->pat_filter = mpegts_open_section_filter( 
     2020                ts, PAT_PID, pat_cb, ts, 1); 
     2021            url_fseek(pb, pos, SEEK_SET); 
     2022            handle_packets(ts, s->probesize); 
    22002023            ts->scanning = 0; 
    22012024 
    22022025            if (ts->nb_services <= 0) { 
     
    22132036                         (ts->pmt_scan_state == PMT_NOT_YET_FOUND)); i++) 
    22142037            { 
    22152038                service = ts->services[i]; 
    2216 #ifdef DEBUG 
     2039#ifdef DEBUG_SI 
    22172040                av_log(ts->stream, AV_LOG_DEBUG, "Tuning to '%s' pnum: 0x%x\n", 
    22182041                       service->name, service->sid); 
    22192042#endif 
     
    22522075                goto fail; 
    22532076            }  
    22542077             
    2255         dprintf(ts->stream, "tuning done\n"); 
    2256  
     2078#ifdef DEBUG_SI 
     2079            av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n"); 
     2080#endif 
    22572081        } 
    22582082        s->ctx_flags |= AVFMTCTX_NOHEADER; 
    22592083    } else { 
    22602084        AVStream *st; 
    22612085        int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l; 
    2262         int64_t pcrs[2], pcr_h; 
     2086        int64_t pcrs[2], pcr_h, position; 
    22632087        int packet_count[2]; 
    22642088        uint8_t packet[TS_PACKET_SIZE]; 
    22652089 
     
    22672091 
    22682092    do_pcr: 
    22692093        st = av_new_stream(s, 0); 
    2270         if (!st) 
    2271         { 
     2094        if (!st) { 
    22722095            av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 
    22732096                   "av_new_stream() failed\n"); 
    22742097            goto fail; 
    22752098        } 
    2276         av_set_pts_info(st, 60, 1, 27000000); 
     2099        av_set_pts_info(st, 33, 1, 27000000); 
    22772100        st->codec->codec_type = CODEC_TYPE_DATA; 
    22782101        st->codec->codec_id = CODEC_ID_MPEG2TS; 
    22792102 
     
    22822105        nb_pcrs = 0; 
    22832106        nb_packets = 0; 
    22842107        for(;;) { 
    2285             ret = read_packet(s->pb, packet, ts->raw_packet_size); 
    2286             if (ret < 0) 
    2287             { 
     2108            ret = read_packet(s->pb, packet, ts->raw_packet_size, &position); 
     2109            if (ret < 0) { 
    22882110                av_log(NULL, AV_LOG_ERROR, "mpegts_read_header: " 
    22892111                       "read_packet() failed\n"); 
    2290                 return -1; 
     2112                goto fail; 
    22912113            } 
    2292             pid = AV_RB16(packet + 1) & 0x1fff; 
     2114            pid = ((packet[1] & 0x1f) << 8) | packet[2]; 
    22932115            if ((pcr_pid == -1 || pcr_pid == pid) && 
    22942116                parse_pcr(&pcr_h, &pcr_l, packet) == 0) { 
    22952117                pcr_pid = pid; 
     
    23292151{ 
    23302152    MpegTSContext *ts = s->priv_data; 
    23312153    int ret, i; 
    2332     int64_t pcr_h, next_pcr_h, pos; 
     2154    int64_t pcr_h, next_pcr_h, pos, position; 
    23332155    int pcr_l, next_pcr_l; 
    23342156    uint8_t pcr_buf[12]; 
    23352157 
    23362158    if (av_new_packet(pkt, TS_PACKET_SIZE) < 0) 
    23372159        return AVERROR(ENOMEM); 
    2338     pkt->pos= url_ftell(s->pb); 
    2339     ret = read_packet(s->pb, pkt->data, ts->raw_packet_size); 
     2160    ret = read_packet(s->pb, pkt->data, ts->raw_packet_size, &position); 
    23402161    if (ret < 0) { 
    23412162        av_free_packet(pkt); 
    23422163        return ret; 
     
    23722193                              AVPacket *pkt) 
    23732194{ 
    23742195    MpegTSContext *ts = s->priv_data; 
    2375     int ret, i; 
    23762196 
    2377     if (url_ftell(s->pb) != ts->last_pos) { 
    2378         /* seek detected, flush pes buffer */ 
    2379         for (i = 0; i < NB_PID_MAX; i++) { 
    2380             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) { 
    2381                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque; 
    2382                 av_freep(&pes->buffer); 
    2383                 pes->data_index = 0; 
    2384                 pes->state = MPEGTS_SKIP; /* skip until pes header */ 
    2385             } 
    2386         } 
     2197    if (!ts->mpeg2ts_raw) { 
     2198        ts->pkt = pkt; 
     2199        return handle_packets(ts, 0); 
     2200    } else { 
     2201        return mpegts_raw_read_packet(s, pkt); 
    23872202    } 
    2388  
    2389     ts->pkt = pkt; 
    2390     ret = handle_packets(ts, 0); 
    2391     if (ret < 0) { 
    2392         /* flush pes data left */ 
    2393         for (i = 0; i < NB_PID_MAX; i++) { 
    2394             if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) { 
    2395                 PESContext *pes = ts->pids[i]->u.pes_filter.opaque; 
    2396                 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) { 
    2397                     new_pes_packet(pes, pkt); 
    2398                     ret = 0; 
    2399                     break; 
    2400                 } 
    2401             } 
    2402         } 
    2403     } 
    2404  
    2405     ts->last_pos = url_ftell(s->pb); 
    2406  
    2407     return ret; 
    24082203} 
    24092204 
    24102205static int mpegts_read_close(AVFormatContext *s) 
     
    25122307                        const uint8_t *buf, int len) 
    25132308{ 
    25142309    int len1; 
     2310    int64_t position = 0; 
    25152311 
    25162312    len1 = len; 
    25172313    ts->pkt = pkt; 
    25182314    ts->stop_parse = 0; 
    25192315    for(;;) { 
    2520         if (ts->stop_parse>0) 
     2316        if (ts->stop_parse) 
    25212317            break; 
    25222318        if (len < TS_PACKET_SIZE) 
    25232319            return -1; 
     
    25252321            buf++; 
    25262322            len--; 
    25272323        } else { 
    2528             handle_packet(ts, buf); 
     2324            handle_packet(ts, buf, position); 
    25292325            buf += TS_PACKET_SIZE; 
    25302326            len -= TS_PACKET_SIZE; 
    25312327        } 
  • libs/libavformat/mpegts.h

     
    11/* 
    22 * MPEG2 transport stream defines 
    3  * Copyright (c) 2003 Fabrice Bellard 
     3 * Copyright (c) 2003 Fabrice Bellard. 
    44 * 
    55 * This file is part of FFmpeg. 
    66 * 
     
    3939#define PMT_TID   0x02 
    4040#define SDT_TID   0x42 
    4141 
     42/* descriptor ids */ 
    4243#define DVB_CAROUSEL_ID             0x13 
    4344#define DVB_VBI_DATA_ID             0x45 
    4445#define DVB_VBI_TELETEXT_ID         0x46 
     
    4647#define DVB_SUBT_DESCID             0x59 
    4748#define DVB_BROADCAST_ID            0x66 
    4849#define DVB_DATA_STREAM             0x52 
     50 
    4951#define STREAM_TYPE_VIDEO_MPEG1     0x01 
    5052#define STREAM_TYPE_VIDEO_MPEG2     0x02 
    5153#define STREAM_TYPE_AUDIO_MPEG1     0x03 
     
    6264 
    6365#define STREAM_TYPE_AUDIO_AC3       0x81 
    6466#define STREAM_TYPE_AUDIO_DTS       0x8a 
    65 #define STREAM_TYPE_AUDIO_HDMV_AC3_PLUS      0x84 
    66 #define STREAM_TYPE_AUDIO_HDMV_AC3_TRUE_HD   0x83 
    67 #define STREAM_TYPE_AUDIO_HDMV_DTS           0x82 
    68 #define STREAM_TYPE_AUDIO_HDMV_DTS_HD        0x85 
    69 #define STREAM_TYPE_AUDIO_HDMV_DTS_HD_MASTER 0x86 
    7067 
    7168#define STREAM_TYPE_SUBTITLE_DVB    0x100 
    7269#define STREAM_TYPE_VBI_DVB         0x101