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.

1311 lines
38KB

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