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.

1326 lines
42KB

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