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.

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