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.

1335 lines
42KB

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