You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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