Mercurial > libavcodec.hg
annotate ac3.c @ 4652:6679d37a3338 libavcodec
Give context to dprintf
author | mbardiaux |
---|---|
date | Mon, 12 Mar 2007 12:36:41 +0000 |
parents | 5683b496ffbc |
children | acdd4b24f5c5 |
rev | line source |
---|---|
0 | 1 /* |
4642 | 2 * Common code between AC3 encoder and decoder |
429 | 3 * Copyright (c) 2000 Fabrice Bellard. |
0 | 4 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
429 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Lesser General Public License for more details. | |
0 | 16 * |
429 | 17 * You should have received a copy of the GNU Lesser General Public |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3668
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
1106 | 21 |
22 /** | |
4642 | 23 * @file ac3.c |
24 * Common code between AC3 encoder and decoder. | |
1106 | 25 */ |
782 | 26 |
4642 | 27 #include "avcodec.h" |
28 #include "ac3.h" | |
0 | 29 #include "ac3tab.h" |
4648 | 30 #include "bitstream.h" |
0 | 31 |
4641 | 32 static inline int calc_lowcomp1(int a, int b0, int b1, int c) |
0 | 33 { |
34 if ((b0 + 256) == b1) { | |
4641 | 35 a = c; |
2967 | 36 } else if (b0 > b1) { |
4641 | 37 a = FFMAX(a - 64, 0); |
0 | 38 } |
39 return a; | |
40 } | |
41 | |
42 static inline int calc_lowcomp(int a, int b0, int b1, int bin) | |
43 { | |
44 if (bin < 7) { | |
4641 | 45 return calc_lowcomp1(a, b0, b1, 384); |
0 | 46 } else if (bin < 20) { |
4641 | 47 return calc_lowcomp1(a, b0, b1, 320); |
0 | 48 } else { |
4641 | 49 return FFMAX(a - 128, 0); |
0 | 50 } |
51 } | |
52 | |
53 /* AC3 bit allocation. The algorithm is the one described in the AC3 | |
782 | 54 spec. */ |
1064 | 55 void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap, |
56 int8_t *exp, int start, int end, | |
782 | 57 int snroffset, int fgain, int is_lfe, |
2967 | 58 int deltbae,int deltnseg, |
1064 | 59 uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba) |
0 | 60 { |
4641 | 61 int bin,i,j,k,end1,v,bndstrt,bndend,lowcomp,begin; |
0 | 62 int fastleak,slowleak,address,tmp; |
1064 | 63 int16_t psd[256]; /* scaled exponents */ |
64 int16_t bndpsd[50]; /* interpolated exponents */ | |
65 int16_t excite[50]; /* excitation */ | |
66 int16_t mask[50]; /* masking value */ | |
0 | 67 |
68 /* exponent mapping to PSD */ | |
69 for(bin=start;bin<end;bin++) { | |
70 psd[bin]=(3072 - (exp[bin] << 7)); | |
71 } | |
72 | |
73 /* PSD integration */ | |
74 j=start; | |
75 k=masktab[start]; | |
76 do { | |
77 v=psd[j]; | |
78 j++; | |
4641 | 79 end1 = FFMIN(bndtab[k+1], end); |
0 | 80 for(i=j;i<end1;i++) { |
81 /* logadd */ | |
4641 | 82 int adr = FFMIN(FFABS(v - psd[j]) >> 1, 255); |
83 v = FFMAX(v, psd[j]) + latab[adr]; | |
0 | 84 j++; |
85 } | |
86 bndpsd[k]=v; | |
87 k++; | |
88 } while (end > bndtab[k]); | |
89 | |
90 /* excitation function */ | |
91 bndstrt = masktab[start]; | |
92 bndend = masktab[end-1] + 1; | |
2967 | 93 |
782 | 94 if (bndstrt == 0) { |
95 lowcomp = 0; | |
4641 | 96 lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1], 384); |
4640 | 97 excite[0] = bndpsd[0] - fgain - lowcomp; |
4641 | 98 lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2], 384); |
4640 | 99 excite[1] = bndpsd[1] - fgain - lowcomp; |
100 begin = 7; | |
782 | 101 for (bin = 2; bin < 7; bin++) { |
102 if (!(is_lfe && bin == 6)) | |
4641 | 103 lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1], 384); |
4640 | 104 fastleak = bndpsd[bin] - fgain; |
105 slowleak = bndpsd[bin] - s->sgain; | |
106 excite[bin] = fastleak - lowcomp; | |
782 | 107 if (!(is_lfe && bin == 6)) { |
108 if (bndpsd[bin] <= bndpsd[bin+1]) { | |
4640 | 109 begin = bin + 1; |
110 break; | |
782 | 111 } |
112 } | |
113 } | |
2967 | 114 |
782 | 115 end1=bndend; |
116 if (end1 > 22) end1=22; | |
2967 | 117 |
782 | 118 for (bin = begin; bin < end1; bin++) { |
119 if (!(is_lfe && bin == 6)) | |
4640 | 120 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin); |
2967 | 121 |
4641 | 122 fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain); |
123 slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain); | |
124 excite[bin] = FFMAX(fastleak - lowcomp, slowleak); | |
782 | 125 } |
126 begin = 22; | |
127 } else { | |
128 /* coupling channel */ | |
129 begin = bndstrt; | |
2967 | 130 |
782 | 131 fastleak = (s->cplfleak << 8) + 768; |
132 slowleak = (s->cplsleak << 8) + 768; | |
0 | 133 } |
134 | |
782 | 135 for (bin = begin; bin < bndend; bin++) { |
4641 | 136 fastleak = FFMAX(fastleak - s->fdecay, bndpsd[bin] - fgain); |
137 slowleak = FFMAX(slowleak - s->sdecay, bndpsd[bin] - s->sgain); | |
138 excite[bin] = FFMAX(fastleak, slowleak); | |
0 | 139 } |
140 | |
141 /* compute masking curve */ | |
142 | |
143 for (bin = bndstrt; bin < bndend; bin++) { | |
144 tmp = s->dbknee - bndpsd[bin]; | |
145 if (tmp > 0) { | |
4641 | 146 excite[bin] += tmp >> 2; |
0 | 147 } |
4641 | 148 mask[bin] = FFMAX(hth[bin >> s->halfratecod][s->fscod], excite[bin]); |
0 | 149 } |
150 | |
782 | 151 /* delta bit allocation */ |
152 | |
153 if (deltbae == 0 || deltbae == 1) { | |
154 int band, seg, delta; | |
4640 | 155 band = 0; |
782 | 156 for (seg = 0; seg < deltnseg; seg++) { |
4640 | 157 band += deltoffst[seg]; |
782 | 158 if (deltba[seg] >= 4) { |
159 delta = (deltba[seg] - 3) << 7; | |
160 } else { | |
161 delta = (deltba[seg] - 4) << 7; | |
162 } | |
163 for (k = 0; k < deltlen[seg]; k++) { | |
4640 | 164 mask[band] += delta; |
165 band++; | |
782 | 166 } |
167 } | |
168 } | |
169 | |
0 | 170 /* compute bit allocation */ |
2967 | 171 |
4640 | 172 i = start; |
173 j = masktab[start]; | |
0 | 174 do { |
4641 | 175 v = (FFMAX(mask[j] - snroffset - s->floor, 0) & 0x1FE0) + s->floor; |
176 end1 = FFMIN(bndtab[j] + bndsz[j], end); | |
0 | 177 for (k = i; k < end1; k++) { |
4641 | 178 address = av_clip((psd[i] - v) >> 5, 0, 63); |
0 | 179 bap[i] = baptab[address]; |
180 i++; | |
181 } | |
4640 | 182 } while (end > bndtab[j++]); |
0 | 183 } |
184 | |
4645
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
185 /** |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
186 * Initializes some tables. |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
187 * note: This function must remain thread safe because it is called by the |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
188 * AVParser init code. |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
189 */ |
4197 | 190 void ac3_common_init(void) |
782 | 191 { |
192 int i, j, k, l, v; | |
193 /* compute bndtab and masktab from bandsz */ | |
194 k = 0; | |
195 l = 0; | |
196 for(i=0;i<50;i++) { | |
197 bndtab[i] = l; | |
198 v = bndsz[i]; | |
199 for(j=0;j<v;j++) masktab[k++]=i; | |
200 l += v; | |
201 } | |
3668
34d76180e5d0
Fix 2 bit allocation bugs. One fix enables using a higher bandwidth code. The other fixes an issue with floorcod=7.
jbr
parents:
3280
diff
changeset
|
202 bndtab[50] = l; |
4645
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
203 |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
204 /* generate ff_ac3_frame_sizes table */ |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
205 for(i=0; i<38; i++) { |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
206 int br = ff_ac3_bitratetab[i >> 1]; |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
207 ff_ac3_frame_sizes[i][0] = ( 2*br ); |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
208 ff_ac3_frame_sizes[i][1] = (320*br / 147) + (i & 1); |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
209 ff_ac3_frame_sizes[i][2] = ( 3*br ); |
056127e5df89
remove redundancy in AC-3 parser by using common tables from ac3tab.h
jbr
parents:
4642
diff
changeset
|
210 } |
782 | 211 } |
4648 | 212 |
213 int ff_ac3_parse_header(const uint8_t buf[7], AC3HeaderInfo *hdr) | |
214 { | |
215 GetBitContext gbc; | |
216 | |
217 memset(hdr, 0, sizeof(*hdr)); | |
218 | |
219 init_get_bits(&gbc, buf, 54); | |
220 | |
221 hdr->sync_word = get_bits(&gbc, 16); | |
222 if(hdr->sync_word != 0x0B77) | |
223 return -1; | |
224 | |
225 /* read ahead to bsid to make sure this is AC-3, not E-AC-3 */ | |
226 hdr->bsid = show_bits_long(&gbc, 29) & 0x1F; | |
227 if(hdr->bsid > 10) | |
228 return -2; | |
229 | |
230 hdr->crc1 = get_bits(&gbc, 16); | |
231 hdr->fscod = get_bits(&gbc, 2); | |
232 if(hdr->fscod == 3) | |
233 return -3; | |
234 | |
235 hdr->frmsizecod = get_bits(&gbc, 6); | |
236 if(hdr->frmsizecod > 37) | |
237 return -4; | |
238 | |
239 skip_bits(&gbc, 5); // skip bsid, already got it | |
240 | |
241 hdr->bsmod = get_bits(&gbc, 3); | |
242 hdr->acmod = get_bits(&gbc, 3); | |
243 if((hdr->acmod & 1) && hdr->acmod != 1) { | |
244 hdr->cmixlev = get_bits(&gbc, 2); | |
245 } | |
246 if(hdr->acmod & 4) { | |
247 hdr->surmixlev = get_bits(&gbc, 2); | |
248 } | |
249 if(hdr->acmod == 2) { | |
250 hdr->dsurmod = get_bits(&gbc, 2); | |
251 } | |
252 hdr->lfeon = get_bits1(&gbc); | |
253 | |
254 hdr->halfratecod = FFMAX(hdr->bsid, 8) - 8; | |
255 hdr->sample_rate = ff_ac3_freqs[hdr->fscod] >> hdr->halfratecod; | |
256 hdr->bit_rate = (ff_ac3_bitratetab[hdr->frmsizecod>>1] * 1000) >> hdr->halfratecod; | |
257 hdr->channels = ff_ac3_channels[hdr->acmod] + hdr->lfeon; | |
258 hdr->frame_size = ff_ac3_frame_sizes[hdr->frmsizecod][hdr->fscod] * 2; | |
259 | |
260 return 0; | |
261 } |