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.

838 lines
20KB

  1. /**
  2. * FLAC audio encoder
  3. * Copyright (c) 2006 Justin Ruggles <jruggle@earthlink.net>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avcodec.h"
  20. #include "bitstream.h"
  21. #include "crc.h"
  22. #include "golomb.h"
  23. #define FLAC_MAX_CH 8
  24. #define FLAC_MIN_BLOCKSIZE 16
  25. #define FLAC_MAX_BLOCKSIZE 65535
  26. #define FLAC_SUBFRAME_CONSTANT 0
  27. #define FLAC_SUBFRAME_VERBATIM 1
  28. #define FLAC_SUBFRAME_FIXED 8
  29. #define FLAC_SUBFRAME_LPC 32
  30. #define FLAC_CHMODE_NOT_STEREO 0
  31. #define FLAC_CHMODE_LEFT_RIGHT 1
  32. #define FLAC_CHMODE_LEFT_SIDE 8
  33. #define FLAC_CHMODE_RIGHT_SIDE 9
  34. #define FLAC_CHMODE_MID_SIDE 10
  35. #define FLAC_STREAMINFO_SIZE 34
  36. typedef struct RiceContext {
  37. int porder;
  38. int params[256];
  39. } RiceContext;
  40. typedef struct FlacSubframe {
  41. int type;
  42. int type_code;
  43. int obits;
  44. int order;
  45. RiceContext rc;
  46. int32_t samples[FLAC_MAX_BLOCKSIZE];
  47. int32_t residual[FLAC_MAX_BLOCKSIZE];
  48. } FlacSubframe;
  49. typedef struct FlacFrame {
  50. FlacSubframe subframes[FLAC_MAX_CH];
  51. int blocksize;
  52. int bs_code[2];
  53. uint8_t crc8;
  54. int ch_mode;
  55. } FlacFrame;
  56. typedef struct FlacEncodeContext {
  57. PutBitContext pb;
  58. int channels;
  59. int ch_code;
  60. int samplerate;
  61. int sr_code[2];
  62. int blocksize;
  63. int max_framesize;
  64. uint32_t frame_count;
  65. FlacFrame frame;
  66. AVCodecContext *avctx;
  67. } FlacEncodeContext;
  68. static const int flac_samplerates[16] = {
  69. 0, 0, 0, 0,
  70. 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
  71. 0, 0, 0, 0
  72. };
  73. static const int flac_blocksizes[16] = {
  74. 0,
  75. 192,
  76. 576, 1152, 2304, 4608,
  77. 0, 0,
  78. 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
  79. };
  80. /**
  81. * Writes streaminfo metadata block to byte array
  82. */
  83. static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
  84. {
  85. PutBitContext pb;
  86. memset(header, 0, FLAC_STREAMINFO_SIZE);
  87. init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
  88. /* streaminfo metadata block */
  89. put_bits(&pb, 16, s->blocksize);
  90. put_bits(&pb, 16, s->blocksize);
  91. put_bits(&pb, 24, 0);
  92. put_bits(&pb, 24, s->max_framesize);
  93. put_bits(&pb, 20, s->samplerate);
  94. put_bits(&pb, 3, s->channels-1);
  95. put_bits(&pb, 5, 15); /* bits per sample - 1 */
  96. flush_put_bits(&pb);
  97. /* total samples = 0 */
  98. /* MD5 signature = 0 */
  99. }
  100. #define BLOCK_TIME_MS 27
  101. /**
  102. * Sets blocksize based on samplerate
  103. * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
  104. */
  105. static int select_blocksize(int samplerate)
  106. {
  107. int i;
  108. int target;
  109. int blocksize;
  110. assert(samplerate > 0);
  111. blocksize = flac_blocksizes[1];
  112. target = (samplerate * BLOCK_TIME_MS) / 1000;
  113. for(i=0; i<16; i++) {
  114. if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
  115. blocksize = flac_blocksizes[i];
  116. }
  117. }
  118. return blocksize;
  119. }
  120. static int flac_encode_init(AVCodecContext *avctx)
  121. {
  122. int freq = avctx->sample_rate;
  123. int channels = avctx->channels;
  124. FlacEncodeContext *s = avctx->priv_data;
  125. int i;
  126. uint8_t *streaminfo;
  127. s->avctx = avctx;
  128. if(avctx->sample_fmt != SAMPLE_FMT_S16) {
  129. return -1;
  130. }
  131. if(channels < 1 || channels > FLAC_MAX_CH) {
  132. return -1;
  133. }
  134. s->channels = channels;
  135. s->ch_code = s->channels-1;
  136. /* find samplerate in table */
  137. if(freq < 1)
  138. return -1;
  139. for(i=4; i<12; i++) {
  140. if(freq == flac_samplerates[i]) {
  141. s->samplerate = flac_samplerates[i];
  142. s->sr_code[0] = i;
  143. s->sr_code[1] = 0;
  144. break;
  145. }
  146. }
  147. /* if not in table, samplerate is non-standard */
  148. if(i == 12) {
  149. if(freq % 1000 == 0 && freq < 255000) {
  150. s->sr_code[0] = 12;
  151. s->sr_code[1] = freq / 1000;
  152. } else if(freq % 10 == 0 && freq < 655350) {
  153. s->sr_code[0] = 14;
  154. s->sr_code[1] = freq / 10;
  155. } else if(freq < 65535) {
  156. s->sr_code[0] = 13;
  157. s->sr_code[1] = freq;
  158. } else {
  159. return -1;
  160. }
  161. s->samplerate = freq;
  162. }
  163. s->blocksize = select_blocksize(s->samplerate);
  164. avctx->frame_size = s->blocksize;
  165. /* set maximum encoded frame size in verbatim mode */
  166. if(s->channels == 2) {
  167. s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3);
  168. } else {
  169. s->max_framesize = 14 + (s->blocksize * s->channels * 2);
  170. }
  171. streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
  172. write_streaminfo(s, streaminfo);
  173. avctx->extradata = streaminfo;
  174. avctx->extradata_size = FLAC_STREAMINFO_SIZE;
  175. s->frame_count = 0;
  176. avctx->coded_frame = avcodec_alloc_frame();
  177. avctx->coded_frame->key_frame = 1;
  178. return 0;
  179. }
  180. static void init_frame(FlacEncodeContext *s)
  181. {
  182. int i, ch;
  183. FlacFrame *frame;
  184. frame = &s->frame;
  185. for(i=0; i<16; i++) {
  186. if(s->blocksize == flac_blocksizes[i]) {
  187. frame->blocksize = flac_blocksizes[i];
  188. frame->bs_code[0] = i;
  189. frame->bs_code[1] = 0;
  190. break;
  191. }
  192. }
  193. if(i == 16) {
  194. frame->blocksize = s->blocksize;
  195. if(frame->blocksize <= 256) {
  196. frame->bs_code[0] = 6;
  197. frame->bs_code[1] = frame->blocksize-1;
  198. } else {
  199. frame->bs_code[0] = 7;
  200. frame->bs_code[1] = frame->blocksize-1;
  201. }
  202. }
  203. for(ch=0; ch<s->channels; ch++) {
  204. frame->subframes[ch].obits = 16;
  205. }
  206. }
  207. /**
  208. * Copy channel-interleaved input samples into separate subframes
  209. */
  210. static void copy_samples(FlacEncodeContext *s, int16_t *samples)
  211. {
  212. int i, j, ch;
  213. FlacFrame *frame;
  214. frame = &s->frame;
  215. for(i=0,j=0; i<frame->blocksize; i++) {
  216. for(ch=0; ch<s->channels; ch++,j++) {
  217. frame->subframes[ch].samples[i] = samples[j];
  218. }
  219. }
  220. }
  221. #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
  222. static int find_optimal_param(uint32_t sum, int n)
  223. {
  224. int k, k_opt;
  225. uint32_t nbits, nbits_opt;
  226. k_opt = 0;
  227. nbits_opt = rice_encode_count(sum, n, 0);
  228. for(k=1; k<=14; k++) {
  229. nbits = rice_encode_count(sum, n, k);
  230. if(nbits < nbits_opt) {
  231. nbits_opt = nbits;
  232. k_opt = k;
  233. }
  234. }
  235. return k_opt;
  236. }
  237. static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
  238. uint32_t *sums, int n, int pred_order)
  239. {
  240. int i;
  241. int k, cnt, part;
  242. uint32_t all_bits;
  243. part = (1 << porder);
  244. all_bits = 0;
  245. cnt = (n >> porder) - pred_order;
  246. for(i=0; i<part; i++) {
  247. if(i == 1) cnt = (n >> porder);
  248. k = find_optimal_param(sums[i], cnt);
  249. rc->params[i] = k;
  250. all_bits += rice_encode_count(sums[i], cnt, k);
  251. }
  252. all_bits += (4 * part);
  253. rc->porder = porder;
  254. return all_bits;
  255. }
  256. static void calc_sums(int pmax, uint32_t *data, int n, int pred_order,
  257. uint32_t sums[][256])
  258. {
  259. int i, j;
  260. int parts, cnt;
  261. uint32_t *res;
  262. /* sums for highest level */
  263. parts = (1 << pmax);
  264. res = &data[pred_order];
  265. cnt = (n >> pmax) - pred_order;
  266. for(i=0; i<parts; i++) {
  267. if(i == 1) cnt = (n >> pmax);
  268. if(i > 0) res = &data[i*cnt];
  269. sums[pmax][i] = 0;
  270. for(j=0; j<cnt; j++) {
  271. sums[pmax][i] += res[j];
  272. }
  273. }
  274. /* sums for lower levels */
  275. for(i=pmax-1; i>=0; i--) {
  276. parts = (1 << i);
  277. for(j=0; j<parts; j++) {
  278. sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
  279. }
  280. }
  281. }
  282. static uint32_t calc_rice_params(RiceContext *rc, int pmax, int32_t *data,
  283. int n, int pred_order)
  284. {
  285. int i;
  286. uint32_t bits, opt_bits;
  287. int opt_porder;
  288. RiceContext opt_rc;
  289. uint32_t *udata;
  290. uint32_t sums[9][256];
  291. assert(pmax >= 0 && pmax <= 8);
  292. udata = av_malloc(n * sizeof(uint32_t));
  293. for(i=0; i<n; i++) {
  294. udata[i] = (2*data[i]) ^ (data[i]>>31);
  295. }
  296. calc_sums(pmax, udata, n, pred_order, sums);
  297. opt_porder = 0;
  298. opt_bits = UINT32_MAX;
  299. for(i=0; i<=pmax; i++) {
  300. bits = calc_optimal_rice_params(rc, i, sums[i], n, pred_order);
  301. if(bits < opt_bits) {
  302. opt_bits = bits;
  303. opt_porder = i;
  304. memcpy(&opt_rc, rc, sizeof(RiceContext));
  305. }
  306. }
  307. if(opt_porder != pmax) {
  308. memcpy(rc, &opt_rc, sizeof(RiceContext));
  309. }
  310. av_freep(&udata);
  311. return opt_bits;
  312. }
  313. static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmax, int32_t *data,
  314. int n, int pred_order, int bps)
  315. {
  316. uint32_t bits;
  317. bits = pred_order*bps + 6;
  318. bits += calc_rice_params(rc, pmax, data, n, pred_order);
  319. return bits;
  320. }
  321. static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
  322. {
  323. assert(n > 0);
  324. memcpy(res, smp, n * sizeof(int32_t));
  325. }
  326. static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
  327. {
  328. int i;
  329. for(i=0; i<order; i++) {
  330. res[i] = smp[i];
  331. }
  332. if(order==0){
  333. for(i=order; i<n; i++)
  334. res[i]= smp[i];
  335. }else if(order==1){
  336. for(i=order; i<n; i++)
  337. res[i]= smp[i] - smp[i-1];
  338. }else if(order==2){
  339. for(i=order; i<n; i++)
  340. res[i]= smp[i] - 2*smp[i-1] + smp[i-2];
  341. }else if(order==3){
  342. for(i=order; i<n; i++)
  343. res[i]= smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3];
  344. }else{
  345. for(i=order; i<n; i++)
  346. res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4];
  347. }
  348. }
  349. static int get_max_p_order(int max_porder, int n, int order)
  350. {
  351. int porder, max_parts;
  352. porder = max_porder;
  353. while(porder > 0) {
  354. max_parts = (1 << porder);
  355. if(!(n % max_parts) && (n > max_parts*order)) {
  356. break;
  357. }
  358. porder--;
  359. }
  360. return porder;
  361. }
  362. static int encode_residual(FlacEncodeContext *ctx, int ch)
  363. {
  364. int i, opt_order, porder, max_porder, n;
  365. FlacFrame *frame;
  366. FlacSubframe *sub;
  367. uint32_t bits[5];
  368. int32_t *res, *smp;
  369. frame = &ctx->frame;
  370. sub = &frame->subframes[ch];
  371. res = sub->residual;
  372. smp = sub->samples;
  373. n = frame->blocksize;
  374. /* CONSTANT */
  375. for(i=1; i<n; i++) {
  376. if(smp[i] != smp[0]) break;
  377. }
  378. if(i == n) {
  379. sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
  380. res[0] = smp[0];
  381. return sub->obits;
  382. }
  383. /* VERBATIM */
  384. if(n < 5) {
  385. sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
  386. encode_residual_verbatim(res, smp, n);
  387. return sub->obits * n;
  388. }
  389. max_porder = 3;
  390. /* FIXED */
  391. opt_order = 0;
  392. bits[0] = UINT32_MAX;
  393. for(i=0; i<=4; i++) {
  394. encode_residual_fixed(res, smp, n, i);
  395. porder = get_max_p_order(max_porder, n, i);
  396. bits[i] = calc_rice_params_fixed(&sub->rc, porder, res, n, i, sub->obits);
  397. if(bits[i] < bits[opt_order]) {
  398. opt_order = i;
  399. }
  400. }
  401. sub->order = opt_order;
  402. sub->type = FLAC_SUBFRAME_FIXED;
  403. sub->type_code = sub->type | sub->order;
  404. if(sub->order != 4) {
  405. encode_residual_fixed(res, smp, n, sub->order);
  406. porder = get_max_p_order(max_porder, n, sub->order);
  407. calc_rice_params_fixed(&sub->rc, porder, res, n, sub->order, sub->obits);
  408. }
  409. return bits[sub->order];
  410. }
  411. static int encode_residual_v(FlacEncodeContext *ctx, int ch)
  412. {
  413. int i, n;
  414. FlacFrame *frame;
  415. FlacSubframe *sub;
  416. int32_t *res, *smp;
  417. frame = &ctx->frame;
  418. sub = &frame->subframes[ch];
  419. res = sub->residual;
  420. smp = sub->samples;
  421. n = frame->blocksize;
  422. /* CONSTANT */
  423. for(i=1; i<n; i++) {
  424. if(smp[i] != smp[0]) break;
  425. }
  426. if(i == n) {
  427. sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
  428. res[0] = smp[0];
  429. return sub->obits;
  430. }
  431. /* VERBATIM */
  432. sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
  433. encode_residual_verbatim(res, smp, n);
  434. return sub->obits * n;
  435. }
  436. static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
  437. {
  438. int i, best;
  439. int32_t lt, rt;
  440. uint64_t sum[4];
  441. uint64_t score[4];
  442. int k;
  443. /* calculate sum of squares for each channel */
  444. sum[0] = sum[1] = sum[2] = sum[3] = 0;
  445. for(i=2; i<n; i++) {
  446. lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
  447. rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
  448. sum[2] += ABS((lt + rt) >> 1);
  449. sum[3] += ABS(lt - rt);
  450. sum[0] += ABS(lt);
  451. sum[1] += ABS(rt);
  452. }
  453. for(i=0; i<4; i++) {
  454. k = find_optimal_param(2*sum[i], n);
  455. sum[i] = rice_encode_count(2*sum[i], n, k);
  456. }
  457. /* calculate score for each mode */
  458. score[0] = sum[0] + sum[1];
  459. score[1] = sum[0] + sum[3];
  460. score[2] = sum[1] + sum[3];
  461. score[3] = sum[2] + sum[3];
  462. /* return mode with lowest score */
  463. best = 0;
  464. for(i=1; i<4; i++) {
  465. if(score[i] < score[best]) {
  466. best = i;
  467. }
  468. }
  469. if(best == 0) {
  470. return FLAC_CHMODE_LEFT_RIGHT;
  471. } else if(best == 1) {
  472. return FLAC_CHMODE_LEFT_SIDE;
  473. } else if(best == 2) {
  474. return FLAC_CHMODE_RIGHT_SIDE;
  475. } else {
  476. return FLAC_CHMODE_MID_SIDE;
  477. }
  478. }
  479. /**
  480. * Perform stereo channel decorrelation
  481. */
  482. static void channel_decorrelation(FlacEncodeContext *ctx)
  483. {
  484. FlacFrame *frame;
  485. int32_t *left, *right;
  486. int i, n;
  487. frame = &ctx->frame;
  488. n = frame->blocksize;
  489. left = frame->subframes[0].samples;
  490. right = frame->subframes[1].samples;
  491. if(ctx->channels != 2) {
  492. frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
  493. return;
  494. }
  495. frame->ch_mode = estimate_stereo_mode(left, right, n);
  496. /* perform decorrelation and adjust bits-per-sample */
  497. if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
  498. return;
  499. }
  500. if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
  501. int32_t tmp;
  502. for(i=0; i<n; i++) {
  503. tmp = left[i];
  504. left[i] = (tmp + right[i]) >> 1;
  505. right[i] = tmp - right[i];
  506. }
  507. frame->subframes[1].obits++;
  508. } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
  509. for(i=0; i<n; i++) {
  510. right[i] = left[i] - right[i];
  511. }
  512. frame->subframes[1].obits++;
  513. } else {
  514. for(i=0; i<n; i++) {
  515. left[i] -= right[i];
  516. }
  517. frame->subframes[0].obits++;
  518. }
  519. }
  520. static void put_sbits(PutBitContext *pb, int bits, int32_t val)
  521. {
  522. assert(bits >= 0 && bits <= 31);
  523. put_bits(pb, bits, val & ((1<<bits)-1));
  524. }
  525. static void write_utf8(PutBitContext *pb, uint32_t val)
  526. {
  527. int bytes, shift;
  528. if(val < 0x80){
  529. put_bits(pb, 8, val);
  530. return;
  531. }
  532. bytes= (av_log2(val)+4) / 5;
  533. shift = (bytes - 1) * 6;
  534. put_bits(pb, 8, (256 - (256>>bytes)) | (val >> shift));
  535. while(shift >= 6){
  536. shift -= 6;
  537. put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
  538. }
  539. }
  540. static void output_frame_header(FlacEncodeContext *s)
  541. {
  542. FlacFrame *frame;
  543. int crc;
  544. frame = &s->frame;
  545. put_bits(&s->pb, 16, 0xFFF8);
  546. put_bits(&s->pb, 4, frame->bs_code[0]);
  547. put_bits(&s->pb, 4, s->sr_code[0]);
  548. if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
  549. put_bits(&s->pb, 4, s->ch_code);
  550. } else {
  551. put_bits(&s->pb, 4, frame->ch_mode);
  552. }
  553. put_bits(&s->pb, 3, 4); /* bits-per-sample code */
  554. put_bits(&s->pb, 1, 0);
  555. write_utf8(&s->pb, s->frame_count);
  556. if(frame->bs_code[0] == 6) {
  557. put_bits(&s->pb, 8, frame->bs_code[1]);
  558. } else if(frame->bs_code[0] == 7) {
  559. put_bits(&s->pb, 16, frame->bs_code[1]);
  560. }
  561. if(s->sr_code[0] == 12) {
  562. put_bits(&s->pb, 8, s->sr_code[1]);
  563. } else if(s->sr_code[0] > 12) {
  564. put_bits(&s->pb, 16, s->sr_code[1]);
  565. }
  566. flush_put_bits(&s->pb);
  567. crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
  568. put_bits(&s->pb, 8, crc);
  569. }
  570. static void output_subframe_constant(FlacEncodeContext *s, int ch)
  571. {
  572. FlacSubframe *sub;
  573. int32_t res;
  574. sub = &s->frame.subframes[ch];
  575. res = sub->residual[0];
  576. put_sbits(&s->pb, sub->obits, res);
  577. }
  578. static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
  579. {
  580. int i;
  581. FlacFrame *frame;
  582. FlacSubframe *sub;
  583. int32_t res;
  584. frame = &s->frame;
  585. sub = &frame->subframes[ch];
  586. for(i=0; i<frame->blocksize; i++) {
  587. res = sub->residual[i];
  588. put_sbits(&s->pb, sub->obits, res);
  589. }
  590. }
  591. static void output_residual(FlacEncodeContext *ctx, int ch)
  592. {
  593. int i, j, p, n, parts;
  594. int k, porder, psize, res_cnt;
  595. FlacFrame *frame;
  596. FlacSubframe *sub;
  597. int32_t *res;
  598. frame = &ctx->frame;
  599. sub = &frame->subframes[ch];
  600. res = sub->residual;
  601. n = frame->blocksize;
  602. /* rice-encoded block */
  603. put_bits(&ctx->pb, 2, 0);
  604. /* partition order */
  605. porder = sub->rc.porder;
  606. psize = n >> porder;
  607. parts = (1 << porder);
  608. put_bits(&ctx->pb, 4, porder);
  609. res_cnt = psize - sub->order;
  610. /* residual */
  611. j = sub->order;
  612. for(p=0; p<parts; p++) {
  613. k = sub->rc.params[p];
  614. put_bits(&ctx->pb, 4, k);
  615. if(p == 1) res_cnt = psize;
  616. for(i=0; i<res_cnt && j<n; i++, j++) {
  617. set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
  618. }
  619. }
  620. }
  621. static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
  622. {
  623. int i;
  624. FlacFrame *frame;
  625. FlacSubframe *sub;
  626. frame = &ctx->frame;
  627. sub = &frame->subframes[ch];
  628. /* warm-up samples */
  629. for(i=0; i<sub->order; i++) {
  630. put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
  631. }
  632. /* residual */
  633. output_residual(ctx, ch);
  634. }
  635. static void output_subframes(FlacEncodeContext *s)
  636. {
  637. FlacFrame *frame;
  638. FlacSubframe *sub;
  639. int ch;
  640. frame = &s->frame;
  641. for(ch=0; ch<s->channels; ch++) {
  642. sub = &frame->subframes[ch];
  643. /* subframe header */
  644. put_bits(&s->pb, 1, 0);
  645. put_bits(&s->pb, 6, sub->type_code);
  646. put_bits(&s->pb, 1, 0); /* no wasted bits */
  647. /* subframe */
  648. if(sub->type == FLAC_SUBFRAME_CONSTANT) {
  649. output_subframe_constant(s, ch);
  650. } else if(sub->type == FLAC_SUBFRAME_VERBATIM) {
  651. output_subframe_verbatim(s, ch);
  652. } else if(sub->type == FLAC_SUBFRAME_FIXED) {
  653. output_subframe_fixed(s, ch);
  654. }
  655. }
  656. }
  657. static void output_frame_footer(FlacEncodeContext *s)
  658. {
  659. int crc;
  660. flush_put_bits(&s->pb);
  661. crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
  662. put_bits(&s->pb, 16, crc);
  663. flush_put_bits(&s->pb);
  664. }
  665. static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
  666. int buf_size, void *data)
  667. {
  668. int ch;
  669. FlacEncodeContext *s;
  670. int16_t *samples = data;
  671. int out_bytes;
  672. s = avctx->priv_data;
  673. s->blocksize = avctx->frame_size;
  674. init_frame(s);
  675. copy_samples(s, samples);
  676. channel_decorrelation(s);
  677. for(ch=0; ch<s->channels; ch++) {
  678. encode_residual(s, ch);
  679. }
  680. init_put_bits(&s->pb, frame, buf_size);
  681. output_frame_header(s);
  682. output_subframes(s);
  683. output_frame_footer(s);
  684. out_bytes = put_bits_count(&s->pb) >> 3;
  685. if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
  686. /* frame too large. use verbatim mode */
  687. for(ch=0; ch<s->channels; ch++) {
  688. encode_residual_v(s, ch);
  689. }
  690. init_put_bits(&s->pb, frame, buf_size);
  691. output_frame_header(s);
  692. output_subframes(s);
  693. output_frame_footer(s);
  694. out_bytes = put_bits_count(&s->pb) >> 3;
  695. if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
  696. /* still too large. must be an error. */
  697. av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
  698. return -1;
  699. }
  700. }
  701. s->frame_count++;
  702. return out_bytes;
  703. }
  704. static int flac_encode_close(AVCodecContext *avctx)
  705. {
  706. av_freep(&avctx->extradata);
  707. avctx->extradata_size = 0;
  708. av_freep(&avctx->coded_frame);
  709. return 0;
  710. }
  711. AVCodec flac_encoder = {
  712. "flac",
  713. CODEC_TYPE_AUDIO,
  714. CODEC_ID_FLAC,
  715. sizeof(FlacEncodeContext),
  716. flac_encode_init,
  717. flac_encode_frame,
  718. flac_encode_close,
  719. NULL,
  720. .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
  721. };