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.

2411 lines
82KB

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