• FFmpeg常用结构体分析


    目录

    1.AVFormatConext

    2.AVInputFormat

    3.AVStream

    4.AVCodecContext

    5.AVPacket

    6.AVCodec

    7.AVFrame

    8.AVIOContext

    9.URLProtocol

    10.URLContext


    1.AVFormatConext


    AVFormatConext是一个贯穿全局地数据结构,AVFormatConext结构包含很多信息,很多函数都要用它作为参数。

    初始化AVFormatConext示例代码:

    1. AVFormatContext *pFormatCtx;
    2. pFormatCtx=avformat_alloc_context();

    2.AVInputFormat


    AVInputFormat是FFmpeg的解复用器对象,AVInputFormat是类似COM接口地数据结构,表示输入文件容器格式,着重于功能类型,一种文件容器格式对应一个AVInputFormat结构,在程序运行时有多个案例。

    3.AVStream


    AVStream是存储每一个视频/音频流信息的结构体。解复用器的目的就是从容器中分离(解析出来)不同的流,FFmpeg中的流对象为AVStream,它是由解复用器的read_header函数创建的,并保存在AVFormatConext的nb_streams(容器中的流条数)及stream数组中。

    4.AVCodecContext


    AVCodecContext是一个描述编解码器上下文的数据结构,包含了众多编解码器需要的参数信息。

    初始化示例代码

    1. AVCodecContext *mAvContext = NULL;
    2. mAvContext = avcodec_alloc_context3(mVcodec);

    5.AVPacket


            FFmpeg 用AVPacket 来存放编码后的视频数据,AVPacket 保存了解复用之后、解码前的数据(仍然是压缩后的数据)和关于这些数据的一些附加信息,如显示时间戳(PTS)解码时间戳(DTS)数据时长、所在媒体流的索引等。
            对于视频(Video)来说,AVPacket 通常包含一个压缩的,而音频 (Audio)则有可能包含多个压缩的帧。并且,一个 Packet 有可能是空的,不包含任何压缩数据,只含有 side dat(side data 指的是容器提供的关于 Packet 的一些附加信息。例如,在编码结束的时候更新一些流的参数)。

    申请AVPacket:

        AVPacket *packet = (AVPacket *) av_malloc(sizeof(AVPacket));

    AVPacket源码解析:

    1. typedef struct AVPacket {
    2. AVBufferRef *buf;//AVBufferRef类型的指针,用来管理data指针引用的数据缓存
    3. int64_t pts;//显示时间戳
    4. int64_t dts;//解码时间戳
    5. uint8_t *data;//指向保存压缩数据的指针,这就是AVPacket实际的数据
    6. int size;
    7. int stream_index;//流索引
    8. int flags;//带有AV_PKT_FLAG属性的组合
    9. AVPacketSideData *side_data;//填充容器的一些附加数据
    10. int side_data_elems;
    11. int64_t duration;//Packet的时长
    12. int64_t pos;//Packet的位置
    13. #if FF_API_CONVERGENCE_DURATION
    14. attribute_deprecated
    15. int64_t convergence_duration;
    16. #endif
    17. } AVPacket;

    6.AVCodec


    AVCodec是存储编解码器信息的结构体,AVCodec Codec通过avcodec_find_decoder(codec_id)找到对应的Codec。

    初始化AVCodec示例代码:

    1. AVCodec *mVcodec = NULL;
    2. mVcodec = avcodec_find_decoder(codec_id);

    7.AVFrame


    AVFrame结构体一般用于存储原始数据(即非压缩数据,例如对视频来说是YUV、RGB,对音频来说是PCM),此外还包含了一些相关信息。

    创建AVFrame

     AVFrame *frame = av_frame_alloc();//分配一个AVFrame结构体,AVFrame结构体一般用于存储原始数据,指向解码后的原始帧

     在解码过程中使用了两个AVFrame,这两个AVFrame分配缓存空间的方法不同:

    • 一个AVFrame用来存放从AVPacket中解码出来的原始数据,这个AVFrame的数据缓存空间通过调用 avcodec_receive_frame(mAvContext, frame)来分配和填充。
    result = avcodec_receive_frame(mAvContext, frame);
    • 另一个AVFrame用来存放将解码出来的原始数据变换为需要的数据格式(例如RGB、RGBA)的数据,这个AVFrame需要手动分配数据缓存空间,代码如下:
    1. AVFrame *rgb_frame = av_frame_alloc();//分配一个AVFrame结构体,指向存放转换成rgb后的帧
    2. //缓存区
    3. uint8_t *out_buffer= (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_RGBA,
    4. mAvContext->width,mAvContext->height));
    5. //与缓存区相关联,设置rgb_frame缓存区
    6. avpicture_fill((AVPicture *)rgb_frame,out_buffer,AV_PIX_FMT_RGBA,mAvContext->width,mAvContext->height);

     释放AVFrame

    1. av_frame_free(&frame);
    2. av_frame_free(&rgb_frame);

    AVFrame源码解析: 

    1. typedef struct AVFrame {
    2. #define AV_NUM_DATA_POINTERS 8
    3. uint8_t *data[AV_NUM_DATA_POINTERS];
    4. int linesize[AV_NUM_DATA_POINTERS];
    5. uint8_t **extended_data;//扩展的数据
    6. int width, height;//视频的宽和高
    7. int nb_samples;//每个信道的音频采样点个数
    8. int format;//帧的像素格式
    9. int key_frame;//1表示关键帧 0表示非关键帧
    10. enum AVPictureType pict_type;//帧的图像类型
    11. AVRational sample_aspect_ratio;//视频帧的采样率
    12. int64_t pts;//显示时间戳
    13. #if FF_API_PKT_PTS
    14. attribute_deprecated
    15. int64_t pkt_pts;//从AVPacket中复制的PTS
    16. #endif
    17. int64_t pkt_dts;//从AVPacket中复制的DTS
    18. int coded_picture_number;//按解码排序后的图像数
    19. int display_picture_number;//按显示位置排序的图像数
    20. int quality;//1~FF_LAMBDA_MAX(256*128-1)之间取值
    21. void *opaque;//私有数据
    22. #if FF_API_ERROR_FRAME
    23. attribute_deprecated
    24. uint64_t error[AV_NUM_DATA_POINTERS];
    25. #endif
    26. int repeat_pict;//解码时有多少个图像被延迟,extra_delay=repeat_pict/(2*fps)
    27. int interlaced_frame;//交错帧,表示图像内容是交错的
    28. int top_field_first;
    29. int palette_has_changed;
    30. int64_t reordered_opaque;
    31. int sample_rate;//音频数据的采样率
    32. uint64_t channel_layout;//音频信道布局
    33. AVBufferRef *buf[AV_NUM_DATA_POINTERS];
    34. AVBufferRef **extended_buf;
    35. int nb_extended_buf;
    36. AVFrameSideData **side_data;
    37. int nb_side_data;
    38. #define AV_FRAME_FLAG_CORRUPT (1 << 0)
    39. #define AV_FRAME_FLAG_DISCARD (1 << 2)
    40. int flags;
    41. enum AVColorRange color_range;
    42. enum AVColorPrimaries color_primaries;
    43. enum AVColorTransferCharacteristic color_trc;
    44. enum AVColorSpace colorspace;//YUV颜色空间类型
    45. enum AVChromaLocation chroma_location;
    46. int64_t best_effort_timestamp;
    47. int64_t pkt_pos;//记录上一包输出解码器时Packet的位置
    48. int64_t pkt_duration;//Packet的时长
    49. AVDictionary *metadata;
    50. int decode_error_flags;
    51. int channels;//音频信道个数
    52. int pkt_size;//Packet的大小
    53. #if FF_API_FRAME_QP
    54. attribute_deprecated
    55. int8_t *qscale_table;
    56. attribute_deprecated
    57. int qstride;
    58. attribute_deprecated
    59. int qscale_type;
    60. attribute_deprecated
    61. AVBufferRef *qp_table_buf;
    62. #endif
    63. AVBufferRef *hw_frames_ctx;
    64. AVBufferRef *opaque_ref;
    65. size_t crop_top;
    66. size_t crop_bottom;
    67. size_t crop_left;
    68. size_t crop_right;
    69. AVBufferRef *private_ref;
    70. } AVFrame;

    8.AVIOContext


    协议(文件)操作的顶层结构式AVIOContext,这个对象实现了带缓冲的读/写操作。FFmpeg的输入对象AVFormat的pb字段指向一个AVIOContext。

    典型应用如下:

    1. AVFormatContext *ic=NULL;
    2. ic=avformat_alloc_context();
    3. unsigned char *iobuffer=(unsigned char*)av_malloc(32768);
    4. AVIOContext *avio=avio_alloc_context(iobuffer,32768,0,buffer,fill_iobuffer,NULL,NULL);
    5. ic->pb=avio;
    6. err=avformat_open_input(&ic,is->filename,is->iformat,&format_opts);

    9.URLProtocol


    URLProtocol是FFmpeg操作文件的结构(包括文件、网络数据流等),包括open、close、read、write、seek等操作。URLProtocol为协议操作对象,针对每种协议会有一个这样的对象,每个协议操作对象和一个协议对象关联。

    10.URLContext


    URLContext对象封装了协议对象及协议操作对象,URLContext在avio.c中通过url_alloc_for_protocol进行初始化。并且分配空间(使用av_malloc(sizeof(URLContext)+strlen(filename)+1)函数)。


    参考:Android 音视频开发(何俊林 著)

  • 相关阅读:
    升级鸿蒙4.2新变化,新增 WLAN 网络自动连接开关!
    灵性·挖掘 3:自我迭代之路
    二维数组练习
    AI绘画使用Stable Diffusion(SDXL)绘制玉雕风格的龙
    JAVA编程思想N刷
    前端数据可视化:ECharts使用
    HTML页面获取URL传递的参数值
    基于ANSYS Polyflow的逆向挤出模头设计攻略
    【C语言】冒泡排序的快排模拟
    Java基础面试题【1】
  • 原文地址:https://blog.csdn.net/weixin_63357306/article/details/133420180