OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [test/] [decoder/] [ldecod/] [src/] [macroblock.c] - Blame information for rev 100

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jamey.hick
 
2
/*!
3
 ***********************************************************************
4
 * \file macroblock.c
5
 *
6
 * \brief
7
 *     Decode a Macroblock
8
 *
9
 * \author
10
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
11
 *    - Inge Lille-Langøy               <inge.lille-langoy@telenor.com>
12
 *    - Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
13
 *    - Jani Lainema                    <jani.lainema@nokia.com>
14
 *    - Sebastian Purreiter             <sebastian.purreiter@mch.siemens.de>
15
 *    - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
16
 *    - Detlev Marpe                    <marpe@hhi.de>
17
 *    - Gabi Blaettermann               <blaetter@hhi.de>
18
 *    - Ye-Kui Wang                     <wyk@ieee.org>
19
 *    - Lowell Winger                   <lwinger@lsil.com>
20
 ***********************************************************************
21
*/
22
 
23
#include "contributors.h"
24
 
25
#include <math.h>
26
#include <stdlib.h>
27
#include <assert.h>
28
#include <string.h>
29
 
30
#include "global.h"
31
#include "mbuffer.h"
32
#include "elements.h"
33
#include "errorconcealment.h"
34
#include "macroblock.h"
35
#include "fmo.h"
36
#include "cabac.h"
37
#include "vlc.h"
38
#include "image.h"
39
#include "mb_access.h"
40
#include "biaridecod.h"
41
 
42
#include "transform8x8.h"
43
 
44
#if TRACE
45
#define TRACE_STRING(s) strncpy(currSE.tracestring, s, TRACESTRING_SIZE)
46
#else
47
#define TRACE_STRING(s) // do nothing
48
#endif
49
 
50
extern int last_dquant;
51
extern ColocatedParams *Co_located;
52
 
53
 
54
static void SetMotionVectorPredictor (struct img_par  *img,
55
                                      short           pmv[2],
56
                                      char            ref_frame,
57
                                      byte            list,
58
                                      char            ***refPic,
59
                                      short           ****tmp_mv,
60
                                      int             block_x,
61
                                      int             block_y,
62
                                      int             blockshape_x,
63
                                      int             blockshape_y);
64
 
65
 
66
/*!
67
 ************************************************************************
68
 * \brief
69
 *    initializes the current macroblock
70
 ************************************************************************
71
 */
72
void start_macroblock(struct img_par *img,int CurrentMBInScanOrder)
73
{
74
  int l,j;
75
  Macroblock *currMB;   // intialization code deleted, see below, StW
76
 
77
  assert (img->current_mb_nr < img->PicSizeInMbs);
78
 
79
  currMB = &img->mb_data[img->current_mb_nr];
80
 
81
  /* Update coordinates of the current macroblock */
82
  if (img->MbaffFrameFlag)
83
  {
84
    img->mb_x = (img->current_mb_nr)%((2*img->width)/MB_BLOCK_SIZE);
85
    img->mb_y = 2*((img->current_mb_nr)/((2*img->width)/MB_BLOCK_SIZE));
86
 
87
    if (img->mb_x & 0x01)
88
    {
89
      img->mb_y++;
90
    }
91
 
92
    img->mb_x /= 2;
93
  }
94
  else
95
  {
96
    img->mb_x = PicPos[img->current_mb_nr][0];
97
    img->mb_y = PicPos[img->current_mb_nr][1];
98
  }
99
 
100
  /* Define vertical positions */
101
  img->block_y = img->mb_y * BLOCK_SIZE;      /* luma block position */
102
  img->pix_y   = img->mb_y * MB_BLOCK_SIZE;   /* luma macroblock position */
103
  img->pix_c_y = img->mb_y * img->mb_cr_size_y; /* chroma macroblock position */
104
 
105
  /* Define horizontal positions */
106
  img->block_x = img->mb_x * BLOCK_SIZE;      /* luma block position */
107
  img->pix_x   = img->mb_x * MB_BLOCK_SIZE;   /* luma pixel position */
108
  img->pix_c_x = img->mb_x * img->mb_cr_size_x; /* chroma pixel position */
109
 
110
  // Save the slice number of this macroblock. When the macroblock below
111
  // is coded it will use this to decide if prediction for above is possible
112
  currMB->slice_nr = img->current_slice_nr;
113
 
114
  if (img->current_slice_nr >= MAX_NUM_SLICES)
115
  {
116
    error ("maximum number of supported slices exceeded, please recompile with increased value for MAX_NUM_SLICES", 200);
117
  }
118
 
119
  dec_picture->slice_id[img->mb_y][img->mb_x] = img->current_slice_nr;
120
  if (img->current_slice_nr > dec_picture->max_slice_id)
121
  {
122
    dec_picture->max_slice_id=img->current_slice_nr;
123
  }
124
 
125
  CheckAvailabilityOfNeighbors();
126
 
127
  // Reset syntax element entries in MB struct
128
  currMB->qp          = img->qp ;
129
  currMB->mb_type     = 0;
130
  currMB->delta_quant = 0;
131
  currMB->cbp         = 0;
132
  currMB->cbp_blk     = 0;
133
  currMB->c_ipred_mode= DC_PRED_8; //GB
134
 
135
  for (l=0; l < 2; l++)
136
    for (j=0; j < BLOCK_MULTIPLE; j++)
137
      memset(&(currMB->mvd[l][j][0][0]),0, BLOCK_MULTIPLE * 2 * sizeof(int));
138
 
139
  currMB->cbp_bits   = 0;
140
 
141
  // initialize img->m7
142
  memset(&(img->m7[0][0]), 0, MB_BLOCK_PIXELS * sizeof(int));
143
 
144
  // store filtering parameters for this MB
145
  currMB->LFDisableIdc = img->currentSlice->LFDisableIdc;
146
  currMB->LFAlphaC0Offset = img->currentSlice->LFAlphaC0Offset;
147
  currMB->LFBetaOffset = img->currentSlice->LFBetaOffset;
148
 
149
}
150
 
151
/*!
152
 ************************************************************************
153
 * \brief
154
 *    set coordinates of the next macroblock
155
 *    check end_of_slice condition
156
 ************************************************************************
157
 */
158
Boolean exit_macroblock(struct img_par *img,struct inp_par *inp,int eos_bit)
159
{
160
 //! The if() statement below resembles the original code, which tested
161
  //! img->current_mb_nr == img->PicSizeInMbs.  Both is, of course, nonsense
162
  //! In an error prone environment, one can only be sure to have a new
163
  //! picture by checking the tr of the next slice header!
164
 
165
// printf ("exit_macroblock: FmoGetLastMBOfPicture %d, img->current_mb_nr %d\n", FmoGetLastMBOfPicture(), img->current_mb_nr);
166
  img->num_dec_mb++;
167
 
168
  if (img->num_dec_mb == img->PicSizeInMbs)
169
//  if (img->current_mb_nr == FmoGetLastMBOfPicture(currSlice->structure))
170
  {
171
//thb
172
/*
173
    if (currSlice->next_header != EOS)
174
      currSlice->next_header = SOP;
175
*/
176
//the
177
//    assert (nal_startcode_follows (img, eos_bit) == TRUE);
178
    return TRUE;
179
  }
180
  // ask for last mb in the slice  UVLC
181
  else
182
  {
183
// printf ("exit_macroblock: Slice %d old MB %d, now using MB %d\n", img->current_slice_nr, img->current_mb_nr, FmoGetNextMBNr (img->current_mb_nr));
184
 
185
    img->current_mb_nr = FmoGetNextMBNr (img->current_mb_nr);
186
 
187
    if (img->current_mb_nr == -1)     // End of Slice group, MUST be end of slice
188
    {
189
      assert (nal_startcode_follows (img, eos_bit) == TRUE);
190
      return TRUE;
191
    }
192
 
193
    if(nal_startcode_follows(img, eos_bit) == FALSE)
194
      return FALSE;
195
 
196
    if(img->type == I_SLICE  || img->type == SI_SLICE || active_pps->entropy_coding_mode_flag == CABAC)
197
      return TRUE;
198
    if(img->cod_counter<=0)
199
      return TRUE;
200
    return FALSE;
201
  }
202
}
203
 
204
/*!
205
 ************************************************************************
206
 * \brief
207
 *    Interpret the mb mode for P-Frames
208
 ************************************************************************
209
 */
210
void interpret_mb_mode_P(struct img_par *img)
211
{
212
  const int ICBPTAB[6] = {0,16,32,15,31,47};
213
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
214
  int         mbmode = currMB->mb_type;
215
 
216
#define ZERO_P8x8     (mbmode==5)
217
#define MODE_IS_P8x8  (mbmode==4 || mbmode==5)
218
#define MODE_IS_I4x4  (mbmode==6)
219
#define I16OFFSET     (mbmode-7)
220
#define MODE_IS_IPCM  (mbmode==31)
221
 
222
  if(mbmode <4)
223
  {
224
    currMB->mb_type = mbmode;
225
    memset(&currMB->b8mode[0],mbmode,4 * sizeof(char));
226
    memset(&currMB->b8pdir[0],0,4 * sizeof(char));
227
  }
228
  else if(MODE_IS_P8x8)
229
  {
230
    currMB->mb_type = P8x8;
231
    img->allrefzero = ZERO_P8x8;
232
  }
233
  else if(MODE_IS_I4x4)
234
  {
235
    currMB->mb_type = I4MB;
236
    memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
237
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
238
  }
239
  else if(MODE_IS_IPCM)
240
  {
241
    currMB->mb_type=IPCM;
242
    currMB->cbp= -1;
243
    currMB->i16mode = 0;
244
 
245
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
246
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
247
  }
248
  else
249
  {
250
    currMB->mb_type = I16MB;
251
    currMB->cbp= ICBPTAB[(I16OFFSET)>>2];
252
    currMB->i16mode = (I16OFFSET) & 0x03;
253
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
254
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
255
  }
256
}
257
 
258
/*!
259
 ************************************************************************
260
 * \brief
261
 *    Interpret the mb mode for I-Frames
262
 ************************************************************************
263
 */
264
void interpret_mb_mode_I(struct img_par *img)
265
{
266
  const int ICBPTAB[6] = {0,16,32,15,31,47};
267
  Macroblock *currMB   = &img->mb_data[img->current_mb_nr];
268
  int         mbmode   = currMB->mb_type;
269
 
270
  if (mbmode==0)
271
  {
272
    currMB->mb_type = I4MB;
273
    memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
274
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
275
  }
276
  else if(mbmode==25)
277
  {
278
    currMB->mb_type=IPCM;
279
    currMB->cbp= -1;
280
    currMB->i16mode = 0;
281
 
282
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
283
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
284
  }
285
  else
286
  {
287
    currMB->mb_type = I16MB;
288
    currMB->cbp= ICBPTAB[(mbmode-1)>>2];
289
    currMB->i16mode = (mbmode-1) & 0x03;
290
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
291
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
292
  }
293
}
294
 
295
/*!
296
 ************************************************************************
297
 * \brief
298
 *    Interpret the mb mode for B-Frames
299
 ************************************************************************
300
 */
301
void interpret_mb_mode_B(struct img_par *img)
302
{
303
  static const int offset2pdir16x16[12]   = {0, 0, 1, 2, 0,0,0,0,0,0,0,0};
304
  static const int offset2pdir16x8[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},{1,0},
305
                                             {0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2},{0,0}};
306
  static const int offset2pdir8x16[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},
307
                                             {1,0},{0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2}};
308
 
309
  const int ICBPTAB[6] = {0,16,32,15,31,47};
310
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
311
 
312
  int i, mbmode;
313
  int mbtype  = currMB->mb_type;
314
 
315
  //--- set mbtype, b8type, and b8pdir ---
316
  if (mbtype==0)       // direct
317
  {
318
    mbmode=0;
319
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
320
    memset(&currMB->b8pdir[0],2,4 * sizeof(char));
321
  }
322
  else if (mbtype==23) // intra4x4
323
  {
324
    mbmode=I4MB;
325
    memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
326
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
327
  }
328
  else if ((mbtype>23) && (mbtype<48) ) // intra16x16
329
  {
330
    mbmode=I16MB;
331
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
332
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
333
 
334
    currMB->cbp     = ICBPTAB[(mbtype-24)>>2];
335
    currMB->i16mode = (mbtype-24) & 0x03;
336
  }
337
  else if (mbtype==22) // 8x8(+split)
338
  {
339
    mbmode=P8x8;       // b8mode and pdir is transmitted in additional codewords
340
  }
341
  else if (mbtype<4)   // 16x16
342
  {
343
    mbmode=1;
344
    memset(&currMB->b8mode[0], 1,4 * sizeof(char));
345
    memset(&currMB->b8pdir[0],offset2pdir16x16[mbtype],4 * sizeof(char));
346
  }
347
  else if(mbtype==48)
348
  {
349
    mbmode=IPCM;
350
    memset(&currMB->b8mode[0], 0,4 * sizeof(char));
351
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
352
 
353
    currMB->cbp= -1;
354
    currMB->i16mode = 0;
355
  }
356
 
357
  else if ((mbtype&0x01)==0) // 16x8
358
  {
359
    mbmode=2;
360
    memset(&currMB->b8mode[0], 2,4 * sizeof(char));
361
    for(i=0;i<4;i++)
362
    {
363
      currMB->b8pdir[i]=offset2pdir16x8 [mbtype][i>>1];
364
    }
365
  }
366
  else
367
  {
368
    mbmode=3;
369
    memset(&currMB->b8mode[0], 3,4 * sizeof(char));
370
    for(i=0;i<4;i++)
371
    {
372
      currMB->b8pdir[i]=offset2pdir8x16 [mbtype][i&0x01];
373
    }
374
  }
375
  currMB->mb_type = mbmode;
376
}
377
/*!
378
 ************************************************************************
379
 * \brief
380
 *    Interpret the mb mode for SI-Frames
381
 ************************************************************************
382
 */
383
void interpret_mb_mode_SI(struct img_par *img)
384
{
385
  const int ICBPTAB[6] = {0,16,32,15,31,47};
386
  Macroblock *currMB   = &img->mb_data[img->current_mb_nr];
387
  int         mbmode   = currMB->mb_type;
388
 
389
  if (mbmode==0)
390
  {
391
    currMB->mb_type = SI4MB;
392
    memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
393
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
394
    img->siblock[img->mb_y][img->mb_x]=1;
395
  }
396
  else if (mbmode==1)
397
  {
398
    currMB->mb_type = I4MB;
399
    memset(&currMB->b8mode[0],IBLOCK,4 * sizeof(char));
400
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
401
  }
402
  else if(mbmode==26)
403
  {
404
    currMB->mb_type=IPCM;
405
    currMB->cbp= -1;
406
    currMB->i16mode = 0;
407
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
408
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
409
  }
410
 
411
  else
412
  {
413
    currMB->mb_type = I16MB;
414
    currMB->cbp= ICBPTAB[(mbmode-1)>>2];
415
    currMB->i16mode = (mbmode-2) & 0x03;
416
    memset(&currMB->b8mode[0],0,4 * sizeof(char));
417
    memset(&currMB->b8pdir[0],-1,4 * sizeof(char));
418
  }
419
}
420
/*!
421
 ************************************************************************
422
 * \brief
423
 *    init macroblock I and P frames
424
 ************************************************************************
425
 */
426
void init_macroblock(struct img_par *img)
427
{
428
  int i,j;
429
 
430
  for(j=img->block_y;j<img->block_y+BLOCK_SIZE;j++)
431
  {                           // reset vectors and pred. modes
432
    memset(&dec_picture->mv[LIST_0][j][img->block_x][0], 0, 2 * BLOCK_SIZE * sizeof(short));
433
    memset(&dec_picture->mv[LIST_1][j][img->block_x][0], 0, 2 * BLOCK_SIZE * sizeof(short));
434
    memset(&dec_picture->ref_idx[LIST_0][j][img->block_x], -1, BLOCK_SIZE * sizeof(char));
435
    memset(&dec_picture->ref_idx[LIST_1][j][img->block_x], -1, BLOCK_SIZE * sizeof(char));
436
    memset(&img->ipredmode[j][img->block_x], DC_PRED, BLOCK_SIZE * sizeof(char));
437
    for (i=img->block_x;i<img->block_x+BLOCK_SIZE;i++)
438
    {
439
      dec_picture->ref_pic_id[LIST_0][j][i] = INT64_MIN;
440
      dec_picture->ref_pic_id[LIST_1][j][i] = INT64_MIN;
441
    }
442
  }
443
}
444
 
445
 
446
/*!
447
 ************************************************************************
448
 * \brief
449
 *    Sets mode for 8x8 block
450
 ************************************************************************
451
 */
452
void SetB8Mode (struct img_par* img, Macroblock* currMB, int value, int i)
453
{
454
  static const int p_v2b8 [ 5] = {4, 5, 6, 7, IBLOCK};
455
  static const int p_v2pd [ 5] = {0, 0, 0, 0, -1};
456
  static const int b_v2b8 [14] = {0, 4, 4, 4, 5, 6, 5, 6, 5, 6, 7, 7, 7, IBLOCK};
457
  static const int b_v2pd [14] = {2, 0, 1, 2, 0, 0, 1, 1, 2, 2, 0, 1, 2, -1};
458
 
459
  if (img->type==B_SLICE)
460
  {
461
    currMB->b8mode[i]   = b_v2b8[value];
462
    currMB->b8pdir[i]   = b_v2pd[value];
463
 
464
  }
465
  else
466
  {
467
    currMB->b8mode[i]   = p_v2b8[value];
468
    currMB->b8pdir[i]   = p_v2pd[value];
469
  }
470
 
471
}
472
 
473
 
474
void reset_coeffs()
475
{
476
  int i, j;
477
 
478
  // reset all coeffs
479
  for (i=0;i<BLOCK_SIZE;i++)
480
  {
481
    for (j=0;j<BLOCK_SIZE +img->num_blk8x8_uv;j++)
482
      memset(&img->cof[i][j][0][0],0,BLOCK_SIZE * BLOCK_SIZE * sizeof(int));
483
  }
484
 
485
  // CAVLC
486
  memset(&img->nz_coeff[img->current_mb_nr][0][0],0, BLOCK_SIZE * (BLOCK_SIZE + img->num_blk8x8_uv) * sizeof(int));
487
}
488
 
489
void field_flag_inference()
490
{
491
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
492
 
493
  if (currMB->mbAvailA)
494
  {
495
    currMB->mb_field = img->mb_data[currMB->mbAddrA].mb_field;
496
  }
497
  else
498
  {
499
    // check top macroblock pair
500
    if (currMB->mbAvailB)
501
    {
502
      currMB->mb_field = img->mb_data[currMB->mbAddrB].mb_field;
503
    }
504
    else
505
      currMB->mb_field = 0;
506
  }
507
 
508
}
509
 
510
void set_chroma_qp(Macroblock* currMB)
511
{
512
  int i;
513
  for (i=0; i<2; i++)
514
  {
515
    currMB->qpc[i] = iClip3 ( -img->bitdepth_chroma_qp_scale, 51, currMB->qp + dec_picture->chroma_qp_offset[i] );
516
    currMB->qpc[i] = currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]];
517
  }
518
}
519
 
520
 
521
/*!
522
 ************************************************************************
523
 * \brief
524
 *    Get the syntax elements from the NAL
525
 ************************************************************************
526
 */
527
int read_one_macroblock(struct img_par *img,struct inp_par *inp)
528
{
529
  int i;
530
 
531
  SyntaxElement currSE;
532
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
533
 
534
  Slice *currSlice = img->currentSlice;
535
  DataPartition *dP;
536
  int *partMap = assignSE2partition[currSlice->dp_mode];
537
  Macroblock *topMB = NULL;
538
  int  prevMbSkipped = 0;
539
  int  img_block_y;
540
  int  check_bottom, read_bottom, read_top;
541
 
542
  if (img->MbaffFrameFlag)
543
  {
544
    if (img->current_mb_nr&0x01)
545
    {
546
      topMB= &img->mb_data[img->current_mb_nr-1];
547
      if(!(img->type == B_SLICE))
548
        prevMbSkipped = (topMB->mb_type == 0);
549
      else
550
        prevMbSkipped = topMB->skip_flag;
551
    }
552
    else
553
      prevMbSkipped = 0;
554
  }
555
 
556
  if ((img->current_mb_nr&0x01) == 0)
557
    currMB->mb_field = 0;
558
  else
559
    currMB->mb_field = img->mb_data[img->current_mb_nr-1].mb_field;
560
 
561
 
562
  currMB->qp = img->qp ;
563
  for (i=0; i<2; i++)
564
  {
565
    currMB->qpc[i] = iClip3 ( -img->bitdepth_chroma_qp_scale, 51, img->qp + dec_picture->chroma_qp_offset[i] );
566
    currMB->qpc[i] = currMB->qpc[i] < 0 ? currMB->qpc[i] : QP_SCALE_CR[currMB->qpc[i]];
567
  }
568
 
569
  currSE.type = SE_MBTYPE;
570
 
571
  //  read MB mode *****************************************************************
572
  dP = &(currSlice->partArr[partMap[currSE.type]]);
573
 
574
  if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)   currSE.mapping = linfo_ue;
575
 
576
  if(img->type == I_SLICE || img->type == SI_SLICE)
577
  {
578
    // read MB aff
579
    if (img->MbaffFrameFlag && (img->current_mb_nr&0x01)==0)
580
    {
581
      TRACE_STRING("mb_field_decoding_flag");
582
      if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
583
      {
584
        currSE.len = 1;
585
        readSyntaxElement_FLC(&currSE, dP->bitstream);
586
      }
587
      else
588
      {
589
        currSE.reading = readFieldModeInfo_CABAC;
590
        dP->readSyntaxElement(&currSE,img,dP);
591
      }
592
      currMB->mb_field = currSE.value1;
593
    }
594
    if(active_pps->entropy_coding_mode_flag  == CABAC)
595
      CheckAvailabilityOfNeighborsCABAC();
596
 
597
    //  read MB type
598
    TRACE_STRING("mb_type");
599
    currSE.reading = readMB_typeInfo_CABAC;
600
    dP->readSyntaxElement(&currSE,img,dP);
601
 
602
    currMB->mb_type = currSE.value1;
603
    if(!dP->bitstream->ei_flag)
604
      currMB->ei_flag = 0;
605
  }
606
  // non I/SI-slice CABAC
607
  else if (active_pps->entropy_coding_mode_flag == CABAC)
608
  {
609
    // read MB skip_flag
610
    if (img->MbaffFrameFlag && ((img->current_mb_nr&0x01) == 0||prevMbSkipped))
611
      field_flag_inference();
612
 
613
    CheckAvailabilityOfNeighborsCABAC();
614
    TRACE_STRING("mb_skip_flag");
615
    currSE.reading = readMB_skip_flagInfo_CABAC;
616
    dP->readSyntaxElement(&currSE,img,dP);
617
 
618
    currMB->mb_type   = currSE.value1;
619
    currMB->skip_flag = !(currSE.value1);
620
 
621
    if (img->type==B_SLICE)
622
      currMB->cbp = currSE.value2;
623
 
624
    if(!dP->bitstream->ei_flag)
625
      currMB->ei_flag = 0;
626
 
627
    if ((img->type==B_SLICE) && currSE.value1==0 && currSE.value2==0)
628
      img->cod_counter=0;
629
 
630
    // read MB AFF
631
    if (img->MbaffFrameFlag)
632
    {
633
      check_bottom=read_bottom=read_top=0;
634
      if ((img->current_mb_nr&0x01)==0)
635
      {
636
        check_bottom =  currMB->skip_flag;
637
        read_top = !check_bottom;
638
      }
639
      else
640
      {
641
        read_bottom = (topMB->skip_flag && (!currMB->skip_flag));
642
       }
643
 
644
      if (read_bottom || read_top)
645
      {
646
        TRACE_STRING("mb_field_decoding_flag");
647
        currSE.reading = readFieldModeInfo_CABAC;
648
        dP->readSyntaxElement(&currSE,img,dP);
649
        currMB->mb_field = currSE.value1;
650
      }
651
      if (check_bottom)
652
        check_next_mb_and_get_field_mode_CABAC(&currSE,img,dP);
653
 
654
    }
655
 
656
    CheckAvailabilityOfNeighborsCABAC();
657
 
658
    // read MB type
659
    if (currMB->mb_type != 0 )
660
    {
661
      currSE.reading = readMB_typeInfo_CABAC;
662
      TRACE_STRING("mb_type");
663
      dP->readSyntaxElement(&currSE,img,dP);
664
      currMB->mb_type = currSE.value1;
665
      if(!dP->bitstream->ei_flag)
666
        currMB->ei_flag = 0;
667
    }
668
  }
669
  // VLC Non-Intra
670
  else
671
  {
672
    if(img->cod_counter == -1)
673
    {
674
      TRACE_STRING("mb_skip_run");
675
      dP->readSyntaxElement(&currSE,img,dP);
676
      img->cod_counter = currSE.value1;
677
    }
678
    if (img->cod_counter==0)
679
    {
680
      // read MB aff
681
      if ((img->MbaffFrameFlag) && (((img->current_mb_nr&0x01)==0) || ((img->current_mb_nr&0x01) && prevMbSkipped)))
682
      {
683
        TRACE_STRING("mb_field_decoding_flag");
684
        currSE.len = 1;
685
        readSyntaxElement_FLC(&currSE, dP->bitstream);
686
        currMB->mb_field = currSE.value1;
687
      }
688
 
689
      // read MB type
690
      TRACE_STRING("mb_type");
691
      dP->readSyntaxElement(&currSE,img,dP);
692
      if(img->type == P_SLICE || img->type == SP_SLICE)
693
        currSE.value1++;
694
      currMB->mb_type = currSE.value1;
695
      if(!dP->bitstream->ei_flag)
696
        currMB->ei_flag = 0;
697
      img->cod_counter--;
698
      currMB->skip_flag = 0;
699
    }
700
    else
701
    {
702
      img->cod_counter--;
703
      currMB->mb_type = 0;
704
      currMB->ei_flag = 0;
705
      currMB->skip_flag = 1;
706
 
707
      // read field flag of bottom block
708
      if(img->MbaffFrameFlag)
709
      {
710
        if(img->cod_counter == 0 && ((img->current_mb_nr&0x01) == 0))
711
        {
712
          TRACE_STRING("mb_field_decoding_flag (of coded bottom mb)");
713
          currSE.len = 1;
714
          readSyntaxElement_FLC(&currSE, dP->bitstream);
715
          dP->bitstream->frame_bitoffset--;
716
          currMB->mb_field = currSE.value1;
717
        }
718
        else if(img->cod_counter > 0 && ((img->current_mb_nr&0x01) == 0))
719
        {
720
          // check left macroblock pair first
721
          if (mb_is_available(img->current_mb_nr-2, img->current_mb_nr)&&((img->current_mb_nr%(img->PicWidthInMbs*2))!=0))
722
          {
723
            currMB->mb_field = img->mb_data[img->current_mb_nr-2].mb_field;
724
          }
725
          else
726
          {
727
            // check top macroblock pair
728
            if (mb_is_available(img->current_mb_nr-2*img->PicWidthInMbs, img->current_mb_nr))
729
            {
730
              currMB->mb_field = img->mb_data[img->current_mb_nr-2*img->PicWidthInMbs].mb_field;
731
            }
732
            else
733
              currMB->mb_field = 0;
734
          }
735
        }
736
      }
737
    }
738
  }
739
 
740
  dec_picture->mb_field[img->current_mb_nr] = currMB->mb_field;
741
 
742
  img->siblock[img->mb_y][img->mb_x]=0;
743
 
744
  if ((img->type==P_SLICE ))    // inter frame
745
    interpret_mb_mode_P(img);
746
  else if (img->type==I_SLICE)                                  // intra frame
747
    interpret_mb_mode_I(img);
748
  else if ((img->type==B_SLICE))       // B frame
749
    interpret_mb_mode_B(img);
750
  else if ((img->type==SP_SLICE))     // SP frame
751
    interpret_mb_mode_P(img);
752
  else if (img->type==SI_SLICE)     // SI frame
753
    interpret_mb_mode_SI(img);
754
 
755
  if(img->MbaffFrameFlag)
756
  {
757
    if(currMB->mb_field)
758
    {
759
      img->num_ref_idx_l0_active <<=1;
760
      img->num_ref_idx_l1_active <<=1;
761
    }
762
  }
763
 
764
  //init NoMbPartLessThan8x8Flag
765
  currMB->NoMbPartLessThan8x8Flag = (IS_DIRECT(currMB) && !(active_sps->direct_8x8_inference_flag))? 0: 1;
766
 
767
  //====== READ 8x8 SUB-PARTITION MODES (modes of 8x8 blocks) and Intra VBST block modes ======
768
  if (IS_P8x8 (currMB))
769
  {
770
    currSE.type    = SE_MBTYPE;
771
    dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
772
 
773
    for (i=0; i<4; i++)
774
    {
775
      if (active_pps->entropy_coding_mode_flag ==UVLC || dP->bitstream->ei_flag) currSE.mapping = linfo_ue;
776
      else                                                  currSE.reading = readB8_typeInfo_CABAC;
777
 
778
      TRACE_STRING("sub_mb_type");
779
      dP->readSyntaxElement (&currSE, img, dP);
780
      SetB8Mode (img, currMB, currSE.value1, i);
781
 
782
      //set NoMbPartLessThan8x8Flag for P8x8 mode
783
      currMB->NoMbPartLessThan8x8Flag &= (currMB->b8mode[i]==0 && active_sps->direct_8x8_inference_flag) ||
784
                                         (currMB->b8mode[i]==4);
785
    }
786
    //--- init macroblock data ---
787
    init_macroblock       (img);
788
    readMotionInfoFromNAL (img, inp);
789
  }
790
 
791
 
792
  //============= Transform Size Flag for INTRA MBs =============
793
  //-------------------------------------------------------------
794
  //transform size flag for INTRA_4x4 and INTRA_8x8 modes
795
  if (currMB->mb_type == I4MB && img->Transform8x8Mode)
796
  {
797
    currSE.type   =  SE_HEADER;
798
    dP = &(currSlice->partArr[partMap[SE_HEADER]]);
799
    currSE.reading = readMB_transform_size_flag_CABAC;
800
    TRACE_STRING("transform_size_8x8_flag");
801
 
802
    // read UVLC transform_size_8x8_flag
803
    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
804
    {
805
      currSE.len = 1;
806
      readSyntaxElement_FLC(&currSE, dP->bitstream);
807
    }
808
    else
809
    {
810
      dP->readSyntaxElement(&currSE,img,dP);
811
    }
812
 
813
    currMB->luma_transform_size_8x8_flag = currSE.value1;
814
 
815
    if (currMB->luma_transform_size_8x8_flag)
816
    {
817
      currMB->mb_type = I8MB;
818
      for (i=0;i<4;i++)
819
      {
820
        currMB->b8mode[i]=I8MB;
821
        currMB->b8pdir[i]=-1;
822
      }
823
    }
824
  }
825
  else
826
  {
827
    currMB->luma_transform_size_8x8_flag = 0;
828
  }
829
 
830
  if(active_pps->constrained_intra_pred_flag && (img->type==P_SLICE|| img->type==B_SLICE))        // inter frame
831
  {
832
    if( !IS_INTRA(currMB) )
833
    {
834
      img->intra_block[img->current_mb_nr] = 0;
835
    }
836
  }
837
 
838
  //! TO for error concealment
839
  //! If we have an INTRA Macroblock and we lost the partition
840
  //! which contains the intra coefficients Copy MB would be better
841
  //! than just a gray block.
842
  //! Seems to be a bit at the wrong place to do this right here, but for this case
843
  //! up to now there is no other way.
844
  dP = &(currSlice->partArr[partMap[SE_CBP_INTRA]]);
845
  if(IS_INTRA (currMB) && dP->bitstream->ei_flag && img->number)
846
  {
847
    currMB->mb_type = 0;
848
    currMB->ei_flag = 1;
849
    for (i=0;i<4;i++) {currMB->b8mode[i]=currMB->b8pdir[i]=0; }
850
  }
851
  dP = &(currSlice->partArr[partMap[currSE.type]]);
852
  //! End TO
853
 
854
 
855
  //--- init macroblock data ---
856
  if (!IS_P8x8 (currMB))
857
    init_macroblock       (img);
858
 
859
  if (IS_DIRECT (currMB) && img->cod_counter >= 0)
860
  {
861
    currMB->cbp = 0;
862
    reset_coeffs();
863
 
864
    if (active_pps->entropy_coding_mode_flag ==CABAC)
865
      img->cod_counter=-1;
866
 
867
    return DECODE_MB;
868
  }
869
 
870
  if (IS_COPY (currMB)) //keep last macroblock
871
  {
872
    int i, j, k;
873
    short pmv[2];
874
    int zeroMotionAbove;
875
    int zeroMotionLeft;
876
    PixelPos mb_a, mb_b;
877
    int      a_mv_y = 0;
878
    int      a_ref_idx = 0;
879
    int      b_mv_y = 0;
880
    int      b_ref_idx = 0;
881
    int      list_offset = ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? 4 : 2 : 0;
882
    short ***cur_mv = dec_picture->mv[LIST_0];
883
    getLuma4x4Neighbour(img->current_mb_nr,-1, 0, &mb_a);
884
    getLuma4x4Neighbour(img->current_mb_nr, 0,-1, &mb_b);
885
 
886
    if (mb_a.available)
887
    {
888
      a_mv_y    = cur_mv[mb_a.pos_y][mb_a.pos_x][1];
889
      a_ref_idx = dec_picture->ref_idx[LIST_0][mb_a.pos_y][mb_a.pos_x];
890
 
891
      if (currMB->mb_field && !img->mb_data[mb_a.mb_addr].mb_field)
892
      {
893
        a_mv_y    /=2;
894
        a_ref_idx *=2;
895
      }
896
      if (!currMB->mb_field && img->mb_data[mb_a.mb_addr].mb_field)
897
      {
898
        a_mv_y    *=2;
899
        a_ref_idx >>=1;
900
      }
901
    }
902
 
903
    if (mb_b.available)
904
    {
905
      b_mv_y    = cur_mv[mb_b.pos_y][mb_b.pos_x][1];
906
      b_ref_idx = dec_picture->ref_idx[LIST_0][mb_b.pos_y][mb_b.pos_x];
907
 
908
      if (currMB->mb_field && !img->mb_data[mb_b.mb_addr].mb_field)
909
      {
910
        b_mv_y    /=2;
911
        b_ref_idx *=2;
912
      }
913
      if (!currMB->mb_field && img->mb_data[mb_b.mb_addr].mb_field)
914
      {
915
        b_mv_y    *=2;
916
        b_ref_idx >>=1;
917
      }
918
    }
919
 
920
    zeroMotionLeft  = !mb_a.available ? 1 : a_ref_idx==0 && cur_mv[mb_a.pos_y][mb_a.pos_x][0]==0 && a_mv_y==0 ? 1 : 0;
921
    zeroMotionAbove = !mb_b.available ? 1 : b_ref_idx==0 && cur_mv[mb_b.pos_y][mb_b.pos_x][0]==0 && b_mv_y==0 ? 1 : 0;
922
 
923
    currMB->cbp = 0;
924
    reset_coeffs();
925
 
926
    img_block_y   = img->block_y;
927
 
928
    if (zeroMotionAbove || zeroMotionLeft)
929
    {
930
      for(j=img_block_y;j<img_block_y + BLOCK_SIZE;j++)
931
      {
932
        memset(&cur_mv[j][img->block_x][0], 0, 2 * BLOCK_SIZE * sizeof(short));
933
      }
934
    }
935
    else
936
    {
937
      SetMotionVectorPredictor (img, pmv, 0, LIST_0, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
938
 
939
      for(j=img_block_y;j<img_block_y + BLOCK_SIZE;j++)
940
      {
941
        for(i=img->block_x;i<img->block_x + BLOCK_SIZE;i++)
942
          for (k=0;k<2;k++)
943
          {
944
            cur_mv[j][i][k] = pmv[k];
945
          }
946
      }
947
    }
948
    for(j=img_block_y;j< img_block_y + BLOCK_SIZE;j++)
949
    {
950
      for(i=img->block_x;i<img->block_x + BLOCK_SIZE;i++)
951
      {
952
        dec_picture->ref_idx[LIST_0][j][i] = 0;
953
        dec_picture->ref_pic_id[LIST_0][j][i] =
954
          dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][(short)dec_picture->ref_idx[LIST_0][j][i]];
955
      }
956
    }
957
    return DECODE_MB;
958
  }
959
  if(currMB->mb_type!=IPCM)
960
  {
961
 
962
    // intra prediction modes for a macroblock 4x4 **********************************************
963
    read_ipred_modes(img,inp);
964
 
965
    // read inter frame vector data *********************************************************
966
    if (IS_INTERMV (currMB) && (!IS_P8x8(currMB)))
967
    {
968
      readMotionInfoFromNAL (img, inp);
969
    }
970
    // read CBP and Coeffs  ***************************************************************
971
    readCBPandCoeffsFromNAL (img,inp);
972
  }
973
  else
974
  {
975
    //read pcm_alignment_zero_bit and pcm_byte[i]
976
 
977
    // here dP is assigned with the same dP as SE_MBTYPE, because IPCM syntax is in the
978
    // same category as MBTYPE
979
    dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
980
    readIPCMcoeffsFromNAL(img,inp,dP);
981
  }
982
 
983
 
984
  return DECODE_MB;
985
}
986
 
987
 
988
 
989
/*!
990
 ************************************************************************
991
 * \brief
992
 *    Initialize decoding engine after decoding an IPCM macroblock
993
 *    (for IPCM CABAC  28/11/2003)
994
 *
995
 * \author
996
 *    Dong Wang <Dong.Wang@bristol.ac.uk>
997
 ************************************************************************
998
 */
999
void init_decoding_engine_IPCM(struct img_par *img)
1000
{
1001
  Slice *currSlice = img->currentSlice;
1002
  Bitstream *currStream;
1003
  int ByteStartPosition;
1004
  int PartitionNumber;
1005
  int i;
1006
 
1007
  if(currSlice->dp_mode==PAR_DP_1)
1008
    PartitionNumber=1;
1009
  else if(currSlice->dp_mode==PAR_DP_3)
1010
    PartitionNumber=3;
1011
  else
1012
  {
1013
    printf("Partition Mode is not supported\n");
1014
    exit(1);
1015
  }
1016
 
1017
  for(i=0;i<PartitionNumber;i++)
1018
  {
1019
    currStream = currSlice->partArr[i].bitstream;
1020
    ByteStartPosition = currStream->read_len;
1021
 
1022
 
1023
    arideco_start_decoding (&currSlice->partArr[i].de_cabac, currStream->streamBuffer, ByteStartPosition, &currStream->read_len, img->type);
1024
  }
1025
}
1026
 
1027
 
1028
 
1029
 
1030
/*!
1031
 ************************************************************************
1032
 * \brief
1033
 *    Read IPCM pcm_alignment_zero_bit and pcm_byte[i] from stream to img->cof
1034
 *    (for IPCM CABAC and IPCM CAVLC)
1035
 *
1036
 * \author
1037
 *    Dong Wang <Dong.Wang@bristol.ac.uk>
1038
 ************************************************************************
1039
 */
1040
 
1041
void readIPCMcoeffsFromNAL(struct img_par *img, struct inp_par *inp, struct datapartition *dP)
1042
{
1043
  SyntaxElement currSE;
1044
  int i,j;
1045
 
1046
  //For CABAC, we don't need to read bits to let stream byte aligned
1047
  //  because we have variable for integer bytes position
1048
  if(active_pps->entropy_coding_mode_flag  == CABAC)
1049
  {
1050
    //read luma and chroma IPCM coefficients
1051
    currSE.len=8;
1052
    TRACE_STRING("pcm_byte luma");
1053
 
1054
    for(i=0;i<MB_BLOCK_SIZE;i++)
1055
    {
1056
      for(j=0;j<MB_BLOCK_SIZE;j++)
1057
      {
1058
        readIPCMBytes_CABAC(&currSE, dP->bitstream);
1059
        img->cof[(i>>2)][(j>>2)][i & 0x03][j & 0x03]=currSE.value1;
1060
      }
1061
    }
1062
    if (dec_picture->chroma_format_idc != YUV400)
1063
    {
1064
      TRACE_STRING("pcm_byte chroma");
1065
      for(i=0;i<img->mb_cr_size_y;i++)
1066
      {
1067
        for(j=0;j<img->mb_cr_size_x;j++)
1068
        {
1069
          readIPCMBytes_CABAC(&currSE, dP->bitstream);
1070
          img->cof[(i>>2)][(j>>2)+4][i & 0x03][j & 0x03]=currSE.value1;
1071
        }
1072
      }
1073
      for(i=0;i<img->mb_cr_size_y;i++)
1074
      {
1075
        for(j=0;j<img->mb_cr_size_x;j++)
1076
        {
1077
          readIPCMBytes_CABAC(&currSE, dP->bitstream);
1078
          img->cof[(i>>2)+2][(j>>2)+4][i & 0x03][j & 0x03]=currSE.value1;
1079
        }
1080
      }
1081
    }
1082
    //If the decoded MB is IPCM MB, decoding engine is initialized
1083
 
1084
    // here the decoding engine is directly initialized without checking End of Slice
1085
    // The reason is that, whether current MB is the last MB in slice or not, there is
1086
    // at least one 'end of slice' syntax after this MB. So when fetching bytes in this
1087
    // initialisation process, we can guarantee there is bits available in bitstream.
1088
 
1089
    init_decoding_engine_IPCM(img);
1090
  }
1091
  else
1092
  {
1093
    //read bits to let stream byte aligned
1094
 
1095
    if((dP->bitstream->frame_bitoffset)%8!=0)
1096
    {
1097
      TRACE_STRING("pcm_alignment_zero_bit");
1098
      currSE.len=8-(dP->bitstream->frame_bitoffset)%8;
1099
      readSyntaxElement_FLC(&currSE, dP->bitstream);
1100
    }
1101
 
1102
    //read luma and chroma IPCM coefficients
1103
    currSE.len=img->bitdepth_luma;
1104
    TRACE_STRING("pcm_sample_luma");
1105
 
1106
    for(i=0;i<MB_BLOCK_SIZE;i++)
1107
    {
1108
      for(j=0;j<MB_BLOCK_SIZE;j++)
1109
      {
1110
        readSyntaxElement_FLC(&currSE, dP->bitstream);
1111
        img->cof[(i>>2)][(j>>2)][i & 0x03][j & 0x03]=currSE.value1;
1112
      }
1113
    }
1114
    currSE.len=img->bitdepth_chroma;
1115
    if (dec_picture->chroma_format_idc != YUV400)
1116
    {
1117
      TRACE_STRING("pcm_sample_chroma (u)");
1118
      for(i=0;i<img->mb_cr_size_y;i++)
1119
      {
1120
        for(j=0;j<img->mb_cr_size_x;j++)
1121
        {
1122
          readSyntaxElement_FLC(&currSE, dP->bitstream);
1123
          img->cof[(i>>2)][(j>>2)+4][i & 0x03][j & 0x03]=currSE.value1;
1124
        }
1125
      }
1126
      TRACE_STRING("pcm_sample_chroma (v)");
1127
      for(i=0;i<img->mb_cr_size_y;i++)
1128
      {
1129
        for(j=0;j<img->mb_cr_size_x;j++)
1130
        {
1131
          readSyntaxElement_FLC(&currSE, dP->bitstream);
1132
          img->cof[(i>>2)+2][(j>>2)+4][i & 0x03][j & 0x03]=currSE.value1;
1133
        }
1134
      }
1135
    }
1136
  }
1137
}
1138
 
1139
 
1140
 
1141
void read_ipred_modes(struct img_par *img,struct inp_par *inp)
1142
{
1143
  int b8,i,j,bi,bj,bx,by,dec;
1144
  SyntaxElement currSE;
1145
  Slice *currSlice;
1146
  DataPartition *dP;
1147
  int *partMap;
1148
  Macroblock *currMB;
1149
  int ts, ls;
1150
  int mostProbableIntraPredMode;
1151
  int upIntraPredMode;
1152
  int leftIntraPredMode;
1153
  int IntraChromaPredModeFlag;
1154
  int bs_x, bs_y;
1155
  int ii,jj;
1156
 
1157
  PixelPos left_block;
1158
  PixelPos top_block;
1159
 
1160
  currMB = &img->mb_data[img->current_mb_nr];
1161
 
1162
  IntraChromaPredModeFlag = IS_INTRA(currMB);
1163
 
1164
  currSlice = img->currentSlice;
1165
  partMap = assignSE2partition[currSlice->dp_mode];
1166
 
1167
  currSE.type = SE_INTRAPREDMODE;
1168
 
1169
  TRACE_STRING("intra4x4_pred_mode");
1170
  dP = &(currSlice->partArr[partMap[currSE.type]]);
1171
 
1172
  if (!(active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag))
1173
    currSE.reading = readIntraPredMode_CABAC;
1174
 
1175
  for(b8=0;b8<4;b8++)  //loop 8x8 blocks
1176
  {
1177
    if((currMB->b8mode[b8]==IBLOCK )||(currMB->b8mode[b8]==I8MB))
1178
    {
1179
      bs_x = bs_y = (currMB->b8mode[b8] == I8MB)?8:4;
1180
 
1181
      IntraChromaPredModeFlag = 1;
1182
 
1183
      ii=(bs_x>>2);
1184
      jj=(bs_y>>2);
1185
 
1186
      for(j=0;j<2;j+=jj)  //loop subblocks
1187
      {
1188
        by = (b8&2) + j;
1189
        bj = img->block_y + by;
1190
        for(i=0;i<2;i+=ii)
1191
        {
1192
          bx = ((b8&1)<<1) + i;
1193
          bi = img->block_x + bx;
1194
          //get from stream
1195
          if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
1196
            readSyntaxElement_Intra4x4PredictionMode(&currSE,img,dP);
1197
          else
1198
          {
1199
            currSE.context=(b8<<2)+(j<<1)+i;
1200
            dP->readSyntaxElement(&currSE,img,dP);
1201
          }
1202
 
1203
          getLuma4x4Neighbour(img->current_mb_nr, (bx<<2) - 1, (by<<2),     &left_block);
1204
          getLuma4x4Neighbour(img->current_mb_nr, (bx<<2),     (by<<2) - 1, &top_block);
1205
 
1206
          //get from array and decode
1207
 
1208
          if (active_pps->constrained_intra_pred_flag)
1209
          {
1210
            left_block.available = left_block.available ? img->intra_block[left_block.mb_addr] : 0;
1211
            top_block.available  = top_block.available  ? img->intra_block[top_block.mb_addr]  : 0;
1212
          }
1213
 
1214
          // !! KS: not sure if the following is still correct...
1215
          ts = ls = 0;   // Check to see if the neighboring block is SI
1216
          if (IS_OLDINTRA(currMB) && img->type == SI_SLICE)           // need support for MBINTLC1
1217
          {
1218
            if (left_block.available)
1219
              if (img->siblock [left_block.pos_y][left_block.pos_x])
1220
                ls=1;
1221
 
1222
            if (top_block.available)
1223
              if (img->siblock [top_block.pos_y][top_block.pos_x])
1224
                ts=1;
1225
          }
1226
 
1227
          upIntraPredMode            = (top_block.available  &&(ts == 0)) ? img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
1228
          leftIntraPredMode          = (left_block.available &&(ls == 0)) ? img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
1229
 
1230
          mostProbableIntraPredMode  = (upIntraPredMode < 0 || leftIntraPredMode < 0) ? DC_PRED : upIntraPredMode < leftIntraPredMode ? upIntraPredMode : leftIntraPredMode;
1231
 
1232
          dec = (currSE.value1 == -1) ? mostProbableIntraPredMode : currSE.value1 + (currSE.value1 >= mostProbableIntraPredMode);
1233
 
1234
          //set
1235
          for(jj=0;jj<(bs_y>>2);jj++)   //loop 4x4s in the subblock for 8x8 prediction setting
1236
            memset(&img->ipredmode[bj+jj][bi], dec, (bs_x>>2) * sizeof(char));
1237
        }
1238
      }
1239
    }
1240
  }
1241
 
1242
  if (IntraChromaPredModeFlag && dec_picture->chroma_format_idc != YUV400)
1243
  {
1244
    currSE.type = SE_INTRAPREDMODE;
1245
    TRACE_STRING("intra_chroma_pred_mode");
1246
    dP = &(currSlice->partArr[partMap[currSE.type]]);
1247
 
1248
    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) currSE.mapping = linfo_ue;
1249
    else                                                    currSE.reading = readCIPredMode_CABAC;
1250
 
1251
    dP->readSyntaxElement(&currSE,img,dP);
1252
    currMB->c_ipred_mode = currSE.value1;
1253
 
1254
    if (currMB->c_ipred_mode < DC_PRED_8 || currMB->c_ipred_mode > PLANE_8)
1255
    {
1256
      error("illegal chroma intra pred mode!\n", 600);
1257
    }
1258
  }
1259
}
1260
 
1261
/*!
1262
 ************************************************************************
1263
 * \brief
1264
 *    Set motion vector predictor
1265
 ************************************************************************
1266
 */
1267
static void SetMotionVectorPredictor (struct img_par  *img,
1268
                                      short           pmv[2],
1269
                                      char            ref_frame,
1270
                                      byte            list,
1271
                                      char            ***refPic,
1272
                                      short           ****tmp_mv,
1273
                                      int             block_x,
1274
                                      int             block_y,
1275
                                      int             blockshape_x,
1276
                                      int             blockshape_y)
1277
{
1278
  int mb_x                 = BLOCK_SIZE*block_x;
1279
  int mb_y                 = BLOCK_SIZE*block_y;
1280
  int mb_nr                = img->current_mb_nr;
1281
 
1282
  int mv_a, mv_b, mv_c, pred_vec=0;
1283
  int mvPredType, rFrameL, rFrameU, rFrameUR;
1284
  int hv;
1285
 
1286
 
1287
  PixelPos block_a, block_b, block_c, block_d;
1288
 
1289
  getLuma4x4Neighbour(mb_nr, mb_x - 1,            mb_y,     &block_a);
1290
  getLuma4x4Neighbour(mb_nr, mb_x,                mb_y - 1, &block_b);
1291
  getLuma4x4Neighbour(mb_nr, mb_x + blockshape_x, mb_y - 1, &block_c);
1292
  getLuma4x4Neighbour(mb_nr, mb_x - 1,            mb_y - 1, &block_d);
1293
 
1294
  if (mb_y > 0)
1295
  {
1296
    if (mb_x < 8)  // first column of 8x8 blocks
1297
    {
1298
      if (mb_y==8)
1299
      {
1300
        if (blockshape_x == 16)      block_c.available  = 0;
1301
      }
1302
      else
1303
      {
1304
        if (mb_x+blockshape_x == 8)  block_c.available  = 0;
1305
      }
1306
    }
1307
    else
1308
    {
1309
      if (mb_x+blockshape_x == 16)   block_c.available  = 0;
1310
    }
1311
  }
1312
 
1313
  if (!block_c.available)
1314
  {
1315
    block_c=block_d;
1316
  }
1317
 
1318
  mvPredType = MVPRED_MEDIAN;
1319
 
1320
  if (!img->MbaffFrameFlag)
1321
  {
1322
    rFrameL    = block_a.available    ? refPic[list][block_a.pos_y][block_a.pos_x] : -1;
1323
    rFrameU    = block_b.available    ? refPic[list][block_b.pos_y][block_b.pos_x] : -1;
1324
    rFrameUR   = block_c.available    ? refPic[list][block_c.pos_y][block_c.pos_x] : -1;
1325
  }
1326
  else
1327
  {
1328
    if (img->mb_data[img->current_mb_nr].mb_field)
1329
    {
1330
      rFrameL    = block_a.available    ?
1331
        img->mb_data[block_a.mb_addr].mb_field ?
1332
        refPic[list][block_a.pos_y][block_a.pos_x]:
1333
        refPic[list][block_a.pos_y][block_a.pos_x] * 2:
1334
        -1;
1335
      rFrameU    = block_b.available    ?
1336
        img->mb_data[block_b.mb_addr].mb_field ?
1337
        refPic[list][block_b.pos_y][block_b.pos_x]:
1338
        refPic[list][block_b.pos_y][block_b.pos_x] * 2:
1339
        -1;
1340
      rFrameUR    = block_c.available    ?
1341
        img->mb_data[block_c.mb_addr].mb_field ?
1342
        refPic[list][block_c.pos_y][block_c.pos_x]:
1343
        refPic[list][block_c.pos_y][block_c.pos_x] * 2:
1344
        -1;
1345
    }
1346
    else
1347
    {
1348
      rFrameL    = block_a.available    ?
1349
        img->mb_data[block_a.mb_addr].mb_field ?
1350
        refPic[list][block_a.pos_y][block_a.pos_x] >>1:
1351
        refPic[list][block_a.pos_y][block_a.pos_x] :
1352
        -1;
1353
      rFrameU    = block_b.available    ?
1354
        img->mb_data[block_b.mb_addr].mb_field ?
1355
        refPic[list][block_b.pos_y][block_b.pos_x] >>1:
1356
        refPic[list][block_b.pos_y][block_b.pos_x] :
1357
        -1;
1358
      rFrameUR    = block_c.available    ?
1359
        img->mb_data[block_c.mb_addr].mb_field ?
1360
        refPic[list][block_c.pos_y][block_c.pos_x] >>1:
1361
        refPic[list][block_c.pos_y][block_c.pos_x] :
1362
        -1;
1363
    }
1364
  }
1365
 
1366
 
1367
  /* Prediction if only one of the neighbors uses the reference frame
1368
   * we are checking
1369
   */
1370
  if(rFrameL == ref_frame && rFrameU != ref_frame && rFrameUR != ref_frame)       mvPredType = MVPRED_L;
1371
  else if(rFrameL != ref_frame && rFrameU == ref_frame && rFrameUR != ref_frame)  mvPredType = MVPRED_U;
1372
  else if(rFrameL != ref_frame && rFrameU != ref_frame && rFrameUR == ref_frame)  mvPredType = MVPRED_UR;
1373
  // Directional predictions
1374
  if(blockshape_x == 8 && blockshape_y == 16)
1375
  {
1376
    if(mb_x == 0)
1377
    {
1378
      if(rFrameL == ref_frame)
1379
        mvPredType = MVPRED_L;
1380
    }
1381
    else
1382
    {
1383
      if( rFrameUR == ref_frame)
1384
        mvPredType = MVPRED_UR;
1385
    }
1386
  }
1387
  else if(blockshape_x == 16 && blockshape_y == 8)
1388
  {
1389
    if(mb_y == 0)
1390
    {
1391
      if(rFrameU == ref_frame)
1392
        mvPredType = MVPRED_U;
1393
    }
1394
    else
1395
    {
1396
      if(rFrameL == ref_frame)
1397
        mvPredType = MVPRED_L;
1398
    }
1399
  }
1400
 
1401
  for (hv=0; hv < 2; hv++)
1402
  {
1403
    if (!img->MbaffFrameFlag || hv==0)
1404
    {
1405
      mv_a = block_a.available  ? tmp_mv[list][block_a.pos_y][block_a.pos_x][hv] : 0;
1406
      mv_b = block_b.available  ? tmp_mv[list][block_b.pos_y][block_b.pos_x][hv] : 0;
1407
      mv_c = block_c.available  ? tmp_mv[list][block_c.pos_y][block_c.pos_x][hv] : 0;
1408
    }
1409
    else
1410
    {
1411
      if (img->mb_data[img->current_mb_nr].mb_field)
1412
      {
1413
        mv_a = block_a.available  ? img->mb_data[block_a.mb_addr].mb_field?
1414
          tmp_mv[list][block_a.pos_y][block_a.pos_x][hv]:
1415
          tmp_mv[list][block_a.pos_y][block_a.pos_x][hv] / 2:
1416
          0;
1417
        mv_b = block_b.available  ? img->mb_data[block_b.mb_addr].mb_field?
1418
          tmp_mv[list][block_b.pos_y][block_b.pos_x][hv]:
1419
          tmp_mv[list][block_b.pos_y][block_b.pos_x][hv] / 2:
1420
          0;
1421
        mv_c = block_c.available  ? img->mb_data[block_c.mb_addr].mb_field?
1422
          tmp_mv[list][block_c.pos_y][block_c.pos_x][hv]:
1423
          tmp_mv[list][block_c.pos_y][block_c.pos_x][hv] / 2:
1424
          0;
1425
      }
1426
      else
1427
      {
1428
        mv_a = block_a.available  ? img->mb_data[block_a.mb_addr].mb_field?
1429
          tmp_mv[list][block_a.pos_y][block_a.pos_x][hv] * 2:
1430
          tmp_mv[list][block_a.pos_y][block_a.pos_x][hv]:
1431
          0;
1432
        mv_b = block_b.available  ? img->mb_data[block_b.mb_addr].mb_field?
1433
          tmp_mv[list][block_b.pos_y][block_b.pos_x][hv] * 2:
1434
          tmp_mv[list][block_b.pos_y][block_b.pos_x][hv]:
1435
          0;
1436
        mv_c = block_c.available  ? img->mb_data[block_c.mb_addr].mb_field?
1437
          tmp_mv[list][block_c.pos_y][block_c.pos_x][hv] * 2:
1438
          tmp_mv[list][block_c.pos_y][block_c.pos_x][hv]:
1439
          0;
1440
      }
1441
    }
1442
 
1443
    switch (mvPredType)
1444
    {
1445
    case MVPRED_MEDIAN:
1446
      if(!(block_b.available || block_c.available))
1447
        pred_vec = mv_a;
1448
      else
1449
        pred_vec = mv_a+mv_b+mv_c-imin(mv_a,imin(mv_b,mv_c))-imax(mv_a,imax(mv_b,mv_c));
1450
      break;
1451
    case MVPRED_L:
1452
      pred_vec = mv_a;
1453
      break;
1454
    case MVPRED_U:
1455
      pred_vec = mv_b;
1456
      break;
1457
    case MVPRED_UR:
1458
      pred_vec = mv_c;
1459
      break;
1460
    default:
1461
      break;
1462
    }
1463
 
1464
    pmv[hv] = pred_vec;
1465
  }
1466
}
1467
 
1468
 
1469
/*!
1470
 ************************************************************************
1471
 * \brief
1472
 *    Set context for reference frames
1473
 ************************************************************************
1474
 */
1475
int
1476
BType2CtxRef (int btype)
1477
{
1478
  if (btype<4)  return 0;
1479
  else          return 1;
1480
}
1481
 
1482
/*!
1483
 ************************************************************************
1484
 * \brief
1485
 *    Read motion info
1486
 ************************************************************************
1487
 */
1488
void readMotionInfoFromNAL (struct img_par *img, struct inp_par *inp)
1489
{
1490
  int i,j,k;
1491
  int step_h,step_v;
1492
  int curr_mvd;
1493
  Macroblock *currMB  = &img->mb_data[img->current_mb_nr];
1494
  SyntaxElement currSE;
1495
  Slice *currSlice    = img->currentSlice;
1496
  DataPartition *dP;
1497
  int *partMap        = assignSE2partition[currSlice->dp_mode];
1498
  int bframe          = (img->type==B_SLICE);
1499
  int partmode        = (IS_P8x8(currMB)?4:currMB->mb_type);
1500
  int step_h0         = BLOCK_STEP [partmode][0];
1501
  int step_v0         = BLOCK_STEP [partmode][1];
1502
 
1503
  int mv_mode, i0, j0;
1504
  char refframe;
1505
  short pmv[2];
1506
  int j4, i4, ii,jj;
1507
  int vec;
1508
 
1509
  int mv_scale = 0;
1510
 
1511
  int flag_mode;
1512
 
1513
  int list_offset = ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? 4 : 2 : 0;
1514
 
1515
  byte  **    moving_block;
1516
  short ****  co_located_mv;
1517
  char  ***   co_located_ref_idx;
1518
  int64 ***   co_located_ref_id;
1519
 
1520
  if ((img->MbaffFrameFlag)&&(currMB->mb_field))
1521
  {
1522
    if(img->current_mb_nr&0x01)
1523
    {
1524
      moving_block = Co_located->bottom_moving_block;
1525
      co_located_mv = Co_located->bottom_mv;
1526
      co_located_ref_idx = Co_located->bottom_ref_idx;
1527
      co_located_ref_id = Co_located->bottom_ref_pic_id;
1528
    }
1529
    else
1530
    {
1531
      moving_block = Co_located->top_moving_block;
1532
      co_located_mv = Co_located->top_mv;
1533
      co_located_ref_idx = Co_located->top_ref_idx;
1534
      co_located_ref_id = Co_located->top_ref_pic_id;
1535
    }
1536
  }
1537
  else
1538
  {
1539
    moving_block = Co_located->moving_block;
1540
    co_located_mv = Co_located->mv;
1541
    co_located_ref_idx = Co_located->ref_idx;
1542
    co_located_ref_id = Co_located->ref_pic_id;
1543
  }
1544
 
1545
  if (bframe && IS_P8x8 (currMB))
1546
  {
1547
    if (img->direct_spatial_mv_pred_flag)
1548
    {
1549
      int imgblock_y= ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? (img->block_y-4)>>1:img->block_y>>1: img->block_y;
1550
      int l0_rFrameL, l0_rFrameU, l0_rFrameUL, l0_rFrameUR;
1551
      int l1_rFrameL, l1_rFrameU, l1_rFrameUL, l1_rFrameUR;
1552
 
1553
      PixelPos mb_left, mb_up, mb_upleft, mb_upright;
1554
 
1555
      char  l0_rFrame,l1_rFrame;
1556
      short pmvl0[2]={0,0}, pmvl1[2]={0,0};
1557
 
1558
      getLuma4x4Neighbour(img->current_mb_nr, -1,  0, &mb_left);
1559
      getLuma4x4Neighbour(img->current_mb_nr,  0, -1, &mb_up);
1560
      getLuma4x4Neighbour(img->current_mb_nr, 16, -1, &mb_upright);
1561
      getLuma4x4Neighbour(img->current_mb_nr, -1, -1, &mb_upleft);
1562
 
1563
      if (!img->MbaffFrameFlag)
1564
      {
1565
        l0_rFrameL = mb_left.available ? dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] : -1;
1566
        l0_rFrameU = mb_up.available ? dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] : -1;
1567
        l0_rFrameUL = mb_upleft.available ? dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
1568
        l0_rFrameUR = mb_upright.available ? dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] : l0_rFrameUL;
1569
 
1570
        l1_rFrameL = mb_left.available ? dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] : -1;
1571
        l1_rFrameU = mb_up.available ? dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] : -1;
1572
        l1_rFrameUL = mb_upleft.available ? dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
1573
        l1_rFrameUR = mb_upright.available ? dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] : l1_rFrameUL;
1574
      }
1575
      else
1576
      {
1577
        if (img->mb_data[img->current_mb_nr].mb_field)
1578
        {
1579
          l0_rFrameL = mb_left.available
1580
            ? img->mb_data[mb_left.mb_addr].mb_field  || dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] < 0
1581
            ? dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]
1582
            : dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] * 2: -1;
1583
 
1584
          l0_rFrameU = mb_up.available
1585
            ? img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] < 0
1586
            ? dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x]
1587
            : dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] * 2: -1;
1588
 
1589
          l0_rFrameUL = mb_upleft.available
1590
            ? img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] < 0
1591
            ? dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x]
1592
            : dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] *2: -1;
1593
 
1594
          l0_rFrameUR = mb_upright.available
1595
            ? img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] < 0
1596
            ? dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x]
1597
            : dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] * 2: l0_rFrameUL;
1598
 
1599
          l1_rFrameL = mb_left.available
1600
            ? img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x]  < 0
1601
            ? dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x]
1602
            : dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] * 2: -1;
1603
 
1604
          l1_rFrameU = mb_up.available
1605
            ? img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x]  < 0
1606
            ? dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x]
1607
            : dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] * 2: -1;
1608
 
1609
          l1_rFrameUL = mb_upleft.available
1610
            ? img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x]  < 0
1611
            ? dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x]
1612
            : dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] *2 : -1;
1613
 
1614
          l1_rFrameUR = mb_upright.available
1615
            ? img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x]  < 0
1616
            ? dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x]
1617
            : dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] * 2: l1_rFrameUL;
1618
 
1619
        }
1620
        else
1621
        {
1622
          l0_rFrameL = mb_left.available ?
1623
            img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]  < 0 ?
1624
            dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] >> 1 :
1625
          dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]: -1;
1626
 
1627
          l0_rFrameU = mb_up.available ?
1628
            img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x]  < 0 ?
1629
            dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] >> 1 :
1630
          dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] : -1;
1631
 
1632
          l0_rFrameUL = mb_upleft.available ?
1633
            img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] < 0 ?
1634
            dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x]>> 1 :
1635
          dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
1636
 
1637
          l0_rFrameUR = mb_upright.available ?
1638
            img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x]  < 0 ?
1639
            dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] >> 1 :
1640
          dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] : l0_rFrameUL;
1641
 
1642
          l1_rFrameL = mb_left.available ?
1643
            img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] < 0 ?
1644
            dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] >> 1 :
1645
          dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] : -1;
1646
          l1_rFrameU = mb_up.available ?
1647
            img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] < 0 ?
1648
            dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] >> 1 :
1649
          dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] : -1;
1650
 
1651
          l1_rFrameUL = mb_upleft.available ?
1652
            img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x]  < 0 ?
1653
            dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] >> 1 :
1654
          dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
1655
 
1656
          l1_rFrameUR = mb_upright.available ?
1657
            img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
1658
            dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] >> 1:
1659
          dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] : l1_rFrameUL;
1660
        }
1661
      }
1662
 
1663
      l0_rFrame = (l0_rFrameL >= 0 && l0_rFrameU >= 0) ? imin(l0_rFrameL,l0_rFrameU): imax(l0_rFrameL,l0_rFrameU);
1664
      l0_rFrame = (l0_rFrame >= 0 && l0_rFrameUR >= 0) ? imin(l0_rFrame,l0_rFrameUR): imax(l0_rFrame,l0_rFrameUR);
1665
 
1666
      l1_rFrame = (l1_rFrameL >= 0 && l1_rFrameU >= 0) ? imin(l1_rFrameL,l1_rFrameU): imax(l1_rFrameL,l1_rFrameU);
1667
      l1_rFrame = (l1_rFrame >= 0 && l1_rFrameUR >= 0) ? imin(l1_rFrame,l1_rFrameUR): imax(l1_rFrame,l1_rFrameUR);
1668
 
1669
 
1670
      if (l0_rFrame >=0)
1671
        SetMotionVectorPredictor (img, pmvl0, l0_rFrame, LIST_0, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
1672
 
1673
      if (l1_rFrame >=0)
1674
        SetMotionVectorPredictor (img, pmvl1, l1_rFrame, LIST_1, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
1675
 
1676
 
1677
      for (i=0;i<4;i++)
1678
      {
1679
        if (currMB->b8mode[i] == 0)
1680
        {
1681
          for(j=2*(i>>1);j<2*(i>>1)+2;j++)
1682
          {
1683
            for(k=2*(i&0x01);k<2*(i&0x01)+2;k++)
1684
            {
1685
              int j6 = imgblock_y+j;
1686
              j4 = img->block_y+j;
1687
              i4 = img->block_x+k;
1688
 
1689
 
1690
              if (l0_rFrame >= 0)
1691
              {
1692
                if  (!l0_rFrame  && ((!moving_block[j6][i4]) && (!listX[1+list_offset][0]->is_long_term)))
1693
                {
1694
                  dec_picture->mv  [LIST_0][j4][i4][0] = 0;
1695
                  dec_picture->mv  [LIST_0][j4][i4][1] = 0;
1696
                  dec_picture->ref_idx[LIST_0][j4][i4] = 0;
1697
                }
1698
                else
1699
                {
1700
                  dec_picture->mv  [LIST_0][j4][i4][0] = pmvl0[0];
1701
                  dec_picture->mv  [LIST_0][j4][i4][1] = pmvl0[1];
1702
                  dec_picture->ref_idx[LIST_0][j4][i4] = l0_rFrame;
1703
                }
1704
              }
1705
              else
1706
              {
1707
                dec_picture->mv  [LIST_0][j4][i4][0] = 0;
1708
                dec_picture->mv  [LIST_0][j4][i4][1] = 0;
1709
                dec_picture->ref_idx[LIST_0][j4][i4] = -1;
1710
              }
1711
 
1712
              if (l1_rFrame >= 0)
1713
              {
1714
                if  (l1_rFrame==0 && ((!moving_block[j6][i4])&& (!listX[1+list_offset][0]->is_long_term)))
1715
                {
1716
                  dec_picture->mv  [LIST_1][j4][i4][0] = 0;
1717
                  dec_picture->mv  [LIST_1][j4][i4][1] = 0;
1718
                  dec_picture->ref_idx[LIST_1][j4][i4] = 0;
1719
                }
1720
                else
1721
                {
1722
                  dec_picture->mv  [LIST_1][j4][i4][0] = pmvl1[0];
1723
                  dec_picture->mv  [LIST_1][j4][i4][1] = pmvl1[1];
1724
                  dec_picture->ref_idx[LIST_1][j4][i4] = l1_rFrame;
1725
                }
1726
              }
1727
              else
1728
              {
1729
                dec_picture->mv  [LIST_1][j4][i4][0] = 0;
1730
                dec_picture->mv  [LIST_1][j4][i4][1] = 0;
1731
                dec_picture->ref_idx[LIST_1][j4][i4] = -1;
1732
              }
1733
 
1734
              if (l0_rFrame <0 && l1_rFrame <0)
1735
              {
1736
                dec_picture->ref_idx[LIST_0][j4][i4] = 0;
1737
                dec_picture->ref_idx[LIST_1][j4][i4] = 0;
1738
              }
1739
            }
1740
          }
1741
        }
1742
      }
1743
    }
1744
    else
1745
    {
1746
      for (i=0;i<4;i++)
1747
      {
1748
        if (currMB->b8mode[i] == 0)
1749
        {
1750
          for(j=2*(i>>1);j<2*(i>>1)+2;j++)
1751
          {
1752
            for(k=2*(i&0x01);k<2*(i&0x01)+2;k++)
1753
            {
1754
 
1755
              int list_offset = ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? 4 : 2 : 0;
1756
              int imgblock_y  = ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? (img->block_y-4)>>1 : img->block_y>>1 : img->block_y;
1757
              int refList = co_located_ref_idx[LIST_0 ][imgblock_y+j][img->block_x+k]== -1 ? LIST_1 : LIST_0;
1758
              int ref_idx = co_located_ref_idx[refList][imgblock_y + j][img->block_x + k];
1759
              int mapped_idx=-1, iref;
1760
 
1761
              if (ref_idx == -1)
1762
              {
1763
                dec_picture->ref_idx [LIST_0][img->block_y + j][img->block_x + k] = 0;
1764
                dec_picture->ref_idx [LIST_1][img->block_y + j][img->block_x + k] = 0;
1765
              }
1766
              else
1767
              {
1768
                for (iref=0;iref<imin(img->num_ref_idx_l0_active,listXsize[LIST_0 + list_offset]);iref++)
1769
                {
1770
                  int curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
1771
 
1772
                  if(img->structure==0 && curr_mb_field==0)
1773
                  {
1774
                    // If the current MB is a frame MB and the colocated is from a field picture,
1775
                    // then the co_located_ref_id may have been generated from the wrong value of
1776
                    // frame_poc if it references it's complementary field, so test both POC values
1777
                    if(listX[0][iref]->top_poc*2 == co_located_ref_id[refList][imgblock_y + j][img->block_x + k]
1778
                    || listX[0][iref]->bottom_poc*2 == co_located_ref_id[refList][imgblock_y + j][img->block_x + k])
1779
                    {
1780
                      mapped_idx=iref;
1781
                      break;
1782
                    }
1783
                    else //! invalid index. Default to zero even though this case should not happen
1784
                      mapped_idx=INVALIDINDEX;
1785
                    continue;
1786
                  }
1787
                  if (dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][iref]==co_located_ref_id[refList][imgblock_y + j][img->block_x + k])
1788
                  {
1789
                    mapped_idx=iref;
1790
                    break;
1791
                  }
1792
                  else //! invalid index. Default to zero even though this case should not happen
1793
                    mapped_idx=INVALIDINDEX;
1794
                }
1795
                if (INVALIDINDEX == mapped_idx)
1796
                {
1797
                  error("temporal direct error\ncolocated block has ref that is unavailable",-1111);
1798
                }
1799
                dec_picture->ref_idx [LIST_0][img->block_y + j][img->block_x + k] = mapped_idx;
1800
                dec_picture->ref_idx [LIST_1][img->block_y + j][img->block_x + k] = 0;
1801
              }
1802
            }
1803
          }
1804
        }
1805
      }
1806
    }
1807
  }
1808
 
1809
  //  If multiple ref. frames, read reference frame for the MB *********************************
1810
  if(img->num_ref_idx_l0_active>1)
1811
  {
1812
    flag_mode = ( img->num_ref_idx_l0_active == 2 ? 1 : 0);
1813
 
1814
    currSE.type = SE_REFFRAME;
1815
    dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
1816
 
1817
    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)   currSE.mapping = linfo_ue;
1818
    else                                                      currSE.reading = readRefFrame_CABAC;
1819
 
1820
    for (j0=0; j0<4; j0+=step_v0)
1821
    {
1822
      for (i0=0; i0<4; i0+=step_h0)
1823
      {
1824
        k=2*(j0>>1)+(i0>>1);
1825
        if ((currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2) && currMB->b8mode[k]!=0)
1826
        {
1827
          TRACE_STRING("ref_idx_l0");
1828
 
1829
          img->subblock_x = i0;
1830
          img->subblock_y = j0;
1831
 
1832
          if (!IS_P8x8 (currMB) || bframe || (!bframe && !img->allrefzero))
1833
          {
1834
            currSE.context = BType2CtxRef (currMB->b8mode[k]);
1835
            if( (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) && flag_mode )
1836
            {
1837
              currSE.len = 1;
1838
              readSyntaxElement_FLC(&currSE, dP->bitstream);
1839
              currSE.value1 = 1 - currSE.value1;
1840
            }
1841
            else
1842
            {
1843
              currSE.value2 = LIST_0;
1844
              dP->readSyntaxElement (&currSE,img,dP);
1845
            }
1846
            refframe = currSE.value1;
1847
 
1848
          }
1849
          else
1850
          {
1851
            refframe = 0;
1852
          }
1853
 
1854
          for (j=img->block_y +j0; j<img->block_y +j0+step_v0;j++)
1855
            memset(&dec_picture->ref_idx[LIST_0][j][img->block_x + i0], refframe, step_h0 * sizeof(char));
1856
        }
1857
      }
1858
    }
1859
  }
1860
  else
1861
  {
1862
    for (j0=0; j0<4; j0+=step_v0)
1863
    {
1864
      for (i0=0; i0<4; i0+=step_h0)
1865
      {
1866
        k=2*(j0>>1)+(i0>>1);
1867
        if ((currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2) && currMB->b8mode[k]!=0)
1868
        {
1869
          for (j=img->block_y + j0; j < img->block_y + j0+step_v0;j++)
1870
            memset(&dec_picture->ref_idx[LIST_0][j][img->block_x + i0], 0, step_h0 * sizeof(char));
1871
        }
1872
      }
1873
    }
1874
  }
1875
 
1876
  //  If backward multiple ref. frames, read backward reference frame for the MB *********************************
1877
  if(img->num_ref_idx_l1_active>1)
1878
  {
1879
    flag_mode = ( img->num_ref_idx_l1_active == 2 ? 1 : 0);
1880
 
1881
    currSE.type = SE_REFFRAME;
1882
    dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
1883
    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
1884
      currSE.mapping = linfo_ue;
1885
    else
1886
      currSE.reading = readRefFrame_CABAC;
1887
 
1888
    for (j0=0; j0<4; j0+=step_v0)
1889
    {
1890
      for (i0=0; i0<4; i0+=step_h0)
1891
      {
1892
        k=2*(j0>>1)+(i0>>1);
1893
        if ((currMB->b8pdir[k]==1 || currMB->b8pdir[k]==2) && currMB->b8mode[k]!=0)
1894
        {
1895
          TRACE_STRING("ref_idx_l1");
1896
 
1897
          img->subblock_x = i0;
1898
          img->subblock_y = j0;
1899
 
1900
          currSE.context = BType2CtxRef (currMB->b8mode[k]);
1901
          if( (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) && flag_mode )
1902
          {
1903
            currSE.len = 1;
1904
            readSyntaxElement_FLC(&currSE, dP->bitstream);
1905
            currSE.value1 = 1-currSE.value1;
1906
          }
1907
          else
1908
          {
1909
            currSE.value2 = LIST_1;
1910
            dP->readSyntaxElement (&currSE,img,dP);
1911
          }
1912
          refframe = currSE.value1;
1913
 
1914
          for (j=img->block_y + j0; j<img->block_y + j0+step_v0;j++)
1915
          {
1916
            memset(&dec_picture->ref_idx[LIST_1][j][img->block_x + i0], refframe, step_h0 * sizeof(char));
1917
          }
1918
        }
1919
      }
1920
    }
1921
  }
1922
  else
1923
  {
1924
    for (j0=0; j0<4; j0+=step_v0)
1925
    {
1926
      for (i0=0; i0<4; i0+=step_h0)
1927
      {
1928
        k=2*(j0>>1)+(i0>>1);
1929
        if ((currMB->b8pdir[k]==1 || currMB->b8pdir[k]==2) && currMB->b8mode[k]!=0)
1930
        {
1931
          for (j=img->block_y + j0; j<img->block_y + j0+step_v0;j++)
1932
            memset(&dec_picture->ref_idx[LIST_1][ j][img->block_x + i0], 0, step_h0 * sizeof(char));
1933
        }
1934
      }
1935
    }
1936
  }
1937
 
1938
  //=====  READ FORWARD MOTION VECTORS =====
1939
  currSE.type = SE_MVD;
1940
  dP = &(currSlice->partArr[partMap[SE_MVD]]);
1941
 
1942
  if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) currSE.mapping = linfo_se;
1943
  else                                                  currSE.reading = readMVD_CABAC;
1944
 
1945
  for (j0=0; j0<4; j0+=step_v0)
1946
    for (i0=0; i0<4; i0+=step_h0)
1947
    {
1948
      k=2*(j0>>1)+(i0>>1);
1949
 
1950
      if ((currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2) && (currMB->b8mode[k] !=0))//has forward vector
1951
      {
1952
        mv_mode  = currMB->b8mode[k];
1953
        step_h   = BLOCK_STEP [mv_mode][0];
1954
        step_v   = BLOCK_STEP [mv_mode][1];
1955
 
1956
        refframe = dec_picture->ref_idx[LIST_0][img->block_y+j0][img->block_x+i0];
1957
 
1958
        for (j=j0; j<j0+step_v0; j+=step_v)
1959
        {
1960
          j4 = img->block_y+j;
1961
          for (i=i0; i<i0+step_h0; i+=step_h)
1962
          {
1963
            i4 = img->block_x+i;
1964
 
1965
            // first make mv-prediction
1966
            SetMotionVectorPredictor (img, pmv, refframe, LIST_0, dec_picture->ref_idx, dec_picture->mv, i, j, 4*step_h, 4*step_v);
1967
 
1968
            for (k=0; k < 2; k++)
1969
            {
1970
              TRACE_STRING("mvd_l0");
1971
 
1972
              img->subblock_x = i; // position used for context determination
1973
              img->subblock_y = j; // position used for context determination
1974
              currSE.value2 = k<<1; // identifies the component; only used for context determination
1975
              dP->readSyntaxElement(&currSE,img,dP);
1976
              curr_mvd = currSE.value1;
1977
 
1978
              vec=curr_mvd+pmv[k];           /* find motion vector */
1979
 
1980
              for(jj=0;jj<step_v;jj++)
1981
              {
1982
                for(ii=0;ii<step_h;ii++)
1983
                {
1984
                  dec_picture->mv [LIST_0][j4+jj][i4+ii][k] = vec;
1985
                  currMB->mvd     [LIST_0][j +jj][i +ii][k] = curr_mvd;
1986
                }
1987
              }
1988
            }
1989
          }
1990
        }
1991
      }
1992
      else if (currMB->b8mode[k=2*(j0>>1)+(i0>>1)]==0)
1993
      {
1994
        if (!img->direct_spatial_mv_pred_flag)
1995
        {
1996
          int list_offset = ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? 4 : 2 : 0;
1997
          int imgblock_y  = ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? (img->block_y-4)>>1:img->block_y>>1 : img->block_y;
1998
 
1999
          int refList = (co_located_ref_idx[LIST_0 ][imgblock_y+j0][img->block_x+i0]== -1 ? LIST_1 : LIST_0);
2000
          int ref_idx =  co_located_ref_idx[refList][imgblock_y+j0][img->block_x+i0];
2001
 
2002
          if (ref_idx==-1)
2003
          {
2004
            for (j4=img->block_y+j0; j4<img->block_y+j0+step_v0; j4++)
2005
            {
2006
              for (i4=img->block_x+i0; i4<img->block_x+i0+step_h0; i4++)
2007
              {
2008
                dec_picture->ref_idx [LIST_1][j4][i4]=0;
2009
                dec_picture->ref_idx [LIST_0][j4][i4]=0;
2010
 
2011
                for (ii=0; ii < 2; ii++)
2012
                {
2013
                  dec_picture->mv [LIST_0][j4][i4][ii]=0;
2014
                  dec_picture->mv [LIST_1][j4][i4][ii]=0;
2015
                }
2016
              }
2017
            }
2018
          }
2019
          else
2020
          {
2021
            int mapped_idx=-1, iref;
2022
            int j6;
2023
 
2024
            for (iref = 0; iref < imin(img->num_ref_idx_l0_active, listXsize[LIST_0 + list_offset]); iref++)
2025
            {
2026
              int curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
2027
 
2028
              if(img->structure==0 && curr_mb_field==0)
2029
              {
2030
                // If the current MB is a frame MB and the colocated is from a field picture,
2031
                // then the co_located_ref_id may have been generated from the wrong value of
2032
                // frame_poc if it references it's complementary field, so test both POC values
2033
                if(listX[0][iref]->top_poc * 2    == co_located_ref_id[refList][imgblock_y + j0][img->block_x + i0]
2034
                || listX[0][iref]->bottom_poc * 2 == co_located_ref_id[refList][imgblock_y + j0][img->block_x + i0])
2035
                {
2036
                  mapped_idx=iref;
2037
                  break;
2038
                }
2039
                else //! invalid index. Default to zero even though this case should not happen
2040
                  mapped_idx=INVALIDINDEX;
2041
                continue;
2042
              }
2043
              if (dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][iref]==co_located_ref_id[refList][imgblock_y+j0][img->block_x+i0])
2044
              {
2045
                mapped_idx=iref;
2046
                break;
2047
              }
2048
              else //! invalid index. Default to zero even though this case should not happen
2049
                mapped_idx=INVALIDINDEX;
2050
            }
2051
 
2052
            if (INVALIDINDEX == mapped_idx)
2053
            {
2054
              error("temporal direct error\ncolocated block has ref that is unavailable",-1111);
2055
            }
2056
 
2057
 
2058
            for (j=j0; j<j0+step_v0; j++)
2059
            {
2060
              j4 = img->block_y+j;
2061
              j6 = imgblock_y + j;
2062
 
2063
              for (i4=img->block_x+i0; i4<img->block_x+i0+step_h0; i4++)
2064
              {
2065
                mv_scale = img->mvscale[LIST_0 + list_offset][mapped_idx];
2066
 
2067
                dec_picture->ref_idx [LIST_0][j4][i4] = mapped_idx;
2068
                dec_picture->ref_idx [LIST_1][j4][i4] = 0;
2069
 
2070
 
2071
                for (ii=0; ii < 2; ii++)
2072
                {
2073
                  if (mv_scale == 9999 || listX[LIST_0+list_offset][mapped_idx]->is_long_term)
2074
                  {
2075
                    dec_picture->mv  [LIST_0][j4][i4][ii] = co_located_mv[refList][j6][i4][ii];
2076
                    dec_picture->mv  [LIST_1][j4][i4][ii] = 0;
2077
                  }
2078
                  else
2079
                  {
2080
                    dec_picture->mv  [LIST_0][j4][i4][ii] = (mv_scale * co_located_mv[refList][j6][i4][ii] + 128 ) >> 8;
2081
                    dec_picture->mv  [LIST_1][j4][i4][ii] = dec_picture->mv[LIST_0][j4][i4][ii] - co_located_mv[refList][j6][i4][ii];
2082
                  }
2083
                }
2084
              }
2085
            }
2086
          }
2087
        }
2088
      }
2089
    }
2090
 
2091
  //=====  READ BACKWARD MOTION VECTORS =====
2092
  currSE.type = SE_MVD;
2093
  dP          = &(currSlice->partArr[partMap[SE_MVD]]);
2094
 
2095
  if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) currSE.mapping = linfo_se;
2096
  else                                                    currSE.reading = readMVD_CABAC;
2097
 
2098
  for (j0=0; j0<4; j0+=step_v0)
2099
  {
2100
    for (i0=0; i0<4; i0+=step_h0)
2101
    {
2102
      k=2*(j0>>1)+(i0>>1);
2103
      if ((currMB->b8pdir[k]==1 || currMB->b8pdir[k]==2) && (currMB->b8mode[k]!=0))//has backward vector
2104
      {
2105
        mv_mode  = currMB->b8mode[k];
2106
        step_h   = BLOCK_STEP [mv_mode][0];
2107
        step_v   = BLOCK_STEP [mv_mode][1];
2108
 
2109
        refframe = dec_picture->ref_idx[LIST_1][img->block_y+j0][img->block_x+i0];
2110
 
2111
        for (j=j0; j<j0+step_v0; j+=step_v)
2112
        {
2113
          j4 = img->block_y+j;
2114
          for (i=i0; i<i0+step_h0; i+=step_h)
2115
          {
2116
            i4 = img->block_x+i;
2117
 
2118
            // first make mv-prediction
2119
            SetMotionVectorPredictor (img, pmv, refframe, LIST_1, dec_picture->ref_idx, dec_picture->mv, i, j, 4*step_h, 4*step_v);
2120
 
2121
            for (k=0; k < 2; k++)
2122
            {
2123
              TRACE_STRING("mvd_l1");
2124
 
2125
              img->subblock_x = i; // position used for context determination
2126
              img->subblock_y = j; // position used for context determination
2127
              currSE.value2   = (k<<1) +1; // identifies the component; only used for context determination
2128
              dP->readSyntaxElement(&currSE,img,dP);
2129
              curr_mvd = currSE.value1;
2130
 
2131
              vec=curr_mvd+pmv[k];           /* find motion vector */
2132
 
2133
              for(jj=0;jj<step_v;jj++)
2134
              {
2135
                for(ii=0;ii<step_h;ii++)
2136
                {
2137
                  dec_picture->mv  [LIST_1][j4+jj][i4+ii][k] = vec;
2138
                  currMB->mvd      [LIST_1][j+jj] [i+ii] [k] = curr_mvd;
2139
                }
2140
              }
2141
            }
2142
          }
2143
        }
2144
      }
2145
    }
2146
  }
2147
  // record reference picture Ids for deblocking decisions
2148
 
2149
 
2150
  for(j4=img->block_y;j4<(img->block_y+4);j4++)
2151
  {
2152
    for(i4=img->block_x;i4<(img->block_x+4);i4++)
2153
    {
2154
      if (dec_picture->ref_idx[LIST_0][j4][i4]>=0)
2155
         dec_picture->ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][(short)dec_picture->ref_idx[LIST_0][j4][i4]];
2156
      else
2157
         dec_picture->ref_pic_id[LIST_0][j4][i4] = INT64_MIN;
2158
      if (dec_picture->ref_idx[LIST_1][j4][i4]>=0)
2159
         dec_picture->ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_1 + list_offset][(short)dec_picture->ref_idx[LIST_1][j4][i4]];
2160
      else
2161
         dec_picture->ref_pic_id[LIST_1][j4][i4] = INT64_MIN;
2162
    }
2163
  }
2164
}
2165
 
2166
 
2167
 
2168
/*!
2169
 ************************************************************************
2170
 * \brief
2171
 *    Get the Prediction from the Neighboring Blocks for Number of Nonzero Coefficients
2172
 *
2173
 *    Luma Blocks
2174
 ************************************************************************
2175
 */
2176
int predict_nnz(struct img_par *img, int i,int j)
2177
{
2178
  PixelPos pix;
2179
 
2180
  int pred_nnz = 0;
2181
  int cnt      = 0;
2182
  int mb_nr    = img->current_mb_nr;
2183
  Macroblock *currMB = &(img->mb_data[mb_nr]);
2184
 
2185
  // left block
2186
  getLuma4x4Neighbour(mb_nr, (i<<2) - 1, (j<<2), &pix);
2187
 
2188
  if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && (img->currentSlice->dp_mode==PAR_DP_3))
2189
  {
2190
    pix.available &= img->intra_block[pix.mb_addr];
2191
    if (!pix.available)
2192
      cnt++;
2193
  }
2194
 
2195
  if (pix.available)
2196
  {
2197
    pred_nnz = img->nz_coeff [pix.mb_addr ][pix.x][pix.y];
2198
    cnt++;
2199
  }
2200
 
2201
  // top block
2202
  getLuma4x4Neighbour(mb_nr, (i<<2), (j<<2) - 1, &pix);
2203
 
2204
  if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && (img->currentSlice->dp_mode==PAR_DP_3))
2205
  {
2206
    pix.available &= img->intra_block[pix.mb_addr];
2207
    if (!pix.available)
2208
      cnt++;
2209
  }
2210
 
2211
  if (pix.available)
2212
  {
2213
    pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][pix.y];
2214
    cnt++;
2215
  }
2216
 
2217
  if (cnt==2)
2218
  {
2219
    pred_nnz++;
2220
    pred_nnz>>=1;
2221
  }
2222
 
2223
  return pred_nnz;
2224
}
2225
 
2226
 
2227
/*!
2228
 ************************************************************************
2229
 * \brief
2230
 *    Get the Prediction from the Neighboring Blocks for Number of Nonzero Coefficients
2231
 *
2232
 *    Chroma Blocks
2233
 ************************************************************************
2234
 */
2235
int predict_nnz_chroma(struct img_par *img, int i,int j)
2236
{
2237
  PixelPos pix;
2238
 
2239
  int pred_nnz = 0;
2240
  int cnt      =0;
2241
  int mb_nr    = img->current_mb_nr;
2242
  static const int j_off_tab [12] = {0,0,0,0,4,4,4,4,8,8,8,8};
2243
  int j_off = j_off_tab[j];
2244
  Macroblock *currMB = &(img->mb_data[mb_nr]);
2245
 
2246
  if (dec_picture->chroma_format_idc != YUV444)
2247
  {
2248
    //YUV420 and YUV422
2249
    // left block
2250
    getChroma4x4Neighbour(mb_nr, ((i&0x01)<<2) - 1, ((j-4)<<2), &pix);
2251
 
2252
    if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && (img->currentSlice->dp_mode==PAR_DP_3))
2253
    {
2254
      pix.available &= img->intra_block[pix.mb_addr];
2255
      if (!pix.available)
2256
        cnt++;
2257
    }
2258
 
2259
    if (pix.available)
2260
    {
2261
      pred_nnz = img->nz_coeff [pix.mb_addr ][2 * (i>>1) + pix.x][4 + pix.y];
2262
      cnt++;
2263
    }
2264
 
2265
    // top block
2266
    getChroma4x4Neighbour(mb_nr, ((i&0x01)<<2), ((j-4)<<2) - 1, &pix);
2267
 
2268
    if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && (img->currentSlice->dp_mode==PAR_DP_3))
2269
    {
2270
      pix.available &= img->intra_block[pix.mb_addr];
2271
      if (!pix.available)
2272
        cnt++;
2273
    }
2274
 
2275
    if (pix.available)
2276
    {
2277
      pred_nnz += img->nz_coeff [pix.mb_addr ][2 * (i>>1) + pix.x][4 + pix.y];
2278
      cnt++;
2279
    }
2280
  }
2281
  else
2282
  {
2283
    //YUV444
2284
    // left block
2285
    getChroma4x4Neighbour(mb_nr, (i<<2) - 1, ((j-j_off)<<2), &pix);
2286
 
2287
    if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && (img->currentSlice->dp_mode==PAR_DP_3))
2288
    {
2289
      pix.available &= img->intra_block[pix.mb_addr];
2290
      cnt--;
2291
    }
2292
 
2293
    if (pix.available)
2294
    {
2295
      pred_nnz = img->nz_coeff [pix.mb_addr ][pix.x][j_off + pix.y];
2296
      cnt++;
2297
    }
2298
 
2299
    // top block
2300
    getChroma4x4Neighbour(mb_nr, (i<<2), ((j-j_off)<<2) -1, &pix);
2301
 
2302
    if (IS_INTRA(currMB) && pix.available && active_pps->constrained_intra_pred_flag && (img->currentSlice->dp_mode==PAR_DP_3))
2303
    {
2304
      pix.available &= img->intra_block[pix.mb_addr];
2305
      cnt--;
2306
    }
2307
 
2308
    if (pix.available)
2309
    {
2310
      pred_nnz += img->nz_coeff [pix.mb_addr ][pix.x][j_off + pix.y];
2311
      cnt++;
2312
    }
2313
  }
2314
 
2315
  if (cnt==2)
2316
  {
2317
    pred_nnz++;
2318
    pred_nnz>>=1;
2319
  }
2320
 
2321
  return pred_nnz;
2322
}
2323
 
2324
 
2325
/*!
2326
 ************************************************************************
2327
 * \brief
2328
 *    Reads coeff of an 4x4 block (CAVLC)
2329
 *
2330
 * \author
2331
 *    Karl Lillevold <karll@real.com>
2332
 *    contributions by James Au <james@ubvideo.com>
2333
 ************************************************************************
2334
 */
2335
 
2336
 
2337
void readCoeff4x4_CAVLC (struct img_par *img,struct inp_par *inp,
2338
                        int block_type,
2339
                        int i, int j, int levarr[16], int runarr[16],
2340
                        int *number_coefficients)
2341
{
2342
  int mb_nr = img->current_mb_nr;
2343
  Macroblock *currMB = &img->mb_data[mb_nr];
2344
  SyntaxElement currSE;
2345
  Slice *currSlice = img->currentSlice;
2346
  DataPartition *dP;
2347
  int *partMap = assignSE2partition[currSlice->dp_mode];
2348
 
2349
 
2350
  int k, code, vlcnum;
2351
  int numcoeff, numtrailingones, numcoeff_vlc;
2352
  int level_two_or_higher;
2353
  int numones, totzeros, level, cdc=0, cac=0;
2354
  int zerosleft, ntr, dptype = 0;
2355
  int max_coeff_num = 0, nnz;
2356
  char type[15];
2357
  static int incVlc[] = {0,3,6,12,24,48,32768};    // maximum vlc = 6
2358
 
2359
  numcoeff = 0;
2360
 
2361
  switch (block_type)
2362
  {
2363
  case LUMA:
2364
    max_coeff_num = 16;
2365
#if TRACE
2366
    sprintf(type, "%s", "Luma");
2367
#endif
2368
    dptype = IS_INTRA (currMB) ? SE_LUM_AC_INTRA : SE_LUM_AC_INTER;
2369
    break;
2370
  case LUMA_INTRA16x16DC:
2371
    max_coeff_num = 16;
2372
#if TRACE
2373
    sprintf(type, "%s", "Lum16DC");
2374
#endif
2375
    dptype = SE_LUM_DC_INTRA;
2376
    break;
2377
  case LUMA_INTRA16x16AC:
2378
    max_coeff_num = 15;
2379
#if TRACE
2380
    sprintf(type, "%s", "Lum16AC");
2381
#endif
2382
    dptype = SE_LUM_AC_INTRA;
2383
    break;
2384
 
2385
  case CHROMA_DC:
2386
    max_coeff_num = img->num_cdc_coeff;
2387
    cdc = 1;
2388
#if TRACE
2389
    sprintf(type, "%s", "ChrDC");
2390
#endif
2391
    dptype = IS_INTRA (currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER;
2392
    break;
2393
  case CHROMA_AC:
2394
    max_coeff_num = 15;
2395
    cac = 1;
2396
#if TRACE
2397
    sprintf(type, "%s", "ChrAC");
2398
#endif
2399
    dptype = IS_INTRA (currMB) ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER;
2400
    break;
2401
  default:
2402
    error ("readCoeff4x4_CAVLC: invalid block type", 600);
2403
    break;
2404
  }
2405
 
2406
  currSE.type = dptype;
2407
  dP = &(currSlice->partArr[partMap[dptype]]);
2408
 
2409
  img->nz_coeff[img->current_mb_nr][i][j] = 0;
2410
 
2411
 
2412
  if (!cdc)
2413
  {
2414
    // luma or chroma AC
2415
    if (!cac)
2416
    {
2417
      nnz = predict_nnz(img, i, j);
2418
    }
2419
    else
2420
    {
2421
      nnz = predict_nnz_chroma(img, i, j);
2422
    }
2423
 
2424
    if (nnz < 2)
2425
    {
2426
      numcoeff_vlc = 0;
2427
    }
2428
    else if (nnz < 4)
2429
    {
2430
      numcoeff_vlc = 1;
2431
    }
2432
    else if (nnz < 8)
2433
    {
2434
      numcoeff_vlc = 2;
2435
    }
2436
    else //
2437
    {
2438
      numcoeff_vlc = 3;
2439
    }
2440
 
2441
    currSE.value1 = numcoeff_vlc;
2442
 
2443
    readSyntaxElement_NumCoeffTrailingOnes(&currSE, dP, type);
2444
 
2445
    numcoeff =  currSE.value1;
2446
    numtrailingones =  currSE.value2;
2447
 
2448
    img->nz_coeff[img->current_mb_nr][i][j] = numcoeff;
2449
  }
2450
  else
2451
  {
2452
    // chroma DC
2453
    readSyntaxElement_NumCoeffTrailingOnesChromaDC(&currSE, dP);
2454
 
2455
    numcoeff =  currSE.value1;
2456
    numtrailingones =  currSE.value2;
2457
  }
2458
 
2459
 
2460
  for (k = 0; k < max_coeff_num; k++)
2461
  {
2462
    levarr[k] = 0;
2463
    runarr[k] = 0;
2464
  }
2465
 
2466
  numones = numtrailingones;
2467
  *number_coefficients = numcoeff;
2468
 
2469
  if (numcoeff)
2470
  {
2471
    if (numtrailingones)
2472
    {
2473
 
2474
      currSE.len = numtrailingones;
2475
 
2476
#if TRACE
2477
      snprintf(currSE.tracestring,
2478
        TRACESTRING_SIZE, "%s trailing ones sign (%d,%d)", type, i, j);
2479
#endif
2480
 
2481
      readSyntaxElement_FLC (&currSE, dP->bitstream);
2482
 
2483
      code = currSE.inf;
2484
      ntr = numtrailingones;
2485
      for (k = numcoeff-1; k > numcoeff-1-numtrailingones; k--)
2486
      {
2487
        ntr --;
2488
        levarr[k] = (code>>ntr)&1 ? -1 : 1;
2489
      }
2490
    }
2491
 
2492
    // decode levels
2493
    level_two_or_higher = (numcoeff > 3 && numtrailingones == 3)? 0 : 1;
2494
    vlcnum = (numcoeff > 10 && numtrailingones < 3) ? 1 : 0;
2495
 
2496
    for (k = numcoeff - 1 - numtrailingones; k >= 0; k--)
2497
    {
2498
 
2499
#if TRACE
2500
      snprintf(currSE.tracestring,
2501
        TRACESTRING_SIZE, "%s lev (%d,%d) k=%d vlc=%d ", type,
2502
          i, j, k, vlcnum);
2503
#endif
2504
 
2505
      if (vlcnum == 0)
2506
          readSyntaxElement_Level_VLC0(&currSE, dP);
2507
      else
2508
          readSyntaxElement_Level_VLCN(&currSE, vlcnum, dP);
2509
 
2510
      if (level_two_or_higher)
2511
      {
2512
        currSE.inf += (currSE.inf > 0) ? 1 : -1;
2513
        level_two_or_higher = 0;
2514
      }
2515
 
2516
      level = levarr[k] = currSE.inf;
2517
      if (iabs(level) == 1)
2518
        numones ++;
2519
 
2520
      // update VLC table
2521
      if (iabs(level)>incVlc[vlcnum])
2522
        vlcnum++;
2523
 
2524
      if (k == numcoeff - 1 - numtrailingones && iabs(level)>3)
2525
        vlcnum = 2;
2526
 
2527
    }
2528
 
2529
    if (numcoeff < max_coeff_num)
2530
    {
2531
      // decode total run
2532
      vlcnum = numcoeff-1;
2533
      currSE.value1 = vlcnum;
2534
 
2535
#if TRACE
2536
      snprintf(currSE.tracestring,
2537
        TRACESTRING_SIZE, "%s totalrun (%d,%d) vlc=%d ", type, i,j, vlcnum);
2538
#endif
2539
      if (cdc)
2540
        readSyntaxElement_TotalZerosChromaDC(&currSE, dP);
2541
      else
2542
        readSyntaxElement_TotalZeros(&currSE, dP);
2543
 
2544
      totzeros = currSE.value1;
2545
    }
2546
    else
2547
    {
2548
      totzeros = 0;
2549
    }
2550
 
2551
    // decode run before each coefficient
2552
    zerosleft = totzeros;
2553
    i = numcoeff - 1;
2554
    if (zerosleft > 0 && i > 0)
2555
    {
2556
      do
2557
      {
2558
        // select VLC for runbefore
2559
        vlcnum = zerosleft - 1;
2560
        if (vlcnum > RUNBEFORE_NUM - 1)
2561
          vlcnum = RUNBEFORE_NUM - 1;
2562
 
2563
        currSE.value1 = vlcnum;
2564
#if TRACE
2565
        snprintf(currSE.tracestring,
2566
          TRACESTRING_SIZE, "%s run (%d,%d) k=%d vlc=%d ",
2567
            type, i, j, i, vlcnum);
2568
#endif
2569
 
2570
        readSyntaxElement_Run(&currSE, dP);
2571
        runarr[i] = currSE.value1;
2572
 
2573
        zerosleft -= runarr[i];
2574
        i --;
2575
      } while (zerosleft != 0 && i != 0);
2576
    }
2577
    runarr[i] = zerosleft;
2578
 
2579
  } // if numcoeff
2580
}
2581
 
2582
/*!
2583
 ************************************************************************
2584
 * \brief
2585
 *    Calculate the quantisation and inverse quantisation parameters
2586
 *
2587
 ************************************************************************
2588
 */
2589
void CalculateQuant8Param()
2590
{
2591
  int i, j, k, temp;
2592
 
2593
  for(k=0; k<6; k++)
2594
    for(j=0; j<8; j++)
2595
    {
2596
      for(i=0; i<8; i++)
2597
      {
2598
        temp = (i<<3)+j;
2599
        InvLevelScale8x8Luma_Intra[k][i][j] = dequant_coef8[k][j][i]*qmatrix[6][temp];
2600
        InvLevelScale8x8Luma_Inter[k][i][j] = dequant_coef8[k][j][i]*qmatrix[7][temp];
2601
      }
2602
    }
2603
}
2604
 
2605
/*!
2606
************************************************************************
2607
* \brief
2608
*    Get coefficients (run/level) of one 8x8 block
2609
*    from the NAL (CABAC Mode)
2610
************************************************************************
2611
*/
2612
void readLumaCoeff8x8_CABAC (struct img_par *img,struct inp_par *inp, int b8)
2613
{
2614
  int i,j,k;
2615
  int level = 1;
2616
  int mb_nr = img->current_mb_nr;
2617
  Macroblock *currMB = &img->mb_data[mb_nr];
2618
  int cbp = currMB->cbp;
2619
  SyntaxElement currSE;
2620
  Slice *currSlice = img->currentSlice;
2621
  DataPartition *dP;
2622
  int *partMap = assignSE2partition[currSlice->dp_mode];
2623
  int start_scan = 0; // take all coeffs
2624
  int coef_ctr = start_scan - 1;// i0, j0;
2625
  int boff_x, boff_y;
2626
 
2627
  int run, len;
2628
 
2629
  int qp_per    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)/6;
2630
  int qp_rem    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)%6;
2631
  Boolean lossless_qpprime = (Boolean) ((img->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
2632
  int (*InvLevelScale8x8)[8] = IS_INTRA(currMB)? InvLevelScale8x8Luma_Intra[qp_rem] : InvLevelScale8x8Luma_Inter[qp_rem];
2633
  // select scan type
2634
  const byte (*pos_scan8x8)[2] = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8 : FIELD_SCAN8x8;
2635
 
2636
  img->is_intra_block = IS_INTRA(currMB);
2637
 
2638
  if (cbp & (1<<b8))  // are there any coeff in current block at all
2639
  {
2640
    // === set offset in current macroblock ===
2641
    boff_x = (b8&0x01) << 3;
2642
    boff_y = (b8 >> 1) << 3;
2643
 
2644
    img->subblock_x = boff_x >> 2; // position for coeff_count ctx
2645
    img->subblock_y = boff_y >> 2; // position for coeff_count ctx
2646
 
2647
    if(!lossless_qpprime)
2648
    {
2649
      for(k=start_scan;(k < 65) && (level != 0);k++)
2650
      {
2651
        //============ read =============
2652
        /*
2653
        * make distinction between INTRA and INTER coded
2654
        * luminance coefficients
2655
        */
2656
        currSE.context = LUMA_8x8;
2657
        currSE.type    = ((img->is_intra_block == 1)
2658
          ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
2659
          : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
2660
 
2661
#if TRACE
2662
        sprintf(currSE.tracestring, "Luma8x8 sng ");
2663
#endif
2664
        dP = &(currSlice->partArr[partMap[currSE.type]]);
2665
        currSE.reading = readRunLevel_CABAC;
2666
 
2667
        dP->readSyntaxElement(&currSE,img,dP);
2668
        level = currSE.value1;
2669
        run   = currSE.value2;
2670
        len   = currSE.len;
2671
 
2672
        //============ decode =============
2673
        if (level != 0)    /* leave if len=1 */
2674
        {
2675
          coef_ctr += run + 1;
2676
 
2677
          i=pos_scan8x8[coef_ctr][0];
2678
          j=pos_scan8x8[coef_ctr][1];
2679
 
2680
          currMB->cbp_blk |= 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
2681
          img->m7[boff_y + j][boff_x + i] = rshift_rnd_sf((level * InvLevelScale8x8[j][i]) << qp_per, 6); // dequantization
2682
        }
2683
      }
2684
    }
2685
    else
2686
    {
2687
      for(k=start_scan;(k < 65) && (level != 0);k++)
2688
      {
2689
        //============ read =============
2690
        /*
2691
        * make distinction between INTRA and INTER coded
2692
        * luminance coefficients
2693
        */
2694
        currSE.context = LUMA_8x8;
2695
        currSE.type    = ((img->is_intra_block == 1)
2696
          ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
2697
          : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
2698
 
2699
#if TRACE
2700
        sprintf(currSE.tracestring, "Luma8x8 sng ");
2701
#endif
2702
        dP = &(currSlice->partArr[partMap[currSE.type]]);
2703
        currSE.reading = readRunLevel_CABAC;
2704
 
2705
        dP->readSyntaxElement(&currSE,img,dP);
2706
        level = currSE.value1;
2707
        run   = currSE.value2;
2708
        len   = currSE.len;
2709
 
2710
        //============ decode =============
2711
        if (level != 0)    /* leave if len=1 */
2712
        {
2713
          coef_ctr += run+1;
2714
 
2715
          i=pos_scan8x8[coef_ctr][0];
2716
          j=pos_scan8x8[coef_ctr][1];
2717
 
2718
          currMB->cbp_blk |= 51 << (4 * b8 - 2 * (b8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
2719
          img->m7[boff_y + j][boff_x + i] = level;
2720
        }
2721
      }
2722
    }
2723
  }
2724
}
2725
 
2726
/*!
2727
 ************************************************************************
2728
 * \brief
2729
 *    Get coded block pattern and coefficients (run/level)
2730
 *    from the NAL
2731
 ************************************************************************
2732
 */
2733
void readCBPandCoeffsFromNAL(struct img_par *img,struct inp_par *inp)
2734
{
2735
  int i,j,k;
2736
  int level;
2737
  int mb_nr = img->current_mb_nr;
2738
  int ii,jj;
2739
  int m2,jg2;// i1,j1;
2740
  Macroblock *currMB = &img->mb_data[mb_nr];
2741
  int cbp;
2742
  SyntaxElement currSE;
2743
  Slice *currSlice = img->currentSlice;
2744
  DataPartition *dP;
2745
  int *partMap = assignSE2partition[currSlice->dp_mode];
2746
  int coef_ctr, i0, j0, b8;
2747
  int ll;
2748
  int block_x,block_y;
2749
  int start_scan;
2750
  int run, len;
2751
  int levarr[16], runarr[16], numcoeff;
2752
 
2753
  int qp_const;
2754
  int qp_per    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)/6;
2755
  int qp_rem    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)%6;
2756
  int smb       = ((img->type==SP_SLICE) && IS_INTER (currMB)) || (img->type == SI_SLICE && currMB->mb_type == SI4MB);
2757
 
2758
  int uv;
2759
  int qp_const_uv[2];
2760
  int qp_per_uv[2];
2761
  int qp_rem_uv[2];
2762
 
2763
  int intra     = IS_INTRA (currMB);
2764
  int temp[4];
2765
 
2766
  int b4;
2767
  int yuv = dec_picture->chroma_format_idc-1;
2768
  int m5[4];
2769
  int m6[4];
2770
 
2771
  int need_transform_size_flag;
2772
  Boolean lossless_qpprime = (Boolean) ((img->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
2773
 
2774
  int (*InvLevelScale4x4)[4] = NULL;
2775
  int (*InvLevelScale8x8)[8] = NULL;
2776
  // select scan type
2777
  const byte (*pos_scan8x8)[2] = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN8x8 : FIELD_SCAN8x8;
2778
  const byte (*pos_scan4x4)[2] = ((img->structure == FRAME) && (!currMB->mb_field)) ? SNGL_SCAN : FIELD_SCAN;
2779
 
2780
  if(img->type==SP_SLICE  && currMB->mb_type!=I16MB )
2781
    smb=1;
2782
 
2783
  // QPI
2784
  //init constants for every chroma qp offset
2785
  if (dec_picture->chroma_format_idc != YUV400)
2786
  {
2787
    for (i=0; i<2; i++)
2788
    {
2789
      qp_per_uv[i] = (currMB->qpc[i] + img->bitdepth_chroma_qp_scale)/6;
2790
      qp_rem_uv[i] = (currMB->qpc[i] + img->bitdepth_chroma_qp_scale)%6;
2791
    }
2792
  }
2793
 
2794
  // read CBP if not new intra mode
2795
  if (!IS_NEWINTRA (currMB))
2796
  {
2797
    //=====   C B P   =====
2798
    //---------------------
2799
    currSE.type = (IS_OLDINTRA (currMB) || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
2800
      ? SE_CBP_INTRA
2801
      : SE_CBP_INTER;
2802
 
2803
    dP = &(currSlice->partArr[partMap[currSE.type]]);
2804
 
2805
    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
2806
    {
2807
      currSE.mapping = (IS_OLDINTRA (currMB) || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
2808
        ? linfo_cbp_intra
2809
        : linfo_cbp_inter;
2810
    }
2811
    else
2812
    {
2813
      currSE.reading = readCBP_CABAC;
2814
    }
2815
 
2816
    TRACE_STRING("coded_block_pattern");
2817
 
2818
    dP->readSyntaxElement(&currSE,img,dP);
2819
    currMB->cbp = cbp = currSE.value1;
2820
 
2821
 
2822
    //============= Transform size flag for INTER MBs =============
2823
    //-------------------------------------------------------------
2824
    need_transform_size_flag = (((currMB->mb_type >= 1 && currMB->mb_type <= 3)||
2825
                                (IS_DIRECT(currMB) && active_sps->direct_8x8_inference_flag) ||
2826
                                (currMB->NoMbPartLessThan8x8Flag))
2827
                                && currMB->mb_type != I8MB && currMB->mb_type != I4MB
2828
                                && (currMB->cbp&15)
2829
                                && img->Transform8x8Mode);
2830
 
2831
    if (need_transform_size_flag)
2832
    {
2833
      currSE.type   =  SE_HEADER;
2834
      dP = &(currSlice->partArr[partMap[SE_HEADER]]);
2835
      currSE.reading = readMB_transform_size_flag_CABAC;
2836
      TRACE_STRING("transform_size_8x8_flag");
2837
 
2838
      // read UVLC transform_size_8x8_flag
2839
      if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
2840
      {
2841
        currSE.len = 1;
2842
        readSyntaxElement_FLC(&currSE, dP->bitstream);
2843
      }
2844
      else
2845
      {
2846
        dP->readSyntaxElement(&currSE,img,dP);
2847
      }
2848
      currMB->luma_transform_size_8x8_flag = currSE.value1;
2849
    }
2850
 
2851
    //=====   DQUANT   =====
2852
    //----------------------
2853
    // Delta quant only if nonzero coeffs
2854
    if (cbp !=0)
2855
    {
2856
      currSE.type = (IS_INTER (currMB)) ? SE_DELTA_QUANT_INTER : SE_DELTA_QUANT_INTRA;
2857
 
2858
      dP = &(currSlice->partArr[partMap[currSE.type]]);
2859
 
2860
      if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
2861
      {
2862
        currSE.mapping = linfo_se;
2863
      }
2864
      else
2865
        currSE.reading= readDquant_CABAC; //gabi
2866
 
2867
      TRACE_STRING("mb_qp_delta");
2868
 
2869
      dP->readSyntaxElement(&currSE,img,dP);
2870
      currMB->delta_quant = currSE.value1;
2871
      if ((currMB->delta_quant < -(26 + img->bitdepth_luma_qp_scale/2)) || (currMB->delta_quant > (25 + img->bitdepth_luma_qp_scale/2)))
2872
        error ("mb_qp_delta is out of range", 500);
2873
 
2874
      img->qp= ((img->qp + currMB->delta_quant + 52 + 2*img->bitdepth_luma_qp_scale)%(52+img->bitdepth_luma_qp_scale)) -
2875
                 img->bitdepth_luma_qp_scale;
2876
    }
2877
  }
2878
  else
2879
  {
2880
    cbp = currMB->cbp;
2881
  }
2882
 
2883
  for (i=0;i<BLOCK_SIZE;i++)
2884
    for (j=0;j<BLOCK_SIZE;j++)
2885
      memset(&img->cof[i][j][0][0], 0, BLOCK_SIZE * BLOCK_SIZE * sizeof(int)); // reset luma coeffs
2886
 
2887
 
2888
  if (IS_NEWINTRA (currMB)) // read DC coeffs for new intra modes
2889
  {
2890
    currSE.type = SE_DELTA_QUANT_INTRA;
2891
 
2892
    dP = &(currSlice->partArr[partMap[currSE.type]]);
2893
 
2894
    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
2895
    {
2896
      currSE.mapping = linfo_se;
2897
    }
2898
    else
2899
    {
2900
      currSE.reading= readDquant_CABAC;
2901
    }
2902
#if TRACE
2903
    snprintf(currSE.tracestring, TRACESTRING_SIZE, "Delta quant ");
2904
#endif
2905
    dP->readSyntaxElement(&currSE,img,dP);
2906
    currMB->delta_quant = currSE.value1;
2907
    if ((currMB->delta_quant < -(26 + img->bitdepth_luma_qp_scale/2)) || (currMB->delta_quant > (25 + img->bitdepth_luma_qp_scale/2)))
2908
      error ("mb_qp_delta is out of range", 500);
2909
 
2910
    img->qp= ((img->qp + currMB->delta_quant + 52 + 2*img->bitdepth_luma_qp_scale)%(52+img->bitdepth_luma_qp_scale)) -
2911
               img->bitdepth_luma_qp_scale;
2912
 
2913
    for (i=0;i<BLOCK_SIZE;i++)
2914
      for (j=0;j<BLOCK_SIZE;j++)
2915
        img->ipredmode[img->block_y+j][img->block_x+i]=DC_PRED;
2916
 
2917
 
2918
    if (active_pps->entropy_coding_mode_flag == UVLC)
2919
    {
2920
      readCoeff4x4_CAVLC(img, inp, LUMA_INTRA16x16DC, 0, 0,
2921
                          levarr, runarr, &numcoeff);
2922
 
2923
      coef_ctr=-1;
2924
      level = 1;                            // just to get inside the loop
2925
      for(k = 0; k < numcoeff; k++)
2926
      {
2927
        if (levarr[k] != 0)                     // leave if len=1
2928
        {
2929
          coef_ctr=coef_ctr+runarr[k]+1;
2930
 
2931
          i0=pos_scan4x4[coef_ctr][0];
2932
          j0=pos_scan4x4[coef_ctr][1];
2933
 
2934
          img->cof[i0][j0][0][0]=levarr[k];// add new intra DC coeff
2935
        }
2936
      }
2937
    }
2938
    else
2939
    {
2940
 
2941
      currSE.type = SE_LUM_DC_INTRA;
2942
      dP = &(currSlice->partArr[partMap[currSE.type]]);
2943
 
2944
      currSE.context      = LUMA_16DC;
2945
      currSE.type         = SE_LUM_DC_INTRA;
2946
      img->is_intra_block = 1;
2947
 
2948
      if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
2949
      {
2950
        currSE.mapping = linfo_levrun_inter;
2951
      }
2952
      else
2953
      {
2954
        currSE.reading = readRunLevel_CABAC;
2955
      }
2956
 
2957
      coef_ctr = -1;
2958
      level = 1;                            // just to get inside the loop
2959
 
2960
      for(k=0;(k<17) && (level!=0);k++)
2961
      {
2962
#if TRACE
2963
        snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC luma 16x16 ");
2964
#endif
2965
        dP->readSyntaxElement(&currSE,img,dP);
2966
        level = currSE.value1;
2967
        run   = currSE.value2;
2968
        len   = currSE.len;
2969
 
2970
        if (level != 0)                     // leave if len=1
2971
        {
2972
          coef_ctr=coef_ctr+run+1;
2973
 
2974
          i0=pos_scan4x4[coef_ctr][0];
2975
          j0=pos_scan4x4[coef_ctr][1];
2976
 
2977
          img->cof[i0][j0][0][0]=level;// add new intra DC coeff
2978
        }
2979
      }
2980
    }
2981
    if(!lossless_qpprime)
2982
      itrans_2(img);// transform new intra DC
2983
  }
2984
 
2985
  currMB->qp = img->qp;
2986
 
2987
  set_chroma_qp(currMB);
2988
 
2989
  qp_per    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)/6;
2990
  qp_rem    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)%6;
2991
  qp_const  = 1<<(3-qp_per);
2992
 
2993
  InvLevelScale4x4 = intra? InvLevelScale4x4Luma_Intra[qp_rem] : InvLevelScale4x4Luma_Inter[qp_rem];
2994
  InvLevelScale8x8 = intra? InvLevelScale8x8Luma_Intra[qp_rem] : InvLevelScale8x8Luma_Inter[qp_rem];
2995
 
2996
  //init constants for every chroma qp offset
2997
  if (dec_picture->chroma_format_idc != YUV400)
2998
  {
2999
    for(i=0; i < 2; i++)
3000
    {
3001
      qp_per_uv[i] = (currMB->qpc[i] + img->bitdepth_chroma_qp_scale)/6;
3002
      qp_rem_uv[i] = (currMB->qpc[i] + img->bitdepth_chroma_qp_scale)%6;
3003
    }
3004
  }
3005
 
3006
 
3007
  // luma coefficients
3008
  for (block_y=0; block_y < 4; block_y += 2) /* all modes */
3009
  {
3010
    for (block_x=0; block_x < 4; block_x += 2)
3011
    {
3012
 
3013
      b8 = 2*(block_y>>1) + (block_x>>1);
3014
      if (active_pps->entropy_coding_mode_flag == UVLC)
3015
      {
3016
        for (j=block_y; j < block_y+2; j++)
3017
        {
3018
          for (i=block_x; i < block_x+2; i++)
3019
          {
3020
            ii = block_x >> 1;
3021
            jj = block_y >> 1;
3022
            b8 = 2 * jj + ii;
3023
 
3024
            if (cbp & (1<<b8))  /* are there any coeff in current block at all */
3025
            {
3026
              readCoeff4x4_CAVLC(img, inp, (IS_NEWINTRA(currMB) ? LUMA_INTRA16x16AC : LUMA), i, j, levarr, runarr, &numcoeff);
3027
 
3028
              start_scan = IS_NEWINTRA(currMB) ? 1 : 0;
3029
              coef_ctr = start_scan - 1;
3030
 
3031
              if(!lossless_qpprime)
3032
              {
3033
                if (!currMB->luma_transform_size_8x8_flag) // 4x4 transform
3034
                {
3035
                  for (k = 0; k < numcoeff; k++)
3036
                  {
3037
                    if (levarr[k] != 0)
3038
                    {
3039
                      coef_ctr += runarr[k]+1;
3040
 
3041
                      i0 = pos_scan4x4[coef_ctr][0];
3042
                      j0 = pos_scan4x4[coef_ctr][1];
3043
 
3044
                      // inverse quant for 4x4 transform only
3045
                      currMB->cbp_blk      |= (int64) 1 << ((j<<2) + i);
3046
                      img->cof[i][j][j0][i0]= rshift_rnd_sf((levarr[k]*InvLevelScale4x4[j0][i0])<<qp_per, 4);
3047
                    }
3048
                  }
3049
                }
3050
                else // 8x8 transform
3051
                {
3052
                  int b4, iz, jz;
3053
                  for (k = 0; k < numcoeff; k++)
3054
                  {
3055
                    if (levarr[k] != 0)
3056
                    {
3057
                      coef_ctr += runarr[k]+1;
3058
 
3059
                      // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
3060
                      //as containing coefficients
3061
                      currMB->cbp_blk  |= 51 << ((block_y<<2) + block_x);
3062
 
3063
                      b4 = 2*(j - block_y)+(i - block_x);
3064
 
3065
                      iz = pos_scan8x8[(coef_ctr << 2) + b4][0];
3066
                      jz = pos_scan8x8[(coef_ctr << 2) + b4][1];
3067
 
3068
                      img->m7[block_y*4 +jz][block_x*4 +iz] = rshift_rnd_sf((levarr[k]*InvLevelScale8x8[jz][iz])<<qp_per, 6); // dequantization
3069
                    }
3070
                  }//else (!currMB->luma_transform_size_8x8_flag)
3071
                }
3072
              }
3073
              else
3074
              {
3075
                if (!currMB->luma_transform_size_8x8_flag) // inverse quant for 4x4 transform
3076
                {
3077
                  for (k = 0; k < numcoeff; k++)
3078
                  {
3079
                    if (levarr[k] != 0)
3080
                    {
3081
                      coef_ctr += runarr[k]+1;
3082
 
3083
                      i0=pos_scan4x4[coef_ctr][0];
3084
                      j0=pos_scan4x4[coef_ctr][1];
3085
 
3086
                      currMB->cbp_blk      |= (int64) 1 << ((j<<2) + i);
3087
                      img->cof[i][j][j0][i0]= levarr[k];
3088
                    }
3089
                  }
3090
                }
3091
                else // inverse quant for 8x8 transform
3092
                {
3093
                  int b4, iz, jz;
3094
                  for (k = 0; k < numcoeff; k++)
3095
                  {
3096
                    if (levarr[k] != 0)
3097
                    {
3098
                      coef_ctr += runarr[k]+1;
3099
 
3100
                      // do same as CABAC for deblocking: any coeff in the 8x8 marks all the 4x4s
3101
                      //as containing coefficients
3102
                      currMB->cbp_blk  |= 51 << ((block_y<<2) + block_x);
3103
 
3104
                      b4 = 2*(j-block_y)+(i-block_x);
3105
 
3106
                      iz=pos_scan8x8[coef_ctr*4+b4][0];
3107
                      jz=pos_scan8x8[coef_ctr*4+b4][1];
3108
 
3109
                      img->m7[block_y*4 +jz][block_x*4 +iz] = levarr[k];
3110
                    }
3111
                  }
3112
                }//else (!currMB->luma_transform_size_8x8_flag)
3113
              }
3114
            }
3115
            else
3116
            {
3117
              img->nz_coeff[img->current_mb_nr][i][j] = 0;
3118
            }
3119
          }
3120
        }
3121
      } // VLC
3122
      else
3123
      {
3124
        if(currMB->luma_transform_size_8x8_flag)
3125
          readLumaCoeff8x8_CABAC(img, inp, b8); //======= 8x8 trannsform size & CABAC ========
3126
        else
3127
        {
3128
          //======= Other Modes & CABAC ========
3129
          //------------------------------------
3130
          for (j=block_y; j < block_y+2; j++)
3131
          {
3132
            for (i=block_x; i < block_x+2; i++)
3133
            {
3134
              start_scan = IS_NEWINTRA (currMB)? 1 : 0;
3135
 
3136
              img->subblock_x = i; // position for coeff_count ctx
3137
              img->subblock_y = j; // position for coeff_count ctx
3138
 
3139
              if (cbp & (1<<b8))  // are there any coeff in current block at all
3140
              {
3141
                coef_ctr = start_scan - 1;
3142
                level    = 1;
3143
                img->is_intra_block = IS_INTRA(currMB);
3144
 
3145
                if(!lossless_qpprime)
3146
                {
3147
                  for(k=start_scan;(k<17) && (level!=0);k++)
3148
                  {
3149
                    /*
3150
                    * make distinction between INTRA and INTER coded
3151
                    * luminance coefficients
3152
                    */
3153
                    currSE.context      = (IS_NEWINTRA(currMB) ? LUMA_16AC : LUMA_4x4);
3154
                    currSE.type         = (img->is_intra_block
3155
                      ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
3156
                      : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
3157
 
3158
#if TRACE
3159
                    sprintf(currSE.tracestring, "Luma sng ");
3160
#endif
3161
                    dP = &(currSlice->partArr[partMap[currSE.type]]);
3162
 
3163
                    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3164
                      currSE.mapping = linfo_levrun_inter;
3165
                    else
3166
                      currSE.reading = readRunLevel_CABAC;
3167
 
3168
                    dP->readSyntaxElement(&currSE,img,dP);
3169
                    level = currSE.value1;
3170
                    run   = currSE.value2;
3171
                    len   = currSE.len;
3172
 
3173
                    if (level != 0)    /* leave if len=1 */
3174
                    {
3175
                      coef_ctr += run+1;
3176
 
3177
                      i0=pos_scan4x4[coef_ctr][0];
3178
                      j0=pos_scan4x4[coef_ctr][1];
3179
 
3180
                      currMB->cbp_blk |= (int64)1 << ((j<<2) + i) ;
3181
                      img->cof[i][j][j0][i0]= rshift_rnd_sf((level*InvLevelScale4x4[j0][i0]) << qp_per, 4);
3182
                    }
3183
                  }
3184
                }
3185
                else
3186
                {
3187
                  for(k=start_scan;(k<17) && (level!=0);k++)
3188
                  {
3189
                    /*
3190
                    * make distinction between INTRA and INTER coded
3191
                    * luminance coefficients
3192
                    */
3193
                    currSE.context      = (IS_NEWINTRA(currMB) ? LUMA_16AC : LUMA_4x4);
3194
                    currSE.type         = (img->is_intra_block
3195
                      ? (k==0 ? SE_LUM_DC_INTRA : SE_LUM_AC_INTRA)
3196
                      : (k==0 ? SE_LUM_DC_INTER : SE_LUM_AC_INTER));
3197
 
3198
#if TRACE
3199
                    sprintf(currSE.tracestring, "Luma sng ");
3200
#endif
3201
                    dP = &(currSlice->partArr[partMap[currSE.type]]);
3202
 
3203
                    if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3204
                      currSE.mapping = linfo_levrun_inter;
3205
                    else
3206
                      currSE.reading = readRunLevel_CABAC;
3207
 
3208
                    dP->readSyntaxElement(&currSE,img,dP);
3209
                    level = currSE.value1;
3210
                    run   = currSE.value2;
3211
                    len   = currSE.len;
3212
 
3213
                    if (level != 0)    /* leave if len=1 */
3214
                    {
3215
                      coef_ctr += run+1;
3216
 
3217
                      i0=pos_scan4x4[coef_ctr][0];
3218
                      j0=pos_scan4x4[coef_ctr][1];
3219
 
3220
                      currMB->cbp_blk |= (int64)1 << ((j<<2) + i) ;
3221
 
3222
                      img->cof[i][j][j0][i0]= level;
3223
                    }
3224
                  }
3225
                }
3226
              }
3227
            }
3228
          }
3229
        }
3230
      }
3231
    }
3232
  }
3233
 
3234
  if (dec_picture->chroma_format_idc != YUV400)
3235
  {
3236
 
3237
    for (j=4;j<(4+img->num_blk8x8_uv);j++) // reset all chroma coeffs before read
3238
    for (i=0;i<4;i++)
3239
      memset(&img->cof[i][j][0][0], 0, 16 * sizeof(int));
3240
 
3241
    m2  = img->mb_x * 2;
3242
    jg2 = img->mb_y * 2;
3243
 
3244
    qp_const_uv[0] = 1<<(3-qp_per_uv[0]);
3245
    qp_const_uv[1] = 1<<(3-qp_per_uv[1]);
3246
 
3247
 
3248
    //========================== CHROMA DC ============================
3249
    //-----------------------------------------------------------------
3250
    // chroma DC coeff
3251
    if(cbp>15)
3252
    {
3253
      for (ll=0;ll<3;ll+=2)
3254
      {
3255
        uv = ll>>1;
3256
 
3257
        if (dec_picture->chroma_format_idc == YUV420)
3258
        {
3259
          int (*InvLevelScale4x4Chroma)[4] = intra
3260
            ? InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv[uv]]
3261
            : InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv[uv]];
3262
 
3263
          //===================== CHROMA DC YUV420 ======================
3264
          memset(&img->cofu[0], 0, 4 *sizeof(int));
3265
 
3266
          if (active_pps->entropy_coding_mode_flag == UVLC)
3267
          {
3268
            readCoeff4x4_CAVLC(img, inp, CHROMA_DC, 0, 0,
3269
                               levarr, runarr, &numcoeff);
3270
            coef_ctr=-1;
3271
            level=1;
3272
            for(k = 0; k < numcoeff; k++)
3273
            {
3274
              if (levarr[k] != 0)
3275
              {
3276
                currMB->cbp_blk |= 0xf0000 << (ll<<1) ;
3277
                coef_ctr=coef_ctr+runarr[k]+1;
3278
                img->cofu[coef_ctr]=levarr[k];
3279
              }
3280
            }
3281
          }
3282
          else
3283
          {
3284
            coef_ctr=-1;
3285
            level=1;
3286
            for(k=0;(k<(img->num_cdc_coeff+1))&&(level!=0);k++)
3287
            {
3288
              currSE.context      = CHROMA_DC;
3289
              currSE.type         = (IS_INTRA(currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
3290
              img->is_intra_block =  IS_INTRA(currMB);
3291
              img->is_v_block     = ll;
3292
 
3293
  #if TRACE
3294
              snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x2 DC Chroma ");
3295
  #endif
3296
              dP = &(currSlice->partArr[partMap[currSE.type]]);
3297
 
3298
              if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3299
                currSE.mapping = linfo_levrun_c2x2;
3300
              else
3301
                currSE.reading = readRunLevel_CABAC;
3302
 
3303
              dP->readSyntaxElement(&currSE,img,dP);
3304
              level = currSE.value1;
3305
              run = currSE.value2;
3306
              len = currSE.len;
3307
              if (level != 0)
3308
              {
3309
                currMB->cbp_blk |= 0xf0000 << (ll<<1) ;
3310
                coef_ctr=coef_ctr+run+1;
3311
                // Bug: img->cofu has only 4 entries, hence coef_ctr MUST be <4 (which is
3312
                // caught by the assert().  If it is bigger than 4, it starts patching the
3313
                // img->predmode pointer, which leads to bugs later on.
3314
                //
3315
                // This assert() should be left in the code, because it captures a very likely
3316
                // bug early when testing in error prone environments (or when testing NAL
3317
                // functionality).
3318
                assert (coef_ctr < img->num_cdc_coeff);
3319
                img->cofu[coef_ctr]=level;
3320
              }
3321
            }
3322
          }
3323
 
3324
          if (smb // check to see if MB type is SPred or SIntra4x4
3325
              || lossless_qpprime)
3326
          {
3327
            img->cof[0+ll][4][0][0]=img->cofu[0];
3328
            img->cof[1+ll][4][0][0]=img->cofu[1];
3329
            img->cof[0+ll][5][0][0]=img->cofu[2];
3330
            img->cof[1+ll][5][0][0]=img->cofu[3];
3331
          }
3332
          else
3333
          {
3334
            temp[0]=(img->cofu[0]+img->cofu[1]+img->cofu[2]+img->cofu[3]);
3335
            temp[1]=(img->cofu[0]-img->cofu[1]+img->cofu[2]-img->cofu[3]);
3336
            temp[2]=(img->cofu[0]+img->cofu[1]-img->cofu[2]-img->cofu[3]);
3337
            temp[3]=(img->cofu[0]-img->cofu[1]-img->cofu[2]+img->cofu[3]);
3338
 
3339
            for (i=0;i<img->num_cdc_coeff;i++)
3340
            {
3341
              if(qp_per_uv[uv]<5)
3342
              {
3343
                  temp[i]=(temp[i]*InvLevelScale4x4Chroma[0][0])>>(5-qp_per_uv[uv]);
3344
              }
3345
              else
3346
              {
3347
                  temp[i]=(temp[i]*InvLevelScale4x4Chroma[0][0])<<(qp_per_uv[uv]-5);
3348
              }
3349
            }
3350
            img->cof[0+ll][4][0][0]=temp[0];
3351
            img->cof[1+ll][4][0][0]=temp[1];
3352
            img->cof[0+ll][5][0][0]=temp[2];
3353
            img->cof[1+ll][5][0][0]=temp[3];
3354
          }
3355
        }
3356
        else if (dec_picture->chroma_format_idc == YUV422)
3357
        {
3358
          int i,j,j1;
3359
          int uv_idx = ll;
3360
          int m3[2][4] = {{0,0,0,0},{0,0,0,0}};
3361
          int m4[2][4] = {{0,0,0,0},{0,0,0,0}};
3362
          int qp_per_uv_dc = (currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale)/6;       //for YUV422 only
3363
          int qp_rem_uv_dc = (currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale)%6;       //for YUV422 only
3364
 
3365
          //===================== CHROMA DC YUV422 ======================
3366
          if (active_pps->entropy_coding_mode_flag == UVLC)
3367
          {
3368
            readCoeff4x4_CAVLC(img, inp, CHROMA_DC, 0, 0,
3369
                               levarr, runarr, &numcoeff);
3370
            coef_ctr=-1;
3371
            level=1;
3372
            for(k = 0; k < numcoeff; k++)
3373
            {
3374
              if (levarr[k] != 0)
3375
              {
3376
                currMB->cbp_blk |= ((int64)0xff0000) << (ll<<2);
3377
                coef_ctr=coef_ctr+runarr[k]+1;
3378
                i0 = SCAN_YUV422[coef_ctr][0];
3379
                j0 = SCAN_YUV422[coef_ctr][1];
3380
 
3381
                m3[i0][j0]=levarr[k];
3382
              }
3383
            }
3384
          }
3385
          else
3386
          {
3387
            coef_ctr=-1;
3388
            level=1;
3389
            for(k=0;(k<9)&&(level!=0);k++)
3390
            {
3391
              currSE.context      = CHROMA_DC_2x4;
3392
              currSE.type         = (IS_INTRA(currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
3393
              img->is_intra_block =  IS_INTRA(currMB);
3394
              img->is_v_block     = ll;
3395
 
3396
  #if TRACE
3397
              snprintf(currSE.tracestring, TRACESTRING_SIZE, "2x4 DC Chroma ");
3398
  #endif
3399
              dP = &(currSlice->partArr[partMap[currSE.type]]);
3400
 
3401
              if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3402
                currSE.mapping = linfo_levrun_c2x2;
3403
              else
3404
                currSE.reading = readRunLevel_CABAC;
3405
 
3406
              dP->readSyntaxElement(&currSE,img,dP);
3407
              level = currSE.value1;
3408
              run = currSE.value2;
3409
              len = currSE.len;
3410
              if (level != 0)
3411
              {
3412
                currMB->cbp_blk |= ((int64)0xff0000) << (ll<<2) ;
3413
                coef_ctr=coef_ctr+run+1;
3414
                assert (coef_ctr < img->num_cdc_coeff);
3415
                i0=SCAN_YUV422[coef_ctr][0];
3416
                j0=SCAN_YUV422[coef_ctr][1];
3417
 
3418
                m3[i0][j0]=level;
3419
              }
3420
            }
3421
          }
3422
          // inverse CHROMA DC YUV422 transform
3423
          // horizontal
3424
          if(!lossless_qpprime)
3425
          {
3426
            m4[0][0] = m3[0][0] + m3[1][0];
3427
            m4[0][1] = m3[0][1] + m3[1][1];
3428
            m4[0][2] = m3[0][2] + m3[1][2];
3429
            m4[0][3] = m3[0][3] + m3[1][3];
3430
 
3431
            m4[1][0] = m3[0][0] - m3[1][0];
3432
            m4[1][1] = m3[0][1] - m3[1][1];
3433
            m4[1][2] = m3[0][2] - m3[1][2];
3434
            m4[1][3] = m3[0][3] - m3[1][3];
3435
          }
3436
          else
3437
          {
3438
            for(i=0;i<2;i++)
3439
              for(j=0;j<4;j++)
3440
                img->cof[i+uv_idx][j+4][0][0]=m3[i][j];
3441
          }
3442
 
3443
          // vertical
3444
          for (i=0;i<2 && !lossless_qpprime;i++)
3445
          {
3446
            int (*imgcof)[4][4] = img->cof[i+uv_idx];
3447
            for (j=0; j < 4;j++)    //TODO: remove m5 with m4
3448
              m5[j]=m4[i][j];
3449
 
3450
            m6[0]=m5[0]+m5[2];
3451
            m6[1]=m5[0]-m5[2];
3452
            m6[2]=m5[1]-m5[3];
3453
            m6[3]=m5[1]+m5[3];
3454
 
3455
            for (j=0;j<2;j++)
3456
            {
3457
              j1=3-j;
3458
              if(qp_per_uv_dc<4)
3459
              {
3460
                if(intra == 1)
3461
                {
3462
                  imgcof[j +4][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv_dc][0][0]+(1<<(3-qp_per_uv_dc)))>>(4-qp_per_uv_dc))+2)>>2;
3463
                  imgcof[j1+4][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv_dc][0][0]+(1<<(3-qp_per_uv_dc)))>>(4-qp_per_uv_dc))+2)>>2;
3464
                }
3465
                else
3466
                {
3467
                  imgcof[j +4][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv_dc][0][0]+(1<<(3-qp_per_uv_dc)))>>(4-qp_per_uv_dc))+2)>>2;
3468
                  imgcof[j1+4][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv_dc][0][0]+(1<<(3-qp_per_uv_dc)))>>(4-qp_per_uv_dc))+2)>>2;
3469
                }
3470
              }
3471
              else
3472
              {
3473
                if(intra == 1)
3474
                {
3475
                  imgcof[j +4][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv_dc][0][0])<<(qp_per_uv_dc-4))+2)>>2;
3476
                  imgcof[j1+4][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv_dc][0][0])<<(qp_per_uv_dc-4))+2)>>2;
3477
                }
3478
                else
3479
                {
3480
                  imgcof[j +4][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv_dc][0][0])<<(qp_per_uv_dc-4))+2)>>2;
3481
                  imgcof[j1+4][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv_dc][0][0])<<(qp_per_uv_dc-4))+2)>>2;
3482
                }
3483
              }
3484
            }//for (j=0;j<2;j++)
3485
          }//for (i=0;i<2;i++)
3486
        }//else if (dec_picture->chroma_format_idc == YUV422)
3487
        else
3488
        {
3489
          //===================== CHROMA DC YUV444 ======================
3490
          int i,j,i1,j1;
3491
          int uv_idx = 4 + (ll<<1);
3492
 
3493
          if (active_pps->entropy_coding_mode_flag == UVLC)
3494
          {
3495
            readCoeff4x4_CAVLC(img, inp, CHROMA_DC, 0, 0,
3496
                               levarr, runarr, &numcoeff);
3497
            coef_ctr=-1;
3498
            level=1;
3499
            for(k = 0; k < numcoeff; k++)
3500
            {
3501
              if (levarr[k] != 0)
3502
              {
3503
                currMB->cbp_blk |= ((int64)0xffff0000) << (ll<<3) ;
3504
                coef_ctr=coef_ctr+runarr[k]+1;
3505
                i0=SNGL_SCAN[coef_ctr][0];
3506
                j0=SNGL_SCAN[coef_ctr][1];
3507
 
3508
                img->cof[i0][j0+uv_idx][0][0]=levarr[k];
3509
              }
3510
            }
3511
          }
3512
          else
3513
          {
3514
            coef_ctr=-1;
3515
            level=1;
3516
            for(k=0;(k<17)&&(level!=0);k++)
3517
            {
3518
              currSE.context      = CHROMA_DC_4x4;
3519
              currSE.type         = (IS_INTRA(currMB) ? SE_CHR_DC_INTRA : SE_CHR_DC_INTER);
3520
              img->is_intra_block =  IS_INTRA(currMB);
3521
              img->is_v_block     = ll;
3522
 
3523
  #if TRACE
3524
              snprintf(currSE.tracestring, TRACESTRING_SIZE, "DC Chroma ");
3525
  #endif
3526
              dP = &(currSlice->partArr[partMap[currSE.type]]);
3527
 
3528
              if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3529
                currSE.mapping = linfo_levrun_c2x2;
3530
              else
3531
                currSE.reading = readRunLevel_CABAC;
3532
 
3533
              dP->readSyntaxElement(&currSE,img,dP);
3534
              level = currSE.value1;
3535
              run = currSE.value2;
3536
              len = currSE.len;
3537
              if (level != 0)
3538
              {
3539
                currMB->cbp_blk |= ((int64)0xffff0000) << (ll<<3) ;
3540
                coef_ctr=coef_ctr+run+1;
3541
                assert (coef_ctr < img->num_cdc_coeff);
3542
                i0=SNGL_SCAN[coef_ctr][0];
3543
                j0=SNGL_SCAN[coef_ctr][1];
3544
 
3545
                img->cof[i0][j0+uv_idx][0][0]=level;
3546
              }
3547
            }
3548
          }
3549
 
3550
          // inverse CHROMA DC YUV444 transform
3551
          // horizontal
3552
          for (j=uv_idx; (j < 4+uv_idx) && !lossless_qpprime;j++)
3553
          {
3554
            for (i=0;i<4;i++)
3555
              m5[i]=img->cof[i][j][0][0];
3556
 
3557
            m6[0]=m5[0]+m5[2];
3558
            m6[1]=m5[0]-m5[2];
3559
            m6[2]=m5[1]-m5[3];
3560
            m6[3]=m5[1]+m5[3];
3561
 
3562
            for (i=0;i<2;i++)
3563
            {
3564
              i1=3-i;
3565
              img->cof[i][j][0][0]= m6[i]+m6[i1];
3566
              img->cof[i1][j][0][0]=m6[i]-m6[i1];
3567
            }
3568
          }
3569
 
3570
          // vertical
3571
          for (i=0;i<4 && !lossless_qpprime;i++)
3572
          {
3573
            for (j=0; j < 4;j++)
3574
              m5[j]=img->cof[i][j+uv_idx][0][0];
3575
 
3576
            m6[0]=m5[0]+m5[2];
3577
            m6[1]=m5[0]-m5[2];
3578
            m6[2]=m5[1]-m5[3];
3579
            m6[3]=m5[1]+m5[3];
3580
 
3581
            for (j=0;j<2;j++)
3582
            {
3583
              j1=3-j;
3584
              if(qp_per_uv[uv]<4)
3585
              {
3586
                if(intra == 1)
3587
                {
3588
                  img->cof[i][j +uv_idx][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv[uv]][0][0]+(1<<(3-qp_per_uv[uv])))>>(4-qp_per_uv[uv]))+2)>>2;
3589
                  img->cof[i][j1+uv_idx][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv[uv]][0][0]+(1<<(3-qp_per_uv[uv])))>>(4-qp_per_uv[uv]))+2)>>2;
3590
                }
3591
                else
3592
                {
3593
                  img->cof[i][j +uv_idx][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv[uv]][0][0]+(1<<(3-qp_per_uv[uv])))>>(4-qp_per_uv[uv]))+2)>>2;
3594
                  img->cof[i][j1+uv_idx][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv[uv]][0][0]+(1<<(3-qp_per_uv[uv])))>>(4-qp_per_uv[uv]))+2)>>2;
3595
                }
3596
              }
3597
              else
3598
              {
3599
                if(intra == 1)
3600
                {
3601
                  img->cof[i][j +uv_idx][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv[uv]][0][0])<<(qp_per_uv[uv]-4))+2)>>2;
3602
                  img->cof[i][j1+uv_idx][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Intra[uv][qp_rem_uv[uv]][0][0])<<(qp_per_uv[uv]-4))+2)>>2;
3603
                }
3604
                else
3605
                {
3606
                  img->cof[i][j +uv_idx][0][0]=((((m6[j]+m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv[uv]][0][0])<<(qp_per_uv[uv]-4))+2)>>2;
3607
                  img->cof[i][j1+uv_idx][0][0]=((((m6[j]-m6[j1])*InvLevelScale4x4Chroma_Inter[uv][qp_rem_uv[uv]][0][0])<<(qp_per_uv[uv]-4))+2)>>2;
3608
                }
3609
              }
3610
            }
3611
          }//for (i=0;i<4;i++)
3612
        }//else (dec_picture->chroma_format_idc == YUV444)
3613
      }//for (ll=0;ll<3;ll+=2)
3614
    }
3615
 
3616
    // chroma AC coeff, all zero fram start_scan
3617
    if (cbp<=31)
3618
        for (i=0; i < 4; i++)
3619
          memset(&img->nz_coeff [img->current_mb_nr ][i][4], 0, img->num_blk8x8_uv * sizeof(int));
3620
 
3621
 
3622
    //========================== CHROMA AC ============================
3623
    //-----------------------------------------------------------------
3624
    // chroma AC coeff, all zero fram start_scan
3625
    if (cbp>31)
3626
    {
3627
      for (b8=0; b8 < img->num_blk8x8_uv; b8++)
3628
      {
3629
        int uvc = (b8 > ((img->num_blk8x8_uv>>1) - 1 ));
3630
        int (*InvLevelScale4x4Chroma)[4] = intra
3631
          ? InvLevelScale4x4Chroma_Intra[uvc][qp_rem_uv[uvc]]
3632
          : InvLevelScale4x4Chroma_Inter[uvc][qp_rem_uv[uvc]];
3633
 
3634
        img->is_v_block = uv = uvc;
3635
 
3636
        for (b4=0; b4 < 4; b4++)
3637
        {
3638
          i = cofuv_blk_x[yuv][b8][b4];
3639
          j = cofuv_blk_y[yuv][b8][b4];
3640
 
3641
 
3642
          if (active_pps->entropy_coding_mode_flag == UVLC)
3643
          {
3644
            readCoeff4x4_CAVLC(img, inp, CHROMA_AC, i, j,
3645
                                levarr, runarr, &numcoeff);
3646
            coef_ctr=0;
3647
            level=1;
3648
            if(!lossless_qpprime)
3649
            {
3650
              for(k = 0; k < numcoeff;k++)
3651
              {
3652
                if (levarr[k] != 0)
3653
                {
3654
                  currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
3655
                  coef_ctr=coef_ctr+runarr[k]+1;
3656
 
3657
                  i0=pos_scan4x4[coef_ctr][0];
3658
                  j0=pos_scan4x4[coef_ctr][1];
3659
 
3660
                  img->cof[i][j][j0][i0] = rshift_rnd_sf((levarr[k]*InvLevelScale4x4Chroma[j0][i0])<<qp_per_uv[uv], 4);
3661
                }
3662
              }
3663
            }
3664
            else
3665
            {
3666
              for(k = 0; k < numcoeff;k++)
3667
              {
3668
                if (levarr[k] != 0)
3669
                {
3670
                  currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
3671
                  coef_ctr=coef_ctr+runarr[k]+1;
3672
 
3673
                  i0=pos_scan4x4[coef_ctr][0];
3674
                  j0=pos_scan4x4[coef_ctr][1];
3675
 
3676
                  img->cof[i][j][j0][i0]=levarr[k];
3677
                }
3678
              }
3679
            }
3680
          }
3681
          else
3682
          {
3683
            coef_ctr=0;
3684
            level=1;
3685
 
3686
            img->subblock_y = subblk_offset_y[yuv][b8][b4]>>2;
3687
            img->subblock_x = subblk_offset_x[yuv][b8][b4]>>2;
3688
 
3689
            currSE.context      = CHROMA_AC;
3690
            currSE.type         = (IS_INTRA(currMB) ? SE_CHR_AC_INTRA : SE_CHR_AC_INTER);
3691
            img->is_intra_block =  IS_INTRA(currMB);
3692
 
3693
            if(!lossless_qpprime)
3694
            {
3695
              for(k=0;(k<16)&&(level!=0);k++)
3696
              {
3697
#if TRACE
3698
                snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
3699
#endif
3700
                dP = &(currSlice->partArr[partMap[currSE.type]]);
3701
 
3702
                if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3703
                  currSE.mapping = linfo_levrun_inter;
3704
                else
3705
                  currSE.reading = readRunLevel_CABAC;
3706
 
3707
                dP->readSyntaxElement(&currSE,img,dP);
3708
                level = currSE.value1;
3709
                run = currSE.value2;
3710
                len = currSE.len;
3711
 
3712
                if (level != 0)
3713
                {
3714
                  currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
3715
                  coef_ctr += (run + 1);
3716
 
3717
                  i0=pos_scan4x4[coef_ctr][0];
3718
                  j0=pos_scan4x4[coef_ctr][1];
3719
 
3720
                  img->cof[i][j][j0][i0] = rshift_rnd_sf((level*InvLevelScale4x4Chroma[j0][i0])<<qp_per_uv[uv], 4);
3721
                }
3722
              } //for(k=0;(k<16)&&(level!=0);k++)
3723
            }
3724
            else
3725
            {
3726
              for(k=0;(k<16)&&(level!=0);k++)
3727
              {
3728
#if TRACE
3729
                snprintf(currSE.tracestring, TRACESTRING_SIZE, "AC Chroma ");
3730
#endif
3731
                dP = &(currSlice->partArr[partMap[currSE.type]]);
3732
 
3733
                if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag)
3734
                  currSE.mapping = linfo_levrun_inter;
3735
                else
3736
                  currSE.reading = readRunLevel_CABAC;
3737
 
3738
                dP->readSyntaxElement(&currSE,img,dP);
3739
                level = currSE.value1;
3740
                run = currSE.value2;
3741
                len = currSE.len;
3742
 
3743
                if (level != 0)
3744
                {
3745
                  currMB->cbp_blk |= ((int64)1) << cbp_blk_chroma[b8][b4];
3746
                  coef_ctr += (run + 1);
3747
 
3748
                  i0=pos_scan4x4[coef_ctr][0];
3749
                  j0=pos_scan4x4[coef_ctr][1];
3750
 
3751
                  img->cof[i][j][j0][i0]=level;
3752
                }
3753
              } //for(k=0;(k<16)&&(level!=0);k++)
3754
            }
3755
          } //else / if (active_pps->entropy_coding_mode_flag == UVLC)
3756
        } //for (b4=0; b4 < 4; b4++)
3757
      } //for (b8=0; b8 < img->num_blk8x8_uv; b8++)
3758
    } //if (cbp>31)
3759
  } //if (dec_picture->chroma_format_idc != YUV400)
3760
}
3761
 
3762
 
3763
/*!
3764
 ************************************************************************
3765
 * \brief
3766
 *    Copy IPCM coefficients to decoded picture buffer and set parameters for this MB
3767
 *    (for IPCM CABAC and IPCM CAVLC  28/11/2003)
3768
 *
3769
 * \author
3770
 *    Dong Wang <Dong.Wang@bristol.ac.uk>
3771
 ************************************************************************
3772
 */
3773
 
3774
void decode_ipcm_mb(struct img_par *img)
3775
{
3776
  int i,j;
3777
 
3778
  Macroblock *currMb = &img->mb_data[img->current_mb_nr];
3779
 
3780
  //Copy coefficients to decoded picture buffer
3781
  //IPCM coefficients are stored in img->cof which is set in function readIPCMcoeffsFromNAL()
3782
 
3783
  for(i=0;i<16;i++)
3784
    for(j=0;j<16;j++)
3785
      dec_picture->imgY[img->pix_y+i][img->pix_x+j]=img->cof[(i>>2)][(j>>2)][i & 0x03][j & 0x03];
3786
 
3787
  if (dec_picture->chroma_format_idc != YUV400)
3788
  {
3789
    for(i=0;i<img->mb_cr_size_y;i++)
3790
      for(j=0;j<img->mb_cr_size_x;j++)
3791
        dec_picture->imgUV[0][img->pix_c_y+i][img->pix_c_x+j]=img->cof[(i>>2)  ][(j>>2)+4][i & 0x03][j & 0x03];
3792
 
3793
    for(i=0;i<img->mb_cr_size_y;i++)
3794
      for(j=0;j<img->mb_cr_size_x;j++)
3795
        dec_picture->imgUV[1][img->pix_c_y+i][img->pix_c_x+j]=img->cof[(i>>2)+2][(j>>2)+4][i & 0x03][j & 0x03];
3796
  }
3797
 
3798
  // for deblocking filter
3799
  currMb->qp=0;
3800
  set_chroma_qp(currMb);
3801
 
3802
  // for CAVLC: Set the nz_coeff to 16.
3803
  // These parameters are to be used in CAVLC decoding of neighbour blocks
3804
  for(i=0;i<4;i++)
3805
    for (j=0;j<(4 + img->num_blk8x8_uv);j++)
3806
      img->nz_coeff[img->current_mb_nr][i][j]=16;
3807
 
3808
 
3809
  // for CABAC decoding of MB skip flag
3810
  currMb->skip_flag = 0;
3811
 
3812
  //for deblocking filter CABAC
3813
  currMb->cbp_blk=0xFFFF;
3814
 
3815
  //For CABAC decoding of Dquant
3816
  last_dquant=0;
3817
}
3818
 
3819
/*!
3820
 ************************************************************************
3821
 * \brief
3822
 *    decode one macroblock
3823
 ************************************************************************
3824
 */
3825
 
3826
int decode_one_macroblock(struct img_par *img,struct inp_par *inp)
3827
{
3828
  int tmp_block[BLOCK_SIZE][BLOCK_SIZE];
3829
  int tmp_blockbw[BLOCK_SIZE][BLOCK_SIZE];
3830
  int i=0,j=0,k,l,ii=0,jj=0,i1=0,j1=0,j4=0,i4=0;
3831
  int uv, hv;
3832
  int vec1_x=0,vec1_y=0,vec2_x=0,vec2_y=0;
3833
  int ioff,joff;
3834
  int block8x8;   // needed for ABT
3835
 
3836
  int bw_pred=0, fw_pred=0, pred, ifx;
3837
  int ii0,jj0,ii1,jj1,if1,jf1,if0,jf0;
3838
  int mv_mul, f1_x, f1_y, f2_x, f2_y, f3, f4;
3839
 
3840
  static const byte decode_block_scan[16] = {0,1,4,5,2,3,6,7,8,9,12,13,10,11,14,15};
3841
 
3842
  Macroblock *currMB   = &img->mb_data[img->current_mb_nr];
3843
  short ref_idx, l0_refframe=-1, l1_refframe=-1;
3844
  int mv_mode, pred_dir, intra_prediction; // = currMB->ref_frame;
3845
  short l0_ref_idx=-1, l1_ref_idx=-1;
3846
  int alpha_l0, alpha_l1, wp_offset;
3847
 
3848
  short  *** mv_array, ***l0_mv_array, ***l1_mv_array;
3849
 
3850
  int mv_scale;
3851
 
3852
  int mb_nr     = img->current_mb_nr;
3853
  int smb       = ((img->type==SP_SLICE) && IS_INTER (currMB)) || (img->type == SI_SLICE && currMB->mb_type == SI4MB);
3854
  int list_offset;
3855
  int max_y_cr;
3856
 
3857
  StorablePicture **list;
3858
 
3859
  int jf;
3860
 
3861
  char l0_rFrame = -1, l1_rFrame = -1;
3862
 
3863
  short pmvl0[2]={0,0},
3864
        pmvl1[2]={0,0};
3865
 
3866
  int direct_pdir=-1;
3867
 
3868
  int curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
3869
 
3870
  byte  **    moving_block;
3871
  short ****  co_located_mv;
3872
  char  ***   co_located_ref_idx;
3873
  int64 ***   co_located_ref_id;
3874
 
3875
  int need_4x4_transform = (!currMB->luma_transform_size_8x8_flag);
3876
 
3877
  int b8, b4;
3878
  int uv_shift;
3879
  int yuv = dec_picture->chroma_format_idc - 1;
3880
 
3881
  if(img->type==SP_SLICE && currMB->mb_type!=I16MB)
3882
    smb=1;// modif ES added
3883
 
3884
  if(currMB->mb_type==IPCM)
3885
  {
3886
    //copy readed data into imgY and set parameters
3887
    decode_ipcm_mb(img);
3888
    return 0;
3889
  }
3890
 
3891
//////////////////////////
3892
 
3893
  // find out the correct list offsets
3894
  if (curr_mb_field)
3895
  {
3896
    if(mb_nr&0x01)
3897
    {
3898
      list_offset = 4; // top field mb
3899
      moving_block = Co_located->bottom_moving_block;
3900
      co_located_mv = Co_located->bottom_mv;
3901
      co_located_ref_idx = Co_located->bottom_ref_idx;
3902
      co_located_ref_id = Co_located->bottom_ref_pic_id;
3903
    }
3904
    else
3905
    {
3906
      list_offset = 2; // bottom field mb
3907
      moving_block = Co_located->top_moving_block;
3908
      co_located_mv = Co_located->top_mv;
3909
      co_located_ref_idx = Co_located->top_ref_idx;
3910
      co_located_ref_id = Co_located->top_ref_pic_id;
3911
    }
3912
    max_y_cr = (dec_picture->size_y_cr>>1)-1;
3913
  }
3914
  else
3915
  {
3916
    list_offset = 0;  // no mb aff or frame mb
3917
    moving_block = Co_located->moving_block;
3918
    co_located_mv = Co_located->mv;
3919
    co_located_ref_idx = Co_located->ref_idx;
3920
    co_located_ref_id = Co_located->ref_pic_id;
3921
    max_y_cr = dec_picture->size_y_cr-1;
3922
  }
3923
 
3924
  if (!img->MbaffFrameFlag)
3925
  {
3926
    for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
3927
    {
3928
      for(k = 0; k < listXsize[l]; k++)
3929
      {
3930
        listX[l][k]->chroma_vector_adjustment= 0;
3931
        if(img->structure == TOP_FIELD && img->structure != listX[l][k]->structure)
3932
          listX[l][k]->chroma_vector_adjustment = -2;
3933
        if(img->structure == BOTTOM_FIELD && img->structure != listX[l][k]->structure)
3934
          listX[l][k]->chroma_vector_adjustment = 2;
3935
      }
3936
    }
3937
  }
3938
  else
3939
  {
3940
    if (curr_mb_field)
3941
    {
3942
      for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
3943
      {
3944
        for(k = 0; k < listXsize[l]; k++)
3945
        {
3946
          listX[l][k]->chroma_vector_adjustment= 0;
3947
          if(img->current_mb_nr % 2 == 0 && listX[l][k]->structure == BOTTOM_FIELD)
3948
            listX[l][k]->chroma_vector_adjustment = -2;
3949
          if(img->current_mb_nr % 2 == 1 && listX[l][k]->structure == TOP_FIELD)
3950
            listX[l][k]->chroma_vector_adjustment = 2;
3951
        }
3952
      }
3953
    }
3954
    else
3955
    {
3956
      for (l = LIST_0 + list_offset; l <= (LIST_1 + list_offset); l++)
3957
      {
3958
        for(k = 0; k < listXsize[l]; k++)
3959
        {
3960
          listX[l][k]->chroma_vector_adjustment= 0;
3961
        }
3962
      }
3963
    }
3964
  }
3965
 
3966
  mv_mul=4;
3967
 
3968
  // luma decoding **************************************************
3969
 
3970
  // get prediction for INTRA_MB_16x16
3971
  if (IS_NEWINTRA (currMB))
3972
  {
3973
    intrapred_luma_16x16(img, currMB->i16mode);
3974
  }
3975
 
3976
  if (img->type==B_SLICE && img->direct_spatial_mv_pred_flag && (IS_DIRECT (currMB) ||
3977
    (IS_P8x8(currMB) && !(currMB->b8mode[0] && currMB->b8mode[1] && currMB->b8mode[2] && currMB->b8mode[3]))))
3978
  {
3979
    char l0_rFrameL, l0_rFrameU, l0_rFrameUL, l0_rFrameUR;
3980
    char l1_rFrameL, l1_rFrameU, l1_rFrameUL, l1_rFrameUR;
3981
 
3982
    PixelPos mb_left, mb_up, mb_upleft, mb_upright;
3983
 
3984
    getLuma4x4Neighbour(img->current_mb_nr, -1,  0, &mb_left);
3985
    getLuma4x4Neighbour(img->current_mb_nr,  0, -1, &mb_up);
3986
    getLuma4x4Neighbour(img->current_mb_nr, 16, -1, &mb_upright);
3987
    getLuma4x4Neighbour(img->current_mb_nr, -1, -1, &mb_upleft);
3988
 
3989
    if (!img->MbaffFrameFlag)
3990
    {
3991
      l0_rFrameL  = mb_left.available    ? dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]       : -1;
3992
      l0_rFrameU  = mb_up.available      ? dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x]           : -1;
3993
      l0_rFrameUL = mb_upleft.available  ? dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x]   : -1;
3994
      l0_rFrameUR = mb_upright.available ? dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] : l0_rFrameUL;
3995
 
3996
      l1_rFrameL = mb_left.available     ? dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x]       : -1;
3997
      l1_rFrameU = mb_up.available       ? dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x]           : -1;
3998
      l1_rFrameUL = mb_upleft.available  ? dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x]   : -1;
3999
      l1_rFrameUR = mb_upright.available ? dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] : l1_rFrameUL;
4000
    }
4001
    else
4002
    {
4003
      if (img->mb_data[img->current_mb_nr].mb_field)
4004
      {
4005
        l0_rFrameL = mb_left.available ?
4006
          img->mb_data[mb_left.mb_addr].mb_field  || dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] < 0?
4007
          dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] :
4008
          dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] * 2: -1;
4009
 
4010
        l0_rFrameU = mb_up.available ?
4011
          img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] < 0?
4012
          dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] :
4013
        dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] * 2: -1;
4014
 
4015
        l0_rFrameUL = mb_upleft.available ?
4016
          img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] < 0?
4017
          dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] :
4018
        dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] *2: -1;
4019
 
4020
        l0_rFrameUR = mb_upright.available ?
4021
          img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] < 0?
4022
          dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] :
4023
        dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] * 2: l0_rFrameUL;
4024
 
4025
        l1_rFrameL = mb_left.available ?
4026
          img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] < 0?
4027
          dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] :
4028
        dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] * 2: -1;
4029
 
4030
        l1_rFrameU = mb_up.available ?
4031
          img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] < 0?
4032
          dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] :
4033
        dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] * 2: -1;
4034
 
4035
        l1_rFrameUL = mb_upleft.available ?
4036
          img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] < 0?
4037
          dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] :
4038
        dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] *2: -1;
4039
 
4040
        l1_rFrameUR = mb_upright.available ?
4041
          img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] < 0?
4042
          dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] :
4043
        dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] * 2: l1_rFrameUL;
4044
      }
4045
      else
4046
      {
4047
        l0_rFrameL = mb_left.available ?
4048
          img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]  < 0 ?
4049
          dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] >> 1 :
4050
        dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]: -1;
4051
 
4052
        l0_rFrameU = mb_up.available ?
4053
          img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] < 0 ?
4054
          dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] >> 1 :
4055
        dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] : -1;
4056
 
4057
        l0_rFrameUL = mb_upleft.available ?
4058
          img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] < 0 ?
4059
          dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x]>> 1 :
4060
        dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
4061
 
4062
        l0_rFrameUR = mb_upright.available ?
4063
          img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
4064
          dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] >> 1 :
4065
        dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] : l0_rFrameUL;
4066
 
4067
        l1_rFrameL = mb_left.available ?
4068
          img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] < 0 ?
4069
          dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] >> 1 :
4070
        dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] : -1;
4071
 
4072
        l1_rFrameU = mb_up.available ?
4073
          img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] < 0 ?
4074
          dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] >> 1 :
4075
        dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] : -1;
4076
 
4077
        l1_rFrameUL = mb_upleft.available ?
4078
          img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] < 0 ?
4079
          dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] >> 1 :
4080
        dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
4081
 
4082
        l1_rFrameUR = mb_upright.available ?
4083
          img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
4084
          dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] >> 1:
4085
        dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] : l1_rFrameUL;
4086
      }
4087
    }
4088
 
4089
    l0_rFrame = (l0_rFrameL >= 0 && l0_rFrameU >= 0) ? imin(l0_rFrameL,l0_rFrameU): imax(l0_rFrameL,l0_rFrameU);
4090
    l0_rFrame = (l0_rFrame >= 0 && l0_rFrameUR >= 0) ? imin(l0_rFrame,l0_rFrameUR): imax(l0_rFrame,l0_rFrameUR);
4091
 
4092
    l1_rFrame = (l1_rFrameL >= 0 && l1_rFrameU >= 0) ? imin(l1_rFrameL,l1_rFrameU): imax(l1_rFrameL,l1_rFrameU);
4093
    l1_rFrame = (l1_rFrame >= 0 && l1_rFrameUR >= 0) ? imin(l1_rFrame,l1_rFrameUR): imax(l1_rFrame,l1_rFrameUR);
4094
 
4095
    if (l0_rFrame >=0)
4096
      SetMotionVectorPredictor (img, pmvl0, l0_rFrame, LIST_0, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
4097
 
4098
    if (l1_rFrame >=0)
4099
      SetMotionVectorPredictor (img, pmvl1, l1_rFrame, LIST_1, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
4100
  }
4101
 
4102
  for (block8x8=0; block8x8<4; block8x8++)
4103
  {
4104
    if (currMB->b8mode[block8x8] == I8MB)
4105
    {
4106
      //=========== 8x8 BLOCK TYPE ============
4107
      ioff = 8*(block8x8&0x01);
4108
      joff = 8*(block8x8>>1);
4109
 
4110
      //PREDICTION
4111
      intrapred8x8(img, block8x8);
4112
      itrans8x8(img,ioff,joff);      // use DCT transform and make 8x8 block m7 from prediction block mpr
4113
 
4114
      for(jj=joff;jj<joff + 8;jj++)
4115
      {
4116
        for(ii=ioff;ii<ioff + 8;ii++)
4117
        {
4118
          dec_picture->imgY[img->pix_y + jj][img->pix_x + ii] = img->m7[jj][ii]; // construct picture from 4x4 blocks
4119
        }
4120
      }
4121
      continue;
4122
    }
4123
 
4124
    for (k = block8x8*4; k < block8x8*4+4; k ++)
4125
    {
4126
      i =  (decode_block_scan[k] & 3);
4127
      j = ((decode_block_scan[k] >> 2) & 3);
4128
 
4129
      ioff = (i << 2);
4130
      i4   = img->block_x + i;
4131
 
4132
      joff = (j << 2);
4133
      j4   = img->block_y + j;
4134
 
4135
      mv_mode  = currMB->b8mode[2*(j>>1)+(i>>1)];
4136
      pred_dir = currMB->b8pdir[2*(j>>1)+(i>>1)];
4137
 
4138
      assert (pred_dir<=2);
4139
 
4140
      // PREDICTION
4141
      if (mv_mode==IBLOCK)
4142
      {
4143
        //===== INTRA PREDICTION =====
4144
        if (intrapred(img,ioff,joff,i4,j4)==SEARCH_SYNC)  /* make 4x4 prediction block mpr from given prediction img->mb_mode */
4145
          return SEARCH_SYNC;                   /* bit error */
4146
      }
4147
      else if (!IS_NEWINTRA (currMB))
4148
      {
4149
        if (pred_dir != 2)
4150
        {
4151
          //===== FORWARD/BACKWARD PREDICTION =====
4152
          l0_refframe = ref_idx  = dec_picture->ref_idx[LIST_0 + pred_dir][j4][i4];
4153
          mv_array = dec_picture->mv[LIST_0 + pred_dir];
4154
          list     = listX[0+list_offset+ pred_dir];
4155
          vec1_x = i4*4*mv_mul + mv_array[j4][i4][0];
4156
 
4157
          if (!curr_mb_field)
4158
          {
4159
            vec1_y = j4 * 4 * mv_mul + mv_array[j4][i4][1];
4160
          }
4161
          else
4162
          {
4163
            if ((mb_nr&0x01) == 0)
4164
              vec1_y = (img->block_y * 2 + joff) * mv_mul + mv_array[j4][i4][1];
4165
            else
4166
              vec1_y = ((img->block_y-4) * 2 + joff)* mv_mul + mv_array[j4][i4][1];
4167
          }
4168
 
4169
          get_block (ref_idx, list, vec1_x, vec1_y, img, tmp_block);
4170
 
4171
          if (img->apply_weights)
4172
          {
4173
            if (((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
4174
              (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE))) && curr_mb_field)
4175
            {
4176
              ref_idx >>=1;
4177
            }
4178
 
4179
            alpha_l0  = img->wp_weight[pred_dir][ref_idx][0];
4180
            wp_offset = img->wp_offset[pred_dir][l0_refframe>>curr_mb_field][0];
4181
            for(jj=0;jj<BLOCK_SIZE;jj++)
4182
            {
4183
              int jpos = jj+joff;
4184
              for(ii=0;ii<BLOCK_SIZE;ii++)
4185
                img->mpr[jpos][ii+ioff] = iClip1(img->max_imgpel_value,
4186
                (rshift_rnd_sf((alpha_l0 *  tmp_block[jj][ii]), img->luma_log2_weight_denom)  + wp_offset ));
4187
            }
4188
          }
4189
          else
4190
          {
4191
            for(jj=0;jj<BLOCK_SIZE;jj++)
4192
            {
4193
              int jpos = jj+joff;
4194
              for(ii=0;ii<BLOCK_SIZE;ii++)
4195
                img->mpr[jpos][ii+ioff] = tmp_block[jj][ii];
4196
            }
4197
          }
4198
        }
4199
        else
4200
        {
4201
          if (mv_mode != 0)
4202
          {
4203
            //===== BI-DIRECTIONAL PREDICTION =====
4204
            l0_mv_array = dec_picture->mv[LIST_0];
4205
            l1_mv_array = dec_picture->mv[LIST_1];
4206
 
4207
            l0_refframe = dec_picture->ref_idx[LIST_0][j4][i4];
4208
            l1_refframe = dec_picture->ref_idx[LIST_1][j4][i4];
4209
            l0_ref_idx  = l0_refframe;
4210
            l1_ref_idx  = l1_refframe;
4211
          }
4212
          else
4213
          {
4214
            //===== DIRECT PREDICTION =====
4215
            l0_mv_array = dec_picture->mv[LIST_0];
4216
            l1_mv_array = dec_picture->mv[LIST_1];
4217
            l1_refframe = 0;
4218
 
4219
            if (img->direct_spatial_mv_pred_flag )
4220
            {
4221
              int imgblock_y= ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? (img->block_y-4)>>1:img->block_y>>1: img->block_y;
4222
              int j6 = imgblock_y + j;
4223
 
4224
              if (l0_rFrame >=0)
4225
              {
4226
                if (!l0_rFrame  && ((!moving_block[j6][i4]) && (!listX[1+list_offset][0]->is_long_term)))
4227
                {
4228
                  dec_picture->mv  [LIST_0][j4][i4][0] = 0;
4229
                  dec_picture->mv  [LIST_0][j4][i4][1] = 0;
4230
                  dec_picture->ref_idx[LIST_0][j4][i4] = 0;
4231
                }
4232
                else
4233
                {
4234
                  dec_picture->mv  [LIST_0][j4][i4][0] = pmvl0[0];
4235
                  dec_picture->mv  [LIST_0][j4][i4][1] = pmvl0[1];
4236
                  dec_picture->ref_idx[LIST_0][j4][i4] = l0_rFrame;
4237
                }
4238
              }
4239
              else
4240
              {
4241
                dec_picture->ref_idx[LIST_0][j4][i4] = -1;
4242
                dec_picture->mv  [LIST_0][j4][i4][0] = 0;
4243
                dec_picture->mv  [LIST_0][j4][i4][1] = 0;
4244
              }
4245
 
4246
              if (l1_rFrame >=0)
4247
              {
4248
                if  (l1_rFrame==0 && ((!moving_block[j6][i4]) && (!listX[1+list_offset][0]->is_long_term)))
4249
                {
4250
 
4251
                  dec_picture->mv  [LIST_1][j4][i4][0] = 0;
4252
                  dec_picture->mv  [LIST_1][j4][i4][1] = 0;
4253
                  dec_picture->ref_idx[LIST_1][j4][i4] = l1_rFrame;
4254
 
4255
                }
4256
                else
4257
                {
4258
                  dec_picture->mv  [LIST_1][j4][i4][0] = pmvl1[0];
4259
                  dec_picture->mv  [LIST_1][j4][i4][1] = pmvl1[1];
4260
                  dec_picture->ref_idx[LIST_1][j4][i4] = l1_rFrame;
4261
                }
4262
              }
4263
              else
4264
              {
4265
                dec_picture->mv  [LIST_1][j4][i4][0] = 0;
4266
                dec_picture->mv  [LIST_1][j4][i4][1] = 0;
4267
                dec_picture->ref_idx[LIST_1][j4][i4] = -1;
4268
              }
4269
 
4270
              if (l0_rFrame < 0 && l1_rFrame < 0)
4271
              {
4272
                dec_picture->ref_idx[LIST_0][j4][i4] = 0;
4273
                dec_picture->ref_idx[LIST_1][j4][i4] = 0;
4274
              }
4275
 
4276
              l0_refframe = (dec_picture->ref_idx[LIST_0][j4][i4]!=-1) ? dec_picture->ref_idx[LIST_0][j4][i4] : 0;
4277
              l1_refframe = (dec_picture->ref_idx[LIST_1][j4][i4]!=-1) ? dec_picture->ref_idx[LIST_1][j4][i4] : 0;
4278
 
4279
              l0_ref_idx = l0_refframe;
4280
              l1_ref_idx = l1_refframe;
4281
 
4282
              if      (dec_picture->ref_idx[LIST_1][j4][i4]==-1)
4283
                direct_pdir = 0;
4284
              else if (dec_picture->ref_idx[LIST_0][j4][i4]==-1)
4285
                direct_pdir = 1;
4286
              else
4287
                direct_pdir = 2;
4288
 
4289
            }
4290
            else // Temporal Mode
4291
            {
4292
 
4293
              int imgblock_y= ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr&0x01) ? (img->block_y-4)>>1:img->block_y>>1: img->block_y;
4294
              int j6= imgblock_y + j;
4295
 
4296
              int refList = (co_located_ref_idx[LIST_0][j6][i4]== -1 ? LIST_1 : LIST_0);
4297
              int ref_idx =  co_located_ref_idx[refList][j6][i4];
4298
 
4299
 
4300
              if(ref_idx==-1) // co-located is intra mode
4301
              {
4302
                for(hv=0; hv<2; hv++)
4303
                {
4304
                  dec_picture->mv  [LIST_0][j4][i4][hv]=0;
4305
                  dec_picture->mv  [LIST_1][j4][i4][hv]=0;
4306
                }
4307
 
4308
                dec_picture->ref_idx[LIST_0][j4][i4] = 0;
4309
                dec_picture->ref_idx[LIST_1][j4][i4] = 0;
4310
 
4311
                l0_refframe = 0;
4312
                l0_ref_idx = 0;
4313
              }
4314
              else // co-located skip or inter mode
4315
              {
4316
                int mapped_idx=0;
4317
                int iref;
4318
 
4319
                {
4320
                  for (iref=0;iref<imin(img->num_ref_idx_l0_active,listXsize[LIST_0 + list_offset]);iref++)
4321
                  {
4322
                    if(img->structure==0 && curr_mb_field==0)
4323
                    {
4324
                      // If the current MB is a frame MB and the colocated is from a field picture,
4325
                      // then the co_located_ref_id may have been generated from the wrong value of
4326
                      // frame_poc if it references it's complementary field, so test both POC values
4327
                      if(listX[0][iref]->top_poc*2 == co_located_ref_id[refList][j6][i4] || listX[0][iref]->bottom_poc*2 == co_located_ref_id[refList][j6][i4])
4328
                      {
4329
                        mapped_idx=iref;
4330
                        break;
4331
                      }
4332
                      else //! invalid index. Default to zero even though this case should not happen
4333
                        mapped_idx=INVALIDINDEX;
4334
                      continue;
4335
                    }
4336
 
4337
                    if (dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][iref]==co_located_ref_id[refList][j6][i4])
4338
                    {
4339
                      mapped_idx=iref;
4340
                      break;
4341
                    }
4342
                    else //! invalid index. Default to zero even though this case should not happen
4343
                    {
4344
                      mapped_idx=INVALIDINDEX;
4345
                    }
4346
                  }
4347
                  if (INVALIDINDEX == mapped_idx)
4348
                  {
4349
                    error("temporal direct error\ncolocated block has ref that is unavailable",-1111);
4350
                  }
4351
                }
4352
 
4353
                l0_ref_idx = mapped_idx;
4354
                mv_scale = img->mvscale[LIST_0 + list_offset][mapped_idx];
4355
 
4356
                //! In such case, an array is needed for each different reference.
4357
                if (mv_scale == 9999 || listX[LIST_0+list_offset][mapped_idx]->is_long_term)
4358
                {
4359
                  dec_picture->mv  [LIST_0][j4][i4][0]=co_located_mv[refList][j6][i4][0];
4360
                  dec_picture->mv  [LIST_0][j4][i4][1]=co_located_mv[refList][j6][i4][1];
4361
 
4362
                  dec_picture->mv  [LIST_1][j4][i4][0]=0;
4363
                  dec_picture->mv  [LIST_1][j4][i4][1]=0;
4364
                }
4365
                else
4366
                {
4367
                  dec_picture->mv  [LIST_0][j4][i4][0]=(mv_scale * co_located_mv[refList][j6][i4][0] + 128 ) >> 8;
4368
                  dec_picture->mv  [LIST_0][j4][i4][1]=(mv_scale * co_located_mv[refList][j6][i4][1] + 128 ) >> 8;
4369
 
4370
                  dec_picture->mv  [LIST_1][j4][i4][0]=dec_picture->mv[LIST_0][j4][i4][0] - co_located_mv[refList][j6][i4][0] ;
4371
                  dec_picture->mv  [LIST_1][j4][i4][1]=dec_picture->mv[LIST_0][j4][i4][1] - co_located_mv[refList][j6][i4][1] ;
4372
                }
4373
 
4374
                l0_refframe = dec_picture->ref_idx[LIST_0][j4][i4] = mapped_idx; //listX[1][0]->ref_idx[refList][j4][i4];
4375
                l1_refframe = dec_picture->ref_idx[LIST_1][j4][i4] = 0;
4376
 
4377
                l0_ref_idx = l0_refframe;
4378
                l1_ref_idx = l1_refframe;
4379
              }
4380
            }
4381
            // store reference picture ID determined by direct mode
4382
            dec_picture->ref_pic_id[LIST_0][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][(short)dec_picture->ref_idx[LIST_0][j4][i4]];
4383
            dec_picture->ref_pic_id[LIST_1][j4][i4] = dec_picture->ref_pic_num[img->current_slice_nr][LIST_1 + list_offset][(short)dec_picture->ref_idx[LIST_1][j4][i4]];
4384
          }
4385
 
4386
          if (mv_mode==0 && img->direct_spatial_mv_pred_flag )
4387
          {
4388
            if (dec_picture->ref_idx[LIST_0][j4][i4] >= 0)
4389
            {
4390
 
4391
              vec1_x = i4*4*mv_mul + l0_mv_array[j4][i4][0];
4392
              if (!curr_mb_field)
4393
              {
4394
                vec1_y = j4*4*mv_mul + l0_mv_array[j4][i4][1];
4395
              }
4396
              else
4397
              {
4398
                if ((mb_nr&0x01) == 0)
4399
                {
4400
                  vec1_y = (img->block_y * 2 + joff) * mv_mul + l0_mv_array[j4][i4][1];
4401
                }
4402
                else
4403
                {
4404
                  vec1_y = ((img->block_y-4) * 2 + joff)* mv_mul + l0_mv_array[j4][i4][1];
4405
                }
4406
              }
4407
              get_block(l0_refframe, listX[0+list_offset], vec1_x, vec1_y, img, tmp_block);
4408
            }
4409
 
4410
            if (dec_picture->ref_idx[LIST_1][j4][i4] >= 0)
4411
            {
4412
              vec2_x = i4*4*mv_mul + l1_mv_array[j4][i4][0];
4413
              if (!curr_mb_field)
4414
              {
4415
                vec2_y = j4*4*mv_mul + l1_mv_array[j4][i4][1];
4416
              }
4417
              else
4418
              {
4419
                if ((mb_nr&0x01) == 0)
4420
                {
4421
                  vec2_y = (img->block_y * 2 + joff) * mv_mul + l1_mv_array[j4][i4][1];
4422
                }
4423
                else
4424
                {
4425
                  vec2_y = ((img->block_y-4) * 2 + joff)* mv_mul + l1_mv_array[j4][i4][1];
4426
                }
4427
              }
4428
              get_block(l1_refframe, listX[1+list_offset], vec2_x, vec2_y, img, tmp_blockbw);
4429
            }
4430
          }
4431
          else
4432
          {
4433
            vec1_x = i4 * 4 * mv_mul + l0_mv_array[j4][i4][0];
4434
            vec2_x = i4 * 4 * mv_mul + l1_mv_array[j4][i4][0];
4435
 
4436
            if (!curr_mb_field)
4437
            {
4438
              vec1_y = j4 * 4 * mv_mul + l0_mv_array[j4][i4][1];
4439
              vec2_y = j4 * 4 * mv_mul + l1_mv_array[j4][i4][1];
4440
            }
4441
            else
4442
            {
4443
              if ((mb_nr&0x01) == 0)
4444
              {
4445
                vec1_y = (img->block_y * 2 + joff) * mv_mul + l0_mv_array[j4][i4][1];
4446
                vec2_y = (img->block_y * 2 + joff) * mv_mul + l1_mv_array[j4][i4][1];
4447
              }
4448
              else
4449
              {
4450
                vec1_y = ((img->block_y-4) * 2 + joff)* mv_mul + l0_mv_array[j4][i4][1];
4451
                vec2_y = ((img->block_y-4) * 2 + joff)* mv_mul + l1_mv_array[j4][i4][1];
4452
              }
4453
            }
4454
 
4455
            get_block(l0_refframe, listX[0+list_offset], vec1_x, vec1_y, img, tmp_block);
4456
            get_block(l1_refframe, listX[1+list_offset], vec2_x, vec2_y, img, tmp_blockbw);
4457
          }
4458
 
4459
          if (mv_mode==0 && img->direct_spatial_mv_pred_flag && direct_pdir==0)
4460
          {
4461
            if (img->apply_weights)
4462
            {
4463
              if (((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
4464
                (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE))) && curr_mb_field)
4465
              {
4466
                l0_ref_idx >>=1;
4467
              }
4468
 
4469
              alpha_l0  = img->wp_weight[LIST_0][l0_ref_idx][0];
4470
              wp_offset = img->wp_offset[LIST_0][l0_refframe>>curr_mb_field][0];
4471
 
4472
              for(jj=0;jj<BLOCK_SIZE;jj++)
4473
                for(ii=0;ii<BLOCK_SIZE;ii++)
4474
                  img->mpr[jj+joff][ii+ioff] = iClip1(img->max_imgpel_value,
4475
                  (rshift_rnd_sf((alpha_l0 * tmp_block[jj][ii]), img->luma_log2_weight_denom) + wp_offset));
4476
            }
4477
            else
4478
            {
4479
              for(jj=0;jj<BLOCK_SIZE;jj++)
4480
                for(ii=0;ii<BLOCK_SIZE;ii++)
4481
                  img->mpr[jj+joff][ii+ioff] = tmp_block[jj][ii];
4482
            }
4483
          }
4484
          else if (mv_mode==0 && img->direct_spatial_mv_pred_flag && direct_pdir==1)
4485
          {
4486
            if (img->apply_weights)
4487
            {
4488
              if (((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
4489
                (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE))) && curr_mb_field)
4490
              {
4491
                l1_ref_idx >>=1;
4492
              }
4493
              alpha_l1  = img->wp_weight[LIST_1][l1_ref_idx][0];
4494
              wp_offset = img->wp_offset[LIST_1][l1_refframe>>curr_mb_field][0];
4495
 
4496
              for(jj=0;jj<BLOCK_SIZE;jj++)
4497
                for(ii=0;ii<BLOCK_SIZE;ii++)
4498
                  img->mpr[jj+joff][ii+ioff] = iClip1(img->max_imgpel_value,
4499
                  (rshift_rnd_sf((alpha_l1 * tmp_blockbw[jj][ii]), img->luma_log2_weight_denom) + wp_offset));
4500
            }
4501
            else
4502
            {
4503
              for(jj=0;jj<BLOCK_SIZE;jj++)
4504
                for(ii=0;ii<BLOCK_SIZE;ii++)
4505
                  img->mpr[jj+joff][ii+ioff] = tmp_blockbw[jj][ii];
4506
            }
4507
          }
4508
          else if(img->apply_weights)
4509
          {
4510
            int wt_list_offset = (active_pps->weighted_bipred_idc==2)?list_offset:0;
4511
 
4512
            if (mv_mode==0 && img->direct_spatial_mv_pred_flag==0 )l1_ref_idx=0;    //temporal direct
4513
 
4514
            if (((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
4515
              (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE))) && curr_mb_field)
4516
            {
4517
              l0_ref_idx >>=1;
4518
              l1_ref_idx >>=1;
4519
            }
4520
 
4521
            alpha_l0  =   img->wbp_weight[LIST_0 + wt_list_offset][l0_ref_idx][l1_ref_idx][0];
4522
            alpha_l1  =   img->wbp_weight[LIST_1 + wt_list_offset][l0_ref_idx][l1_ref_idx][0];
4523
            wp_offset = ((img->wp_offset [LIST_0 + wt_list_offset][l0_ref_idx][0] + img->wp_offset[LIST_1 + wt_list_offset][l1_ref_idx][0] + 1) >>1);
4524
 
4525
            for(ii=0;ii<BLOCK_SIZE;ii++)
4526
              for(jj=0;jj<BLOCK_SIZE;jj++)
4527
                img->mpr[jj+joff][ii+ioff] = (int)iClip1(img->max_imgpel_value,
4528
                (rshift_rnd_sf((alpha_l0 * tmp_block[jj][ii] + alpha_l1 * tmp_blockbw[jj][ii]), (img->luma_log2_weight_denom + 1)) + wp_offset));
4529
          }
4530
          else
4531
          {
4532
            for(jj=0;jj<BLOCK_SIZE;jj++)
4533
              for(ii=0;ii<BLOCK_SIZE;ii++)
4534
                img->mpr[jj+joff][ii+ioff] = (tmp_block[jj][ii]+tmp_blockbw[jj][ii]+1)>>1;
4535
          }
4536
        }
4537
      }
4538
 
4539
      // =============== 4x4 itrans ================
4540
      // -------------------------------------------
4541
      if (smb && mv_mode!=IBLOCK)
4542
      {
4543
        if(!IS_NEWINTRA (currMB))// modif ES added
4544
          itrans_sp(img,ioff,joff,i,j);
4545
        else //modif ES added
4546
          itrans(img,ioff,joff,i,j,0); // modif ES added
4547
      }
4548
      else
4549
      {
4550
        if(need_4x4_transform)
4551
        {
4552
          if(img->type==SP_SLICE && currMB->mb_type != I16MB) // ES added
4553
            itrans_sp(img,ioff,joff,i,j);//ES added
4554
          else
4555
            itrans   (img,ioff,joff,i,j, 0);      // use DCT transform and make 4x4 block m7 from prediction block mpr
4556
        }
4557
      }
4558
      if(need_4x4_transform)
4559
      {
4560
        int j_pos = j4 * BLOCK_SIZE;
4561
        int i_pos = i4 * BLOCK_SIZE;
4562
 
4563
        for(jj=0;jj<BLOCK_SIZE;jj++)
4564
        {
4565
          for(ii=0;ii<BLOCK_SIZE;ii++)
4566
          {
4567
            dec_picture->imgY[j_pos + jj][i_pos + ii]=img->m7[jj][ii]; // construct picture from 4x4 blocks
4568
          }
4569
        }
4570
      }// if(need_4x4_transform)
4571
    }
4572
 
4573
    if(!need_4x4_transform)
4574
    {
4575
      // =============== 8x8 itrans ================
4576
      // -------------------------------------------
4577
      ioff = 8*(block8x8&0x01);
4578
      joff = 8*(block8x8>>1);
4579
 
4580
      itrans8x8(img,ioff,joff);      // use DCT transform and make 8x8 block m7 from prediction block mpr
4581
 
4582
      for(jj=joff;jj<joff + 8;jj++)
4583
      {
4584
        for(ii=ioff;ii<ioff + 8;ii++)
4585
        {
4586
          dec_picture->imgY[img->pix_y + jj][img->pix_x + ii]=img->m7[jj][ii]; // construct picture from 4x4 blocks
4587
        }
4588
      }
4589
    }
4590
  }
4591
 
4592
  if (dec_picture->chroma_format_idc != YUV400)
4593
  {
4594
    // chroma decoding *******************************************************
4595
    f1_x = 64/img->mb_cr_size_x;
4596
    f2_x = f1_x-1;
4597
 
4598
    f1_y = 64/img->mb_cr_size_y;
4599
    f2_y = f1_y-1;
4600
 
4601
    f3 = f1_x*f1_y;
4602
    f4 = f3>>1;
4603
 
4604
    for(uv=0;uv<2;uv++)
4605
    {
4606
      uv_shift = uv*(img->num_blk8x8_uv>>1);
4607
      intra_prediction = IS_INTRA (currMB);
4608
 
4609
      if (intra_prediction)
4610
      {
4611
        intrapred_chroma(img, uv);
4612
      }
4613
 
4614
      for (b8=0;b8<(img->num_blk8x8_uv>>1);b8++)
4615
      {
4616
        for(b4=0;b4<4;b4++)
4617
        {
4618
          joff = subblk_offset_y[yuv][b8][b4];
4619
          j4   = img->pix_c_y+joff;
4620
          ioff = subblk_offset_x[yuv][b8][b4];
4621
          i4   = img->pix_c_x+ioff;
4622
 
4623
          mv_mode  = currMB->b8mode[block8x8_idx[yuv][b8][b4]];
4624
          pred_dir = currMB->b8pdir[block8x8_idx[yuv][b8][b4]];
4625
          assert (pred_dir<=2);
4626
 
4627
 
4628
          if (!intra_prediction)
4629
          {
4630
            if (pred_dir != 2)
4631
            {
4632
              int jpos;
4633
              //--- FORWARD/BACKWARD PREDICTION ---
4634
              mv_array = dec_picture->mv[LIST_0 + pred_dir];
4635
              list = listX[0+list_offset+pred_dir];
4636
 
4637
              for(jj=0;jj<4;jj++)
4638
              {
4639
                jf=(j4+jj)/(img->mb_size_blk[1][1]);     // jf  = Subblock_y-coordinate
4640
 
4641
                if (!curr_mb_field)
4642
                  jpos=(j4+jj)*f1_y;
4643
                else
4644
                {
4645
                  if ((mb_nr&0x01) == 0)
4646
                    jpos=(((img->pix_c_y)>>1)                   + jj + joff)*f1_y;
4647
                  else
4648
                    jpos=(((img->pix_c_y-img->mb_cr_size_y)>>1) + jj + joff)*f1_y;
4649
                }
4650
 
4651
                for(ii=0;ii<4;ii++)
4652
                {
4653
                  ifx=(i4+ii)/(img->mb_size_blk[1][0]);  // ifx = Subblock_x-coordinate
4654
                  l0_refframe = ref_idx = dec_picture->ref_idx[LIST_0+pred_dir][jf][ifx];
4655
                  i1=(i4+ii)*f1_x+mv_array[jf][ifx][0];
4656
 
4657
                  j1 = jpos+mv_array[jf][ifx][1];
4658
                  if (active_sps->chroma_format_idc == 1)
4659
                    j1 += list[ref_idx]->chroma_vector_adjustment;
4660
 
4661
                  ii0 = iClip3 (0, img->width_cr_m1, i1/f1_x);
4662
                  jj0 = iClip3 (0, max_y_cr, j1/f1_y);
4663
                  ii1 = iClip3 (0, img->width_cr_m1, ((i1+f2_x)/f1_x));
4664
                  jj1 = iClip3 (0, max_y_cr, ((j1+f2_y)/f1_y));
4665
 
4666
                  if1=(i1 & f2_x);
4667
                  jf1=(j1 & f2_y);
4668
                  if0=f1_x-if1;
4669
                  jf0=f1_y-jf1;
4670
 
4671
                  if (img->apply_weights)
4672
                  {
4673
                    imgpel **curUV = list[ref_idx]->imgUV[uv];
4674
                    pred = (if0*jf0*curUV[jj0][ii0] + if1*jf0*curUV[jj0][ii1]+
4675
                      if0*jf1*curUV[jj1][ii0] + if1*jf1*curUV[jj1][ii1]+f4)/f3;
4676
 
4677
                    if (((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
4678
                      (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE))) && curr_mb_field)
4679
                    {
4680
                      ref_idx >>=1;
4681
                    }
4682
 
4683
                    img->mpr[jj+joff][ii+ioff] = iClip1(img->max_imgpel_value_uv, (((img->wp_weight[pred_dir][ref_idx][uv+1] * pred  + img->wp_round_chroma)>>img->chroma_log2_weight_denom) + img->wp_offset[pred_dir][ref_idx][uv+1]));
4684
                  }
4685
                  else
4686
                  {
4687
                    imgpel **curUV = list[ref_idx]->imgUV[uv];
4688
                    img->mpr[jj+joff][ii+ioff]=(
4689
                      if0*jf0*curUV[jj0][ii0] + if1*jf0*curUV[jj0][ii1]+
4690
                      if0*jf1*curUV[jj1][ii0] + if1*jf1*curUV[jj1][ii1]+f4)/f3;
4691
                  }
4692
                }
4693
              }
4694
            }
4695
            else
4696
            {
4697
              l0_mv_array = dec_picture->mv[LIST_0];
4698
              l1_mv_array = dec_picture->mv[LIST_1];
4699
 
4700
              for(jj=0;jj<4;jj++)
4701
              {
4702
                int jpos;
4703
                jf=(j4+jj)/(img->mb_size_blk[1][1]);     // jf  = Subblock_y-coordinate
4704
 
4705
                if (!curr_mb_field)
4706
                {
4707
                  jpos=(j4+jj)*f1_y;
4708
                }
4709
                else
4710
                {
4711
                  if ((mb_nr&0x01) == 0)
4712
                    jpos=(((img->pix_c_y)>>1)                   + jj + joff)*f1_y;
4713
                  else
4714
                    jpos=(((img->pix_c_y-img->mb_cr_size_y)>>1) + jj + joff)*f1_y;
4715
                }
4716
 
4717
                for(ii=0;ii<4;ii++)
4718
                {
4719
                  ifx=(i4+ii)/(img->mb_size_blk[1][0]);  // ifx = Subblock_x-coordinate
4720
                  direct_pdir = 2;
4721
 
4722
                  if (mv_mode == 0 && img->direct_spatial_mv_pred_flag)
4723
                  {
4724
                    //===== DIRECT PREDICTION =====
4725
                    if (dec_picture->ref_idx[LIST_0][2*(jf>>1)][(ifx>>1)*2]!=-1)
4726
                    {
4727
                      l0_refframe = dec_picture->ref_idx[LIST_0][2*(jf>>1)][(ifx>>1)*2];
4728
                      l0_ref_idx = l0_refframe;
4729
                    }
4730
                    if (dec_picture->ref_idx[LIST_1][2*(jf>>1)][(ifx>>1)*2]!=-1)
4731
                    {
4732
                      l1_refframe = dec_picture->ref_idx[LIST_1][2*(jf>>1)][(ifx>>1)*2];
4733
                      l1_ref_idx = l1_refframe;
4734
                    }
4735
 
4736
                    if      (dec_picture->ref_idx[LIST_1][2*(jf>>1)][(ifx>>1)*2]==-1) direct_pdir = 0;
4737
                    else if (dec_picture->ref_idx[LIST_0][2*(jf>>1)][(ifx>>1)*2]==-1) direct_pdir = 1;
4738
 
4739
                    if (direct_pdir == 0 || direct_pdir == 2)
4740
                    {
4741
                      i1=(i4+ii)*f1_x+l0_mv_array[jf][ifx][0];
4742
 
4743
                      j1=jpos+l0_mv_array[jf][ifx][1];
4744
                      if (active_sps->chroma_format_idc == 1)
4745
                        j1 += listX[0+list_offset][l0_refframe]->chroma_vector_adjustment;
4746
 
4747
                      ii0 = iClip3 (0, img->width_cr_m1, i1/f1_x);
4748
                      jj0 = iClip3 (0, max_y_cr, j1/f1_y);
4749
                      ii1 = iClip3 (0, img->width_cr_m1, ((i1+f2_x)/f1_x));
4750
                      jj1 = iClip3 (0, max_y_cr, ((j1+f2_y)/f1_y));
4751
 
4752
 
4753
                      if1=(i1 & f2_x);
4754
                      jf1=(j1 & f2_y);
4755
                      if0=f1_x-if1;
4756
                      jf0=f1_y-jf1;
4757
 
4758
                      {
4759
                        imgpel **curUV = listX[LIST_0 + list_offset][l0_refframe]->imgUV[uv];
4760
                        fw_pred=(if0*jf0 * curUV[jj0][ii0]+
4761
                          if1*jf0 * curUV[jj0][ii1]+
4762
                          if0*jf1 * curUV[jj1][ii0]+
4763
                          if1*jf1 * curUV[jj1][ii1]+f4)/f3;
4764
                      }
4765
                    }
4766
                    if (direct_pdir == 1 || direct_pdir == 2)
4767
                    {
4768
                      i1=(i4+ii)*f1_x+l1_mv_array[jf][ifx][0];
4769
 
4770
                      j1=jpos+l1_mv_array[jf][ifx][1];
4771
                      if (active_sps->chroma_format_idc == 1)
4772
                        j1 += listX[1+list_offset][l1_refframe]->chroma_vector_adjustment;
4773
 
4774
                      ii0 = iClip3 (0, img->width_cr_m1, i1/f1_x);
4775
                      jj0 = iClip3 (0, max_y_cr, j1/f1_y);
4776
                      ii1 = iClip3 (0, img->width_cr_m1, ((i1+f2_x)/f1_x));
4777
                      jj1 = iClip3 (0, max_y_cr, ((j1+f2_y)/f1_y));
4778
 
4779
                      if1=(i1 & f2_x);
4780
                      jf1=(j1 & f2_y);
4781
                      if0=f1_x-if1;
4782
                      jf0=f1_y-jf1;
4783
 
4784
                      {
4785
                        imgpel **curUV = listX[1+list_offset][l1_refframe]->imgUV[uv];
4786
                        bw_pred=(if0*jf0*curUV[jj0][ii0] + if1*jf0*curUV[jj0][ii1]+
4787
                          if0*jf1*curUV[jj1][ii0] + if1*jf1*curUV[jj1][ii1]+f4)/f3;
4788
                      }
4789
                    }
4790
 
4791
                  }
4792
                  else
4793
                  {
4794
                    //===== BI-DIRECTIONAL PREDICTION =====
4795
                    l0_refframe = dec_picture->ref_idx[LIST_0][jf][ifx];
4796
                    l1_refframe = dec_picture->ref_idx[LIST_1][jf][ifx];
4797
 
4798
                    l0_ref_idx = l0_refframe;
4799
                    l1_ref_idx = l1_refframe;
4800
 
4801
                    i1=(i4+ii)*f1_x+l0_mv_array[jf][ifx][0];
4802
 
4803
                    j1=jpos+l0_mv_array[jf][ifx][1];
4804
                    if (active_sps->chroma_format_idc == 1)
4805
                      j1 += listX[0+list_offset][l0_refframe]->chroma_vector_adjustment;
4806
 
4807
                    ii0 = iClip3 (0, img->width_cr_m1, i1/f1_x);
4808
                    jj0 = iClip3 (0, max_y_cr, j1/f1_y);
4809
                    ii1 = iClip3 (0, img->width_cr_m1, ((i1+f2_x)/f1_x));
4810
                    jj1 = iClip3 (0, max_y_cr, ((j1+f2_y)/f1_y));
4811
 
4812
                    if1=(i1 & f2_x);
4813
                    jf1=(j1 & f2_y);
4814
                    if0=f1_x-if1;
4815
                    jf0=f1_y-jf1;
4816
                    {
4817
                      imgpel **curUV = listX[0+list_offset][l0_refframe]->imgUV[uv];
4818
 
4819
                      fw_pred=(if0*jf0*curUV[jj0][ii0]+
4820
                        if1*jf0*curUV[jj0][ii1]+
4821
                        if0*jf1*curUV[jj1][ii0]+
4822
                        if1*jf1*curUV[jj1][ii1]+f4)/f3;
4823
                    }
4824
                    i1=(i4+ii)*f1_x+l1_mv_array[jf][ifx][0];
4825
 
4826
                    j1=jpos+l1_mv_array[jf][ifx][1];
4827
                    if (active_sps->chroma_format_idc == 1)
4828
                      j1 += listX[1+list_offset][l1_refframe]->chroma_vector_adjustment;
4829
 
4830
                    ii0 = iClip3 (0, img->width_cr_m1, i1/f1_x);
4831
                    jj0 = iClip3 (0, max_y_cr, j1/f1_y);
4832
                    ii1 = iClip3 (0, img->width_cr_m1, ((i1+f2_x)/f1_x));
4833
                    jj1 = iClip3 (0, max_y_cr, ((j1+f2_y)/f1_y));
4834
 
4835
 
4836
                    if1=(i1 & f2_x);
4837
                    jf1=(j1 & f2_y);
4838
                    if0=f1_x-if1;
4839
                    jf0=f1_y-jf1;
4840
 
4841
                    {
4842
                      imgpel **curUV = listX[1+list_offset][l1_refframe]->imgUV[uv];
4843
                      bw_pred=(if0*jf0*curUV[jj0][ii0]+ if1*jf0*curUV[jj0][ii1]+
4844
                        if0*jf1*curUV[jj1][ii0]+if1*jf1*curUV[jj1][ii1]+f4)/f3;
4845
                    }
4846
 
4847
                  }
4848
 
4849
                  if (img->apply_weights)
4850
                  {
4851
                    if (((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
4852
                      (active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE))) && curr_mb_field)
4853
                    {
4854
                      l0_ref_idx >>=1;
4855
                      l1_ref_idx >>=1;
4856
                    }
4857
 
4858
                    if (img->direct_spatial_mv_pred_flag && direct_pdir==1)
4859
                    {
4860
                      img->mpr[jj+joff][ii+ioff]= iClip1(img->max_imgpel_value_uv,
4861
                        (((img->wp_weight[1][l1_ref_idx][uv+1] * bw_pred  + img->wp_round_chroma)>>img->chroma_log2_weight_denom) + img->wp_offset[1][l1_refframe>>curr_mb_field][uv+1]));   // Replaced with integer only operations
4862
                    }
4863
                    else if (img->direct_spatial_mv_pred_flag && direct_pdir==0)
4864
                    {
4865
                      img->mpr[jj+joff][ii+ioff]=iClip1(img->max_imgpel_value_uv, (((img->wp_weight[0][l0_ref_idx][uv+1] * fw_pred + img->wp_round_chroma)>>img->chroma_log2_weight_denom) + img->wp_offset[0][l0_refframe>>curr_mb_field][uv+1]));   // Replaced with integer only operations
4866
                    }
4867
                    else
4868
                    {
4869
                      int wt_list_offset = (active_pps->weighted_bipred_idc==2)?list_offset:0;
4870
 
4871
                      alpha_l0 = img->wbp_weight[0+wt_list_offset][l0_ref_idx][l1_ref_idx][uv+1];
4872
                      alpha_l1 = img->wbp_weight[1+wt_list_offset][l0_ref_idx][l1_ref_idx][uv+1];
4873
 
4874
                      img->mpr[jj+joff][ii+ioff]= iClip1(img->max_imgpel_value_uv, (((alpha_l0 * fw_pred + alpha_l1 * bw_pred  + (1<<img->chroma_log2_weight_denom)) >> (img->chroma_log2_weight_denom + 1))+ ((img->wp_offset[wt_list_offset + 0][l0_ref_idx][uv+1] + img->wp_offset[wt_list_offset + 1][l1_ref_idx][uv+1] + 1)>>1)));
4875
                    }
4876
                  }
4877
                  else
4878
                  {
4879
                    if (img->direct_spatial_mv_pred_flag && direct_pdir==1)
4880
                    {
4881
                      img->mpr[jj+joff][ii+ioff]=bw_pred;
4882
                    }
4883
                    else if (img->direct_spatial_mv_pred_flag && direct_pdir==0)
4884
                    {
4885
                      img->mpr[jj+joff][ii+ioff]=fw_pred;
4886
                    }
4887
                    else
4888
                    {
4889
                      img->mpr[jj+joff][ii+ioff]=(fw_pred + bw_pred + 1 )>>1;
4890
                    }
4891
                  }
4892
                }
4893
              }
4894
            }
4895
          } //if (!intra_prediction)
4896
 
4897
          if (!smb)
4898
          {
4899
            imgpel **curUV = dec_picture->imgUV[uv];
4900
            itrans(img,ioff,joff, cofuv_blk_x[yuv][b8+uv_shift][b4], cofuv_blk_y[yuv][b8+uv_shift][b4], 1);
4901
 
4902
            for(jj=0;jj<4;jj++)
4903
              for(ii=0;ii<4;ii++)
4904
              {
4905
                curUV[j4+jj][i4+ii]=img->m7[jj][ii];
4906
              }
4907
          }
4908
        }
4909
      }
4910
 
4911
      if(smb)
4912
      {
4913
        imgpel **curUV = dec_picture->imgUV[uv];
4914
        itrans_sp_chroma(img,2*uv);
4915
 
4916
        for (j=4;j<6;j++)
4917
        {
4918
          joff=(j-4)*4;
4919
          j4=img->pix_c_y+joff;
4920
          for(i=0;i<2;i++)
4921
          {
4922
            ioff=i*4;
4923
            i4=img->pix_c_x+ioff;
4924
            itrans(img,ioff,joff,2*uv+i,j, 1);
4925
 
4926
            for(jj=0;jj<4;jj++)
4927
              for(ii=0;ii<4;ii++)
4928
              {
4929
                curUV[j4+jj][i4+ii]=img->m7[jj][ii];
4930
              }
4931
          }
4932
        }
4933
      }
4934
    }
4935
  }
4936
 
4937
  return 0;
4938
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.