我的图像格式是“YUV422_8_UYVY”,它是打包的AV_PIX_FMT_UYVY422格式,我正在尝试将其转换为平面“ AV_PIX_FMT_YUVJ422P ”,但还没有成功,下面是我正在处理的代码。
错误消息:[swscaler @ 004b3fa0] deprecetd pixel format used,确保你正确设置了范围
具有 0 k 大小的结果图像(文件)
av_image_alloc()用于 16,32 等转换的最后一个参数是什么
我的目标是将数据包 yuv 图像转换为平面 yuv 格式
static AVCodecContext *pCodecCtx;
static AVFormatContext *pFormatCtx;
static AVCodec *pCodec;
static AVOutputFormat* fmt;
static AVFrame *RawPic;
static AVFrame *ScalePic;
static AVPacket pkt;
static AVStream* video_st;
static FILE *file;
static struct SwsContext *sws_ctx;
enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_UYVY422;
enum AVPixelFormat dst_pix_fmt = AV_PIX_FMT_YUVJ422P;
int main( ) {
FILE *in_file = NULL; //packed Source
FILE *out_file = NULL; //planar output
int in_width = 2448; //YUV's width
int in_height = 2050; //YUV's heigh
int out_width = 2448; //YUV's width
int out_height = 2050; //YUV's heigh
unsigned long int ret;
in_file = fopen("c:\\yuv422_8_uyvy.yuv","rb"); //Source Input File
if(in_file == NULL) { printf("\n\tinput File Opening error...!!"); exit(1); }
out_file = fopen("d:\\test_Planar.yuv", "wb"); //Source Input File
if(out_file == NULL) { printf("\n\toutput File Opening error...!!"); exit(1); }
else { printf("\n\tOutput File Created...!!"); }
//------Loads the whole database of available codecs and formats------
av_register_all();
printf("\t\n\tCodac database Loaded...\n");
//------Contex Variable assignment--------------------------------
pFormatCtx = avformat_alloc_context();
fmt = NULL;
fmt = av_guess_format("mjpeg",NULL,NULL);
pFormatCtx->oformat = fmt;
video_st = avformat_new_stream(pFormatCtx, 0); if (video_st==NULL) return -1;
pCodecCtx = video_st->codec;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = src_pix_fmt;
printf("\t\n\tContex Variable assigned...\n");
//------Allocate Source Image Buffer--------------------------------
AVFrame *RawPic = av_frame_alloc();
if(!RawPic) { printf("\nCould not allocate Raw Image frame\n"); exit(1);}
RawPic->format = pCodecCtx->pix_fmt;
RawPic->width = in_width;
RawPic->height = in_height;
ret = av_image_alloc(RawPic->data,RawPic->linesize,in_width,in_height,src_pix_fmt, 16);
if(ret < 0) { printf("\nCould not allocate raw picture buffer\n"); exit(1);}
printf("\n\tAllocate Source Image Buffer");
//------Allocate Desitnation Image Buffer-------------------
AVFrame *ScalePic = av_frame_alloc();
if(!ScalePic) { printf("\nCould not allocate Scale Image frame\n"); exit(1);}
ScalePic->format = pCodecCtx->pix_fmt;
ScalePic->width = out_width;
ScalePic->height = out_height;
ret = av_image_alloc(ScalePic->data,ScalePic->linesize,out_width,out_height,dst_pix_fmt, 32);
if(ret < 0) { printf("\nCould not allocate Scale picture buffer\n"); exit(1);}
dst_bufsize = ret;
printf("\n\tAllocate Destination Image Buffer");
//------Create scaling context------------------------------sws_getContex
printf("\t\n\tCreating Scaling context..[sws_getContext]\n");
sws_ctx = sws_getContext( in_width, in_height, src_pix_fmt,
out_width, out_height, dst_pix_fmt,
SWS_BICUBIC, NULL, NULL, NULL);
if(!sws_ctx) { printf("\nContext Error..\n"); }
printf("\t\n\tScaling context...Created\n");
//------Create scaling context---OR CONVERTED TO DESTINATION FORMAT--
sws_scale(sws_ctx, RawPic->data, RawPic->linesize, 0, in_height, ScalePic->data, ScalePic->linesize);
printf("\t\n\tCreating Scaling context...sws_scale...done\n");
int num_bytes = avpicture_get_size(src_pix_fmt,in_width,in_height);
uint8_t* ScalePic_Buffer = (uint8_t *)av_malloc(num_bytes*sizeof(int8_t));
avpicture_fill((AVPicture*)ScalePic,ScalePic_Buffer,AV_PIX_FMT_YUVJ422P,out_width,out_height);
//-----Write Scale Image to outputfile----------------------------
fwrite(ScalePic->data,1,dst_bufsize,out_file);
//---Release all memory and close file----------------------------------
fclose(in_file);
fclose(out_file);
avcodec_close(pCodecCtx);
av_free(pCodecCtx);
av_freep(&RawPic->data[0]);
av_frame_free(&RawPic);
av_freep(&ScalePic->data[0]);
av_frame_free(&ScalePic);
av_frame_free(&RawPic);
printf("\n\n");
system("PAUSE");
exit(1);
}