OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [getblk.c] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.