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.

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