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.

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