00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "contributors.h"
00015 #include "global.h"
00016 #include "memalloc.h"
00017 #include "img_distortion.h"
00018
00019 #define YUV2RGB_YOFFSET
00020
00021
00022 #ifdef YUV2RGB_YOFFSET
00023 #define OFFSET_Y 16
00024 static const float K0 = 1.164f;
00025 static const float K1 = 1.596f;
00026 static const float K2 = 0.391f;
00027 static const float K3 = 0.813f;
00028 static const float K4 = 2.018f;
00029 static int offset_y, offset_cr;
00030 #else
00031 static const float K0 = 1.000f;
00032 static const float K1 = 1.402f;
00033 static const float K2 = 0.34414f;
00034 static const float K3 = 0.71414f;
00035 static const float K4 = 1.772f;
00036 #endif
00037 static int wk0, wk1, wk2, wk3, wk4;
00038
00039 ImageStructure imgRGB_src, imgRGB_ref;
00040
00041 int create_RGB_memory(ImageParameters *img)
00042 {
00043 int memory_size = 0;
00044 int j;
00045 for( j = 0; j < 3; j++ )
00046 {
00047 memory_size += get_mem2Dpel (&imgRGB_src.data[j], img->height, img->width);
00048 }
00049 for( j = 0; j < 3; j++ )
00050 {
00051 memory_size += get_mem2Dpel (&imgRGB_ref.data[j], img->height, img->width);
00052 }
00053
00054 return memory_size;
00055 }
00056
00057 void delete_RGB_memory(void)
00058 {
00059 int i;
00060 for( i = 0; i < 3; i++ )
00061 {
00062 free_mem2Dpel(imgRGB_src.data[i]);
00063 }
00064 for( i = 0; i < 3; i++ )
00065 {
00066 free_mem2Dpel(imgRGB_ref.data[i]);
00067 }
00068 }
00069
00070 void init_YUVtoRGB(void)
00071 {
00072 float conv_scale = (float) (65536.0f);
00073
00074 wk0 = float2int( conv_scale * K0);
00075 wk1 = float2int( conv_scale * K1);
00076 wk2 = float2int( -conv_scale * K2);
00077 wk3 = float2int( -conv_scale * K3);
00078 wk4 = float2int( conv_scale * K4);
00079
00080 #ifdef YUV2RGB_YOFFSET
00081 offset_y = OFFSET_Y << (params->output.bit_depth[0] - 8);
00082 offset_cr = 1 << (params->output.bit_depth[0] - 1);
00083 #endif
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 void YUVtoRGB(ImageStructure *YUV, ImageStructure *RGB)
00096 {
00097 static int i, j, j_cr, i_cr;
00098 static int sy, su, sv;
00099 static int wbuv, wguv, wruv;
00100 static imgpel *Y, *U, *V, *R, *G, *B;
00101 FrameFormat format = YUV->format;
00102 int width = format.width;
00103 int height = format.height;
00104 int max_value = format.max_value[0];
00105
00106
00107 for (j = 0; j < height; j++)
00108 {
00109 j_cr = j >> shift_cr_y;
00110 Y = YUV->data[0][j];
00111 U = YUV->data[1][j_cr];
00112 V = YUV->data[2][j_cr];
00113 R = RGB->data[0][j];
00114 G = RGB->data[1][j];
00115 B = RGB->data[2][j];
00116
00117 for (i = 0; i < width; i++)
00118 {
00119 i_cr = i >> shift_cr_x;
00120
00121 su = U[i_cr] - offset_cr;
00122 sv = V[i_cr] - offset_cr;
00123
00124 wruv = wk1 * sv;
00125 wguv = wk2 * su + wk3 * sv;
00126 wbuv = wk4 * su;
00127
00128 #ifdef YUV2RGB_YOFFSET // Y offset value of 16 is considered
00129 sy = wk0 * (Y[i] - offset_y);
00130 #else
00131 sy = wk0 * Y[i];
00132 #endif
00133
00134 R[i] = iClip1( max_value, rshift_rnd(sy + wruv, 16));
00135 G[i] = iClip1( max_value, rshift_rnd(sy + wguv, 16));
00136 B[i] = iClip1( max_value, rshift_rnd(sy + wbuv, 16));
00137 }
00138 }
00139
00140 RGB->format = format;
00141 RGB->format.yuv_format = YUV444;
00142 RGB->format.color_model = CM_RGB;
00143 RGB->format.height_cr = format.height;
00144 RGB->format.width_cr = format.width;
00145 for (i = 1; i < 3; i++)
00146 {
00147 RGB->format.size_cmp[i] = format.size_cmp[0];
00148 RGB->format.bit_depth[i] = format.bit_depth[0];
00149 RGB->format.max_value[i] = max_value;
00150 RGB->format.max_value_sq[i] = format.max_value_sq[0];
00151 }
00152 }