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