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