Subversion Repositories shark

Rev

Rev 1624 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1624 giacomo 1
/* getvlc.c, variable length decoding                                       */
2
 
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
 
5
/*
6
 * Disclaimer of Warranty
7
 *
8
 * These software programs are available to the user without any license fee or
9
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
10
 * any and all warranties, whether express, implied, or statuary, including any
11
 * implied warranties or merchantability or of fitness for a particular
12
 * purpose.  In no event shall the copyright-holder be liable for any
13
 * incidental, punitive, or consequential damages of any kind whatsoever
14
 * arising from the use of these programs.
15
 *
16
 * This disclaimer of warranty extends to the user of these programs and user's
17
 * customers, employees, agents, transferees, successors, and assigns.
18
 *
19
 * The MPEG Software Simulation Group does not represent or warrant that the
20
 * programs furnished hereunder are free of infringement of any third-party
21
 * patents.
22
 *
23
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24
 * are subject to royalty fees to patent holders.  Many of these patents are
25
 * general enough such that they are unavoidable regardless of implementation
26
 * design.
27
 *
28
 */
29
 
30
#include "config.h"
31
#include "global.h"
32
#include "getvlc.h"
33
 
34
/* private prototypes */
35
/* generic picture macroblock type processing functions */
36
static int Get_I_macroblock_type _ANSI_ARGS_((void));
37
static int Get_P_macroblock_type _ANSI_ARGS_((void));
38
static int Get_B_macroblock_type _ANSI_ARGS_((void));
39
static int Get_D_macroblock_type _ANSI_ARGS_((void));
40
 
41
/* spatial picture macroblock type processing functions */
42
static int Get_I_Spatial_macroblock_type _ANSI_ARGS_((void));
43
static int Get_P_Spatial_macroblock_type _ANSI_ARGS_((void));
44
static int Get_B_Spatial_macroblock_type _ANSI_ARGS_((void));
45
static int Get_SNR_macroblock_type _ANSI_ARGS_((void));
46
 
47
int Get_macroblock_type()
48
{
49
  int macroblock_type = 0;
50
 
51
  if (ld->scalable_mode==SC_SNR)
52
    macroblock_type = Get_SNR_macroblock_type();
53
  else
54
  {
55
    switch (picture_coding_type)
56
    {
57
    case I_TYPE:
58
      macroblock_type = ld->pict_scal ? Get_I_Spatial_macroblock_type() : Get_I_macroblock_type();
59
      break;
60
    case P_TYPE:
61
      macroblock_type = ld->pict_scal ? Get_P_Spatial_macroblock_type() : Get_P_macroblock_type();
62
      break;
63
    case B_TYPE:
64
      macroblock_type = ld->pict_scal ? Get_B_Spatial_macroblock_type() : Get_B_macroblock_type();
65
      break;
66
    case D_TYPE:
67
      macroblock_type = Get_D_macroblock_type();
68
      break;
69
    default:
70
      cprintf("Get_macroblock_type(): unrecognized picture coding type\n");
71
      break;
72
    }
73
  }
74
 
75
  return macroblock_type;
76
}
77
 
78
static int Get_I_macroblock_type()
79
{
80
#ifdef TRACE
81
  if (Trace_Flag)
82
    cprintf("macroblock_type(I) ");
83
#endif /* TRACE */
84
 
85
  if (Get_Bits1())
86
  {
87
#ifdef TRACE
88
    if (Trace_Flag)
89
      cprintf("(1): Intra (1)\n");
90
#endif /* TRACE */
91
    return 1;
92
  }
93
 
94
  if (!Get_Bits1())
95
  {
96
    if (!Quiet_Flag)
97
      cprintf("Invalid macroblock_type code\n");
98
    Fault_Flag = 1;
99
  }
100
 
101
#ifdef TRACE
102
  if (Trace_Flag)
103
    cprintf("(01): Intra, Quant (17)\n");
104
#endif /* TRACE */
105
 
106
  return 17;
107
}
108
 
109
static char *MBdescr[]={
110
  "",                  "Intra",        "No MC, Coded",         "",
111
  "Bwd, Not Coded",    "",             "Bwd, Coded",           "",
112
  "Fwd, Not Coded",    "",             "Fwd, Coded",           "",
113
  "Interp, Not Coded", "",             "Interp, Coded",        "",
114
  "",                  "Intra, Quant", "No MC, Coded, Quant",  "",
115
  "",                  "",             "Bwd, Coded, Quant",    "",
116
  "",                  "",             "Fwd, Coded, Quant",    "",
117
  "",                  "",             "Interp, Coded, Quant", ""
118
};
119
 
120
static int Get_P_macroblock_type()
121
{
122
  int code;
123
 
124
#ifdef TRACE
125
  if (Trace_Flag)
126
    cprintf("macroblock_type(P) (");
127
#endif /* TRACE */
128
 
129
  if ((code = Show_Bits(6))>=8)
130
  {
131
    code >>= 3;
132
    Flush_Buffer(PMBtab0[code].len);
133
#ifdef TRACE
134
    if (Trace_Flag)
135
    {
136
      Print_Bits(code,3,PMBtab0[code].len);
137
      cprintf("): %s (%d)\n",MBdescr[(int)PMBtab0[code].val],PMBtab0[code].val);
138
    }
139
#endif /* TRACE */
140
    return PMBtab0[code].val;
141
  }
142
 
143
  if (code==0)
144
  {
145
    if (!Quiet_Flag)
146
      cprintf("Invalid macroblock_type code\n");
147
    Fault_Flag = 1;
148
    return 0;
149
  }
150
 
151
  Flush_Buffer(PMBtab1[code].len);
152
 
153
#ifdef TRACE
154
  if (Trace_Flag)
155
  {
156
    Print_Bits(code,6,PMBtab1[code].len);
157
    cprintf("): %s (%d)\n",MBdescr[(int)PMBtab1[code].val],PMBtab1[code].val);
158
  }
159
#endif /* TRACE */
160
 
161
  return PMBtab1[code].val;
162
}
163
 
164
static int Get_B_macroblock_type()
165
{
166
  int code;
167
 
168
#ifdef TRACE
169
  if (Trace_Flag)
170
    cprintf("macroblock_type(B) (");
171
#endif /* TRACE */
172
 
173
  if ((code = Show_Bits(6))>=8)
174
  {
175
    code >>= 2;
176
    Flush_Buffer(BMBtab0[code].len);
177
 
178
#ifdef TRACE
179
    if (Trace_Flag)
180
    {
181
      Print_Bits(code,4,BMBtab0[code].len);
182
      cprintf("): %s (%d)\n",MBdescr[(int)BMBtab0[code].val],BMBtab0[code].val);
183
    }
184
#endif /* TRACE */
185
 
186
    return BMBtab0[code].val;
187
  }
188
 
189
  if (code==0)
190
  {
191
    if (!Quiet_Flag)
192
      cprintf("Invalid macroblock_type code\n");
193
    Fault_Flag = 1;
194
    return 0;
195
  }
196
 
197
  Flush_Buffer(BMBtab1[code].len);
198
 
199
#ifdef TRACE
200
  if (Trace_Flag)
201
  {
202
    Print_Bits(code,6,BMBtab1[code].len);
203
    cprintf("): %s (%d)\n",MBdescr[(int)BMBtab1[code].val],BMBtab1[code].val);
204
  }
205
#endif /* TRACE */
206
 
207
  return BMBtab1[code].val;
208
}
209
 
210
static int Get_D_macroblock_type()
211
{
212
  if (!Get_Bits1())
213
  {
214
    if (!Quiet_Flag)
215
      cprintf("Invalid macroblock_type code\n");
216
    Fault_Flag=1;
217
  }
218
 
219
  return 1;
220
}
221
 
222
/* macroblock_type for pictures with spatial scalability */
223
static int Get_I_Spatial_macroblock_type()
224
{
225
  int code;
226
 
227
#ifdef TRACE
228
  if (Trace_Flag)
229
    cprintf("macroblock_type(I,spat) (");
230
#endif /* TRACE */
231
 
232
  code = Show_Bits(4);
233
 
234
  if (code==0)
235
  {
236
    if (!Quiet_Flag)
237
      cprintf("Invalid macroblock_type code\n");
238
    Fault_Flag = 1;
239
    return 0;
240
  }
241
 
242
#ifdef TRACE
243
  if (Trace_Flag)
244
  {
245
    Print_Bits(code,4,spIMBtab[code].len);
246
    cprintf("): %02x\n",spIMBtab[code].val);
247
  }
248
#endif /* TRACE */
249
 
250
  Flush_Buffer(spIMBtab[code].len);
251
  return spIMBtab[code].val;
252
}
253
 
254
static int Get_P_Spatial_macroblock_type()
255
{
256
  int code;
257
 
258
#ifdef TRACE
259
  if (Trace_Flag)
260
    cprintf("macroblock_type(P,spat) (");
261
#endif /* TRACE */
262
 
263
  code = Show_Bits(7);
264
 
265
  if (code<2)
266
  {
267
    if (!Quiet_Flag)
268
      cprintf("Invalid macroblock_type code\n");
269
    Fault_Flag = 1;
270
    return 0;
271
  }
272
 
273
  if (code>=16)
274
  {
275
    code >>= 3;
276
    Flush_Buffer(spPMBtab0[code].len);
277
 
278
#ifdef TRACE
279
    if (Trace_Flag)
280
    {
281
      Print_Bits(code,4,spPMBtab0[code].len);
282
      cprintf("): %02x\n",spPMBtab0[code].val);
283
    }
284
#endif /* TRACE */
285
 
286
    return spPMBtab0[code].val;
287
  }
288
 
289
  Flush_Buffer(spPMBtab1[code].len);
290
 
291
#ifdef TRACE
292
  if (Trace_Flag)
293
  {
294
    Print_Bits(code,7,spPMBtab1[code].len);
295
    cprintf("): %02x\n",spPMBtab1[code].val);
296
  }
297
#endif /* TRACE */
298
 
299
  return spPMBtab1[code].val;
300
}
301
 
302
static int Get_B_Spatial_macroblock_type()
303
{
304
  int code;
305
  VLCtab *p;
306
 
307
#ifdef TRACE
308
  if (Trace_Flag)
309
    cprintf("macroblock_type(B,spat) (");
310
#endif /* TRACE */
311
 
312
  code = Show_Bits(9);
313
 
314
  if (code>=64)
315
    p = &spBMBtab0[(code>>5)-2];
316
  else if (code>=16)
317
    p = &spBMBtab1[(code>>2)-4];
318
  else if (code>=8)
319
    p = &spBMBtab2[code-8];
320
  else
321
  {
322
    if (!Quiet_Flag)
323
      cprintf("Invalid macroblock_type code\n");
324
    Fault_Flag = 1;
325
    return 0;
326
  }
327
 
328
  Flush_Buffer(p->len);
329
 
330
#ifdef TRACE
331
  if (Trace_Flag)
332
  {
333
    Print_Bits(code,9,p->len);
334
    printf("): %02x\n",p->val);
335
  }
336
#endif /* TRACE */
337
 
338
  return p->val;
339
}
340
 
341
static int Get_SNR_macroblock_type()
342
{
343
  int code;
344
 
345
#ifdef TRACE                    /* *CH* */
346
  if (Trace_Flag)
347
    cprintf("macroblock_type(SNR) (");
348
#endif
349
 
350
  code = Show_Bits(3);
351
 
352
  if (code==0)
353
  {
354
    if (!Quiet_Flag)
355
      cprintf("Invalid macroblock_type code\n");
356
    Fault_Flag = 1;
357
    return 0;
358
  }
359
 
360
  Flush_Buffer(SNRMBtab[code].len);
361
 
362
#ifdef TRACE                    /* *CH* */
363
  if (Trace_Flag)
364
  {
365
    Print_Bits(code,3,SNRMBtab[code].len);
366
    cprintf("): %s (%d)\n",MBdescr[(int)SNRMBtab[code].val],SNRMBtab[code].val);
367
  }
368
#endif
369
 
370
 
371
  return SNRMBtab[code].val;
372
}
373
 
374
int Get_motion_code()
375
{
376
  int code;
377
 
378
#ifdef TRACE
379
  if (Trace_Flag)
380
    cprintf("motion_code (");
381
#endif /* TRACE */
382
 
383
  if (Get_Bits1())
384
  {
385
#ifdef TRACE
386
    if (Trace_Flag)
387
      cprintf("0): 0\n");
388
#endif /* TRACE */
389
    return 0;
390
  }
391
 
392
  if ((code = Show_Bits(9))>=64)
393
  {
394
    code >>= 6;
395
    Flush_Buffer(MVtab0[code].len);
396
 
397
#ifdef TRACE
398
    if (Trace_Flag)
399
    {
400
      Print_Bits(code,3,MVtab0[code].len);
401
      cprintf("%d): %d\n",
402
        Show_Bits(1),Show_Bits(1)?-MVtab0[code].val:MVtab0[code].val);
403
    }
404
#endif /* TRACE */
405
 
406
    return Get_Bits1()?-MVtab0[code].val:MVtab0[code].val;
407
  }
408
 
409
  if (code>=24)
410
  {
411
    code >>= 3;
412
    Flush_Buffer(MVtab1[code].len);
413
 
414
#ifdef TRACE
415
    if (Trace_Flag)
416
    {
417
      Print_Bits(code,6,MVtab1[code].len);
418
      cprintf("%d): %d\n",
419
        Show_Bits(1),Show_Bits(1)?-MVtab1[code].val:MVtab1[code].val);
420
    }
421
#endif /* TRACE */
422
 
423
    return Get_Bits1()?-MVtab1[code].val:MVtab1[code].val;
424
  }
425
 
426
  if ((code-=12)<0)
427
  {
428
    if (!Quiet_Flag)
429
/* HACK */
430
      cprintf("Invalid motion_vector code (MBA %d, pic %d)\n", global_MBA, global_pic);
431
    Fault_Flag=1;
432
    return 0;
433
  }
434
 
435
  Flush_Buffer(MVtab2[code].len);
436
 
437
#ifdef TRACE
438
  if (Trace_Flag)
439
  {
440
    Print_Bits(code+12,9,MVtab2[code].len);
441
    cprintf("%d): %d\n",
442
      Show_Bits(1),Show_Bits(1)?-MVtab2[code].val:MVtab2[code].val);
443
  }
444
#endif /* TRACE */
445
 
446
  return Get_Bits1() ? -MVtab2[code].val : MVtab2[code].val;
447
}
448
 
449
/* get differential motion vector (for dual prime prediction) */
450
int Get_dmvector()
451
{
452
#ifdef TRACE
453
  if (Trace_Flag)
454
    cprintf("dmvector (");
455
#endif /* TRACE */
456
 
457
  if (Get_Bits(1))
458
  {
459
#ifdef TRACE
460
    if (Trace_Flag)
461
      cprintf(Show_Bits(1) ? "11): -1\n" : "10): 1\n");
462
#endif /* TRACE */
463
    return Get_Bits(1) ? -1 : 1;
464
  }
465
  else
466
  {
467
#ifdef TRACE
468
    if (Trace_Flag)
469
      cprintf("0): 0\n");
470
#endif /* TRACE */
471
    return 0;
472
  }
473
}
474
 
475
int Get_coded_block_pattern()
476
{
477
  int code;
478
 
479
#ifdef TRACE
480
  if (Trace_Flag)
481
    cprintf("coded_block_pattern_420 (");
482
#endif /* TRACE */
483
 
484
  if ((code = Show_Bits(9))>=128)
485
  {
486
    code >>= 4;
487
    Flush_Buffer(CBPtab0[code].len);
488
 
489
#ifdef TRACE
490
    if (Trace_Flag)
491
    {
492
      Print_Bits(code,5,CBPtab0[code].len);
493
      cprintf("): ");
494
      Print_Bits(CBPtab0[code].val,6,6);
495
      cprintf(" (%d)\n",CBPtab0[code].val);
496
    }
497
#endif /* TRACE */
498
 
499
    return CBPtab0[code].val;
500
  }
501
 
502
  if (code>=8)
503
  {
504
    code >>= 1;
505
    Flush_Buffer(CBPtab1[code].len);
506
 
507
#ifdef TRACE
508
    if (Trace_Flag)
509
    {
510
      Print_Bits(code,8,CBPtab1[code].len);
511
      cprintf("): ");
512
      Print_Bits(CBPtab1[code].val,6,6);
513
      cprintf(" (%d)\n",CBPtab1[code].val);
514
    }
515
#endif /* TRACE */
516
 
517
    return CBPtab1[code].val;
518
  }
519
 
520
  if (code<1)
521
  {
522
    if (!Quiet_Flag)
523
      cprintf("Invalid coded_block_pattern code\n");
524
    Fault_Flag = 1;
525
    return 0;
526
  }
527
 
528
  Flush_Buffer(CBPtab2[code].len);
529
 
530
#ifdef TRACE
531
  if (Trace_Flag)
532
  {
533
    Print_Bits(code,9,CBPtab2[code].len);
534
    cprintf("): ");
535
    Print_Bits(CBPtab2[code].val,6,6);
536
    cprintf(" (%d)\n",CBPtab2[code].val);
537
  }
538
#endif /* TRACE */
539
 
540
  return CBPtab2[code].val;
541
}
542
 
543
int Get_macroblock_address_increment()
544
{
545
  int code, val;
546
 
547
#ifdef TRACE
548
  if (Trace_Flag)
549
    cprintf("macroblock_address_increment (");
550
#endif /* TRACE */
551
 
552
  val = 0;
553
 
554
  while ((code = Show_Bits(11))<24)
555
  {
556
    if (code!=15) /* if not macroblock_stuffing */
557
    {
558
      if (code==8) /* if macroblock_escape */
559
      {
560
#ifdef TRACE
561
        if (Trace_Flag)
562
          cprintf("00000001000 ");
563
#endif /* TRACE */
564
 
565
        val+= 33;
566
      }
567
      else
568
      {
569
        if (!Quiet_Flag)
570
          cprintf("Invalid macroblock_address_increment code\n");
571
 
572
        Fault_Flag = 1;
573
        return 1;
574
      }
575
    }
576
    else /* macroblock suffing */
577
    {
578
#ifdef TRACE
579
      if (Trace_Flag)
580
        cprintf("00000001111 ");
581
#endif /* TRACE */
582
    }
583
 
584
    Flush_Buffer(11);
585
  }
586
 
587
  /* macroblock_address_increment == 1 */
588
  /* ('1' is in the MSB position of the lookahead) */
589
  if (code>=1024)
590
  {
591
    Flush_Buffer(1);
592
#ifdef TRACE
593
    if (Trace_Flag)
594
      cprintf("1): %d\n",val+1);
595
#endif /* TRACE */
596
    return val + 1;
597
  }
598
 
599
  /* codes 00010 ... 011xx */
600
  if (code>=128)
601
  {
602
    /* remove leading zeros */
603
    code >>= 6;
604
    Flush_Buffer(MBAtab1[code].len);
605
 
606
#ifdef TRACE
607
    if (Trace_Flag)
608
    {
609
      Print_Bits(code,5,MBAtab1[code].len);
610
      printf("): %d\n",val+MBAtab1[code].val);
611
    }
612
#endif /* TRACE */
613
 
614
 
615
    return val + MBAtab1[code].val;
616
  }
617
 
618
  /* codes 00000011000 ... 0000111xxxx */
619
  code-= 24; /* remove common base */
620
  Flush_Buffer(MBAtab2[code].len);
621
 
622
#ifdef TRACE
623
  if (Trace_Flag)
624
  {
625
    Print_Bits(code+24,11,MBAtab2[code].len);
626
    cprintf("): %d\n",val+MBAtab2[code].val);
627
  }
628
#endif /* TRACE */
629
 
630
  return val + MBAtab2[code].val;
631
}
632
 
633
/* combined MPEG-1 and MPEG-2 stage. parse VLC and
634
   perform dct_diff arithmetic.
635
 
636
   MPEG-1:  ISO/IEC 11172-2 section
637
   MPEG-2:  ISO/IEC 13818-2 section 7.2.1
638
 
639
   Note: the arithmetic here is presented more elegantly than
640
   the spec, yet the results, dct_diff, are the same.
641
*/
642
 
643
int Get_Luma_DC_dct_diff()
644
{
645
  int code, size, dct_diff;
646
 
647
#ifdef TRACE
648
/*
649
  if (Trace_Flag)
650
    printf("dct_dc_size_luminance: (");
651
*/
652
#endif /* TRACE */
653
 
654
  /* decode length */
655
  code = Show_Bits(5);
656
 
657
  if (code<31)
658
  {
659
    size = DClumtab0[code].val;
660
    Flush_Buffer(DClumtab0[code].len);
661
#ifdef TRACE
662
/*
663
    if (Trace_Flag)
664
    {
665
      Print_Bits(code,5,DClumtab0[code].len);
666
      printf("): %d",size);
667
    }
668
*/
669
#endif /* TRACE */
670
  }
671
  else
672
  {
673
    code = Show_Bits(9) - 0x1f0;
674
    size = DClumtab1[code].val;
675
    Flush_Buffer(DClumtab1[code].len);
676
 
677
#ifdef TRACE
678
/*
679
    if (Trace_Flag)
680
    {
681
      Print_Bits(code+0x1f0,9,DClumtab1[code].len);
682
      printf("): %d",size);
683
    }
684
*/
685
#endif /* TRACE */
686
  }
687
 
688
#ifdef TRACE
689
/*
690
  if (Trace_Flag)
691
    printf(", dct_dc_differential (");
692
*/
693
#endif /* TRACE */
694
 
695
  if (size==0)
696
    dct_diff = 0;
697
  else
698
  {
699
    dct_diff = Get_Bits(size);
700
#ifdef TRACE
701
/*
702
    if (Trace_Flag)
703
      Print_Bits(dct_diff,size,size);
704
*/
705
#endif /* TRACE */
706
    if ((dct_diff & (1<<(size-1)))==0)
707
      dct_diff-= (1<<size) - 1;
708
  }
709
 
710
#ifdef TRACE
711
/*
712
  if (Trace_Flag)
713
    printf("): %d\n",dct_diff);
714
*/
715
#endif /* TRACE */
716
 
717
  return dct_diff;
718
}
719
 
720
 
721
int Get_Chroma_DC_dct_diff()
722
{
723
  int code, size, dct_diff;
724
 
725
#ifdef TRACE
726
/*
727
  if (Trace_Flag)
728
    printf("dct_dc_size_chrominance: (");
729
*/
730
#endif /* TRACE */
731
 
732
  /* decode length */
733
  code = Show_Bits(5);
734
 
735
  if (code<31)
736
  {
737
    size = DCchromtab0[code].val;
738
    Flush_Buffer(DCchromtab0[code].len);
739
 
740
#ifdef TRACE
741
/*
742
    if (Trace_Flag)
743
    {
744
      Print_Bits(code,5,DCchromtab0[code].len);
745
      printf("): %d",size);
746
    }
747
*/
748
#endif /* TRACE */
749
  }
750
  else
751
  {
752
    code = Show_Bits(10) - 0x3e0;
753
    size = DCchromtab1[code].val;
754
    Flush_Buffer(DCchromtab1[code].len);
755
 
756
#ifdef TRACE
757
/*
758
    if (Trace_Flag)
759
    {
760
      Print_Bits(code+0x3e0,10,DCchromtab1[code].len);
761
      printf("): %d",size);
762
    }
763
*/
764
#endif /* TRACE */
765
  }
766
 
767
#ifdef TRACE
768
/*
769
  if (Trace_Flag)
770
    printf(", dct_dc_differential (");
771
*/
772
#endif /* TRACE */
773
 
774
  if (size==0)
775
    dct_diff = 0;
776
  else
777
  {
778
    dct_diff = Get_Bits(size);
779
#ifdef TRACE
780
/*
781
    if (Trace_Flag)
782
      Print_Bits(dct_diff,size,size);
783
*/
784
#endif /* TRACE */
785
    if ((dct_diff & (1<<(size-1)))==0)
786
      dct_diff-= (1<<size) - 1;
787
  }
788
 
789
#ifdef TRACE
790
/*
791
  if (Trace_Flag)
792
    printf("): %d\n",dct_diff);
793
*/
794
#endif /* TRACE */
795
 
796
  return dct_diff;
797
}