1 
2 /*!
3  ***********************************************************************
4  *  \file
5  *      block.c
6  *
7  *  \brief
8  *      Block functions
9  *
10  *  \author
11  *      Main contributors (see contributors.h for copyright, address and affiliation details)
12  *      - Inge Lille-Langoy          <inge.lille-langoy@telenor.com>
13  *      - Rickard Sjoberg            <rickard.sjoberg@era.ericsson.se>
14  ***********************************************************************
15  */
16 
17 #include "contributors.h"
18 
19 #include "global.h"
20 #include "block.h"
21 #include "blk_prediction.h"
22 #include "image.h"
23 #include "mb_access.h"
24 #include "transform.h"
25 #include "quant.h"
26 #include "memalloc.h"
27 
28 /*!
29  ***********************************************************************
30  * \brief
31  *    Inverse 4x4 transformation, transforms cof to mb_rres
32  ***********************************************************************
33  */
itrans4x4(Macroblock * currMB,ColorPlane pl,int ioff,int joff)34 void itrans4x4(Macroblock *currMB,   //!< current macroblock
35                ColorPlane pl,        //!< used color plane
36                int ioff,             //!< index to 4x4 block
37                int joff)             //!< index to 4x4 block
38 {
39   Slice *currSlice = currMB->p_Slice;
40   int    **mb_rres = currSlice->mb_rres[pl];
41 
42   inverse4x4(currSlice->cof[pl],mb_rres,joff,ioff);
43 
44   sample_reconstruct (&currSlice->mb_rec[pl][joff], &currSlice->mb_pred[pl][joff], &mb_rres[joff], ioff, ioff, BLOCK_SIZE, BLOCK_SIZE, currMB->p_Vid->max_pel_value_comp[pl], DQ_BITS);
45 }
46 
47 /*!
48  ****************************************************************************
49  * \brief
50  *    Inverse 4x4 lossless_qpprime transformation, transforms cof to mb_rres
51  ****************************************************************************
52  */
itrans4x4_ls(Macroblock * currMB,ColorPlane pl,int ioff,int joff)53 void itrans4x4_ls(Macroblock *currMB,   //!< current macroblock
54                   ColorPlane pl,        //!< Color plane (for 4:4:4)
55                   int ioff,             //!< index to 4x4 block
56                   int joff)             //!< index to 4x4 block
57 {
58   int i,j;
59 
60   Slice *currSlice = currMB->p_Slice;
61   VideoParameters *p_Vid = currMB->p_Vid;
62   int max_imgpel_value = p_Vid->max_pel_value_comp[pl];
63 
64   imgpel **mb_pred = currSlice->mb_pred[pl];
65   imgpel **mb_rec  = currSlice->mb_rec[pl];
66   int    **mb_rres = currSlice->mb_rres [pl];
67 
68   for (j = joff; j < joff + BLOCK_SIZE; ++j)
69   {
70     for (i = ioff; i < ioff + BLOCK_SIZE; ++i)
71     {
72       mb_rec[j][i] = (imgpel) iClip1(max_imgpel_value, mb_pred[j][i] + mb_rres[j][i]);
73     }
74   }
75 }
76 
77 /*!
78 ************************************************************************
79 * \brief
80 *    Inverse residual DPCM for Intra lossless coding
81 *
82 ************************************************************************
83 */
Inv_Residual_trans_4x4(Macroblock * currMB,ColorPlane pl,int ioff,int joff)84 void Inv_Residual_trans_4x4(Macroblock *currMB,   //!< current macroblock
85                             ColorPlane pl,        //!< used color plane
86                             int ioff,             //!< index to 4x4 block
87                             int joff)             //!< index to 4x4 block
88 {
89   int i,j;
90   int temp[4][4];
91   Slice *currSlice = currMB->p_Slice;
92   imgpel **mb_pred = currSlice->mb_pred[pl];
93   imgpel **mb_rec  = currSlice->mb_rec[pl];
94   int    **mb_rres = currSlice->mb_rres[pl];
95   int    **cof     = currSlice->cof[pl];
96 
97   if(currMB->ipmode_DPCM == VERT_PRED)
98   {
99     for(i=0; i<4; ++i)
100     {
101       temp[0][i] = cof[joff + 0][ioff + i];
102       temp[1][i] = cof[joff + 1][ioff + i] + temp[0][i];
103       temp[2][i] = cof[joff + 2][ioff + i] + temp[1][i];
104       temp[3][i] = cof[joff + 3][ioff + i] + temp[2][i];
105     }
106 
107     for(i=0; i<4; ++i)
108     {
109       mb_rres[joff    ][ioff + i]=temp[0][i];
110       mb_rres[joff + 1][ioff + i]=temp[1][i];
111       mb_rres[joff + 2][ioff + i]=temp[2][i];
112       mb_rres[joff + 3][ioff + i]=temp[3][i];
113     }
114   }
115   else if(currMB->ipmode_DPCM == HOR_PRED)
116   {
117     for(j=0; j<4; ++j)
118     {
119       temp[j][0] = cof[joff + j][ioff    ];
120       temp[j][1] = cof[joff + j][ioff + 1] + temp[j][0];
121       temp[j][2] = cof[joff + j][ioff + 2] + temp[j][1];
122       temp[j][3] = cof[joff + j][ioff + 3] + temp[j][2];
123     }
124 
125     for(j=0; j<4; ++j)
126     {
127       mb_rres[joff + j][ioff    ]=temp[j][0];
128       mb_rres[joff + j][ioff + 1]=temp[j][1];
129       mb_rres[joff + j][ioff + 2]=temp[j][2];
130       mb_rres[joff + j][ioff + 3]=temp[j][3];
131     }
132   }
133   else
134   {
135     for (j = joff; j < joff + BLOCK_SIZE; ++j)
136       for (i = ioff; i < ioff + BLOCK_SIZE; ++i)
137         mb_rres[j][i] = cof[j][i];
138   }
139 
140   for (j = joff; j < joff + BLOCK_SIZE; ++j)
141   {
142     for (i = ioff; i < ioff + BLOCK_SIZE; ++i)
143     {
144       mb_rec[j][i] = (imgpel) (mb_rres[j][i] + mb_pred[j][i]);
145     }
146   }
147 }
148 
149 /*!
150 ************************************************************************
151 * \brief
152 *    Inverse residual DPCM for Intra lossless coding
153 *
154 * \par Input:
155 *    ioff_x,joff_y: Block position inside a macro block (0,8).
156 ************************************************************************
157 */
158 //For residual DPCM
Inv_Residual_trans_8x8(Macroblock * currMB,ColorPlane pl,int ioff,int joff)159 void Inv_Residual_trans_8x8(Macroblock *currMB, ColorPlane pl, int ioff,int joff)
160 {
161   Slice *currSlice = currMB->p_Slice;
162   int i, j;
163   int temp[8][8];
164   imgpel **mb_pred = currSlice->mb_pred[pl];
165   imgpel **mb_rec  = currSlice->mb_rec[pl];
166   int    **mb_rres = currSlice->mb_rres[pl];
167 
168   if(currMB->ipmode_DPCM == VERT_PRED)
169   {
170     for(i=0; i<8; ++i)
171     {
172       temp[0][i] = mb_rres[joff + 0][ioff + i];
173       temp[1][i] = mb_rres[joff + 1][ioff + i] + temp[0][i];
174       temp[2][i] = mb_rres[joff + 2][ioff + i] + temp[1][i];
175       temp[3][i] = mb_rres[joff + 3][ioff + i] + temp[2][i];
176       temp[4][i] = mb_rres[joff + 4][ioff + i] + temp[3][i];
177       temp[5][i] = mb_rres[joff + 5][ioff + i] + temp[4][i];
178       temp[6][i] = mb_rres[joff + 6][ioff + i] + temp[5][i];
179       temp[7][i] = mb_rres[joff + 7][ioff + i] + temp[6][i];
180     }
181     for(i=0; i<8; ++i)
182     {
183       mb_rres[joff  ][ioff+i]=temp[0][i];
184       mb_rres[joff+1][ioff+i]=temp[1][i];
185       mb_rres[joff+2][ioff+i]=temp[2][i];
186       mb_rres[joff+3][ioff+i]=temp[3][i];
187       mb_rres[joff+4][ioff+i]=temp[4][i];
188       mb_rres[joff+5][ioff+i]=temp[5][i];
189       mb_rres[joff+6][ioff+i]=temp[6][i];
190       mb_rres[joff+7][ioff+i]=temp[7][i];
191     }
192   }
193   else if(currMB->ipmode_DPCM == HOR_PRED)//HOR_PRED
194   {
195     for(i=0; i<8; ++i)
196     {
197       temp[i][0] = mb_rres[joff + i][ioff + 0];
198       temp[i][1] = mb_rres[joff + i][ioff + 1] + temp[i][0];
199       temp[i][2] = mb_rres[joff + i][ioff + 2] + temp[i][1];
200       temp[i][3] = mb_rres[joff + i][ioff + 3] + temp[i][2];
201       temp[i][4] = mb_rres[joff + i][ioff + 4] + temp[i][3];
202       temp[i][5] = mb_rres[joff + i][ioff + 5] + temp[i][4];
203       temp[i][6] = mb_rres[joff + i][ioff + 6] + temp[i][5];
204       temp[i][7] = mb_rres[joff + i][ioff + 7] + temp[i][6];
205     }
206     for(i=0; i<8; ++i)
207     {
208       mb_rres[joff+i][ioff+0]=temp[i][0];
209       mb_rres[joff+i][ioff+1]=temp[i][1];
210       mb_rres[joff+i][ioff+2]=temp[i][2];
211       mb_rres[joff+i][ioff+3]=temp[i][3];
212       mb_rres[joff+i][ioff+4]=temp[i][4];
213       mb_rres[joff+i][ioff+5]=temp[i][5];
214       mb_rres[joff+i][ioff+6]=temp[i][6];
215       mb_rres[joff+i][ioff+7]=temp[i][7];
216     }
217   }
218 
219   for (j = joff; j < joff + BLOCK_SIZE*2; ++j)
220   {
221     for (i = ioff; i < ioff + BLOCK_SIZE*2; ++i)
222     {
223       mb_rec [j][i]  = (imgpel) (mb_rres[j][i] + mb_pred[j][i]);
224     }
225   }
226 }
227 
228 
229 
230 /*!
231 ************************************************************************
232 * \brief
233 *    Inverse residual DPCM for Intra lossless coding
234 *
235 ************************************************************************
236 */
Inv_Residual_trans_16x16(Macroblock * currMB,ColorPlane pl)237 void Inv_Residual_trans_16x16(Macroblock *currMB,   //!< current macroblock
238                               ColorPlane pl)        //!< used color plane
239 {
240   int i,j;
241   int temp[16][16];
242   Slice *currSlice = currMB->p_Slice;
243   imgpel **mb_pred = currSlice->mb_pred[pl];
244   imgpel **mb_rec  = currSlice->mb_rec[pl];
245   int    **mb_rres = currSlice->mb_rres[pl];
246   int    **cof     = currSlice->cof[pl];
247 
248   if(currMB->ipmode_DPCM == VERT_PRED_16)
249   {
250     for(i=0; i<MB_BLOCK_SIZE; ++i)
251     {
252       temp[0][i] = cof[0][i];
253       for(j = 1; j < MB_BLOCK_SIZE; j++)
254         temp[j][i] = cof[j][i] + temp[j-1][i];
255     }
256 
257     for(i=0; i<MB_BLOCK_SIZE; ++i)
258     {
259       for(j = 0; j < MB_BLOCK_SIZE; j++)
260         mb_rres[j][i]=temp[j][i];
261     }
262   }
263   else if(currMB->ipmode_DPCM == HOR_PRED_16)
264   {
265     for(j=0; j<MB_BLOCK_SIZE; ++j)
266     {
267       temp[j][ 0] = cof[j][ 0  ];
268       for(i = 1; i < MB_BLOCK_SIZE; i++)
269         temp[j][i] = cof[j][i] + temp[j][i-1];
270     }
271 
272     for(j=0; j<MB_BLOCK_SIZE; ++j)
273     {
274       for(i = 0; i < MB_BLOCK_SIZE; ++i)
275         mb_rres[j][i]=temp[j][i];
276     }
277   }
278   else
279   {
280     for (j = 0; j < MB_BLOCK_SIZE; ++j)
281       for (i = 0; i < MB_BLOCK_SIZE; ++i)
282         mb_rres[j][i] = cof[j][i];
283   }
284 
285   for (j = 0; j < MB_BLOCK_SIZE; ++j)
286   {
287     for (i = 0; i < MB_BLOCK_SIZE; ++i)
288     {
289       mb_rec[j][i] = (imgpel) (mb_rres[j][i] + mb_pred[j][i]);
290     }
291   }
292 }
293 
294 
295 /*!
296 ************************************************************************
297 * \brief
298 *    Inverse residual DPCM for Intra lossless coding
299 *
300 ************************************************************************
301 */
Inv_Residual_trans_Chroma(Macroblock * currMB,int uv)302 void Inv_Residual_trans_Chroma(Macroblock *currMB, int uv)
303 {
304   int i, j;
305   int temp[16][16];
306   Slice *currSlice = currMB->p_Slice;
307   //imgpel **mb_pred = currSlice->mb_pred[uv+1];
308   //imgpel **mb_rec  = currSlice->mb_rec[uv+1];
309   int    **mb_rres = currSlice->mb_rres[uv+1];
310   int    **cof     = currSlice->cof[uv+1];
311   int width, height;
312 
313   width = currMB->p_Vid->mb_cr_size_x;
314   height = currMB->p_Vid->mb_cr_size_y;
315 
316   if(currMB->c_ipred_mode == VERT_PRED_8)
317   {
318     for(i=0; i<width; i++)
319     {
320       temp[0][i] = cof[0][i];
321       for(j = 1; j < height; j++)
322         temp[j][i] = temp[j-1][i] + cof[j][i];
323     }
324     for(i=0; i<width; i++)
325     {
326       for(j = 0; j < height; j++)
327         mb_rres[j][i] = temp[j][i];
328     }
329   }
330   else //HOR_PRED_8
331   {
332     for(i=0; i<height; i++)
333     {
334       temp[i][0] = cof[i][0];
335       for(j = 1; j < width; j++)
336         temp[i][j] = temp[i][j-1] + cof[i][j];
337     }
338     for(i=0; i<height; i++)
339     {
340       for(j = 0; j < width; j++)
341         mb_rres[i][j] = temp[i][j];
342     }
343   }
344 }
345 
346 
347 /*!
348  ***********************************************************************
349  * \brief
350  *    Luma DC inverse transform
351  ***********************************************************************
352  */
itrans_2(Macroblock * currMB,ColorPlane pl)353 void itrans_2(Macroblock *currMB,    //!< current macroblock
354               ColorPlane pl)         //!< used color plane
355 {
356   Slice *currSlice = currMB->p_Slice;
357   VideoParameters *p_Vid = currMB->p_Vid;
358   int j;
359 
360   int transform_pl = (p_Vid->separate_colour_plane_flag != 0) ? PLANE_Y : pl;
361   int **cof = currSlice->cof[transform_pl];
362   int qp_scaled = currMB->qp_scaled[transform_pl];
363 
364   int qp_per = p_Vid->qp_per_matrix[ qp_scaled ];
365   int qp_rem = p_Vid->qp_rem_matrix[ qp_scaled ];
366 
367   int invLevelScale = currSlice->InvLevelScale4x4_Intra[pl][qp_rem][0][0];
368   int **M4;
369   get_mem2Dint(&M4, BLOCK_SIZE, BLOCK_SIZE);
370 
371   // horizontal
372   for (j=0; j < 4;++j)
373   {
374     M4[j][0]=cof[j<<2][0];
375     M4[j][1]=cof[j<<2][4];
376     M4[j][2]=cof[j<<2][8];
377     M4[j][3]=cof[j<<2][12];
378   }
379 
380   ihadamard4x4(M4, M4);
381 
382   // vertical
383   for (j=0; j < 4;++j)
384   {
385     cof[j<<2][0]  = rshift_rnd((( M4[j][0] * invLevelScale) << qp_per), 6);
386     cof[j<<2][4]  = rshift_rnd((( M4[j][1] * invLevelScale) << qp_per), 6);
387     cof[j<<2][8]  = rshift_rnd((( M4[j][2] * invLevelScale) << qp_per), 6);
388     cof[j<<2][12] = rshift_rnd((( M4[j][3] * invLevelScale) << qp_per), 6);
389   }
390 
391   free_mem2Dint(M4);
392 }
393 
394 
itrans_sp(Macroblock * currMB,ColorPlane pl,int ioff,int joff)395 void itrans_sp(Macroblock *currMB,   //!< current macroblock
396                ColorPlane pl,        //!< used color plane
397                int ioff,             //!< index to 4x4 block
398                int joff)             //!< index to 4x4 block
399 {
400   VideoParameters *p_Vid = currMB->p_Vid;
401   Slice *currSlice = currMB->p_Slice;
402   int i,j;
403   int ilev, icof;
404 
405   int qp = (currSlice->slice_type == SI_SLICE) ? currSlice->qs : currSlice->qp;
406   int qp_per = p_Vid->qp_per_matrix[ qp ];
407   int qp_rem = p_Vid->qp_rem_matrix[ qp ];
408 
409   int qp_per_sp = p_Vid->qp_per_matrix[ currSlice->qs ];
410   int qp_rem_sp = p_Vid->qp_rem_matrix[ currSlice->qs ];
411   int q_bits_sp = Q_BITS + qp_per_sp;
412 
413   imgpel **mb_pred = currSlice->mb_pred[pl];
414   imgpel **mb_rec  = currSlice->mb_rec[pl];
415   int    **mb_rres = currSlice->mb_rres[pl];
416   int    **cof     = currSlice->cof[pl];
417   int max_imgpel_value = p_Vid->max_pel_value_comp[pl];
418 
419   const int (*InvLevelScale4x4)  [4] = dequant_coef[qp_rem];
420   const int (*InvLevelScale4x4SP)[4] = dequant_coef[qp_rem_sp];
421   int **PBlock;
422 
423   get_mem2Dint(&PBlock, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
424 
425   for (j=0; j< BLOCK_SIZE; ++j)
426   {
427     PBlock[j][0] = mb_pred[j+joff][ioff    ];
428     PBlock[j][1] = mb_pred[j+joff][ioff + 1];
429     PBlock[j][2] = mb_pred[j+joff][ioff + 2];
430     PBlock[j][3] = mb_pred[j+joff][ioff + 3];
431   }
432 
433   forward4x4(PBlock, PBlock, 0, 0);
434 
435   if(currSlice->sp_switch || currSlice->slice_type==SI_SLICE)
436   {
437     for (j=0;j<BLOCK_SIZE;++j)
438     {
439       for (i=0;i<BLOCK_SIZE;++i)
440       {
441         // recovering coefficient since they are already dequantized earlier
442         icof = (cof[joff + j][ioff + i] >> qp_per) / InvLevelScale4x4[j][i];
443         //icof = ((cof[joff + j][ioff + i] * quant_coef[qp_rem][j][i])>> (qp_per + 15)) ;
444         // icof  = rshift_rnd_sf(cof[joff + j][ioff + i] * quant_coef[qp_rem][j][i], qp_per + 15);
445         ilev  = rshift_rnd_sf(iabs(PBlock[j][i]) * quant_coef[qp_rem_sp][j][i], q_bits_sp);
446         ilev  = isignab(ilev, PBlock[j][i]) + icof;
447         cof[joff + j][ioff + i] = ilev * InvLevelScale4x4SP[j][i] << qp_per_sp;
448       }
449     }
450   }
451   else
452   {
453     for (j=0;j<BLOCK_SIZE;++j)
454     {
455       for (i=0;i<BLOCK_SIZE;++i)
456       {
457         // recovering coefficient since they are already dequantized earlier
458         icof = (cof[joff + j][ioff + i] >> qp_per) / InvLevelScale4x4[j][i];
459         //icof = cof[joff + j][ioff + i];
460         //icof  = rshift_rnd_sf(cof[joff + j][ioff + i] * quant_coef[qp_rem][j][i], qp_per + 15);
461         ilev = PBlock[j][i] + ((icof * InvLevelScale4x4[j][i] * A[j][i] <<  qp_per) >> 6);
462         ilev  = isign(ilev) * rshift_rnd_sf(iabs(ilev) * quant_coef[qp_rem_sp][j][i], q_bits_sp);
463         //cof[joff + j][ioff + i] = ilev * InvLevelScale4x4SP[j][i] << qp_per_sp;
464         cof[joff + j][ioff + i] = ilev * InvLevelScale4x4SP[j][i] << qp_per_sp;
465       }
466     }
467   }
468 
469   inverse4x4(cof, mb_rres, joff, ioff);
470 
471   for (j=joff; j<joff +BLOCK_SIZE;++j)
472   {
473     mb_rec[j][ioff   ] = (imgpel) iClip1(max_imgpel_value,rshift_rnd_sf(mb_rres[j][ioff   ], DQ_BITS));
474     mb_rec[j][ioff+ 1] = (imgpel) iClip1(max_imgpel_value,rshift_rnd_sf(mb_rres[j][ioff+ 1], DQ_BITS));
475     mb_rec[j][ioff+ 2] = (imgpel) iClip1(max_imgpel_value,rshift_rnd_sf(mb_rres[j][ioff+ 2], DQ_BITS));
476     mb_rec[j][ioff+ 3] = (imgpel) iClip1(max_imgpel_value,rshift_rnd_sf(mb_rres[j][ioff+ 3], DQ_BITS));
477   }
478 
479   free_mem2Dint(PBlock);
480 }
481 
482 
itrans_sp_cr(Macroblock * currMB,int uv)483 void itrans_sp_cr(Macroblock *currMB, int uv)
484 {
485   Slice *currSlice = currMB->p_Slice;
486   VideoParameters *p_Vid = currMB->p_Vid;
487   int i,j,ilev, icof, n2,n1;
488   int mp1[BLOCK_SIZE];
489   int qp_per,qp_rem;
490   int qp_per_sp,qp_rem_sp,q_bits_sp;
491   imgpel **mb_pred = currSlice->mb_pred[uv + 1];
492   int    **cof = currSlice->cof[uv + 1];
493   int **PBlock = new_mem2Dint(MB_BLOCK_SIZE, MB_BLOCK_SIZE);
494 
495   qp_per    = p_Vid->qp_per_matrix[ ((currSlice->qp < 0 ? currSlice->qp : QP_SCALE_CR[currSlice->qp]))];
496   qp_rem    = p_Vid->qp_rem_matrix[ ((currSlice->qp < 0 ? currSlice->qp : QP_SCALE_CR[currSlice->qp]))];
497 
498   qp_per_sp = p_Vid->qp_per_matrix[ ((currSlice->qs < 0 ? currSlice->qs : QP_SCALE_CR[currSlice->qs]))];
499   qp_rem_sp = p_Vid->qp_rem_matrix[ ((currSlice->qs < 0 ? currSlice->qs : QP_SCALE_CR[currSlice->qs]))];
500   q_bits_sp = Q_BITS + qp_per_sp;
501 
502   if (currSlice->slice_type == SI_SLICE)
503   {
504     qp_per = qp_per_sp;
505     qp_rem = qp_rem_sp;
506   }
507 
508   for (j=0; j < p_Vid->mb_cr_size_y; ++j)
509   {
510     for (i=0; i < p_Vid->mb_cr_size_x; ++i)
511     {
512       PBlock[j][i] = mb_pred[j][i];
513       mb_pred[j][i] = 0;
514     }
515   }
516 
517   for (n2=0; n2 < p_Vid->mb_cr_size_y; n2 += BLOCK_SIZE)
518   {
519     for (n1=0; n1 < p_Vid->mb_cr_size_x; n1 += BLOCK_SIZE)
520     {
521       forward4x4(PBlock, PBlock, n2, n1);
522     }
523   }
524 
525   //     2X2 transform of DC coeffs.
526   mp1[0] = (PBlock[0][0] + PBlock[4][0] + PBlock[0][4] + PBlock[4][4]);
527   mp1[1] = (PBlock[0][0] - PBlock[4][0] + PBlock[0][4] - PBlock[4][4]);
528   mp1[2] = (PBlock[0][0] + PBlock[4][0] - PBlock[0][4] - PBlock[4][4]);
529   mp1[3] = (PBlock[0][0] - PBlock[4][0] - PBlock[0][4] + PBlock[4][4]);
530 
531   if (currSlice->sp_switch || currSlice->slice_type == SI_SLICE)
532   {
533     for (n2=0; n2 < 2; ++n2 )
534     {
535       for (n1=0; n1 < 2; ++n1 )
536       {
537         //quantization fo predicted block
538         ilev = rshift_rnd_sf(iabs (mp1[n1+n2*2]) * quant_coef[qp_rem_sp][0][0], q_bits_sp + 1);
539         //addition
540         ilev = isignab(ilev, mp1[n1+n2*2]) + cof[n2<<2][n1<<2];
541         //dequantization
542         mp1[n1+n2*2] =ilev * dequant_coef[qp_rem_sp][0][0] << qp_per_sp;
543       }
544     }
545 
546     for (n2 = 0; n2 < p_Vid->mb_cr_size_y; n2 += BLOCK_SIZE)
547     {
548       for (n1 = 0; n1 < p_Vid->mb_cr_size_x; n1 += BLOCK_SIZE)
549       {
550         for (j = 0; j < BLOCK_SIZE; ++j)
551         {
552           for (i = 0; i < BLOCK_SIZE; ++i)
553           {
554             // recovering coefficient since they are already dequantized earlier
555             cof[n2 + j][n1 + i] = (cof[n2 + j][n1 + i] >> qp_per) / dequant_coef[qp_rem][j][i];
556 
557             //quantization of the predicted block
558             ilev = rshift_rnd_sf(iabs(PBlock[n2 + j][n1 + i]) * quant_coef[qp_rem_sp][j][i], q_bits_sp);
559             //addition of the residual
560             ilev = isignab(ilev,PBlock[n2 + j][n1 + i]) + cof[n2 + j][n1 + i];
561             // Inverse quantization
562             cof[n2 + j][n1 + i] = ilev * dequant_coef[qp_rem_sp][j][i] << qp_per_sp;
563           }
564         }
565       }
566     }
567   }
568   else
569   {
570     for (n2=0; n2 < 2; ++n2 )
571     {
572       for (n1=0; n1 < 2; ++n1 )
573       {
574         ilev = mp1[n1+n2*2] + (((cof[n2<<2][n1<<2] * dequant_coef[qp_rem][0][0] * A[0][0]) << qp_per) >> 5);
575         ilev = isign(ilev) * rshift_rnd_sf(iabs(ilev) * quant_coef[qp_rem_sp][0][0], q_bits_sp + 1);
576         //ilev = isignab(rshift_rnd_sf(iabs(ilev)* quant_coef[qp_rem_sp][0][0], q_bits_sp + 1), ilev);
577         mp1[n1+n2*2] = ilev * dequant_coef[qp_rem_sp][0][0] << qp_per_sp;
578       }
579     }
580 
581     for (n2 = 0; n2 < p_Vid->mb_cr_size_y; n2 += BLOCK_SIZE)
582     {
583       for (n1 = 0; n1 < p_Vid->mb_cr_size_x; n1 += BLOCK_SIZE)
584       {
585         for (j = 0; j< BLOCK_SIZE; ++j)
586         {
587           for (i = 0; i< BLOCK_SIZE; ++i)
588           {
589             // recovering coefficient since they are already dequantized earlier
590             //icof = ((((cof[n2 + j][n1 + i] << 4) + qp_per/2)>> qp_per) + dequant_coef[qp_rem][j][i]/2) / dequant_coef[qp_rem][j][i];
591             icof = (cof[n2 + j][n1 + i] >> qp_per) / dequant_coef[qp_rem][j][i];
592             //dequantization and addition of the predicted block
593             ilev = PBlock[n2 + j][n1 + i] + ((icof * dequant_coef[qp_rem][j][i] * A[j][i] << qp_per) >> 6);
594             //quantization and dequantization
595             ilev = isign(ilev) * rshift_rnd_sf(iabs(ilev) * quant_coef[qp_rem_sp][j][i], q_bits_sp);
596             cof[n2 + j][n1 + i] = ilev * dequant_coef[qp_rem_sp][j][i] << qp_per_sp;
597             //printf( " %d %d %d\n", j, i, quant_coef[qp_rem_sp][j][i]);
598           }
599         }
600       }
601     }
602   }
603 
604   cof[0][0] = (mp1[0] + mp1[1] + mp1[2] + mp1[3]) >> 1;
605   cof[0][4] = (mp1[0] + mp1[1] - mp1[2] - mp1[3]) >> 1;
606   cof[4][0] = (mp1[0] - mp1[1] + mp1[2] - mp1[3]) >> 1;
607   cof[4][4] = (mp1[0] - mp1[1] - mp1[2] + mp1[3]) >> 1;
608 
609   free_mem2Dint(PBlock);
610 }
611 
iMBtrans4x4(Macroblock * currMB,ColorPlane pl,int smb)612 void iMBtrans4x4(Macroblock *currMB, ColorPlane pl, int smb)
613 {
614   Slice *currSlice = currMB->p_Slice;
615   //VideoParameters *p_Vid = currMB->p_Vid;
616 
617   StorablePicture *dec_picture = currMB->p_Slice->dec_picture;
618   int jj, ii;
619   int block8x8;
620   int k;
621 
622   imgpel **curr_img = pl ? dec_picture->imgUV[pl - 1]: dec_picture->imgY;
623 
624   // =============== 4x4 itrans ================
625   // -------------------------------------------
626   if (currMB->is_lossless && currMB->mb_type == I16MB)
627   {
628     Inv_Residual_trans_16x16(currMB, pl) ;
629   }
630   else if (smb || currMB->is_lossless == TRUE)
631   {
632     currMB->itrans_4x4 = (smb) ? itrans_sp : ((currMB->is_lossless == FALSE) ? itrans4x4 : Inv_Residual_trans_4x4);
633     for (block8x8=0; block8x8 < MB_BLOCK_SIZE; block8x8 += 4)
634     {
635       for (k = block8x8; k < block8x8 + 4; ++k )
636       {
637         jj = ((decode_block_scan[k] >> 2) & 3) << BLOCK_SHIFT;
638         ii = (decode_block_scan[k] & 3) << BLOCK_SHIFT;
639 
640         currMB->itrans_4x4(currMB, pl, ii, jj);   // use integer transform and make 4x4 block mb_rres from prediction block mb_pred
641       }
642     }
643   }
644   else
645   {
646     int **cof = currSlice->cof[pl];
647     int **mb_rres = currSlice->mb_rres[pl];
648 
649     if (currMB->is_intra_block == FALSE)
650     {
651       if (currMB->cbp & 0x01)
652       {
653         inverse4x4(cof, mb_rres, 0, 0);
654         inverse4x4(cof, mb_rres, 0, 4);
655         inverse4x4(cof, mb_rres, 4, 0);
656         inverse4x4(cof, mb_rres, 4, 4);
657       }
658       if (currMB->cbp & 0x02)
659       {
660         inverse4x4(cof, mb_rres, 0, 8);
661         inverse4x4(cof, mb_rres, 0, 12);
662         inverse4x4(cof, mb_rres, 4, 8);
663         inverse4x4(cof, mb_rres, 4, 12);
664       }
665       if (currMB->cbp & 0x04)
666       {
667         inverse4x4(cof, mb_rres, 8, 0);
668         inverse4x4(cof, mb_rres, 8, 4);
669         inverse4x4(cof, mb_rres, 12, 0);
670         inverse4x4(cof, mb_rres, 12, 4);
671       }
672       if (currMB->cbp & 0x08)
673       {
674         inverse4x4(cof, mb_rres, 8, 8);
675         inverse4x4(cof, mb_rres, 8, 12);
676         inverse4x4(cof, mb_rres, 12, 8);
677         inverse4x4(cof, mb_rres, 12, 12);
678       }
679     }
680     else
681     {
682       for (jj = 0; jj < MB_BLOCK_SIZE; jj += BLOCK_SIZE)
683       {
684         inverse4x4(cof, mb_rres, jj, 0);
685         inverse4x4(cof, mb_rres, jj, 4);
686         inverse4x4(cof, mb_rres, jj, 8);
687         inverse4x4(cof, mb_rres, jj, 12);
688       }
689     }
690     sample_reconstruct (currSlice->mb_rec[pl], currSlice->mb_pred[pl], mb_rres, 0, 0, MB_BLOCK_SIZE, MB_BLOCK_SIZE, currMB->p_Vid->max_pel_value_comp[pl], DQ_BITS);
691   }
692 
693   // construct picture from 4x4 blocks
694   copy_image_data_16x16(&curr_img[currMB->pix_y], currSlice->mb_rec[pl], currMB->pix_x, 0);
695 }
696 
iMBtrans8x8(Macroblock * currMB,ColorPlane pl)697 void iMBtrans8x8(Macroblock *currMB, ColorPlane pl)
698 {
699   //VideoParameters *p_Vid = currMB->p_Vid;
700   StorablePicture *dec_picture = currMB->p_Slice->dec_picture;
701   imgpel **curr_img = pl ? dec_picture->imgUV[pl - 1]: dec_picture->imgY;
702 
703   // Perform 8x8 idct
704   if (currMB->cbp & 0x01)
705     itrans8x8(currMB, pl, 0, 0);
706   else
707     icopy8x8(currMB, pl, 0, 0);
708 
709   if (currMB->cbp & 0x02)
710     itrans8x8(currMB, pl, 8, 0);
711   else
712     icopy8x8(currMB, pl, 8, 0);
713 
714   if (currMB->cbp & 0x04)
715     itrans8x8(currMB, pl, 0, 8);
716   else
717     icopy8x8(currMB, pl, 0, 8);
718 
719   if (currMB->cbp & 0x08)
720     itrans8x8(currMB, pl, 8, 8);
721   else
722     icopy8x8(currMB, pl, 8, 8);
723 
724   copy_image_data_16x16(&curr_img[currMB->pix_y], currMB->p_Slice->mb_rec[pl], currMB->pix_x, 0);
725 }
726 
iTransform(Macroblock * currMB,ColorPlane pl,int smb)727 void iTransform(Macroblock *currMB, ColorPlane pl, int smb)
728 {
729   Slice *currSlice = currMB->p_Slice;
730   VideoParameters *p_Vid = currMB->p_Vid;
731   StorablePicture *dec_picture = currSlice->dec_picture;
732   imgpel **curr_img;
733   int uv = pl-1;
734 
735   if ((currMB->cbp & 15) != 0 || smb)
736   {
737     if(currMB->luma_transform_size_8x8_flag == 0) // 4x4 inverse transform
738     {
739       iMBtrans4x4(currMB, pl, smb);
740     }
741     else // 8x8 inverse transform
742     {
743       iMBtrans8x8(currMB, pl);
744     }
745   }
746   else
747   {
748     curr_img = pl ? dec_picture->imgUV[uv] : dec_picture->imgY;
749     copy_image_data_16x16(&curr_img[currMB->pix_y], currSlice->mb_pred[pl], currMB->pix_x, 0);
750   }
751   if(smb)
752     currSlice->is_reset_coeff = FALSE;
753 
754   if ((dec_picture->chroma_format_idc != YUV400) && (dec_picture->chroma_format_idc != YUV444))
755   {
756     imgpel **curUV;
757     int b8;
758     int ioff, joff;
759     imgpel **mb_rec;
760 
761     for(uv = PLANE_U; uv <= PLANE_V; ++uv)
762     {
763       // =============== 4x4 itrans ================
764       // -------------------------------------------
765       curUV = &dec_picture->imgUV[uv - 1][currMB->pix_c_y];
766       mb_rec = currSlice->mb_rec[uv];
767 
768       if (!smb && (currMB->cbp >> 4))
769       {
770         if (currMB->is_lossless == FALSE)
771         {
772           const unsigned char *x_pos, *y_pos;
773 
774           for (b8 = 0; b8 < (p_Vid->num_uv_blocks); ++b8)
775           {
776             x_pos = subblk_offset_x[1][b8];
777             y_pos = subblk_offset_y[1][b8];
778 
779             itrans4x4(currMB, uv, *x_pos++, *y_pos++);
780             itrans4x4(currMB, uv, *x_pos++, *y_pos++);
781             itrans4x4(currMB, uv, *x_pos++, *y_pos++);
782             itrans4x4(currMB, uv, *x_pos  , *y_pos  );
783           }
784           sample_reconstruct (mb_rec, currSlice->mb_pred[uv], currSlice->mb_rres[uv], 0, 0,
785             p_Vid->mb_size[1][0], p_Vid->mb_size[1][1], currMB->p_Vid->max_pel_value_comp[uv], DQ_BITS);
786         }
787         else
788         {
789           const unsigned char *x_pos, *y_pos;
790           for (b8 = 0; b8 < (p_Vid->num_uv_blocks); ++b8)
791           {
792             int i,j;
793             x_pos = subblk_offset_x[1][b8];
794             y_pos = subblk_offset_y[1][b8];
795 
796             for (i = 0 ; i < p_Vid->mb_cr_size_y ; i ++)
797             {
798               for (j = 0 ; j < p_Vid->mb_cr_size_x ; j ++)
799               {
800                 currSlice->mb_rres[uv][i][j] = currSlice->cof[uv][i][j] ;
801               }
802             }
803 
804             itrans4x4_ls(currMB, uv, *x_pos++, *y_pos++);
805             itrans4x4_ls(currMB, uv, *x_pos++, *y_pos++);
806             itrans4x4_ls(currMB, uv, *x_pos++, *y_pos++);
807             itrans4x4_ls(currMB, uv, *x_pos  , *y_pos  );
808           }
809         }
810         copy_image_data(curUV, mb_rec, currMB->pix_c_x, 0, p_Vid->mb_size[1][0], p_Vid->mb_size[1][1]);
811 
812         currSlice->is_reset_coeff_cr = FALSE;
813       }
814       else if (smb)
815       {
816         currMB->itrans_4x4 = (currMB->is_lossless == FALSE) ? itrans4x4 : itrans4x4_ls;
817         itrans_sp_cr(currMB, uv - 1);
818 
819         for (joff = 0; joff < p_Vid->mb_cr_size_y; joff += BLOCK_SIZE)
820         {
821           for(ioff = 0; ioff < p_Vid->mb_cr_size_x ;ioff += BLOCK_SIZE)
822           {
823             currMB->itrans_4x4(currMB, uv, ioff, joff);
824           }
825         }
826 
827         copy_image_data(curUV, mb_rec, currMB->pix_c_x, 0, p_Vid->mb_size[1][0], p_Vid->mb_size[1][1]);
828         currSlice->is_reset_coeff_cr = FALSE;
829       }
830       else
831       {
832         copy_image_data(curUV, currSlice->mb_pred[uv], currMB->pix_c_x, 0, p_Vid->mb_size[1][0], p_Vid->mb_size[1][1]);
833       }
834     }
835   }
836 }
837 
838 /*!
839  *************************************************************************************
840  * \brief
841  *    Copy ImgPel Data from one structure to another (16x16)
842  *************************************************************************************
843  */
copy_image_data_16x16(imgpel ** imgBuf1,imgpel ** imgBuf2,int off1,int off2)844 void copy_image_data_16x16(imgpel  **imgBuf1, imgpel  **imgBuf2, int off1, int off2)
845 {
846   int j;
847   for(j = 0; j < MB_BLOCK_SIZE; j += 4)
848   {
849     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), MB_BLOCK_SIZE * sizeof (imgpel));
850     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), MB_BLOCK_SIZE * sizeof (imgpel));
851     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), MB_BLOCK_SIZE * sizeof (imgpel));
852     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), MB_BLOCK_SIZE * sizeof (imgpel));
853   }
854 }
855 
856 /*!
857  *************************************************************************************
858  * \brief
859  *    Copy ImgPel Data from one structure to another (8x8)
860  *************************************************************************************
861  */
copy_image_data_8x8(imgpel ** imgBuf1,imgpel ** imgBuf2,int off1,int off2)862 void copy_image_data_8x8(imgpel  **imgBuf1, imgpel  **imgBuf2, int off1, int off2)
863 {
864   int j;
865   for(j = 0; j < BLOCK_SIZE_8x8; j+=4)
866   {
867     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE_8x8 * sizeof (imgpel));
868     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE_8x8 * sizeof (imgpel));
869     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE_8x8 * sizeof (imgpel));
870     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE_8x8 * sizeof (imgpel));
871   }
872 }
873 
874 
875 /*!
876  *************************************************************************************
877  * \brief
878  *    Copy ImgPel Data from one structure to another (4x4)
879  *************************************************************************************
880  */
copy_image_data_4x4(imgpel ** imgBuf1,imgpel ** imgBuf2,int off1,int off2)881 void copy_image_data_4x4(imgpel  **imgBuf1, imgpel  **imgBuf2, int off1, int off2)
882 {
883   memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE * sizeof (imgpel));
884   memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE * sizeof (imgpel));
885   memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), BLOCK_SIZE * sizeof (imgpel));
886   memcpy((*imgBuf1   + off1), (*imgBuf2   + off2), BLOCK_SIZE * sizeof (imgpel));
887 }
888 
CheckVertMV(Macroblock * currMB,int vec1_y,int block_size_y)889 int CheckVertMV(Macroblock *currMB, int vec1_y, int block_size_y)
890 {
891   VideoParameters *p_Vid = currMB->p_Vid;
892   StorablePicture *dec_picture = currMB->p_Slice->dec_picture;
893   int y_pos = vec1_y>>2;
894   int maxold_y = (currMB->mb_field) ? (dec_picture->size_y >> 1) - 1 : dec_picture->size_y_m1;
895 
896   if(y_pos < (-p_Vid->iLumaPadY + 2) || y_pos > (maxold_y + p_Vid->iLumaPadY - block_size_y - 2))
897     return 1;
898   else
899     return 0;
900 }
901 
902 
903 /*!
904  *************************************************************************************
905  * \brief
906  *    Copy ImgPel Data from one structure to another (8x8)
907  *************************************************************************************
908  */
copy_image_data(imgpel ** imgBuf1,imgpel ** imgBuf2,int off1,int off2,int width,int height)909 void copy_image_data(imgpel  **imgBuf1, imgpel  **imgBuf2, int off1, int off2, int width, int height)
910 {
911   int j;
912   for(j = 0; j < height; ++j)
913   {
914     memcpy((*imgBuf1++ + off1), (*imgBuf2++ + off2), width * sizeof (imgpel));
915   }
916 }
917