Mercurial > libavcodec.hg
annotate mpegaudio.c @ 2497:69adfbbdcdeb libavcodec
- samples from mplayer ftp in the "adv" profile seem to have profile=2,
which isn't the advanced one; and indeed, using adv. profile parser fails.
Using normal parser works, and that's what is done
- attempt at taking care of stride for NORM2 bitplane decoding
- duplication of much code from msmpeg4.c; this code isn't yet used, but
goes down as far as the block layer (mainly Transform Type stuff, the
remains are wild editing without checking). Unusable yet, and lacks the AC
decoding (but a step further in bitstream parsing)
patch by anonymous
| author | michael |
|---|---|
| date | Fri, 04 Feb 2005 02:20:38 +0000 |
| parents | 582e635cfa08 |
| children | e25782262d7d |
| rev | line source |
|---|---|
| 0 | 1 /* |
| 2 * The simplest mpeg audio layer 2 encoder | |
| 429 | 3 * Copyright (c) 2000, 2001 Fabrice Bellard. |
| 0 | 4 * |
| 429 | 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. | |
| 0 | 9 * |
| 429 | 10 * This library is distributed in the hope that it will be useful, |
| 0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 * Lesser General Public License for more details. | |
| 0 | 14 * |
| 429 | 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 | |
| 0 | 18 */ |
| 1106 | 19 |
| 20 /** | |
| 21 * @file mpegaudio.c | |
| 22 * The simplest mpeg audio layer 2 encoder. | |
| 23 */ | |
| 24 | |
| 64 | 25 #include "avcodec.h" |
|
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2281
diff
changeset
|
26 #include "bitstream.h" |
| 0 | 27 #include "mpegaudio.h" |
| 28 | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
29 /* currently, cannot change these constants (need to modify |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
30 quantization stage) */ |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
31 #define FRAC_BITS 15 |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
32 #define WFRAC_BITS 14 |
| 1064 | 33 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
34 #define FIX(a) ((int)((a) * (1 << FRAC_BITS))) |
| 84 | 35 |
| 36 #define SAMPLES_BUF_SIZE 4096 | |
| 37 | |
| 38 typedef struct MpegAudioContext { | |
| 39 PutBitContext pb; | |
| 40 int nb_channels; | |
| 41 int freq, bit_rate; | |
| 42 int lsf; /* 1 if mpeg2 low bitrate selected */ | |
| 43 int bitrate_index; /* bit rate */ | |
| 44 int freq_index; | |
| 45 int frame_size; /* frame size, in bits, without padding */ | |
| 1064 | 46 int64_t nb_samples; /* total number of samples encoded */ |
| 84 | 47 /* padding computation */ |
| 48 int frame_frac, frame_frac_incr, do_padding; | |
| 49 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */ | |
| 50 int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */ | |
| 51 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]; | |
| 52 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */ | |
| 53 /* code to group 3 scale factors */ | |
| 54 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; | |
| 55 int sblimit; /* number of used subbands */ | |
| 56 const unsigned char *alloc_table; | |
| 57 } MpegAudioContext; | |
| 58 | |
| 0 | 59 /* define it to use floats in quantization (I don't like floats !) */ |
| 60 //#define USE_FLOATS | |
| 61 | |
| 62 #include "mpegaudiotab.h" | |
| 63 | |
| 1057 | 64 static int MPA_encode_init(AVCodecContext *avctx) |
| 0 | 65 { |
| 66 MpegAudioContext *s = avctx->priv_data; | |
| 67 int freq = avctx->sample_rate; | |
| 68 int bitrate = avctx->bit_rate; | |
| 69 int channels = avctx->channels; | |
| 84 | 70 int i, v, table; |
| 0 | 71 float a; |
| 72 | |
| 73 if (channels > 2) | |
| 74 return -1; | |
| 75 bitrate = bitrate / 1000; | |
| 76 s->nb_channels = channels; | |
| 77 s->freq = freq; | |
| 78 s->bit_rate = bitrate * 1000; | |
| 79 avctx->frame_size = MPA_FRAME_SIZE; | |
| 80 | |
| 81 /* encoding freq */ | |
| 82 s->lsf = 0; | |
| 83 for(i=0;i<3;i++) { | |
| 84 | 84 if (mpa_freq_tab[i] == freq) |
| 0 | 85 break; |
| 84 | 86 if ((mpa_freq_tab[i] / 2) == freq) { |
| 0 | 87 s->lsf = 1; |
| 88 break; | |
| 89 } | |
| 90 } | |
| 2124 | 91 if (i == 3){ |
| 92 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq); | |
| 0 | 93 return -1; |
| 2124 | 94 } |
| 0 | 95 s->freq_index = i; |
| 96 | |
| 97 /* encoding bitrate & frequency */ | |
| 98 for(i=0;i<15;i++) { | |
| 84 | 99 if (mpa_bitrate_tab[s->lsf][1][i] == bitrate) |
| 0 | 100 break; |
| 101 } | |
| 2124 | 102 if (i == 15){ |
| 103 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate); | |
| 0 | 104 return -1; |
| 2124 | 105 } |
| 0 | 106 s->bitrate_index = i; |
| 107 | |
| 108 /* compute total header size & pad bit */ | |
| 109 | |
| 110 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0); | |
| 111 s->frame_size = ((int)a) * 8; | |
| 112 | |
| 113 /* frame fractional size to compute padding */ | |
| 114 s->frame_frac = 0; | |
| 115 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0); | |
| 116 | |
| 117 /* select the right allocation table */ | |
| 84 | 118 table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf); |
| 119 | |
| 0 | 120 /* number of used subbands */ |
| 121 s->sblimit = sblimit_table[table]; | |
| 122 s->alloc_table = alloc_tables[table]; | |
| 123 | |
| 124 #ifdef DEBUG | |
|
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
125 av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n", |
| 0 | 126 bitrate, freq, s->frame_size, table, s->frame_frac_incr); |
| 127 #endif | |
| 128 | |
| 129 for(i=0;i<s->nb_channels;i++) | |
| 130 s->samples_offset[i] = 0; | |
| 131 | |
| 84 | 132 for(i=0;i<257;i++) { |
| 133 int v; | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
134 v = mpa_enwindow[i]; |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
135 #if WFRAC_BITS != 16 |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
136 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
137 #endif |
| 84 | 138 filter_bank[i] = v; |
| 139 if ((i & 63) != 0) | |
| 140 v = -v; | |
| 141 if (i != 0) | |
| 142 filter_bank[512 - i] = v; | |
| 0 | 143 } |
| 84 | 144 |
| 0 | 145 for(i=0;i<64;i++) { |
| 146 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20)); | |
| 147 if (v <= 0) | |
| 148 v = 1; | |
| 149 scale_factor_table[i] = v; | |
| 150 #ifdef USE_FLOATS | |
| 151 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20); | |
| 152 #else | |
| 153 #define P 15 | |
| 154 scale_factor_shift[i] = 21 - P - (i / 3); | |
| 155 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0); | |
| 156 #endif | |
| 157 } | |
| 158 for(i=0;i<128;i++) { | |
| 159 v = i - 64; | |
| 160 if (v <= -3) | |
| 161 v = 0; | |
| 162 else if (v < 0) | |
| 163 v = 1; | |
| 164 else if (v == 0) | |
| 165 v = 2; | |
| 166 else if (v < 3) | |
| 167 v = 3; | |
| 168 else | |
| 169 v = 4; | |
| 170 scale_diff_table[i] = v; | |
| 171 } | |
| 172 | |
| 173 for(i=0;i<17;i++) { | |
| 174 v = quant_bits[i]; | |
| 175 if (v < 0) | |
| 176 v = -v; | |
| 177 else | |
| 178 v = v * 3; | |
| 179 total_quant_bits[i] = 12 * v; | |
| 180 } | |
| 181 | |
| 925 | 182 avctx->coded_frame= avcodec_alloc_frame(); |
| 183 avctx->coded_frame->key_frame= 1; | |
| 184 | |
| 0 | 185 return 0; |
| 186 } | |
| 187 | |
| 84 | 188 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */ |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
189 static void idct32(int *out, int *tab) |
| 0 | 190 { |
| 191 int i, j; | |
| 192 int *t, *t1, xr; | |
| 193 const int *xp = costab32; | |
| 194 | |
| 195 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2]; | |
| 196 | |
| 197 t = tab + 30; | |
| 198 t1 = tab + 2; | |
| 199 do { | |
| 200 t[0] += t[-4]; | |
| 201 t[1] += t[1 - 4]; | |
| 202 t -= 4; | |
| 203 } while (t != t1); | |
| 204 | |
| 205 t = tab + 28; | |
| 206 t1 = tab + 4; | |
| 207 do { | |
| 208 t[0] += t[-8]; | |
| 209 t[1] += t[1-8]; | |
| 210 t[2] += t[2-8]; | |
| 211 t[3] += t[3-8]; | |
| 212 t -= 8; | |
| 213 } while (t != t1); | |
| 214 | |
| 215 t = tab; | |
| 216 t1 = tab + 32; | |
| 217 do { | |
| 218 t[ 3] = -t[ 3]; | |
| 219 t[ 6] = -t[ 6]; | |
| 220 | |
| 221 t[11] = -t[11]; | |
| 222 t[12] = -t[12]; | |
| 223 t[13] = -t[13]; | |
| 224 t[15] = -t[15]; | |
| 225 t += 16; | |
| 226 } while (t != t1); | |
| 227 | |
| 228 | |
| 229 t = tab; | |
| 230 t1 = tab + 8; | |
| 231 do { | |
| 232 int x1, x2, x3, x4; | |
| 233 | |
| 234 x3 = MUL(t[16], FIX(SQRT2*0.5)); | |
| 235 x4 = t[0] - x3; | |
| 236 x3 = t[0] + x3; | |
| 237 | |
| 238 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5)); | |
| 239 x1 = MUL((t[8] - x2), xp[0]); | |
| 240 x2 = MUL((t[8] + x2), xp[1]); | |
| 241 | |
| 242 t[ 0] = x3 + x1; | |
| 243 t[ 8] = x4 - x2; | |
| 244 t[16] = x4 + x2; | |
| 245 t[24] = x3 - x1; | |
| 246 t++; | |
| 247 } while (t != t1); | |
| 248 | |
| 249 xp += 2; | |
| 250 t = tab; | |
| 251 t1 = tab + 4; | |
| 252 do { | |
| 253 xr = MUL(t[28],xp[0]); | |
| 254 t[28] = (t[0] - xr); | |
| 255 t[0] = (t[0] + xr); | |
| 256 | |
| 257 xr = MUL(t[4],xp[1]); | |
| 258 t[ 4] = (t[24] - xr); | |
| 259 t[24] = (t[24] + xr); | |
| 260 | |
| 261 xr = MUL(t[20],xp[2]); | |
| 262 t[20] = (t[8] - xr); | |
| 263 t[ 8] = (t[8] + xr); | |
| 264 | |
| 265 xr = MUL(t[12],xp[3]); | |
| 266 t[12] = (t[16] - xr); | |
| 267 t[16] = (t[16] + xr); | |
| 268 t++; | |
| 269 } while (t != t1); | |
| 270 xp += 4; | |
| 271 | |
| 272 for (i = 0; i < 4; i++) { | |
| 273 xr = MUL(tab[30-i*4],xp[0]); | |
| 274 tab[30-i*4] = (tab[i*4] - xr); | |
| 275 tab[ i*4] = (tab[i*4] + xr); | |
| 276 | |
| 277 xr = MUL(tab[ 2+i*4],xp[1]); | |
| 278 tab[ 2+i*4] = (tab[28-i*4] - xr); | |
| 279 tab[28-i*4] = (tab[28-i*4] + xr); | |
| 280 | |
| 281 xr = MUL(tab[31-i*4],xp[0]); | |
| 282 tab[31-i*4] = (tab[1+i*4] - xr); | |
| 283 tab[ 1+i*4] = (tab[1+i*4] + xr); | |
| 284 | |
| 285 xr = MUL(tab[ 3+i*4],xp[1]); | |
| 286 tab[ 3+i*4] = (tab[29-i*4] - xr); | |
| 287 tab[29-i*4] = (tab[29-i*4] + xr); | |
| 288 | |
| 289 xp += 2; | |
| 290 } | |
| 291 | |
| 292 t = tab + 30; | |
| 293 t1 = tab + 1; | |
| 294 do { | |
| 295 xr = MUL(t1[0], *xp); | |
| 296 t1[0] = (t[0] - xr); | |
| 297 t[0] = (t[0] + xr); | |
| 298 t -= 2; | |
| 299 t1 += 2; | |
| 300 xp++; | |
| 301 } while (t >= tab); | |
| 302 | |
| 303 for(i=0;i<32;i++) { | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
304 out[i] = tab[bitinv32[i]]; |
| 0 | 305 } |
| 306 } | |
| 307 | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
308 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS) |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
309 |
| 0 | 310 static void filter(MpegAudioContext *s, int ch, short *samples, int incr) |
| 311 { | |
| 312 short *p, *q; | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
313 int sum, offset, i, j; |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
314 int tmp[64]; |
| 0 | 315 int tmp1[32]; |
| 316 int *out; | |
| 317 | |
| 318 // print_pow1(samples, 1152); | |
| 319 | |
| 320 offset = s->samples_offset[ch]; | |
| 321 out = &s->sb_samples[ch][0][0][0]; | |
| 322 for(j=0;j<36;j++) { | |
| 323 /* 32 samples at once */ | |
| 324 for(i=0;i<32;i++) { | |
| 325 s->samples_buf[ch][offset + (31 - i)] = samples[0]; | |
| 326 samples += incr; | |
| 327 } | |
| 328 | |
| 329 /* filter */ | |
| 330 p = s->samples_buf[ch] + offset; | |
| 331 q = filter_bank; | |
| 332 /* maxsum = 23169 */ | |
| 333 for(i=0;i<64;i++) { | |
| 334 sum = p[0*64] * q[0*64]; | |
| 335 sum += p[1*64] * q[1*64]; | |
| 336 sum += p[2*64] * q[2*64]; | |
| 337 sum += p[3*64] * q[3*64]; | |
| 338 sum += p[4*64] * q[4*64]; | |
| 339 sum += p[5*64] * q[5*64]; | |
| 340 sum += p[6*64] * q[6*64]; | |
| 341 sum += p[7*64] * q[7*64]; | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
342 tmp[i] = sum; |
| 0 | 343 p++; |
| 344 q++; | |
| 345 } | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
346 tmp1[0] = tmp[16] >> WSHIFT; |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
347 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT; |
|
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
348 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT; |
| 0 | 349 |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
350 idct32(out, tmp1); |
| 0 | 351 |
| 352 /* advance of 32 samples */ | |
| 353 offset -= 32; | |
| 354 out += 32; | |
| 355 /* handle the wrap around */ | |
| 356 if (offset < 0) { | |
| 357 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32), | |
| 358 s->samples_buf[ch], (512 - 32) * 2); | |
| 359 offset = SAMPLES_BUF_SIZE - 512; | |
| 360 } | |
| 361 } | |
| 362 s->samples_offset[ch] = offset; | |
| 363 | |
| 364 // print_pow(s->sb_samples, 1152); | |
| 365 } | |
| 366 | |
| 367 static void compute_scale_factors(unsigned char scale_code[SBLIMIT], | |
| 368 unsigned char scale_factors[SBLIMIT][3], | |
| 369 int sb_samples[3][12][SBLIMIT], | |
| 370 int sblimit) | |
| 371 { | |
| 372 int *p, vmax, v, n, i, j, k, code; | |
| 373 int index, d1, d2; | |
| 374 unsigned char *sf = &scale_factors[0][0]; | |
| 375 | |
| 376 for(j=0;j<sblimit;j++) { | |
| 377 for(i=0;i<3;i++) { | |
| 378 /* find the max absolute value */ | |
| 379 p = &sb_samples[i][0][j]; | |
| 380 vmax = abs(*p); | |
| 381 for(k=1;k<12;k++) { | |
| 382 p += SBLIMIT; | |
| 383 v = abs(*p); | |
| 384 if (v > vmax) | |
| 385 vmax = v; | |
| 386 } | |
| 387 /* compute the scale factor index using log 2 computations */ | |
| 388 if (vmax > 0) { | |
| 70 | 389 n = av_log2(vmax); |
| 0 | 390 /* n is the position of the MSB of vmax. now |
| 391 use at most 2 compares to find the index */ | |
| 392 index = (21 - n) * 3 - 3; | |
| 393 if (index >= 0) { | |
| 394 while (vmax <= scale_factor_table[index+1]) | |
| 395 index++; | |
| 396 } else { | |
| 397 index = 0; /* very unlikely case of overflow */ | |
| 398 } | |
| 399 } else { | |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
400 index = 62; /* value 63 is not allowed */ |
| 0 | 401 } |
|
89
2e88e3afecd0
corrected mpeg audio encoding overflows - now it should give correct quality even for very high volumes
glantau
parents:
84
diff
changeset
|
402 |
| 0 | 403 #if 0 |
| 404 printf("%2d:%d in=%x %x %d\n", | |
| 405 j, i, vmax, scale_factor_table[index], index); | |
| 406 #endif | |
| 407 /* store the scale factor */ | |
| 408 assert(index >=0 && index <= 63); | |
| 409 sf[i] = index; | |
| 410 } | |
| 411 | |
| 412 /* compute the transmission factor : look if the scale factors | |
| 413 are close enough to each other */ | |
| 414 d1 = scale_diff_table[sf[0] - sf[1] + 64]; | |
| 415 d2 = scale_diff_table[sf[1] - sf[2] + 64]; | |
| 416 | |
| 417 /* handle the 25 cases */ | |
| 418 switch(d1 * 5 + d2) { | |
| 419 case 0*5+0: | |
| 420 case 0*5+4: | |
| 421 case 3*5+4: | |
| 422 case 4*5+0: | |
| 423 case 4*5+4: | |
| 424 code = 0; | |
| 425 break; | |
| 426 case 0*5+1: | |
| 427 case 0*5+2: | |
| 428 case 4*5+1: | |
| 429 case 4*5+2: | |
| 430 code = 3; | |
| 431 sf[2] = sf[1]; | |
| 432 break; | |
| 433 case 0*5+3: | |
| 434 case 4*5+3: | |
| 435 code = 3; | |
| 436 sf[1] = sf[2]; | |
| 437 break; | |
| 438 case 1*5+0: | |
| 439 case 1*5+4: | |
| 440 case 2*5+4: | |
| 441 code = 1; | |
| 442 sf[1] = sf[0]; | |
| 443 break; | |
| 444 case 1*5+1: | |
| 445 case 1*5+2: | |
| 446 case 2*5+0: | |
| 447 case 2*5+1: | |
| 448 case 2*5+2: | |
| 449 code = 2; | |
| 450 sf[1] = sf[2] = sf[0]; | |
| 451 break; | |
| 452 case 2*5+3: | |
| 453 case 3*5+3: | |
| 454 code = 2; | |
| 455 sf[0] = sf[1] = sf[2]; | |
| 456 break; | |
| 457 case 3*5+0: | |
| 458 case 3*5+1: | |
| 459 case 3*5+2: | |
| 460 code = 2; | |
| 461 sf[0] = sf[2] = sf[1]; | |
| 462 break; | |
| 463 case 1*5+3: | |
| 464 code = 2; | |
| 465 if (sf[0] > sf[2]) | |
| 466 sf[0] = sf[2]; | |
| 467 sf[1] = sf[2] = sf[0]; | |
| 468 break; | |
| 469 default: | |
| 2281 | 470 assert(0); //cant happen |
| 0 | 471 } |
| 472 | |
| 473 #if 0 | |
| 474 printf("%d: %2d %2d %2d %d %d -> %d\n", j, | |
| 475 sf[0], sf[1], sf[2], d1, d2, code); | |
| 476 #endif | |
| 477 scale_code[j] = code; | |
| 478 sf += 3; | |
| 479 } | |
| 480 } | |
| 481 | |
| 482 /* The most important function : psycho acoustic module. In this | |
| 483 encoder there is basically none, so this is the worst you can do, | |
| 484 but also this is the simpler. */ | |
| 485 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]) | |
| 486 { | |
| 487 int i; | |
| 488 | |
| 489 for(i=0;i<s->sblimit;i++) { | |
| 490 smr[i] = (int)(fixed_smr[i] * 10); | |
| 491 } | |
| 492 } | |
| 493 | |
| 494 | |
| 495 #define SB_NOTALLOCATED 0 | |
| 496 #define SB_ALLOCATED 1 | |
| 497 #define SB_NOMORE 2 | |
| 498 | |
| 499 /* Try to maximize the smr while using a number of bits inferior to | |
| 500 the frame size. I tried to make the code simpler, faster and | |
| 501 smaller than other encoders :-) */ | |
| 502 static void compute_bit_allocation(MpegAudioContext *s, | |
| 503 short smr1[MPA_MAX_CHANNELS][SBLIMIT], | |
| 504 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | |
| 505 int *padding) | |
| 506 { | |
| 507 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size; | |
| 508 int incr; | |
| 509 short smr[MPA_MAX_CHANNELS][SBLIMIT]; | |
| 510 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT]; | |
| 511 const unsigned char *alloc; | |
| 512 | |
| 513 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT); | |
| 514 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT); | |
| 515 memset(bit_alloc, 0, s->nb_channels * SBLIMIT); | |
| 516 | |
| 517 /* compute frame size and padding */ | |
| 518 max_frame_size = s->frame_size; | |
| 519 s->frame_frac += s->frame_frac_incr; | |
| 520 if (s->frame_frac >= 65536) { | |
| 521 s->frame_frac -= 65536; | |
| 522 s->do_padding = 1; | |
| 523 max_frame_size += 8; | |
| 524 } else { | |
| 525 s->do_padding = 0; | |
| 526 } | |
| 527 | |
| 528 /* compute the header + bit alloc size */ | |
| 529 current_frame_size = 32; | |
| 530 alloc = s->alloc_table; | |
| 531 for(i=0;i<s->sblimit;i++) { | |
| 532 incr = alloc[0]; | |
| 533 current_frame_size += incr * s->nb_channels; | |
| 534 alloc += 1 << incr; | |
| 535 } | |
| 536 for(;;) { | |
| 537 /* look for the subband with the largest signal to mask ratio */ | |
| 538 max_sb = -1; | |
| 539 max_ch = -1; | |
| 540 max_smr = 0x80000000; | |
| 541 for(ch=0;ch<s->nb_channels;ch++) { | |
| 542 for(i=0;i<s->sblimit;i++) { | |
| 543 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) { | |
| 544 max_smr = smr[ch][i]; | |
| 545 max_sb = i; | |
| 546 max_ch = ch; | |
| 547 } | |
| 548 } | |
| 549 } | |
| 550 #if 0 | |
| 551 printf("current=%d max=%d max_sb=%d alloc=%d\n", | |
| 552 current_frame_size, max_frame_size, max_sb, | |
| 553 bit_alloc[max_sb]); | |
| 554 #endif | |
| 555 if (max_sb < 0) | |
| 556 break; | |
| 557 | |
| 558 /* find alloc table entry (XXX: not optimal, should use | |
| 559 pointer table) */ | |
| 560 alloc = s->alloc_table; | |
| 561 for(i=0;i<max_sb;i++) { | |
| 562 alloc += 1 << alloc[0]; | |
| 563 } | |
| 564 | |
| 565 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) { | |
| 566 /* nothing was coded for this band: add the necessary bits */ | |
| 567 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6; | |
| 568 incr += total_quant_bits[alloc[1]]; | |
| 569 } else { | |
| 570 /* increments bit allocation */ | |
| 571 b = bit_alloc[max_ch][max_sb]; | |
| 572 incr = total_quant_bits[alloc[b + 1]] - | |
| 573 total_quant_bits[alloc[b]]; | |
| 574 } | |
| 575 | |
| 576 if (current_frame_size + incr <= max_frame_size) { | |
| 577 /* can increase size */ | |
| 578 b = ++bit_alloc[max_ch][max_sb]; | |
| 579 current_frame_size += incr; | |
| 580 /* decrease smr by the resolution we added */ | |
| 581 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]]; | |
| 582 /* max allocation size reached ? */ | |
| 583 if (b == ((1 << alloc[0]) - 1)) | |
| 584 subband_status[max_ch][max_sb] = SB_NOMORE; | |
| 585 else | |
| 586 subband_status[max_ch][max_sb] = SB_ALLOCATED; | |
| 587 } else { | |
| 588 /* cannot increase the size of this subband */ | |
| 589 subband_status[max_ch][max_sb] = SB_NOMORE; | |
| 590 } | |
| 591 } | |
| 592 *padding = max_frame_size - current_frame_size; | |
| 593 assert(*padding >= 0); | |
| 594 | |
| 595 #if 0 | |
| 596 for(i=0;i<s->sblimit;i++) { | |
| 597 printf("%d ", bit_alloc[i]); | |
| 598 } | |
| 599 printf("\n"); | |
| 600 #endif | |
| 601 } | |
| 602 | |
| 603 /* | |
| 604 * Output the mpeg audio layer 2 frame. Note how the code is small | |
| 605 * compared to other encoders :-) | |
| 606 */ | |
| 607 static void encode_frame(MpegAudioContext *s, | |
| 608 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], | |
| 609 int padding) | |
| 610 { | |
| 611 int i, j, k, l, bit_alloc_bits, b, ch; | |
| 612 unsigned char *sf; | |
| 613 int q[3]; | |
| 614 PutBitContext *p = &s->pb; | |
| 615 | |
| 616 /* header */ | |
| 617 | |
| 618 put_bits(p, 12, 0xfff); | |
| 619 put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ | |
| 620 put_bits(p, 2, 4-2); /* layer 2 */ | |
| 621 put_bits(p, 1, 1); /* no error protection */ | |
| 622 put_bits(p, 4, s->bitrate_index); | |
| 623 put_bits(p, 2, s->freq_index); | |
| 624 put_bits(p, 1, s->do_padding); /* use padding */ | |
| 625 put_bits(p, 1, 0); /* private_bit */ | |
| 626 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO); | |
| 627 put_bits(p, 2, 0); /* mode_ext */ | |
| 628 put_bits(p, 1, 0); /* no copyright */ | |
| 629 put_bits(p, 1, 1); /* original */ | |
| 630 put_bits(p, 2, 0); /* no emphasis */ | |
| 631 | |
| 632 /* bit allocation */ | |
| 633 j = 0; | |
| 634 for(i=0;i<s->sblimit;i++) { | |
| 635 bit_alloc_bits = s->alloc_table[j]; | |
| 636 for(ch=0;ch<s->nb_channels;ch++) { | |
| 637 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); | |
| 638 } | |
| 639 j += 1 << bit_alloc_bits; | |
| 640 } | |
| 641 | |
| 642 /* scale codes */ | |
| 643 for(i=0;i<s->sblimit;i++) { | |
| 644 for(ch=0;ch<s->nb_channels;ch++) { | |
| 645 if (bit_alloc[ch][i]) | |
| 646 put_bits(p, 2, s->scale_code[ch][i]); | |
| 647 } | |
| 648 } | |
| 649 | |
| 650 /* scale factors */ | |
| 651 for(i=0;i<s->sblimit;i++) { | |
| 652 for(ch=0;ch<s->nb_channels;ch++) { | |
| 653 if (bit_alloc[ch][i]) { | |
| 654 sf = &s->scale_factors[ch][i][0]; | |
| 655 switch(s->scale_code[ch][i]) { | |
| 656 case 0: | |
| 657 put_bits(p, 6, sf[0]); | |
| 658 put_bits(p, 6, sf[1]); | |
| 659 put_bits(p, 6, sf[2]); | |
| 660 break; | |
| 661 case 3: | |
| 662 case 1: | |
| 663 put_bits(p, 6, sf[0]); | |
| 664 put_bits(p, 6, sf[2]); | |
| 665 break; | |
| 666 case 2: | |
| 667 put_bits(p, 6, sf[0]); | |
| 668 break; | |
| 669 } | |
| 670 } | |
| 671 } | |
| 672 } | |
| 673 | |
| 674 /* quantization & write sub band samples */ | |
| 675 | |
| 676 for(k=0;k<3;k++) { | |
| 677 for(l=0;l<12;l+=3) { | |
| 678 j = 0; | |
| 679 for(i=0;i<s->sblimit;i++) { | |
| 680 bit_alloc_bits = s->alloc_table[j]; | |
| 681 for(ch=0;ch<s->nb_channels;ch++) { | |
| 682 b = bit_alloc[ch][i]; | |
| 683 if (b) { | |
| 684 int qindex, steps, m, sample, bits; | |
| 685 /* we encode 3 sub band samples of the same sub band at a time */ | |
| 686 qindex = s->alloc_table[j+b]; | |
| 687 steps = quant_steps[qindex]; | |
| 688 for(m=0;m<3;m++) { | |
| 689 sample = s->sb_samples[ch][k][l + m][i]; | |
| 690 /* divide by scale factor */ | |
| 691 #ifdef USE_FLOATS | |
| 692 { | |
| 693 float a; | |
| 694 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]]; | |
| 695 q[m] = (int)((a + 1.0) * steps * 0.5); | |
| 696 } | |
| 697 #else | |
| 698 { | |
| 699 int q1, e, shift, mult; | |
| 700 e = s->scale_factors[ch][i][k]; | |
| 701 shift = scale_factor_shift[e]; | |
| 702 mult = scale_factor_mult[e]; | |
| 703 | |
| 704 /* normalize to P bits */ | |
| 705 if (shift < 0) | |
| 706 q1 = sample << (-shift); | |
| 707 else | |
| 708 q1 = sample >> shift; | |
| 709 q1 = (q1 * mult) >> P; | |
| 710 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1); | |
| 711 } | |
| 712 #endif | |
| 713 if (q[m] >= steps) | |
| 714 q[m] = steps - 1; | |
| 715 assert(q[m] >= 0 && q[m] < steps); | |
| 716 } | |
| 717 bits = quant_bits[qindex]; | |
| 718 if (bits < 0) { | |
| 719 /* group the 3 values to save bits */ | |
| 720 put_bits(p, -bits, | |
| 721 q[0] + steps * (q[1] + steps * q[2])); | |
| 722 #if 0 | |
| 723 printf("%d: gr1 %d\n", | |
| 724 i, q[0] + steps * (q[1] + steps * q[2])); | |
| 725 #endif | |
| 726 } else { | |
| 727 #if 0 | |
| 728 printf("%d: gr3 %d %d %d\n", | |
| 729 i, q[0], q[1], q[2]); | |
| 730 #endif | |
| 731 put_bits(p, bits, q[0]); | |
| 732 put_bits(p, bits, q[1]); | |
| 733 put_bits(p, bits, q[2]); | |
| 734 } | |
| 735 } | |
| 736 } | |
| 737 /* next subband in alloc table */ | |
| 738 j += 1 << bit_alloc_bits; | |
| 739 } | |
| 740 } | |
| 741 } | |
| 742 | |
| 743 /* padding */ | |
| 744 for(i=0;i<padding;i++) | |
| 745 put_bits(p, 1, 0); | |
| 746 | |
| 747 /* flush */ | |
| 748 flush_put_bits(p); | |
| 749 } | |
| 750 | |
| 1057 | 751 static int MPA_encode_frame(AVCodecContext *avctx, |
| 752 unsigned char *frame, int buf_size, void *data) | |
| 0 | 753 { |
| 754 MpegAudioContext *s = avctx->priv_data; | |
| 755 short *samples = data; | |
| 756 short smr[MPA_MAX_CHANNELS][SBLIMIT]; | |
| 757 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; | |
| 758 int padding, i; | |
| 759 | |
| 760 for(i=0;i<s->nb_channels;i++) { | |
| 761 filter(s, i, samples + i, s->nb_channels); | |
| 762 } | |
| 763 | |
| 764 for(i=0;i<s->nb_channels;i++) { | |
| 765 compute_scale_factors(s->scale_code[i], s->scale_factors[i], | |
| 766 s->sb_samples[i], s->sblimit); | |
| 767 } | |
| 768 for(i=0;i<s->nb_channels;i++) { | |
| 769 psycho_acoustic_model(s, smr[i]); | |
| 770 } | |
| 771 compute_bit_allocation(s, smr, bit_alloc, &padding); | |
| 772 | |
|
1522
79dddc5cd990
removed the obsolete and unused parameters of init_put_bits
alex
parents:
1106
diff
changeset
|
773 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE); |
| 0 | 774 |
| 775 encode_frame(s, bit_alloc, padding); | |
| 776 | |
| 777 s->nb_samples += MPA_FRAME_SIZE; | |
|
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
89
diff
changeset
|
778 return pbBufPtr(&s->pb) - s->pb.buf; |
| 0 | 779 } |
| 780 | |
| 925 | 781 static int MPA_encode_close(AVCodecContext *avctx) |
| 782 { | |
| 783 av_freep(&avctx->coded_frame); | |
|
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
925
diff
changeset
|
784 return 0; |
| 925 | 785 } |
| 0 | 786 |
| 787 AVCodec mp2_encoder = { | |
| 788 "mp2", | |
| 789 CODEC_TYPE_AUDIO, | |
| 790 CODEC_ID_MP2, | |
| 791 sizeof(MpegAudioContext), | |
| 792 MPA_encode_init, | |
| 793 MPA_encode_frame, | |
| 925 | 794 MPA_encode_close, |
| 0 | 795 NULL, |
| 796 }; | |
|
440
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
797 |
|
000aeeac27a2
* started to cleanup name clashes for onetime compilation
kabi
parents:
429
diff
changeset
|
798 #undef FIX |
