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.

1318 lines
42KB

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