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.

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