## ffmpeg / libavcodec / ac3enc.c @ 5509bffa

History | View | Annotate | Download (42.6 KB)

1 | de6d9b64 | Fabrice Bellard | ```
/*
``` |
---|---|---|---|

2 | ```
* The simplest AC3 encoder
``` |
||

3 | ff4ec49e | Fabrice Bellard | ```
* Copyright (c) 2000 Fabrice Bellard.
``` |

4 | de6d9b64 | Fabrice Bellard | ```
*
``` |

5 | ff4ec49e | Fabrice Bellard | ```
* This library is free software; you can redistribute it and/or
``` |

6 | ```
* modify it under the terms of the GNU Lesser General Public
``` |
||

7 | ```
* License as published by the Free Software Foundation; either
``` |
||

8 | ```
* version 2 of the License, or (at your option) any later version.
``` |
||

9 | de6d9b64 | Fabrice Bellard | ```
*
``` |

10 | ff4ec49e | Fabrice Bellard | ```
* This library is distributed in the hope that it will be useful,
``` |

11 | de6d9b64 | Fabrice Bellard | ```
* but WITHOUT ANY WARRANTY; without even the implied warranty of
``` |

12 | ff4ec49e | Fabrice Bellard | ```
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
``` |

13 | ```
* Lesser General Public License for more details.
``` |
||

14 | de6d9b64 | Fabrice Bellard | ```
*
``` |

15 | ff4ec49e | Fabrice Bellard | ```
* You should have received a copy of the GNU Lesser General Public
``` |

16 | ```
* License along with this library; if not, write to the Free Software
``` |
||

17 | 5509bffa | Diego Biurrun | ```
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
``` |

18 | de6d9b64 | Fabrice Bellard | ```
*/
``` |

19 | 983e3246 | Michael Niedermayer | |

20 | ```
/**
``` |
||

21 | ```
* @file ac3enc.c
``` |
||

22 | ```
* The simplest AC3 encoder.
``` |
||

23 | ```
*/
``` |
||

24 | 1a565432 | Fabrice Bellard | ```
//#define DEBUG
``` |

25 | ```
//#define DEBUG_BITALLOC
``` |
||

26 | de6d9b64 | Fabrice Bellard | #include "avcodec.h" |

27 | caa336b4 | Michael Niedermayer | #include "bitstream.h" |

28 | 6107fa87 | Fabrice Bellard | #include "ac3.h" |

29 | |||

30 | typedef struct AC3EncodeContext { |
||

31 | PutBitContext pb; |
||

32 | ```
int nb_channels;
``` |
||

33 | ```
int nb_all_channels;
``` |
||

34 | ```
int lfe_channel;
``` |
||

35 | ```
int bit_rate;
``` |
||

36 | 5c91a675 | Zdenek Kabelac | unsigned int sample_rate; |

37 | unsigned int bsid; |
||

38 | unsigned int frame_size_min; /* minimum frame size in case rounding is necessary */ |
||

39 | unsigned int frame_size; /* current frame size in words */ |
||

40 | 6107fa87 | Fabrice Bellard | ```
int halfratecod;
``` |

41 | 5c91a675 | Zdenek Kabelac | unsigned int frmsizecod; |

42 | unsigned int fscod; /* frequency */ |
||

43 | unsigned int acmod; |
||

44 | 6107fa87 | Fabrice Bellard | ```
int lfe;
``` |

45 | 5c91a675 | Zdenek Kabelac | unsigned int bsmod; |

46 | 6107fa87 | Fabrice Bellard | short last_samples[AC3_MAX_CHANNELS][256]; |

47 | 5c91a675 | Zdenek Kabelac | unsigned int chbwcod[AC3_MAX_CHANNELS]; |

48 | 6107fa87 | Fabrice Bellard | ```
int nb_coefs[AC3_MAX_CHANNELS];
``` |

49 | 115329f1 | Diego Biurrun | |

50 | 6107fa87 | Fabrice Bellard | ```
/* bitrate allocation control */
``` |

51 | 115329f1 | Diego Biurrun | ```
int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod;
``` |

52 | 6107fa87 | Fabrice Bellard | AC3BitAllocParameters bit_alloc; |

53 | ```
int csnroffst;
``` |
||

54 | ```
int fgaincod[AC3_MAX_CHANNELS];
``` |
||

55 | ```
int fsnroffst[AC3_MAX_CHANNELS];
``` |
||

56 | ```
/* mantissa encoding */
``` |
||

57 | ```
int mant1_cnt, mant2_cnt, mant4_cnt;
``` |
||

58 | } AC3EncodeContext; |
||

59 | de6d9b64 | Fabrice Bellard | |

60 | 6107fa87 | Fabrice Bellard | #include "ac3tab.h" |

61 | de6d9b64 | Fabrice Bellard | |

62 | #define MDCT_NBITS 9 |
||

63 | #define N (1 << MDCT_NBITS) |
||

64 | |||

65 | ```
/* new exponents are sent if their Norm 1 exceed this number */
``` |
||

66 | #define EXP_DIFF_THRESHOLD 1000 |
||

67 | |||

68 | static void fft_init(int ln); |
||

69 | static void ac3_crc_init(void); |
||

70 | |||

71 | 0c1a9eda | Zdenek Kabelac | static inline int16_t fix15(float a) |

72 | de6d9b64 | Fabrice Bellard | { |

73 | ```
int v;
``` |
||

74 | v = (int)(a * (float)(1 << 15)); |
||

75 | if (v < -32767) |
||

76 | ```
v = -32767;
``` |
||

77 | 115329f1 | Diego Biurrun | else if (v > 32767) |

78 | de6d9b64 | Fabrice Bellard | ```
v = 32767;
``` |

79 | ```
return v;
``` |
||

80 | } |
||

81 | |||

82 | static inline int calc_lowcomp1(int a, int b0, int b1) |
||

83 | { |
||

84 | if ((b0 + 256) == b1) { |
||

85 | ```
a = 384 ;
``` |
||

86 | 115329f1 | Diego Biurrun | } else if (b0 > b1) { |

87 | de6d9b64 | Fabrice Bellard | ```
a = a - 64;
``` |

88 | if (a < 0) a=0; |
||

89 | } |
||

90 | ```
return a;
``` |
||

91 | } |
||

92 | |||

93 | static inline int calc_lowcomp(int a, int b0, int b1, int bin) |
||

94 | { |
||

95 | if (bin < 7) { |
||

96 | if ((b0 + 256) == b1) { |
||

97 | ```
a = 384 ;
``` |
||

98 | 115329f1 | Diego Biurrun | } else if (b0 > b1) { |

99 | de6d9b64 | Fabrice Bellard | ```
a = a - 64;
``` |

100 | if (a < 0) a=0; |
||

101 | } |
||

102 | } else if (bin < 20) { |
||

103 | if ((b0 + 256) == b1) { |
||

104 | ```
a = 320 ;
``` |
||

105 | } else if (b0 > b1) { |
||

106 | ```
a= a - 64;
``` |
||

107 | if (a < 0) a=0; |
||

108 | } |
||

109 | ```
} else {
``` |
||

110 | ```
a = a - 128;
``` |
||

111 | if (a < 0) a=0; |
||

112 | } |
||

113 | ```
return a;
``` |
||

114 | } |
||

115 | |||

116 | ```
/* AC3 bit allocation. The algorithm is the one described in the AC3
``` |
||

117 | 6107fa87 | Fabrice Bellard | ```
spec. */
``` |

118 | 0c1a9eda | Zdenek Kabelac | ```
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
``` |

119 | int8_t *exp, int start, int end, |
||

120 | 6107fa87 | Fabrice Bellard | int snroffset, int fgain, int is_lfe, |

121 | 115329f1 | Diego Biurrun | int deltbae,int deltnseg, |

122 | 0c1a9eda | Zdenek Kabelac | uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba) |

123 | de6d9b64 | Fabrice Bellard | { |

124 | ```
int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
``` |
||

125 | ```
int fastleak,slowleak,address,tmp;
``` |
||

126 | 0c1a9eda | Zdenek Kabelac | int16_t psd[256]; /* scaled exponents */ |

127 | int16_t bndpsd[50]; /* interpolated exponents */ |
||

128 | int16_t excite[50]; /* excitation */ |
||

129 | int16_t mask[50]; /* masking value */ |
||

130 | de6d9b64 | Fabrice Bellard | |

131 | ```
/* exponent mapping to PSD */
``` |
||

132 | ```
for(bin=start;bin<end;bin++) {
``` |
||

133 | psd[bin]=(3072 - (exp[bin] << 7)); |
||

134 | } |
||

135 | |||

136 | ```
/* PSD integration */
``` |
||

137 | j=start; |
||

138 | k=masktab[start]; |
||

139 | ```
do {
``` |
||

140 | v=psd[j]; |
||

141 | j++; |
||

142 | ```
end1=bndtab[k+1];
``` |
||

143 | ```
if (end1 > end) end1=end;
``` |
||

144 | ```
for(i=j;i<end1;i++) {
``` |
||

145 | ```
int c,adr;
``` |
||

146 | ```
/* logadd */
``` |
||

147 | v1=psd[j]; |
||

148 | c=v-v1; |
||

149 | if (c >= 0) { |
||

150 | ```
adr=c >> 1;
``` |
||

151 | if (adr > 255) adr=255; |
||

152 | v=v + latab[adr]; |
||

153 | ```
} else {
``` |
||

154 | ```
adr=(-c) >> 1;
``` |
||

155 | if (adr > 255) adr=255; |
||

156 | v=v1 + latab[adr]; |
||

157 | } |
||

158 | j++; |
||

159 | } |
||

160 | bndpsd[k]=v; |
||

161 | k++; |
||

162 | ```
} while (end > bndtab[k]);
``` |
||

163 | |||

164 | ```
/* excitation function */
``` |
||

165 | bndstrt = masktab[start]; |
||

166 | bndend = masktab[end-1] + 1; |
||

167 | 115329f1 | Diego Biurrun | |

168 | 6107fa87 | Fabrice Bellard | if (bndstrt == 0) { |

169 | ```
lowcomp = 0;
``` |
||

170 | lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ; |
||

171 | excite[0] = bndpsd[0] - fgain - lowcomp ; |
||

172 | lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ; |
||

173 | excite[1] = bndpsd[1] - fgain - lowcomp ; |
||

174 | ```
begin = 7 ;
``` |
||

175 | for (bin = 2; bin < 7; bin++) { |
||

176 | if (!(is_lfe && bin == 6)) |
||

177 | ```
lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
``` |
||

178 | fastleak = bndpsd[bin] - fgain ; |
||

179 | slowleak = bndpsd[bin] - s->sgain ; |
||

180 | excite[bin] = fastleak - lowcomp ; |
||

181 | if (!(is_lfe && bin == 6)) { |
||

182 | if (bndpsd[bin] <= bndpsd[bin+1]) { |
||

183 | ```
begin = bin + 1 ;
``` |
||

184 | ```
break ;
``` |
||

185 | } |
||

186 | } |
||

187 | } |
||

188 | 115329f1 | Diego Biurrun | |

189 | 6107fa87 | Fabrice Bellard | end1=bndend; |

190 | if (end1 > 22) end1=22; |
||

191 | 115329f1 | Diego Biurrun | |

192 | 6107fa87 | Fabrice Bellard | ```
for (bin = begin; bin < end1; bin++) {
``` |

193 | if (!(is_lfe && bin == 6)) |
||

194 | ```
lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
``` |
||

195 | 115329f1 | Diego Biurrun | |

196 | 6107fa87 | Fabrice Bellard | fastleak -= s->fdecay ; |

197 | v = bndpsd[bin] - fgain; |
||

198 | ```
if (fastleak < v) fastleak = v;
``` |
||

199 | 115329f1 | Diego Biurrun | |

200 | 6107fa87 | Fabrice Bellard | slowleak -= s->sdecay ; |

201 | v = bndpsd[bin] - s->sgain; |
||

202 | ```
if (slowleak < v) slowleak = v;
``` |
||

203 | 115329f1 | Diego Biurrun | |

204 | 6107fa87 | Fabrice Bellard | v=fastleak - lowcomp; |

205 | ```
if (slowleak > v) v=slowleak;
``` |
||

206 | 115329f1 | Diego Biurrun | |

207 | 6107fa87 | Fabrice Bellard | excite[bin] = v; |

208 | } |
||

209 | ```
begin = 22;
``` |
||

210 | ```
} else {
``` |
||

211 | ```
/* coupling channel */
``` |
||

212 | begin = bndstrt; |
||

213 | 115329f1 | Diego Biurrun | |

214 | 6107fa87 | Fabrice Bellard | fastleak = (s->cplfleak << 8) + 768; |

215 | slowleak = (s->cplsleak << 8) + 768; |
||

216 | de6d9b64 | Fabrice Bellard | } |

217 | |||

218 | 6107fa87 | Fabrice Bellard | ```
for (bin = begin; bin < bndend; bin++) {
``` |

219 | de6d9b64 | Fabrice Bellard | fastleak -= s->fdecay ; |

220 | v = bndpsd[bin] - fgain; |
||

221 | ```
if (fastleak < v) fastleak = v;
``` |
||

222 | slowleak -= s->sdecay ; |
||

223 | v = bndpsd[bin] - s->sgain; |
||

224 | ```
if (slowleak < v) slowleak = v;
``` |
||

225 | |||

226 | v=fastleak; |
||

227 | ```
if (slowleak > v) v = slowleak;
``` |
||

228 | excite[bin] = v; |
||

229 | } |
||

230 | |||

231 | ```
/* compute masking curve */
``` |
||

232 | |||

233 | ```
for (bin = bndstrt; bin < bndend; bin++) {
``` |
||

234 | v1 = excite[bin]; |
||

235 | tmp = s->dbknee - bndpsd[bin]; |
||

236 | if (tmp > 0) { |
||

237 | ```
v1 += tmp >> 2;
``` |
||

238 | } |
||

239 | v=hth[bin >> s->halfratecod][s->fscod]; |
||

240 | ```
if (v1 > v) v=v1;
``` |
||

241 | mask[bin] = v; |
||

242 | } |
||

243 | |||

244 | 6107fa87 | Fabrice Bellard | ```
/* delta bit allocation */
``` |

245 | |||

246 | if (deltbae == 0 || deltbae == 1) { |
||

247 | ```
int band, seg, delta;
``` |
||

248 | ```
band = 0 ;
``` |
||

249 | for (seg = 0; seg < deltnseg; seg++) { |
||

250 | band += deltoffst[seg] ; |
||

251 | if (deltba[seg] >= 4) { |
||

252 | delta = (deltba[seg] - 3) << 7; |
||

253 | ```
} else {
``` |
||

254 | delta = (deltba[seg] - 4) << 7; |
||

255 | } |
||

256 | for (k = 0; k < deltlen[seg]; k++) { |
||

257 | mask[band] += delta ; |
||

258 | band++ ; |
||

259 | } |
||

260 | } |
||

261 | } |
||

262 | |||

263 | de6d9b64 | Fabrice Bellard | ```
/* compute bit allocation */
``` |

264 | 115329f1 | Diego Biurrun | |

265 | de6d9b64 | Fabrice Bellard | i = start ; |

266 | j = masktab[start] ; |
||

267 | ```
do {
``` |
||

268 | v=mask[j]; |
||

269 | v -= snroffset ; |
||

270 | v -= s->floor ; |
||

271 | if (v < 0) v = 0; |
||

272 | ```
v &= 0x1fe0 ;
``` |
||

273 | v += s->floor ; |
||

274 | |||

275 | end1=bndtab[j] + bndsz[j]; |
||

276 | ```
if (end1 > end) end1=end;
``` |
||

277 | |||

278 | ```
for (k = i; k < end1; k++) {
``` |
||

279 | ```
address = (psd[i] - v) >> 5 ;
``` |
||

280 | if (address < 0) address=0; |
||

281 | else if (address > 63) address=63; |
||

282 | bap[i] = baptab[address]; |
||

283 | i++; |
||

284 | } |
||

285 | ```
} while (end > bndtab[j++]) ;
``` |
||

286 | } |
||

287 | |||

288 | typedef struct IComplex { |
||

289 | ```
short re,im;
``` |
||

290 | } IComplex; |
||

291 | |||

292 | static void fft_init(int ln) |
||

293 | { |
||

294 | ```
int i, j, m, n;
``` |
||

295 | ```
float alpha;
``` |
||

296 | |||

297 | ```
n = 1 << ln;
``` |
||

298 | |||

299 | for(i=0;i<(n/2);i++) { |
||

300 | alpha = 2 * M_PI * (float)i / (float)n; |
||

301 | costab[i] = fix15(cos(alpha)); |
||

302 | sintab[i] = fix15(sin(alpha)); |
||

303 | } |
||

304 | |||

305 | for(i=0;i<n;i++) { |
||

306 | ```
m=0;
``` |
||

307 | for(j=0;j<ln;j++) { |
||

308 | m |= ((i >> j) & 1) << (ln-j-1); |
||

309 | } |
||

310 | fft_rev[i]=m; |
||

311 | } |
||

312 | } |
||

313 | |||

314 | ```
/* butter fly op */
``` |
||

315 | ```
#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
``` |
||

316 | {\ |
||

317 | ```
int ax, ay, bx, by;\
``` |
||

318 | bx=pre1;\ |
||

319 | by=pim1;\ |
||

320 | ax=qre1;\ |
||

321 | ay=qim1;\ |
||

322 | ```
pre = (bx + ax) >> 1;\
``` |
||

323 | ```
pim = (by + ay) >> 1;\
``` |
||

324 | ```
qre = (bx - ax) >> 1;\
``` |
||

325 | ```
qim = (by - ay) >> 1;\
``` |
||

326 | } |
||

327 | |||

328 | ```
#define MUL16(a,b) ((a) * (b))
``` |
||

329 | |||

330 | ```
#define CMUL(pre, pim, are, aim, bre, bim) \
``` |
||

331 | {\ |
||

332 | ```
pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\
``` |
||

333 | ```
pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15;\
``` |
||

334 | } |
||

335 | |||

336 | |||

337 | ```
/* do a 2^n point complex fft on 2^ln points. */
``` |
||

338 | static void fft(IComplex *z, int ln) |
||

339 | { |
||

340 | bb270c08 | Diego Biurrun | ```
int j, l, np, np2;
``` |

341 | ```
int nblocks, nloops;
``` |
||

342 | de6d9b64 | Fabrice Bellard | ```
register IComplex *p,*q;
``` |

343 | ```
int tmp_re, tmp_im;
``` |
||

344 | |||

345 | ```
np = 1 << ln;
``` |
||

346 | |||

347 | ```
/* reverse */
``` |
||

348 | for(j=0;j<np;j++) { |
||

349 | ```
int k;
``` |
||

350 | IComplex tmp; |
||

351 | k = fft_rev[j]; |
||

352 | ```
if (k < j) {
``` |
||

353 | tmp = z[k]; |
||

354 | z[k] = z[j]; |
||

355 | z[j] = tmp; |
||

356 | } |
||

357 | } |
||

358 | |||

359 | ```
/* pass 0 */
``` |
||

360 | |||

361 | ```
p=&z[0];
``` |
||

362 | ```
j=(np >> 1);
``` |
||

363 | ```
do {
``` |
||

364 | 115329f1 | Diego Biurrun | BF(p[0].re, p[0].im, p[1].re, p[1].im, |

365 | de6d9b64 | Fabrice Bellard | p[0].re, p[0].im, p[1].re, p[1].im); |

366 | ```
p+=2;
``` |
||

367 | } while (--j != 0); |
||

368 | |||

369 | ```
/* pass 1 */
``` |
||

370 | |||

371 | ```
p=&z[0];
``` |
||

372 | ```
j=np >> 2;
``` |
||

373 | ```
do {
``` |
||

374 | 115329f1 | Diego Biurrun | BF(p[0].re, p[0].im, p[2].re, p[2].im, |

375 | de6d9b64 | Fabrice Bellard | p[0].re, p[0].im, p[2].re, p[2].im); |

376 | 115329f1 | Diego Biurrun | BF(p[1].re, p[1].im, p[3].re, p[3].im, |

377 | de6d9b64 | Fabrice Bellard | p[1].re, p[1].im, p[3].im, -p[3].re); |

378 | ```
p+=4;
``` |
||

379 | } while (--j != 0); |
||

380 | |||

381 | ```
/* pass 2 .. ln-1 */
``` |
||

382 | |||

383 | ```
nblocks = np >> 3;
``` |
||

384 | nloops = 1 << 2; |
||

385 | ```
np2 = np >> 1;
``` |
||

386 | ```
do {
``` |
||

387 | p = z; |
||

388 | q = z + nloops; |
||

389 | for (j = 0; j < nblocks; ++j) { |
||

390 | |||

391 | BF(p->re, p->im, q->re, q->im, |
||

392 | p->re, p->im, q->re, q->im); |
||

393 | 115329f1 | Diego Biurrun | |

394 | de6d9b64 | Fabrice Bellard | p++; |

395 | q++; |
||

396 | ```
for(l = nblocks; l < np2; l += nblocks) {
``` |
||

397 | CMUL(tmp_re, tmp_im, costab[l], -sintab[l], q->re, q->im); |
||

398 | BF(p->re, p->im, q->re, q->im, |
||

399 | p->re, p->im, tmp_re, tmp_im); |
||

400 | p++; |
||

401 | q++; |
||

402 | } |
||

403 | p += nloops; |
||

404 | q += nloops; |
||

405 | } |
||

406 | ```
nblocks = nblocks >> 1;
``` |
||

407 | ```
nloops = nloops << 1;
``` |
||

408 | } while (nblocks != 0); |
||

409 | } |
||

410 | |||

411 | ```
/* do a 512 point mdct */
``` |
||

412 | 0c1a9eda | Zdenek Kabelac | static void mdct512(int32_t *out, int16_t *in) |

413 | de6d9b64 | Fabrice Bellard | { |

414 | ```
int i, re, im, re1, im1;
``` |
||

415 | 115329f1 | Diego Biurrun | int16_t rot[N]; |

416 | de6d9b64 | Fabrice Bellard | ```
IComplex x[N/4];
``` |

417 | |||

418 | ```
/* shift to simplify computations */
``` |
||

419 | for(i=0;i<N/4;i++) |
||

420 | rot[i] = -in[i + 3*N/4]; |
||

421 | for(i=N/4;i<N;i++) |
||

422 | ```
rot[i] = in[i - N/4];
``` |
||

423 | 115329f1 | Diego Biurrun | |

424 | de6d9b64 | Fabrice Bellard | ```
/* pre rotation */
``` |

425 | for(i=0;i<N/4;i++) { |
||

426 | re = ((int)rot[2*i] - (int)rot[N-1-2*i]) >> 1; |
||

427 | im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1; |
||

428 | CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]); |
||

429 | } |
||

430 | |||

431 | ```
fft(x, MDCT_NBITS - 2);
``` |
||

432 | 115329f1 | Diego Biurrun | |

433 | de6d9b64 | Fabrice Bellard | ```
/* post rotation */
``` |

434 | for(i=0;i<N/4;i++) { |
||

435 | re = x[i].re; |
||

436 | im = x[i].im; |
||

437 | CMUL(re1, im1, re, im, xsin1[i], xcos1[i]); |
||

438 | ```
out[2*i] = im1;
``` |
||

439 | out[N/2-1-2*i] = re1; |
||

440 | } |
||

441 | } |
||

442 | |||

443 | ```
/* XXX: use another norm ? */
``` |
||

444 | 0c1a9eda | Zdenek Kabelac | static int calc_exp_diff(uint8_t *exp1, uint8_t *exp2, int n) |

445 | de6d9b64 | Fabrice Bellard | { |

446 | ```
int sum, i;
``` |
||

447 | ```
sum = 0;
``` |
||

448 | for(i=0;i<n;i++) { |
||

449 | sum += abs(exp1[i] - exp2[i]); |
||

450 | } |
||

451 | ```
return sum;
``` |
||

452 | } |
||

453 | |||

454 | 0c1a9eda | Zdenek Kabelac | static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |

455 | ```
uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
``` |
||

456 | 30b68f33 | Zdenek Kabelac | int ch, int is_lfe) |

457 | de6d9b64 | Fabrice Bellard | { |

458 | ```
int i, j;
``` |
||

459 | ```
int exp_diff;
``` |
||

460 | 115329f1 | Diego Biurrun | |

461 | de6d9b64 | Fabrice Bellard | ```
/* estimate if the exponent variation & decide if they should be
``` |

462 | ```
reused in the next frame */
``` |
||

463 | ```
exp_strategy[0][ch] = EXP_NEW;
``` |
||

464 | for(i=1;i<NB_BLOCKS;i++) { |
||

465 | exp_diff = calc_exp_diff(exp[i][ch], exp[i-1][ch], N/2); |
||

466 | 115329f1 | Diego Biurrun | ```
#ifdef DEBUG
``` |

467 | 3d0ef6dd | Michael Niedermayer | av_log(NULL, AV_LOG_DEBUG, "exp_diff=%d\n", exp_diff); |

468 | de6d9b64 | Fabrice Bellard | ```
#endif
``` |

469 | ```
if (exp_diff > EXP_DIFF_THRESHOLD)
``` |
||

470 | exp_strategy[i][ch] = EXP_NEW; |
||

471 | ```
else
``` |
||

472 | exp_strategy[i][ch] = EXP_REUSE; |
||

473 | } |
||

474 | 30b68f33 | Zdenek Kabelac | ```
if (is_lfe)
``` |

475 | bb270c08 | Diego Biurrun | ```
return;
``` |

476 | 30b68f33 | Zdenek Kabelac | |

477 | de6d9b64 | Fabrice Bellard | ```
/* now select the encoding strategy type : if exponents are often
``` |

478 | ```
recoded, we use a coarse encoding */
``` |
||

479 | ```
i = 0;
``` |
||

480 | ```
while (i < NB_BLOCKS) {
``` |
||

481 | ```
j = i + 1;
``` |
||

482 | ```
while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE)
``` |
||

483 | j++; |
||

484 | ```
switch(j - i) {
``` |
||

485 | case 1: |
||

486 | exp_strategy[i][ch] = EXP_D45; |
||

487 | ```
break;
``` |
||

488 | case 2: |
||

489 | case 3: |
||

490 | exp_strategy[i][ch] = EXP_D25; |
||

491 | ```
break;
``` |
||

492 | ```
default:
``` |
||

493 | exp_strategy[i][ch] = EXP_D15; |
||

494 | ```
break;
``` |
||

495 | } |
||

496 | bb270c08 | Diego Biurrun | i = j; |

497 | de6d9b64 | Fabrice Bellard | } |

498 | } |
||

499 | |||

500 | ```
/* set exp[i] to min(exp[i], exp1[i]) */
``` |
||

501 | 0c1a9eda | Zdenek Kabelac | static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) |

502 | de6d9b64 | Fabrice Bellard | { |

503 | ```
int i;
``` |
||

504 | |||

505 | for(i=0;i<n;i++) { |
||

506 | ```
if (exp1[i] < exp[i])
``` |
||

507 | exp[i] = exp1[i]; |
||

508 | } |
||

509 | } |
||

510 | 115329f1 | Diego Biurrun | |

511 | de6d9b64 | Fabrice Bellard | ```
/* update the exponents so that they are the ones the decoder will
``` |

512 | ```
decode. Return the number of bits used to code the exponents */
``` |
||

513 | 115329f1 | Diego Biurrun | static int encode_exp(uint8_t encoded_exp[N/2], |

514 | ```
uint8_t exp[N/2],
``` |
||

515 | de6d9b64 | Fabrice Bellard | ```
int nb_exps,
``` |

516 | ```
int exp_strategy)
``` |
||

517 | { |
||

518 | e44cad52 | Jeff Muizelaar | ```
int group_size, nb_groups, i, j, k, exp_min;
``` |

519 | 0c1a9eda | Zdenek Kabelac | ```
uint8_t exp1[N/2];
``` |

520 | de6d9b64 | Fabrice Bellard | |

521 | ```
switch(exp_strategy) {
``` |
||

522 | ```
case EXP_D15:
``` |
||

523 | ```
group_size = 1;
``` |
||

524 | ```
break;
``` |
||

525 | ```
case EXP_D25:
``` |
||

526 | ```
group_size = 2;
``` |
||

527 | ```
break;
``` |
||

528 | ```
default:
``` |
||

529 | ```
case EXP_D45:
``` |
||

530 | ```
group_size = 4;
``` |
||

531 | ```
break;
``` |
||

532 | } |
||

533 | nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3; |
||

534 | |||

535 | ```
/* for each group, compute the minimum exponent */
``` |
||

536 | exp1[0] = exp[0]; /* DC exponent is handled separately */ |
||

537 | ```
k = 1;
``` |
||

538 | for(i=1;i<=nb_groups;i++) { |
||

539 | exp_min = exp[k]; |
||

540 | assert(exp_min >= 0 && exp_min <= 24); |
||

541 | for(j=1;j<group_size;j++) { |
||

542 | ```
if (exp[k+j] < exp_min)
``` |
||

543 | exp_min = exp[k+j]; |
||

544 | } |
||

545 | exp1[i] = exp_min; |
||

546 | k += group_size; |
||

547 | } |
||

548 | |||

549 | ```
/* constraint for DC exponent */
``` |
||

550 | if (exp1[0] > 15) |
||

551 | exp1[0] = 15; |
||

552 | |||

553 | e44cad52 | Jeff Muizelaar | ```
/* Decrease the delta between each groups to within 2
``` |

554 | ```
* so that they can be differentially encoded */
``` |
||

555 | for (i=1;i<=nb_groups;i++) |
||

556 | bb270c08 | Diego Biurrun | exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2); |

557 | e44cad52 | Jeff Muizelaar | for (i=nb_groups-1;i>=0;i--) |

558 | bb270c08 | Diego Biurrun | exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2); |

559 | e44cad52 | Jeff Muizelaar | |

560 | de6d9b64 | Fabrice Bellard | ```
/* now we have the exponent values the decoder will see */
``` |

561 | encoded_exp[0] = exp1[0]; |
||

562 | ```
k = 1;
``` |
||

563 | for(i=1;i<=nb_groups;i++) { |
||

564 | for(j=0;j<group_size;j++) { |
||

565 | encoded_exp[k+j] = exp1[i]; |
||

566 | } |
||

567 | k += group_size; |
||

568 | } |
||

569 | 115329f1 | Diego Biurrun | |

570 | de6d9b64 | Fabrice Bellard | ```
#if defined(DEBUG)
``` |

571 | 3d0ef6dd | Michael Niedermayer | av_log(NULL, AV_LOG_DEBUG, "exponents: strategy=%d\n", exp_strategy); |

572 | de6d9b64 | Fabrice Bellard | for(i=0;i<=nb_groups * group_size;i++) { |

573 | 3d0ef6dd | Michael Niedermayer | av_log(NULL, AV_LOG_DEBUG, "%d ", encoded_exp[i]); |

574 | de6d9b64 | Fabrice Bellard | } |

575 | 3d0ef6dd | Michael Niedermayer | av_log(NULL, AV_LOG_DEBUG, "\n"); |

576 | de6d9b64 | Fabrice Bellard | ```
#endif
``` |

577 | |||

578 | return 4 + (nb_groups / 3) * 7; |
||

579 | } |
||

580 | |||

581 | ```
/* return the size in bits taken by the mantissa */
``` |
||

582 | 0c1a9eda | Zdenek Kabelac | static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) |

583 | de6d9b64 | Fabrice Bellard | { |

584 | ```
int bits, mant, i;
``` |
||

585 | |||

586 | ```
bits = 0;
``` |
||

587 | for(i=0;i<nb_coefs;i++) { |
||

588 | mant = m[i]; |
||

589 | ```
switch(mant) {
``` |
||

590 | case 0: |
||

591 | ```
/* nothing */
``` |
||

592 | ```
break;
``` |
||

593 | case 1: |
||

594 | ```
/* 3 mantissa in 5 bits */
``` |
||

595 | 115329f1 | Diego Biurrun | if (s->mant1_cnt == 0) |

596 | de6d9b64 | Fabrice Bellard | ```
bits += 5;
``` |

597 | if (++s->mant1_cnt == 3) |
||

598 | ```
s->mant1_cnt = 0;
``` |
||

599 | ```
break;
``` |
||

600 | case 2: |
||

601 | ```
/* 3 mantissa in 7 bits */
``` |
||

602 | 115329f1 | Diego Biurrun | if (s->mant2_cnt == 0) |

603 | de6d9b64 | Fabrice Bellard | ```
bits += 7;
``` |

604 | if (++s->mant2_cnt == 3) |
||

605 | ```
s->mant2_cnt = 0;
``` |
||

606 | ```
break;
``` |
||

607 | case 3: |
||

608 | ```
bits += 3;
``` |
||

609 | ```
break;
``` |
||

610 | case 4: |
||

611 | ```
/* 2 mantissa in 7 bits */
``` |
||

612 | if (s->mant4_cnt == 0) |
||

613 | ```
bits += 7;
``` |
||

614 | 115329f1 | Diego Biurrun | if (++s->mant4_cnt == 2) |

615 | de6d9b64 | Fabrice Bellard | ```
s->mant4_cnt = 0;
``` |

616 | ```
break;
``` |
||

617 | case 14: |
||

618 | ```
bits += 14;
``` |
||

619 | ```
break;
``` |
||

620 | case 15: |
||

621 | ```
bits += 16;
``` |
||

622 | ```
break;
``` |
||

623 | ```
default:
``` |
||

624 | ```
bits += mant - 1;
``` |
||

625 | ```
break;
``` |
||

626 | } |
||

627 | } |
||

628 | ```
return bits;
``` |
||

629 | } |
||

630 | |||

631 | |||

632 | static int bit_alloc(AC3EncodeContext *s, |
||

633 | 0c1a9eda | Zdenek Kabelac | ```
uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
``` |

634 | ```
uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
``` |
||

635 | uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
||

636 | de6d9b64 | Fabrice Bellard | int frame_bits, int csnroffst, int fsnroffst) |

637 | { |
||

638 | ```
int i, ch;
``` |
||

639 | |||

640 | ```
/* compute size */
``` |
||

641 | for(i=0;i<NB_BLOCKS;i++) { |
||

642 | ```
s->mant1_cnt = 0;
``` |
||

643 | ```
s->mant2_cnt = 0;
``` |
||

644 | ```
s->mant4_cnt = 0;
``` |
||

645 | 30b68f33 | Zdenek Kabelac | for(ch=0;ch<s->nb_all_channels;ch++) { |

646 | 115329f1 | Diego Biurrun | ac3_parametric_bit_allocation(&s->bit_alloc, |

647 | bap[i][ch], (int8_t *)encoded_exp[i][ch], |
||

648 | ```
0, s->nb_coefs[ch],
``` |
||

649 | (((csnroffst-15) << 4) + |
||

650 | ```
fsnroffst) << 2,
``` |
||

651 | 6107fa87 | Fabrice Bellard | fgaintab[s->fgaincod[ch]], |

652 | ch == s->lfe_channel, |
||

653 | 2, 0, NULL, NULL, NULL); |
||

654 | 115329f1 | Diego Biurrun | frame_bits += compute_mantissa_size(s, bap[i][ch], |

655 | de6d9b64 | Fabrice Bellard | s->nb_coefs[ch]); |

656 | } |
||

657 | } |
||

658 | ```
#if 0
``` |
||

659 | 115329f1 | Diego Biurrun | ```
printf("csnr=%d fsnr=%d frame_bits=%d diff=%d\n",
``` |

660 | ```
csnroffst, fsnroffst, frame_bits,
``` |
||

661 | de6d9b64 | Fabrice Bellard | ```
16 * s->frame_size - ((frame_bits + 7) & ~7));
``` |

662 | ```
#endif
``` |
||

663 | return 16 * s->frame_size - frame_bits; |
||

664 | } |
||

665 | |||

666 | #define SNR_INC1 4 |
||

667 | |||

668 | static int compute_bit_allocation(AC3EncodeContext *s, |
||

669 | 0c1a9eda | Zdenek Kabelac | ```
uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
``` |

670 | ```
uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
``` |
||

671 | uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], |
||

672 | de6d9b64 | Fabrice Bellard | ```
int frame_bits)
``` |

673 | { |
||

674 | ```
int i, ch;
``` |
||

675 | ```
int csnroffst, fsnroffst;
``` |
||

676 | 0c1a9eda | Zdenek Kabelac | ```
uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
``` |

677 | 30b68f33 | Zdenek Kabelac | static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; |

678 | de6d9b64 | Fabrice Bellard | |

679 | ```
/* init default parameters */
``` |
||

680 | ```
s->sdecaycod = 2;
``` |
||

681 | ```
s->fdecaycod = 1;
``` |
||

682 | ```
s->sgaincod = 1;
``` |
||

683 | ```
s->dbkneecod = 2;
``` |
||

684 | ```
s->floorcod = 4;
``` |
||

685 | 115329f1 | Diego Biurrun | for(ch=0;ch<s->nb_all_channels;ch++) |

686 | de6d9b64 | Fabrice Bellard | ```
s->fgaincod[ch] = 4;
``` |

687 | 115329f1 | Diego Biurrun | |

688 | de6d9b64 | Fabrice Bellard | ```
/* compute real values */
``` |

689 | 6107fa87 | Fabrice Bellard | s->bit_alloc.fscod = s->fscod; |

690 | s->bit_alloc.halfratecod = s->halfratecod; |
||

691 | s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod; |
||

692 | s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod; |
||

693 | s->bit_alloc.sgain = sgaintab[s->sgaincod]; |
||

694 | s->bit_alloc.dbknee = dbkneetab[s->dbkneecod]; |
||

695 | s->bit_alloc.floor = floortab[s->floorcod]; |
||

696 | 115329f1 | Diego Biurrun | |

697 | de6d9b64 | Fabrice Bellard | ```
/* header size */
``` |

698 | ```
frame_bits += 65;
``` |
||

699 | 30b68f33 | Zdenek Kabelac | ```
// if (s->acmod == 2)
``` |

700 | ```
// frame_bits += 2;
``` |
||

701 | frame_bits += frame_bits_inc[s->acmod]; |
||

702 | de6d9b64 | Fabrice Bellard | |

703 | ```
/* audio blocks */
``` |
||

704 | for(i=0;i<NB_BLOCKS;i++) { |
||

705 | 30b68f33 | Zdenek Kabelac | frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */ |

706 | 132041f0 | Justin Ruggles | if (s->acmod == 2) { |

707 | 30b68f33 | Zdenek Kabelac | ```
frame_bits++; /* rematstr */
``` |

708 | 132041f0 | Justin Ruggles | if(i==0) frame_bits += 4; |

709 | } |
||

710 | 30b68f33 | Zdenek Kabelac | frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */ |

711 | bb270c08 | Diego Biurrun | ```
if (s->lfe)
``` |

712 | ```
frame_bits++; /* lfeexpstr */
``` |
||

713 | de6d9b64 | Fabrice Bellard | for(ch=0;ch<s->nb_channels;ch++) { |

714 | ```
if (exp_strategy[i][ch] != EXP_REUSE)
``` |
||

715 | 30b68f33 | Zdenek Kabelac | frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */ |

716 | de6d9b64 | Fabrice Bellard | } |

717 | ```
frame_bits++; /* baie */
``` |
||

718 | ```
frame_bits++; /* snr */
``` |
||

719 | frame_bits += 2; /* delta / skip */ |
||

720 | } |
||

721 | ```
frame_bits++; /* cplinu for block 0 */
``` |
||

722 | ```
/* bit alloc info */
``` |
||

723 | 30b68f33 | Zdenek Kabelac | ```
/* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
``` |

724 | ```
/* csnroffset[6] */
``` |
||

725 | ```
/* (fsnoffset[4] + fgaincod[4]) * c */
``` |
||

726 | frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3); |
||

727 | de6d9b64 | Fabrice Bellard | |

728 | 9ab2717a | Michael Niedermayer | ```
/* auxdatae, crcrsv */
``` |

729 | ```
frame_bits += 2;
``` |
||

730 | |||

731 | de6d9b64 | Fabrice Bellard | ```
/* CRC */
``` |

732 | ```
frame_bits += 16;
``` |
||

733 | |||

734 | ```
/* now the big work begins : do the bit allocation. Modify the snr
``` |
||

735 | ```
offset until we can pack everything in the requested frame size */
``` |
||

736 | |||

737 | csnroffst = s->csnroffst; |
||

738 | 115329f1 | Diego Biurrun | while (csnroffst >= 0 && |

739 | bb270c08 | Diego Biurrun | bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0) |

740 | csnroffst -= SNR_INC1; |
||

741 | de6d9b64 | Fabrice Bellard | if (csnroffst < 0) { |

742 | bb270c08 | Diego Biurrun | av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n"); |

743 | return -1; |
||

744 | de6d9b64 | Fabrice Bellard | } |

745 | 115329f1 | Diego Biurrun | while ((csnroffst + SNR_INC1) <= 63 && |

746 | bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, |
||

747 | de6d9b64 | Fabrice Bellard | csnroffst + SNR_INC1, 0) >= 0) { |

748 | csnroffst += SNR_INC1; |
||

749 | ```
memcpy(bap, bap1, sizeof(bap1));
``` |
||

750 | } |
||

751 | 115329f1 | Diego Biurrun | while ((csnroffst + 1) <= 63 && |

752 | de6d9b64 | Fabrice Bellard | bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) { |

753 | csnroffst++; |
||

754 | ```
memcpy(bap, bap1, sizeof(bap1));
``` |
||

755 | } |
||

756 | |||

757 | ```
fsnroffst = 0;
``` |
||

758 | 115329f1 | Diego Biurrun | while ((fsnroffst + SNR_INC1) <= 15 && |

759 | bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, |
||

760 | de6d9b64 | Fabrice Bellard | ```
csnroffst, fsnroffst + SNR_INC1) >= 0) {
``` |

761 | fsnroffst += SNR_INC1; |
||

762 | ```
memcpy(bap, bap1, sizeof(bap1));
``` |
||

763 | } |
||

764 | 115329f1 | Diego Biurrun | while ((fsnroffst + 1) <= 15 && |

765 | bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, |
||

766 | de6d9b64 | Fabrice Bellard | csnroffst, fsnroffst + 1) >= 0) { |

767 | fsnroffst++; |
||

768 | ```
memcpy(bap, bap1, sizeof(bap1));
``` |
||

769 | } |
||

770 | 115329f1 | Diego Biurrun | |

771 | de6d9b64 | Fabrice Bellard | s->csnroffst = csnroffst; |

772 | 30b68f33 | Zdenek Kabelac | for(ch=0;ch<s->nb_all_channels;ch++) |

773 | de6d9b64 | Fabrice Bellard | s->fsnroffst[ch] = fsnroffst; |

774 | ```
#if defined(DEBUG_BITALLOC)
``` |
||

775 | { |
||

776 | ```
int j;
``` |
||

777 | |||

778 | for(i=0;i<6;i++) { |
||

779 | 30b68f33 | Zdenek Kabelac | for(ch=0;ch<s->nb_all_channels;ch++) { |

780 | de6d9b64 | Fabrice Bellard | ```
printf("Block #%d Ch%d:\n", i, ch);
``` |

781 | ```
printf("bap=");
``` |
||

782 | for(j=0;j<s->nb_coefs[ch];j++) { |
||

783 | ```
printf("%d ",bap[i][ch][j]);
``` |
||

784 | } |
||

785 | ```
printf("\n");
``` |
||

786 | } |
||

787 | } |
||

788 | } |
||

789 | ```
#endif
``` |
||

790 | return 0; |
||

791 | } |
||

792 | |||

793 | 6107fa87 | Fabrice Bellard | void ac3_common_init(void) |

794 | { |
||

795 | ```
int i, j, k, l, v;
``` |
||

796 | ```
/* compute bndtab and masktab from bandsz */
``` |
||

797 | ```
k = 0;
``` |
||

798 | ```
l = 0;
``` |
||

799 | for(i=0;i<50;i++) { |
||

800 | bndtab[i] = l; |
||

801 | v = bndsz[i]; |
||

802 | for(j=0;j<v;j++) masktab[k++]=i; |
||

803 | l += v; |
||

804 | } |
||

805 | bndtab[50] = 0; |
||

806 | } |
||

807 | |||

808 | |||

809 | de6d9b64 | Fabrice Bellard | static int AC3_encode_init(AVCodecContext *avctx) |

810 | { |
||

811 | ```
int freq = avctx->sample_rate;
``` |
||

812 | ```
int bitrate = avctx->bit_rate;
``` |
||

813 | ```
int channels = avctx->channels;
``` |
||

814 | AC3EncodeContext *s = avctx->priv_data; |
||

815 | 6107fa87 | Fabrice Bellard | ```
int i, j, ch;
``` |

816 | de6d9b64 | Fabrice Bellard | ```
float alpha;
``` |

817 | 0c1a9eda | Zdenek Kabelac | static const uint8_t acmod_defs[6] = { |

818 | bb270c08 | Diego Biurrun | 0x01, /* C */ |

819 | 0x02, /* L R */ |
||

820 | 0x03, /* L C R */ |
||

821 | 0x06, /* L R SL SR */ |
||

822 | 0x07, /* L C R SL SR */ |
||

823 | 0x07, /* L C R SL SR (+LFE) */ |
||

824 | 30b68f33 | Zdenek Kabelac | }; |

825 | de6d9b64 | Fabrice Bellard | |

826 | avctx->frame_size = AC3_FRAME_SIZE; |
||

827 | 115329f1 | Diego Biurrun | |

828 | de6d9b64 | Fabrice Bellard | ```
/* number of channels */
``` |

829 | 30b68f33 | Zdenek Kabelac | if (channels < 1 || channels > 6) |

830 | bb270c08 | Diego Biurrun | return -1; |

831 | 30b68f33 | Zdenek Kabelac | ```
s->acmod = acmod_defs[channels - 1];
``` |

832 | s->lfe = (channels == 6) ? 1 : 0; |
||

833 | s->nb_all_channels = channels; |
||

834 | s->nb_channels = channels > 5 ? 5 : channels; |
||

835 | s->lfe_channel = s->lfe ? 5 : -1; |
||

836 | de6d9b64 | Fabrice Bellard | |

837 | ```
/* frequency */
``` |
||

838 | for(i=0;i<3;i++) { |
||

839 | 115329f1 | Diego Biurrun | for(j=0;j<3;j++) |

840 | 6107fa87 | Fabrice Bellard | ```
if ((ac3_freqs[j] >> i) == freq)
``` |

841 | de6d9b64 | Fabrice Bellard | ```
goto found;
``` |

842 | } |
||

843 | return -1; |
||

844 | 115329f1 | Diego Biurrun | ```
found:
``` |

845 | de6d9b64 | Fabrice Bellard | s->sample_rate = freq; |

846 | s->halfratecod = i; |
||

847 | s->fscod = j; |
||

848 | ```
s->bsid = 8 + s->halfratecod;
``` |
||

849 | s->bsmod = 0; /* complete main audio service */ |
||

850 | |||

851 | ```
/* bitrate & frame size */
``` |
||

852 | ```
bitrate /= 1000;
``` |
||

853 | for(i=0;i<19;i++) { |
||

854 | 6107fa87 | Fabrice Bellard | ```
if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate)
``` |

855 | de6d9b64 | Fabrice Bellard | ```
break;
``` |

856 | } |
||

857 | if (i == 19) |
||

858 | return -1; |
||

859 | s->bit_rate = bitrate; |
||

860 | ```
s->frmsizecod = i << 1;
``` |
||

861 | s->frame_size_min = (bitrate * 1000 * AC3_FRAME_SIZE) / (freq * 16); |
||

862 | ```
/* for now we do not handle fractional sizes */
``` |
||

863 | s->frame_size = s->frame_size_min; |
||

864 | 115329f1 | Diego Biurrun | |

865 | de6d9b64 | Fabrice Bellard | ```
/* bit allocation init */
``` |

866 | for(ch=0;ch<s->nb_channels;ch++) { |
||

867 | ```
/* bandwidth for each channel */
``` |
||

868 | ```
/* XXX: should compute the bandwidth according to the frame
``` |
||

869 | ```
size, so that we avoid anoying high freq artefacts */
``` |
||

870 | s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */ |
||

871 | s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37; |
||

872 | } |
||

873 | 30b68f33 | Zdenek Kabelac | ```
if (s->lfe) {
``` |

874 | bb270c08 | Diego Biurrun | s->nb_coefs[s->lfe_channel] = 7; /* fixed */ |

875 | 30b68f33 | Zdenek Kabelac | } |

876 | de6d9b64 | Fabrice Bellard | ```
/* initial snr offset */
``` |

877 | ```
s->csnroffst = 40;
``` |
||

878 | |||

879 | 6107fa87 | Fabrice Bellard | ac3_common_init(); |

880 | de6d9b64 | Fabrice Bellard | |

881 | ```
/* mdct init */
``` |
||

882 | ```
fft_init(MDCT_NBITS - 2);
``` |
||

883 | for(i=0;i<N/4;i++) { |
||

884 | alpha = 2 * M_PI * (i + 1.0 / 8.0) / (float)N; |
||

885 | xcos1[i] = fix15(-cos(alpha)); |
||

886 | xsin1[i] = fix15(-sin(alpha)); |
||

887 | } |
||

888 | |||

889 | ac3_crc_init(); |
||

890 | 115329f1 | Diego Biurrun | |

891 | 492cd3a9 | Michael Niedermayer | avctx->coded_frame= avcodec_alloc_frame(); |

892 | ```
avctx->coded_frame->key_frame= 1;
``` |
||

893 | de6d9b64 | Fabrice Bellard | |

894 | return 0; |
||

895 | } |
||

896 | |||

897 | ```
/* output the AC3 frame header */
``` |
||

898 | static void output_frame_header(AC3EncodeContext *s, unsigned char *frame) |
||

899 | { |
||

900 | ed7debda | Alex Beregszaszi | init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE); |

901 | de6d9b64 | Fabrice Bellard | |

902 | put_bits(&s->pb, 16, 0x0b77); /* frame header */ |
||

903 | put_bits(&s->pb, 16, 0); /* crc1: will be filled later */ |
||

904 | ```
put_bits(&s->pb, 2, s->fscod);
``` |
||

905 | ```
put_bits(&s->pb, 6, s->frmsizecod + (s->frame_size - s->frame_size_min));
``` |
||

906 | ```
put_bits(&s->pb, 5, s->bsid);
``` |
||

907 | ```
put_bits(&s->pb, 3, s->bsmod);
``` |
||

908 | ```
put_bits(&s->pb, 3, s->acmod);
``` |
||

909 | 30b68f33 | Zdenek Kabelac | if ((s->acmod & 0x01) && s->acmod != 0x01) |

910 | bb270c08 | Diego Biurrun | put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */ |

911 | 30b68f33 | Zdenek Kabelac | if (s->acmod & 0x04) |

912 | bb270c08 | Diego Biurrun | put_bits(&s->pb, 2, 1); /* XXX -6 dB */ |

913 | 30b68f33 | Zdenek Kabelac | if (s->acmod == 0x02) |

914 | de6d9b64 | Fabrice Bellard | put_bits(&s->pb, 2, 0); /* surround not indicated */ |

915 | 30b68f33 | Zdenek Kabelac | put_bits(&s->pb, 1, s->lfe); /* LFE */ |

916 | de6d9b64 | Fabrice Bellard | put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */ |

917 | put_bits(&s->pb, 1, 0); /* no compression control word */ |
||

918 | put_bits(&s->pb, 1, 0); /* no lang code */ |
||

919 | put_bits(&s->pb, 1, 0); /* no audio production info */ |
||

920 | put_bits(&s->pb, 1, 0); /* no copyright */ |
||

921 | put_bits(&s->pb, 1, 1); /* original bitstream */ |
||

922 | put_bits(&s->pb, 1, 0); /* no time code 1 */ |
||

923 | put_bits(&s->pb, 1, 0); /* no time code 2 */ |
||

924 | put_bits(&s->pb, 1, 0); /* no addtional bit stream info */ |
||

925 | } |
||

926 | |||

927 | ```
/* symetric quantization on 'levels' levels */
``` |
||

928 | static inline int sym_quant(int c, int e, int levels) |
||

929 | { |
||

930 | ```
int v;
``` |
||

931 | |||

932 | if (c >= 0) { |
||

933 | 8d67072f | Fabrice Bellard | ```
v = (levels * (c << e)) >> 24;
``` |

934 | v = (v + 1) >> 1; |
||

935 | de6d9b64 | Fabrice Bellard | ```
v = (levels >> 1) + v;
``` |

936 | ```
} else {
``` |
||

937 | 8d67072f | Fabrice Bellard | ```
v = (levels * ((-c) << e)) >> 24;
``` |

938 | v = (v + 1) >> 1; |
||

939 | de6d9b64 | Fabrice Bellard | ```
v = (levels >> 1) - v;
``` |

940 | } |
||

941 | ```
assert (v >= 0 && v < levels);
``` |
||

942 | ```
return v;
``` |
||

943 | } |
||

944 | |||

945 | ```
/* asymetric quantization on 2^qbits levels */
``` |
||

946 | static inline int asym_quant(int c, int e, int qbits) |
||

947 | { |
||

948 | ```
int lshift, m, v;
``` |
||

949 | |||

950 | ```
lshift = e + qbits - 24;
``` |
||

951 | if (lshift >= 0) |
||

952 | v = c << lshift; |
||

953 | ```
else
``` |
||

954 | v = c >> (-lshift); |
||

955 | ```
/* rounding */
``` |
||

956 | v = (v + 1) >> 1; |
||

957 | m = (1 << (qbits-1)); |
||

958 | ```
if (v >= m)
``` |
||

959 | ```
v = m - 1;
``` |
||

960 | assert(v >= -m); |
||

961 | return v & ((1 << qbits)-1); |
||

962 | } |
||

963 | |||

964 | ```
/* Output one audio block. There are NB_BLOCKS audio blocks in one AC3
``` |
||

965 | ```
frame */
``` |
||

966 | static void output_audio_block(AC3EncodeContext *s, |
||

967 | 0c1a9eda | Zdenek Kabelac | uint8_t exp_strategy[AC3_MAX_CHANNELS], |

968 | ```
uint8_t encoded_exp[AC3_MAX_CHANNELS][N/2],
``` |
||

969 | ```
uint8_t bap[AC3_MAX_CHANNELS][N/2],
``` |
||

970 | ```
int32_t mdct_coefs[AC3_MAX_CHANNELS][N/2],
``` |
||

971 | int8_t global_exp[AC3_MAX_CHANNELS], |
||

972 | de6d9b64 | Fabrice Bellard | ```
int block_num)
``` |

973 | { |
||

974 | 743739d2 | Michael Niedermayer | ```
int ch, nb_groups, group_size, i, baie, rbnd;
``` |

975 | 0c1a9eda | Zdenek Kabelac | uint8_t *p; |

976 | ```
uint16_t qmant[AC3_MAX_CHANNELS][N/2];
``` |
||

977 | de6d9b64 | Fabrice Bellard | ```
int exp0, exp1;
``` |

978 | ```
int mant1_cnt, mant2_cnt, mant4_cnt;
``` |
||

979 | 0c1a9eda | Zdenek Kabelac | uint16_t *qmant1_ptr, *qmant2_ptr, *qmant4_ptr; |

980 | de6d9b64 | Fabrice Bellard | ```
int delta0, delta1, delta2;
``` |

981 | |||

982 | 115329f1 | Diego Biurrun | for(ch=0;ch<s->nb_channels;ch++) |

983 | de6d9b64 | Fabrice Bellard | put_bits(&s->pb, 1, 0); /* 512 point MDCT */ |

984 | 115329f1 | Diego Biurrun | for(ch=0;ch<s->nb_channels;ch++) |

985 | de6d9b64 | Fabrice Bellard | put_bits(&s->pb, 1, 1); /* no dither */ |

986 | put_bits(&s->pb, 1, 0); /* no dynamic range */ |
||

987 | if (block_num == 0) { |
||

988 | ```
/* for block 0, even if no coupling, we must say it. This is a
``` |
||

989 | ```
waste of bit :-) */
``` |
||

990 | put_bits(&s->pb, 1, 1); /* coupling strategy present */ |
||

991 | put_bits(&s->pb, 1, 0); /* no coupling strategy */ |
||

992 | ```
} else {
``` |
||

993 | put_bits(&s->pb, 1, 0); /* no new coupling strategy */ |
||

994 | } |
||

995 | |||

996 | 743739d2 | Michael Niedermayer | if (s->acmod == 2) |

997 | { |
||

998 | bb270c08 | Diego Biurrun | if(block_num==0) |

999 | { |
||

1000 | ```
/* first block must define rematrixing (rematstr) */
``` |
||

1001 | put_bits(&s->pb, 1, 1); |
||

1002 | |||

1003 | ```
/* dummy rematrixing rematflg(1:4)=0 */
``` |
||

1004 | for (rbnd=0;rbnd<4;rbnd++) |
||

1005 | put_bits(&s->pb, 1, 0); |
||

1006 | } |
||

1007 | ```
else
``` |
||

1008 | { |
||

1009 | ```
/* no matrixing (but should be used in the future) */
``` |
||

1010 | put_bits(&s->pb, 1, 0); |
||

1011 | } |
||

1012 | 743739d2 | Michael Niedermayer | } |

1013 | de6d9b64 | Fabrice Bellard | |

1014 | 115329f1 | Diego Biurrun | ```
#if defined(DEBUG)
``` |

1015 | de6d9b64 | Fabrice Bellard | { |

1016 | 743739d2 | Michael Niedermayer | static int count = 0; |

1017 | 3d0ef6dd | Michael Niedermayer | av_log(NULL, AV_LOG_DEBUG, "Block #%d (%d)\n", block_num, count++); |

1018 | de6d9b64 | Fabrice Bellard | } |

1019 | ```
#endif
``` |
||

1020 | ```
/* exponent strategy */
``` |
||

1021 | for(ch=0;ch<s->nb_channels;ch++) { |
||

1022 | ```
put_bits(&s->pb, 2, exp_strategy[ch]);
``` |
||

1023 | } |
||

1024 | 115329f1 | Diego Biurrun | |

1025 | 30b68f33 | Zdenek Kabelac | ```
if (s->lfe) {
``` |

1026 | bb270c08 | Diego Biurrun | ```
put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
``` |

1027 | 30b68f33 | Zdenek Kabelac | } |

1028 | |||

1029 | de6d9b64 | Fabrice Bellard | for(ch=0;ch<s->nb_channels;ch++) { |

1030 | ```
if (exp_strategy[ch] != EXP_REUSE)
``` |
||

1031 | ```
put_bits(&s->pb, 6, s->chbwcod[ch]);
``` |
||

1032 | } |
||

1033 | 115329f1 | Diego Biurrun | |

1034 | de6d9b64 | Fabrice Bellard | ```
/* exponents */
``` |

1035 | 30b68f33 | Zdenek Kabelac | for (ch = 0; ch < s->nb_all_channels; ch++) { |

1036 | de6d9b64 | Fabrice Bellard | ```
switch(exp_strategy[ch]) {
``` |

1037 | ```
case EXP_REUSE:
``` |
||

1038 | ```
continue;
``` |
||

1039 | ```
case EXP_D15:
``` |
||

1040 | ```
group_size = 1;
``` |
||

1041 | ```
break;
``` |
||

1042 | ```
case EXP_D25:
``` |
||

1043 | ```
group_size = 2;
``` |
||

1044 | ```
break;
``` |
||

1045 | ```
default:
``` |
||

1046 | ```
case EXP_D45:
``` |
||

1047 | ```
group_size = 4;
``` |
||

1048 | ```
break;
``` |
||

1049 | } |
||

1050 | bb270c08 | Diego Biurrun | nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size); |

1051 | de6d9b64 | Fabrice Bellard | p = encoded_exp[ch]; |

1052 | |||

1053 | ```
/* first exponent */
``` |
||

1054 | exp1 = *p++; |
||

1055 | ```
put_bits(&s->pb, 4, exp1);
``` |
||

1056 | |||

1057 | ```
/* next ones are delta encoded */
``` |
||

1058 | for(i=0;i<nb_groups;i++) { |
||

1059 | ```
/* merge three delta in one code */
``` |
||

1060 | exp0 = exp1; |
||

1061 | ```
exp1 = p[0];
``` |
||

1062 | p += group_size; |
||

1063 | ```
delta0 = exp1 - exp0 + 2;
``` |
||

1064 | |||

1065 | exp0 = exp1; |
||

1066 | ```
exp1 = p[0];
``` |
||

1067 | p += group_size; |
||

1068 | ```
delta1 = exp1 - exp0 + 2;
``` |
||

1069 | |||

1070 | exp0 = exp1; |
||

1071 | ```
exp1 = p[0];
``` |
||

1072 | p += group_size; |
||

1073 | ```
delta2 = exp1 - exp0 + 2;
``` |
||

1074 | |||

1075 | put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2); |
||

1076 | } |
||

1077 | |||

1078 | bb270c08 | Diego Biurrun | ```
if (ch != s->lfe_channel)
``` |

1079 | put_bits(&s->pb, 2, 0); /* no gain range info */ |
||

1080 | de6d9b64 | Fabrice Bellard | } |

1081 | |||

1082 | ```
/* bit allocation info */
``` |
||

1083 | ```
baie = (block_num == 0);
``` |
||

1084 | ```
put_bits(&s->pb, 1, baie);
``` |
||

1085 | ```
if (baie) {
``` |
||

1086 | ```
put_bits(&s->pb, 2, s->sdecaycod);
``` |
||

1087 | ```
put_bits(&s->pb, 2, s->fdecaycod);
``` |
||

1088 | ```
put_bits(&s->pb, 2, s->sgaincod);
``` |
||

1089 | ```
put_bits(&s->pb, 2, s->dbkneecod);
``` |
||

1090 | ```
put_bits(&s->pb, 3, s->floorcod);
``` |
||

1091 | } |
||

1092 | |||

1093 | ```
/* snr offset */
``` |
||

1094 | put_bits(&s->pb, 1, baie); /* always present with bai */ |
||

1095 | ```
if (baie) {
``` |
||

1096 | ```
put_bits(&s->pb, 6, s->csnroffst);
``` |
||

1097 | 30b68f33 | Zdenek Kabelac | for(ch=0;ch<s->nb_all_channels;ch++) { |

1098 | de6d9b64 | Fabrice Bellard | ```
put_bits(&s->pb, 4, s->fsnroffst[ch]);
``` |

1099 | ```
put_bits(&s->pb, 3, s->fgaincod[ch]);
``` |
||

1100 | } |
||

1101 | } |
||

1102 | 115329f1 | Diego Biurrun | |

1103 | de6d9b64 | Fabrice Bellard | put_bits(&s->pb, 1, 0); /* no delta bit allocation */ |

1104 | put_bits(&s->pb, 1, 0); /* no data to skip */ |
||

1105 | |||

1106 | ```
/* mantissa encoding : we use two passes to handle the grouping. A
``` |
||

1107 | ```
one pass method may be faster, but it would necessitate to
``` |
||

1108 | ```
modify the output stream. */
``` |
||

1109 | |||

1110 | ```
/* first pass: quantize */
``` |
||

1111 | ```
mant1_cnt = mant2_cnt = mant4_cnt = 0;
``` |
||

1112 | ```
qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
``` |
||

1113 | |||

1114 | 30b68f33 | Zdenek Kabelac | for (ch = 0; ch < s->nb_all_channels; ch++) { |

1115 | de6d9b64 | Fabrice Bellard | ```
int b, c, e, v;
``` |

1116 | |||

1117 | for(i=0;i<s->nb_coefs[ch];i++) { |
||

1118 | c = mdct_coefs[ch][i]; |
||

1119 | e = encoded_exp[ch][i] - global_exp[ch]; |
||

1120 | b = bap[ch][i]; |
||

1121 | ```
switch(b) {
``` |
||

1122 | case 0: |
||

1123 | ```
v = 0;
``` |
||

1124 | ```
break;
``` |
||

1125 | case 1: |
||

1126 | ```
v = sym_quant(c, e, 3);
``` |
||

1127 | ```
switch(mant1_cnt) {
``` |
||

1128 | case 0: |
||

1129 | qmant1_ptr = &qmant[ch][i]; |
||

1130 | ```
v = 9 * v;
``` |
||

1131 | ```
mant1_cnt = 1;
``` |
||

1132 | ```
break;
``` |
||

1133 | case 1: |
||

1134 | ```
*qmant1_ptr += 3 * v;
``` |
||

1135 | ```
mant1_cnt = 2;
``` |
||

1136 | ```
v = 128;
``` |
||

1137 | ```
break;
``` |
||

1138 | ```
default:
``` |
||

1139 | *qmant1_ptr += v; |
||

1140 | ```
mant1_cnt = 0;
``` |
||

1141 | ```
v = 128;
``` |
||

1142 | ```
break;
``` |
||

1143 | } |
||

1144 | ```
break;
``` |
||

1145 | case 2: |
||

1146 | ```
v = sym_quant(c, e, 5);
``` |
||

1147 | ```
switch(mant2_cnt) {
``` |
||

1148 | case 0: |
||

1149 | qmant2_ptr = &qmant[ch][i]; |
||

1150 | ```
v = 25 * v;
``` |
||

1151 | ```
mant2_cnt = 1;
``` |
||

1152 | ```
break;
``` |
||

1153 | case 1: |
||

1154 | ```
*qmant2_ptr += 5 * v;
``` |
||

1155 | ```
mant2_cnt = 2;
``` |
||

1156 | ```
v = 128;
``` |
||

1157 | ```
break;
``` |
||

1158 | ```
default:
``` |
||

1159 | *qmant2_ptr += v; |
||

1160 | ```
mant2_cnt = 0;
``` |
||

1161 | ```
v = 128;
``` |
||

1162 | ```
break;
``` |
||

1163 | } |
||

1164 | ```
break;
``` |
||

1165 | case 3: |
||

1166 | ```
v = sym_quant(c, e, 7);
``` |
||

1167 | ```
break;
``` |
||

1168 | case 4: |
||

1169 | ```
v = sym_quant(c, e, 11);
``` |
||

1170 | ```
switch(mant4_cnt) {
``` |
||

1171 | case 0: |
||

1172 | qmant4_ptr = &qmant[ch][i]; |
||

1173 | ```
v = 11 * v;
``` |
||

1174 | ```
mant4_cnt = 1;
``` |
||

1175 | ```
break;
``` |
||

1176 | ```
default:
``` |
||

1177 | *qmant4_ptr += v; |
||

1178 | ```
mant4_cnt = 0;
``` |
||

1179 | ```
v = 128;
``` |
||

1180 | ```
break;
``` |
||

1181 | } |
||

1182 | ```
break;
``` |
||

1183 | case 5: |
||

1184 | ```
v = sym_quant(c, e, 15);
``` |
||

1185 | ```
break;
``` |
||

1186 | case 14: |
||

1187 | ```
v = asym_quant(c, e, 14);
``` |
||

1188 | ```
break;
``` |
||

1189 | case 15: |
||

1190 | ```
v = asym_quant(c, e, 16);
``` |
||

1191 | ```
break;
``` |
||

1192 | ```
default:
``` |
||

1193 | ```
v = asym_quant(c, e, b - 1);
``` |
||

1194 | ```
break;
``` |
||

1195 | } |
||

1196 | qmant[ch][i] = v; |
||

1197 | } |
||

1198 | } |
||

1199 | |||

1200 | ```
/* second pass : output the values */
``` |
||

1201 | 30b68f33 | Zdenek Kabelac | for (ch = 0; ch < s->nb_all_channels; ch++) { |

1202 | de6d9b64 | Fabrice Bellard | ```
int b, q;
``` |

1203 | 115329f1 | Diego Biurrun | |

1204 | de6d9b64 | Fabrice Bellard | for(i=0;i<s->nb_coefs[ch];i++) { |

1205 | q = qmant[ch][i]; |
||

1206 | b = bap[ch][i]; |
||

1207 | ```
switch(b) {
``` |
||

1208 | case 0: |
||

1209 | ```
break;
``` |
||

1210 | case 1: |
||

1211 | 115329f1 | Diego Biurrun | if (q != 128) |

1212 | de6d9b64 | Fabrice Bellard | ```
put_bits(&s->pb, 5, q);
``` |

1213 | ```
break;
``` |
||

1214 | case 2: |
||

1215 | 115329f1 | Diego Biurrun | if (q != 128) |

1216 | de6d9b64 | Fabrice Bellard | ```
put_bits(&s->pb, 7, q);
``` |

1217 | ```
break;
``` |
||

1218 | case 3: |
||

1219 | ```
put_bits(&s->pb, 3, q);
``` |
||

1220 | ```
break;
``` |
||

1221 | case 4: |
||

1222 | if (q != 128) |
||

1223 | ```
put_bits(&s->pb, 7, q);
``` |
||

1224 | ```
break;
``` |
||

1225 | case 14: |
||

1226 | ```
put_bits(&s->pb, 14, q);
``` |
||

1227 | ```
break;
``` |
||

1228 | case 15: |
||

1229 | ```
put_bits(&s->pb, 16, q);
``` |
||

1230 | ```
break;
``` |
||

1231 | ```
default:
``` |
||

1232 | ```
put_bits(&s->pb, b - 1, q);
``` |
||

1233 | ```
break;
``` |
||

1234 | } |
||

1235 | } |
||

1236 | } |
||

1237 | } |
||

1238 | |||

1239 | ```
/* compute the ac3 crc */
``` |
||

1240 | |||

1241 | #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16)) |
||

1242 | |||

1243 | static void ac3_crc_init(void) |
||

1244 | { |
||

1245 | unsigned int c, n, k; |
||

1246 | |||

1247 | for(n=0;n<256;n++) { |
||

1248 | ```
c = n << 8;
``` |
||

1249 | for (k = 0; k < 8; k++) { |
||

1250 | 115329f1 | Diego Biurrun | if (c & (1 << 15)) |

1251 | de6d9b64 | Fabrice Bellard | c = ((c << 1) & 0xffff) ^ (CRC16_POLY & 0xffff); |

1252 | ```
else
``` |
||

1253 | ```
c = c << 1;
``` |
||

1254 | } |
||

1255 | crc_table[n] = c; |
||

1256 | } |
||

1257 | } |
||

1258 | |||

1259 | 0c1a9eda | Zdenek Kabelac | static unsigned int ac3_crc(uint8_t *data, int n, unsigned int crc) |

1260 | de6d9b64 | Fabrice Bellard | { |

1261 | ```
int i;
``` |
||

1262 | for(i=0;i<n;i++) { |
||

1263 | crc = (crc_table[data[i] ^ (crc >> 8)] ^ (crc << 8)) & 0xffff; |
||

1264 | } |
||

1265 | ```
return crc;
``` |
||

1266 | } |
||

1267 | |||

1268 | static unsigned int mul_poly(unsigned int a, unsigned int b, unsigned int poly) |
||

1269 | { |
||

1270 | unsigned int c; |
||

1271 | |||

1272 | ```
c = 0;
``` |
||

1273 | ```
while (a) {
``` |
||

1274 | if (a & 1) |
||

1275 | c ^= b; |
||

1276 | ```
a = a >> 1;
``` |
||

1277 | ```
b = b << 1;
``` |
||

1278 | if (b & (1 << 16)) |
||

1279 | b ^= poly; |
||

1280 | } |
||

1281 | ```
return c;
``` |
||

1282 | } |
||

1283 | |||

1284 | static unsigned int pow_poly(unsigned int a, unsigned int n, unsigned int poly) |
||

1285 | { |
||

1286 | unsigned int r; |
||

1287 | ```
r = 1;
``` |
||

1288 | ```
while (n) {
``` |
||

1289 | if (n & 1) |
||

1290 | r = mul_poly(r, a, poly); |
||

1291 | a = mul_poly(a, a, poly); |
||

1292 | ```
n >>= 1;
``` |
||

1293 | } |
||

1294 | ```
return r;
``` |
||

1295 | } |
||

1296 | |||

1297 | |||

1298 | ```
/* compute log2(max(abs(tab[]))) */
``` |
||

1299 | 0c1a9eda | Zdenek Kabelac | static int log2_tab(int16_t *tab, int n) |

1300 | de6d9b64 | Fabrice Bellard | { |

1301 | ```
int i, v;
``` |
||

1302 | |||

1303 | ```
v = 0;
``` |
||

1304 | for(i=0;i<n;i++) { |
||

1305 | v |= abs(tab[i]); |
||

1306 | } |
||

1307 | 34763c15 | Fabrice Bellard | ```
return av_log2(v);
``` |

1308 | de6d9b64 | Fabrice Bellard | } |

1309 | |||

1310 | 0c1a9eda | Zdenek Kabelac | static void lshift_tab(int16_t *tab, int n, int lshift) |

1311 | de6d9b64 | Fabrice Bellard | { |

1312 | ```
int i;
``` |
||

1313 | |||

1314 | if (lshift > 0) { |
||

1315 | for(i=0;i<n;i++) { |
||

1316 | tab[i] <<= lshift; |
||

1317 | } |
||

1318 | } else if (lshift < 0) { |
||

1319 | lshift = -lshift; |
||

1320 | for(i=0;i<n;i++) { |
||

1321 | tab[i] >>= lshift; |
||

1322 | } |
||

1323 | } |
||

1324 | } |
||

1325 | |||

1326 | ```
/* fill the end of the frame and compute the two crcs */
``` |
||

1327 | static int output_frame_end(AC3EncodeContext *s) |
||

1328 | { |
||

1329 | ```
int frame_size, frame_size_58, n, crc1, crc2, crc_inv;
``` |
||

1330 | 0c1a9eda | Zdenek Kabelac | uint8_t *frame; |

1331 | de6d9b64 | Fabrice Bellard | |

1332 | ```
frame_size = s->frame_size; /* frame size in words */
``` |
||

1333 | ```
/* align to 8 bits */
``` |
||

1334 | flush_put_bits(&s->pb); |
||

1335 | ```
/* add zero bytes to reach the frame size */
``` |
||

1336 | frame = s->pb.buf; |
||

1337 | 17592475 | Michael Niedermayer | n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2; |

1338 | de6d9b64 | Fabrice Bellard | ```
assert(n >= 0);
``` |

1339 | 743739d2 | Michael Niedermayer | if(n>0) |

1340 | ```
memset(pbBufPtr(&s->pb), 0, n);
``` |
||

1341 | 115329f1 | Diego Biurrun | |

1342 | de6d9b64 | Fabrice Bellard | ```
/* Now we must compute both crcs : this is not so easy for crc1
``` |

1343 | ```
because it is at the beginning of the data... */
``` |
||

1344 | frame_size_58 = (frame_size >> 1) + (frame_size >> 3); |
||

1345 | crc1 = ac3_crc(frame + 4, (2 * frame_size_58) - 4, 0); |
||

1346 | ```
/* XXX: could precompute crc_inv */
``` |
||

1347 | crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY); |
||

1348 | crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); |
||

1349 | frame[2] = crc1 >> 8; |
||

1350 | ```
frame[3] = crc1;
``` |
||

1351 | 115329f1 | Diego Biurrun | |

1352 | de6d9b64 | Fabrice Bellard | crc2 = ac3_crc(frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2, 0); |

1353 | frame[2*frame_size - 2] = crc2 >> 8; |
||

1354 | frame[2*frame_size - 1] = crc2; |
||

1355 | |||

1356 | ```
// printf("n=%d frame_size=%d\n", n, frame_size);
``` |
||

1357 | return frame_size * 2; |
||

1358 | } |
||

1359 | |||

1360 | 6107fa87 | Fabrice Bellard | static int AC3_encode_frame(AVCodecContext *avctx, |

1361 | unsigned char *frame, int buf_size, void *data) |
||

1362 | de6d9b64 | Fabrice Bellard | { |

1363 | AC3EncodeContext *s = avctx->priv_data; |
||

1364 | 21d7e2ff | Jeff Muizelaar | int16_t *samples = data; |

1365 | de6d9b64 | Fabrice Bellard | ```
int i, j, k, v, ch;
``` |

1366 | 0c1a9eda | Zdenek Kabelac | int16_t input_samples[N]; |

1367 | ```
int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
``` |
||

1368 | ```
uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
``` |
||

1369 | uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; |
||

1370 | ```
uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
``` |
||

1371 | ```
uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
``` |
||

1372 | int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; |
||

1373 | de6d9b64 | Fabrice Bellard | ```
int frame_bits;
``` |

1374 | |||

1375 | ```
frame_bits = 0;
``` |
||

1376 | 30b68f33 | Zdenek Kabelac | for(ch=0;ch<s->nb_all_channels;ch++) { |

1377 | de6d9b64 | Fabrice Bellard | ```
/* fixed mdct to the six sub blocks & exponent computation */
``` |

1378 | for(i=0;i<NB_BLOCKS;i++) { |
||

1379 | 0c1a9eda | Zdenek Kabelac | int16_t *sptr; |

1380 | de6d9b64 | Fabrice Bellard | ```
int sinc;
``` |

1381 | |||

1382 | ```
/* compute input samples */
``` |
||

1383 | 0c1a9eda | Zdenek Kabelac | memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t)); |

1384 | 30b68f33 | Zdenek Kabelac | sinc = s->nb_all_channels; |

1385 | de6d9b64 | Fabrice Bellard | ```
sptr = samples + (sinc * (N/2) * i) + ch;
``` |

1386 | for(j=0;j<N/2;j++) { |
||

1387 | v = *sptr; |
||

1388 | ```
input_samples[j + N/2] = v;
``` |
||

1389 | 115329f1 | Diego Biurrun | s->last_samples[ch][j] = v; |

1390 | de6d9b64 | Fabrice Bellard | sptr += sinc; |

1391 | } |
||

1392 | |||

1393 | ```
/* apply the MDCT window */
``` |
||

1394 | for(j=0;j<N/2;j++) { |
||

1395 | 115329f1 | Diego Biurrun | input_samples[j] = MUL16(input_samples[j], |

1396 | de6d9b64 | Fabrice Bellard | ```
ac3_window[j]) >> 15;
``` |

1397 | 115329f1 | Diego Biurrun | input_samples[N-j-1] = MUL16(input_samples[N-j-1], |

1398 | de6d9b64 | Fabrice Bellard | ```
ac3_window[j]) >> 15;
``` |

1399 | } |
||

1400 | 115329f1 | Diego Biurrun | |

1401 | de6d9b64 | Fabrice Bellard | ```
/* Normalize the samples to use the maximum available
``` |

1402 | ```
precision */
``` |
||

1403 | ```
v = 14 - log2_tab(input_samples, N);
``` |
||

1404 | if (v < 0) |
||

1405 | ```
v = 0;
``` |
||

1406 | ```
exp_samples[i][ch] = v - 8;
``` |
||

1407 | lshift_tab(input_samples, N, v); |
||

1408 | |||

1409 | ```
/* do the MDCT */
``` |
||

1410 | mdct512(mdct_coef[i][ch], input_samples); |
||

1411 | 115329f1 | Diego Biurrun | |

1412 | de6d9b64 | Fabrice Bellard | ```
/* compute "exponents". We take into account the
``` |

1413 | ```
normalization there */
``` |
||

1414 | for(j=0;j<N/2;j++) { |
||

1415 | ```
int e;
``` |
||

1416 | v = abs(mdct_coef[i][ch][j]); |
||

1417 | if (v == 0) |
||

1418 | ```
e = 24;
``` |
||

1419 | ```
else {
``` |
||

1420 | 34763c15 | Fabrice Bellard | ```
e = 23 - av_log2(v) + exp_samples[i][ch];
``` |

1421 | de6d9b64 | Fabrice Bellard | if (e >= 24) { |

1422 | ```
e = 24;
``` |
||

1423 | ```
mdct_coef[i][ch][j] = 0;
``` |
||

1424 | } |
||

1425 | } |
||

1426 | exp[i][ch][j] = e; |
||

1427 | } |
||

1428 | } |
||

1429 | 115329f1 | Diego Biurrun | |

1430 | 30b68f33 | Zdenek Kabelac | compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel); |

1431 | de6d9b64 | Fabrice Bellard | |

1432 | ```
/* compute the exponents as the decoder will see them. The
``` |
||

1433 | ```
EXP_REUSE case must be handled carefully : we select the
``` |
||

1434 | ```
min of the exponents */
``` |
||

1435 | ```
i = 0;
``` |
||

1436 | ```
while (i < NB_BLOCKS) {
``` |
||

1437 | ```
j = i + 1;
``` |
||

1438 | ```
while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) {
``` |
||

1439 | exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]); |
||

1440 | j++; |
||

1441 | } |
||

1442 | frame_bits += encode_exp(encoded_exp[i][ch], |
||

1443 | 115329f1 | Diego Biurrun | exp[i][ch], s->nb_coefs[ch], |

1444 | de6d9b64 | Fabrice Bellard | exp_strategy[i][ch]); |

1445 | ```
/* copy encoded exponents for reuse case */
``` |
||

1446 | for(k=i+1;k<j;k++) { |
||

1447 | 115329f1 | Diego Biurrun | memcpy(encoded_exp[k][ch], encoded_exp[i][ch], |

1448 | 0c1a9eda | Zdenek Kabelac | ```
s->nb_coefs[ch] * sizeof(uint8_t));
``` |

1449 | de6d9b64 | Fabrice Bellard | } |

1450 | i = j; |
||

1451 | } |
||

1452 | } |
||

1453 | |||

1454 | compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); |
||

1455 | ```
/* everything is known... let's output the frame */
``` |
||

1456 | output_frame_header(s, frame); |
||

1457 | 115329f1 | Diego Biurrun | |

1458 | de6d9b64 | Fabrice Bellard | for(i=0;i<NB_BLOCKS;i++) { |

1459 | 115329f1 | Diego Biurrun | output_audio_block(s, exp_strategy[i], encoded_exp[i], |

1460 | de6d9b64 | Fabrice Bellard | bap[i], mdct_coef[i], exp_samples[i], i); |

1461 | } |
||

1462 | ```
return output_frame_end(s);
``` |
||

1463 | } |
||

1464 | |||

1465 | 492cd3a9 | Michael Niedermayer | static int AC3_encode_close(AVCodecContext *avctx) |

1466 | { |
||

1467 | av_freep(&avctx->coded_frame); |
||

1468 | ef9f7306 | Måns Rullgård | return 0; |

1469 | 492cd3a9 | Michael Niedermayer | } |

1470 | |||

1471 | de6d9b64 | Fabrice Bellard | ```
#if 0
``` |

1472 | ```
/*************************************************************************/
``` |
||

1473 | ```
/* TEST */
``` |
||

1474 | |||

1475 | ```
#define FN (N/4)
``` |
||

1476 | |||

1477 | ```
void fft_test(void)
``` |
||

1478 | ```
{
``` |
||

1479 | ```
IComplex in[FN], in1[FN];
``` |
||

1480 | ```
int k, n, i;
``` |
||

1481 | ```
float sum_re, sum_im, a;
``` |
||

1482 | |||

1483 | ```
/* FFT test */
``` |
||

1484 | |||

1485 | ```
for(i=0;i<FN;i++) {
``` |
||

1486 | ```
in[i].re = random() % 65535 - 32767;
``` |
||

1487 | ```
in[i].im = random() % 65535 - 32767;
``` |
||

1488 | ```
in1[i] = in[i];
``` |
||

1489 | ```
}
``` |
||

1490 | ```
fft(in, 7);
``` |
||

1491 | |||

1492 | ```
/* do it by hand */
``` |
||

1493 | ```
for(k=0;k<FN;k++) {
``` |
||

1494 | ```
sum_re = 0;
``` |
||

1495 | ```
sum_im = 0;
``` |
||

1496 | ```
for(n=0;n<FN;n++) {
``` |
||

1497 | ```
a = -2 * M_PI * (n * k) / FN;
``` |
||

1498 | ```
sum_re += in1[n].re * cos(a) - in1[n].im * sin(a);
``` |
||

1499 | ```
sum_im += in1[n].re * sin(a) + in1[n].im * cos(a);
``` |
||

1500 | ```
}
``` |
||

1501 | 115329f1 | Diego Biurrun | ```
printf("%3d: %6d,%6d %6.0f,%6.0f\n",
``` |

1502 | ```
k, in[k].re, in[k].im, sum_re / FN, sum_im / FN);
``` |
||

1503 | de6d9b64 | Fabrice Bellard | ```
}
``` |

1504 | ```
}
``` |
||

1505 | |||

1506 | ```
void mdct_test(void)
``` |
||

1507 | ```
{
``` |
||

1508 | 0c1a9eda | Zdenek Kabelac | ```
int16_t input[N];
``` |

1509 | ```
int32_t output[N/2];
``` |
||

1510 | de6d9b64 | Fabrice Bellard | ```
float input1[N];
``` |

1511 | ```
float output1[N/2];
``` |
||

1512 | ```
float s, a, err, e, emax;
``` |
||

1513 | ```
int i, k, n;
``` |
||

1514 | |||

1515 | ```
for(i=0;i<N;i++) {
``` |
||

1516 | ```
input[i] = (random() % 65535 - 32767) * 9 / 10;
``` |
||

1517 | ```
input1[i] = input[i];
``` |
||

1518 | ```
}
``` |
||

1519 | |||

1520 | ```
mdct512(output, input);
``` |
||

1521 | 115329f1 | Diego Biurrun | |

1522 | de6d9b64 | Fabrice Bellard | ```
/* do it by hand */
``` |

1523 | ```
for(k=0;k<N/2;k++) {
``` |
||

1524 | ```
s = 0;
``` |
||

1525 | ```
for(n=0;n<N;n++) {
``` |
||

1526 | ```
a = (2*M_PI*(2*n+1+N/2)*(2*k+1) / (4 * N));
``` |
||

1527 | ```
s += input1[n] * cos(a);
``` |
||

1528 | ```
}
``` |
||

1529 | ```
output1[k] = -2 * s / N;
``` |
||

1530 | ```
}
``` |
||

1531 | 115329f1 | Diego Biurrun | |

1532 | de6d9b64 | Fabrice Bellard | ```
err = 0;
``` |

1533 | ```
emax = 0;
``` |
||

1534 | ```
for(i=0;i<N/2;i++) {
``` |
||

1535 | ```
printf("%3d: %7d %7.0f\n", i, output[i], output1[i]);
``` |
||

1536 | ```
e = output[i] - output1[i];
``` |
||

1537 | ```
if (e > emax)
``` |
||

1538 | ```
emax = e;
``` |
||

1539 | ```
err += e * e;
``` |
||

1540 | ```
}
``` |
||

1541 | ```
printf("err2=%f emax=%f\n", err / (N/2), emax);
``` |
||

1542 | ```
}
``` |
||

1543 | |||

1544 | ```
void test_ac3(void)
``` |
||

1545 | ```
{
``` |
||

1546 | ```
AC3EncodeContext ctx;
``` |
||

1547 | ```
unsigned char frame[AC3_MAX_CODED_FRAME_SIZE];
``` |
||

1548 | ```
short samples[AC3_FRAME_SIZE];
``` |
||

1549 | ```
int ret, i;
``` |
||

1550 | 115329f1 | Diego Biurrun | |

1551 | de6d9b64 | Fabrice Bellard | ```
AC3_encode_init(&ctx, 44100, 64000, 1);
``` |

1552 | |||

1553 | ```
fft_test();
``` |
||

1554 | ```
mdct_test();
``` |
||

1555 | |||

1556 | ```
for(i=0;i<AC3_FRAME_SIZE;i++)
``` |
||

1557 | ```
samples[i] = (int)(sin(2*M_PI*i*1000.0/44100) * 10000);
``` |
||

1558 | ```
ret = AC3_encode_frame(&ctx, frame, samples);
``` |
||

1559 | ```
printf("ret=%d\n", ret);
``` |
||

1560 | ```
}
``` |
||

1561 | ```
#endif
``` |
||

1562 | |||

1563 | AVCodec ac3_encoder = { |
||

1564 | ```
"ac3",
``` |
||

1565 | CODEC_TYPE_AUDIO, |
||

1566 | CODEC_ID_AC3, |
||

1567 | ```
sizeof(AC3EncodeContext),
``` |
||

1568 | AC3_encode_init, |
||

1569 | AC3_encode_frame, |
||

1570 | 492cd3a9 | Michael Niedermayer | AC3_encode_close, |

1571 | de6d9b64 | Fabrice Bellard | ```
NULL,
``` |

1572 | }; |