You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

604 lines
19KB

  1. /*
  2. * DCA encoder
  3. * Copyright (C) 2008 Alexander E. Patrakov
  4. * 2010 Benjamin Larsson
  5. * 2011 Xiang Wang
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/common.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/audioconvert.h"
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "internal.h"
  29. #include "put_bits.h"
  30. #include "dcaenc.h"
  31. #include "dcadata.h"
  32. #include "dca.h"
  33. #undef NDEBUG
  34. #define MAX_CHANNELS 6
  35. #define DCA_SUBBANDS_32 32
  36. #define DCA_MAX_FRAME_SIZE 16383
  37. #define DCA_HEADER_SIZE 13
  38. #define DCA_SUBBANDS 32 ///< Subband activity count
  39. #define QUANTIZER_BITS 16
  40. #define SUBFRAMES 1
  41. #define SUBSUBFRAMES 4
  42. #define PCM_SAMPLES (SUBFRAMES*SUBSUBFRAMES*8)
  43. #define LFE_BITS 8
  44. #define LFE_INTERPOLATION 64
  45. #define LFE_PRESENT 2
  46. #define LFE_MISSING 0
  47. static const int8_t dca_lfe_index[] = {
  48. 1,2,2,2,2,3,2,3,2,3,2,3,1,3,2,3
  49. };
  50. static const int8_t dca_channel_reorder_lfe[][9] = {
  51. { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
  52. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  53. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  54. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  55. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  56. { 1, 2, 0, -1, -1, -1, -1, -1, -1 },
  57. { 0, 1, -1, 2, -1, -1, -1, -1, -1 },
  58. { 1, 2, 0, -1, 3, -1, -1, -1, -1 },
  59. { 0, 1, -1, 2, 3, -1, -1, -1, -1 },
  60. { 1, 2, 0, -1, 3, 4, -1, -1, -1 },
  61. { 2, 3, -1, 0, 1, 4, 5, -1, -1 },
  62. { 1, 2, 0, -1, 3, 4, 5, -1, -1 },
  63. { 0, -1, 4, 5, 2, 3, 1, -1, -1 },
  64. { 3, 4, 1, -1, 0, 2, 5, 6, -1 },
  65. { 2, 3, -1, 5, 7, 0, 1, 4, 6 },
  66. { 3, 4, 1, -1, 0, 2, 5, 7, 6 },
  67. };
  68. static const int8_t dca_channel_reorder_nolfe[][9] = {
  69. { 0, -1, -1, -1, -1, -1, -1, -1, -1 },
  70. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  71. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  72. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  73. { 0, 1, -1, -1, -1, -1, -1, -1, -1 },
  74. { 1, 2, 0, -1, -1, -1, -1, -1, -1 },
  75. { 0, 1, 2, -1, -1, -1, -1, -1, -1 },
  76. { 1, 2, 0, 3, -1, -1, -1, -1, -1 },
  77. { 0, 1, 2, 3, -1, -1, -1, -1, -1 },
  78. { 1, 2, 0, 3, 4, -1, -1, -1, -1 },
  79. { 2, 3, 0, 1, 4, 5, -1, -1, -1 },
  80. { 1, 2, 0, 3, 4, 5, -1, -1, -1 },
  81. { 0, 4, 5, 2, 3, 1, -1, -1, -1 },
  82. { 3, 4, 1, 0, 2, 5, 6, -1, -1 },
  83. { 2, 3, 5, 7, 0, 1, 4, 6, -1 },
  84. { 3, 4, 1, 0, 2, 5, 7, 6, -1 },
  85. };
  86. typedef struct {
  87. PutBitContext pb;
  88. int32_t history[MAX_CHANNELS][512]; /* This is a circular buffer */
  89. int start[MAX_CHANNELS];
  90. int frame_size;
  91. int prim_channels;
  92. int lfe_channel;
  93. int sample_rate_code;
  94. int scale_factor[MAX_CHANNELS][DCA_SUBBANDS_32];
  95. int lfe_scale_factor;
  96. int lfe_data[SUBFRAMES*SUBSUBFRAMES*4];
  97. int a_mode; ///< audio channels arrangement
  98. int num_channel;
  99. int lfe_state;
  100. int lfe_offset;
  101. const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
  102. int32_t pcm[FFMAX(LFE_INTERPOLATION, DCA_SUBBANDS_32)];
  103. int32_t subband[PCM_SAMPLES][MAX_CHANNELS][DCA_SUBBANDS_32]; /* [sample][channel][subband] */
  104. } DCAContext;
  105. static int32_t cos_table[128];
  106. static inline int32_t mul32(int32_t a, int32_t b)
  107. {
  108. int64_t r = (int64_t) a * b;
  109. /* round the result before truncating - improves accuracy */
  110. return (r + 0x80000000) >> 32;
  111. }
  112. /* Integer version of the cosine modulated Pseudo QMF */
  113. static void qmf_init(void)
  114. {
  115. int i;
  116. int32_t c[17], s[17];
  117. s[0] = 0; /* sin(index * PI / 64) * 0x7fffffff */
  118. c[0] = 0x7fffffff; /* cos(index * PI / 64) * 0x7fffffff */
  119. for (i = 1; i <= 16; i++) {
  120. s[i] = 2 * (mul32(c[i - 1], 105372028) + mul32(s[i - 1], 2144896908));
  121. c[i] = 2 * (mul32(c[i - 1], 2144896908) - mul32(s[i - 1], 105372028));
  122. }
  123. for (i = 0; i < 16; i++) {
  124. cos_table[i ] = c[i] >> 3; /* avoid output overflow */
  125. cos_table[i + 16] = s[16 - i] >> 3;
  126. cos_table[i + 32] = -s[i] >> 3;
  127. cos_table[i + 48] = -c[16 - i] >> 3;
  128. cos_table[i + 64] = -c[i] >> 3;
  129. cos_table[i + 80] = -s[16 - i] >> 3;
  130. cos_table[i + 96] = s[i] >> 3;
  131. cos_table[i + 112] = c[16 - i] >> 3;
  132. }
  133. }
  134. static int32_t band_delta_factor(int band, int sample_num)
  135. {
  136. int index = band * (2 * sample_num + 1);
  137. if (band == 0)
  138. return 0x07ffffff;
  139. else
  140. return cos_table[index & 127];
  141. }
  142. static void add_new_samples(DCAContext *c, const int32_t *in,
  143. int count, int channel)
  144. {
  145. int i;
  146. /* Place new samples into the history buffer */
  147. for (i = 0; i < count; i++) {
  148. c->history[channel][c->start[channel] + i] = in[i];
  149. av_assert0(c->start[channel] + i < 512);
  150. }
  151. c->start[channel] += count;
  152. if (c->start[channel] == 512)
  153. c->start[channel] = 0;
  154. av_assert0(c->start[channel] < 512);
  155. }
  156. static void qmf_decompose(DCAContext *c, int32_t in[32], int32_t out[32],
  157. int channel)
  158. {
  159. int band, i, j, k;
  160. int32_t resp;
  161. int32_t accum[DCA_SUBBANDS_32] = {0};
  162. add_new_samples(c, in, DCA_SUBBANDS_32, channel);
  163. /* Calculate the dot product of the signal with the (possibly inverted)
  164. reference decoder's response to this vector:
  165. (0.0, 0.0, ..., 0.0, -1.0, 1.0, 0.0, ..., 0.0)
  166. so that -1.0 cancels 1.0 from the previous step */
  167. for (k = 48, j = 0, i = c->start[channel]; i < 512; k++, j++, i++)
  168. accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
  169. for (i = 0; i < c->start[channel]; k++, j++, i++)
  170. accum[(k & 32) ? (31 - (k & 31)) : (k & 31)] += mul32(c->history[channel][i], UnQMF[j]);
  171. resp = 0;
  172. /* TODO: implement FFT instead of this naive calculation */
  173. for (band = 0; band < DCA_SUBBANDS_32; band++) {
  174. for (j = 0; j < 32; j++)
  175. resp += mul32(accum[j], band_delta_factor(band, j));
  176. out[band] = (band & 2) ? (-resp) : resp;
  177. }
  178. }
  179. static int32_t lfe_fir_64i[512];
  180. static int lfe_downsample(DCAContext *c, int32_t in[LFE_INTERPOLATION])
  181. {
  182. int i, j;
  183. int channel = c->prim_channels;
  184. int32_t accum = 0;
  185. add_new_samples(c, in, LFE_INTERPOLATION, channel);
  186. for (i = c->start[channel], j = 0; i < 512; i++, j++)
  187. accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
  188. for (i = 0; i < c->start[channel]; i++, j++)
  189. accum += mul32(c->history[channel][i], lfe_fir_64i[j]);
  190. return accum;
  191. }
  192. static void init_lfe_fir(void)
  193. {
  194. static int initialized = 0;
  195. int i;
  196. if (initialized)
  197. return;
  198. for (i = 0; i < 512; i++)
  199. lfe_fir_64i[i] = lfe_fir_64[i] * (1 << 25); //float -> int32_t
  200. initialized = 1;
  201. }
  202. static void put_frame_header(DCAContext *c)
  203. {
  204. /* SYNC */
  205. put_bits(&c->pb, 16, 0x7ffe);
  206. put_bits(&c->pb, 16, 0x8001);
  207. /* Frame type: normal */
  208. put_bits(&c->pb, 1, 1);
  209. /* Deficit sample count: none */
  210. put_bits(&c->pb, 5, 31);
  211. /* CRC is not present */
  212. put_bits(&c->pb, 1, 0);
  213. /* Number of PCM sample blocks */
  214. put_bits(&c->pb, 7, PCM_SAMPLES-1);
  215. /* Primary frame byte size */
  216. put_bits(&c->pb, 14, c->frame_size-1);
  217. /* Audio channel arrangement: L + R (stereo) */
  218. put_bits(&c->pb, 6, c->num_channel);
  219. /* Core audio sampling frequency */
  220. put_bits(&c->pb, 4, c->sample_rate_code);
  221. /* Transmission bit rate: 1411.2 kbps */
  222. put_bits(&c->pb, 5, 0x16); /* FIXME: magic number */
  223. /* Embedded down mix: disabled */
  224. put_bits(&c->pb, 1, 0);
  225. /* Embedded dynamic range flag: not present */
  226. put_bits(&c->pb, 1, 0);
  227. /* Embedded time stamp flag: not present */
  228. put_bits(&c->pb, 1, 0);
  229. /* Auxiliary data flag: not present */
  230. put_bits(&c->pb, 1, 0);
  231. /* HDCD source: no */
  232. put_bits(&c->pb, 1, 0);
  233. /* Extension audio ID: N/A */
  234. put_bits(&c->pb, 3, 0);
  235. /* Extended audio data: not present */
  236. put_bits(&c->pb, 1, 0);
  237. /* Audio sync word insertion flag: after each sub-frame */
  238. put_bits(&c->pb, 1, 0);
  239. /* Low frequency effects flag: not present or interpolation factor=64 */
  240. put_bits(&c->pb, 2, c->lfe_state);
  241. /* Predictor history switch flag: on */
  242. put_bits(&c->pb, 1, 1);
  243. /* No CRC */
  244. /* Multirate interpolator switch: non-perfect reconstruction */
  245. put_bits(&c->pb, 1, 0);
  246. /* Encoder software revision: 7 */
  247. put_bits(&c->pb, 4, 7);
  248. /* Copy history: 0 */
  249. put_bits(&c->pb, 2, 0);
  250. /* Source PCM resolution: 16 bits, not DTS ES */
  251. put_bits(&c->pb, 3, 0);
  252. /* Front sum/difference coding: no */
  253. put_bits(&c->pb, 1, 0);
  254. /* Surrounds sum/difference coding: no */
  255. put_bits(&c->pb, 1, 0);
  256. /* Dialog normalization: 0 dB */
  257. put_bits(&c->pb, 4, 0);
  258. }
  259. static void put_primary_audio_header(DCAContext *c)
  260. {
  261. static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
  262. static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
  263. int ch, i;
  264. /* Number of subframes */
  265. put_bits(&c->pb, 4, SUBFRAMES - 1);
  266. /* Number of primary audio channels */
  267. put_bits(&c->pb, 3, c->prim_channels - 1);
  268. /* Subband activity count */
  269. for (ch = 0; ch < c->prim_channels; ch++)
  270. put_bits(&c->pb, 5, DCA_SUBBANDS - 2);
  271. /* High frequency VQ start subband */
  272. for (ch = 0; ch < c->prim_channels; ch++)
  273. put_bits(&c->pb, 5, DCA_SUBBANDS - 1);
  274. /* Joint intensity coding index: 0, 0 */
  275. for (ch = 0; ch < c->prim_channels; ch++)
  276. put_bits(&c->pb, 3, 0);
  277. /* Transient mode codebook: A4, A4 (arbitrary) */
  278. for (ch = 0; ch < c->prim_channels; ch++)
  279. put_bits(&c->pb, 2, 0);
  280. /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
  281. for (ch = 0; ch < c->prim_channels; ch++)
  282. put_bits(&c->pb, 3, 6);
  283. /* Bit allocation quantizer select: linear 5-bit */
  284. for (ch = 0; ch < c->prim_channels; ch++)
  285. put_bits(&c->pb, 3, 6);
  286. /* Quantization index codebook select: dummy data
  287. to avoid transmission of scale factor adjustment */
  288. for (i = 1; i < 11; i++)
  289. for (ch = 0; ch < c->prim_channels; ch++)
  290. put_bits(&c->pb, bitlen[i], thr[i]);
  291. /* Scale factor adjustment index: not transmitted */
  292. }
  293. /**
  294. * 8-23 bits quantization
  295. * @param sample
  296. * @param bits
  297. */
  298. static inline uint32_t quantize(int32_t sample, int bits)
  299. {
  300. av_assert0(sample < 1 << (bits - 1));
  301. av_assert0(sample >= -(1 << (bits - 1)));
  302. return sample & ((1 << bits) - 1);
  303. }
  304. static inline int find_scale_factor7(int64_t max_value, int bits)
  305. {
  306. int i = 0, j = 128, q;
  307. max_value = ((max_value << 15) / lossy_quant[bits + 3]) >> (bits - 1);
  308. while (i < j) {
  309. q = (i + j) >> 1;
  310. if (max_value < scale_factor_quant7[q])
  311. j = q;
  312. else
  313. i = q + 1;
  314. }
  315. av_assert1(i < 128);
  316. return i;
  317. }
  318. static inline void put_sample7(DCAContext *c, int64_t sample, int bits,
  319. int scale_factor)
  320. {
  321. sample = (sample << 15) / ((int64_t) lossy_quant[bits + 3] * scale_factor_quant7[scale_factor]);
  322. put_bits(&c->pb, bits, quantize((int) sample, bits));
  323. }
  324. static void put_subframe(DCAContext *c,
  325. int32_t subband_data[8 * SUBSUBFRAMES][MAX_CHANNELS][32],
  326. int subframe)
  327. {
  328. int i, sub, ss, ch, max_value;
  329. int32_t *lfe_data = c->lfe_data + 4 * SUBSUBFRAMES * subframe;
  330. /* Subsubframes count */
  331. put_bits(&c->pb, 2, SUBSUBFRAMES -1);
  332. /* Partial subsubframe sample count: dummy */
  333. put_bits(&c->pb, 3, 0);
  334. /* Prediction mode: no ADPCM, in each channel and subband */
  335. for (ch = 0; ch < c->prim_channels; ch++)
  336. for (sub = 0; sub < DCA_SUBBANDS; sub++)
  337. put_bits(&c->pb, 1, 0);
  338. /* Prediction VQ addres: not transmitted */
  339. /* Bit allocation index */
  340. for (ch = 0; ch < c->prim_channels; ch++)
  341. for (sub = 0; sub < DCA_SUBBANDS; sub++)
  342. put_bits(&c->pb, 5, QUANTIZER_BITS+3);
  343. if (SUBSUBFRAMES > 1) {
  344. /* Transition mode: none for each channel and subband */
  345. for (ch = 0; ch < c->prim_channels; ch++)
  346. for (sub = 0; sub < DCA_SUBBANDS; sub++)
  347. put_bits(&c->pb, 1, 0); /* codebook A4 */
  348. }
  349. /* Determine scale_factor */
  350. for (ch = 0; ch < c->prim_channels; ch++)
  351. for (sub = 0; sub < DCA_SUBBANDS; sub++) {
  352. max_value = 0;
  353. for (i = 0; i < 8 * SUBSUBFRAMES; i++)
  354. max_value = FFMAX(max_value, FFABS(subband_data[i][ch][sub]));
  355. c->scale_factor[ch][sub] = find_scale_factor7(max_value, QUANTIZER_BITS);
  356. }
  357. if (c->lfe_channel) {
  358. max_value = 0;
  359. for (i = 0; i < 4 * SUBSUBFRAMES; i++)
  360. max_value = FFMAX(max_value, FFABS(lfe_data[i]));
  361. c->lfe_scale_factor = find_scale_factor7(max_value, LFE_BITS);
  362. }
  363. /* Scale factors: the same for each channel and subband,
  364. encoded according to Table D.1.2 */
  365. for (ch = 0; ch < c->prim_channels; ch++)
  366. for (sub = 0; sub < DCA_SUBBANDS; sub++)
  367. put_bits(&c->pb, 7, c->scale_factor[ch][sub]);
  368. /* Joint subband scale factor codebook select: not transmitted */
  369. /* Scale factors for joint subband coding: not transmitted */
  370. /* Stereo down-mix coefficients: not transmitted */
  371. /* Dynamic range coefficient: not transmitted */
  372. /* Stde information CRC check word: not transmitted */
  373. /* VQ encoded high frequency subbands: not transmitted */
  374. /* LFE data */
  375. if (c->lfe_channel) {
  376. for (i = 0; i < 4 * SUBSUBFRAMES; i++)
  377. put_sample7(c, lfe_data[i], LFE_BITS, c->lfe_scale_factor);
  378. put_bits(&c->pb, 8, c->lfe_scale_factor);
  379. }
  380. /* Audio data (subsubframes) */
  381. for (ss = 0; ss < SUBSUBFRAMES ; ss++)
  382. for (ch = 0; ch < c->prim_channels; ch++)
  383. for (sub = 0; sub < DCA_SUBBANDS; sub++)
  384. for (i = 0; i < 8; i++)
  385. put_sample7(c, subband_data[ss * 8 + i][ch][sub], QUANTIZER_BITS, c->scale_factor[ch][sub]);
  386. /* DSYNC */
  387. put_bits(&c->pb, 16, 0xffff);
  388. }
  389. static void put_frame(DCAContext *c,
  390. int32_t subband_data[PCM_SAMPLES][MAX_CHANNELS][32],
  391. uint8_t *frame)
  392. {
  393. int i;
  394. init_put_bits(&c->pb, frame + DCA_HEADER_SIZE, DCA_MAX_FRAME_SIZE-DCA_HEADER_SIZE);
  395. put_primary_audio_header(c);
  396. for (i = 0; i < SUBFRAMES; i++)
  397. put_subframe(c, &subband_data[SUBSUBFRAMES * 8 * i], i);
  398. flush_put_bits(&c->pb);
  399. c->frame_size = (put_bits_count(&c->pb) >> 3) + DCA_HEADER_SIZE;
  400. init_put_bits(&c->pb, frame, DCA_HEADER_SIZE);
  401. put_frame_header(c);
  402. flush_put_bits(&c->pb);
  403. }
  404. static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  405. const AVFrame *frame, int *got_packet_ptr)
  406. {
  407. int i, k, channel;
  408. DCAContext *c = avctx->priv_data;
  409. const int16_t *samples;
  410. int ret, real_channel = 0;
  411. if ((ret = ff_alloc_packet2(avctx, avpkt, DCA_MAX_FRAME_SIZE + DCA_HEADER_SIZE)))
  412. return ret;
  413. samples = (const int16_t *)frame->data[0];
  414. for (i = 0; i < PCM_SAMPLES; i ++) { /* i is the decimated sample number */
  415. for (channel = 0; channel < c->prim_channels + 1; channel++) {
  416. real_channel = c->channel_order_tab[channel];
  417. if (real_channel >= 0) {
  418. /* Get 32 PCM samples */
  419. for (k = 0; k < 32; k++) { /* k is the sample number in a 32-sample block */
  420. c->pcm[k] = samples[avctx->channels * (32 * i + k) + channel] << 16;
  421. }
  422. /* Put subband samples into the proper place */
  423. qmf_decompose(c, c->pcm, &c->subband[i][real_channel][0], real_channel);
  424. }
  425. }
  426. }
  427. if (c->lfe_channel) {
  428. for (i = 0; i < PCM_SAMPLES / 2; i++) {
  429. for (k = 0; k < LFE_INTERPOLATION; k++) /* k is the sample number in a 32-sample block */
  430. c->pcm[k] = samples[avctx->channels * (LFE_INTERPOLATION*i+k) + c->lfe_offset] << 16;
  431. c->lfe_data[i] = lfe_downsample(c, c->pcm);
  432. }
  433. }
  434. put_frame(c, c->subband, avpkt->data);
  435. avpkt->size = c->frame_size;
  436. *got_packet_ptr = 1;
  437. return 0;
  438. }
  439. static int encode_init(AVCodecContext *avctx)
  440. {
  441. DCAContext *c = avctx->priv_data;
  442. int i;
  443. uint64_t layout = avctx->channel_layout;
  444. c->prim_channels = avctx->channels;
  445. c->lfe_channel = (avctx->channels == 3 || avctx->channels == 6);
  446. if (!layout) {
  447. av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "
  448. "encoder will guess the layout, but it "
  449. "might be incorrect.\n");
  450. layout = av_get_default_channel_layout(avctx->channels);
  451. }
  452. switch (layout) {
  453. case AV_CH_LAYOUT_STEREO: c->a_mode = 2; c->num_channel = 2; break;
  454. case AV_CH_LAYOUT_5POINT0: c->a_mode = 9; c->num_channel = 9; break;
  455. case AV_CH_LAYOUT_5POINT1: c->a_mode = 9; c->num_channel = 9; break;
  456. case AV_CH_LAYOUT_5POINT0_BACK: c->a_mode = 9; c->num_channel = 9; break;
  457. case AV_CH_LAYOUT_5POINT1_BACK: c->a_mode = 9; c->num_channel = 9; break;
  458. default:
  459. av_log(avctx, AV_LOG_ERROR,
  460. "Only stereo, 5.0, 5.1 channel layouts supported at the moment!\n");
  461. return AVERROR_PATCHWELCOME;
  462. }
  463. if (c->lfe_channel) {
  464. init_lfe_fir();
  465. c->prim_channels--;
  466. c->channel_order_tab = dca_channel_reorder_lfe[c->a_mode];
  467. c->lfe_state = LFE_PRESENT;
  468. c->lfe_offset = dca_lfe_index[c->a_mode];
  469. } else {
  470. c->channel_order_tab = dca_channel_reorder_nolfe[c->a_mode];
  471. c->lfe_state = LFE_MISSING;
  472. }
  473. for (i = 0; i < 16; i++) {
  474. if (avpriv_dca_sample_rates[i] && (avpriv_dca_sample_rates[i] == avctx->sample_rate))
  475. break;
  476. }
  477. if (i == 16) {
  478. av_log(avctx, AV_LOG_ERROR, "Sample rate %iHz not supported, only ", avctx->sample_rate);
  479. for (i = 0; i < 16; i++)
  480. av_log(avctx, AV_LOG_ERROR, "%d, ", avpriv_dca_sample_rates[i]);
  481. av_log(avctx, AV_LOG_ERROR, "supported.\n");
  482. return -1;
  483. }
  484. c->sample_rate_code = i;
  485. avctx->frame_size = 32 * PCM_SAMPLES;
  486. if (!cos_table[127])
  487. qmf_init();
  488. return 0;
  489. }
  490. AVCodec ff_dca_encoder = {
  491. .name = "dca",
  492. .type = AVMEDIA_TYPE_AUDIO,
  493. .id = AV_CODEC_ID_DTS,
  494. .priv_data_size = sizeof(DCAContext),
  495. .init = encode_init,
  496. .encode2 = encode_frame,
  497. .capabilities = CODEC_CAP_EXPERIMENTAL,
  498. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  499. AV_SAMPLE_FMT_NONE },
  500. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  501. };