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.

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