Main Page | File List

SAC.C

00001 
00002 /*********************************************************************
00003  *      SAC Decoder Module
00004  *      Algorithm as Specified in H26P Annex -E
00005  *              (c) 1995 BT Labs
00006  *
00007  *      Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00008  *
00009  *********************************************************************/
00010 
00011 #include <stdio.h>
00012 #include <string.h>
00013 
00014 #include "config.h"
00015 #include "global.h"
00016 
00017 #define   q1    16384
00018 #define   q2    32768
00019 #define   q3    49152
00020 #define   top   65535
00021 
00022 /* local prototypes */
00023 void bit_out_psc_layer();
00024 
00025 /*********************************************************************
00026  *      SAC Decoder Algorithm as Specified in H26P Annex -E
00027  *
00028  *      Name:           decode_a_symbol
00029  *
00030  *      Description:    Decodes an Aritmetically Encoded Symbol
00031  *
00032  *      Input:          array holding cumulative freq. data
00033  *                      also uses static data for decoding endpoints
00034  *                      and code_value variable
00035  *
00036  *      Returns:        Index to relevant symbol model
00037  *
00038  *      Side Effects:   Modifies low, high, length, cum and code_value
00039  *
00040  *      Author:         Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00041  *
00042  *********************************************************************/
00043  
00044 static long low, high, code_value, bit, length, sacindex, cum, zerorun=0;
00045 
00046 int decode_a_symbol(int cumul_freq[ ])
00047 {
00048 
00049   length = high - low + 1;
00050   cum = (-1 + (code_value - low + 1) * cumul_freq[0]) / length;
00051   for (sacindex = 1; cumul_freq[sacindex] > cum; sacindex++);
00052   high = low - 1 + (length * cumul_freq[sacindex-1]) / cumul_freq[0];
00053   low += (length * cumul_freq[sacindex]) / cumul_freq[0];
00054 
00055   for ( ; ; ) {  
00056     if (high < q2) ;
00057     else if (low >= q2) {
00058       code_value -= q2; 
00059       low -= q2; 
00060       high -= q2;
00061     }
00062     else if (low >= q1 && high < q3) {
00063       code_value -= q1; 
00064       low -= q1; 
00065       high -= q1;
00066     }
00067     else {
00068       break;
00069     }
00070  
00071     low *= 2; 
00072     high = 2*high + 1;
00073     bit_out_psc_layer(); 
00074     code_value = 2*code_value + bit;
00075   }
00076 
00077   return (sacindex-1);
00078 }
00079  
00080 /*********************************************************************
00081  *
00082  *      Name:           decoder_reset
00083  *
00084  *      Description:    Fills Decoder FIFO after a fixed word length
00085  *                      string has been detected.
00086  *
00087  *      Input:          None
00088  *
00089  *      Returns:        Nothing
00090  *
00091  *      Side Effects:   Fills Arithmetic Decoder FIFO
00092  *
00093  *      Author:         Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00094  *
00095  *********************************************************************/
00096 
00097 void decoder_reset( )
00098 {
00099   int i;
00100   zerorun = 0;                  /* clear consecutive zero's counter */
00101   code_value = 0;
00102   low = 0;
00103   high = top;
00104   for (i = 1;   i <= 16;   i++) {
00105     bit_out_psc_layer(); 
00106     code_value = 2 * code_value + bit;
00107   }
00108 }
00109 
00110 /*********************************************************************
00111  *
00112  *      Name:           bit_out_psc_layer
00113  *
00114  *      Description:    Gets a bit from the Encoded Stream, Checks for
00115  *                      and removes any PSC emulation prevention bits
00116  *                      inserted at the decoder, provides 'zeros' to the
00117  *                      Arithmetic Decoder FIFO to allow it to finish 
00118  *                      data prior to the next PSC. (Garbage bits)
00119  *
00120  *      Input:          None
00121  *
00122  *      Returns:        Nothing
00123  *
00124  *      Side Effects:   Gets a bit from the Input Data Stream
00125  *
00126  *      Author:         Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
00127  *
00128  *********************************************************************/
00129 
00130 void bit_out_psc_layer()
00131 {
00132   if (showbits(17)!=1) { /* check for startcode in Arithmetic Decoder FIFO */
00133 
00134     bit = getbits(1);
00135 
00136     if(zerorun > 13) {  /* if number of consecutive zeros = 14 */        
00137       if (!bit) {
00138         zerorun = 1;            
00139       }
00140       else { /* if there is a 'stuffing bit present */
00141         bit = getbits(1);       /* overwrite the last bit */    
00142         zerorun = !bit;         /* zerorun=1 if bit is a '0' */
00143       }
00144     }
00145 
00146     else { /* if consecutive zero's not exceeded 14 */
00147 
00148       if (!bit)
00149         zerorun++;
00150       else
00151         zerorun = 0;
00152     }
00153 
00154   } /* end of if !(showbits(17)) */
00155 
00156   else {
00157     bit = 0;
00158   }
00159 
00160    /*   
00161    printf("lastbit = %ld bit = %ld zerorun = %ld \n", lastbit, bit, zerorun); 
00162    lastbit = bit;
00163    */
00164                                 /* latent diagnostics */
00165         
00166 }
00167 

Generated on Mon May 8 22:27:08 2006 by  doxygen 1.3.9.1