]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/getblk.c
COMP: Remove compiler warnings using gcc
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / getblk.c
1 /* getblk.c, DCT block 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
35
36 /* defined in getvlc.h */
37 typedef struct {
38   char run, level, len;
39 } DCTtab;
40
41 extern DCTtab DCTtabfirst[],DCTtabnext[],DCTtab0[],DCTtab1[];
42 extern DCTtab DCTtab2[],DCTtab3[],DCTtab4[],DCTtab5[],DCTtab6[];
43 extern DCTtab DCTtab0a[],DCTtab1a[];
44
45
46 /* decode one intra coded MPEG-1 block */
47
48 void Decode_MPEG1_Intra_Block(comp,dc_dct_pred)
49 int comp;
50 int dc_dct_pred[];
51 {
52   int val, i, j, sign;
53   unsigned int code;
54   DCTtab *tab;
55   short *bp;
56
57   bp = ld->block[comp];
58
59   /* ISO/IEC 11172-2 section 2.4.3.7: Block layer. */
60   /* decode DC coefficients */
61   if (comp<4)
62     bp[0] = (dc_dct_pred[0]+=Get_Luma_DC_dct_diff()) << 3;
63   else if (comp==4)
64     bp[0] = (dc_dct_pred[1]+=Get_Chroma_DC_dct_diff()) << 3;
65   else
66     bp[0] = (dc_dct_pred[2]+=Get_Chroma_DC_dct_diff()) << 3;
67
68   if (Fault_Flag) return;
69
70   /* D-pictures do not contain AC coefficients */
71   if(picture_coding_type == D_TYPE)
72     return;
73
74   /* decode AC coefficients */
75   for (i=1; ; i++)
76   {
77     code = Show_Bits(16);
78     if (code>=16384)
79       tab = &DCTtabnext[(code>>12)-4];
80     else if (code>=1024)
81       tab = &DCTtab0[(code>>8)-4];
82     else if (code>=512)
83       tab = &DCTtab1[(code>>6)-8];
84     else if (code>=256)
85       tab = &DCTtab2[(code>>4)-16];
86     else if (code>=128)
87       tab = &DCTtab3[(code>>3)-16];
88     else if (code>=64)
89       tab = &DCTtab4[(code>>2)-16];
90     else if (code>=32)
91       tab = &DCTtab5[(code>>1)-16];
92     else if (code>=16)
93       tab = &DCTtab6[code-16];
94     else
95     {
96       if (!Quiet_Flag)
97         printf("invalid Huffman code in Decode_MPEG1_Intra_Block()\n");
98       Fault_Flag = 1;
99       return;
100     }
101
102     Flush_Buffer(tab->len);
103
104     if (tab->run==64) /* end_of_block */
105       return;
106
107     if (tab->run==65) /* escape */
108     {
109       i+= Get_Bits(6);
110
111       val = Get_Bits(8);
112       if (val==0)
113         val = Get_Bits(8);
114       else if (val==128)
115         val = Get_Bits(8) - 256;
116       else if (val>128)
117         val -= 256;
118
119       if((sign = (val<0)))
120         val = -val;
121     }
122     else
123     {
124       i+= tab->run;
125       val = tab->level;
126       sign = Get_Bits(1);
127     }
128
129     if (i>=64)
130     {
131       if (!Quiet_Flag)
132         fprintf(stderr,"DCT coeff index (i) out of bounds (intra)\n");
133       Fault_Flag = 1;
134       return;
135     }
136
137     j = scan[ZIG_ZAG][i];
138     val = (val*ld->quantizer_scale*ld->intra_quantizer_matrix[j]) >> 3;
139
140     /* mismatch control ('oddification') */
141     if (val!=0) /* should always be true, but it's not guaranteed */
142       val = (val-1) | 1; /* equivalent to: if ((val&1)==0) val = val - 1; */
143
144     /* saturation */
145     if (!sign)
146       bp[j] = (val>2047) ?  2047 :  val; /* positive */
147     else
148       bp[j] = (val>2048) ? -2048 : -val; /* negative */
149   }
150 }
151
152
153 /* decode one non-intra coded MPEG-1 block */
154
155 void Decode_MPEG1_Non_Intra_Block(comp)
156 int comp;
157 {
158   int val, i, j, sign;
159   unsigned int code;
160   DCTtab *tab;
161   short *bp;
162
163   bp = ld->block[comp];
164
165   /* decode AC coefficients */
166   for (i=0; ; i++)
167   {
168     code = Show_Bits(16);
169     if (code>=16384)
170     {
171       if (i==0)
172         tab = &DCTtabfirst[(code>>12)-4];
173       else
174         tab = &DCTtabnext[(code>>12)-4];
175     }
176     else if (code>=1024)
177       tab = &DCTtab0[(code>>8)-4];
178     else if (code>=512)
179       tab = &DCTtab1[(code>>6)-8];
180     else if (code>=256)
181       tab = &DCTtab2[(code>>4)-16];
182     else if (code>=128)
183       tab = &DCTtab3[(code>>3)-16];
184     else if (code>=64)
185       tab = &DCTtab4[(code>>2)-16];
186     else if (code>=32)
187       tab = &DCTtab5[(code>>1)-16];
188     else if (code>=16)
189       tab = &DCTtab6[code-16];
190     else
191     {
192       if (!Quiet_Flag)
193         printf("invalid Huffman code in Decode_MPEG1_Non_Intra_Block()\n");
194       Fault_Flag = 1;
195       return;
196     }
197
198     Flush_Buffer(tab->len);
199
200     if (tab->run==64) /* end_of_block */
201       return;
202
203     if (tab->run==65) /* escape */
204     {
205       i+= Get_Bits(6);
206
207       val = Get_Bits(8);
208       if (val==0)
209         val = Get_Bits(8);
210       else if (val==128)
211         val = Get_Bits(8) - 256;
212       else if (val>128)
213         val -= 256;
214
215       if((sign = (val<0)))
216         val = -val;
217     }
218     else
219     {
220       i+= tab->run;
221       val = tab->level;
222       sign = Get_Bits(1);
223     }
224
225     if (i>=64)
226     {
227       if (!Quiet_Flag)
228         fprintf(stderr,"DCT coeff index (i) out of bounds (inter)\n");
229       Fault_Flag = 1;
230       return;
231     }
232
233     j = scan[ZIG_ZAG][i];
234     val = (((val<<1)+1)*ld->quantizer_scale*ld->non_intra_quantizer_matrix[j]) >> 4;
235
236     /* mismatch control ('oddification') */
237     if (val!=0) /* should always be true, but it's not guaranteed */
238       val = (val-1) | 1; /* equivalent to: if ((val&1)==0) val = val - 1; */
239
240     /* saturation */
241     if (!sign)
242       bp[j] = (val>2047) ?  2047 :  val; /* positive */
243     else
244       bp[j] = (val>2048) ? -2048 : -val; /* negative */
245   }
246 }
247
248
249 /* decode one intra coded MPEG-2 block */
250
251 void Decode_MPEG2_Intra_Block(comp,dc_dct_pred)
252 int comp;
253 int dc_dct_pred[];
254 {
255   int val, i, j, sign, nc, cc, run;
256   unsigned int code;
257   DCTtab *tab;
258   short *bp;
259   int *qmat;
260   struct layer_data *ld1;
261
262   /* with data partitioning, data always goes to base layer */
263   ld1 = (ld->scalable_mode==SC_DP) ? &base : ld;
264   bp = ld1->block[comp];
265
266   if (base.scalable_mode==SC_DP)
267     {
268     if (base.priority_breakpoint<64)
269       ld = &enhan;
270     else
271       ld = &base;
272     }
273
274   cc = (comp<4) ? 0 : (comp&1)+1;
275
276   qmat = (comp<4 || chroma_format==CHROMA420)
277          ? ld1->intra_quantizer_matrix
278          : ld1->chroma_intra_quantizer_matrix;
279
280   /* ISO/IEC 13818-2 section 7.2.1: decode DC coefficients */
281   if (cc==0)
282     val = (dc_dct_pred[0]+= Get_Luma_DC_dct_diff());
283   else if (cc==1)
284     val = (dc_dct_pred[1]+= Get_Chroma_DC_dct_diff());
285   else
286     val = (dc_dct_pred[2]+= Get_Chroma_DC_dct_diff());
287
288   if (Fault_Flag) return;
289
290   bp[0] = val << (3-intra_dc_precision);
291
292   nc=0;
293
294 #ifdef TRACE
295   if (Trace_Flag)
296     printf("DCT(%d)i:",comp);
297 #endif /* TRACE */
298
299   /* decode AC coefficients */
300   for (i=1; ; i++)
301   {
302     code = Show_Bits(16);
303     if (code>=16384 && !intra_vlc_format)
304       tab = &DCTtabnext[(code>>12)-4];
305     else if (code>=1024)
306     {
307       if (intra_vlc_format)
308         tab = &DCTtab0a[(code>>8)-4];
309       else
310         tab = &DCTtab0[(code>>8)-4];
311     }
312     else if (code>=512)
313     {
314       if (intra_vlc_format)
315         tab = &DCTtab1a[(code>>6)-8];
316       else
317         tab = &DCTtab1[(code>>6)-8];
318     }
319     else if (code>=256)
320       tab = &DCTtab2[(code>>4)-16];
321     else if (code>=128)
322       tab = &DCTtab3[(code>>3)-16];
323     else if (code>=64)
324       tab = &DCTtab4[(code>>2)-16];
325     else if (code>=32)
326       tab = &DCTtab5[(code>>1)-16];
327     else if (code>=16)
328       tab = &DCTtab6[code-16];
329     else
330     {
331       if (!Quiet_Flag)
332         printf("invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
333       Fault_Flag = 1;
334       return;
335     }
336
337     Flush_Buffer(tab->len);
338
339 #ifdef TRACE
340     if (Trace_Flag)
341     {
342       printf(" (");
343       Print_Bits(code,16,tab->len);
344     }
345 #endif /* TRACE */
346
347     if (tab->run==64) /* end_of_block */
348     {
349 #ifdef TRACE
350       if (Trace_Flag)
351         printf("): EOB\n");
352 #endif /* TRACE */
353       return;
354     }
355
356     if (tab->run==65) /* escape */
357     {
358 #ifdef TRACE
359       if (Trace_Flag)
360       {
361         putchar(' ');
362         Print_Bits(Show_Bits(6),6,6);
363       }
364 #endif /* TRACE */
365
366       i+= run = Get_Bits(6);
367
368 #ifdef TRACE
369       if (Trace_Flag)
370       {
371         putchar(' ');
372         Print_Bits(Show_Bits(12),12,12);
373       }
374 #endif /* TRACE */
375
376       val = Get_Bits(12);
377       if ((val&2047)==0)
378       {
379         if (!Quiet_Flag)
380           printf("invalid escape in Decode_MPEG2_Intra_Block()\n");
381         Fault_Flag = 1;
382         return;
383       }
384       if((sign = (val>=2048)))
385         val = 4096 - val;
386     }
387     else
388     {
389       i+= run = tab->run;
390       val = tab->level;
391       sign = Get_Bits(1);
392
393 #ifdef TRACE
394       if (Trace_Flag)
395         printf("%d",sign);
396 #endif /* TRACE */
397     }
398
399     if (i>=64)
400     {
401       if (!Quiet_Flag)
402         fprintf(stderr,"DCT coeff index (i) out of bounds (intra2)\n");
403       Fault_Flag = 1;
404       return;
405     }
406
407 #ifdef TRACE
408     if (Trace_Flag)
409       printf("): %d/%d",run,sign ? -val : val);
410 #endif /* TRACE */
411
412     j = scan[ld1->alternate_scan][i];
413     val = (val * ld1->quantizer_scale * qmat[j]) >> 4;
414     bp[j] = sign ? -val : val;
415     nc++;
416
417     if (base.scalable_mode==SC_DP && nc==base.priority_breakpoint-63)
418       ld = &enhan;
419   }
420 }
421
422
423 /* decode one non-intra coded MPEG-2 block */
424
425 void Decode_MPEG2_Non_Intra_Block(comp)
426 int comp;
427 {
428   int val, i, j, sign, nc, run;
429   unsigned int code;
430   DCTtab *tab;
431   short *bp;
432   int *qmat;
433   struct layer_data *ld1;
434
435   /* with data partitioning, data always goes to base layer */
436   ld1 = (ld->scalable_mode==SC_DP) ? &base : ld;
437   bp = ld1->block[comp];
438
439   if (base.scalable_mode==SC_DP)
440     {
441     if (base.priority_breakpoint<64)
442       ld = &enhan;
443     else
444       ld = &base;
445     }
446
447   qmat = (comp<4 || chroma_format==CHROMA420)
448          ? ld1->non_intra_quantizer_matrix
449          : ld1->chroma_non_intra_quantizer_matrix;
450
451   nc = 0;
452
453 #ifdef TRACE
454   if (Trace_Flag)
455     printf("DCT(%d)n:",comp);
456 #endif /* TRACE */
457
458   /* decode AC coefficients */
459   for (i=0; ; i++)
460   {
461     code = Show_Bits(16);
462     if (code>=16384)
463     {
464       if (i==0)
465         tab = &DCTtabfirst[(code>>12)-4];
466       else
467         tab = &DCTtabnext[(code>>12)-4];
468     }
469     else if (code>=1024)
470       tab = &DCTtab0[(code>>8)-4];
471     else if (code>=512)
472       tab = &DCTtab1[(code>>6)-8];
473     else if (code>=256)
474       tab = &DCTtab2[(code>>4)-16];
475     else if (code>=128)
476       tab = &DCTtab3[(code>>3)-16];
477     else if (code>=64)
478       tab = &DCTtab4[(code>>2)-16];
479     else if (code>=32)
480       tab = &DCTtab5[(code>>1)-16];
481     else if (code>=16)
482       tab = &DCTtab6[code-16];
483     else
484     {
485       if (!Quiet_Flag)
486         printf("invalid Huffman code in Decode_MPEG2_Non_Intra_Block()\n");
487       Fault_Flag = 1;
488       return;
489     }
490
491     Flush_Buffer(tab->len);
492
493 #ifdef TRACE
494     if (Trace_Flag)
495     {
496       printf(" (");
497       Print_Bits(code,16,tab->len);
498     }
499 #endif /* TRACE */
500
501     if (tab->run==64) /* end_of_block */
502     {
503 #ifdef TRACE
504       if (Trace_Flag)
505         printf("): EOB\n");
506 #endif /* TRACE */
507       return;
508     }
509
510     if (tab->run==65) /* escape */
511     {
512 #ifdef TRACE
513       if (Trace_Flag)
514       {
515         putchar(' ');
516         Print_Bits(Show_Bits(6),6,6);
517       }
518 #endif /* TRACE */
519
520       i+= run = Get_Bits(6);
521
522 #ifdef TRACE
523       if (Trace_Flag)
524       {
525         putchar(' ');
526         Print_Bits(Show_Bits(12),12,12);
527       }
528 #endif /* TRACE */
529
530       val = Get_Bits(12);
531       if ((val&2047)==0)
532       {
533         if (!Quiet_Flag)
534           printf("invalid escape in Decode_MPEG2_Intra_Block()\n");
535         Fault_Flag = 1;
536         return;
537       }
538       if((sign = (val>=2048)))
539         val = 4096 - val;
540     }
541     else
542     {
543       i+= run = tab->run;
544       val = tab->level;
545       sign = Get_Bits(1);
546
547 #ifdef TRACE
548       if (Trace_Flag)
549         printf("%d",sign);
550 #endif /* TRACE */
551     }
552
553     if (i>=64)
554     {
555       if (!Quiet_Flag)
556         fprintf(stderr,"DCT coeff index (i) out of bounds (inter2)\n");
557       Fault_Flag = 1;
558       return;
559     }
560
561 #ifdef TRACE
562     if (Trace_Flag)
563       printf("): %d/%d",run,sign?-val:val);
564 #endif /* TRACE */
565
566     j = scan[ld1->alternate_scan][i];
567     val = (((val<<1)+1) * ld1->quantizer_scale * qmat[j]) >> 5;
568     bp[j] = sign ? -val : val;
569     nc++;
570
571     if (base.scalable_mode==SC_DP && nc==base.priority_breakpoint-63)
572       ld = &enhan;
573   }
574 }