3 * Copyright (c) 2006 Justin Ruggles <jruggle@earthlink.net>
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.
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.
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
21 #include "bitstream.h"
26 #define FLAC_MIN_BLOCKSIZE 16
27 #define FLAC_MAX_BLOCKSIZE 65535
29 #define FLAC_SUBFRAME_CONSTANT 0
30 #define FLAC_SUBFRAME_VERBATIM 1
31 #define FLAC_SUBFRAME_FIXED 8
32 #define FLAC_SUBFRAME_LPC 32
34 #define FLAC_CHMODE_NOT_STEREO 0
35 #define FLAC_CHMODE_LEFT_RIGHT 1
36 #define FLAC_CHMODE_LEFT_SIDE 8
37 #define FLAC_CHMODE_RIGHT_SIDE 9
38 #define FLAC_CHMODE_MID_SIDE 10
40 #define FLAC_STREAMINFO_SIZE 34
42 typedef struct FlacSubframe {
47 int32_t samples[FLAC_MAX_BLOCKSIZE];
48 int32_t residual[FLAC_MAX_BLOCKSIZE];
51 typedef struct FlacFrame {
52 FlacSubframe subframes[FLAC_MAX_CH];
59 typedef struct FlacEncodeContext {
71 static const int flac_samplerates[16] = {
73 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
77 static const int flac_blocksizes[16] = {
80 576, 1152, 2304, 4608,
82 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
86 * Writes streaminfo metadata block to byte array
88 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
92 memset(header, 0, FLAC_STREAMINFO_SIZE);
93 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
95 /* streaminfo metadata block */
96 put_bits(&pb, 16, s->blocksize);
97 put_bits(&pb, 16, s->blocksize);
99 put_bits(&pb, 24, s->max_framesize);
100 put_bits(&pb, 20, s->samplerate);
101 put_bits(&pb, 3, s->channels-1);
102 put_bits(&pb, 5, 15); /* bits per sample - 1 */
104 /* total samples = 0 */
105 /* MD5 signature = 0 */
108 #define BLOCK_TIME_MS 105
111 * Sets blocksize based on samplerate
112 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
114 static int select_blocksize(int samplerate)
120 assert(samplerate > 0);
121 blocksize = flac_blocksizes[1];
122 target = (samplerate * BLOCK_TIME_MS) / 1000;
123 for(i=0; i<16; i++) {
124 if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
125 blocksize = flac_blocksizes[i];
131 static int flac_encode_init(AVCodecContext *avctx)
133 int freq = avctx->sample_rate;
134 int channels = avctx->channels;
135 FlacEncodeContext *s = avctx->priv_data;
139 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
143 if(channels < 1 || channels > FLAC_MAX_CH) {
146 s->channels = channels;
147 s->ch_code = s->channels-1;
149 /* find samplerate in table */
152 for(i=4; i<12; i++) {
153 if(freq == flac_samplerates[i]) {
154 s->samplerate = flac_samplerates[i];
160 /* if not in table, samplerate is non-standard */
162 if(freq % 1000 == 0 && freq < 255000) {
164 s->sr_code[1] = freq / 1000;
165 } else if(freq % 10 == 0 && freq < 655350) {
167 s->sr_code[1] = freq / 10;
168 } else if(freq < 65535) {
170 s->sr_code[1] = freq;
174 s->samplerate = freq;
177 s->blocksize = select_blocksize(s->samplerate);
178 avctx->frame_size = s->blocksize;
180 s->max_framesize = 14 + (s->blocksize * s->channels * 2);
182 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
183 write_streaminfo(s, streaminfo);
184 avctx->extradata = streaminfo;
185 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
189 avctx->coded_frame = avcodec_alloc_frame();
190 avctx->coded_frame->key_frame = 1;
195 static int init_frame(FlacEncodeContext *s)
202 for(i=0; i<16; i++) {
203 if(s->blocksize == flac_blocksizes[i]) {
204 frame->blocksize = flac_blocksizes[i];
205 frame->bs_code[0] = i;
206 frame->bs_code[1] = 0;
211 frame->blocksize = s->blocksize;
212 if(frame->blocksize <= 256) {
213 frame->bs_code[0] = 6;
214 frame->bs_code[1] = frame->blocksize-1;
216 frame->bs_code[0] = 7;
217 frame->bs_code[1] = frame->blocksize-1;
221 for(ch=0; ch<s->channels; ch++) {
222 frame->subframes[ch].obits = 16;
224 if(s->channels == 2) {
225 frame->ch_mode = FLAC_CHMODE_LEFT_RIGHT;
227 frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
234 * Copy channel-interleaved input samples into separate subframes
236 static void copy_samples(FlacEncodeContext *s, int16_t *samples)
242 for(i=0,j=0; i<frame->blocksize; i++) {
243 for(ch=0; ch<s->channels; ch++,j++) {
244 frame->subframes[ch].samples[i] = samples[j];
249 static void encode_residual_verbatim(FlacEncodeContext *s, int ch)
258 sub = &frame->subframes[ch];
261 n = frame->blocksize;
264 sub->type = FLAC_SUBFRAME_VERBATIM;
265 sub->type_code = sub->type;
267 memcpy(res, smp, n * sizeof(int32_t));
270 static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
274 for(i=0; i<order; i++) {
279 for(i=order; i<n; i++)
282 for(i=order; i<n; i++)
283 res[i]= smp[i] - smp[i-1];
285 for(i=order; i<n; i++)
286 res[i]= smp[i] - 2*smp[i-1] + smp[i-2];
288 for(i=order; i<n; i++)
289 res[i]= smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3];
291 for(i=order; i<n; i++)
292 res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4];
296 static void encode_residual(FlacEncodeContext *s, int ch)
305 sub = &frame->subframes[ch];
308 n = frame->blocksize;
311 sub->type = FLAC_SUBFRAME_FIXED;
312 sub->type_code = sub->type | sub->order;
313 encode_residual_fixed(res, smp, n, sub->order);
317 put_sbits(PutBitContext *pb, int bits, int32_t val)
319 assert(bits >= 0 && bits <= 31);
321 put_bits(pb, bits, val & ((1<<bits)-1));
325 write_utf8(PutBitContext *pb, uint32_t val)
330 put_bits(pb, 8, val);
334 bytes= (av_log2(val)+4) / 5;
335 shift = (bytes - 1) * 6;
336 put_bits(pb, 8, (256 - (256>>bytes)) | (val >> shift));
339 put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
344 output_frame_header(FlacEncodeContext *s)
351 put_bits(&s->pb, 16, 0xFFF8);
352 put_bits(&s->pb, 4, frame->bs_code[0]);
353 put_bits(&s->pb, 4, s->sr_code[0]);
354 if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
355 put_bits(&s->pb, 4, s->ch_code);
357 put_bits(&s->pb, 4, frame->ch_mode);
359 put_bits(&s->pb, 3, 4); /* bits-per-sample code */
360 put_bits(&s->pb, 1, 0);
361 write_utf8(&s->pb, s->frame_count);
362 if(frame->bs_code[1] > 0) {
363 if(frame->bs_code[1] < 256) {
364 put_bits(&s->pb, 8, frame->bs_code[1]);
366 put_bits(&s->pb, 16, frame->bs_code[1]);
369 if(s->sr_code[1] > 0) {
370 if(s->sr_code[1] < 256) {
371 put_bits(&s->pb, 8, s->sr_code[1]);
373 put_bits(&s->pb, 16, s->sr_code[1]);
376 flush_put_bits(&s->pb);
377 crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
378 put_bits(&s->pb, 8, crc);
381 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
389 sub = &frame->subframes[ch];
391 for(i=0; i<frame->blocksize; i++) {
392 res = sub->residual[i];
393 put_sbits(&s->pb, sub->obits, res);
398 output_residual(FlacEncodeContext *ctx, int ch)
401 int k, porder, psize, res_cnt;
406 sub = &frame->subframes[ch];
408 /* rice-encoded block */
409 put_bits(&ctx->pb, 2, 0);
411 /* partition order */
413 psize = frame->blocksize;
414 //porder = sub->rc.porder;
415 //psize = frame->blocksize >> porder;
416 put_bits(&ctx->pb, 4, porder);
417 res_cnt = psize - sub->order;
421 for(p=0; p<(1 << porder); p++) {
422 //k = sub->rc.params[p];
424 put_bits(&ctx->pb, 4, k);
425 if(p == 1) res_cnt = psize;
426 for(i=0; i<res_cnt && j<frame->blocksize; i++, j++) {
427 set_sr_golomb_flac(&ctx->pb, sub->residual[j], k, INT32_MAX, 0);
433 output_subframe_fixed(FlacEncodeContext *ctx, int ch)
440 sub = &frame->subframes[ch];
442 /* warm-up samples */
443 for(i=0; i<sub->order; i++) {
444 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
448 output_residual(ctx, ch);
451 static void output_subframes(FlacEncodeContext *s)
459 for(ch=0; ch<s->channels; ch++) {
460 sub = &frame->subframes[ch];
462 /* subframe header */
463 put_bits(&s->pb, 1, 0);
464 put_bits(&s->pb, 6, sub->type_code);
465 put_bits(&s->pb, 1, 0); /* no wasted bits */
468 if(sub->type == FLAC_SUBFRAME_VERBATIM) {
469 output_subframe_verbatim(s, ch);
471 output_subframe_fixed(s, ch);
476 static void output_frame_footer(FlacEncodeContext *s)
479 flush_put_bits(&s->pb);
480 crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
481 put_bits(&s->pb, 16, crc);
482 flush_put_bits(&s->pb);
485 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
486 int buf_size, void *data)
489 FlacEncodeContext *s;
490 int16_t *samples = data;
493 s = avctx->priv_data;
495 s->blocksize = avctx->frame_size;
500 copy_samples(s, samples);
502 for(ch=0; ch<s->channels; ch++) {
503 encode_residual(s, ch);
505 init_put_bits(&s->pb, frame, buf_size);
506 output_frame_header(s);
508 output_frame_footer(s);
509 out_bytes = put_bits_count(&s->pb) >> 3;
511 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
512 /* frame too large. use verbatim mode */
513 for(ch=0; ch<s->channels; ch++) {
514 encode_residual_verbatim(s, ch);
516 init_put_bits(&s->pb, frame, buf_size);
517 output_frame_header(s);
519 output_frame_footer(s);
520 out_bytes = put_bits_count(&s->pb) >> 3;
522 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
523 /* still too large. must be an error. */
524 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
533 static int flac_encode_close(AVCodecContext *avctx)
535 av_freep(&avctx->coded_frame);
539 AVCodec flac_encoder = {
543 sizeof(FlacEncodeContext),
548 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,