Statistics
| Branch: | Revision:

ffmpeg / libavcodec / flacenc.c @ 9e96ab03

History | View | Annotate | Download (13.6 KB)

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

    
20
#include "avcodec.h"
21
#include "bitstream.h"
22
#include "crc.h"
23
#include "golomb.h"
24

    
25
#define FLAC_MAX_CH  8
26
#define FLAC_MIN_BLOCKSIZE  16
27
#define FLAC_MAX_BLOCKSIZE  65535
28

    
29
#define FLAC_SUBFRAME_CONSTANT  0
30
#define FLAC_SUBFRAME_VERBATIM  1
31
#define FLAC_SUBFRAME_FIXED     8
32
#define FLAC_SUBFRAME_LPC      32
33

    
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
39

    
40
#define FLAC_STREAMINFO_SIZE  34
41

    
42
typedef struct FlacSubframe {
43
    int type;
44
    int type_code;
45
    int obits;
46
    int order;
47
    int32_t samples[FLAC_MAX_BLOCKSIZE];
48
    int32_t residual[FLAC_MAX_BLOCKSIZE];
49
} FlacSubframe;
50

    
51
typedef struct FlacFrame {
52
    FlacSubframe subframes[FLAC_MAX_CH];
53
    int blocksize;
54
    int bs_code[2];
55
    uint8_t crc8;
56
    int ch_mode;
57
} FlacFrame;
58

    
59
typedef struct FlacEncodeContext {
60
    PutBitContext pb;
61
    int channels;
62
    int ch_code;
63
    int samplerate;
64
    int sr_code[2];
65
    int blocksize;
66
    int max_framesize;
67
    uint32_t frame_count;
68
    FlacFrame frame;
69
} FlacEncodeContext;
70

    
71
static const int flac_samplerates[16] = {
72
    0, 0, 0, 0,
73
    8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
74
    0, 0, 0, 0
75
};
76

    
77
static const int flac_blocksizes[16] = {
78
    0,
79
    192,
80
    576, 1152, 2304, 4608,
81
    0, 0,
82
    256, 512, 1024, 2048, 4096, 8192, 16384, 32768
83
};
84

    
85
static const int flac_blocksizes_ordered[14] = {
86
    0, 192, 256, 512, 576, 1024, 1152, 2048, 2304, 4096, 4608, 8192, 16384, 32768
87
};
88

    
89
/**
90
 * Writes streaminfo metadata block to byte array
91
 */
92
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
93
{
94
    PutBitContext pb;
95

    
96
    memset(header, 0, FLAC_STREAMINFO_SIZE);
97
    init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
98

    
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 */
107
    flush_put_bits(&pb);
108
    /* total samples = 0 */
109
    /* MD5 signature = 0 */
110
}
111

    
112
#define BLOCK_TIME_MS 105
113

    
114
/**
115
 * Sets blocksize based on samplerate
116
 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
117
 */
118
static int select_blocksize(int samplerate)
119
{
120
    int i;
121
    int target;
122
    int blocksize;
123

    
124
    assert(samplerate > 0);
125
    blocksize = 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];
130
            break;
131
        }
132
    }
133
    if(blocksize == 0) {
134
        blocksize = flac_blocksizes_ordered[1];
135
    }
136
    return blocksize;
137
}
138

    
139
static int flac_encode_init(AVCodecContext *avctx)
140
{
141
    int freq = avctx->sample_rate;
142
    int channels = avctx->channels;
143
    FlacEncodeContext *s = avctx->priv_data;
144
    int i;
145
    uint8_t *streaminfo;
146

    
147
    if(s == NULL) {
148
        return -1;
149
    }
150

    
151
    if(avctx->sample_fmt != SAMPLE_FMT_S16) {
152
        return -1;
153
    }
154

    
155
    if(channels < 1 || channels > FLAC_MAX_CH) {
156
        return -1;
157
    }
158
    s->channels = channels;
159
    s->ch_code = s->channels-1;
160

    
161
    /* find samplerate in table */
162
    if(freq < 1)
163
        return -1;
164
    for(i=4; i<12; i++) {
165
        if(freq == flac_samplerates[i]) {
166
            s->samplerate = flac_samplerates[i];
167
            s->sr_code[0] = i;
168
            s->sr_code[1] = 0;
169
            break;
170
        }
171
    }
172
    /* if not in table, samplerate is non-standard */
173
    if(i == 12) {
174
        if(freq % 1000 == 0 && freq < 255000) {
175
            s->sr_code[0] = 12;
176
            s->sr_code[1] = freq / 1000;
177
        } else if(freq % 10 == 0 && freq < 655350) {
178
            s->sr_code[0] = 14;
179
            s->sr_code[1] = freq / 10;
180
        } else if(freq < 65535) {
181
            s->sr_code[0] = 13;
182
            s->sr_code[1] = freq;
183
        } else {
184
            return -1;
185
        }
186
        s->samplerate = freq;
187
    }
188

    
189
    s->blocksize = select_blocksize(s->samplerate);
190
    avctx->frame_size = s->blocksize;
191

    
192
    s->max_framesize = 14 + (s->blocksize * s->channels * 2);
193

    
194
    streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
195
    write_streaminfo(s, streaminfo);
196
    avctx->extradata = streaminfo;
197
    avctx->extradata_size = FLAC_STREAMINFO_SIZE;
198

    
199
    s->frame_count = 0;
200

    
201
    avctx->coded_frame = avcodec_alloc_frame();
202
    avctx->coded_frame->key_frame = 1;
203

    
204
    return 0;
205
}
206

    
207
static int init_frame(FlacEncodeContext *s)
208
{
209
    int i, ch;
210
    FlacFrame *frame;
211

    
212
    frame = &s->frame;
213

    
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;
219
            break;
220
        }
221
    }
222
    if(i == 16) {
223
        frame->blocksize = s->blocksize;
224
        if(frame->blocksize <= 256) {
225
            frame->bs_code[0] = 6;
226
            frame->bs_code[1] = frame->blocksize-1;
227
        } else {
228
            frame->bs_code[0] = 7;
229
            frame->bs_code[1] = frame->blocksize-1;
230
        }
231
    }
232

    
233
    for(ch=0; ch<s->channels; ch++) {
234
        frame->subframes[ch].obits = 16;
235
    }
236
    if(s->channels == 2) {
237
        frame->ch_mode = FLAC_CHMODE_LEFT_RIGHT;
238
    } else {
239
        frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
240
    }
241

    
242
    return 0;
243
}
244

    
245
/**
246
 * Copy channel-interleaved input samples into separate subframes
247
 */
248
static void copy_samples(FlacEncodeContext *s, int16_t *samples)
249
{
250
    int i, j, ch;
251
    FlacFrame *frame;
252

    
253
    frame = &s->frame;
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];
257
        }
258
    }
259
}
260

    
261
static void encode_residual_verbatim(FlacEncodeContext *s, int ch)
262
{
263
    FlacFrame *frame;
264
    FlacSubframe *sub;
265
    int32_t *res;
266
    int32_t *smp;
267
    int n;
268

    
269
    frame = &s->frame;
270
    sub = &frame->subframes[ch];
271
    res = sub->residual;
272
    smp = sub->samples;
273
    n = frame->blocksize;
274

    
275
    sub->order = 0;
276
    sub->type = FLAC_SUBFRAME_VERBATIM;
277
    sub->type_code = sub->type;
278

    
279
    memcpy(res, smp, n * sizeof(int32_t));
280
}
281

    
282
static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
283
{
284
    int i;
285
    int32_t pred;
286

    
287
    for(i=0; i<order; i++) {
288
        res[i] = smp[i];
289
    }
290
    for(i=order; i<n; i++) {
291
        pred = 0;
292
        switch(order) {
293
            case 0: pred = 0;
294
                    break;
295
            case 1: pred = smp[i-1];
296
                    break;
297
            case 2: pred = 2*smp[i-1] - smp[i-2];
298
                    break;
299
            case 3: pred = 3*smp[i-1] - 3*smp[i-2] + smp[i-3];
300
                    break;
301
            case 4: pred = 4*smp[i-1] - 6*smp[i-2] + 4*smp[i-3] - smp[i-4];
302
                    break;
303
        }
304
        res[i] = smp[i] - pred;
305
    }
306
}
307

    
308
static void encode_residual(FlacEncodeContext *s, int ch)
309
{
310
    FlacFrame *frame;
311
    FlacSubframe *sub;
312
    int32_t *res;
313
    int32_t *smp;
314
    int n;
315

    
316
    frame = &s->frame;
317
    sub = &frame->subframes[ch];
318
    res = sub->residual;
319
    smp = sub->samples;
320
    n = frame->blocksize;
321

    
322
    sub->order = 2;
323
    sub->type = FLAC_SUBFRAME_FIXED;
324
    sub->type_code = sub->type | sub->order;
325
    encode_residual_fixed(res, smp, n, sub->order);
326
}
327

    
328
static void
329
put_sbits(PutBitContext *pb, int bits, int32_t val)
330
{
331
    uint32_t uval;
332

    
333
    assert(bits >= 0 && bits <= 31);
334
    uval = (val < 0) ? (1UL << bits) + val : val;
335
    put_bits(pb, bits, uval);
336
}
337

    
338
static void
339
write_utf8(PutBitContext *pb, uint32_t val)
340
{
341
    int i, bytes, mask, shift;
342

    
343
    bytes = 1;
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++;
349

    
350
    if(bytes == 1) {
351
        put_bits(pb, 8, val);
352
        return;
353
    }
354

    
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++) {
359
        shift -= 6;
360
        put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
361
    }
362
}
363

    
364
static void
365
output_frame_header(FlacEncodeContext *s)
366
{
367
    FlacFrame *frame;
368
    int crc;
369

    
370
    frame = &s->frame;
371

    
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);
377
    } else {
378
        put_bits(&s->pb, 4, frame->ch_mode);
379
    }
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]);
386
        } else {
387
            put_bits(&s->pb, 16, frame->bs_code[1]);
388
        }
389
    }
390
    if(s->sr_code[1] > 0) {
391
        if(s->sr_code[1] < 256) {
392
            put_bits(&s->pb, 8, s->sr_code[1]);
393
        } else {
394
            put_bits(&s->pb, 16, s->sr_code[1]);
395
        }
396
    }
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);
400
}
401

    
402
static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
403
{
404
    int i;
405
    FlacFrame *frame;
406
    FlacSubframe *sub;
407
    int32_t res;
408

    
409
    frame = &s->frame;
410
    sub = &frame->subframes[ch];
411

    
412
    for(i=0; i<frame->blocksize; i++) {
413
        res = sub->residual[i];
414
        put_sbits(&s->pb, sub->obits, res);
415
    }
416
}
417

    
418
static void
419
output_residual(FlacEncodeContext *ctx, int ch)
420
{
421
    int i, j, p;
422
    int k, porder, psize, res_cnt;
423
    FlacFrame *frame;
424
    FlacSubframe *sub;
425

    
426
    frame = &ctx->frame;
427
    sub = &frame->subframes[ch];
428

    
429
    /* rice-encoded block */
430
    put_bits(&ctx->pb, 2, 0);
431

    
432
    /* partition order */
433
    porder = 0;
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;
439

    
440
    /* residual */
441
    j = sub->order;
442
    for(p=0; p<(1 << porder); p++) {
443
        //k = sub->rc.params[p];
444
        k = 9;
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);
449
        }
450
    }
451
}
452

    
453
static void
454
output_subframe_fixed(FlacEncodeContext *ctx, int ch)
455
{
456
    int i;
457
    FlacFrame *frame;
458
    FlacSubframe *sub;
459

    
460
    frame = &ctx->frame;
461
    sub = &frame->subframes[ch];
462

    
463
    /* warm-up samples */
464
    for(i=0; i<sub->order; i++) {
465
        put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
466
    }
467

    
468
    /* residual */
469
    output_residual(ctx, ch);
470
}
471

    
472
static void output_subframes(FlacEncodeContext *s)
473
{
474
    FlacFrame *frame;
475
    FlacSubframe *sub;
476
    int ch;
477

    
478
    frame = &s->frame;
479

    
480
    for(ch=0; ch<s->channels; ch++) {
481
        sub = &frame->subframes[ch];
482

    
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 */
487

    
488
        /* subframe */
489
        if(sub->type == FLAC_SUBFRAME_VERBATIM) {
490
            output_subframe_verbatim(s, ch);
491
        } else {
492
            output_subframe_fixed(s, ch);
493
        }
494
    }
495
}
496

    
497
static void output_frame_footer(FlacEncodeContext *s)
498
{
499
    int crc;
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);
504
}
505

    
506
static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
507
                             int buf_size, void *data)
508
{
509
    int ch;
510
    FlacEncodeContext *s;
511
    int16_t *samples = data;
512
    int out_bytes;
513

    
514
    s = avctx->priv_data;
515

    
516
    s->blocksize = avctx->frame_size;
517
    if(init_frame(s)) {
518
        return 0;
519
    }
520

    
521
    copy_samples(s, samples);
522

    
523
    for(ch=0; ch<s->channels; ch++) {
524
        encode_residual(s, ch);
525
    }
526
    init_put_bits(&s->pb, frame, buf_size);
527
    output_frame_header(s);
528
    output_subframes(s);
529
    output_frame_footer(s);
530
    out_bytes = put_bits_count(&s->pb) >> 3;
531

    
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);
536
        }
537
        init_put_bits(&s->pb, frame, buf_size);
538
        output_frame_header(s);
539
        output_subframes(s);
540
        output_frame_footer(s);
541
        out_bytes = put_bits_count(&s->pb) >> 3;
542

    
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");
546
            return -1;
547
        }
548
    }
549

    
550
    s->frame_count++;
551
    return out_bytes;
552
}
553

    
554
static int flac_encode_close(AVCodecContext *avctx)
555
{
556
    av_freep(&avctx->coded_frame);
557
    return 0;
558
}
559

    
560
AVCodec flac_encoder = {
561
    "flac",
562
    CODEC_TYPE_AUDIO,
563
    CODEC_ID_FLAC,
564
    sizeof(FlacEncodeContext),
565
    flac_encode_init,
566
    flac_encode_frame,
567
    flac_encode_close,
568
    NULL,
569
    .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
570
};