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