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.

1293 lines
39KB

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