FFMPEG解码流程:
1. 注册所有容器格式和CODEC: av_register_all()
2. 打开文件: av_open_input_file()
3. 从文件中提取流信息: av_find_stream_info()
4. 穷举所有的流,查找其中种类为CODEC_TYPE_VIDEO
5. 查找对应的解码器: avcodec_find_decoder()
6. 打开编解码器: avcodec_open2()
这里需要注意,从stream里面获得的codecCtx不能直接使用
从stream里面获得的codecCtx指的是
pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
这个pCodecCtxOrig不能直接使用,
而要拷贝到另外一个位置,使用新的
原文是这么写的
Note that we must not use the AVCodecContext from the video stream directly!
So we have to use avcodec_copy_context() to copy the context to a new location
(after allocating memory for it, of course).
7. 为解码帧分配内存: avcodec_alloc_frame()
8. 不停地从码流中提取出帧数据: av_read_frame()
9. 判断帧的类型,对于视频帧调用: avcodec_decode_video2()
10.每读取完一个packet,解码之后,释放packet,然后重新读: av_free_packet(&packet)
11.释放分配的内存(所有的packet解码之后):av_frame_free
12. 解码完后,释放解码器: avcodec_close()
13. 关闭输入文件: avformat_close_input_file()
========
有几个函数不一定在基本流程里面使用,但是有必要介绍一下
av_malloc av_free
`av_malloc` is ffmpeg's malloc that is just a simple wrapper around malloc
that makes sure the memory addresses are aligned and such. It will _not_
protect you from memory leaks, double freeing, or other malloc problems.
翻译过来之后是:
av_malloc 简单的封装了malloc,保证了内存地址对齐,但是以前malloc具有的内存泄漏,多次释放和其他问题还是需要自己注意。
avpicture_fill
Now we use avpicture_fill to associate the frame with our newly allocated
buffer. About the AVPicture cast: the AVPicture struct is a subset of the
AVFrame struct - the beginning of the AVFrame struct is identical to the
AVPicture struct.
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
其中buffer是刚刚用av_malloc分配的内存
1. 注册所有容器格式和CODEC: av_register_all()
2. 打开文件: av_open_input_file()
3. 从文件中提取流信息: av_find_stream_info()
4. 穷举所有的流,查找其中种类为CODEC_TYPE_VIDEO
5. 查找对应的解码器: avcodec_find_decoder()
6. 打开编解码器: avcodec_open2()
这里需要注意,从stream里面获得的codecCtx不能直接使用
从stream里面获得的codecCtx指的是
pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec;
这个pCodecCtxOrig不能直接使用,
而要拷贝到另外一个位置,使用新的
原文是这么写的
Note that we must not use the AVCodecContext from the video stream directly!
So we have to use avcodec_copy_context() to copy the context to a new location
(after allocating memory for it, of course).
7. 为解码帧分配内存: avcodec_alloc_frame()
8. 不停地从码流中提取出帧数据: av_read_frame()
9. 判断帧的类型,对于视频帧调用: avcodec_decode_video2()
10.每读取完一个packet,解码之后,释放packet,然后重新读: av_free_packet(&packet)
11.释放分配的内存(所有的packet解码之后):av_frame_free
12. 解码完后,释放解码器: avcodec_close()
13. 关闭输入文件: avformat_close_input_file()
========
有几个函数不一定在基本流程里面使用,但是有必要介绍一下
av_malloc av_free
`av_malloc` is ffmpeg's malloc that is just a simple wrapper around malloc
that makes sure the memory addresses are aligned and such. It will _not_
protect you from memory leaks, double freeing, or other malloc problems.
翻译过来之后是:
av_malloc 简单的封装了malloc,保证了内存地址对齐,但是以前malloc具有的内存泄漏,多次释放和其他问题还是需要自己注意。
avpicture_fill
Now we use avpicture_fill to associate the frame with our newly allocated
buffer. About the AVPicture cast: the AVPicture struct is a subset of the
AVFrame struct - the beginning of the AVFrame struct is identical to the
AVPicture struct.
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
其中buffer是刚刚用av_malloc分配的内存