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.

1338 lines
43KB

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