00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "put_bits.h"
00029
00030 #define FRAC_BITS 15
00031 #define WFRAC_BITS 14
00032
00033 #include "mpegaudio.h"
00034
00035
00036
00037 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
00038
00039 #define SAMPLES_BUF_SIZE 4096
00040
00041 typedef struct MpegAudioContext {
00042 PutBitContext pb;
00043 int nb_channels;
00044 int lsf;
00045 int bitrate_index;
00046 int freq_index;
00047 int frame_size;
00048
00049 int frame_frac, frame_frac_incr, do_padding;
00050 short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE];
00051 int samples_offset[MPA_MAX_CHANNELS];
00052 int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
00053 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3];
00054
00055 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
00056 int sblimit;
00057 const unsigned char *alloc_table;
00058 } MpegAudioContext;
00059
00060
00061 #define USE_FLOATS
00062
00063 #include "mpegaudiodata.h"
00064 #include "mpegaudiotab.h"
00065
00066 static av_cold int MPA_encode_init(AVCodecContext *avctx)
00067 {
00068 MpegAudioContext *s = avctx->priv_data;
00069 int freq = avctx->sample_rate;
00070 int bitrate = avctx->bit_rate;
00071 int channels = avctx->channels;
00072 int i, v, table;
00073 float a;
00074
00075 if (channels <= 0 || channels > 2){
00076 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed in mp2\n", channels);
00077 return -1;
00078 }
00079 bitrate = bitrate / 1000;
00080 s->nb_channels = channels;
00081 avctx->frame_size = MPA_FRAME_SIZE;
00082
00083
00084 s->lsf = 0;
00085 for(i=0;i<3;i++) {
00086 if (ff_mpa_freq_tab[i] == freq)
00087 break;
00088 if ((ff_mpa_freq_tab[i] / 2) == freq) {
00089 s->lsf = 1;
00090 break;
00091 }
00092 }
00093 if (i == 3){
00094 av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
00095 return -1;
00096 }
00097 s->freq_index = i;
00098
00099
00100 for(i=0;i<15;i++) {
00101 if (ff_mpa_bitrate_tab[s->lsf][1][i] == bitrate)
00102 break;
00103 }
00104 if (i == 15){
00105 av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
00106 return -1;
00107 }
00108 s->bitrate_index = i;
00109
00110
00111
00112 a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
00113 s->frame_size = ((int)a) * 8;
00114
00115
00116 s->frame_frac = 0;
00117 s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
00118
00119
00120 table = ff_mpa_l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
00121
00122
00123 s->sblimit = ff_mpa_sblimit_table[table];
00124 s->alloc_table = ff_mpa_alloc_tables[table];
00125
00126 av_dlog(avctx, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
00127 bitrate, freq, s->frame_size, table, s->frame_frac_incr);
00128
00129 for(i=0;i<s->nb_channels;i++)
00130 s->samples_offset[i] = 0;
00131
00132 for(i=0;i<257;i++) {
00133 int v;
00134 v = ff_mpa_enwindow[i];
00135 #if WFRAC_BITS != 16
00136 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
00137 #endif
00138 filter_bank[i] = v;
00139 if ((i & 63) != 0)
00140 v = -v;
00141 if (i != 0)
00142 filter_bank[512 - i] = v;
00143 }
00144
00145 for(i=0;i<64;i++) {
00146 v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
00147 if (v <= 0)
00148 v = 1;
00149 scale_factor_table[i] = v;
00150 #ifdef USE_FLOATS
00151 scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
00152 #else
00153 #define P 15
00154 scale_factor_shift[i] = 21 - P - (i / 3);
00155 scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
00156 #endif
00157 }
00158 for(i=0;i<128;i++) {
00159 v = i - 64;
00160 if (v <= -3)
00161 v = 0;
00162 else if (v < 0)
00163 v = 1;
00164 else if (v == 0)
00165 v = 2;
00166 else if (v < 3)
00167 v = 3;
00168 else
00169 v = 4;
00170 scale_diff_table[i] = v;
00171 }
00172
00173 for(i=0;i<17;i++) {
00174 v = ff_mpa_quant_bits[i];
00175 if (v < 0)
00176 v = -v;
00177 else
00178 v = v * 3;
00179 total_quant_bits[i] = 12 * v;
00180 }
00181
00182 avctx->coded_frame= avcodec_alloc_frame();
00183 avctx->coded_frame->key_frame= 1;
00184
00185 return 0;
00186 }
00187
00188
00189 static void idct32(int *out, int *tab)
00190 {
00191 int i, j;
00192 int *t, *t1, xr;
00193 const int *xp = costab32;
00194
00195 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
00196
00197 t = tab + 30;
00198 t1 = tab + 2;
00199 do {
00200 t[0] += t[-4];
00201 t[1] += t[1 - 4];
00202 t -= 4;
00203 } while (t != t1);
00204
00205 t = tab + 28;
00206 t1 = tab + 4;
00207 do {
00208 t[0] += t[-8];
00209 t[1] += t[1-8];
00210 t[2] += t[2-8];
00211 t[3] += t[3-8];
00212 t -= 8;
00213 } while (t != t1);
00214
00215 t = tab;
00216 t1 = tab + 32;
00217 do {
00218 t[ 3] = -t[ 3];
00219 t[ 6] = -t[ 6];
00220
00221 t[11] = -t[11];
00222 t[12] = -t[12];
00223 t[13] = -t[13];
00224 t[15] = -t[15];
00225 t += 16;
00226 } while (t != t1);
00227
00228
00229 t = tab;
00230 t1 = tab + 8;
00231 do {
00232 int x1, x2, x3, x4;
00233
00234 x3 = MUL(t[16], FIX(SQRT2*0.5));
00235 x4 = t[0] - x3;
00236 x3 = t[0] + x3;
00237
00238 x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
00239 x1 = MUL((t[8] - x2), xp[0]);
00240 x2 = MUL((t[8] + x2), xp[1]);
00241
00242 t[ 0] = x3 + x1;
00243 t[ 8] = x4 - x2;
00244 t[16] = x4 + x2;
00245 t[24] = x3 - x1;
00246 t++;
00247 } while (t != t1);
00248
00249 xp += 2;
00250 t = tab;
00251 t1 = tab + 4;
00252 do {
00253 xr = MUL(t[28],xp[0]);
00254 t[28] = (t[0] - xr);
00255 t[0] = (t[0] + xr);
00256
00257 xr = MUL(t[4],xp[1]);
00258 t[ 4] = (t[24] - xr);
00259 t[24] = (t[24] + xr);
00260
00261 xr = MUL(t[20],xp[2]);
00262 t[20] = (t[8] - xr);
00263 t[ 8] = (t[8] + xr);
00264
00265 xr = MUL(t[12],xp[3]);
00266 t[12] = (t[16] - xr);
00267 t[16] = (t[16] + xr);
00268 t++;
00269 } while (t != t1);
00270 xp += 4;
00271
00272 for (i = 0; i < 4; i++) {
00273 xr = MUL(tab[30-i*4],xp[0]);
00274 tab[30-i*4] = (tab[i*4] - xr);
00275 tab[ i*4] = (tab[i*4] + xr);
00276
00277 xr = MUL(tab[ 2+i*4],xp[1]);
00278 tab[ 2+i*4] = (tab[28-i*4] - xr);
00279 tab[28-i*4] = (tab[28-i*4] + xr);
00280
00281 xr = MUL(tab[31-i*4],xp[0]);
00282 tab[31-i*4] = (tab[1+i*4] - xr);
00283 tab[ 1+i*4] = (tab[1+i*4] + xr);
00284
00285 xr = MUL(tab[ 3+i*4],xp[1]);
00286 tab[ 3+i*4] = (tab[29-i*4] - xr);
00287 tab[29-i*4] = (tab[29-i*4] + xr);
00288
00289 xp += 2;
00290 }
00291
00292 t = tab + 30;
00293 t1 = tab + 1;
00294 do {
00295 xr = MUL(t1[0], *xp);
00296 t1[0] = (t[0] - xr);
00297 t[0] = (t[0] + xr);
00298 t -= 2;
00299 t1 += 2;
00300 xp++;
00301 } while (t >= tab);
00302
00303 for(i=0;i<32;i++) {
00304 out[i] = tab[bitinv32[i]];
00305 }
00306 }
00307
00308 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
00309
00310 static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
00311 {
00312 short *p, *q;
00313 int sum, offset, i, j;
00314 int tmp[64];
00315 int tmp1[32];
00316 int *out;
00317
00318
00319
00320 offset = s->samples_offset[ch];
00321 out = &s->sb_samples[ch][0][0][0];
00322 for(j=0;j<36;j++) {
00323
00324 for(i=0;i<32;i++) {
00325 s->samples_buf[ch][offset + (31 - i)] = samples[0];
00326 samples += incr;
00327 }
00328
00329
00330 p = s->samples_buf[ch] + offset;
00331 q = filter_bank;
00332
00333 for(i=0;i<64;i++) {
00334 sum = p[0*64] * q[0*64];
00335 sum += p[1*64] * q[1*64];
00336 sum += p[2*64] * q[2*64];
00337 sum += p[3*64] * q[3*64];
00338 sum += p[4*64] * q[4*64];
00339 sum += p[5*64] * q[5*64];
00340 sum += p[6*64] * q[6*64];
00341 sum += p[7*64] * q[7*64];
00342 tmp[i] = sum;
00343 p++;
00344 q++;
00345 }
00346 tmp1[0] = tmp[16] >> WSHIFT;
00347 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
00348 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
00349
00350 idct32(out, tmp1);
00351
00352
00353 offset -= 32;
00354 out += 32;
00355
00356 if (offset < 0) {
00357 memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
00358 s->samples_buf[ch], (512 - 32) * 2);
00359 offset = SAMPLES_BUF_SIZE - 512;
00360 }
00361 }
00362 s->samples_offset[ch] = offset;
00363
00364
00365 }
00366
00367 static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
00368 unsigned char scale_factors[SBLIMIT][3],
00369 int sb_samples[3][12][SBLIMIT],
00370 int sblimit)
00371 {
00372 int *p, vmax, v, n, i, j, k, code;
00373 int index, d1, d2;
00374 unsigned char *sf = &scale_factors[0][0];
00375
00376 for(j=0;j<sblimit;j++) {
00377 for(i=0;i<3;i++) {
00378
00379 p = &sb_samples[i][0][j];
00380 vmax = abs(*p);
00381 for(k=1;k<12;k++) {
00382 p += SBLIMIT;
00383 v = abs(*p);
00384 if (v > vmax)
00385 vmax = v;
00386 }
00387
00388 if (vmax > 1) {
00389 n = av_log2(vmax);
00390
00391
00392 index = (21 - n) * 3 - 3;
00393 if (index >= 0) {
00394 while (vmax <= scale_factor_table[index+1])
00395 index++;
00396 } else {
00397 index = 0;
00398 }
00399 } else {
00400 index = 62;
00401 }
00402
00403 #if 0
00404 printf("%2d:%d in=%x %x %d\n",
00405 j, i, vmax, scale_factor_table[index], index);
00406 #endif
00407
00408 assert(index >=0 && index <= 63);
00409 sf[i] = index;
00410 }
00411
00412
00413
00414 d1 = scale_diff_table[sf[0] - sf[1] + 64];
00415 d2 = scale_diff_table[sf[1] - sf[2] + 64];
00416
00417
00418 switch(d1 * 5 + d2) {
00419 case 0*5+0:
00420 case 0*5+4:
00421 case 3*5+4:
00422 case 4*5+0:
00423 case 4*5+4:
00424 code = 0;
00425 break;
00426 case 0*5+1:
00427 case 0*5+2:
00428 case 4*5+1:
00429 case 4*5+2:
00430 code = 3;
00431 sf[2] = sf[1];
00432 break;
00433 case 0*5+3:
00434 case 4*5+3:
00435 code = 3;
00436 sf[1] = sf[2];
00437 break;
00438 case 1*5+0:
00439 case 1*5+4:
00440 case 2*5+4:
00441 code = 1;
00442 sf[1] = sf[0];
00443 break;
00444 case 1*5+1:
00445 case 1*5+2:
00446 case 2*5+0:
00447 case 2*5+1:
00448 case 2*5+2:
00449 code = 2;
00450 sf[1] = sf[2] = sf[0];
00451 break;
00452 case 2*5+3:
00453 case 3*5+3:
00454 code = 2;
00455 sf[0] = sf[1] = sf[2];
00456 break;
00457 case 3*5+0:
00458 case 3*5+1:
00459 case 3*5+2:
00460 code = 2;
00461 sf[0] = sf[2] = sf[1];
00462 break;
00463 case 1*5+3:
00464 code = 2;
00465 if (sf[0] > sf[2])
00466 sf[0] = sf[2];
00467 sf[1] = sf[2] = sf[0];
00468 break;
00469 default:
00470 assert(0);
00471 code = 0;
00472 }
00473
00474 #if 0
00475 printf("%d: %2d %2d %2d %d %d -> %d\n", j,
00476 sf[0], sf[1], sf[2], d1, d2, code);
00477 #endif
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 assert(*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 assert(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,
00736 unsigned char *frame, int buf_size, void *data)
00737 {
00738 MpegAudioContext *s = avctx->priv_data;
00739 const short *samples = data;
00740 short smr[MPA_MAX_CHANNELS][SBLIMIT];
00741 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
00742 int padding, i;
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 init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
00758
00759 encode_frame(s, bit_alloc, padding);
00760
00761 return put_bits_ptr(&s->pb) - s->pb.buf;
00762 }
00763
00764 static av_cold int MPA_encode_close(AVCodecContext *avctx)
00765 {
00766 av_freep(&avctx->coded_frame);
00767 return 0;
00768 }
00769
00770 AVCodec ff_mp2_encoder = {
00771 "mp2",
00772 AVMEDIA_TYPE_AUDIO,
00773 CODEC_ID_MP2,
00774 sizeof(MpegAudioContext),
00775 MPA_encode_init,
00776 MPA_encode_frame,
00777 MPA_encode_close,
00778 NULL,
00779 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
00780 .supported_samplerates= (const int[]){44100, 48000, 32000, 22050, 24000, 16000, 0},
00781 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
00782 };