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.

1342 lines
40KB

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