📄 colorspace.c
字号:
/*****************************************************************************
*
* XVID MPEG-4 VIDEO CODEC
* - Colorspace conversion functions -
*
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
*
* This program is free software ; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: colorspace.c,v 1.14 2006/11/10 18:58:39 chl Exp $
*
****************************************************************************/
#include <string.h> /* memcpy */
#include "../global.h"
#include "colorspace.h"
/* function pointers */
/* input */
packedFuncPtr rgb555_to_yv12;
packedFuncPtr rgb565_to_yv12;
packedFuncPtr rgb_to_yv12;
packedFuncPtr bgr_to_yv12;
packedFuncPtr bgra_to_yv12;
packedFuncPtr abgr_to_yv12;
packedFuncPtr rgba_to_yv12;
packedFuncPtr argb_to_yv12;
packedFuncPtr yuyv_to_yv12;
packedFuncPtr uyvy_to_yv12;
packedFuncPtr rgb555i_to_yv12;
packedFuncPtr rgb565i_to_yv12;
packedFuncPtr rgbi_to_yv12;
packedFuncPtr bgri_to_yv12;
packedFuncPtr bgrai_to_yv12;
packedFuncPtr abgri_to_yv12;
packedFuncPtr rgbai_to_yv12;
packedFuncPtr argbi_to_yv12;
packedFuncPtr yuyvi_to_yv12;
packedFuncPtr uyvyi_to_yv12;
/* output */
packedFuncPtr yv12_to_rgb555;
packedFuncPtr yv12_to_rgb565;
packedFuncPtr yv12_to_bgr;
packedFuncPtr yv12_to_bgra;
packedFuncPtr yv12_to_abgr;
packedFuncPtr yv12_to_rgb;
packedFuncPtr yv12_to_rgba;
packedFuncPtr yv12_to_argb;
packedFuncPtr yv12_to_yuyv;
packedFuncPtr yv12_to_uyvy;
packedFuncPtr yv12_to_rgb555i;
packedFuncPtr yv12_to_rgb565i;
packedFuncPtr yv12_to_bgri;
packedFuncPtr yv12_to_bgrai;
packedFuncPtr yv12_to_abgri;
packedFuncPtr yv12_to_rgbi;
packedFuncPtr yv12_to_rgbai;
packedFuncPtr yv12_to_argbi;
packedFuncPtr yv12_to_yuyvi;
packedFuncPtr yv12_to_uyvyi;
planarFuncPtr yv12_to_yv12;
static int32_t RGB_Y_tab[256];
static int32_t B_U_tab[256];
static int32_t G_U_tab[256];
static int32_t G_V_tab[256];
static int32_t R_V_tab[256];
/********** generic colorspace macro **********/
#define MAKE_COLORSPACE(NAME,SIZE,PIXELS,VPIXELS,FUNC,C1,C2,C3,C4) \
void \
NAME(uint8_t * x_ptr, int x_stride, \
uint8_t * y_ptr, uint8_t * u_ptr, uint8_t * v_ptr, \
int y_stride, int uv_stride, \
int width, int height, int vflip) \
{ \
int fixed_width = (width + 1) & ~1; \
int x_dif = x_stride - (SIZE)*fixed_width; \
int y_dif = y_stride - fixed_width; \
int uv_dif = uv_stride - (fixed_width / 2); \
int x, y; \
if (vflip) { \
x_ptr += (height - 1) * x_stride; \
x_dif = -(SIZE)*fixed_width - x_stride; \
x_stride = -x_stride; \
} \
for (y = 0; y < height; y+=(VPIXELS)) { \
FUNC##_ROW(SIZE,C1,C2,C3,C4); \
for (x = 0; x < fixed_width; x+=(PIXELS)) { \
FUNC(SIZE,C1,C2,C3,C4); \
x_ptr += (PIXELS)*(SIZE); \
y_ptr += (PIXELS); \
u_ptr += (PIXELS)/2; \
v_ptr += (PIXELS)/2; \
} \
x_ptr += x_dif + (VPIXELS-1)*x_stride; \
y_ptr += y_dif + (VPIXELS-1)*y_stride; \
u_ptr += uv_dif + ((VPIXELS/2)-1)*uv_stride; \
v_ptr += uv_dif + ((VPIXELS/2)-1)*uv_stride; \
} \
}
/********** colorspace input (xxx_to_yv12) functions **********/
/* rgb -> yuv def's
this following constants are "official spec"
Video Demystified" (ISBN 1-878707-09-4)
rgb<->yuv _is_ lossy, since most programs do the conversion differently
SCALEBITS/FIX taken from ffmpeg
*/
#define Y_R_IN 0.257
#define Y_G_IN 0.504
#define Y_B_IN 0.098
#define Y_ADD_IN 16
#define U_R_IN 0.148
#define U_G_IN 0.291
#define U_B_IN 0.439
#define U_ADD_IN 128
#define V_R_IN 0.439
#define V_G_IN 0.368
#define V_B_IN 0.071
#define V_ADD_IN 128
#define SCALEBITS_IN 8
#define FIX_IN(x) ((uint16_t) ((x) * (1L<<SCALEBITS_IN) + 0.5))
/* rgb16/rgb16i input */
#define MK_RGB555_B(RGB) ((RGB) << 3) & 0xf8
#define MK_RGB555_G(RGB) ((RGB) >> 2) & 0xf8
#define MK_RGB555_R(RGB) ((RGB) >> 7) & 0xf8
#define MK_RGB565_B(RGB) ((RGB) << 3) & 0xf8
#define MK_RGB565_G(RGB) ((RGB) >> 3) & 0xfc
#define MK_RGB565_R(RGB) ((RGB) >> 8) & 0xf8
#define READ_RGB16_Y(ROW, UVID, C1,C2,C3,C4) \
rgb = *(uint16_t *) (x_ptr + ((ROW)*x_stride) + 0); \
b##UVID += b = C1##_B(rgb); \
g##UVID += g = C1##_G(rgb); \
r##UVID += r = C1##_R(rgb); \
y_ptr[(ROW)*y_stride+0] = \
(uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g + \
FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN; \
rgb = *(uint16_t *) (x_ptr + ((ROW)*x_stride) + 2); \
b##UVID += b = C1##_B(rgb); \
g##UVID += g = C1##_G(rgb); \
r##UVID += r = C1##_R(rgb); \
y_ptr[(ROW)*y_stride+1] = \
(uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g + \
FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;
#define READ_RGB16_UV(UV_ROW,UVID) \
u_ptr[(UV_ROW)*uv_stride] = \
(uint8_t) ((-FIX_IN(U_R_IN) * r##UVID - FIX_IN(U_G_IN) * g##UVID + \
FIX_IN(U_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + U_ADD_IN; \
v_ptr[(UV_ROW)*uv_stride] = \
(uint8_t) ((FIX_IN(V_R_IN) * r##UVID - FIX_IN(V_G_IN) * g##UVID - \
FIX_IN(V_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + V_ADD_IN;
#define RGB16_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
/* nothing */
#define RGB16_TO_YV12(SIZE,C1,C2,C3,C4) \
uint32_t rgb, r, g, b, r0, g0, b0; \
r0 = g0 = b0 = 0; \
READ_RGB16_Y (0, 0, C1,C2,C3,C4) \
READ_RGB16_Y (1, 0, C1,C2,C3,C4) \
READ_RGB16_UV(0, 0)
#define RGB16I_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
/* nothing */
#define RGB16I_TO_YV12(SIZE,C1,C2,C3,C4) \
uint32_t rgb, r, g, b, r0, g0, b0, r1, g1, b1; \
r0 = g0 = b0 = r1 = g1 = b1 = 0; \
READ_RGB16_Y (0, 0, C1,C2,C3,C4) \
READ_RGB16_Y (1, 1, C1,C2,C3,C4) \
READ_RGB16_Y (2, 0, C1,C2,C3,C4) \
READ_RGB16_Y (3, 1, C1,C2,C3,C4) \
READ_RGB16_UV(0, 0) \
READ_RGB16_UV(1, 1)
/* rgb/rgbi input */
#define READ_RGB_Y(SIZE, ROW, UVID, C1,C2,C3,C4) \
r##UVID += r = x_ptr[(ROW)*x_stride+(C1)]; \
g##UVID += g = x_ptr[(ROW)*x_stride+(C2)]; \
b##UVID += b = x_ptr[(ROW)*x_stride+(C3)]; \
y_ptr[(ROW)*y_stride+0] = \
(uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g + \
FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN; \
r##UVID += r = x_ptr[(ROW)*x_stride+(SIZE)+(C1)]; \
g##UVID += g = x_ptr[(ROW)*x_stride+(SIZE)+(C2)]; \
b##UVID += b = x_ptr[(ROW)*x_stride+(SIZE)+(C3)]; \
y_ptr[(ROW)*y_stride+1] = \
(uint8_t) ((FIX_IN(Y_R_IN) * r + FIX_IN(Y_G_IN) * g + \
FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN;
#define READ_RGB_UV(UV_ROW,UVID) \
u_ptr[(UV_ROW)*uv_stride] = \
(uint8_t) ((-FIX_IN(U_R_IN) * r##UVID - FIX_IN(U_G_IN) * g##UVID + \
FIX_IN(U_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + U_ADD_IN; \
v_ptr[(UV_ROW)*uv_stride] = \
(uint8_t) ((FIX_IN(V_R_IN) * r##UVID - FIX_IN(V_G_IN) * g##UVID - \
FIX_IN(V_B_IN) * b##UVID) >> (SCALEBITS_IN + 2)) + V_ADD_IN;
#define RGB_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
/* nothing */
#define RGB_TO_YV12(SIZE,C1,C2,C3,C4) \
uint32_t r, g, b, r0, g0, b0; \
r0 = g0 = b0 = 0; \
READ_RGB_Y(SIZE, 0, 0, C1,C2,C3,C4) \
READ_RGB_Y(SIZE, 1, 0, C1,C2,C3,C4) \
READ_RGB_UV( 0, 0)
#define RGBI_TO_YV12_ROW(SIZE,C1,C2,C3,C4) \
/* nothing */
#define RGBI_TO_YV12(SIZE,C1,C2,C3,C4) \
uint32_t r, g, b, r0, g0, b0, r1, g1, b1; \
r0 = g0 = b0 = r1 = g1 = b1 = 0; \
READ_RGB_Y(SIZE, 0, 0, C1,C2,C3,C4) \
READ_RGB_Y(SIZE, 1, 1, C1,C2,C3,C4) \
READ_RGB_Y(SIZE, 2, 0, C1,C2,C3,C4) \
READ_RGB_Y(SIZE, 3, 1, C1,C2,C3,C4) \
READ_RGB_UV( 0, 0) \
READ_RGB_UV( 1, 1)
/* yuyv/yuyvi input */
#define READ_YUYV_Y(ROW,C1,C2,C3,C4) \
y_ptr[(ROW)*y_stride+0] = x_ptr[(ROW)*x_stride+(C1)]; \
y_ptr[(ROW)*y_stride+1] = x_ptr[(ROW)*x_stride+(C3)];
#define READ_YUYV_UV(UV_ROW,ROW1,ROW2,C1,C2,C3,C4) \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -