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.

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