]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/getblk.c
New method SerieHelper::AddSeriesDetail() to allow user to specifiy
[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 "config.h"
31 #include "global.h"
32
33
34 /* defined in getvlc.h */
35 typedef struct {
36   char run, level, len;
37 } DCTtab;
38
39 extern DCTtab DCTtabfirst[],DCTtabnext[],DCTtab0[],DCTtab1[];
40 extern DCTtab DCTtab2[],DCTtab3[],DCTtab4[],DCTtab5[],DCTtab6[];
41 extern DCTtab DCTtab0a[],DCTtab1a[];
42
43
44 /* decode one intra coded MPEG-1 block */
45
46 void Decode_MPEG1_Intra_Block(comp,dc_dct_pred)
47 int comp;
48 int dc_dct_pred[];
49 {
50   int val, i, j, sign;
51   unsigned int code;
52   DCTtab *tab;
53   short *bp;
54
55   bp = ld->block[comp];
56
57   /* ISO/IEC 11172-2 section 2.4.3.7: Block layer. */
58   /* decode DC coefficients */
59   if (comp<4)
60     bp[0] = (dc_dct_pred[0]+=Get_Luma_DC_dct_diff()) << 3;
61   else if (comp==4)
62     bp[0] = (dc_dct_pred[1]+=Get_Chroma_DC_dct_diff()) << 3;
63   else
64     bp[0] = (dc_dct_pred[2]+=Get_Chroma_DC_dct_diff()) << 3;
65
66   if (Fault_Flag) return;
67
68   /* D-pictures do not contain AC coefficients */
69   if(picture_coding_type == D_TYPE)
70     return;
71
72   /* decode AC coefficients */
73   for (i=1; ; i++)
74   {
75     code = Show_Bits(16);
76     if (code>=16384)
77       tab = &DCTtabnext[(code>>12)-4];
78     else if (code>=1024)
79       tab = &DCTtab0[(code>>8)-4];
80     else if (code>=512)
81       tab = &DCTtab1[(code>>6)-8];
82     else if (code>=256)
83       tab = &DCTtab2[(code>>4)-16];
84     else if (code>=128)
85       tab = &DCTtab3[(code>>3)-16];
86     else if (code>=64)
87       tab = &DCTtab4[(code>>2)-16];
88     else if (code>=32)
89       tab = &DCTtab5[(code>>1)-16];
90     else if (code>=16)
91       tab = &DCTtab6[code-16];
92     else
93     {
94       if (!Quiet_Flag)
95         printf("invalid Huffman code in Decode_MPEG1_Intra_Block()\n");
96       Fault_Flag = 1;
97       return;
98     }
99
100     Flush_Buffer(tab->len);
101
102     if (tab->run==64) /* end_of_block */
103       return;
104
105     if (tab->run==65) /* escape */
106     {
107       i+= Get_Bits(6);
108
109       val = Get_Bits(8);
110       if (val==0)
111         val = Get_Bits(8);
112       else if (val==128)
113         val = Get_Bits(8) - 256;
114       else if (val>128)
115         val -= 256;
116
117       if((sign = (val<0)))
118         val = -val;
119     }
120     else
121     {
122       i+= tab->run;
123       val = tab->level;
124       sign = Get_Bits(1);
125     }
126
127     if (i>=64)
128     {
129       if (!Quiet_Flag)
130         fprintf(stderr,"DCT coeff index (i) out of bounds (intra)\n");
131       Fault_Flag = 1;
132       return;
133     }
134
135     j = scan[ZIG_ZAG][i];
136     val = (val*ld->quantizer_scale*ld->intra_quantizer_matrix[j]) >> 3;
137
138     /* mismatch control ('oddification') */
139     if (val!=0) /* should always be true, but it's not guaranteed */
140       val = (val-1) | 1; /* equivalent to: if ((val&1)==0) val = val - 1; */
141
142     /* saturation */
143     if (!sign)
144       bp[j] = (val>2047) ?  2047 :  val; /* positive */
145     else
146       bp[j] = (val>2048) ? -2048 : -val; /* negative */
147   }
148 }
149
150
151 /* decode one non-intra coded MPEG-1 block */
152
153 void Decode_MPEG1_Non_Intra_Block(comp)
154 int comp;
155 {
156   int val, i, j, sign;
157   unsigned int code;
158   DCTtab *tab;
159   short *bp;
160
161   bp = ld->block[comp];
162
163   /* decode AC coefficients */
164   for (i=0; ; i++)
165   {
166     code = Show_Bits(16);
167     if (code>=16384)
168     {
169       if (i==0)
170         tab = &DCTtabfirst[(code>>12)-4];
171       else
172         tab = &DCTtabnext[(code>>12)-4];
173     }
174     else if (code>=1024)
175       tab = &DCTtab0[(code>>8)-4];
176     else if (code>=512)
177       tab = &DCTtab1[(code>>6)-8];
178     else if (code>=256)
179       tab = &DCTtab2[(code>>4)-16];
180     else if (code>=128)
181       tab = &DCTtab3[(code>>3)-16];
182     else if (code>=64)
183       tab = &DCTtab4[(code>>2)-16];
184     else if (code>=32)
185       tab = &DCTtab5[(code>>1)-16];
186     else if (code>=16)
187       tab = &DCTtab6[code-16];
188     else
189     {
190       if (!Quiet_Flag)
191         printf("invalid Huffman code in Decode_MPEG1_Non_Intra_Block()\n");
192       Fault_Flag = 1;
193       return;
194     }
195
196     Flush_Buffer(tab->len);
197
198     if (tab->run==64) /* end_of_block */
199       return;
200
201     if (tab->run==65) /* escape */
202     {
203       i+= Get_Bits(6);
204
205       val = Get_Bits(8);
206       if (val==0)
207         val = Get_Bits(8);
208       else if (val==128)
209         val = Get_Bits(8) - 256;
210       else if (val>128)
211         val -= 256;
212
213       if((sign = (val<0)))
214         val = -val;
215     }
216     else
217     {
218       i+= tab->run;
219       val = tab->level;
220       sign = Get_Bits(1);
221     }
222
223     if (i>=64)
224     {
225       if (!Quiet_Flag)
226         fprintf(stderr,"DCT coeff index (i) out of bounds (inter)\n");
227       Fault_Flag = 1;
228       return;
229     }
230
231     j = scan[ZIG_ZAG][i];
232     val = (((val<<1)+1)*ld->quantizer_scale*ld->non_intra_quantizer_matrix[j]) >> 4;
233
234     /* mismatch control ('oddification') */
235     if (val!=0) /* should always be true, but it's not guaranteed */
236       val = (val-1) | 1; /* equivalent to: if ((val&1)==0) val = val - 1; */
237
238     /* saturation */
239     if (!sign)
240       bp[j] = (val>2047) ?  2047 :  val; /* positive */
241     else
242       bp[j] = (val>2048) ? -2048 : -val; /* negative */
243   }
244 }
245
246
247 /* decode one intra coded MPEG-2 block */
248
249 void Decode_MPEG2_Intra_Block(comp,dc_dct_pred)
250 int comp;
251 int dc_dct_pred[];
252 {
253   int val, i, j, sign, nc, cc, run;
254   unsigned int code;
255   DCTtab *tab;
256   short *bp;
257   int *qmat;
258   struct layer_data *ld1;
259
260   /* with data partitioning, data always goes to base layer */
261   ld1 = (ld->scalable_mode==SC_DP) ? &base : ld;
262   bp = ld1->block[comp];
263
264   if (base.scalable_mode==SC_DP)
265     {
266     if (base.priority_breakpoint<64)
267       ld = &enhan;
268     else
269       ld = &base;
270     }
271
272   cc = (comp<4) ? 0 : (comp&1)+1;
273
274   qmat = (comp<4 || chroma_format==CHROMA420)
275          ? ld1->intra_quantizer_matrix
276          : ld1->chroma_intra_quantizer_matrix;
277
278   /* ISO/IEC 13818-2 section 7.2.1: decode DC coefficients */
279   if (cc==0)
280     val = (dc_dct_pred[0]+= Get_Luma_DC_dct_diff());
281   else if (cc==1)
282     val = (dc_dct_pred[1]+= Get_Chroma_DC_dct_diff());
283   else
284     val = (dc_dct_pred[2]+= Get_Chroma_DC_dct_diff());
285
286   if (Fault_Flag) return;
287
288   bp[0] = val << (3-intra_dc_precision);
289
290   nc=0;
291
292 #ifdef TRACE
293   if (Trace_Flag)
294     printf("DCT(%d)i:",comp);
295 #endif /* TRACE */
296
297   /* decode AC coefficients */
298   for (i=1; ; i++)
299   {
300     code = Show_Bits(16);
301     if (code>=16384 && !intra_vlc_format)
302       tab = &DCTtabnext[(code>>12)-4];
303     else if (code>=1024)
304     {
305       if (intra_vlc_format)
306         tab = &DCTtab0a[(code>>8)-4];
307       else
308         tab = &DCTtab0[(code>>8)-4];
309     }
310     else if (code>=512)
311     {
312       if (intra_vlc_format)
313         tab = &DCTtab1a[(code>>6)-8];
314       else
315         tab = &DCTtab1[(code>>6)-8];
316     }
317     else if (code>=256)
318       tab = &DCTtab2[(code>>4)-16];
319     else if (code>=128)
320       tab = &DCTtab3[(code>>3)-16];
321     else if (code>=64)
322       tab = &DCTtab4[(code>>2)-16];
323     else if (code>=32)
324       tab = &DCTtab5[(code>>1)-16];
325     else if (code>=16)
326       tab = &DCTtab6[code-16];
327     else
328     {
329       if (!Quiet_Flag)
330         printf("invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
331       Fault_Flag = 1;
332       return;
333     }
334
335     Flush_Buffer(tab->len);
336
337 #ifdef TRACE
338     if (Trace_Flag)
339     {
340       printf(" (");
341       Print_Bits(code,16,tab->len);
342     }
343 #endif /* TRACE */
344
345     if (tab->run==64) /* end_of_block */
346     {
347 #ifdef TRACE
348       if (Trace_Flag)
349         printf("): EOB\n");
350 #endif /* TRACE */
351       return;
352     }
353
354     if (tab->run==65) /* escape */
355     {
356 #ifdef TRACE
357       if (Trace_Flag)
358       {
359         putchar(' ');
360         Print_Bits(Show_Bits(6),6,6);
361       }
362 #endif /* TRACE */
363
364       i+= run = Get_Bits(6);
365
366 #ifdef TRACE
367       if (Trace_Flag)
368       {
369         putchar(' ');
370         Print_Bits(Show_Bits(12),12,12);
371       }
372 #endif /* TRACE */
373
374       val = Get_Bits(12);
375       if ((val&2047)==0)
376       {
377         if (!Quiet_Flag)
378           printf("invalid escape in Decode_MPEG2_Intra_Block()\n");
379         Fault_Flag = 1;
380         return;
381       }
382       if((sign = (val>=2048)))
383         val = 4096 - val;
384     }
385     else
386     {
387       i+= run = tab->run;
388       val = tab->level;
389       sign = Get_Bits(1);
390
391 #ifdef TRACE
392       if (Trace_Flag)
393         printf("%d",sign);
394 #endif /* TRACE */
395     }
396
397     if (i>=64)
398     {
399       if (!Quiet_Flag)
400         fprintf(stderr,"DCT coeff index (i) out of bounds (intra2)\n");
401       Fault_Flag = 1;
402       return;
403     }
404
405 #ifdef TRACE
406     if (Trace_Flag)
407       printf("): %d/%d",run,sign ? -val : val);
408 #else
409     (void)run;
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 #else
565     (void)run;
566 #endif /* TRACE */
567
568     j = scan[ld1->alternate_scan][i];
569     val = (((val<<1)+1) * ld1->quantizer_scale * qmat[j]) >> 5;
570     bp[j] = sign ? -val : val;
571     nc++;
572
573     if (base.scalable_mode==SC_DP && nc==base.priority_breakpoint-63)
574       ld = &enhan;
575   }
576 }