You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

798 lines
23KB

  1. /*
  2. * The simplest mpeg audio layer 2 encoder
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file mpegaudio.c
  21. * The simplest mpeg audio layer 2 encoder.
  22. */
  23. #include "avcodec.h"
  24. #include "mpegaudio.h"
  25. /* currently, cannot change these constants (need to modify
  26. quantization stage) */
  27. #define FRAC_BITS 15
  28. #define WFRAC_BITS 14
  29. #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
  30. #define FIX(a) ((int)((a) * (1 << FRAC_BITS)))
  31. #define SAMPLES_BUF_SIZE 4096
  32. typedef struct MpegAudioContext {
  33. PutBitContext pb;
  34. int nb_channels;
  35. int freq, bit_rate;
  36. int lsf; /* 1 if mpeg2 low bitrate selected */
  37. int bitrate_index; /* bit rate */
  38. int freq_index;
  39. int frame_size; /* frame size, in bits, without padding */
  40. int64_t nb_samples; /* total number of samples encoded */
  41. /* padding computation */
  42. int frame_frac, frame_frac_incr, do_padding;
  43. short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]; /* buffer for filter */
  44. int samples_offset[MPA_MAX_CHANNELS]; /* offset in samples_buf */
  45. int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT];
  46. unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]; /* scale factors */
  47. /* code to group 3 scale factors */
  48. unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
  49. int sblimit; /* number of used subbands */
  50. const unsigned char *alloc_table;
  51. } MpegAudioContext;
  52. /* define it to use floats in quantization (I don't like floats !) */
  53. //#define USE_FLOATS
  54. #include "mpegaudiotab.h"
  55. static int MPA_encode_init(AVCodecContext *avctx)
  56. {
  57. MpegAudioContext *s = avctx->priv_data;
  58. int freq = avctx->sample_rate;
  59. int bitrate = avctx->bit_rate;
  60. int channels = avctx->channels;
  61. int i, v, table;
  62. float a;
  63. if (channels > 2)
  64. return -1;
  65. bitrate = bitrate / 1000;
  66. s->nb_channels = channels;
  67. s->freq = freq;
  68. s->bit_rate = bitrate * 1000;
  69. avctx->frame_size = MPA_FRAME_SIZE;
  70. /* encoding freq */
  71. s->lsf = 0;
  72. for(i=0;i<3;i++) {
  73. if (mpa_freq_tab[i] == freq)
  74. break;
  75. if ((mpa_freq_tab[i] / 2) == freq) {
  76. s->lsf = 1;
  77. break;
  78. }
  79. }
  80. if (i == 3){
  81. av_log(avctx, AV_LOG_ERROR, "Sampling rate %d is not allowed in mp2\n", freq);
  82. return -1;
  83. }
  84. s->freq_index = i;
  85. /* encoding bitrate & frequency */
  86. for(i=0;i<15;i++) {
  87. if (mpa_bitrate_tab[s->lsf][1][i] == bitrate)
  88. break;
  89. }
  90. if (i == 15){
  91. av_log(avctx, AV_LOG_ERROR, "bitrate %d is not allowed in mp2\n", bitrate);
  92. return -1;
  93. }
  94. s->bitrate_index = i;
  95. /* compute total header size & pad bit */
  96. a = (float)(bitrate * 1000 * MPA_FRAME_SIZE) / (freq * 8.0);
  97. s->frame_size = ((int)a) * 8;
  98. /* frame fractional size to compute padding */
  99. s->frame_frac = 0;
  100. s->frame_frac_incr = (int)((a - floor(a)) * 65536.0);
  101. /* select the right allocation table */
  102. table = l2_select_table(bitrate, s->nb_channels, freq, s->lsf);
  103. /* number of used subbands */
  104. s->sblimit = sblimit_table[table];
  105. s->alloc_table = alloc_tables[table];
  106. #ifdef DEBUG
  107. av_log(avctx, AV_LOG_DEBUG, "%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
  108. bitrate, freq, s->frame_size, table, s->frame_frac_incr);
  109. #endif
  110. for(i=0;i<s->nb_channels;i++)
  111. s->samples_offset[i] = 0;
  112. for(i=0;i<257;i++) {
  113. int v;
  114. v = mpa_enwindow[i];
  115. #if WFRAC_BITS != 16
  116. v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
  117. #endif
  118. filter_bank[i] = v;
  119. if ((i & 63) != 0)
  120. v = -v;
  121. if (i != 0)
  122. filter_bank[512 - i] = v;
  123. }
  124. for(i=0;i<64;i++) {
  125. v = (int)(pow(2.0, (3 - i) / 3.0) * (1 << 20));
  126. if (v <= 0)
  127. v = 1;
  128. scale_factor_table[i] = v;
  129. #ifdef USE_FLOATS
  130. scale_factor_inv_table[i] = pow(2.0, -(3 - i) / 3.0) / (float)(1 << 20);
  131. #else
  132. #define P 15
  133. scale_factor_shift[i] = 21 - P - (i / 3);
  134. scale_factor_mult[i] = (1 << P) * pow(2.0, (i % 3) / 3.0);
  135. #endif
  136. }
  137. for(i=0;i<128;i++) {
  138. v = i - 64;
  139. if (v <= -3)
  140. v = 0;
  141. else if (v < 0)
  142. v = 1;
  143. else if (v == 0)
  144. v = 2;
  145. else if (v < 3)
  146. v = 3;
  147. else
  148. v = 4;
  149. scale_diff_table[i] = v;
  150. }
  151. for(i=0;i<17;i++) {
  152. v = quant_bits[i];
  153. if (v < 0)
  154. v = -v;
  155. else
  156. v = v * 3;
  157. total_quant_bits[i] = 12 * v;
  158. }
  159. avctx->coded_frame= avcodec_alloc_frame();
  160. avctx->coded_frame->key_frame= 1;
  161. return 0;
  162. }
  163. /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
  164. static void idct32(int *out, int *tab)
  165. {
  166. int i, j;
  167. int *t, *t1, xr;
  168. const int *xp = costab32;
  169. for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
  170. t = tab + 30;
  171. t1 = tab + 2;
  172. do {
  173. t[0] += t[-4];
  174. t[1] += t[1 - 4];
  175. t -= 4;
  176. } while (t != t1);
  177. t = tab + 28;
  178. t1 = tab + 4;
  179. do {
  180. t[0] += t[-8];
  181. t[1] += t[1-8];
  182. t[2] += t[2-8];
  183. t[3] += t[3-8];
  184. t -= 8;
  185. } while (t != t1);
  186. t = tab;
  187. t1 = tab + 32;
  188. do {
  189. t[ 3] = -t[ 3];
  190. t[ 6] = -t[ 6];
  191. t[11] = -t[11];
  192. t[12] = -t[12];
  193. t[13] = -t[13];
  194. t[15] = -t[15];
  195. t += 16;
  196. } while (t != t1);
  197. t = tab;
  198. t1 = tab + 8;
  199. do {
  200. int x1, x2, x3, x4;
  201. x3 = MUL(t[16], FIX(SQRT2*0.5));
  202. x4 = t[0] - x3;
  203. x3 = t[0] + x3;
  204. x2 = MUL(-(t[24] + t[8]), FIX(SQRT2*0.5));
  205. x1 = MUL((t[8] - x2), xp[0]);
  206. x2 = MUL((t[8] + x2), xp[1]);
  207. t[ 0] = x3 + x1;
  208. t[ 8] = x4 - x2;
  209. t[16] = x4 + x2;
  210. t[24] = x3 - x1;
  211. t++;
  212. } while (t != t1);
  213. xp += 2;
  214. t = tab;
  215. t1 = tab + 4;
  216. do {
  217. xr = MUL(t[28],xp[0]);
  218. t[28] = (t[0] - xr);
  219. t[0] = (t[0] + xr);
  220. xr = MUL(t[4],xp[1]);
  221. t[ 4] = (t[24] - xr);
  222. t[24] = (t[24] + xr);
  223. xr = MUL(t[20],xp[2]);
  224. t[20] = (t[8] - xr);
  225. t[ 8] = (t[8] + xr);
  226. xr = MUL(t[12],xp[3]);
  227. t[12] = (t[16] - xr);
  228. t[16] = (t[16] + xr);
  229. t++;
  230. } while (t != t1);
  231. xp += 4;
  232. for (i = 0; i < 4; i++) {
  233. xr = MUL(tab[30-i*4],xp[0]);
  234. tab[30-i*4] = (tab[i*4] - xr);
  235. tab[ i*4] = (tab[i*4] + xr);
  236. xr = MUL(tab[ 2+i*4],xp[1]);
  237. tab[ 2+i*4] = (tab[28-i*4] - xr);
  238. tab[28-i*4] = (tab[28-i*4] + xr);
  239. xr = MUL(tab[31-i*4],xp[0]);
  240. tab[31-i*4] = (tab[1+i*4] - xr);
  241. tab[ 1+i*4] = (tab[1+i*4] + xr);
  242. xr = MUL(tab[ 3+i*4],xp[1]);
  243. tab[ 3+i*4] = (tab[29-i*4] - xr);
  244. tab[29-i*4] = (tab[29-i*4] + xr);
  245. xp += 2;
  246. }
  247. t = tab + 30;
  248. t1 = tab + 1;
  249. do {
  250. xr = MUL(t1[0], *xp);
  251. t1[0] = (t[0] - xr);
  252. t[0] = (t[0] + xr);
  253. t -= 2;
  254. t1 += 2;
  255. xp++;
  256. } while (t >= tab);
  257. for(i=0;i<32;i++) {
  258. out[i] = tab[bitinv32[i]];
  259. }
  260. }
  261. #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
  262. static void filter(MpegAudioContext *s, int ch, short *samples, int incr)
  263. {
  264. short *p, *q;
  265. int sum, offset, i, j;
  266. int tmp[64];
  267. int tmp1[32];
  268. int *out;
  269. // print_pow1(samples, 1152);
  270. offset = s->samples_offset[ch];
  271. out = &s->sb_samples[ch][0][0][0];
  272. for(j=0;j<36;j++) {
  273. /* 32 samples at once */
  274. for(i=0;i<32;i++) {
  275. s->samples_buf[ch][offset + (31 - i)] = samples[0];
  276. samples += incr;
  277. }
  278. /* filter */
  279. p = s->samples_buf[ch] + offset;
  280. q = filter_bank;
  281. /* maxsum = 23169 */
  282. for(i=0;i<64;i++) {
  283. sum = p[0*64] * q[0*64];
  284. sum += p[1*64] * q[1*64];
  285. sum += p[2*64] * q[2*64];
  286. sum += p[3*64] * q[3*64];
  287. sum += p[4*64] * q[4*64];
  288. sum += p[5*64] * q[5*64];
  289. sum += p[6*64] * q[6*64];
  290. sum += p[7*64] * q[7*64];
  291. tmp[i] = sum;
  292. p++;
  293. q++;
  294. }
  295. tmp1[0] = tmp[16] >> WSHIFT;
  296. for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >> WSHIFT;
  297. for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >> WSHIFT;
  298. idct32(out, tmp1);
  299. /* advance of 32 samples */
  300. offset -= 32;
  301. out += 32;
  302. /* handle the wrap around */
  303. if (offset < 0) {
  304. memmove(s->samples_buf[ch] + SAMPLES_BUF_SIZE - (512 - 32),
  305. s->samples_buf[ch], (512 - 32) * 2);
  306. offset = SAMPLES_BUF_SIZE - 512;
  307. }
  308. }
  309. s->samples_offset[ch] = offset;
  310. // print_pow(s->sb_samples, 1152);
  311. }
  312. static void compute_scale_factors(unsigned char scale_code[SBLIMIT],
  313. unsigned char scale_factors[SBLIMIT][3],
  314. int sb_samples[3][12][SBLIMIT],
  315. int sblimit)
  316. {
  317. int *p, vmax, v, n, i, j, k, code;
  318. int index, d1, d2;
  319. unsigned char *sf = &scale_factors[0][0];
  320. for(j=0;j<sblimit;j++) {
  321. for(i=0;i<3;i++) {
  322. /* find the max absolute value */
  323. p = &sb_samples[i][0][j];
  324. vmax = abs(*p);
  325. for(k=1;k<12;k++) {
  326. p += SBLIMIT;
  327. v = abs(*p);
  328. if (v > vmax)
  329. vmax = v;
  330. }
  331. /* compute the scale factor index using log 2 computations */
  332. if (vmax > 0) {
  333. n = av_log2(vmax);
  334. /* n is the position of the MSB of vmax. now
  335. use at most 2 compares to find the index */
  336. index = (21 - n) * 3 - 3;
  337. if (index >= 0) {
  338. while (vmax <= scale_factor_table[index+1])
  339. index++;
  340. } else {
  341. index = 0; /* very unlikely case of overflow */
  342. }
  343. } else {
  344. index = 62; /* value 63 is not allowed */
  345. }
  346. #if 0
  347. printf("%2d:%d in=%x %x %d\n",
  348. j, i, vmax, scale_factor_table[index], index);
  349. #endif
  350. /* store the scale factor */
  351. assert(index >=0 && index <= 63);
  352. sf[i] = index;
  353. }
  354. /* compute the transmission factor : look if the scale factors
  355. are close enough to each other */
  356. d1 = scale_diff_table[sf[0] - sf[1] + 64];
  357. d2 = scale_diff_table[sf[1] - sf[2] + 64];
  358. /* handle the 25 cases */
  359. switch(d1 * 5 + d2) {
  360. case 0*5+0:
  361. case 0*5+4:
  362. case 3*5+4:
  363. case 4*5+0:
  364. case 4*5+4:
  365. code = 0;
  366. break;
  367. case 0*5+1:
  368. case 0*5+2:
  369. case 4*5+1:
  370. case 4*5+2:
  371. code = 3;
  372. sf[2] = sf[1];
  373. break;
  374. case 0*5+3:
  375. case 4*5+3:
  376. code = 3;
  377. sf[1] = sf[2];
  378. break;
  379. case 1*5+0:
  380. case 1*5+4:
  381. case 2*5+4:
  382. code = 1;
  383. sf[1] = sf[0];
  384. break;
  385. case 1*5+1:
  386. case 1*5+2:
  387. case 2*5+0:
  388. case 2*5+1:
  389. case 2*5+2:
  390. code = 2;
  391. sf[1] = sf[2] = sf[0];
  392. break;
  393. case 2*5+3:
  394. case 3*5+3:
  395. code = 2;
  396. sf[0] = sf[1] = sf[2];
  397. break;
  398. case 3*5+0:
  399. case 3*5+1:
  400. case 3*5+2:
  401. code = 2;
  402. sf[0] = sf[2] = sf[1];
  403. break;
  404. case 1*5+3:
  405. code = 2;
  406. if (sf[0] > sf[2])
  407. sf[0] = sf[2];
  408. sf[1] = sf[2] = sf[0];
  409. break;
  410. default:
  411. assert(0); //cant happen
  412. }
  413. #if 0
  414. printf("%d: %2d %2d %2d %d %d -> %d\n", j,
  415. sf[0], sf[1], sf[2], d1, d2, code);
  416. #endif
  417. scale_code[j] = code;
  418. sf += 3;
  419. }
  420. }
  421. /* The most important function : psycho acoustic module. In this
  422. encoder there is basically none, so this is the worst you can do,
  423. but also this is the simpler. */
  424. static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
  425. {
  426. int i;
  427. for(i=0;i<s->sblimit;i++) {
  428. smr[i] = (int)(fixed_smr[i] * 10);
  429. }
  430. }
  431. #define SB_NOTALLOCATED 0
  432. #define SB_ALLOCATED 1
  433. #define SB_NOMORE 2
  434. /* Try to maximize the smr while using a number of bits inferior to
  435. the frame size. I tried to make the code simpler, faster and
  436. smaller than other encoders :-) */
  437. static void compute_bit_allocation(MpegAudioContext *s,
  438. short smr1[MPA_MAX_CHANNELS][SBLIMIT],
  439. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
  440. int *padding)
  441. {
  442. int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
  443. int incr;
  444. short smr[MPA_MAX_CHANNELS][SBLIMIT];
  445. unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];
  446. const unsigned char *alloc;
  447. memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);
  448. memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);
  449. memset(bit_alloc, 0, s->nb_channels * SBLIMIT);
  450. /* compute frame size and padding */
  451. max_frame_size = s->frame_size;
  452. s->frame_frac += s->frame_frac_incr;
  453. if (s->frame_frac >= 65536) {
  454. s->frame_frac -= 65536;
  455. s->do_padding = 1;
  456. max_frame_size += 8;
  457. } else {
  458. s->do_padding = 0;
  459. }
  460. /* compute the header + bit alloc size */
  461. current_frame_size = 32;
  462. alloc = s->alloc_table;
  463. for(i=0;i<s->sblimit;i++) {
  464. incr = alloc[0];
  465. current_frame_size += incr * s->nb_channels;
  466. alloc += 1 << incr;
  467. }
  468. for(;;) {
  469. /* look for the subband with the largest signal to mask ratio */
  470. max_sb = -1;
  471. max_ch = -1;
  472. max_smr = 0x80000000;
  473. for(ch=0;ch<s->nb_channels;ch++) {
  474. for(i=0;i<s->sblimit;i++) {
  475. if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {
  476. max_smr = smr[ch][i];
  477. max_sb = i;
  478. max_ch = ch;
  479. }
  480. }
  481. }
  482. #if 0
  483. printf("current=%d max=%d max_sb=%d alloc=%d\n",
  484. current_frame_size, max_frame_size, max_sb,
  485. bit_alloc[max_sb]);
  486. #endif
  487. if (max_sb < 0)
  488. break;
  489. /* find alloc table entry (XXX: not optimal, should use
  490. pointer table) */
  491. alloc = s->alloc_table;
  492. for(i=0;i<max_sb;i++) {
  493. alloc += 1 << alloc[0];
  494. }
  495. if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {
  496. /* nothing was coded for this band: add the necessary bits */
  497. incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;
  498. incr += total_quant_bits[alloc[1]];
  499. } else {
  500. /* increments bit allocation */
  501. b = bit_alloc[max_ch][max_sb];
  502. incr = total_quant_bits[alloc[b + 1]] -
  503. total_quant_bits[alloc[b]];
  504. }
  505. if (current_frame_size + incr <= max_frame_size) {
  506. /* can increase size */
  507. b = ++bit_alloc[max_ch][max_sb];
  508. current_frame_size += incr;
  509. /* decrease smr by the resolution we added */
  510. smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];
  511. /* max allocation size reached ? */
  512. if (b == ((1 << alloc[0]) - 1))
  513. subband_status[max_ch][max_sb] = SB_NOMORE;
  514. else
  515. subband_status[max_ch][max_sb] = SB_ALLOCATED;
  516. } else {
  517. /* cannot increase the size of this subband */
  518. subband_status[max_ch][max_sb] = SB_NOMORE;
  519. }
  520. }
  521. *padding = max_frame_size - current_frame_size;
  522. assert(*padding >= 0);
  523. #if 0
  524. for(i=0;i<s->sblimit;i++) {
  525. printf("%d ", bit_alloc[i]);
  526. }
  527. printf("\n");
  528. #endif
  529. }
  530. /*
  531. * Output the mpeg audio layer 2 frame. Note how the code is small
  532. * compared to other encoders :-)
  533. */
  534. static void encode_frame(MpegAudioContext *s,
  535. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
  536. int padding)
  537. {
  538. int i, j, k, l, bit_alloc_bits, b, ch;
  539. unsigned char *sf;
  540. int q[3];
  541. PutBitContext *p = &s->pb;
  542. /* header */
  543. put_bits(p, 12, 0xfff);
  544. put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */
  545. put_bits(p, 2, 4-2); /* layer 2 */
  546. put_bits(p, 1, 1); /* no error protection */
  547. put_bits(p, 4, s->bitrate_index);
  548. put_bits(p, 2, s->freq_index);
  549. put_bits(p, 1, s->do_padding); /* use padding */
  550. put_bits(p, 1, 0); /* private_bit */
  551. put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
  552. put_bits(p, 2, 0); /* mode_ext */
  553. put_bits(p, 1, 0); /* no copyright */
  554. put_bits(p, 1, 1); /* original */
  555. put_bits(p, 2, 0); /* no emphasis */
  556. /* bit allocation */
  557. j = 0;
  558. for(i=0;i<s->sblimit;i++) {
  559. bit_alloc_bits = s->alloc_table[j];
  560. for(ch=0;ch<s->nb_channels;ch++) {
  561. put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
  562. }
  563. j += 1 << bit_alloc_bits;
  564. }
  565. /* scale codes */
  566. for(i=0;i<s->sblimit;i++) {
  567. for(ch=0;ch<s->nb_channels;ch++) {
  568. if (bit_alloc[ch][i])
  569. put_bits(p, 2, s->scale_code[ch][i]);
  570. }
  571. }
  572. /* scale factors */
  573. for(i=0;i<s->sblimit;i++) {
  574. for(ch=0;ch<s->nb_channels;ch++) {
  575. if (bit_alloc[ch][i]) {
  576. sf = &s->scale_factors[ch][i][0];
  577. switch(s->scale_code[ch][i]) {
  578. case 0:
  579. put_bits(p, 6, sf[0]);
  580. put_bits(p, 6, sf[1]);
  581. put_bits(p, 6, sf[2]);
  582. break;
  583. case 3:
  584. case 1:
  585. put_bits(p, 6, sf[0]);
  586. put_bits(p, 6, sf[2]);
  587. break;
  588. case 2:
  589. put_bits(p, 6, sf[0]);
  590. break;
  591. }
  592. }
  593. }
  594. }
  595. /* quantization & write sub band samples */
  596. for(k=0;k<3;k++) {
  597. for(l=0;l<12;l+=3) {
  598. j = 0;
  599. for(i=0;i<s->sblimit;i++) {
  600. bit_alloc_bits = s->alloc_table[j];
  601. for(ch=0;ch<s->nb_channels;ch++) {
  602. b = bit_alloc[ch][i];
  603. if (b) {
  604. int qindex, steps, m, sample, bits;
  605. /* we encode 3 sub band samples of the same sub band at a time */
  606. qindex = s->alloc_table[j+b];
  607. steps = quant_steps[qindex];
  608. for(m=0;m<3;m++) {
  609. sample = s->sb_samples[ch][k][l + m][i];
  610. /* divide by scale factor */
  611. #ifdef USE_FLOATS
  612. {
  613. float a;
  614. a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
  615. q[m] = (int)((a + 1.0) * steps * 0.5);
  616. }
  617. #else
  618. {
  619. int q1, e, shift, mult;
  620. e = s->scale_factors[ch][i][k];
  621. shift = scale_factor_shift[e];
  622. mult = scale_factor_mult[e];
  623. /* normalize to P bits */
  624. if (shift < 0)
  625. q1 = sample << (-shift);
  626. else
  627. q1 = sample >> shift;
  628. q1 = (q1 * mult) >> P;
  629. q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
  630. }
  631. #endif
  632. if (q[m] >= steps)
  633. q[m] = steps - 1;
  634. assert(q[m] >= 0 && q[m] < steps);
  635. }
  636. bits = quant_bits[qindex];
  637. if (bits < 0) {
  638. /* group the 3 values to save bits */
  639. put_bits(p, -bits,
  640. q[0] + steps * (q[1] + steps * q[2]));
  641. #if 0
  642. printf("%d: gr1 %d\n",
  643. i, q[0] + steps * (q[1] + steps * q[2]));
  644. #endif
  645. } else {
  646. #if 0
  647. printf("%d: gr3 %d %d %d\n",
  648. i, q[0], q[1], q[2]);
  649. #endif
  650. put_bits(p, bits, q[0]);
  651. put_bits(p, bits, q[1]);
  652. put_bits(p, bits, q[2]);
  653. }
  654. }
  655. }
  656. /* next subband in alloc table */
  657. j += 1 << bit_alloc_bits;
  658. }
  659. }
  660. }
  661. /* padding */
  662. for(i=0;i<padding;i++)
  663. put_bits(p, 1, 0);
  664. /* flush */
  665. flush_put_bits(p);
  666. }
  667. static int MPA_encode_frame(AVCodecContext *avctx,
  668. unsigned char *frame, int buf_size, void *data)
  669. {
  670. MpegAudioContext *s = avctx->priv_data;
  671. short *samples = data;
  672. short smr[MPA_MAX_CHANNELS][SBLIMIT];
  673. unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
  674. int padding, i;
  675. for(i=0;i<s->nb_channels;i++) {
  676. filter(s, i, samples + i, s->nb_channels);
  677. }
  678. for(i=0;i<s->nb_channels;i++) {
  679. compute_scale_factors(s->scale_code[i], s->scale_factors[i],
  680. s->sb_samples[i], s->sblimit);
  681. }
  682. for(i=0;i<s->nb_channels;i++) {
  683. psycho_acoustic_model(s, smr[i]);
  684. }
  685. compute_bit_allocation(s, smr, bit_alloc, &padding);
  686. init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);
  687. encode_frame(s, bit_alloc, padding);
  688. s->nb_samples += MPA_FRAME_SIZE;
  689. return pbBufPtr(&s->pb) - s->pb.buf;
  690. }
  691. static int MPA_encode_close(AVCodecContext *avctx)
  692. {
  693. av_freep(&avctx->coded_frame);
  694. return 0;
  695. }
  696. AVCodec mp2_encoder = {
  697. "mp2",
  698. CODEC_TYPE_AUDIO,
  699. CODEC_ID_MP2,
  700. sizeof(MpegAudioContext),
  701. MPA_encode_init,
  702. MPA_encode_frame,
  703. MPA_encode_close,
  704. NULL,
  705. };
  706. #undef FIX