Subversion Repositories shark

Rev

Rev 1624 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1624 giacomo 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
        cprintf("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
        cprintf("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
        cprintf("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
        cprintf("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
    if (base.priority_breakpoint<64)
266
      ld = &enhan;
267
    else
268
      ld = &base;
269
  }
270
 
271
  cc = (comp<4) ? 0 : (comp&1)+1;
272
 
273
  qmat = (comp<4 || chroma_format==CHROMA420)
274
         ? ld1->intra_quantizer_matrix
275
         : ld1->chroma_intra_quantizer_matrix;
276
 
277
  /* ISO/IEC 13818-2 section 7.2.1: decode DC coefficients */
278
  if (cc==0)
279
    val = (dc_dct_pred[0]+= Get_Luma_DC_dct_diff());
280
  else if (cc==1)
281
    val = (dc_dct_pred[1]+= Get_Chroma_DC_dct_diff());
282
  else
283
    val = (dc_dct_pred[2]+= Get_Chroma_DC_dct_diff());
284
 
285
  if (Fault_Flag) return;
286
 
287
  bp[0] = val << (3-intra_dc_precision);
288
 
289
  nc=0;
290
 
291
#ifdef TRACE
292
  if (Trace_Flag)
293
    cprintf("DCT(%d)i:",comp);
294
#endif /* TRACE */
295
 
296
  /* decode AC coefficients */
297
  for (i=1; ; i++)
298
  {
299
    code = Show_Bits(16);
300
    if (code>=16384 && !intra_vlc_format)
301
      tab = &DCTtabnext[(code>>12)-4];
302
    else if (code>=1024)
303
    {
304
      if (intra_vlc_format)
305
        tab = &DCTtab0a[(code>>8)-4];
306
      else
307
        tab = &DCTtab0[(code>>8)-4];
308
    }
309
    else if (code>=512)
310
    {
311
      if (intra_vlc_format)
312
        tab = &DCTtab1a[(code>>6)-8];
313
      else
314
        tab = &DCTtab1[(code>>6)-8];
315
    }
316
    else if (code>=256)
317
      tab = &DCTtab2[(code>>4)-16];
318
    else if (code>=128)
319
      tab = &DCTtab3[(code>>3)-16];
320
    else if (code>=64)
321
      tab = &DCTtab4[(code>>2)-16];
322
    else if (code>=32)
323
      tab = &DCTtab5[(code>>1)-16];
324
    else if (code>=16)
325
      tab = &DCTtab6[code-16];
326
    else
327
    {
328
      if (!Quiet_Flag)
329
        cprintf("invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
330
      Fault_Flag = 1;
331
      return;
332
    }
333
 
334
    Flush_Buffer(tab->len);
335
 
336
#ifdef TRACE
337
    if (Trace_Flag)
338
    {
339
      printf(" (");
340
      Print_Bits(code,16,tab->len);
341
    }
342
#endif /* TRACE */
343
 
344
    if (tab->run==64) /* end_of_block */
345
    {
346
#ifdef TRACE
347
      if (Trace_Flag)
348
        printf("): EOB\n");
349
#endif /* TRACE */
350
      return;
351
    }
352
 
353
    if (tab->run==65) /* escape */
354
    {
355
#ifdef TRACE
356
      if (Trace_Flag)
357
      {
358
        putchar(' ');
359
        Print_Bits(Show_Bits(6),6,6);
360
      }
361
#endif /* TRACE */
362
 
363
      i+= run = Get_Bits(6);
364
 
365
#ifdef TRACE
366
      if (Trace_Flag)
367
      {
368
        putchar(' ');
369
        Print_Bits(Show_Bits(12),12,12);
370
      }
371
#endif /* TRACE */
372
 
373
      val = Get_Bits(12);
374
      if ((val&2047)==0)
375
      {
376
        if (!Quiet_Flag)
377
          cprintf("invalid escape in Decode_MPEG2_Intra_Block()\n");
378
        Fault_Flag = 1;
379
        return;
380
      }
381
      if((sign = (val>=2048)))
382
        val = 4096 - val;
383
    }
384
    else
385
    {
386
      i+= run = tab->run;
387
      val = tab->level;
388
      sign = Get_Bits(1);
389
 
390
#ifdef TRACE
391
      if (Trace_Flag)
392
        cprintf("%d",sign);
393
#endif /* TRACE */
394
    }
395
 
396
    if (i>=64)
397
    {
398
      if (!Quiet_Flag)
399
        cprintf("DCT coeff index (i) out of bounds (intra2)\n");
400
      Fault_Flag = 1;
401
      return;
402
    }
403
 
404
#ifdef TRACE
405
    if (Trace_Flag)
406
      cprintf("): %d/%d",run,sign ? -val : val);
407
#endif /* TRACE */
408
 
409
    j = scan[ld1->alternate_scan][i];
410
    val = (val * ld1->quantizer_scale * qmat[j]) >> 4;
411
    bp[j] = sign ? -val : val;
412
    nc++;
413
 
414
    if (base.scalable_mode==SC_DP && nc==base.priority_breakpoint-63)
415
      ld = &enhan;
416
  }
417
}
418
 
419
 
420
/* decode one non-intra coded MPEG-2 block */
421
 
422
void Decode_MPEG2_Non_Intra_Block(comp)
423
int comp;
424
{
425
  int val, i, j, sign, nc, run;
426
  unsigned int code;
427
  DCTtab *tab;
428
  short *bp;
429
  int *qmat;
430
  struct layer_data *ld1;
431
 
432
  /* with data partitioning, data always goes to base layer */
433
  ld1 = (ld->scalable_mode==SC_DP) ? &base : ld;
434
  bp = ld1->block[comp];
435
 
436
  if (base.scalable_mode==SC_DP) {
437
    if (base.priority_breakpoint<64)
438
      ld = &enhan;
439
    else
440
      ld = &base;
441
  }
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
    cprintf("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
        cprintf("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
      cprintf(" (");
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
        cprintf("): 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
        cprintf(" ");
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
        cprintf(" ");
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
          cprintf("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
        cprintf("%d",sign);
546
#endif /* TRACE */
547
    }
548
 
549
    if (i>=64)
550
    {
551
      if (!Quiet_Flag)
552
        cprintf("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
      cprintf("): %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
}
571