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