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

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [test/] [decoder/] [ldecod/] [src/] [cabac.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 cabac.c
5
 *
6
 * \brief
7
 *    CABAC entropy coding routines
8
 *
9
 * \author
10
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
11
 *    - Detlev Marpe                    <marpe@hhi.de>
12
 **************************************************************************************
13
 */
14
 
15
#include <stdlib.h>
16
#include <string.h>
17
 
18
#include "global.h"
19
#include "cabac.h"
20
#include "memalloc.h"
21
#include "elements.h"
22
#include "image.h"
23
#include "biaridecod.h"
24
#include "mb_access.h"
25
 
26
int symbolCount = 0;
27
int last_dquant = 0;
28
 
29
 
30
/***********************************************************************
31
 * L O C A L L Y   D E F I N E D   F U N C T I O N   P R O T O T Y P E S
32
 ***********************************************************************
33
 */
34
unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
35
                              BiContextTypePtr ctx,
36
                              int ctx_offset);
37
 
38
 
39
unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
40
                                  BiContextTypePtr ctx,
41
                                  int ctx_offset,
42
                                  unsigned int max_symbol);
43
 
44
unsigned int unary_exp_golomb_level_decode( DecodingEnvironmentPtr dep_dp,
45
                                            BiContextTypePtr ctx);
46
 
47
unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp,
48
                                        BiContextTypePtr ctx,
49
                                        unsigned int max_bin);
50
 
51
 
52
void CheckAvailabilityOfNeighborsCABAC()
53
{
54
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
55
  PixelPos up, left;
56
 
57
  getNeighbour(img->current_mb_nr, -1,  0, IS_LUMA, &left);
58
  getNeighbour(img->current_mb_nr,  0, -1, IS_LUMA, &up);
59
 
60
  if (up.available)
61
    currMB->mb_available_up = &img->mb_data[up.mb_addr];
62
  else
63
    currMB->mb_available_up = NULL;
64
 
65
  if (left.available)
66
    currMB->mb_available_left = &img->mb_data[left.mb_addr];
67
  else
68
    currMB->mb_available_left = NULL;
69
}
70
 
71
void cabac_new_slice()
72
{
73
  last_dquant=0;
74
}
75
 
76
/*!
77
 ************************************************************************
78
 * \brief
79
 *    Allocation of contexts models for the motion info
80
 *    used for arithmetic decoding
81
 *
82
 ************************************************************************
83
 */
84
MotionInfoContexts* create_contexts_MotionInfo(void)
85
{
86
  MotionInfoContexts *deco_ctx;
87
 
88
  deco_ctx = (MotionInfoContexts*) calloc(1, sizeof(MotionInfoContexts) );
89
  if( deco_ctx == NULL )
90
    no_mem_exit("create_contexts_MotionInfo: deco_ctx");
91
 
92
  return deco_ctx;
93
}
94
 
95
 
96
/*!
97
 ************************************************************************
98
 * \brief
99
 *    Allocates of contexts models for the texture info
100
 *    used for arithmetic decoding
101
 ************************************************************************
102
 */
103
TextureInfoContexts* create_contexts_TextureInfo(void)
104
{
105
  TextureInfoContexts *deco_ctx;
106
 
107
  deco_ctx = (TextureInfoContexts*) calloc(1, sizeof(TextureInfoContexts) );
108
  if( deco_ctx == NULL )
109
    no_mem_exit("create_contexts_TextureInfo: deco_ctx");
110
 
111
  return deco_ctx;
112
}
113
 
114
 
115
 
116
 
117
/*!
118
 ************************************************************************
119
 * \brief
120
 *    Frees the memory of the contexts models
121
 *    used for arithmetic decoding of the motion info.
122
 ************************************************************************
123
 */
124
void delete_contexts_MotionInfo(MotionInfoContexts *deco_ctx)
125
{
126
  if( deco_ctx == NULL )
127
    return;
128
 
129
  free( deco_ctx );
130
 
131
  return;
132
}
133
 
134
 
135
/*!
136
 ************************************************************************
137
 * \brief
138
 *    Frees the memory of the contexts models
139
 *    used for arithmetic decoding of the texture info.
140
 ************************************************************************
141
 */
142
void delete_contexts_TextureInfo(TextureInfoContexts *deco_ctx)
143
{
144
  if( deco_ctx == NULL )
145
    return;
146
 
147
  free( deco_ctx );
148
 
149
  return;
150
}
151
 
152
void readFieldModeInfo_CABAC( SyntaxElement *se,
153
                              struct img_par *img,
154
                              DecodingEnvironmentPtr dep_dp)
155
{
156
  int a,b,act_ctx;
157
  MotionInfoContexts *ctx         = (img->currentSlice)->mot_ctx;
158
  Macroblock         *currMB      = &img->mb_data[img->current_mb_nr];
159
 
160
  if (currMB->mbAvailA)
161
    a = img->mb_data[currMB->mbAddrA].mb_field;
162
  else
163
    a = 0;
164
  if (currMB->mbAvailB)
165
    b = img->mb_data[currMB->mbAddrB].mb_field;
166
  else
167
    b=0;
168
 
169
  act_ctx = a + b;
170
 
171
  se->value1 = biari_decode_symbol (dep_dp, &ctx->mb_aff_contexts[act_ctx]);
172
 
173
#if TRACE
174
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
175
  fflush(p_trace);
176
#endif
177
}
178
 
179
 
180
int check_next_mb_and_get_field_mode_CABAC( SyntaxElement *se,
181
                                            struct img_par *img,
182
                                            DataPartition  *act_dp)
183
{
184
  BiContextTypePtr          mb_type_ctx_copy[4];
185
  BiContextTypePtr          mb_aff_ctx_copy;
186
  DecodingEnvironmentPtr    dep_dp_copy;
187
 
188
  int length;
189
  DecodingEnvironmentPtr    dep_dp = &(act_dp->de_cabac);
190
 
191
  int bframe = (img->type==B_SLICE);
192
  int skip   = 0;
193
  int field  = 0;
194
  int i;
195
 
196
  Macroblock *currMB;
197
 
198
  //get next MB
199
  img->current_mb_nr++;
200
 
201
  currMB = &img->mb_data[img->current_mb_nr];
202
  currMB->slice_nr = img->current_slice_nr;
203
  currMB->mb_field = img->mb_data[img->current_mb_nr-1].mb_field;
204
 
205
  CheckAvailabilityOfNeighbors();
206
  CheckAvailabilityOfNeighborsCABAC();
207
 
208
  //create
209
  dep_dp_copy = (DecodingEnvironmentPtr) calloc(1, sizeof(DecodingEnvironment) );
210
  for (i=0;i<4;i++)
211
    mb_type_ctx_copy[i] = (BiContextTypePtr) calloc(NUM_MB_TYPE_CTX, sizeof(BiContextType) );
212
  mb_aff_ctx_copy = (BiContextTypePtr) calloc(NUM_MB_AFF_CTX, sizeof(BiContextType) );
213
 
214
  //copy
215
  memcpy(dep_dp_copy,dep_dp,sizeof(DecodingEnvironment));
216
  length = *(dep_dp_copy->Dcodestrm_len) = *(dep_dp->Dcodestrm_len);
217
  for (i=0;i<4;i++)
218
    memcpy(mb_type_ctx_copy[i], img->currentSlice->mot_ctx->mb_type_contexts[i],NUM_MB_TYPE_CTX*sizeof(BiContextType) );
219
  memcpy(mb_aff_ctx_copy, img->currentSlice->mot_ctx->mb_aff_contexts,NUM_MB_AFF_CTX*sizeof(BiContextType) );
220
 
221
 
222
  //check_next_mb
223
#if TRACE
224
  strncpy(se->tracestring, "mb_skip_flag (of following bottom MB)", TRACESTRING_SIZE);
225
#endif
226
  last_dquant = 0;
227
  readMB_skip_flagInfo_CABAC(se,img,dep_dp);
228
 
229
  skip = (bframe)? (se->value1==0 && se->value2==0) : (se->value1==0);
230
  if (!skip)
231
  {
232
#if TRACE
233
    strncpy(se->tracestring, "mb_field_decoding_flag (of following bottom MB)", TRACESTRING_SIZE);
234
#endif
235
    readFieldModeInfo_CABAC( se,img,dep_dp);
236
    field = se->value1;
237
    img->mb_data[img->current_mb_nr-1].mb_field = field;
238
  }
239
 
240
  //reset
241
  img->current_mb_nr--;
242
 
243
  memcpy(dep_dp,dep_dp_copy,sizeof(DecodingEnvironment));
244
  *(dep_dp->Dcodestrm_len) = length;
245
  for (i=0;i<4;i++)
246
    memcpy(img->currentSlice->mot_ctx->mb_type_contexts[i],mb_type_ctx_copy[i], NUM_MB_TYPE_CTX*sizeof(BiContextType) );
247
  memcpy( img->currentSlice->mot_ctx->mb_aff_contexts,mb_aff_ctx_copy,NUM_MB_AFF_CTX*sizeof(BiContextType) );
248
 
249
  CheckAvailabilityOfNeighborsCABAC();
250
 
251
  //delete
252
  free(dep_dp_copy);
253
  for (i=0;i<4;i++)
254
    free(mb_type_ctx_copy[i]);
255
  free(mb_aff_ctx_copy);
256
 
257
  return skip;
258
}
259
 
260
 
261
 
262
 
263
/*!
264
 ************************************************************************
265
 * \brief
266
 *    This function is used to arithmetically decode the motion
267
 *    vector data of a B-frame MB.
268
 ************************************************************************
269
 */
270
void readMVD_CABAC( SyntaxElement *se,
271
                    struct img_par *img,
272
                    DecodingEnvironmentPtr dep_dp)
273
{
274
  int i = img->subblock_x;
275
  int j = img->subblock_y;
276
  int a, b;
277
  int act_ctx;
278
  int act_sym;
279
  int mv_local_err;
280
  int mv_sign;
281
  int list_idx = se->value2 & 0x01;
282
  int k = (se->value2>>1); // MVD component
283
 
284
  PixelPos block_a, block_b;
285
 
286
  MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
287
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
288
 
289
  getLuma4x4Neighbour(img->current_mb_nr, (i<<2) - 1, (j<<2),     &block_a);
290
  getLuma4x4Neighbour(img->current_mb_nr, (i<<2),     (j<<2) - 1, &block_b);
291
 
292
  if (block_b.available)
293
  {
294
    b = iabs(img->mb_data[block_b.mb_addr].mvd[list_idx][block_b.y][block_b.x][k]);
295
    if (img->MbaffFrameFlag && (k==1))
296
    {
297
      if ((currMB->mb_field==0) && (img->mb_data[block_b.mb_addr].mb_field==1))
298
        b *= 2;
299
      else if ((currMB->mb_field==1) && (img->mb_data[block_b.mb_addr].mb_field==0))
300
        b /= 2;
301
    }
302
  }
303
  else
304
    b=0;
305
 
306
  if (block_a.available)
307
  {
308
    a = iabs(img->mb_data[block_a.mb_addr].mvd[list_idx][block_a.y][block_a.x][k]);
309
    if (img->MbaffFrameFlag && (k==1))
310
    {
311
      if ((currMB->mb_field==0) && (img->mb_data[block_a.mb_addr].mb_field==1))
312
        a *= 2;
313
      else if ((currMB->mb_field==1) && (img->mb_data[block_a.mb_addr].mb_field==0))
314
        a /= 2;
315
    }
316
  }
317
  else
318
    a = 0;
319
 
320
  if ((mv_local_err=a+b)<3)
321
    act_ctx = 5*k;
322
  else
323
  {
324
    if (mv_local_err>32)
325
      act_ctx=5*k+3;
326
    else
327
      act_ctx=5*k+2;
328
  }
329
  se->context = act_ctx;
330
 
331
  act_sym = biari_decode_symbol(dep_dp,&ctx->mv_res_contexts[0][act_ctx] );
332
 
333
  if (act_sym != 0)
334
  {
335
    act_ctx=5*k;
336
    act_sym = unary_exp_golomb_mv_decode(dep_dp,ctx->mv_res_contexts[1]+act_ctx,3);
337
    act_sym++;
338
    mv_sign = biari_decode_symbol_eq_prob(dep_dp);
339
 
340
    if(mv_sign)
341
      act_sym = -act_sym;
342
  }
343
  se->value1 = act_sym;
344
 
345
#if TRACE
346
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
347
  fflush(p_trace);
348
#endif
349
}
350
 
351
 
352
/*!
353
 ************************************************************************
354
 * \brief
355
 *    This function is used to arithmetically decode the 8x8 block type.
356
 ************************************************************************
357
 */
358
void readB8_typeInfo_CABAC (SyntaxElement *se,
359
                            struct img_par *img,
360
                            DecodingEnvironmentPtr dep_dp)
361
{
362
  int act_sym = 0;
363
  int bframe  = (img->type==B_SLICE);
364
 
365
  MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
366
 
367
 
368
  if (!bframe)
369
  {
370
    if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[0][1]))
371
    {
372
      act_sym = 0;
373
    }
374
    else
375
    {
376
      if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[0][3]))
377
      {
378
        if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[0][4])) act_sym = 2;
379
        else                                                            act_sym = 3;
380
      }
381
      else
382
      {
383
        act_sym = 1;
384
      }
385
    }
386
  }
387
  else
388
  {
389
    if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][0]))
390
    {
391
      if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][1]))
392
      {
393
        if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][2]))
394
        {
395
          if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3]))
396
          {
397
            act_sym = 10;
398
            if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym++;
399
          }
400
          else
401
          {
402
            act_sym = 6;
403
            if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym+=2;
404
            if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym++;
405
          }
406
        }
407
        else
408
        {
409
          act_sym=2;
410
          if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym+=2;
411
          if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym+=1;
412
        }
413
      }
414
      else
415
      {
416
        if (biari_decode_symbol (dep_dp, &ctx->b8_type_contexts[1][3])) act_sym = 1;
417
        else                                                            act_sym = 0;
418
      }
419
      act_sym++;
420
    }
421
    else
422
    {
423
      act_sym= 0;
424
    }
425
  }
426
  se->value1 = act_sym;
427
 
428
#if TRACE
429
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
430
  fflush(p_trace);
431
#endif
432
}
433
 
434
/*!
435
 ************************************************************************
436
 * \brief
437
 *    This function is used to arithmetically decode the macroblock
438
 *    type info of a given MB.
439
 ************************************************************************
440
 */
441
void readMB_skip_flagInfo_CABAC( SyntaxElement *se,
442
                                 struct img_par *img,
443
                                 DecodingEnvironmentPtr dep_dp)
444
{
445
  int a, b;
446
  int act_ctx;
447
  int bframe=(img->type==B_SLICE);
448
  MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
449
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
450
 
451
 
452
  if (bframe)
453
  {
454
    if (currMB->mb_available_up == NULL)
455
      b = 0;
456
    else
457
      b = (currMB->mb_available_up->skip_flag==0 ? 1 : 0);
458
    if (currMB->mb_available_left == NULL)
459
      a = 0;
460
    else
461
      a = (currMB->mb_available_left->skip_flag==0 ? 1 : 0);
462
 
463
    act_ctx = 7 + a + b;
464
 
465
    if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][act_ctx]) == 1)
466
      se->value1 = se->value2 = 0;
467
    else
468
      se->value1 = se->value2 = 1;
469
  }
470
  else
471
  {
472
    if (currMB->mb_available_up == NULL)
473
      b = 0;
474
    else
475
      b = (( (currMB->mb_available_up)->skip_flag == 0) ? 1 : 0 );
476
    if (currMB->mb_available_left == NULL)
477
      a = 0;
478
    else
479
      a = (( (currMB->mb_available_left)->skip_flag == 0) ? 1 : 0 );
480
 
481
    act_ctx = a + b;
482
 
483
    if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][act_ctx]) == 1)
484
      se->value1 = 0;
485
    else
486
      se->value1 = 1;
487
  }
488
 
489
 
490
#if TRACE
491
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
492
  fflush(p_trace);
493
#endif
494
  if (!se->value1)
495
  {
496
    last_dquant=0;
497
  }
498
  return;
499
}
500
 
501
/*!
502
***************************************************************************
503
* \brief
504
*    This function is used to arithmetically decode the macroblock
505
*    intra_pred_size flag info of a given MB.
506
***************************************************************************
507
*/
508
 
509
void readMB_transform_size_flag_CABAC( SyntaxElement *se,
510
                                  struct img_par *img,
511
                                  DecodingEnvironmentPtr dep_dp)
512
{
513
  int a, b;
514
  int act_ctx = 0;
515
  int act_sym;
516
 
517
  MotionInfoContexts *ctx         = (img->currentSlice)->mot_ctx;
518
  Macroblock         *currMB      = &img->mb_data[img->current_mb_nr];
519
 
520
  if (currMB->mb_available_up == NULL)
521
    b = 0;
522
  else
523
    b = currMB->mb_available_up->luma_transform_size_8x8_flag;
524
 
525
  if (currMB->mb_available_left == NULL)
526
    a = 0;
527
  else
528
    a = currMB->mb_available_left->luma_transform_size_8x8_flag;
529
 
530
  act_ctx     = a + b;
531
 
532
 
533
  act_sym = biari_decode_symbol(dep_dp, ctx->transform_size_contexts + act_ctx );
534
  se->value1 = act_sym;
535
 
536
#if TRACE
537
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
538
  fflush(p_trace);
539
#endif
540
 
541
}
542
 
543
/*!
544
 ************************************************************************
545
 * \brief
546
 *    This function is used to arithmetically decode the macroblock
547
 *    type info of a given MB.
548
 ************************************************************************
549
 */
550
void readMB_typeInfo_CABAC( SyntaxElement *se,
551
                            struct img_par *img,
552
                            DecodingEnvironmentPtr dep_dp)
553
{
554
  int a, b;
555
  int act_ctx;
556
  int act_sym;
557
  int bframe=(img->type==B_SLICE);
558
  int mode_sym;
559
  int ct = 0;
560
  int curr_mb_type;
561
 
562
 
563
  MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
564
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
565
 
566
  if(img->type == I_SLICE)  // INTRA-frame
567
  {
568
    if (currMB->mb_available_up == NULL)
569
      b = 0;
570
    else
571
      b = (((currMB->mb_available_up)->mb_type != I4MB && currMB->mb_available_up->mb_type != I8MB) ? 1 : 0 );
572
 
573
    if (currMB->mb_available_left == NULL)
574
      a = 0;
575
    else
576
      a = (((currMB->mb_available_left)->mb_type != I4MB && currMB->mb_available_left->mb_type != I8MB) ? 1 : 0 );
577
 
578
    act_ctx = a + b;
579
    act_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx);
580
    se->context = act_ctx; // store context
581
 
582
    if (act_sym==0) // 4x4 Intra
583
    {
584
      curr_mb_type = act_sym;
585
    }
586
    else // 16x16 Intra
587
    {
588
      mode_sym = biari_decode_final(dep_dp);
589
      if(mode_sym == 1)
590
      {
591
        curr_mb_type = 25;
592
      }
593
      else
594
      {
595
        act_sym = 1;
596
        act_ctx = 4;
597
        mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx ); // decoding of AC/no AC
598
        act_sym += mode_sym*12;
599
        act_ctx = 5;
600
        // decoding of cbp: 0,1,2
601
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
602
        if (mode_sym!=0)
603
        {
604
          act_ctx=6;
605
          mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
606
          act_sym+=4;
607
          if (mode_sym!=0)
608
              act_sym+=4;
609
            }
610
          // decoding of I pred-mode: 0,1,2,3
611
          act_ctx = 7;
612
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
613
          act_sym += mode_sym*2;
614
          act_ctx = 8;
615
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
616
          act_sym += mode_sym;
617
          curr_mb_type = act_sym;
618
      }
619
    }
620
  }
621
  else if(img->type == SI_SLICE)  // SI-frame
622
  {
623
    // special ctx's for SI4MB
624
    if (currMB->mb_available_up == NULL)
625
      b = 0;
626
    else
627
      b = (( (currMB->mb_available_up)->mb_type != SI4MB) ? 1 : 0 );
628
    if (currMB->mb_available_left == NULL)
629
      a = 0;
630
    else
631
      a = (( (currMB->mb_available_left)->mb_type != SI4MB) ? 1 : 0 );
632
 
633
    act_ctx = a + b;
634
    act_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx);
635
    se->context = act_ctx; // store context
636
 
637
    if (act_sym==0) //  SI 4x4 Intra
638
    {
639
      curr_mb_type = 0;
640
    }
641
    else // analog INTRA_IMG
642
    {
643
      if (currMB->mb_available_up == NULL)
644
        b = 0;
645
      else
646
        b = (( (currMB->mb_available_up)->mb_type != I4MB) ? 1 : 0 );
647
      if (currMB->mb_available_left == NULL)
648
        a = 0;
649
      else
650
        a = (( (currMB->mb_available_left)->mb_type != I4MB) ? 1 : 0 );
651
 
652
      act_ctx = a + b;
653
      act_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx);
654
      se->context = act_ctx; // store context
655
 
656
 
657
      if (act_sym==0) // 4x4 Intra
658
      {
659
        curr_mb_type = 1;
660
      }
661
      else // 16x16 Intra
662
      {
663
        mode_sym = biari_decode_final(dep_dp);
664
        if( mode_sym==1 )
665
        {
666
          curr_mb_type = 26;
667
        }
668
        else
669
        {
670
          act_sym = 2;
671
          act_ctx = 4;
672
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx ); // decoding of AC/no AC
673
          act_sym += mode_sym*12;
674
          act_ctx = 5;
675
          // decoding of cbp: 0,1,2
676
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
677
          if (mode_sym!=0)
678
          {
679
            act_ctx=6;
680
            mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
681
            act_sym+=4;
682
            if (mode_sym!=0)
683
              act_sym+=4;
684
          }
685
          // decoding of I pred-mode: 0,1,2,3
686
          act_ctx = 7;
687
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
688
          act_sym += mode_sym*2;
689
          act_ctx = 8;
690
          mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[0] + act_ctx );
691
          act_sym += mode_sym;
692
          curr_mb_type = act_sym;
693
        }
694
      }
695
    }
696
  }
697
  else
698
  {
699
    if (bframe)
700
    {
701
      ct = 1;
702
      if (currMB->mb_available_up == NULL)
703
        b = 0;
704
      else
705
        b = (( (currMB->mb_available_up)->mb_type != 0) ? 1 : 0 );
706
      if (currMB->mb_available_left == NULL)
707
        a = 0;
708
      else
709
        a = (( (currMB->mb_available_left)->mb_type != 0) ? 1 : 0 );
710
 
711
      act_ctx = a + b;
712
 
713
      if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][act_ctx]))
714
      {
715
        if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][4]))
716
        {
717
          if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][5]))
718
          {
719
            act_sym=12;
720
            if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=8;
721
            if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=4;
722
            if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=2;
723
 
724
            if      (act_sym==24)  act_sym=11;
725
            else if (act_sym==26)  act_sym=22;
726
            else
727
            {
728
              if (act_sym==22)     act_sym=23;
729
              if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=1;
730
            }
731
          }
732
          else
733
          {
734
            act_sym=3;
735
            if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=4;
736
            if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=2;
737
            if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym+=1;
738
          }
739
        }
740
        else
741
        {
742
          if (biari_decode_symbol (dep_dp, &ctx->mb_type_contexts[2][6])) act_sym=2;
743
          else                                                            act_sym=1;
744
        }
745
      }
746
      else
747
      {
748
        act_sym = 0;
749
      }
750
    }
751
    else // P-frame
752
    {
753
      {
754
        if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][4] ))
755
        {
756
          if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][7] ))   act_sym = 7;
757
          else                                                              act_sym = 6;
758
        }
759
        else
760
        {
761
          if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][5] ))
762
          {
763
            if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][7] )) act_sym = 2;
764
            else                                                            act_sym = 3;
765
          }
766
          else
767
          {
768
            if (biari_decode_symbol(dep_dp, &ctx->mb_type_contexts[1][6] )) act_sym = 4;
769
            else                                                            act_sym = 1;
770
          }
771
        }
772
      }
773
    }
774
 
775
    if (act_sym<=6 || (((img->type == B_SLICE)?1:0) && act_sym<=23))
776
    {
777
      curr_mb_type = act_sym;
778
    }
779
    else  // additional info for 16x16 Intra-mode
780
    {
781
      mode_sym = biari_decode_final(dep_dp);
782
      if( mode_sym==1 )
783
      {
784
        if(bframe)  // B frame
785
          curr_mb_type = 48;
786
        else      // P frame
787
          curr_mb_type = 31;
788
      }
789
      else
790
      {
791
        act_ctx = 8;
792
        mode_sym =  biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx ); // decoding of AC/no AC
793
        act_sym += mode_sym*12;
794
 
795
        // decoding of cbp: 0,1,2
796
        act_ctx = 9;
797
        mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
798
        if (mode_sym != 0)
799
        {
800
          act_sym+=4;
801
          mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
802
          if (mode_sym != 0)
803
            act_sym+=4;
804
        }
805
 
806
        // decoding of I pred-mode: 0,1,2,3
807
        act_ctx = 10;
808
        mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
809
        act_sym += mode_sym*2;
810
        mode_sym = biari_decode_symbol(dep_dp, ctx->mb_type_contexts[1] + act_ctx );
811
        act_sym += mode_sym;
812
        curr_mb_type = act_sym;
813
      }
814
    }
815
  }
816
  se->value1 = curr_mb_type;
817
 
818
//  if (curr_mb_type >= 23)       printf(" stopx");
819
#if TRACE
820
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
821
  fflush(p_trace);
822
#endif
823
}
824
 
825
/*!
826
 ************************************************************************
827
 * \brief
828
 *    This function is used to arithmetically decode a pair of
829
 *    intra prediction modes of a given MB.
830
 ************************************************************************
831
 */
832
void readIntraPredMode_CABAC( SyntaxElement *se,
833
                              struct img_par *img,
834
                              DecodingEnvironmentPtr dep_dp)
835
{
836
  TextureInfoContexts *ctx     = img->currentSlice->tex_ctx;
837
  int act_sym;
838
 
839
  // use_most_probable_mode
840
  act_sym = biari_decode_symbol(dep_dp, ctx->ipr_contexts);
841
 
842
  // remaining_mode_selector
843
  if (act_sym == 1)
844
    se->value1 = -1;
845
  else
846
  {
847
    se->value1  = 0;
848
    se->value1 |= (biari_decode_symbol(dep_dp, ctx->ipr_contexts+1)     );
849
    se->value1 |= (biari_decode_symbol(dep_dp, ctx->ipr_contexts+1) << 1);
850
    se->value1 |= (biari_decode_symbol(dep_dp, ctx->ipr_contexts+1) << 2);
851
  }
852
 
853
#if TRACE
854
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
855
  fflush(p_trace);
856
#endif
857
}
858
/*!
859
 ************************************************************************
860
 * \brief
861
 *    This function is used to arithmetically decode the reference
862
 *    parameter of a given MB.
863
 ************************************************************************
864
 */
865
void readRefFrame_CABAC( SyntaxElement *se,
866
                         struct img_par *img,
867
                         DecodingEnvironmentPtr dep_dp)
868
{
869
  MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
870
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
871
 
872
  int   addctx  = 0;
873
  int   a, b;
874
  int   act_ctx;
875
  int   act_sym;
876
  char** refframe_array = dec_picture->ref_idx[se->value2];
877
  int   b8a, b8b;
878
 
879
  PixelPos block_a, block_b;
880
 
881
  getLuma4x4Neighbour(img->current_mb_nr, (img->subblock_x<<2) - 1, (img->subblock_y<<2),     &block_a);
882
  getLuma4x4Neighbour(img->current_mb_nr, (img->subblock_x<<2),     (img->subblock_y<<2) - 1, &block_b);
883
 
884
  b8a=((block_a.x/2)%2)+2*((block_a.y/2)%2);
885
  b8b=((block_b.x/2)%2)+2*((block_b.y/2)%2);
886
 
887
  if (!block_b.available)
888
    b=0;
889
  else if ( (img->mb_data[block_b.mb_addr].mb_type==IPCM) || IS_DIRECT(&img->mb_data[block_b.mb_addr]) || (img->mb_data[block_b.mb_addr].b8mode[b8b]==0 && img->mb_data[block_b.mb_addr].b8pdir[b8b]==2))
890
    b=0;
891
  else
892
  {
893
    if (img->MbaffFrameFlag && (currMB->mb_field == 0) && (img->mb_data[block_b.mb_addr].mb_field == 1))
894
      b = (refframe_array[block_b.pos_y][block_b.pos_x] > 1 ? 1 : 0);
895
    else
896
      b = (refframe_array[block_b.pos_y][block_b.pos_x] > 0 ? 1 : 0);
897
  }
898
 
899
  if (!block_a.available)
900
    a=0;
901
  else if ((img->mb_data[block_a.mb_addr].mb_type==IPCM) || IS_DIRECT(&img->mb_data[block_a.mb_addr]) || (img->mb_data[block_a.mb_addr].b8mode[b8a]==0 && img->mb_data[block_a.mb_addr].b8pdir[b8a]==2))
902
    a=0;
903
  else
904
  {
905
    if (img->MbaffFrameFlag && (currMB->mb_field == 0) && (img->mb_data[block_a.mb_addr].mb_field == 1))
906
      a = (refframe_array[block_a.pos_y][block_a.pos_x] > 1 ? 1 : 0);
907
    else
908
      a = (refframe_array[block_a.pos_y][block_a.pos_x] > 0 ? 1 : 0);
909
  }
910
 
911
  act_ctx = a + 2*b;
912
  se->context = act_ctx; // store context
913
 
914
  act_sym = biari_decode_symbol(dep_dp,ctx->ref_no_contexts[addctx] + act_ctx );
915
 
916
  if (act_sym != 0)
917
  {
918
    act_ctx = 4;
919
    act_sym = unary_bin_decode(dep_dp,ctx->ref_no_contexts[addctx]+act_ctx,1);
920
    act_sym++;
921
  }
922
  se->value1 = act_sym;
923
 
924
#if TRACE
925
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
926
//  fprintf(p_trace," c: %d :%d \n",ctx->ref_no_contexts[addctx][act_ctx].cum_freq[0],ctx->ref_no_contexts[addctx][act_ctx].cum_freq[1]);
927
  fflush(p_trace);
928
#endif
929
}
930
 
931
 
932
/*!
933
 ************************************************************************
934
 * \brief
935
 *    This function is used to arithmetically decode the delta qp
936
 *     of a given MB.
937
 ************************************************************************
938
 */
939
void readDquant_CABAC( SyntaxElement *se,
940
                       struct img_par *img,
941
                       DecodingEnvironmentPtr dep_dp)
942
{
943
  MotionInfoContexts *ctx = img->currentSlice->mot_ctx;
944
 
945
  int act_ctx;
946
  int act_sym;
947
  int dquant;
948
 
949
  act_ctx = ( (last_dquant != 0) ? 1 : 0);
950
 
951
  act_sym = biari_decode_symbol(dep_dp,ctx->delta_qp_contexts + act_ctx );
952
  if (act_sym != 0)
953
  {
954
    act_ctx = 2;
955
    act_sym = unary_bin_decode(dep_dp,ctx->delta_qp_contexts+act_ctx,1);
956
    act_sym++;
957
  }
958
 
959
  dquant = (act_sym+1)/2;
960
  if((act_sym & 0x01)==0)                           // lsb is signed bit
961
    dquant = -dquant;
962
  se->value1 = dquant;
963
 
964
  last_dquant = dquant;
965
 
966
#if TRACE
967
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
968
  fflush(p_trace);
969
#endif
970
}
971
/*!
972
 ************************************************************************
973
 * \brief
974
 *    This function is used to arithmetically decode the coded
975
 *    block pattern of a given MB.
976
 ************************************************************************
977
 */
978
void readCBP_CABAC(SyntaxElement *se,
979
                   struct img_par *img,
980
                   DecodingEnvironmentPtr dep_dp)
981
{
982
  TextureInfoContexts *ctx = img->currentSlice->tex_ctx;
983
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
984
 
985
  int mb_x, mb_y;
986
  int a, b;
987
  int curr_cbp_ctx, curr_cbp_idx;
988
  int cbp = 0;
989
  int cbp_bit;
990
  int mask;
991
  PixelPos block_a;
992
 
993
  //  coding of luma part (bit by bit)
994
  for (mb_y=0; mb_y < 4; mb_y += 2)
995
  {
996
    for (mb_x=0; mb_x < 4; mb_x += 2)
997
    {
998
      if (currMB->b8mode[mb_y+(mb_x/2)]==IBLOCK)
999
        curr_cbp_idx = 0;
1000
      else
1001
        curr_cbp_idx = 1;
1002
 
1003
      if (mb_y == 0)
1004
      {
1005
        if (currMB->mb_available_up == NULL)
1006
          b = 0;
1007
        else
1008
        {
1009
          if((currMB->mb_available_up)->mb_type==IPCM)
1010
            b=0;
1011
          else
1012
            b = (( ((currMB->mb_available_up)->cbp & (1<<(2+mb_x/2))) == 0) ? 1 : 0);
1013
        }
1014
 
1015
      }
1016
      else
1017
        b = ( ((cbp & (1<<(mb_x/2))) == 0) ? 1: 0);
1018
 
1019
      if (mb_x == 0)
1020
      {
1021
        getLuma4x4Neighbour(img->current_mb_nr, (mb_x<<2) - 1, (mb_y << 2), &block_a);
1022
        if (block_a.available)
1023
        {
1024
          {
1025
            if(img->mb_data[block_a.mb_addr].mb_type==IPCM)
1026
              a=0;
1027
            else
1028
              a = (( (img->mb_data[block_a.mb_addr].cbp & (1<<(2*(block_a.y/2)+1))) == 0) ? 1 : 0);
1029
          }
1030
 
1031
        }
1032
        else
1033
          a=0;
1034
      }
1035
      else
1036
        a = ( ((cbp & (1<<mb_y)) == 0) ? 1: 0);
1037
 
1038
      curr_cbp_ctx = a+2*b;
1039
      mask = (1<<(mb_y+mb_x/2));
1040
      cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[0] + curr_cbp_ctx );
1041
      if (cbp_bit) cbp += mask;
1042
    }
1043
  }
1044
 
1045
 
1046
  if (dec_picture->chroma_format_idc != YUV400)
1047
  {
1048
    // coding of chroma part
1049
    // CABAC decoding for BinIdx 0
1050
    b = 0;
1051
    if (currMB->mb_available_up != NULL)
1052
    {
1053
      if((currMB->mb_available_up)->mb_type==IPCM)
1054
        b=1;
1055
      else
1056
        b = ((currMB->mb_available_up)->cbp > 15) ? 1 : 0;
1057
    }
1058
 
1059
 
1060
    a = 0;
1061
    if (currMB->mb_available_left != NULL)
1062
    {
1063
      if((currMB->mb_available_left)->mb_type==IPCM)
1064
        a=1;
1065
      else
1066
        a = ((currMB->mb_available_left)->cbp > 15) ? 1 : 0;
1067
    }
1068
 
1069
 
1070
    curr_cbp_ctx = a+2*b;
1071
    cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[1] + curr_cbp_ctx );
1072
 
1073
    // CABAC decoding for BinIdx 1
1074
    if (cbp_bit) // set the chroma bits
1075
    {
1076
      b = 0;
1077
      if (currMB->mb_available_up != NULL)
1078
      {
1079
        if((currMB->mb_available_up)->mb_type==IPCM)
1080
          b=1;
1081
        else
1082
          if ((currMB->mb_available_up)->cbp > 15)
1083
            b = (( ((currMB->mb_available_up)->cbp >> 4) == 2) ? 1 : 0);
1084
      }
1085
 
1086
 
1087
      a = 0;
1088
      if (currMB->mb_available_left != NULL)
1089
      {
1090
        if((currMB->mb_available_left)->mb_type==IPCM)
1091
          a=1;
1092
        else
1093
          if ((currMB->mb_available_left)->cbp > 15)
1094
            a = (( ((currMB->mb_available_left)->cbp >> 4) == 2) ? 1 : 0);
1095
      }
1096
 
1097
 
1098
      curr_cbp_ctx = a+2*b;
1099
      cbp_bit = biari_decode_symbol(dep_dp, ctx->cbp_contexts[2] + curr_cbp_ctx );
1100
      cbp += (cbp_bit == 1) ? 32 : 16;
1101
    }
1102
  }
1103
 
1104
  se->value1 = cbp;
1105
 
1106
  if (!cbp)
1107
  {
1108
    last_dquant=0;
1109
  }
1110
 
1111
#if TRACE
1112
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
1113
  fflush(p_trace);
1114
#endif
1115
}
1116
 
1117
/*!
1118
 ************************************************************************
1119
 * \brief
1120
 *    This function is used to arithmetically decode the chroma
1121
 *    intra prediction mode of a given MB.
1122
 ************************************************************************
1123
 */  //GB
1124
void readCIPredMode_CABAC(SyntaxElement *se,
1125
                          struct img_par *img,
1126
                          DecodingEnvironmentPtr dep_dp)
1127
{
1128
 
1129
  TextureInfoContexts *ctx = img->currentSlice->tex_ctx;
1130
  Macroblock          *currMB  = &img->mb_data[img->current_mb_nr];
1131
  int                 act_ctx,a,b;
1132
  int                 act_sym  = se->value1;
1133
 
1134
  if (currMB->mb_available_up == NULL) b = 0;
1135
  else
1136
  {
1137
    if( (currMB->mb_available_up)->mb_type==IPCM)
1138
      b=0;
1139
    else
1140
      b = ( ((currMB->mb_available_up)->c_ipred_mode != 0) ? 1 : 0);
1141
  }
1142
 
1143
 
1144
  if (currMB->mb_available_left == NULL) a = 0;
1145
  else
1146
  {
1147
    if( (currMB->mb_available_left)->mb_type==IPCM)
1148
      a=0;
1149
    else
1150
      a = ( ((currMB->mb_available_left)->c_ipred_mode != 0) ? 1 : 0);
1151
  }
1152
 
1153
 
1154
  act_ctx = a+b;
1155
 
1156
  act_sym = biari_decode_symbol(dep_dp, ctx->cipr_contexts + act_ctx );
1157
 
1158
  if (act_sym!=0)
1159
    act_sym = unary_bin_max_decode(dep_dp,ctx->cipr_contexts+3,0,2)+1;
1160
 
1161
 
1162
  se->value1 = act_sym;
1163
 
1164
 
1165
#if TRACE
1166
  fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, se->tracestring, se->value1);
1167
  fflush(p_trace);
1168
#endif
1169
 
1170
}
1171
 
1172
static const int maxpos       [] = {16, 15, 64, 32, 32, 16,  4, 15,  8, 16};
1173
static const int c1isdc       [] = { 1,  0,  1,  1,  1,  1,  1,  0,  1,  1};
1174
 
1175
static const int type2ctx_bcbp[] = { 0,  1,  2,  2,  3,  4,  5,  6,  5,  5}; // 7
1176
static const int type2ctx_map [] = { 0,  1,  2,  3,  4,  5,  6,  7,  6,  6}; // 8
1177
static const int type2ctx_last[] = { 0,  1,  2,  3,  4,  5,  6,  7,  6,  6}; // 8
1178
static const int type2ctx_one [] = { 0,  1,  2,  3,  3,  4,  5,  6,  5,  5}; // 7
1179
static const int type2ctx_abs [] = { 0,  1,  2,  3,  3,  4,  5,  6,  5,  5}; // 7
1180
static const int max_c2       [] = { 4,  4,  4,  4,  4,  4,  3,  4,  3,  3}; // 9
1181
 
1182
/*!
1183
 ************************************************************************
1184
 * \brief
1185
 *    Read CBP4-BIT
1186
 ************************************************************************
1187
*/
1188
int read_and_store_CBP_block_bit (Macroblock              *currMB,
1189
                                  DecodingEnvironmentPtr  dep_dp,
1190
                                  struct img_par          *img,
1191
                                  int                     type)
1192
{
1193
#define BIT_SET(x,n)  ((int)(((x)&((int64)1<<(n)))>>(n)))
1194
 
1195
  int y_ac        = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4);
1196
  int y_dc        = (type==LUMA_16DC);
1197
  int u_ac        = (type==CHROMA_AC && !img->is_v_block);
1198
  int v_ac        = (type==CHROMA_AC &&  img->is_v_block);
1199
  int chroma_dc   = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
1200
  int u_dc        = (chroma_dc && !img->is_v_block);
1201
  int v_dc        = (chroma_dc &&  img->is_v_block);
1202
  int j           = (y_ac || u_ac || v_ac ? img->subblock_y : 0);
1203
  int i           = (y_ac || u_ac || v_ac ? img->subblock_x : 0);
1204
  int bit         = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
1205
  int default_bit = (img->is_intra_block ? 1 : 0);
1206
  int upper_bit   = default_bit;
1207
  int left_bit    = default_bit;
1208
  int cbp_bit     = 1;  // always one for 8x8 mode
1209
  int ctx;
1210
  int bit_pos_a   = 0;
1211
  int bit_pos_b   = 0;
1212
 
1213
  PixelPos block_a, block_b;
1214
  if (y_ac || y_dc)
1215
  {
1216
    getLuma4x4Neighbour(img->current_mb_nr, (i<<2) - 1, (j<<2),     &block_a);
1217
    getLuma4x4Neighbour(img->current_mb_nr, (i<<2),     (j<<2) - 1, &block_b);
1218
    if (y_ac)
1219
    {
1220
      if (block_a.available)
1221
        bit_pos_a = 4*block_a.y + block_a.x;
1222
      if (block_b.available)
1223
        bit_pos_b = 4*block_b.y + block_b.x;
1224
    }
1225
  }
1226
  else
1227
  {
1228
    getChroma4x4Neighbour(img->current_mb_nr, (i<<2) - 1, (j<<2),     &block_a);
1229
    getChroma4x4Neighbour(img->current_mb_nr, (i<<2),     (j<<2) - 1, &block_b);
1230
    if (u_ac||v_ac)
1231
    {
1232
      if (block_a.available)
1233
        bit_pos_a = 4*block_a.y + block_a.x;
1234
      if (block_b.available)
1235
        bit_pos_b = 4*block_b.y + block_b.x;
1236
    }
1237
  }
1238
 
1239
  if (type!=LUMA_8x8)
1240
  {
1241
    //--- get bits from neighbouring blocks ---
1242
    if (block_b.available)
1243
    {
1244
      if(img->mb_data[block_b.mb_addr].mb_type==IPCM)
1245
        upper_bit=1;
1246
      else
1247
        upper_bit = BIT_SET(img->mb_data[block_b.mb_addr].cbp_bits,bit+bit_pos_b);
1248
    }
1249
 
1250
 
1251
    if (block_a.available)
1252
    {
1253
      if(img->mb_data[block_a.mb_addr].mb_type==IPCM)
1254
        left_bit=1;
1255
      else
1256
        left_bit = BIT_SET(img->mb_data[block_a.mb_addr].cbp_bits,bit+bit_pos_a);
1257
    }
1258
 
1259
 
1260
    ctx = 2*upper_bit+left_bit;
1261
 
1262
 
1263
    //===== encode symbol =====
1264
    cbp_bit = biari_decode_symbol (dep_dp, img->currentSlice->tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
1265
  }
1266
 
1267
  //--- set bits for current block ---
1268
  bit         = (y_dc ? 0 : y_ac ? 1+4*j+i : u_dc ? 17 : v_dc ? 18 : u_ac ? 19+4*j+i : 35+4*j+i);
1269
 
1270
  if (cbp_bit)
1271
  {
1272
    if (type==LUMA_8x8)
1273
    {
1274
      currMB->cbp_bits   |= ((int64)1<< bit   );
1275
      currMB->cbp_bits   |= ((int64)1<<(bit+1));
1276
      currMB->cbp_bits   |= ((int64)1<<(bit+4));
1277
      currMB->cbp_bits   |= ((int64)1<<(bit+5));
1278
    }
1279
    else if (type==LUMA_8x4)
1280
    {
1281
      currMB->cbp_bits   |= ((int64)1<< bit   );
1282
      currMB->cbp_bits   |= ((int64)1<<(bit+1));
1283
    }
1284
    else if (type==LUMA_4x8)
1285
    {
1286
      currMB->cbp_bits   |= ((int64)1<< bit   );
1287
      currMB->cbp_bits   |= ((int64)1<<(bit+4));
1288
    }
1289
    else
1290
    {
1291
      currMB->cbp_bits   |= ((int64)1<<bit);
1292
    }
1293
  }
1294
 
1295
  return cbp_bit;
1296
}
1297
 
1298
 
1299
 
1300
 
1301
 
1302
//===== position -> ctx for MAP =====
1303
//--- zig-zag scan ----
1304
static const int  pos2ctx_map8x8 [] = { 0,  1,  2,  3,  4,  5,  5,  4,  4,  3,  3,  4,  4,  4,  5,  5,
1305
                                        4,  4,  4,  4,  3,  3,  6,  7,  7,  7,  8,  9, 10,  9,  8,  7,
1306
                                        7,  6, 11, 12, 13, 11,  6,  7,  8,  9, 14, 10,  9,  8,  6, 11,
1307
                                       12, 13, 11,  6,  9, 14, 10,  9, 11, 12, 13, 11 ,14, 10, 12, 14}; // 15 CTX
1308
static const int  pos2ctx_map8x4 [] = { 0,  1,  2,  3,  4,  5,  7,  8,  9, 10, 11,  9,  8,  6,  7,  8,
1309
                                        9, 10, 11,  9,  8,  6, 12,  8,  9, 10, 11,  9, 13, 13, 14, 14}; // 15 CTX
1310
static const int  pos2ctx_map4x4 [] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 14}; // 15 CTX
1311
static const int  pos2ctx_map2x4c[] = { 0,  0,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2}; // 15 CTX
1312
static const int  pos2ctx_map4x4c[] = { 0,  0,  0,  0,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2}; // 15 CTX
1313
static const int* pos2ctx_map    [] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
1314
                                       pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
1315
                                       pos2ctx_map2x4c, pos2ctx_map4x4c};
1316
//--- interlace scan ----
1317
//taken from ABT
1318
static const int  pos2ctx_map8x8i[] = { 0,  1,  1,  2,  2,  3,  3,  4,  5,  6,  7,  7,  7,  8,  4,  5,
1319
                                        6,  9, 10, 10,  8, 11, 12, 11,  9,  9, 10, 10,  8, 11, 12, 11,
1320
                                        9,  9, 10, 10,  8, 11, 12, 11,  9,  9, 10, 10,  8, 13, 13,  9,
1321
                                        9, 10, 10,  8, 13, 13,  9,  9, 10, 10, 14, 14, 14, 14, 14, 14}; // 15 CTX
1322
static const int  pos2ctx_map8x4i[] = { 0,  1,  2,  3,  4,  5,  6,  3,  4,  5,  6,  3,  4,  7,  6,  8,
1323
                                        9,  7,  6,  8,  9, 10, 11, 12, 12, 10, 11, 13, 13, 14, 14, 14}; // 15 CTX
1324
static const int  pos2ctx_map4x8i[] = { 0,  1,  1,  1,  2,  3,  3,  4,  4,  4,  5,  6,  2,  7,  7,  8,
1325
                                        8,  8,  5,  6,  9, 10, 10, 11, 11, 11, 12, 13, 13, 14, 14, 14}; // 15 CTX
1326
static const int* pos2ctx_map_int[] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
1327
                                       pos2ctx_map4x8i,pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
1328
                                       pos2ctx_map2x4c, pos2ctx_map4x4c};
1329
 
1330
 
1331
//===== position -> ctx for LAST =====
1332
static const int  pos2ctx_last8x8 [] = { 0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
1333
                                         2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
1334
                                         3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,
1335
                                         5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8}; //  9 CTX
1336
static const int  pos2ctx_last8x4 [] = { 0,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,
1337
                                         3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  6,  6,  7,  7,  8,  8}; //  9 CTX
1338
 
1339
static const int  pos2ctx_last4x4 [] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15}; // 15 CTX
1340
static const int  pos2ctx_last2x4c[] = { 0,  0,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2}; // 15 CTX
1341
static const int  pos2ctx_last4x4c[] = { 0,  0,  0,  0,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2}; // 15 CTX
1342
static const int* pos2ctx_last    [] = {pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
1343
                                        pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
1344
                                        pos2ctx_last2x4c, pos2ctx_last4x4c};
1345
 
1346
 
1347
 
1348
 
1349
 
1350
/*!
1351
 ************************************************************************
1352
 * \brief
1353
 *    Read Significance MAP
1354
 ************************************************************************
1355
 */
1356
int read_significance_map (Macroblock              *currMB,
1357
                           DecodingEnvironmentPtr  dep_dp,
1358
                           struct img_par          *img,
1359
                           int                     type,
1360
                           int                     coeff[])
1361
{
1362
  int   i, sig;
1363
  int   coeff_ctr = 0;
1364
  int   i0        = 0;
1365
  int   i1        = maxpos[type]-1;
1366
 
1367
  int               fld       = ( img->structure!=FRAME || currMB->mb_field );
1368
  const int **pos2ctx_Map = (fld) ? pos2ctx_map_int : pos2ctx_map;
1369
  TextureInfoContexts *tex_ctx = img->currentSlice->tex_ctx;
1370
 
1371
  BiContextTypePtr  map_ctx   = ( fld ? tex_ctx->fld_map_contexts[type2ctx_map [type]]
1372
                                      : tex_ctx->     map_contexts[type2ctx_map [type]] );
1373
  BiContextTypePtr  last_ctx  = ( fld ? tex_ctx->fld_last_contexts[type2ctx_last[type]]
1374
                                      : tex_ctx->    last_contexts[type2ctx_last[type]] );
1375
 
1376
  if (!c1isdc[type])
1377
  {
1378
    i0++; i1++; coeff--;
1379
  }
1380
 
1381
  for (i=i0; i<i1; i++) // if last coeff is reached, it has to be significant
1382
  {
1383
    //--- read significance symbol ---
1384
    sig = biari_decode_symbol   (dep_dp, map_ctx + pos2ctx_Map [type][i]);
1385
 
1386
    if (sig)
1387
    {
1388
      coeff[i] = 1;
1389
      coeff_ctr++;
1390
      //--- read last coefficient symbol ---
1391
      if (biari_decode_symbol (dep_dp, last_ctx + pos2ctx_last[type][i]))
1392
      {
1393
        for (i++; i<i1+1; i++) coeff[i] = 0;
1394
      }
1395
    }
1396
    else
1397
    {
1398
      coeff[i] = 0;
1399
    }
1400
  }
1401
  //--- last coefficient must be significant if no last symbol was received ---
1402
  if (i<i1+1)
1403
  {
1404
    coeff[i] = 1;
1405
    coeff_ctr++;
1406
  }
1407
 
1408
  return coeff_ctr;
1409
}
1410
 
1411
 
1412
 
1413
/*!
1414
 ************************************************************************
1415
 * \brief
1416
 *    Read Levels
1417
 ************************************************************************
1418
 */
1419
void read_significant_coefficients (DecodingEnvironmentPtr  dep_dp,
1420
                                    struct img_par          *img,
1421
                                    int                     type,
1422
                                    int                     coeff[])
1423
{
1424
  int   i, ctx;
1425
  int   c1 = 1;
1426
  int   c2 = 0;
1427
 
1428
  for (i=maxpos[type]-1; i>=0; i--)
1429
  {
1430
    if (coeff[i]!=0)
1431
    {
1432
      ctx = imin (c1,4);
1433
      coeff[i] += biari_decode_symbol (dep_dp, img->currentSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);
1434
      if (coeff[i]==2)
1435
      {
1436
        ctx = imin (c2, max_c2[type]);
1437
        coeff[i] += unary_exp_golomb_level_decode (dep_dp, img->currentSlice->tex_ctx->abs_contexts[type2ctx_abs[type]]+ctx);
1438
        c1=0;
1439
        c2++;
1440
      }
1441
      else if (c1)
1442
      {
1443
        c1++;
1444
      }
1445
      if (biari_decode_symbol_eq_prob(dep_dp))
1446
      {
1447
        coeff[i] *= -1;
1448
      }
1449
    }
1450
  }
1451
}
1452
 
1453
 
1454
/*!
1455
 ************************************************************************
1456
 * \brief
1457
 *    Read Block-Transform Coefficients
1458
 ************************************************************************
1459
 */
1460
void readRunLevel_CABAC (SyntaxElement  *se,
1461
                         struct img_par *img,
1462
                         DecodingEnvironmentPtr dep_dp)
1463
{
1464
  static int  coeff[64]; // one more for EOB
1465
  static int  coeff_ctr = -1;
1466
  static int  pos       =  0;
1467
 
1468
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
1469
 
1470
  //--- read coefficients for whole block ---
1471
  if (coeff_ctr < 0)
1472
  {
1473
    //===== decode CBP-BIT =====
1474
    if ((coeff_ctr = read_and_store_CBP_block_bit (currMB, dep_dp, img, se->context) )!=0)
1475
    {
1476
      //===== decode significance map =====
1477
      coeff_ctr = read_significance_map (currMB, dep_dp, img, se->context, coeff);
1478
 
1479
      //===== decode significant coefficients =====
1480
      read_significant_coefficients     (dep_dp, img, se->context, coeff);
1481
    }
1482
  }
1483
 
1484
  //--- set run and level ---
1485
  if (coeff_ctr)
1486
  {
1487
    //--- set run and level (coefficient) ---
1488
    for (se->value2=0; coeff[pos]==0; pos++, se->value2++);
1489
    se->value1=coeff[pos++];
1490
  }
1491
  else
1492
  {
1493
    //--- set run and level (EOB) ---
1494
    se->value1 = se->value2 = 0;
1495
  }
1496
  //--- decrement coefficient counter and re-set position ---
1497
  if (coeff_ctr-- == 0) pos=0;
1498
 
1499
#if TRACE
1500
  fprintf(p_trace, "@%-6d %-53s %3d  %3d\n",symbolCount++, se->tracestring, se->value1,se->value2);
1501
  fflush(p_trace);
1502
#endif
1503
}
1504
 
1505
 
1506
 
1507
/*!
1508
 ************************************************************************
1509
 * \brief
1510
 *    arithmetic decoding
1511
 ************************************************************************
1512
 */
1513
int readSyntaxElement_CABAC(SyntaxElement *se, struct img_par *img, DataPartition *this_dataPart)
1514
{
1515
  int curr_len;
1516
  DecodingEnvironmentPtr dep_dp = &(this_dataPart->de_cabac);
1517
 
1518
  curr_len = arideco_bits_read(dep_dp);
1519
 
1520
  // perform the actual decoding by calling the appropriate method
1521
  se->reading(se, img, dep_dp);
1522
 
1523
  return (se->len = (arideco_bits_read(dep_dp) - curr_len));
1524
}
1525
 
1526
 
1527
/*!
1528
 ************************************************************************
1529
 * \brief
1530
 *    decoding of unary binarization using one or 2 distinct
1531
 *    models for the first and all remaining bins; no terminating
1532
 *    "0" for max_symbol
1533
 ***********************************************************************
1534
 */
1535
unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
1536
                                  BiContextTypePtr ctx,
1537
                                  int ctx_offset,
1538
                                  unsigned int max_symbol)
1539
{
1540
  unsigned int l;
1541
  unsigned int symbol;
1542
  BiContextTypePtr ictx;
1543
 
1544
  symbol =  biari_decode_symbol(dep_dp, ctx );
1545
 
1546
  if (symbol==0)
1547
    return 0;
1548
  else
1549
  {
1550
    if (max_symbol == 1)
1551
    return symbol;
1552
    symbol=0;
1553
    ictx=ctx+ctx_offset;
1554
    do
1555
    {
1556
      l=biari_decode_symbol(dep_dp, ictx);
1557
      symbol++;
1558
    }
1559
    while( (l!=0) && (symbol<max_symbol-1) );
1560
    if ((l!=0) && (symbol==max_symbol-1))
1561
      symbol++;
1562
    return symbol;
1563
  }
1564
}
1565
 
1566
 
1567
/*!
1568
 ************************************************************************
1569
 * \brief
1570
 *    decoding of unary binarization using one or 2 distinct
1571
 *    models for the first and all remaining bins
1572
 ***********************************************************************
1573
 */
1574
unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
1575
                              BiContextTypePtr ctx,
1576
                              int ctx_offset)
1577
{
1578
  unsigned int l;
1579
  unsigned int symbol;
1580
  BiContextTypePtr ictx;
1581
 
1582
  symbol = biari_decode_symbol(dep_dp, ctx );
1583
 
1584
  if (symbol==0)
1585
    return 0;
1586
  else
1587
  {
1588
    symbol=0;
1589
    ictx=ctx+ctx_offset;
1590
    do
1591
    {
1592
      l=biari_decode_symbol(dep_dp, ictx);
1593
      symbol++;
1594
    }
1595
    while( l!=0 );
1596
    return symbol;
1597
  }
1598
}
1599
 
1600
 
1601
/*!
1602
 ************************************************************************
1603
 * \brief
1604
 *    finding end of a slice in case this is not the end of a frame
1605
 *
1606
 * Unsure whether the "correction" below actually solves an off-by-one
1607
 * problem or whether it introduces one in some cases :-(  Anyway,
1608
 * with this change the bit stream format works with CABAC again.
1609
 * StW, 8.7.02
1610
 ************************************************************************
1611
 */
1612
int cabac_startcode_follows(struct img_par *img, int eos_bit)
1613
{
1614
  Slice         *currSlice  = img->currentSlice;
1615
  int           *partMap    = assignSE2partition[currSlice->dp_mode];
1616
  DataPartition *dP;
1617
  unsigned int  bit;
1618
  DecodingEnvironmentPtr dep_dp;
1619
 
1620
  dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
1621
  dep_dp = &(dP->de_cabac);
1622
 
1623
  if( eos_bit )
1624
  {
1625
    bit = biari_decode_final (dep_dp); //GB
1626
 
1627
#if TRACE
1628
    fprintf(p_trace, "@%-6d %-63s (%3d)\n",symbolCount++, "end_of_slice_flag", bit);
1629
    fflush(p_trace);
1630
#endif
1631
  }
1632
  else
1633
  {
1634
    bit = 0;
1635
  }
1636
 
1637
  return (bit==1?1:0);
1638
}
1639
 
1640
 
1641
 
1642
 
1643
 
1644
/*!
1645
 ************************************************************************
1646
 * \brief
1647
 *    Exp Golomb binarization and decoding of a symbol
1648
 *    with prob. of 0.5
1649
 ************************************************************************
1650
 */
1651
unsigned int exp_golomb_decode_eq_prob( DecodingEnvironmentPtr dep_dp,
1652
                                        int k)
1653
{
1654
  unsigned int l;
1655
  int symbol = 0;
1656
  int binary_symbol = 0;
1657
 
1658
  do
1659
  {
1660
    l=biari_decode_symbol_eq_prob(dep_dp);
1661
    if (l==1)
1662
    {
1663
      symbol += (1<<k);
1664
      k++;
1665
    }
1666
  }
1667
  while (l!=0);
1668
 
1669
  while (k--)                             //next binary part
1670
    if (biari_decode_symbol_eq_prob(dep_dp)==1)
1671
      binary_symbol |= (1<<k);
1672
 
1673
  return (unsigned int) (symbol+binary_symbol);
1674
}
1675
 
1676
 
1677
/*!
1678
 ************************************************************************
1679
 * \brief
1680
 *    Exp-Golomb decoding for LEVELS
1681
 ***********************************************************************
1682
 */
1683
unsigned int unary_exp_golomb_level_decode( DecodingEnvironmentPtr dep_dp,
1684
                                            BiContextTypePtr ctx)
1685
{
1686
  unsigned int l,k;
1687
  unsigned int symbol;
1688
  unsigned int exp_start = 13;
1689
 
1690
  symbol = biari_decode_symbol(dep_dp, ctx );
1691
 
1692
  if (symbol==0)
1693
    return 0;
1694
  else
1695
  {
1696
    symbol=0;
1697
    k=1;
1698
    do
1699
    {
1700
      l=biari_decode_symbol(dep_dp, ctx);
1701
      symbol++;
1702
      k++;
1703
    }
1704
    while((l!=0) && (k!=exp_start));
1705
    if (l!=0)
1706
      symbol += exp_golomb_decode_eq_prob(dep_dp,0)+1;
1707
    return symbol;
1708
  }
1709
}
1710
 
1711
 
1712
 
1713
 
1714
/*!
1715
 ************************************************************************
1716
 * \brief
1717
 *    Exp-Golomb decoding for Motion Vectors
1718
 ***********************************************************************
1719
 */
1720
unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp,
1721
                                        BiContextTypePtr ctx,
1722
                                        unsigned int max_bin)
1723
{
1724
  unsigned int l,k;
1725
  unsigned int bin=1;
1726
  unsigned int symbol;
1727
  unsigned int exp_start = 8;
1728
 
1729
  BiContextTypePtr ictx=ctx;
1730
 
1731
  symbol = biari_decode_symbol(dep_dp, ictx );
1732
 
1733
  if (symbol==0)
1734
    return 0;
1735
  else
1736
  {
1737
    symbol=0;
1738
    k=1;
1739
 
1740
    ictx++;
1741
    do
1742
    {
1743
      l=biari_decode_symbol(dep_dp, ictx  );
1744
      if ((++bin)==2) ictx++;
1745
      if (bin==max_bin) ictx++;
1746
      symbol++;
1747
      k++;
1748
    }
1749
    while((l!=0) && (k!=exp_start));
1750
    if (l!=0)
1751
      symbol += exp_golomb_decode_eq_prob(dep_dp,3)+1;
1752
    return symbol;
1753
  }
1754
}
1755
 
1756
 
1757
/*!
1758
 ************************************************************************
1759
 * \brief
1760
 *    Read one byte from CABAC-partition.
1761
 *    Bitstream->read_len will be modified
1762
 *    (for IPCM CABAC  28/11/2003)
1763
 *
1764
 * \author
1765
 *    Dong Wang <Dong.Wang@bristol.ac.uk>
1766
 ************************************************************************
1767
*/
1768
void readIPCMBytes_CABAC(SyntaxElement *sym, Bitstream *currStream)
1769
{
1770
  int read_len = currStream->read_len;
1771
  int code_len = currStream->code_len;
1772
  byte *buf = currStream->streamBuffer;
1773
 
1774
  sym->len=8;
1775
 
1776
  if(read_len<code_len)
1777
    sym->inf=buf[read_len++];
1778
 
1779
  sym->value1=sym->inf;
1780
 
1781
  currStream->read_len=read_len;
1782
 
1783
#if TRACE
1784
  tracebits2(sym->tracestring, sym->len, sym->inf);
1785
#endif
1786
 
1787
}
1788
 

powered by: WebSVN 2.1.0

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