00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/audioconvert.h"
00028
00029 #include "avcodec.h"
00030 #include "internal.h"
00031 #include "put_bits.h"
00032
00033 #define FRAC_BITS 15
00034 #define WFRAC_BITS 14
00035
00036 #include "mpegaudio.h"
00037 #include "mpegaudiodsp.h"
00038
00039
00040
00041 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
00042
00043 #define SAMPLES_BUF_SIZE 4096
00044
00045 typedef struct MpegAudioContext {
00046 PutBitContext pb;
00047 int nb_channels;
00048 int lsf;
00049 int bitrate_index;
00050 int freq_index;
00051 int frame_size;
00052
00053 int frame_frac, frame_frac_incr, do_padding;
00054 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE];
00055 int samples_offset[MPA_MAX_CHANNELS];
00056 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
00057 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3];
00058
00059 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
00060 int sblimit;
00061 const unsigned char *alloc_table;
00062 } MpegAudioContext;
00063
00064
00065 #define USE_FLOATS
00066
00067 #include "mpegaudiodata.h"
00068 #include "mpegaudiotab.h"
00069
00070 static av_cold int MPA_encode_init(AVCodecContext *avctx)
00071 {
00072 MpegAudioContext *s = avctx->priv_data;
00073 int freq = avctx->sample_rate;
00074 int bitrate = avctx->bit_rate;
00075 int channels = avctx->channels;
00076 int i, v, table;
00077 float a;
00078
00079 if (channels <= 0 || channels > 2){
00080 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
00081 return AVERROR(EINVAL);
00082 }
00083 bitrate = bitrate / 1000;
00084 s->nb_channels = channels;
00085 avctx->frame_size = MPA_FRAME_SIZE;
00086 avctx->delay = 512 - 32 + 1;
00087
00088
00089 s->lsf = 0;
00090 for(i=0;i<3;i++) {
00091 if (avpriv_mpa_freq_tab[i] == freq)
00092 break;
00093 if ((avpriv_mpa_freq_tab[i] / 2) == freq) {
00094 s->lsf = 1;
00095 break;
00096 }
00097 }
00098 if (i == 3){
00099 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
00100 return AVERROR(EINVAL);
00101 }
00102 s->freq_index = i;
00103
00104
00105 for(i=0;i<15;i++) {
00106 if (avpriv_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
00107 break;
00108 }
00109 if (i == 15){
00110 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
00111 return AVERROR(EINVAL);
00112 }
00113 s->bitrate_index = i;
00114
00115
00116
00117 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
00118 s->frame_size = ((int)a) * 8;
00119
00120
00121 s->frame_frac = 0;
00122 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
00123
00124
00125 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
00126
00127
00128 s->sblimit = ff_mpa_sblimit_table[table];
00129 s->alloc_table = ff_mpa_alloc_tables[table];
00130
00131 av_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
00132 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
00133
00134 for(i=0;i<s->nb_channels;i++)
00135 s->samples_offset[i] = 0;
00136
00137 for(i=0;i<257;i++) {
00138 int v;
00139 v = ff_mpa_enwindow[i];
00140 #if WFRAC_BITS != 16
00141 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
00142 #endif
00143 filter_bank[i] = v;
00144 if ((i & 63) != 0)
00145 v = -v;
00146 if (i != 0)
00147 filter_bank[512 - i] = v;
00148 }
00149
00150 for(i=0;i<64;i++) {
00151 v = (int)(exp2((3 - i) / 3.0) * (1 << 20));
00152 if (v <= 0)
00153 v = 1;
00154 scale_factor_table[i] = v;
00155 #ifdef USE_FLOATS
00156 scale_factor_inv_table[i] = exp2(-(3 - i) / 3.0) / (float)(1 << 20);
00157 #else
00158 #define P 15
00159 scale_factor_shift[i] = 21 - P - (i / 3);
00160 scale_factor_mult[i] = (1 << P) * exp2((i % 3) / 3.0);
00161 #endif
00162 }
00163 for(i=0;i<128;i++) {
00164 v = i - 64;
00165 if (v <= -3)
00166 v = 0;
00167 else if (v < 0)
00168 v = 1;
00169 else if (v == 0)
00170 v = 2;
00171 else if (v < 3)
00172 v = 3;
00173 else
00174 v = 4;
00175 scale_diff_table[i] = v;
00176 }
00177
00178 for(i=0;i<17;i++) {
00179 v = ff_mpa_quant_bits[i];
00180 if (v < 0)
00181 v = -v;
00182 else
00183 v = v * 3;
00184 total_quant_bits[i] = 12 * v;
00185 }
00186
00187 #if FF_API_OLD_ENCODE_AUDIO
00188 avctx->coded_frame= avcodec_alloc_frame();
00189 if (!avctx->coded_frame)
00190 return AVERROR(ENOMEM);
00191 #endif
00192
00193 return 0;
00194 }
00195
00196
00197 static void idct32(int *out, int *tab)
00198 {
00199 int i, j;
00200 int *t, *t1, xr;
00201 const int *xp = costab32;
00202
00203 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
00204
00205 t = tab + 30;
00206 t1 = tab + 2;
00207 do {
00208 t[0] += t[-4];
00209 t[1] += t[1 - 4];
00210 t -= 4;
00211 } while (t != t1);
00212
00213 t = tab + 28;
00214 t1 = tab + 4;
00215 do {
00216 t[0] += t[-8];
00217 t[1] += t[1-8];
00218 t[2] += t[2-8];
00219 t[3] += t[3-8];
00220 t -= 8;
00221 } while (t != t1);
00222
00223 t = tab;
00224 t1 = tab + 32;
00225 do {
00226 t[ 3] = -t[ 3];
00227 t[ 6] = -t[ 6];
00228
00229 t[11] = -t[11];
00230 t[12] = -t[12];
00231 t[13] = -t[13];
00232 t[15] = -t[15];
00233 t += 16;
00234 } while (t != t1);
00235
00236
00237 t = tab;
00238 t1 = tab + 8;
00239 do {
00240 int x1, x2, x3, x4;
00241
00242 x3 = MUL(t[16], FIX(SQRT2*0.5));
00243 x4 = t[0] - x3;
00244 x3 = t[0] + x3;
00245
00246 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
00247 x1 = MUL((t[8] - x2), xp[0]);
00248 x2 = MUL((t[8] + x2), xp[1]);
00249
00250 t[ 0] = x3 + x1;
00251 t[ 8] = x4 - x2;
00252 t[16] = x4 + x2;
00253 t[24] = x3 - x1;
00254 t++;
00255 } while (t != t1);
00256
00257 xp += 2;
00258 t = tab;
00259 t1 = tab + 4;
00260 do {
00261 xr = MUL(t[28],xp[0]);
00262 t[28] = (t[0] - xr);
00263 t[0] = (t[0] + xr);
00264
00265 xr = MUL(t[4],xp[1]);
00266 t[ 4] = (t[24] - xr);
00267 t[24] = (t[24] + xr);
00268
00269 xr = MUL(t[20],xp[2]);
00270 t[20] = (t[8] - xr);
00271 t[ 8] = (t[8] + xr);
00272
00273 xr = MUL(t[12],xp[3]);
00274 t[12] = (t[16] - xr);
00275 t[16] = (t[16] + xr);
00276 t++;
00277 } while (t != t1);
00278 xp += 4;
00279
00280 for (i = 0; i < 4; i++) {
00281 xr = MUL(tab[30-i*4],xp[0]);
00282 tab[30-i*4] = (tab[i*4] - xr);
00283 tab[ i*4] = (tab[i*4] + xr);
00284
00285 xr = MUL(tab[ 2+i*4],xp[1]);
00286 tab[ 2+i*4] = (tab[28-i*4] - xr);
00287 tab[28-i*4] = (tab[28-i*4] + xr);
00288
00289 xr = MUL(tab[31-i*4],xp[0]);
00290 tab[31-i*4] = (tab[1+i*4] - xr);
00291 tab[ 1+i*4] = (tab[1+i*4] + xr);
00292
00293 xr = MUL(tab[ 3+i*4],xp[1]);
00294 tab[ 3+i*4] = (tab[29-i*4] - xr);
00295 tab[29-i*4] = (tab[29-i*4] + xr);
00296
00297 xp += 2;
00298 }
00299
00300 t = tab + 30;
00301 t1 = tab + 1;
00302 do {
00303 xr = MUL(t1[0], *xp);
00304 t1[0] = (t[0] - xr);
00305 t[0] = (t[0] + xr);
00306 t -= 2;
00307 t1 += 2;
00308 xp++;
00309 } while (t >= tab);
00310
00311 for(i=0;i<32;i++) {
00312 out[i] = tab[bitinv32[i]];
00313 }
00314 }
00315
00316 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
00317
00318 static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
00319 {
00320 short *p, *q;
00321 int sum, offset, i, j;
00322 int tmp[64];
00323 int tmp1[32];
00324 int *out;
00325
00326 offset = s->samples_offset[ch];
00327 out = &s->sb_samples[ch][0][0][0];
00328 for(j=0;j<36;j++) {
00329
00330 for(i=0;i<32;i++) {
00331 s->samples_buf[ch][offset + (31 - i)] = samples[0];
00332 samples += incr;
00333 }
00334
00335
00336 p = s->samples_buf[ch] + offset;
00337 q = filter_bank;
00338
00339 for(i=0;i<64;i++) {
00340 sum = p[0*64] * q[0*64];
00341 sum += p[1*64] * q[1*64];
00342 sum += p[2*64] * q[2*64];
00343 sum += p[3*64] * q[3*64];
00344 sum += p[4*64] * q[4*64];
00345 sum += p[5*64] * q[5*64];
00346 sum += p[6*64] * q[6*64];
00347 sum += p[7*64] * q[7*64];
00348 tmp[i] = sum;
00349 p++;
00350 q++;
00351 }
00352 tmp1[0] = tmp[16] >> WSHIFT;
00353 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
00354 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
00355
00356 idct32(out, tmp1);
00357
00358
00359 offset -= 32;
00360 out += 32;
00361
00362 if (offset < 0) {
00363 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
00364 s->samples_buf[ch], (512 - 32) * 2);
00365 offset = SAMPLES_BUF_SIZE - 512;
00366 }
00367 }
00368 s->samples_offset[ch] = offset;
00369 }
00370
00371 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
00372 unsigned char scale_factors[SBLIMIT][3],
00373 int sb_samples[3][12][SBLIMIT],
00374 int sblimit)
00375 {
00376 int *p, vmax, v, n, i, j, k, code;
00377 int index, d1, d2;
00378 unsigned char *sf = &scale_factors[0][0];
00379
00380 for(j=0;j<sblimit;j++) {
00381 for(i=0;i<3;i++) {
00382
00383 p = &sb_samples[i][0][j];
00384 vmax = abs(*p);
00385 for(k=1;k<12;k++) {
00386 p += SBLIMIT;
00387 v = abs(*p);
00388 if (v > vmax)
00389 vmax = v;
00390 }
00391
00392 if (vmax > 1) {
00393 n = av_log2(vmax);
00394
00395
00396 index = (21 - n) * 3 - 3;
00397 if (index >= 0) {
00398 while (vmax <= scale_factor_table[index+1])
00399 index++;
00400 } else {
00401 index = 0;
00402 }
00403 } else {
00404 index = 62;
00405 }
00406
00407 av_dlog(NULL, "%2d:%d in=%x %x %d\n",
00408 j, i, vmax, scale_factor_table[index], index);
00409
00410 av_assert2(index >=0 && index <= 63);
00411 sf[i] = index;
00412 }
00413
00414
00415
00416 d1 = scale_diff_table[sf[0] - sf[1] + 64];
00417 d2 = scale_diff_table[sf[1] - sf[2] + 64];
00418
00419
00420 switch(d1 * 5 + d2) {
00421 case 0*5+0:
00422 case 0*5+4:
00423 case 3*5+4:
00424 case 4*5+0:
00425 case 4*5+4:
00426 code = 0;
00427 break;
00428 case 0*5+1:
00429 case 0*5+2:
00430 case 4*5+1:
00431 case 4*5+2:
00432 code = 3;
00433 sf[2] = sf[1];
00434 break;
00435 case 0*5+3:
00436 case 4*5+3:
00437 code = 3;
00438 sf[1] = sf[2];
00439 break;
00440 case 1*5+0:
00441 case 1*5+4:
00442 case 2*5+4:
00443 code = 1;
00444 sf[1] = sf[0];
00445 break;
00446 case 1*5+1:
00447 case 1*5+2:
00448 case 2*5+0:
00449 case 2*5+1:
00450 case 2*5+2:
00451 code = 2;
00452 sf[1] = sf[2] = sf[0];
00453 break;
00454 case 2*5+3:
00455 case 3*5+3:
00456 code = 2;
00457 sf[0] = sf[1] = sf[2];
00458 break;
00459 case 3*5+0:
00460 case 3*5+1:
00461 case 3*5+2:
00462 code = 2;
00463 sf[0] = sf[2] = sf[1];
00464 break;
00465 case 1*5+3:
00466 code = 2;
00467 if (sf[0] > sf[2])
00468 sf[0] = sf[2];
00469 sf[1] = sf[2] = sf[0];
00470 break;
00471 default:
00472 av_assert2(0);
00473 code = 0;
00474 }
00475
00476 av_dlog(NULL, "%d: %2d %2d %2d %d %d -> %d\n", j,
00477 sf[0], sf[1], sf[2], d1, d2, code);
00478 scale_code[j] = code;
00479 sf += 3;
00480 }
00481 }
00482
00483
00484
00485
00486 static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
00487 {
00488 int i;
00489
00490 for(i=0;i<s->sblimit;i++) {
00491 smr[i] = (int)(fixed_smr[i] * 10);
00492 }
00493 }
00494
00495
00496 #define SB_NOTALLOCATED 0
00497 #define SB_ALLOCATED 1
00498 #define SB_NOMORE 2
00499
00500
00501
00502
00503 static void compute_bit_allocation(MpegAudioContext *s,
00504 short smr1[MPA_MAX_CHANNELS][SBLIMIT],
00505 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
00506 int *padding)
00507 {
00508 int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
00509 int incr;
00510 short smr[MPA_MAX_CHANNELS][SBLIMIT];
00511 unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
00512 const unsigned char *alloc;
00513
00514 memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
00515 memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
00516 memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
00517
00518
00519 max_frame_size = s->frame_size;
00520 s->frame_frac += s->frame_frac_incr;
00521 if (s->frame_frac >= 65536) {
00522 s->frame_frac -= 65536;
00523 s->do_padding = 1;
00524 max_frame_size += 8;
00525 } else {
00526 s->do_padding = 0;
00527 }
00528
00529
00530 current_frame_size = 32;
00531 alloc = s->alloc_table;
00532 for(i=0;i<s->sblimit;i++) {
00533 incr = alloc[0];
00534 current_frame_size += incr * s->nb_channels;
00535 alloc += 1 << incr;
00536 }
00537 for(;;) {
00538
00539 max_sb = -1;
00540 max_ch = -1;
00541 max_smr = INT_MIN;
00542 for(ch=0;ch<s->nb_channels;ch++) {
00543 for(i=0;i<s->sblimit;i++) {
00544 if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
00545 max_smr = smr[ch][i];
00546 max_sb = i;
00547 max_ch = ch;
00548 }
00549 }
00550 }
00551 if (max_sb < 0)
00552 break;
00553 av_dlog(NULL, "current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
00554 current_frame_size, max_frame_size, max_sb, max_ch,
00555 bit_alloc[max_ch][max_sb]);
00556
00557
00558
00559 alloc = s->alloc_table;
00560 for(i=0;i<max_sb;i++) {
00561 alloc += 1 << alloc[0];
00562 }
00563
00564 if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
00565
00566 incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
00567 incr += total_quant_bits[alloc[1]];
00568 } else {
00569
00570 b = bit_alloc[max_ch][max_sb];
00571 incr = total_quant_bits[alloc[b + 1]] -
00572 total_quant_bits[alloc[b]];
00573 }
00574
00575 if (current_frame_size + incr <= max_frame_size) {
00576
00577 b = ++bit_alloc[max_ch][max_sb];
00578 current_frame_size += incr;
00579
00580 smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
00581
00582 if (b == ((1 << alloc[0]) - 1))
00583 subband_status[max_ch][max_sb] = SB_NOMORE;
00584 else
00585 subband_status[max_ch][max_sb] = SB_ALLOCATED;
00586 } else {
00587
00588 subband_status[max_ch][max_sb] = SB_NOMORE;
00589 }
00590 }
00591 *padding = max_frame_size - current_frame_size;
00592 av_assert0(*padding >= 0);
00593 }
00594
00595
00596
00597
00598
00599 static void encode_frame(MpegAudioContext *s,
00600 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
00601 int padding)
00602 {
00603 int i, j, k, l, bit_alloc_bits, b, ch;
00604 unsigned char *sf;
00605 int q[3];
00606 PutBitContext *p = &s->pb;
00607
00608
00609
00610 put_bits(p, 12, 0xfff);
00611 put_bits(p, 1, 1 - s->lsf);
00612 put_bits(p, 2, 4-2);
00613 put_bits(p, 1, 1);
00614 put_bits(p, 4, s->bitrate_index);
00615 put_bits(p, 2, s->freq_index);
00616 put_bits(p, 1, s->do_padding);
00617 put_bits(p, 1, 0);
00618 put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
00619 put_bits(p, 2, 0);
00620 put_bits(p, 1, 0);
00621 put_bits(p, 1, 1);
00622 put_bits(p, 2, 0);
00623
00624
00625 j = 0;
00626 for(i=0;i<s->sblimit;i++) {
00627 bit_alloc_bits = s->alloc_table[j];
00628 for(ch=0;ch<s->nb_channels;ch++) {
00629 put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
00630 }
00631 j += 1 << bit_alloc_bits;
00632 }
00633
00634
00635 for(i=0;i<s->sblimit;i++) {
00636 for(ch=0;ch<s->nb_channels;ch++) {
00637 if (bit_alloc[ch][i])
00638 put_bits(p, 2, s->scale_code[ch][i]);
00639 }
00640 }
00641
00642
00643 for(i=0;i<s->sblimit;i++) {
00644 for(ch=0;ch<s->nb_channels;ch++) {
00645 if (bit_alloc[ch][i]) {
00646 sf = &s->scale_factors[ch][i][0];
00647 switch(s->scale_code[ch][i]) {
00648 case 0:
00649 put_bits(p, 6, sf[0]);
00650 put_bits(p, 6, sf[1]);
00651 put_bits(p, 6, sf[2]);
00652 break;
00653 case 3:
00654 case 1:
00655 put_bits(p, 6, sf[0]);
00656 put_bits(p, 6, sf[2]);
00657 break;
00658 case 2:
00659 put_bits(p, 6, sf[0]);
00660 break;
00661 }
00662 }
00663 }
00664 }
00665
00666
00667
00668 for(k=0;k<3;k++) {
00669 for(l=0;l<12;l+=3) {
00670 j = 0;
00671 for(i=0;i<s->sblimit;i++) {
00672 bit_alloc_bits = s->alloc_table[j];
00673 for(ch=0;ch<s->nb_channels;ch++) {
00674 b = bit_alloc[ch][i];
00675 if (b) {
00676 int qindex, steps, m, sample, bits;
00677
00678 qindex = s->alloc_table[j+b];
00679 steps = ff_mpa_quant_steps[qindex];
00680 for(m=0;m<3;m++) {
00681 sample = s->sb_samples[ch][k][l + m][i];
00682
00683 #ifdef USE_FLOATS
00684 {
00685 float a;
00686 a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
00687 q[m] = (int)((a + 1.0) * steps * 0.5);
00688 }
00689 #else
00690 {
00691 int q1, e, shift, mult;
00692 e = s->scale_factors[ch][i][k];
00693 shift = scale_factor_shift[e];
00694 mult = scale_factor_mult[e];
00695
00696
00697 if (shift < 0)
00698 q1 = sample << (-shift);
00699 else
00700 q1 = sample >> shift;
00701 q1 = (q1 * mult) >> P;
00702 q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
00703 }
00704 #endif
00705 if (q[m] >= steps)
00706 q[m] = steps - 1;
00707 av_assert2(q[m] >= 0 && q[m] < steps);
00708 }
00709 bits = ff_mpa_quant_bits[qindex];
00710 if (bits < 0) {
00711
00712 put_bits(p, -bits,
00713 q[0] + steps * (q[1] + steps * q[2]));
00714 } else {
00715 put_bits(p, bits, q[0]);
00716 put_bits(p, bits, q[1]);
00717 put_bits(p, bits, q[2]);
00718 }
00719 }
00720 }
00721
00722 j += 1 << bit_alloc_bits;
00723 }
00724 }
00725 }
00726
00727
00728 for(i=0;i<padding;i++)
00729 put_bits(p, 1, 0);
00730
00731
00732 flush_put_bits(p);
00733 }
00734
00735 static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
00736 const AVFrame *frame, int *got_packet_ptr)
00737 {
00738 MpegAudioContext *s = avctx->priv_data;
00739 const int16_t *samples = (const int16_t *)frame->data[0];
00740 short smr[MPA_MAX_CHANNELS][SBLIMIT];
00741 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
00742 int padding, i, ret;
00743
00744 for(i=0;i<s->nb_channels;i++) {
00745 filter(s, i, samples + i, s->nb_channels);
00746 }
00747
00748 for(i=0;i<s->nb_channels;i++) {
00749 compute_scale_factors(s->scale_code[i], s->scale_factors[i],
00750 s->sb_samples[i], s->sblimit);
00751 }
00752 for(i=0;i<s->nb_channels;i++) {
00753 psycho_acoustic_model(s, smr[i]);
00754 }
00755 compute_bit_allocation(s, smr, bit_alloc, &padding);
00756
00757 if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)))
00758 return ret;
00759
00760 init_put_bits(&s->pb, avpkt->data, avpkt->size);
00761
00762 encode_frame(s, bit_alloc, padding);
00763
00764 if (frame->pts != AV_NOPTS_VALUE)
00765 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
00766
00767 avpkt->size = put_bits_count(&s->pb) / 8;
00768 *got_packet_ptr = 1;
00769 return 0;
00770 }
00771
00772 static av_cold int MPA_encode_close(AVCodecContext *avctx)
00773 {
00774 #if FF_API_OLD_ENCODE_AUDIO
00775 av_freep(&avctx->coded_frame);
00776 #endif
00777 return 0;
00778 }
00779
00780 static const AVCodecDefault mp2_defaults[] = {
00781 { "b", "128k" },
00782 { NULL },
00783 };
00784
00785 AVCodec ff_mp2_encoder = {
00786 .name = "mp2",
00787 .type = AVMEDIA_TYPE_AUDIO,
00788 .id = AV_CODEC_ID_MP2,
00789 .priv_data_size = sizeof(MpegAudioContext),
00790 .init = MPA_encode_init,
00791 .encode2 = MPA_encode_frame,
00792 .close = MPA_encode_close,
00793 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00794 AV_SAMPLE_FMT_NONE },
00795 .supported_samplerates = (const int[]){
00796 44100, 48000, 32000, 22050, 24000, 16000, 0
00797 },
00798 .channel_layouts = (const uint64_t[]){ AV_CH_LAYOUT_MONO,
00799 AV_CH_LAYOUT_STEREO,
00800 0 },
00801 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
00802 .defaults = mp2_defaults,
00803 };