encoder.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * LAME MP3 encoding engine
  3. *
  4. * Copyright (c) 1999 Mark Taylor
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. /* $Id: encoder.c,v 1.43 2001/03/12 04:38:35 markt Exp $ */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <assert.h>
  26. #include "lame.h"
  27. #include "util.h"
  28. #include "newmdct.h"
  29. #include "psymodel.h"
  30. #include "quantize.h"
  31. #include "quantize_pvt.h"
  32. #include "bitstream.h"
  33. #include "VbrTag.h"
  34. #ifdef WITH_DMALLOC
  35. #include <dmalloc.h>
  36. #endif
  37. /*
  38. * auto-adjust of ATH, useful for low volume
  39. * Gabriel Bouvigne 3 feb 2001
  40. *
  41. * modifies some values in
  42. * gfp->internal_flags->ATH
  43. * (gfc->ATH)
  44. */
  45. void
  46. adjust_ATH( lame_global_flags* const gfp,
  47. FLOAT8 tot_ener[2][4] )
  48. {
  49. lame_internal_flags* const gfc = gfp->internal_flags;
  50. int gr, channel;
  51. if (gfc->ATH->use_adjust) {
  52. FLOAT8 max_val = 0;
  53. for ( gr = 0; gr < gfc->mode_gr; ++gr )
  54. for ( channel = 0; channel < gfc->channels_out; ++channel )
  55. max_val = Max( max_val, tot_ener[gr][channel] );
  56. /* scale to 0..1, and then rescale to 0..32767 */
  57. max_val *= 32767/1e13;
  58. /* adjust ATH depending on range of maximum value
  59. */
  60. if (vbr_mtrh == gfp->VBR) {
  61. /* this code reduces slowly the ATH (speed of 12 dB per second)
  62. * with some supporting stages to limit the reduction
  63. * 640 -> ~17 dB
  64. * :
  65. * 32640 -> ~0.01 dB
  66. */
  67. FLOAT8
  68. x = Max (640, 320*(int)(max_val/320));
  69. x = x/32768;
  70. gfc->ATH->adjust *= gfc->ATH->decay;
  71. if (gfc->ATH->adjust < x) /* but not more than f(x) dB */
  72. gfc->ATH->adjust = x;
  73. }
  74. else {
  75. #ifdef OLD_ATH_AUTO_ADJUST
  76. if (0.5 < max_val / 32768) { /* value above 50 % */
  77. gfc->ATH->adjust = 1.0; /* do not reduce ATH */
  78. }
  79. else if (0.3 < max_val / 32768) { /* value above 30 % */
  80. gfc->ATH->adjust *= 0.955; /* reduce by ~0.2 dB */
  81. if (gfc->ATH->adjust < 0.3) /* but ~5 dB in maximum */
  82. gfc->ATH->adjust = 0.3;
  83. }
  84. else { /* value below 30 % */
  85. gfc->ATH->adjust *= 0.93; /* reduce by ~0.3 dB */
  86. if (gfc->ATH->adjust < 0.01) /* but 20 dB in maximum */
  87. gfc->ATH->adjust = 0.01;
  88. }
  89. #else /* jd - 27 feb 2001 */
  90. /* continuous curves based on approximation */
  91. /* to GB's original values */
  92. FLOAT8 max_val_n = max_val / 32768;
  93. FLOAT8 adj_lim_new;
  94. /* For an increase in approximate loudness, */
  95. /* set ATH adjust to adjust_limit immediately*/
  96. /* after a delay of one frame. */
  97. /* For a loudness decrease, reduce ATH adjust*/
  98. /* towards adjust_limit gradually. */
  99. if( max_val_n > 0.25) { /* sqrt((1 - 0.01) / 15.84) from curve below*/
  100. if( gfc->ATH->adjust >= 1.0) {
  101. gfc->ATH->adjust = 1.0;
  102. } else { /* preceding frame has lower ATH adjust; */
  103. /* ascend only to the preceding adjust_limit */
  104. /* in case there is leading low volume */
  105. if( gfc->ATH->adjust < gfc->ATH->adjust_limit) {
  106. gfc->ATH->adjust = gfc->ATH->adjust_limit;
  107. }
  108. }
  109. gfc->ATH->adjust_limit = 1.0;
  110. } else { /* adjustment curve (parabolic) */
  111. adj_lim_new = 15.84 * (max_val_n * max_val_n) + 0.01;
  112. if( gfc->ATH->adjust >= adj_lim_new) { /* descend gradually */
  113. gfc->ATH->adjust *= adj_lim_new * 0.075 + 0.925;
  114. if( gfc->ATH->adjust < adj_lim_new) { /* stop descent */
  115. gfc->ATH->adjust = adj_lim_new;
  116. }
  117. } else { /* ascend */
  118. if( gfc->ATH->adjust_limit >= adj_lim_new) {
  119. gfc->ATH->adjust = adj_lim_new;
  120. } else { /* preceding frame has lower ATH adjust; */
  121. /* ascend only to the preceding adjust_limit */
  122. if( gfc->ATH->adjust < gfc->ATH->adjust_limit) {
  123. gfc->ATH->adjust = gfc->ATH->adjust_limit;
  124. }
  125. }
  126. }
  127. gfc->ATH->adjust_limit = adj_lim_new;
  128. }
  129. #endif
  130. }
  131. }
  132. }
  133. /************************************************************************
  134. *
  135. * encodeframe() Layer 3
  136. *
  137. * encode a single frame
  138. *
  139. ************************************************************************
  140. lame_encode_frame()
  141. gr 0 gr 1
  142. inbuf: |--------------|---------------|-------------|
  143. MDCT output: |--------------|---------------|-------------|
  144. FFT's <---------1024---------->
  145. <---------1024-------->
  146. inbuf = buffer of PCM data size=MP3 framesize
  147. encoder acts on inbuf[ch][0], but output is delayed by MDCTDELAY
  148. so the MDCT coefficints are from inbuf[ch][-MDCTDELAY]
  149. psy-model FFT has a 1 granule delay, so we feed it data for the
  150. next granule.
  151. FFT is centered over granule: 224+576+224
  152. So FFT starts at: 576-224-MDCTDELAY
  153. MPEG2: FFT ends at: BLKSIZE+576-224-MDCTDELAY
  154. MPEG1: FFT ends at: BLKSIZE+2*576-224-MDCTDELAY (1904)
  155. FFT starts at 576-224-MDCTDELAY (304) = 576-FFTOFFSET
  156. */
  157. typedef FLOAT8 chgrdata[2][2];
  158. int lame_encode_mp3_frame ( // Output
  159. lame_global_flags* const gfp, // Context
  160. sample_t* inbuf_l, // Input
  161. sample_t* inbuf_r, // Input
  162. unsigned char* mp3buf, // Output
  163. int mp3buf_size ) // Output
  164. {
  165. #ifdef macintosh /* PLL 14/04/2000 */
  166. static FLOAT8 xr[2][2][576];
  167. static int l3_enc[2][2][576];
  168. #else
  169. FLOAT8 xr[2][2][576];
  170. int l3_enc[2][2][576];
  171. #endif
  172. int mp3count;
  173. III_psy_ratio masking_LR[2][2]; /*LR masking & energy */
  174. III_psy_ratio masking_MS[2][2]; /*MS masking & energy */
  175. III_psy_ratio (*masking)[2][2]; /*pointer to selected maskings*/
  176. III_scalefac_t scalefac[2][2];
  177. const sample_t *inbuf[2];
  178. lame_internal_flags *gfc=gfp->internal_flags;
  179. FLOAT8 tot_ener[2][4];
  180. FLOAT8 ms_ener_ratio[2]={.5,.5};
  181. chgrdata pe,pe_MS;
  182. chgrdata *pe_use;
  183. int ch,gr,mean_bits;
  184. int bitsPerFrame;
  185. int check_ms_stereo;
  186. FLOAT8 ms_ratio_next = 0.;
  187. FLOAT8 ms_ratio_prev = 0.;
  188. memset((char *) masking_LR, 0, sizeof(masking_LR));
  189. memset((char *) masking_MS, 0, sizeof(masking_MS));
  190. memset((char *) scalefac, 0, sizeof(scalefac));
  191. inbuf[0]=inbuf_l;
  192. inbuf[1]=inbuf_r;
  193. check_ms_stereo = (gfp->mode == JOINT_STEREO);
  194. gfc->mode_ext = MPG_MD_LR_LR;
  195. if (gfc->lame_encode_frame_init==0 ) {
  196. gfc->lame_encode_frame_init=1;
  197. /* padding method as described in
  198. * "MPEG-Layer3 / Bitstream Syntax and Decoding"
  199. * by Martin Sieler, Ralph Sperschneider
  200. *
  201. * note: there is no padding for the very first frame
  202. *
  203. * Robert.Hegemann@gmx.de 2000-06-22
  204. */
  205. gfc->frac_SpF = ((gfp->version+1)*72000L*gfp->brate) % gfp->out_samplerate;
  206. gfc->slot_lag = gfc->frac_SpF;
  207. /* check FFT will not use a negative starting offset */
  208. #if 576 < FFTOFFSET
  209. # error FFTOFFSET greater than 576: FFT uses a negative offset
  210. #endif
  211. /* check if we have enough data for FFT */
  212. assert(gfc->mf_size>=(BLKSIZE+gfp->framesize-FFTOFFSET));
  213. /* check if we have enough data for polyphase filterbank */
  214. /* it needs 1152 samples + 286 samples ignored for one granule */
  215. /* 1152+576+286 samples for two granules */
  216. assert(gfc->mf_size>=(286+576*(1+gfc->mode_gr)));
  217. /* prime the MDCT/polyphase filterbank with a short block */
  218. {
  219. int i,j;
  220. sample_t primebuff0[286+1152+576];
  221. sample_t primebuff1[286+1152+576];
  222. for (i=0, j=0; i<286+576*(1+gfc->mode_gr); ++i) {
  223. if (i<576*gfc->mode_gr) {
  224. primebuff0[i]=0;
  225. if (gfc->channels_out==2)
  226. primebuff1[i]=0;
  227. }else{
  228. primebuff0[i]=inbuf[0][j];
  229. if (gfc->channels_out==2)
  230. primebuff1[i]=inbuf[1][j];
  231. ++j;
  232. }
  233. }
  234. /* polyphase filtering / mdct */
  235. for ( gr = 0; gr < gfc->mode_gr; gr++ ) {
  236. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  237. gfc->l3_side.gr[gr].ch[ch].tt.block_type=SHORT_TYPE;
  238. }
  239. }
  240. mdct_sub48(gfc, primebuff0, primebuff1, xr);
  241. }
  242. iteration_init(gfp);
  243. /* prepare for ATH auto adjustment:
  244. * we want to decrease the ATH by 12 dB per second
  245. */ {
  246. FLOAT8 frame_duration = 576. * gfc->mode_gr / gfp->out_samplerate;
  247. gfc->ATH->decay = pow(10., -12./10. * frame_duration);
  248. gfc->ATH->adjust = 1.0;
  249. gfc->ATH->adjust_limit = 0.01;
  250. }
  251. }
  252. /********************** padding *****************************/
  253. switch (gfp->padding_type) {
  254. case 0:
  255. gfc->padding=0;
  256. break;
  257. case 1:
  258. gfc->padding=1;
  259. break;
  260. case 2:
  261. default:
  262. if (gfp->VBR!=vbr_off) {
  263. gfc->padding=0;
  264. } else {
  265. if (gfp->disable_reservoir) {
  266. gfc->padding = 0;
  267. /* if the user specified --nores, dont very gfc->padding either */
  268. /* tiny changes in frac_SpF rounding will cause file differences */
  269. }else{
  270. /* padding method as described in
  271. * "MPEG-Layer3 / Bitstream Syntax and Decoding"
  272. * by Martin Sieler, Ralph Sperschneider
  273. *
  274. * note: there is no padding for the very first frame
  275. *
  276. * Robert.Hegemann@gmx.de 2000-06-22
  277. */
  278. gfc->slot_lag -= gfc->frac_SpF;
  279. if (gfc->slot_lag < 0) {
  280. gfc->slot_lag += gfp->out_samplerate;
  281. gfc->padding = 1;
  282. } else {
  283. gfc->padding = 0;
  284. }
  285. } /* reservoir enabled */
  286. }
  287. }
  288. if (gfc->psymodel) {
  289. /* psychoacoustic model
  290. * psy model has a 1 granule (576) delay that we must compensate for
  291. * (mt 6/99).
  292. */
  293. int ret;
  294. const sample_t *bufp[2]; /* address of beginning of left & right granule */
  295. int blocktype[2];
  296. ms_ratio_prev=gfc->ms_ratio[gfc->mode_gr-1];
  297. for (gr=0; gr < gfc->mode_gr ; gr++) {
  298. for ( ch = 0; ch < gfc->channels_out; ch++ )
  299. bufp[ch] = &inbuf[ch][576 + gr*576-FFTOFFSET];
  300. if (gfc->nsPsy.use) {
  301. ret=L3psycho_anal_ns( gfp, bufp, gr,
  302. &gfc->ms_ratio[gr],&ms_ratio_next,
  303. masking_LR, masking_MS,
  304. pe[gr],pe_MS[gr],tot_ener[gr],blocktype);
  305. } else {
  306. ret=L3psycho_anal( gfp, bufp, gr,
  307. &gfc->ms_ratio[gr],&ms_ratio_next,
  308. masking_LR, masking_MS,
  309. pe[gr],pe_MS[gr],tot_ener[gr],blocktype);
  310. }
  311. if (ret!=0) return -4;
  312. for ( ch = 0; ch < gfc->channels_out; ch++ )
  313. gfc->l3_side.gr[gr].ch[ch].tt.block_type=blocktype[ch];
  314. if (check_ms_stereo) {
  315. ms_ener_ratio[gr] = tot_ener[gr][2]+tot_ener[gr][3];
  316. if (ms_ener_ratio[gr]>0)
  317. ms_ener_ratio[gr] = tot_ener[gr][3]/ms_ener_ratio[gr];
  318. }
  319. }
  320. }else{
  321. for (gr=0; gr < gfc->mode_gr ; gr++)
  322. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  323. gfc->l3_side.gr[gr].ch[ch].tt.block_type=NORM_TYPE;
  324. pe_MS[gr][ch]=pe[gr][ch]=700;
  325. }
  326. }
  327. /* auto-adjust of ATH, useful for low volume */
  328. adjust_ATH( gfp, tot_ener );
  329. /* block type flags */
  330. for( gr = 0; gr < gfc->mode_gr; gr++ ) {
  331. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  332. gr_info *cod_info = &gfc->l3_side.gr[gr].ch[ch].tt;
  333. cod_info->mixed_block_flag = 0; /* never used by this model */
  334. if (cod_info->block_type == NORM_TYPE )
  335. cod_info->window_switching_flag = 0;
  336. else
  337. cod_info->window_switching_flag = 1;
  338. }
  339. }
  340. /* polyphase filtering / mdct */
  341. mdct_sub48(gfc, inbuf[0], inbuf[1], xr);
  342. /* re-order the short blocks, for more efficient encoding below */
  343. for (gr = 0; gr < gfc->mode_gr; gr++) {
  344. for (ch = 0; ch < gfc->channels_out; ch++) {
  345. gr_info *cod_info = &gfc->l3_side.gr[gr].ch[ch].tt;
  346. if (cod_info->block_type==SHORT_TYPE) {
  347. freorder(gfc->scalefac_band.s,xr[gr][ch]);
  348. }
  349. }
  350. }
  351. /* use m/s gfc->channels_out? */
  352. if (check_ms_stereo) {
  353. int gr0 = 0, gr1 = gfc->mode_gr-1;
  354. /* make sure block type is the same in each channel */
  355. check_ms_stereo =
  356. (gfc->l3_side.gr[gr0].ch[0].tt.block_type==gfc->l3_side.gr[gr0].ch[1].tt.block_type) &&
  357. (gfc->l3_side.gr[gr1].ch[0].tt.block_type==gfc->l3_side.gr[gr1].ch[1].tt.block_type);
  358. }
  359. /* Here will be selected MS or LR coding of the 2 stereo channels */
  360. assert ( gfc->mode_ext == MPG_MD_LR_LR );
  361. gfc->mode_ext = MPG_MD_LR_LR;
  362. if (gfp->force_ms) {
  363. gfc->mode_ext = MPG_MD_MS_LR;
  364. } else if (check_ms_stereo) {
  365. /* ms_ratio = is scaled, for historical reasons, to look like
  366. a ratio of side_channel / total.
  367. 0 = signal is 100% mono
  368. .5 = L & R uncorrelated
  369. */
  370. /* [0] and [1] are the results for the two granules in MPEG-1,
  371. * in MPEG-2 it's only a faked averaging of the same value
  372. * _prev is the value of the last granule of the previous frame
  373. * _next is the value of the first granule of the next frame
  374. */
  375. FLOAT8 ms_ratio_ave1;
  376. FLOAT8 ms_ratio_ave2;
  377. FLOAT8 threshold1 = 0.35;
  378. FLOAT8 threshold2 = 0.45;
  379. /* take an average */
  380. if (gfc->mode_gr==1) {
  381. /* MPEG2 - no second granule */
  382. ms_ratio_ave1 = 0.33 * ( gfc->ms_ratio[0] + ms_ratio_prev + ms_ratio_next );
  383. ms_ratio_ave2 = gfc->ms_ratio[0];
  384. }else{
  385. ms_ratio_ave1 = 0.25 * ( gfc->ms_ratio[0] + gfc->ms_ratio[1] + ms_ratio_prev + ms_ratio_next );
  386. ms_ratio_ave2 = 0.50 * ( gfc->ms_ratio[0] + gfc->ms_ratio[1] );
  387. }
  388. if (gfp->mode_automs) {
  389. if ( gfp->compression_ratio < 11.025 ) {
  390. /* 11.025 => 1, 6.3 => 0 */
  391. double thr = (gfp->compression_ratio - 6.3) / (11.025 - 6.3);
  392. if (thr<0) thr=0;
  393. threshold1 *= thr;
  394. threshold2 *= thr;
  395. }
  396. }
  397. if ((ms_ratio_ave1 < threshold1 && ms_ratio_ave2 < threshold2) || gfc->nsPsy.use) {
  398. int sum_pe_MS = pe_MS[0][0] + pe_MS[0][1] + pe_MS[1][0] + pe_MS[1][1];
  399. int sum_pe_LR = pe [0][0] + pe [0][1] + pe [1][0] + pe [1][1];
  400. /* based on PE: M/S coding would not use much more bits than L/R coding */
  401. if (sum_pe_MS <= 1.07 * sum_pe_LR && !gfc->nsPsy.use) gfc->mode_ext = MPG_MD_MS_LR;
  402. if (sum_pe_MS <= 1.00 * sum_pe_LR && gfc->nsPsy.use) gfc->mode_ext = MPG_MD_MS_LR;
  403. }
  404. }
  405. /* copy data for MP3 frame analyzer */
  406. if (gfp->analysis && gfc->pinfo != NULL) {
  407. for ( gr = 0; gr < gfc->mode_gr; gr++ ) {
  408. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  409. gfc->pinfo->ms_ratio[gr]=gfc->ms_ratio[gr];
  410. gfc->pinfo->ms_ener_ratio[gr]=ms_ener_ratio[gr];
  411. gfc->pinfo->blocktype[gr][ch]=
  412. gfc->l3_side.gr[gr].ch[ch].tt.block_type;
  413. memcpy(gfc->pinfo->xr[gr][ch],xr[gr][ch],sizeof(xr[gr][ch]));
  414. /* in psymodel, LR and MS data was stored in pinfo.
  415. switch to MS data: */
  416. if (gfc->mode_ext==MPG_MD_MS_LR) {
  417. gfc->pinfo->pe[gr][ch]=gfc->pinfo->pe[gr][ch+2];
  418. gfc->pinfo->ers[gr][ch]=gfc->pinfo->ers[gr][ch+2];
  419. memcpy(gfc->pinfo->energy[gr][ch],gfc->pinfo->energy[gr][ch+2],
  420. sizeof(gfc->pinfo->energy[gr][ch]));
  421. }
  422. }
  423. }
  424. }
  425. /* bit and noise allocation */
  426. if (MPG_MD_MS_LR == gfc->mode_ext) {
  427. masking = &masking_MS; /* use MS masking */
  428. pe_use = &pe_MS;
  429. } else {
  430. masking = &masking_LR; /* use LR masking */
  431. pe_use = &pe;
  432. }
  433. if (gfc->nsPsy.use && (gfp->VBR == vbr_off || gfp->VBR == vbr_abr)) {
  434. static FLOAT fircoef[19] = {
  435. -0.0207887,-0.0378413,-0.0432472,-0.031183,
  436. 7.79609e-18,0.0467745,0.10091,0.151365,
  437. 0.187098,0.2,0.187098,0.151365,
  438. 0.10091,0.0467745,7.79609e-18,-0.031183,
  439. -0.0432472,-0.0378413,-0.0207887,
  440. };
  441. int i;
  442. FLOAT8 f;
  443. for(i=0;i<18;i++) gfc->nsPsy.pefirbuf[i] = gfc->nsPsy.pefirbuf[i+1];
  444. i=0;
  445. gfc->nsPsy.pefirbuf[18] = 0;
  446. for ( gr = 0; gr < gfc->mode_gr; gr++ ) {
  447. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  448. gfc->nsPsy.pefirbuf[18] += (*pe_use)[gr][ch];
  449. i++;
  450. }
  451. }
  452. gfc->nsPsy.pefirbuf[18] = gfc->nsPsy.pefirbuf[18] / i;
  453. f = 0;
  454. for(i=0;i<19;i++) f += gfc->nsPsy.pefirbuf[i] * fircoef[i];
  455. for ( gr = 0; gr < gfc->mode_gr; gr++ ) {
  456. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  457. (*pe_use)[gr][ch] *= 670 / f;
  458. }
  459. }
  460. }
  461. switch (gfp->VBR){
  462. default:
  463. case vbr_off:
  464. iteration_loop( gfp,*pe_use,ms_ener_ratio, xr, *masking, l3_enc, scalefac);
  465. break;
  466. case vbr_mt:
  467. VBR_quantize( gfp,*pe_use,ms_ener_ratio, xr, *masking, l3_enc, scalefac);
  468. break;
  469. case vbr_rh:
  470. case vbr_mtrh:
  471. VBR_iteration_loop( gfp,*pe_use,ms_ener_ratio, xr, *masking, l3_enc, scalefac);
  472. break;
  473. case vbr_abr:
  474. ABR_iteration_loop( gfp,*pe_use,ms_ener_ratio, xr, *masking, l3_enc, scalefac);
  475. break;
  476. }
  477. /* write the frame to the bitstream */
  478. getframebits(gfp, &bitsPerFrame, &mean_bits);
  479. format_bitstream( gfp, bitsPerFrame, l3_enc, scalefac);
  480. /* copy mp3 bit buffer into array */
  481. mp3count = copy_buffer(mp3buf,mp3buf_size,&gfc->bs);
  482. if (gfp->bWriteVbrTag) AddVbrFrame(gfp);
  483. /* copy data for MP3 frame analyzer */
  484. if (gfp->analysis && gfc->pinfo != NULL) {
  485. int j;
  486. for ( ch = 0; ch < gfc->channels_out; ch++ ) {
  487. for ( j = 0; j < FFTOFFSET; j++ )
  488. gfc->pinfo->pcmdata[ch][j] = gfc->pinfo->pcmdata[ch][j+gfp->framesize];
  489. for ( j = FFTOFFSET; j < 1600; j++ ) {
  490. gfc->pinfo->pcmdata[ch][j] = inbuf[ch][j-FFTOFFSET];
  491. }
  492. }
  493. set_frame_pinfo (gfp, xr, *masking, l3_enc, scalefac);
  494. }
  495. updateStats( gfc );
  496. return mp3count;
  497. }