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.

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