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.

1280 lines
40KB

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