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.

2400 lines
83KB

  1. /**
  2. * MLP encoder
  3. * Copyright (c) 2008 Ramiro Polla
  4. * Copyright (c) 2016-2019 Jai Luthra
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "put_bits.h"
  25. #include "audio_frame_queue.h"
  26. #include "libavutil/crc.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/samplefmt.h"
  29. #include "mlp.h"
  30. #include "lpc.h"
  31. #define MAJOR_HEADER_INTERVAL 16
  32. #define MLP_MIN_LPC_ORDER 1
  33. #define MLP_MAX_LPC_ORDER 8
  34. #define MLP_MIN_LPC_SHIFT 8
  35. #define MLP_MAX_LPC_SHIFT 15
  36. typedef struct {
  37. uint8_t min_channel; ///< The index of the first channel coded in this substream.
  38. uint8_t max_channel; ///< The index of the last channel coded in this substream.
  39. uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
  40. uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
  41. uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
  42. int data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.
  43. int32_t lossless_check_data; ///< XOR of all output samples
  44. uint8_t max_huff_lsbs; ///< largest huff_lsbs
  45. uint8_t max_output_bits; ///< largest output bit-depth
  46. } RestartHeader;
  47. typedef struct {
  48. uint8_t count; ///< number of matrices to apply
  49. uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
  50. int32_t forco[MAX_MATRICES][MAX_CHANNELS+2]; ///< forward coefficients
  51. int32_t coeff[MAX_MATRICES][MAX_CHANNELS+2]; ///< decoding coefficients
  52. uint8_t fbits[MAX_CHANNELS]; ///< fraction bits
  53. int8_t shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
  54. } MatrixParams;
  55. enum ParamFlags {
  56. PARAMS_DEFAULT = 0xff,
  57. PARAM_PRESENCE_FLAGS = 1 << 8,
  58. PARAM_BLOCKSIZE = 1 << 7,
  59. PARAM_MATRIX = 1 << 6,
  60. PARAM_OUTSHIFT = 1 << 5,
  61. PARAM_QUANTSTEP = 1 << 4,
  62. PARAM_FIR = 1 << 3,
  63. PARAM_IIR = 1 << 2,
  64. PARAM_HUFFOFFSET = 1 << 1,
  65. PARAM_PRESENT = 1 << 0,
  66. };
  67. typedef struct {
  68. uint16_t blocksize; ///< number of PCM samples in current audio block
  69. uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
  70. MatrixParams matrix_params;
  71. uint8_t param_presence_flags; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
  72. } DecodingParams;
  73. typedef struct BestOffset {
  74. int32_t offset;
  75. int bitcount;
  76. int lsb_bits;
  77. int32_t min;
  78. int32_t max;
  79. } BestOffset;
  80. #define HUFF_OFFSET_MIN (-16384)
  81. #define HUFF_OFFSET_MAX ( 16383)
  82. /** Number of possible codebooks (counting "no codebooks") */
  83. #define NUM_CODEBOOKS 4
  84. typedef struct {
  85. AVCodecContext *avctx;
  86. int num_substreams; ///< Number of substreams contained within this stream.
  87. int num_channels; /**< Number of channels in major_scratch_buffer.
  88. * Normal channels + noise channels. */
  89. int coded_sample_fmt [2]; ///< sample format encoded for MLP
  90. int coded_sample_rate[2]; ///< sample rate encoded for MLP
  91. int coded_peak_bitrate; ///< peak bitrate for this major sync header
  92. int flags; ///< major sync info flags
  93. /* channel_meaning */
  94. int substream_info;
  95. int fs;
  96. int wordlength;
  97. int channel_occupancy;
  98. int summary_info;
  99. int32_t *inout_buffer; ///< Pointer to data currently being read from lavc or written to bitstream.
  100. int32_t *major_inout_buffer; ///< Buffer with all in/out data for one entire major frame interval.
  101. int32_t *write_buffer; ///< Pointer to data currently being written to bitstream.
  102. int32_t *sample_buffer; ///< Pointer to current access unit samples.
  103. int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.
  104. int32_t *last_frame; ///< Pointer to last frame with data to encode.
  105. int32_t *lpc_sample_buffer;
  106. unsigned int major_number_of_frames;
  107. unsigned int next_major_number_of_frames;
  108. unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.
  109. unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.
  110. int32_t *lossless_check_data; ///< Array with lossless_check_data for each access unit.
  111. unsigned int *max_output_bits; ///< largest output bit-depth
  112. unsigned int *frame_size; ///< Array with number of samples/channel in each access unit.
  113. unsigned int frame_index; ///< Index of current frame being encoded.
  114. unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
  115. unsigned int max_restart_interval; ///< Max interval of access units in between two major frames.
  116. unsigned int min_restart_interval; ///< Min interval of access units in between two major frames.
  117. unsigned int restart_intervals; ///< Number of possible major frame sizes.
  118. uint16_t timestamp; ///< Timestamp of current access unit.
  119. uint16_t dts; ///< Decoding timestamp of current access unit.
  120. uint8_t channel_arrangement; ///< channel arrangement for MLP streams
  121. uint8_t ch_modifier_thd0; ///< channel modifier for TrueHD stream 0
  122. uint8_t ch_modifier_thd1; ///< channel modifier for TrueHD stream 1
  123. uint8_t ch_modifier_thd2; ///< channel modifier for TrueHD stream 2
  124. unsigned int seq_size [MAJOR_HEADER_INTERVAL];
  125. unsigned int seq_offset[MAJOR_HEADER_INTERVAL];
  126. unsigned int sequence_size;
  127. ChannelParams *channel_params;
  128. BestOffset best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
  129. DecodingParams *decoding_params;
  130. RestartHeader restart_header [MAX_SUBSTREAMS];
  131. ChannelParams major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
  132. DecodingParams major_decoding_params[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]; ///< DecodingParams to be written to bitstream.
  133. int major_params_changed[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]; ///< params_changed to be written to bitstream.
  134. unsigned int major_cur_subblock_index;
  135. unsigned int major_filter_state_subblock;
  136. unsigned int major_number_of_subblocks;
  137. BestOffset (*cur_best_offset)[NUM_CODEBOOKS];
  138. ChannelParams *cur_channel_params;
  139. DecodingParams *cur_decoding_params;
  140. RestartHeader *cur_restart_header;
  141. AudioFrameQueue afq;
  142. /* Analysis stage. */
  143. unsigned int starting_frame_index;
  144. unsigned int number_of_frames;
  145. unsigned int number_of_samples;
  146. unsigned int number_of_subblocks;
  147. unsigned int seq_index; ///< Sequence index for high compression levels.
  148. ChannelParams *prev_channel_params;
  149. DecodingParams *prev_decoding_params;
  150. ChannelParams *seq_channel_params;
  151. DecodingParams *seq_decoding_params;
  152. unsigned int max_codebook_search;
  153. LPCContext lpc_ctx;
  154. } MLPEncodeContext;
  155. static ChannelParams restart_channel_params[MAX_CHANNELS];
  156. static DecodingParams restart_decoding_params[MAX_SUBSTREAMS];
  157. static BestOffset restart_best_offset[NUM_CODEBOOKS] = {{0}};
  158. #define SYNC_MAJOR 0xf8726f
  159. #define MAJOR_SYNC_INFO_SIGNATURE 0xB752
  160. #define SYNC_MLP 0xbb
  161. #define SYNC_TRUEHD 0xba
  162. /* must be set for DVD-A */
  163. #define FLAGS_DVDA 0x4000
  164. /* FIFO delay must be constant */
  165. #define FLAGS_CONST 0x8000
  166. #define SUBSTREAM_INFO_MAX_2_CHAN 0x01
  167. #define SUBSTREAM_INFO_HIGH_RATE 0x02
  168. #define SUBSTREAM_INFO_ALWAYS_SET 0x04
  169. #define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
  170. /****************************************************************************
  171. ************ Functions that copy, clear, or compare parameters *************
  172. ****************************************************************************/
  173. /** Compares two FilterParams structures and returns 1 if anything has
  174. * changed. Returns 0 if they are both equal.
  175. */
  176. static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
  177. {
  178. const FilterParams *prev = &prev_cp->filter_params[filter];
  179. const FilterParams *fp = &cp->filter_params[filter];
  180. int i;
  181. if (prev->order != fp->order)
  182. return 1;
  183. if (!prev->order)
  184. return 0;
  185. if (prev->shift != fp->shift)
  186. return 1;
  187. for (i = 0; i < fp->order; i++)
  188. if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
  189. return 1;
  190. return 0;
  191. }
  192. /** Compare two primitive matrices and returns 1 if anything has changed.
  193. * Returns 0 if they are both equal.
  194. */
  195. static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
  196. {
  197. RestartHeader *rh = ctx->cur_restart_header;
  198. unsigned int channel, mat;
  199. if (prev->count != mp->count)
  200. return 1;
  201. if (!prev->count)
  202. return 0;
  203. for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
  204. if (prev->fbits[channel] != mp->fbits[channel])
  205. return 1;
  206. for (mat = 0; mat < mp->count; mat++) {
  207. if (prev->outch[mat] != mp->outch[mat])
  208. return 1;
  209. for (channel = 0; channel < ctx->num_channels; channel++)
  210. if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. /** Compares two DecodingParams and ChannelParams structures to decide if a
  216. * new decoding params header has to be written.
  217. */
  218. static int compare_decoding_params(MLPEncodeContext *ctx)
  219. {
  220. DecodingParams *prev = ctx->prev_decoding_params;
  221. DecodingParams *dp = ctx->cur_decoding_params;
  222. MatrixParams *prev_mp = &prev->matrix_params;
  223. MatrixParams *mp = &dp->matrix_params;
  224. RestartHeader *rh = ctx->cur_restart_header;
  225. unsigned int ch;
  226. int retval = 0;
  227. if (prev->param_presence_flags != dp->param_presence_flags)
  228. retval |= PARAM_PRESENCE_FLAGS;
  229. if (prev->blocksize != dp->blocksize)
  230. retval |= PARAM_BLOCKSIZE;
  231. if (compare_matrix_params(ctx, prev_mp, mp))
  232. retval |= PARAM_MATRIX;
  233. for (ch = 0; ch <= rh->max_matrix_channel; ch++)
  234. if (prev_mp->shift[ch] != mp->shift[ch]) {
  235. retval |= PARAM_OUTSHIFT;
  236. break;
  237. }
  238. for (ch = 0; ch <= rh->max_channel; ch++)
  239. if (prev->quant_step_size[ch] != dp->quant_step_size[ch]) {
  240. retval |= PARAM_QUANTSTEP;
  241. break;
  242. }
  243. for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
  244. ChannelParams *prev_cp = &ctx->prev_channel_params[ch];
  245. ChannelParams *cp = &ctx->cur_channel_params[ch];
  246. if (!(retval & PARAM_FIR) &&
  247. compare_filter_params(prev_cp, cp, FIR))
  248. retval |= PARAM_FIR;
  249. if (!(retval & PARAM_IIR) &&
  250. compare_filter_params(prev_cp, cp, IIR))
  251. retval |= PARAM_IIR;
  252. if (prev_cp->huff_offset != cp->huff_offset)
  253. retval |= PARAM_HUFFOFFSET;
  254. if (prev_cp->codebook != cp->codebook ||
  255. prev_cp->huff_lsbs != cp->huff_lsbs )
  256. retval |= 0x1;
  257. }
  258. return retval;
  259. }
  260. static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
  261. {
  262. FilterParams *dst = &dst_cp->filter_params[filter];
  263. FilterParams *src = &src_cp->filter_params[filter];
  264. unsigned int order;
  265. dst->order = src->order;
  266. if (dst->order) {
  267. dst->shift = src->shift;
  268. dst->coeff_shift = src->coeff_shift;
  269. dst->coeff_bits = src->coeff_bits;
  270. }
  271. for (order = 0; order < dst->order; order++)
  272. dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
  273. }
  274. static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
  275. {
  276. dst->count = src->count;
  277. if (dst->count) {
  278. unsigned int channel, count;
  279. for (channel = 0; channel < MAX_CHANNELS; channel++) {
  280. dst->fbits[channel] = src->fbits[channel];
  281. dst->shift[channel] = src->shift[channel];
  282. for (count = 0; count < MAX_MATRICES; count++)
  283. dst->coeff[count][channel] = src->coeff[count][channel];
  284. }
  285. for (count = 0; count < MAX_MATRICES; count++)
  286. dst->outch[count] = src->outch[count];
  287. }
  288. }
  289. static void copy_restart_frame_params(MLPEncodeContext *ctx,
  290. unsigned int substr)
  291. {
  292. unsigned int index;
  293. for (index = 0; index < ctx->number_of_subblocks; index++) {
  294. DecodingParams *dp = ctx->seq_decoding_params + index*(ctx->num_substreams) + substr;
  295. unsigned int channel;
  296. copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
  297. for (channel = 0; channel < ctx->avctx->channels; channel++) {
  298. ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
  299. unsigned int filter;
  300. dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
  301. dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
  302. if (index)
  303. for (filter = 0; filter < NUM_FILTERS; filter++)
  304. copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
  305. }
  306. }
  307. }
  308. /** Clears a DecodingParams struct the way it should be after a restart header. */
  309. static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
  310. {
  311. unsigned int substr;
  312. for (substr = 0; substr < ctx->num_substreams; substr++) {
  313. DecodingParams *dp = &decoding_params[substr];
  314. dp->param_presence_flags = 0xff;
  315. dp->blocksize = 8;
  316. memset(&dp->matrix_params , 0, sizeof(MatrixParams ));
  317. memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
  318. }
  319. }
  320. /** Clears a ChannelParams struct the way it should be after a restart header. */
  321. static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS])
  322. {
  323. unsigned int channel;
  324. for (channel = 0; channel < ctx->avctx->channels; channel++) {
  325. ChannelParams *cp = &channel_params[channel];
  326. memset(&cp->filter_params, 0, sizeof(cp->filter_params));
  327. /* Default audio coding is 24-bit raw PCM. */
  328. cp->huff_offset = 0;
  329. cp->codebook = 0;
  330. cp->huff_lsbs = 24;
  331. }
  332. }
  333. /** Sets default vales in our encoder for a DecodingParams struct. */
  334. static void default_decoding_params(MLPEncodeContext *ctx,
  335. DecodingParams decoding_params[MAX_SUBSTREAMS])
  336. {
  337. unsigned int substr;
  338. clear_decoding_params(ctx, decoding_params);
  339. for (substr = 0; substr < ctx->num_substreams; substr++) {
  340. DecodingParams *dp = &decoding_params[substr];
  341. uint8_t param_presence_flags = 0;
  342. param_presence_flags |= PARAM_BLOCKSIZE;
  343. param_presence_flags |= PARAM_MATRIX;
  344. param_presence_flags |= PARAM_OUTSHIFT;
  345. param_presence_flags |= PARAM_QUANTSTEP;
  346. param_presence_flags |= PARAM_FIR;
  347. /* param_presence_flags |= PARAM_IIR; */
  348. param_presence_flags |= PARAM_HUFFOFFSET;
  349. param_presence_flags |= PARAM_PRESENT;
  350. dp->param_presence_flags = param_presence_flags;
  351. }
  352. }
  353. /****************************************************************************/
  354. /** Calculates the smallest number of bits it takes to encode a given signed
  355. * value in two's complement.
  356. */
  357. static int inline number_sbits(int number)
  358. {
  359. if (number < -1)
  360. number++;
  361. return av_log2(FFABS(number)) + 1 + !!number;
  362. }
  363. enum InputBitDepth {
  364. BITS_16,
  365. BITS_20,
  366. BITS_24,
  367. };
  368. static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
  369. {
  370. return ((peak_bitrate << 4) - 8) / sample_rate;
  371. }
  372. static av_cold int mlp_encode_init(AVCodecContext *avctx)
  373. {
  374. MLPEncodeContext *ctx = avctx->priv_data;
  375. unsigned int substr, index;
  376. unsigned int sum = 0;
  377. unsigned int size;
  378. int ret;
  379. ctx->avctx = avctx;
  380. switch (avctx->sample_rate) {
  381. case 44100 << 0:
  382. avctx->frame_size = 40 << 0;
  383. ctx->coded_sample_rate[0] = 0x08 + 0;
  384. ctx->fs = 0x08 + 1;
  385. break;
  386. case 44100 << 1:
  387. avctx->frame_size = 40 << 1;
  388. ctx->coded_sample_rate[0] = 0x08 + 1;
  389. ctx->fs = 0x0C + 1;
  390. break;
  391. case 44100 << 2:
  392. ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
  393. avctx->frame_size = 40 << 2;
  394. ctx->coded_sample_rate[0] = 0x08 + 2;
  395. ctx->fs = 0x10 + 1;
  396. break;
  397. case 48000 << 0:
  398. avctx->frame_size = 40 << 0;
  399. ctx->coded_sample_rate[0] = 0x00 + 0;
  400. ctx->fs = 0x08 + 2;
  401. break;
  402. case 48000 << 1:
  403. avctx->frame_size = 40 << 1;
  404. ctx->coded_sample_rate[0] = 0x00 + 1;
  405. ctx->fs = 0x0C + 2;
  406. break;
  407. case 48000 << 2:
  408. ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
  409. avctx->frame_size = 40 << 2;
  410. ctx->coded_sample_rate[0] = 0x00 + 2;
  411. ctx->fs = 0x10 + 2;
  412. break;
  413. default:
  414. av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d. Supported "
  415. "sample rates are 44100, 88200, 176400, 48000, "
  416. "96000, and 192000.\n", avctx->sample_rate);
  417. return AVERROR(EINVAL);
  418. }
  419. ctx->coded_sample_rate[1] = -1 & 0xf;
  420. /* TODO Keep count of bitrate and calculate real value. */
  421. ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
  422. /* TODO support more channels. */
  423. if (avctx->channels > 2) {
  424. av_log(avctx, AV_LOG_WARNING,
  425. "Only mono and stereo are supported at the moment.\n");
  426. }
  427. ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
  428. if (avctx->channels <= 2) {
  429. ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
  430. }
  431. switch (avctx->sample_fmt) {
  432. case AV_SAMPLE_FMT_S16:
  433. ctx->coded_sample_fmt[0] = BITS_16;
  434. ctx->wordlength = 16;
  435. avctx->bits_per_raw_sample = 16;
  436. break;
  437. /* TODO 20 bits: */
  438. case AV_SAMPLE_FMT_S32:
  439. ctx->coded_sample_fmt[0] = BITS_24;
  440. ctx->wordlength = 24;
  441. avctx->bits_per_raw_sample = 24;
  442. break;
  443. default:
  444. av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
  445. "Only 16- and 24-bit samples are supported.\n");
  446. return AVERROR(EINVAL);
  447. }
  448. ctx->coded_sample_fmt[1] = -1 & 0xf;
  449. ctx->dts = -avctx->frame_size;
  450. ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
  451. ctx->one_sample_buffer_size = avctx->frame_size
  452. * ctx->num_channels;
  453. /* TODO Let user pass major header interval as parameter. */
  454. ctx->max_restart_interval = MAJOR_HEADER_INTERVAL;
  455. ctx->max_codebook_search = 3;
  456. ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
  457. ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
  458. /* TODO Let user pass parameters for LPC filter. */
  459. size = avctx->frame_size * ctx->max_restart_interval;
  460. ctx->lpc_sample_buffer = av_malloc_array(size, sizeof(int32_t));
  461. if (!ctx->lpc_sample_buffer) {
  462. av_log(avctx, AV_LOG_ERROR,
  463. "Not enough memory for buffering samples.\n");
  464. return AVERROR(ENOMEM);
  465. }
  466. size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
  467. ctx->major_scratch_buffer = av_malloc_array(size, sizeof(int32_t));
  468. if (!ctx->major_scratch_buffer) {
  469. av_log(avctx, AV_LOG_ERROR,
  470. "Not enough memory for buffering samples.\n");
  471. return AVERROR(ENOMEM);
  472. }
  473. ctx->major_inout_buffer = av_malloc_array(size, sizeof(int32_t));
  474. if (!ctx->major_inout_buffer) {
  475. av_log(avctx, AV_LOG_ERROR,
  476. "Not enough memory for buffering samples.\n");
  477. return AVERROR(ENOMEM);
  478. }
  479. ff_mlp_init_crc();
  480. ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
  481. if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
  482. /* MLP */
  483. switch(avctx->channel_layout) {
  484. case AV_CH_LAYOUT_MONO:
  485. ctx->channel_arrangement = 0; break;
  486. case AV_CH_LAYOUT_STEREO:
  487. ctx->channel_arrangement = 1; break;
  488. case AV_CH_LAYOUT_2_1:
  489. ctx->channel_arrangement = 2; break;
  490. case AV_CH_LAYOUT_QUAD:
  491. ctx->channel_arrangement = 3; break;
  492. case AV_CH_LAYOUT_2POINT1:
  493. ctx->channel_arrangement = 4; break;
  494. case AV_CH_LAYOUT_SURROUND:
  495. ctx->channel_arrangement = 7; break;
  496. case AV_CH_LAYOUT_4POINT0:
  497. ctx->channel_arrangement = 8; break;
  498. case AV_CH_LAYOUT_5POINT0_BACK:
  499. ctx->channel_arrangement = 9; break;
  500. case AV_CH_LAYOUT_3POINT1:
  501. ctx->channel_arrangement = 10; break;
  502. case AV_CH_LAYOUT_4POINT1:
  503. ctx->channel_arrangement = 11; break;
  504. case AV_CH_LAYOUT_5POINT1_BACK:
  505. ctx->channel_arrangement = 12; break;
  506. default:
  507. av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
  508. return AVERROR(EINVAL);
  509. }
  510. ctx->flags = FLAGS_DVDA;
  511. ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
  512. ctx->summary_info = ff_mlp_ch_info[ctx->channel_arrangement].summary_info ;
  513. } else {
  514. /* TrueHD */
  515. switch(avctx->channel_layout) {
  516. case AV_CH_LAYOUT_STEREO:
  517. ctx->ch_modifier_thd0 = 0;
  518. ctx->ch_modifier_thd1 = 0;
  519. ctx->ch_modifier_thd2 = 0;
  520. ctx->channel_arrangement = 1;
  521. break;
  522. case AV_CH_LAYOUT_5POINT0_BACK:
  523. ctx->ch_modifier_thd0 = 1;
  524. ctx->ch_modifier_thd1 = 1;
  525. ctx->ch_modifier_thd2 = 1;
  526. ctx->channel_arrangement = 11;
  527. break;
  528. case AV_CH_LAYOUT_5POINT1_BACK:
  529. ctx->ch_modifier_thd0 = 2;
  530. ctx->ch_modifier_thd1 = 1;
  531. ctx->ch_modifier_thd2 = 2;
  532. ctx->channel_arrangement = 15;
  533. break;
  534. default:
  535. av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
  536. return AVERROR(EINVAL);
  537. }
  538. ctx->flags = 0;
  539. ctx->channel_occupancy = 0;
  540. ctx->summary_info = 0;
  541. }
  542. size = sizeof(unsigned int) * ctx->max_restart_interval;
  543. ctx->frame_size = av_malloc(size);
  544. if (!ctx->frame_size)
  545. return AVERROR(ENOMEM);
  546. ctx->max_output_bits = av_malloc(size);
  547. if (!ctx->max_output_bits)
  548. return AVERROR(ENOMEM);
  549. size = sizeof(int32_t)
  550. * ctx->num_substreams * ctx->max_restart_interval;
  551. ctx->lossless_check_data = av_malloc(size);
  552. if (!ctx->lossless_check_data)
  553. return AVERROR(ENOMEM);
  554. for (index = 0; index < ctx->restart_intervals; index++) {
  555. ctx->seq_offset[index] = sum;
  556. ctx->seq_size [index] = ((index + 1) * ctx->min_restart_interval) + 1;
  557. sum += ctx->seq_size[index];
  558. }
  559. ctx->sequence_size = sum;
  560. size = sizeof(ChannelParams)
  561. * ctx->restart_intervals * ctx->sequence_size * ctx->avctx->channels;
  562. ctx->channel_params = av_malloc(size);
  563. if (!ctx->channel_params) {
  564. av_log(avctx, AV_LOG_ERROR,
  565. "Not enough memory for analysis context.\n");
  566. return AVERROR(ENOMEM);
  567. }
  568. size = sizeof(DecodingParams)
  569. * ctx->restart_intervals * ctx->sequence_size * ctx->num_substreams;
  570. ctx->decoding_params = av_malloc(size);
  571. if (!ctx->decoding_params) {
  572. av_log(avctx, AV_LOG_ERROR,
  573. "Not enough memory for analysis context.\n");
  574. return AVERROR(ENOMEM);
  575. }
  576. for (substr = 0; substr < ctx->num_substreams; substr++) {
  577. RestartHeader *rh = &ctx->restart_header [substr];
  578. /* TODO see if noisegen_seed is really worth it. */
  579. rh->noisegen_seed = 0;
  580. rh->min_channel = 0;
  581. rh->max_channel = avctx->channels - 1;
  582. /* FIXME: this works for 1 and 2 channels, but check for more */
  583. rh->max_matrix_channel = rh->max_channel;
  584. }
  585. clear_channel_params(ctx, restart_channel_params);
  586. clear_decoding_params(ctx, restart_decoding_params);
  587. if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples,
  588. MLP_MAX_LPC_ORDER, FF_LPC_TYPE_LEVINSON)) < 0) {
  589. av_log(avctx, AV_LOG_ERROR,
  590. "Not enough memory for LPC context.\n");
  591. return ret;
  592. }
  593. ff_af_queue_init(avctx, &ctx->afq);
  594. return 0;
  595. }
  596. /****************************************************************************
  597. ****************** Functions that write to the bitstream *******************
  598. ****************************************************************************/
  599. /** Writes a major sync header to the bitstream. */
  600. static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
  601. {
  602. PutBitContext pb;
  603. init_put_bits(&pb, buf, buf_size);
  604. put_bits(&pb, 24, SYNC_MAJOR );
  605. if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
  606. put_bits(&pb, 8, SYNC_MLP );
  607. put_bits(&pb, 4, ctx->coded_sample_fmt [0]);
  608. put_bits(&pb, 4, ctx->coded_sample_fmt [1]);
  609. put_bits(&pb, 4, ctx->coded_sample_rate[0]);
  610. put_bits(&pb, 4, ctx->coded_sample_rate[1]);
  611. put_bits(&pb, 4, 0 ); /* ignored */
  612. put_bits(&pb, 4, 0 ); /* multi_channel_type */
  613. put_bits(&pb, 3, 0 ); /* ignored */
  614. put_bits(&pb, 5, ctx->channel_arrangement );
  615. } else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
  616. put_bits(&pb, 8, SYNC_TRUEHD );
  617. put_bits(&pb, 4, ctx->coded_sample_rate[0]);
  618. put_bits(&pb, 4, 0 ); /* ignored */
  619. put_bits(&pb, 2, ctx->ch_modifier_thd0 );
  620. put_bits(&pb, 2, ctx->ch_modifier_thd1 );
  621. put_bits(&pb, 5, ctx->channel_arrangement );
  622. put_bits(&pb, 2, ctx->ch_modifier_thd2 );
  623. put_bits(&pb, 13, ctx->channel_arrangement );
  624. }
  625. put_bits(&pb, 16, MAJOR_SYNC_INFO_SIGNATURE);
  626. put_bits(&pb, 16, ctx->flags );
  627. put_bits(&pb, 16, 0 ); /* ignored */
  628. put_bits(&pb, 1, 1 ); /* is_vbr */
  629. put_bits(&pb, 15, ctx->coded_peak_bitrate );
  630. put_bits(&pb, 4, 1 ); /* num_substreams */
  631. put_bits(&pb, 4, 0x1 ); /* ignored */
  632. /* channel_meaning */
  633. put_bits(&pb, 8, ctx->substream_info );
  634. put_bits(&pb, 5, ctx->fs );
  635. put_bits(&pb, 5, ctx->wordlength );
  636. put_bits(&pb, 6, ctx->channel_occupancy );
  637. put_bits(&pb, 3, 0 ); /* ignored */
  638. put_bits(&pb, 10, 0 ); /* speaker_layout */
  639. put_bits(&pb, 3, 0 ); /* copy_protection */
  640. put_bits(&pb, 16, 0x8080 ); /* ignored */
  641. put_bits(&pb, 7, 0 ); /* ignored */
  642. put_bits(&pb, 4, 0 ); /* source_format */
  643. put_bits(&pb, 5, ctx->summary_info );
  644. flush_put_bits(&pb);
  645. AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
  646. }
  647. /** Writes a restart header to the bitstream. Damaged streams can start being
  648. * decoded losslessly again after such a header and the subsequent decoding
  649. * params header.
  650. */
  651. static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
  652. {
  653. RestartHeader *rh = ctx->cur_restart_header;
  654. uint8_t lossless_check = xor_32_to_8(rh->lossless_check_data);
  655. unsigned int start_count = put_bits_count(pb);
  656. PutBitContext tmpb;
  657. uint8_t checksum;
  658. unsigned int ch;
  659. put_bits(pb, 14, 0x31ea ); /* TODO 0x31eb */
  660. put_bits(pb, 16, ctx->timestamp );
  661. put_bits(pb, 4, rh->min_channel );
  662. put_bits(pb, 4, rh->max_channel );
  663. put_bits(pb, 4, rh->max_matrix_channel);
  664. put_bits(pb, 4, rh->noise_shift );
  665. put_bits(pb, 23, rh->noisegen_seed );
  666. put_bits(pb, 4, 0 ); /* TODO max_shift */
  667. put_bits(pb, 5, rh->max_huff_lsbs );
  668. put_bits(pb, 5, rh->max_output_bits );
  669. put_bits(pb, 5, rh->max_output_bits );
  670. put_bits(pb, 1, rh->data_check_present);
  671. put_bits(pb, 8, lossless_check );
  672. put_bits(pb, 16, 0 ); /* ignored */
  673. for (ch = 0; ch <= rh->max_matrix_channel; ch++)
  674. put_bits(pb, 6, ch);
  675. /* Data must be flushed for the checksum to be correct. */
  676. tmpb = *pb;
  677. flush_put_bits(&tmpb);
  678. checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
  679. put_bits(pb, 8, checksum);
  680. }
  681. /** Writes matrix params for all primitive matrices to the bitstream. */
  682. static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
  683. {
  684. DecodingParams *dp = ctx->cur_decoding_params;
  685. MatrixParams *mp = &dp->matrix_params;
  686. unsigned int mat;
  687. put_bits(pb, 4, mp->count);
  688. for (mat = 0; mat < mp->count; mat++) {
  689. unsigned int channel;
  690. put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
  691. put_bits(pb, 4, mp->fbits[mat]);
  692. put_bits(pb, 1, 0 ); /* lsb_bypass */
  693. for (channel = 0; channel < ctx->num_channels; channel++) {
  694. int32_t coeff = mp->coeff[mat][channel];
  695. if (coeff) {
  696. put_bits(pb, 1, 1);
  697. coeff >>= 14 - mp->fbits[mat];
  698. put_sbits(pb, mp->fbits[mat] + 2, coeff);
  699. } else {
  700. put_bits(pb, 1, 0);
  701. }
  702. }
  703. }
  704. }
  705. /** Writes filter parameters for one filter to the bitstream. */
  706. static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb,
  707. unsigned int channel, unsigned int filter)
  708. {
  709. FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
  710. put_bits(pb, 4, fp->order);
  711. if (fp->order > 0) {
  712. int i;
  713. int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
  714. put_bits(pb, 4, fp->shift );
  715. put_bits(pb, 5, fp->coeff_bits );
  716. put_bits(pb, 3, fp->coeff_shift);
  717. for (i = 0; i < fp->order; i++) {
  718. put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
  719. }
  720. /* TODO state data for IIR filter. */
  721. put_bits(pb, 1, 0);
  722. }
  723. }
  724. /** Writes decoding parameters to the bitstream. These change very often,
  725. * usually at almost every frame.
  726. */
  727. static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
  728. int params_changed)
  729. {
  730. DecodingParams *dp = ctx->cur_decoding_params;
  731. RestartHeader *rh = ctx->cur_restart_header;
  732. MatrixParams *mp = &dp->matrix_params;
  733. unsigned int ch;
  734. if (dp->param_presence_flags != PARAMS_DEFAULT &&
  735. params_changed & PARAM_PRESENCE_FLAGS) {
  736. put_bits(pb, 1, 1);
  737. put_bits(pb, 8, dp->param_presence_flags);
  738. } else {
  739. put_bits(pb, 1, 0);
  740. }
  741. if (dp->param_presence_flags & PARAM_BLOCKSIZE) {
  742. if (params_changed & PARAM_BLOCKSIZE) {
  743. put_bits(pb, 1, 1);
  744. put_bits(pb, 9, dp->blocksize);
  745. } else {
  746. put_bits(pb, 1, 0);
  747. }
  748. }
  749. if (dp->param_presence_flags & PARAM_MATRIX) {
  750. if (params_changed & PARAM_MATRIX) {
  751. put_bits(pb, 1, 1);
  752. write_matrix_params(ctx, pb);
  753. } else {
  754. put_bits(pb, 1, 0);
  755. }
  756. }
  757. if (dp->param_presence_flags & PARAM_OUTSHIFT) {
  758. if (params_changed & PARAM_OUTSHIFT) {
  759. put_bits(pb, 1, 1);
  760. for (ch = 0; ch <= rh->max_matrix_channel; ch++)
  761. put_sbits(pb, 4, mp->shift[ch]);
  762. } else {
  763. put_bits(pb, 1, 0);
  764. }
  765. }
  766. if (dp->param_presence_flags & PARAM_QUANTSTEP) {
  767. if (params_changed & PARAM_QUANTSTEP) {
  768. put_bits(pb, 1, 1);
  769. for (ch = 0; ch <= rh->max_channel; ch++)
  770. put_bits(pb, 4, dp->quant_step_size[ch]);
  771. } else {
  772. put_bits(pb, 1, 0);
  773. }
  774. }
  775. for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
  776. ChannelParams *cp = &ctx->cur_channel_params[ch];
  777. if (dp->param_presence_flags & 0xF) {
  778. put_bits(pb, 1, 1);
  779. if (dp->param_presence_flags & PARAM_FIR) {
  780. if (params_changed & PARAM_FIR) {
  781. put_bits(pb, 1, 1);
  782. write_filter_params(ctx, pb, ch, FIR);
  783. } else {
  784. put_bits(pb, 1, 0);
  785. }
  786. }
  787. if (dp->param_presence_flags & PARAM_IIR) {
  788. if (params_changed & PARAM_IIR) {
  789. put_bits(pb, 1, 1);
  790. write_filter_params(ctx, pb, ch, IIR);
  791. } else {
  792. put_bits(pb, 1, 0);
  793. }
  794. }
  795. if (dp->param_presence_flags & PARAM_HUFFOFFSET) {
  796. if (params_changed & PARAM_HUFFOFFSET) {
  797. put_bits (pb, 1, 1);
  798. put_sbits(pb, 15, cp->huff_offset);
  799. } else {
  800. put_bits(pb, 1, 0);
  801. }
  802. }
  803. if (cp->codebook > 0 && cp->huff_lsbs > 24) {
  804. av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs\n");
  805. }
  806. put_bits(pb, 2, cp->codebook );
  807. put_bits(pb, 5, cp->huff_lsbs);
  808. } else {
  809. put_bits(pb, 1, 0);
  810. }
  811. }
  812. }
  813. /** Writes the residuals to the bitstream. That is, the VLC codes from the
  814. * codebooks (if any is used), and then the residual.
  815. */
  816. static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
  817. {
  818. DecodingParams *dp = ctx->cur_decoding_params;
  819. RestartHeader *rh = ctx->cur_restart_header;
  820. int32_t *sample_buffer = ctx->write_buffer;
  821. int32_t sign_huff_offset[MAX_CHANNELS];
  822. int codebook_index [MAX_CHANNELS];
  823. int lsb_bits [MAX_CHANNELS];
  824. unsigned int i, ch;
  825. for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
  826. ChannelParams *cp = &ctx->cur_channel_params[ch];
  827. int sign_shift;
  828. lsb_bits [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
  829. codebook_index [ch] = cp->codebook - 1;
  830. sign_huff_offset[ch] = cp->huff_offset;
  831. sign_shift = lsb_bits[ch] + (cp->codebook ? 2 - cp->codebook : -1);
  832. if (cp->codebook > 0)
  833. sign_huff_offset[ch] -= 7 << lsb_bits[ch];
  834. /* Unsign if needed. */
  835. if (sign_shift >= 0)
  836. sign_huff_offset[ch] -= 1 << sign_shift;
  837. }
  838. for (i = 0; i < dp->blocksize; i++) {
  839. for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
  840. int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
  841. sample -= sign_huff_offset[ch];
  842. if (codebook_index[ch] >= 0) {
  843. int vlc = sample >> lsb_bits[ch];
  844. put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
  845. ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
  846. }
  847. put_sbits(pb, lsb_bits[ch], sample);
  848. }
  849. sample_buffer += 2; /* noise channels */
  850. }
  851. ctx->write_buffer = sample_buffer;
  852. }
  853. /** Writes the substreams data to the bitstream. */
  854. static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
  855. int restart_frame,
  856. uint16_t substream_data_len[MAX_SUBSTREAMS])
  857. {
  858. int32_t *lossless_check_data = ctx->lossless_check_data;
  859. unsigned int substr;
  860. int end = 0;
  861. lossless_check_data += ctx->frame_index * ctx->num_substreams;
  862. for (substr = 0; substr < ctx->num_substreams; substr++) {
  863. unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
  864. unsigned int num_subblocks = ctx->major_filter_state_subblock;
  865. unsigned int subblock;
  866. RestartHeader *rh = &ctx->restart_header [substr];
  867. int substr_restart_frame = restart_frame;
  868. uint8_t parity, checksum;
  869. PutBitContext pb;
  870. int params_changed;
  871. ctx->cur_restart_header = rh;
  872. init_put_bits(&pb, buf, buf_size);
  873. for (subblock = 0; subblock <= num_subblocks; subblock++) {
  874. unsigned int subblock_index;
  875. subblock_index = cur_subblock_index++;
  876. ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
  877. ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
  878. params_changed = ctx->major_params_changed[subblock_index][substr];
  879. if (substr_restart_frame || params_changed) {
  880. put_bits(&pb, 1, 1);
  881. if (substr_restart_frame) {
  882. put_bits(&pb, 1, 1);
  883. write_restart_header(ctx, &pb);
  884. rh->lossless_check_data = 0;
  885. } else {
  886. put_bits(&pb, 1, 0);
  887. }
  888. write_decoding_params(ctx, &pb, params_changed);
  889. } else {
  890. put_bits(&pb, 1, 0);
  891. }
  892. write_block_data(ctx, &pb);
  893. put_bits(&pb, 1, !substr_restart_frame);
  894. substr_restart_frame = 0;
  895. }
  896. put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
  897. rh->lossless_check_data ^= *lossless_check_data++;
  898. if (ctx->last_frame == ctx->inout_buffer) {
  899. /* TODO find a sample and implement shorten_by. */
  900. put_bits(&pb, 32, END_OF_STREAM);
  901. }
  902. /* Data must be flushed for the checksum and parity to be correct;
  903. * notice that we already are word-aligned here. */
  904. flush_put_bits(&pb);
  905. parity = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
  906. checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3);
  907. put_bits(&pb, 8, parity );
  908. put_bits(&pb, 8, checksum);
  909. flush_put_bits(&pb);
  910. end += put_bits_count(&pb) >> 3;
  911. substream_data_len[substr] = end;
  912. buf += put_bits_count(&pb) >> 3;
  913. }
  914. ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
  915. ctx->major_filter_state_subblock = 0;
  916. return buf;
  917. }
  918. /** Writes the access unit and substream headers to the bitstream. */
  919. static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header,
  920. uint8_t *substream_headers, unsigned int length,
  921. int restart_frame,
  922. uint16_t substream_data_len[MAX_SUBSTREAMS])
  923. {
  924. uint16_t access_unit_header = 0;
  925. uint16_t parity_nibble = 0;
  926. unsigned int substr;
  927. parity_nibble = ctx->dts;
  928. parity_nibble ^= length;
  929. for (substr = 0; substr < ctx->num_substreams; substr++) {
  930. uint16_t substr_hdr = 0;
  931. substr_hdr |= (0 << 15); /* extraword */
  932. substr_hdr |= (!restart_frame << 14); /* !restart_frame */
  933. substr_hdr |= (1 << 13); /* checkdata */
  934. substr_hdr |= (0 << 12); /* ??? */
  935. substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
  936. AV_WB16(substream_headers, substr_hdr);
  937. parity_nibble ^= *substream_headers++;
  938. parity_nibble ^= *substream_headers++;
  939. }
  940. parity_nibble ^= parity_nibble >> 8;
  941. parity_nibble ^= parity_nibble >> 4;
  942. parity_nibble &= 0xF;
  943. access_unit_header |= (parity_nibble ^ 0xF) << 12;
  944. access_unit_header |= length & 0xFFF;
  945. AV_WB16(frame_header , access_unit_header);
  946. AV_WB16(frame_header+2, ctx->dts );
  947. }
  948. /** Writes an entire access unit to the bitstream. */
  949. static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
  950. int buf_size, int restart_frame)
  951. {
  952. uint16_t substream_data_len[MAX_SUBSTREAMS];
  953. uint8_t *buf1, *buf0 = buf;
  954. unsigned int substr;
  955. int total_length;
  956. if (buf_size < 4)
  957. return AVERROR(EINVAL);
  958. /* Frame header will be written at the end. */
  959. buf += 4;
  960. buf_size -= 4;
  961. if (restart_frame) {
  962. if (buf_size < 28)
  963. return AVERROR(EINVAL);
  964. write_major_sync(ctx, buf, buf_size);
  965. buf += 28;
  966. buf_size -= 28;
  967. }
  968. buf1 = buf;
  969. /* Substream headers will be written at the end. */
  970. for (substr = 0; substr < ctx->num_substreams; substr++) {
  971. buf += 2;
  972. buf_size -= 2;
  973. }
  974. buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
  975. total_length = buf - buf0;
  976. write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
  977. return total_length;
  978. }
  979. /****************************************************************************
  980. ****************** Functions that input data to context ********************
  981. ****************************************************************************/
  982. /** Inputs data from the samples passed by lavc into the context, shifts them
  983. * appropriately depending on the bit-depth, and calculates the
  984. * lossless_check_data that will be written to the restart header.
  985. */
  986. static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
  987. int is24)
  988. {
  989. int32_t *lossless_check_data = ctx->lossless_check_data;
  990. const int32_t *samples_32 = (const int32_t *) samples;
  991. const int16_t *samples_16 = (const int16_t *) samples;
  992. unsigned int substr;
  993. lossless_check_data += ctx->frame_index * ctx->num_substreams;
  994. for (substr = 0; substr < ctx->num_substreams; substr++) {
  995. RestartHeader *rh = &ctx->restart_header [substr];
  996. int32_t *sample_buffer = ctx->inout_buffer;
  997. int32_t temp_lossless_check_data = 0;
  998. uint32_t greatest = 0;
  999. unsigned int channel;
  1000. int i;
  1001. for (i = 0; i < ctx->frame_size[ctx->frame_index]; i++) {
  1002. for (channel = 0; channel <= rh->max_channel; channel++) {
  1003. uint32_t abs_sample;
  1004. int32_t sample;
  1005. sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
  1006. /* TODO Find out if number_sbits can be used for negative values. */
  1007. abs_sample = FFABS(sample);
  1008. if (greatest < abs_sample)
  1009. greatest = abs_sample;
  1010. temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
  1011. *sample_buffer++ = sample;
  1012. }
  1013. sample_buffer += 2; /* noise channels */
  1014. }
  1015. ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
  1016. *lossless_check_data++ = temp_lossless_check_data;
  1017. }
  1018. }
  1019. /** Wrapper function for inputting data in two different bit-depths. */
  1020. static void input_data(MLPEncodeContext *ctx, void *samples)
  1021. {
  1022. if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1023. input_data_internal(ctx, samples, 1);
  1024. else
  1025. input_data_internal(ctx, samples, 0);
  1026. }
  1027. static void input_to_sample_buffer(MLPEncodeContext *ctx)
  1028. {
  1029. int32_t *sample_buffer = ctx->sample_buffer;
  1030. unsigned int index;
  1031. for (index = 0; index < ctx->number_of_frames; index++) {
  1032. unsigned int cur_index = (ctx->starting_frame_index + index) % ctx->max_restart_interval;
  1033. int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
  1034. unsigned int i, channel;
  1035. for (i = 0; i < ctx->frame_size[cur_index]; i++) {
  1036. for (channel = 0; channel < ctx->avctx->channels; channel++)
  1037. *sample_buffer++ = *input_buffer++;
  1038. sample_buffer += 2; /* noise_channels */
  1039. input_buffer += 2; /* noise_channels */
  1040. }
  1041. }
  1042. }
  1043. /****************************************************************************
  1044. ********* Functions that analyze the data and set the parameters ***********
  1045. ****************************************************************************/
  1046. /** Counts the number of trailing zeroes in a value */
  1047. static int number_trailing_zeroes(int32_t sample)
  1048. {
  1049. int bits;
  1050. for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
  1051. /* All samples are 0. TODO Return previous quant_step_size to avoid
  1052. * writing a new header. */
  1053. if (bits == 24)
  1054. return 0;
  1055. return bits;
  1056. }
  1057. /** Determines how many bits are zero at the end of all samples so they can be
  1058. * shifted out.
  1059. */
  1060. static void determine_quant_step_size(MLPEncodeContext *ctx)
  1061. {
  1062. DecodingParams *dp = ctx->cur_decoding_params;
  1063. RestartHeader *rh = ctx->cur_restart_header;
  1064. MatrixParams *mp = &dp->matrix_params;
  1065. int32_t *sample_buffer = ctx->sample_buffer;
  1066. int32_t sample_mask[MAX_CHANNELS];
  1067. unsigned int channel;
  1068. int i;
  1069. memset(sample_mask, 0x00, sizeof(sample_mask));
  1070. for (i = 0; i < ctx->number_of_samples; i++) {
  1071. for (channel = 0; channel <= rh->max_channel; channel++)
  1072. sample_mask[channel] |= *sample_buffer++;
  1073. sample_buffer += 2; /* noise channels */
  1074. }
  1075. for (channel = 0; channel <= rh->max_channel; channel++)
  1076. dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
  1077. }
  1078. /** Determines the smallest number of bits needed to encode the filter
  1079. * coefficients, and if it's possible to right-shift their values without
  1080. * losing any precision.
  1081. */
  1082. static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
  1083. {
  1084. int min = INT_MAX, max = INT_MIN;
  1085. int bits, shift;
  1086. int coeff_mask = 0;
  1087. int order;
  1088. for (order = 0; order < fp->order; order++) {
  1089. int coeff = fcoeff[order];
  1090. if (coeff < min)
  1091. min = coeff;
  1092. if (coeff > max)
  1093. max = coeff;
  1094. coeff_mask |= coeff;
  1095. }
  1096. bits = FFMAX(number_sbits(min), number_sbits(max));
  1097. for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
  1098. fp->coeff_bits = bits;
  1099. fp->coeff_shift = shift;
  1100. }
  1101. /** Determines the best filter parameters for the given data and writes the
  1102. * necessary information to the context.
  1103. * TODO Add IIR filter predictor!
  1104. */
  1105. static void set_filter_params(MLPEncodeContext *ctx,
  1106. unsigned int channel, unsigned int filter,
  1107. int clear_filter)
  1108. {
  1109. ChannelParams *cp = &ctx->cur_channel_params[channel];
  1110. FilterParams *fp = &cp->filter_params[filter];
  1111. if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
  1112. clear_filter) {
  1113. fp->order = 0;
  1114. } else if (filter == IIR) {
  1115. fp->order = 0;
  1116. } else if (filter == FIR) {
  1117. const int max_order = (ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE)
  1118. ? 4 : MLP_MAX_LPC_ORDER;
  1119. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1120. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  1121. int32_t *lpc_samples = ctx->lpc_sample_buffer;
  1122. int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
  1123. int shift[MLP_MAX_LPC_ORDER];
  1124. unsigned int i;
  1125. int order;
  1126. for (i = 0; i < ctx->number_of_samples; i++) {
  1127. *lpc_samples++ = *sample_buffer;
  1128. sample_buffer += ctx->num_channels;
  1129. }
  1130. order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
  1131. ctx->number_of_samples, MLP_MIN_LPC_ORDER,
  1132. max_order, 11, coefs, shift, FF_LPC_TYPE_LEVINSON, 0,
  1133. ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT,
  1134. MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
  1135. fp->order = order;
  1136. fp->shift = shift[order-1];
  1137. for (i = 0; i < order; i++)
  1138. fcoeff[i] = coefs[order-1][i];
  1139. code_filter_coeffs(ctx, fp, fcoeff);
  1140. }
  1141. }
  1142. /** Tries to determine a good prediction filter, and applies it to the samples
  1143. * buffer if the filter is good enough. Sets the filter data to be cleared if
  1144. * no good filter was found.
  1145. */
  1146. static void determine_filters(MLPEncodeContext *ctx)
  1147. {
  1148. RestartHeader *rh = ctx->cur_restart_header;
  1149. int channel, filter;
  1150. for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
  1151. for (filter = 0; filter < NUM_FILTERS; filter++)
  1152. set_filter_params(ctx, channel, filter, 0);
  1153. }
  1154. }
  1155. enum MLPChMode {
  1156. MLP_CHMODE_LEFT_RIGHT,
  1157. MLP_CHMODE_LEFT_SIDE,
  1158. MLP_CHMODE_RIGHT_SIDE,
  1159. MLP_CHMODE_MID_SIDE,
  1160. };
  1161. static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
  1162. {
  1163. uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
  1164. int32_t *right_ch = ctx->sample_buffer + 1;
  1165. int32_t *left_ch = ctx->sample_buffer;
  1166. int i;
  1167. enum MLPChMode best = 0;
  1168. for(i = 2; i < ctx->number_of_samples; i++) {
  1169. int32_t left = left_ch [i * ctx->num_channels] - 2 * left_ch [(i - 1) * ctx->num_channels] + left_ch [(i - 2) * ctx->num_channels];
  1170. int32_t right = right_ch[i * ctx->num_channels] - 2 * right_ch[(i - 1) * ctx->num_channels] + right_ch[(i - 2) * ctx->num_channels];
  1171. sum[0] += FFABS( left );
  1172. sum[1] += FFABS( right);
  1173. sum[2] += FFABS((left + right) >> 1);
  1174. sum[3] += FFABS( left - right);
  1175. }
  1176. score[MLP_CHMODE_LEFT_RIGHT] = sum[0] + sum[1];
  1177. score[MLP_CHMODE_LEFT_SIDE] = sum[0] + sum[3];
  1178. score[MLP_CHMODE_RIGHT_SIDE] = sum[1] + sum[3];
  1179. score[MLP_CHMODE_MID_SIDE] = sum[2] + sum[3];
  1180. for(i = 1; i < 3; i++)
  1181. if(score[i] < score[best])
  1182. best = i;
  1183. return best;
  1184. }
  1185. /** Determines how many fractional bits are needed to encode matrix
  1186. * coefficients. Also shifts the coefficients to fit within 2.14 bits.
  1187. */
  1188. static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
  1189. {
  1190. DecodingParams *dp = ctx->cur_decoding_params;
  1191. MatrixParams *mp = &dp->matrix_params;
  1192. int32_t coeff_mask = 0;
  1193. unsigned int channel;
  1194. unsigned int bits;
  1195. for (channel = 0; channel < ctx->num_channels; channel++) {
  1196. int32_t coeff = mp->coeff[mat][channel];
  1197. coeff_mask |= coeff;
  1198. }
  1199. for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
  1200. mp->fbits [mat] = 14 - bits;
  1201. }
  1202. /** Determines best coefficients to use for the lossless matrix. */
  1203. static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
  1204. {
  1205. DecodingParams *dp = ctx->cur_decoding_params;
  1206. MatrixParams *mp = &dp->matrix_params;
  1207. unsigned int shift = 0;
  1208. unsigned int channel;
  1209. int mat;
  1210. enum MLPChMode mode;
  1211. /* No decorrelation for non-stereo. */
  1212. if (ctx->num_channels - 2 != 2) {
  1213. mp->count = 0;
  1214. return;
  1215. }
  1216. mode = estimate_stereo_mode(ctx);
  1217. switch(mode) {
  1218. /* TODO: add matrix for MID_SIDE */
  1219. case MLP_CHMODE_MID_SIDE:
  1220. case MLP_CHMODE_LEFT_RIGHT:
  1221. mp->count = 0;
  1222. break;
  1223. case MLP_CHMODE_LEFT_SIDE:
  1224. mp->count = 1;
  1225. mp->outch[0] = 1;
  1226. mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = -(1 << 14);
  1227. mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
  1228. mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
  1229. mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
  1230. break;
  1231. case MLP_CHMODE_RIGHT_SIDE:
  1232. mp->count = 1;
  1233. mp->outch[0] = 0;
  1234. mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = 1 << 14;
  1235. mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
  1236. mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
  1237. mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
  1238. break;
  1239. }
  1240. for (mat = 0; mat < mp->count; mat++)
  1241. code_matrix_coeffs(ctx, mat);
  1242. for (channel = 0; channel < ctx->num_channels; channel++)
  1243. mp->shift[channel] = shift;
  1244. }
  1245. /** Min and max values that can be encoded with each codebook. The values for
  1246. * the third codebook take into account the fact that the sign shift for this
  1247. * codebook is outside the coded value, so it has one more bit of precision.
  1248. * It should actually be -7 -> 7, shifted down by 0.5.
  1249. */
  1250. static const int codebook_extremes[3][2] = {
  1251. {-9, 8}, {-8, 7}, {-15, 14},
  1252. };
  1253. /** Determines the amount of bits needed to encode the samples using no
  1254. * codebooks and a specified offset.
  1255. */
  1256. static void no_codebook_bits_offset(MLPEncodeContext *ctx,
  1257. unsigned int channel, int16_t offset,
  1258. int32_t min, int32_t max,
  1259. BestOffset *bo)
  1260. {
  1261. DecodingParams *dp = ctx->cur_decoding_params;
  1262. int32_t unsign = 0;
  1263. int lsb_bits;
  1264. min -= offset;
  1265. max -= offset;
  1266. lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
  1267. lsb_bits += !!lsb_bits;
  1268. if (lsb_bits > 0)
  1269. unsign = 1 << (lsb_bits - 1);
  1270. bo->offset = offset;
  1271. bo->lsb_bits = lsb_bits;
  1272. bo->bitcount = lsb_bits * dp->blocksize;
  1273. bo->min = offset - unsign + 1;
  1274. bo->max = offset + unsign;
  1275. }
  1276. /** Determines the least amount of bits needed to encode the samples using no
  1277. * codebooks.
  1278. */
  1279. static void no_codebook_bits(MLPEncodeContext *ctx,
  1280. unsigned int channel,
  1281. int32_t min, int32_t max,
  1282. BestOffset *bo)
  1283. {
  1284. DecodingParams *dp = ctx->cur_decoding_params;
  1285. int16_t offset;
  1286. int32_t unsign = 0;
  1287. uint32_t diff;
  1288. int lsb_bits;
  1289. /* Set offset inside huffoffset's boundaries by adjusting extremes
  1290. * so that more bits are used, thus shifting the offset. */
  1291. if (min < HUFF_OFFSET_MIN)
  1292. max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
  1293. if (max > HUFF_OFFSET_MAX)
  1294. min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
  1295. /* Determine offset and minimum number of bits. */
  1296. diff = max - min;
  1297. lsb_bits = number_sbits(diff) - 1;
  1298. if (lsb_bits > 0)
  1299. unsign = 1 << (lsb_bits - 1);
  1300. /* If all samples are the same (lsb_bits == 0), offset must be
  1301. * adjusted because of sign_shift. */
  1302. offset = min + diff / 2 + !!lsb_bits;
  1303. bo->offset = offset;
  1304. bo->lsb_bits = lsb_bits;
  1305. bo->bitcount = lsb_bits * dp->blocksize;
  1306. bo->min = max - unsign + 1;
  1307. bo->max = min + unsign;
  1308. }
  1309. /** Determines the least amount of bits needed to encode the samples using a
  1310. * given codebook and a given offset.
  1311. */
  1312. static inline void codebook_bits_offset(MLPEncodeContext *ctx,
  1313. unsigned int channel, int codebook,
  1314. int32_t sample_min, int32_t sample_max,
  1315. int16_t offset, BestOffset *bo)
  1316. {
  1317. int32_t codebook_min = codebook_extremes[codebook][0];
  1318. int32_t codebook_max = codebook_extremes[codebook][1];
  1319. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1320. DecodingParams *dp = ctx->cur_decoding_params;
  1321. int codebook_offset = 7 + (2 - codebook);
  1322. int32_t unsign_offset = offset;
  1323. int lsb_bits = 0, bitcount = 0;
  1324. int offset_min = INT_MAX, offset_max = INT_MAX;
  1325. int unsign, mask;
  1326. int i;
  1327. sample_min -= offset;
  1328. sample_max -= offset;
  1329. while (sample_min < codebook_min || sample_max > codebook_max) {
  1330. lsb_bits++;
  1331. sample_min >>= 1;
  1332. sample_max >>= 1;
  1333. }
  1334. unsign = 1 << lsb_bits;
  1335. mask = unsign - 1;
  1336. if (codebook == 2) {
  1337. unsign_offset -= unsign;
  1338. lsb_bits++;
  1339. }
  1340. for (i = 0; i < dp->blocksize; i++) {
  1341. int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
  1342. int temp_min, temp_max;
  1343. sample -= unsign_offset;
  1344. temp_min = sample & mask;
  1345. if (temp_min < offset_min)
  1346. offset_min = temp_min;
  1347. temp_max = unsign - temp_min - 1;
  1348. if (temp_max < offset_max)
  1349. offset_max = temp_max;
  1350. sample >>= lsb_bits;
  1351. bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
  1352. sample_buffer += ctx->num_channels;
  1353. }
  1354. bo->offset = offset;
  1355. bo->lsb_bits = lsb_bits;
  1356. bo->bitcount = lsb_bits * dp->blocksize + bitcount;
  1357. bo->min = FFMAX(offset - offset_min, HUFF_OFFSET_MIN);
  1358. bo->max = FFMIN(offset + offset_max, HUFF_OFFSET_MAX);
  1359. }
  1360. /** Determines the least amount of bits needed to encode the samples using a
  1361. * given codebook. Searches for the best offset to minimize the bits.
  1362. */
  1363. static inline void codebook_bits(MLPEncodeContext *ctx,
  1364. unsigned int channel, int codebook,
  1365. int offset, int32_t min, int32_t max,
  1366. BestOffset *bo, int direction)
  1367. {
  1368. int previous_count = INT_MAX;
  1369. int offset_min, offset_max;
  1370. int is_greater = 0;
  1371. offset_min = FFMAX(min, HUFF_OFFSET_MIN);
  1372. offset_max = FFMIN(max, HUFF_OFFSET_MAX);
  1373. while (offset <= offset_max && offset >= offset_min) {
  1374. BestOffset temp_bo;
  1375. codebook_bits_offset(ctx, channel, codebook,
  1376. min, max, offset,
  1377. &temp_bo);
  1378. if (temp_bo.bitcount < previous_count) {
  1379. if (temp_bo.bitcount < bo->bitcount)
  1380. *bo = temp_bo;
  1381. is_greater = 0;
  1382. } else if (++is_greater >= ctx->max_codebook_search)
  1383. break;
  1384. previous_count = temp_bo.bitcount;
  1385. if (direction) {
  1386. offset = temp_bo.max + 1;
  1387. } else {
  1388. offset = temp_bo.min - 1;
  1389. }
  1390. }
  1391. }
  1392. /** Determines the least amount of bits needed to encode the samples using
  1393. * any or no codebook.
  1394. */
  1395. static void determine_bits(MLPEncodeContext *ctx)
  1396. {
  1397. DecodingParams *dp = ctx->cur_decoding_params;
  1398. RestartHeader *rh = ctx->cur_restart_header;
  1399. unsigned int channel;
  1400. for (channel = 0; channel <= rh->max_channel; channel++) {
  1401. ChannelParams *cp = &ctx->cur_channel_params[channel];
  1402. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1403. int32_t min = INT32_MAX, max = INT32_MIN;
  1404. int no_filters_used = !cp->filter_params[FIR].order;
  1405. int average = 0;
  1406. int offset = 0;
  1407. int i;
  1408. /* Determine extremes and average. */
  1409. for (i = 0; i < dp->blocksize; i++) {
  1410. int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
  1411. if (sample < min)
  1412. min = sample;
  1413. if (sample > max)
  1414. max = sample;
  1415. average += sample;
  1416. sample_buffer += ctx->num_channels;
  1417. }
  1418. average /= dp->blocksize;
  1419. /* If filtering is used, we always set the offset to zero, otherwise
  1420. * we search for the offset that minimizes the bitcount. */
  1421. if (no_filters_used) {
  1422. no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
  1423. offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
  1424. } else {
  1425. no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
  1426. }
  1427. for (i = 1; i < NUM_CODEBOOKS; i++) {
  1428. BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
  1429. int16_t offset_max;
  1430. codebook_bits_offset(ctx, channel, i - 1,
  1431. min, max, offset,
  1432. &temp_bo);
  1433. if (no_filters_used) {
  1434. offset_max = temp_bo.max;
  1435. codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
  1436. min, max, &temp_bo, 0);
  1437. codebook_bits(ctx, channel, i - 1, offset_max + 1,
  1438. min, max, &temp_bo, 1);
  1439. }
  1440. ctx->cur_best_offset[channel][i] = temp_bo;
  1441. }
  1442. }
  1443. }
  1444. /****************************************************************************
  1445. *************** Functions that process the data in some way ****************
  1446. ****************************************************************************/
  1447. #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
  1448. #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
  1449. #define MSB_MASK(bits) (-(int)(1u << (bits)))
  1450. /** Applies the filter to the current samples, and saves the residual back
  1451. * into the samples buffer. If the filter is too bad and overflows the
  1452. * maximum amount of bits allowed (24), the samples buffer is left as is and
  1453. * the function returns -1.
  1454. */
  1455. static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
  1456. {
  1457. FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
  1458. &ctx->cur_channel_params[channel].filter_params[IIR], };
  1459. int32_t *filter_state_buffer[NUM_FILTERS] = { NULL };
  1460. int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
  1461. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1462. unsigned int number_of_samples = ctx->number_of_samples;
  1463. unsigned int filter_shift = fp[FIR]->shift;
  1464. int filter;
  1465. int i, ret = 0;
  1466. for (i = 0; i < NUM_FILTERS; i++) {
  1467. unsigned int size = ctx->number_of_samples;
  1468. filter_state_buffer[i] = av_malloc(size*sizeof(int32_t));
  1469. if (!filter_state_buffer[i]) {
  1470. av_log(ctx->avctx, AV_LOG_ERROR,
  1471. "Not enough memory for applying filters.\n");
  1472. ret = AVERROR(ENOMEM);
  1473. goto free_and_return;
  1474. }
  1475. }
  1476. for (i = 0; i < 8; i++) {
  1477. filter_state_buffer[FIR][i] = *sample_buffer;
  1478. filter_state_buffer[IIR][i] = *sample_buffer;
  1479. sample_buffer += ctx->num_channels;
  1480. }
  1481. for (i = 8; i < number_of_samples; i++) {
  1482. int32_t sample = *sample_buffer;
  1483. unsigned int order;
  1484. int64_t accum = 0;
  1485. int64_t residual;
  1486. for (filter = 0; filter < NUM_FILTERS; filter++) {
  1487. int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
  1488. for (order = 0; order < fp[filter]->order; order++)
  1489. accum += (int64_t)filter_state_buffer[filter][i - 1 - order] *
  1490. fcoeff[order];
  1491. }
  1492. accum >>= filter_shift;
  1493. residual = sample - (accum & mask);
  1494. if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {
  1495. ret = AVERROR_INVALIDDATA;
  1496. goto free_and_return;
  1497. }
  1498. filter_state_buffer[FIR][i] = sample;
  1499. filter_state_buffer[IIR][i] = (int32_t) residual;
  1500. sample_buffer += ctx->num_channels;
  1501. }
  1502. sample_buffer = ctx->sample_buffer + channel;
  1503. for (i = 0; i < number_of_samples; i++) {
  1504. *sample_buffer = filter_state_buffer[IIR][i];
  1505. sample_buffer += ctx->num_channels;
  1506. }
  1507. free_and_return:
  1508. for (i = 0; i < NUM_FILTERS; i++) {
  1509. av_freep(&filter_state_buffer[i]);
  1510. }
  1511. return ret;
  1512. }
  1513. static void apply_filters(MLPEncodeContext *ctx)
  1514. {
  1515. RestartHeader *rh = ctx->cur_restart_header;
  1516. int channel;
  1517. for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
  1518. if (apply_filter(ctx, channel) < 0) {
  1519. /* Filter is horribly wrong.
  1520. * Clear filter params and update state. */
  1521. set_filter_params(ctx, channel, FIR, 1);
  1522. set_filter_params(ctx, channel, IIR, 1);
  1523. apply_filter(ctx, channel);
  1524. }
  1525. }
  1526. }
  1527. /** Generates two noise channels worth of data. */
  1528. static void generate_2_noise_channels(MLPEncodeContext *ctx)
  1529. {
  1530. int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
  1531. RestartHeader *rh = ctx->cur_restart_header;
  1532. unsigned int i;
  1533. uint32_t seed = rh->noisegen_seed;
  1534. for (i = 0; i < ctx->number_of_samples; i++) {
  1535. uint16_t seed_shr7 = seed >> 7;
  1536. *sample_buffer++ = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
  1537. *sample_buffer++ = ((int8_t) seed_shr7) * (1 << rh->noise_shift);
  1538. seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
  1539. sample_buffer += ctx->num_channels - 2;
  1540. }
  1541. rh->noisegen_seed = seed & ((1 << 24)-1);
  1542. }
  1543. /** Rematrixes all channels using chosen coefficients. */
  1544. static void rematrix_channels(MLPEncodeContext *ctx)
  1545. {
  1546. DecodingParams *dp = ctx->cur_decoding_params;
  1547. MatrixParams *mp = &dp->matrix_params;
  1548. int32_t *sample_buffer = ctx->sample_buffer;
  1549. unsigned int mat, i, maxchan;
  1550. maxchan = ctx->num_channels;
  1551. for (mat = 0; mat < mp->count; mat++) {
  1552. unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
  1553. int32_t mask = MSB_MASK(msb_mask_bits);
  1554. unsigned int outch = mp->outch[mat];
  1555. sample_buffer = ctx->sample_buffer;
  1556. for (i = 0; i < ctx->number_of_samples; i++) {
  1557. unsigned int src_ch;
  1558. int64_t accum = 0;
  1559. for (src_ch = 0; src_ch < maxchan; src_ch++) {
  1560. int32_t sample = *(sample_buffer + src_ch);
  1561. accum += (int64_t) sample * mp->forco[mat][src_ch];
  1562. }
  1563. sample_buffer[outch] = (accum >> 14) & mask;
  1564. sample_buffer += ctx->num_channels;
  1565. }
  1566. }
  1567. }
  1568. /****************************************************************************
  1569. **** Functions that deal with determining the best parameters and output ***
  1570. ****************************************************************************/
  1571. typedef struct {
  1572. char path[MAJOR_HEADER_INTERVAL + 2];
  1573. int cur_idx;
  1574. int bitcount;
  1575. } PathCounter;
  1576. #define CODEBOOK_CHANGE_BITS 21
  1577. static void clear_path_counter(PathCounter *path_counter)
  1578. {
  1579. memset(path_counter, 0, (NUM_CODEBOOKS + 1) * sizeof(*path_counter));
  1580. }
  1581. static int compare_best_offset(BestOffset *prev, BestOffset *cur)
  1582. {
  1583. if (prev->lsb_bits != cur->lsb_bits)
  1584. return 1;
  1585. return 0;
  1586. }
  1587. static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
  1588. PathCounter *src, int cur_codebook)
  1589. {
  1590. int idx = src->cur_idx;
  1591. BestOffset *cur_bo = ctx->best_offset[idx][channel],
  1592. *prev_bo = idx ? ctx->best_offset[idx - 1][channel] : restart_best_offset;
  1593. int bitcount = src->bitcount;
  1594. int prev_codebook = src->path[idx];
  1595. bitcount += cur_bo[cur_codebook].bitcount;
  1596. if (prev_codebook != cur_codebook ||
  1597. compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
  1598. bitcount += CODEBOOK_CHANGE_BITS;
  1599. return bitcount;
  1600. }
  1601. static void set_best_codebook(MLPEncodeContext *ctx)
  1602. {
  1603. DecodingParams *dp = ctx->cur_decoding_params;
  1604. RestartHeader *rh = ctx->cur_restart_header;
  1605. unsigned int channel;
  1606. for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
  1607. BestOffset *cur_bo, *prev_bo = restart_best_offset;
  1608. PathCounter path_counter[NUM_CODEBOOKS + 1];
  1609. unsigned int best_codebook;
  1610. unsigned int index;
  1611. char *best_path;
  1612. clear_path_counter(path_counter);
  1613. for (index = 0; index < ctx->number_of_subblocks; index++) {
  1614. unsigned int best_bitcount = INT_MAX;
  1615. unsigned int codebook;
  1616. cur_bo = ctx->best_offset[index][channel];
  1617. for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
  1618. int prev_best_bitcount = INT_MAX;
  1619. int last_best;
  1620. for (last_best = 0; last_best < 2; last_best++) {
  1621. PathCounter *dst_path = &path_counter[codebook];
  1622. PathCounter *src_path;
  1623. int temp_bitcount;
  1624. /* First test last path with same headers,
  1625. * then with last best. */
  1626. if (last_best) {
  1627. src_path = &path_counter[NUM_CODEBOOKS];
  1628. } else {
  1629. if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
  1630. continue;
  1631. else
  1632. src_path = &path_counter[codebook];
  1633. }
  1634. temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
  1635. if (temp_bitcount < best_bitcount) {
  1636. best_bitcount = temp_bitcount;
  1637. best_codebook = codebook;
  1638. }
  1639. if (temp_bitcount < prev_best_bitcount) {
  1640. prev_best_bitcount = temp_bitcount;
  1641. if (src_path != dst_path)
  1642. memcpy(dst_path, src_path, sizeof(PathCounter));
  1643. if (dst_path->cur_idx < FF_ARRAY_ELEMS(dst_path->path) - 1)
  1644. dst_path->path[++dst_path->cur_idx] = codebook;
  1645. dst_path->bitcount = temp_bitcount;
  1646. }
  1647. }
  1648. }
  1649. prev_bo = cur_bo;
  1650. memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
  1651. }
  1652. best_path = path_counter[NUM_CODEBOOKS].path + 1;
  1653. /* Update context. */
  1654. for (index = 0; index < ctx->number_of_subblocks; index++) {
  1655. ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
  1656. best_codebook = *best_path++;
  1657. cur_bo = &ctx->best_offset[index][channel][best_codebook];
  1658. cp->huff_offset = cur_bo->offset;
  1659. cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];
  1660. cp->codebook = best_codebook;
  1661. }
  1662. }
  1663. }
  1664. /** Analyzes all collected bitcounts and selects the best parameters for each
  1665. * individual access unit.
  1666. * TODO This is just a stub!
  1667. */
  1668. static void set_major_params(MLPEncodeContext *ctx)
  1669. {
  1670. RestartHeader *rh = ctx->cur_restart_header;
  1671. unsigned int index;
  1672. unsigned int substr;
  1673. uint8_t max_huff_lsbs = 0;
  1674. uint8_t max_output_bits = 0;
  1675. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1676. DecodingParams *seq_dp = (DecodingParams *) ctx->decoding_params+
  1677. (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
  1678. (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
  1679. ChannelParams *seq_cp = (ChannelParams *) ctx->channel_params +
  1680. (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
  1681. (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
  1682. unsigned int channel;
  1683. for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
  1684. memcpy(&ctx->major_decoding_params[index][substr], seq_dp + index*(ctx->num_substreams) + substr, sizeof(DecodingParams));
  1685. for (channel = 0; channel < ctx->avctx->channels; channel++) {
  1686. uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
  1687. if (max_huff_lsbs < huff_lsbs)
  1688. max_huff_lsbs = huff_lsbs;
  1689. memcpy(&ctx->major_channel_params[index][channel],
  1690. (seq_cp + index*(ctx->avctx->channels) + channel),
  1691. sizeof(ChannelParams));
  1692. }
  1693. }
  1694. }
  1695. rh->max_huff_lsbs = max_huff_lsbs;
  1696. for (index = 0; index < ctx->number_of_frames; index++)
  1697. if (max_output_bits < ctx->max_output_bits[index])
  1698. max_output_bits = ctx->max_output_bits[index];
  1699. rh->max_output_bits = max_output_bits;
  1700. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1701. ctx->cur_restart_header = &ctx->restart_header[substr];
  1702. ctx->prev_decoding_params = &restart_decoding_params[substr];
  1703. ctx->prev_channel_params = restart_channel_params;
  1704. for (index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
  1705. ctx->cur_decoding_params = &ctx->major_decoding_params[index][substr];
  1706. ctx->cur_channel_params = ctx->major_channel_params[index];
  1707. ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
  1708. ctx->prev_decoding_params = ctx->cur_decoding_params;
  1709. ctx->prev_channel_params = ctx->cur_channel_params;
  1710. }
  1711. }
  1712. ctx->major_number_of_subblocks = ctx->number_of_subblocks;
  1713. ctx->major_filter_state_subblock = 1;
  1714. ctx->major_cur_subblock_index = 0;
  1715. }
  1716. static void analyze_sample_buffer(MLPEncodeContext *ctx)
  1717. {
  1718. ChannelParams *seq_cp = ctx->seq_channel_params;
  1719. DecodingParams *seq_dp = ctx->seq_decoding_params;
  1720. unsigned int index;
  1721. unsigned int substr;
  1722. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1723. ctx->cur_restart_header = &ctx->restart_header[substr];
  1724. ctx->cur_decoding_params = seq_dp + 1*(ctx->num_substreams) + substr;
  1725. ctx->cur_channel_params = seq_cp + 1*(ctx->avctx->channels);
  1726. determine_quant_step_size(ctx);
  1727. generate_2_noise_channels(ctx);
  1728. lossless_matrix_coeffs (ctx);
  1729. rematrix_channels (ctx);
  1730. determine_filters (ctx);
  1731. apply_filters (ctx);
  1732. copy_restart_frame_params(ctx, substr);
  1733. /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
  1734. * decoding_params[0] is for the filter state subblock.
  1735. */
  1736. for (index = 0; index < ctx->number_of_frames; index++) {
  1737. DecodingParams *dp = seq_dp + (index + 1)*(ctx->num_substreams) + substr;
  1738. dp->blocksize = ctx->frame_size[index];
  1739. }
  1740. /* The official encoder seems to always encode a filter state subblock
  1741. * even if there are no filters. TODO check if it is possible to skip
  1742. * the filter state subblock for no filters.
  1743. */
  1744. (seq_dp + substr)->blocksize = 8;
  1745. (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
  1746. for (index = 0; index < ctx->number_of_subblocks; index++) {
  1747. ctx->cur_decoding_params = seq_dp + index*(ctx->num_substreams) + substr;
  1748. ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
  1749. ctx->cur_best_offset = ctx->best_offset[index];
  1750. determine_bits(ctx);
  1751. ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
  1752. }
  1753. set_best_codebook(ctx);
  1754. }
  1755. }
  1756. static void process_major_frame(MLPEncodeContext *ctx)
  1757. {
  1758. unsigned int substr;
  1759. ctx->sample_buffer = ctx->major_inout_buffer;
  1760. ctx->starting_frame_index = 0;
  1761. ctx->number_of_frames = ctx->major_number_of_frames;
  1762. ctx->number_of_samples = ctx->major_frame_size;
  1763. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1764. ctx->cur_restart_header = &ctx->restart_header[substr];
  1765. ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
  1766. ctx->cur_channel_params = ctx->major_channel_params[1];
  1767. generate_2_noise_channels(ctx);
  1768. rematrix_channels (ctx);
  1769. apply_filters(ctx);
  1770. }
  1771. }
  1772. /****************************************************************************/
  1773. static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  1774. const AVFrame *frame, int *got_packet)
  1775. {
  1776. MLPEncodeContext *ctx = avctx->priv_data;
  1777. unsigned int bytes_written = 0;
  1778. int restart_frame, ret;
  1779. uint8_t *data;
  1780. if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
  1781. return ret;
  1782. /* add current frame to queue */
  1783. if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
  1784. return ret;
  1785. data = frame->data[0];
  1786. ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
  1787. ctx->inout_buffer = ctx->major_inout_buffer
  1788. + ctx->frame_index * ctx->one_sample_buffer_size;
  1789. if (ctx->last_frame == ctx->inout_buffer) {
  1790. return 0;
  1791. }
  1792. ctx->sample_buffer = ctx->major_scratch_buffer
  1793. + ctx->frame_index * ctx->one_sample_buffer_size;
  1794. ctx->write_buffer = ctx->inout_buffer;
  1795. if (avctx->frame_number < ctx->max_restart_interval) {
  1796. if (data) {
  1797. goto input_and_return;
  1798. } else {
  1799. /* There are less frames than the requested major header interval.
  1800. * Update the context to reflect this.
  1801. */
  1802. ctx->max_restart_interval = avctx->frame_number;
  1803. ctx->frame_index = 0;
  1804. ctx->sample_buffer = ctx->major_scratch_buffer;
  1805. ctx->inout_buffer = ctx->major_inout_buffer;
  1806. }
  1807. }
  1808. if (ctx->frame_size[ctx->frame_index] > MAX_BLOCKSIZE) {
  1809. av_log(avctx, AV_LOG_ERROR, "Invalid frame size (%d > %d)\n",
  1810. ctx->frame_size[ctx->frame_index], MAX_BLOCKSIZE);
  1811. return AVERROR_INVALIDDATA;
  1812. }
  1813. restart_frame = !ctx->frame_index;
  1814. if (restart_frame) {
  1815. set_major_params(ctx);
  1816. if (ctx->min_restart_interval != ctx->max_restart_interval)
  1817. process_major_frame(ctx);
  1818. }
  1819. if (ctx->min_restart_interval == ctx->max_restart_interval)
  1820. ctx->write_buffer = ctx->sample_buffer;
  1821. bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
  1822. ctx->timestamp += ctx->frame_size[ctx->frame_index];
  1823. ctx->dts += ctx->frame_size[ctx->frame_index];
  1824. input_and_return:
  1825. if (data) {
  1826. ctx->frame_size[ctx->frame_index] = avctx->frame_size;
  1827. ctx->next_major_frame_size += avctx->frame_size;
  1828. ctx->next_major_number_of_frames++;
  1829. input_data(ctx, data);
  1830. } else if (!ctx->last_frame) {
  1831. ctx->last_frame = ctx->inout_buffer;
  1832. }
  1833. restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
  1834. if (!restart_frame) {
  1835. int seq_index;
  1836. for (seq_index = 0;
  1837. seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
  1838. seq_index++) {
  1839. unsigned int number_of_samples = 0;
  1840. unsigned int index;
  1841. ctx->sample_buffer = ctx->major_scratch_buffer;
  1842. ctx->inout_buffer = ctx->major_inout_buffer;
  1843. ctx->seq_index = seq_index;
  1844. ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
  1845. - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
  1846. ctx->number_of_frames = ctx->next_major_number_of_frames;
  1847. ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
  1848. ctx->seq_channel_params = (ChannelParams *) ctx->channel_params +
  1849. (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->avctx->channels) +
  1850. (ctx->seq_offset[seq_index])*(ctx->avctx->channels);
  1851. ctx->seq_decoding_params = (DecodingParams *) ctx->decoding_params +
  1852. (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->num_substreams) +
  1853. (ctx->seq_offset[seq_index])*(ctx->num_substreams);
  1854. for (index = 0; index < ctx->number_of_frames; index++) {
  1855. number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
  1856. }
  1857. ctx->number_of_samples = number_of_samples;
  1858. for (index = 0; index < ctx->seq_size[seq_index]; index++) {
  1859. clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels));
  1860. default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams));
  1861. }
  1862. input_to_sample_buffer(ctx);
  1863. analyze_sample_buffer(ctx);
  1864. }
  1865. if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
  1866. ctx->major_frame_size = ctx->next_major_frame_size;
  1867. ctx->next_major_frame_size = 0;
  1868. ctx->major_number_of_frames = ctx->next_major_number_of_frames;
  1869. ctx->next_major_number_of_frames = 0;
  1870. if (!ctx->major_frame_size)
  1871. goto no_data_left;
  1872. }
  1873. }
  1874. no_data_left:
  1875. ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
  1876. &avpkt->duration);
  1877. avpkt->size = bytes_written;
  1878. *got_packet = 1;
  1879. return 0;
  1880. }
  1881. static av_cold int mlp_encode_close(AVCodecContext *avctx)
  1882. {
  1883. MLPEncodeContext *ctx = avctx->priv_data;
  1884. ff_lpc_end(&ctx->lpc_ctx);
  1885. av_freep(&ctx->lossless_check_data);
  1886. av_freep(&ctx->major_scratch_buffer);
  1887. av_freep(&ctx->major_inout_buffer);
  1888. av_freep(&ctx->lpc_sample_buffer);
  1889. av_freep(&ctx->decoding_params);
  1890. av_freep(&ctx->channel_params);
  1891. av_freep(&ctx->frame_size);
  1892. av_freep(&ctx->max_output_bits);
  1893. ff_af_queue_close(&ctx->afq);
  1894. return 0;
  1895. }
  1896. #if CONFIG_MLP_ENCODER
  1897. AVCodec ff_mlp_encoder = {
  1898. .name ="mlp",
  1899. .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
  1900. .type = AVMEDIA_TYPE_AUDIO,
  1901. .id = AV_CODEC_ID_MLP,
  1902. .priv_data_size = sizeof(MLPEncodeContext),
  1903. .init = mlp_encode_init,
  1904. .encode2 = mlp_encode_frame,
  1905. .close = mlp_encode_close,
  1906. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_EXPERIMENTAL,
  1907. .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
  1908. .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
  1909. .channel_layouts = ff_mlp_channel_layouts,
  1910. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1911. };
  1912. #endif
  1913. #if CONFIG_TRUEHD_ENCODER
  1914. AVCodec ff_truehd_encoder = {
  1915. .name ="truehd",
  1916. .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
  1917. .type = AVMEDIA_TYPE_AUDIO,
  1918. .id = AV_CODEC_ID_TRUEHD,
  1919. .priv_data_size = sizeof(MLPEncodeContext),
  1920. .init = mlp_encode_init,
  1921. .encode2 = mlp_encode_frame,
  1922. .close = mlp_encode_close,
  1923. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_EXPERIMENTAL,
  1924. .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
  1925. .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
  1926. .channel_layouts = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},
  1927. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  1928. };
  1929. #endif