1 /************************************************************************
2 *
3 * sac.c, part of 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 /*********************************************************************
55 * SAC Decoder Module
56 * Algorithm as Specified in H26P Annex -E
57 * (c) 1995 BT Labs
58 *
59 * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
60 *
61 *********************************************************************/
62
63 #include <stdio.h>
64 #include <string.h>
65
66 #include "config.h"
67 #include "tmndec.h"
68 #include "global.h"
69
70 #define q1 16384
71 #define q2 32768
72 #define q3 49152
73 #define top 65535
74
75 /* local prototypes */
76 void bit_out_psc_layer ();
77
78 /*********************************************************************
79 * SAC Decoder Algorithm as Specified in H26P Annex -E
80 *
81 * Name: decode_a_symbol
82 *
83 * Description: Decodes an Aritmetically Encoded Symbol
84 *
85 * Input: array holding cumulative freq. data
86 * also uses static data for decoding endpoints
87 * and code_value variable
88 *
89 * Returns: Index to relevant symbol model
90 *
91 * Side Effects: Modifies low, high, length, cum and code_value
92 *
93 * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
94 *
95 *********************************************************************/
96
97 static long low, high, code_value, bit, length, sacindex, cum, zerorun = 0;
98
99 int decode_a_symbol (int cumul_freq[])
100 {
101
102 length = high - low + 1;
103 cum = (-1 + (code_value - low + 1) * cumul_freq[0]) / length;
104 for (sacindex = 1; cumul_freq[sacindex] > cum; sacindex++);
105 high = low - 1 + (length * cumul_freq[sacindex - 1]) / cumul_freq[0];
106 low += (length * cumul_freq[sacindex]) / cumul_freq[0];
107
108 for (;;)
109 {
110 if (high < q2);
111 else if (low >= q2)
112 {
113 code_value -= q2;
114 low -= q2;
115 high -= q2;
116 } else if (low >= q1 && high < q3)
117 {
118 code_value -= q1;
119 low -= q1;
120 high -= q1;
121 } else
122 {
123 break;
124 }
125
126 low *= 2;
127 high = 2 * high + 1;
128 bit_out_psc_layer ();
129 code_value = 2 * code_value + bit;
130 }
131
132 return (sacindex - 1);
133 }
134
135 /*********************************************************************
136 *
137 * Name: decoder_reset
138 *
139 * Description: Fills Decoder FIFO after a fixed word length
140 * string has been detected.
141 *
142 * Input: None
143 *
144 * Returns: Nothing
145 *
146 * Side Effects: Fills Arithmetic Decoder FIFO
147 *
148 * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
149 *
150 *********************************************************************/
151
152 void decoder_reset ()
153 {
154 int i;
155 zerorun = 0; /* clear consecutive zero's counter */
156 code_value = 0;
157 low = 0;
158 high = top;
159 for (i = 1; i <= 16; i++)
160 {
161 bit_out_psc_layer ();
162 code_value = 2 * code_value + bit;
163 }
164 if (trace)
165 fprintf (trace_file, "\nArithmetic Decoder Reset \n");
166 }
167
168 /*********************************************************************
169 *
170 * Name: bit_out_psc_layer
171 *
172 * Description: Gets a bit from the Encoded Stream, Checks for
173 * and removes any PSC emulation prevention bits
174 * inserted at the decoder, provides 'zeros' to the
175 * Arithmetic Decoder FIFO to allow it to finish
176 * data prior to the next PSC. (Garbage bits)
177 *
178 * Input: None
179 *
180 * Returns: Nothing
181 *
182 * Side Effects: Gets a bit from the Input Data Stream
183 *
184 * Author: Wayne Ellis <ellis_w_wayne@bt-web.bt.co.uk>
185 *
186 *********************************************************************/
187
188 void bit_out_psc_layer ()
189 {
190 if (showbits (17) != 1)
191 { /* check for startcode in Arithmetic
192 * Decoder FIFO */
193
194 bit = getbits (1);
195
196 if (zerorun > 13)
197 { /* if number of consecutive zeros = 14 */
198 if (!bit)
199 {
200 if (trace)
201 fprintf (trace_file, "PSC/GBSC, Header Data, or Encoded Stream Error \n");
202 zerorun = 1;
203 } else
204 { /* if there is a 'stuffing bit present */
205 if (trace)
206 fprintf (trace_file, "Removing Startcode Emulation Prevention bit \n");
207 bit = getbits (1); /* overwrite the last bit */
208 zerorun = !bit; /* zerorun=1 if bit is a '' */
209 }
210 } else
211 { /* if consecutive zero's not exceeded 14 */
212
213 if (!bit)
214 zerorun++;
215 else
216 zerorun = 0;
217 }
218
219 }
220 /* end of if !(showbits(17)) */
221 else
222 {
223 bit = 0;
224 if (trace)
225 fprintf (trace_file, "Startcode Found:Finishing Arithmetic Decoding using 'Garbage bits'\n");
226 }
227
228 /* printf("lastbit = %ld bit = %ld zerorun = %ld \n", lastbit, bit,
229 * zerorun); lastbit = bit; */
230 /* latent diagnostics */
231
232 }
233
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.