annotate ac3enc.c @ 175:bd77d3cbb233 libavcodec

new IDCT code by Michael Niedermayer (michaelni@gmx.at) - #define SIMPLE_IDCT to enable
author arpi_esp
date Sun, 09 Dec 2001 12:39:54 +0000
parents 6f0e167504e8
children 5fc0c3af3fe4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * The simplest AC3 encoder
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
64
5aa6292a1660 win32 fixes
glantau
parents: 25
diff changeset
19 //#define DEBUG
5aa6292a1660 win32 fixes
glantau
parents: 25
diff changeset
20 //#define DEBUG_BITALLOC
5aa6292a1660 win32 fixes
glantau
parents: 25
diff changeset
21 #include "avcodec.h"
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include <math.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
23
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include "ac3enc.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "ac3tab.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26
986e461dc072 Initial revision
glantau
parents:
diff changeset
27
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 #define MDCT_NBITS 9
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 #define N (1 << MDCT_NBITS)
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 #define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
31
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 /* new exponents are sent if their Norm 1 exceed this number */
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 #define EXP_DIFF_THRESHOLD 1000
986e461dc072 Initial revision
glantau
parents:
diff changeset
34
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 /* exponent encoding strategy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 #define EXP_REUSE 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 #define EXP_NEW 1
986e461dc072 Initial revision
glantau
parents:
diff changeset
38
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 #define EXP_D15 1
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 #define EXP_D25 2
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 #define EXP_D45 3
986e461dc072 Initial revision
glantau
parents:
diff changeset
42
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 static void fft_init(int ln);
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 static void ac3_crc_init(void);
986e461dc072 Initial revision
glantau
parents:
diff changeset
45
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 static inline INT16 fix15(float a)
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 int v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 v = (int)(a * (float)(1 << 15));
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 if (v < -32767)
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 v = -32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 else if (v > 32767)
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 v = 32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 return v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
56
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 static inline int calc_lowcomp1(int a, int b0, int b1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 if ((b0 + 256) == b1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 a = 384 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 } else if (b0 > b1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 a = a - 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 if (a < 0) a=0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 return a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
67
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 static inline int calc_lowcomp(int a, int b0, int b1, int bin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 if (bin < 7) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 if ((b0 + 256) == b1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 a = 384 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 } else if (b0 > b1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 a = a - 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 if (a < 0) a=0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 } else if (bin < 20) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 if ((b0 + 256) == b1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 a = 320 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 } else if (b0 > b1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 a= a - 64;
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 if (a < 0) a=0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 a = a - 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 if (a < 0) a=0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 return a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
90
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 /* AC3 bit allocation. The algorithm is the one described in the AC3
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 spec with some optimizations because of our simplified encoding
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 assumptions. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 INT8 *exp, int start, int end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 int snroffset, int fgain)
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 int fastleak,slowleak,address,tmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 INT16 psd[256]; /* scaled exponents */
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 INT16 bndpsd[50]; /* interpolated exponents */
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 INT16 excite[50]; /* excitation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 INT16 mask[50]; /* masking value */
986e461dc072 Initial revision
glantau
parents:
diff changeset
104
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 /* exponent mapping to PSD */
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 for(bin=start;bin<end;bin++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 psd[bin]=(3072 - (exp[bin] << 7));
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
109
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 /* PSD integration */
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 j=start;
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 k=masktab[start];
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 v=psd[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 j++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 end1=bndtab[k+1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 if (end1 > end) end1=end;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 for(i=j;i<end1;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 int c,adr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 /* logadd */
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 v1=psd[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 c=v-v1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 if (c >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 adr=c >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 if (adr > 255) adr=255;
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 v=v + latab[adr];
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 adr=(-c) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 if (adr > 255) adr=255;
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 v=v1 + latab[adr];
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 j++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 bndpsd[k]=v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 k++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 } while (end > bndtab[k]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
137
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 /* excitation function */
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 bndstrt = masktab[start];
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 bndend = masktab[end-1] + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
141
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 lowcomp = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 excite[0] = bndpsd[0] - fgain - lowcomp ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 excite[1] = bndpsd[1] - fgain - lowcomp ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 begin = 7 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 for (bin = 2; bin < 7; bin++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 fastleak = bndpsd[bin] - fgain ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 slowleak = bndpsd[bin] - s->sgain ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 excite[bin] = fastleak - lowcomp ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 if (bndpsd[bin] <= bndpsd[bin+1]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 begin = bin + 1 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 break ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
158
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 end1=bndend;
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 if (end1 > 22) end1=22;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 for (bin = begin; bin < end1; bin++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 fastleak -= s->fdecay ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 v = bndpsd[bin] - fgain;
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 if (fastleak < v) fastleak = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
168
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 slowleak -= s->sdecay ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 v = bndpsd[bin] - s->sgain;
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 if (slowleak < v) slowleak = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 v=fastleak - lowcomp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 if (slowleak > v) v=slowleak;
986e461dc072 Initial revision
glantau
parents:
diff changeset
175
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 excite[bin] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
178
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 for (bin = 22; bin < bndend; bin++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 fastleak -= s->fdecay ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 v = bndpsd[bin] - fgain;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 if (fastleak < v) fastleak = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 slowleak -= s->sdecay ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 v = bndpsd[bin] - s->sgain;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 if (slowleak < v) slowleak = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
186
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 v=fastleak;
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 if (slowleak > v) v = slowleak;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 excite[bin] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
191
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 /* compute masking curve */
986e461dc072 Initial revision
glantau
parents:
diff changeset
193
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 for (bin = bndstrt; bin < bndend; bin++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 v1 = excite[bin];
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 tmp = s->dbknee - bndpsd[bin];
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 if (tmp > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 v1 += tmp >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 v=hth[bin >> s->halfratecod][s->fscod];
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 if (v1 > v) v=v1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 mask[bin] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
204
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 /* compute bit allocation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
206
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 i = start ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 j = masktab[start] ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 v=mask[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 v -= snroffset ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 v -= s->floor ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 if (v < 0) v = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 v &= 0x1fe0 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 v += s->floor ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
216
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 end1=bndtab[j] + bndsz[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 if (end1 > end) end1=end;
986e461dc072 Initial revision
glantau
parents:
diff changeset
219
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 for (k = i; k < end1; k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 address = (psd[i] - v) >> 5 ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 if (address < 0) address=0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 else if (address > 63) address=63;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 bap[i] = baptab[address];
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 } while (end > bndtab[j++]) ;
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
229
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 typedef struct IComplex {
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 short re,im;
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 } IComplex;
986e461dc072 Initial revision
glantau
parents:
diff changeset
233
986e461dc072 Initial revision
glantau
parents:
diff changeset
234 static void fft_init(int ln)
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 int i, j, m, n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 float alpha;
986e461dc072 Initial revision
glantau
parents:
diff changeset
238
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 n = 1 << ln;
986e461dc072 Initial revision
glantau
parents:
diff changeset
240
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 for(i=0;i<(n/2);i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 alpha = 2 * M_PI * (float)i / (float)n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
243 costab[i] = fix15(cos(alpha));
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 sintab[i] = fix15(sin(alpha));
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
246
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 m=0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 for(j=0;j<ln;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 m |= ((i >> j) & 1) << (ln-j-1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 fft_rev[i]=m;
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
255
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 /* butter fly op */
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 {\
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 int ax, ay, bx, by;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 bx=pre1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 by=pim1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 ax=qre1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 ay=qim1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 pre = (bx + ax) >> 1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 pim = (by + ay) >> 1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 qre = (bx - ax) >> 1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 qim = (by - ay) >> 1;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
269
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 #define MUL16(a,b) ((a) * (b))
986e461dc072 Initial revision
glantau
parents:
diff changeset
271
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 #define CMUL(pre, pim, are, aim, bre, bim) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 {\
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
277
986e461dc072 Initial revision
glantau
parents:
diff changeset
278
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 /* do a 2^n point complex fft on 2^ln points. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 static void fft(IComplex *z, int ln)
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 int j, l, np, np2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 int nblocks, nloops;
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 register IComplex *p,*q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 int tmp_re, tmp_im;
986e461dc072 Initial revision
glantau
parents:
diff changeset
286
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 np = 1 << ln;
986e461dc072 Initial revision
glantau
parents:
diff changeset
288
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 /* reverse */
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 for(j=0;j<np;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 int k;
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 IComplex tmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 k = fft_rev[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 if (k < j) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 tmp = z[k];
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 z[k] = z[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 z[j] = tmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
300
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 /* pass 0 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
302
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 p=&z[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 j=(np >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 BF(p[0].re, p[0].im, p[1].re, p[1].im,
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 p[0].re, p[0].im, p[1].re, p[1].im);
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 p+=2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 } while (--j != 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
310
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 /* pass 1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
312
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 p=&z[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 j=np >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 BF(p[0].re, p[0].im, p[2].re, p[2].im,
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 p[0].re, p[0].im, p[2].re, p[2].im);
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 BF(p[1].re, p[1].im, p[3].re, p[3].im,
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 p[1].re, p[1].im, p[3].im, -p[3].re);
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 p+=4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 } while (--j != 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
322
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 /* pass 2 .. ln-1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
324
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 nblocks = np >> 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 nloops = 1 << 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 np2 = np >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
329 p = z;
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 q = z + nloops;
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 for (j = 0; j < nblocks; ++j) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
332
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 BF(p->re, p->im, q->re, q->im,
986e461dc072 Initial revision
glantau
parents:
diff changeset
334 p->re, p->im, q->re, q->im);
986e461dc072 Initial revision
glantau
parents:
diff changeset
335
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 q++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 for(l = nblocks; l < np2; l += nblocks) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
339 CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im);
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 BF(p->re, p->im, q->re, q->im,
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 p->re, p->im, tmp_re, tmp_im);
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 q++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 p += nloops;
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 q += nloops;
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
348 nblocks = nblocks >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 nloops = nloops << 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 } while (nblocks != 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
352
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 /* do a 512 point mdct */
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 static void mdct512(INT32 *out, INT16 *in)
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 int i, re, im, re1, im1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 INT16 rot[N];
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 IComplex x[N/4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
359
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 /* shift to simplify computations */
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 for(i=0;i<N/4;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 rot[i] = -in[i + 3*N/4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 for(i=N/4;i<N;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 rot[i] = in[i - N/4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
365
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 /* pre rotation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 for(i=0;i<N/4;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
372
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 fft(x, MDCT_NBITS - 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
374
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 /* post rotation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 for(i=0;i<N/4;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 re = x[i].re;
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 im = x[i].im;
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 CMUL(re1, im1, re, im, xsin1[i], xcos1[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 out[2*i] = im1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 out[N/2-1-2*i] = re1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
384
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 /* XXX: use another norm ? */
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 int sum, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 sum = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 sum += abs(exp1[i] - exp2[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
392 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 return sum;
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
395
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 int ch)
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 int i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 int exp_diff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
402
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 /* estimate if the exponent variation & decide if they should be
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 reused in the next frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 exp_strategy[0][ch] = EXP_NEW;
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 for(i=1;i<NB_BLOCKS;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 #ifdef DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 printf("exp_diff=%d\n", exp_diff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 if (exp_diff > EXP_DIFF_THRESHOLD)
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 exp_strategy[i][ch] = EXP_NEW;
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 exp_strategy[i][ch] = EXP_REUSE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 /* now select the encoding strategy type : if exponents are often
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 recoded, we use a coarse encoding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
419 while (i < NB_BLOCKS) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
420 j = i + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
421 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 j++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
423 switch(j - i) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 exp_strategy[i][ch] = EXP_D45;
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
427 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 exp_strategy[i][ch] = EXP_D25;
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 exp_strategy[i][ch] = EXP_D15;
986e461dc072 Initial revision
glantau
parents:
diff changeset
433 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 i = j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
438
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 /* set exp[i] to min(exp[i], exp1[i]) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
440 static void exponent_min(UINT8 exp[N/2], UINT8 exp1[N/2], int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
443
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 if (exp1[i] < exp[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 exp[i] = exp1[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
449
986e461dc072 Initial revision
glantau
parents:
diff changeset
450 /* update the exponents so that they are the ones the decoder will
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 decode. Return the number of bits used to code the exponents */
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 static int encode_exp(UINT8 encoded_exp[N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 UINT8 exp[N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 int nb_exps,
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 int exp_strategy)
986e461dc072 Initial revision
glantau
parents:
diff changeset
456 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
457 int group_size, nb_groups, i, j, k, recurse, exp_min, delta;
986e461dc072 Initial revision
glantau
parents:
diff changeset
458 UINT8 exp1[N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
459
986e461dc072 Initial revision
glantau
parents:
diff changeset
460 switch(exp_strategy) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
461 case EXP_D15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 group_size = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 case EXP_D25:
986e461dc072 Initial revision
glantau
parents:
diff changeset
465 group_size = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
466 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 case EXP_D45:
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 group_size = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
472 nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
473
986e461dc072 Initial revision
glantau
parents:
diff changeset
474 /* for each group, compute the minimum exponent */
986e461dc072 Initial revision
glantau
parents:
diff changeset
475 exp1[0] = exp[0]; /* DC exponent is handled separately */
986e461dc072 Initial revision
glantau
parents:
diff changeset
476 k = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
477 for(i=1;i<=nb_groups;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
478 exp_min = exp[k];
986e461dc072 Initial revision
glantau
parents:
diff changeset
479 assert(exp_min >= 0 && exp_min <= 24);
986e461dc072 Initial revision
glantau
parents:
diff changeset
480 for(j=1;j<group_size;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
481 if (exp[k+j] < exp_min)
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 exp_min = exp[k+j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
484 exp1[i] = exp_min;
986e461dc072 Initial revision
glantau
parents:
diff changeset
485 k += group_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
487
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 /* constraint for DC exponent */
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 if (exp1[0] > 15)
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 exp1[0] = 15;
986e461dc072 Initial revision
glantau
parents:
diff changeset
491
986e461dc072 Initial revision
glantau
parents:
diff changeset
492 /* Iterate until the delta constraints between each groups are
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 satisfyed. I'm sure it is possible to find a better algorithm,
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 but I am lazy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 recurse = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 for(i=1;i<=nb_groups;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
498 delta = exp1[i] - exp1[i-1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 if (delta > 2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
500 /* if delta too big, we encode a smaller exponent */
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 exp1[i] = exp1[i-1] + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 } else if (delta < -2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 /* if delta is too small, we must decrease the previous
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 exponent, which means we must recurse */
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 recurse = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 exp1[i-1] = exp1[i] + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 } while (recurse);
986e461dc072 Initial revision
glantau
parents:
diff changeset
510
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 /* now we have the exponent values the decoder will see */
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 encoded_exp[0] = exp1[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
513 k = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 for(i=1;i<=nb_groups;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 for(j=0;j<group_size;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 encoded_exp[k+j] = exp1[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 k += group_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
520
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 #if defined(DEBUG)
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 printf("exponents: strategy=%d\n", exp_strategy);
986e461dc072 Initial revision
glantau
parents:
diff changeset
523 for(i=0;i<=nb_groups * group_size;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 printf("%d ", encoded_exp[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
528
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 return 4 + (nb_groups / 3) * 7;
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
531
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 /* return the size in bits taken by the mantissa */
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 int compute_mantissa_size(AC3EncodeContext *s, UINT8 *m, int nb_coefs)
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 int bits, mant, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
536
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 bits = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 for(i=0;i<nb_coefs;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 mant = m[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 switch(mant) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 /* nothing */
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 /* 3 mantissa in 5 bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 if (s->mant1_cnt == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 bits += 5;
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 if (++s->mant1_cnt == 3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 s->mant1_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 /* 3 mantissa in 7 bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
553 if (s->mant2_cnt == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 bits += 7;
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 if (++s->mant2_cnt == 3)
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 s->mant2_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
558 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 bits += 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
561 case 4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 /* 2 mantissa in 7 bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 if (s->mant4_cnt == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 bits += 7;
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 if (++s->mant4_cnt == 2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 s->mant4_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 case 14:
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 bits += 14;
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 case 15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 bits += 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 bits += mant - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
578 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 return bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
581
986e461dc072 Initial revision
glantau
parents:
diff changeset
582
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 static int bit_alloc(AC3EncodeContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 int frame_bits, int csnroffst, int fsnroffst)
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 int i, ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
590
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 /* compute size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
592 for(i=0;i<NB_BLOCKS;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 s->mant1_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 s->mant2_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 s->mant4_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 for(ch=0;ch<s->nb_channels;ch++) {
25
2f603eb2f23d types fix
glantau
parents: 0
diff changeset
597 parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch],
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 0, s->nb_coefs[ch],
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 (((csnroffst-15) << 4) +
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 fsnroffst) << 2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 fgaintab[s->fgaincod[ch]]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 frame_bits += compute_mantissa_size(s, bap[i][ch],
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 s->nb_coefs[ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
605 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 csnroffst, fsnroffst, frame_bits,
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 16 * s->frame_size - ((frame_bits + 7) & ~7));
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 return 16 * s->frame_size - frame_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
613
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 #define SNR_INC1 4
986e461dc072 Initial revision
glantau
parents:
diff changeset
615
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 static int compute_bit_allocation(AC3EncodeContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 int frame_bits)
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 int i, ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 int csnroffst, fsnroffst;
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
625
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 /* init default parameters */
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 s->sdecaycod = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 s->fdecaycod = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 s->sgaincod = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 s->dbkneecod = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 s->floorcod = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 for(ch=0;ch<s->nb_channels;ch++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
633 s->fgaincod[ch] = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
634
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 /* compute real values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 s->sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 s->fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 s->sgain = sgaintab[s->sgaincod];
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 s->dbknee = dbkneetab[s->dbkneecod];
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 s->floor = floortab[s->floorcod];
986e461dc072 Initial revision
glantau
parents:
diff changeset
641
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 /* header size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 frame_bits += 65;
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 if (s->acmod == 2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 frame_bits += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
646
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 /* audio blocks */
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 for(i=0;i<NB_BLOCKS;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 frame_bits += s->nb_channels * 2 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 if (s->acmod == 2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 frame_bits++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 frame_bits += 2 * s->nb_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 if (exp_strategy[i][ch] != EXP_REUSE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 frame_bits += 6 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
657 frame_bits++; /* baie */
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 frame_bits++; /* snr */
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 frame_bits += 2; /* delta / skip */
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 frame_bits++; /* cplinu for block 0 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 /* bit alloc info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 frame_bits += 2*4 + 3 + 6 + s->nb_channels * (4 + 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
664
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 /* CRC */
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 frame_bits += 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
667
986e461dc072 Initial revision
glantau
parents:
diff changeset
668 /* now the big work begins : do the bit allocation. Modify the snr
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 offset until we can pack everything in the requested frame size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
670
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 csnroffst = s->csnroffst;
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 while (csnroffst >= 0 &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 csnroffst -= SNR_INC1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 if (csnroffst < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 fprintf(stderr, "Error !!!\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
679 while ((csnroffst + SNR_INC1) <= 63 &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 csnroffst + SNR_INC1, 0) >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 csnroffst += SNR_INC1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 memcpy(bap, bap1, sizeof(bap1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
685 while ((csnroffst + 1) <= 63 &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
686 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
687 csnroffst++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
688 memcpy(bap, bap1, sizeof(bap1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
689 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
690
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 fsnroffst = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
692 while ((fsnroffst + SNR_INC1) <= 15 &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
693 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 csnroffst, fsnroffst + SNR_INC1) >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
695 fsnroffst += SNR_INC1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 memcpy(bap, bap1, sizeof(bap1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
697 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 while ((fsnroffst + 1) <= 15 &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
986e461dc072 Initial revision
glantau
parents:
diff changeset
700 csnroffst, fsnroffst + 1) >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
701 fsnroffst++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 memcpy(bap, bap1, sizeof(bap1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
704
986e461dc072 Initial revision
glantau
parents:
diff changeset
705 s->csnroffst = csnroffst;
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 for(ch=0;ch<s->nb_channels;ch++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
707 s->fsnroffst[ch] = fsnroffst;
986e461dc072 Initial revision
glantau
parents:
diff changeset
708 #if defined(DEBUG_BITALLOC)
986e461dc072 Initial revision
glantau
parents:
diff changeset
709 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
710 int j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
711
986e461dc072 Initial revision
glantau
parents:
diff changeset
712 for(i=0;i<6;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
713 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
714 printf("Block #%d Ch%d:\n", i, ch);
986e461dc072 Initial revision
glantau
parents:
diff changeset
715 printf("bap=");
986e461dc072 Initial revision
glantau
parents:
diff changeset
716 for(j=0;j<s->nb_coefs[ch];j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
717 printf("%d ",bap[i][ch][j]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
718 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 printf("\n");
986e461dc072 Initial revision
glantau
parents:
diff changeset
720 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
721 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
723 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
724 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
725 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
726
986e461dc072 Initial revision
glantau
parents:
diff changeset
727 static int AC3_encode_init(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
729 int freq = avctx->sample_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 int bitrate = avctx->bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 int channels = avctx->channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 AC3EncodeContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 int i, j, k, l, ch, v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
734 float alpha;
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 static unsigned short freqs[3] = { 48000, 44100, 32000 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
736
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 avctx->frame_size = AC3_FRAME_SIZE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 avctx->key_frame = 1; /* always key frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
739
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 /* number of channels */
986e461dc072 Initial revision
glantau
parents:
diff changeset
741 if (channels == 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
742 s->acmod = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 else if (channels == 2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 s->acmod = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
746 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 s->nb_channels = channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
748
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 /* frequency */
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
751 for(j=0;j<3;j++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 if ((freqs[j] >> i) == freq)
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 goto found;
986e461dc072 Initial revision
glantau
parents:
diff changeset
754 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
756 found:
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 s->sample_rate = freq;
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 s->halfratecod = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 s->fscod = j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 s->bsid = 8 + s->halfratecod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
761 s->bsmod = 0; /* complete main audio service */
986e461dc072 Initial revision
glantau
parents:
diff changeset
762
986e461dc072 Initial revision
glantau
parents:
diff changeset
763 /* bitrate & frame size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
764 bitrate /= 1000;
986e461dc072 Initial revision
glantau
parents:
diff changeset
765 for(i=0;i<19;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 if ((bitratetab[i] >> s->halfratecod) == bitrate)
986e461dc072 Initial revision
glantau
parents:
diff changeset
767 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
769 if (i == 19)
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 s->bit_rate = bitrate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 s->frmsizecod = i << 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
774 /* for now we do not handle fractional sizes */
986e461dc072 Initial revision
glantau
parents:
diff changeset
775 s->frame_size = s->frame_size_min;
986e461dc072 Initial revision
glantau
parents:
diff changeset
776
986e461dc072 Initial revision
glantau
parents:
diff changeset
777 /* bit allocation init */
986e461dc072 Initial revision
glantau
parents:
diff changeset
778 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
779 /* bandwidth for each channel */
986e461dc072 Initial revision
glantau
parents:
diff changeset
780 /* XXX: should compute the bandwidth according to the frame
986e461dc072 Initial revision
glantau
parents:
diff changeset
781 size, so that we avoid anoying high freq artefacts */
986e461dc072 Initial revision
glantau
parents:
diff changeset
782 s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
986e461dc072 Initial revision
glantau
parents:
diff changeset
784 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
785 /* initial snr offset */
986e461dc072 Initial revision
glantau
parents:
diff changeset
786 s->csnroffst = 40;
986e461dc072 Initial revision
glantau
parents:
diff changeset
787
986e461dc072 Initial revision
glantau
parents:
diff changeset
788 /* compute bndtab and masktab from bandsz */
986e461dc072 Initial revision
glantau
parents:
diff changeset
789 k = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
790 l = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
791 for(i=0;i<50;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 bndtab[i] = l;
986e461dc072 Initial revision
glantau
parents:
diff changeset
793 v = bndsz[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
794 for(j=0;j<v;j++) masktab[k++]=i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
795 l += v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
796 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
797 bndtab[50] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
798
986e461dc072 Initial revision
glantau
parents:
diff changeset
799 /* mdct init */
986e461dc072 Initial revision
glantau
parents:
diff changeset
800 fft_init(MDCT_NBITS - 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
801 for(i=0;i<N/4;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
802 alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N;
986e461dc072 Initial revision
glantau
parents:
diff changeset
803 xcos1[i] = fix15(-cos(alpha));
986e461dc072 Initial revision
glantau
parents:
diff changeset
804 xsin1[i] = fix15(-sin(alpha));
986e461dc072 Initial revision
glantau
parents:
diff changeset
805 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
806
986e461dc072 Initial revision
glantau
parents:
diff changeset
807 ac3_crc_init();
986e461dc072 Initial revision
glantau
parents:
diff changeset
808
986e461dc072 Initial revision
glantau
parents:
diff changeset
809 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
810 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
811
986e461dc072 Initial revision
glantau
parents:
diff changeset
812 /* output the AC3 frame header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
813 static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
986e461dc072 Initial revision
glantau
parents:
diff changeset
814 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
815 init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE, NULL, NULL);
986e461dc072 Initial revision
glantau
parents:
diff changeset
816
986e461dc072 Initial revision
glantau
parents:
diff changeset
817 put_bits(&s->pb, 16, 0x0b77); /* frame header */
986e461dc072 Initial revision
glantau
parents:
diff changeset
818 put_bits(&s->pb, 16, 0); /* crc1: will be filled later */
986e461dc072 Initial revision
glantau
parents:
diff changeset
819 put_bits(&s->pb, 2, s->fscod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
820 put_bits(&s->pb, 6, s->frmsizecod + (s->frame_size - s->frame_size_min));
986e461dc072 Initial revision
glantau
parents:
diff changeset
821 put_bits(&s->pb, 5, s->bsid);
986e461dc072 Initial revision
glantau
parents:
diff changeset
822 put_bits(&s->pb, 3, s->bsmod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
823 put_bits(&s->pb, 3, s->acmod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
824 if (s->acmod == 2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
825 put_bits(&s->pb, 2, 0); /* surround not indicated */
986e461dc072 Initial revision
glantau
parents:
diff changeset
826 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
827 put_bits(&s->pb, 1, 0); /* no LFE */
986e461dc072 Initial revision
glantau
parents:
diff changeset
828 put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
986e461dc072 Initial revision
glantau
parents:
diff changeset
829 put_bits(&s->pb, 1, 0); /* no compression control word */
986e461dc072 Initial revision
glantau
parents:
diff changeset
830 put_bits(&s->pb, 1, 0); /* no lang code */
986e461dc072 Initial revision
glantau
parents:
diff changeset
831 put_bits(&s->pb, 1, 0); /* no audio production info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
832 put_bits(&s->pb, 1, 0); /* no copyright */
986e461dc072 Initial revision
glantau
parents:
diff changeset
833 put_bits(&s->pb, 1, 1); /* original bitstream */
986e461dc072 Initial revision
glantau
parents:
diff changeset
834 put_bits(&s->pb, 1, 0); /* no time code 1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
835 put_bits(&s->pb, 1, 0); /* no time code 2 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
836 put_bits(&s->pb, 1, 0); /* no addtional bit stream info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
837 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
838
986e461dc072 Initial revision
glantau
parents:
diff changeset
839 /* symetric quantization on 'levels' levels */
986e461dc072 Initial revision
glantau
parents:
diff changeset
840 static inline int sym_quant(int c, int e, int levels)
986e461dc072 Initial revision
glantau
parents:
diff changeset
841 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
842 int v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
843
986e461dc072 Initial revision
glantau
parents:
diff changeset
844 if (c >= 0) {
87
6f0e167504e8 fixed symetric quantization (better quality!)
glantau
parents: 65
diff changeset
845 v = (levels * (c << e)) >> 24;
6f0e167504e8 fixed symetric quantization (better quality!)
glantau
parents: 65
diff changeset
846 v = (v + 1) >> 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
847 v = (levels >> 1) + v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 } else {
87
6f0e167504e8 fixed symetric quantization (better quality!)
glantau
parents: 65
diff changeset
849 v = (levels * ((-c) << e)) >> 24;
6f0e167504e8 fixed symetric quantization (better quality!)
glantau
parents: 65
diff changeset
850 v = (v + 1) >> 1;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
851 v = (levels >> 1) - v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
852 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
853 assert (v >= 0 && v < levels);
986e461dc072 Initial revision
glantau
parents:
diff changeset
854 return v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
855 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
856
986e461dc072 Initial revision
glantau
parents:
diff changeset
857 /* asymetric quantization on 2^qbits levels */
986e461dc072 Initial revision
glantau
parents:
diff changeset
858 static inline int asym_quant(int c, int e, int qbits)
986e461dc072 Initial revision
glantau
parents:
diff changeset
859 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
860 int lshift, m, v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
861
986e461dc072 Initial revision
glantau
parents:
diff changeset
862 lshift = e + qbits - 24;
986e461dc072 Initial revision
glantau
parents:
diff changeset
863 if (lshift >= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
864 v = c << lshift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
865 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
866 v = c >> (-lshift);
986e461dc072 Initial revision
glantau
parents:
diff changeset
867 /* rounding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
868 v = (v + 1) >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
869 m = (1 << (qbits-1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
870 if (v >= m)
986e461dc072 Initial revision
glantau
parents:
diff changeset
871 v = m - 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
872 assert(v >= -m);
986e461dc072 Initial revision
glantau
parents:
diff changeset
873 return v & ((1 << qbits)-1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
874 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
875
986e461dc072 Initial revision
glantau
parents:
diff changeset
876 /* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
986e461dc072 Initial revision
glantau
parents:
diff changeset
877 frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
878 static void output_audio_block(AC3EncodeContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
879 UINT8 exp_strategy[AC3_MAX_CHANNELS],
986e461dc072 Initial revision
glantau
parents:
diff changeset
880 UINT8 encoded_exp[AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
881 UINT8 bap[AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
882 INT32 mdct_coefs[AC3_MAX_CHANNELS][N/2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
883 INT8 global_exp[AC3_MAX_CHANNELS],
986e461dc072 Initial revision
glantau
parents:
diff changeset
884 int block_num)
986e461dc072 Initial revision
glantau
parents:
diff changeset
885 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
886 int ch, nb_groups, group_size, i, baie;
986e461dc072 Initial revision
glantau
parents:
diff changeset
887 UINT8 *p;
986e461dc072 Initial revision
glantau
parents:
diff changeset
888 UINT16 qmant[AC3_MAX_CHANNELS][N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
889 int exp0, exp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
890 int mant1_cnt, mant2_cnt, mant4_cnt;
986e461dc072 Initial revision
glantau
parents:
diff changeset
891 UINT16 *qmant1_ptr, *qmant2_ptr, *qmant4_ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
892 int delta0, delta1, delta2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
893
986e461dc072 Initial revision
glantau
parents:
diff changeset
894 for(ch=0;ch<s->nb_channels;ch++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
895 put_bits(&s->pb, 1, 0); /* 512 point MDCT */
986e461dc072 Initial revision
glantau
parents:
diff changeset
896 for(ch=0;ch<s->nb_channels;ch++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
897 put_bits(&s->pb, 1, 1); /* no dither */
986e461dc072 Initial revision
glantau
parents:
diff changeset
898 put_bits(&s->pb, 1, 0); /* no dynamic range */
986e461dc072 Initial revision
glantau
parents:
diff changeset
899 if (block_num == 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
900 /* for block 0, even if no coupling, we must say it. This is a
986e461dc072 Initial revision
glantau
parents:
diff changeset
901 waste of bit :-) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
902 put_bits(&s->pb, 1, 1); /* coupling strategy present */
986e461dc072 Initial revision
glantau
parents:
diff changeset
903 put_bits(&s->pb, 1, 0); /* no coupling strategy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
904 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
905 put_bits(&s->pb, 1, 0); /* no new coupling strategy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
906 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
907
986e461dc072 Initial revision
glantau
parents:
diff changeset
908 if (s->acmod == 2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
909 put_bits(&s->pb, 1, 0); /* no matrixing (but should be used in the future) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
910 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
911
986e461dc072 Initial revision
glantau
parents:
diff changeset
912 #if defined(DEBUG)
986e461dc072 Initial revision
glantau
parents:
diff changeset
913 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
914 static int count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
915 printf("Block #%d (%d)\n", block_num, count++);
986e461dc072 Initial revision
glantau
parents:
diff changeset
916 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
917 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
918 /* exponent strategy */
986e461dc072 Initial revision
glantau
parents:
diff changeset
919 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
920 put_bits(&s->pb, 2, exp_strategy[ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
921 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
922
986e461dc072 Initial revision
glantau
parents:
diff changeset
923 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
924 if (exp_strategy[ch] != EXP_REUSE)
986e461dc072 Initial revision
glantau
parents:
diff changeset
925 put_bits(&s->pb, 6, s->chbwcod[ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
926 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
927
986e461dc072 Initial revision
glantau
parents:
diff changeset
928 /* exponents */
986e461dc072 Initial revision
glantau
parents:
diff changeset
929 for (ch = 0; ch < s->nb_channels; ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
930 switch(exp_strategy[ch]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
931 case EXP_REUSE:
986e461dc072 Initial revision
glantau
parents:
diff changeset
932 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
933 case EXP_D15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
934 group_size = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
935 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
936 case EXP_D25:
986e461dc072 Initial revision
glantau
parents:
diff changeset
937 group_size = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
938 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
939 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
940 case EXP_D45:
986e461dc072 Initial revision
glantau
parents:
diff changeset
941 group_size = 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
942 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
943 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
944 nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
945 p = encoded_exp[ch];
986e461dc072 Initial revision
glantau
parents:
diff changeset
946
986e461dc072 Initial revision
glantau
parents:
diff changeset
947 /* first exponent */
986e461dc072 Initial revision
glantau
parents:
diff changeset
948 exp1 = *p++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
949 put_bits(&s->pb, 4, exp1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
950
986e461dc072 Initial revision
glantau
parents:
diff changeset
951 /* next ones are delta encoded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
952 for(i=0;i<nb_groups;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
953 /* merge three delta in one code */
986e461dc072 Initial revision
glantau
parents:
diff changeset
954 exp0 = exp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
955 exp1 = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
956 p += group_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
957 delta0 = exp1 - exp0 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
958
986e461dc072 Initial revision
glantau
parents:
diff changeset
959 exp0 = exp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
960 exp1 = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
961 p += group_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
962 delta1 = exp1 - exp0 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
963
986e461dc072 Initial revision
glantau
parents:
diff changeset
964 exp0 = exp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
965 exp1 = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
966 p += group_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
967 delta2 = exp1 - exp0 + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
968
986e461dc072 Initial revision
glantau
parents:
diff changeset
969 put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
970 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
971
986e461dc072 Initial revision
glantau
parents:
diff changeset
972 put_bits(&s->pb, 2, 0); /* no gain range info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
973 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
974
986e461dc072 Initial revision
glantau
parents:
diff changeset
975 /* bit allocation info */
986e461dc072 Initial revision
glantau
parents:
diff changeset
976 baie = (block_num == 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
977 put_bits(&s->pb, 1, baie);
986e461dc072 Initial revision
glantau
parents:
diff changeset
978 if (baie) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
979 put_bits(&s->pb, 2, s->sdecaycod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
980 put_bits(&s->pb, 2, s->fdecaycod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
981 put_bits(&s->pb, 2, s->sgaincod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
982 put_bits(&s->pb, 2, s->dbkneecod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
983 put_bits(&s->pb, 3, s->floorcod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
984 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
985
986e461dc072 Initial revision
glantau
parents:
diff changeset
986 /* snr offset */
986e461dc072 Initial revision
glantau
parents:
diff changeset
987 put_bits(&s->pb, 1, baie); /* always present with bai */
986e461dc072 Initial revision
glantau
parents:
diff changeset
988 if (baie) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
989 put_bits(&s->pb, 6, s->csnroffst);
986e461dc072 Initial revision
glantau
parents:
diff changeset
990 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
991 put_bits(&s->pb, 4, s->fsnroffst[ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
992 put_bits(&s->pb, 3, s->fgaincod[ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
993 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
994 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
995
986e461dc072 Initial revision
glantau
parents:
diff changeset
996 put_bits(&s->pb, 1, 0); /* no delta bit allocation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
997 put_bits(&s->pb, 1, 0); /* no data to skip */
986e461dc072 Initial revision
glantau
parents:
diff changeset
998
986e461dc072 Initial revision
glantau
parents:
diff changeset
999 /* mantissa encoding : we use two passes to handle the grouping. A
986e461dc072 Initial revision
glantau
parents:
diff changeset
1000 one pass method may be faster, but it would necessitate to
986e461dc072 Initial revision
glantau
parents:
diff changeset
1001 modify the output stream. */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1002
986e461dc072 Initial revision
glantau
parents:
diff changeset
1003 /* first pass: quantize */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1004 mant1_cnt = mant2_cnt = mant4_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1005 qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1006
986e461dc072 Initial revision
glantau
parents:
diff changeset
1007 for (ch = 0; ch < s->nb_channels; ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1008 int b, c, e, v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1009
986e461dc072 Initial revision
glantau
parents:
diff changeset
1010 for(i=0;i<s->nb_coefs[ch];i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1011 c = mdct_coefs[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1012 e = encoded_exp[ch][i] - global_exp[ch];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1013 b = bap[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1014 switch(b) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1015 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1016 v = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1017 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1018 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1019 v = sym_quant(c, e, 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1020 switch(mant1_cnt) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1021 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1022 qmant1_ptr = &qmant[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1023 v = 9 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1024 mant1_cnt = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1025 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1026 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1027 *qmant1_ptr += 3 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1028 mant1_cnt = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1029 v = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1030 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1031 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1032 *qmant1_ptr += v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1033 mant1_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1034 v = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1035 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1036 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1037 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1038 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1039 v = sym_quant(c, e, 5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1040 switch(mant2_cnt) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1041 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1042 qmant2_ptr = &qmant[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1043 v = 25 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1044 mant2_cnt = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1045 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1046 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1047 *qmant2_ptr += 5 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1048 mant2_cnt = 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1049 v = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1050 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1051 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1052 *qmant2_ptr += v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1053 mant2_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1054 v = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1055 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1056 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1057 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1058 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1059 v = sym_quant(c, e, 7);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1060 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1061 case 4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1062 v = sym_quant(c, e, 11);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1063 switch(mant4_cnt) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1064 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1065 qmant4_ptr = &qmant[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1066 v = 11 * v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1067 mant4_cnt = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1068 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1069 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1070 *qmant4_ptr += v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1071 mant4_cnt = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1072 v = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1073 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1074 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1075 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1076 case 5:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1077 v = sym_quant(c, e, 15);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1078 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1079 case 14:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1080 v = asym_quant(c, e, 14);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1081 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1082 case 15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1083 v = asym_quant(c, e, 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1084 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1085 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1086 v = asym_quant(c, e, b - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1087 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1088 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1089 qmant[ch][i] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1090 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1091 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1092
986e461dc072 Initial revision
glantau
parents:
diff changeset
1093 /* second pass : output the values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1094 for (ch = 0; ch < s->nb_channels; ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1095 int b, q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1096
986e461dc072 Initial revision
glantau
parents:
diff changeset
1097 for(i=0;i<s->nb_coefs[ch];i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1098 q = qmant[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1099 b = bap[ch][i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1100 switch(b) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1101 case 0:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1102 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1103 case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1104 if (q != 128)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1105 put_bits(&s->pb, 5, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1106 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1107 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1108 if (q != 128)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1109 put_bits(&s->pb, 7, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1110 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1111 case 3:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1112 put_bits(&s->pb, 3, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1113 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1114 case 4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1115 if (q != 128)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1116 put_bits(&s->pb, 7, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1117 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1118 case 14:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1119 put_bits(&s->pb, 14, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1120 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1121 case 15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1122 put_bits(&s->pb, 16, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1123 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1124 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1125 put_bits(&s->pb, b - 1, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1126 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1127 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1128 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1129 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1130 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1131
986e461dc072 Initial revision
glantau
parents:
diff changeset
1132 /* compute the ac3 crc */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1133
986e461dc072 Initial revision
glantau
parents:
diff changeset
1134 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
986e461dc072 Initial revision
glantau
parents:
diff changeset
1135
986e461dc072 Initial revision
glantau
parents:
diff changeset
1136 static void ac3_crc_init(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1137 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1138 unsigned int c, n, k;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1139
986e461dc072 Initial revision
glantau
parents:
diff changeset
1140 for(n=0;n<256;n++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1141 c = n << 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1142 for (k = 0; k < 8; k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1143 if (c & (1 << 15))
986e461dc072 Initial revision
glantau
parents:
diff changeset
1144 c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1145 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1146 c = c << 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1147 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1148 crc_table[n] = c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1149 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1150 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1151
986e461dc072 Initial revision
glantau
parents:
diff changeset
1152 static unsigned int ac3_crc(UINT8 *data, int n, unsigned int crc)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1153 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1154 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1155 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1156 crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1157 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1158 return crc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1159 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1160
986e461dc072 Initial revision
glantau
parents:
diff changeset
1161 static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1162 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1163 unsigned int c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1164
986e461dc072 Initial revision
glantau
parents:
diff changeset
1165 c = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1166 while (a) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1167 if (a & 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1168 c ^= b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1169 a = a >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1170 b = b << 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1171 if (b & (1 << 16))
986e461dc072 Initial revision
glantau
parents:
diff changeset
1172 b ^= poly;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1173 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1174 return c;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1175 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1176
986e461dc072 Initial revision
glantau
parents:
diff changeset
1177 static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1178 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1179 unsigned int r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1180 r = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1181 while (n) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1182 if (n & 1)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1183 r = mul_poly(r, a, poly);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1184 a = mul_poly(a, a, poly);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1185 n >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1186 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1187 return r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1188 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1189
986e461dc072 Initial revision
glantau
parents:
diff changeset
1190
986e461dc072 Initial revision
glantau
parents:
diff changeset
1191 /* compute log2(max(abs(tab[]))) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1192 static int log2_tab(INT16 *tab, int n)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1193 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1194 int i, v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1195
986e461dc072 Initial revision
glantau
parents:
diff changeset
1196 v = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1197 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1198 v |= abs(tab[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1199 }
65
bc27d8c5dc2e renamed log2 to av_log2
glantau
parents: 64
diff changeset
1200 return av_log2(v);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1201 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1202
986e461dc072 Initial revision
glantau
parents:
diff changeset
1203 static void lshift_tab(INT16 *tab, int n, int lshift)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1204 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1205 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1206
986e461dc072 Initial revision
glantau
parents:
diff changeset
1207 if (lshift > 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1208 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1209 tab[i] <<= lshift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1210 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1211 } else if (lshift < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1212 lshift = -lshift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1213 for(i=0;i<n;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1214 tab[i] >>= lshift;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1215 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1216 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1217 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1218
986e461dc072 Initial revision
glantau
parents:
diff changeset
1219 /* fill the end of the frame and compute the two crcs */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1220 static int output_frame_end(AC3EncodeContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1221 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1222 int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1223 UINT8 *frame;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1224
986e461dc072 Initial revision
glantau
parents:
diff changeset
1225 frame_size = s->frame_size; /* frame size in words */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1226 /* align to 8 bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1227 flush_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1228 /* add zero bytes to reach the frame size */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1229 frame = s->pb.buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1230 n = 2 * s->frame_size - (s->pb.buf_ptr - frame) - 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1231 assert(n >= 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1232 memset(s->pb.buf_ptr, 0, n);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1233
986e461dc072 Initial revision
glantau
parents:
diff changeset
1234 /* Now we must compute both crcs : this is not so easy for crc1
986e461dc072 Initial revision
glantau
parents:
diff changeset
1235 because it is at the beginning of the data... */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1236 frame_size_58 = (frame_size >> 1) + (frame_size >> 3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1237 crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1238 /* XXX: could precompute crc_inv */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1239 crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1240 crc1 = mul_poly(crc_inv, crc1, CRC16_POLY);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1241 frame[2] = crc1 >> 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1242 frame[3] = crc1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1243
986e461dc072 Initial revision
glantau
parents:
diff changeset
1244 crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1245 frame[2*frame_size - 2] = crc2 >> 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1246 frame[2*frame_size - 1] = crc2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1247
986e461dc072 Initial revision
glantau
parents:
diff changeset
1248 // printf("n=%d frame_size=%d\n", n, frame_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1249 return frame_size * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1250 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1251
986e461dc072 Initial revision
glantau
parents:
diff changeset
1252 int AC3_encode_frame(AVCodecContext *avctx,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1253 unsigned char *frame, int buf_size, void *data)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1254 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1255 AC3EncodeContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1256 short *samples = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1257 int i, j, k, v, ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1258 INT16 input_samples[N];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1259 INT32 mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1260 UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1261 UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1262 UINT8 encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1263 UINT8 bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1264 INT8 exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1265 int frame_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1266
986e461dc072 Initial revision
glantau
parents:
diff changeset
1267 frame_bits = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1268 for(ch=0;ch<s->nb_channels;ch++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1269 /* fixed mdct to the six sub blocks & exponent computation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1270 for(i=0;i<NB_BLOCKS;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1271 INT16 *sptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1272 int sinc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1273
986e461dc072 Initial revision
glantau
parents:
diff changeset
1274 /* compute input samples */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1275 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1276 sinc = s->nb_channels;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1277 sptr = samples + (sinc * (N/2) * i) + ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1278 for(j=0;j<N/2;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1279 v = *sptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1280 input_samples[j + N/2] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1281 s->last_samples[ch][j] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1282 sptr += sinc;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1283 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1284
986e461dc072 Initial revision
glantau
parents:
diff changeset
1285 /* apply the MDCT window */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1286 for(j=0;j<N/2;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1287 input_samples[j] = MUL16(input_samples[j],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1288 ac3_window[j]) >> 15;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1289 input_samples[N-j-1] = MUL16(input_samples[N-j-1],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1290 ac3_window[j]) >> 15;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1291 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1292
986e461dc072 Initial revision
glantau
parents:
diff changeset
1293 /* Normalize the samples to use the maximum available
986e461dc072 Initial revision
glantau
parents:
diff changeset
1294 precision */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1295 v = 14 - log2_tab(input_samples, N);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1296 if (v < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1297 v = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1298 exp_samples[i][ch] = v - 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1299 lshift_tab(input_samples, N, v);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1300
986e461dc072 Initial revision
glantau
parents:
diff changeset
1301 /* do the MDCT */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1302 mdct512(mdct_coef[i][ch], input_samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1303
986e461dc072 Initial revision
glantau
parents:
diff changeset
1304 /* compute "exponents". We take into account the
986e461dc072 Initial revision
glantau
parents:
diff changeset
1305 normalization there */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1306 for(j=0;j<N/2;j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1307 int e;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1308 v = abs(mdct_coef[i][ch][j]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1309 if (v == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1310 e = 24;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1311 else {
65
bc27d8c5dc2e renamed log2 to av_log2
glantau
parents: 64
diff changeset
1312 e = 23 - av_log2(v) + exp_samples[i][ch];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1313 if (e >= 24) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1314 e = 24;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1315 mdct_coef[i][ch][j] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1316 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1317 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1318 exp[i][ch][j] = e;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1319 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1320 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1321
986e461dc072 Initial revision
glantau
parents:
diff changeset
1322 compute_exp_strategy(exp_strategy, exp, ch);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1323
986e461dc072 Initial revision
glantau
parents:
diff changeset
1324 /* compute the exponents as the decoder will see them. The
986e461dc072 Initial revision
glantau
parents:
diff changeset
1325 EXP_REUSE case must be handled carefully : we select the
986e461dc072 Initial revision
glantau
parents:
diff changeset
1326 min of the exponents */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1327 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1328 while (i < NB_BLOCKS) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1329 j = i + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1330 while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1331 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1332 j++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1333 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1334 frame_bits += encode_exp(encoded_exp[i][ch],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1335 exp[i][ch], s->nb_coefs[ch],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1336 exp_strategy[i][ch]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1337 /* copy encoded exponents for reuse case */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1338 for(k=i+1;k<j;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1339 memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1340 s->nb_coefs[ch] * sizeof(UINT8));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1341 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1342 i = j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1343 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1344 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1345
986e461dc072 Initial revision
glantau
parents:
diff changeset
1346 compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1347 /* everything is known... let's output the frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1348 output_frame_header(s, frame);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1349
986e461dc072 Initial revision
glantau
parents:
diff changeset
1350 for(i=0;i<NB_BLOCKS;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1351 output_audio_block(s, exp_strategy[i], encoded_exp[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1352 bap[i], mdct_coef[i], exp_samples[i], i);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1353 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1354 return output_frame_end(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1355 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1356
986e461dc072 Initial revision
glantau
parents:
diff changeset
1357 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1358 /*************************************************************************/
986e461dc072 Initial revision
glantau
parents:
diff changeset
1359 /* TEST */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1360
986e461dc072 Initial revision
glantau
parents:
diff changeset
1361 #define FN (N/4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1362
986e461dc072 Initial revision
glantau
parents:
diff changeset
1363 void fft_test(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1364 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1365 IComplex in[FN], in1[FN];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1366 int k, n, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1367 float sum_re, sum_im, a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1368
986e461dc072 Initial revision
glantau
parents:
diff changeset
1369 /* FFT test */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1370
986e461dc072 Initial revision
glantau
parents:
diff changeset
1371 for(i=0;i<FN;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1372 in[i].re = random() % 65535 - 32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1373 in[i].im = random() % 65535 - 32767;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1374 in1[i] = in[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1375 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1376 fft(in, 7);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1377
986e461dc072 Initial revision
glantau
parents:
diff changeset
1378 /* do it by hand */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1379 for(k=0;k<FN;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1380 sum_re = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1381 sum_im = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1382 for(n=0;n<FN;n++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1383 a = -2 * M_PI * (n * k) / FN;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1384 sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1385 sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1386 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1387 printf("%3d: %6d,%6d %6.0f,%6.0f\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1388 k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1389 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1390 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1391
986e461dc072 Initial revision
glantau
parents:
diff changeset
1392 void mdct_test(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1393 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1394 INT16 input[N];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1395 INT32 output[N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1396 float input1[N];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1397 float output1[N/2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1398 float s, a, err, e, emax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1399 int i, k, n;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1400
986e461dc072 Initial revision
glantau
parents:
diff changeset
1401 for(i=0;i<N;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1402 input[i] = (random() % 65535 - 32767) * 9 / 10;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1403 input1[i] = input[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1404 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1405
986e461dc072 Initial revision
glantau
parents:
diff changeset
1406 mdct512(output, input);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1407
986e461dc072 Initial revision
glantau
parents:
diff changeset
1408 /* do it by hand */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1409 for(k=0;k<N/2;k++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1410 s = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1411 for(n=0;n<N;n++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1412 a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1413 s += input1[n] * cos(a);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1414 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1415 output1[k] = -2 * s / N;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1416 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1417
986e461dc072 Initial revision
glantau
parents:
diff changeset
1418 err = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1419 emax = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1420 for(i=0;i<N/2;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1421 printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1422 e = output[i] - output1[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1423 if (e > emax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1424 emax = e;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1425 err += e * e;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1426 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1427 printf("err2=%f emax=%f\n", err / (N/2), emax);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1428 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1429
986e461dc072 Initial revision
glantau
parents:
diff changeset
1430 void test_ac3(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1431 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1432 AC3EncodeContext ctx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1433 unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1434 short samples[AC3_FRAME_SIZE];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1435 int ret, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1436
986e461dc072 Initial revision
glantau
parents:
diff changeset
1437 AC3_encode_init(&ctx, 44100, 64000, 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1438
986e461dc072 Initial revision
glantau
parents:
diff changeset
1439 fft_test();
986e461dc072 Initial revision
glantau
parents:
diff changeset
1440 mdct_test();
986e461dc072 Initial revision
glantau
parents:
diff changeset
1441
986e461dc072 Initial revision
glantau
parents:
diff changeset
1442 for(i=0;i<AC3_FRAME_SIZE;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1443 samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1444 ret = AC3_encode_frame(&ctx, frame, samples);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1445 printf("ret=%d\n", ret);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1446 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1447 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
1448
986e461dc072 Initial revision
glantau
parents:
diff changeset
1449 AVCodec ac3_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1450 "ac3",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1451 CODEC_TYPE_AUDIO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1452 CODEC_ID_AC3,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1453 sizeof(AC3EncodeContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1454 AC3_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1455 AC3_encode_frame,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1456 NULL,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1457 };