FFMPeg代码分析:AVPacket结构体和av_read_frame函数

AVPacket结构用于保存压缩编码过的数据。在解码时,该结构的实例通常作为解复用器(demuxer)的输出并输入到解码器中;在编码时,通常是编码器的输出,并输入到复用器(muxer)中。该结构体的定义如下:

typedef struct AVPacket {
    /**
     * A reference to the reference-counted buffer where the packet data is
     * stored.
     * May be NULL, then the packet data is not reference-counted.
     */
    AVBufferRef *buf;
    /**
     * Presentation timestamp in AVStream->time_base units; the time at which
     * the decompressed packet will be presented to the user.
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     * pts MUST be larger or equal to dts as presentation cannot happen before
     * decompression, unless one wants to view hex dumps. Some formats misuse
     * the terms dts and pts/cts to mean something different. Such timestamps
     * must be converted to true pts/dts before they are stored in AVPacket.
     */
    int64_t pts;//显示时间戳
    /**
     * Decompression timestamp in AVStream->time_base units; the time at which
     * the packet is decompressed.
     * Can be AV_NOPTS_VALUE if it is not stored in the file.
     */
    int64_t dts;//解码时间戳
    uint8_t *data;//实例所包含的压缩数据,直接获取该指针指向缓存的数据可以得到压缩过的码流;
    int   size;//原始压缩数据的大小
    int   stream_index;//标识当前AVPacket所从属的码流
    /**
     * A combination of AV_PKT_FLAG values
     */
    int   flags;
    /**
     * Additional packet data that can be provided by the container.
     * Packet can contain several types of side information.
     */
    struct {
        uint8_t *data;
        int      size;
        enum AVPacketSideDataType type;
    } *side_data;
    int side_data_elems;

    /**
     * Duration of this packet in AVStream->time_base units, 0 if unknown.
     * Equals next_pts - this_pts in presentation order.
     */
    int   duration;
#if FF_API_DESTRUCT_PACKET
    attribute_deprecated
    void  (*destruct)(struct AVPacket *);
    attribute_deprecated
    void  *priv;
#endif
    int64_t pos;                            ///< byte position in stream, -1 if unknown

    /**
     * Time difference in AVStream->time_base units from the pts of this
     * packet to the point at which the output from the decoder has converged
     * independent from the availability of previous frames. That is, the
     * frames are virtually identical no matter if decoding started from
     * the very first frame or from this keyframe.
     * Is AV_NOPTS_VALUE if unknown.
     * This field is not the display duration of the current packet.
     * This field has no meaning if the packet does not have AV_PKT_FLAG_KEY
     * set.
     *
     * The purpose of this field is to allow seeking in streams that have no
     * keyframes in the conventional sense. It corresponds to the
     * recovery point SEI in H.264 and match_time_delta in NUT. It is also
     * essential for some types of subtitle streams to ensure that all
     * subtitles are correctly displayed after seeking.
     */
    int64_t convergence_duration;
} AVPacket;

在demo中,调用了av_read_frame(pFormatCtx, &packet)函数从pFormatCtx所指向的环境的文件中读取压缩码流数据,保存到AVPacket实例packet中。av_read_frame()函数实现如下所示:

int av_read_frame(AVFormatContext *s, AVPacket *pkt)
{
    const int genpts = s->flags & AVFMT_FLAG_GENPTS;
    int          eof = 0;
    int ret;
    AVStream *st;

    if (!genpts) {
        ret = s->packet_buffer ?
            read_from_packet_buffer(&s->packet_buffer, &s->packet_buffer_end, pkt) :
            read_frame_internal(s, pkt);
        if (ret < 0)
            return ret;
        goto return_packet;
    }

    for (;;) {
        AVPacketList *pktl = s->packet_buffer;

        if (pktl) {
            AVPacket *next_pkt = &pktl->pkt;

            if (next_pkt->dts != AV_NOPTS_VALUE) {
                int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
                // last dts seen for this stream. if any of packets following
                // current one had no dts, we will set this to AV_NOPTS_VALUE.
                int64_t last_dts = next_pkt->dts;
                while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
                    if (pktl->pkt.stream_index == next_pkt->stream_index &&
                        (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0)) {
                        if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) { //not b frame
                            next_pkt->pts = pktl->pkt.dts;
                        }
                        if (last_dts != AV_NOPTS_VALUE) {
                            // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
                            last_dts = pktl->pkt.dts;
                        }
                    }
                    pktl = pktl->next;
                }
                if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
                    // Fixing the last reference frame had none pts issue (For MXF etc).
                    // We only do this when
                    // 1. eof.
                    // 2. we are not able to resolve a pts value for current packet.
                    // 3. the packets for this stream at the end of the files had valid dts.
                    next_pkt->pts = last_dts + next_pkt->duration;
                }
                pktl = s->packet_buffer;
            }

            /* read packet from packet buffer, if there is data */
            if (!(next_pkt->pts == AV_NOPTS_VALUE &&
                  next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
                ret = read_from_packet_buffer(&s->packet_buffer,
                                               &s->packet_buffer_end, pkt);
                goto return_packet;
            }
        }

        ret = read_frame_internal(s, pkt);
        if (ret < 0) {
            if (pktl && ret != AVERROR(EAGAIN)) {
                eof = 1;
                continue;
            } else
                return ret;
        }

        if (av_dup_packet(add_to_pktbuf(&s->packet_buffer, pkt,
                          &s->packet_buffer_end)) < 0)
            return AVERROR(ENOMEM);
    }

return_packet:

    st = s->streams[pkt->stream_index];
    if (st->skip_samples) {
        uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
        if (p) {
            AV_WL32(p, st->skip_samples);
            av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d\n", st->skip_samples);
        }
        st->skip_samples = 0;
    }

    if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
        ff_reduce_index(s, st->index);
        av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
    }

    if (is_relative(pkt->dts))
        pkt->dts -= RELATIVE_TS_BASE;
    if (is_relative(pkt->pts))
        pkt->pts -= RELATIVE_TS_BASE;

    return ret;
}
时间: 2024-08-06 23:57:09

FFMPeg代码分析:AVPacket结构体和av_read_frame函数的相关文章

FFMPeg代码分析:AVFormatContext结构体

从先前的demo中可以看到,进入main函数所定义的第一个变量就是AVFormatContext的指针: int main(int argc, char *argv[]) { AVFormatContext *pFormatCtx = NULL; .... } 而且,往下看就会知道这个结构体将贯穿函数始终,avformat_open_input.av_find_stream_info.av_read_frame.av_close_input_file都需要这个结构作为参数.该结构在FFMPEG中

C语言结构体中的函数指针

引言 指针是C语言的重要组成部分, 于是深入理解指针并且高效地使用指针可以使程序员写出更加老练的程序.我们要记住指针是一个指向内存地址的变量.指针可以引用如int.char--常见的数据类型,例如: int * intptr; // 声明一个指向整型值的指针 int intval = 5 ; // 定义一个整型变量 intptr = & intval ; // intptr现在包含intval的地址 指针不仅仅指向常规的类型还可以指向函数 函数指针 函数指针的内容不难理解,不再赘述,参见<C

详解C语言结构体中的函数指针_C 语言

结构体是由一系列具有相同类型或不同类型的数据构成的数据集合.所以,标准C中的结构体是不允许包含成员函数的,当然C++中的结构体对此进行了扩展.那么,我们在C语言的结构体中,只能通过定义函数指针的方式,用函数指针指向相应函数,以此达到调用函数的目的. 函数指针 函数类型 (*指针变量名)(形参列表):第一个括号一定不能少. "函数类型"说明函数的返回类型,由于"()"的优先级高于"*",所以指针变量名外的括号必不可少.  注意指针函数与函数指针表示

linux驱动-Linux驱动结构体中probe函数的参数怎么得到?

问题描述 Linux驱动结构体中probe函数的参数怎么得到? 解决方案 他既然都写了那在驱动文件里面应该会有这个函数啊,这种敦泰的TP驱动在SDK里面能找到好几个吧 解决方案二: static int ft5x_ts_probe(struct i2c_client *client const struct i2c_device_id *id) 函数原型是这个,我疑问的是原函数的这两个参数怎么确定的?{...

c语言-C语言求助!!!写了一个返回结构体指针的函数,编译时总是提醒没有初始化q,但是初始化了。

问题描述 C语言求助!!!写了一个返回结构体指针的函数,编译时总是提醒没有初始化q,但是初始化了. 下面是源码下载:http://pan.baidu.com/s/1dDhplwH 程序是用C语言写的,VS2013编译. 这是其中使用结构体的定义 这是哈夫曼函数的定义 解决方案 创建的时候可以 -- ,*q = NULL; malloc 之后 memset(q, 0 , sizeof(--)): 解决方案二: 创建的时候可以 -- ,*q = NULL; malloc 之后 memset(q, 0

xcode-c语言关于结构体类型的函数的返回值return

问题描述 c语言关于结构体类型的函数的返回值return 定义了一个全局变量的结构体,然后又定义了一个结构体类型的函数,函数的返回值如何返回一个空值,显然return 0;是不行的 解决方案 1)可以用结构体指针,因为指针的null是一个典型的"空",相对于int的0更能体现"空值"的含义,也最方便 2)结构体属于你自己定义的变量,所以你也可以规定当这个结构体里面的变量的值是某种情况是,认为这个结构体类型的变量是"空",比如一般意义上的int认为

c语言 结构体 指针-写了一个返回结构体指针的函数,编译时总是提醒没有初始化p,但是初始化了。

问题描述 写了一个返回结构体指针的函数,编译时总是提醒没有初始化p,但是初始化了. 源码 程序是用C写的,VS2013编译. 这是其中使用结构体的定义 这是哈夫曼函数的定义 解决方案 结构体指针的初始化 解决方案二: 哪有你所说的指针p啊 解决方案三: 是否用的c语言方式编译.编译器是否用c++方式编译

C++动态分配和撤销内存以及结构体类型作为函数参数_C 语言

C++动态分配内存(new)和撤销内存(delete) 在软件开发过程中,常常需要动态地分配和撤销内存空间,例如对动态链表中结点的插入与删除.在C语言中是利用库函数malloc和free来分配和撤销内存空间的.C++提供了较简便而功能较强的运算符new和delete来取代malloc和free函数. 注意: new和delete是运算符,不是函数,因此执行效率高. 虽然为了与C语言兼容,C++仍保留malloc和free函数,但建议用户不用malloc和free函数,而用new和delete运算

FFMPeg代码分析:AVCodec结构体以及编解码器的查找和加载

书接上回.在调用av_find_stream_info函数分析媒体文件并找到其中的视频流之后,视频流的相关信息被存放在了AVFormatContext结构体实例中.此时AVCodecContext实例所保存的AVCodec仍然为空.该结构体的定义如下: typedef struct AVCodec { const char *name;//codec名称,如果是解码HEVC的文件,那就是"hevc" const char *long_name;//codec全名,"HEVC(