代码参考:(https://blog.youkuaiyun.com/qq_29350001/article/details/78214267)
解码流程:(https://www.cnblogs.com/wangguchangqing/p/5734998.html)
视频解码流程
首先了解几个概念:
- 编解码器(CODEC):能够进行视频和音频压缩(CO)与解压缩(DEC),是视频编解码的核心部分。
- 容器/多媒体文件(Container/File):没有了解视频的编解码之前,总是错误的认为平常下载的电影的文件的后缀(avi,mkv,rmvb等)就是视频的编码方式。事实上,刚才提到的几种文件的后缀并不是视频的编码方式,只是其封装的方式。一个视频文件通常有视频数据、音频数据以及字幕等,封装的格式决定这些数据在文件中是如何的存放的,封装在一起音频、视频等数据组成的多媒体文件,也可以叫做容器(其中包含了视音频数据)。所以,只看多媒体文件的后缀名是难以知道视音频的编码方式的。
- 流数据 Stream,例如视频流(Video Stream),音频流(Audio Stream)。流中的数据元素被称为帧Frame。
FFmpeg视频解码过程
通常来说,FFmpeg的视频解码过程有以下几个步骤:
- 注册所支持的所有的文件(容器)格式及其对应的CODEC av_register_all()
- 打开文件 avformat_open_input()
- 从文件中提取流信息 avformat_find_stream_info()
- 在多个数据流中找到视频流 video stream(类型为MEDIA_TYPE_VIDEO)
- 查找video stream 相对应的解码器 avcodec_find_decoder
- 打开解码器 avcodec_open2()
- 为解码帧分配内存 av_frame_alloc()
- 从流中读取读取数据到Packet中 av_read_frame()
- 对video 帧进行解码,调用 avcodec_decode_video2() //本文不解码,获取码流,直接用fwrite 保存为文件
1. 注册
av_register_all该函数注册支持的所有的文件格式(容器)及其对应的CODEC,只需要调用一次,故一般放在main函数中。也可以注册某个特定的容器格式,但通常来说不需要这么做。
2. 打开文件
avformat_open_input该函数读取文件的头信息,并将其信息保存到AVFormatContext结构体中。其调用如下
AVFormatContext* pFormatCtx = nullptr;
avformat_open_input(&pFormatCtx, filenName, nullptr, nullptr)
第一个参数是AVFormatContext结构体的指针,第二个参数为文件路径;第三个参数用来设定输入文件的格式,如果设为null,将自动检测文件格式;第四个参数用来填充AVFormatContext一些字段以及Demuxer的private选项。
AVFormatContext包含有较多的码流信息参数,通常由avformat_open_input创建并填充关键字段。
3. 获取必要的CODEC参数
avformat_open_input通过解析多媒体文件或流的头信息及其他的辅助数据,能够获取到足够多的关于文件、流和CODEC的信息,并将这些信息填充到AVFormatContext结构体中。但任何一种多媒体格式(容器)提供的信息都是有限的,而且不同的多媒体制作软件对头信息的设置也不尽相同,在制作多媒体文件的时候难免会引入一些错误。也就是说,仅仅通过avformat_open_input并不能保证能够获取所需要的信息,所以一般要使用
avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
avformat_find_stream_info主要用来获取必要的CODEC参数,设置到ic->streams[i]->codec。
在解码的过程中,首先要获取到各个stream所对应的CODEC类型和id,CODEC的类型和id是两个枚举值,其定义如下:
enum AVMediaType {
AVMEDIA_TYPE_UNKNOWN = -1,
AVMEDIA_TYPE_VIDEO,
AVMEDIA_TYPE_AUDIO,
AVMEDIA_TYPE_DATA,
AVMEDIA_TYPE_SUBTITLE,
AVMEDIA_TYPE_ATTACHMENT,
AVMEDIA_TYPE_NB
};
enum CodecID {
CODEC_ID_NONE, /* video codecs */
CODEC_ID_MPEG1VIDEO,
CODEC_ID_MPEG2VIDEO, ///< preferred ID for MPEG-1/2 video decoding
CODEC_ID_MPEG2VIDEO_XVMC,
CODEC_ID_H261,
CODEC_ID_H263,
...
}
通常,如果多媒体文件具有完整而正确的头信息,通过avformat_open_input即可用获得这两个参数。
4. 打开解码器
经过上面的步骤,已经将文件格式信息读取到了AVFormatContext中,要打开流数据相应的CODEC需要经过下面几个步骤
找到视频流 video stream
一个多媒体文件包含有多个原始流,例如 movie.mkv这个多媒体文件可能包含下面的流数据
原始流 1 h.264 video
原始流 2 aac audio for Chinese
原始流 3 aac audio for English
原始流 4 Chinese Subtitle
原始流 5 English Subtitle
要解码视频,首先要在AVFormatContext包含的多个流中找到CODEC类型为AVMEDIA_TYPE_VIDEO,代码如下:
//查找视频流 video stream
int videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStream = i;
break;
}
}
if (videoStream == -1)
return -1; // 没有找到视频流video stream
结构体AVFormatContext中的streams字段是一个AVStream指针的数组,包含了文件所有流的描述,上述上述代码在该数组中查找CODEC类型为
AVMEDIA_TYPE_VIDEO的流的下标。
根据codec_id找到相应的CODEC,并打开
结构体AVCodecContext描述了CODEC上下文,包含了众多CODEC所需要的参数信息。
AVCodecContext* pCodecCtxOrg = nullptr;
AVCodec* pCodec = nullptr;
pCodecCtxOrg = pFormatCtx->streams[videoStream]->codec; // codec context
// 找到video stream的 decoder
pCodec = avcodec_find_decoder(pCodecCtxOrg->codec_id);
// open codec
if (avcodec_open2(pCodecCtxOrg , pCodec, nullptr) < 0)
return -1; // Could open codec
上述代码,首先通过codec_id找到相应的CODEC,然后调用avcodec_open2打开相应的CODEC。
5. 读取数据帧并解码
已经有了相应的解码器,下面的工作就是将数据从流中读出,并解码为没有压缩的原始数据
AVPacket packet;
while (av_read_frame(pFormatCtx, &packet) >= 0)
{
if (packet.stream_index == videoStream)
{
int frameFinished = 0;
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished)
{
doSomething();
}
}
}
上述代码调用av_read_frame将数据从流中读取数据到packet中,并调用avcodec_decode_video2对读取的数据进行解码。
6. 关闭
需要关闭avformat_open_input打开的输入流,avcodec_open2打开的CODEC
avcodec_close(pCodecCtxOrg);
avformat_close_input(&pFormatCtx);
补充
在配置好FFmpeg的开发环境后,在C++中使用FFmpeg的库函数,会出现解析不出函数的名称链接错误,这是由于FFmpeg库是C语言实现,要在C++调用C函数需要 extern "C"的声明。
extern "C"
{
# include <libavcodec\avcodec.h>
# include <libavformat\avformat.h>
# include <libswscale\swscale.h>
}
FFmpeg获取rtsp传输的h264裸流并保存
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
//#include "SDL2/SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endif
int main()
{
AVFormatContext *pFormatCtx;
int i, videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
uint8_t *out_buffer;
AVPacket *packet;
int ret, got_picture;
struct SwsContext *img_convert_ctx;
//下面是RTSP地址,按照使用的网络摄像机的URL格式即可
char filepath[] = "rtsp://user:passport@192.168.x.xxx:xxx/h264/ch1/main/av_stream";
av_register_all(); //初始化所有组件,只有调用了该函数,才能使用复用器和编解码器,在所有FFmpeg程序中第一个被调用
avformat_network_init(); //加载socket库以及网络加密协议相关的库,为后续使用网络相关提供支持
pFormatCtx = avformat_alloc_context(); //用来申请AVFormatContext类变量并初始化默认参数。申请的空间通过void avformat_free_context(AVFormatContext *s)函数释放。
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]->codec->codec_type == AVMEDIA_TYPE_VIDEO) //在多个数据流中找到视频流 video stream(类型为AVMEDIA_TYPE_VIDEO)
{
videoindex = i;
break;
}
if (videoindex == -1)
{
printf("Didn't find a video stream.\n");
return -1;
}
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id); //查找video stream 相对应的解码器
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;
}
pFrame = av_frame_alloc(); //为解码帧分配内存
pFrameYUV = av_frame_alloc();
out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
//Output Info---输出一些文件(RTSP)信息
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, 4, NULL, NULL, NULL);
packet = (AVPacket *)av_malloc(sizeof(AVPacket));
FILE *fpSave;
if ((fpSave = fopen("video.h264", "ab")) == NULL) //h264保存的文件名
return 0;
//for (;;)
//{
//------------------------------
if (av_read_frame(pFormatCtx, packet) >= 0) //从流中读取读取数据到Packet中
{
if (packet->stream_index == videoindex)
{
fwrite(packet->data, 1, packet->size, fpSave);//写数据到文件中
}
av_free_packet(packet);
}
//}
//--------------
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx); //需要关闭avformat_open_input打开的输入流,avcodec_open2打开的CODEC
avformat_close_input(&pFormatCtx);
return 0;
}