OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [getvlc.c] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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