Version 0.94.1 of the bladeenc mp3 encoder
[swftools.git] / lib / bladeenc / codec.c
1 /*
2                         (c) Copyright 1998-2000 - Tord Jansson
3                         ======================================
4
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.
9
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.
13
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.
18
19
20
21         ------------    CHANGES    ------------
22
23         2000-11-22  Andre Piotrowski
24
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!
27
28         2000-12-11  ap
29
30         -       reformatted
31         -       speed up: use single buffer
32 */
33
34 #include        <stdlib.h>
35 #include        <assert.h>
36
37 #include        "common.h"
38 #include        "l3psy.h"
39 #include        "mdct.h"
40 #include        "reservoir.h"
41 #include        "formatbitstream2.h"
42 #include        "l3bitstream.h"
43 #include        "loop.h"
44 #include        "encoder.h"
45 #include        "codec.h"
46
47
48
49
50
51 extern  int                             fInit_fft;
52
53
54
55
56
57 /************************************************************************/
58
59 #define SAMPLES_PER_FRAME               1152
60
61 static  L3SBS                                   l3_sb_sample;
62
63 static  layer                                   info;
64
65
66
67 #if ORG_BUFFERS
68 static  short                                   buffer[2][1152];
69 /*      static  float                                   snr32[32]; */
70 static  short                                   sam[2][2048];
71 #else
72 static  FLOAT                                   buffer[2][2048];
73 static  int                                             buffer_idx;
74 #endif
75
76
77
78 static  int                                             whole_SpF;
79
80 static  double                                  frac_SpF, slot_lag;
81
82 static  int                                             stereo, error_protection;
83
84 static  III_side_info_t                 l3_side;
85 static  CodecInitOut                    sOut;
86
87 static  frame_params                    fr_ps;
88
89
90
91 char                                    *pEncodedOutput;
92 int                                             outputBit;
93
94 volatile double                 avg_slots_per_frame;
95
96
97
98
99
100 /*____ codecInit() ____________________________________________________________*/
101
102 CodecInitOut                    *codecInit (CodecInitIn *psIn)
103 {
104         int                                             j;
105
106         /* Read psIn */
107
108         switch (psIn->frequency)
109         {
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;
114         }
115
116         switch (psIn->mode)
117         {
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;
122         }
123
124         j = 0;
125         while (j < 15  &&  bitratex[1][j] != psIn->bitrate)
126                 j++;
127         info.bitrate_index    = j;
128
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;
135
136
137 /*_______ Static-fix _______________*/
138
139
140         fInit_mdct_sub = 0;
141         fInit_mdct = 0;
142
143         fInit_fft = 0;
144
145         fInit_iteration_loop = 0;
146 /*      DON'T DO THAT ALL THE TIME
147         fInit_huffman_read_flag = 0;
148 */
149
150         fixStatic_loop();
151
152         l3_side.main_data_begin = 0;
153         fixStatic_reservoir();
154
155
156 /*___________________________________*/
157
158     psycho_anal_init (psIn->frequency);
159         initWindowFilterSubband ();
160         initFormatBitstream ();
161
162 /*     clear buffers */
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));*/
166 #if ORG_BUFFERS
167     memset((char *) sam, 0, sizeof(sam));
168 #endif
169
170
171
172     fr_ps.header          = &info;
173     fr_ps.tab_num         = -1;   /* no table loaded */
174     fr_ps.alloc           = NULL;
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;
179
180     
181     stereo = fr_ps.stereo;
182     error_protection = info.error_protection;
183
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;
190
191 /*    if (frac_SpF == 0)
192         info.padding = 0;
193 */
194         genNoisePowTab();
195
196 /*________________________*/
197
198
199         if( stereo != 2 )
200                 sOut.nSamples = SAMPLES_PER_FRAME;
201         else
202                 sOut.nSamples = SAMPLES_PER_FRAME*2;
203
204         sOut.bufferSize = 2048;
205
206         return  &sOut;                  /* How many samples we want in each chunk... */
207 }
208
209
210
211
212
213 /*____ codecEncodeChunk() _____________________________________________________*/
214
215 unsigned int                    codecEncodeChunk
216 (
217         int                                             nSamples,
218         short                                   *pSamples,
219         char                                    *pDest
220 )
221 {
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;
228         int                                                     gr, ch;
229         int                                                     mean_bits, sideinfo_len;
230         int                                                     bitsPerFrame;
231         int                                                     j;
232
233 /*
234         static  int                                     fFirst = TRUE;
235
236         if (fFirst)
237         {
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));
245
246                 fFirst = FALSE;
247         }
248 */
249
250
251 /* rebuffer audio */
252
253 #if ORG_BUFFERS
254         rebuffer_audio (buffer, pSamples, nSamples, stereo);
255 #else
256         rebuffer_audio (pSamples, buffer, &buffer_idx, nSamples, stereo);
257 #endif
258
259
260 /* psychoacoustic model */
261
262         for (gr = 0;  gr < 2;  gr++)
263                 for (ch = 0;  ch < stereo;  ch++)
264                         psycho_anal
265                         (
266 #if ORG_BUFFERS
267                                 &buffer[ch][gr*576],
268                                 &sam[ch][0],
269 #else
270                         buffer[ch],
271                         (buffer_idx+gr*576) & 2047,
272 #endif
273                                 ch,
274                                 3,
275                                 /*snr32,*/
276                                 &ratio.l[gr][ch][0],
277                                 &ratio.s[gr][ch][0],
278                                 &pe[gr][ch],
279                                 &l3_side.gr[gr].ch[ch].tt
280                         );
281
282
283 /* polyphase filtering */
284
285         for (gr = 0;  gr < 2;  gr++)
286         {
287                 int             gr_plus_1 = gr_idx[gr+1];
288
289                 for (ch = 0;  ch < stereo;  ch++)
290                 {
291                         for (j = 0;  j < 18;  j++)
292                         {
293                                 windowFilterSubband
294                                 (
295 #if ORG_BUFFERS
296                                         &buffer[ch][gr*18*32+32*j],
297                                         ch,
298 #else
299                                         buffer[ch],
300                                         (buffer_idx+768-480+gr*18*32+32*j) & 2047,
301 #endif
302                                         l3_sb_sample[ch][gr_plus_1][j]
303                                 );
304                         }
305                 }
306         }
307
308
309 /* apply mdct to the polyphase outputs */
310
311         mdct_sub (&l3_sb_sample, xr, stereo, &l3_side, 2);
312
313
314         pEncodedOutput = pDest;
315         outputBit = 8;
316         pEncodedOutput[0] = 0;
317
318
319         if (frac_SpF != 0)
320         {
321                 if (slot_lag > (frac_SpF-1.0))
322                 {
323                         slot_lag -= frac_SpF;
324                         info.padding = 0;
325                 }
326                 else
327                 {
328                         info.padding = 1;
329                         slot_lag += (1-frac_SpF);
330                 }
331         }
332
333         bitsPerFrame = 8 * whole_SpF + (info.padding * 8);
334
335
336 /* determine the mean bitrate for main data */
337
338         sideinfo_len = 32;
339
340         if (stereo == 1)
341                 sideinfo_len += 136;
342         else
343                 sideinfo_len += 256;
344
345         if (info.error_protection)
346                 sideinfo_len += 16;
347
348         mean_bits = (bitsPerFrame - sideinfo_len) / 2;
349
350
351 /* bit and noise allocation */
352
353         iteration_loop
354         (
355                 pe,
356                 xr,
357                 &ratio,
358                 &l3_side,
359                 l3_enc,
360                 mean_bits,
361                 stereo,
362                 xr_dec,
363                 &scalefac,
364                 &fr_ps,
365                 0,
366                 bitsPerFrame
367         );
368
369
370 /* write the frame to the bitstream */
371
372         III_format_bitstream
373         (
374                 bitsPerFrame,
375                 &fr_ps,
376                 l3_enc,
377                 &l3_side,
378                 &scalefac,
379                 xr,
380                 NULL,
381                 0
382         );
383
384
385         return  pEncodedOutput - pDest;
386 }
387
388
389
390
391
392 /*____ codecExit() ____________________________________________________________*/
393
394 unsigned int                    codecExit (char *pDest)
395 {
396         pEncodedOutput = pDest;
397         outputBit = 8;
398         pEncodedOutput[0] = 0;
399
400         psycho_anal_exit ();
401         exitFormatBitstream ();
402         III_FlushBitstream ();
403
404         return pEncodedOutput - pDest;
405 }
406
407
408
409
410
411 /*____ codecFlush() _________________________________________________________*/
412
413 unsigned int                    codecFlush (char *pDest)
414 {
415         pEncodedOutput = pDest;
416         outputBit = 8;
417         pEncodedOutput[0] = 0;
418
419         flushFrame ();
420
421         whole_SpF = (int) avg_slots_per_frame;
422         frac_SpF  = avg_slots_per_frame - (double) whole_SpF;
423         slot_lag  = -frac_SpF;
424
425         l3_side.main_data_begin = 0;
426         fixStatic_reservoir ();
427
428         return pEncodedOutput - pDest;
429 }
430
431
432