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.

2410 lines
82KB

  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 -1;
  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 -1;
  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 -1;
  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 -1;
  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. put_bits(pb, 2, cp->codebook );
  804. put_bits(pb, 5, cp->huff_lsbs);
  805. } else {
  806. put_bits(pb, 1, 0);
  807. }
  808. }
  809. }
  810. /** Writes the residuals to the bitstream. That is, the VLC codes from the
  811. * codebooks (if any is used), and then the residual.
  812. */
  813. static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
  814. {
  815. DecodingParams *dp = ctx->cur_decoding_params;
  816. RestartHeader *rh = ctx->cur_restart_header;
  817. int32_t *sample_buffer = ctx->write_buffer;
  818. int32_t sign_huff_offset[MAX_CHANNELS];
  819. int codebook_index [MAX_CHANNELS];
  820. int lsb_bits [MAX_CHANNELS];
  821. unsigned int i, ch;
  822. for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
  823. ChannelParams *cp = &ctx->cur_channel_params[ch];
  824. int sign_shift;
  825. lsb_bits [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
  826. codebook_index [ch] = cp->codebook - 1;
  827. sign_huff_offset[ch] = cp->huff_offset;
  828. sign_shift = lsb_bits[ch] + (cp->codebook ? 2 - cp->codebook : -1);
  829. if (cp->codebook > 0)
  830. sign_huff_offset[ch] -= 7 << lsb_bits[ch];
  831. /* Unsign if needed. */
  832. if (sign_shift >= 0)
  833. sign_huff_offset[ch] -= 1 << sign_shift;
  834. }
  835. for (i = 0; i < dp->blocksize; i++) {
  836. for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
  837. int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
  838. sample -= sign_huff_offset[ch];
  839. if (codebook_index[ch] >= 0) {
  840. int vlc = sample >> lsb_bits[ch];
  841. put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
  842. ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
  843. }
  844. put_sbits(pb, lsb_bits[ch], sample);
  845. }
  846. sample_buffer += 2; /* noise channels */
  847. }
  848. ctx->write_buffer = sample_buffer;
  849. }
  850. /** Writes the substreams data to the bitstream. */
  851. static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
  852. int restart_frame,
  853. uint16_t substream_data_len[MAX_SUBSTREAMS])
  854. {
  855. int32_t *lossless_check_data = ctx->lossless_check_data;
  856. unsigned int substr;
  857. int end = 0;
  858. lossless_check_data += ctx->frame_index * ctx->num_substreams;
  859. for (substr = 0; substr < ctx->num_substreams; substr++) {
  860. unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
  861. unsigned int num_subblocks = ctx->major_filter_state_subblock;
  862. unsigned int subblock;
  863. RestartHeader *rh = &ctx->restart_header [substr];
  864. int substr_restart_frame = restart_frame;
  865. uint8_t parity, checksum;
  866. PutBitContext pb, tmpb;
  867. int params_changed;
  868. ctx->cur_restart_header = rh;
  869. init_put_bits(&pb, buf, buf_size);
  870. for (subblock = 0; subblock <= num_subblocks; subblock++) {
  871. unsigned int subblock_index;
  872. subblock_index = cur_subblock_index++;
  873. ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
  874. ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
  875. params_changed = ctx->major_params_changed[subblock_index][substr];
  876. if (substr_restart_frame || params_changed) {
  877. put_bits(&pb, 1, 1);
  878. if (substr_restart_frame) {
  879. put_bits(&pb, 1, 1);
  880. write_restart_header(ctx, &pb);
  881. rh->lossless_check_data = 0;
  882. } else {
  883. put_bits(&pb, 1, 0);
  884. }
  885. write_decoding_params(ctx, &pb, params_changed);
  886. } else {
  887. put_bits(&pb, 1, 0);
  888. }
  889. write_block_data(ctx, &pb);
  890. put_bits(&pb, 1, !substr_restart_frame);
  891. substr_restart_frame = 0;
  892. }
  893. put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
  894. rh->lossless_check_data ^= *lossless_check_data++;
  895. if (ctx->last_frame == ctx->inout_buffer) {
  896. /* TODO find a sample and implement shorten_by. */
  897. put_bits(&pb, 32, END_OF_STREAM);
  898. }
  899. /* Data must be flushed for the checksum and parity to be correct. */
  900. tmpb = pb;
  901. flush_put_bits(&tmpb);
  902. parity = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
  903. checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3);
  904. put_bits(&pb, 8, parity );
  905. put_bits(&pb, 8, checksum);
  906. flush_put_bits(&pb);
  907. end += put_bits_count(&pb) >> 3;
  908. substream_data_len[substr] = end;
  909. buf += put_bits_count(&pb) >> 3;
  910. }
  911. ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
  912. ctx->major_filter_state_subblock = 0;
  913. return buf;
  914. }
  915. /** Writes the access unit and substream headers to the bitstream. */
  916. static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header,
  917. uint8_t *substream_headers, unsigned int length,
  918. int restart_frame,
  919. uint16_t substream_data_len[MAX_SUBSTREAMS])
  920. {
  921. uint16_t access_unit_header = 0;
  922. uint16_t parity_nibble = 0;
  923. unsigned int substr;
  924. parity_nibble = ctx->dts;
  925. parity_nibble ^= length;
  926. for (substr = 0; substr < ctx->num_substreams; substr++) {
  927. uint16_t substr_hdr = 0;
  928. substr_hdr |= (0 << 15); /* extraword */
  929. substr_hdr |= (!restart_frame << 14); /* !restart_frame */
  930. substr_hdr |= (1 << 13); /* checkdata */
  931. substr_hdr |= (0 << 12); /* ??? */
  932. substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
  933. AV_WB16(substream_headers, substr_hdr);
  934. parity_nibble ^= *substream_headers++;
  935. parity_nibble ^= *substream_headers++;
  936. }
  937. parity_nibble ^= parity_nibble >> 8;
  938. parity_nibble ^= parity_nibble >> 4;
  939. parity_nibble &= 0xF;
  940. access_unit_header |= (parity_nibble ^ 0xF) << 12;
  941. access_unit_header |= length & 0xFFF;
  942. AV_WB16(frame_header , access_unit_header);
  943. AV_WB16(frame_header+2, ctx->dts );
  944. }
  945. /** Writes an entire access unit to the bitstream. */
  946. static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
  947. int buf_size, int restart_frame)
  948. {
  949. uint16_t substream_data_len[MAX_SUBSTREAMS];
  950. uint8_t *buf1, *buf0 = buf;
  951. unsigned int substr;
  952. int total_length;
  953. if (buf_size < 4)
  954. return -1;
  955. /* Frame header will be written at the end. */
  956. buf += 4;
  957. buf_size -= 4;
  958. if (restart_frame) {
  959. if (buf_size < 28)
  960. return -1;
  961. write_major_sync(ctx, buf, buf_size);
  962. buf += 28;
  963. buf_size -= 28;
  964. }
  965. buf1 = buf;
  966. /* Substream headers will be written at the end. */
  967. for (substr = 0; substr < ctx->num_substreams; substr++) {
  968. buf += 2;
  969. buf_size -= 2;
  970. }
  971. buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
  972. total_length = buf - buf0;
  973. write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
  974. return total_length;
  975. }
  976. /****************************************************************************
  977. ****************** Functions that input data to context ********************
  978. ****************************************************************************/
  979. /** Inputs data from the samples passed by lavc into the context, shifts them
  980. * appropriately depending on the bit-depth, and calculates the
  981. * lossless_check_data that will be written to the restart header.
  982. */
  983. static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
  984. int is24)
  985. {
  986. int32_t *lossless_check_data = ctx->lossless_check_data;
  987. const int32_t *samples_32 = (const int32_t *) samples;
  988. const int16_t *samples_16 = (const int16_t *) samples;
  989. unsigned int substr;
  990. lossless_check_data += ctx->frame_index * ctx->num_substreams;
  991. for (substr = 0; substr < ctx->num_substreams; substr++) {
  992. RestartHeader *rh = &ctx->restart_header [substr];
  993. int32_t *sample_buffer = ctx->inout_buffer;
  994. int32_t temp_lossless_check_data = 0;
  995. uint32_t greatest = 0;
  996. unsigned int channel;
  997. int i;
  998. for (i = 0; i < ctx->frame_size[ctx->frame_index]; i++) {
  999. for (channel = 0; channel <= rh->max_channel; channel++) {
  1000. uint32_t abs_sample;
  1001. int32_t sample;
  1002. sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
  1003. /* TODO Find out if number_sbits can be used for negative values. */
  1004. abs_sample = FFABS(sample);
  1005. if (greatest < abs_sample)
  1006. greatest = abs_sample;
  1007. temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
  1008. *sample_buffer++ = sample;
  1009. }
  1010. sample_buffer += 2; /* noise channels */
  1011. }
  1012. ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
  1013. *lossless_check_data++ = temp_lossless_check_data;
  1014. }
  1015. }
  1016. /** Wrapper function for inputting data in two different bit-depths. */
  1017. static void input_data(MLPEncodeContext *ctx, void *samples)
  1018. {
  1019. if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1020. input_data_internal(ctx, samples, 1);
  1021. else
  1022. input_data_internal(ctx, samples, 0);
  1023. }
  1024. static void input_to_sample_buffer(MLPEncodeContext *ctx)
  1025. {
  1026. int32_t *sample_buffer = ctx->sample_buffer;
  1027. unsigned int index;
  1028. for (index = 0; index < ctx->number_of_frames; index++) {
  1029. unsigned int cur_index = (ctx->starting_frame_index + index) % ctx->max_restart_interval;
  1030. int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
  1031. unsigned int i, channel;
  1032. for (i = 0; i < ctx->frame_size[cur_index]; i++) {
  1033. for (channel = 0; channel < ctx->avctx->channels; channel++)
  1034. *sample_buffer++ = *input_buffer++;
  1035. sample_buffer += 2; /* noise_channels */
  1036. input_buffer += 2; /* noise_channels */
  1037. }
  1038. }
  1039. }
  1040. /****************************************************************************
  1041. ********* Functions that analyze the data and set the parameters ***********
  1042. ****************************************************************************/
  1043. /** Counts the number of trailing zeroes in a value */
  1044. static int number_trailing_zeroes(int32_t sample)
  1045. {
  1046. int bits;
  1047. for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
  1048. /* All samples are 0. TODO Return previous quant_step_size to avoid
  1049. * writing a new header. */
  1050. if (bits == 24)
  1051. return 0;
  1052. return bits;
  1053. }
  1054. /** Determines how many bits are zero at the end of all samples so they can be
  1055. * shifted out.
  1056. */
  1057. static void determine_quant_step_size(MLPEncodeContext *ctx)
  1058. {
  1059. DecodingParams *dp = ctx->cur_decoding_params;
  1060. RestartHeader *rh = ctx->cur_restart_header;
  1061. MatrixParams *mp = &dp->matrix_params;
  1062. int32_t *sample_buffer = ctx->sample_buffer;
  1063. int32_t sample_mask[MAX_CHANNELS];
  1064. unsigned int channel;
  1065. int i;
  1066. memset(sample_mask, 0x00, sizeof(sample_mask));
  1067. for (i = 0; i < ctx->number_of_samples; i++) {
  1068. for (channel = 0; channel <= rh->max_channel; channel++)
  1069. sample_mask[channel] |= *sample_buffer++;
  1070. sample_buffer += 2; /* noise channels */
  1071. }
  1072. for (channel = 0; channel <= rh->max_channel; channel++)
  1073. dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
  1074. }
  1075. /** Determines the smallest number of bits needed to encode the filter
  1076. * coefficients, and if it's possible to right-shift their values without
  1077. * losing any precision.
  1078. */
  1079. static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
  1080. {
  1081. int min = INT_MAX, max = INT_MIN;
  1082. int bits, shift;
  1083. int coeff_mask = 0;
  1084. int order;
  1085. for (order = 0; order < fp->order; order++) {
  1086. int coeff = fcoeff[order];
  1087. if (coeff < min)
  1088. min = coeff;
  1089. if (coeff > max)
  1090. max = coeff;
  1091. coeff_mask |= coeff;
  1092. }
  1093. bits = FFMAX(number_sbits(min), number_sbits(max));
  1094. for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
  1095. fp->coeff_bits = bits;
  1096. fp->coeff_shift = shift;
  1097. }
  1098. /** Determines the best filter parameters for the given data and writes the
  1099. * necessary information to the context.
  1100. * TODO Add IIR filter predictor!
  1101. */
  1102. static void set_filter_params(MLPEncodeContext *ctx,
  1103. unsigned int channel, unsigned int filter,
  1104. int clear_filter)
  1105. {
  1106. ChannelParams *cp = &ctx->cur_channel_params[channel];
  1107. FilterParams *fp = &cp->filter_params[filter];
  1108. if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
  1109. clear_filter) {
  1110. fp->order = 0;
  1111. } else if (filter == IIR) {
  1112. fp->order = 0;
  1113. } else if (filter == FIR) {
  1114. const int max_order = (ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE)
  1115. ? 4 : MLP_MAX_LPC_ORDER;
  1116. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1117. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  1118. int32_t *lpc_samples = ctx->lpc_sample_buffer;
  1119. int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
  1120. int shift[MLP_MAX_LPC_ORDER];
  1121. unsigned int i;
  1122. int order;
  1123. for (i = 0; i < ctx->number_of_samples; i++) {
  1124. *lpc_samples++ = *sample_buffer;
  1125. sample_buffer += ctx->num_channels;
  1126. }
  1127. order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
  1128. ctx->number_of_samples, MLP_MIN_LPC_ORDER,
  1129. max_order, 11, coefs, shift, FF_LPC_TYPE_LEVINSON, 0,
  1130. ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT,
  1131. MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
  1132. fp->order = order;
  1133. fp->shift = shift[order-1];
  1134. for (i = 0; i < order; i++)
  1135. fcoeff[i] = coefs[order-1][i];
  1136. code_filter_coeffs(ctx, fp, fcoeff);
  1137. }
  1138. }
  1139. /** Tries to determine a good prediction filter, and applies it to the samples
  1140. * buffer if the filter is good enough. Sets the filter data to be cleared if
  1141. * no good filter was found.
  1142. */
  1143. static void determine_filters(MLPEncodeContext *ctx)
  1144. {
  1145. RestartHeader *rh = ctx->cur_restart_header;
  1146. int channel, filter;
  1147. for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
  1148. for (filter = 0; filter < NUM_FILTERS; filter++)
  1149. set_filter_params(ctx, channel, filter, 0);
  1150. }
  1151. }
  1152. enum MLPChMode {
  1153. MLP_CHMODE_LEFT_RIGHT,
  1154. MLP_CHMODE_LEFT_SIDE,
  1155. MLP_CHMODE_RIGHT_SIDE,
  1156. MLP_CHMODE_MID_SIDE,
  1157. };
  1158. static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
  1159. {
  1160. uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
  1161. int32_t *right_ch = ctx->sample_buffer + 1;
  1162. int32_t *left_ch = ctx->sample_buffer;
  1163. int i;
  1164. enum MLPChMode best = 0;
  1165. for(i = 2; i < ctx->number_of_samples; i++) {
  1166. int32_t left = left_ch [i * ctx->num_channels] - 2 * left_ch [(i - 1) * ctx->num_channels] + left_ch [(i - 2) * ctx->num_channels];
  1167. int32_t right = right_ch[i * ctx->num_channels] - 2 * right_ch[(i - 1) * ctx->num_channels] + right_ch[(i - 2) * ctx->num_channels];
  1168. sum[0] += FFABS( left );
  1169. sum[1] += FFABS( right);
  1170. sum[2] += FFABS((left + right) >> 1);
  1171. sum[3] += FFABS( left - right);
  1172. }
  1173. score[MLP_CHMODE_LEFT_RIGHT] = sum[0] + sum[1];
  1174. score[MLP_CHMODE_LEFT_SIDE] = sum[0] + sum[3];
  1175. score[MLP_CHMODE_RIGHT_SIDE] = sum[1] + sum[3];
  1176. score[MLP_CHMODE_MID_SIDE] = sum[2] + sum[3];
  1177. for(i = 1; i < 3; i++)
  1178. if(score[i] < score[best])
  1179. best = i;
  1180. return best;
  1181. }
  1182. /** Determines how many fractional bits are needed to encode matrix
  1183. * coefficients. Also shifts the coefficients to fit within 2.14 bits.
  1184. */
  1185. static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
  1186. {
  1187. DecodingParams *dp = ctx->cur_decoding_params;
  1188. MatrixParams *mp = &dp->matrix_params;
  1189. int32_t coeff_mask = 0;
  1190. unsigned int channel;
  1191. unsigned int bits;
  1192. for (channel = 0; channel < ctx->num_channels; channel++) {
  1193. int32_t coeff = mp->coeff[mat][channel];
  1194. coeff_mask |= coeff;
  1195. }
  1196. for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
  1197. mp->fbits [mat] = 14 - bits;
  1198. }
  1199. /** Determines best coefficients to use for the lossless matrix. */
  1200. static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
  1201. {
  1202. DecodingParams *dp = ctx->cur_decoding_params;
  1203. MatrixParams *mp = &dp->matrix_params;
  1204. unsigned int shift = 0;
  1205. unsigned int channel;
  1206. int mat;
  1207. enum MLPChMode mode;
  1208. /* No decorrelation for non-stereo. */
  1209. if (ctx->num_channels - 2 != 2) {
  1210. mp->count = 0;
  1211. return;
  1212. }
  1213. mode = estimate_stereo_mode(ctx);
  1214. switch(mode) {
  1215. /* TODO: add matrix for MID_SIDE */
  1216. case MLP_CHMODE_MID_SIDE:
  1217. case MLP_CHMODE_LEFT_RIGHT:
  1218. mp->count = 0;
  1219. break;
  1220. case MLP_CHMODE_LEFT_SIDE:
  1221. mp->count = 1;
  1222. mp->outch[0] = 1;
  1223. mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = -(1 << 14);
  1224. mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
  1225. mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
  1226. mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
  1227. break;
  1228. case MLP_CHMODE_RIGHT_SIDE:
  1229. mp->count = 1;
  1230. mp->outch[0] = 0;
  1231. mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = 1 << 14;
  1232. mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
  1233. mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
  1234. mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
  1235. break;
  1236. }
  1237. for (mat = 0; mat < mp->count; mat++)
  1238. code_matrix_coeffs(ctx, mat);
  1239. for (channel = 0; channel < ctx->num_channels; channel++)
  1240. mp->shift[channel] = shift;
  1241. }
  1242. /** Min and max values that can be encoded with each codebook. The values for
  1243. * the third codebook take into account the fact that the sign shift for this
  1244. * codebook is outside the coded value, so it has one more bit of precision.
  1245. * It should actually be -7 -> 7, shifted down by 0.5.
  1246. */
  1247. static const int codebook_extremes[3][2] = {
  1248. {-9, 8}, {-8, 7}, {-15, 14},
  1249. };
  1250. /** Determines the amount of bits needed to encode the samples using no
  1251. * codebooks and a specified offset.
  1252. */
  1253. static void no_codebook_bits_offset(MLPEncodeContext *ctx,
  1254. unsigned int channel, int16_t offset,
  1255. int32_t min, int32_t max,
  1256. BestOffset *bo)
  1257. {
  1258. DecodingParams *dp = ctx->cur_decoding_params;
  1259. int32_t unsign = 0;
  1260. int lsb_bits;
  1261. min -= offset;
  1262. max -= offset;
  1263. lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
  1264. lsb_bits += !!lsb_bits;
  1265. if (lsb_bits > 0)
  1266. unsign = 1 << (lsb_bits - 1);
  1267. bo->offset = offset;
  1268. bo->lsb_bits = lsb_bits;
  1269. bo->bitcount = lsb_bits * dp->blocksize;
  1270. bo->min = offset - unsign + 1;
  1271. bo->max = offset + unsign;
  1272. }
  1273. /** Determines the least amount of bits needed to encode the samples using no
  1274. * codebooks.
  1275. */
  1276. static void no_codebook_bits(MLPEncodeContext *ctx,
  1277. unsigned int channel,
  1278. int32_t min, int32_t max,
  1279. BestOffset *bo)
  1280. {
  1281. DecodingParams *dp = ctx->cur_decoding_params;
  1282. int16_t offset;
  1283. int32_t unsign = 0;
  1284. uint32_t diff;
  1285. int lsb_bits;
  1286. /* Set offset inside huffoffset's boundaries by adjusting extremes
  1287. * so that more bits are used, thus shifting the offset. */
  1288. if (min < HUFF_OFFSET_MIN)
  1289. max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
  1290. if (max > HUFF_OFFSET_MAX)
  1291. min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
  1292. /* Determine offset and minimum number of bits. */
  1293. diff = max - min;
  1294. lsb_bits = number_sbits(diff) - 1;
  1295. if (lsb_bits > 0)
  1296. unsign = 1 << (lsb_bits - 1);
  1297. /* If all samples are the same (lsb_bits == 0), offset must be
  1298. * adjusted because of sign_shift. */
  1299. offset = min + diff / 2 + !!lsb_bits;
  1300. bo->offset = offset;
  1301. bo->lsb_bits = lsb_bits;
  1302. bo->bitcount = lsb_bits * dp->blocksize;
  1303. bo->min = max - unsign + 1;
  1304. bo->max = min + unsign;
  1305. }
  1306. /** Determines the least amount of bits needed to encode the samples using a
  1307. * given codebook and a given offset.
  1308. */
  1309. static inline void codebook_bits_offset(MLPEncodeContext *ctx,
  1310. unsigned int channel, int codebook,
  1311. int32_t sample_min, int32_t sample_max,
  1312. int16_t offset, BestOffset *bo)
  1313. {
  1314. int32_t codebook_min = codebook_extremes[codebook][0];
  1315. int32_t codebook_max = codebook_extremes[codebook][1];
  1316. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1317. DecodingParams *dp = ctx->cur_decoding_params;
  1318. int codebook_offset = 7 + (2 - codebook);
  1319. int32_t unsign_offset = offset;
  1320. int lsb_bits = 0, bitcount = 0;
  1321. int offset_min = INT_MAX, offset_max = INT_MAX;
  1322. int unsign, mask;
  1323. int i;
  1324. sample_min -= offset;
  1325. sample_max -= offset;
  1326. while (sample_min < codebook_min || sample_max > codebook_max) {
  1327. lsb_bits++;
  1328. sample_min >>= 1;
  1329. sample_max >>= 1;
  1330. }
  1331. unsign = 1 << lsb_bits;
  1332. mask = unsign - 1;
  1333. if (codebook == 2) {
  1334. unsign_offset -= unsign;
  1335. lsb_bits++;
  1336. }
  1337. for (i = 0; i < dp->blocksize; i++) {
  1338. int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
  1339. int temp_min, temp_max;
  1340. sample -= unsign_offset;
  1341. temp_min = sample & mask;
  1342. if (temp_min < offset_min)
  1343. offset_min = temp_min;
  1344. temp_max = unsign - temp_min - 1;
  1345. if (temp_max < offset_max)
  1346. offset_max = temp_max;
  1347. sample >>= lsb_bits;
  1348. bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
  1349. sample_buffer += ctx->num_channels;
  1350. }
  1351. bo->offset = offset;
  1352. bo->lsb_bits = lsb_bits;
  1353. bo->bitcount = lsb_bits * dp->blocksize + bitcount;
  1354. bo->min = FFMAX(offset - offset_min, HUFF_OFFSET_MIN);
  1355. bo->max = FFMIN(offset + offset_max, HUFF_OFFSET_MAX);
  1356. }
  1357. /** Determines the least amount of bits needed to encode the samples using a
  1358. * given codebook. Searches for the best offset to minimize the bits.
  1359. */
  1360. static inline void codebook_bits(MLPEncodeContext *ctx,
  1361. unsigned int channel, int codebook,
  1362. int offset, int32_t min, int32_t max,
  1363. BestOffset *bo, int direction)
  1364. {
  1365. int previous_count = INT_MAX;
  1366. int offset_min, offset_max;
  1367. int is_greater = 0;
  1368. offset_min = FFMAX(min, HUFF_OFFSET_MIN);
  1369. offset_max = FFMIN(max, HUFF_OFFSET_MAX);
  1370. while (offset <= offset_max && offset >= offset_min) {
  1371. BestOffset temp_bo;
  1372. codebook_bits_offset(ctx, channel, codebook,
  1373. min, max, offset,
  1374. &temp_bo);
  1375. if (temp_bo.bitcount < previous_count) {
  1376. if (temp_bo.bitcount < bo->bitcount)
  1377. *bo = temp_bo;
  1378. is_greater = 0;
  1379. } else if (++is_greater >= ctx->max_codebook_search)
  1380. break;
  1381. previous_count = temp_bo.bitcount;
  1382. if (direction) {
  1383. offset = temp_bo.max + 1;
  1384. } else {
  1385. offset = temp_bo.min - 1;
  1386. }
  1387. }
  1388. }
  1389. /** Determines the least amount of bits needed to encode the samples using
  1390. * any or no codebook.
  1391. */
  1392. static void determine_bits(MLPEncodeContext *ctx)
  1393. {
  1394. DecodingParams *dp = ctx->cur_decoding_params;
  1395. RestartHeader *rh = ctx->cur_restart_header;
  1396. unsigned int channel;
  1397. for (channel = 0; channel <= rh->max_channel; channel++) {
  1398. ChannelParams *cp = &ctx->cur_channel_params[channel];
  1399. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1400. int32_t min = INT32_MAX, max = INT32_MIN;
  1401. int no_filters_used = !cp->filter_params[FIR].order;
  1402. int average = 0;
  1403. int offset = 0;
  1404. int i;
  1405. /* Determine extremes and average. */
  1406. for (i = 0; i < dp->blocksize; i++) {
  1407. int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
  1408. if (sample < min)
  1409. min = sample;
  1410. if (sample > max)
  1411. max = sample;
  1412. average += sample;
  1413. sample_buffer += ctx->num_channels;
  1414. }
  1415. average /= dp->blocksize;
  1416. /* If filtering is used, we always set the offset to zero, otherwise
  1417. * we search for the offset that minimizes the bitcount. */
  1418. if (no_filters_used) {
  1419. no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
  1420. offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
  1421. } else {
  1422. no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
  1423. }
  1424. for (i = 1; i < NUM_CODEBOOKS; i++) {
  1425. BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
  1426. int16_t offset_max;
  1427. codebook_bits_offset(ctx, channel, i - 1,
  1428. min, max, offset,
  1429. &temp_bo);
  1430. if (no_filters_used) {
  1431. offset_max = temp_bo.max;
  1432. codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
  1433. min, max, &temp_bo, 0);
  1434. codebook_bits(ctx, channel, i - 1, offset_max + 1,
  1435. min, max, &temp_bo, 1);
  1436. }
  1437. ctx->cur_best_offset[channel][i] = temp_bo;
  1438. }
  1439. }
  1440. }
  1441. /****************************************************************************
  1442. *************** Functions that process the data in some way ****************
  1443. ****************************************************************************/
  1444. #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
  1445. #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
  1446. #define MSB_MASK(bits) (-(int)(1u << (bits)))
  1447. /** Applies the filter to the current samples, and saves the residual back
  1448. * into the samples buffer. If the filter is too bad and overflows the
  1449. * maximum amount of bits allowed (24), the samples buffer is left as is and
  1450. * the function returns -1.
  1451. */
  1452. static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
  1453. {
  1454. FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
  1455. &ctx->cur_channel_params[channel].filter_params[IIR], };
  1456. int32_t *filter_state_buffer[NUM_FILTERS] = { NULL };
  1457. int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
  1458. int32_t *sample_buffer = ctx->sample_buffer + channel;
  1459. unsigned int number_of_samples = ctx->number_of_samples;
  1460. unsigned int filter_shift = fp[FIR]->shift;
  1461. int filter;
  1462. int i, ret = 0;
  1463. for (i = 0; i < NUM_FILTERS; i++) {
  1464. unsigned int size = ctx->number_of_samples;
  1465. filter_state_buffer[i] = av_malloc(size*sizeof(int32_t));
  1466. if (!filter_state_buffer[i]) {
  1467. av_log(ctx->avctx, AV_LOG_ERROR,
  1468. "Not enough memory for applying filters.\n");
  1469. return -1;
  1470. }
  1471. }
  1472. for (i = 0; i < 8; i++) {
  1473. filter_state_buffer[FIR][i] = *sample_buffer;
  1474. filter_state_buffer[IIR][i] = *sample_buffer;
  1475. sample_buffer += ctx->num_channels;
  1476. }
  1477. for (i = 8; i < number_of_samples; i++) {
  1478. int32_t sample = *sample_buffer;
  1479. unsigned int order;
  1480. int64_t accum = 0;
  1481. int64_t residual;
  1482. for (filter = 0; filter < NUM_FILTERS; filter++) {
  1483. int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
  1484. for (order = 0; order < fp[filter]->order; order++)
  1485. accum += (int64_t)filter_state_buffer[filter][i - 1 - order] *
  1486. fcoeff[order];
  1487. }
  1488. accum >>= filter_shift;
  1489. residual = sample - (accum & mask);
  1490. if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {
  1491. ret = -1;
  1492. goto free_and_return;
  1493. }
  1494. filter_state_buffer[FIR][i] = sample;
  1495. filter_state_buffer[IIR][i] = (int32_t) residual;
  1496. sample_buffer += ctx->num_channels;
  1497. }
  1498. sample_buffer = ctx->sample_buffer + channel;
  1499. for (i = 0; i < number_of_samples; i++) {
  1500. *sample_buffer = filter_state_buffer[IIR][i];
  1501. sample_buffer += ctx->num_channels;
  1502. }
  1503. free_and_return:
  1504. for (i = 0; i < NUM_FILTERS; i++) {
  1505. av_freep(&filter_state_buffer[i]);
  1506. }
  1507. return ret;
  1508. }
  1509. static void apply_filters(MLPEncodeContext *ctx)
  1510. {
  1511. RestartHeader *rh = ctx->cur_restart_header;
  1512. int channel;
  1513. for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
  1514. if (apply_filter(ctx, channel) < 0) {
  1515. /* Filter is horribly wrong.
  1516. * Clear filter params and update state. */
  1517. set_filter_params(ctx, channel, FIR, 1);
  1518. set_filter_params(ctx, channel, IIR, 1);
  1519. apply_filter(ctx, channel);
  1520. }
  1521. }
  1522. }
  1523. /** Generates two noise channels worth of data. */
  1524. static void generate_2_noise_channels(MLPEncodeContext *ctx)
  1525. {
  1526. int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
  1527. RestartHeader *rh = ctx->cur_restart_header;
  1528. unsigned int i;
  1529. uint32_t seed = rh->noisegen_seed;
  1530. for (i = 0; i < ctx->number_of_samples; i++) {
  1531. uint16_t seed_shr7 = seed >> 7;
  1532. *sample_buffer++ = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
  1533. *sample_buffer++ = ((int8_t) seed_shr7) * (1 << rh->noise_shift);
  1534. seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
  1535. sample_buffer += ctx->num_channels - 2;
  1536. }
  1537. rh->noisegen_seed = seed & ((1 << 24)-1);
  1538. }
  1539. /** Rematrixes all channels using chosen coefficients. */
  1540. static void rematrix_channels(MLPEncodeContext *ctx)
  1541. {
  1542. DecodingParams *dp = ctx->cur_decoding_params;
  1543. MatrixParams *mp = &dp->matrix_params;
  1544. int32_t *sample_buffer = ctx->sample_buffer;
  1545. unsigned int mat, i, maxchan;
  1546. maxchan = ctx->num_channels;
  1547. for (mat = 0; mat < mp->count; mat++) {
  1548. unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
  1549. int32_t mask = MSB_MASK(msb_mask_bits);
  1550. unsigned int outch = mp->outch[mat];
  1551. sample_buffer = ctx->sample_buffer;
  1552. for (i = 0; i < ctx->number_of_samples; i++) {
  1553. unsigned int src_ch;
  1554. int64_t accum = 0;
  1555. for (src_ch = 0; src_ch < maxchan; src_ch++) {
  1556. int32_t sample = *(sample_buffer + src_ch);
  1557. accum += (int64_t) sample * mp->forco[mat][src_ch];
  1558. }
  1559. sample_buffer[outch] = (accum >> 14) & mask;
  1560. sample_buffer += ctx->num_channels;
  1561. }
  1562. }
  1563. }
  1564. /****************************************************************************
  1565. **** Functions that deal with determining the best parameters and output ***
  1566. ****************************************************************************/
  1567. typedef struct {
  1568. char path[MAJOR_HEADER_INTERVAL + 3];
  1569. int bitcount;
  1570. } PathCounter;
  1571. static const char *path_counter_codebook[] = { "0", "1", "2", "3", };
  1572. #define ZERO_PATH '0'
  1573. #define CODEBOOK_CHANGE_BITS 21
  1574. static void clear_path_counter(PathCounter *path_counter)
  1575. {
  1576. unsigned int i;
  1577. for (i = 0; i < NUM_CODEBOOKS + 1; i++) {
  1578. path_counter[i].path[0] = ZERO_PATH;
  1579. path_counter[i].path[1] = 0x00;
  1580. path_counter[i].bitcount = 0;
  1581. }
  1582. }
  1583. static int compare_best_offset(BestOffset *prev, BestOffset *cur)
  1584. {
  1585. if (prev->lsb_bits != cur->lsb_bits)
  1586. return 1;
  1587. return 0;
  1588. }
  1589. static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
  1590. PathCounter *src, int cur_codebook)
  1591. {
  1592. BestOffset *cur_bo, *prev_bo = restart_best_offset;
  1593. int bitcount = src->bitcount;
  1594. char *path = src->path + 1;
  1595. int prev_codebook;
  1596. int i;
  1597. for (i = 0; path[i]; i++)
  1598. prev_bo = ctx->best_offset[i][channel];
  1599. prev_codebook = path[i - 1] - ZERO_PATH;
  1600. cur_bo = ctx->best_offset[i][channel];
  1601. bitcount += cur_bo[cur_codebook].bitcount;
  1602. if (prev_codebook != cur_codebook ||
  1603. compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
  1604. bitcount += CODEBOOK_CHANGE_BITS;
  1605. return bitcount;
  1606. }
  1607. static void set_best_codebook(MLPEncodeContext *ctx)
  1608. {
  1609. DecodingParams *dp = ctx->cur_decoding_params;
  1610. RestartHeader *rh = ctx->cur_restart_header;
  1611. unsigned int channel;
  1612. for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
  1613. BestOffset *cur_bo, *prev_bo = restart_best_offset;
  1614. PathCounter path_counter[NUM_CODEBOOKS + 1];
  1615. unsigned int best_codebook;
  1616. unsigned int index;
  1617. char *best_path;
  1618. clear_path_counter(path_counter);
  1619. for (index = 0; index < ctx->number_of_subblocks; index++) {
  1620. unsigned int best_bitcount = INT_MAX;
  1621. unsigned int codebook;
  1622. cur_bo = ctx->best_offset[index][channel];
  1623. for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
  1624. int prev_best_bitcount = INT_MAX;
  1625. int last_best;
  1626. for (last_best = 0; last_best < 2; last_best++) {
  1627. PathCounter *dst_path = &path_counter[codebook];
  1628. PathCounter *src_path;
  1629. int temp_bitcount;
  1630. /* First test last path with same headers,
  1631. * then with last best. */
  1632. if (last_best) {
  1633. src_path = &path_counter[NUM_CODEBOOKS];
  1634. } else {
  1635. if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
  1636. continue;
  1637. else
  1638. src_path = &path_counter[codebook];
  1639. }
  1640. temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
  1641. if (temp_bitcount < best_bitcount) {
  1642. best_bitcount = temp_bitcount;
  1643. best_codebook = codebook;
  1644. }
  1645. if (temp_bitcount < prev_best_bitcount) {
  1646. prev_best_bitcount = temp_bitcount;
  1647. if (src_path != dst_path)
  1648. memcpy(dst_path, src_path, sizeof(PathCounter));
  1649. av_strlcat(dst_path->path, path_counter_codebook[codebook], sizeof(dst_path->path));
  1650. dst_path->bitcount = temp_bitcount;
  1651. }
  1652. }
  1653. }
  1654. prev_bo = cur_bo;
  1655. memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
  1656. }
  1657. best_path = path_counter[NUM_CODEBOOKS].path + 1;
  1658. /* Update context. */
  1659. for (index = 0; index < ctx->number_of_subblocks; index++) {
  1660. ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
  1661. best_codebook = *best_path++ - ZERO_PATH;
  1662. cur_bo = &ctx->best_offset[index][channel][best_codebook];
  1663. cp->huff_offset = cur_bo->offset;
  1664. cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];
  1665. cp->codebook = best_codebook;
  1666. }
  1667. }
  1668. }
  1669. /** Analyzes all collected bitcounts and selects the best parameters for each
  1670. * individual access unit.
  1671. * TODO This is just a stub!
  1672. */
  1673. static void set_major_params(MLPEncodeContext *ctx)
  1674. {
  1675. RestartHeader *rh = ctx->cur_restart_header;
  1676. unsigned int index;
  1677. unsigned int substr;
  1678. uint8_t max_huff_lsbs = 0;
  1679. uint8_t max_output_bits = 0;
  1680. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1681. DecodingParams *seq_dp = (DecodingParams *) ctx->decoding_params+
  1682. (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
  1683. (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
  1684. ChannelParams *seq_cp = (ChannelParams *) ctx->channel_params +
  1685. (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
  1686. (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
  1687. unsigned int channel;
  1688. for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
  1689. memcpy(&ctx->major_decoding_params[index][substr], seq_dp + index*(ctx->num_substreams) + substr, sizeof(DecodingParams));
  1690. for (channel = 0; channel < ctx->avctx->channels; channel++) {
  1691. uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
  1692. if (max_huff_lsbs < huff_lsbs)
  1693. max_huff_lsbs = huff_lsbs;
  1694. memcpy(&ctx->major_channel_params[index][channel],
  1695. (seq_cp + index*(ctx->avctx->channels) + channel),
  1696. sizeof(ChannelParams));
  1697. }
  1698. }
  1699. }
  1700. rh->max_huff_lsbs = max_huff_lsbs;
  1701. for (index = 0; index < ctx->number_of_frames; index++)
  1702. if (max_output_bits < ctx->max_output_bits[index])
  1703. max_output_bits = ctx->max_output_bits[index];
  1704. rh->max_output_bits = max_output_bits;
  1705. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1706. ctx->cur_restart_header = &ctx->restart_header[substr];
  1707. ctx->prev_decoding_params = &restart_decoding_params[substr];
  1708. ctx->prev_channel_params = restart_channel_params;
  1709. for (index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
  1710. ctx->cur_decoding_params = &ctx->major_decoding_params[index][substr];
  1711. ctx->cur_channel_params = ctx->major_channel_params[index];
  1712. ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
  1713. ctx->prev_decoding_params = ctx->cur_decoding_params;
  1714. ctx->prev_channel_params = ctx->cur_channel_params;
  1715. }
  1716. }
  1717. ctx->major_number_of_subblocks = ctx->number_of_subblocks;
  1718. ctx->major_filter_state_subblock = 1;
  1719. ctx->major_cur_subblock_index = 0;
  1720. }
  1721. static void analyze_sample_buffer(MLPEncodeContext *ctx)
  1722. {
  1723. ChannelParams *seq_cp = ctx->seq_channel_params;
  1724. DecodingParams *seq_dp = ctx->seq_decoding_params;
  1725. unsigned int index;
  1726. unsigned int substr;
  1727. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1728. ctx->cur_restart_header = &ctx->restart_header[substr];
  1729. ctx->cur_decoding_params = seq_dp + 1*(ctx->num_substreams) + substr;
  1730. ctx->cur_channel_params = seq_cp + 1*(ctx->avctx->channels);
  1731. determine_quant_step_size(ctx);
  1732. generate_2_noise_channels(ctx);
  1733. lossless_matrix_coeffs (ctx);
  1734. rematrix_channels (ctx);
  1735. determine_filters (ctx);
  1736. apply_filters (ctx);
  1737. copy_restart_frame_params(ctx, substr);
  1738. /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
  1739. * decoding_params[0] is for the filter state subblock.
  1740. */
  1741. for (index = 0; index < ctx->number_of_frames; index++) {
  1742. DecodingParams *dp = seq_dp + (index + 1)*(ctx->num_substreams) + substr;
  1743. dp->blocksize = ctx->frame_size[index];
  1744. }
  1745. /* The official encoder seems to always encode a filter state subblock
  1746. * even if there are no filters. TODO check if it is possible to skip
  1747. * the filter state subblock for no filters.
  1748. */
  1749. (seq_dp + substr)->blocksize = 8;
  1750. (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
  1751. for (index = 0; index < ctx->number_of_subblocks; index++) {
  1752. ctx->cur_decoding_params = seq_dp + index*(ctx->num_substreams) + substr;
  1753. ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
  1754. ctx->cur_best_offset = ctx->best_offset[index];
  1755. determine_bits(ctx);
  1756. ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
  1757. }
  1758. set_best_codebook(ctx);
  1759. }
  1760. }
  1761. static void process_major_frame(MLPEncodeContext *ctx)
  1762. {
  1763. unsigned int substr;
  1764. ctx->sample_buffer = ctx->major_inout_buffer;
  1765. ctx->starting_frame_index = 0;
  1766. ctx->number_of_frames = ctx->major_number_of_frames;
  1767. ctx->number_of_samples = ctx->major_frame_size;
  1768. for (substr = 0; substr < ctx->num_substreams; substr++) {
  1769. ctx->cur_restart_header = &ctx->restart_header[substr];
  1770. ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
  1771. ctx->cur_channel_params = ctx->major_channel_params[1];
  1772. generate_2_noise_channels(ctx);
  1773. rematrix_channels (ctx);
  1774. apply_filters(ctx);
  1775. }
  1776. }
  1777. /****************************************************************************/
  1778. static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  1779. const AVFrame *frame, int *got_packet)
  1780. {
  1781. MLPEncodeContext *ctx = avctx->priv_data;
  1782. unsigned int bytes_written = 0;
  1783. int restart_frame, ret;
  1784. uint8_t *data;
  1785. if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
  1786. return ret;
  1787. if (!frame)
  1788. return 1;
  1789. /* add current frame to queue */
  1790. if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
  1791. return ret;
  1792. data = frame->data[0];
  1793. ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
  1794. ctx->inout_buffer = ctx->major_inout_buffer
  1795. + ctx->frame_index * ctx->one_sample_buffer_size;
  1796. if (ctx->last_frame == ctx->inout_buffer) {
  1797. return 0;
  1798. }
  1799. ctx->sample_buffer = ctx->major_scratch_buffer
  1800. + ctx->frame_index * ctx->one_sample_buffer_size;
  1801. ctx->write_buffer = ctx->inout_buffer;
  1802. if (avctx->frame_number < ctx->max_restart_interval) {
  1803. if (data) {
  1804. goto input_and_return;
  1805. } else {
  1806. /* There are less frames than the requested major header interval.
  1807. * Update the context to reflect this.
  1808. */
  1809. ctx->max_restart_interval = avctx->frame_number;
  1810. ctx->frame_index = 0;
  1811. ctx->sample_buffer = ctx->major_scratch_buffer;
  1812. ctx->inout_buffer = ctx->major_inout_buffer;
  1813. }
  1814. }
  1815. if (ctx->frame_size[ctx->frame_index] > MAX_BLOCKSIZE) {
  1816. av_log(avctx, AV_LOG_ERROR, "Invalid frame size (%d > %d)\n",
  1817. ctx->frame_size[ctx->frame_index], MAX_BLOCKSIZE);
  1818. return -1;
  1819. }
  1820. restart_frame = !ctx->frame_index;
  1821. if (restart_frame) {
  1822. set_major_params(ctx);
  1823. if (ctx->min_restart_interval != ctx->max_restart_interval)
  1824. process_major_frame(ctx);
  1825. }
  1826. if (ctx->min_restart_interval == ctx->max_restart_interval)
  1827. ctx->write_buffer = ctx->sample_buffer;
  1828. bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
  1829. ctx->timestamp += ctx->frame_size[ctx->frame_index];
  1830. ctx->dts += ctx->frame_size[ctx->frame_index];
  1831. input_and_return:
  1832. if (data) {
  1833. ctx->frame_size[ctx->frame_index] = avctx->frame_size;
  1834. ctx->next_major_frame_size += avctx->frame_size;
  1835. ctx->next_major_number_of_frames++;
  1836. input_data(ctx, data);
  1837. } else if (!ctx->last_frame) {
  1838. ctx->last_frame = ctx->inout_buffer;
  1839. }
  1840. restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
  1841. if (!restart_frame) {
  1842. int seq_index;
  1843. for (seq_index = 0;
  1844. seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
  1845. seq_index++) {
  1846. unsigned int number_of_samples = 0;
  1847. unsigned int index;
  1848. ctx->sample_buffer = ctx->major_scratch_buffer;
  1849. ctx->inout_buffer = ctx->major_inout_buffer;
  1850. ctx->seq_index = seq_index;
  1851. ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
  1852. - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
  1853. ctx->number_of_frames = ctx->next_major_number_of_frames;
  1854. ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
  1855. ctx->seq_channel_params = (ChannelParams *) ctx->channel_params +
  1856. (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->avctx->channels) +
  1857. (ctx->seq_offset[seq_index])*(ctx->avctx->channels);
  1858. ctx->seq_decoding_params = (DecodingParams *) ctx->decoding_params +
  1859. (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->num_substreams) +
  1860. (ctx->seq_offset[seq_index])*(ctx->num_substreams);
  1861. for (index = 0; index < ctx->number_of_frames; index++) {
  1862. number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
  1863. }
  1864. ctx->number_of_samples = number_of_samples;
  1865. for (index = 0; index < ctx->seq_size[seq_index]; index++) {
  1866. clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels));
  1867. default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams));
  1868. }
  1869. input_to_sample_buffer(ctx);
  1870. analyze_sample_buffer(ctx);
  1871. }
  1872. if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
  1873. ctx->major_frame_size = ctx->next_major_frame_size;
  1874. ctx->next_major_frame_size = 0;
  1875. ctx->major_number_of_frames = ctx->next_major_number_of_frames;
  1876. ctx->next_major_number_of_frames = 0;
  1877. if (!ctx->major_frame_size)
  1878. goto no_data_left;
  1879. }
  1880. }
  1881. no_data_left:
  1882. ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
  1883. &avpkt->duration);
  1884. avpkt->size = bytes_written;
  1885. *got_packet = 1;
  1886. return 0;
  1887. }
  1888. static av_cold int mlp_encode_close(AVCodecContext *avctx)
  1889. {
  1890. MLPEncodeContext *ctx = avctx->priv_data;
  1891. ff_lpc_end(&ctx->lpc_ctx);
  1892. av_freep(&ctx->lossless_check_data);
  1893. av_freep(&ctx->major_scratch_buffer);
  1894. av_freep(&ctx->major_inout_buffer);
  1895. av_freep(&ctx->lpc_sample_buffer);
  1896. av_freep(&ctx->decoding_params);
  1897. av_freep(&ctx->channel_params);
  1898. av_freep(&ctx->frame_size);
  1899. ff_af_queue_close(&ctx->afq);
  1900. return 0;
  1901. }
  1902. #if CONFIG_MLP_ENCODER
  1903. AVCodec ff_mlp_encoder = {
  1904. .name ="mlp",
  1905. .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
  1906. .type = AVMEDIA_TYPE_AUDIO,
  1907. .id = AV_CODEC_ID_MLP,
  1908. .priv_data_size = sizeof(MLPEncodeContext),
  1909. .init = mlp_encode_init,
  1910. .encode2 = mlp_encode_frame,
  1911. .close = mlp_encode_close,
  1912. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
  1913. .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
  1914. .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
  1915. .channel_layouts = ff_mlp_channel_layouts,
  1916. };
  1917. #endif
  1918. #if CONFIG_TRUEHD_ENCODER
  1919. AVCodec ff_truehd_encoder = {
  1920. .name ="truehd",
  1921. .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
  1922. .type = AVMEDIA_TYPE_AUDIO,
  1923. .id = AV_CODEC_ID_TRUEHD,
  1924. .priv_data_size = sizeof(MLPEncodeContext),
  1925. .init = mlp_encode_init,
  1926. .encode2 = mlp_encode_frame,
  1927. .close = mlp_encode_close,
  1928. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
  1929. .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
  1930. .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
  1931. .channel_layouts = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},
  1932. };
  1933. #endif