]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/getblk.c
Oops. I forgot this one
[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 #else
411     (void)run;
412 #endif /* TRACE */
413
414     j = scan[ld1->alternate_scan][i];
415     val = (val * ld1->quantizer_scale * qmat[j]) >> 4;
416     bp[j] = sign ? -val : val;
417     nc++;
418
419     if (base.scalable_mode==SC_DP && nc==base.priority_breakpoint-63)
420       ld = &enhan;
421   }
422 }
423
424
425 /* decode one non-intra coded MPEG-2 block */
426
427 void Decode_MPEG2_Non_Intra_Block(comp)
428 int comp;
429 {
430   int val, i, j, sign, nc, run;
431   unsigned int code;
432   DCTtab *tab;
433   short *bp;
434   int *qmat;
435   struct layer_data *ld1;
436
437   /* with data partitioning, data always goes to base layer */
438   ld1 = (ld->scalable_mode==SC_DP) ? &base : ld;
439   bp = ld1->block[comp];
440
441   if (base.scalable_mode==SC_DP)
442     {
443     if (base.priority_breakpoint<64)
444       ld = &enhan;
445     else
446       ld = &base;
447     }
448
449   qmat = (comp<4 || chroma_format==CHROMA420)
450          ? ld1->non_intra_quantizer_matrix
451          : ld1->chroma_non_intra_quantizer_matrix;
452
453   nc = 0;
454
455 #ifdef TRACE
456   if (Trace_Flag)
457     printf("DCT(%d)n:",comp);
458 #endif /* TRACE */
459
460   /* decode AC coefficients */
461   for (i=0; ; i++)
462   {
463     code = Show_Bits(16);
464     if (code>=16384)
465     {
466       if (i==0)
467         tab = &DCTtabfirst[(code>>12)-4];
468       else
469         tab = &DCTtabnext[(code>>12)-4];
470     }
471     else if (code>=1024)
472       tab = &DCTtab0[(code>>8)-4];
473     else if (code>=512)
474       tab = &DCTtab1[(code>>6)-8];
475     else if (code>=256)
476       tab = &DCTtab2[(code>>4)-16];
477     else if (code>=128)
478       tab = &DCTtab3[(code>>3)-16];
479     else if (code>=64)
480       tab = &DCTtab4[(code>>2)-16];
481     else if (code>=32)
482       tab = &DCTtab5[(code>>1)-16];
483     else if (code>=16)
484       tab = &DCTtab6[code-16];
485     else
486     {
487       if (!Quiet_Flag)
488         printf("invalid Huffman code in Decode_MPEG2_Non_Intra_Block()\n");
489       Fault_Flag = 1;
490       return;
491     }
492
493     Flush_Buffer(tab->len);
494
495 #ifdef TRACE
496     if (Trace_Flag)
497     {
498       printf(" (");
499       Print_Bits(code,16,tab->len);
500     }
501 #endif /* TRACE */
502
503     if (tab->run==64) /* end_of_block */
504     {
505 #ifdef TRACE
506       if (Trace_Flag)
507         printf("): EOB\n");
508 #endif /* TRACE */
509       return;
510     }
511
512     if (tab->run==65) /* escape */
513     {
514 #ifdef TRACE
515       if (Trace_Flag)
516       {
517         putchar(' ');
518         Print_Bits(Show_Bits(6),6,6);
519       }
520 #endif /* TRACE */
521
522       i+= run = Get_Bits(6);
523
524 #ifdef TRACE
525       if (Trace_Flag)
526       {
527         putchar(' ');
528         Print_Bits(Show_Bits(12),12,12);
529       }
530 #endif /* TRACE */
531
532       val = Get_Bits(12);
533       if ((val&2047)==0)
534       {
535         if (!Quiet_Flag)
536           printf("invalid escape in Decode_MPEG2_Intra_Block()\n");
537         Fault_Flag = 1;
538         return;
539       }
540       if((sign = (val>=2048)))
541         val = 4096 - val;
542     }
543     else
544     {
545       i+= run = tab->run;
546       val = tab->level;
547       sign = Get_Bits(1);
548
549 #ifdef TRACE
550       if (Trace_Flag)
551         printf("%d",sign);
552 #endif /* TRACE */
553     }
554
555     if (i>=64)
556     {
557       if (!Quiet_Flag)
558         fprintf(stderr,"DCT coeff index (i) out of bounds (inter2)\n");
559       Fault_Flag = 1;
560       return;
561     }
562
563 #ifdef TRACE
564     if (Trace_Flag)
565       printf("): %d/%d",run,sign?-val:val);
566 #else
567     (void)run;
568 #endif /* TRACE */
569
570     j = scan[ld1->alternate_scan][i];
571     val = (((val<<1)+1) * ld1->quantizer_scale * qmat[j]) >> 5;
572     bp[j] = sign ? -val : val;
573     nc++;
574
575     if (base.scalable_mode==SC_DP && nc==base.priority_breakpoint-63)
576       ld = &enhan;
577   }
578 }