• FFmpeg关键函数介绍


    目录

    1. av_register_all()——弃用

    2. avformat_alloc_context()

    3.avformat_open_input()

    4.avformat_find_stream_info()

    5.av_read_frame()

     6.avcodec_send_packet

    7.avcodec_receive_frame()

    8.avcodec_open2

    9. sws_getContext()

    10.sws_scale()


    1. av_register_all()——弃用


    初始化 libavformat 和注册所有的复用器、解复用器和协议处理器。如果不调用这个函数,可以调用下面的三个函数来选择支持的格式。
    • 注册复用器的函数是av_register_output_format()。
    • 注册解复用器的函数是av_register_input_format()。
    • 注册协议处理器的函数是ffurl_register_protocol()。
    注:FFmpeg4.0 以上的版本,这个函数已经被废弃。

    2. avformat_alloc_context()


    当使用AVFormatContext时, avformat_alloc_context()函数用来对AVFormatContext分配空间,

    AVFormatContext *avFormatContext = avformat_alloc_context();//获取上下文

    3.avformat_open_input()


    avformat_open_input的主要功能是打开一个文件,读取header,不会涉及打开编码器

    与之对应的是通过avformat_close_input函数关闭文件。

    示例代码:

    1. int error;
    2. //打开视频地址并获取里面的内容(解封装)
    3. error = avformat_open_input(&avFormatContext, inputPath, NULL, NULL);
    4. if (error < 0) {
    5. LOGE("打开视频失败")
    6. return;
    7. }

    4.avformat_find_stream_info()


    读取音视频数据来获取一些相关的信息。

    示例代码:

    1. if (avformat_find_stream_info(avFormatContext, NULL) < 0) {
    2. LOGE("获取内容失败")
    3. return;
    4. }

    5.av_read_frame()


    在FFmpeg中av_read_frame函数的作用是读取码流中的若干音频帧或者1帧数据。在解码视频时,每解码一个视频帧,需要先调用av_read_frame获取1帧视频的压缩数据,然后才能对该数据进行解码。

    示例代码:

    1. while(1) {
    2. int ret = av_read_frame(avFormatContext, packet);
    3. if (ret != 0) {
    4. av_strerror(ret, buf, sizeof(buf));
    5. LOGE("--%s--\n", buf);
    6. av_packet_unref(packet);
    7. break;
    8. }
    9. if (ret >= 0 && packet->stream_index != mVideoStreamIdx) {
    10. av_packet_unref(packet);
    11. continue;
    12. }
    13. //省略...
    14. }

     6.avcodec_send_packet


    发送数据到ffmepg,放到解码队列中

    1. // 发送待解码包
    2. int result = avcodec_send_packet(mAvContext, packet);

    7.avcodec_receive_frame()


    将成功的解码队列中取出1个frame

    示例代码:

    1. while (result >= 0) {
    2. result = avcodec_receive_frame(mAvContext, frame);
    3. if (result == AVERROR_EOF)
    4. break;
    5. else if (result == AVERROR(EAGAIN)) {
    6. result = 0;
    7. break;
    8. } else if (result < 0) {
    9. av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
    10. av_frame_unref(frame);
    11. break;
    12. }
    13. // 省略...
    14. }

    8.avcodec_open2


    打开解码器

    示例代码:

    1. // 打开解码器
    2. if (avcodec_open2(mAvContext, mVcodec, NULL) != 0){
    3. LOGE("打开失败")
    4. return;
    5. }

    9. sws_getContext()


    分配和返回一个 SwsContext。其原型如下:

    struct SwsContext *sws_getContext(int srcW, int srcH,

    enum AVPixelFormat srcFormat,

    int dstW, int dstH,

    enum AVPixelFormat dstFormat,

    int flags,

    SwsFilter *srcFilter,

    SwsFilter *dstFilter,

    const double *param);

    •  srcW:输入图像的宽度 
    • srcH: 输入图像的宽度 
    • srcFormat: 输入图像的像素格式 
    • dstW:输出图像的宽度 
    • dstH: 输出图像的高度 
    • dstFormat:输出图像的像素格式 
    • flags 选择缩放算法(只有当输入输出图像大小不同时有效),一般选择SWS_FAST_BILINEAR 
    • srcFilter: 输入图像的滤波器信息, 若不需要传NULL 
    • dstFilter:输出图像的滤波器信息, 若不需要传NULL 
    • param : 特定缩放算法需要的参数(?),默认为NULL 
    • 成功后返回SwsContext 类型的结构体。

    示例代码:

    1. SwsContext* swsContext = sws_getContext(mAvContext->width,mAvContext->height,mAvContext->pix_fmt,
    2. mAvContext->width,mAvContext->height,AV_PIX_FMT_RGBA,
    3. SWS_BICUBIC,NULL,NULL,NULL);

    10.sws_scale()


    处理图像数据。

    其原型如下:

    int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],

    const int srcStride[], int srcSliceY, int srcSliceH,

    uint8_t *const dst[], const int dstStride[]);

    参数:

     SwsContext *c: 转换格式的上下文。

    srcSlice[],:输入图像的每个颜色通道的数据指针。

    srcStride[]:输入图像的每个颜色通道的跨度。.

    srcSliceY:起始位置。

    srcSliceH:处理多少行。

    dst[]:输出图像的每个颜色通道数据指针。

    dstStride[]:输出图像的每个颜色通道行字节数。

     示例代码:

    1. //转换为rgb格式
    2. sws_scale(swsContext, (const uint8_t *const *) frame->data, frame->linesize, 0,
    3. frame->height, rgb_frame->data,
    4. rgb_frame->linesize);

  • 相关阅读:
    Java项目:ssm赛事打分系统
    HWS-2022 夏令营线上赛 WP
    java标注
    OmniGraffle Pro v7.22.3(流程图UML图)
    【C语言航路】第六站:指针初阶
    Mybatis 在 insert 插入操作后返回主键 id
    【PTA-训练day10】L2-022 重排链表 + L1-043 阅览室
    【牛客】SQL125 得分不小于平均分的最低分
    深入理解多线程编程
    每日4道算法题——第021天
  • 原文地址:https://blog.csdn.net/weixin_63357306/article/details/133420804