~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Open Mash Cross Reference
mash/codec/tmndec/getblk.c

Component: ~ [ mash ] ~ [ apps ] ~ [ gsm ] ~ [ lib ] ~ [ otcl ] ~ [ srm ] ~ [ tcl8.3 ] ~ [ tclcl ] ~ [ tk8.3 ] ~ [ tutorials ] ~

  1 /************************************************************************
  2  *
  3  *  getblk.c, DCT block decoding for tmndecode (H.263 decoder)
  4  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
  5  *
  6  *  Contacts:
  7  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
  8  *
  9  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
 10  *  P.O.Box 83                        tel.:   +47 63 84 84 00
 11  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
 12  *
 13  *  Copyright (C) 1997  University of BC, Canada
 14  *  Modified by: Michael Gallant <mikeg@ee.ubc.ca>
 15  *               Guy Cote <guyc@ee.ubc.ca>
 16  *               Berna Erol <bernae@ee.ubc.ca>
 17  *
 18  *  Contacts:
 19  *  Michael Gallant                   <mikeg@ee.ubc.ca>
 20  *
 21  *  UBC Image Processing Laboratory   http://www.ee.ubc.ca/image
 22  *  2356 Main Mall                    tel.: +1 604 822 4051
 23  *  Vancouver BC Canada V6T1Z4        fax.: +1 604 822 5949
 24  *
 25  ************************************************************************/
 26 
 27 /* Disclaimer of Warranty
 28  * 
 29  * These software programs are available to the user without any license fee
 30  * or royalty on an "as is" basis. The University of British Columbia
 31  * disclaims any and all warranties, whether express, implied, or
 32  * statuary, including any implied warranties or merchantability or of
 33  * fitness for a particular purpose.  In no event shall the
 34  * copyright-holder be liable for any incidental, punitive, or
 35  * consequential damages of any kind whatsoever arising from the use of
 36  * these programs.
 37  * 
 38  * This disclaimer of warranty extends to the user of these programs and
 39  * user's customers, employees, agents, transferees, successors, and
 40  * assigns.
 41  * 
 42  * The University of British Columbia does not represent or warrant that the
 43  * programs furnished hereunder are free of infringement of any
 44  * third-party patents.
 45  * 
 46  * Commercial implementations of H.263, including shareware, are subject to
 47  * royalty fees to patent holders.  Many of these patents are general
 48  * enough such that they are unavoidable regardless of implementation
 49  * design.
 50  * 
 51  */
 52 
 53 
 54 /* based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
 55  * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de>
 56  * 
 57  */
 58 
 59 
 60 #include <stdio.h>
 61 #include <stdlib.h>
 62 
 63 #include "config.h"
 64 #include "tmndec.h"
 65 #include "global.h"
 66 
 67 #define INDICES
 68 #include "indices.h"
 69 
 70 #define SACTABLES
 71 #include "sactbls.h"
 72 
 73 
 74 typedef struct
 75 {
 76   char run, level, len;
 77 } DCTtab;
 78 typedef struct
 79 {
 80   int val, len;
 81 } VLCtabI;
 82 typedef struct
 83 {
 84   int val, run, sign;
 85 } RunCoef;
 86 
 87 typedef struct
 88 {
 89   int code, level, last, run, sign, extended_level;
 90 } CodeCoeff;
 91 
 92 
 93 
 94 /* local prototypes */
 95 RunCoef vlc_word_decode _ANSI_ARGS_ ((int symbol_word, int *last));
 96 RunCoef Decode_Escape_Char _ANSI_ARGS_ ((int intra, int *last));
 97 int DecodeTCoef _ANSI_ARGS_ ((int position, int intra));
 98 
 99 
100 
101 extern VLCtabI DCT3Dtab0[], DCT3Dtab1[], DCT3Dtab2[];
102 
103 /* Tables used in advanced intra coding mode */
104 extern VLCtabI INTRA_DCT3Dtab0[], INTRA_DCT3Dtab1[], INTRA_DCT3Dtab2[];
105 
106 extern MQ_chroma_QP_table[];
107 
108 void getblock (int comp, int mode, int INTRA_AC_DC, int Mode)
109 {
110   int val, i, j, sign;
111   unsigned int code;
112   VLCtabI *tab;
113   short *bp;
114   int run, last, level, QP;
115   short *qval;
116 
117 
118   /* store_code used in alternative inter VLC mode */
119   int use_intra = 0;
120   CodeCoeff store_code[64];
121   int coeff_ind;
122 
123   bp = ld->block[comp];
124 
125   /* decode AC coefficients (or all coefficients in advanced intra coding
126    * mode) */
127   if (trace)
128     fprintf (trace_file, "block: %i\n", comp);
129 
130   if (alternative_inter_VLC_mode && !(Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
131   {
132     coeff_ind = 0;
133     use_intra = 0;
134     for (i = (mode == 0);; i++)
135     {
136       code = showbits (12);
137       store_code[coeff_ind].code = code;
138       if (code >= 512)
139         tab = &DCT3Dtab0[(code >> 5) - 16];
140       else if (code >= 128)
141         tab = &DCT3Dtab1[(code >> 2) - 32];
142       else if (code >= 8)
143         tab = &DCT3Dtab2[(code >> 0) - 8];
144       else
145       {
146         if (!quiet)
147           fprintf (stderr, "invalid Huffman code in getblock()\n");
148         fault = 1;
149         return;
150       }
151 
152       run = (tab->val >> 4) & 255;
153       last = (tab->val >> 12) & 1;
154       flushbits (tab->len);
155       if (tab->val == ESCAPE)
156       {
157         last = getbits1 ();
158         store_code[coeff_ind].last = last;
159 
160         i += run = getbits (6);
161         store_code[coeff_ind].run = run;
162 
163         level = getbits (8);
164         store_code[coeff_ind].level = level;
165 
166 
167         if (level == 128 && modified_quantization_mode)
168         {
169           level = getbits (11);
170           store_code[coeff_ind].extended_level = level;
171         }
172       } else
173       {
174         store_code[coeff_ind].sign = getbits (1);
175         i += run;
176       }
177       coeff_ind += 1;
178 
179       if (i >= 64)
180         use_intra = 1;
181       if (last)
182         break;
183     }
184     if (trace)
185       fprintf (trace_file, "inter VLC Table: %s\n", (use_intra ? "Intra" : "Inter"));
186   }
187   coeff_ind = 0;
188 
189   for (i = (mode == 0);; i++)
190   {
191     if ((alternative_inter_VLC_mode) && !(Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
192     {
193       if (use_intra)
194       {
195         if ((store_code[coeff_ind].code) >= 512)
196           tab = &INTRA_DCT3Dtab0[((store_code[coeff_ind].code) >> 5) - 16];
197         else if ((store_code[coeff_ind].code) >= 128)
198           tab = &INTRA_DCT3Dtab1[((store_code[coeff_ind].code) >> 2) - 32];
199         else if ((store_code[coeff_ind].code) >= 8)
200           tab = &INTRA_DCT3Dtab2[((store_code[coeff_ind].code) >> 0) - 8];
201         else
202         {
203           if (!quiet)
204             fprintf (stderr, "invalid Huffman code in getblock()\n");
205           fault = 1;
206           return;
207         }
208         run = (tab->val >> 6) & 63;
209         level = tab->val & 63;
210         last = (tab->val >> 12) & 1;
211       } else
212       {
213         if (store_code[coeff_ind].code >= 512)
214           tab = &DCT3Dtab0[(store_code[coeff_ind].code >> 5) - 16];
215         else if (store_code[coeff_ind].code >= 128)
216           tab = &DCT3Dtab1[(store_code[coeff_ind].code >> 2) - 32];
217         else if (store_code[coeff_ind].code >= 8)
218           tab = &DCT3Dtab2[(store_code[coeff_ind].code >> 0) - 8];
219         else
220         {
221           if (!quiet)
222             fprintf (stderr, "invalid Huffman code in getblock()\n");
223           fault = 1;
224           return;
225         }
226 
227         run = (tab->val >> 4) & 255;
228         level = tab->val & 15;
229         last = (tab->val >> 12) & 1;
230       }
231 
232       if (trace)
233       {
234         fprintf (trace_file, " (");
235         printbits (store_code[coeff_ind].code, 12, tab->len);
236       }
237       if (tab->val == ESCAPE)
238       {                         /* escape */
239         if (trace)
240         {
241           fprintf (trace_file, " ");
242           printbits (store_code[coeff_ind].last, 1, 1);
243         }
244         last = store_code[coeff_ind].last;
245         if (trace)
246         {
247           fprintf (trace_file, " ");
248           printbits (store_code[coeff_ind].run, 6, 6);
249         }
250         i += run = store_code[coeff_ind].run;
251         if (trace)
252         {
253           fprintf (trace_file, " ");
254           printbits (store_code[coeff_ind].level, 8, 8);
255         }
256         level = store_code[coeff_ind].level;
257 
258         if ((sign = (level >= 128)))
259           val = 256 - level;
260         else
261           val = level;
262 
263         if (level == 128 && modified_quantization_mode)
264         {
265           level = store_code[coeff_ind].extended_level;
266           level = (level >> 6 & 0x001F) | level << 5;
267           /* correctly set the sign */
268           level = (level << (sizeof (int) * 8 - 11)) >> (sizeof (int) * 8 - 11);
269           if (level >= 0)
270             sign = 0;
271           else
272             sign = 1;
273           val = abs (level);
274         }
275       } else
276       {
277         sign = store_code[coeff_ind].sign;
278         i += run;
279         val = level;
280 
281         if (trace)
282           fprintf (trace_file, "%d", sign);
283       }
284       coeff_ind += 1;
285 
286     } else
287     {
288       code = showbits (12);
289       if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
290       {
291         if (code >= 512)
292           tab = &INTRA_DCT3Dtab0[(code >> 5) - 16];
293         else if (code >= 128)
294           tab = &INTRA_DCT3Dtab1[(code >> 2) - 32];
295         else if (code >= 8)
296           tab = &INTRA_DCT3Dtab2[(code >> 0) - 8];
297         else
298         {
299           if (!quiet)
300             fprintf (stderr, "invalid Huffman code in getblock()\n");
301           fault = 1;
302           return;
303         }
304         run = (tab->val >> 6) & 63;
305         level = tab->val & 63;
306         last = (tab->val >> 12) & 1;
307       } else
308       {
309 
310         if (code >= 512)
311           tab = &DCT3Dtab0[(code >> 5) - 16];
312         else if (code >= 128)
313           tab = &DCT3Dtab1[(code >> 2) - 32];
314         else if (code >= 8)
315           tab = &DCT3Dtab2[(code >> 0) - 8];
316         else
317         {
318           if (!quiet)
319             fprintf (stderr, "invalid Huffman code in getblock()\n");
320           fault = 1;
321           return;
322         }
323 
324         run = (tab->val >> 4) & 255;
325         level = tab->val & 15;
326         last = (tab->val >> 12) & 1;
327       }
328       flushbits (tab->len);
329       if (trace)
330       {
331         fprintf (trace_file, " (");
332         printbits (code, 12, tab->len);
333       }
334       if (tab->val == ESCAPE)
335       {                         /* escape */
336         if (trace)
337         {
338           fprintf (trace_file, " ");
339           printbits (showbits (1), 1, 1);
340         }
341         last = getbits1 ();
342         if (trace)
343         {
344           fprintf (trace_file, " ");
345           printbits (showbits (6), 6, 6);
346         }
347         i += run = getbits (6);
348         if (trace)
349         {
350           fprintf (trace_file, " ");
351           printbits (showbits (8), 8, 8);
352         }
353         level = getbits (8);
354 
355         if ((sign = (level >= 128)))
356           val = 256 - level;
357         else
358           val = level;
359 
360         if (level == 128 && modified_quantization_mode)
361         {
362           /* extended escape code was received */
363           level = getbits (11);
364           level = (level >> 6 & 0x001F) | level << 5;
365           /* correctly set the sign */
366           level = (level << (sizeof (int) * 8 - 11)) >> (sizeof (int) * 8 - 11);
367           if (level >= 0)
368             sign = 0;
369           else
370             sign = 1;
371           val = abs (level);
372         }
373       } else
374       {
375         i += run;
376         val = level;
377         sign = getbits (1);
378         if (trace)
379           fprintf (trace_file, "%d", sign);
380       }
381     }
382 
383     if (i >= 64)
384     {
385       if (!quiet)
386         fprintf (stderr, "DCT coeff index (i) out of bounds\n");
387       fault = 1;
388       return;
389     }
390     if (trace)
391       fprintf (trace_file, "): %d/%d\n", run, sign ? -val : val);
392 
393     /* Descan in the proper order in advanced intra coding mode */
394 
395     if (advanced_intra_coding && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
396     {
397       switch (INTRA_AC_DC)
398       {
399         case INTRA_MODE_DC:
400           j = zig_zag_scan[i];
401           break;
402         case INTRA_MODE_VERT_AC:
403           j = alternate_horizontal_scan[i];
404           break;
405         case INTRA_MODE_HORI_AC:
406           j = alternate_vertical_scan[i];
407           break;
408         default:
409           printf ("Invalid Intra_Mode in Advanced Intra Coding\n");
410           exit (-1);
411           break;
412       }
413     } else
414     {
415       j = zig_zag_scan[i];
416     }
417     qval = &bp[j];
418     if (comp >= 6)
419       QP = mmax (1, mmin (31, (bquant_tab[bquant] * quant) >> 2));
420     else
421       QP = quant;
422 
423     if (modified_quantization_mode && (comp == 4 || comp == 5 || comp == 10 || comp == 11))
424     {
425       /* when modified quantization mode is on, use modified quantizer for
426        * chorominance coefficients */
427 
428       QP = MQ_chroma_QP_table[QP];
429     }
430     if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
431     {
432       /* Advanced intra coding dequantization */
433       *qval = (sign ? -(QP * (2 * val)) : QP * (2 * val));
434     } else
435     {
436       /* TMN3 dequantization */
437       if ((QP % 2) == 1)
438         *qval = (sign ? -(QP * (2 * val + 1)) : QP * (2 * val + 1));
439       else
440         *qval = (sign ? -(QP * (2 * val + 1) - 1) : QP * (2 * val + 1) - 1);
441     }
442     if (last)
443     {                           /* That's it */
444       if (trace)
445         fprintf (trace_file, "last\n------------------------------------------------------------------\n");
446       return;
447     }
448   }
449 }
450 
451 
452 
453 /*********************************************************************
454  *
455  *        Name:        get_sac_block
456  *
457  *      Description:    Decodes blocks of Arithmetic Encoded DCT Coeffs.
458  *        and performs Run Length Decoding and Coefficient
459  *        Dequantisation.
460  *
461  *      Input:        Picture block type and number.
462  *
463  *      Returns:        Nothing.
464  *
465  *      Side Effects:
466  *
467  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
468  *
469  *********************************************************************/
470 
471 
472 void get_sac_block (int comp, int ptype, int INTRA_AC_DC, int Mode)
473 {
474   int position = 0;
475   int TCOEF_index, symbol_word;
476   int last = 0, QP, i, j;
477   short *qval;
478   short *bp;
479   RunCoef DCTcoef;
480   int intra;
481 
482 
483   bp = ld->block[comp];
484 
485   i = (ptype == 0);
486   intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
487 
488   while (!last)
489   {                             /* while there are DCT coefficients
490                                  * remaining */
491     position++;                 /* coefficient counter relates to Coeff.
492                                  * model */
493     TCOEF_index = DecodeTCoef (position, intra);
494 
495     if (TCOEF_index == ESCAPE_INDEX)
496     {                           /* ESCAPE code encountered */
497       DCTcoef = Decode_Escape_Char (intra, &last);
498       if (trace)
499         fprintf (trace_file, "ESC: ");
500     } else
501     {
502       symbol_word = tcoeftab[TCOEF_index];
503 
504       DCTcoef = vlc_word_decode (symbol_word, &last);
505     }
506 
507     if (trace)
508     {
509       fprintf (trace_file, "val: %d, run: %d, sign: %d, last: %d\n",
510                DCTcoef.val, DCTcoef.run, DCTcoef.sign, last);
511     }
512     i += DCTcoef.run;
513     if (advanced_intra_coding && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
514     {
515       switch (INTRA_AC_DC)
516       {
517         case INTRA_MODE_DC:
518           j = zig_zag_scan[i];
519           break;
520         case INTRA_MODE_VERT_AC:
521           j = alternate_horizontal_scan[i];
522           break;
523         case INTRA_MODE_HORI_AC:
524           j = alternate_vertical_scan[i];
525           break;
526         default:
527           printf ("Invalid Intra_Mode in Advanced Intra Coding");
528           exit (-1);
529           break;
530       }
531     } else
532     {
533       j = zig_zag_scan[i];
534     }
535 
536     qval = &bp[j];
537 
538     i++;
539 
540     if (comp >= 6)
541       QP = mmax (1, mmin (31, (bquant_tab[bquant] * quant) >> 2));
542     else
543       QP = quant;
544     if ((advanced_intra_coding) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
545     {
546       /* Advanced intra coding dequantization */
547       *qval = (DCTcoef.sign ? -(QP * (2 * DCTcoef.val)) : QP * (2 * DCTcoef.val));
548     } else
549     {
550 
551       if ((QP % 2) == 1)
552         *qval = ((DCTcoef.sign) ? -(QP * (2 * (DCTcoef.val) + 1)) :
553                  QP * (2 * (DCTcoef.val) + 1));
554       else
555         *qval = ((DCTcoef.sign) ? -(QP * (2 * (DCTcoef.val) + 1) - 1) :
556                  QP * (2 * (DCTcoef.val) + 1) - 1);
557     }
558   }
559   return;
560 }
561 
562 /*********************************************************************
563  *
564  *        Name:        vlc_word_decode
565  *
566  *      Description:    Fills Decoder FIFO after a fixed word length
567  *        string has been detected.
568  *
569  *      Input:        Symbol to be decoded, last data flag.
570  *
571  *      Returns:        Decoded Symbol via the structure DCTcoeff.
572  *
573  *      Side Effects:   Updates last flag.
574  *
575  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
576  *
577  *********************************************************************/
578 
579 RunCoef vlc_word_decode (int symbol_word, int *last)
580 {
581   int sign_index;
582   RunCoef DCTcoef;
583 
584   *last = (symbol_word >> 12) & 01;
585 
586   DCTcoef.run = (symbol_word >> 4) & 255;
587 
588   DCTcoef.val = (symbol_word) & 15;
589 
590   sign_index = decode_a_symbol (cumf_SIGN);
591 
592   DCTcoef.sign = signtab[sign_index];
593 
594   return (DCTcoef);
595 }
596 
597 /*********************************************************************
598  *
599  *        Name:        Decode_Escape_Char
600  *
601  *      Description:    Decodes all components for a Symbol when an
602  *        ESCAPE character has been detected.
603  *
604  *      Input:        Picture Type and last data flag.
605  *
606  *      Returns:        Decoded Symbol via the structure DCTcoeff.
607  *
608  *      Side Effects:   Modifies last data flag.
609  *
610  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
611  *
612  *********************************************************************/
613 
614 RunCoef Decode_Escape_Char (int intra, int *last)
615 {
616   int last_index, run, run_index, level, level_index;
617   RunCoef DCTcoef;
618 
619   if (intra)
620   {
621     last_index = decode_a_symbol (cumf_LAST_intra);
622     *last = last_intratab[last_index];
623   } else
624   {
625     last_index = decode_a_symbol (cumf_LAST);
626     *last = lasttab[last_index];
627   }
628 
629   if (intra)
630     run_index = decode_a_symbol (cumf_RUN_intra);
631   else
632     run_index = decode_a_symbol (cumf_RUN);
633 
634   run = runtab[run_index];
635 
636   /* $if (mrun) run|=64;$ */
637 
638   DCTcoef.run = run;
639 
640   if (intra)
641     level_index = decode_a_symbol (cumf_LEVEL_intra);
642   else
643     level_index = decode_a_symbol (cumf_LEVEL);
644 
645   if (trace)
646     fprintf (trace_file, "level_idx: %d ", level_index);
647 
648   level = leveltab[level_index];
649 
650   if (level > 128)
651     level -= 256;
652 
653   if (level < 0)
654   {
655     DCTcoef.sign = 1;
656     DCTcoef.val = abs (level);
657   } else
658   {
659     DCTcoef.sign = 0;
660     DCTcoef.val = level;
661   }
662 
663   return (DCTcoef);
664 
665 }
666 /*********************************************************************
667  *
668  *        Name:        DecodeTCoef
669  *
670  *      Description:    Decodes a.c DCT Coefficients using the
671  *        relevant arithmetic decoding model.
672  *
673  *      Input:        DCT Coeff count and Picture Type.
674  *
675  *      Returns:        Index to LUT
676  *
677  *      Side Effects:   None
678  *
679  *      Author:        Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
680  *
681  *********************************************************************/
682 
683 int DecodeTCoef (int position, int intra)
684 {
685   int index;
686 
687   switch (position)
688   {
689     case 1:
690       {
691         if (intra)
692           index = decode_a_symbol (cumf_TCOEF1_intra);
693         else
694           index = decode_a_symbol (cumf_TCOEF1);
695         break;
696       }
697     case 2:
698       {
699         if (intra)
700           index = decode_a_symbol (cumf_TCOEF2_intra);
701         else
702           index = decode_a_symbol (cumf_TCOEF2);
703         break;
704       }
705     case 3:
706       {
707         if (intra)
708           index = decode_a_symbol (cumf_TCOEF3_intra);
709         else
710           index = decode_a_symbol (cumf_TCOEF3);
711         break;
712       }
713     default:
714       {
715         if (intra)
716           index = decode_a_symbol (cumf_TCOEFr_intra);
717         else
718           index = decode_a_symbol (cumf_TCOEFr);
719         break;
720       }
721   }
722 
723   return (index);
724 }
725 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.