Browse code

first rudimentary version of (Justin Ruggles jruggle earthlink net) flac encoder

Originally committed as revision 5514 to svn://svn.ffmpeg.org/ffmpeg/trunk

Michael Niedermayer authored on 2006/06/24 19:20:15
Showing 5 changed files
... ...
@@ -68,6 +68,9 @@ endif
68 68
 ifeq ($(CONFIG_FLAC_DECODER),yes)
69 69
     OBJS+= flac.o
70 70
 endif
71
+ifeq ($(CONFIG_FLAC_ENCODER),yes)
72
+    OBJS+= flacenc.o
73
+endif
71 74
 ifeq ($(CONFIG_FLIC_DECODER),yes)
72 75
     OBJS+= flicvideo.o
73 76
 endif
... ...
@@ -72,6 +72,9 @@ void avcodec_register_all(void)
72 72
     register_avcodec(&faac_encoder);
73 73
 #endif //CONFIG_FAAC_ENCODER
74 74
 #endif
75
+#ifdef CONFIG_FLAC_ENCODER
76
+    register_avcodec(&flac_encoder);
77
+#endif
75 78
 #ifdef CONFIG_XVID
76 79
 #ifdef CONFIG_XVID_ENCODER
77 80
     register_avcodec(&xvid_encoder);
... ...
@@ -2066,6 +2066,7 @@ extern AVCodec mp3lame_encoder;
2066 2066
 extern AVCodec oggvorbis_encoder;
2067 2067
 extern AVCodec oggtheora_encoder;
2068 2068
 extern AVCodec faac_encoder;
2069
+extern AVCodec flac_encoder;
2069 2070
 extern AVCodec xvid_encoder;
2070 2071
 extern AVCodec mpeg1video_encoder;
2071 2072
 extern AVCodec mpeg2video_encoder;
2072 2073
new file mode 100644
... ...
@@ -0,0 +1,570 @@
0
+/**
1
+ * FLAC audio encoder
2
+ * Copyright (c) 2006  Justin Ruggles <jruggle@earthlink.net>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+ */
18
+
19
+#include "avcodec.h"
20
+#include "bitstream.h"
21
+#include "crc.h"
22
+#include "golomb.h"
23
+
24
+#define FLAC_MAX_CH  8
25
+#define FLAC_MIN_BLOCKSIZE  16
26
+#define FLAC_MAX_BLOCKSIZE  65535
27
+
28
+#define FLAC_SUBFRAME_CONSTANT  0
29
+#define FLAC_SUBFRAME_VERBATIM  1
30
+#define FLAC_SUBFRAME_FIXED     8
31
+#define FLAC_SUBFRAME_LPC      32
32
+
33
+#define FLAC_CHMODE_NOT_STEREO      0
34
+#define FLAC_CHMODE_LEFT_RIGHT      1
35
+#define FLAC_CHMODE_LEFT_SIDE       8
36
+#define FLAC_CHMODE_RIGHT_SIDE      9
37
+#define FLAC_CHMODE_MID_SIDE       10
38
+
39
+#define FLAC_STREAMINFO_SIZE  34
40
+
41
+typedef struct FlacSubframe {
42
+    int type;
43
+    int type_code;
44
+    int obits;
45
+    int order;
46
+    int32_t samples[FLAC_MAX_BLOCKSIZE];
47
+    int32_t residual[FLAC_MAX_BLOCKSIZE];
48
+} FlacSubframe;
49
+
50
+typedef struct FlacFrame {
51
+    FlacSubframe subframes[FLAC_MAX_CH];
52
+    int blocksize;
53
+    int bs_code[2];
54
+    uint8_t crc8;
55
+    int ch_mode;
56
+} FlacFrame;
57
+
58
+typedef struct FlacEncodeContext {
59
+    PutBitContext pb;
60
+    int channels;
61
+    int ch_code;
62
+    int samplerate;
63
+    int sr_code[2];
64
+    int blocksize;
65
+    int max_framesize;
66
+    uint32_t frame_count;
67
+    FlacFrame frame;
68
+} FlacEncodeContext;
69
+
70
+static const int flac_samplerates[16] = {
71
+    0, 0, 0, 0,
72
+    8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
73
+    0, 0, 0, 0
74
+};
75
+
76
+static const int flac_blocksizes[16] = {
77
+    0,
78
+    192,
79
+    576, 1152, 2304, 4608,
80
+    0, 0,
81
+    256, 512, 1024, 2048, 4096, 8192, 16384, 32768
82
+};
83
+
84
+static const int flac_blocksizes_ordered[14] = {
85
+    0, 192, 256, 512, 576, 1024, 1152, 2048, 2304, 4096, 4608, 8192, 16384, 32768
86
+};
87
+
88
+/**
89
+ * Writes streaminfo metadata block to byte array
90
+ */
91
+static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
92
+{
93
+    PutBitContext pb;
94
+
95
+    memset(header, 0, FLAC_STREAMINFO_SIZE);
96
+    init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
97
+
98
+    /* streaminfo metadata block */
99
+    put_bits(&pb, 16, s->blocksize);
100
+    put_bits(&pb, 16, s->blocksize);
101
+    put_bits(&pb, 24, 0);
102
+    put_bits(&pb, 24, s->max_framesize);
103
+    put_bits(&pb, 20, s->samplerate);
104
+    put_bits(&pb, 3, s->channels-1);
105
+    put_bits(&pb, 5, 15);       /* bits per sample - 1 */
106
+    flush_put_bits(&pb);
107
+    /* total samples = 0 */
108
+    /* MD5 signature = 0 */
109
+}
110
+
111
+#define BLOCK_TIME_MS 105
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)
118
+{
119
+    int i;
120
+    int target;
121
+    int blocksize;
122
+
123
+    assert(samplerate > 0);
124
+    blocksize = 0;
125
+    target = (samplerate * BLOCK_TIME_MS) / 1000;
126
+    for(i=13; i>=0; i--) {
127
+        if(target >= flac_blocksizes_ordered[i]) {
128
+            blocksize = flac_blocksizes_ordered[i];
129
+            break;
130
+        }
131
+    }
132
+    if(blocksize == 0) {
133
+        blocksize = flac_blocksizes_ordered[1];
134
+    }
135
+    return blocksize;
136
+}
137
+
138
+static int flac_encode_init(AVCodecContext *avctx)
139
+{
140
+    int freq = avctx->sample_rate;
141
+    int channels = avctx->channels;
142
+    FlacEncodeContext *s = avctx->priv_data;
143
+    int i;
144
+    uint8_t *streaminfo;
145
+
146
+    if(s == NULL) {
147
+        return -1;
148
+    }
149
+
150
+    if(avctx->sample_fmt != SAMPLE_FMT_S16) {
151
+        return -1;
152
+    }
153
+
154
+    if(channels < 1 || channels > FLAC_MAX_CH) {
155
+        return -1;
156
+    }
157
+    s->channels = channels;
158
+    s->ch_code = s->channels-1;
159
+
160
+    /* find samplerate in table */
161
+    if(freq < 1)
162
+        return -1;
163
+    for(i=4; i<12; i++) {
164
+        if(freq == flac_samplerates[i]) {
165
+            s->samplerate = flac_samplerates[i];
166
+            s->sr_code[0] = i;
167
+            s->sr_code[1] = 0;
168
+            break;
169
+        }
170
+    }
171
+    /* if not in table, samplerate is non-standard */
172
+    if(i == 12) {
173
+        if(freq % 1000 == 0 && freq < 255000) {
174
+            s->sr_code[0] = 12;
175
+            s->sr_code[1] = freq / 1000;
176
+        } else if(freq % 10 == 0 && freq < 655350) {
177
+            s->sr_code[0] = 14;
178
+            s->sr_code[1] = freq / 10;
179
+        } else if(freq < 65535) {
180
+            s->sr_code[0] = 13;
181
+            s->sr_code[1] = freq;
182
+        } else {
183
+            return -1;
184
+        }
185
+        s->samplerate = freq;
186
+    }
187
+
188
+    s->blocksize = select_blocksize(s->samplerate);
189
+    avctx->frame_size = s->blocksize;
190
+
191
+    s->max_framesize = 14 + (s->blocksize * s->channels * 2);
192
+
193
+    streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
194
+    write_streaminfo(s, streaminfo);
195
+    avctx->extradata = streaminfo;
196
+    avctx->extradata_size = FLAC_STREAMINFO_SIZE;
197
+
198
+    s->frame_count = 0;
199
+
200
+    avctx->coded_frame = avcodec_alloc_frame();
201
+    avctx->coded_frame->key_frame = 1;
202
+
203
+    return 0;
204
+}
205
+
206
+static int init_frame(FlacEncodeContext *s)
207
+{
208
+    int i, ch;
209
+    FlacFrame *frame;
210
+
211
+    frame = &s->frame;
212
+
213
+    for(i=0; i<16; i++) {
214
+        if(s->blocksize == flac_blocksizes[i]) {
215
+            frame->blocksize = flac_blocksizes[i];
216
+            frame->bs_code[0] = i;
217
+            frame->bs_code[1] = 0;
218
+            break;
219
+        }
220
+    }
221
+    if(i == 16) {
222
+        frame->blocksize = s->blocksize;
223
+        if(frame->blocksize <= 256) {
224
+            frame->bs_code[0] = 6;
225
+            frame->bs_code[1] = frame->blocksize-1;
226
+        } else {
227
+            frame->bs_code[0] = 7;
228
+            frame->bs_code[1] = frame->blocksize-1;
229
+        }
230
+    }
231
+
232
+    for(ch=0; ch<s->channels; ch++) {
233
+        frame->subframes[ch].obits = 16;
234
+    }
235
+    if(s->channels == 2) {
236
+        frame->ch_mode = FLAC_CHMODE_LEFT_RIGHT;
237
+    } else {
238
+        frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
239
+    }
240
+
241
+    return 0;
242
+}
243
+
244
+/**
245
+ * Copy channel-interleaved input samples into separate subframes
246
+ */
247
+static void copy_samples(FlacEncodeContext *s, int16_t *samples)
248
+{
249
+    int i, j, ch;
250
+    FlacFrame *frame;
251
+
252
+    frame = &s->frame;
253
+    for(i=0,j=0; i<frame->blocksize; i++) {
254
+        for(ch=0; ch<s->channels; ch++,j++) {
255
+            frame->subframes[ch].samples[i] = samples[j];
256
+        }
257
+    }
258
+}
259
+
260
+static void encode_residual_verbatim(FlacEncodeContext *s, int ch)
261
+{
262
+    FlacFrame *frame;
263
+    FlacSubframe *sub;
264
+    int32_t *res;
265
+    int32_t *smp;
266
+    int n;
267
+
268
+    frame = &s->frame;
269
+    sub = &frame->subframes[ch];
270
+    res = sub->residual;
271
+    smp = sub->samples;
272
+    n = frame->blocksize;
273
+
274
+    sub->order = 0;
275
+    sub->type = FLAC_SUBFRAME_VERBATIM;
276
+    sub->type_code = sub->type;
277
+
278
+    memcpy(res, smp, n * sizeof(int32_t));
279
+}
280
+
281
+static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
282
+{
283
+    int i;
284
+    int32_t pred;
285
+
286
+    for(i=0; i<order; i++) {
287
+        res[i] = smp[i];
288
+    }
289
+    for(i=order; i<n; i++) {
290
+        pred = 0;
291
+        switch(order) {
292
+            case 0: pred = 0;
293
+                    break;
294
+            case 1: pred = smp[i-1];
295
+                    break;
296
+            case 2: pred = 2*smp[i-1] - smp[i-2];
297
+                    break;
298
+            case 3: pred = 3*smp[i-1] - 3*smp[i-2] + smp[i-3];
299
+                    break;
300
+            case 4: pred = 4*smp[i-1] - 6*smp[i-2] + 4*smp[i-3] - smp[i-4];
301
+                    break;
302
+        }
303
+        res[i] = smp[i] - pred;
304
+    }
305
+}
306
+
307
+static void encode_residual(FlacEncodeContext *s, int ch)
308
+{
309
+    FlacFrame *frame;
310
+    FlacSubframe *sub;
311
+    int32_t *res;
312
+    int32_t *smp;
313
+    int n;
314
+
315
+    frame = &s->frame;
316
+    sub = &frame->subframes[ch];
317
+    res = sub->residual;
318
+    smp = sub->samples;
319
+    n = frame->blocksize;
320
+
321
+    sub->order = 2;
322
+    sub->type = FLAC_SUBFRAME_FIXED;
323
+    sub->type_code = sub->type | sub->order;
324
+    encode_residual_fixed(res, smp, n, sub->order);
325
+}
326
+
327
+static void
328
+put_sbits(PutBitContext *pb, int bits, int32_t val)
329
+{
330
+    uint32_t uval;
331
+
332
+    assert(bits >= 0 && bits <= 31);
333
+    uval = (val < 0) ? (1UL << bits) + val : val;
334
+    put_bits(pb, bits, uval);
335
+}
336
+
337
+static void
338
+write_utf8(PutBitContext *pb, uint32_t val)
339
+{
340
+    int i, bytes, mask, shift;
341
+
342
+    bytes = 1;
343
+    if(val >= 0x80)      bytes++;
344
+    if(val >= 0x800)     bytes++;
345
+    if(val >= 0x10000)   bytes++;
346
+    if(val >= 0x200000)  bytes++;
347
+    if(val >= 0x4000000) bytes++;
348
+
349
+    if(bytes == 1) {
350
+        put_bits(pb, 8, val);
351
+        return;
352
+    }
353
+
354
+    shift = (bytes - 1) * 6;
355
+    mask = 0x80 + ((1 << 7) - (1 << (8 - bytes)));
356
+    put_bits(pb, 8, mask | (val >> shift));
357
+    for(i=0; i<bytes-1; i++) {
358
+        shift -= 6;
359
+        put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
360
+    }
361
+}
362
+
363
+static void
364
+output_frame_header(FlacEncodeContext *s)
365
+{
366
+    FlacFrame *frame;
367
+    int crc;
368
+
369
+    frame = &s->frame;
370
+
371
+    put_bits(&s->pb, 16, 0xFFF8);
372
+    put_bits(&s->pb, 4, frame->bs_code[0]);
373
+    put_bits(&s->pb, 4, s->sr_code[0]);
374
+    if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
375
+        put_bits(&s->pb, 4, s->ch_code);
376
+    } else {
377
+        put_bits(&s->pb, 4, frame->ch_mode);
378
+    }
379
+    put_bits(&s->pb, 3, 4); /* bits-per-sample code */
380
+    put_bits(&s->pb, 1, 0);
381
+    write_utf8(&s->pb, s->frame_count);
382
+    if(frame->bs_code[1] > 0) {
383
+        if(frame->bs_code[1] < 256) {
384
+            put_bits(&s->pb, 8, frame->bs_code[1]);
385
+        } else {
386
+            put_bits(&s->pb, 16, frame->bs_code[1]);
387
+        }
388
+    }
389
+    if(s->sr_code[1] > 0) {
390
+        if(s->sr_code[1] < 256) {
391
+            put_bits(&s->pb, 8, s->sr_code[1]);
392
+        } else {
393
+            put_bits(&s->pb, 16, s->sr_code[1]);
394
+        }
395
+    }
396
+    flush_put_bits(&s->pb);
397
+    crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
398
+    put_bits(&s->pb, 8, crc);
399
+}
400
+
401
+static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
402
+{
403
+    int i;
404
+    FlacFrame *frame;
405
+    FlacSubframe *sub;
406
+    int32_t res;
407
+
408
+    frame = &s->frame;
409
+    sub = &frame->subframes[ch];
410
+
411
+    for(i=0; i<frame->blocksize; i++) {
412
+        res = sub->residual[i];
413
+        put_sbits(&s->pb, sub->obits, res);
414
+    }
415
+}
416
+
417
+static void
418
+output_residual(FlacEncodeContext *ctx, int ch)
419
+{
420
+    int i, j, p;
421
+    int k, porder, psize, res_cnt;
422
+    FlacFrame *frame;
423
+    FlacSubframe *sub;
424
+
425
+    frame = &ctx->frame;
426
+    sub = &frame->subframes[ch];
427
+
428
+    /* rice-encoded block */
429
+    put_bits(&ctx->pb, 2, 0);
430
+
431
+    /* partition order */
432
+    porder = 0;
433
+    psize = frame->blocksize;
434
+    //porder = sub->rc.porder;
435
+    //psize = frame->blocksize >> porder;
436
+    put_bits(&ctx->pb, 4, porder);
437
+    res_cnt = psize - sub->order;
438
+
439
+    /* residual */
440
+    j = sub->order;
441
+    for(p=0; p<(1 << porder); p++) {
442
+        //k = sub->rc.params[p];
443
+        k = 9;
444
+        put_bits(&ctx->pb, 4, k);
445
+        if(p == 1) res_cnt = psize;
446
+        for(i=0; i<res_cnt && j<frame->blocksize; i++, j++) {
447
+            set_sr_golomb_flac(&ctx->pb, sub->residual[j], k, INT32_MAX, 0);
448
+        }
449
+    }
450
+}
451
+
452
+static void
453
+output_subframe_fixed(FlacEncodeContext *ctx, int ch)
454
+{
455
+    int i;
456
+    FlacFrame *frame;
457
+    FlacSubframe *sub;
458
+
459
+    frame = &ctx->frame;
460
+    sub = &frame->subframes[ch];
461
+
462
+    /* warm-up samples */
463
+    for(i=0; i<sub->order; i++) {
464
+        put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
465
+    }
466
+
467
+    /* residual */
468
+    output_residual(ctx, ch);
469
+}
470
+
471
+static void output_subframes(FlacEncodeContext *s)
472
+{
473
+    FlacFrame *frame;
474
+    FlacSubframe *sub;
475
+    int ch;
476
+
477
+    frame = &s->frame;
478
+
479
+    for(ch=0; ch<s->channels; ch++) {
480
+        sub = &frame->subframes[ch];
481
+
482
+        /* subframe header */
483
+        put_bits(&s->pb, 1, 0);
484
+        put_bits(&s->pb, 6, sub->type_code);
485
+        put_bits(&s->pb, 1, 0); /* no wasted bits */
486
+
487
+        /* subframe */
488
+        if(sub->type == FLAC_SUBFRAME_VERBATIM) {
489
+            output_subframe_verbatim(s, ch);
490
+        } else {
491
+            output_subframe_fixed(s, ch);
492
+        }
493
+    }
494
+}
495
+
496
+static void output_frame_footer(FlacEncodeContext *s)
497
+{
498
+    int crc;
499
+    flush_put_bits(&s->pb);
500
+    crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
501
+    put_bits(&s->pb, 16, crc);
502
+    flush_put_bits(&s->pb);
503
+}
504
+
505
+static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
506
+                             int buf_size, void *data)
507
+{
508
+    int ch;
509
+    FlacEncodeContext *s;
510
+    int16_t *samples = data;
511
+    int out_bytes;
512
+
513
+    s = avctx->priv_data;
514
+
515
+    s->blocksize = avctx->frame_size;
516
+    if(init_frame(s)) {
517
+        return 0;
518
+    }
519
+
520
+    copy_samples(s, samples);
521
+
522
+    for(ch=0; ch<s->channels; ch++) {
523
+        encode_residual(s, ch);
524
+    }
525
+    init_put_bits(&s->pb, frame, buf_size);
526
+    output_frame_header(s);
527
+    output_subframes(s);
528
+    output_frame_footer(s);
529
+    out_bytes = put_bits_count(&s->pb) >> 3;
530
+
531
+    if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
532
+        /* frame too large. use verbatim mode */
533
+        for(ch=0; ch<s->channels; ch++) {
534
+            encode_residual_verbatim(s, ch);
535
+        }
536
+        init_put_bits(&s->pb, frame, buf_size);
537
+        output_frame_header(s);
538
+        output_subframes(s);
539
+        output_frame_footer(s);
540
+        out_bytes = put_bits_count(&s->pb) >> 3;
541
+
542
+        if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
543
+            /* still too large. must be an error. */
544
+            av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
545
+            return -1;
546
+        }
547
+    }
548
+
549
+    s->frame_count++;
550
+    return out_bytes;
551
+}
552
+
553
+static int flac_encode_close(AVCodecContext *avctx)
554
+{
555
+    av_freep(&avctx->coded_frame);
556
+    return 0;
557
+}
558
+
559
+AVCodec flac_encoder = {
560
+    "flac",
561
+    CODEC_TYPE_AUDIO,
562
+    CODEC_ID_FLAC,
563
+    sizeof(FlacEncodeContext),
564
+    flac_encode_init,
565
+    flac_encode_frame,
566
+    flac_encode_close,
567
+    NULL,
568
+    .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
569
+};
... ...
@@ -435,6 +435,10 @@ static inline void set_ur_golomb_jpegls(PutBitContext *pb, int i, int k, int lim
435 435
 
436 436
     e= (i>>k) + 1;
437 437
     if(e<limit){
438
+        while(e > 31) {
439
+            put_bits(pb, 31, 0);
440
+            e -= 31;
441
+        }
438 442
         put_bits(pb, e, 1);
439 443
         if(k)
440 444
             put_bits(pb, k, i&((1<<k)-1));