~ubuntu-branches/debian/sid/mplayer/sid

« back to all changes in this revision

Viewing changes to libavcodec/vorbis_enc.c

  • Committer: Bazaar Package Importer
  • Author(s): A Mennucc1
  • Date: 2009-03-23 10:05:45 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090323100545-x8h79obawnnte7kk
Tags: 1.0~rc2+svn20090303-5
debian/control : move docbook-xml,docbook-xsl,xsltproc from 
Build-Depends-Indep to Build-Depends, since they are needed to run
configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
/**
22
 
 * @file vorbis_enc.c
 
22
 * @file libavcodec/vorbis_enc.c
23
23
 * Native Vorbis encoder.
24
24
 * @author Oded Shimon <ods15@ods15.dyndns.org>
25
25
 */
30
30
#include "vorbis.h"
31
31
#include "vorbis_enc_data.h"
32
32
 
 
33
#define BITSTREAM_WRITER_LE
 
34
#include "bitstream.h"
 
35
 
33
36
#undef NDEBUG
34
37
#include <assert.h>
35
38
 
45
48
    int * quantlist;
46
49
    float * dimentions;
47
50
    float * pow2;
48
 
} codebook_t;
 
51
} vorbis_enc_codebook;
49
52
 
50
53
typedef struct {
51
54
    int dim;
52
55
    int subclass;
53
56
    int masterbook;
54
57
    int * books;
55
 
} floor_class_t;
 
58
} vorbis_enc_floor_class;
56
59
 
57
60
typedef struct {
58
61
    int partitions;
59
62
    int * partition_to_class;
60
63
    int nclasses;
61
 
    floor_class_t * classes;
 
64
    vorbis_enc_floor_class * classes;
62
65
    int multiplier;
63
66
    int rangebits;
64
67
    int values;
65
 
    floor1_entry_t * list;
66
 
} floor_t;
 
68
    vorbis_floor1_entry * list;
 
69
} vorbis_enc_floor;
67
70
 
68
71
typedef struct {
69
72
    int type;
74
77
    int classbook;
75
78
    int8_t (*books)[8];
76
79
    float (*maxes)[2];
77
 
} residue_t;
 
80
} vorbis_enc_residue;
78
81
 
79
82
typedef struct {
80
83
    int submaps;
84
87
    int coupling_steps;
85
88
    int * magnitude;
86
89
    int * angle;
87
 
} mapping_t;
 
90
} vorbis_enc_mapping;
88
91
 
89
92
typedef struct {
90
93
    int blockflag;
91
94
    int mapping;
92
 
} vorbis_mode_t;
 
95
} vorbis_enc_mode;
93
96
 
94
97
typedef struct {
95
98
    int channels;
105
108
    float quality;
106
109
 
107
110
    int ncodebooks;
108
 
    codebook_t * codebooks;
 
111
    vorbis_enc_codebook * codebooks;
109
112
 
110
113
    int nfloors;
111
 
    floor_t * floors;
 
114
    vorbis_enc_floor * floors;
112
115
 
113
116
    int nresidues;
114
 
    residue_t * residues;
 
117
    vorbis_enc_residue * residues;
115
118
 
116
119
    int nmappings;
117
 
    mapping_t * mappings;
 
120
    vorbis_enc_mapping * mappings;
118
121
 
119
122
    int nmodes;
120
 
    vorbis_mode_t * modes;
121
 
} venc_context_t;
122
 
 
123
 
typedef struct {
124
 
    int total;
125
 
    int total_pos;
126
 
    int pos;
127
 
    uint8_t * buf_ptr;
128
 
} PutBitContext;
129
 
 
130
 
static inline void init_put_bits(PutBitContext * pb, uint8_t * buf, int buffer_len) {
131
 
    pb->total = buffer_len * 8;
132
 
    pb->total_pos = 0;
133
 
    pb->pos = 0;
134
 
    pb->buf_ptr = buf;
135
 
}
136
 
 
137
 
static void put_bits(PutBitContext * pb, int bits, uint64_t val) {
138
 
    if ((pb->total_pos += bits) >= pb->total) return;
139
 
    if (!bits) return;
140
 
    if (pb->pos) {
141
 
        if (pb->pos > bits) {
142
 
            *pb->buf_ptr |= val << (8 - pb->pos);
143
 
            pb->pos -= bits;
144
 
            bits = 0;
145
 
        } else {
146
 
            *pb->buf_ptr++ |= (val << (8 - pb->pos)) & 0xFF;
147
 
            val >>= pb->pos;
148
 
            bits -= pb->pos;
149
 
            pb->pos = 0;
150
 
        }
151
 
    }
152
 
    for (; bits >= 8; bits -= 8) {
153
 
        *pb->buf_ptr++ = val & 0xFF;
154
 
        val >>= 8;
155
 
    }
156
 
    if (bits) {
157
 
        *pb->buf_ptr = val;
158
 
        pb->pos = 8 - bits;
159
 
    }
160
 
}
161
 
 
162
 
static inline void flush_put_bits(PutBitContext * pb) {
163
 
}
164
 
 
165
 
static inline int put_bits_count(PutBitContext * pb) {
166
 
    return pb->total_pos;
167
 
}
168
 
 
169
 
static inline void put_codeword(PutBitContext * pb, codebook_t * cb, int entry) {
 
123
    vorbis_enc_mode * modes;
 
124
 
 
125
    int64_t sample_count;
 
126
} vorbis_enc_context;
 
127
 
 
128
static inline void put_codeword(PutBitContext * pb, vorbis_enc_codebook * cb, int entry) {
170
129
    assert(entry >= 0);
171
130
    assert(entry < cb->nentries);
172
131
    assert(cb->lens[entry]);
179
138
    return 0;
180
139
}
181
140
 
182
 
static void ready_codebook(codebook_t * cb) {
 
141
static void ready_codebook(vorbis_enc_codebook * cb) {
183
142
    int i;
184
143
 
185
144
    ff_vorbis_len2vlc(cb->lens, cb->codewords, cb->nentries);
212
171
    }
213
172
}
214
173
 
215
 
static void ready_residue(residue_t * rc, venc_context_t * venc) {
 
174
static void ready_residue(vorbis_enc_residue * rc, vorbis_enc_context * venc) {
216
175
    int i;
217
176
    assert(rc->type == 2);
218
177
    rc->maxes = av_mallocz(sizeof(float[2]) * rc->classifications);
219
178
    for (i = 0; i < rc->classifications; i++) {
220
179
        int j;
221
 
        codebook_t * cb;
 
180
        vorbis_enc_codebook * cb;
222
181
        for (j = 0; j < 8; j++)
223
182
            if (rc->books[i][j] != -1) break;
224
183
        if (j == 8) continue; // zero
244
203
    }
245
204
}
246
205
 
247
 
static void create_vorbis_context(venc_context_t * venc, AVCodecContext * avccontext) {
248
 
    floor_t * fc;
249
 
    residue_t * rc;
250
 
    mapping_t * mc;
 
206
static void create_vorbis_context(vorbis_enc_context * venc, AVCodecContext * avccontext) {
 
207
    vorbis_enc_floor * fc;
 
208
    vorbis_enc_residue * rc;
 
209
    vorbis_enc_mapping * mc;
251
210
    int i, book;
252
211
 
253
212
    venc->channels = avccontext->channels;
254
213
    venc->sample_rate = avccontext->sample_rate;
255
214
    venc->log2_blocksize[0] = venc->log2_blocksize[1] = 11;
256
215
 
257
 
    venc->ncodebooks = sizeof(cvectors)/sizeof(cvectors[0]);
258
 
    venc->codebooks = av_malloc(sizeof(codebook_t) * venc->ncodebooks);
 
216
    venc->ncodebooks = FF_ARRAY_ELEMS(cvectors);
 
217
    venc->codebooks = av_malloc(sizeof(vorbis_enc_codebook) * venc->ncodebooks);
259
218
 
260
219
    // codebook 0..14 - floor1 book, values 0..255
261
220
    // codebook 15 residue masterbook
262
221
    // codebook 16..29 residue
263
222
    for (book = 0; book < venc->ncodebooks; book++) {
264
 
        codebook_t * cb = &venc->codebooks[book];
 
223
        vorbis_enc_codebook * cb = &venc->codebooks[book];
265
224
        int vals;
266
225
        cb->ndimentions = cvectors[book].dim;
267
226
        cb->nentries = cvectors[book].real_len;
287
246
    }
288
247
 
289
248
    venc->nfloors = 1;
290
 
    venc->floors = av_malloc(sizeof(floor_t) * venc->nfloors);
 
249
    venc->floors = av_malloc(sizeof(vorbis_enc_floor) * venc->nfloors);
291
250
 
292
251
    // just 1 floor
293
252
    fc = &venc->floors[0];
300
259
        fc->nclasses = FFMAX(fc->nclasses, fc->partition_to_class[i]);
301
260
    }
302
261
    fc->nclasses++;
303
 
    fc->classes = av_malloc(sizeof(floor_class_t) * fc->nclasses);
 
262
    fc->classes = av_malloc(sizeof(vorbis_enc_floor_class) * fc->nclasses);
304
263
    for (i = 0; i < fc->nclasses; i++) {
305
 
        floor_class_t * c = &fc->classes[i];
 
264
        vorbis_enc_floor_class * c = &fc->classes[i];
306
265
        int j, books;
307
266
        c->dim = floor_classes[i].dim;
308
267
        c->subclass = floor_classes[i].subclass;
319
278
    for (i = 0; i < fc->partitions; i++)
320
279
        fc->values += fc->classes[fc->partition_to_class[i]].dim;
321
280
 
322
 
    fc->list = av_malloc(sizeof(floor1_entry_t) * fc->values);
 
281
    fc->list = av_malloc(sizeof(vorbis_floor1_entry) * fc->values);
323
282
    fc->list[0].x = 0;
324
283
    fc->list[1].x = 1 << fc->rangebits;
325
284
    for (i = 2; i < fc->values; i++) {
333
292
    ff_vorbis_ready_floor1_list(fc->list, fc->values);
334
293
 
335
294
    venc->nresidues = 1;
336
 
    venc->residues = av_malloc(sizeof(residue_t) * venc->nresidues);
 
295
    venc->residues = av_malloc(sizeof(vorbis_enc_residue) * venc->nresidues);
337
296
 
338
297
    // single residue
339
298
    rc = &venc->residues[0];
362
321
    ready_residue(rc, venc);
363
322
 
364
323
    venc->nmappings = 1;
365
 
    venc->mappings = av_malloc(sizeof(mapping_t) * venc->nmappings);
 
324
    venc->mappings = av_malloc(sizeof(vorbis_enc_mapping) * venc->nmappings);
366
325
 
367
326
    // single mapping
368
327
    mc = &venc->mappings[0];
385
344
    }
386
345
 
387
346
    venc->nmodes = 1;
388
 
    venc->modes = av_malloc(sizeof(vorbis_mode_t) * venc->nmodes);
 
347
    venc->modes = av_malloc(sizeof(vorbis_enc_mode) * venc->nmodes);
389
348
 
390
349
    // single mode
391
350
    venc->modes[0].blockflag = 0;
414
373
    put_bits(pb, 32, res);
415
374
}
416
375
 
417
 
static void put_codebook_header(PutBitContext * pb, codebook_t * cb) {
 
376
static void put_codebook_header(PutBitContext * pb, vorbis_enc_codebook * cb) {
418
377
    int i;
419
378
    int ordered = 0;
420
379
 
473
432
    }
474
433
}
475
434
 
476
 
static void put_floor_header(PutBitContext * pb, floor_t * fc) {
 
435
static void put_floor_header(PutBitContext * pb, vorbis_enc_floor * fc) {
477
436
    int i;
478
437
 
479
438
    put_bits(pb, 16, 1); // type, only floor1 is supported
505
464
        put_bits(pb, fc->rangebits, fc->list[i].x);
506
465
}
507
466
 
508
 
static void put_residue_header(PutBitContext * pb, residue_t * rc) {
 
467
static void put_residue_header(PutBitContext * pb, vorbis_enc_residue * rc) {
509
468
    int i;
510
469
 
511
470
    put_bits(pb, 16, rc->type);
536
495
    }
537
496
}
538
497
 
539
 
static int put_main_header(venc_context_t * venc, uint8_t ** out) {
 
498
static int put_main_header(vorbis_enc_context * venc, uint8_t ** out) {
540
499
    int i;
541
500
    PutBitContext pb;
542
501
    uint8_t buffer[50000] = {0}, * p = buffer;
605
564
    // mappings
606
565
    put_bits(&pb, 6, venc->nmappings - 1);
607
566
    for (i = 0; i < venc->nmappings; i++) {
608
 
        mapping_t * mc = &venc->mappings[i];
 
567
        vorbis_enc_mapping * mc = &venc->mappings[i];
609
568
        int j;
610
569
        put_bits(&pb, 16, 0); // mapping type
611
570
 
665
624
    return p - *out;
666
625
}
667
626
 
668
 
static float get_floor_average(floor_t * fc, float * coeffs, int i) {
 
627
static float get_floor_average(vorbis_enc_floor * fc, float * coeffs, int i) {
669
628
    int begin = fc->list[fc->list[FFMAX(i-1, 0)].sort].x;
670
629
    int end   = fc->list[fc->list[FFMIN(i+1, fc->values - 1)].sort].x;
671
630
    int j;
676
635
    return average / (end - begin);
677
636
}
678
637
 
679
 
static void floor_fit(venc_context_t * venc, floor_t * fc, float * coeffs, uint_fast16_t * posts, int samples) {
 
638
static void floor_fit(vorbis_enc_context * venc, vorbis_enc_floor * fc, float * coeffs, uint_fast16_t * posts, int samples) {
680
639
    int range = 255 / fc->multiplier + 1;
681
640
    int i;
682
641
    float tot_average = 0.;
704
663
    return y0 +  (x - x0) * (y1 - y0) / (x1 - x0);
705
664
}
706
665
 
707
 
static void floor_encode(venc_context_t * venc, floor_t * fc, PutBitContext * pb, uint_fast16_t * posts, float * floor, int samples) {
 
666
static void floor_encode(vorbis_enc_context * venc, vorbis_enc_floor * fc, PutBitContext * pb, uint_fast16_t * posts, float * floor, int samples) {
708
667
    int range = 255 / fc->multiplier + 1;
709
668
    int coded[fc->values]; // first 2 values are unused
710
669
    int i, counter;
745
704
 
746
705
    counter = 2;
747
706
    for (i = 0; i < fc->partitions; i++) {
748
 
        floor_class_t * c = &fc->classes[fc->partition_to_class[i]];
 
707
        vorbis_enc_floor_class * c = &fc->classes[fc->partition_to_class[i]];
749
708
        int k, cval = 0, csub = 1<<c->subclass;
750
709
        if (c->subclass) {
751
 
            codebook_t * book = &venc->codebooks[c->masterbook];
 
710
            vorbis_enc_codebook * book = &venc->codebooks[c->masterbook];
752
711
            int cshift = 0;
753
712
            for (k = 0; k < c->dim; k++) {
754
713
                int l;
778
737
    ff_vorbis_floor1_render_list(fc->list, fc->values, posts, coded, fc->multiplier, floor, samples);
779
738
}
780
739
 
781
 
static float * put_vector(codebook_t * book, PutBitContext * pb, float * num) {
 
740
static float * put_vector(vorbis_enc_codebook * book, PutBitContext * pb, float * num) {
782
741
    int i, entry = -1;
783
742
    float distance = FLT_MAX;
784
743
    assert(book->dimentions);
797
756
    return &book->dimentions[entry * book->ndimentions];
798
757
}
799
758
 
800
 
static void residue_encode(venc_context_t * venc, residue_t * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
 
759
static void residue_encode(vorbis_enc_context * venc, vorbis_enc_residue * rc, PutBitContext * pb, float * coeffs, int samples, int real_ch) {
801
760
    int pass, i, j, p, k;
802
761
    int psize = rc->partition_size;
803
762
    int partitions = (rc->end - rc->begin) / psize;
826
785
        while (p < partitions) {
827
786
            if (pass == 0)
828
787
                for (j = 0; j < channels; j++) {
829
 
                    codebook_t * book = &venc->codebooks[rc->classbook];
 
788
                    vorbis_enc_codebook * book = &venc->codebooks[rc->classbook];
830
789
                    int entry = 0;
831
790
                    for (i = 0; i < classwords; i++) {
832
791
                        entry *= rc->classifications;
837
796
            for (i = 0; i < classwords && p < partitions; i++, p++) {
838
797
                for (j = 0; j < channels; j++) {
839
798
                    int nbook = rc->books[classes[j][p]][pass];
840
 
                    codebook_t * book = &venc->codebooks[nbook];
 
799
                    vorbis_enc_codebook * book = &venc->codebooks[nbook];
841
800
                    float * buf = coeffs + samples*j + rc->begin + p*psize;
842
801
                    if (nbook == -1) continue;
843
802
 
882
841
    }
883
842
}
884
843
 
885
 
static int apply_window_and_mdct(venc_context_t * venc, signed short * audio, int samples) {
 
844
static int apply_window_and_mdct(vorbis_enc_context * venc, signed short * audio, int samples) {
886
845
    int i, j, channel;
887
846
    const float * win = venc->win[0];
888
847
    int window_len = 1 << (venc->log2_blocksize[0] - 1);
915
874
    }
916
875
 
917
876
    for (channel = 0; channel < venc->channels; channel++) {
918
 
        ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2, venc->floor/*tmp*/);
 
877
        ff_mdct_calc(&venc->mdct[0], venc->coeffs + channel*window_len, venc->samples + channel*window_len*2);
919
878
    }
920
879
 
921
880
    if (samples) {
932
891
    return 1;
933
892
}
934
893
 
935
 
static int vorbis_encode_init(AVCodecContext * avccontext)
 
894
static av_cold int vorbis_encode_init(AVCodecContext * avccontext)
936
895
{
937
 
    venc_context_t * venc = avccontext->priv_data;
 
896
    vorbis_enc_context * venc = avccontext->priv_data;
938
897
 
939
898
    if (avccontext->channels != 2) {
940
899
        av_log(avccontext, AV_LOG_ERROR, "Current FFmpeg Vorbis encoder only supports 2 channels.\n");
961
920
 
962
921
static int vorbis_encode_frame(AVCodecContext * avccontext, unsigned char * packets, int buf_size, void *data)
963
922
{
964
 
    venc_context_t * venc = avccontext->priv_data;
 
923
    vorbis_enc_context * venc = avccontext->priv_data;
965
924
    signed short * audio = data;
966
925
    int samples = data ? avccontext->frame_size : 0;
967
 
    vorbis_mode_t * mode;
968
 
    mapping_t * mapping;
 
926
    vorbis_enc_mode * mode;
 
927
    vorbis_enc_mapping * mapping;
969
928
    PutBitContext pb;
970
929
    int i;
971
930
 
986
945
    }
987
946
 
988
947
    for (i = 0; i < venc->channels; i++) {
989
 
        floor_t * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
 
948
        vorbis_enc_floor * fc = &venc->floors[mapping->floor[mapping->mux[i]]];
990
949
        uint_fast16_t posts[fc->values];
991
950
        floor_fit(venc, fc, &venc->coeffs[i * samples], posts, samples);
992
951
        floor_encode(venc, fc, &pb, posts, &venc->floor[i * samples], samples);
1010
969
 
1011
970
    residue_encode(venc, &venc->residues[mapping->residue[mapping->mux[0]]], &pb, venc->coeffs, samples, venc->channels);
1012
971
 
 
972
    avccontext->coded_frame->pts = venc->sample_count;
 
973
    venc->sample_count += avccontext->frame_size;
1013
974
    flush_put_bits(&pb);
1014
975
    return (put_bits_count(&pb) + 7) / 8;
1015
976
}
1016
977
 
1017
978
 
1018
 
static int vorbis_encode_close(AVCodecContext * avccontext)
 
979
static av_cold int vorbis_encode_close(AVCodecContext * avccontext)
1019
980
{
1020
 
    venc_context_t * venc = avccontext->priv_data;
 
981
    vorbis_enc_context * venc = avccontext->priv_data;
1021
982
    int i;
1022
983
 
1023
984
    if (venc->codebooks)
1079
1040
    "vorbis",
1080
1041
    CODEC_TYPE_AUDIO,
1081
1042
    CODEC_ID_VORBIS,
1082
 
    sizeof(venc_context_t),
 
1043
    sizeof(vorbis_enc_context),
1083
1044
    vorbis_encode_init,
1084
1045
    vorbis_encode_frame,
1085
1046
    vorbis_encode_close,
1086
1047
    .capabilities= CODEC_CAP_DELAY,
 
1048
    .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
 
1049
    .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
1087
1050
};