整体方案:
采集端:摄像头采集(YUV)->编码(YUV转H264)->RTMP推流
客户端:RTMP拉流->解码(H264转YUV)->YUV显示(SDL2)
H264码流转YUV是视频解码部分,具体的代码实现如下。
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endifint main(int argc, char *argv[])
{char* in_file = NULL;char* out_file = NULL;av_log_set_level(AV_LOG_INFO);if(argc < 3){av_log(NULL, AV_LOG_ERROR, "缺少输入文件和输出文件参数");return -1;}in_file = argv[1];out_file = argv[2];AVFormatContext *fmt_ctx = NULL;AVCodecContext *cod_ctx = NULL;AVCodec *cod = NULL;struct SwsContext *img_convert_ctx = NULL;int ret = 0;AVPacket packet;//第一步创建输入文件AVFormatContextfmt_ctx = avformat_alloc_context();if (fmt_ctx == NULL){ret = -1;av_log(NULL, AV_LOG_ERROR,"alloc fail");goto __ERROR;}if (avformat_open_input(&fmt_ctx