2 (c) Copyright 1998-2000 - Tord Jansson
3 ======================================
5 This file is part of the BladeEnc MP3 Encoder, based on
6 ISO's reference code for MPEG Layer 3 compression, and might
7 contain smaller or larger sections that are directly taken
8 from ISO's reference code.
10 All changes to the ISO reference code herein are either
11 copyrighted by Tord Jansson (tord.jansson@swipnet.se)
12 or sublicensed to Tord Jansson by a third party.
14 BladeEnc is free software; you can redistribute this file
15 and/or modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2.1 of the License, or (at your option) any later version.
21 ------------ CHANGES ------------
23 2000-11-22 Andre Piotrowski
25 - big fix: module - original dist10 error - l3_sb_sample has to be of type 'L3SBS' not 'SBS'.
26 - bug fix: codecInit() - don't reset fInit-huffman_read_flag - read_huffcodetab() is a memory eater!
31 - speed up: use single buffer
40 #include "reservoir.h"
41 #include "formatbitstream2.h"
42 #include "l3bitstream.h"
57 /************************************************************************/
59 #define SAMPLES_PER_FRAME 1152
61 static L3SBS l3_sb_sample;
68 static short buffer[2][1152];
69 /* static float snr32[32]; */
70 static short sam[2][2048];
72 static FLOAT buffer[2][2048];
73 static int buffer_idx;
80 static double frac_SpF, slot_lag;
82 static int stereo, error_protection;
84 static III_side_info_t l3_side;
85 static CodecInitOut sOut;
87 static frame_params fr_ps;
94 volatile double avg_slots_per_frame;
100 /*____ codecInit() ____________________________________________________________*/
102 CodecInitOut *codecInit (CodecInitIn *psIn)
108 switch (psIn->frequency)
110 case 48000: info.sampling_frequency = 1; break;
111 case 44100: info.sampling_frequency = 0; break;
112 case 32000: info.sampling_frequency = 2; break;
113 default : return FALSE;
118 case 0: info.mode = MPG_MD_STEREO ; info.mode_ext = 0; break;
119 case 2: info.mode = MPG_MD_DUAL_CHANNEL; info.mode_ext = 0; break;
120 case 3: info.mode = MPG_MD_MONO ; info.mode_ext = 0; break;
121 default: return FALSE;
125 while (j < 15 && bitratex[1][j] != psIn->bitrate)
127 info.bitrate_index = j;
129 info.version = 1; /* Default: MPEG-1 */
130 info.emphasis = psIn->emphasis;
131 info.extension = psIn->fPrivate;
132 info.copyright = psIn->fCopyright;
133 info.original = psIn->fOriginal;
134 info.error_protection = psIn->fCRC;
137 /*_______ Static-fix _______________*/
145 fInit_iteration_loop = 0;
146 /* DON'T DO THAT ALL THE TIME
147 fInit_huffman_read_flag = 0;
152 l3_side.main_data_begin = 0;
153 fixStatic_reservoir();
156 /*___________________________________*/
158 psycho_anal_init (psIn->frequency);
159 initWindowFilterSubband ();
160 initFormatBitstream ();
163 memset ((char *) l3_sb_sample, 0, sizeof(l3_sb_sample));
164 memset((char *) buffer, 0, sizeof(buffer));
165 /* memset((char *) snr32, 0, sizeof(snr32));*/
167 memset((char *) sam, 0, sizeof(sam));
172 fr_ps.header = &info;
173 fr_ps.tab_num = -1; /* no table loaded */
175 fr_ps.actual_mode = info.mode;
176 fr_ps.stereo = (info.mode == MPG_MD_MONO) ? 1 : 2;
177 fr_ps.sblimit = SBLIMIT;
178 fr_ps.jsbound = SBLIMIT;
181 stereo = fr_ps.stereo;
182 error_protection = info.error_protection;
184 avg_slots_per_frame =
185 ((double) SAMPLES_PER_FRAME / s_freq[1][info.sampling_frequency]) *
186 ((double) bitratex[1][info.bitrate_index] / 8.0);
187 whole_SpF = (int) avg_slots_per_frame;
188 frac_SpF = avg_slots_per_frame - (double) whole_SpF;
189 slot_lag = -frac_SpF;
191 /* if (frac_SpF == 0)
196 /*________________________*/
200 sOut.nSamples = SAMPLES_PER_FRAME;
202 sOut.nSamples = SAMPLES_PER_FRAME*2;
204 sOut.bufferSize = 2048;
206 return &sOut; /* How many samples we want in each chunk... */
213 /*____ codecEncodeChunk() _____________________________________________________*/
215 unsigned int codecEncodeChunk
222 static double xr[2][2][576];
223 static double xr_dec[2][2][576];
224 static double pe[2][2];
225 static int l3_enc[2][2][576];
226 static III_psy_ratio ratio;
227 static III_scalefac_t scalefac;
229 int mean_bits, sideinfo_len;
234 static int fFirst = TRUE;
238 memset ((char *) &xr , 0, sizeof(xr));
239 memset ((char *) &xr_dec , 0, sizeof(xr_dec));
240 memset ((char *) &pe , 0, sizeof(pe));
241 memset ((char *) &l3_enc , 0, sizeof(l3_enc));
242 memset ((char *) &ratio , 0, sizeof(ratio));
243 memset ((char *) &l3_side , 0, sizeof(l3_side));
244 memset ((char *) &scalefac, 0, sizeof(scalefac));
254 rebuffer_audio (buffer, pSamples, nSamples, stereo);
256 rebuffer_audio (pSamples, buffer, &buffer_idx, nSamples, stereo);
260 /* psychoacoustic model */
262 for (gr = 0; gr < 2; gr++)
263 for (ch = 0; ch < stereo; ch++)
271 (buffer_idx+gr*576) & 2047,
279 &l3_side.gr[gr].ch[ch].tt
283 /* polyphase filtering */
285 for (gr = 0; gr < 2; gr++)
287 int gr_plus_1 = gr_idx[gr+1];
289 for (ch = 0; ch < stereo; ch++)
291 for (j = 0; j < 18; j++)
296 &buffer[ch][gr*18*32+32*j],
300 (buffer_idx+768-480+gr*18*32+32*j) & 2047,
302 l3_sb_sample[ch][gr_plus_1][j]
309 /* apply mdct to the polyphase outputs */
311 mdct_sub (&l3_sb_sample, xr, stereo, &l3_side, 2);
314 pEncodedOutput = pDest;
316 pEncodedOutput[0] = 0;
321 if (slot_lag > (frac_SpF-1.0))
323 slot_lag -= frac_SpF;
329 slot_lag += (1-frac_SpF);
333 bitsPerFrame = 8 * whole_SpF + (info.padding * 8);
336 /* determine the mean bitrate for main data */
345 if (info.error_protection)
348 mean_bits = (bitsPerFrame - sideinfo_len) / 2;
351 /* bit and noise allocation */
370 /* write the frame to the bitstream */
385 return pEncodedOutput - pDest;
392 /*____ codecExit() ____________________________________________________________*/
394 unsigned int codecExit (char *pDest)
396 pEncodedOutput = pDest;
398 pEncodedOutput[0] = 0;
401 exitFormatBitstream ();
402 III_FlushBitstream ();
404 return pEncodedOutput - pDest;
411 /*____ codecFlush() _________________________________________________________*/
413 unsigned int codecFlush (char *pDest)
415 pEncodedOutput = pDest;
417 pEncodedOutput[0] = 0;
421 whole_SpF = (int) avg_slots_per_frame;
422 frac_SpF = avg_slots_per_frame - (double) whole_SpF;
423 slot_lag = -frac_SpF;
425 l3_side.main_data_begin = 0;
426 fixStatic_reservoir ();
428 return pEncodedOutput - pDest;