网上有很多使用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")
int videoDecoder::Frame2JPG(AVFrame *pFrame, const char *out_name)
{
int width = pFrame->width;
int height = pFrame->height;
AVCodecContext *pCodeCtx = NULL;
if(pFrame == NULL || out_name == NULL){
}
AVFormatContext *pFormatCtx = avformat_alloc_context();
if(pFormatCtx == nullptr){
return -1;
}
// 设置输出文件格式
pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);
// 创建并初始化输出AVIOContext
if (avio_open(&pFormatCtx->pb, out_name, AVIO_FLAG_READ_WRITE) < 0) {
printf("Couldn't open output file.");
avformat_free_context(pFormatCtx);
pFormatCtx = nullptr;
return -1;
}
// 构建一个新stream
AVStream *pAVStream = avformat_new_stream(pFormatCtx, 0);
if (pAVStream == NULL) {
avformat_free_context(pFormatCtx);
pFormatCtx = nullptr;
return -1;
}
AVCodecParameters *parameters = pAVStream->codecpar;
parameters->codec_id = pFormatCtx->oformat->video_codec;
parameters->codec_type = AVMEDIA_TYPE_VIDEO;
parameters->format = AV_PIX_FMT_YUVJ420P;
parameters->width = pFrame->width;
parameters->height = pFrame->height;
const AVCodec *pCodec = avcodec_find_encoder(pAVStream->codecpar->codec_id);
if (!pCodec) {
avformat_free_context(pFormatCtx);
pFormatCtx = nullptr;
printf("Could not find encoder\n");
return -1;
}
pCodeCtx = avcodec_alloc_context3(pCodec);
if (!pCodeCtx) {
fprintf(stderr, "Could not allocate video codec context\n");
return -1;
}
if ((avcodec_parameters_to_context(pCodeCtx, pAVStream->codecpar)) < 0) {
fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return -1;
}
pCodeCtx->time_base = (AVRational) {1, 25};
if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0) {
printf("Could not open codec.");
return -1;
}
int ret = avformat_write_header(pFormatCtx, NULL);
if (ret < 0) {
printf("write_header fail\n");
return -1;
}
int y_size = width * height;
//Encode
AVPacket pkt;
av_new_packet(&pkt, y_size * 3);
// 数据送往编码器
ret = avcodec_send_frame(pCodeCtx, pFrame);
if (ret < 0) {
printf("Could not avcodec_send_frame.");
return -1;
}
// 得到编码后数据
ret = avcodec_receive_packet(pCodeCtx, &pkt);
if (ret < 0) {
printf("Could not avcodec_receive_packet");
return -1;
}
ret = av_write_frame(pFormatCtx, &pkt);
if (ret < 0) {
printf("Could not av_write_frame");
return -1;
}
av_packet_unref(&pkt);
//Write Trailer
av_write_trailer(pFormatCtx);
avcodec_close(pCodeCtx);
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
return 0;
}
bool videoDecoder::YUV2JPGFile(unsigned char *psrc, const char *fileName, int width, int height, int fmt)
{
bool ret = false;
AVPixelFormat src_fmt = (AVPixelFormat)fmt;
struct SwsContext *img_convert_ctx = NULL;
unsigned char *out_buffer = NULL;
AVFrame *pSrcFrame = NULL, *pFrameYUV420 = NULL;
AVFrame *pFrame = NULL;
if(psrc == nullptr || width <= 0 || height <= 0)return false;
pSrcFrame = av_frame_alloc();
if (pSrcFrame == NULL){
printf("memory allocation error\n");
return ret;
}
pSrcFrame->format = src_fmt;
pSrcFrame->width = width;
pSrcFrame->height = height;
av_image_fill_arrays(pSrcFrame->data,
pSrcFrame->linesize,
psrc,
src_fmt,
width,
height,
1);
if (src_fmt != AV_PIX_FMT_YUV420P) {
pFrameYUV420 = av_frame_alloc();
if (pFrameYUV420 == NULL) {
printf("memory allocation error\n");
goto LEAVE;
}
out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 1));
av_image_fill_arrays(pFrameYUV420->data, pFrameYUV420->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);
if (img_convert_ctx == NULL) {
printf("sws_getContext error\n");
goto LEAVE;
}
sws_scale(img_convert_ctx, (const unsigned char* const*)pSrcFrame->data, pSrcFrame->linesize, 0, height,
pFrameYUV420->data, pFrameYUV420->linesize);
pFrameYUV420->format = AV_PIX_FMT_YUV420P;
pFrameYUV420->width = width;
pFrameYUV420->height = height;
pFrame = pFrameYUV420;
}else{
pFrame = pSrcFrame;
}
Frame2JPG(pFrame,fileName);
LEAVE:
if(img_convert_ctx)sws_freeContext(img_convert_ctx);
if (out_buffer)av_free(out_buffer);
if(pFrameYUV420)av_frame_free(&pFrameYUV420);
av_frame_free(&pSrcFrame);
if (ret != 0)return false;
return true;
}
当然如果是解码出来的YUV压缩JPG可以在解码后做格式转换,不用像这样这么麻烦;
个人对ffmpeg理解还比较浅显,如果有错误和改进地方欢迎指正,谢谢!