使用FFMPEG将非YUV420p压缩成JPG

网上有很多使用FFMPEG将YUV压缩成JPG的文章和代码,不过基本上都是针对YUV420P,以YUV420的内存排布方式顺序填充AVFrame的三个data数组再编码JPG,但是有时候我们存储下来的数据并非YUV420P,比如有些安防芯片ISP处理后出来的是NV12格式,UV交织排布的,因此还需通过ffmpeg做下格式转换,以下是我添加了格式转化后的代码,以供有需要的人参考:

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

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")//
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "swscale.lib")
static int Frame2JPG(AVFrame* pFrame, char *fileName)
{
	AVFormatContext* pFormatCtx = NULL;
	AVPacket pkt;
	AVStream* pAVStream = NULL;
	AVCodecContext* pCodecCtx = NULL;
	AVCodec* pCodec = NULL;
	int ret = -1;
	int width = 0, height = 0;
	char out_file[MAX_PATH] = { 0 };
	if (pFrame == NULL) {
		DBG_PRINT("pFrame error\n");
		return ret;
	}
	width = pFrame->width;
	height = pFrame->height;
	if (width <= 0 || height <= 0) {
		DBG_PRINT("[%d x %d] error\n",width,height);
		return ret;
	}
	if (fileName == NULL) {
		sprintf_s(out_file, sizeof(out_file), "%s.jpg", "test");
	}
	else {
		strcpy_s(out_file, fileName);
	}
	pFormatCtx = avformat_alloc_context();
	pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);

	if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
	{
		DBG_PRINT("Couldn't open output file");
		goto LEAVE;
	}
	
	pAVStream = avformat_new_stream(pFormatCtx, 0);
	if (pAVStream == NULL)
	{
		DBG_PRINT("Frame2JPG::avformat_new_stream error.\n");
		goto LEAVE;
	}
	
	pCodecCtx = avcodec_alloc_context3(NULL);
	if (pCodecCtx == NULL)
	{
		DBG_PRINT("Could not allocate AVCodecContext\n");
		goto LEAVE;
	}
	avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[AVMEDIA_TYPE_VIDEO]->codecpar);

	pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;
	pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
	pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
	pCodecCtx->width = width;
	pCodecCtx->height = height;
	pCodecCtx->time_base.num = 1;
	pCodecCtx->time_base.den = 25;

	pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
	if (!pCodec)
	{
		DBG_PRINT("avcodec_find_encoder() error.\n");
		goto LEAVE;
	}
	
	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
	{
		DBG_PRINT("Could not open codec.\n");
		goto LEAVE;
	}
	 
	ret = avformat_write_header(pFormatCtx, NULL);
	if (ret < 0)
	{
		DBG_PRINT("avformat_write_header() error.\n");
		goto LEAVE;
	}
	int y_size = pCodecCtx->width * pCodecCtx->height;
	av_new_packet(&pkt, y_size * 3);
	ret = avcodec_send_frame(pCodecCtx, pFrame);
	if (ret < 0) {
		DBG_PRINT("Error encoding video frame: %s\n");
		av_packet_unref(&pkt);
		goto LEAVE;
	}
	while (ret >= 0)
	{
		ret = avcodec_receive_packet(pCodecCtx, &pkt);
		if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
			break;
		}
		else if (ret < 0) {
			fprintf(stderr, "Error encoding audio frame\n");
			break;
		}
		ret = av_write_frame(pFormatCtx, &pkt);
	}

	//Write Trailer  
	av_packet_unref(&pkt);
	av_write_trailer(pFormatCtx);
	ret = 0;
LEAVE:
	avcodec_close(pCodecCtx);
	pCodecCtx = NULL;
	avio_close(pFormatCtx->pb);
	avformat_free_context(pFormatCtx);
	pFormatCtx = NULL;
	return ret;
}

bool YUV2JPGFile(const unsigned char *psrc, int width, int height, AVPixelFormat src_fmt, const char *fileName)
{
	int ret = 0;
	struct SwsContext	*img_convert_ctx = NULL;
	unsigned char *out_buffer = NULL;
	AVFrame *pFrame = NULL, *pFrameYUV=NULL;
	pFrame = av_frame_alloc();
	pFrameYUV = av_frame_alloc();
	if (pFrame == NULL || pFrameYUV == NULL)
	{
		DBG_PRINT("memory allocation error\n");
		return false;
	}

	pFrame->format = src_fmt;
	pFrame->width = width;
	pFrame->height = height;
	out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 1));
	av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,
		AV_PIX_FMT_YUV420P, width, height, 1);
	img_convert_ctx = sws_getContext(width, height, src_fmt,
		width,height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

	av_image_fill_arrays(pFrame->data,
		pFrame->linesize,
		psrc,
		src_fmt,
		width,
		height,
		1);
	sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, height,
		pFrameYUV->data, pFrameYUV->linesize);
	pFrameYUV->format = AV_PIX_FMT_YUV420P;
	pFrameYUV->width = width;
	pFrameYUV->height = height;
	ret = Frame2JPG(pFrameYUV, fileName);
	sws_freeContext(img_convert_ctx);
	img_convert_ctx = NULL;
	av_free(out_buffer);
	out_buffer = NULL;
	av_frame_free(&pFrameYUV);
	av_frame_free(&pFrame);
	if (ret != 0)return false;
	return true;
}

当然如果是解码出来的YUV压缩JPG可以在解码后做格式转换,不用像这样这么麻烦;
个人对ffmpeg理解还比较浅显,如果有错误和改进地方欢迎指正,谢谢!


版权声明:本文为lishi_1991原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。