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.

780 lines
23KB

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