00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "global.h"
00016 #include "memalloc.h"
00017 #include "q_offsets.h"
00018 #include "q_around.h"
00019
00020 static const int AdaptRndCrPos[2][5] =
00021 {
00022
00023 { 4, 7, 1, 4, 1},
00024 { 10, 13, 10, 10, 10}
00025 };
00026
00027 static const int AdaptRndPos[4][5] =
00028 {
00029
00030 { 3, 6, 0, 3, 0},
00031 { 1, 2, 0, 1, 2},
00032 { 9, 12, 9, 9, 9},
00033 { 3, 4, 3, 3, 3},
00034 };
00035
00036
00037
00038
00039
00040
00041
00042
00043 void store_adaptive_rounding_4x4 (ImageParameters *p_Img, int****ARCofAdj, int mode, int block_y, int block_x)
00044 {
00045 int j;
00046
00047 for (j = block_y; j < block_y + 4; j++)
00048 memcpy(&ARCofAdj[0][DUMMY][j][block_x], &ARCofAdj[0][mode][j][block_x], BLOCK_SIZE * sizeof(int));
00049
00050 if (p_Img->P444_joined)
00051 {
00052 for (j = block_y; j < block_y + 4; j++)
00053 {
00054 memcpy(&ARCofAdj[1][DUMMY][j][block_x],&ARCofAdj[1][mode][j][block_x], BLOCK_SIZE * sizeof(int));
00055 }
00056 for (j = block_y; j < block_y + 4; j++)
00057 {
00058 memcpy(&ARCofAdj[2][DUMMY][j][block_x],&ARCofAdj[2][mode][j][block_x], BLOCK_SIZE * sizeof(int));
00059 }
00060 }
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070 void update_adaptive_rounding_4x4 (ImageParameters *p_Img, int****ARCofAdj , int mode, int block_y, int block_x)
00071 {
00072 int j;
00073
00074 for (j = block_y; j < block_y + BLOCK_SIZE; j++)
00075 memcpy (&ARCofAdj[0][mode][j][block_x],&ARCofAdj[0][DUMMY][j][block_x], BLOCK_SIZE * sizeof(int));
00076
00077 if (p_Img->P444_joined)
00078 {
00079 for (j = 0; j < block_y + BLOCK_SIZE; j++)
00080 {
00081 memcpy (&ARCofAdj[1][mode][j][block_x], &ARCofAdj[1][DUMMY][j][block_x], BLOCK_SIZE * sizeof(int));
00082 }
00083 for (j = 0; j < block_y + BLOCK_SIZE; j++)
00084 {
00085 memcpy (&ARCofAdj[2][mode][j][block_x], &ARCofAdj[2][DUMMY][j][block_x], BLOCK_SIZE * sizeof(int));
00086 }
00087 }
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 void store_adaptive_rounding_16x16 (ImageParameters *p_Img, int****ARCofAdj, int mode)
00097 {
00098 memcpy(&ARCofAdj[0][DUMMY][0][0], &ARCofAdj[0][mode][0][0], MB_PIXELS * sizeof(int));
00099 if (p_Img->P444_joined)
00100 {
00101 memcpy(&ARCofAdj[1][DUMMY][0][0], &ARCofAdj[1][mode][0][0], MB_PIXELS * sizeof(int));
00102 memcpy(&ARCofAdj[2][DUMMY][0][0], &ARCofAdj[2][mode][0][0], MB_PIXELS * sizeof(int));
00103 }
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113 void update_adaptive_rounding_16x16(ImageParameters *p_Img, int****ARCofAdj , int mode)
00114 {
00115 memcpy (&ARCofAdj[0][mode][0][0],&ARCofAdj[0][DUMMY][0][0], MB_PIXELS * sizeof(int));
00116 if (p_Img->P444_joined)
00117 {
00118 memcpy (&ARCofAdj[1][mode][0][0], &ARCofAdj[1][DUMMY][0][0], MB_PIXELS * sizeof(int));
00119 memcpy (&ARCofAdj[2][mode][0][0], &ARCofAdj[2][DUMMY][0][0], MB_PIXELS * sizeof(int));
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 void update_offset_params(Macroblock *currMB, int mode, Boolean luma_transform_size_8x8_flag)
00133 {
00134 ImageParameters *p_Img = currMB->p_Img;
00135 InputParameters *p_Inp = currMB->p_Inp;
00136 int is_inter = (mode != I4MB)&&(mode != I16MB) && (mode != I8MB);
00137 int luma_pos = AdaptRndPos[(is_inter<<1) + luma_transform_size_8x8_flag][p_Img->type];
00138 int i,j;
00139 int qp = currMB->qp + p_Img->bitdepth_luma_qp_scale;
00140 int cur_qp = p_Inp->AdaptRoundingFixed ? 0 : qp;
00141 int temp = 0;
00142 QuantParameters *p_Quant = p_Img->p_Quant;
00143 int offsetRange = 1 << (OffsetBits - 1);
00144 int blk_mask = 0x03 + (luma_transform_size_8x8_flag<<2);
00145 int blk_shift = 2 + luma_transform_size_8x8_flag;
00146 short **offsetList = luma_transform_size_8x8_flag ? p_Quant->OffsetList8x8[cur_qp] : p_Quant->OffsetList4x4[cur_qp];
00147
00148 int **fAdjust = luma_transform_size_8x8_flag ? p_Img->ARCofAdj8x8[0][mode] : p_Img->ARCofAdj4x4[0][mode];
00149
00150 if (mode == IPCM) return;
00151
00152 if( (p_Img->active_sps->chroma_format_idc == YUV444) && IS_INDEPENDENT(p_Inp) )
00153 {
00154 if( luma_transform_size_8x8_flag )
00155 luma_pos += 5 * p_Img->colour_plane_id;
00156 else
00157 luma_pos += p_Img->colour_plane_id;
00158 }
00159
00160 for (j=0; j < MB_BLOCK_SIZE; j++)
00161 {
00162 int j_pos = ((j & blk_mask)<<blk_shift);
00163 for (i=0; i < MB_BLOCK_SIZE; i++)
00164 {
00165 temp = j_pos + (i & blk_mask);
00166 offsetList[luma_pos][temp] = offsetList[luma_pos][temp] + (short) fAdjust[j][i];
00167 offsetList[luma_pos][temp] = (short) iClip3(0, offsetRange, offsetList[luma_pos][temp]);
00168 }
00169 }
00170
00171 if(p_Img->P444_joined)
00172 {
00173 int **fAdjustCbCr;
00174 int uv;
00175
00176 for(uv = 0; uv < 2; uv++)
00177 {
00178 luma_pos = AdaptRndPos[(is_inter<<1) + luma_transform_size_8x8_flag][p_Img->type];
00179 fAdjustCbCr = luma_transform_size_8x8_flag ? p_Img->ARCofAdj8x8[uv + 1][mode] : p_Img->ARCofAdj4x4[uv + 1][mode];
00180 if(luma_transform_size_8x8_flag )
00181 luma_pos += 5 * (uv+1);
00182 else
00183 luma_pos += (uv+1);
00184 for (j=0; j < MB_BLOCK_SIZE; j++)
00185 {
00186 int j_pos = ((j & blk_mask)<<blk_shift);
00187 for (i=0; i < MB_BLOCK_SIZE; i++)
00188 {
00189 temp = j_pos + (i & blk_mask);
00190 offsetList[luma_pos][temp] = offsetList[luma_pos][temp] + (short) fAdjustCbCr[j][i];
00191 offsetList[luma_pos][temp] = (short) iClip3(0,offsetRange,offsetList[luma_pos][temp]);
00192 }
00193 }
00194 }
00195 }
00196
00197 if ((p_Inp->AdaptRndChroma) && (p_Img->yuv_format == YUV420 || p_Img->yuv_format == YUV422 ))
00198 {
00199 int temp_offset;
00200 int u_pos = AdaptRndCrPos[is_inter][p_Img->type];
00201 int v_pos = u_pos + 1;
00202 int jpos;
00203
00204
00205
00206 int **fAdjustCb = (luma_transform_size_8x8_flag && mode == P8x8 )? p_Img->ARCofAdj4x4[1][4] : p_Img->ARCofAdj4x4[1][mode];
00207 int **fAdjustCr = (luma_transform_size_8x8_flag && mode == P8x8 )? p_Img->ARCofAdj4x4[2][4] : p_Img->ARCofAdj4x4[2][mode];
00208
00209
00210 for (j = 0; j < p_Img->mb_cr_size_y; j++)
00211 {
00212 jpos = ((j & 0x03)<<2);
00213 for (i = 0; i < p_Img->mb_cr_size_x; i++)
00214 {
00215 temp = jpos + (i & 0x03);
00216 temp_offset = p_Quant->OffsetList4x4[cur_qp][u_pos][temp] + (short) fAdjustCb[j][i];
00217 p_Quant->OffsetList4x4[cur_qp][u_pos][temp] = (short) iClip3(0, offsetRange, temp_offset);
00218 temp_offset = p_Quant->OffsetList4x4[cur_qp][v_pos][temp] + (short) fAdjustCr[j][i];
00219 p_Quant->OffsetList4x4[cur_qp][v_pos][temp] = (short) iClip3(0, offsetRange, temp_offset);
00220 }
00221 }
00222 }
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232 void reset_adaptive_rounding(ImageParameters *p_Img)
00233 {
00234 int maxplane;
00235 maxplane = (p_Img->yuv_format == YUV400)? 1 : 3;
00236 memset(&p_Img->ARCofAdj4x4[0][0][0][0], 0, maxplane * MAXMODE * MB_PIXELS * sizeof (int));
00237 maxplane = (p_Img->yuv_format == YUV400)? 1 : (p_Img->P444_joined ? 3 : 1);
00238 memset(&p_Img->ARCofAdj8x8[0][0][0][0], 0, maxplane * MAXMODE * MB_PIXELS * sizeof (int));
00239 }
00240
00241
00242
00243
00244
00245
00246
00247 void reset_adaptive_rounding_direct(ImageParameters *p_Img)
00248 {
00249 int maxplane, pl;
00250 maxplane = (p_Img->yuv_format == YUV400)? 1 : 3;
00251 for (pl = 0; pl < maxplane; pl++)
00252 memset(&p_Img->ARCofAdj4x4[pl][DUMMY][0][0], 0, MB_PIXELS * sizeof (int));
00253
00254 maxplane = (p_Img->yuv_format == YUV400)? 1 : (p_Img->P444_joined ? 3 : 1);
00255 for (pl = 0; pl < maxplane; pl++)
00256 memset(&p_Img->ARCofAdj8x8[pl][DUMMY][0][0], 0, MB_PIXELS * sizeof (int));
00257
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 void update_adaptive_rounding_8x8(ImageParameters *p_Img, InputParameters *p_Inp, RD_8x8DATA* dataTr, int**** ARCofAdj)
00267 {
00268 int b8, pl, plane, j0, i0, j;
00269
00270 if (p_Inp->AdaptiveRounding)
00271 {
00272 plane = (p_Img->P444_joined) ? 3 : 1;
00273 for (pl = 0; pl < plane; pl++)
00274 {
00275 for (b8 = 0; b8 < 4; b8++)
00276 {
00277 j0 = 8*(b8 >> 1);
00278 i0 = 8*(b8 & 0x01);
00279 if (dataTr->part[b8].mode > 0)
00280 {
00281 for (j = j0; j < j0 + BLOCK_SIZE_8x8; j++)
00282 {
00283 memcpy(&ARCofAdj[pl][P8x8][j][i0], &ARCofAdj[pl][(short) dataTr->part[b8].mode][j][i0], BLOCK_SIZE_8x8 * sizeof(int));
00284 }
00285 }
00286 }
00287 }
00288 }
00289 }
00290
00291