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.

1343 lines
42KB

  1. /*
  2. * FLAC audio encoder
  3. * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/crc.h"
  22. #include "libavutil/intmath.h"
  23. #include "libavutil/md5.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "get_bits.h"
  28. #include "golomb.h"
  29. #include "internal.h"
  30. #include "lpc.h"
  31. #include "flac.h"
  32. #include "flacdata.h"
  33. #include "flacdsp.h"
  34. #define FLAC_SUBFRAME_CONSTANT 0
  35. #define FLAC_SUBFRAME_VERBATIM 1
  36. #define FLAC_SUBFRAME_FIXED 8
  37. #define FLAC_SUBFRAME_LPC 32
  38. #define MAX_FIXED_ORDER 4
  39. #define MAX_PARTITION_ORDER 8
  40. #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
  41. #define MAX_LPC_PRECISION 15
  42. #define MAX_LPC_SHIFT 15
  43. enum CodingMode {
  44. CODING_MODE_RICE = 4,
  45. CODING_MODE_RICE2 = 5,
  46. };
  47. typedef struct CompressionOptions {
  48. int compression_level;
  49. int block_time_ms;
  50. enum FFLPCType lpc_type;
  51. int lpc_passes;
  52. int lpc_coeff_precision;
  53. int min_prediction_order;
  54. int max_prediction_order;
  55. int prediction_order_method;
  56. int min_partition_order;
  57. int max_partition_order;
  58. int ch_mode;
  59. } CompressionOptions;
  60. typedef struct RiceContext {
  61. enum CodingMode coding_mode;
  62. int porder;
  63. int params[MAX_PARTITIONS];
  64. } RiceContext;
  65. typedef struct FlacSubframe {
  66. int type;
  67. int type_code;
  68. int obits;
  69. int wasted;
  70. int order;
  71. int32_t coefs[MAX_LPC_ORDER];
  72. int shift;
  73. RiceContext rc;
  74. int32_t samples[FLAC_MAX_BLOCKSIZE];
  75. int32_t residual[FLAC_MAX_BLOCKSIZE+1];
  76. } FlacSubframe;
  77. typedef struct FlacFrame {
  78. FlacSubframe subframes[FLAC_MAX_CHANNELS];
  79. int blocksize;
  80. int bs_code[2];
  81. uint8_t crc8;
  82. int ch_mode;
  83. int verbatim_only;
  84. } FlacFrame;
  85. typedef struct FlacEncodeContext {
  86. AVClass *class;
  87. PutBitContext pb;
  88. int channels;
  89. int samplerate;
  90. int sr_code[2];
  91. int bps_code;
  92. int max_blocksize;
  93. int min_framesize;
  94. int max_framesize;
  95. int max_encoded_framesize;
  96. uint32_t frame_count;
  97. uint64_t sample_count;
  98. uint8_t md5sum[16];
  99. FlacFrame frame;
  100. CompressionOptions options;
  101. AVCodecContext *avctx;
  102. LPCContext lpc_ctx;
  103. struct AVMD5 *md5ctx;
  104. uint8_t *md5_buffer;
  105. unsigned int md5_buffer_size;
  106. DSPContext dsp;
  107. FLACDSPContext flac_dsp;
  108. } FlacEncodeContext;
  109. /**
  110. * Write streaminfo metadata block to byte array.
  111. */
  112. static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
  113. {
  114. PutBitContext pb;
  115. memset(header, 0, FLAC_STREAMINFO_SIZE);
  116. init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
  117. /* streaminfo metadata block */
  118. put_bits(&pb, 16, s->max_blocksize);
  119. put_bits(&pb, 16, s->max_blocksize);
  120. put_bits(&pb, 24, s->min_framesize);
  121. put_bits(&pb, 24, s->max_framesize);
  122. put_bits(&pb, 20, s->samplerate);
  123. put_bits(&pb, 3, s->channels-1);
  124. put_bits(&pb, 5, s->avctx->bits_per_raw_sample - 1);
  125. /* write 36-bit sample count in 2 put_bits() calls */
  126. put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
  127. put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
  128. flush_put_bits(&pb);
  129. memcpy(&header[18], s->md5sum, 16);
  130. }
  131. /**
  132. * Set blocksize based on samplerate.
  133. * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
  134. */
  135. static int select_blocksize(int samplerate, int block_time_ms)
  136. {
  137. int i;
  138. int target;
  139. int blocksize;
  140. assert(samplerate > 0);
  141. blocksize = ff_flac_blocksize_table[1];
  142. target = (samplerate * block_time_ms) / 1000;
  143. for (i = 0; i < 16; i++) {
  144. if (target >= ff_flac_blocksize_table[i] &&
  145. ff_flac_blocksize_table[i] > blocksize) {
  146. blocksize = ff_flac_blocksize_table[i];
  147. }
  148. }
  149. return blocksize;
  150. }
  151. static av_cold void dprint_compression_options(FlacEncodeContext *s)
  152. {
  153. AVCodecContext *avctx = s->avctx;
  154. CompressionOptions *opt = &s->options;
  155. av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
  156. switch (opt->lpc_type) {
  157. case FF_LPC_TYPE_NONE:
  158. av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
  159. break;
  160. case FF_LPC_TYPE_FIXED:
  161. av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
  162. break;
  163. case FF_LPC_TYPE_LEVINSON:
  164. av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
  165. break;
  166. case FF_LPC_TYPE_CHOLESKY:
  167. av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
  168. opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
  169. break;
  170. }
  171. av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
  172. opt->min_prediction_order, opt->max_prediction_order);
  173. switch (opt->prediction_order_method) {
  174. case ORDER_METHOD_EST:
  175. av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
  176. break;
  177. case ORDER_METHOD_2LEVEL:
  178. av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
  179. break;
  180. case ORDER_METHOD_4LEVEL:
  181. av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
  182. break;
  183. case ORDER_METHOD_8LEVEL:
  184. av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
  185. break;
  186. case ORDER_METHOD_SEARCH:
  187. av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
  188. break;
  189. case ORDER_METHOD_LOG:
  190. av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
  191. break;
  192. }
  193. av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
  194. opt->min_partition_order, opt->max_partition_order);
  195. av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
  196. av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
  197. opt->lpc_coeff_precision);
  198. }
  199. static av_cold int flac_encode_init(AVCodecContext *avctx)
  200. {
  201. int freq = avctx->sample_rate;
  202. int channels = avctx->channels;
  203. FlacEncodeContext *s = avctx->priv_data;
  204. int i, level, ret;
  205. uint8_t *streaminfo;
  206. s->avctx = avctx;
  207. switch (avctx->sample_fmt) {
  208. case AV_SAMPLE_FMT_S16:
  209. avctx->bits_per_raw_sample = 16;
  210. s->bps_code = 4;
  211. break;
  212. case AV_SAMPLE_FMT_S32:
  213. if (avctx->bits_per_raw_sample != 24)
  214. av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
  215. avctx->bits_per_raw_sample = 24;
  216. s->bps_code = 6;
  217. break;
  218. }
  219. if (channels < 1 || channels > FLAC_MAX_CHANNELS)
  220. return -1;
  221. s->channels = channels;
  222. /* find samplerate in table */
  223. if (freq < 1)
  224. return -1;
  225. for (i = 4; i < 12; i++) {
  226. if (freq == ff_flac_sample_rate_table[i]) {
  227. s->samplerate = ff_flac_sample_rate_table[i];
  228. s->sr_code[0] = i;
  229. s->sr_code[1] = 0;
  230. break;
  231. }
  232. }
  233. /* if not in table, samplerate is non-standard */
  234. if (i == 12) {
  235. if (freq % 1000 == 0 && freq < 255000) {
  236. s->sr_code[0] = 12;
  237. s->sr_code[1] = freq / 1000;
  238. } else if (freq % 10 == 0 && freq < 655350) {
  239. s->sr_code[0] = 14;
  240. s->sr_code[1] = freq / 10;
  241. } else if (freq < 65535) {
  242. s->sr_code[0] = 13;
  243. s->sr_code[1] = freq;
  244. } else {
  245. return -1;
  246. }
  247. s->samplerate = freq;
  248. }
  249. /* set compression option defaults based on avctx->compression_level */
  250. if (avctx->compression_level < 0)
  251. s->options.compression_level = 5;
  252. else
  253. s->options.compression_level = avctx->compression_level;
  254. level = s->options.compression_level;
  255. if (level > 12) {
  256. av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
  257. s->options.compression_level);
  258. return -1;
  259. }
  260. s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
  261. if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
  262. s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED,
  263. FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
  264. FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
  265. FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
  266. FF_LPC_TYPE_LEVINSON})[level];
  267. s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
  268. s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
  269. if (s->options.prediction_order_method < 0)
  270. s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
  271. ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
  272. ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
  273. ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
  274. ORDER_METHOD_SEARCH})[level];
  275. if (s->options.min_partition_order > s->options.max_partition_order) {
  276. av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
  277. s->options.min_partition_order, s->options.max_partition_order);
  278. return AVERROR(EINVAL);
  279. }
  280. if (s->options.min_partition_order < 0)
  281. s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
  282. if (s->options.max_partition_order < 0)
  283. s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
  284. if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
  285. s->options.min_prediction_order = 0;
  286. } else if (avctx->min_prediction_order >= 0) {
  287. if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
  288. if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
  289. av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
  290. avctx->min_prediction_order);
  291. return -1;
  292. }
  293. } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
  294. avctx->min_prediction_order > MAX_LPC_ORDER) {
  295. av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
  296. avctx->min_prediction_order);
  297. return -1;
  298. }
  299. s->options.min_prediction_order = avctx->min_prediction_order;
  300. }
  301. if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
  302. s->options.max_prediction_order = 0;
  303. } else if (avctx->max_prediction_order >= 0) {
  304. if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
  305. if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
  306. av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
  307. avctx->max_prediction_order);
  308. return -1;
  309. }
  310. } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
  311. avctx->max_prediction_order > MAX_LPC_ORDER) {
  312. av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
  313. avctx->max_prediction_order);
  314. return -1;
  315. }
  316. s->options.max_prediction_order = avctx->max_prediction_order;
  317. }
  318. if (s->options.max_prediction_order < s->options.min_prediction_order) {
  319. av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
  320. s->options.min_prediction_order, s->options.max_prediction_order);
  321. return -1;
  322. }
  323. if (avctx->frame_size > 0) {
  324. if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
  325. avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
  326. av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
  327. avctx->frame_size);
  328. return -1;
  329. }
  330. } else {
  331. s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
  332. }
  333. s->max_blocksize = s->avctx->frame_size;
  334. /* set maximum encoded frame size in verbatim mode */
  335. s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
  336. s->channels,
  337. s->avctx->bits_per_raw_sample);
  338. /* initialize MD5 context */
  339. s->md5ctx = av_md5_alloc();
  340. if (!s->md5ctx)
  341. return AVERROR(ENOMEM);
  342. av_md5_init(s->md5ctx);
  343. streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
  344. if (!streaminfo)
  345. return AVERROR(ENOMEM);
  346. write_streaminfo(s, streaminfo);
  347. avctx->extradata = streaminfo;
  348. avctx->extradata_size = FLAC_STREAMINFO_SIZE;
  349. s->frame_count = 0;
  350. s->min_framesize = s->max_framesize;
  351. #if FF_API_OLD_ENCODE_AUDIO
  352. avctx->coded_frame = avcodec_alloc_frame();
  353. if (!avctx->coded_frame)
  354. return AVERROR(ENOMEM);
  355. #endif
  356. ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
  357. s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
  358. ff_dsputil_init(&s->dsp, avctx);
  359. ff_flacdsp_init(&s->flac_dsp, avctx->sample_fmt,
  360. avctx->bits_per_raw_sample);
  361. dprint_compression_options(s);
  362. return ret;
  363. }
  364. static void init_frame(FlacEncodeContext *s, int nb_samples)
  365. {
  366. int i, ch;
  367. FlacFrame *frame;
  368. frame = &s->frame;
  369. for (i = 0; i < 16; i++) {
  370. if (nb_samples == ff_flac_blocksize_table[i]) {
  371. frame->blocksize = ff_flac_blocksize_table[i];
  372. frame->bs_code[0] = i;
  373. frame->bs_code[1] = 0;
  374. break;
  375. }
  376. }
  377. if (i == 16) {
  378. frame->blocksize = nb_samples;
  379. if (frame->blocksize <= 256) {
  380. frame->bs_code[0] = 6;
  381. frame->bs_code[1] = frame->blocksize-1;
  382. } else {
  383. frame->bs_code[0] = 7;
  384. frame->bs_code[1] = frame->blocksize-1;
  385. }
  386. }
  387. for (ch = 0; ch < s->channels; ch++) {
  388. FlacSubframe *sub = &frame->subframes[ch];
  389. sub->wasted = 0;
  390. sub->obits = s->avctx->bits_per_raw_sample;
  391. if (sub->obits > 16)
  392. sub->rc.coding_mode = CODING_MODE_RICE2;
  393. else
  394. sub->rc.coding_mode = CODING_MODE_RICE;
  395. }
  396. frame->verbatim_only = 0;
  397. }
  398. /**
  399. * Copy channel-interleaved input samples into separate subframes.
  400. */
  401. static void copy_samples(FlacEncodeContext *s, const void *samples)
  402. {
  403. int i, j, ch;
  404. FlacFrame *frame;
  405. int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
  406. s->avctx->bits_per_raw_sample;
  407. #define COPY_SAMPLES(bits) do { \
  408. const int ## bits ## _t *samples0 = samples; \
  409. frame = &s->frame; \
  410. for (i = 0, j = 0; i < frame->blocksize; i++) \
  411. for (ch = 0; ch < s->channels; ch++, j++) \
  412. frame->subframes[ch].samples[i] = samples0[j] >> shift; \
  413. } while (0)
  414. if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  415. COPY_SAMPLES(16);
  416. else
  417. COPY_SAMPLES(32);
  418. }
  419. static uint64_t rice_count_exact(int32_t *res, int n, int k)
  420. {
  421. int i;
  422. uint64_t count = 0;
  423. for (i = 0; i < n; i++) {
  424. int32_t v = -2 * res[i] - 1;
  425. v ^= v >> 31;
  426. count += (v >> k) + 1 + k;
  427. }
  428. return count;
  429. }
  430. static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
  431. int pred_order)
  432. {
  433. int p, porder, psize;
  434. int i, part_end;
  435. uint64_t count = 0;
  436. /* subframe header */
  437. count += 8;
  438. /* subframe */
  439. if (sub->type == FLAC_SUBFRAME_CONSTANT) {
  440. count += sub->obits;
  441. } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
  442. count += s->frame.blocksize * sub->obits;
  443. } else {
  444. /* warm-up samples */
  445. count += pred_order * sub->obits;
  446. /* LPC coefficients */
  447. if (sub->type == FLAC_SUBFRAME_LPC)
  448. count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
  449. /* rice-encoded block */
  450. count += 2;
  451. /* partition order */
  452. porder = sub->rc.porder;
  453. psize = s->frame.blocksize >> porder;
  454. count += 4;
  455. /* residual */
  456. i = pred_order;
  457. part_end = psize;
  458. for (p = 0; p < 1 << porder; p++) {
  459. int k = sub->rc.params[p];
  460. count += sub->rc.coding_mode;
  461. count += rice_count_exact(&sub->residual[i], part_end - i, k);
  462. i = part_end;
  463. part_end = FFMIN(s->frame.blocksize, part_end + psize);
  464. }
  465. }
  466. return count;
  467. }
  468. #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
  469. /**
  470. * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
  471. */
  472. static int find_optimal_param(uint64_t sum, int n, int max_param)
  473. {
  474. int k;
  475. uint64_t sum2;
  476. if (sum <= n >> 1)
  477. return 0;
  478. sum2 = sum - (n >> 1);
  479. k = av_log2(av_clipl_int32(sum2 / n));
  480. return FFMIN(k, max_param);
  481. }
  482. static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder,
  483. uint64_t *sums, int n, int pred_order)
  484. {
  485. int i;
  486. int k, cnt, part, max_param;
  487. uint64_t all_bits;
  488. max_param = (1 << rc->coding_mode) - 2;
  489. part = (1 << porder);
  490. all_bits = 4 * part;
  491. cnt = (n >> porder) - pred_order;
  492. for (i = 0; i < part; i++) {
  493. k = find_optimal_param(sums[i], cnt, max_param);
  494. rc->params[i] = k;
  495. all_bits += rice_encode_count(sums[i], cnt, k);
  496. cnt = n >> porder;
  497. }
  498. rc->porder = porder;
  499. return all_bits;
  500. }
  501. static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
  502. uint64_t sums[][MAX_PARTITIONS])
  503. {
  504. int i, j;
  505. int parts;
  506. uint32_t *res, *res_end;
  507. /* sums for highest level */
  508. parts = (1 << pmax);
  509. res = &data[pred_order];
  510. res_end = &data[n >> pmax];
  511. for (i = 0; i < parts; i++) {
  512. uint64_t sum = 0;
  513. while (res < res_end)
  514. sum += *(res++);
  515. sums[pmax][i] = sum;
  516. res_end += n >> pmax;
  517. }
  518. /* sums for lower levels */
  519. for (i = pmax - 1; i >= pmin; i--) {
  520. parts = (1 << i);
  521. for (j = 0; j < parts; j++)
  522. sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
  523. }
  524. }
  525. static uint64_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
  526. int32_t *data, int n, int pred_order)
  527. {
  528. int i;
  529. uint64_t bits[MAX_PARTITION_ORDER+1];
  530. int opt_porder;
  531. RiceContext tmp_rc;
  532. uint32_t *udata;
  533. uint64_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
  534. assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
  535. assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
  536. assert(pmin <= pmax);
  537. tmp_rc.coding_mode = rc->coding_mode;
  538. udata = av_malloc(n * sizeof(uint32_t));
  539. for (i = 0; i < n; i++)
  540. udata[i] = (2*data[i]) ^ (data[i]>>31);
  541. calc_sums(pmin, pmax, udata, n, pred_order, sums);
  542. opt_porder = pmin;
  543. bits[pmin] = UINT32_MAX;
  544. for (i = pmin; i <= pmax; i++) {
  545. bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
  546. if (bits[i] <= bits[opt_porder]) {
  547. opt_porder = i;
  548. *rc = tmp_rc;
  549. }
  550. }
  551. av_freep(&udata);
  552. return bits[opt_porder];
  553. }
  554. static int get_max_p_order(int max_porder, int n, int order)
  555. {
  556. int porder = FFMIN(max_porder, av_log2(n^(n-1)));
  557. if (order > 0)
  558. porder = FFMIN(porder, av_log2(n/order));
  559. return porder;
  560. }
  561. static uint64_t find_subframe_rice_params(FlacEncodeContext *s,
  562. FlacSubframe *sub, int pred_order)
  563. {
  564. int pmin = get_max_p_order(s->options.min_partition_order,
  565. s->frame.blocksize, pred_order);
  566. int pmax = get_max_p_order(s->options.max_partition_order,
  567. s->frame.blocksize, pred_order);
  568. uint64_t bits = 8 + pred_order * sub->obits + 2 + sub->rc.coding_mode;
  569. if (sub->type == FLAC_SUBFRAME_LPC)
  570. bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
  571. bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
  572. s->frame.blocksize, pred_order);
  573. return bits;
  574. }
  575. static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
  576. int order)
  577. {
  578. int i;
  579. for (i = 0; i < order; i++)
  580. res[i] = smp[i];
  581. if (order == 0) {
  582. for (i = order; i < n; i++)
  583. res[i] = smp[i];
  584. } else if (order == 1) {
  585. for (i = order; i < n; i++)
  586. res[i] = smp[i] - smp[i-1];
  587. } else if (order == 2) {
  588. int a = smp[order-1] - smp[order-2];
  589. for (i = order; i < n; i += 2) {
  590. int b = smp[i ] - smp[i-1];
  591. res[i] = b - a;
  592. a = smp[i+1] - smp[i ];
  593. res[i+1] = a - b;
  594. }
  595. } else if (order == 3) {
  596. int a = smp[order-1] - smp[order-2];
  597. int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
  598. for (i = order; i < n; i += 2) {
  599. int b = smp[i ] - smp[i-1];
  600. int d = b - a;
  601. res[i] = d - c;
  602. a = smp[i+1] - smp[i ];
  603. c = a - b;
  604. res[i+1] = c - d;
  605. }
  606. } else {
  607. int a = smp[order-1] - smp[order-2];
  608. int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
  609. int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
  610. for (i = order; i < n; i += 2) {
  611. int b = smp[i ] - smp[i-1];
  612. int d = b - a;
  613. int f = d - c;
  614. res[i ] = f - e;
  615. a = smp[i+1] - smp[i ];
  616. c = a - b;
  617. e = c - d;
  618. res[i+1] = e - f;
  619. }
  620. }
  621. }
  622. static int encode_residual_ch(FlacEncodeContext *s, int ch)
  623. {
  624. int i, n;
  625. int min_order, max_order, opt_order, omethod;
  626. FlacFrame *frame;
  627. FlacSubframe *sub;
  628. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  629. int shift[MAX_LPC_ORDER];
  630. int32_t *res, *smp;
  631. frame = &s->frame;
  632. sub = &frame->subframes[ch];
  633. res = sub->residual;
  634. smp = sub->samples;
  635. n = frame->blocksize;
  636. /* CONSTANT */
  637. for (i = 1; i < n; i++)
  638. if(smp[i] != smp[0])
  639. break;
  640. if (i == n) {
  641. sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
  642. res[0] = smp[0];
  643. return subframe_count_exact(s, sub, 0);
  644. }
  645. /* VERBATIM */
  646. if (frame->verbatim_only || n < 5) {
  647. sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
  648. memcpy(res, smp, n * sizeof(int32_t));
  649. return subframe_count_exact(s, sub, 0);
  650. }
  651. min_order = s->options.min_prediction_order;
  652. max_order = s->options.max_prediction_order;
  653. omethod = s->options.prediction_order_method;
  654. /* FIXED */
  655. sub->type = FLAC_SUBFRAME_FIXED;
  656. if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
  657. s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
  658. uint64_t bits[MAX_FIXED_ORDER+1];
  659. if (max_order > MAX_FIXED_ORDER)
  660. max_order = MAX_FIXED_ORDER;
  661. opt_order = 0;
  662. bits[0] = UINT32_MAX;
  663. for (i = min_order; i <= max_order; i++) {
  664. encode_residual_fixed(res, smp, n, i);
  665. bits[i] = find_subframe_rice_params(s, sub, i);
  666. if (bits[i] < bits[opt_order])
  667. opt_order = i;
  668. }
  669. sub->order = opt_order;
  670. sub->type_code = sub->type | sub->order;
  671. if (sub->order != max_order) {
  672. encode_residual_fixed(res, smp, n, sub->order);
  673. find_subframe_rice_params(s, sub, sub->order);
  674. }
  675. return subframe_count_exact(s, sub, sub->order);
  676. }
  677. /* LPC */
  678. sub->type = FLAC_SUBFRAME_LPC;
  679. opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
  680. s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
  681. s->options.lpc_passes, omethod,
  682. MAX_LPC_SHIFT, 0);
  683. if (omethod == ORDER_METHOD_2LEVEL ||
  684. omethod == ORDER_METHOD_4LEVEL ||
  685. omethod == ORDER_METHOD_8LEVEL) {
  686. int levels = 1 << omethod;
  687. uint64_t bits[1 << ORDER_METHOD_8LEVEL];
  688. int order = -1;
  689. int opt_index = levels-1;
  690. opt_order = max_order-1;
  691. bits[opt_index] = UINT32_MAX;
  692. for (i = levels-1; i >= 0; i--) {
  693. int last_order = order;
  694. order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
  695. order = av_clip(order, min_order - 1, max_order - 1);
  696. if (order == last_order)
  697. continue;
  698. s->flac_dsp.lpc_encode(res, smp, n, order+1, coefs[order],
  699. shift[order]);
  700. bits[i] = find_subframe_rice_params(s, sub, order+1);
  701. if (bits[i] < bits[opt_index]) {
  702. opt_index = i;
  703. opt_order = order;
  704. }
  705. }
  706. opt_order++;
  707. } else if (omethod == ORDER_METHOD_SEARCH) {
  708. // brute-force optimal order search
  709. uint64_t bits[MAX_LPC_ORDER];
  710. opt_order = 0;
  711. bits[0] = UINT32_MAX;
  712. for (i = min_order-1; i < max_order; i++) {
  713. s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
  714. bits[i] = find_subframe_rice_params(s, sub, i+1);
  715. if (bits[i] < bits[opt_order])
  716. opt_order = i;
  717. }
  718. opt_order++;
  719. } else if (omethod == ORDER_METHOD_LOG) {
  720. uint64_t bits[MAX_LPC_ORDER];
  721. int step;
  722. opt_order = min_order - 1 + (max_order-min_order)/3;
  723. memset(bits, -1, sizeof(bits));
  724. for (step = 16; step; step >>= 1) {
  725. int last = opt_order;
  726. for (i = last-step; i <= last+step; i += step) {
  727. if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
  728. continue;
  729. s->flac_dsp.lpc_encode(res, smp, n, i+1, coefs[i], shift[i]);
  730. bits[i] = find_subframe_rice_params(s, sub, i+1);
  731. if (bits[i] < bits[opt_order])
  732. opt_order = i;
  733. }
  734. }
  735. opt_order++;
  736. }
  737. sub->order = opt_order;
  738. sub->type_code = sub->type | (sub->order-1);
  739. sub->shift = shift[sub->order-1];
  740. for (i = 0; i < sub->order; i++)
  741. sub->coefs[i] = coefs[sub->order-1][i];
  742. s->flac_dsp.lpc_encode(res, smp, n, sub->order, sub->coefs, sub->shift);
  743. find_subframe_rice_params(s, sub, sub->order);
  744. return subframe_count_exact(s, sub, sub->order);
  745. }
  746. static int count_frame_header(FlacEncodeContext *s)
  747. {
  748. uint8_t av_unused tmp;
  749. int count;
  750. /*
  751. <14> Sync code
  752. <1> Reserved
  753. <1> Blocking strategy
  754. <4> Block size in inter-channel samples
  755. <4> Sample rate
  756. <4> Channel assignment
  757. <3> Sample size in bits
  758. <1> Reserved
  759. */
  760. count = 32;
  761. /* coded frame number */
  762. PUT_UTF8(s->frame_count, tmp, count += 8;)
  763. /* explicit block size */
  764. if (s->frame.bs_code[0] == 6)
  765. count += 8;
  766. else if (s->frame.bs_code[0] == 7)
  767. count += 16;
  768. /* explicit sample rate */
  769. count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
  770. /* frame header CRC-8 */
  771. count += 8;
  772. return count;
  773. }
  774. static int encode_frame(FlacEncodeContext *s)
  775. {
  776. int ch;
  777. uint64_t count;
  778. count = count_frame_header(s);
  779. for (ch = 0; ch < s->channels; ch++)
  780. count += encode_residual_ch(s, ch);
  781. count += (8 - (count & 7)) & 7; // byte alignment
  782. count += 16; // CRC-16
  783. count >>= 3;
  784. if (count > INT_MAX)
  785. return AVERROR_BUG;
  786. return count;
  787. }
  788. static void remove_wasted_bits(FlacEncodeContext *s)
  789. {
  790. int ch, i;
  791. for (ch = 0; ch < s->channels; ch++) {
  792. FlacSubframe *sub = &s->frame.subframes[ch];
  793. int32_t v = 0;
  794. for (i = 0; i < s->frame.blocksize; i++) {
  795. v |= sub->samples[i];
  796. if (v & 1)
  797. break;
  798. }
  799. if (v && !(v & 1)) {
  800. v = av_ctz(v);
  801. for (i = 0; i < s->frame.blocksize; i++)
  802. sub->samples[i] >>= v;
  803. sub->wasted = v;
  804. sub->obits -= v;
  805. /* for 24-bit, check if removing wasted bits makes the range better
  806. suited for using RICE instead of RICE2 for entropy coding */
  807. if (sub->obits <= 17)
  808. sub->rc.coding_mode = CODING_MODE_RICE;
  809. }
  810. }
  811. }
  812. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n,
  813. int max_rice_param)
  814. {
  815. int i, best;
  816. int32_t lt, rt;
  817. uint64_t sum[4];
  818. uint64_t score[4];
  819. int k;
  820. /* calculate sum of 2nd order residual for each channel */
  821. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  822. for (i = 2; i < n; i++) {
  823. lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
  824. rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
  825. sum[2] += FFABS((lt + rt) >> 1);
  826. sum[3] += FFABS(lt - rt);
  827. sum[0] += FFABS(lt);
  828. sum[1] += FFABS(rt);
  829. }
  830. /* estimate bit counts */
  831. for (i = 0; i < 4; i++) {
  832. k = find_optimal_param(2 * sum[i], n, max_rice_param);
  833. sum[i] = rice_encode_count( 2 * sum[i], n, k);
  834. }
  835. /* calculate score for each mode */
  836. score[0] = sum[0] + sum[1];
  837. score[1] = sum[0] + sum[3];
  838. score[2] = sum[1] + sum[3];
  839. score[3] = sum[2] + sum[3];
  840. /* return mode with lowest score */
  841. best = 0;
  842. for (i = 1; i < 4; i++)
  843. if (score[i] < score[best])
  844. best = i;
  845. return best;
  846. }
  847. /**
  848. * Perform stereo channel decorrelation.
  849. */
  850. static void channel_decorrelation(FlacEncodeContext *s)
  851. {
  852. FlacFrame *frame;
  853. int32_t *left, *right;
  854. int i, n;
  855. frame = &s->frame;
  856. n = frame->blocksize;
  857. left = frame->subframes[0].samples;
  858. right = frame->subframes[1].samples;
  859. if (s->channels != 2) {
  860. frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
  861. return;
  862. }
  863. if (s->options.ch_mode < 0) {
  864. int max_rice_param = (1 << frame->subframes[0].rc.coding_mode) - 2;
  865. frame->ch_mode = estimate_stereo_mode(left, right, n, max_rice_param);
  866. } else
  867. frame->ch_mode = s->options.ch_mode;
  868. /* perform decorrelation and adjust bits-per-sample */
  869. if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
  870. return;
  871. if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
  872. int32_t tmp;
  873. for (i = 0; i < n; i++) {
  874. tmp = left[i];
  875. left[i] = (tmp + right[i]) >> 1;
  876. right[i] = tmp - right[i];
  877. }
  878. frame->subframes[1].obits++;
  879. } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
  880. for (i = 0; i < n; i++)
  881. right[i] = left[i] - right[i];
  882. frame->subframes[1].obits++;
  883. } else {
  884. for (i = 0; i < n; i++)
  885. left[i] -= right[i];
  886. frame->subframes[0].obits++;
  887. }
  888. }
  889. static void write_utf8(PutBitContext *pb, uint32_t val)
  890. {
  891. uint8_t tmp;
  892. PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
  893. }
  894. static void write_frame_header(FlacEncodeContext *s)
  895. {
  896. FlacFrame *frame;
  897. int crc;
  898. frame = &s->frame;
  899. put_bits(&s->pb, 16, 0xFFF8);
  900. put_bits(&s->pb, 4, frame->bs_code[0]);
  901. put_bits(&s->pb, 4, s->sr_code[0]);
  902. if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
  903. put_bits(&s->pb, 4, s->channels-1);
  904. else
  905. put_bits(&s->pb, 4, frame->ch_mode + FLAC_MAX_CHANNELS - 1);
  906. put_bits(&s->pb, 3, s->bps_code);
  907. put_bits(&s->pb, 1, 0);
  908. write_utf8(&s->pb, s->frame_count);
  909. if (frame->bs_code[0] == 6)
  910. put_bits(&s->pb, 8, frame->bs_code[1]);
  911. else if (frame->bs_code[0] == 7)
  912. put_bits(&s->pb, 16, frame->bs_code[1]);
  913. if (s->sr_code[0] == 12)
  914. put_bits(&s->pb, 8, s->sr_code[1]);
  915. else if (s->sr_code[0] > 12)
  916. put_bits(&s->pb, 16, s->sr_code[1]);
  917. flush_put_bits(&s->pb);
  918. crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
  919. put_bits_count(&s->pb) >> 3);
  920. put_bits(&s->pb, 8, crc);
  921. }
  922. static void write_subframes(FlacEncodeContext *s)
  923. {
  924. int ch;
  925. for (ch = 0; ch < s->channels; ch++) {
  926. FlacSubframe *sub = &s->frame.subframes[ch];
  927. int i, p, porder, psize;
  928. int32_t *part_end;
  929. int32_t *res = sub->residual;
  930. int32_t *frame_end = &sub->residual[s->frame.blocksize];
  931. /* subframe header */
  932. put_bits(&s->pb, 1, 0);
  933. put_bits(&s->pb, 6, sub->type_code);
  934. put_bits(&s->pb, 1, !!sub->wasted);
  935. if (sub->wasted)
  936. put_bits(&s->pb, sub->wasted, 1);
  937. /* subframe */
  938. if (sub->type == FLAC_SUBFRAME_CONSTANT) {
  939. put_sbits(&s->pb, sub->obits, res[0]);
  940. } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
  941. while (res < frame_end)
  942. put_sbits(&s->pb, sub->obits, *res++);
  943. } else {
  944. /* warm-up samples */
  945. for (i = 0; i < sub->order; i++)
  946. put_sbits(&s->pb, sub->obits, *res++);
  947. /* LPC coefficients */
  948. if (sub->type == FLAC_SUBFRAME_LPC) {
  949. int cbits = s->options.lpc_coeff_precision;
  950. put_bits( &s->pb, 4, cbits-1);
  951. put_sbits(&s->pb, 5, sub->shift);
  952. for (i = 0; i < sub->order; i++)
  953. put_sbits(&s->pb, cbits, sub->coefs[i]);
  954. }
  955. /* rice-encoded block */
  956. put_bits(&s->pb, 2, sub->rc.coding_mode - 4);
  957. /* partition order */
  958. porder = sub->rc.porder;
  959. psize = s->frame.blocksize >> porder;
  960. put_bits(&s->pb, 4, porder);
  961. /* residual */
  962. part_end = &sub->residual[psize];
  963. for (p = 0; p < 1 << porder; p++) {
  964. int k = sub->rc.params[p];
  965. put_bits(&s->pb, sub->rc.coding_mode, k);
  966. while (res < part_end)
  967. set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
  968. part_end = FFMIN(frame_end, part_end + psize);
  969. }
  970. }
  971. }
  972. }
  973. static void write_frame_footer(FlacEncodeContext *s)
  974. {
  975. int crc;
  976. flush_put_bits(&s->pb);
  977. crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
  978. put_bits_count(&s->pb)>>3));
  979. put_bits(&s->pb, 16, crc);
  980. flush_put_bits(&s->pb);
  981. }
  982. static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
  983. {
  984. init_put_bits(&s->pb, avpkt->data, avpkt->size);
  985. write_frame_header(s);
  986. write_subframes(s);
  987. write_frame_footer(s);
  988. return put_bits_count(&s->pb) >> 3;
  989. }
  990. static int update_md5_sum(FlacEncodeContext *s, const void *samples)
  991. {
  992. const uint8_t *buf;
  993. int buf_size = s->frame.blocksize * s->channels *
  994. ((s->avctx->bits_per_raw_sample + 7) / 8);
  995. if (s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
  996. av_fast_malloc(&s->md5_buffer, &s->md5_buffer_size, buf_size);
  997. if (!s->md5_buffer)
  998. return AVERROR(ENOMEM);
  999. }
  1000. if (s->avctx->bits_per_raw_sample <= 16) {
  1001. buf = (const uint8_t *)samples;
  1002. #if HAVE_BIGENDIAN
  1003. s->dsp.bswap16_buf((uint16_t *)s->md5_buffer,
  1004. (const uint16_t *)samples, buf_size / 2);
  1005. buf = s->md5_buffer;
  1006. #endif
  1007. } else {
  1008. int i;
  1009. const int32_t *samples0 = samples;
  1010. uint8_t *tmp = s->md5_buffer;
  1011. for (i = 0; i < s->frame.blocksize * s->channels; i++) {
  1012. int32_t v = samples0[i] >> 8;
  1013. *tmp++ = (v ) & 0xFF;
  1014. *tmp++ = (v >> 8) & 0xFF;
  1015. *tmp++ = (v >> 16) & 0xFF;
  1016. }
  1017. buf = s->md5_buffer;
  1018. }
  1019. av_md5_update(s->md5ctx, buf, buf_size);
  1020. return 0;
  1021. }
  1022. static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  1023. const AVFrame *frame, int *got_packet_ptr)
  1024. {
  1025. FlacEncodeContext *s;
  1026. int frame_bytes, out_bytes, ret;
  1027. s = avctx->priv_data;
  1028. /* when the last block is reached, update the header in extradata */
  1029. if (!frame) {
  1030. s->max_framesize = s->max_encoded_framesize;
  1031. av_md5_final(s->md5ctx, s->md5sum);
  1032. write_streaminfo(s, avctx->extradata);
  1033. return 0;
  1034. }
  1035. /* change max_framesize for small final frame */
  1036. if (frame->nb_samples < s->frame.blocksize) {
  1037. s->max_framesize = ff_flac_get_max_frame_size(frame->nb_samples,
  1038. s->channels,
  1039. avctx->bits_per_raw_sample);
  1040. }
  1041. init_frame(s, frame->nb_samples);
  1042. copy_samples(s, frame->data[0]);
  1043. channel_decorrelation(s);
  1044. remove_wasted_bits(s);
  1045. frame_bytes = encode_frame(s);
  1046. /* fallback to verbatim mode if the compressed frame is larger than it
  1047. would be if encoded uncompressed. */
  1048. if (frame_bytes < 0 || frame_bytes > s->max_framesize) {
  1049. s->frame.verbatim_only = 1;
  1050. frame_bytes = encode_frame(s);
  1051. if (frame_bytes < 0) {
  1052. av_log(avctx, AV_LOG_ERROR, "Bad frame count\n");
  1053. return frame_bytes;
  1054. }
  1055. }
  1056. if ((ret = ff_alloc_packet(avpkt, frame_bytes))) {
  1057. av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
  1058. return ret;
  1059. }
  1060. out_bytes = write_frame(s, avpkt);
  1061. s->frame_count++;
  1062. s->sample_count += frame->nb_samples;
  1063. if ((ret = update_md5_sum(s, frame->data[0])) < 0) {
  1064. av_log(avctx, AV_LOG_ERROR, "Error updating MD5 checksum\n");
  1065. return ret;
  1066. }
  1067. if (out_bytes > s->max_encoded_framesize)
  1068. s->max_encoded_framesize = out_bytes;
  1069. if (out_bytes < s->min_framesize)
  1070. s->min_framesize = out_bytes;
  1071. avpkt->pts = frame->pts;
  1072. avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
  1073. avpkt->size = out_bytes;
  1074. *got_packet_ptr = 1;
  1075. return 0;
  1076. }
  1077. static av_cold int flac_encode_close(AVCodecContext *avctx)
  1078. {
  1079. if (avctx->priv_data) {
  1080. FlacEncodeContext *s = avctx->priv_data;
  1081. av_freep(&s->md5ctx);
  1082. av_freep(&s->md5_buffer);
  1083. ff_lpc_end(&s->lpc_ctx);
  1084. }
  1085. av_freep(&avctx->extradata);
  1086. avctx->extradata_size = 0;
  1087. #if FF_API_OLD_ENCODE_AUDIO
  1088. av_freep(&avctx->coded_frame);
  1089. #endif
  1090. return 0;
  1091. }
  1092. #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  1093. static const AVOption options[] = {
  1094. { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
  1095. { "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
  1096. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1097. { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1098. { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1099. { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1100. { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.i64 = -1 }, INT_MIN, INT_MAX, FLAGS },
  1101. { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
  1102. { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
  1103. { "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
  1104. { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1105. { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1106. { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1107. { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1108. { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1109. { "log", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1110. { "ch_mode", "Stereo decorrelation mode", offsetof(FlacEncodeContext, options.ch_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FLAC_CHMODE_MID_SIDE, FLAGS, "ch_mode" },
  1111. { "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
  1112. { "indep", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_INDEPENDENT }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
  1113. { "left_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_LEFT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
  1114. { "right_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_RIGHT_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
  1115. { "mid_side", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FLAC_CHMODE_MID_SIDE }, INT_MIN, INT_MAX, FLAGS, "ch_mode" },
  1116. { NULL },
  1117. };
  1118. static const AVClass flac_encoder_class = {
  1119. "FLAC encoder",
  1120. av_default_item_name,
  1121. options,
  1122. LIBAVUTIL_VERSION_INT,
  1123. };
  1124. AVCodec ff_flac_encoder = {
  1125. .name = "flac",
  1126. .type = AVMEDIA_TYPE_AUDIO,
  1127. .id = AV_CODEC_ID_FLAC,
  1128. .priv_data_size = sizeof(FlacEncodeContext),
  1129. .init = flac_encode_init,
  1130. .encode2 = flac_encode_frame,
  1131. .close = flac_encode_close,
  1132. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  1133. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
  1134. AV_SAMPLE_FMT_S32,
  1135. AV_SAMPLE_FMT_NONE },
  1136. .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
  1137. .priv_class = &flac_encoder_class,
  1138. };