1
2 /*!
3 *************************************************************************************
4 * \file block.c
5 *
6 * \brief
7 * Process one block
8 *
9 * \author
10 * Main contributors (see contributors.h for copyright, address and affiliation details)
11 * - Inge Lille-Langoy <inge.lille-langoy@telenor.com>
12 * - Rickard Sjoberg <rickard.sjoberg@era.ericsson.se>
13 * - Stephan Wenger <stewe@cs.tu-berlin.de>
14 * - Jani Lainema <jani.lainema@nokia.com>
15 * - Detlev Marpe
16 * - Thomas Wedi <wedi@tnt.uni-hannover.de>
17 * - Ragip Kurceren <ragip.kurceren@nokia.com>
18 * - Greg Conklin <gregc@real.com>
19 * - Alexis Michael Tourapis <alexismt@ieee.org>
20 *************************************************************************************
21 */
22
23 #include "contributors.h"
24
25 #include <math.h>
26
27 #include "global.h"
28 #include "blk_prediction.h"
29 #include "enc_statistics.h"
30 #include "memalloc.h"
31 #include "image.h"
32 #include "mb_access.h"
33 #include "block.h"
34 #include "vlc.h"
35 #include "transform.h"
36 #include "mc_prediction.h"
37 #include "q_offsets.h"
38 #include "q_matrix.h"
39 #include "quant4x4.h"
40 #include "quantChroma.h"
41 #include "md_common.h"
42 #include "transform8x8.h"
43
44
45 // Notation for comments regarding prediction and predictors.
46 // The pels of the 4x4 block are labelled a..p. The predictor pels above
47 // are labelled A..H, from the left I..P, and from above left X, as follows:
48 //
49 // X A B C D E F G H
50 // I a b c d
51 // J e f g h
52 // K i j k l
53 // L m n o p
54 //
55
56 // Predictor array index definitions
57 #define P_X (PredPel[0])
58 #define P_A (PredPel[1])
59 #define P_B (PredPel[2])
60 #define P_C (PredPel[3])
61 #define P_D (PredPel[4])
62 #define P_E (PredPel[5])
63 #define P_F (PredPel[6])
64 #define P_G (PredPel[7])
65 #define P_H (PredPel[8])
66 #define P_I (PredPel[9])
67 #define P_J (PredPel[10])
68 #define P_K (PredPel[11])
69 #define P_L (PredPel[12])
70
71 //! array used to find expensive coefficients
72 static const byte COEFF_COST4x4[3][16] =
73 {
74 {3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0},
75 {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9},
76 {3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0},
77 };
78
79 static const byte SCAN_YUV420 [4][2] =
80 {
81 {0,0},
82 {0,1},
83 {0,2},
84 {0,3}
85 };
86
87 //! single scan pattern
88 static const byte SCAN_YUV422 [8][2] =
89 {
90 {0,0},{0,1},
91 {1,0},{0,2},
92 {0,3},{1,1},
93 {1,2},{1,3}
94 };
95
96 //! look up tables for FRExt-chroma support
97 static const unsigned char hor_offset[4][4][4] = {
98 {
99 {0, 0, 0, 0},
100 {0, 0, 0, 0},
101 {0, 0, 0, 0},
102 {0, 0, 0, 0}
103 },
104 {
105 {0, 4, 0, 4},
106 {0, 0, 0, 0},
107 {0, 0, 0, 0},
108 {0, 0, 0, 0}
109 },
110 {
111 {0, 4, 0, 4},
112 {0, 4, 0, 4},
113 {0, 0, 0, 0},
114 {0, 0, 0, 0}
115 },
116 {
117 {0, 4, 0, 4},
118 {8,12, 8,12},
119 {0, 4, 0, 4},
120 {8,12, 8,12}
121 }
122 };
123
124 static const unsigned char ver_offset[4][4][4] = {
125 {
126 {0, 0, 0, 0},
127 {0, 0, 0, 0},
128 {0, 0, 0, 0},
129 {0, 0, 0, 0}
130 },
131 {
132 {0, 0, 4, 4},
133 {0, 0, 0, 0},
134 {0, 0, 0, 0},
135 {0, 0, 0, 0}
136 },
137 {
138 {0, 0, 4, 4},
139 {8, 8,12,12},
140 {0, 0, 0, 0},
141 {0, 0, 0, 0}
142 },
143 {
144 {0, 0, 4, 4},
145 {0, 0, 4, 4},
146 {8, 8,12,12},
147 {8, 8,12,12}
148 }
149 };
150
151 static const int A[4][4] = {
152 { 16, 20, 16, 20},
153 { 20, 25, 20, 25},
154 { 16, 20, 16, 20},
155 { 20, 25, 20, 25}
156 };
157
158 static const unsigned char cbp_blk_chroma[8][4] = {
159 {16, 17, 18, 19},
160 {20, 21, 22, 23},
161 {24, 25, 26, 27},
162 {28, 29, 30, 31},
163 {32, 33, 34, 35},
164 {36, 37, 38, 39},
165 {40, 41, 42, 43},
166 {44, 45, 46, 47}
167 };
168
169 //! single scan pattern
170 static const byte SNGL_SCAN[16][2] =
171 {
172 {0,0},{1,0},{0,1},{0,2},
173 {1,1},{2,0},{3,0},{2,1},
174 {1,2},{0,3},{1,3},{2,2},
175 {3,1},{3,2},{2,3},{3,3}
176 };
177
178 //! field scan pattern
179 static const byte FIELD_SCAN[16][2] =
180 {
181 {0,0},{0,1},{1,0},{0,2},
182 {0,3},{1,1},{1,2},{1,3},
183 {2,0},{2,1},{2,2},{2,3},
184 {3,0},{3,1},{3,2},{3,3}
185 };
186
187
188 //For residual DPCM
189 static int Residual_DPCM_16x16(int **mb_ores, int ipmode);
190 static int Inv_Residual_DPCM_16x16(int **mb_ores, int ipmode);
191 static int Residual_DPCM_4x4(int ipmode, int **mb_ores, int **mb_rres, int block_y, int block_x);
192 static int Inv_Residual_DPCM_4x4 (Macroblock *currMB, int **m7, int block_y, int block_x);
193 static int Residual_DPCM_Chroma(int ipmode, int **ores, int **rres, int width, int height);
194 static int Inv_Residual_DPCM_Chroma(int ipmode, int **m7, int width, int height);
195
196 /*!
197 ************************************************************************
198 * \brief
199 * For new intra pred routines
200 *
201 * \par Input:
202 * Image par, 16x16 based intra mode
203 *
204 * \par Output:
205 * none
206 ************************************************************************
207 */
residual_transform_quant_luma_16x16(Macroblock * currMB,ColorPlane pl)208 int residual_transform_quant_luma_16x16(Macroblock *currMB, ColorPlane pl)
209 {
210 int i,j;
211 int ii,jj;
212
213 int ac_coef = 0;
214 imgpel *img_Y, *predY;
215 int nonzero = FALSE;
216
217 int jpos, ipos;
218 int b8, b4;
219
220 //begin the changes
221 int pl_off = pl<<2;
222 Slice *currSlice = currMB->p_Slice;
223 VideoParameters *p_Vid = currSlice->p_Vid;
224 QuantParameters *p_Quant = p_Vid->p_Quant;
225
226 int* DCLevel = currSlice->cofDC[pl][0];
227 int* DCRun = currSlice->cofDC[pl][1];
228 int ****cofAC = &currSlice->cofAC[pl_off];
229 int coeff_cost;
230 int new_intra_mode = currMB->i16mode;
231 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
232 int max_imgpel_value = p_Vid->max_imgpel_value;
233 int qp = (p_Vid->yuv_format==YUV444 && !currSlice->P444_joined)? currMB->qp_scaled[(int)(p_Vid->colour_plane_id)]: currMB->qp_scaled[pl]; //currMB->qp_scaled[pl];
234 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
235 imgpel ***curr_mpr_16x16 = currSlice->mpr_16x16[pl];
236
237 int qp_per = p_Quant->qp_per_matrix[qp];
238
239 // select scaling parameters
240 // needs a fix for 444 modes (pl should be current color plane)
241
242 QuantMethods quant_methods;
243 // set quantization parameters
244
245 quant_methods.qp = qp;
246 quant_methods.q_params = p_Quant->q_params_4x4[pl][1][qp];
247 quant_methods.fadjust = p_Vid->AdaptiveRounding ? (&p_Vid->ARCofAdj4x4[pl][I16MB][0]): NULL;
248 quant_methods.pos_scan = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
249 quant_methods.c_cost = COEFF_COST4x4[currSlice->disthres];
250 quant_methods.coeff_cost = &coeff_cost;
251 quant_methods.type = LUMA_16AC;
252
253
254 for (j = 0; j < 16; ++j)
255 {
256 predY = curr_mpr_16x16[new_intra_mode][j];
257 img_Y = &p_Vid->pCurImg[currMB->opix_y + j][currMB->pix_x];
258 for (i = 0; i < 16; ++i)
259 {
260 currSlice->tblk16x16[j][i] = img_Y[i] - predY[i];
261 }
262 }
263
264 // forward 4x4 integer transform
265 for (j = 0; j < 16; j+=4)
266 {
267 for (i = 0;i < 16; i+=4)
268 {
269 forward4x4(currSlice->tblk16x16, currSlice->tblk16x16, j, i);
270 }
271 }
272
273 // pick out DC coeff
274 for (j = 0; j < 4; ++j)
275 for (i = 0; i < 4; ++i)
276 currSlice->tblk4x4[j][i]= currSlice->tblk16x16[j << 2][i << 2];
277
278 // hadamard of DC coefficients
279 hadamard4x4(currSlice->tblk4x4, currSlice->tblk4x4);
280
281 nonzero = currSlice->quant_dc4x4(currMB, &currSlice->tblk4x4[0], qp, DCLevel, DCRun, &quant_methods.q_params[0][0], pos_scan);
282
283
284 // inverse DC transform
285 if (nonzero)
286 {
287 ihadamard4x4(currSlice->tblk4x4, currSlice->tblk4x4);
288
289 // inverse quantization for the DC coefficients
290 for (j = 0; j < MB_BLOCK_SIZE; j += BLOCK_SIZE)
291 {
292 for (i = 0; i < MB_BLOCK_SIZE;i += BLOCK_SIZE)
293 {
294 currSlice->tblk16x16[j][i] = rshift_rnd_sf(((currSlice->tblk4x4[j>>2][i>>2]) * quant_methods.q_params[0][0].InvScaleComp) << qp_per, 6);
295 }
296 }
297 }
298 else // All DC equal to 0.
299 {
300 for (j = 0; j < MB_BLOCK_SIZE; j += BLOCK_SIZE)
301 {
302 for (i = 0; i < MB_BLOCK_SIZE; i += BLOCK_SIZE)
303 {
304 currSlice->tblk16x16[j][i] = 0;
305 }
306 }
307 }
308
309 // AC processing for MB
310 for (jj=0;jj<4;++jj)
311 {
312 jpos = (jj << 2);
313 currMB->subblock_y = (short) jpos;
314 for (ii=0;ii<4;++ii)
315 {
316 ipos = (ii << 2);
317 currMB->subblock_x = (short) ipos;
318
319 b8 = 2*(jj >> 1) + (ii >> 1);
320 b4 = 2*(jj & 0x01) + (ii & 0x01);
321 quant_methods.block_x = ipos;
322 quant_methods.block_y = jpos;
323
324 quant_methods.ACLevel = cofAC[b8][b4][0];
325 quant_methods.ACRun = cofAC[b8][b4][1];
326
327 // Quantization process
328 nonzero = currSlice->quant_ac4x4(currMB, &currSlice->tblk16x16[jpos], &quant_methods);
329
330 if (nonzero)
331 ac_coef = 15;
332
333 //inverse transform
334 if (currSlice->tblk16x16[jpos][ipos]!= 0 || nonzero)
335 inverse4x4(currSlice->tblk16x16, currSlice->tblk16x16, jpos, ipos);
336 }
337 }
338
339
340 // Reconstruct samples
341 sample_reconstruct (&img_enc[currMB->pix_y], curr_mpr_16x16[new_intra_mode], currSlice->tblk16x16, 0, currMB->pix_x, 16, 16, max_imgpel_value, DQ_BITS);
342
343 if(currSlice->slice_type == SP_SLICE || currSlice->slice_type == SI_SLICE)
344 {
345 for (j = currMB->pix_y; j < currMB->pix_y + 16;++j)
346 for (i = currMB->pix_x; i < currMB->pix_x + 16;++i)
347 p_Vid->lrec[j][i]=-16; //signals an I16 block in the SP frame
348 }
349
350 return ac_coef;
351 }
352
Residual_DPCM_16x16(int ** m7,int ipmode)353 static int Residual_DPCM_16x16(int **m7, int ipmode)
354 {
355 int i,j;
356 int temp[16][16];
357
358 if(ipmode==VERT_PRED_16)
359 {
360 for (i=1; i<16; ++i)
361 for (j=0; j<16; ++j)
362 temp[i][j] = m7[i][j] - m7[i-1][j];
363
364 for (i=1; i<16; ++i)
365 for (j=0; j<16; ++j)
366 m7[i][j] = temp[i][j];
367 }
368 else //HOR_PRED_16
369 {
370 for (i=0; i<16; ++i)
371 for (j=1; j<16; ++j)
372 temp[i][j] = m7[i][j] - m7[i][j-1];
373
374 for (i=0; i<16; ++i)
375 for (j=1; j<16; ++j)
376 m7[i][j] = temp[i][j];
377 }
378 return 0;
379 }
380
Inv_Residual_DPCM_16x16(int ** m7,int ipmode)381 static int Inv_Residual_DPCM_16x16(int **m7, int ipmode)
382 {
383 int i;
384 int temp[16][16];
385
386 if(ipmode==VERT_PRED_16)
387 {
388 for (i=0; i<16; ++i)
389 {
390 temp[0][i] = m7[0][i];
391 temp[1][i] = m7[1][i] + temp[0][i];
392 temp[2][i] = m7[2][i] + temp[1][i];
393 temp[3][i] = m7[3][i] + temp[2][i];
394 temp[4][i] = m7[4][i] + temp[3][i];
395 temp[5][i] = m7[5][i] + temp[4][i];
396 temp[6][i] = m7[6][i] + temp[5][i];
397 temp[7][i] = m7[7][i] + temp[6][i];
398 temp[8][i] = m7[8][i] + temp[7][i];
399 temp[9][i] = m7[9][i] + temp[8][i];
400 temp[10][i] = m7[10][i] + temp[9][i];
401 temp[11][i] = m7[11][i] + temp[10][i];
402 temp[12][i] = m7[12][i] + temp[11][i];
403 temp[13][i] = m7[13][i] + temp[12][i];
404 temp[14][i] = m7[14][i] + temp[13][i];
405 temp[15][i] = m7[15][i] + temp[14][i];
406 }
407 // These could now just use a memcpy
408 for (i=0; i<16; ++i)
409 {
410 m7[1][i] = temp[1][i];
411 m7[2][i] = temp[2][i];
412 m7[3][i] = temp[3][i];
413 m7[4][i] = temp[4][i];
414 m7[5][i] = temp[5][i];
415 m7[6][i] = temp[6][i];
416 m7[7][i] = temp[7][i];
417 m7[8][i] = temp[8][i];
418 m7[9][i] = temp[9][i];
419 m7[10][i] = temp[10][i];
420 m7[11][i] = temp[11][i];
421 m7[12][i] = temp[12][i];
422 m7[13][i] = temp[13][i];
423 m7[14][i] = temp[14][i];
424 m7[15][i] = temp[15][i];
425 }
426 }
427 else //HOR_PRED_16
428 {
429 for(i=0; i<16; ++i)
430 {
431 temp[i][0] = m7[i][0];
432 temp[i][1] = m7[i][1] + temp[i][0];
433 temp[i][2] = m7[i][2] + temp[i][1];
434 temp[i][3] = m7[i][3] + temp[i][2];
435 temp[i][4] = m7[i][4] + temp[i][3];
436 temp[i][5] = m7[i][5] + temp[i][4];
437 temp[i][6] = m7[i][6] + temp[i][5];
438 temp[i][7] = m7[i][7] + temp[i][6];
439 temp[i][8] = m7[i][8] + temp[i][7];
440 temp[i][9] = m7[i][9] + temp[i][8];
441 temp[i][10] = m7[i][10] + temp[i][9];
442 temp[i][11] = m7[i][11] + temp[i][10];
443 temp[i][12] = m7[i][12] + temp[i][11];
444 temp[i][13] = m7[i][13] + temp[i][12];
445 temp[i][14] = m7[i][14] + temp[i][13];
446 temp[i][15] = m7[i][15] + temp[i][14];
447 }
448 for (i=0; i<16; ++i)
449 {
450 m7[i][1] = temp[i][1];
451 m7[i][2] = temp[i][2];
452 m7[i][3] = temp[i][3];
453 m7[i][4] = temp[i][4];
454 m7[i][5] = temp[i][5];
455 m7[i][6] = temp[i][6];
456 m7[i][7] = temp[i][7];
457 m7[i][8] = temp[i][8];
458 m7[i][9] = temp[i][9];
459 m7[i][10] = temp[i][10];
460 m7[i][11] = temp[i][11];
461 m7[i][12] = temp[i][12];
462 m7[i][13] = temp[i][13];
463 m7[i][14] = temp[i][14];
464 m7[i][15] = temp[i][15];
465 }
466 }
467 return 0;
468 }
469
470
471 /*!
472 ************************************************************************
473 * \brief
474 * For new intra pred routines
475 *
476 * \par Input:
477 * Image par, 16x16 based intra mode
478 *
479 * \par Output:
480 * none
481 ************************************************************************
482 */
residual_transform_quant_luma_16x16_ls(Macroblock * currMB,ColorPlane pl)483 int residual_transform_quant_luma_16x16_ls(Macroblock *currMB, ColorPlane pl)
484 {
485 int i,j;
486 int ii,jj;
487
488 int run,scan_pos,coeff_ctr;
489 int ac_coef = 0;
490 imgpel *img_Y, *predY;
491
492 int b8, b4;
493
494 //begin the changes
495 int pl_off = pl<<2;
496 Slice *currSlice = currMB->p_Slice;
497 VideoParameters *p_Vid = currSlice->p_Vid;
498 Boolean is_cavlc = (Boolean) (currSlice->symbol_mode == CAVLC);
499 int* DCLevel = currSlice->cofDC[pl][0];
500 int* DCRun = currSlice->cofDC[pl][1];
501 int* ACLevel;
502 int* ACRun;
503 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
504 int *m7;
505 int new_intra_mode = currMB->i16mode;
506
507 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
508 imgpel ***curr_mpr_16x16 = currSlice->mpr_16x16[pl];
509
510 for (j = 0; j < 16; ++j)
511 {
512 predY = curr_mpr_16x16[new_intra_mode][j];
513 img_Y = &p_Vid->pCurImg[currMB->opix_y + j][currMB->pix_x];
514 for (i = 0; i < 16; ++i)
515 {
516 currSlice->tblk16x16[j][i] = img_Y[i] - predY[i];
517 }
518 }
519
520 if (new_intra_mode < 2)
521 {
522 Residual_DPCM_16x16(currSlice->tblk16x16, new_intra_mode);
523 }
524
525 // pick out DC coeff
526 for (j = 0; j < 4;++j)
527 for (i = 0; i < 4;++i)
528 currSlice->tblk4x4[j][i]= currSlice->tblk16x16[j << 2][i << 2];
529
530 run=-1;
531 scan_pos=0;
532
533 for (coeff_ctr=0;coeff_ctr<16;++coeff_ctr)
534 {
535 i=pos_scan[coeff_ctr][0];
536 j=pos_scan[coeff_ctr][1];
537
538 ++run;
539
540 m7 = &currSlice->tblk4x4[j][i];
541
542 if (*m7 != 0)
543 {
544 if (is_cavlc)
545 *m7 = iClip3(-CAVLC_LEVEL_LIMIT, CAVLC_LEVEL_LIMIT, *m7);
546
547 DCLevel[scan_pos ] = *m7;
548 DCRun [scan_pos++] = run;
549 run=-1;
550 }
551 }
552 DCLevel[scan_pos]=0;
553
554 // replace DC coeff. This is needed in case of out of limits for CAVLC. Could be done only for CAVLC
555 for (j = 0; j < 4;++j)
556 for (i = 0; i < 4;++i)
557 currSlice->tblk16x16[j << 2][i << 2] = currSlice->tblk4x4[j][i];
558
559 // AC inverse trans/quant for MB
560 for (jj = 0; jj < 4; ++jj)
561 {
562 for (ii = 0; ii < 4; ++ii)
563 {
564 for (j=0;j<4;++j)
565 {
566 memcpy(currSlice->tblk4x4[j],&currSlice->tblk16x16[(jj<<2)+j][(ii<<2)], BLOCK_SIZE * sizeof(int));
567 }
568
569 run = -1;
570 scan_pos = 0;
571 b8 = 2*(jj >> 1) + (ii >> 1);
572 b4 = 2*(jj & 0x01) + (ii & 0x01);
573 ACLevel = currSlice->cofAC [b8+pl_off][b4][0];
574 ACRun = currSlice->cofAC [b8+pl_off][b4][1];
575
576 for (coeff_ctr=1;coeff_ctr<16;coeff_ctr++) // set in AC coeff
577 {
578 i=pos_scan[coeff_ctr][0];
579 j=pos_scan[coeff_ctr][1];
580
581 run++;
582 m7 = &currSlice->tblk4x4[j][i];
583
584 if (*m7 != 0)
585 {
586 if (is_cavlc)
587 *m7 = iClip3(-CAVLC_LEVEL_LIMIT, CAVLC_LEVEL_LIMIT, *m7);
588
589 ac_coef = 15;
590 ACLevel[scan_pos ] = *m7;
591 ACRun [scan_pos++] = run;
592 run=-1;
593 }
594 // set adaptive rounding p_Inp to 0 since process is not meaningful here.
595 }
596 ACLevel[scan_pos] = 0;
597
598 for (j=0;j<4;++j)
599 memcpy(&currSlice->tblk16x16[(jj<<2)+j][(ii<<2)],currSlice->tblk4x4[j], BLOCK_SIZE * sizeof(int));
600 }
601 }
602
603 if (new_intra_mode < 2)
604 {
605 Inv_Residual_DPCM_16x16(currSlice->tblk16x16, new_intra_mode);
606 }
607
608
609 for (j = 0; j < 16; ++j)
610 {
611 img_Y = &img_enc[currMB->pix_y + j][currMB->pix_x];
612 predY = curr_mpr_16x16[new_intra_mode][j];
613 for (i = 0; i < 16; ++i)
614 img_Y[i]=(imgpel)(currSlice->tblk16x16[j][i] + predY[i]);
615 }
616
617 if(currSlice->slice_type == SP_SLICE || currSlice->slice_type == SI_SLICE)
618 {
619 for (j = currMB->pix_y; j < currMB->pix_y + 16;++j)
620 for (i = currMB->pix_x; i < currMB->pix_x + 16;++i)
621 p_Vid->lrec[j][i]=-16; //signals an I16 block in the SP frame
622 }
623
624 return ac_coef;
625 }
626
check_zero(int ** mb_ores,int block_x)627 static inline int check_zero(int **mb_ores, int block_x)
628 {
629 int i, j, k = 0;
630
631 for (j = 0; (j < BLOCK_SIZE) && (k == 0); ++j)
632 {
633 for (i = block_x; (i< block_x + BLOCK_SIZE) && (k == 0); ++i)
634 {
635 //k |= (mb_ores[j][i] != 0);
636 k |= mb_ores[j][i];
637 }
638 }
639 return k;
640 }
641
642 /*!
643 ************************************************************************
644 * \brief
645 * The routine performs transform,quantization,inverse transform,
646 * adds the diff to the prediction and writes the result to the
647 * decoded luma frame.
648 *
649 * \par Input:
650 * currMB: Current macroblock.
651 * pl: Color plane for 4:4:4 coding.
652 * block_x,block_y: Block position inside a macro block (0,4,8,12).
653 * intra: Intra block indicator.
654 *
655 * \par Output_
656 * nonzero: 0 if no levels are nonzero. \n
657 * 1 if there are nonzero levels.\n
658 * coeff_cost: Coeff coding cost for thresholding consideration.\n
659 ************************************************************************
660 */
residual_transform_quant_luma_4x4(Macroblock * currMB,ColorPlane pl,int block_x,int block_y,int * coeff_cost,int intra)661 int residual_transform_quant_luma_4x4(Macroblock *currMB, ColorPlane pl, int block_x,int block_y, int *coeff_cost, int intra)
662 {
663 int nonzero = FALSE;
664
665 int pos_x = block_x >> BLOCK_SHIFT;
666 int pos_y = block_y >> BLOCK_SHIFT;
667 int b8 = 2*(pos_y >> 1) + (pos_x >> 1) + (pl<<2);
668 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
669 Slice *currSlice = currMB->p_Slice;
670 VideoParameters *p_Vid = currSlice->p_Vid;
671
672 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
673 imgpel **mb_pred = currSlice->mb_pred[pl];
674 int **mb_ores = currSlice->mb_ores[pl];
675
676 if (check_zero(&mb_ores[block_y], block_x) != 0) // check if any coefficients in block
677 {
678 int **mb_rres = currSlice->mb_rres[pl];
679 int max_imgpel_value = p_Vid->max_imgpel_value;
680 int qp = (p_Vid->yuv_format==YUV444 && !currSlice->P444_joined)? currMB->qp_scaled[(int)(p_Vid->colour_plane_id)]: currMB->qp_scaled[pl];
681 QuantParameters *p_Quant = p_Vid->p_Quant;
682 QuantMethods quant_methods;
683 quant_methods.ACLevel = currSlice->cofAC[b8][b4][0];
684 quant_methods.ACRun = currSlice->cofAC[b8][b4][1];
685
686 quant_methods.block_x = block_x;
687 quant_methods.block_y = block_y;
688 quant_methods.qp = qp;
689 quant_methods.q_params = p_Quant->q_params_4x4[pl][intra][qp];
690 quant_methods.fadjust = p_Vid->AdaptiveRounding ? (&p_Vid->ARCofAdj4x4[pl][currMB->ar_mode][block_y]) : NULL;
691 quant_methods.coeff_cost = coeff_cost;
692 quant_methods.pos_scan = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
693 quant_methods.c_cost = COEFF_COST4x4[currSlice->disthres];
694
695 currMB->subblock_x = ((b8&0x1)==0) ? (((b4&0x1)==0)? 0: 4) : (((b4&0x1)==0)? 8: 12); // horiz. position for coeff_count context
696 currMB->subblock_y = (b8<2) ? ((b4<2) ? 0: 4) : ((b4<2) ? 8: 12); // vert. position for coeff_count context
697
698 // Forward 4x4 transform
699 forward4x4(mb_ores, currSlice->tblk16x16, block_y, block_x);
700
701 // Quantization process
702 nonzero = currSlice->quant_4x4(currMB, &currSlice->tblk16x16[block_y], &quant_methods);
703
704 // Decoded block moved to frame memory
705 if (nonzero)
706 {
707 // Inverse 4x4 transform
708 inverse4x4(currSlice->tblk16x16, mb_rres, block_y, block_x);
709
710 // generate final block
711 sample_reconstruct (&img_enc[currMB->pix_y + block_y], &mb_pred[block_y], &mb_rres[block_y], block_x, currMB->pix_x + block_x, BLOCK_SIZE, BLOCK_SIZE, max_imgpel_value, DQ_BITS);
712 }
713 else // if (nonzero) => No transformed residual. Just use prediction.
714 {
715 copy_image_data_4x4(&img_enc[currMB->pix_y + block_y], &mb_pred[block_y], currMB->pix_x + block_x, block_x);
716 }
717 }
718 else
719 {
720 currSlice->cofAC[b8][b4][0][0] = 0;
721 copy_image_data_4x4(&img_enc[currMB->pix_y + block_y], &mb_pred[block_y], currMB->pix_x + block_x, block_x);
722 }
723
724 return nonzero;
725 }
726
727
728
729 /*!
730 ************************************************************************
731 * \brief
732 * Process for lossless coding of coefficients.
733 * The routine performs transform, quantization,inverse transform,
734 * adds the diff to the prediction and writes the result to the
735 * decoded luma frame.
736 *
737 * \par Input:
738 * currMB: Current macroblock.
739 * pl: Color plane for 4:4:4 coding.
740 * block_x,block_y: Block position inside a macro block (0,4,8,12).
741 * intra: Intra block indicator.
742 *
743 * \par Output_
744 * nonzero: 0 if no levels are nonzero. \n
745 * 1 if there are nonzero levels.\n
746 * coeff_cost: Coeff coding cost for thresholding consideration.\n
747 ************************************************************************
748 */
residual_transform_quant_luma_4x4_ls(Macroblock * currMB,ColorPlane pl,int block_x,int block_y,int * coeff_cost,int intra)749 int residual_transform_quant_luma_4x4_ls(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra)
750 {
751 int i,j, coeff_ctr;
752 int run = -1;
753 int nonzero = FALSE;
754
755 int pos_x = block_x >> BLOCK_SHIFT;
756 int pos_y = block_y >> BLOCK_SHIFT;
757 int b8 = 2*(pos_y >> 1) + (pos_x >> 1) + (pl<<2);
758 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
759
760 Slice *currSlice = currMB->p_Slice;
761 VideoParameters *p_Vid = currSlice->p_Vid;
762 Boolean is_cavlc = (Boolean) (currSlice->symbol_mode == CAVLC);
763 int* ACL = &currSlice->cofAC[b8][b4][0][0];
764 int* ACR = &currSlice->cofAC[b8][b4][1][0];
765
766 int pix_y, pix_x;
767 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
768 imgpel **mb_pred = currSlice->mb_pred[pl];
769 int **mb_ores = currSlice->mb_ores[pl];
770 int **mb_rres = currSlice->mb_rres[pl];
771 int *m7;
772
773 const byte *p_scan = currMB->is_field_mode ? &FIELD_SCAN[0][0] : &SNGL_SCAN[0][0];
774
775 // select scaling parameters
776 int **fadjust4x4 = p_Vid->AdaptiveRounding ? (&p_Vid->ARCofAdj4x4[pl][currMB->ar_mode][block_y]) : NULL;
777
778 if( (currMB->ipmode_DPCM < 2) && (intra))
779 {
780 Residual_DPCM_4x4(currMB->ipmode_DPCM, mb_ores, mb_rres, block_y, block_x);
781 }
782 else
783 {
784 for (j=block_y; j < block_y + BLOCK_SIZE; ++j)
785 for (i=block_x; i < block_x + BLOCK_SIZE; ++i)
786 mb_rres[j][i] = mb_ores[j][i];
787 }
788
789 for (coeff_ctr=0;coeff_ctr < 16;coeff_ctr++)
790 {
791 i = *p_scan++;
792 j = *p_scan++;
793
794 run++;
795
796 m7 = &mb_rres[block_y + j][block_x + i];
797
798 if (p_Vid->AdaptiveRounding)
799 fadjust4x4[j][block_x+i] = 0;
800
801 if (*m7 != 0)
802 {
803 if (is_cavlc)
804 *m7 = iClip3(-CAVLC_LEVEL_LIMIT, CAVLC_LEVEL_LIMIT, *m7);
805
806 nonzero=TRUE;
807 *coeff_cost += MAX_VALUE;
808 *ACL++ = *m7;
809 *ACR++ = run;
810 run=-1; // reset zero level counter
811 }
812 }
813 *ACL = 0;
814
815 if( (currMB->ipmode_DPCM < 2) && (intra))
816 {
817 Inv_Residual_DPCM_4x4(currMB, mb_rres, block_y, block_x);
818 }
819
820 for (j=0; j < BLOCK_SIZE; ++j)
821 {
822 pix_y = currMB->pix_y + block_y + j;
823 pix_x = currMB->pix_x+block_x;
824 for (i=0; i < BLOCK_SIZE; ++i)
825 {
826 img_enc[pix_y][pix_x+i] = (imgpel) (mb_rres[j+block_y][i+block_x] + mb_pred[j+block_y][i+block_x]);
827 }
828 }
829
830 return nonzero;
831 }
832
833 /*!
834 ************************************************************************
835 * \brief
836 * Residual DPCM for Intra lossless coding
837 *
838 * \par Input:
839 * block_x,block_y: Block position inside a macro block (0,4,8,12).
840 ************************************************************************
841 */
Residual_DPCM_4x4(int ipmode,int ** mb_ores,int ** mb_rres,int block_y,int block_x)842 static int Residual_DPCM_4x4(int ipmode, int **mb_ores, int **mb_rres, int block_y, int block_x)
843 {
844 int i;
845 int temp[4][4];
846
847 if(ipmode==VERT_PRED)
848 {
849 for (i=0; i<4; ++i)
850 {
851 temp[0][i] = mb_ores[block_y + 0][block_x + i];
852 temp[1][i] = mb_ores[block_y + 1][block_x + i] - mb_ores[block_y ][block_x + i];
853 temp[2][i] = mb_ores[block_y + 2][block_x + i] - mb_ores[block_y + 1][block_x + i];
854 temp[3][i] = mb_ores[block_y + 3][block_x + i] - mb_ores[block_y + 2][block_x + i];
855 }
856
857 for (i = 0; i < 4; ++i)
858 {
859 mb_rres[block_y + 0][block_x + i] = temp[0][i];
860 mb_rres[block_y + 1][block_x + i] = temp[1][i];
861 mb_rres[block_y + 2][block_x + i] = temp[2][i];
862 mb_rres[block_y + 3][block_x + i] = temp[3][i];
863 }
864 }
865 else //HOR_PRED
866 {
867 for (i=0; i<4; ++i)
868 {
869 temp[i][0] = mb_ores[block_y + i][block_x];
870 temp[i][1] = mb_ores[block_y + i][block_x + 1] - mb_ores[block_y + i][block_x ];
871 temp[i][2] = mb_ores[block_y + i][block_x + 2] - mb_ores[block_y + i][block_x + 1];
872 temp[i][3] = mb_ores[block_y + i][block_x + 3] - mb_ores[block_y + i][block_x + 2];
873 }
874
875 for (i=0; i<4; ++i)
876 {
877 mb_rres[block_y + i][block_x + 0] = temp[i][0];
878 mb_rres[block_y + i][block_x + 1] = temp[i][1];
879 mb_rres[block_y + i][block_x + 2] = temp[i][2];
880 mb_rres[block_y + i][block_x + 3] = temp[i][3];
881 }
882 }
883 return 0;
884 }
885
886 /*!
887 ************************************************************************
888 * \brief
889 * Inverse residual DPCM for Intra lossless coding
890 *
891 * \par Input:
892 * block_x,block_y: Block position inside a macro block (0,4,8,12).
893 ************************************************************************
894 */
895 //For residual DPCM
Inv_Residual_DPCM_4x4(Macroblock * currMB,int ** m7,int block_y,int block_x)896 static int Inv_Residual_DPCM_4x4(Macroblock *currMB, int **m7, int block_y, int block_x)
897 {
898 int i;
899 int temp[4][4];
900
901 if(currMB->ipmode_DPCM == VERT_PRED)
902 {
903 for(i=0; i<4; ++i)
904 {
905 temp[0][i] = m7[block_y + 0][block_x + i];
906 temp[1][i] = m7[block_y + 1][block_x + i] + temp[0][i];
907 temp[2][i] = m7[block_y + 2][block_x + i] + temp[1][i];
908 temp[3][i] = m7[block_y + 3][block_x + i] + temp[2][i];
909 }
910 for(i=0; i<4; ++i)
911 {
912 m7[block_y + 0][block_x + i] = temp[0][i];
913 m7[block_y + 1][block_x + i] = temp[1][i];
914 m7[block_y + 2][block_x + i] = temp[2][i];
915 m7[block_y + 3][block_x + i] = temp[3][i];
916 }
917 }
918 else //HOR_PRED
919 {
920 for(i=0; i<4; ++i)
921 {
922 temp[i][0] = m7[block_y + i][block_x + 0];
923 temp[i][1] = m7[block_y + i][block_x + 1] + temp[i][0];
924 temp[i][2] = m7[block_y + i][block_x + 2] + temp[i][1];
925 temp[i][3] = m7[block_y + i][block_x + 3] + temp[i][2];
926 }
927 for(i=0; i<4; ++i)
928 {
929 m7[block_y+i][block_x ] = temp[i][0];
930 m7[block_y+i][block_x+1] = temp[i][1];
931 m7[block_y+i][block_x+2] = temp[i][2];
932 m7[block_y+i][block_x+3] = temp[i][3];
933 }
934 }
935 return 0;
936 }
937
938 /*!
939 ************************************************************************
940 * \brief
941 * Transform,quantization,inverse transform for chroma.
942 * The main reason why this is done in a separate routine is the
943 * additional 2x2 transform of DC-coeffs. This routine is called
944 * once for each of the chroma components.
945 *
946 * \par Input:
947 * uv : Make difference between the U and V chroma component \n
948 * cr_cbp: chroma coded block pattern
949 *
950 * \par Output:
951 * cr_cbp: Updated chroma coded block pattern.
952 ************************************************************************
953 */
residual_transform_quant_chroma_4x4(Macroblock * currMB,int uv,int cr_cbp)954 int residual_transform_quant_chroma_4x4(Macroblock *currMB, int uv, int cr_cbp)
955 {
956 int i, j, n2, n1, coeff_ctr;
957 int *m1;
958 int coeff_cost = 0;
959 int cr_cbp_tmp = 0;
960 int DCzero = FALSE;
961 int nonzero[4][4] = {{FALSE}};
962 int nonezero = FALSE;
963 //int empty_block = TRUE;
964 Slice *currSlice = currMB->p_Slice;
965 VideoParameters *p_Vid = currSlice->p_Vid;
966 QuantParameters *p_Quant = p_Vid->p_Quant;
967
968 int b4;
969 int* DCLevel = currSlice->cofDC[uv+1][0];
970 int* DCRun = currSlice->cofDC[uv+1][1];
971 int intra = is_intra (currMB);
972 int uv_scale = uv * (p_Vid->num_blk8x8_uv >> 1);
973
974 //FRExt
975 static const int64 cbpblk_pattern[4]={0, 0xf0000, 0xff0000, 0xffff0000};
976 int yuv = p_Vid->yuv_format;
977 int b8;
978
979 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
980 int cur_qp = currMB->qpc[uv] + currSlice->bitdepth_chroma_qp_scale;
981
982 int max_imgpel_value_uv = p_Vid->max_pel_value_comp[uv + 1];
983
984 int **mb_rres = currSlice->mb_rres[uv + 1];
985 int **mb_ores = currSlice->mb_ores[uv + 1];
986 imgpel **mb_pred = currSlice->mb_pred[uv + 1];
987
988 QuantMethods quant_methods;
989 // set quantization parameters
990 quant_methods.qp = cur_qp;
991 quant_methods.q_params = p_Quant->q_params_4x4[uv + 1][intra][cur_qp];
992 quant_methods.type = CHROMA_AC;
993 if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
994 {
995 //P8x8, transform 8x8 chroma adjustments must be stored in a different array to avoid conflict with transform 4x4
996 quant_methods.fadjust = p_Vid->AdaptiveRounding ? p_Vid->ARCofAdj4x4[uv + 1][4] : NULL;
997 }
998 else
999 {
1000 quant_methods.fadjust = p_Vid->AdaptiveRounding ? p_Vid->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
1001 }
1002
1003 quant_methods.pos_scan = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
1004 quant_methods.c_cost = (currSlice->disthres == 2 && intra ) ? COEFF_COST4x4[1] : COEFF_COST4x4[currSlice->disthres];
1005 quant_methods.coeff_cost = &coeff_cost;
1006
1007
1008 p_Vid->is_v_block = uv;
1009
1010 //============= integer transform ===============
1011 for (n2=0; n2 < p_Vid->mb_cr_size_y; n2 += BLOCK_SIZE)
1012 {
1013 for (n1=0; n1 < p_Vid->mb_cr_size_x; n1 += BLOCK_SIZE)
1014 {
1015 if (check_zero(&mb_ores[n2], n1) == 0) // check if any coefficients in block
1016 {
1017 for (j = n2; j < n2 + BLOCK_SIZE; j++)
1018 for (i = n1; i < n1 + BLOCK_SIZE; i++)
1019 mb_rres[j][i] = 0;
1020 }
1021 else
1022 {
1023 forward4x4(mb_ores, mb_rres, n2, n1);
1024 //empty_block = FALSE;
1025 }
1026 }
1027 }
1028
1029 if (yuv == YUV420)
1030 {
1031 int **fadjust2x2 = NULL;
1032
1033 m1 = currSlice->tblk4x4[0];
1034 //================== CHROMA DC YUV420 ===================
1035
1036 // forward 2x2 hadamard
1037 hadamard2x2(mb_rres, m1);
1038
1039 // Quantization process of chroma 2X2 hadamard transformed DC coeffs.
1040 DCzero = currSlice->quant_dc_cr(currMB, &m1, cur_qp, DCLevel, DCRun, &quant_methods.q_params[0][0], fadjust2x2, SCAN_YUV420);
1041
1042 if (DCzero)
1043 {
1044 currMB->cbp_blk |= 0xf0000 << (uv << 2) ; // if one of the 2x2-DC levels is != 0 set the
1045 cr_cbp=imax(1,cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-19 or 20-23)
1046 }
1047
1048 // Inverse transform of 2x2 DC levels
1049 ihadamard2x2(m1, m1);
1050
1051 mb_rres[0][0] = m1[0] >> 5;
1052 mb_rres[0][4] = m1[1] >> 5;
1053 mb_rres[4][0] = m1[2] >> 5;
1054 mb_rres[4][4] = m1[3] >> 5;
1055 }
1056 else if (yuv == YUV422)
1057 {
1058 int **fadjust4x2 = NULL; // note that this is in fact 2x4 but the coefficients have been transposed for better memory access
1059 //for YUV422 only
1060 int cur_qp_dc = currMB->qpc[uv] + 3 + currSlice->bitdepth_chroma_qp_scale;
1061
1062 LevelQuantParams **quant_paramsDC = p_Quant->q_params_4x4[uv + 1][intra][cur_qp_dc];
1063
1064 //================== CHROMA DC YUV422 ===================
1065 //pick out DC coeff
1066 for (j=0; j < p_Vid->mb_cr_size_y; j+=BLOCK_SIZE)
1067 {
1068 for (i=0; i < p_Vid->mb_cr_size_x; i+=BLOCK_SIZE)
1069 currSlice->tblk4x4[i>>2][j>>2]= mb_rres[j][i];
1070 }
1071
1072 // forward hadamard transform. Note that coeffs have been transposed (4x2 instead of 2x4) which makes transform a bit faster
1073 hadamard4x2(currSlice->tblk4x4, currSlice->tblk4x4);
1074
1075 // Quantization process of chroma transformed DC coeffs.
1076 DCzero = currSlice->quant_dc_cr(currMB, currSlice->tblk4x4, cur_qp_dc, DCLevel, DCRun, &quant_paramsDC[0][0], fadjust4x2, SCAN_YUV422);
1077
1078 if (DCzero)
1079 {
1080 currMB->cbp_blk |= 0xff0000 << (uv << 3) ; // if one of the DC levels is != 0 set the
1081 cr_cbp=imax(1,cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-31 or 32-47) //YUV444
1082 }
1083
1084 //inverse DC transform. Note that now currSlice->tblk4x4 is transposed back
1085 ihadamard4x2(currSlice->tblk4x4, currSlice->tblk4x4);
1086
1087 // This code assumes sizeof(int) > 16. Therefore, no need to have conditional
1088 for (j = 0; j < 4; ++j)
1089 {
1090 mb_rres[j << 2 ][0] = rshift_rnd_sf(currSlice->tblk4x4[j][0], 6);
1091 mb_rres[j << 2 ][4] = rshift_rnd_sf(currSlice->tblk4x4[j][1], 6);
1092 }
1093 }
1094
1095 // Quant of chroma AC-coeffs.
1096 for (b8=0; b8 < (p_Vid->num_blk8x8_uv >> 1); b8++)
1097 {
1098 for (b4=0; b4 < 4; b4++)
1099 {
1100 int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
1101 n1 = hor_offset[yuv][b8][b4];
1102 n2 = ver_offset[yuv][b8][b4];
1103 quant_methods.ACLevel = currSlice->cofAC[4 + b8 + uv_scale][b4][0];
1104 quant_methods.ACRun = currSlice->cofAC[4 + b8 + uv_scale][b4][1];
1105 quant_methods.block_x = n1;
1106 quant_methods.block_y = n2;
1107
1108 currMB->subblock_y = subblk_offset_y[p_Vid->yuv_format - 1][b8][b4];
1109 currMB->subblock_x = subblk_offset_x[p_Vid->yuv_format - 1][b8][b4];
1110 if (p_Vid->AdaptiveRounding)
1111 {
1112 if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
1113 {
1114 //P8x8, transform 8x8 chroma adjustments must be stored in a different array to avoid conflict with transform 4x4
1115 quant_methods.fadjust = &p_Vid->ARCofAdj4x4[uv + 1][4][n2];
1116 }
1117 else
1118 {
1119 quant_methods.fadjust = &p_Vid->ARCofAdj4x4[uv + 1][currMB->ar_mode][n2];
1120 }
1121 }
1122 else
1123 {
1124 quant_methods.fadjust = NULL;
1125 }
1126
1127 // Quantization process
1128 nonzero[n2>>2][n1>>2] = currSlice->quant_ac4x4cr(currMB, &mb_rres[n2], &quant_methods);
1129
1130 if (nonzero[n2>>2][n1>>2])
1131 {
1132 currMB->cbp_blk |= uv_cbpblk;
1133 cr_cbp_tmp = 2;
1134 nonezero = TRUE;
1135 }
1136 }
1137 }
1138
1139 // Perform thresholding
1140 // * reset chroma coeffs
1141 if(nonezero && coeff_cost < _CHROMA_COEFF_COST_)
1142 {
1143 int64 uv_cbpblk = ((int64)cbpblk_pattern[yuv] << (uv << (1+yuv)));
1144 cr_cbp_tmp = 0;
1145
1146 for (b8 = 0; b8 < (p_Vid->num_blk8x8_uv >> 1); b8++)
1147 {
1148 for (b4 = 0; b4 < 4; b4++)
1149 {
1150 n1 = hor_offset[yuv][b8][b4];
1151 n2 = ver_offset[yuv][b8][b4];
1152 if (nonzero[n2>>2][n1>>2] == TRUE)
1153 {
1154 nonzero[n2>>2][n1>>2] = FALSE;
1155 quant_methods.ACLevel = currSlice->cofAC[4 + b8 + uv_scale][b4][0];
1156 quant_methods.ACRun = currSlice->cofAC[4 + b8 + uv_scale][b4][1];
1157
1158 if (DCzero == 0)
1159 currMB->cbp_blk &= ~(uv_cbpblk); // if no chroma DC's: then reset coded-bits of this chroma subblock
1160
1161 quant_methods.ACLevel[0] = 0;
1162
1163 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)// ac coeff
1164 {
1165 mb_rres[n2 + pos_scan[coeff_ctr][1]][n1 + pos_scan[coeff_ctr][0]] = 0;
1166 quant_methods.ACLevel[coeff_ctr] = 0;
1167 }
1168 }
1169 }
1170 }
1171 }
1172
1173 // inverse transform.
1174 // Horizontal.
1175 if(cr_cbp_tmp == 2)
1176 cr_cbp = 2;
1177
1178 nonezero = FALSE;
1179 for (n2=0; n2 < p_Vid->mb_cr_size_y; n2 += BLOCK_SIZE)
1180 {
1181 for (n1=0; n1 < p_Vid->mb_cr_size_x; n1 += BLOCK_SIZE)
1182 {
1183 if (mb_rres[n2][n1] != 0 || nonzero[n2>>2][n1>>2] == TRUE)
1184 {
1185 inverse4x4(mb_rres, mb_rres, n2, n1);
1186 nonezero = TRUE;
1187 }
1188 }
1189 }
1190
1191 // Decoded block moved to memory
1192 if (nonezero == TRUE)
1193 {
1194 sample_reconstruct (&p_Vid->enc_picture->imgUV[uv][currMB->pix_c_y], mb_pred, mb_rres, 0, currMB->pix_c_x, p_Vid->mb_cr_size_x, p_Vid->mb_cr_size_y, max_imgpel_value_uv, DQ_BITS);
1195 }
1196 else
1197 {
1198 copy_image_data(&p_Vid->enc_picture->imgUV[uv][currMB->pix_c_y], mb_pred, currMB->pix_c_x, 0, p_Vid->mb_cr_size_x, p_Vid->mb_cr_size_y);
1199 }
1200
1201 return cr_cbp;
1202 }
1203
Residual_DPCM_Chroma(int ipmode,int ** ores,int ** rres,int width,int height)1204 static int Residual_DPCM_Chroma(int ipmode, int **ores, int **rres, int width, int height)
1205 {
1206 int i,j;
1207 int temp[16][16];
1208
1209 if(ipmode==VERT_PRED_8)
1210 {
1211 for (j=0; j<width; j++)
1212 temp[0][j] = ores[0][j];
1213
1214 for (j=0; j<width; j++)
1215 for (i=1; i<height; i++)
1216 temp[i][j] = ores[i][j] - ores[i-1][j];
1217
1218 for (i = 0; i < height; i++)
1219 for (j = 0; j < width; j++)
1220 rres[i][j] = temp[i][j];
1221 }
1222 else //HOR_PRED_8
1223 {
1224 for (i=0; i<height; i++)
1225 temp[i][0] = ores[i][0];
1226
1227 for (i=0; i<height; i++)
1228 for (j=1; j<width; j++)
1229 temp[i][j] = ores[i][j] - ores[i][j-1];
1230
1231 for (i=0; i<height; i++)
1232 for (j=0; j<width; j++)
1233 rres[i][j] = temp[i][j];
1234 }
1235
1236 return 0;
1237 }
1238
Inv_Residual_DPCM_Chroma(int ipmode,int ** m7,int width,int height)1239 static int Inv_Residual_DPCM_Chroma(int ipmode, int **m7, int width, int height)
1240 {
1241 int i, j;
1242 int temp[16][16];
1243
1244 if(ipmode == VERT_PRED_8)
1245 {
1246 for(i=0; i<width; i++)
1247 {
1248 temp[0][i] = m7[0][i];
1249 for(j=1; j<height; j++)
1250 {
1251 temp[j][i] = temp[j-1][i] + m7[j][i];
1252 }
1253 }
1254 for(i=0; i<height; i++)
1255 {
1256 for(j = 0; j < width; j++)
1257 {
1258 m7[i][j] = temp[i][j];
1259 }
1260 }
1261 }
1262 else //HOR_PRED_8
1263 {
1264 for(i=0; i<height; i++)
1265 {
1266 temp[i][0] = m7[i][0];
1267 for(j = 1; j < width; j++)
1268 {
1269 temp[i][j] = temp[i][j-1] + m7[i][j];
1270 }
1271 }
1272 for(i=0; i<height; i++)
1273 {
1274 for(j = 0; j < width; j++)
1275 {
1276 m7[i][j] = temp[i][j];
1277 }
1278 }
1279 }
1280 return 0;
1281 }
1282
1283 /*!
1284 ************************************************************************
1285 * \brief
1286 * Transform,quantization,inverse transform for chroma.
1287 * The main reason why this is done in a separate routine is the
1288 * additional 2x2 transform of DC-coeffs. This routine is called
1289 * once for each of the chroma components.
1290 *
1291 * \par Input:
1292 * uv : Make difference between the U and V chroma component \n
1293 * cr_cbp: chroma coded block pattern
1294 *
1295 * \par Output:
1296 * cr_cbp: Updated chroma coded block pattern.
1297 ************************************************************************
1298 */
residual_transform_quant_chroma_4x4_ls(Macroblock * currMB,int uv,int cr_cbp)1299 int residual_transform_quant_chroma_4x4_ls(Macroblock *currMB, int uv, int cr_cbp)
1300 {
1301 int i,j,n2,n1,coeff_ctr,level ,scan_pos,run;
1302 int m1[BLOCK_SIZE];
1303 int coeff_cost;
1304 imgpel *orig_img, *pred_img;
1305
1306 int b4;
1307 Slice *currSlice = currMB->p_Slice;
1308 VideoParameters *p_Vid = currSlice->p_Vid;
1309 Boolean is_cavlc = (Boolean) (currSlice->symbol_mode == CAVLC);
1310 int* DCLevel = currSlice->cofDC[uv+1][0];
1311 int* DCRun = currSlice->cofDC[uv+1][1];
1312 int* ACLevel;
1313 int* ACRun;
1314 int uv_scale = uv * (p_Vid->num_blk8x8_uv >> 1);
1315
1316 //FRExt
1317 int yuv = p_Vid->yuv_format;
1318 int b8;
1319 int *m7;
1320 int m3[4][4];
1321
1322 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
1323 int **mb_rres = currSlice->mb_rres[uv + 1];
1324 int **mb_ores = currSlice->mb_ores[uv + 1];
1325 imgpel **mb_pred = currSlice->mb_pred[uv + 1];
1326 int **fadjust4x4;
1327
1328 int intra = ((currMB->mb_type == I4MB) || (currMB->mb_type == I8MB) || (currMB->mb_type == I16MB));
1329
1330
1331 if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
1332 {
1333 //P8x8, transform 8x8 chroma adjustments must be stored in a different array to avoid conflict with transform 4x4
1334 fadjust4x4 = p_Vid->AdaptiveRounding ? p_Vid->ARCofAdj4x4[uv + 1][4] : NULL;
1335 }
1336 else
1337 {
1338 fadjust4x4 = p_Vid->AdaptiveRounding ? p_Vid->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
1339 }
1340
1341 if((currMB->c_ipred_mode == HOR_PRED_8 || currMB->c_ipred_mode == VERT_PRED_8) && (intra))
1342 {
1343 Residual_DPCM_Chroma(currMB->c_ipred_mode, mb_ores, mb_rres, p_Vid->mb_cr_size_x, p_Vid->mb_cr_size_y);
1344 }
1345 else
1346 {
1347 for (j=0; j < p_Vid->mb_cr_size_y; j++)
1348 {
1349 for (i=0; i < p_Vid->mb_cr_size_x; i++)
1350 {
1351 mb_rres[j][i] = mb_ores[j][i];
1352 }
1353 }
1354 }
1355
1356 if (yuv == YUV420)
1357 {
1358 //================== CHROMA DC YUV420 ===================
1359 // 2X2 transform of DC coeffs.
1360 run=-1;
1361 scan_pos=0;
1362 m1[0] = mb_rres[0][0] ;
1363 m1[1] = mb_rres[0][4] ;
1364 m1[2] = mb_rres[4][0] ;
1365 m1[3] = mb_rres[4][4] ;
1366
1367 for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
1368 {
1369 run++;
1370
1371 level =iabs(m1[coeff_ctr]);
1372
1373 if (level != 0)
1374 {
1375 if (is_cavlc)
1376 level = imin(level, CAVLC_LEVEL_LIMIT);
1377
1378 currMB->cbp_blk |= 0xf0000 << (uv << 2) ; // if one of the 2x2-DC levels is != 0 set the
1379 cr_cbp=imax(1, cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-19 or 20-23)
1380 level = isignab(level, m1[coeff_ctr]);
1381 DCLevel[scan_pos ] = level;
1382 DCRun [scan_pos++] = run;
1383 run=-1;
1384 }
1385 }
1386 DCLevel[scan_pos] = 0;
1387 }
1388 else if(yuv == YUV422)
1389 {
1390 //================== CHROMA DC YUV422 ===================
1391 //transform DC coeff
1392 //horizontal
1393
1394 //pick out DC coeff
1395 for (j=0; j < p_Vid->mb_cr_size_y; j+=BLOCK_SIZE)
1396 {
1397 for (i=0; i < p_Vid->mb_cr_size_x; i+=BLOCK_SIZE)
1398 {
1399 m3[i>>2][j>>2] = mb_rres[j][i];
1400 }
1401 }
1402
1403
1404 run=-1;
1405 scan_pos=0;
1406
1407 //quant of chroma DC-coeffs
1408 for (coeff_ctr=0;coeff_ctr<8;coeff_ctr++)
1409 {
1410 i=SCAN_YUV422[coeff_ctr][0];
1411 j=SCAN_YUV422[coeff_ctr][1];
1412
1413 run++;
1414
1415 level = iabs(m3[i][j]);
1416 currSlice->tblk4x4[i][j]=m3[i][j];
1417
1418 if (level != 0)
1419 {
1420 //YUV422
1421 currMB->cbp_blk |= 0xff0000 << (uv << 3) ; // if one of the DC levels is != 0 set the
1422 cr_cbp=imax(1,cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-31 or 32-47) //YUV444
1423
1424 DCLevel[scan_pos ] = isignab(level,currSlice->tblk4x4[i][j]);
1425 DCRun [scan_pos++] = run;
1426 run=-1;
1427 }
1428 }
1429 DCLevel[scan_pos]=0;
1430
1431 //inverse DC transform
1432 //horizontal
1433 }
1434
1435 // Quant of chroma AC-coeffs.
1436 coeff_cost=0;
1437
1438 for (b8=0; b8 < (p_Vid->num_blk8x8_uv >> 1); b8++)
1439 {
1440 for (b4=0; b4 < 4; b4++)
1441 {
1442 int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
1443 n1 = hor_offset[yuv][b8][b4];
1444 n2 = ver_offset[yuv][b8][b4];
1445 ACLevel = currSlice->cofAC[4 + b8 + uv_scale][b4][0];
1446 ACRun = currSlice->cofAC[4 + b8 + uv_scale][b4][1];
1447 run=-1;
1448 scan_pos=0;
1449
1450 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)// start change rd_quant
1451 {
1452 i=pos_scan[coeff_ctr][0];
1453 j=pos_scan[coeff_ctr][1];
1454
1455 ++run;
1456
1457 level = iabs(mb_rres[n2+j][n1+i]);
1458
1459 if (p_Vid->AdaptiveRounding)
1460 {
1461 fadjust4x4[n2+j][n1+i] = 0;
1462 }
1463
1464 if (level != 0)
1465 {
1466 cr_cbp=imax(2, cr_cbp);
1467
1468 currMB->cbp_blk |= uv_cbpblk;
1469 coeff_cost += MAX_VALUE; // set high cost, shall not be discarded
1470
1471 ACLevel[scan_pos ] = isignab(level, mb_rres[n2+j][n1+i]);
1472 ACRun [scan_pos++] = run;
1473 run=-1;
1474
1475 level = isignab(level, mb_rres[n2+j][n1+i]);
1476 }
1477 }
1478 ACLevel[scan_pos] = 0;
1479 }
1480 }
1481
1482 if((currMB->c_ipred_mode == HOR_PRED_8 || currMB->c_ipred_mode == VERT_PRED_8) && intra)
1483 {
1484 Inv_Residual_DPCM_Chroma(currMB->c_ipred_mode, mb_rres, p_Vid->mb_cr_size_x, p_Vid->mb_cr_size_y) ;
1485 }
1486
1487 for (j=0; j < p_Vid->mb_cr_size_y; ++j)
1488 {
1489 orig_img = &p_Vid->enc_picture->imgUV[uv][currMB->pix_c_y + j][currMB->pix_c_x];
1490 m7 = mb_rres[j];
1491 pred_img = mb_pred[j];
1492 for (i=0; i < p_Vid->mb_cr_size_x; ++i)
1493 {
1494 orig_img[i] = (imgpel) m7[i] + pred_img[i];
1495 }
1496 }
1497
1498 return cr_cbp;
1499 }
1500
1501 /*!
1502 ************************************************************************
1503 * \brief
1504 * The routine performs transform,quantization,inverse transform, adds the diff.
1505 * to the prediction and writes the result to the decoded luma frame. Includes the
1506 * RD constrained quantization also.
1507 *
1508 * \par Input:
1509 * block_x,block_y: Block position inside a macro block (0,4,8,12).
1510 *
1511 * \par Output:
1512 * nonzero: 0 if no levels are nonzero. 1 if there are nonzero levels. \n
1513 * coeff_cost: Counter for nonzero coefficients, used to discard expensive levels.
1514 *
1515 *
1516 ************************************************************************
1517 */
residual_transform_quant_luma_4x4_sp(Macroblock * currMB,ColorPlane pl,int block_x,int block_y,int * coeff_cost,int intra)1518 int residual_transform_quant_luma_4x4_sp(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra)
1519 {
1520 int i,j,coeff_ctr;
1521 int qp_const,ilev, level,scan_pos = 0,run = -1;
1522 int nonzero = FALSE;
1523 VideoParameters *p_Vid = currMB->p_Vid;
1524 Slice *currSlice = currMB->p_Slice;
1525
1526 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
1527 imgpel **mb_pred = currSlice->mb_pred[pl];
1528 int **mb_rres = currSlice->mb_rres[pl];
1529 int **mb_ores = currSlice->mb_ores[pl];
1530 int c_err,qp_const2;
1531
1532 int qp = currMB->qp_scaled[pl];
1533 int qp_sp = (currMB->qpsp);
1534
1535 const byte *c_cost = COEFF_COST4x4[currSlice->disthres];
1536 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
1537
1538 int pos_x = block_x >> BLOCK_SHIFT;
1539 int pos_y = block_y >> BLOCK_SHIFT;
1540 int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
1541 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
1542 int* ACLevel = currSlice->cofAC[b8][b4][0];
1543 int* ACRun = currSlice->cofAC[b8][b4][1];
1544
1545 // For encoding optimization
1546 int c_err1, c_err2, level1, level2;
1547 double D_dis1, D_dis2;
1548 int len, info;
1549 double lambda_mode = 0.85 * pow (2, (qp - SHIFT_QP)/3.0) * 4;
1550
1551 QuantParameters *p_Quant = p_Vid->p_Quant;
1552
1553 int qp_per = p_Quant->qp_per_matrix[qp];
1554 int q_bits = Q_BITS + qp_per;
1555 int qp_per_sp = p_Quant->qp_per_matrix[qp_sp];
1556 int q_bits_sp = Q_BITS + qp_per_sp;
1557
1558 LevelQuantParams **q_params_4x4 = p_Quant->q_params_4x4[pl][intra][qp];
1559 LevelQuantParams **quant_params_sp = p_Quant->q_params_4x4[pl][intra][qp_sp];
1560
1561 qp_const = (1<<q_bits)/6; // inter
1562 qp_const2 = (1<<q_bits_sp)>>1; //sp_pred
1563
1564 // Horizontal transform
1565 for (j=block_y; j< block_y + BLOCK_SIZE; ++j)
1566 {
1567 for (i=block_x; i< block_x + BLOCK_SIZE; ++i)
1568 {
1569 mb_rres[j][i] = mb_ores[j][i];
1570 mb_rres[j][i]+=mb_pred[j][i];
1571 currSlice->tblk16x16[j][i] = mb_pred[j][i];
1572 }
1573 }
1574
1575 // 4x4 transform
1576 forward4x4(mb_rres, mb_rres, block_y, block_x);
1577 forward4x4(currSlice->tblk16x16, currSlice->tblk16x16, block_y, block_x);
1578
1579 for (coeff_ctr = 0;coeff_ctr < 16;coeff_ctr++)
1580 {
1581 i = pos_scan[coeff_ctr][0];
1582 j = pos_scan[coeff_ctr][1];
1583
1584 run++;
1585 ilev=0;
1586
1587 // decide prediction
1588
1589 // case 1
1590 level1 = (iabs (currSlice->tblk16x16[j+block_y][i+block_x]) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp;
1591 level1 = (level1 << q_bits_sp) / quant_params_sp[j][i].ScaleComp;
1592 c_err1 = mb_rres[j+block_y][i+block_x] - isignab(level1, currSlice->tblk16x16[j+block_y][i+block_x]);
1593 level1 = (iabs (c_err1) * q_params_4x4[j][i].ScaleComp + qp_const) >> q_bits;
1594
1595 // case 2
1596 c_err2 = mb_rres[j+block_y][i+block_x] - currSlice->tblk16x16[j+block_y][i+block_x];
1597 level2 = (iabs (c_err2) * q_params_4x4[j][i].ScaleComp + qp_const) >> q_bits;
1598
1599 // select prediction
1600 if ((level1 != level2) && (level1 != 0) && (level2 != 0))
1601 {
1602 D_dis1 = mb_rres[j+block_y][i+block_x] -
1603 ((isignab(level1,c_err1) * (q_params_4x4[j][i].InvScaleComp >> 4) * A[j][i]<< qp_per) >>6)
1604 - currSlice->tblk16x16[j+block_y][i+block_x];
1605 levrun_linfo_inter(level1, run, &len, &info);
1606 D_dis1 = D_dis1 * D_dis1 + lambda_mode * len;
1607
1608 D_dis2 = mb_rres[j+block_y][i+block_x] -
1609 ((isignab(level2,c_err2)* (q_params_4x4[j][i].InvScaleComp >> 4)* A[j][i]<< qp_per) >>6)
1610 - currSlice->tblk16x16[j+block_y][i+block_x];
1611 levrun_linfo_inter(level2, run, &len, &info);
1612 D_dis2 = D_dis2 * D_dis2 + lambda_mode * len;
1613
1614 if (D_dis1 == D_dis2)
1615 level = (iabs(level1) < iabs(level2)) ? level1 : level2;
1616 else if (D_dis1 < D_dis2)
1617 level = level1;
1618 else
1619 level = level2;
1620
1621 c_err = (level == level1) ? c_err1 : c_err2;
1622 }
1623 else if (level1 == level2)
1624 {
1625 level = level1;
1626 c_err = c_err1;
1627 }
1628 else
1629 {
1630 level = (level1 == 0) ? level1 : level2;
1631 c_err = (level1 == 0) ? c_err1 : c_err2;
1632 }
1633
1634 if (level != 0)
1635 {
1636 nonzero = TRUE;
1637
1638 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
1639
1640 level = isignab(level,c_err);
1641 ACLevel[scan_pos] = level;
1642 ACRun [scan_pos] = run;
1643 ++scan_pos;
1644 run=-1; // reset zero level counter
1645 ilev=((level * (q_params_4x4[j][i].InvScaleComp >> 4)* A[j][i] << qp_per) >>6);
1646 }
1647
1648 ilev += currSlice->tblk16x16[j+block_y][i+block_x];
1649
1650 if(currSlice->slice_type != SI_SLICE && !p_Vid->sp2_frame_indicator)//stores the SP frame coefficients in p_Vid->lrec, will be useful to encode these and create SI or SP switching frame
1651 {
1652 p_Vid->lrec[currMB->pix_y+block_y+j][currMB->pix_x+block_x+i]=
1653 isignab((iabs(ilev) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp, ilev);
1654 }
1655
1656 // Yrec Qs and DeQs
1657 mb_rres[j+block_y][i+block_x] = isignab((iabs(ilev) * quant_params_sp[j][i].ScaleComp + qp_const2)>> q_bits_sp, ilev) * (quant_params_sp[j][i].InvScaleComp >> 4) << qp_per_sp;
1658 }
1659 ACLevel[scan_pos] = 0;
1660
1661 // inverse transform
1662 inverse4x4(mb_rres, mb_rres, block_y, block_x);
1663
1664 for (j=block_y; j < block_y+BLOCK_SIZE; ++j)
1665 {
1666 for (i=block_x; i < block_x+BLOCK_SIZE; ++i)
1667 {
1668 mb_rres[j][i] = iClip1 (p_Vid->max_imgpel_value, rshift_rnd_sf(mb_rres[j][i], DQ_BITS));
1669 }
1670 }
1671
1672 // Decoded block moved to frame memory
1673 for (j=0; j < BLOCK_SIZE; ++j)
1674 {
1675 for (i=0; i < BLOCK_SIZE; ++i)
1676 {
1677 img_enc[currMB->pix_y+block_y+j][currMB->pix_x+block_x+i]= (imgpel) mb_rres[block_y+j][block_x+i];
1678 }
1679 }
1680
1681 return nonzero;
1682 }
1683
1684 /*!
1685 ************************************************************************
1686 * \brief
1687 * Transform,quantization,inverse transform for chroma.
1688 * The main reason why this is done in a separate routine is the
1689 * additional 2x2 transform of DC-coeffs. This routine is called
1690 * once for each of the chroma components.
1691 *
1692 * \par Input:
1693 * uv : Make difference between the U and V chroma component
1694 * cr_cbp: chroma coded block pattern
1695 *
1696 * \par Output:
1697 * cr_cbp: Updated chroma coded block pattern.
1698 ************************************************************************
1699 */
residual_transform_quant_chroma_4x4_sp(Macroblock * currMB,int uv,int cr_cbp)1700 int residual_transform_quant_chroma_4x4_sp(Macroblock *currMB, int uv,int cr_cbp)
1701 {
1702 int i,j,ilev,n2,n1,coeff_ctr,c_err,level ,scan_pos,run;
1703 int m1[BLOCK_SIZE];
1704 int coeff_cost;
1705 int cr_cbp_tmp;
1706 int mp1[BLOCK_SIZE];
1707 Slice *currSlice = currMB->p_Slice;
1708 VideoParameters *p_Vid = currSlice->p_Vid;
1709 Boolean is_cavlc = (Boolean) (currSlice->symbol_mode == CAVLC);
1710 const byte *c_cost = COEFF_COST4x4[currSlice->disthres];
1711 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
1712
1713 int intra = is_intra (currMB);
1714
1715 int b4;
1716 int* DCLevel = currSlice->cofDC[uv+1][0];
1717 int* DCRun = currSlice->cofDC[uv+1][1];
1718
1719 int c_err1, c_err2, level1, level2;
1720 int len, info;
1721 double D_dis1, D_dis2;
1722 double lambda_mode = 0.85 * pow (2, (currMB->qp -SHIFT_QP)/3.0) * 4;
1723 int max_imgpel_value_uv = p_Vid->max_pel_value_comp[1];
1724
1725 int qpChroma = currMB->qpc[uv] + currSlice->bitdepth_chroma_qp_scale;
1726 int qpc_sp = iClip3(-currSlice->bitdepth_chroma_qp_scale, 51, currMB->qpsp + p_Vid->active_pps->chroma_qp_index_offset);
1727 int qpChromaSP = qpc_sp < 0 ? qpc_sp : QP_SCALE_CR[qpc_sp];
1728
1729 int **mb_rres = currSlice->mb_rres[uv + 1];
1730 int **mb_ores = currSlice->mb_ores[uv + 1];
1731 imgpel **mb_pred = currSlice->mb_pred[uv + 1];
1732
1733 QuantMethods quant_methods;
1734 QuantParameters *p_Quant = p_Vid->p_Quant;
1735
1736 int qp_per = p_Quant->qp_per_matrix[qpChroma];
1737 int q_bits = Q_BITS + qp_per;
1738 int qp_const = (1<<q_bits)/6; // inter
1739 int qp_per_sp = p_Quant->qp_per_matrix[qpChromaSP];
1740 int q_bits_sp = Q_BITS + qp_per_sp;
1741 int qp_const2 = (1<<q_bits_sp)>>1; //sp_pred
1742
1743 LevelQuantParams **q_params_4x4 = p_Quant->q_params_4x4[uv + 1][intra][qpChroma];
1744
1745 LevelQuantParams **quant_params_sp = p_Quant->q_params_4x4[uv + 1][intra][qpChromaSP];
1746
1747 quant_methods.type = CHROMA_AC;
1748
1749 for (j=0; j < p_Vid->mb_cr_size_y; ++j)
1750 {
1751 for (i=0; i < p_Vid->mb_cr_size_x; ++i)
1752 {
1753 mb_rres[j][i] = mb_ores[j][i];
1754 mb_rres[j][i] += mb_pred[j][i];
1755 currSlice->tblk16x16[j][i] = mb_pred[j][i];
1756 }
1757 }
1758
1759 for (n2=0; n2 < p_Vid->mb_cr_size_y; n2 += BLOCK_SIZE)
1760 {
1761 for (n1=0; n1 < p_Vid->mb_cr_size_x; n1 += BLOCK_SIZE)
1762 {
1763 forward4x4(mb_rres, mb_rres, n2, n1);
1764 forward4x4(currSlice->tblk16x16, currSlice->tblk16x16, n2, n1);
1765 }
1766 }
1767
1768 // 2X2 transform of DC coeffs.
1769 hadamard2x2(mb_rres, m1);
1770 hadamard2x2(currSlice->tblk16x16, mp1);
1771
1772 run=-1;
1773 scan_pos=0;
1774
1775 for (coeff_ctr = 0; coeff_ctr < 4; coeff_ctr++)
1776 {
1777 run++;
1778 ilev=0;
1779
1780 // case 1
1781 c_err1 = (iabs (mp1[coeff_ctr]) * quant_params_sp[0][0].ScaleComp + 2 * qp_const2) >> (q_bits_sp + 1);
1782 c_err1 = (c_err1 << (q_bits_sp + 1)) / quant_params_sp[0][0].ScaleComp;
1783 c_err1 = m1[coeff_ctr] - isignab(c_err1, mp1[coeff_ctr]);
1784 level1 = (iabs(c_err1) * q_params_4x4[0][0].ScaleComp + 2 * qp_const) >> (q_bits+1);
1785
1786 // case 2
1787 c_err2 = m1[coeff_ctr] - mp1[coeff_ctr];
1788 level2 = (iabs(c_err2) * q_params_4x4[0][0].ScaleComp + 2 * qp_const) >> (q_bits+1);
1789
1790 if (level1 != level2 && level1 != 0 && level2 != 0)
1791 {
1792 D_dis1 = m1[coeff_ctr] - ((isignab(level1,c_err1)* (q_params_4x4[0][0].InvScaleComp >> 4) * A[0][0]<< qp_per) >>5)- mp1[coeff_ctr];
1793 levrun_linfo_c2x2(level1, run, &len, &info);
1794 D_dis1 = D_dis1 * D_dis1 + lambda_mode * len;
1795
1796 D_dis2 = m1[coeff_ctr] - ((isignab(level2,c_err2)* (q_params_4x4[0][0].InvScaleComp >> 4) * A[0][0]<< qp_per) >>5)- mp1[coeff_ctr];
1797 levrun_linfo_c2x2(level2, run, &len, &info);
1798 D_dis2 = D_dis2 * D_dis2 + lambda_mode * len;
1799
1800 if (D_dis1 == D_dis2)
1801 level = (iabs(level1) < iabs(level2)) ? level1 : level2;
1802 else if (D_dis1 < D_dis2)
1803 level = level1;
1804 else
1805 level = level2;
1806
1807 c_err = (level == level1) ? c_err1 : c_err2;
1808 }
1809 else if (level1 == level2)
1810 {
1811 level = level1;
1812 c_err = c_err1;
1813 }
1814 else
1815 {
1816 level = (level1 == 0) ? level1 : level2;
1817 c_err = (level1 == 0) ? c_err1 : c_err2;
1818 }
1819
1820 if (level != 0)
1821 {
1822 if (is_cavlc)
1823 level = imin(level, CAVLC_LEVEL_LIMIT);
1824
1825 currMB->cbp_blk |= 0xf0000 << (uv << 2) ; // if one of the 2x2-DC levels is != 0 the coded-bit
1826 cr_cbp = imax(1, cr_cbp);
1827 DCLevel[scan_pos ] = isignab(level ,c_err);
1828 DCRun [scan_pos++] = run;
1829 run=-1;
1830 ilev=((isignab(level,c_err)* (q_params_4x4[0][0].InvScaleComp*A[0][0] >> 4) << qp_per) >>5);
1831 }
1832
1833 ilev+= mp1[coeff_ctr];
1834 m1[coeff_ctr]=isignab((iabs(ilev) * quant_params_sp[0][0].ScaleComp + 2 * qp_const2) >> (q_bits_sp+1), ilev) * (quant_params_sp[0][0].InvScaleComp >> 4) << qp_per_sp;
1835 if(currSlice->slice_type != SI_SLICE && !p_Vid->sp2_frame_indicator)
1836 p_Vid->lrec_uv[uv][currMB->pix_c_y + 4*(coeff_ctr & 0x01)][currMB->pix_c_x + 4*(coeff_ctr>>1)]=isignab((iabs(ilev) * quant_params_sp[0][0].ScaleComp + 2 * qp_const2) >> (q_bits_sp+1), ilev);// stores the SP frames coefficients, will be useful to encode SI or switching SP frame
1837 }
1838 DCLevel[scan_pos] = 0;
1839
1840 // Inverse transform of 2x2 DC levels
1841 ihadamard2x2(m1, m1);
1842
1843 mb_rres[0][0]=(m1[0])>>1;
1844 mb_rres[0][4]=(m1[1])>>1;
1845 mb_rres[4][0]=(m1[2])>>1;
1846 mb_rres[4][4]=(m1[3])>>1;
1847
1848 // Quant of chroma AC-coeffs.
1849 coeff_cost=0;
1850 cr_cbp_tmp=0;
1851
1852 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
1853 {
1854 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
1855 {
1856 b4 = 2*(n2 >> 2) + (n1 >> 2);
1857 quant_methods.ACLevel = currSlice->cofAC[uv+4][b4][0];
1858 quant_methods.ACRun = currSlice->cofAC[uv+4][b4][1];
1859
1860 run = -1;
1861 scan_pos = 0;
1862
1863 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)// start change rd_quant
1864 {
1865 i=pos_scan[coeff_ctr][0];
1866 j=pos_scan[coeff_ctr][1];
1867
1868 ++run;
1869 ilev=0;
1870
1871 // quantization on prediction
1872 c_err1 = (iabs(currSlice->tblk16x16[n2+j][n1+i]) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp;
1873 c_err1 = (c_err1 << q_bits_sp) / quant_params_sp[j][i].ScaleComp;
1874 c_err1 = mb_rres[n2+j][n1+i] - isignab(c_err1, currSlice->tblk16x16[n2+j][n1+i]);
1875 level1 = (iabs(c_err1) * q_params_4x4[j][i].ScaleComp + qp_const) >> q_bits;
1876
1877 // no quantization on prediction
1878 c_err2 = mb_rres[n2+j][n1+i] - currSlice->tblk16x16[n2+j][n1+i];
1879 level2 = (iabs(c_err2) * q_params_4x4[j][i].ScaleComp + qp_const) >> q_bits;
1880
1881 if (level1 != level2 && level1 != 0 && level2 != 0)
1882 {
1883 D_dis1 = mb_rres[n2+j][n1+i] - ((isignab(level1,c_err1) * (q_params_4x4[j][i].InvScaleComp >> 4) * A[j][i]<< qp_per) >>6) - currSlice->tblk16x16[n2+j][n1+i];
1884
1885 levrun_linfo_inter(level1, run, &len, &info);
1886 D_dis1 = D_dis1 * D_dis1 + lambda_mode * len;
1887
1888 D_dis2 = mb_rres[n2+j][n1+i] - ((isignab(level2,c_err2)* (q_params_4x4[j][i].InvScaleComp*A[j][i] >> 4) << qp_per) >>6) - currSlice->tblk16x16[n2+j][n1+i];
1889 levrun_linfo_inter(level2, run, &len, &info);
1890 D_dis2 = D_dis2 * D_dis2 + lambda_mode * len;
1891
1892 if (D_dis1 == D_dis2)
1893 level = (iabs(level1) < iabs(level2)) ? level1 : level2;
1894 else
1895 {
1896 if (D_dis1 < D_dis2)
1897 level = level1;
1898 else
1899 level = level2;
1900 }
1901 c_err = (level == level1) ? c_err1 : c_err2;
1902 }
1903 else if (level1 == level2)
1904 {
1905 level = level1;
1906 c_err = c_err1;
1907 }
1908 else
1909 {
1910 level = (level1 == 0) ? level1 : level2;
1911 c_err = (level1 == 0) ? c_err1 : c_err2;
1912 }
1913
1914 if (level != 0)
1915 {
1916 currMB->cbp_blk |= (int64)1 << (16 + (uv << 2) + ((n2 >> 1) + (n1 >> 2))) ;
1917
1918 coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run]; // set high cost, shall not be discarded
1919
1920 cr_cbp_tmp=2;
1921 level = isignab(level,c_err);
1922 quant_methods.ACLevel[scan_pos] = level;
1923 quant_methods.ACRun [scan_pos] = run;
1924 ++scan_pos;
1925 run=-1;
1926 ilev=((level * (q_params_4x4[j][i].InvScaleComp*A[j][i] >> 4) << qp_per) >>6);
1927 }
1928 ilev+=currSlice->tblk16x16[n2+j][n1+i];
1929 if(currSlice->slice_type != SI_SLICE && !p_Vid->sp2_frame_indicator)
1930 if(!( ((n2+j) & 0x03)==0 && ((n1+i) & 0x03) ==0 ))
1931 p_Vid->lrec_uv[uv][currMB->pix_c_y + n2+j][currMB->pix_c_x + n1 + i]=isignab((iabs(ilev) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp,ilev);//stores the SP frames coefficients, will be useful to encode SI or switching SP frame
1932 mb_rres[n2+j][n1+i] = isignab((iabs(ilev) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp,ilev) * (quant_params_sp[j][i].InvScaleComp >> 4) << qp_per_sp;
1933 }
1934 quant_methods.ACLevel[scan_pos] = 0;
1935 }
1936 }
1937
1938 // * reset chroma coeffs
1939
1940 if(cr_cbp_tmp==2)
1941 cr_cbp=2;
1942
1943 // inverse transform.
1944 // Horizontal.
1945 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
1946 {
1947 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
1948 {
1949 inverse4x4(mb_rres, mb_rres, n2, n1);
1950
1951 for (j=0; j < BLOCK_SIZE; ++j)
1952 for (i=0; i < BLOCK_SIZE; ++i)
1953 {
1954 mb_rres[n2+j][n1+i] = iClip1 (max_imgpel_value_uv,rshift_rnd_sf(mb_rres[n2+j][n1+i], DQ_BITS));
1955 }
1956 }
1957 }
1958
1959 // Decoded block moved to memory
1960 for (j=0; j < BLOCK_SIZE*2; ++j)
1961 for (i=0; i < BLOCK_SIZE*2; ++i)
1962 {
1963 p_Vid->enc_picture->imgUV[uv][currMB->pix_c_y + j][currMB->pix_c_x + i]= (imgpel) mb_rres[j][i];
1964 }
1965
1966 return cr_cbp;
1967 }
1968
1969
1970 /*!
1971 ************************************************************************
1972 * \brief
1973 * The routine performs transform,quantization,inverse transform, adds the diff.
1974 * to the prediction and writes the result to the decoded luma frame. Includes the
1975 * RD constrained quantization also.
1976 *
1977 * \par Input:
1978 * block_x,block_y: Block position inside a macro block (0,4,8,12).
1979 *
1980 * \par Output:
1981 * nonzero: 0 if no levels are nonzero. 1 if there are nonzero levels. \n
1982 * coeff_cost: Counter for nonzero coefficients, used to discard expensive levels.
1983 ************************************************************************
1984 */
copyblock_sp(Macroblock * currMB,ColorPlane pl,int block_x,int block_y)1985 void copyblock_sp(Macroblock *currMB, ColorPlane pl, int block_x,int block_y)
1986 {
1987 int i, j;
1988
1989 Slice *currSlice = currMB->p_Slice;
1990 VideoParameters *p_Vid = currSlice->p_Vid;
1991 QuantParameters *p_Quant = p_Vid->p_Quant;
1992 int cur_qp = currMB->qpsp + p_Vid->bitdepth_luma_qp_scale;
1993 int qp_per = p_Quant->qp_per_matrix[cur_qp];
1994 int q_bits = Q_BITS + qp_per;
1995 int qp_const2 = (1 << q_bits) >> 1; //sp_pred
1996 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
1997 imgpel **mb_pred = currSlice->mb_pred[pl];
1998 int **mb_ores = currSlice->mb_ores[pl];
1999 int **mb_rres = currSlice->mb_rres[pl];
2000
2001 LevelQuantParams **q_params_4x4 = p_Quant->q_params_4x4[pl][0][cur_qp];
2002
2003 // Horizontal transform
2004 for (j=0; j< BLOCK_SIZE; ++j)
2005 {
2006 for (i=0; i< BLOCK_SIZE; ++i)
2007 {
2008 mb_rres[j][i] = mb_ores[j+block_y][i+block_x];
2009 currSlice->tblk16x16[j][i]=mb_pred[j+block_y][i+block_x];
2010 }
2011 }
2012
2013 forward4x4(currSlice->tblk16x16, currSlice->tblk16x16, 0, 0);
2014
2015 // Quant
2016 for (j=0;j < BLOCK_SIZE; ++j)
2017 {
2018 for (i=0; i < BLOCK_SIZE; ++i)
2019 {
2020 mb_rres[j][i]=isignab((iabs(currSlice->tblk16x16[j][i])* q_params_4x4[j][i].ScaleComp+qp_const2)>> q_bits,
2021 currSlice->tblk16x16[j][i]) * (q_params_4x4[j][i].InvScaleComp >> 4) <<qp_per;
2022 if(currSlice->slice_type != SI_SLICE && !p_Vid->sp2_frame_indicator)
2023 {
2024 p_Vid->lrec[currMB->pix_y+block_y+j][currMB->pix_x+block_x+i] =
2025 isignab((iabs(currSlice->tblk16x16[j][i]) * q_params_4x4[j][i].ScaleComp + qp_const2) >> q_bits, currSlice->tblk16x16[j][i]);// stores the SP frames coefficients, will be useful to encode SI or switching SP frame
2026 }
2027 }
2028 }
2029
2030 // inverse transform.
2031 // horizontal
2032 inverse4x4(mb_rres, mb_rres, 0, 0);
2033
2034 // Decoded block moved to frame memory
2035 for (j=0; j < BLOCK_SIZE; ++j)
2036 {
2037 for (i=0; i < BLOCK_SIZE; ++i)
2038 {
2039 mb_rres[j][i] = iClip1 (p_Vid->max_imgpel_value, rshift_rnd_sf(mb_rres[j][i], DQ_BITS));
2040 img_enc[currMB->pix_y+block_y+j][currMB->pix_x+block_x+i]=(imgpel) mb_rres[j][i];
2041 }
2042 }
2043
2044 }
2045
2046 /*!
2047 ************************************************************************
2048 * \brief Eric Setton
2049 * Encoding of a secondary SP / SI frame.
2050 * For an SI frame the predicted block should only come from spatial pred.
2051 * The original image signal is the error coefficients of a primary SP in the raw data stream
2052 * the difference with the primary SP are :
2053 * - the prediction signal is transformed and quantized (qpsp) but not dequantized
2054 * - only one kind of prediction is considered and not two
2055 * - the resulting error coefficients are not quantized before being sent to the VLC
2056 *
2057 * \par Input:
2058 * block_x,block_y: Block position inside a macro block (0,4,8,12).
2059 *
2060 * \par Output:
2061 * nonzero: 0 if no levels are nonzero. 1 if there are nonzero levels.
2062 * coeff_cost: Counter for nonzero coefficients, used to discard expensive levels.
2063 *
2064 *
2065 ************************************************************************
2066 */
2067
residual_transform_quant_luma_4x4_sp2(Macroblock * currMB,ColorPlane pl,int block_x,int block_y,int * coeff_cost,int intra)2068 int residual_transform_quant_luma_4x4_sp2(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra)
2069 {
2070 int i,j,ilev,coeff_ctr;
2071 int level,scan_pos = 0,run = -1;
2072 int nonzero = FALSE;
2073
2074 Slice *currSlice = currMB->p_Slice;
2075 VideoParameters *p_Vid = currSlice->p_Vid;
2076 QuantParameters *p_Quant = p_Vid->p_Quant;
2077 imgpel **img_enc = p_Vid->enc_picture->p_curr_img;
2078 imgpel **mb_pred = currSlice->mb_pred[pl];
2079 // int **mb_ores = currSlice->mb_ores[pl];
2080 int **mb_rres = currSlice->mb_rres[pl];
2081 int c_err,qp_const2;
2082
2083 int pos_x = block_x >> BLOCK_SHIFT;
2084 int pos_y = block_y >> BLOCK_SHIFT;
2085 int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
2086 int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
2087 const byte *c_cost = COEFF_COST4x4[currSlice->disthres];
2088 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
2089
2090 int level1;
2091
2092 int qp_sp = (currMB->qpsp);
2093
2094 int qp_per_sp = p_Quant->qp_per_matrix[qp_sp];
2095 int q_bits_sp = Q_BITS + qp_per_sp;
2096
2097 //LevelQuantParams **q_params_4x4 = p_Quant->q_params_4x4[pl][intra][qp];
2098 LevelQuantParams **quant_params_sp = p_Quant->q_params_4x4[pl][intra][qp_sp];
2099 QuantMethods quant_methods;
2100
2101 quant_methods.ACLevel = currSlice->cofAC[b8][b4][0];
2102 quant_methods.ACRun = currSlice->cofAC[b8][b4][1];
2103
2104 qp_const2=(1<<q_bits_sp)>>1; //sp_pred
2105
2106 for (j=0; j< BLOCK_SIZE; ++j)
2107 {
2108 for (i=0; i< BLOCK_SIZE; ++i)
2109 {
2110 //Coefficients obtained from the prior encoding of the SP frame
2111 mb_rres[j][i] = p_Vid->lrec[currMB->pix_y+block_y+j][currMB->pix_x+block_x+i];
2112 //Predicted block
2113 currSlice->tblk16x16[j][i]=mb_pred[j+block_y][i+block_x];
2114 }
2115 }
2116 // forward transform
2117 forward4x4(currSlice->tblk16x16, currSlice->tblk16x16, 0, 0);
2118
2119 for (coeff_ctr=0;coeff_ctr < 16;coeff_ctr++) // 8 times if double scan, 16 normal scan
2120 {
2121
2122 i=pos_scan[coeff_ctr][0];
2123 j=pos_scan[coeff_ctr][1];
2124
2125 run++;
2126 ilev=0;
2127
2128 //quantization of the predicted block
2129 level1 = (iabs (currSlice->tblk16x16[j][i]) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp;
2130 //substracted from p_Vid->lrec
2131 c_err = mb_rres[j][i]-isignab(level1, currSlice->tblk16x16[j][i]); //substracting the predicted block
2132
2133 level = iabs(c_err);
2134 if (level != 0)
2135 {
2136 nonzero=TRUE;
2137
2138 *coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
2139
2140 quant_methods.ACLevel[scan_pos] = isignab(level,c_err);
2141 quant_methods.ACRun [scan_pos] = run;
2142 ++scan_pos;
2143 run=-1; // reset zero level counter
2144 }
2145 //from now on we are in decoder land
2146 ilev=c_err + isignab(level1,currSlice->tblk16x16[j][i]) ; // adding the quantized predicted block
2147 mb_rres[j][i] = ilev * (quant_params_sp[j][i].InvScaleComp >> 4) << qp_per_sp;
2148 }
2149 quant_methods.ACLevel[scan_pos] = 0;
2150
2151 // Inverse transform
2152 inverse4x4(mb_rres, mb_rres, 0, 0);
2153
2154 for (j=0; j < BLOCK_SIZE; ++j)
2155 {
2156 for (i=0; i < BLOCK_SIZE; ++i)
2157 {
2158 img_enc[currMB->pix_y+block_y+j][currMB->pix_x+block_x+i] = (imgpel) iClip3 (0, p_Vid->max_imgpel_value,rshift_rnd_sf(mb_rres[j][i], DQ_BITS));
2159 }
2160 }
2161
2162 return nonzero;
2163 }
2164
2165
2166 /*!
2167 ************************************************************************
2168 * \brief Eric Setton
2169 * Encoding of the chroma of a secondary SP / SI frame.
2170 * For an SI frame the predicted block should only come from spatial pred.
2171 * The original image signal is the error coefficients of a primary SP in the raw data stream
2172 * the difference with the primary SP are :
2173 * - the prediction signal is transformed and quantized (qpsp) but not dequantized
2174 * - the resulting error coefficients are not quantized before being sent to the VLC
2175 *
2176 * \par Input:
2177 * uv : Make difference between the U and V chroma component
2178 * cr_cbp: chroma coded block pattern
2179 *
2180 * \par Output:
2181 * cr_cbp: Updated chroma coded block pattern.
2182 *
2183 ************************************************************************
2184 */
residual_transform_quant_chroma_4x4_sp2(Macroblock * currMB,int uv,int cr_cbp)2185 int residual_transform_quant_chroma_4x4_sp2(Macroblock *currMB, int uv,int cr_cbp)
2186 {
2187 int i,j,ilev,n2,n1,coeff_ctr,c_err,level ,scan_pos = 0,run = -1;
2188 int m1[BLOCK_SIZE];
2189 int coeff_cost;
2190 int cr_cbp_tmp;
2191 int mp1[BLOCK_SIZE];
2192 Slice *currSlice = currMB->p_Slice;
2193 VideoParameters *p_Vid = currSlice->p_Vid;
2194 QuantParameters *p_Quant = p_Vid->p_Quant;
2195 QuantMethods quant_methods;
2196 const byte *c_cost = COEFF_COST4x4[currSlice->disthres];
2197 const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
2198
2199 int b4;
2200 int* DCLevel = currSlice->cofDC[uv+1][0];
2201 int* DCRun = currSlice->cofDC[uv+1][1];
2202 int level1;
2203 int **mb_rres = currSlice->mb_rres[uv + 1];
2204 imgpel **mb_pred = currSlice->mb_pred[uv + 1];
2205 int intra = is_intra (currMB);
2206
2207 int qpChroma = currMB->qpc[uv] + currSlice->bitdepth_chroma_qp_scale;
2208
2209 int qpc_sp = iClip3(-currSlice->bitdepth_chroma_qp_scale, 51, currMB->qpsp + p_Vid->active_pps->chroma_qp_index_offset);
2210
2211 int qpChromaSP = qpc_sp < 0 ? qpc_sp : QP_SCALE_CR[qpc_sp];
2212
2213 int qp_per = p_Quant->qp_per_matrix[qpChroma];
2214
2215 int qp_per_sp = p_Quant->qp_per_matrix[qpChromaSP];
2216 int q_bits_sp = Q_BITS + qp_per;
2217 int qp_const2 = (1 << q_bits_sp)>>1; //sp_pred
2218
2219 //LevelQuantParams **q_params_4x4 = p_Quant->q_params_4x4[uv + 1][intra][qpChroma];
2220 LevelQuantParams **quant_params_sp = p_Quant->q_params_4x4[uv + 1][intra][qpChromaSP];
2221
2222 for (j=0; j < MB_BLOCK_SIZE>>1; ++j)
2223 {
2224 for (i=0; i < MB_BLOCK_SIZE>>1; ++i)
2225 {
2226 currSlice->tblk16x16[j][i]=mb_pred[j][i];
2227 mb_rres[j][i]=p_Vid->lrec_uv[uv][currMB->pix_c_y + j][currMB->pix_c_x + i];
2228 }
2229 }
2230
2231
2232 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
2233 {
2234 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
2235 {
2236 forward4x4(currSlice->tblk16x16, currSlice->tblk16x16, n2, n1);
2237 }
2238 }
2239
2240 // DC coefficients already transformed and quantized
2241 m1[0]= mb_rres[0][0];
2242 m1[1]= mb_rres[0][4];
2243 m1[2]= mb_rres[4][0];
2244 m1[3]= mb_rres[4][4];
2245
2246 // 2X2 transform of predicted DC coeffs.
2247 hadamard2x2(currSlice->tblk16x16, mp1);
2248
2249 for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
2250 {
2251 run++;
2252 ilev=0;
2253
2254 //quantization of predicted DC coeff
2255 level1 = (iabs (mp1[coeff_ctr]) * quant_params_sp[0][0].ScaleComp + 2 * qp_const2) >> (q_bits_sp + 1);
2256 //substratcted from lrecUV
2257 c_err = m1[coeff_ctr] - isignab(level1, mp1[coeff_ctr]);
2258 level = iabs(c_err);
2259
2260 if (level != 0)
2261 {
2262 currMB->cbp_blk |= 0xf0000 << (uv << 2) ; // if one of the 2x2-DC levels is != 0 the coded-bit
2263 cr_cbp=imax(1,cr_cbp);
2264 DCLevel[scan_pos] = isignab(level ,c_err);
2265 DCRun [scan_pos] = run;
2266 scan_pos++;
2267 run=-1;
2268 }
2269
2270 //from now on decoder world
2271 ilev = c_err + isignab(level1,mp1[coeff_ctr]) ; // we have perfect reconstruction here
2272
2273 m1[coeff_ctr]= ilev * (quant_params_sp[0][0].InvScaleComp >> 4) << qp_per_sp;
2274
2275 }
2276 DCLevel[scan_pos] = 0;
2277
2278 // Inverse transform of 2x2 DC levels
2279 ihadamard2x2(m1, m1);
2280
2281 mb_rres[0][0]=m1[0]>>1;
2282 mb_rres[0][4]=m1[1]>>1;
2283 mb_rres[4][0]=m1[2]>>1;
2284 mb_rres[4][4]=m1[3]>>1;
2285
2286 // Quant of chroma AC-coeffs.
2287 coeff_cost=0;
2288 cr_cbp_tmp=0;
2289
2290 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
2291 {
2292 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
2293 {
2294 b4 = 2*(n2 >> 2) + (n1 >> 2);
2295 quant_methods.ACLevel = currSlice->cofAC[uv+4][b4][0];
2296 quant_methods.ACRun = currSlice->cofAC[uv+4][b4][1];
2297
2298 run = -1;
2299 scan_pos = 0;
2300
2301 for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)// start change rd_quant
2302 {
2303
2304 i=pos_scan[coeff_ctr][0];
2305 j=pos_scan[coeff_ctr][1];
2306
2307 ++run;
2308 ilev=0;
2309 // quantization on prediction
2310 level1 = (iabs(currSlice->tblk16x16[n2+j][n1+i]) * quant_params_sp[j][i].ScaleComp + qp_const2) >> q_bits_sp;
2311 //substracted from p_Vid->lrec
2312 c_err = mb_rres[n2+j][n1+i] - isignab(level1, currSlice->tblk16x16[n2+j][n1+i]);
2313 level = iabs(c_err) ;
2314
2315 if (level != 0)
2316 {
2317 currMB->cbp_blk |= (int64)1 << (16 + (uv << 2) + ((n2 >> 1) + (n1 >> 2))) ;
2318
2319 coeff_cost += (level > 1) ? MAX_VALUE : c_cost[run];
2320
2321 cr_cbp_tmp=2;
2322 quant_methods.ACLevel[scan_pos] = isignab(level,c_err);
2323 quant_methods.ACRun [scan_pos] = run;
2324 ++scan_pos;
2325 run=-1;
2326 }
2327
2328 //from now on decoder land
2329 ilev=c_err + isignab(level1,currSlice->tblk16x16[n2+j][n1+i]);
2330 mb_rres[n2+j][n1+i] = ilev * (quant_params_sp[j][i].InvScaleComp >> 4) << qp_per_sp;
2331 }
2332 quant_methods.ACLevel[scan_pos] = 0;
2333 }
2334 }
2335 // * reset chroma coeffs
2336
2337 if(cr_cbp_tmp==2)
2338 cr_cbp=2;
2339
2340 // inverse transform.
2341 // Horizontal.
2342 for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
2343 {
2344 for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
2345 {
2346 inverse4x4(mb_rres, mb_rres, n2, n1);
2347
2348 // Vertical.
2349 for (j=0; j < BLOCK_SIZE; ++j)
2350 {
2351 for (i=0; i < BLOCK_SIZE; ++i)
2352 {
2353 p_Vid->enc_picture->imgUV[uv][currMB->pix_c_y + j + n2][currMB->pix_c_x + i + n1 ] =
2354 (imgpel) iClip3 (0, p_Vid->max_imgpel_value,rshift_rnd_sf(mb_rres[n2+j][n1+i], DQ_BITS));
2355 }
2356 }
2357 }
2358 }
2359
2360 return cr_cbp;
2361 }
2362
2363
select_transform(Macroblock * currMB)2364 void select_transform(Macroblock *currMB)
2365 {
2366 Slice *currSlice = currMB->p_Slice;
2367
2368 if (currSlice->slice_type != SP_SLICE && currSlice->slice_type != SI_SLICE)
2369 {
2370 VideoParameters *p_Vid = currMB->p_Vid;
2371 if (p_Vid->active_sps->lossless_qpprime_flag == 1)
2372 {
2373 if (currMB->qp_scaled[(short) p_Vid->colour_plane_id] == 0)
2374 {
2375 currMB->residual_transform_quant_luma_4x4 = residual_transform_quant_luma_4x4_ls;
2376 currMB->residual_transform_quant_luma_16x16 = residual_transform_quant_luma_16x16_ls;
2377 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8_ls;
2378 }
2379 else
2380 {
2381 currMB->residual_transform_quant_luma_4x4 = residual_transform_quant_luma_4x4;
2382 currMB->residual_transform_quant_luma_16x16 = residual_transform_quant_luma_16x16;
2383 if (currSlice->symbol_mode == CAVLC)
2384 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8_cavlc;
2385 else
2386 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8;
2387 currMB->residual_transform_quant_chroma_4x4[0] = residual_transform_quant_chroma_4x4;
2388 currMB->residual_transform_quant_chroma_4x4[1] = residual_transform_quant_chroma_4x4;
2389 }
2390
2391 if (currMB->qp_scaled[PLANE_U] == 0)
2392 {
2393 currMB->residual_transform_quant_chroma_4x4[0] = residual_transform_quant_chroma_4x4_ls;
2394 }
2395 if (currMB->qp_scaled[PLANE_V] == 0)
2396 {
2397 currMB->residual_transform_quant_chroma_4x4[1] = residual_transform_quant_chroma_4x4_ls;
2398 }
2399 }
2400 else
2401 {
2402 currMB->residual_transform_quant_luma_4x4 = residual_transform_quant_luma_4x4;
2403 currMB->residual_transform_quant_luma_16x16 = residual_transform_quant_luma_16x16;
2404 if (currSlice->symbol_mode == CAVLC)
2405 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8_cavlc;
2406 else
2407 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8;
2408
2409 currMB->residual_transform_quant_chroma_4x4[0] = residual_transform_quant_chroma_4x4;
2410 currMB->residual_transform_quant_chroma_4x4[1] = residual_transform_quant_chroma_4x4;
2411 }
2412 }
2413 else if(currSlice->slice_type != SI_SLICE && !currSlice->sp2_frame_indicator)
2414 {
2415 currMB->residual_transform_quant_luma_4x4 = residual_transform_quant_luma_4x4_sp;
2416 currMB->residual_transform_quant_luma_16x16 = residual_transform_quant_luma_16x16;
2417 if (currSlice->symbol_mode == CAVLC)
2418 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8_cavlc;
2419 else
2420 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8;
2421
2422 currMB->residual_transform_quant_chroma_4x4[0] = residual_transform_quant_chroma_4x4_sp;
2423 currMB->residual_transform_quant_chroma_4x4[1] = residual_transform_quant_chroma_4x4_sp;
2424 }
2425 else
2426 {
2427 currMB->residual_transform_quant_luma_4x4 = residual_transform_quant_luma_4x4_sp2;
2428 currMB->residual_transform_quant_luma_16x16 = residual_transform_quant_luma_16x16;
2429 if (currSlice->symbol_mode == CAVLC)
2430 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8_cavlc;
2431 else
2432 currMB->residual_transform_quant_luma_8x8 = residual_transform_quant_luma_8x8;
2433
2434 currMB->residual_transform_quant_chroma_4x4[0] = residual_transform_quant_chroma_4x4_sp2;
2435 currMB->residual_transform_quant_chroma_4x4[1] = residual_transform_quant_chroma_4x4_sp2;
2436 }
2437 }
2438