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.

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