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