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.

550 lines
13KB

  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 FlacSubframe {
  37. int type;
  38. int type_code;
  39. int obits;
  40. int order;
  41. int32_t samples[FLAC_MAX_BLOCKSIZE];
  42. int32_t residual[FLAC_MAX_BLOCKSIZE];
  43. } FlacSubframe;
  44. typedef struct FlacFrame {
  45. FlacSubframe subframes[FLAC_MAX_CH];
  46. int blocksize;
  47. int bs_code[2];
  48. uint8_t crc8;
  49. int ch_mode;
  50. } FlacFrame;
  51. typedef struct FlacEncodeContext {
  52. PutBitContext pb;
  53. int channels;
  54. int ch_code;
  55. int samplerate;
  56. int sr_code[2];
  57. int blocksize;
  58. int max_framesize;
  59. uint32_t frame_count;
  60. FlacFrame frame;
  61. } FlacEncodeContext;
  62. static const int flac_samplerates[16] = {
  63. 0, 0, 0, 0,
  64. 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
  65. 0, 0, 0, 0
  66. };
  67. static const int flac_blocksizes[16] = {
  68. 0,
  69. 192,
  70. 576, 1152, 2304, 4608,
  71. 0, 0,
  72. 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
  73. };
  74. /**
  75. * Writes streaminfo metadata block to byte array
  76. */
  77. static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
  78. {
  79. PutBitContext pb;
  80. memset(header, 0, FLAC_STREAMINFO_SIZE);
  81. init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
  82. /* streaminfo metadata block */
  83. put_bits(&pb, 16, s->blocksize);
  84. put_bits(&pb, 16, s->blocksize);
  85. put_bits(&pb, 24, 0);
  86. put_bits(&pb, 24, s->max_framesize);
  87. put_bits(&pb, 20, s->samplerate);
  88. put_bits(&pb, 3, s->channels-1);
  89. put_bits(&pb, 5, 15); /* bits per sample - 1 */
  90. flush_put_bits(&pb);
  91. /* total samples = 0 */
  92. /* MD5 signature = 0 */
  93. }
  94. #define BLOCK_TIME_MS 105
  95. /**
  96. * Sets blocksize based on samplerate
  97. * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
  98. */
  99. static int select_blocksize(int samplerate)
  100. {
  101. int i;
  102. int target;
  103. int blocksize;
  104. assert(samplerate > 0);
  105. blocksize = flac_blocksizes[1];
  106. target = (samplerate * BLOCK_TIME_MS) / 1000;
  107. for(i=0; i<16; i++) {
  108. if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
  109. blocksize = flac_blocksizes[i];
  110. }
  111. }
  112. return blocksize;
  113. }
  114. static int flac_encode_init(AVCodecContext *avctx)
  115. {
  116. int freq = avctx->sample_rate;
  117. int channels = avctx->channels;
  118. FlacEncodeContext *s = avctx->priv_data;
  119. int i;
  120. uint8_t *streaminfo;
  121. if(avctx->sample_fmt != SAMPLE_FMT_S16) {
  122. return -1;
  123. }
  124. if(channels < 1 || channels > FLAC_MAX_CH) {
  125. return -1;
  126. }
  127. s->channels = channels;
  128. s->ch_code = s->channels-1;
  129. /* find samplerate in table */
  130. if(freq < 1)
  131. return -1;
  132. for(i=4; i<12; i++) {
  133. if(freq == flac_samplerates[i]) {
  134. s->samplerate = flac_samplerates[i];
  135. s->sr_code[0] = i;
  136. s->sr_code[1] = 0;
  137. break;
  138. }
  139. }
  140. /* if not in table, samplerate is non-standard */
  141. if(i == 12) {
  142. if(freq % 1000 == 0 && freq < 255000) {
  143. s->sr_code[0] = 12;
  144. s->sr_code[1] = freq / 1000;
  145. } else if(freq % 10 == 0 && freq < 655350) {
  146. s->sr_code[0] = 14;
  147. s->sr_code[1] = freq / 10;
  148. } else if(freq < 65535) {
  149. s->sr_code[0] = 13;
  150. s->sr_code[1] = freq;
  151. } else {
  152. return -1;
  153. }
  154. s->samplerate = freq;
  155. }
  156. s->blocksize = select_blocksize(s->samplerate);
  157. avctx->frame_size = s->blocksize;
  158. s->max_framesize = 14 + (s->blocksize * s->channels * 2);
  159. streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
  160. write_streaminfo(s, streaminfo);
  161. avctx->extradata = streaminfo;
  162. avctx->extradata_size = FLAC_STREAMINFO_SIZE;
  163. s->frame_count = 0;
  164. avctx->coded_frame = avcodec_alloc_frame();
  165. avctx->coded_frame->key_frame = 1;
  166. return 0;
  167. }
  168. static int init_frame(FlacEncodeContext *s)
  169. {
  170. int i, ch;
  171. FlacFrame *frame;
  172. frame = &s->frame;
  173. for(i=0; i<16; i++) {
  174. if(s->blocksize == flac_blocksizes[i]) {
  175. frame->blocksize = flac_blocksizes[i];
  176. frame->bs_code[0] = i;
  177. frame->bs_code[1] = 0;
  178. break;
  179. }
  180. }
  181. if(i == 16) {
  182. frame->blocksize = s->blocksize;
  183. if(frame->blocksize <= 256) {
  184. frame->bs_code[0] = 6;
  185. frame->bs_code[1] = frame->blocksize-1;
  186. } else {
  187. frame->bs_code[0] = 7;
  188. frame->bs_code[1] = frame->blocksize-1;
  189. }
  190. }
  191. for(ch=0; ch<s->channels; ch++) {
  192. frame->subframes[ch].obits = 16;
  193. }
  194. if(s->channels == 2) {
  195. frame->ch_mode = FLAC_CHMODE_LEFT_RIGHT;
  196. } else {
  197. frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
  198. }
  199. return 0;
  200. }
  201. /**
  202. * Copy channel-interleaved input samples into separate subframes
  203. */
  204. static void copy_samples(FlacEncodeContext *s, int16_t *samples)
  205. {
  206. int i, j, ch;
  207. FlacFrame *frame;
  208. frame = &s->frame;
  209. for(i=0,j=0; i<frame->blocksize; i++) {
  210. for(ch=0; ch<s->channels; ch++,j++) {
  211. frame->subframes[ch].samples[i] = samples[j];
  212. }
  213. }
  214. }
  215. static void encode_residual_verbatim(FlacEncodeContext *s, int ch)
  216. {
  217. FlacFrame *frame;
  218. FlacSubframe *sub;
  219. int32_t *res;
  220. int32_t *smp;
  221. int n;
  222. frame = &s->frame;
  223. sub = &frame->subframes[ch];
  224. res = sub->residual;
  225. smp = sub->samples;
  226. n = frame->blocksize;
  227. sub->order = 0;
  228. sub->type = FLAC_SUBFRAME_VERBATIM;
  229. sub->type_code = sub->type;
  230. memcpy(res, smp, n * sizeof(int32_t));
  231. }
  232. static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
  233. {
  234. int i;
  235. for(i=0; i<order; i++) {
  236. res[i] = smp[i];
  237. }
  238. if(order==0){
  239. for(i=order; i<n; i++)
  240. res[i]= smp[i];
  241. }else if(order==1){
  242. for(i=order; i<n; i++)
  243. res[i]= smp[i] - smp[i-1];
  244. }else if(order==2){
  245. for(i=order; i<n; i++)
  246. res[i]= smp[i] - 2*smp[i-1] + smp[i-2];
  247. }else if(order==3){
  248. for(i=order; i<n; i++)
  249. res[i]= smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3];
  250. }else{
  251. for(i=order; i<n; i++)
  252. res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4];
  253. }
  254. }
  255. static void encode_residual(FlacEncodeContext *s, int ch)
  256. {
  257. FlacFrame *frame;
  258. FlacSubframe *sub;
  259. int32_t *res;
  260. int32_t *smp;
  261. int n;
  262. frame = &s->frame;
  263. sub = &frame->subframes[ch];
  264. res = sub->residual;
  265. smp = sub->samples;
  266. n = frame->blocksize;
  267. sub->order = 2;
  268. sub->type = FLAC_SUBFRAME_FIXED;
  269. sub->type_code = sub->type | sub->order;
  270. encode_residual_fixed(res, smp, n, sub->order);
  271. }
  272. static void
  273. put_sbits(PutBitContext *pb, int bits, int32_t val)
  274. {
  275. assert(bits >= 0 && bits <= 31);
  276. put_bits(pb, bits, val & ((1<<bits)-1));
  277. }
  278. static void
  279. write_utf8(PutBitContext *pb, uint32_t val)
  280. {
  281. int bytes, shift;
  282. if(val < 0x80){
  283. put_bits(pb, 8, val);
  284. return;
  285. }
  286. bytes= (av_log2(val)-1) / 5;
  287. shift = (bytes - 1) * 6;
  288. put_bits(pb, 8, (256 - (256>>bytes)) | (val >> shift));
  289. while(shift >= 6){
  290. shift -= 6;
  291. put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
  292. }
  293. }
  294. static void
  295. output_frame_header(FlacEncodeContext *s)
  296. {
  297. FlacFrame *frame;
  298. int crc;
  299. frame = &s->frame;
  300. put_bits(&s->pb, 16, 0xFFF8);
  301. put_bits(&s->pb, 4, frame->bs_code[0]);
  302. put_bits(&s->pb, 4, s->sr_code[0]);
  303. if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
  304. put_bits(&s->pb, 4, s->ch_code);
  305. } else {
  306. put_bits(&s->pb, 4, frame->ch_mode);
  307. }
  308. put_bits(&s->pb, 3, 4); /* bits-per-sample code */
  309. put_bits(&s->pb, 1, 0);
  310. write_utf8(&s->pb, s->frame_count);
  311. if(frame->bs_code[1] > 0) {
  312. if(frame->bs_code[1] < 256) {
  313. put_bits(&s->pb, 8, frame->bs_code[1]);
  314. } else {
  315. put_bits(&s->pb, 16, frame->bs_code[1]);
  316. }
  317. }
  318. if(s->sr_code[1] > 0) {
  319. if(s->sr_code[1] < 256) {
  320. put_bits(&s->pb, 8, s->sr_code[1]);
  321. } else {
  322. put_bits(&s->pb, 16, s->sr_code[1]);
  323. }
  324. }
  325. flush_put_bits(&s->pb);
  326. crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
  327. put_bits(&s->pb, 8, crc);
  328. }
  329. static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
  330. {
  331. int i;
  332. FlacFrame *frame;
  333. FlacSubframe *sub;
  334. int32_t res;
  335. frame = &s->frame;
  336. sub = &frame->subframes[ch];
  337. for(i=0; i<frame->blocksize; i++) {
  338. res = sub->residual[i];
  339. put_sbits(&s->pb, sub->obits, res);
  340. }
  341. }
  342. static void
  343. output_residual(FlacEncodeContext *ctx, int ch)
  344. {
  345. int i, j, p;
  346. int k, porder, psize, res_cnt;
  347. FlacFrame *frame;
  348. FlacSubframe *sub;
  349. frame = &ctx->frame;
  350. sub = &frame->subframes[ch];
  351. /* rice-encoded block */
  352. put_bits(&ctx->pb, 2, 0);
  353. /* partition order */
  354. porder = 0;
  355. psize = frame->blocksize;
  356. //porder = sub->rc.porder;
  357. //psize = frame->blocksize >> porder;
  358. put_bits(&ctx->pb, 4, porder);
  359. res_cnt = psize - sub->order;
  360. /* residual */
  361. j = sub->order;
  362. for(p=0; p<(1 << porder); p++) {
  363. //k = sub->rc.params[p];
  364. k = 9;
  365. put_bits(&ctx->pb, 4, k);
  366. if(p == 1) res_cnt = psize;
  367. for(i=0; i<res_cnt && j<frame->blocksize; i++, j++) {
  368. set_sr_golomb_flac(&ctx->pb, sub->residual[j], k, INT32_MAX, 0);
  369. }
  370. }
  371. }
  372. static void
  373. output_subframe_fixed(FlacEncodeContext *ctx, int ch)
  374. {
  375. int i;
  376. FlacFrame *frame;
  377. FlacSubframe *sub;
  378. frame = &ctx->frame;
  379. sub = &frame->subframes[ch];
  380. /* warm-up samples */
  381. for(i=0; i<sub->order; i++) {
  382. put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
  383. }
  384. /* residual */
  385. output_residual(ctx, ch);
  386. }
  387. static void output_subframes(FlacEncodeContext *s)
  388. {
  389. FlacFrame *frame;
  390. FlacSubframe *sub;
  391. int ch;
  392. frame = &s->frame;
  393. for(ch=0; ch<s->channels; ch++) {
  394. sub = &frame->subframes[ch];
  395. /* subframe header */
  396. put_bits(&s->pb, 1, 0);
  397. put_bits(&s->pb, 6, sub->type_code);
  398. put_bits(&s->pb, 1, 0); /* no wasted bits */
  399. /* subframe */
  400. if(sub->type == FLAC_SUBFRAME_VERBATIM) {
  401. output_subframe_verbatim(s, ch);
  402. } else {
  403. output_subframe_fixed(s, ch);
  404. }
  405. }
  406. }
  407. static void output_frame_footer(FlacEncodeContext *s)
  408. {
  409. int crc;
  410. flush_put_bits(&s->pb);
  411. crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
  412. put_bits(&s->pb, 16, crc);
  413. flush_put_bits(&s->pb);
  414. }
  415. static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
  416. int buf_size, void *data)
  417. {
  418. int ch;
  419. FlacEncodeContext *s;
  420. int16_t *samples = data;
  421. int out_bytes;
  422. s = avctx->priv_data;
  423. s->blocksize = avctx->frame_size;
  424. if(init_frame(s)) {
  425. return 0;
  426. }
  427. copy_samples(s, samples);
  428. for(ch=0; ch<s->channels; ch++) {
  429. encode_residual(s, ch);
  430. }
  431. init_put_bits(&s->pb, frame, buf_size);
  432. output_frame_header(s);
  433. output_subframes(s);
  434. output_frame_footer(s);
  435. out_bytes = put_bits_count(&s->pb) >> 3;
  436. if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
  437. /* frame too large. use verbatim mode */
  438. for(ch=0; ch<s->channels; ch++) {
  439. encode_residual_verbatim(s, ch);
  440. }
  441. init_put_bits(&s->pb, frame, buf_size);
  442. output_frame_header(s);
  443. output_subframes(s);
  444. output_frame_footer(s);
  445. out_bytes = put_bits_count(&s->pb) >> 3;
  446. if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
  447. /* still too large. must be an error. */
  448. av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
  449. return -1;
  450. }
  451. }
  452. s->frame_count++;
  453. return out_bytes;
  454. }
  455. static int flac_encode_close(AVCodecContext *avctx)
  456. {
  457. av_freep(&avctx->coded_frame);
  458. return 0;
  459. }
  460. AVCodec flac_encoder = {
  461. "flac",
  462. CODEC_TYPE_AUDIO,
  463. CODEC_ID_FLAC,
  464. sizeof(FlacEncodeContext),
  465. flac_encode_init,
  466. flac_encode_frame,
  467. flac_encode_close,
  468. NULL,
  469. .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
  470. };