--- colorspace.c 2002/11/07 10:28:15 1.3.2.2 +++ colorspace.c 2006/11/10 18:58:39 1.14 @@ -1,60 +1,55 @@ -/************************************************************************** +/***************************************************************************** * - * XVID MPEG-4 VIDEO CODEC - * colorspace conversions + * XVID MPEG-4 VIDEO CODEC + * - Colorspace conversion functions - * - * 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. + * Copyright(C) 2001-2003 Peter Ross * - * 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. + * 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. * - * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * 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. * - *************************************************************************/ - -/************************************************************************** - * - * History: + * 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 * - * 14.04.2002 added user_to_yuv_c() - * 30.02.2002 out_yuv dst_stride2 fix - * 26.02.2002 rgb555, rgb565 - * 24.11.2001 accuracy improvement to yuyv/vyuy conversion - * 28.10.2001 total rewrite + * $Id: colorspace.c,v 1.14 2006/11/10 18:58:39 chl Exp $ * - **************************************************************************/ + ****************************************************************************/ -#include // memcpy +#include /* memcpy */ +#include "../global.h" #include "colorspace.h" -#include "../divx4.h" // DEC_PICTURE -// function pointers +/* 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 yuv_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; @@ -64,8 +59,9 @@ packedFuncPtr yv12_to_bgr; packedFuncPtr yv12_to_bgra; packedFuncPtr yv12_to_abgr; +packedFuncPtr yv12_to_rgb; packedFuncPtr yv12_to_rgba; -packedFuncPtr yv12_to_yuv; +packedFuncPtr yv12_to_argb; packedFuncPtr yv12_to_yuyv; packedFuncPtr yv12_to_uyvy; @@ -74,23 +70,22 @@ 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; -int32_t RGB_Y_tab[256]; -int32_t B_U_tab[256]; -int32_t G_U_tab[256]; -int32_t G_V_tab[256]; -int32_t R_V_tab[256]; +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]; -#define MIN(A,B) ((A)<(B)?(A):(B)) -#define MAX(A,B) ((A)>(B)?(A):(B)) - /********** generic colorspace macro **********/ @@ -138,7 +133,7 @@ Video Demystified" (ISBN 1-878707-09-4) rgb<->yuv _is_ lossy, since most programs do the conversion differently - + SCALEBITS/FIX taken from ffmpeg */ @@ -233,7 +228,7 @@ 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; + FIX_IN(Y_B_IN) * b) >> SCALEBITS_IN) + Y_ADD_IN; #define READ_RGB_UV(UV_ROW,UVID) \ u_ptr[(UV_ROW)*uv_stride] = \ @@ -297,8 +292,10 @@ MAKE_COLORSPACE(rgb565_to_yv12_c, 2,2,2, RGB16_TO_YV12, MK_RGB565, 0,0,0) MAKE_COLORSPACE(bgr_to_yv12_c, 3,2,2, RGB_TO_YV12, 2,1,0, 0) MAKE_COLORSPACE(bgra_to_yv12_c, 4,2,2, RGB_TO_YV12, 2,1,0, 0) +MAKE_COLORSPACE(rgb_to_yv12_c, 3,2,2, RGB_TO_YV12, 0,1,2, 0) MAKE_COLORSPACE(abgr_to_yv12_c, 4,2,2, RGB_TO_YV12, 3,2,1, 0) MAKE_COLORSPACE(rgba_to_yv12_c, 4,2,2, RGB_TO_YV12, 0,1,2, 0) +MAKE_COLORSPACE(argb_to_yv12_c, 4,2,2, RGB_TO_YV12, 1,2,3, 0) MAKE_COLORSPACE(yuyv_to_yv12_c, 2,2,2, YUYV_TO_YV12, 0,1,2,3) MAKE_COLORSPACE(uyvy_to_yv12_c, 2,2,2, YUYV_TO_YV12, 1,0,3,2) @@ -307,7 +304,9 @@ MAKE_COLORSPACE(bgri_to_yv12_c, 3,2,4, RGBI_TO_YV12, 2,1,0, 0) MAKE_COLORSPACE(bgrai_to_yv12_c, 4,2,4, RGBI_TO_YV12, 2,1,0, 0) MAKE_COLORSPACE(abgri_to_yv12_c, 4,2,4, RGBI_TO_YV12, 3,2,1, 0) +MAKE_COLORSPACE(rgbi_to_yv12_c, 3,2,4, RGBI_TO_YV12, 0,1,2, 0) MAKE_COLORSPACE(rgbai_to_yv12_c, 4,2,4, RGBI_TO_YV12, 0,1,2, 0) +MAKE_COLORSPACE(argbi_to_yv12_c, 4,2,4, RGBI_TO_YV12, 1,2,3, 0) MAKE_COLORSPACE(yuyvi_to_yv12_c, 2,2,4, YUYVI_TO_YV12, 0,1,2,3) MAKE_COLORSPACE(uyvyi_to_yv12_c, 2,2,4, YUYVI_TO_YV12, 1,0,3,2) @@ -344,12 +343,12 @@ ((MAX(0,MIN(255, B)) >> 3) & 0x001f) #define WRITE_RGB16(ROW,UV_ROW,C1) \ - rgb_y = RGB_Y_tab[ y_ptr[y_stride + 0] ]; \ + rgb_y = RGB_Y_tab[ y_ptr[y_stride*(ROW) + 0] ]; \ b[ROW] = (b[ROW] & 0x7) + ((rgb_y + b_u##UV_ROW) >> SCALEBITS_OUT); \ g[ROW] = (g[ROW] & 0x7) + ((rgb_y - g_uv##UV_ROW) >> SCALEBITS_OUT); \ r[ROW] = (r[ROW] & 0x7) + ((rgb_y + r_v##UV_ROW) >> SCALEBITS_OUT); \ *(uint16_t *) (x_ptr+((ROW)*x_stride)+0) = C1(r[ROW], g[ROW], b[ROW]); \ - rgb_y = RGB_Y_tab[ y_ptr[y_stride + 1] ]; \ + rgb_y = RGB_Y_tab[ y_ptr[y_stride*(ROW) + 1] ]; \ b[ROW] = (b[ROW] & 0x7) + ((rgb_y + b_u##UV_ROW) >> SCALEBITS_OUT); \ g[ROW] = (g[ROW] & 0x7) + ((rgb_y - g_uv##UV_ROW) >> SCALEBITS_OUT); \ r[ROW] = (r[ROW] & 0x7) + ((rgb_y + r_v##UV_ROW) >> SCALEBITS_OUT); \ @@ -383,7 +382,7 @@ WRITE_RGB16(1, 1, C1) \ WRITE_RGB16(2, 0, C1) \ WRITE_RGB16(3, 1, C1) \ - + /* rgb/rgbi output */ @@ -435,7 +434,7 @@ #define YV12_TO_YUYV_ROW(SIZE,C1,C2,C3,C4) /* nothing */ #define YV12_TO_YUYV(SIZE,C1,C2,C3,C4) \ WRITE_YUYV(0, 0, C1,C2,C3,C4) \ - WRITE_YUYV(1, 0, C1,C2,C3,C4) + WRITE_YUYV(1, 0, C1,C2,C3,C4) #define YV12_TO_YUYVI_ROW(SIZE,C1,C2,C3,C4) /* nothing */ #define YV12_TO_YUYVI(SIZE,C1,C2,C3,C4) \ @@ -447,10 +446,12 @@ MAKE_COLORSPACE(yv12_to_rgb555_c, 2,2,2, YV12_TO_RGB16, MK_RGB555, 0,0,0) MAKE_COLORSPACE(yv12_to_rgb565_c, 2,2,2, YV12_TO_RGB16, MK_RGB565, 0,0,0) -MAKE_COLORSPACE(yv12_to_bgr_c, 3,2,2, YV12_TO_RGB, 2,1,0, 0) +MAKE_COLORSPACE(yv12_to_bgr_c, 3,2,2, YV12_TO_RGB, 2,1,0,0) MAKE_COLORSPACE(yv12_to_bgra_c, 4,2,2, YV12_TO_RGB, 2,1,0,3) MAKE_COLORSPACE(yv12_to_abgr_c, 4,2,2, YV12_TO_RGB, 3,2,1,0) +MAKE_COLORSPACE(yv12_to_rgb_c, 3,2,2, YV12_TO_RGB, 0,1,2,0) MAKE_COLORSPACE(yv12_to_rgba_c, 4,2,2, YV12_TO_RGB, 0,1,2,3) +MAKE_COLORSPACE(yv12_to_argb_c, 4,2,2, YV12_TO_RGB, 1,2,3,0) MAKE_COLORSPACE(yv12_to_yuyv_c, 2,2,2, YV12_TO_YUYV, 0,1,2,3) MAKE_COLORSPACE(yv12_to_uyvy_c, 2,2,2, YV12_TO_YUYV, 1,0,3,2) @@ -459,7 +460,9 @@ MAKE_COLORSPACE(yv12_to_bgri_c, 3,2,4, YV12_TO_RGBI, 2,1,0, 0) MAKE_COLORSPACE(yv12_to_bgrai_c, 4,2,4, YV12_TO_RGBI, 2,1,0,3) MAKE_COLORSPACE(yv12_to_abgri_c, 4,2,4, YV12_TO_RGBI, 3,2,1,0) +MAKE_COLORSPACE(yv12_to_rgbi_c, 3,2,4, YV12_TO_RGBI, 0,1,2,0) MAKE_COLORSPACE(yv12_to_rgbai_c, 4,2,4, YV12_TO_RGBI, 0,1,2,3) +MAKE_COLORSPACE(yv12_to_argbi_c, 4,2,4, YV12_TO_RGBI, 1,2,3,0) MAKE_COLORSPACE(yv12_to_yuyvi_c, 2,2,4, YV12_TO_YUYVI, 0,1,2,3) MAKE_COLORSPACE(yv12_to_uyvyi_c, 2,2,4, YV12_TO_YUYVI, 1,0,3,2) @@ -468,20 +471,23 @@ /* yv12 to yv12 copy function */ void -yv12_to_yv12_c(uint8_t * y_dst, uint8_t * u_dst, uint8_t * v_dst, +yv12_to_yv12_c(uint8_t * y_dst, uint8_t * u_dst, uint8_t * v_dst, int y_dst_stride, int uv_dst_stride, - uint8_t * y_src, uint8_t * u_src, uint8_t * v_src, + uint8_t * y_src, uint8_t * u_src, uint8_t * v_src, int y_src_stride, int uv_src_stride, int width, int height, int vflip) { int width2 = width / 2; int height2 = height / 2; int y; + const int with_uv = (u_src!=0 && v_src!=0); if (vflip) { y_src += (height - 1) * y_src_stride; - u_src += (height2 - 1) * uv_src_stride; - v_src += (height2 - 1) * uv_src_stride; + if (with_uv) { + u_src += (height2 - 1) * uv_src_stride; + v_src += (height2 - 1) * uv_src_stride; + } y_src_stride = -y_src_stride; uv_src_stride = -uv_src_stride; } @@ -492,16 +498,23 @@ y_dst += y_dst_stride; } - for (y = height2; y; y--) { - memcpy(u_dst, u_src, width2); - u_src += uv_src_stride; - u_dst += uv_dst_stride; + if (with_uv) { + for (y = height2; y; y--) { + memcpy(u_dst, u_src, width2); + memcpy(v_dst, v_src, width2); + u_src += uv_src_stride; + u_dst += uv_dst_stride; + v_src += uv_src_stride; + v_dst += uv_dst_stride; + } } - - for (y = height2; y; y--) { - memcpy(v_dst, v_src, width2); - v_src += uv_src_stride; - v_dst += uv_dst_stride; + else { + for (y = height2; y; y--) { + memset(u_dst, 0x80, width2); + memset(v_dst, 0x80, width2); + u_dst += uv_dst_stride; + v_dst += uv_dst_stride; + } } }