RGB24 转换为 YUV12 的算法

头文件:
#ifndef __rgb_2yuv_h__
#define __rgb_2yuv_h__

#ifdef __cplusplus
extern "C" 
ExpandedBlockStart.gif {
#endif 

int RGB2YUV (int x_dim, int y_dim, void* bmp, void* y_out, void* u_out, void* v_out, int flip);

#ifdef __cplusplus
}
#endif


#endif

实现文件:
ExpandedBlockStart.gif /**************************************************************************
*                                                                        *
* This code is developed by Adam Li.  This software is an                *
* implementation of a part of one or more MPEG-4 Video tools as          *
* specified in ISO/IEC 14496-2 standard.  Those intending to use this    *
* software module in hardware or software products are advised that its  *
* use may infringe existing patents or copyrights, and any such use      *
* would be at such party's own risk.  The original developer of this     *
* software module and his/her company, and subsequent editors and their  *
* companies (including Project Mayo), will have no liability for use of  *
* this software or modifications or derivatives thereof.                 *
*                                                                        *
* Project Mayo gives users of the Codec a license to this software       *
* module or modifications thereof for use in hardware or software        *
* products claiming conformance to the MPEG-4 Video Standard as          *
* described in the Open DivX license.                                    *
*                                                                        *
* The complete Open DivX license can be found at                         *
http://www.projectmayo.com/opendivx/license.php .                      *
*                                                                        *
*************************************************************************
*/

ExpandedBlockStart.gif /**************************************************************************
*
*  rgb2yuv.c, 24-bit RGB bitmap to YUV converter
*
*  Copyright (C) 2001  Project Mayo
*
*  Adam Li
*
*  DivX Advance Research Center <darc@projectmayo.com>
*
*************************************************************************
*/

ExpandedBlockStart.gif /* This file contains RGB to YUV transformation functions.                */

#include "stdlib.h"
#include "rgb2yuv.h"

static  float RGBYUV02990[256], RGBYUV05870[256], RGBYUV01140[256];
static  float RGBYUV01684[256], RGBYUV03316[256];
static  float RGBYUV04187[256], RGBYUV00813[256];

void InitLookupTable();

ExpandedBlockStart.gif /************************************************************************
*
*  int RGB2YUV (int x_dim, int y_dim, void *bmp, YUV *yuv)
*
*    Purpose :    It takes a 24-bit RGB bitmap and convert it into
*                YUV (4:2:0) format
*
*  Input :        x_dim    the x dimension of the bitmap
*                y_dim    the y dimension of the bitmap
*                bmp        pointer to the buffer of the bitmap
*                yuv        pointer to the YUV structure
*
*  Output :    0        OK
*                1        wrong dimension
*                2        memory allocation error
*
*    Side Effect :
*                None
*
*    Date :        09/28/2000
*
*  Contacts:
*
*  Adam Li
*
*  DivX Advance Research Center <darc@projectmayo.com>
*
***********************************************************************
*/

int RGB2YUV ( int x_dim,  int y_dim,  void *bmp,  void *y_out,  void *u_out,  void *v_out,  int flip)
ExpandedBlockStart.gif {
    static int init_done = 0;

    long i, j, size;
    unsigned char *r, *g, *b;
    unsigned char *y, *u, *v;
    unsigned char *pu1, *pu2, *pv1, *pv2, *psu, *psv;
    unsigned char *y_buffer, *u_buffer, *v_buffer;
    unsigned char *sub_u_buf, *sub_v_buf;

    if (init_done == 0)
ExpandedSubBlockStart.gif    {
        InitLookupTable();
        init_done = 1;
    }

    // check to see if x_dim and y_dim are divisible by 2
    if ((x_dim % 2) || (y_dim % 2)) return 1;
    size = x_dim * y_dim;

    // allocate memory
    y_buffer = (unsigned char *)y_out;
    sub_u_buf = (unsigned char *)u_out;
    sub_v_buf = (unsigned char *)v_out;
    u_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
    v_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
    if (!(u_buffer && v_buffer))
ExpandedSubBlockStart.gif    {
        if (u_buffer) free(u_buffer);
        if (v_buffer) free(v_buffer);
        return 2;
    }

    b = (unsigned char *)bmp;
    y = y_buffer;
    u = u_buffer;
    v = v_buffer;

    // convert RGB to YUV
ExpandedSubBlockStart.gif
    if (!flip) {
        for (j = 0; j < y_dim; j ++)
ExpandedSubBlockStart.gif        {
            y = y_buffer + (y_dim - j - 1) * x_dim;
            u = u_buffer + (y_dim - j - 1) * x_dim;
            v = v_buffer + (y_dim - j - 1) * x_dim;

ExpandedSubBlockStart.gif            for (i = 0; i < x_dim; i ++) {
                g = b + 1;
                r = b + 2;
                *y = (unsigned char)(  RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
                *u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2          + 128);
                *v = (unsigned char)(  (*r)/2          - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
                b += 3;
                y ++;
                u ++;
                v ++;
            }
        }
ExpandedSubBlockStart.gif    } else {
        for (i = 0; i < size; i++)
ExpandedSubBlockStart.gif        {
            g = b + 1;
            r = b + 2;
            *y = (unsigned char)(  RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
            *u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2          + 128);
            *v = (unsigned char)(  (*r)/2          - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
            b += 3;
            y ++;
            u ++;
            v ++;
        }
    }

    // subsample UV
    for (j = 0; j < y_dim/2; j ++)
ExpandedSubBlockStart.gif    {
        psu = sub_u_buf + j * x_dim / 2;
        psv = sub_v_buf + j * x_dim / 2;
        pu1 = u_buffer + 2 * j * x_dim;
        pu2 = u_buffer + (2 * j + 1) * x_dim;
        pv1 = v_buffer + 2 * j * x_dim;
        pv2 = v_buffer + (2 * j + 1) * x_dim;
        for (i = 0; i < x_dim/2; i ++)
ExpandedSubBlockStart.gif        {
            *psu = (*pu1 + *(pu1+1) + *pu2 + *(pu2+1)) / 4;
            *psv = (*pv1 + *(pv1+1) + *pv2 + *(pv2+1)) / 4;
            psu ++;
            psv ++;
            pu1 += 2;
            pu2 += 2;
            pv1 += 2;
            pv2 += 2;
        }
    }

    free(u_buffer);
    free(v_buffer);

    return 0;
}


void InitLookupTable()
ExpandedBlockStart.gif {
    int i;

    for (i = 0; i < 256; i++) RGBYUV02990[i] = (float)0.2990 * i;
    for (i = 0; i < 256; i++) RGBYUV05870[i] = (float)0.5870 * i;
    for (i = 0; i < 256; i++) RGBYUV01140[i] = (float)0.1140 * i;
    for (i = 0; i < 256; i++) RGBYUV01684[i] = (float)0.1684 * i;
    for (i = 0; i < 256; i++) RGBYUV03316[i] = (float)0.3316 * i;
    for (i = 0; i < 256; i++) RGBYUV04187[i] = (float)0.4187 * i;
    for (i = 0; i < 256; i++) RGBYUV00813[i] = (float)0.0813 * i;
}

测试程序:
ExpandedBlockStart.gif /*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at 
http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is MPEG4IP.
 * 
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 * Portions created by Cisco Systems Inc. are
 * Copyright (C) Cisco Systems Inc. 2000, 2001.  All Rights Reserved.
 * 
 * Contributor(s): 
 *        Dave Mackie        dmackie@cisco.com
 
*/

#include <mpeg4ip.h>
#include <mpeg4ip_getopt.h>
#include "rgb2yuv.h"


ExpandedBlockStart.gif /* globals */
char* progName;

ExpandedBlockStart.gif /*
 * rgb2yuv
 * required arg1 should be the input RAW RGB24 file
 * required arg2 should be the output RAW YUV12 file
 
*/ 
static  const  char *usage = 
"\t--flip           - flip image\n"
"\t--height <value> - specify height\n"
"\t--width <value>  - specify width\n"
"\t--version        - display version\n";

int main( int argc,  char** argv)
ExpandedBlockStart.gif {
ExpandedSubBlockStart.gif    /* variables controlable from command line */
ExpandedSubBlockStart.gif    u_int frameWidth = 320;            /* --width=<uint> */
ExpandedSubBlockStart.gif    u_int frameHeight = 240;        /* --height=<uint> */
ExpandedSubBlockStart.gif    bool flip = FALSE;                /* --flip */

ExpandedSubBlockStart.gif    /* internal variables */
    char* rgbFileName = NULL;
    char* yuvFileName = NULL;
    FILE* rgbFile = NULL;
    FILE* yuvFile = NULL;
    u_int8_t* rgbBuf = NULL;
    u_int8_t* yBuf = NULL;
    u_int8_t* uBuf = NULL;
    u_int8_t* vBuf = NULL;
    u_int32_t videoFramesWritten = 0;
    bool gotheight, gotwidth;

    gotheight = gotwidth = FALSE;

ExpandedSubBlockStart.gif    /* begin process command line */
    progName = argv[0];
ExpandedSubBlockStart.gif    while (1) {
        int c = -1;
        int option_index = 0;
ExpandedSubBlockStart.gif        static struct option long_options[] = {
ExpandedSubBlockStart.gif            { "flip", 0, 0, 'f' },
ExpandedSubBlockStart.gif            { "height", 1, 0, 'h' },
ExpandedSubBlockStart.gif            { "width", 1, 0, 'w' },
ExpandedSubBlockStart.gif            { "version", 0, 0, 'V' },
ExpandedSubBlockStart.gif            { NULL, 0, 0, 0 }
        };

        c = getopt_long_only(argc, argv, "fh:w:V",
            long_options, &option_index);

        if (c == -1)
            break;

ExpandedSubBlockStart.gif        switch (c) {
ExpandedSubBlockStart.gif        case 'f': {
            flip = TRUE;
            break;
        }
ExpandedSubBlockStart.gif        case 'h': {
ExpandedSubBlockStart.gif            /* --height <pixels> */
            u_int i;
            gotheight = TRUE;
ExpandedSubBlockStart.gif            if (sscanf(optarg, "%u", &i) < 1) {
                fprintf(stderr, 
                    "%s: bad height specified: %s\n",
                     progName, optarg);
ExpandedSubBlockStart.gif            } else if (i & 1) {
                fprintf(stderr, 
                    "%s: bad height specified, must be multiple of 2: %s\n",
                     progName, optarg);
ExpandedSubBlockStart.gif            } else {
ExpandedSubBlockStart.gif                /* currently no range checking */
                frameHeight = i;
            }
            break;
        }
ExpandedSubBlockStart.gif        case 'w': {
ExpandedSubBlockStart.gif            /* -width <pixels> */
            u_int i;
            gotwidth = TRUE;
ExpandedSubBlockStart.gif            if (sscanf(optarg, "%u", &i) < 1) {
                fprintf(stderr, 
                    "%s: bad width specified: %s\n",
                     progName, optarg);
ExpandedSubBlockStart.gif            } else if (i & 1) {
                fprintf(stderr, 
                    "%s: bad width specified, must be multiple of 2: %s\n",
                     progName, optarg);
ExpandedSubBlockStart.gif            } else {
ExpandedSubBlockStart.gif                /* currently no range checking */
                frameWidth = i;
            }
            break;
        }
        case '?':
          fprintf(stderr, 
              "usage: %s <rgb-file> <yuv-file>\n%s",
              progName, usage);
          return (0);
        case 'V':
          fprintf(stderr, "%s - %s version %s\n",
              progName, MPEG4IP_PACKAGE, MPEG4IP_VERSION);
          return (0);
        default:
            fprintf(stderr, "%s: unknown option specified, ignoring: %c\n", 
                progName, c);
        }
    }

ExpandedSubBlockStart.gif    /* check that we have at least two non-option arguments */
ExpandedSubBlockStart.gif    if ((argc - optind) < 2) {
        fprintf(stderr, 
            "usage: %s <rgb-file> <yuv-file>\n%s",
            progName, usage);
        exit(1);
    }

ExpandedSubBlockStart.gif    if (gotheight == FALSE || gotwidth == FALSE) {
      fprintf(stderr, "%s - you haven't specified height or width - going with %dx%d", 
          progName, frameWidth, frameHeight);
    }
ExpandedSubBlockStart.gif    /* point to the specified file names */
    rgbFileName = argv[optind++];
    yuvFileName = argv[optind++];

ExpandedSubBlockStart.gif    /* warn about extraneous non-option arguments */
ExpandedSubBlockStart.gif    if (optind < argc) {
        fprintf(stderr, "%s: unknown options specified, ignoring: ");
ExpandedSubBlockStart.gif        while (optind < argc) {
            fprintf(stderr, "%s ", argv[optind++]);
        }
        fprintf(stderr, "\n");
    }

ExpandedSubBlockStart.gif    /* end processing of command line */

ExpandedSubBlockStart.gif    /* open the RGB file */
    rgbFile = fopen(rgbFileName, "rb");
ExpandedSubBlockStart.gif    if (rgbFile == NULL) {
        fprintf(stderr, 
            "%s: error %s: %s\n",
            progName, rgbFileName, strerror(errno));
        exit(4);
    }

ExpandedSubBlockStart.gif    /* open the RAW file */
    yuvFile = fopen(yuvFileName, "wb");
ExpandedSubBlockStart.gif    if (yuvFile == NULL) {
        fprintf(stderr,
            "%s: error opening %s: %s\n",
            progName, yuvFileName, strerror(errno));
        exit(5);
    }

ExpandedSubBlockStart.gif    /* get an input buffer for a frame */
    rgbBuf = (u_int8_t*)malloc(frameWidth * frameHeight * 3);

ExpandedSubBlockStart.gif    /* get the output buffers for a frame */
    yBuf = (u_int8_t*)malloc(frameWidth * frameHeight);
    uBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);
    vBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);

ExpandedSubBlockStart.gif    if (rgbBuf == NULL || yBuf == NULL || uBuf == NULL || vBuf == NULL) {
        fprintf(stderr,
            "%s: error allocating memory: %s\n",
            progName, strerror(errno));
        exit(6);
    }

ExpandedSubBlockStart.gif    while (fread(rgbBuf, 1, frameWidth * frameHeight * 3, rgbFile)) {

        RGB2YUV(frameWidth, frameHeight, rgbBuf, yBuf, uBuf, vBuf, flip);

        fwrite(yBuf, 1, frameWidth * frameHeight, yuvFile);
        fwrite(uBuf, 1, (frameWidth * frameHeight) / 4, yuvFile);
        fwrite(vBuf, 1, (frameWidth * frameHeight) / 4, yuvFile);

        videoFramesWritten++;
    }

    printf("%u %ux%u video frames written\n", 
        videoFramesWritten, frameWidth, frameHeight);

ExpandedSubBlockStart.gif    /* cleanup */
    fclose(rgbFile);
    fclose(yuvFile);

    return(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值