图像结构体
typedef struct __tag_ASVL_OFFSCREEN
{
MUInt32 u32PixelArrayFormat; //图像格式
MInt32 i32Width; //图像宽
MInt32 i32Height; //图像高
MUInt8* ppu8Plane[4]; //存储图像数据
MInt32 pi32Pitch[4]; //图像的步长
}ASVLOFFSCREEN, *LPASVLOFFSCREEN;
1、UYVY转NV12
MRESULT UYVY_NV12_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage)
{
/* TODO: Write your code here */
if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV12)
return MERR_UNSUPPORTED;
size_t width = pSrcImage->i32Width;
size_t height = pSrcImage->i32Height;
size_t y_size = width * height;
size_t pixels_in_a_row = width * 2;
char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
char *nv12_content = (char *)(pDstImage->ppu8Plane[0]);
char *nv12_y_ptr = nv12_content;
char *nv12_uv_ptr = nv12_content + y_size;
int lines = 0;
size_t frame_size = 2 * width * height;
for (int i = 0;i < frame_size;i += 4) {
// copy y channel
*nv12_y_ptr++ = uyvy_content[i + 1];
*nv12_y_ptr++ = uyvy_content[i + 3];
if (0 == i % pixels_in_a_row) {
++lines;
}
if (lines % 2) { // extract the UV value of odd rows
// copy uv channel
*nv12_uv_ptr++ = uyvy_content[i + 2];
*nv12_uv_ptr++ = uyvy_content[i];
}
}
return MOK;
}
2、UYVY转NV21
MRESULT UYVY_NV21_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage)
{
/* TODO: Write your code here */
if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV21)
return MERR_UNSUPPORTED;
size_t width = pSrcImage->i32Width;
size_t height = pSrcImage->i32Height;
size_t y_size = width * height;
size_t pixels_in_a_row = width * 2;
char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
char *nv21_content = (char *)(pDstImage->ppu8Plane[0]);
char *nv21_y_ptr = nv21_content;
char *nv21_uv_ptr = nv21_content + y_size;
int lines = 0;
size_t frame_size = 2 * width * height;
for (int i = 0;i < frame_size;i += 4) {
// copy y channel
*nv21_y_ptr++ = uyvy_content[i + 1];
*nv21_y_ptr++ = uyvy_content[i + 3];
if (0 == i % pixels_in_a_row) {
++lines;
}
if (lines % 2) { // extract the UV value of odd rows
// copy uv channel
*nv21_uv_ptr++ = uyvy_content[i];
*nv21_uv_ptr++ = uyvy_content[i + 2];
}
}
return MOK;
}
3、YUYV转NV12
MRESULT YUYV_NV12_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage)
{
/* TODO: Write your code here */
if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV12)
return MERR_UNSUPPORTED;
size_t width = pSrcImage->i32Width;
size_t height = pSrcImage->i32Height;
size_t y_size = width * height;
size_t pixels_in_a_row = width * 2;
char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
char *nv12_content = (char *)(pDstImage->ppu8Plane[0]);
char *nv12_y_ptr = nv12_content;
char *nv12_uv_ptr = nv12_content + y_size;
int lines = 0;
size_t frame_size = 2 * width * height;
for (int i = 0;i < frame_size;i += 4) {
// copy y channel
*nv12_y_ptr++ = uyvy_content[i];
*nv12_y_ptr++ = uyvy_content[i + 2];
if (0 == i % pixels_in_a_row) {
++lines;
}
if (lines % 2) { // extract the UV value of odd rows
// copy uv channel
*nv12_uv_ptr++ = uyvy_content[i + 1];
*nv12_uv_ptr++ = uyvy_content[i + 3];
}
}
return MOK;
}
4、I420转NV12
MRESULT I420_NV12_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage)
{
/* TODO: Write your code here */
if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_NV12)
return MERR_UNSUPPORTED;
size_t width = pSrcImage->i32Width;
size_t height = pSrcImage->i32Height;
size_t y_size = width * height;
size_t uv_size = width * height / 4;
char *i420_content = (char *)(pSrcImage->ppu8Plane[0]);
char *nv12_content = (char *)(pDstImage->ppu8Plane[0]);
char *i420_u_base = i420_content + y_size;
char *i420_v_base = i420_u_base + uv_size;
char *nv12_uv_base = nv12_content + y_size;
//copy y channel
memcopy(nv21_content, i420_content, y_size);
//copy uv channel
int i = 0, j = 0;
for (;i < uv_size;i++) {
nv12_uv_base[j] = i420_u_base[i];
nv12_uv_base[j + 1] = i420_v_base[i];
j += 2;
}
return MOK;
}
5、UYVY转I420
MRESULT UYVY_I420_Convertion(LPASVLOFFSCREEN pSrcImage, LPASVLOFFSCREEN pDstImage)
{
/* TODO: Write your code here */
if(!pSrcImage || !pDstImage || pDstImage->u32PixelArrayFormat != ASVL_PAF_I420)
return MERR_UNSUPPORTED;
size_t width = pSrcImage->i32Width;
size_t height = pSrcImage->i32Height;
size_t y_size = width * height;
size_t uv_size = width * height / 4;
char *uyvy_content = (char *)(pSrcImage->ppu8Plane[0]);
char *i420_content = (char *)(pDstImage->ppu8Plane[0]);
char *i420_y_base = i420_content;
char *i420_u_base = i420_y_base + width * height;
char *i420_v_base = i420_u_base + width * height / 4;
int y_index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < 2 * width; j += 4) {
y_index = i * 2 * width + j;
*i420_y_base++ = uyvy_content[y_index + 1];
*i420_y_base++ = uyvy_content[y_index + 3];
if (i % 2) {
*i420_u_base++ = uyvy_content[y_index];
*i420_v_base++ = uyvy_content[y_index + 2];
}
}
}
return MOK;
}