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.

1283 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 find_subblock_rice_params(RiceContext *rc, int pmin, int pmax,
  525. int32_t *data, int n, int pred_order,
  526. int bps, int precision)
  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. if (precision > 0)
  533. bits += 4 + 5 + pred_order * precision;
  534. bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
  535. return bits;
  536. }
  537. static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
  538. int order)
  539. {
  540. int i;
  541. for (i = 0; i < order; i++)
  542. res[i] = smp[i];
  543. if (order == 0) {
  544. for (i = order; i < n; i++)
  545. res[i] = smp[i];
  546. } else if (order == 1) {
  547. for (i = order; i < n; i++)
  548. res[i] = smp[i] - smp[i-1];
  549. } else if (order == 2) {
  550. int a = smp[order-1] - smp[order-2];
  551. for (i = order; i < n; i += 2) {
  552. int b = smp[i ] - smp[i-1];
  553. res[i] = b - a;
  554. a = smp[i+1] - smp[i ];
  555. res[i+1] = a - b;
  556. }
  557. } else if (order == 3) {
  558. int a = smp[order-1] - smp[order-2];
  559. int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
  560. for (i = order; i < n; i += 2) {
  561. int b = smp[i ] - smp[i-1];
  562. int d = b - a;
  563. res[i] = d - c;
  564. a = smp[i+1] - smp[i ];
  565. c = a - b;
  566. res[i+1] = c - d;
  567. }
  568. } else {
  569. int a = smp[order-1] - smp[order-2];
  570. int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
  571. int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
  572. for (i = order; i < n; i += 2) {
  573. int b = smp[i ] - smp[i-1];
  574. int d = b - a;
  575. int f = d - c;
  576. res[i ] = f - e;
  577. a = smp[i+1] - smp[i ];
  578. c = a - b;
  579. e = c - d;
  580. res[i+1] = e - f;
  581. }
  582. }
  583. }
  584. #define LPC1(x) {\
  585. int c = coefs[(x)-1];\
  586. p0 += c * s;\
  587. s = smp[i-(x)+1];\
  588. p1 += c * s;\
  589. }
  590. static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
  591. const int32_t *smp, int n, int order,
  592. const int32_t *coefs, int shift, int big)
  593. {
  594. int i;
  595. for (i = order; i < n; i += 2) {
  596. int s = smp[i-order];
  597. int p0 = 0, p1 = 0;
  598. if (big) {
  599. switch (order) {
  600. case 32: LPC1(32)
  601. case 31: LPC1(31)
  602. case 30: LPC1(30)
  603. case 29: LPC1(29)
  604. case 28: LPC1(28)
  605. case 27: LPC1(27)
  606. case 26: LPC1(26)
  607. case 25: LPC1(25)
  608. case 24: LPC1(24)
  609. case 23: LPC1(23)
  610. case 22: LPC1(22)
  611. case 21: LPC1(21)
  612. case 20: LPC1(20)
  613. case 19: LPC1(19)
  614. case 18: LPC1(18)
  615. case 17: LPC1(17)
  616. case 16: LPC1(16)
  617. case 15: LPC1(15)
  618. case 14: LPC1(14)
  619. case 13: LPC1(13)
  620. case 12: LPC1(12)
  621. case 11: LPC1(11)
  622. case 10: LPC1(10)
  623. case 9: LPC1( 9)
  624. LPC1( 8)
  625. LPC1( 7)
  626. LPC1( 6)
  627. LPC1( 5)
  628. LPC1( 4)
  629. LPC1( 3)
  630. LPC1( 2)
  631. LPC1( 1)
  632. }
  633. } else {
  634. switch (order) {
  635. case 8: LPC1( 8)
  636. case 7: LPC1( 7)
  637. case 6: LPC1( 6)
  638. case 5: LPC1( 5)
  639. case 4: LPC1( 4)
  640. case 3: LPC1( 3)
  641. case 2: LPC1( 2)
  642. case 1: LPC1( 1)
  643. }
  644. }
  645. res[i ] = smp[i ] - (p0 >> shift);
  646. res[i+1] = smp[i+1] - (p1 >> shift);
  647. }
  648. }
  649. static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
  650. int order, const int32_t *coefs, int shift)
  651. {
  652. int i;
  653. for (i = 0; i < order; i++)
  654. res[i] = smp[i];
  655. #if CONFIG_SMALL
  656. for (i = order; i < n; i += 2) {
  657. int j;
  658. int s = smp[i];
  659. int p0 = 0, p1 = 0;
  660. for (j = 0; j < order; j++) {
  661. int c = coefs[j];
  662. p1 += c * s;
  663. s = smp[i-j-1];
  664. p0 += c * s;
  665. }
  666. res[i ] = smp[i ] - (p0 >> shift);
  667. res[i+1] = smp[i+1] - (p1 >> shift);
  668. }
  669. #else
  670. switch (order) {
  671. case 1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
  672. case 2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
  673. case 3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
  674. case 4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
  675. case 5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
  676. case 6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
  677. case 7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
  678. case 8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
  679. default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
  680. }
  681. #endif
  682. }
  683. static int encode_residual_ch(FlacEncodeContext *s, int ch)
  684. {
  685. int i, n;
  686. int min_order, max_order, opt_order, precision, omethod;
  687. int min_porder, max_porder;
  688. FlacFrame *frame;
  689. FlacSubframe *sub;
  690. int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
  691. int shift[MAX_LPC_ORDER];
  692. int32_t *res, *smp;
  693. frame = &s->frame;
  694. sub = &frame->subframes[ch];
  695. res = sub->residual;
  696. smp = sub->samples;
  697. n = frame->blocksize;
  698. /* CONSTANT */
  699. for (i = 1; i < n; i++)
  700. if(smp[i] != smp[0])
  701. break;
  702. if (i == n) {
  703. sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
  704. res[0] = smp[0];
  705. return sub->obits;
  706. }
  707. /* VERBATIM */
  708. if (frame->verbatim_only || n < 5) {
  709. sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
  710. memcpy(res, smp, n * sizeof(int32_t));
  711. return sub->obits * n;
  712. }
  713. min_order = s->options.min_prediction_order;
  714. max_order = s->options.max_prediction_order;
  715. min_porder = s->options.min_partition_order;
  716. max_porder = s->options.max_partition_order;
  717. precision = s->options.lpc_coeff_precision;
  718. omethod = s->options.prediction_order_method;
  719. /* FIXED */
  720. if (s->options.lpc_type == AV_LPC_TYPE_NONE ||
  721. s->options.lpc_type == AV_LPC_TYPE_FIXED || n <= max_order) {
  722. uint32_t bits[MAX_FIXED_ORDER+1];
  723. if (max_order > MAX_FIXED_ORDER)
  724. max_order = MAX_FIXED_ORDER;
  725. opt_order = 0;
  726. bits[0] = UINT32_MAX;
  727. for (i = min_order; i <= max_order; i++) {
  728. encode_residual_fixed(res, smp, n, i);
  729. bits[i] = find_subblock_rice_params(&sub->rc, min_porder,
  730. max_porder, res, n, i,
  731. sub->obits, 0);
  732. if (bits[i] < bits[opt_order])
  733. opt_order = i;
  734. }
  735. sub->order = opt_order;
  736. sub->type = FLAC_SUBFRAME_FIXED;
  737. sub->type_code = sub->type | sub->order;
  738. if (sub->order != max_order) {
  739. encode_residual_fixed(res, smp, n, sub->order);
  740. return find_subblock_rice_params(&sub->rc, min_porder, max_porder,
  741. res, n, sub->order, sub->obits, 0);
  742. }
  743. return bits[sub->order];
  744. }
  745. /* LPC */
  746. opt_order = ff_lpc_calc_coefs(&s->dsp, smp, n, min_order, max_order,
  747. precision, coefs, shift, s->options.lpc_type,
  748. s->options.lpc_passes, omethod,
  749. MAX_LPC_SHIFT, 0);
  750. if (omethod == ORDER_METHOD_2LEVEL ||
  751. omethod == ORDER_METHOD_4LEVEL ||
  752. omethod == ORDER_METHOD_8LEVEL) {
  753. int levels = 1 << omethod;
  754. uint32_t bits[1 << ORDER_METHOD_8LEVEL];
  755. int order;
  756. int opt_index = levels-1;
  757. opt_order = max_order-1;
  758. bits[opt_index] = UINT32_MAX;
  759. for (i = levels-1; i >= 0; i--) {
  760. order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
  761. if (order < 0)
  762. order = 0;
  763. encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
  764. bits[i] = find_subblock_rice_params(&sub->rc, min_porder,
  765. max_porder, res, n, order+1,
  766. sub->obits, precision);
  767. if (bits[i] < bits[opt_index]) {
  768. opt_index = i;
  769. opt_order = order;
  770. }
  771. }
  772. opt_order++;
  773. } else if (omethod == ORDER_METHOD_SEARCH) {
  774. // brute-force optimal order search
  775. uint32_t bits[MAX_LPC_ORDER];
  776. opt_order = 0;
  777. bits[0] = UINT32_MAX;
  778. for (i = min_order-1; i < max_order; i++) {
  779. encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
  780. bits[i] = find_subblock_rice_params(&sub->rc, min_porder,
  781. max_porder, res, n, i+1,
  782. sub->obits, precision);
  783. if (bits[i] < bits[opt_order])
  784. opt_order = i;
  785. }
  786. opt_order++;
  787. } else if (omethod == ORDER_METHOD_LOG) {
  788. uint32_t bits[MAX_LPC_ORDER];
  789. int step;
  790. opt_order = min_order - 1 + (max_order-min_order)/3;
  791. memset(bits, -1, sizeof(bits));
  792. for (step = 16; step; step >>= 1) {
  793. int last = opt_order;
  794. for (i = last-step; i <= last+step; i += step) {
  795. if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
  796. continue;
  797. encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
  798. bits[i] = find_subblock_rice_params(&sub->rc, min_porder,
  799. max_porder, res, n, i+1,
  800. sub->obits, precision);
  801. if (bits[i] < bits[opt_order])
  802. opt_order = i;
  803. }
  804. }
  805. opt_order++;
  806. }
  807. sub->order = opt_order;
  808. sub->type = FLAC_SUBFRAME_LPC;
  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. return find_subblock_rice_params(&sub->rc, min_porder, max_porder, res, n,
  815. sub->order, sub->obits, precision);
  816. }
  817. static int count_frame_header(FlacEncodeContext *s)
  818. {
  819. uint8_t 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. count += FFMAX(0, s->frame.bs_code[0] - 5) * 8;
  836. /* explicit sample rate */
  837. count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
  838. /* frame header CRC-8 */
  839. count += 8;
  840. return count;
  841. }
  842. static int encode_frame(FlacEncodeContext *s)
  843. {
  844. int ch, count;
  845. count = count_frame_header(s);
  846. for (ch = 0; ch < s->channels; ch++)
  847. count += encode_residual_ch(s, ch);
  848. count += (8 - (count & 7)) & 7; // byte alignment
  849. count += 16; // CRC-16
  850. return count >> 3;
  851. }
  852. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
  853. {
  854. int i, best;
  855. int32_t lt, rt;
  856. uint64_t sum[4];
  857. uint64_t score[4];
  858. int k;
  859. /* calculate sum of 2nd order residual for each channel */
  860. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  861. for (i = 2; i < n; i++) {
  862. lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
  863. rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
  864. sum[2] += FFABS((lt + rt) >> 1);
  865. sum[3] += FFABS(lt - rt);
  866. sum[0] += FFABS(lt);
  867. sum[1] += FFABS(rt);
  868. }
  869. /* estimate bit counts */
  870. for (i = 0; i < 4; i++) {
  871. k = find_optimal_param(2 * sum[i], n);
  872. sum[i] = rice_encode_count( 2 * sum[i], n, k);
  873. }
  874. /* calculate score for each mode */
  875. score[0] = sum[0] + sum[1];
  876. score[1] = sum[0] + sum[3];
  877. score[2] = sum[1] + sum[3];
  878. score[3] = sum[2] + sum[3];
  879. /* return mode with lowest score */
  880. best = 0;
  881. for (i = 1; i < 4; i++)
  882. if (score[i] < score[best])
  883. best = i;
  884. if (best == 0) {
  885. return FLAC_CHMODE_INDEPENDENT;
  886. } else if (best == 1) {
  887. return FLAC_CHMODE_LEFT_SIDE;
  888. } else if (best == 2) {
  889. return FLAC_CHMODE_RIGHT_SIDE;
  890. } else {
  891. return FLAC_CHMODE_MID_SIDE;
  892. }
  893. }
  894. /**
  895. * Perform stereo channel decorrelation.
  896. */
  897. static void channel_decorrelation(FlacEncodeContext *s)
  898. {
  899. FlacFrame *frame;
  900. int32_t *left, *right;
  901. int i, n;
  902. frame = &s->frame;
  903. n = frame->blocksize;
  904. left = frame->subframes[0].samples;
  905. right = frame->subframes[1].samples;
  906. if (s->channels != 2) {
  907. frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
  908. return;
  909. }
  910. frame->ch_mode = estimate_stereo_mode(left, right, n);
  911. /* perform decorrelation and adjust bits-per-sample */
  912. if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
  913. return;
  914. if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
  915. int32_t tmp;
  916. for (i = 0; i < n; i++) {
  917. tmp = left[i];
  918. left[i] = (tmp + right[i]) >> 1;
  919. right[i] = tmp - right[i];
  920. }
  921. frame->subframes[1].obits++;
  922. } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
  923. for (i = 0; i < n; i++)
  924. right[i] = left[i] - right[i];
  925. frame->subframes[1].obits++;
  926. } else {
  927. for (i = 0; i < n; i++)
  928. left[i] -= right[i];
  929. frame->subframes[0].obits++;
  930. }
  931. }
  932. static void write_utf8(PutBitContext *pb, uint32_t val)
  933. {
  934. uint8_t tmp;
  935. PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
  936. }
  937. static void output_frame_header(FlacEncodeContext *s)
  938. {
  939. FlacFrame *frame;
  940. int crc;
  941. frame = &s->frame;
  942. put_bits(&s->pb, 16, 0xFFF8);
  943. put_bits(&s->pb, 4, frame->bs_code[0]);
  944. put_bits(&s->pb, 4, s->sr_code[0]);
  945. if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
  946. put_bits(&s->pb, 4, s->channels-1);
  947. else
  948. put_bits(&s->pb, 4, frame->ch_mode);
  949. put_bits(&s->pb, 3, 4); /* bits-per-sample code */
  950. put_bits(&s->pb, 1, 0);
  951. write_utf8(&s->pb, s->frame_count);
  952. if (frame->bs_code[0] == 6)
  953. put_bits(&s->pb, 8, frame->bs_code[1]);
  954. else if (frame->bs_code[0] == 7)
  955. put_bits(&s->pb, 16, frame->bs_code[1]);
  956. if (s->sr_code[0] == 12)
  957. put_bits(&s->pb, 8, s->sr_code[1]);
  958. else if (s->sr_code[0] > 12)
  959. put_bits(&s->pb, 16, s->sr_code[1]);
  960. flush_put_bits(&s->pb);
  961. crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
  962. put_bits_count(&s->pb) >> 3);
  963. put_bits(&s->pb, 8, crc);
  964. }
  965. static void output_subframes(FlacEncodeContext *s)
  966. {
  967. int ch;
  968. for (ch = 0; ch < s->channels; ch++) {
  969. FlacSubframe *sub = &s->frame.subframes[ch];
  970. int i, p, porder, psize;
  971. int32_t *part_end;
  972. int32_t *res = sub->residual;
  973. int32_t *frame_end = &sub->residual[s->frame.blocksize];
  974. /* subframe header */
  975. put_bits(&s->pb, 1, 0);
  976. put_bits(&s->pb, 6, sub->type_code);
  977. put_bits(&s->pb, 1, 0); /* no wasted bits */
  978. /* subframe */
  979. if (sub->type == FLAC_SUBFRAME_CONSTANT) {
  980. put_sbits(&s->pb, sub->obits, res[0]);
  981. } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
  982. while (res < frame_end)
  983. put_sbits(&s->pb, sub->obits, *res++);
  984. } else {
  985. /* warm-up samples */
  986. for (i = 0; i < sub->order; i++)
  987. put_sbits(&s->pb, sub->obits, *res++);
  988. /* LPC coefficients */
  989. if (sub->type == FLAC_SUBFRAME_LPC) {
  990. int cbits = s->options.lpc_coeff_precision;
  991. put_bits( &s->pb, 4, cbits-1);
  992. put_sbits(&s->pb, 5, sub->shift);
  993. for (i = 0; i < sub->order; i++)
  994. put_sbits(&s->pb, cbits, sub->coefs[i]);
  995. }
  996. /* rice-encoded block */
  997. put_bits(&s->pb, 2, 0);
  998. /* partition order */
  999. porder = sub->rc.porder;
  1000. psize = s->frame.blocksize >> porder;
  1001. put_bits(&s->pb, 4, porder);
  1002. /* residual */
  1003. part_end = &sub->residual[psize];
  1004. for (p = 0; p < 1 << porder; p++) {
  1005. int k = sub->rc.params[p];
  1006. put_bits(&s->pb, 4, k);
  1007. while (res < part_end)
  1008. set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
  1009. part_end = FFMIN(frame_end, part_end + psize);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. static void output_frame_footer(FlacEncodeContext *s)
  1015. {
  1016. int crc;
  1017. flush_put_bits(&s->pb);
  1018. crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
  1019. put_bits_count(&s->pb)>>3));
  1020. put_bits(&s->pb, 16, crc);
  1021. flush_put_bits(&s->pb);
  1022. }
  1023. static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
  1024. {
  1025. init_put_bits(&s->pb, frame, buf_size);
  1026. output_frame_header(s);
  1027. output_subframes(s);
  1028. output_frame_footer(s);
  1029. return put_bits_count(&s->pb) >> 3;
  1030. }
  1031. static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
  1032. {
  1033. #if HAVE_BIGENDIAN
  1034. int i;
  1035. for (i = 0; i < s->frame.blocksize * s->channels; i++) {
  1036. int16_t smp = av_le2ne16(samples[i]);
  1037. av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
  1038. }
  1039. #else
  1040. av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
  1041. #endif
  1042. }
  1043. static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
  1044. int buf_size, void *data)
  1045. {
  1046. FlacEncodeContext *s;
  1047. const int16_t *samples = data;
  1048. int out_bytes;
  1049. s = avctx->priv_data;
  1050. if (buf_size < s->max_framesize * 2) {
  1051. av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
  1052. return 0;
  1053. }
  1054. /* when the last block is reached, update the header in extradata */
  1055. if (!data) {
  1056. s->max_framesize = s->max_encoded_framesize;
  1057. av_md5_final(s->md5ctx, s->md5sum);
  1058. write_streaminfo(s, avctx->extradata);
  1059. return 0;
  1060. }
  1061. init_frame(s);
  1062. copy_samples(s, samples);
  1063. channel_decorrelation(s);
  1064. encode_frame(s);
  1065. out_bytes = write_frame(s, frame, buf_size);
  1066. /* fallback to verbatim mode if the compressed frame is larger than it
  1067. would be if encoded uncompressed. */
  1068. if (out_bytes > s->max_framesize) {
  1069. s->frame.verbatim_only = 1;
  1070. encode_frame(s);
  1071. out_bytes = write_frame(s, frame, buf_size);
  1072. }
  1073. s->frame_count++;
  1074. avctx->coded_frame->pts = s->sample_count;
  1075. s->sample_count += avctx->frame_size;
  1076. update_md5_sum(s, samples);
  1077. if (out_bytes > s->max_encoded_framesize)
  1078. s->max_encoded_framesize = out_bytes;
  1079. if (out_bytes < s->min_framesize)
  1080. s->min_framesize = out_bytes;
  1081. return out_bytes;
  1082. }
  1083. static av_cold int flac_encode_close(AVCodecContext *avctx)
  1084. {
  1085. if (avctx->priv_data) {
  1086. FlacEncodeContext *s = avctx->priv_data;
  1087. av_freep(&s->md5ctx);
  1088. }
  1089. av_freep(&avctx->extradata);
  1090. avctx->extradata_size = 0;
  1091. av_freep(&avctx->coded_frame);
  1092. return 0;
  1093. }
  1094. AVCodec flac_encoder = {
  1095. "flac",
  1096. AVMEDIA_TYPE_AUDIO,
  1097. CODEC_ID_FLAC,
  1098. sizeof(FlacEncodeContext),
  1099. flac_encode_init,
  1100. flac_encode_frame,
  1101. flac_encode_close,
  1102. NULL,
  1103. .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
  1104. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  1105. .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
  1106. };