annotate ac3enc.c @ 4671:97e3364d267a libavcodec

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