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