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.

1345 lines
40KB

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