Main Page | Class List | File List | Class Members | File Members

vid_sac.c

Go to the documentation of this file.
00001 /************************************************************************
00002  *
00003  *  vid_sac.c, part of tmn (TMN encoder)
00004  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00005  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
00006  *  
00007  *  Contacts: 
00008  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>, or
00009  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00010  *
00011  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00012  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00013  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00014  *  
00015  ************************************************************************/
00016 
00017 /*
00018  * Disclaimer of Warranty
00019  *
00020  * These software programs are available to the user without any
00021  * license fee or royalty on an "as is" basis.  Telenor Research and
00022  * Development disclaims any and all warranties, whether express,
00023  * implied, or statuary, including any implied warranties or
00024  * merchantability or of fitness for a particular purpose.  In no
00025  * event shall the copyright-holder be liable for any incidental,
00026  * punitive, or consequential damages of any kind whatsoever arising
00027  * from the use of these programs.
00028  *
00029  * This disclaimer of warranty extends to the user of these programs
00030  * and user's customers, employees, agents, transferees, successors,
00031  * and assigns.
00032  *
00033  * Telenor Research and Development does not represent or warrant that
00034  * the programs furnished hereunder are free of infringement of any
00035  * third-party patents.
00036  *
00037  * Commercial implementations of H.263, including shareware, are
00038  * subject to royalty fees to patent holders.  Many of these patents
00039  * are general enough such that they are unavoidable regardless of
00040  * implementation design.
00041  * */
00042 
00043 /*********************************************************************
00044  *
00045  * SAC Encoder Module
00046  * Algorithm as specified in H263 (Annex E)
00047  *         (c) BT Labs 1995
00048  *
00049  * Author: Pat Mulroy <pmulroy@visual.bt.co.uk>
00050  *
00051  *********************************************************************/
00052 
00053 #include <stdio.h>
00054 #include "vid_sim.h"
00055 #include "video_codec.h"
00056 
00057 extern video_codec *VidSt;
00058 
00059 #define   q1    16384
00060 #define   q2    32768
00061 #define   q3    49152
00062 #define   top   65535
00063 
00064 static long   low=0, high=top, opposite_bits=0, length=0, zerorun=0;
00065 
00066 /*********************************************************************
00067  *
00068  *      Name:           AR_Encode
00069  *
00070  *      Description:    Encodes a symbol using syntax based arithmetic
00071  *        coding. Algorithm specified in H.263 (Annex E).
00072  *
00073  *      Input:          Array holding cumulative frequency data.
00074  *        Index into specific cumulative frequency array.
00075  *                      Static data for encoding endpoints.
00076  *
00077  *      Returns:        Number of bits used while encoding symbol.
00078  *
00079  *      Side Effects:   Modifies low, high, length and opposite_bits
00080  *        variables.
00081  *
00082  *      Author:         pmulroy@visual.bt.co.uk
00083  *
00084  *********************************************************************/
00085 
00086 int AR_Encode(int index, int cumul_freq[ ])
00087 {
00088   int bitcount=0;
00089 
00090   if (index<0) 
00091     return -1; /* Escape Code */
00092 
00093   length = high - low + 1;
00094   high = low - 1 + (length * cumul_freq[index]) / cumul_freq[0];
00095   low += (length * cumul_freq[index+1]) / cumul_freq[0];
00096 
00097   for ( ; ; ) {
00098     if (high < q2) {
00099       bitcount+=bit_opp_bits(0);
00100     }
00101     else if (low >= q2) {
00102       bitcount+=bit_opp_bits(1);        
00103       low -= q2; 
00104       high -= q2;
00105     }
00106     else if (low >= q1 && high < q3) {
00107       opposite_bits += 1; 
00108       low -= q1; 
00109       high -= q1;
00110     }
00111     else break;
00112  
00113     low *= 2; 
00114     high = 2*high+1;
00115   }
00116   return bitcount;
00117 }
00118 
00119 int bit_opp_bits(int bit) /* Output a bit and the following opposite bits */              
00120 {                                   
00121   int bitcount=0;
00122 
00123   bitcount = bit_in_psc_layer(bit);
00124 
00125   while(opposite_bits > 0){
00126     bitcount += bit_in_psc_layer(!bit);
00127     opposite_bits--;
00128   }
00129   return bitcount;
00130 }
00131 
00132 /*********************************************************************
00133  *
00134  *      Name:           encoder_flush
00135  *
00136  *      Description:    Completes arithmetic coding stream before any
00137  *        fixed length codes are transmitted.
00138  *
00139  *      Input:          None
00140  *
00141  *      Returns:        Number of bits used.
00142  *
00143  *      Side Effects:   Resets low, high, zerorun and opposite_bits 
00144  *        variables.
00145  *
00146  *      Author:         pmulroy@visual.bt.co.uk
00147  *
00148  *********************************************************************/
00149 
00150 int encoder_flush()
00151 {
00152   int bitcount = 0;
00153 
00154   if ((VidSt->trace))
00155     fprintf((VidSt->tf), "encoder_flush:\n");
00156 
00157   opposite_bits++;
00158   if (low < q1) {
00159     bitcount+=bit_opp_bits(0);
00160   }
00161   else {
00162     bitcount+=bit_opp_bits(1);
00163   }
00164   low = 0; 
00165   high = top;
00166 
00167   zerorun=0;
00168 
00169   return bitcount;
00170 }
00171 
00172 /*********************************************************************
00173  *
00174  *      Name:           bit_in_psc_layer
00175  *
00176  *      Description:    Inserts a bit into output bitstream and avoids
00177  *        picture start code emulation by stuffing a one
00178  *        bit.
00179  *
00180  *      Input:          Bit to be output.
00181  *
00182  *      Returns:        Nothing
00183  *
00184  *      Side Effects:   Updates zerorun variable.
00185  *
00186  *      Author:         pmulroy@visual.bt.co.uk
00187  *
00188  *********************************************************************/
00189 
00190 int bit_in_psc_layer(int bit)
00191 {
00192   void putbits (int, int);
00193   int bitcount = 0;
00194 
00195   if (zerorun > 13) {
00196     if ((VidSt->trace))
00197       fprintf((VidSt->tf), "PSC emulation ... Bit stuffed.\n");
00198     putbits (1, 1);
00199     bitcount++;
00200     zerorun = 0;
00201   }
00202 
00203   putbits (1, bit);
00204   bitcount++;
00205 
00206   if (bit)
00207     zerorun = 0;
00208   else
00209     zerorun++;
00210 
00211   return bitcount;
00212 }
00213 
00214 /*********************************************************************
00215  *
00216  *      Name:           indexfn
00217  *
00218  *      Description:    Translates between symbol value and symbol
00219  *        index.
00220  *
00221  *      Input:          Symbol value, index table, max number of
00222  *        values.
00223  *
00224  *      Returns:        Index into cumulative frequency tables or
00225  *        escape code.
00226  *
00227  *      Side Effects:   none
00228  *
00229  *      Author:         pmulroy@visual.bt.co.uk
00230  *
00231  *********************************************************************/
00232 
00233 int indexfn(int value, int table[], int max)
00234 {
00235   int n=0;
00236 
00237   while(1) {
00238     if (table[n++]==value) return n-1;
00239     if (n>max) return -1;
00240   }
00241 
00242 }
00243 

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1