#ifndef _H264_2D_H
#define _H264_2D_H
#define DEF_H264_2D_Y_UNIT_WIDTH 16
#define DEF_H264_2D_Y_UNIT_HEIGHT 16
#define DEF_H264_2D_Y_UNIT (DEF_H264_2D_Y_UNIT_WIDTH*DEF_H264_2D_Y_UNIT_HEIGHT)
#define DEF_H264_2D_UV_UNIT_WIDTH 16
#define DEF_H264_2D_UV_UNIT_HEIGHT 8
#define DEF_H264_2D_UV_UNIT (DEF_H264_2D_UV_UNIT_WIDTH*DEF_H264_2D_UV_UNIT_HEIGHT)
int convertH264_2D_Y(char *src,char *dst,int src_W,int src_H);
int convertH264_2D_UV(char *src_u,char *src_v,char *dst,int src_W,int src_H);
#endif
#include "../include/h264_2d.h"
/****************************
*
*
*
*eg:如果是CIF输入转码:应当为convertH264_2D_Y(&src,&dst,352,288)
* ************************/
int convertH264_2D_Y(char *src,char *dst,int src_W,int src_H)
{
int wTimes = 0,hTimes = 0;
int nSrc = 0, nDst =0;
int iw=0,ih=0;
int nTotal=0;
int w=0,h=0;
int nUnits = (src_W * src_H)/(DEF_H264_2D_Y_UNIT);
if(src_W>0 && (src_W%DEF_H264_2D_Y_UNIT_WIDTH==0))
{
wTimes = src_W/DEF_H264_2D_Y_UNIT_WIDTH;
}else
{
printf("ERROR: Width must be %d times and greater than 0",DEF_H264_2D_Y_UNIT_WIDTH);
return -1;
}
if(src_H>0 && (src_H%DEF_H264_2D_Y_UNIT_HEIGHT==0))
{
hTimes = src_H/DEF_H264_2D_Y_UNIT_HEIGHT;
}else
{
printf("ERROR: Height must be %d times and greater than 0",DEF_H264_2D_Y_UNIT_HEIGHT);
return -2;
}
#if 0
for(h = 0; h < hTimes; h ++)
{
for(w = 0; w < wTimes; w ++)
{
for(ih = 0;ih < DEF_H264_2D_Y_UNIT_HEIGHT; ih ++)
{
for(iw = 0;iw < DEF_H264_2D_Y_UNIT_WIDTH; iw ++)
{
dst[h*(wTimes*DEF_H264_2D_Y_UNIT) + w*DEF_H264_2D_Y_UNIT + ih*DEF_H264_2D_Y_UNIT_HEIGHT + iw] = src[h*(wTimes*DEF_H264_2D_Y_UNIT) + w*DEF_H264_2D_Y_UNIT_HEIGHT + ih*DEF_H264_2D_Y_UNIT +iw];
}
}
}
}
#else
for(h = 0; h < hTimes; h ++)
{
for(w = 0; w < wTimes; w ++)
{
nDst = ( h * wTimes + w) * DEF_H264_2D_Y_UNIT;
nSrc = h * ( wTimes * DEF_H264_2D_Y_UNIT ) + w*DEF_H264_2D_Y_UNIT_HEIGHT;
for(ih = 0;ih < DEF_H264_2D_Y_UNIT_HEIGHT; ih ++)
{
for(iw = 0;iw < DEF_H264_2D_Y_UNIT_WIDTH; iw ++)
{
dst[ nDst + ih*DEF_H264_2D_Y_UNIT_WIDTH + iw] = src[ nSrc + ih*src_W +iw];
nTotal++;
}
}
}
}
#endif
return nTotal;
}
/****************************
*
*
*
*eg:如果是CIF输入转码:应当为convertH264_2D_UV(&v_src,&u_dst,176,144)
* ************************/
int convertH264_2D_UV(char *src_v,char *src_u,char *dst,int src_W,int src_H)
{
int wTimes = 0,hTimes = 0;
int nSrc = 0, nDst =0;
int iw=0,ih=0;
int nTotal=0;
int w=0,h=0;
int nUnits = (src_W * src_H)/(DEF_H264_2D_UV_UNIT);
if(src_W>0 && (src_W%DEF_H264_2D_UV_UNIT_WIDTH==0))
{
wTimes = 2*src_W/DEF_H264_2D_UV_UNIT_WIDTH;
}else
{
printf("ERROR: Width must be %d times and greater than 0",DEF_H264_2D_UV_UNIT_WIDTH);
return -1;
}
if(src_H>0 && (src_H%DEF_H264_2D_UV_UNIT_HEIGHT==0))
{
hTimes = src_H/DEF_H264_2D_UV_UNIT_HEIGHT;
}else
{
printf("ERROR: Height must be %d times and greater than 0",DEF_H264_2D_UV_UNIT_HEIGHT);
return -2;
}
#if 0
for(h = 0; h < hTimes; h ++)
{
for(w = 0; w < wTimes; w ++)
{
for(ih = 0;ih < DEF_H264_2D_UV_UNIT_HEIGHT; ih ++)
{
for(iw = 0;iw < DEF_H264_2D_UV_UNIT_WIDTH/2; iw ++)
{
dst[h*(wTimes*DEF_H264_2D_UV_UNIT) + w*DEF_H264_2D_UV_UNIT + ih*DEF_H264_2D_UV_UNIT_WIDTH + iw]
= src_u[h*(wTimes*DEF_H264_2D_UV_UNIT/2) + w*DEF_H264_2D_UV_UNIT_WIDTH/(2) + ih*DEF_H264_2D_UV_UNIT/2 +iw];
nTotal++;
dst[h*(wTimes*DEF_H264_2D_UV_UNIT) + w*DEF_H264_2D_UV_UNIT + ih*DEF_H264_2D_UV_UNIT_WIDTH + iw + DEF_H264_2D_UV_UNIT_WIDTH/2]
= src_v[h*(wTimes*DEF_H264_2D_UV_UNIT/2) + w*DEF_H264_2D_UV_UNIT_WIDTH/(2) + ih*DEF_H264_2D_UV_UNIT/2 +iw];//-DEF_H264_2D_UV_UNIT_WIDTH/2
nTotal++;//TRACE("%d",)
}
}
}
}
#else
for(h = 0; h < hTimes; h ++)
{
for(w = 0; w < wTimes; w ++)
{
nDst = h*(wTimes*DEF_H264_2D_UV_UNIT) + w*DEF_H264_2D_UV_UNIT;
nSrc = h*(wTimes*DEF_H264_2D_UV_UNIT/2) + w*DEF_H264_2D_UV_UNIT_WIDTH/(2);
for(ih = 0;ih < DEF_H264_2D_UV_UNIT_HEIGHT; ih ++)
{
for(iw = 0;iw < DEF_H264_2D_UV_UNIT_WIDTH/2; iw ++)
{
dst[ nDst + ih*DEF_H264_2D_UV_UNIT_WIDTH + iw] = src_u[ nSrc + ih*src_W +iw];
nTotal++;
dst[ nDst + ih*DEF_H264_2D_UV_UNIT_WIDTH + iw + DEF_H264_2D_UV_UNIT_WIDTH/2] = src_v[ nSrc + ih*src_W +iw];
}
}
}
}
#endif
return nTotal;
}