Revision 6107fa87

View differences:

libavcodec/ac3.h
1
/*
2
 * Common code between AC3 encoder and decoder
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19

  
20
#define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
21
#define AC3_MAX_CHANNELS 6 /* including LFE channel */
22

  
23
#define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
24
#define AC3_FRAME_SIZE (NB_BLOCKS * 256)
25

  
26
/* exponent encoding strategy */
27
#define EXP_REUSE 0
28
#define EXP_NEW   1
29

  
30
#define EXP_D15   1
31
#define EXP_D25   2
32
#define EXP_D45   3
33

  
34
typedef struct AC3BitAllocParameters {
35
    int fscod; /* frequency */
36
    int halfratecod;
37
    int sgain, sdecay, fdecay, dbknee, floor;
38
    int cplfleak, cplsleak;
39
} AC3BitAllocParameters;
40

  
41
extern const UINT16 ac3_freqs[3];
42
extern const UINT16 ac3_bitratetab[19];
43
extern const INT16 ac3_window[256];
44
extern const UINT8 sdecaytab[4];
45
extern const UINT8 fdecaytab[4];
46
extern const UINT16 sgaintab[4];
47
extern const UINT16 dbkneetab[4];
48
extern const UINT16 floortab[8];
49
extern const UINT16 fgaintab[8];
50

  
51
void ac3_common_init(void);
52
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, UINT8 *bap,
53
                                   INT8 *exp, int start, int end,
54
                                   int snroffset, int fgain, int is_lfe,
55
                                   int deltbae,int deltnseg, 
56
                                   UINT8 *deltoffst, UINT8 *deltlen, UINT8 *deltba);
libavcodec/ac3enc.c
20 20
//#define DEBUG_BITALLOC
21 21
#include "avcodec.h"
22 22

  
23
#include "ac3enc.h"
24
#include "ac3tab.h"
23
#include "ac3.h"
24

  
25
typedef struct AC3EncodeContext {
26
    PutBitContext pb;
27
    int nb_channels;
28
    int nb_all_channels;
29
    int lfe_channel;
30
    int bit_rate;
31
    int sample_rate;
32
    int bsid;
33
    int frame_size_min; /* minimum frame size in case rounding is necessary */
34
    int frame_size; /* current frame size in words */
35
    int halfratecod;
36
    int frmsizecod;
37
    int fscod; /* frequency */
38
    int acmod;
39
    int lfe;
40
    int bsmod;
41
    short last_samples[AC3_MAX_CHANNELS][256];
42
    int chbwcod[AC3_MAX_CHANNELS];
43
    int nb_coefs[AC3_MAX_CHANNELS];
44
    
45
    /* bitrate allocation control */
46
    int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod; 
47
    AC3BitAllocParameters bit_alloc;
48
    int csnroffst;
49
    int fgaincod[AC3_MAX_CHANNELS];
50
    int fsnroffst[AC3_MAX_CHANNELS];
51
    /* mantissa encoding */
52
    int mant1_cnt, mant2_cnt, mant4_cnt;
53
} AC3EncodeContext;
25 54

  
55
#include "ac3tab.h"
26 56

  
27 57
#define MDCT_NBITS 9
28 58
#define N         (1 << MDCT_NBITS)
29
#define NB_BLOCKS 6 /* number of PCM blocks inside an AC3 frame */
30 59

  
31 60
/* new exponents are sent if their Norm 1 exceed this number */
32 61
#define EXP_DIFF_THRESHOLD 1000
33 62

  
34
/* exponent encoding strategy */
35
#define EXP_REUSE 0
36
#define EXP_NEW   1
37

  
38
#define EXP_D15   1
39
#define EXP_D25   2
40
#define EXP_D45   3
41

  
42 63
static void fft_init(int ln);
43 64
static void ac3_crc_init(void);
44 65

  
......
88 109
}
89 110

  
90 111
/* AC3 bit allocation. The algorithm is the one described in the AC3
91
   spec with some optimizations because of our simplified encoding
92
   assumptions. */
93
void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
94
                               INT8 *exp, int start, int end,
95
                               int snroffset, int fgain, int is_lfe)
112
   spec. */
113
void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, UINT8 *bap,
114
                                   INT8 *exp, int start, int end,
115
                                   int snroffset, int fgain, int is_lfe,
116
                                   int deltbae,int deltnseg, 
117
                                   UINT8 *deltoffst, UINT8 *deltlen, UINT8 *deltba)
96 118
{
97 119
    int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
98 120
    int fastleak,slowleak,address,tmp;
......
138 160
    bndstrt = masktab[start];
139 161
    bndend = masktab[end-1] + 1;
140 162
    
141
    lowcomp = 0;
142
    lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
143
    excite[0] = bndpsd[0] - fgain - lowcomp ;
144
    lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
145
    excite[1] = bndpsd[1] - fgain - lowcomp ;
146
    begin = 7 ;
147
    for (bin = 2; bin < 7; bin++) {
148
	if (!(is_lfe && bin == 6))
149
	    lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
150
        fastleak = bndpsd[bin] - fgain ;
151
        slowleak = bndpsd[bin] - s->sgain ;
152
        excite[bin] = fastleak - lowcomp ;
153
	if (!(is_lfe && bin == 6)) {
154
	    if (bndpsd[bin] <= bndpsd[bin+1]) {
155
		begin = bin + 1 ;
156
		break ;
157
	    }
158
	}
159
    }
163
    if (bndstrt == 0) {
164
        lowcomp = 0;
165
        lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
166
        excite[0] = bndpsd[0] - fgain - lowcomp ;
167
        lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
168
        excite[1] = bndpsd[1] - fgain - lowcomp ;
169
        begin = 7 ;
170
        for (bin = 2; bin < 7; bin++) {
171
            if (!(is_lfe && bin == 6))
172
                lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
173
            fastleak = bndpsd[bin] - fgain ;
174
            slowleak = bndpsd[bin] - s->sgain ;
175
            excite[bin] = fastleak - lowcomp ;
176
            if (!(is_lfe && bin == 6)) {
177
                if (bndpsd[bin] <= bndpsd[bin+1]) {
178
                    begin = bin + 1 ;
179
                    break ;
180
                }
181
            }
182
        }
160 183
    
161
    end1=bndend;
162
    if (end1 > 22) end1=22;
184
        end1=bndend;
185
        if (end1 > 22) end1=22;
163 186
    
164
    for (bin = begin; bin < end1; bin++) {
165
	if (!(is_lfe && bin == 6))
166
	    lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
187
        for (bin = begin; bin < end1; bin++) {
188
            if (!(is_lfe && bin == 6))
189
                lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
167 190
        
168
        fastleak -= s->fdecay ;
169
        v = bndpsd[bin] - fgain;
170
        if (fastleak < v) fastleak = v;
191
            fastleak -= s->fdecay ;
192
            v = bndpsd[bin] - fgain;
193
            if (fastleak < v) fastleak = v;
171 194
        
172
        slowleak -= s->sdecay ;
173
        v = bndpsd[bin] - s->sgain;
174
        if (slowleak < v) slowleak = v;
195
            slowleak -= s->sdecay ;
196
            v = bndpsd[bin] - s->sgain;
197
            if (slowleak < v) slowleak = v;
175 198
        
176
        v=fastleak - lowcomp;
177
        if (slowleak > v) v=slowleak;
199
            v=fastleak - lowcomp;
200
            if (slowleak > v) v=slowleak;
178 201
        
179
        excite[bin] = v;
202
            excite[bin] = v;
203
        }
204
        begin = 22;
205
    } else {
206
        /* coupling channel */
207
        begin = bndstrt;
208
        
209
        fastleak = (s->cplfleak << 8) + 768;
210
        slowleak = (s->cplsleak << 8) + 768;
180 211
    }
181 212

  
182
    for (bin = 22; bin < bndend; bin++) {
213
    for (bin = begin; bin < bndend; bin++) {
183 214
        fastleak -= s->fdecay ;
184 215
        v = bndpsd[bin] - fgain;
185 216
        if (fastleak < v) fastleak = v;
......
205 236
        mask[bin] = v;
206 237
    }
207 238

  
239
    /* delta bit allocation */
240

  
241
    if (deltbae == 0 || deltbae == 1) {
242
        int band, seg, delta;
243
        band = 0 ;
244
        for (seg = 0; seg < deltnseg; seg++) {
245
            band += deltoffst[seg] ;
246
            if (deltba[seg] >= 4) {
247
                delta = (deltba[seg] - 3) << 7;
248
            } else {
249
                delta = (deltba[seg] - 4) << 7;
250
            }
251
            for (k = 0; k < deltlen[seg]; k++) {
252
                mask[band] += delta ;
253
                band++ ;
254
            }
255
        }
256
    }
257

  
208 258
    /* compute bit allocation */
209 259
    
210 260
    i = start ;
......
600 650
        s->mant2_cnt = 0;
601 651
        s->mant4_cnt = 0;
602 652
        for(ch=0;ch<s->nb_all_channels;ch++) {
603
            parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch], 
604
                                      0, s->nb_coefs[ch], 
605
                                      (((csnroffst-15) << 4) + 
606
                                       fsnroffst) << 2, 
607
                                      fgaintab[s->fgaincod[ch]],
608
				      ch == s->lfe_channel);
653
            ac3_parametric_bit_allocation(&s->bit_alloc, 
654
                                          bap[i][ch], (INT8 *)encoded_exp[i][ch], 
655
                                          0, s->nb_coefs[ch], 
656
                                          (((csnroffst-15) << 4) + 
657
                                           fsnroffst) << 2, 
658
                                          fgaintab[s->fgaincod[ch]],
659
                                          ch == s->lfe_channel,
660
                                          2, 0, NULL, NULL, NULL);
609 661
            frame_bits += compute_mantissa_size(s, bap[i][ch], 
610 662
                                                 s->nb_coefs[ch]);
611 663
        }
......
641 693
        s->fgaincod[ch] = 4;
642 694
    
643 695
    /* compute real values */
644
    s->sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
645
    s->fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
646
    s->sgain = sgaintab[s->sgaincod];
647
    s->dbknee = dbkneetab[s->dbkneecod];
648
    s->floor = floortab[s->floorcod];
649

  
696
    s->bit_alloc.fscod = s->fscod;
697
    s->bit_alloc.halfratecod = s->halfratecod;
698
    s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;
699
    s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;
700
    s->bit_alloc.sgain = sgaintab[s->sgaincod];
701
    s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];
702
    s->bit_alloc.floor = floortab[s->floorcod];
703
    
650 704
    /* header size */
651 705
    frame_bits += 65;
652 706
    // if (s->acmod == 2)
......
738 792
    return 0;
739 793
}
740 794

  
795
void ac3_common_init(void)
796
{
797
    int i, j, k, l, v;
798
    /* compute bndtab and masktab from bandsz */
799
    k = 0;
800
    l = 0;
801
    for(i=0;i<50;i++) {
802
        bndtab[i] = l;
803
        v = bndsz[i];
804
        for(j=0;j<v;j++) masktab[k++]=i;
805
        l += v;
806
    }
807
    bndtab[50] = 0;
808
}
809

  
810

  
741 811
static int AC3_encode_init(AVCodecContext *avctx)
742 812
{
743 813
    int freq = avctx->sample_rate;
744 814
    int bitrate = avctx->bit_rate;
745 815
    int channels = avctx->channels;
746 816
    AC3EncodeContext *s = avctx->priv_data;
747
    int i, j, k, l, ch, v;
817
    int i, j, ch;
748 818
    float alpha;
749
    static unsigned short freqs[3] = { 48000, 44100, 32000 };
750
    static int acmod_defs[6] = {
819
    static const UINT8 acmod_defs[6] = {
751 820
	0x01, /* C */
752 821
	0x02, /* L R */
753 822
	0x03, /* L C R */
......
771 840
    /* frequency */
772 841
    for(i=0;i<3;i++) {
773 842
        for(j=0;j<3;j++) 
774
            if ((freqs[j] >> i) == freq)
843
            if ((ac3_freqs[j] >> i) == freq)
775 844
                goto found;
776 845
    }
777 846
    return -1;
......
785 854
    /* bitrate & frame size */
786 855
    bitrate /= 1000;
787 856
    for(i=0;i<19;i++) {
788
        if ((bitratetab[i] >> s->halfratecod) == bitrate)
857
        if ((ac3_bitratetab[i] >> s->halfratecod) == bitrate)
789 858
            break;
790 859
    }
791 860
    if (i == 19)
......
810 879
    /* initial snr offset */
811 880
    s->csnroffst = 40;
812 881

  
813
    /* compute bndtab and masktab from bandsz */
814
    k = 0;
815
    l = 0;
816
    for(i=0;i<50;i++) {
817
        bndtab[i] = l;
818
        v = bndsz[i];
819
        for(j=0;j<v;j++) masktab[k++]=i;
820
        l += v;
821
    }
822
    bndtab[50] = 0;
882
    ac3_common_init();
823 883

  
824 884
    /* mdct init */
825 885
    fft_init(MDCT_NBITS - 2);
......
1282 1342
    return frame_size * 2;
1283 1343
}
1284 1344

  
1285
int AC3_encode_frame(AVCodecContext *avctx,
1286
                     unsigned char *frame, int buf_size, void *data)
1345
static int AC3_encode_frame(AVCodecContext *avctx,
1346
                            unsigned char *frame, int buf_size, void *data)
1287 1347
{
1288 1348
    AC3EncodeContext *s = avctx->priv_data;
1289 1349
    short *samples = data;
libavcodec/ac3enc.h
1

  
2
#define AC3_FRAME_SIZE (6*256)
3
#define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
4
#define AC3_MAX_CHANNELS 6
5

  
6
typedef struct AC3EncodeContext {
7
    PutBitContext pb;
8
    int nb_channels;
9
    int nb_all_channels;
10
    int lfe_channel;
11
    int bit_rate;
12
    int sample_rate;
13
    int bsid;
14
    int frame_size_min; /* minimum frame size in case rounding is necessary */
15
    int frame_size; /* current frame size in words */
16
    int halfratecod;
17
    int frmsizecod;
18
    int fscod; /* frequency */
19
    int acmod;
20
    int lfe;
21
    int bsmod;
22
    short last_samples[AC3_MAX_CHANNELS][256];
23
    int chbwcod[AC3_MAX_CHANNELS];
24
    int nb_coefs[AC3_MAX_CHANNELS];
25
    
26
    /* bitrate allocation control */
27
    int sgaincod, sdecaycod, fdecaycod, dbkneecod, floorcod; 
28
    int sgain, sdecay, fdecay, dbknee, floor;
29
    int csnroffst;
30
    int fgaincod[AC3_MAX_CHANNELS];
31
    int fsnroffst[AC3_MAX_CHANNELS];
32
    /* mantissa encoding */
33
    int mant1_cnt, mant2_cnt, mant4_cnt;
34
} AC3EncodeContext;
libavcodec/ac3tab.h
1 1
/* tables taken directly from AC3 spec */
2 2

  
3
/* possible frequencies */
4
const UINT16 ac3_freqs[3] = { 48000, 44100, 32000 };
5

  
3 6
/* possible bitrates */
4
static const UINT16 bitratetab[19] = {
7
const UINT16 ac3_bitratetab[19] = {
5 8
    32, 40, 48, 56, 64, 80, 96, 112, 128, 
6 9
    160, 192, 224, 256, 320, 384, 448, 512, 576, 640 
7 10
};
......
9 12
/* AC3 MDCT window */
10 13

  
11 14
/* MDCT window */
12
static const INT16 ac3_window[256]= {
15
const INT16 ac3_window[256] = {
13 16
    4,    7,   12,   16,   21,   28,   34,   42,
14 17
   51,   61,   72,   84,   97,  111,  127,  145,
15 18
  164,  184,  207,  231,  257,  285,  315,  347,
......
138 141
    15, 15, 15, 15,
139 142
};
140 143

  
141
static const UINT8 sdecaytab[4]={ 
144
const UINT8 sdecaytab[4]={ 
142 145
    0x0f, 0x11, 0x13, 0x15,
143 146
};
144 147

  
145
static const UINT8 fdecaytab[4]={ 
148
const UINT8 fdecaytab[4]={ 
146 149
    0x3f, 0x53, 0x67, 0x7b, 
147 150
};
148 151

  
149
static const UINT16 sgaintab[4]= { 
152
const UINT16 sgaintab[4]= { 
150 153
    0x540, 0x4d8, 0x478, 0x410,
151 154
};
152 155

  
153
static const UINT16 dbkneetab[4]= { 
156
const UINT16 dbkneetab[4]= { 
154 157
    0x000, 0x700, 0x900, 0xb00,
155 158
};
156 159

  
157
static const UINT16 floortab[8]= { 
160
const UINT16 floortab[8]= { 
158 161
    0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800,
159 162
};
160 163

  
161
static const UINT16 fgaintab[8]= {
164
const UINT16 fgaintab[8]= {
162 165
    0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400,
163 166
};
164 167

  

Also available in: Unified diff