annotate ac3enc.c @ 3662:fc714e9a5419 libavcodec

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