在Nvidia release的图片转换库的基础上,盒子提供了更加简单易用的libpicconverter.so库,简单的4个API接口就可以完成绝大部分的操作,方便客户专注于上层AI软件的开发。
获取示例代码
git clone https://github.com/apoidea-xhiveai/jetson.git
图片转换的代码路径为:jetson/pic_converter
编译示例代码
copy代码到盒子上
scp -r pic_converter root@<ai box ip address>:/root
ssh登录盒子后,执行以下命令来编译代码:
cd /root/pic_converter
make
编译成功后,生成可执行文件:pic_converter
libpicconverter库的说明
在代码树中,库的头文件为:picconverter.h
1) 基本使用流程为,首先根据配置来创建一个图片转换句柄
/*
* To generate the picture converter handler
*/
PIC_CONV_HANDLE_t PicConvInit(PIC_CONV_IN PicSetting_t *config);
配置信息定义在PicSetting_t结构体中:
typedef struct _PicSetting_t {
PicParam_t src; /*source picture*/
PicParam_t dest; /*destination picture*/PicCropRect_t *cropping; /*NULL: disabled*/
PicFlip_t flip; /*default: NONE*/
PicInterp_t interp; /*default: bilinear*/PicDataType_t pic_type;
int play_id; /*multiple channel id: 1, 2, 3...*/
} PicSetting_t;
2) 对于每帧输入图片,调用下面一个API来实现转换
这两个API本质上实现相同的转换工作,唯一的不同在于dest_pic。 PicConvProc() API不需要调用程序提供buffer,直接返回一个填满转换数据的buffer。PicConvProc_copy()需要调用程序提供足够大的buffer,API会把转换后的数据copy到这个buffer中,这样可以节省一次copy操作。用户可以根据实际的需要来使用。
/*
* To do the configured picture converter
* void *src_pic: the source picture data
* user space buffer: unsigned char * OR ffmpeg AVFrame*
*
* void **dest_pic: the converted picture data
* Created in library and returned back to the caller
*
* unsigned int *dest_pic_sz: the size of the converted picture data
*/
int PicConvProc(PIC_CONV_IN PIC_CONV_HANDLE_t handle,
PIC_CONV_IN void *src_pic,
PIC_CONV_OUT void **dest_pic,
PIC_CONV_OUT unsigned int *dest_pic_sz);/*
void *dest_pic: provided by outside app,
copy the converted picture to outside
*/
int PicConvProc_copy(PIC_CONV_IN PIC_CONV_HANDLE_t handle,
PIC_CONV_IN void *src_pic,
PIC_CONV_INOUT void *dest_pic,
PIC_CONV_OUT unsigned int *dest_pic_sz);
3)转换全部完成后,释放句柄
/*
* To release the picture converter handler
*/
void PicConvRelease(PIC_CONV_IN PIC_CONV_HANDLE_t handle);
执行示例代码
示例程序实现了一个简单的图片转换功能:从文件读入某个尺寸的YUV/RGB图片,然后转换为另外一个尺寸和另外一个像素格式的图片,写入输出文件。
命令的格式如下:
./pic_converter
./pic_converter (compiled Jul 20 2022)
Usage ./pic_converter [OPTION]
-i <input raw picture file>
-o <output converted picture file>
-s <input picture format: (width),(height),(pixel format: nv12/yuv420/bgra/rgba)>
-c <output picture format: (width),(height),(pixel format: nv12/yuv420/bgra/rgba)>
例子:
./pic_converter -i ../ffmpeg/out.yuv -o conv.rgb -s 1920,1080,yuv420 -c 640,480,rgba
将宽1920,高1080的像素格式为yuv420的图片转换为宽640,高480的像素格式为rgba的图片。