annotate ac3enc.c @ 3153:7aa01243b4d3 libavcodec

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