• FFmpeg源代码:AVFormatContext相关接口


     与AVFormatContext结构体相关的接口函数声明位于libavformat\avformat.h中

    avformat_alloc_context

    1. /**
    2. * Allocate an AVFormatContext.
    3. * avformat_free_context() can be used to free the context and everything
    4. * allocated by the framework within it.
    5. */
    6. AVFormatContext *avformat_alloc_context(void);

    初始化结构体,为AVFormatContext结构体分配内存,并设置AVFormatContext的字段的默认值。

    avformat_free_context

    1. /**
    2. * Free an AVFormatContext and all its streams.
    3. * @param s context to free
    4. */
    5. void avformat_free_context(AVFormatContext *s);

    销毁AVFormatContext, 释放内存。

    avformat_open_input

    1. ​​​​​​​
    2. /**
    3. * Open an input stream and read the header. The codecs are not opened.
    4. * The stream must be closed with avformat_close_input().
    5. *
    6. * @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).
    7. *           May be a pointer to NULL, in which case an AVFormatContext is allocated by this
    8. *           function and written into ps.
    9. *           Note that a user-supplied AVFormatContext will be freed on failure.
    10. * @param url URL of the stream to open.
    11. * @param fmt If non-NULL, this parameter forces a specific input format.
    12. *            Otherwise the format is autodetected.
    13. * @param options  A dictionary filled with AVFormatContext and demuxer-private options.
    14. *                 On return this parameter will be destroyed and replaced with a dict containing
    15. *                 options that were not found. May be NULL.
    16. *
    17. * @return 0 on success, a negative AVERROR on failure.
    18. *
    19. * @note If you want to use custom IO, preallocate the format context and set its pb field.
    20. */
    21. int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);

    该接口注释已经写的很详细了,中文简单概述下

    ps:函数调用成功之后处理过的AVFormatContext结构体。
    url:打开的视音频流的URL。
    fmt:强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat。
    dictionay:附加的一些选项,一般情况下可以设置为NULL。

    该接口的功能是打开一个输入媒体文件,读取文件头(如果有的话,MPEG没有format header)并探测视频封装格式,将媒体文件信息存储到ps中。 

    需要注意的是,有些封装格式并没有header 或者header中并没有存储足够的信息,所以还需要调用avformat_find_stream_info函数来读取并解码一些帧来获取足够的信息。

    avformat_open_input 不会打开codec.

     具体代码分析可以参见雷神的这篇文章,写的很详细了,不再赘述:FFmpeg源代码简单分析:avformat_open_input()

    avformat_find_stream_info

    1. /**
    2. * Read packets of a media file to get stream information. This
    3. * is useful for file formats with no headers such as MPEG. This
    4. * function also computes the real framerate in case of MPEG-2 repeat
    5. * frame mode.
    6. * The logical file position is not changed by this function;
    7. * examined packets may be buffered for later processing.
    8. *
    9. * @param ic media file handle
    10. * @param options If non-NULL, an ic.nb_streams long array of pointers to
    11. * dictionaries, where i-th member contains options for
    12. * codec corresponding to i-th stream.
    13. * On return each dictionary will be filled with options that were not found.
    14. * @return >=0 if OK, AVERROR_xxx on error
    15. *
    16. * @note this function isn't guaranteed to open all the codecs, so
    17. * options being non-empty at return is a perfectly normal behavior.
    18. *
    19. * @todo Let the user decide somehow what information is needed so that
    20. * we do not waste time getting stuff the user does not need.
    21. */
    22. int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

     该函数通过读取一部分音视频数据来获取流信息,同时能够获取真实的帧率。该函数主要用于给每个媒体流(音频/视频)的AVStream结构体赋值,获取真实的流信息。

  • 相关阅读:
    Python教程:快速入门-函数、函数参数及三元运算符
    论文解读(g-U-Nets)《Graph U-Nets》
    Mybatis返回自动递增主键值,通过实体
    DSP2335的LED工程笔记
    【Linux】第十七站:进程创建与进程终止
    从TiDB迁移到OceanBase的实践分享
    还在为学不会JVM&G1烦恼吗?看阿里P8源码分析笔记,你想要的都有
    【ES】一、ES入门及JavaAPI使用
    linux下查看所有监听端口
    MySQL—MySQL架构
  • 原文地址:https://blog.csdn.net/ai2000ai/article/details/126271278