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
85 static const int flac_blocksizes_ordered[14] = {
86 0, 192, 256, 512, 576, 1024, 1152, 2048, 2304, 4096, 4608, 8192, 16384, 32768
90 * Writes streaminfo metadata block to byte array
92 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
96 memset(header, 0, FLAC_STREAMINFO_SIZE);
97 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
99 /* streaminfo metadata block */
100 put_bits(&pb, 16, s->blocksize);
101 put_bits(&pb, 16, s->blocksize);
102 put_bits(&pb, 24, 0);
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 */
108 /* total samples = 0 */
109 /* MD5 signature = 0 */
112 #define BLOCK_TIME_MS 105
115 * Sets blocksize based on samplerate
116 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
118 static int select_blocksize(int samplerate)
124 assert(samplerate > 0);
126 target = (samplerate * BLOCK_TIME_MS) / 1000;
127 for(i=13; i>=0; i--) {
128 if(target >= flac_blocksizes_ordered[i]) {
129 blocksize = flac_blocksizes_ordered[i];
134 blocksize = flac_blocksizes_ordered[1];
139 static int flac_encode_init(AVCodecContext *avctx)
141 int freq = avctx->sample_rate;
142 int channels = avctx->channels;
143 FlacEncodeContext *s = avctx->priv_data;
151 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
155 if(channels < 1 || channels > FLAC_MAX_CH) {
158 s->channels = channels;
159 s->ch_code = s->channels-1;
161 /* find samplerate in table */
164 for(i=4; i<12; i++) {
165 if(freq == flac_samplerates[i]) {
166 s->samplerate = flac_samplerates[i];
172 /* if not in table, samplerate is non-standard */
174 if(freq % 1000 == 0 && freq < 255000) {
176 s->sr_code[1] = freq / 1000;
177 } else if(freq % 10 == 0 && freq < 655350) {
179 s->sr_code[1] = freq / 10;
180 } else if(freq < 65535) {
182 s->sr_code[1] = freq;
186 s->samplerate = freq;
189 s->blocksize = select_blocksize(s->samplerate);
190 avctx->frame_size = s->blocksize;
192 s->max_framesize = 14 + (s->blocksize * s->channels * 2);
194 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
195 write_streaminfo(s, streaminfo);
196 avctx->extradata = streaminfo;
197 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
201 avctx->coded_frame = avcodec_alloc_frame();
202 avctx->coded_frame->key_frame = 1;
207 static int init_frame(FlacEncodeContext *s)
214 for(i=0; i<16; i++) {
215 if(s->blocksize == flac_blocksizes[i]) {
216 frame->blocksize = flac_blocksizes[i];
217 frame->bs_code[0] = i;
218 frame->bs_code[1] = 0;
223 frame->blocksize = s->blocksize;
224 if(frame->blocksize <= 256) {
225 frame->bs_code[0] = 6;
226 frame->bs_code[1] = frame->blocksize-1;
228 frame->bs_code[0] = 7;
229 frame->bs_code[1] = frame->blocksize-1;
233 for(ch=0; ch<s->channels; ch++) {
234 frame->subframes[ch].obits = 16;
236 if(s->channels == 2) {
237 frame->ch_mode = FLAC_CHMODE_LEFT_RIGHT;
239 frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
246 * Copy channel-interleaved input samples into separate subframes
248 static void copy_samples(FlacEncodeContext *s, int16_t *samples)
254 for(i=0,j=0; i<frame->blocksize; i++) {
255 for(ch=0; ch<s->channels; ch++,j++) {
256 frame->subframes[ch].samples[i] = samples[j];
261 static void encode_residual_verbatim(FlacEncodeContext *s, int ch)
270 sub = &frame->subframes[ch];
273 n = frame->blocksize;
276 sub->type = FLAC_SUBFRAME_VERBATIM;
277 sub->type_code = sub->type;
279 memcpy(res, smp, n * sizeof(int32_t));
282 static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
287 for(i=0; i<order; i++) {
290 for(i=order; i<n; i++) {
295 case 1: pred = smp[i-1];
297 case 2: pred = 2*smp[i-1] - smp[i-2];
299 case 3: pred = 3*smp[i-1] - 3*smp[i-2] + smp[i-3];
301 case 4: pred = 4*smp[i-1] - 6*smp[i-2] + 4*smp[i-3] - smp[i-4];
304 res[i] = smp[i] - pred;
308 static void encode_residual(FlacEncodeContext *s, int ch)
317 sub = &frame->subframes[ch];
320 n = frame->blocksize;
323 sub->type = FLAC_SUBFRAME_FIXED;
324 sub->type_code = sub->type | sub->order;
325 encode_residual_fixed(res, smp, n, sub->order);
329 put_sbits(PutBitContext *pb, int bits, int32_t val)
333 assert(bits >= 0 && bits <= 31);
334 uval = (val < 0) ? (1UL << bits) + val : val;
335 put_bits(pb, bits, uval);
339 write_utf8(PutBitContext *pb, uint32_t val)
341 int i, bytes, mask, shift;
344 if(val >= 0x80) bytes++;
345 if(val >= 0x800) bytes++;
346 if(val >= 0x10000) bytes++;
347 if(val >= 0x200000) bytes++;
348 if(val >= 0x4000000) bytes++;
351 put_bits(pb, 8, val);
355 shift = (bytes - 1) * 6;
356 mask = 0x80 + ((1 << 7) - (1 << (8 - bytes)));
357 put_bits(pb, 8, mask | (val >> shift));
358 for(i=0; i<bytes-1; i++) {
360 put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
365 output_frame_header(FlacEncodeContext *s)
372 put_bits(&s->pb, 16, 0xFFF8);
373 put_bits(&s->pb, 4, frame->bs_code[0]);
374 put_bits(&s->pb, 4, s->sr_code[0]);
375 if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
376 put_bits(&s->pb, 4, s->ch_code);
378 put_bits(&s->pb, 4, frame->ch_mode);
380 put_bits(&s->pb, 3, 4); /* bits-per-sample code */
381 put_bits(&s->pb, 1, 0);
382 write_utf8(&s->pb, s->frame_count);
383 if(frame->bs_code[1] > 0) {
384 if(frame->bs_code[1] < 256) {
385 put_bits(&s->pb, 8, frame->bs_code[1]);
387 put_bits(&s->pb, 16, frame->bs_code[1]);
390 if(s->sr_code[1] > 0) {
391 if(s->sr_code[1] < 256) {
392 put_bits(&s->pb, 8, s->sr_code[1]);
394 put_bits(&s->pb, 16, s->sr_code[1]);
397 flush_put_bits(&s->pb);
398 crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
399 put_bits(&s->pb, 8, crc);
402 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
410 sub = &frame->subframes[ch];
412 for(i=0; i<frame->blocksize; i++) {
413 res = sub->residual[i];
414 put_sbits(&s->pb, sub->obits, res);
419 output_residual(FlacEncodeContext *ctx, int ch)
422 int k, porder, psize, res_cnt;
427 sub = &frame->subframes[ch];
429 /* rice-encoded block */
430 put_bits(&ctx->pb, 2, 0);
432 /* partition order */
434 psize = frame->blocksize;
435 //porder = sub->rc.porder;
436 //psize = frame->blocksize >> porder;
437 put_bits(&ctx->pb, 4, porder);
438 res_cnt = psize - sub->order;
442 for(p=0; p<(1 << porder); p++) {
443 //k = sub->rc.params[p];
445 put_bits(&ctx->pb, 4, k);
446 if(p == 1) res_cnt = psize;
447 for(i=0; i<res_cnt && j<frame->blocksize; i++, j++) {
448 set_sr_golomb_flac(&ctx->pb, sub->residual[j], k, INT32_MAX, 0);
454 output_subframe_fixed(FlacEncodeContext *ctx, int ch)
461 sub = &frame->subframes[ch];
463 /* warm-up samples */
464 for(i=0; i<sub->order; i++) {
465 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
469 output_residual(ctx, ch);
472 static void output_subframes(FlacEncodeContext *s)
480 for(ch=0; ch<s->channels; ch++) {
481 sub = &frame->subframes[ch];
483 /* subframe header */
484 put_bits(&s->pb, 1, 0);
485 put_bits(&s->pb, 6, sub->type_code);
486 put_bits(&s->pb, 1, 0); /* no wasted bits */
489 if(sub->type == FLAC_SUBFRAME_VERBATIM) {
490 output_subframe_verbatim(s, ch);
492 output_subframe_fixed(s, ch);
497 static void output_frame_footer(FlacEncodeContext *s)
500 flush_put_bits(&s->pb);
501 crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
502 put_bits(&s->pb, 16, crc);
503 flush_put_bits(&s->pb);
506 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
507 int buf_size, void *data)
510 FlacEncodeContext *s;
511 int16_t *samples = data;
514 s = avctx->priv_data;
516 s->blocksize = avctx->frame_size;
521 copy_samples(s, samples);
523 for(ch=0; ch<s->channels; ch++) {
524 encode_residual(s, ch);
526 init_put_bits(&s->pb, frame, buf_size);
527 output_frame_header(s);
529 output_frame_footer(s);
530 out_bytes = put_bits_count(&s->pb) >> 3;
532 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
533 /* frame too large. use verbatim mode */
534 for(ch=0; ch<s->channels; ch++) {
535 encode_residual_verbatim(s, ch);
537 init_put_bits(&s->pb, frame, buf_size);
538 output_frame_header(s);
540 output_frame_footer(s);
541 out_bytes = put_bits_count(&s->pb) >> 3;
543 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
544 /* still too large. must be an error. */
545 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
554 static int flac_encode_close(AVCodecContext *avctx)
556 av_freep(&avctx->coded_frame);
560 AVCodec flac_encoder = {
564 sizeof(FlacEncodeContext),
569 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,