基于libyuv库的NV12格式图片的缩放

来源:互联网 发布:2010年总决赛数据 编辑:程序博客网 时间:2024/06/04 19:54

网上关于使用libyuv库在Linux下对NV12格式进行缩放的教程是在太少了,对于博主这种菜鸡来说简直就是煎熬,因为本人阅读源码的能力实在很差啊!!!但不管怎么样,把这几天所得写下来,希望对大家有帮助。

1、libyuv库简介

libyuv是Google开源的实现各种YUV与RGB之间相互转换、旋转、缩放的库。它是跨平台的,可在Windows、Linux、Mac、Android等操作系统,x86、x64、arm架构上进行编译运行,支持SSE、AVX、NEON等SIMD指令加速。

编译方法(注意编译要求gcc版本必须在4.8以上),把libyuv库克隆到本地仓库,进入libyuv目录,进行编译:

git clone https://github.com/lemenkov/libyuv.gitcd libyuvmake -f linux.mk

就会在当前目录下生成一个libyuv.a静态库,本人没有将libyuv编译安装到本地。只是将libyuv.a静态库 和 include头文件抽取出来单独运行(所需文件在附件那里下载)。
执行 make 就能生成一个可执行文件了。执行可执行文件,可将像素为1920*1080的NV12格式的图片缩小成1280*720。


2、YUV420简介

libyuv不能对NV12格式的图片直接缩放,必须先把NV12转化成I420(内存分布格式是与YV12一样的),其实NV12和YV12同属YUV420,只是UV分量的内存分布形式不一样而已。现在先来了解一下,二者的不同。废话少说,上图(注意看UV分量的内存分布,YV12是YUV三个分量各自顺序存储的,而NV12的UV分量是交叉存储的):
这里写图片描述
这里写图片描述

其实对于此次基于libyuv库的转换来说以下YV12的内存分布更正确(红色方框内的是Y分量,蓝色是U分量,黄色方框的是V分量,注意这个对于指定YUV个分量的内存范围很重要!!!比如:一张YV12像素为WxH的图片来说,Y分量为W,UV都为W/2):
这里写图片描述

到此基础知识就这样了。


3、程序实现缩小NV12格式图片

实现算法:利用libyuv库的libyuv::NV12ToI420()函数将NV12转换成I420,再利用libyuv::I420Scale()函数将I420从像素1920*1080缩小成1280*720,最后利用libyuv::I420ToNV12()函数转换回NV12格式

#include <stdio.h>#include <stdlib.h>#include "libyuv.h"int NV12Scale(uint8 *psrc_buf, int psrc_w, int psrc_h, uint8 *pdst_buf, int pdst_w, int pdst_h, libyuv::FilterModeEnum pfmode);int main(int argc, const char *argv[]){    const int src_w = 1920;    const int src_h = 1080;    const int dst_w = 1280;    const int dst_h =  720;    libyuv::FilterModeEnum fmode = libyuv::kFilterNone;    char inputPathname[30]  = "videotestsrc_1920x1080.nv12";    char outputPathname[30] = "videotestdst_1280x720.nv12";    FILE * fin  = fopen(inputPathname,  "rb");    FILE * fout = fopen(outputPathname, "wb+");    /* Allocate memory for nv12 */    uint8 *src_buf = (uint8 *)malloc((src_w * src_h * 3) >> 1);    uint8 *dst_buf = (uint8 *)malloc((dst_w * dst_h * 3) >> 1);    /* Read file data to buffer */    fread(src_buf, sizeof(uint8), (src_w * src_h * 3) >> 1, fin);    /* Scale NV12 */    NV12Scale(src_buf, src_w, src_h, dst_buf, dst_w, dst_h, fmode);    /* Write the data to file */    fwrite(dst_buf, sizeof(uint8), (dst_w * dst_h * 3) >> 1, fout);    free(src_buf);    free(dst_buf);    fclose(fin);    fclose(fout);    return 0;}int NV12Scale(uint8 *psrc_buf, int psrc_w, int psrc_h, uint8 *pdst_buf, int pdst_w, int pdst_h, libyuv::FilterModeEnum pfmode){    uint8 *i420_buf1 = (uint8 *)malloc((psrc_w * psrc_h * 3) >> 1);    uint8 *i420_buf2 = (uint8 *)malloc((pdst_w * pdst_h * 3) >> 1);    /* NV12_1920x1080 -> I420_1920x1080 */    libyuv::NV12ToI420(&psrc_buf[0],                           psrc_w,                       &psrc_buf[psrc_w * psrc_h],             psrc_w,                       &i420_buf1[0],                          psrc_w,                       &i420_buf1[psrc_w * psrc_h],            psrc_w >> 1,                       &i420_buf1[(psrc_w * psrc_h * 5) >> 2], psrc_w >> 1,                       psrc_w, psrc_h);    /* I420_1920x1080 -> I420_1280x720 */    libyuv::I420Scale(&i420_buf1[0],                          psrc_w,                      &i420_buf1[psrc_w * psrc_h],            psrc_w >> 1,                      &i420_buf1[(psrc_w * psrc_h * 5) >> 2], psrc_w >> 1,                      psrc_w, psrc_h,                      &i420_buf2[0],                          pdst_w,                      &i420_buf2[pdst_w * pdst_h],            pdst_w >> 1,                      &i420_buf2[(pdst_w * pdst_h * 5) >> 2], pdst_w >> 1,                      pdst_w, pdst_h,                      pfmode);    /* I420_1280x720 -> NV12_1280x720 */    libyuv::I420ToNV12(&i420_buf2[0],                          pdst_w,                       &i420_buf2[pdst_w * pdst_h],            pdst_w >> 1,                       &i420_buf2[(pdst_w * pdst_h * 5) >> 2], pdst_w >> 1,                       &pdst_buf[0],                           pdst_w,                       &pdst_buf[pdst_w * pdst_h],             pdst_w,                       pdst_w,pdst_h);    free(i420_buf1);    free(i420_buf2);    return 0;}

4、附件

程序、测试NV12文件及libyuv静态库和头文件。
http://download.csdn.net/detail/sinat_36684217/9899856
yuv查看器(yvplayer)。
http://download.csdn.net/detail/sinat_36684217/9888652


5、NV12加矩形方框效果

想了解关于在NV12格式图片上加上矩形方框的效果可以去看我的另一博客篇。
http://blog.csdn.net/sinat_36684217/article/details/74347031