總結(jié)
1轴或,獲取一幀視頻所占內(nèi)存大小(byte):avpicture_get_size(outPix,witd,height);
2仿贬,獲取一個(gè)像素點(diǎn)所占內(nèi)存大小(bit):av_get_bits_per_pixel(av_pix_fmt_desc_get(src_pix));
根據(jù)以上的數(shù)據(jù)計(jì)算一幀視頻所占的內(nèi)存大胁严:uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * src_bpp / 8);等價(jià)于 uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * 3 /2);
3龙屉,初始化AVFrame結(jié)構(gòu)體變量:
給AVFrame結(jié)構(gòu)體的data妻坝、linesize分配內(nèi)存空間:avpicture_fill((AVPicture *)outFrame,outBuffer,outPix,witd,height);
分配內(nèi)存空間并給data賦值:avpicture_fill((AVPicture *)inFrame, inbuffer, inPix, witd, height);
4市俊,不用AVFrame結(jié)構(gòu)體,自定義data透典、linesize
uint8_t *src_data[4];//聲明長度為4的數(shù)組(yuv420p晴楔,數(shù)組src_data第一個(gè)元素存著y的數(shù)據(jù),第二個(gè)元素存著u的數(shù)據(jù)峭咒,第三個(gè)元素存著v的數(shù)據(jù))
int src_linesize[4];//跟src_data對應(yīng)
uint8_t *dst_data[4];
int dst_linesiz[4];
av_image_alloc(src_data,src_linesize,src_w,src_h,src_pix,1);//根據(jù)像素格式分配相應(yīng)的內(nèi)存大小
av_image_alloc(dst_data,dst_linesiz,dst_w,dst_h,dst_pix,1);//根據(jù)像素格式分配相應(yīng)的內(nèi)存大小
memcpy(src_data[0], src_buffer, src_w*src_h );//給src_data的第一個(gè)元素賦值y的數(shù)據(jù)
memcpy(src_data[1], src_buffer + src_w*src_h, src_w*src_h / 4);//給src_data的第二個(gè)元素賦值u的數(shù)據(jù)
memcpy(src_data[2], src_buffer + src_w*src_h + src_w*src_h/4, src_w*src_h/4);//給src_data的第三個(gè)元素賦值v的數(shù)據(jù)
5, SwsContext:提供一幀視頻數(shù)據(jù)像素格式轉(zhuǎn)換的上下文
1税弃,初始化
1.1 sws_getContext初始化
SwsContext *sws_ctx = sws_getContext(
src_w,src_h,src_pix,
dst_w,dst_h,dst_pix,
SWS_BICUBIC, NULL, NULL, NULL
);
1.2 復(fù)雜但是更靈活的初始化方法
struct SwsContext *img_convert_ctx;
img_convert_ctx =sws_alloc_context();
//Show AVOption
av_opt_show2(img_convert_ctx,stdout,AV_OPT_FLAG_VIDEO_PARAM,0);
//Set Value
av_opt_set_int(img_convert_ctx,"sws_flags",SWS_BICUBIC|SWS_PRINT_INFO,0);
av_opt_set_int(img_convert_ctx,"srcw",src_w,0);
av_opt_set_int(img_convert_ctx,"srch",src_h,0);
av_opt_set_int(img_convert_ctx,"src_format",src_pixfmt,0);
//'0' for MPEG (Y:0-235);'1' for JPEG (Y:0-255)
av_opt_set_int(img_convert_ctx,"src_range",1,0);
av_opt_set_int(img_convert_ctx,"dstw",dst_w,0);
av_opt_set_int(img_convert_ctx,"dsth",dst_h,0);
av_opt_set_int(img_convert_ctx,"dst_format",dst_pixfmt,0);
av_opt_set_int(img_convert_ctx,"dst_range",1,0);
sws_init_context(img_convert_ctx,NULL,NULL);
2,一幀數(shù)據(jù)轉(zhuǎn)換函數(shù):sws_scale(根據(jù)提供的像素格式來轉(zhuǎn)化為相應(yīng)的數(shù)據(jù))
sws_scale(sws_ctx,inFrame->data,inFrame->linesize,0,height,outFrame->data,outFrame->linesize);//借助AVFrame結(jié)構(gòu)體的變量來保存數(shù)據(jù)
sws_scale(sws_ctx,src_data,src_linesize,0,src_h,dst_data,dst_linesiz);//自定義變量來保存數(shù)據(jù)
3凑队,關(guān)閉上下文: sws_freeContext(sws_ctx);
6则果,像素格式:AVPixelFormat
AV_PIX_FMT_YUV420P //平面類型
AV_PIX_FMT_RGB24。//打包類型
7,圖像拉伸
SWS_POINT:鄰域插值,1個(gè)點(diǎn)確定插值的點(diǎn)
SWS_BILINEAR:雙線性插值,4個(gè)點(diǎn)確定插值的點(diǎn)
SWS_BICUBIC:雙三次插值,16個(gè)點(diǎn)確定插值的點(diǎn)
8,yuv取值范圍
Y的取值范圍是16-235漩氨,U西壮、V的取值范圍是16-240。FFmpeg中稱之為“mpeg”范圍叫惊。(0)
以JPEG為代表的標(biāo)準(zhǔn)中款青,Y、U霍狰、V的取值范圍都是0-255抡草。FFmpeg中稱之為“jpeg” 范圍。(1)
src_range
dst_range
一蔗坯,swcale實(shí)現(xiàn)rgb24轉(zhuǎn)yuv420p
int main(int argc, char **argv){
av_register_all();
FILE *infile = fopen("sucai.bmp","rb+");
int witd = 400;
int height = 400;
uint8_t *inbuffer = (uint8_t *)malloc(3 * witd * height);
printf("%x---%x", inbuffer, &inbuffer);
fread(inbuffer, witd*height * 3, 1, infile);
fclose(infile);
enum AVPixelFormat outPix = AV_PIX_FMT_YUV420P, inPix = AV_PIX_FMT_BGR24;
AVFrame *outFrame = av_frame_alloc();
uint8_t *outBuffer = new uint8_t[avpicture_get_size(outPix,witd,height)];
avpicture_fill((AVPicture *)outFrame,outBuffer,outPix,witd,height);
AVFrame *inFrame = av_frame_alloc();
avpicture_fill((AVPicture *)inFrame, inbuffer, inPix, witd, height);
SwsContext *sws_ctx = sws_getContext(
witd,height,inPix,
witd,height,outPix,
SWS_BILINEAR,NULL,NULL,NULL
);
sws_scale(sws_ctx,inFrame->data,inFrame->linesize,0,height,outFrame->data,outFrame->linesize);
sws_freeContext(sws_ctx);
FILE *outFile = fopen("output444.yuv","wb+");
fwrite(outFrame->data[0],witd*height,1,outFile);
fwrite(outFrame->data[1], witd*height / 4, 1, outFile);
fwrite(outFrame->data[2], witd*height / 4, 1, outFile);
fclose(outFile);
return 0;
}
二康震,swcale實(shí)現(xiàn)YUV轉(zhuǎn)RGB
版本一
int main(int argc, char **argv){
av_register_all();
AVPixelFormat src_pix = AV_PIX_FMT_YUV420P, dst_pix = AV_PIX_FMT_RGB24;
FILE *src_file = fopen("sintel_480x272_yuv420p.yuv","rb+");
int src_w = 480, src_h = 272;
uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * 3 /2);
FILE *dst_file = fopen("sintel_1280x720_rgb42-6.rgb","wb+");
int dst_w = 1280, dst_h = 720;
//uint8_t *dst_buffer = (uint8_t *)malloc(dst_w * dst_h * 3);
uint8_t *dst_buffer = new uint8_t[avpicture_get_size(dst_pix, dst_w, dst_h)];//可以使用api提供的方法來設(shè)置dst_buffer所占內(nèi)存的大小
AVFrame *src_frame = av_frame_alloc();
AVFrame *dst_frame = av_frame_alloc();
avpicture_fill((AVPicture *)src_frame, src_buffer, src_pix, src_w, src_h);
avpicture_fill((AVPicture *)dst_frame, dst_buffer, dst_pix, dst_w, dst_h);
SwsContext *sws_ctx = sws_getContext(
src_w,src_h,src_pix,
dst_w,dst_h,dst_pix,
SWS_BICUBIC, NULL, NULL, NULL
);
int i = 0;
while (1)
{
if (fread(src_buffer, 1, src_w*src_h * 3 / 2, src_file) != src_w*src_h * 3 / 2){
break;
}
sws_scale(sws_ctx,src_frame->data,src_frame->linesize,0,src_h,dst_frame->data,dst_frame->linesize);
printf("%d------%d\n", i, sizeof(src_buffer));
fwrite(dst_frame->data[0], 1, dst_w * dst_h * 3, dst_file);
i++;
}
sws_freeContext(sws_ctx);
fclose(src_file);
fclose(dst_file);
return getchar();
}
版本二
int main(int argc, char **argv){
av_register_all();
AVPixelFormat src_pix = AV_PIX_FMT_YUV420P, dst_pix = AV_PIX_FMT_RGB24;
FILE *src_file = fopen("sintel_480x272_yuv420p.yuv","rb+");
int src_w = 480, src_h = 272;
int src_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(src_pix));
uint8_t *src_buffer = (uint8_t *)malloc(src_w * src_h * src_bpp / 8);
FILE *dst_file = fopen("sintel_1280x720_rgb42-9.rgb","wb+");
int dst_w = 1280, dst_h = 720;
int dst_bpp = av_get_bits_per_pixel(av_pix_fmt_desc_get(dst_pix));
uint8_t *src_data[4];
int src_linesize[4];
uint8_t *dst_data[4];
int dst_linesiz[4];
av_image_alloc(src_data,src_linesize,src_w,src_h,src_pix,1);
av_image_alloc(dst_data,dst_linesiz,dst_w,dst_h,dst_pix,1);
SwsContext *sws_ctx = sws_getContext(
src_w,src_h,src_pix,
dst_w,dst_h,dst_pix,
SWS_BICUBIC, NULL, NULL, NULL
);
int i = 0;
while (1)
{
if (fread(src_buffer, 1, src_w*src_h * src_bpp / 8, src_file) != src_w*src_h * src_bpp / 8){
break;
}
memcpy(src_data[0], src_buffer, src_w*src_h );
memcpy(src_data[1], src_buffer + src_w*src_h, src_w*src_h / 4);
memcpy(src_data[2], src_buffer + src_w*src_h + src_w*src_h/4, src_w*src_h/4);
sws_scale(sws_ctx,src_data,src_linesize,0,src_h,dst_data,dst_linesiz);
printf("%d------%d\n", i, sizeof(src_buffer));
fwrite(dst_data[0], 1, dst_w * dst_h * dst_bpp / 8, dst_file);
i++;
}
sws_freeContext(sws_ctx);
fclose(src_file);
fclose(dst_file);
return getchar();
}