2
0

mpegaudioenc_template.c 23 KB

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