YUV420格式转换为H264 2D格式

 H264_2D格式

 

#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;

 

}

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值