📄 yuv2rgb.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS and its licensors.** All rights reserved.**** This file is part of the Qtopia Environment.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.** See below for additional copyright and license information**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************//* * yuv2rgb.c * * This file is part of xine, a unix video player. * * based on work from mpeg2dec: * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> * * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. * * mpeg2dec 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. * * mpeg2dec 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 c_this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: yuv2rgb.c,v 1.3 2002/09/02 17:18:30 harlekin Exp $ */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <inttypes.h>#include "yuv2rgb.h"static scale_line_func_t find_scale_line_func(int step);const int32_t Inverse_Table_6_9[8][4] = { {117504, 138453, 13954, 34903}, /* no sequence_display_extension */ {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */ {104597, 132201, 25675, 53279}, /* unspecified */ {104597, 132201, 25675, 53279}, /* reserved */ {104448, 132798, 24759, 53109}, /* FCC */ {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */ {104597, 132201, 25675, 53279}, /* SMPTE 170M */ {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */};static void *my_malloc_aligned (size_t alignment, size_t size, void **chunk){ char *pMem = (char *)malloc(size+alignment); *chunk = pMem; while ((int) pMem % alignment) pMem++; return pMem;}/* fourcc: bpp: IEEE: plane sizes: (w=width h=height of original image) 444P 24 YUV 4:4:4 Y: w * h U,V: w * h YUY2,UYVY 16 YUV 4:2:2 Y: w * h U,V: (w/2) * h [MJPEG] YV12,I420 12 YUV 4:2:0 Y: w * h U,V: (w/2) * (h/2) [MPEG, h263] 411P 12 YUV 4:1:1 Y: w * h U,V: (w/4) * h [DV-NTSC, CYUV] YVU9,IF09 9 YUV 4:1:0 Y: w * h U,V: (w/4) * (h/4) [Sorenson, Indeo]*/static int yuv2rgb_configure (yuv2rgb_t *c_this, int source_width, int source_height, int y_stride, int uv_stride, int dest_width, int dest_height, int rgb_stride, int format){ c_this->source_width = source_width; c_this->source_height = source_height; c_this->y_stride = y_stride; c_this->uv_stride = uv_stride; c_this->dest_width = dest_width; c_this->dest_height = dest_height; c_this->rgb_stride = rgb_stride;printf("yuv2rgb config - w: %d h: %d\n", source_width, source_height); switch ( format ) { case FORMAT_YUV444: c_this->uv_stretch_x = 0; c_this->uv_stretch_y = 0; break; case FORMAT_YUV422: c_this->uv_stretch_x = 1; c_this->uv_stretch_y = 0; break; case FORMAT_YUV420: c_this->uv_stretch_x = 1; c_this->uv_stretch_y = 1; break; case FORMAT_YUV411: c_this->uv_stretch_x = 2; c_this->uv_stretch_y = 0; break; case FORMAT_YUV410: c_this->uv_stretch_x = 2; c_this->uv_stretch_y = 2; break; } if (c_this->y_chunk) { free (c_this->y_chunk); c_this->y_chunk = 0; c_this->y_buffer = (uint8_t *)c_this->y_chunk; } if (c_this->u_chunk) { free (c_this->u_chunk); c_this->u_chunk = 0; c_this->u_buffer = (uint8_t *)c_this->u_chunk; } if (c_this->v_chunk) { free (c_this->v_chunk); c_this->v_chunk = 0; c_this->v_buffer = (uint8_t *)c_this->v_chunk; } c_this->step_dx = source_width * 32768 / dest_width; c_this->step_dy = source_height * 32768 / dest_height; c_this->scale_line = find_scale_line_func(c_this->step_dx); if ((source_width == dest_width) && (source_height == dest_height) && (format == FORMAT_YUV420) ) { c_this->do_scale = 0; /* * space for two y-lines (for yuv2rgb_mlib) * u,v subsampled 2:1 */ c_this->y_buffer = (uint8_t *)my_malloc_aligned (16, 2*dest_width, &c_this->y_chunk); if (!c_this->y_buffer) return 0; c_this->u_buffer = (uint8_t *)my_malloc_aligned (16, (dest_width+1)/2, &c_this->u_chunk); if (!c_this->u_buffer) return 0; c_this->v_buffer = (uint8_t *)my_malloc_aligned (16, (dest_width+1)/2, &c_this->v_chunk); if (!c_this->v_buffer) return 0; } else { c_this->do_scale = 1; /* * space for two y-lines (for yuv2rgb_mlib) * u,v subsampled 2:1 */ c_this->y_buffer = (uint8_t *)my_malloc_aligned (16, 2*dest_width, &c_this->y_chunk); if (!c_this->y_buffer) return 0; c_this->u_buffer = (uint8_t *)my_malloc_aligned (16, (dest_width+1)/2, &c_this->u_chunk); if (!c_this->u_buffer) return 0; c_this->v_buffer = (uint8_t *)my_malloc_aligned (16, (dest_width+1)/2, &c_this->v_chunk); if (!c_this->v_buffer) return 0; } return 1;}static void scale_line_gen (uint8_t *source, uint8_t *dest, int width, int step){ /* * scales a yuv source row to a dest row, with interpolation * (good quality, but slow) */ int p1; int p2; int dx; p1 = *source++; p2 = *source++; dx = 0; /* * the following code has been optimized by Scott Smith <ssmith@akamai.com>: * * ok now I have a meaningful optimization for yuv2rgb.c:scale_line_gen. * it removes the loop from within the while() loop by separating it out * into 3 cases: where you are enlarging the line (<32768), where you are * between 50% and 100% of the original line (<=65536), and where you are * shrinking it by a lot. anyways, I went from 200 delivered / 100+ * skipped to 200 delivered / 80 skipped for the enlarging case. I * noticed when looking at the assembly that the compiler was able to * unroll these while(width) loops, whereas before it was trying to * unroll the while(dx>32768) loops. so the compiler is better able to * deal with this code. */ if (step < 32768) { while (width) { *dest = p1 + (((p2-p1) * dx)>>15); dx += step; if (dx > 32768) { dx -= 32768; p1 = p2; p2 = *source++; } dest ++; width --; } } else if (step <= 65536) { while (width) { *dest = p1 + (((p2-p1) * dx)>>15); dx += step; if (dx > 65536) { dx -= 65536; p1 = *source++; p2 = *source++; } else { dx -= 32768; p1 = p2; p2 = *source++; } dest ++; width --; } } else { while (width) { int offs; *dest = p1 + (((p2 - p1) * dx) >> 15); dx += step; offs=((dx-1)>>15); dx-=offs<<15; source+=offs-2; p1 = *source++; dest++; p2 = *source++; width--; } }}/* * Interpolates 8 output pixels from 5 source pixels using shifts. * Useful for scaling a PAL svcd input source to 4:3 display format. */static void scale_line_5_8 (uint8_t *source, uint8_t *dest, int width, int ){ int p1, p2; while ((width -= 8) >= 0) { p1 = source[0]; p2 = source[1]; dest[0] = p1; dest[1] = (3*p1 + 5*p2) >> 3; p1 = source[2]; dest[2] = (3*p2 + 1*p1) >> 2; dest[3] = (1*p2 + 7*p1) >> 3; p2 = source[3]; dest[4] = (1*p1 + 1*p2) >> 1; p1 = source[4]; dest[5] = (7*p2 + 1*p1) >> 3; dest[6] = (1*p2 + 3*p1) >> 2; p2 = source[5]; dest[7] = (5*p1 + 3*p2) >> 3; source += 5; dest += 8; } if ((width += 8) <= 0) return; *dest++ = source[0]; if (--width <= 0) return; *dest++ = (3*source[0] + 5*source[1]) >> 3; if (--width <= 0) return; *dest++ = (3*source[1] + 1*source[2]) >> 2; if (--width <= 0) return; *dest++ = (1*source[1] + 7*source[2]) >> 3; if (--width <= 0) return; *dest++ = (1*source[2] + 1*source[3]) >> 1; if (--width <= 0) return; *dest++ = (7*source[3] + 1*source[4]) >> 3; if (--width <= 0) return; *dest++ = (1*source[3] + 3*source[4]) >> 2;}/* * Interpolates 4 output pixels from 3 source pixels using shifts. * Useful for scaling a NTSC svcd input source to 4:3 display format. */static void scale_line_3_4 (uint8_t *source, uint8_t *dest, int width, int ){ int p1, p2; while ((width -= 4) >= 0) { p1 = source[0]; p2 = source[1]; dest[0] = p1; dest[1] = (1*p1 + 3*p2) >> 2; p1 = source[2]; dest[2] = (1*p2 + 1*p1) >> 1; p2 = source[3]; dest[3] = (3*p1 + 1*p2) >> 2; source += 3; dest += 4; } if ((width += 4) <= 0) return; *dest++ = source[0]; if (--width <= 0) return; *dest++ = (1*source[0] + 3*source[1]) >> 2; if (--width <= 0) return; *dest++ = (1*source[1] + 1*source[2]) >> 1;}/* Interpolate 2 output pixels from one source pixel. */static void scale_line_1_2 (uint8_t *source, uint8_t *dest, int width, int ){ int p1, p2; p1 = *source; while ((width -= 4) >= 0) { *dest++ = p1; p2 = *++source; *dest++ = (p1 + p2) >> 1; *dest++ = p2; p1 = *++source; *dest++ = (p2 + p1) >> 1; } if ((width += 4) <= 0) return; *dest++ = source[0]; if (--width <= 0) return; *dest++ = (source[0] + source[1]) >> 1; if (--width <= 0) return; *dest++ = source[1];} /* * Scale line with no horizontal scaling. For NTSC mpeg2 dvd input in * 4:3 output format (720x480 -> 720x540) */static void scale_line_1_1 (uint8_t *source, uint8_t *dest, int width, int ){ memcpy(dest, source, width);} static scale_line_func_t find_scale_line_func(int step){ static struct { int src_step; int dest_step; scale_line_func_t func; char *desc; } scale_line[] = { { 5, 8, scale_line_5_8, "svcd 4:3(pal)" }, // No tr { 3, 4, scale_line_3_4, "svcd 4:3(ntsc)" }, { 1, 2, scale_line_1_2, "2*zoom" }, // No tr { 1, 1, scale_line_1_1, "non-scaled" }, }; unsigned int i; for (i = 0; i < sizeof(scale_line)/sizeof(scale_line[0]); i++) { if (step == scale_line[i].src_step*32768/scale_line[i].dest_step) { printf("yuv2rgb: using %s optimized scale_line\n", scale_line[i].desc); return scale_line[i].func; } } printf("yuv2rgb: using generic scale_line with interpolation\n"); return scale_line_gen;}#define RGB(i,type) \ U = pu[i]; \ V = pv[i]; \ r = (type *) c_this->table_rV[V]; \ g = (type *) (((uint8_t *)c_this->table_gU[U]) + c_this->table_gV[V]); \ b = (type *) c_this->table_bU[U];#define RGB16(i) RGB(i,uint16_t)#define RGB32(i) RGB(i,uint32_t)#define _DST(i,dst,x) \ Y = py_##dst[2*i+x]; \ dst_##dst[2*i+x] = r[Y] + g[Y] + b[Y];#define DST(i,dst) \ _DST(i,dst,0) \ _DST(i,dst,1) \#define DST1(i) DST(i,1)#define DST2(i) DST(i,2)static void yuv2rgb_c_32 (yuv2rgb_t *c_this, uint8_t * _dst, uint8_t * _py, uint8_t * _pu, uint8_t * _pv){ int U, V, Y; uint8_t * py_1, * py_2, * pu, * pv; uint32_t * r, * g, * b; uint32_t * dst_1, * dst_2; int width, height, dst_height; int dy; if (c_this->do_scale) { scale_line_func_t scale_line = c_this->scale_line; scale_line (_pu, c_this->u_buffer, c_this->dest_width >> c_this->uv_stretch_x, c_this->step_dx); scale_line (_pv, c_this->v_buffer, c_this->dest_width >> c_this->uv_stretch_x, c_this->step_dx); scale_line (_py, c_this->y_buffer, c_this->dest_width, c_this->step_dx); dy = 0; dst_height = c_this->dest_height; for (height = 0;; ) { dst_1 = (uint32_t*)_dst; py_1 = c_this->y_buffer; pu = c_this->u_buffer; pv = c_this->v_buffer; width = c_this->dest_width >> 3; do { RGB32(0); DST1(0); RGB32(1); DST1(1); RGB32(2); DST1(2); RGB32(3); DST1(3); pu += 4; pv += 4; py_1 += 8; dst_1 += 8; } while (--width);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -