FFmpeg视频解码器

本博客是摘自雷霄骅大神的课程《基于 FFmpeg + SDL 的视频播放器的制作》课程 里的内容,非常适合音视频小白入门,在这里感谢雷神的指导!

视频解码知识

• 纯净的视频解码流程
压缩编码数据->像素数据。
例如解码H.264,就是“H.264码流->YUV”。

• 一般的视频解码流程
视频码流一般存储在一定的封装格式(例如MP4、AVI等)中。封装格式中通常还包含音频码流等内容。
对于封装格式中的视频,需要先从封装格式中提取中视频码流,然后再进行解码。
例如解码MKV格式的视频文件,就是“MKV->H.264码流->YUV”。

VC下FFmpeg开发环境的搭建

• 新建控制台工程
打开VC++
文件->新建->项目->控制台应用程序

• 拷贝FFmpeg开发文件
头文件(*.h)拷贝至项目文件夹的include子文件夹下
导入库文件(*.lib)拷贝至项目文件夹的lib子文件夹下
动态库文件(*.dll)拷贝至项目文件夹下

• 配置开发文件
• 打开属性面板
解决方案资源管理器->右键单击项目->属性
(平台选择x64)
• 头文件配置
配置属性->C/C+±>常规->附加包含目录,输入“include”(刚才拷贝头文件的目录)
• 导入库配置
配置属性->链接器->常规->附加库目录,输入“lib” (刚才拷贝库文
件的目录)
配置属性->链接器->输入->附加依赖项,输入“avcodec.lib; avformat.lib; avutil.lib; avdevice.lib; avfilter.lib; postproc.lib; swresample.lib; swscale.lib”(导入库的文件名)
• 动态库不用配置

测试
• 创建源代码文件
在工程中创建一个包含main()函数的C/C++文件(如果已经有了可以跳过这一步)。
• 包含头文件
如果是C语言中使用FFmpeg,则直接使用下面代码

#include "libavcodec/avcodec.h" 

如果是C++语言中使用FFmpeg,则使用下面代码
#define __STDC_CONSTANT_MACROS

extern "C"
{
#include "libavcodec/avcodec.h " 
} 

main()中调用一个FFmpeg的接口函数
例如下面代码打印出了FFmpeg的配置信息

int main(int argc, char* argv[]){
	printf("%s", avcodec_configuration());
	return 0;
}

如果运行无误,则代表FFmpeg已经配置完成。

• FFmpeg库简介
FFmpeg一共包含8个库:

▫ avcodec:编解码(最重要的库)。
▫ avformat:封装格式处理。
▫ avfilter:滤镜特效处理。
▫ avdevice:各种设备的输入输出。
▫ avutil:工具库(大部分库都需要这个库的支持)。
▫ postproc:后加工。
▫ swresample:音频采样数据格式转换。
▫ swscale:视频像素数据格式转换。

示例程序运行

因为雷神代码比较久远,在最新环境下很多都不能运行了,但是大体思路还是不变的(在雷神给的vs文件中还是可以运行的),我也将很多废弃的代码进行整改

/**
 * 最简单的基于FFmpeg的解码器
 * Simplest FFmpeg Decoder
 *
 * 雷霄骅 Lei Xiaohua
 * leixiaohua1020@126.com
 * 中国传媒大学/数字电视技术
 * Communication University of China / Digital TV Technology
 * http://blog.csdn.net/leixiaohua1020
 *
 * 本程序实现了视频文件的解码(支持HEVC,H.264,MPEG2等)。
 * 是最简单的FFmpeg视频解码方面的教程。
 * 通过学习本例子可以了解FFmpeg的解码流程。
 * This software is a simplest video decoder based on FFmpeg.
 * Suitable for beginner of FFmpeg.
 *
 */

#include <stdio.h>

#define __STDC_CONSTANT_MACROS

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
};

int main(int argc, char* argv[])
{
    AVFormatContext* pFormatCtx;
    int				i, videoindex;
    AVCodecContext* pCodecCtx;
    AVCodec* pCodec;
    AVFrame* pFrame, * pFrameYUV;
    uint8_t* out_buffer;
    AVPacket* packet;
    int y_size;
    int ret, got_picture;
    struct SwsContext* img_convert_ctx;
    //输入文件路径
    char filepath[] = "Titanic.ts";

    int frame_cnt;

    //av_register_all();
    avformat_network_init();
    pFormatCtx = avformat_alloc_context();

    if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) {
        printf("Couldn't open input stream.\n");
        return -1;
    }
    if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
        printf("Couldn't find stream information.\n");
        return -1;
    }
    videoindex = -1;
    for (i = 0; i < pFormatCtx->nb_streams; i++)
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoindex = i;
            break;
        }
    if (videoindex == -1) {
        printf("Didn't find a video stream.\n");
        return -1;
    }

    AVCodecParameters* codecParameters = pFormatCtx->streams[videoindex]->codecpar;
    pCodecCtx = avcodec_alloc_context3(nullptr);
    avcodec_parameters_to_context(pCodecCtx, codecParameters);

    //pCodecCtx=pFormatCtx->streams[videoindex]->codec;
    pCodec = (AVCodec*)avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL) {
        printf("Codec not found.\n");
        return -1;
    }
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
        printf("Could not open codec.\n");
        return -1;
    }
    FILE* fp, * fp_264, * fp_yuv;
    fopen_s(&fp, "info.txt", "wb+");
    fopen_s(&fp_264, "output264.h264", "wb+");
    fopen_s(&fp_yuv, "outputyuv.yuv", "wb+");
    /*
     * 在此处添加输出视频信息的代码
     * 取自于pFormatCtx,使用fprintf()
     */
    fprintf(fp, "shichang: %d\n", pFormatCtx->duration);
    fprintf(fp, "fengzhuanggeshi:%s\n", pFormatCtx->iformat->long_name);
    fprintf(fp, "kuangao: %d*%d\n", pCodecCtx->width, pCodecCtx->height);

    packet = av_packet_alloc();
    pFrame = av_frame_alloc();
    pFrameYUV = av_frame_alloc();

    out_buffer = (uint8_t*)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));
    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);


    //Output Info-----------------------------
    printf("--------------- File Information ----------------\n");
    av_dump_format(pFormatCtx, 0, filepath, 0);
    printf("-------------------------------------------------\n");
    img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

    frame_cnt = 0;
    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == videoindex) {
            /*
                 * 在此处添加输出H264码流的代码
                 * 取自于packet,使用fwrite()
                 */

            fwrite(packet->data, 1, packet->size, fp_264);

            if (avcodec_send_packet(pCodecCtx, packet) < 0) {
                printf("avcodec_send_packet failed!.\n");
                continue;
            }
            while (1)
            {
                ret = avcodec_receive_frame(pCodecCtx, pFrame);
                if (ret != 0)break;
                sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
                printf("Decoded frame index: %d\n", frame_cnt);

                /*
                 * 在此处添加输出YUV的代码
                 * 取自于pFrameYUV,使用fwrite()
                 */
                fwrite(pFrameYUV->data[0], 1, pCodecCtx->width * pCodecCtx->height, fp_yuv);
                fwrite(pFrameYUV->data[1], 1, pCodecCtx->width * pCodecCtx->height / 4, fp_yuv);
                fwrite(pFrameYUV->data[2], 1, pCodecCtx->width * pCodecCtx->height / 4, fp_yuv);
                frame_cnt++;
            }
        }
        av_packet_unref(packet);
    }

    sws_freeContext(img_convert_ctx);

    fclose(fp);
    fclose(fp_264);
    fclose(fp_yuv);
    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);

    return 0;
}

在这里插入图片描述

FFmpeg解码的函数

FFmpeg解码的流程图如下所示:
在这里插入图片描述

FFmpeg解码函数简介:
• av_register_all():注册所有组件 (目前废弃)
• avformat_open_input():打开输入视频文件。
• avformat_find_stream_info():获取视频文件信息。
• avcodec_find_decoder():查找解码器。
• avcodec_open2():打开解码器。
• av_read_frame():从输入文件读取一帧压缩数据。

• avcodec_decode_video2():解码一帧压缩数据 (目前废弃)
取代:(这两个函数要特别注意,有很多注意点)
avcodec_send_packet():发送编码数据包
avcodec_receive_frame():接收解码后数据

• avcodec_close():关闭解码器。
• avformat_close_input():关闭输入视频文件。

PS:初次学习,一定要将这些函数名称熟记于心

FFmpeg解码的数据结构

FFmpeg解码的数据结构如下所示:
注意AVCodecContext已经被AVCodecParameters取代,但是编解码仍然离不开AVCodecContext,所以有avcodec_parameters_to_context函数可以将AVCodecParameters的信息填充到AVCodecContext中。
在这里插入图片描述
FFmpeg数据结构简介
• AVFormatContext
封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装格式相关信息。

• AVInputFormat
每种封装格式(例如FLV, MKV, MP4, AVI)对应一个该结构体。

• AVStream
视频文件中每个视频(音频)流对应一个该结构体。

• AVCodecParameters
此结构描述编码流的属性。

• AVCodecContext
编码器上下文结构体,保存了视频(音频)编解码相关信息。

• AVCodec
每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。

• AVPacket
存储一帧压缩编码数据。

• AVFrame
存储一帧解码后像素(采样)数据。

FFmpeg数据结构分析
• AVFormatContext
iformat:输入视频的AVInputFormat
nb_streams :输入视频的AVStream 个数
streams :输入视频的AVStream []数组
duration :输入视频的时长(以微秒为单位)
bit_rate :输入视频的码率

• AVInputFormat
name:封装格式名称
long_name:封装格式的长名称
extensions:封装格式的扩展名
id:封装格式ID
一些封装格式处理的接口函数

• AVStream
id:序号
codec:该流对应的AVCodecContext
time_base:该流的时基
r_frame_rate:该流的帧率

• AVCodecContext
codec:编解码器的AVCodec
width, height:图像的宽高(只针对视频)
pix_fmt:像素格式(只针对视频)
sample_rate:采样率(只针对音频)
channels:声道数(只针对音频)
sample_fmt:采样格式(只针对音频)

• AVCodec
name:编解码器名称
long_name:编解码器长名称
type:编解码器类型
id:编解码器ID
一些编解码的接口函数

• AVPacket
pts:显示时间戳
dts :解码时间戳
data :压缩编码数据
size :压缩编码数据大小
stream_index :所属的AVStream

• AVFrame
data:解码后的图像像素数据(音频采样数据)。
linesize:对视频来说是图像中一行像素的大小;对音频来说是整个音频帧的大小。
width, height:图像的宽高(只针对视频)。
key_frame:是否为关键帧(只针对视频) 。
pict_type:帧类型(只针对视频) 。例如I,P,B。

补充小知识
• 解码后的数据为什么要经过sws_scale()函数处理?
• 解码后YUV格式的视频像素数据保存在AVFrame的data[0]、data[1]、data[2]中。但是这些像素值并不是连续存储的,每行有效像素之后存储了一些无效像素 。 以亮度 Y 数据为例 , data[0] 中一共包含了linesize[0]*height个数据。但是出于优化等方面的考虑,linesize[0]实际上并不等于宽度width,而是一个比宽度大一些的值。因此需要使用sws_scale()进行转换。转换后去除了无效数据,width和linesize[0]取值相等。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗吧!骚年!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值