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.

1304 lines
41KB

  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/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. ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
  329. s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
  330. dprint_compression_options(s);
  331. return ret;
  332. }
  333. static void init_frame(FlacEncodeContext *s)
  334. {
  335. int i, ch;
  336. FlacFrame *frame;
  337. frame = &s->frame;
  338. for (i = 0; i < 16; i++) {
  339. if (s->avctx->frame_size == ff_flac_blocksize_table[i]) {
  340. frame->blocksize = ff_flac_blocksize_table[i];
  341. frame->bs_code[0] = i;
  342. frame->bs_code[1] = 0;
  343. break;
  344. }
  345. }
  346. if (i == 16) {
  347. frame->blocksize = s->avctx->frame_size;
  348. if (frame->blocksize <= 256) {
  349. frame->bs_code[0] = 6;
  350. frame->bs_code[1] = frame->blocksize-1;
  351. } else {
  352. frame->bs_code[0] = 7;
  353. frame->bs_code[1] = frame->blocksize-1;
  354. }
  355. }
  356. for (ch = 0; ch < s->channels; ch++)
  357. frame->subframes[ch].obits = 16;
  358. frame->verbatim_only = 0;
  359. }
  360. /**
  361. * Copy channel-interleaved input samples into separate subframes.
  362. */
  363. static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
  364. {
  365. int i, j, ch;
  366. FlacFrame *frame;
  367. frame = &s->frame;
  368. for (i = 0, j = 0; i < frame->blocksize; i++)
  369. for (ch = 0; ch < s->channels; ch++, j++)
  370. frame->subframes[ch].samples[i] = samples[j];
  371. }
  372. static int rice_count_exact(int32_t *res, int n, int k)
  373. {
  374. int i;
  375. int count = 0;
  376. for (i = 0; i < n; i++) {
  377. int32_t v = -2 * res[i] - 1;
  378. v ^= v >> 31;
  379. count += (v >> k) + 1 + k;
  380. }
  381. return count;
  382. }
  383. static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
  384. int pred_order)
  385. {
  386. int p, porder, psize;
  387. int i, part_end;
  388. int count = 0;
  389. /* subframe header */
  390. count += 8;
  391. /* subframe */
  392. if (sub->type == FLAC_SUBFRAME_CONSTANT) {
  393. count += sub->obits;
  394. } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
  395. count += s->frame.blocksize * sub->obits;
  396. } else {
  397. /* warm-up samples */
  398. count += pred_order * sub->obits;
  399. /* LPC coefficients */
  400. if (sub->type == FLAC_SUBFRAME_LPC)
  401. count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
  402. /* rice-encoded block */
  403. count += 2;
  404. /* partition order */
  405. porder = sub->rc.porder;
  406. psize = s->frame.blocksize >> porder;
  407. count += 4;
  408. /* residual */
  409. i = pred_order;
  410. part_end = psize;
  411. for (p = 0; p < 1 << porder; p++) {
  412. int k = sub->rc.params[p];
  413. count += 4;
  414. count += rice_count_exact(&sub->residual[i], part_end - i, k);
  415. i = part_end;
  416. part_end = FFMIN(s->frame.blocksize, part_end + psize);
  417. }
  418. }
  419. return count;
  420. }
  421. #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
  422. /**
  423. * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
  424. */
  425. static int find_optimal_param(uint32_t sum, int n)
  426. {
  427. int k;
  428. uint32_t sum2;
  429. if (sum <= n >> 1)
  430. return 0;
  431. sum2 = sum - (n >> 1);
  432. k = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
  433. return FFMIN(k, MAX_RICE_PARAM);
  434. }
  435. static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
  436. uint32_t *sums, int n, int pred_order)
  437. {
  438. int i;
  439. int k, cnt, part;
  440. uint32_t all_bits;
  441. part = (1 << porder);
  442. all_bits = 4 * part;
  443. cnt = (n >> porder) - pred_order;
  444. for (i = 0; i < part; i++) {
  445. k = find_optimal_param(sums[i], cnt);
  446. rc->params[i] = k;
  447. all_bits += rice_encode_count(sums[i], cnt, k);
  448. cnt = n >> porder;
  449. }
  450. rc->porder = porder;
  451. return all_bits;
  452. }
  453. static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
  454. uint32_t sums[][MAX_PARTITIONS])
  455. {
  456. int i, j;
  457. int parts;
  458. uint32_t *res, *res_end;
  459. /* sums for highest level */
  460. parts = (1 << pmax);
  461. res = &data[pred_order];
  462. res_end = &data[n >> pmax];
  463. for (i = 0; i < parts; i++) {
  464. uint32_t sum = 0;
  465. while (res < res_end)
  466. sum += *(res++);
  467. sums[pmax][i] = sum;
  468. res_end += n >> pmax;
  469. }
  470. /* sums for lower levels */
  471. for (i = pmax - 1; i >= pmin; i--) {
  472. parts = (1 << i);
  473. for (j = 0; j < parts; j++)
  474. sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
  475. }
  476. }
  477. static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
  478. int32_t *data, int n, int pred_order)
  479. {
  480. int i;
  481. uint32_t bits[MAX_PARTITION_ORDER+1];
  482. int opt_porder;
  483. RiceContext tmp_rc;
  484. uint32_t *udata;
  485. uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
  486. assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
  487. assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
  488. assert(pmin <= pmax);
  489. udata = av_malloc(n * sizeof(uint32_t));
  490. for (i = 0; i < n; i++)
  491. udata[i] = (2*data[i]) ^ (data[i]>>31);
  492. calc_sums(pmin, pmax, udata, n, pred_order, sums);
  493. opt_porder = pmin;
  494. bits[pmin] = UINT32_MAX;
  495. for (i = pmin; i <= pmax; i++) {
  496. bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
  497. if (bits[i] <= bits[opt_porder]) {
  498. opt_porder = i;
  499. *rc = tmp_rc;
  500. }
  501. }
  502. av_freep(&udata);
  503. return bits[opt_porder];
  504. }
  505. static int get_max_p_order(int max_porder, int n, int order)
  506. {
  507. int porder = FFMIN(max_porder, av_log2(n^(n-1)));
  508. if (order > 0)
  509. porder = FFMIN(porder, av_log2(n/order));
  510. return porder;
  511. }
  512. static uint32_t find_subframe_rice_params(FlacEncodeContext *s,
  513. FlacSubframe *sub, int pred_order)
  514. {
  515. int pmin = get_max_p_order(s->options.min_partition_order,
  516. s->frame.blocksize, pred_order);
  517. int pmax = get_max_p_order(s->options.max_partition_order,
  518. s->frame.blocksize, pred_order);
  519. uint32_t bits = 8 + pred_order * sub->obits + 2 + 4;
  520. if (sub->type == FLAC_SUBFRAME_LPC)
  521. bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
  522. bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
  523. s->frame.blocksize, pred_order);
  524. return bits;
  525. }
  526. static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
  527. int order)
  528. {
  529. int i;
  530. for (i = 0; i < order; i++)
  531. res[i] = smp[i];
  532. if (order == 0) {
  533. for (i = order; i < n; i++)
  534. res[i] = smp[i];
  535. } else if (order == 1) {
  536. for (i = order; i < n; i++)
  537. res[i] = smp[i] - smp[i-1];
  538. } else if (order == 2) {
  539. int a = smp[order-1] - smp[order-2];
  540. for (i = order; i < n; i += 2) {
  541. int b = smp[i ] - smp[i-1];
  542. res[i] = b - a;
  543. a = smp[i+1] - smp[i ];
  544. res[i+1] = a - b;
  545. }
  546. } else if (order == 3) {
  547. int a = smp[order-1] - smp[order-2];
  548. int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
  549. for (i = order; i < n; i += 2) {
  550. int b = smp[i ] - smp[i-1];
  551. int d = b - a;
  552. res[i] = d - c;
  553. a = smp[i+1] - smp[i ];
  554. c = a - b;
  555. res[i+1] = c - d;
  556. }
  557. } else {
  558. int a = smp[order-1] - smp[order-2];
  559. int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
  560. int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
  561. for (i = order; i < n; i += 2) {
  562. int b = smp[i ] - smp[i-1];
  563. int d = b - a;
  564. int f = d - c;
  565. res[i ] = f - e;
  566. a = smp[i+1] - smp[i ];
  567. c = a - b;
  568. e = c - d;
  569. res[i+1] = e - f;
  570. }
  571. }
  572. }
  573. #define LPC1(x) {\
  574. int c = coefs[(x)-1];\
  575. p0 += c * s;\
  576. s = smp[i-(x)+1];\
  577. p1 += c * s;\
  578. }
  579. static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
  580. const int32_t *smp, int n, int order,
  581. const int32_t *coefs, int shift, int big)
  582. {
  583. int i;
  584. for (i = order; i < n; i += 2) {
  585. int s = smp[i-order];
  586. int p0 = 0, p1 = 0;
  587. if (big) {
  588. switch (order) {
  589. case 32: LPC1(32)
  590. case 31: LPC1(31)
  591. case 30: LPC1(30)
  592. case 29: LPC1(29)
  593. case 28: LPC1(28)
  594. case 27: LPC1(27)
  595. case 26: LPC1(26)
  596. case 25: LPC1(25)
  597. case 24: LPC1(24)
  598. case 23: LPC1(23)
  599. case 22: LPC1(22)
  600. case 21: LPC1(21)
  601. case 20: LPC1(20)
  602. case 19: LPC1(19)
  603. case 18: LPC1(18)
  604. case 17: LPC1(17)
  605. case 16: LPC1(16)
  606. case 15: LPC1(15)
  607. case 14: LPC1(14)
  608. case 13: LPC1(13)
  609. case 12: LPC1(12)
  610. case 11: LPC1(11)
  611. case 10: LPC1(10)
  612. case 9: LPC1( 9)
  613. LPC1( 8)
  614. LPC1( 7)
  615. LPC1( 6)
  616. LPC1( 5)
  617. LPC1( 4)
  618. LPC1( 3)
  619. LPC1( 2)
  620. LPC1( 1)
  621. }
  622. } else {
  623. switch (order) {
  624. case 8: LPC1( 8)
  625. case 7: LPC1( 7)
  626. case 6: LPC1( 6)
  627. case 5: LPC1( 5)
  628. case 4: LPC1( 4)
  629. case 3: LPC1( 3)
  630. case 2: LPC1( 2)
  631. case 1: LPC1( 1)
  632. }
  633. }
  634. res[i ] = smp[i ] - (p0 >> shift);
  635. res[i+1] = smp[i+1] - (p1 >> shift);
  636. }
  637. }
  638. static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
  639. int order, const int32_t *coefs, int shift)
  640. {
  641. int i;
  642. for (i = 0; i < order; i++)
  643. res[i] = smp[i];
  644. #if CONFIG_SMALL
  645. for (i = order; i < n; i += 2) {
  646. int j;
  647. int s = smp[i];
  648. int p0 = 0, p1 = 0;
  649. for (j = 0; j < order; j++) {
  650. int c = coefs[j];
  651. p1 += c * s;
  652. s = smp[i-j-1];
  653. p0 += c * s;
  654. }
  655. res[i ] = smp[i ] - (p0 >> shift);
  656. res[i+1] = smp[i+1] - (p1 >> shift);
  657. }
  658. #else
  659. switch (order) {
  660. case 1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
  661. case 2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
  662. case 3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
  663. case 4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
  664. case 5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
  665. case 6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
  666. case 7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
  667. case 8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
  668. default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
  669. }
  670. #endif
  671. }
  672. static int encode_residual_ch(FlacEncodeContext *s, int ch)
  673. {
  674. int i, n;
  675. int min_order, max_order, opt_order, omethod;
  676. FlacFrame *frame;
  677. FlacSubframe *sub;
  678. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  679. int shift[MAX_LPC_ORDER];
  680. int32_t *res, *smp;
  681. frame = &s->frame;
  682. sub = &frame->subframes[ch];
  683. res = sub->residual;
  684. smp = sub->samples;
  685. n = frame->blocksize;
  686. /* CONSTANT */
  687. for (i = 1; i < n; i++)
  688. if(smp[i] != smp[0])
  689. break;
  690. if (i == n) {
  691. sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
  692. res[0] = smp[0];
  693. return subframe_count_exact(s, sub, 0);
  694. }
  695. /* VERBATIM */
  696. if (frame->verbatim_only || n < 5) {
  697. sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
  698. memcpy(res, smp, n * sizeof(int32_t));
  699. return subframe_count_exact(s, sub, 0);
  700. }
  701. min_order = s->options.min_prediction_order;
  702. max_order = s->options.max_prediction_order;
  703. omethod = s->options.prediction_order_method;
  704. /* FIXED */
  705. sub->type = FLAC_SUBFRAME_FIXED;
  706. if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
  707. s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
  708. uint32_t bits[MAX_FIXED_ORDER+1];
  709. if (max_order > MAX_FIXED_ORDER)
  710. max_order = MAX_FIXED_ORDER;
  711. opt_order = 0;
  712. bits[0] = UINT32_MAX;
  713. for (i = min_order; i <= max_order; i++) {
  714. encode_residual_fixed(res, smp, n, i);
  715. bits[i] = find_subframe_rice_params(s, sub, i);
  716. if (bits[i] < bits[opt_order])
  717. opt_order = i;
  718. }
  719. sub->order = opt_order;
  720. sub->type_code = sub->type | sub->order;
  721. if (sub->order != max_order) {
  722. encode_residual_fixed(res, smp, n, sub->order);
  723. find_subframe_rice_params(s, sub, sub->order);
  724. }
  725. return subframe_count_exact(s, sub, sub->order);
  726. }
  727. /* LPC */
  728. sub->type = FLAC_SUBFRAME_LPC;
  729. opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
  730. s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
  731. s->options.lpc_passes, omethod,
  732. MAX_LPC_SHIFT, 0);
  733. if (omethod == ORDER_METHOD_2LEVEL ||
  734. omethod == ORDER_METHOD_4LEVEL ||
  735. omethod == ORDER_METHOD_8LEVEL) {
  736. int levels = 1 << omethod;
  737. uint32_t bits[1 << ORDER_METHOD_8LEVEL];
  738. int order;
  739. int opt_index = levels-1;
  740. opt_order = max_order-1;
  741. bits[opt_index] = UINT32_MAX;
  742. for (i = levels-1; i >= 0; i--) {
  743. order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
  744. if (order < 0)
  745. order = 0;
  746. encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
  747. bits[i] = find_subframe_rice_params(s, sub, order+1);
  748. if (bits[i] < bits[opt_index]) {
  749. opt_index = i;
  750. opt_order = order;
  751. }
  752. }
  753. opt_order++;
  754. } else if (omethod == ORDER_METHOD_SEARCH) {
  755. // brute-force optimal order search
  756. uint32_t bits[MAX_LPC_ORDER];
  757. opt_order = 0;
  758. bits[0] = UINT32_MAX;
  759. for (i = min_order-1; i < max_order; i++) {
  760. encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
  761. bits[i] = find_subframe_rice_params(s, sub, i+1);
  762. if (bits[i] < bits[opt_order])
  763. opt_order = i;
  764. }
  765. opt_order++;
  766. } else if (omethod == ORDER_METHOD_LOG) {
  767. uint32_t bits[MAX_LPC_ORDER];
  768. int step;
  769. opt_order = min_order - 1 + (max_order-min_order)/3;
  770. memset(bits, -1, sizeof(bits));
  771. for (step = 16; step; step >>= 1) {
  772. int last = opt_order;
  773. for (i = last-step; i <= last+step; i += step) {
  774. if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
  775. continue;
  776. encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
  777. bits[i] = find_subframe_rice_params(s, sub, i+1);
  778. if (bits[i] < bits[opt_order])
  779. opt_order = i;
  780. }
  781. }
  782. opt_order++;
  783. }
  784. sub->order = opt_order;
  785. sub->type_code = sub->type | (sub->order-1);
  786. sub->shift = shift[sub->order-1];
  787. for (i = 0; i < sub->order; i++)
  788. sub->coefs[i] = coefs[sub->order-1][i];
  789. encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
  790. find_subframe_rice_params(s, sub, sub->order);
  791. return subframe_count_exact(s, sub, sub->order);
  792. }
  793. static int count_frame_header(FlacEncodeContext *s)
  794. {
  795. uint8_t av_unused tmp;
  796. int count;
  797. /*
  798. <14> Sync code
  799. <1> Reserved
  800. <1> Blocking strategy
  801. <4> Block size in inter-channel samples
  802. <4> Sample rate
  803. <4> Channel assignment
  804. <3> Sample size in bits
  805. <1> Reserved
  806. */
  807. count = 32;
  808. /* coded frame number */
  809. PUT_UTF8(s->frame_count, tmp, count += 8;)
  810. /* explicit block size */
  811. if (s->frame.bs_code[0] == 6)
  812. count += 8;
  813. else if (s->frame.bs_code[0] == 7)
  814. count += 16;
  815. /* explicit sample rate */
  816. count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
  817. /* frame header CRC-8 */
  818. count += 8;
  819. return count;
  820. }
  821. static int encode_frame(FlacEncodeContext *s)
  822. {
  823. int ch, count;
  824. count = count_frame_header(s);
  825. for (ch = 0; ch < s->channels; ch++)
  826. count += encode_residual_ch(s, ch);
  827. count += (8 - (count & 7)) & 7; // byte alignment
  828. count += 16; // CRC-16
  829. return count >> 3;
  830. }
  831. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
  832. {
  833. int i, best;
  834. int32_t lt, rt;
  835. uint64_t sum[4];
  836. uint64_t score[4];
  837. int k;
  838. /* calculate sum of 2nd order residual for each channel */
  839. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  840. for (i = 2; i < n; i++) {
  841. lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
  842. rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
  843. sum[2] += FFABS((lt + rt) >> 1);
  844. sum[3] += FFABS(lt - rt);
  845. sum[0] += FFABS(lt);
  846. sum[1] += FFABS(rt);
  847. }
  848. /* estimate bit counts */
  849. for (i = 0; i < 4; i++) {
  850. k = find_optimal_param(2 * sum[i], n);
  851. sum[i] = rice_encode_count( 2 * sum[i], n, k);
  852. }
  853. /* calculate score for each mode */
  854. score[0] = sum[0] + sum[1];
  855. score[1] = sum[0] + sum[3];
  856. score[2] = sum[1] + sum[3];
  857. score[3] = sum[2] + sum[3];
  858. /* return mode with lowest score */
  859. best = 0;
  860. for (i = 1; i < 4; i++)
  861. if (score[i] < score[best])
  862. best = i;
  863. if (best == 0) {
  864. return FLAC_CHMODE_INDEPENDENT;
  865. } else if (best == 1) {
  866. return FLAC_CHMODE_LEFT_SIDE;
  867. } else if (best == 2) {
  868. return FLAC_CHMODE_RIGHT_SIDE;
  869. } else {
  870. return FLAC_CHMODE_MID_SIDE;
  871. }
  872. }
  873. /**
  874. * Perform stereo channel decorrelation.
  875. */
  876. static void channel_decorrelation(FlacEncodeContext *s)
  877. {
  878. FlacFrame *frame;
  879. int32_t *left, *right;
  880. int i, n;
  881. frame = &s->frame;
  882. n = frame->blocksize;
  883. left = frame->subframes[0].samples;
  884. right = frame->subframes[1].samples;
  885. if (s->channels != 2) {
  886. frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
  887. return;
  888. }
  889. frame->ch_mode = estimate_stereo_mode(left, right, n);
  890. /* perform decorrelation and adjust bits-per-sample */
  891. if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
  892. return;
  893. if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
  894. int32_t tmp;
  895. for (i = 0; i < n; i++) {
  896. tmp = left[i];
  897. left[i] = (tmp + right[i]) >> 1;
  898. right[i] = tmp - right[i];
  899. }
  900. frame->subframes[1].obits++;
  901. } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
  902. for (i = 0; i < n; i++)
  903. right[i] = left[i] - right[i];
  904. frame->subframes[1].obits++;
  905. } else {
  906. for (i = 0; i < n; i++)
  907. left[i] -= right[i];
  908. frame->subframes[0].obits++;
  909. }
  910. }
  911. static void write_utf8(PutBitContext *pb, uint32_t val)
  912. {
  913. uint8_t tmp;
  914. PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
  915. }
  916. static void write_frame_header(FlacEncodeContext *s)
  917. {
  918. FlacFrame *frame;
  919. int crc;
  920. frame = &s->frame;
  921. put_bits(&s->pb, 16, 0xFFF8);
  922. put_bits(&s->pb, 4, frame->bs_code[0]);
  923. put_bits(&s->pb, 4, s->sr_code[0]);
  924. if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
  925. put_bits(&s->pb, 4, s->channels-1);
  926. else
  927. put_bits(&s->pb, 4, frame->ch_mode);
  928. put_bits(&s->pb, 3, 4); /* bits-per-sample code */
  929. put_bits(&s->pb, 1, 0);
  930. write_utf8(&s->pb, s->frame_count);
  931. if (frame->bs_code[0] == 6)
  932. put_bits(&s->pb, 8, frame->bs_code[1]);
  933. else if (frame->bs_code[0] == 7)
  934. put_bits(&s->pb, 16, frame->bs_code[1]);
  935. if (s->sr_code[0] == 12)
  936. put_bits(&s->pb, 8, s->sr_code[1]);
  937. else if (s->sr_code[0] > 12)
  938. put_bits(&s->pb, 16, s->sr_code[1]);
  939. flush_put_bits(&s->pb);
  940. crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
  941. put_bits_count(&s->pb) >> 3);
  942. put_bits(&s->pb, 8, crc);
  943. }
  944. static void write_subframes(FlacEncodeContext *s)
  945. {
  946. int ch;
  947. for (ch = 0; ch < s->channels; ch++) {
  948. FlacSubframe *sub = &s->frame.subframes[ch];
  949. int i, p, porder, psize;
  950. int32_t *part_end;
  951. int32_t *res = sub->residual;
  952. int32_t *frame_end = &sub->residual[s->frame.blocksize];
  953. /* subframe header */
  954. put_bits(&s->pb, 1, 0);
  955. put_bits(&s->pb, 6, sub->type_code);
  956. put_bits(&s->pb, 1, 0); /* no wasted bits */
  957. /* subframe */
  958. if (sub->type == FLAC_SUBFRAME_CONSTANT) {
  959. put_sbits(&s->pb, sub->obits, res[0]);
  960. } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
  961. while (res < frame_end)
  962. put_sbits(&s->pb, sub->obits, *res++);
  963. } else {
  964. /* warm-up samples */
  965. for (i = 0; i < sub->order; i++)
  966. put_sbits(&s->pb, sub->obits, *res++);
  967. /* LPC coefficients */
  968. if (sub->type == FLAC_SUBFRAME_LPC) {
  969. int cbits = s->options.lpc_coeff_precision;
  970. put_bits( &s->pb, 4, cbits-1);
  971. put_sbits(&s->pb, 5, sub->shift);
  972. for (i = 0; i < sub->order; i++)
  973. put_sbits(&s->pb, cbits, sub->coefs[i]);
  974. }
  975. /* rice-encoded block */
  976. put_bits(&s->pb, 2, 0);
  977. /* partition order */
  978. porder = sub->rc.porder;
  979. psize = s->frame.blocksize >> porder;
  980. put_bits(&s->pb, 4, porder);
  981. /* residual */
  982. part_end = &sub->residual[psize];
  983. for (p = 0; p < 1 << porder; p++) {
  984. int k = sub->rc.params[p];
  985. put_bits(&s->pb, 4, k);
  986. while (res < part_end)
  987. set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
  988. part_end = FFMIN(frame_end, part_end + psize);
  989. }
  990. }
  991. }
  992. }
  993. static void write_frame_footer(FlacEncodeContext *s)
  994. {
  995. int crc;
  996. flush_put_bits(&s->pb);
  997. crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
  998. put_bits_count(&s->pb)>>3));
  999. put_bits(&s->pb, 16, crc);
  1000. flush_put_bits(&s->pb);
  1001. }
  1002. static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
  1003. {
  1004. init_put_bits(&s->pb, frame, buf_size);
  1005. write_frame_header(s);
  1006. write_subframes(s);
  1007. write_frame_footer(s);
  1008. return put_bits_count(&s->pb) >> 3;
  1009. }
  1010. static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
  1011. {
  1012. #if HAVE_BIGENDIAN
  1013. int i;
  1014. for (i = 0; i < s->frame.blocksize * s->channels; i++) {
  1015. int16_t smp = av_le2ne16(samples[i]);
  1016. av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
  1017. }
  1018. #else
  1019. av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
  1020. #endif
  1021. }
  1022. static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
  1023. int buf_size, void *data)
  1024. {
  1025. FlacEncodeContext *s;
  1026. const int16_t *samples = data;
  1027. int frame_bytes, out_bytes;
  1028. s = avctx->priv_data;
  1029. /* when the last block is reached, update the header in extradata */
  1030. if (!data) {
  1031. s->max_framesize = s->max_encoded_framesize;
  1032. av_md5_final(s->md5ctx, s->md5sum);
  1033. write_streaminfo(s, avctx->extradata);
  1034. return 0;
  1035. }
  1036. /* change max_framesize for small final frame */
  1037. if (avctx->frame_size < s->frame.blocksize) {
  1038. s->max_framesize = ff_flac_get_max_frame_size(avctx->frame_size,
  1039. s->channels, 16);
  1040. }
  1041. init_frame(s);
  1042. copy_samples(s, samples);
  1043. channel_decorrelation(s);
  1044. frame_bytes = encode_frame(s);
  1045. /* fallback to verbatim mode if the compressed frame is larger than it
  1046. would be if encoded uncompressed. */
  1047. if (frame_bytes > s->max_framesize) {
  1048. s->frame.verbatim_only = 1;
  1049. frame_bytes = encode_frame(s);
  1050. }
  1051. if (buf_size < frame_bytes) {
  1052. av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
  1053. return 0;
  1054. }
  1055. out_bytes = write_frame(s, frame, buf_size);
  1056. s->frame_count++;
  1057. avctx->coded_frame->pts = s->sample_count;
  1058. s->sample_count += avctx->frame_size;
  1059. update_md5_sum(s, samples);
  1060. if (out_bytes > s->max_encoded_framesize)
  1061. s->max_encoded_framesize = out_bytes;
  1062. if (out_bytes < s->min_framesize)
  1063. s->min_framesize = out_bytes;
  1064. return out_bytes;
  1065. }
  1066. static av_cold int flac_encode_close(AVCodecContext *avctx)
  1067. {
  1068. if (avctx->priv_data) {
  1069. FlacEncodeContext *s = avctx->priv_data;
  1070. av_freep(&s->md5ctx);
  1071. ff_lpc_end(&s->lpc_ctx);
  1072. }
  1073. av_freep(&avctx->extradata);
  1074. avctx->extradata_size = 0;
  1075. av_freep(&avctx->coded_frame);
  1076. return 0;
  1077. }
  1078. #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
  1079. static const AVOption options[] = {
  1080. { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.dbl = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
  1081. { "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" },
  1082. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1083. { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1084. { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1085. { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
  1086. { "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 },
  1087. { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
  1088. { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
  1089. { "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" },
  1090. { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1091. { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1092. { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1093. { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1094. { "search", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1095. { "log", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
  1096. { NULL },
  1097. };
  1098. static const AVClass flac_encoder_class = {
  1099. "FLAC encoder",
  1100. av_default_item_name,
  1101. options,
  1102. LIBAVUTIL_VERSION_INT,
  1103. };
  1104. AVCodec ff_flac_encoder = {
  1105. .name = "flac",
  1106. .type = AVMEDIA_TYPE_AUDIO,
  1107. .id = CODEC_ID_FLAC,
  1108. .priv_data_size = sizeof(FlacEncodeContext),
  1109. .init = flac_encode_init,
  1110. .encode = flac_encode_frame,
  1111. .close = flac_encode_close,
  1112. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  1113. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  1114. .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
  1115. .priv_class = &flac_encoder_class,
  1116. };