ffmpeg中MPEG2 TS 流解码的流程分析

来源:互联网 发布:淘宝联盟 淘客助手 编辑:程序博客网 时间:2024/05/22 05:13

一、FFMPEG 中MPEG2 TS 流解码的流程分析

    说道具体的音频或者视频格式,一上来就是理论,那是国内混资历的所谓教授的做为, 
对于我们,不合适,还是用自己的方式理解这些晦涩不已的理论吧。 
    其实MPEG2 是一族协议,至少已经成为ISO 标准的就有以下几部分: 
    ISO/IEC-13818-1:系统部分; 
    ISO/IEC-13818-2:视频编码格式; 
    ISO/IEC-13818-3:音频编码格式; 
    ISO/IEC-13818-4:一致性测试; 
    ISO/IEC-13818-5:软件部分; 
    ISO/IEC-13818-6:数字存储媒体命令与控制; 
    ISO/IEC-13818-7:高级音频编码; 
    ISO/IEC-13818-8:系统解码实时接口; 
    我不是很想说实际的音视频编码格式,毕竟协议已经很清楚了,我主要想说说这些部分 
怎么组合起来在实际应用中工作的。 
    第一部分(系统部分)很重要,是构成以MPEG2  为基础的应用的基础.  很绕口,是吧, 
我简单解释一下:比如DVD  实际上是以系统部分定义 的PS  流为基础,加上版权管理等其 
他技术构成的。而我们的故事主角,则是另外 种流格式,TS  流,它在现阶段最大的应用 
是在数字电视节目 的传输 存储上,因此,你可以理解TS  实际上是 种传输协议, 实 
际传输的负载关系不大,只是在TS 中传输了音频,视频或者其他数据。先说一下为什么会 
有这两种格式的出现,PS 适用于没有损耗的环境下面存储,而TS 则适用于可能出现损耗或 
者错误的各种物理网络环境,比如你在公交上看 的电视,很有可能就是基于TS 的DVB-T 
的应用:) 
    我们再来看MPEG2 协议中的一些概念,为理解代码做好功课: 
    l ES(Elementary Stream): 
    wiki 上说An elementary stream (ES) is defined by MPEG communication protocol is
usually the output of an audio or video encoder” 
    恩,很简单吧,就是编码器编出的 组数据,可能是音频的,视频的,或者其他数据。 
说到着,其实可以对编码器的流程思考一下,无非是执行:采样,量化,编码这3个步骤中 
的编码而已(有些设备可能会包含前面的采样和量化) 。关于视频编码的基本理论,还是请 
参考其它的资料。 
    l PES(Packetized Elementary Stream): 
    wiki 上说allows an Elementary stream to be divided into packets” 
    其实可以理解成,把 个源源不断的数据(音频,视频或者其他)流,打断成 段 段, 
以便处理. 
    l TS(Transport Stream): 
    l PS(Program Stream): 
    这两个上面已经有所提及,后面会详细分析TS,我对PS 格式兴趣不大. 
    步入正题 
    才进入正题,恩,看来闲话太多了:(,直接看Code. 
    前面说过,TS  是 种传输协议,因此,对应 FFmpeg,可以认为他是 种封装格式。

因此,对应的代码应该先去libavformat 里面找,很容易找 ,就是mpegts.c:)。还是逐步看 
过来:

[libavformat/utils.c] 

[cpp] view plaincopyprint?
  1. int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,   
  2.                     AVInputFormat *fmt,   
  3.                     int buf_size,   
  4.                     AVFormatParameters *ap)   
  5. {  
  6.     int err, probe_size;   
  7.     AVProbeData probe_data, *pd = &probe_data;   
  8.     ByteIOContext *pb = NULL;   
  9.     pd->filename = "";   
  10.     if (filename)   
  11.         pd->filename = filename;   
  12.     pd->buf = NULL;   
  13.     pd->buf_size = 0;   
  14.     ################################################################################  
  15.     【1】这段代码其实是为了针对不需要Open文件的容器Format 的探测,其实就是使用   
  16.      AVFMT_NOFILE标记的容器格式单独处理,现在只有使用了该标记的Demuxer很少,   
  17.      只有image2_demuxer,rtsp_demuxer,因此我们分析TS时候可以不考虑这部分   
  18.     ################################################################################   
  19.     if (!fmt) {   
  20.        /* guess format if no file can be opened */   
  21.         fmt = av_probe_input_format(pd, 0);   
  22.     }   
  23.     /* Do not open file if the format does not need it. XXX: specific  
  24.         hack needed to handle RTSP/TCP */   
  25.     if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {   
  26.         /* if no file needed do not try to open one */   
  27.         #########################################################################   
  28.         【2】这个函数似乎很好理解,无非是带缓冲的IO的封装,不过我们既然此了,         
  29.         不妨跟踪下去,看看别人对带缓冲的IO 操作封装的实现:)   
  30.         #########################################################################   
  31.         if ((err=url_fopen(&pb, filename, URL_RDONLY)) < 0) {   
  32.             goto fail;   
  33.         }   
  34.         if (buf_size > 0) {   
  35.             url_setbufsize(pb, buf_size);   
  36.         }   
  37.         for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){  
  38.             int score= probe_size < PROBE_BUF_MAX ? AVPROBE_SCORE_MAX/4 : 0;   
  39.             /* read probe data */   
  40.             pd->buf= av_realloc(pd->buf, probe_size + AVPROBE_PADDING_SIZE);   
  41.             #######################################################################   
  42.             【3】真正将文件读入 pd 的buffer的地方,实际上最终调用FILE protocol   
  43.              的file_read(),将内容读入 pd 的buf,具体代码如果有兴趣可以自己跟踪   
  44.             #######################################################################   
  45.             pd->buf_size = get_buffer(pb, pd->buf, probe_size);   
  46.             memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);   
  47.             if (url_fseek(pb, 0, SEEK_SET) < 0) {   
  48.                 url_fclose(pb);   
  49.                 if (url_fopen(&pb, filename, URL_RDONLY) < 0) {   
  50.                     pb = NULL;   
  51.                     err = AVERROR(EIO);   
  52.                     goto fail;   
  53.                 }   
  54.             }   
  55.             #####################################################################   
  56.             【4】此时的pd已经有了需要分析的原始文件,只需要查找相应容器format   
  57.              的Tag 比较,以判断读入的究竟为什么容器格式,这里   
  58.             #####################################################################   
  59.             /* guess file format */   
  60.             fmt = av_probe_input_format2(pd, 1, &score);   
  61.         }   
  62.         av_freep(&pd->buf);   
  63.     }   
  64.     /* if still no format found, error */   
  65.     if (!fmt) {   
  66.         err = AVERROR_NOFMT;   
  67.         goto fail;   
  68.     }   
  69.     /* check filename in case an image number is expected */   
  70.     if (fmt->flags & AVFMT_NEEDNUMBER) {   
  71.         if (!av_filename_number_test(filename)) {   
  72.             err = AVERROR_NUMEXPECTED;   
  73.             goto fail;   
  74.         }   
  75.     }   
  76.     err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap);   
  77.     if (err)   
  78.         goto fail;   
  79.     return 0;   
  80. fail:   
  81.     av_freep(&pd->buf);   
  82.     if (pb)   
  83.         url_fclose(pb);   
  84.     *ic_ptr = NULL;   
  85.     return err;   
  86. }  


【2】带缓冲IO的封装的实现 [liavformat/aviobuf.c] 

[cpp] view plaincopyprint?
  1. int url_fopen(ByteIOContext **s, const char *filename, int flags)   
  2. {   
  3.     URLContext *h;   
  4.     int err;   
  5.     err = url_open(&h, filename, flags);   
  6.     if (err < 0)   
  7.         return err;   
  8.     err = url_fdopen(s, h);   
  9.     if (err < 0) {   
  10.         url_close(h);   
  11.         return err;   
  12.     }   
  13.     return 0;   
  14. }  


    可以看 ,下面的这个函数,先查找是否是FFmpeg支持的protocol的格式,如果文件 
名不符合,则默认是FILE protocol 格式,很显然,这里protocol判断是以URL的方式判读 
的,因此基本上所有的IO接口函数都是url_xxx的形式。 
    在这也可以看 ,FFmpeg 支持的protocol 有: 
        /* protocols */ 
        REGISTER_PROTOCOL (FILE, file); 
        REGISTER_PROTOCOL (HTTP, http); 
        REGISTER_PROTOCOL (PIPE, pipe); 
        REGISTER_PROTOCOL (RTP, rtp); 
        REGISTER_PROTOCOL (TCP, tcp); 
        REGISTER_PROTOCOL (UDP, udp); 
    而大部分情况下,如果你不指明类似file://xxx,http://xxx  格 式,它都以FILE  protocol
来处理。

[liavformat/avio.c] 

[cpp] view plaincopyprint?
  1. int url_open(URLContext **puc, const char *filename, int flags)   
  2. {  
  3.     URLProtocol *up;   
  4.     const char *p;   
  5.     char proto_str[128], *q;   
  6.     p = filename;   
  7.     q = proto_str;   
  8.     while (*p  != '\0' && *p  != ':') {   
  9.         /* protocols can only contain alphabetic chars */   
  10.         if (!isalpha(*p))   
  11.             goto file_proto;   
  12.         if ((q - proto_str) < sizeof(proto_str) - 1)   
  13.             *q++ = *p;   
  14.         p++;   
  15.     }   
  16.     /* if the protocol has length 1, we consider it is a dos drive */   
  17.     if (*p == '\0' || (q - proto_str) <= 1) {   
  18.     file_proto:   
  19.         strcpy(proto_str, "file");   
  20.     } else {   
  21.         *q = '\0';   
  22.     }   
  23.     up = first_protocol;   
  24.     while (up  != NULL) {   
  25.         if (!strcmp(proto_str, up->name))   
  26.             #################################################################   
  27.             很显然,此时已经知道up,filename,flags   
  28.             #################################################################   
  29.             return url_open_protocol (puc, up, filename, flags);   
  30.         up = up->next;   
  31.     }   
  32.     *puc = NULL;   
  33.     return AVERROR(ENOENT);   
  34. }  


[libavformat/avio.c] 

[cpp] view plaincopyprint?
  1. int url_open_protocol (URLContext **puc, struct URLProtocol *up,   
  2.                      const char *filename, int flags)   
  3. {   
  4.     URLContext *uc;   
  5.     int err;   
  6.   
  7.     ##########################################################################   
  8.      【a】? 为什么这样分配空间   
  9.     ##########################################################################   
  10.     uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);   
  11.     if (!uc) {   
  12.         err = AVERROR(ENOMEM);   
  13.         goto fail;   
  14.     }   
  15. #if LIBAVFORMAT_VERSION_MAJOR >= 53   
  16.     uc->av_class = &urlcontext_class;   
  17. #endif   
  18.     ##########################################################################   
  19.      【b】? 这样的用意又是为什么   
  20.     ##########################################################################   
  21.     uc->filename = (char *) &uc[1];   
  22.     strcpy(uc->filename, filename);   
  23.     uc->prot = up;   
  24.     uc->flags = flags;   
  25.     uc->is_streamed = 0; /* default = not streamed */   
  26.     uc->max_packet_size = 0; /* default: stream file */   
  27.     err = up->url_open(uc, filename, flags);   
  28.     if (err < 0) {   
  29.         av_free(uc);   
  30.         *puc = NULL;   
  31.         return err;   
  32.     }   
  33.     //We must be carefull here as url_seek() could be slow, for example for   
  34.     //http   
  35.     if((flags & (URL_WRONLY | URL_RDWR)) || !strcmp(up->name, "file"))   
  36.         if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)   
  37.             uc->is_streamed= 1;   
  38.         *puc = uc;   
  39.     return 0;   
  40.  fail:   
  41.     *puc = NULL;   
  42.     return err;   
  43. }  


    上面这个函数不难理解,但有些地方颇值得玩味,比如上面给出问号的地方,你明白 
为什么这样Coding么?很显然,此时up->url_open()实际上调用的是 file_open()
[libavformat/file.c],看完这个函数,对上面的内存分配,是否恍然大悟:) 
    上面只是分析了url_open(),还没有分析url_fdopen(s, h);这部分代码,也留给有好
奇心的你了:)恩,为了追踪这个流程,走得有些远,但不是全然无用:) 
    于来了【4】,我们来看MPEG TS格式的侦测过程,这其实才是我们今天的主角 
    
4. MPEG TS格式的探测过程

[liavformat/mpegts.c] 

[cpp] view plaincopyprint?
  1. static int mpegts_probe(AVProbeData *p)   
  2. {   
  3. #if 1   
  4.     const int size= p->buf_size;   
  5.     int score, fec_score, dvhs_score;   
  6. #define CHECK_COUNT 10   
  7.     if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))   
  8.         return -1;   
  9.     score = analyze(p->buf, TS_PACKET_SIZE * CHECK_COUNT, TS_PACKET_SIZE, NULL);  
  10.     dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE *CHECK_COUNT, TS_DVHS_PACKET_SIZE, NULL);  
  11.     fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);  
  12. //  av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);  
  13. //  we need a clear definition for the returned score otherwise things will become messy sooner or later  
  14.     if(score > fec_score && score > dvhs_score && score > 6)  
  15.         return AVPROBE_SCORE_MAX + score - CHECK_COUNT;  
  16.     else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6)  
  17.         return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;   
  18.     else if(fec_score > 6)  
  19.         return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;  
  20.     else  
  21.         return -1;   
  22. #else   
  23.    /* only use the extension for safer guess */   
  24.     if (match_ext(p->filename, "ts"))   
  25.         return AVPROBE_SCORE_MAX;   
  26.     else   
  27.         return 0;   
  28. #endif   
  29. }  


    之所以会出现3种格式,主要原因是:TS标准是188Bytes,而小日本自己又弄了个 
192Bytes的DVH-S格式,第三种的204Bytes则是在188Bytes的基础上,加上16Bytes的 
FEC(前向纠错).

[cpp] view plaincopyprint?
  1. static int analyze(const uint8_t *buf, int size, int packet_size, int *index)   
  2. {   
  3.     int stat[packet_size];   
  4.     int i;   
  5.     int x=0;   
  6.     int best_score=0;   
  7.     memset(stat, 0, packet_size*sizeof(int));   
  8.   
  9.     ##########################################################################   
  10.     由于查找的特定格式至少3 个Bytes,因此,至少最后3 个Bytes 不用查找   
  11.     ##########################################################################   
  12.     for(x=i=0; i<size-3; i++){   
  13.         ######################################################################   
  14.         参看后面的协议说明   
  15.         ######################################################################   
  16.         if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){   
  17.             stat[x]++;   
  18.             if(stat[x] > best_score){   
  19.                 best_score= stat[x];   
  20.                 if(index)  
  21.                     *index= x;   
  22.             }   
  23.         }  
  24.         x++;   
  25.         if(x == packet_size)  
  26.             x= 0;   
  27.     }   
  28.     return best_score;   
  29. }   


    这个函数简单说来,是在size大小的buf中,寻找满足特定格式,长度为packet_size
的packet的个数,显然,返回的值越大越可能是相应的格式(188/192/204),其中的这个特
定格式,其实就是协议的规定格式: 

[cpp] view plaincopyprint?
  1.     Syntax                            No. of bits                 Mnemonic  
  2. transport_packet(){   
  3.     sync_byte                              8                     bslbf  
  4.     transport_error_indicator              1                     bslbf  
  5.     payload_unit_start_indicator           1                     bslbf  
  6.     transport_priority                     1                     bslbf  
  7.     PID                                    13                    uimsbf  
  8.     transport_scrambling_control           2                     bslbf  
  9.     adaptation_field_control               2                     bslbf  
  10.     continuity_counter                     4                     uimsbf  
  11.     if(adaptation_field_control=='10' || adaptation_field_control=='11'){  
  12.         adaptation_field()   
  13.     }   
  14.     if(adaptation_field_control=='01' || adaptation_field_control=='11') {  
  15.         for (i=0;i<N;i++){   
  16.             data_byte                      8                     bslbf  
  17.         }   
  18.     }  
  19. }      


    其中的sync_byte 固定为0x47,即上面的:    buf[i] == 0x47 
    由于transport_error_indicator 为1 的TS Packet 实际有错误,表示携带的数据无意义,
这样的Packet 显然没什么意义,因此:        !(buf[i+1] & 0x80) 
    对于adaptation_field_control,如果为取值为0x00,则表示为未来保留,现在不用,因此:
buf[i+3] & 0x30 
    这就是MPEG TS的侦测过程,很简单吧:) 
    后面我们分析如何从mpegts文件中获取stream 的过程。
      
5.渐入佳境 
    恩,前面的基础因该已近够了,有点像手剥洋葱头的感 ,我们来看看针对MPEG TS 
的相应解析过程。我们后面的代码,主要集中在[libavformat/mpegts.c]里面,毛爷爷说:集
中优势兵力打围歼,恩,开始吧,蚂蚁啃骨头。

[cpp] view plaincopyprint?
  1. static int mpegts_read_header(AVFormatContext *s,   
  2.                             AVFormatParameters *ap)   
  3. {   
  4.     MpegTSContext *ts = s->priv_data;   
  5.     ByteIOContext *pb = s->pb;   
  6.     uint8_t buf[1024];   
  7.     int len;   
  8.     int64_t pos;   
  9.     ......   
  10.     /* read the first  1024 bytes to get packet size */   
  11.     #####################################################################   
  12.     【1】有了前面分析缓冲IO 的经历,下面的代码就不是什么问题了:)   
  13.     #####################################################################   
  14.     pos = url_ftell(pb);   
  15.     len = get_buffer(pb, buf, sizeof(buf));   
  16.     if (len != sizeof(buf))   
  17.         goto fail;   
  18.     #####################################################################   
  19.     【2】前面侦测文件格式时候其实已经知道TS 包的大小了,这里又侦测 次,其实   
  20.      有些多余,估计是因为解码框架的原因,已近侦测的包大小没能从前面被带过来,   
  21.      可见框架虽好,却也会带来或多或少的 些不利影响   
  22.     #####################################################################   
  23.     ts->raw_packet_size = get_packet_size(buf, sizeof(buf));   
  24.     if (ts->raw_packet_size <= 0)   
  25.         goto fail;   
  26.     ts->stream = s;   
  27.     ts->auto_guess = 0;   
  28.   
  29.     if (s->iformat == &mpegts_demuxer)  {   
  30.         /* normal demux */   
  31.         /* first do a scaning to get all the services */   
  32.         url_fseek(pb, pos, SEEK_SET);   
  33.         #########  
  34.         【3】   
  35.         #########  
  36.         mpegts_scan_sdt(ts);   
  37.         #########  
  38.         【4 】   
  39.         #########  
  40.         mpegts_set_service(ts);   
  41.         #########  
  42.         【5】   
  43.         #########  
  44.         handle_packets(ts, s->probesize);   
  45.         /* if could not find service, enable auto_guess */   
  46.         ts->auto_guess = 1;   
  47. #ifdef DEBUG_SI   
  48.         av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");   
  49. #endif   
  50.         s->ctx_flags |= AVFMTCTX_NOHEADER;   
  51.     } else {   
  52.       ......   
  53.     }   
  54.     url_fseek(pb, pos, SEEK_SET);   
  55.     return 0;   
  56. fail:   
  57.     return -1;   
  58. }  



    这里简单说一下MpegTSContext *ts,从上面可以看 ,其实这是为了解码不同容器格式所使用的私有数据,
只有在相应的诸如mpegts.c文件才可以使用的,这样,增加了这个库的模块化,而模块化的最大好处,
则在于把问题集中了个很小的有限区域里面,如果你自己构造程序时候,不妨多参考其基本思想--这样的化,
你之后的代码,还有你之后的生活,都将轻松许多。
    【3】【4】其实调用的是同个函数:mpegts_open_section_filter() 我们来看看意欲何为。

[cpp] view plaincopyprint?
  1. static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,   
  2.                                            SectionCallback *section_cb,   
  3.                                            void *opaque,   
  4.                                            int check_crc)   
  5. {   
  6.     MpegTSFilter *filter;   
  7.     MpegTSSectionFilter *sec;   
  8. #ifdef DEBUG_SI   
  9.     av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);   
  10. #endif   
  11.     if (pid >= NB_PID_MAX || ts->pids[pid])   
  12.         return NULL;   
  13.     filter = av_mallocz(sizeof(MpegTSFilter));   
  14.     if (!filter)   
  15.         return NULL;   
  16.     ts->pids[pid] = filter;   
  17.     filter->type = MPEGTS_SECTION;   
  18.     filter->pid = pid;   
  19.     filter->last_cc = -1;   
  20.     sec = &filter->u.section_filter;   
  21.     sec->section_cb = section_cb;   
  22.     sec->opaque = opaque;   
  23.     sec->section_buf = av_malloc(MAX_SECTION_SIZE);   
  24.     sec->check_crc = check_crc;   
  25.     if (!sec->section_buf) {   
  26.         av_free(filter);   
  27.         return NULL;   
  28.     }   
  29.     return filter;   
  30. }   



    要完全明白这部分代码,其实需要分析作者对数据结构的定义: 
    依次为: 
            struct MpegTSContext; 
                     | 
                     V 
            struct MpegTSFilter; 
                     | 
                     V 
       +---------------+---------------+ 
       |                                    | 
       V                                   V 
       MpegTSPESFilter              MpegTSSectionFilter

    其实很简单,就是struct MpegTSContext;中有NB_PID_MAX(8192)个TS 的Filter,而 
每个struct MpegTSFilter 可能是PES 的Filter 或者Section 的Filter。 
    我们先说为什么是8192,在前面的分析中: 
    给出过TS 的语法结构: 
   

[cpp] view plaincopyprint?
  1. Syntax                       No. of bits       Mnemonic   
  2.     transport_packet(){   
  3.         sync_byte                     8            bslbf   
  4.         transport_error_indicator     1            bslbf   
  5.         payload_unit_start_indicator  1            bslbf   
  6.         transport_priority            1            bslbf   
  7.         PID                           13           uimsbf   
  8.         transport_scrambling_control  2            bslbf   
  9.         adaptation_field_control      2            bslbf   
  10.         continuity_counter            4            uimsbf   
  11.         if(adaptation_field_control=='10' || adaptation_field_control=='11'){  
  12.             adaptation_field()   
  13.         }   
  14.         if(adaptation_field_control=='01' || adaptation_field_control=='11') {   
  15.             for (i=0;i<N;i++){   
  16.                 data_byte             8            bslbf   
  17.             }   
  18.         }   
  19.     }   


    而8192,则是2^13=8192(PID)的最大数目,而为什么会有PES 和Section 的区分,请参 
考ISO/IEC-13818-1,我实在不太喜欢重复已有的东西. 
    可见【3】【4】,就是挂载了两个Section 类型的过滤器,其实在TS 的两种负载中,section 
是PES 的元数据,只有先解析了section,才能进 步解析PES 数据,因此先挂上section 的 
过滤器。 
    挂载上了两种 section 过滤器,如下: 
    ================================================================== 
    PID                     Section Name                        Callback 
    ================================================================== 
    SDT_PID(0x0011)       ServiceDescriptionTable                  sdt_cb 
    PAT_PID(0x0000)       ProgramAssociationTable                  pat_cb

    既然自是挂上Callback,自然是在后面的地方使用,因此,我们还是继续 
    【5】处的代码看看是最重要的地方了,简单看来: 
    handle_packets() 
        | 
        +->read_packet() 
        | 
        +->handle_packet() 
            | 
            +->write_section_data() 
    read_packet()很简单,就是去找sync_byte(0x47),而看来handle_packet()才会是我们真正 
因该关注的地方了:) 
    这个函数很重要,我们贴出代码,以备分析:

[cpp] view plaincopyprint?
  1. /* handle one TS packet */   
  2. static void handle_packet(MpegTSContext *ts, const uint8_t *packet)   
  3. {   
  4.     AVFormatContext *s = ts->stream;   
  5.     MpegTSFilter *tss;   
  6.     int len, pid, cc, cc_ok, afc, is_start;   
  7.     const uint8_t *p, *p_end;   
  8.   
  9.     ##########################################################   
  10.     获取该包的PID   
  11.     ##########################################################   
  12.     pid = AV_RB16(packet + 1) & 0x1fff;   
  13.     if(pid && discard_pid(ts, pid))   
  14.         return;   
  15.     ##########################################################   
  16.     是否是PES 或者Section 的开头(payload_unit_start_indicator)   
  17.     ##########################################################   
  18.   
  19.     is_start = packet[1] & 0x40;   
  20.     tss = ts->pids[pid];   
  21.   
  22.     ##########################################################   
  23.     ts->auto_guess 此时为0,因此不考虑下面的代码   
  24.      ##########################################################   
  25.     if (ts->auto_guess && tss == NULL && is_start) {   
  26.        add_pes_stream(ts, pid, -1, 0);   
  27.        tss = ts->pids[pid];   
  28.     }   
  29.     if (!tss)   
  30.        return;   
  31.   
  32.     ##########################################################   
  33.     代码说的很清楚,虽然检查,但不利用检查的结果   
  34.     ##########################################################   
  35.     /* continuity check (currently not used) */   
  36.     cc = (packet[3] & 0xf);   
  37.     cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));   
  38.     tss->last_cc = cc;   
  39.   
  40.     ##########################################################   
  41.     跳 adaptation_field_control   
  42.     ##########################################################   
  43.     /* skip adaptation field */   
  44.     afc = (packet[3] >> 4) & 3;   
  45.     p = packet + 4;   
  46.     if (afc == 0) /* reserved value */   
  47.        return;   
  48.     if (afc == 2) /* adaptation field only */   
  49.        return;   
  50.     if (afc == 3) {   
  51.        /* skip adapation field */   
  52.        p += p[0] + 1;   
  53.     }   
  54.   
  55.     ##########################################################   
  56.     p已近 达TS 包中的有效负载的地方   
  57.     ##########################################################   
  58.     /* if past the end of packet, ignore */   
  59.     p_end = packet + TS_PACKET_SIZE;   
  60.     if (p >= p_end)   
  61.        return;   
  62.   
  63.     ts->pos47= url_ftell(ts->stream->pb) % ts->raw_packet_size;   
  64.   
  65.     if (tss->type == MPEGTS_SECTION)  {   
  66.         if (is_start) {   
  67.         ###############################################################   
  68.         针对Section,符合部分第 个字节为pointer field,该字段如果为0,   
  69.         则表示后面紧跟着的是Section的开头,否则是某Section的End部分和   
  70.          另 Section 的开头,因此,这里的流程实际上由两个值is_start   
  71.         (payload_unit_start_indicator)和len(pointer field)起来决定   
  72.          ###############################################################   
  73.         /* pointer field present */   
  74.         len = *p++;   
  75.         if (p + len > p_end)   
  76.             return;   
  77.         if (len && cc_ok) {   
  78.             ########################################################   
  79.             1).is_start == 1   
  80.             len > 0   
  81.             负载部分由A Section 的End 部分和B Section 的Start 组成,把A 的   
  82.               End 部分写入   
  83.               ########################################################   
  84.             /* write remaining section bytes */   
  85.             write_section_data(s, tss, p, len, 0);   
  86.             /* check whether filter has been closed */   
  87.             if (!ts->pids[pid])   
  88.                 return;   
  89.         }   
  90.         p += len;   
  91.         if (p < p_end) {   
  92.             ########################################################   
  93.             2).is_start == 1   
  94.             len > 0   
  95.             负载部分由A Section 的End 部分和B Section 的Start 组成,把B  的   
  96.               Start 部分写入   
  97.               或者:   
  98.               3).   
  99.             is_start == 1   
  100.             len == 0   
  101.             负载部分仅是 个Section 的Start 部分,将其写入   
  102.               ########################################################   
  103.             write_section_data(s, tss, p, p_end - p, 1);   
  104.         }   
  105.     } else if (cc_ok) {   
  106.         ########################################################   
  107.         4).is_start == 0   
  108.         负载部分仅是 个Section 的中间部分部分,将其写入  
  109.          ########################################################   
  110.         write_section_data(s, tss, p, p_end - p, 0);   
  111.     } else {   
  112.         ##########################################################   
  113.         如果是PES 类型,直接调用其Callback,但显然,只有Section 部分   
  114.          解析完成后才可能解析PES   
  115.         ##########################################################   
  116.         tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start);   
  117.     }   
  118. }  


    write_section_data()函数则反复收集buffer中的数据,指导完成相关Section的重组过 
程,然后调用之前注册的两个section_cb: 
    后面我们将分析之前挂在的两个section_cb

二、mpegts.c文件分析

1 综述 
    ffmpeg 框架对应MPEG-2 TS 流的解析的代码在mpegts.c 文件中,该文件有两个解复 
用的实例:mpegts_demuxer 和mpegtsraw_demuxer,mpegts_demuxer 对应的真实的TS 流格 
式,也就是机顶盒直接处理的 TS 流,本文主要分析和该种格式相关 的代码; 
mpegtsraw_demuxer 这个格式我没有遇见过,本文中不做分析。本文针对的ffmpeg 的版本是 
0.5 版本。 
2 mpegts_demuxer 结构分析 
   

[cpp] view plaincopyprint?
  1. AVInputFormat mpegts_demuxer = {au   
  2.        "mpegts"//demux 的名称   
  3.        NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),// 如果定义了   
  4. ONFIG_SMALL 宏,该域返回NULL,也就是取消long_name 域的定义。  
  5.        sizeof(MpegTSContext),//每个demuxer 的结构的私有域的大小   
  6.        mpegts_probe,//检测是否是TS 流格式   
  7.        mpegts_read_header,//下文介绍   
  8.        mpegts_read_packet,//下文介绍   
  9.        mpegts_read_close,//关闭demuxer   
  10.        read_seek,//下文介绍   
  11.        mpegts_get_pcr,//下文介绍   
  12.        .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,//下文介绍   
  13.    };   


    
    该结构通过av_register_all 函数注册 ffmpeg 的主框架中,通过mpegts_probe 函数来 
检测是否是TS 流格式,然后通过mpegts_read_header 函数找路音频流和路视频流(注 
意:在该函数中没有找全所有的音频流和视频流),最后调用mpegts_read_packet函数将找 
的音频流和视频流数据提取出来,通过主框架推入解码器。

3 mpegts_probe 函数分析 
    mpegts_probe被av_probe_input_format2调用,根据返回的score来判断那种格式的可 
能性最大。mpegts_probe调用了analyze函数,我们先分析一下analyze函数。

[cpp] view plaincopyprint?
  1. static int analyze(const uint8_t *buf, int size, int packet_size, int *index)  
  2. {      
  3.     int stat[TS_MAX_PACKET_SIZE];//积分统计结果   
  4.     int i;   
  5.     int x=0;   
  6.     int best_score=0;   
  7.     memset(stat, 0, packet_size*sizeof(int));   
  8.     for(x=i=0; i<size-3; i++){   
  9.         if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){   
  10.             stat[x]++;   
  11.             if(stat[x] > best_score){   
  12.                 best_score= stat[x];   
  13.                 if(index)  
  14.                     *index= x;   
  15.             }   
  16.         }   
  17.         x++;   
  18.         if(x == packet_size)  
  19.             x= 0;   
  20.     }   
  21.     return best_score;   
  22. }  


    analyze 函数的思路: 
    buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)是TS 流同步开始的模式, 
0x47 是TS 流同步的标志,(buf[i+1] & 0x80 是传输错误标志,buf[i+3] & 0x30 为0 时表示为 
ISO/IEC 未来使用保留,目前不存在这样的值。记该模式为TS 流同步模式” 
    stat 数组变量存储的是TS 流同步模式”在某个位置出现的次数。 
    analyze 函数扫描检测数据,如果发现该模式,则stat[i%packet_size]++(函数中的x 变 
量就是用来取模运算的,因为当x==packet_size时,x就被归零),扫描完后,自然是同步 
位置的累加值最大。 
    mpegts_probe函数通过调用analyze函数来得 相应的分数,ffmpeg框架会根据该分 
数判断是否是对应的格式,返回对应的AVInputFormat 实例。
 
4 mpegts_read_header函数分析 
    下文中省略号代表的是和mpegtsraw_demuxer相关的代码,暂不涉及。 
      

[cpp] view plaincopyprint?
  1. <pre class="cpp" name="code">static int mpegts_read_header(AVFormatContext *s,   
  2.                            AVFormatParameters *ap)   
  3. {   
  4.     MpegTSContext *ts = s->priv_data;   
  5.     ByteIOContext *pb = s->pb;   
  6.     uint8_t buf[5*1024];   
  7.     int len;   
  8.     int64_t pos;   
  9.     ......   
  10.     //保存流的当前位置,便于检测操作完成后恢复 原来的位置,   
  11.     //这样在播放的时候就不会浪费 段流。   
  12.     pos = url_ftell(pb);   
  13.     //读取 段流来检测TS 包的大小   
  14.     len = get_buffer(pb, buf, sizeof(buf));   
  15.     if (len != sizeof(buf))   
  16.         goto fail;   
  17.     //得 TS 流包的大小,通常是188bytes,我目前见过的都是188 个字节的。   
  18.     //TS 包的大小有三种:   
  19.     //1) 通常情况下的188 字节   
  20.     //2) 日本弄了个192Bytes 的DVH-S 格式   
  21.     //3)在188Bytes 的基础上,加上16Bytes 的FEC(前向纠错),也就是204bytes   
  22.     ts->raw_packet_size = get_packet_size(buf, sizeof(buf));   
  23.     if (ts->raw_packet_size <= 0)   
  24.         goto fail;   
  25.     ts->stream = s;   
  26.   
  27.     //auto_guess = 1,  则在handle_packet 的函数中只要发现 个PES 的pid 就   
  28.     //建立该PES 的stream   
  29.     //auto_guess = 0,  则忽略。   
  30.     //auto_guess 主要作用是用来在TS 流中没有业务信息时,如果被设置成了1 的话,   
  31.     //那么就会将任何 个PID 的流当做媒体流建立对应的PES 数据结构。   
  32.     //在mpegts_read_header 函数的过程中发现了PES 的pid,但   
  33.     //是不建立对应的流,只是分析PSI 信息。   
  34.     //相关的代码见handle_packet 函数的下面的代码:   
  35.      //tss = ts->pids[pid];   
  36.     //if (ts->auto_guess && tss == NULL && is_start) {   
  37.     //   add_pes_stream(ts, pid, -1, 0);   
  38.     //   tss = ts->pids[pid];   
  39.     //}   
  40.     ts->auto_guess = 0;   
  41.     if (s->iformat == &mpegts_demuxer)  {   
  42.         /* normal demux */   
  43.         /* first do a scaning to get all the services */   
  44.         url_fseek(pb, pos, SEEK_SET);   
  45.         //挂载解析SDT 表的回调函数 ts->pids 变量上,     //这样在handle_packet 函数中根据对应的pid 找 对应处理回调函数。   
  46.          mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);   
  47.         //同上,只是挂上PAT 表解析的回调函数   
  48.          mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);   
  49.         //探测 段流,便于检测出SDT,PAT,PMT 表   
  50.          handle_packets(ts, s->probesize);   
  51.         /* if could not find service, enable auto_guess */   
  52.   
  53.         //打开add pes stream 的标志,这样在handle_packet 函数中发现了pes 的   
  54.          //pid,就会自动建立该pes 的stream。   
  55.          ts->auto_guess = 1;   
  56.         dprintf(ts->stream, "tuning done\n");   
  57.         s->ctx_flags |= AVFMTCTX_NOHEADER;   
  58.     } else {   
  59.        ......   
  60.     }    
  61.     //恢复检测前的位置。  
  62.      url_fseek(pb, pos, SEEK_SET);  
  63.     return 0;   
  64. fail:   
  65.     return -1;   
  66. }  
  67. </pre>  
  68. <pre></pre>  
  69. <p> </p>  
  70. <p>    下面介绍被 mpegts_read_header直接或者间接调用的几个函数: mpegts_open_section_filter,     handle_packets,handle_packet     5 mpegts_open_section_filter 函数分析     这个函数可以解释mpegts.c 代码结构的精妙之处,PSI 业务信 息表的处理 都是通过该函数挂载 MpegTSContext 结构的pids 字段上的。这样如果你 想增加别的业务信息的表处理函数只要通过这个函数来挂载即可,体现了  
  71.  软件设计的著名的开闭”原则。下面分析一下他的代码。        </p>  
  72. <pre class="cpp" name="code">static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,  
  73.                                            SectionCallback *section_cb, void *opaque,   
  74.                                            int check_crc)   
  75. {   
  76.     MpegTSFilter *filter;   
  77.     MpegTSSectionFilter *sec;   
  78.     dprintf(ts->stream, "Filter: pid=0x%x\n", pid);   
  79.     if (pid >= NB_PID_MAX || ts->pids[pid])   
  80.         return NULL;   
  81.     //给filter 分配空间,挂载 MpegTSContext  的pids 上   
  82.     //就是该实例   
  83.     filter = av_mallocz(sizeof(MpegTSFilter));   
  84.     if (!filter)   
  85.         return NULL;   
  86.     //挂载filter 实例   
  87.     ts->pids[pid] = filter;   
  88.     //设置filter 相关的参数,因为业务信息表的分析的单 是段,   
  89.     //所以该filter 的类型是MPEGTS_SECTION   
  90.     filter->type = MPEGTS_SECTION;   
  91.   
  92.     //设置pid   
  93.     filter->pid = pid;   
  94.     filter->last_cc = -1;   
  95.     //设置filter 回调处理函数   
  96.     sec = &filter->u.section_filter;   
  97.     sec->section_cb = section_cb;   
  98.     sec->opaque = opaque;   
  99.     //分配段数据处理的缓冲区,调用handle_packet 函数后会调用   
  100.     //write_section_data 将ts 包中的业务信息表的数据存储在这儿,   
  101.     //直  个段收集完成才交付上面注册的回调函数处理。   
  102.     sec->section_buf = av_malloc(MAX_SECTION_SIZE);   
  103.     sec->check_crc = check_crc;   
  104.     if (!sec->section_buf) {   
  105.         av_free(filter);   
  106.         return NULL;   
  107.     }   
  108.     return filter;   
  109. }  
  110.   
  111. </pre>  
  112. <p><br>  
  113. 6 handle_packets 函数分析 <br>  
  114.     handle_packets 函数在两个地方被调用, 个是mpegts_read_header 函数中, <br>  
  115. 另外 个是mpegts_read_packet 函数中,被mpegts_read_header 函数调用是用 <br>  
  116. 来搜索PSI 业务信息,nb_packets  参数为探测的ts 包的个数;在mpegts_read_packet <br>  
  117. 函数中被调用用来搜索补充PSI 业务信息和demux PES 流,nb_packets 为0,0 不 <br>  
  118. 是表示处理的包的个数为0。 </p>  
  119. <pre class="cpp" name="code">static int handle_packets(MpegTSContext *ts, int nb_packets)   
  120. {   
  121.     AVFormatContext *s = ts->stream;   
  122.     ByteIOContext *pb = s->pb;   
  123.     uint8_t packet[TS_PACKET_SIZE];   
  124.     int packet_num, ret;   
  125.     //该变量指示 次handle_packets 处理的结束。   
  126.     //在mpegts_read_packet 被调用的时候,如果发现完 个PES 的包,则   
  127.     // ts->stop_parse = 1 ,则当前分析结束。   
  128.     ts->stop_parse = 0;   
  129.     packet_num = 0;   
  130.     for(;;) {   
  131.         if (ts->stop_parse>0)   
  132.             break;   
  133.         packet_num++;   
  134.         if (nb_packets != 0 && packet_num >= nb_packets)   
  135.             break;   
  136.         //读取 个ts 包,通常是188bytes   
  137.         ret = read_packet(pb, packet, ts->raw_packet_size);   
  138.         if (ret != 0)   
  139.             return ret;   
  140.         handle_packet(ts, packet);   
  141.     }   
  142.     return 0;   
  143. }   
  144.   
  145. </pre>  
  146. <p><br>  
  147.  </p>  
  148. <p>7 handle_packet 函数分析 <br>  
  149.     可以说handle_packet 是mpegts.c 代码的核心,所有的其他代码都是为 <br>  
  150. 这个函数准备的。 <br>  
  151.     在调用该函数之前先调用read_packet 函数获得个ts包(通常是188bytes), <br>  
  152. 然后传给该函数,packet参数就是TS包。</p>  
  153. <pre class="cpp" name="code">static int handle_packet(MpegTSContext *ts, const uint8_t *packet)   
  154. {   
  155.     AVFormatContext *s = ts->stream;   
  156.     MpegTSFilter *tss;   
  157.     int len, pid, cc, cc_ok, afc, is_start;   
  158.     const uint8_t *p, *p_end;   
  159.     int64_t pos;   
  160.     //从TS 包获得包的PID。   
  161.     pid = AV_RB16(packet + 1) & 0x1fff;   
  162.     if(pid && discard_pid(ts, pid))   
  163.         return 0;   
  164.     is_start = packet[1] & 0x40;   
  165.     tss = ts->pids[pid];   
  166.     //ts->auto_guess 在mpegts_read_header 函数中被设置为0,   
  167.     //也就是说在ts 检测过程中是不建立pes stream 的。   
  168.     if (ts->auto_guess && tss == NULL && is_start) {   
  169.         add_pes_stream(ts, pid, -1, 0);   
  170.         tss = ts->pids[pid];   
  171.     }   
  172.     //mpegts_read_header 函数调用handle_packet 函数只是处理TS 流的   
  173.     //业务信息,因为并没有为对应的PES 建立tss,所以tss 为空,直接返回。   
  174.     if (!tss)   
  175.         return 0;   
  176.     /* continuity check (currently not used) */   
  177.     cc = (packet[3] & 0xf);   
  178.     cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));   
  179.     tss->last_cc = cc;   
  180.     /* skip adaptation field */   
  181.     afc = (packet[3] >> 4) & 3;   
  182.     p = packet + 4;   
  183.     if (afc == 0) /* reserved value */   
  184.         return 0;   
  185.     if (afc == 2) /* adaptation field only */   
  186.         return 0;   
  187.     if (afc == 3) {   
  188.         /* skip adapation field */   
  189.         p += p[0] + 1;   
  190.     }   
  191.     /* if past the end of packet, ignore */   
  192.     p_end = packet + TS_PACKET_SIZE;   
  193.     if (p >= p_end)   
  194.         return 0;   
  195.     pos = url_ftell(ts->stream->pb);   
  196.     ts->pos47= pos % ts->raw_packet_size;   
  197.     if (tss->type == MPEGTS_SECTION)  {   
  198.         //表示当前的TS 包包含 个新的业务信息段   
  199.         if (is_start) {  
  200.             //获取pointer field 字段,   
  201.             //新的段从pointer field 字段指示的位置开始   
  202.               len = *p++;   
  203.             if (p + len > p_end)   
  204.                 return 0;   
  205.             if (len && cc_ok) {   
  206.                 //这个时候TS 的负载有两个部分构成:   
  207.                    //1)从TS 负载开始 pointer field 字段指示的位置;   
  208.                 //2)从pointer field 字段指示的位置 TS 包结束   
  209.   
  210.                 //1)位置代表的是上 个段的末尾部分。   
  211.                 //2)位置代表的新的段开始的部分。   
  212.                 //下面的代码是保存上 个段末尾部分数据,也就是   
  213.                 //1)位置的数据。   
  214.                 write_section_data(s, tss, p, len, 0);   
  215.                 /* check whether filter has been closed */   
  216.                 if (!ts->pids[pid])   
  217.                     return 0;   
  218.             }   
  219.             p += len;   
  220.             //保留新的段数据,也就是2)位置的数据。   
  221.               if (p < p_end) {   
  222.                 write_section_data(s, tss, p, p_end - p, 1);   
  223.             }   
  224.         } else {   
  225.            //保存段中间的数据。   
  226.             if (cc_ok) {   
  227.                 write_section_data(s, tss, p, p_end - p, 0);   
  228.             }   
  229.         }   
  230.     } else {   
  231.         int ret;   
  232.         //正常的PES 数据的处理   
  233.         // Note: The position here points actually behind the current packet.   
  234.         if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,   
  235.                              pos - ts->raw_packet_size)) < 0)   
  236.             return ret;   
  237.     }   
  238.     return 0;   
  239. }  
  240.   
  241. </pre>  
  242. <p><br>  
  243. 8 write_section_data 函数分析 <br>  
  244.     PSI 业务信息表在TS流中是以段为单传输的。 </p>  
  245. <pre class="cpp" name="code">static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,   
  246.                      const uint8_t *buf, int buf_size, int is_start)   
  247. {   
  248.     MpegTSSectionFilter *tss = &tss1->u.section_filter;   
  249.     int len;   
  250.     //buf 中是 个段的开始部分。   
  251.     if (is_start) {   
  252.         //将内容复制 tss->section_buf 中保存   
  253.         memcpy(tss->section_buf, buf, buf_size);   
  254.         //tss->section_index 段索引。   
  255.         tss->section_index = buf_size;   
  256.         //段的长度,现在还不知道,设置为-1   
  257.         tss->section_h_size = -1;   
  258.         //是否 达段的结尾。   
  259.         tss->end_of_section_reached = 0;   
  260.     } else {   
  261.         //buf 中是段中间的数据。   
  262.         if (tss->end_of_section_reached)   
  263.             return;   
  264.         len = 4096 - tss->section_index;   
  265.         if (buf_size < len)   
  266.             len = buf_size;  
  267.         memcpy(tss->section_buf + tss->section_index, buf, len);   
  268.         tss->section_index += len;   
  269.     }   
  270.     //如果条件满足,计算段的长度   
  271.     if (tss->section_h_size == -1 && tss->section_index >= 3) {   
  272.         len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;   
  273.         if (len > 4096)   
  274.             return;   
  275.         tss->section_h_size = len;   
  276.     }   
  277.     //判断段数据是否收集完毕,如果收集完毕,调用相应的回调函数处理该段。   
  278.     if (tss->section_h_size  != -1 && tss->section_index >= tss->section_h_size) {   
  279.         tss->end_of_section_reached = 1;   
  280.         if (!tss->check_crc ||   
  281.             av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,   
  282.                   tss->section_buf, tss->section_h_size) == 0)   
  283.             tss->section_cb(tss1, tss->section_buf, tss->section_h_size);   
  284.     }   
  285. }   
  286.   
  287. </pre>  
  288. <p><br>  
  289.  </p>  
  290. <pre></pre>  
  291. <pre></pre>  
原创粉丝点击