Debian Codegraph @

libtwolame/twolame.c

raw
1 /*
2 * TwoLAME: an optimized MPEG Audio Layer Two encoder
3 *
4 * Copyright (C) 2001-2004 Michael Cheng
5 * Copyright (C) 2004-2018 The TwoLAME Project
6 *
7 * This library 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 * This library 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 this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <assert.h>
28 #include <limits.h>
29
30 #include "twolame.h"
31 #include "common.h"
32 #include "bitbuffer.h"
33 #include "mem.h"
34 #include "crc.h"
35 #include "dab.h"
36 #include "psycho_n1.h"
37 #include "psycho_0.h"
38 #include "psycho_1.h"
39 #include "psycho_2.h"
40 #include "psycho_3.h"
41 #include "psycho_4.h"
42 #include "availbits.h"
43 #include "subband.h"
44 #include "encode.h"
45 #include "energy.h"
46 #include "util.h"
47
48 #include "bitbuffer_inline.h"
49
50
51 /*
52 twolame_init
53 Create a set of encoding options and return a pointer to this structure
54
55 Returns NULL if unsuccessful (can't allocate memory)
56 Otherwise returns pointer to memory block
57 */
58 twolame_options *twolame_init(void)
59 {
60 twolame_options *newoptions = NULL;
61
62 newoptions = (twolame_options *) TWOLAME_MALLOC(sizeof(twolame_options));
63 if (newoptions == NULL) {
64 return NULL;
65 }
66
67 memset(newoptions, 0, sizeof(twolame_options));
68
69 newoptions->version = -1;
70 newoptions->num_channels_in = 0;
71 newoptions->num_channels_out = 0;
72 newoptions->samplerate_in = 0;
73 newoptions->samplerate_out = 0;
74
75 newoptions->mode = TWOLAME_AUTO_MODE; // Choose a proper mode later
76 newoptions->psymodel = 3;
77 newoptions->bitrate = -1; // Default bitrate is set in init_params
78 newoptions->vbr = FALSE;
79 newoptions->freeformat = FALSE;
80 newoptions->vbrlevel = 5.0;
81 newoptions->athlevel = 0.0;
82
83 newoptions->quickmode = FALSE;
84 newoptions->quickcount = 10;
85 newoptions->emphasis = TWOLAME_EMPHASIS_N;
86 newoptions->private_extension = 0;
87 newoptions->copyright = FALSE;
88 newoptions->original = TRUE;
89 newoptions->error_protection = FALSE;
90 newoptions->padding = TWOLAME_PAD_NO;
91 newoptions->do_dab = FALSE;
92 newoptions->dab_crc_len = 2;
93 newoptions->dab_xpad_len = 0;
94 newoptions->verbosity = 2;
95 newoptions->vbr_upper_index = 0;
96
97 newoptions->slots_lag = 0.0;
98
99 newoptions->scale = 1.0; // scaling disabled
100 newoptions->scale_left = 1.0; // scaling disabled
101 newoptions->scale_right = 1.0; // scaling disabled
102
103 newoptions->do_energy_levels = FALSE;
104 newoptions->num_ancillary_bits = -1;
105
106 newoptions->vbr_frame_count = 0; // only used for debugging
107 newoptions->tablenum = 0;
108
109 newoptions->twolame_init = 0;
110 newoptions->subband = NULL;
111 newoptions->j_sample = NULL;
112 newoptions->sb_sample = NULL;
113 newoptions->psycount = 0;
114
115 newoptions->p0mem = NULL;
116 newoptions->p1mem = NULL;
117 newoptions->p2mem = NULL;
118 newoptions->p3mem = NULL;
119 newoptions->p4mem = NULL;
120
121 return (newoptions);
122 }
123
124
125
126 // Returns 0 if successful
127 // Returns -1 if unsuccessful
128 static int init_header_info(twolame_options * glopts)
129 {
130 frame_header *header = &glopts->header;
131
132 /* use the options to create header info structure */
133 header->lay = 2;
134 header->error_protection = glopts->error_protection;
135 header->version = glopts->version;
136
137 // Convert the sampling frequency to the required index
138 header->samplerate_idx = twolame_get_samplerate_index(glopts->samplerate_out);
139 if (header->samplerate_idx < 0) {
140 fprintf(stderr, "Not a valid samplerate: %i\n", glopts->samplerate_out);
141 return -1;
142 }
143
144 // Convert the bitrate to the an index
145 if (glopts->freeformat)
146 header->bitrate_index = 0;
147 else
148 {
149 header->bitrate_index = twolame_get_bitrate_index(glopts->bitrate, header->version);
150 if (header->bitrate_index < 0) {
151 fprintf(stderr, "Not a valid bitrate (%i) for MPEG version '%s'\n", glopts->bitrate,
152 twolame_mpeg_version_name(glopts->version));
153 return -1;
154 }
155 }
156 if (glopts->vbr && glopts->vbr_max_bitrate > 0)
157 {
158 /* user required vbr and a specified max bitrate */
159 /* convert max VBR bitrate to an index */
160 glopts->vbr_upper_index = twolame_get_bitrate_index(glopts->vbr_max_bitrate, header->version);
161 if (glopts->vbr_upper_index < 0) {
162 fprintf(stderr, "Not a valid max VBR bitrate for this version: %i\n",
163 glopts->vbr_max_bitrate);
164 return -1;
165 }
166 }
167 // Copy accross the other settings
168 header->padding = 0; /* when requested, padding will be evaluated later for this frame */
169 header->private_extension = glopts->private_extension;
170 header->mode = glopts->mode;
171 header->mode_ext = 0;
172 header->copyright = glopts->copyright;
173 header->original = glopts->original;
174 header->emphasis = glopts->emphasis;
175
176 return 0;
177 }
178
179
180
181
182 /**
183 * This function should actually *check* the parameters to see if they
184 * make sense.
185 */
186
187 int twolame_init_params(twolame_options * glopts)
188 {
189
190 if (glopts->twolame_init) {
191 fprintf(stderr, "Already called twolame_init_params() once.\n");
192 return 1;
193 }
194 // Check the number of channels
195 if (glopts->num_channels_in != 1 && glopts->num_channels_in != 2) {
196 if (glopts->num_channels_in == 0) {
197 /* this error is due to the caller frontend */
198 fprintf(stderr,
199 "twolame_init_params(): must specify number of input channels using twolame_set_num_channels().\n");
200 }
201 else {
202 /* this error is due to the user feeding a wrong file */
203 fprintf(stderr,
204 "Error: twolame cannot encode files with more than 2 channels.\n");
205 }
206 return -1;
207 }
208 // If not output samplerate has been set, then set it to the input sample rate
209 if (glopts->samplerate_out < 1) {
210 glopts->samplerate_out = glopts->samplerate_in;
211 }
212 // If the MPEG version has not been set, then choose automatically
213 if (glopts->version == -1) {
214 // Get the MPEG version for the chosen samplerate
215 glopts->version = twolame_get_version_for_samplerate(glopts->samplerate_out);
216 if (glopts->version < 0) {
217 fprintf(stderr, "twolame_init_params(): invalid samplerate: %i\n",
218 glopts->samplerate_out);
219 return -1;
220 } else if (glopts->verbosity >= 3) {
221 fprintf(stderr, "Chosen version '%s' for samplerate of %d Hz.\n",
222 twolame_mpeg_version_name(glopts->version), glopts->samplerate_out);
223 }
224 }
225 // Choose mode (if none chosen)
226 if (glopts->mode == TWOLAME_AUTO_MODE) {
227 if (glopts->num_channels_in == 2)
228 glopts->mode = TWOLAME_STEREO;
229 else
230 glopts->mode = TWOLAME_MONO;
231 if (glopts->verbosity >= 3) {
232 fprintf(stderr, "Chosen mode to be '%s' because of %d input channels.\n",
233 twolame_get_mode_name(glopts), glopts->num_channels_in);
234 }
235 }
236 // Choose the bitrate (if none chosen)
237 if (glopts->bitrate < 0 && !glopts->vbr) {
238 if (glopts->mode == TWOLAME_MONO) {
239 switch (glopts->samplerate_out) {
240 case 48000:
241 glopts->bitrate = 96;
242 break; // (LAME=64)
243 case 44100:
244 glopts->bitrate = 96;
245 break; // (LAME=64)
246 case 32000:
247 glopts->bitrate = 80;
248 break; // (LAME=48)
249 case 24000:
250 glopts->bitrate = 48;
251 break; // (LAME=32)
252 case 22050:
253 glopts->bitrate = 48;
254 break; // (LAME=32)
255 case 16000:
256 glopts->bitrate = 32;
257 break; // (LAME=24)
258 }
259 } else {
260 switch (glopts->samplerate_out) {
261 case 48000:
262 glopts->bitrate = 192;
263 break; // (LAME=128)
264 case 44100:
265 glopts->bitrate = 192;
266 break; // (LAME=128)
267 case 32000:
268 glopts->bitrate = 160;
269 break; // (LAME=96)
270 case 24000:
271 glopts->bitrate = 96;
272 break; // (LAME=64)
273 case 22050:
274 glopts->bitrate = 96;
275 break; // (LAME=64)
276 case 16000:
277 glopts->bitrate = 64;
278 break; // (LAME=48)
279 }
280 }
281 if (glopts->verbosity >= 3) {
282 fprintf(stderr, "Chosen bitrate of %dkbps for samplerate of %d Hz.\n",
283 glopts->bitrate, glopts->samplerate_out);
284 }
285 glopts->freeformat = FALSE; // no sense in requiring freeformat encoding without setting a bitrate
286 }
287 if (glopts->bitrate < 0 && glopts->vbr) {
288 /* set the minimum bitrate - 'init_bit_allocation' will fix it if needed */
289 glopts->bitrate = twolame_index_bitrate((int)glopts->version, 1);
290 }
291
292 /* Check for bitrate validity */
293 if (glopts->version == TWOLAME_MPEG1
294 &&
295 !glopts->freeformat
296 &&
297 !glopts->vbr) {
298 /* some limitation apply for MPEG1 when CBR and freeformat is not selected */
299 if (glopts->mode == TWOLAME_MONO) {
300 if (glopts->bitrate > 192) {
301 fprintf(stderr, "twolame_init_params(): %dkbps is an invalid bitrate for mono encoding.\n",
302 glopts->bitrate);
303 return -1;
304 }
305 }
306 else {
307 if (glopts->bitrate < 64 || glopts->bitrate == 80) {
308 fprintf(stderr, "twolame_init_params(): %dkbps is an invalid bitrate for 2ch encoding.\n",
309 glopts->bitrate);
310 return -1;
311 }
312 }
313 }
314
315 /* Can't do DAB and energylevel extensions at the same time Because both of them think they're
316 the only ones inserting information into the ancillary section of the frame */
317 if (glopts->do_dab && glopts->do_energy_levels) {
318 fprintf(stderr, "Error: Can't do DAB and Energy Levels at the same time\n");
319 return -1;
320 }
321
322 /* Set the number of ancillary bits automatically, if none set */
323 if (glopts->num_ancillary_bits < 0) {
324 if (glopts->do_energy_levels) {
325 glopts->num_ancillary_bits = twolame_get_required_energy_bits(glopts);
326 } else {
327 glopts->num_ancillary_bits = 0;
328 }
329 }
330
331 /* Check that if we're doing energy levels, that there's enough space to put the information */
332 if (glopts->do_energy_levels) {
333 int required = twolame_get_required_energy_bits(glopts);
334 if (glopts->num_ancillary_bits < required) {
335 fprintf(stderr, "Warning: Too few ancillary bits to store energy levels: %i<%i\n",
336 glopts->num_ancillary_bits, required);
337 return -1;
338 }
339 }
340
341 /*
342 * MFC Feb 2003: in VBR mode, joint
343 * stereo doesn't make any sense at
344 * the moment, as there are no noisy
345 * subbands according to
346 * bits_for_nonoise in vbr mode
347 */
348 if (glopts->vbr && glopts->mode == TWOLAME_JOINT_STEREO) {
349 fprintf(stderr, "Warning: Can't do Joint Stereo with VBR, switching to normal stereo.\n");
350
351 // force stereo mode
352 twolame_set_mode(glopts, TWOLAME_STEREO);
353 }
354
355 /* Can't do padding and VBR at same time */
356 if (glopts->vbr && glopts->padding == TRUE) {
357 fprintf(stderr, "Error: Can't do padding and VBR at same time\n");
358 return -1;
359 }
360
361 /* Simple patch for the `bit_stream buffer needs to be bigger' warning */
362 /* Fix FREEFORMAT_MAX_BITRATE definition when github issue #51 will be closed */
363 if (glopts->freeformat && glopts->bitrate > FREEFORMAT_MAX_BITRATE) {
364 fprintf(stderr, "twolame_init_params(): cannot encode freeformat stream at %d kbps\n", glopts->bitrate);
365 return -1;
366 }
367
368 // Set the Number of output channels
369 glopts->num_channels_out = (glopts->mode == TWOLAME_MONO) ? 1 : 2;
370
371
372
373 // build mpeg header from parameters
374 if (init_header_info(glopts) < 0) {
375 return -1;
376 }
377 // initialise bitrate allocation
378 if (twolame_init_bit_allocation(glopts) < 0) {
379 return -1;
380 }
381 // Select table number and sblimit
382 if (twolame_encode_init(glopts) < 0) {
383 return -1;
384 }
385 // Check input samplerate is same as output samplerate
386 if (glopts->samplerate_out != glopts->samplerate_in) {
387 fprintf(stderr,
388 "twolame_init_params(): sorry, twolame doesn't support resampling (yet).\n");
389 return -1;
390 }
391
392 // Initialise interal variables
393 glopts->samples_in_buffer = 0;
394 glopts->psycount = 0;
395
396
397 // Allocate memory to larger buffers
398 glopts->subband = (subband_t *) TWOLAME_MALLOC(sizeof(subband_t));
399 glopts->j_sample = (jsb_sample_t *) TWOLAME_MALLOC(sizeof(jsb_sample_t));
400 glopts->sb_sample = (sb_sample_t *) TWOLAME_MALLOC(sizeof(sb_sample_t));
401 if (glopts->subband == NULL
402 ||
403 glopts->j_sample == NULL
404 ||
405 glopts->sb_sample == NULL)
406 {
407 TWOLAME_FREE(glopts->subband);
408 TWOLAME_FREE(glopts->j_sample);
409 TWOLAME_FREE(glopts->sb_sample);
410 return -1;
411 }
412
413 // clear buffers
414 memset((char *) glopts->buffer, 0, sizeof(glopts->buffer));
415 memset((char *) glopts->bit_alloc, 0, sizeof(glopts->bit_alloc));
416 memset((char *) glopts->scfsi, 0, sizeof(glopts->scfsi));
417 memset((char *) glopts->scalar, 0, sizeof(glopts->scalar));
418 memset((char *) glopts->j_scale, 0, sizeof(glopts->j_scale));
419 memset((char *) glopts->smrdef, 0, sizeof(glopts->smrdef));
420 memset((char *) glopts->smr, 0, sizeof(glopts->smr));
421 memset((char *) glopts->max_sc, 0, sizeof(glopts->max_sc));
422
423 // Initialise subband windowfilter
424 if (twolame_init_subband(&glopts->smem) < 0) {
425 return -1;
426 }
427 // All initalised now :)
428 glopts->twolame_init++;
429
430 return (0);
431 }
432
433
434 /* Scale the samples in the frame sample buffer
435 using the user specified values
436 and downmix/upmix according to the number of input/output channels
437 */
438 static void scale_and_mix_samples(twolame_options * glopts)
439 {
440 int num_samples = glopts->samples_in_buffer;
441 int i;
442
443 // apply scaling to both channels
444 if (glopts->scale != 0 && glopts->scale != 1.0) {
445 if (glopts->num_channels_in == 2)
446 for (i = 0; i < num_samples; ++i) {
447 glopts->buffer[0][i] *= glopts->scale;
448 glopts->buffer[1][i] *= glopts->scale;
449 }
450 else
451 for (i = 0; i < num_samples; ++i)
452 glopts->buffer[0][i] *= glopts->scale;
453 }
454 // apply scaling to channel 0 (left)
455 if (glopts->scale_left != 0 && glopts->scale_left != 1.0) {
456 for (i = 0; i < num_samples; ++i) {
457 glopts->buffer[0][i] *= glopts->scale_left;
458 }
459 }
460 // apply scaling to channel 1 (right)
461 if (glopts->scale_right != 0 && glopts->scale_right != 1.0) {
462 for (i = 0; i < num_samples; ++i) {
463 glopts->buffer[1][i] *= glopts->scale_right;
464 }
465 }
466 // Downmix to Mono if 2 channels in and 1 channel out
467 if (glopts->num_channels_in == 2 && glopts->num_channels_out == 1) {
468 for (i = 0; i < num_samples; ++i) {
469 glopts->buffer[0][i] = ((long) glopts->buffer[0][i] + glopts->buffer[1][i]) / 2;
470 glopts->buffer[1][i] = 0;
471 }
472 }
473 // Upmix to Stereo if 2 channels out and 1 channel in
474 if (glopts->num_channels_in == 1 && glopts->num_channels_out == 2) {
475 for (i = 0; i < num_samples; ++i) {
476 glopts->buffer[1][i] = glopts->buffer[0][i];
477 }
478 }
479
480 }
481
482 /*
483 Encode a single frame of audio from 1152 samples
484 Audio samples are taken from glopts->buffer
485 Encoded bit stream is placed in to parameter bs
486 (not intended for use outside the library)
487
488 Returns the size of the frame
489 or -1 if there is an error
490 */
491 static int encode_frame(twolame_options * glopts, bit_stream * bs)
492 {
493 int nch = glopts->num_channels_out;
494 int sb, ch, adb, i;
495 unsigned long frameBits, initial_bits;
496 short sam[2][1056];
497
498 if (!glopts->twolame_init) {
499 fprintf(stderr, "Please call twolame_init_params() before starting encoding.\n");
500 return -1;
501 }
502 // Scale and mix the input buffer
503 scale_and_mix_samples(glopts);
504
505
506 // Clear the saved audio buffer
507 memset((char *) sam, 0, sizeof(sam));
508
509 // Number of bits to calculate CRC on
510 glopts->num_crc_bits = 0;
511
512 // Store the number of bits initially in the bit buffer
513 initial_bits = twolame_buffer_sstell(bs);
514
515 adb = twolame_available_bits(glopts);
516
517 /* allow the user to reserve some space at the end of the frame This will however leave fewer
518 bits for the audio. Need to do a sanity check here to see that there are *some* bits left. */
519 if (glopts->num_ancillary_bits > 0.6 * adb) {
520 /* Trying to reserve more than 60% of the frame. 0.6 is arbitrary. but since most
521 applications probably only want to reserve a few bytes, this seems fine. Typical frame
522 size is about 800bytes */
523 fprintf(stderr,
524 "You're trying to reserve more than 60%% of the mpeg frame for ancillary data\n");
525 fprintf(stderr, "This is probably an error. But I'll keep going anyway...\n");
526 }
527
528 adb -= glopts->num_ancillary_bits;
529
530
531 /* The DAB scf-crc calc is done below. The frontend will have to keep the previous frame in
532 memory. As of 09May 2014 all that needs to be done is for the frontend to buffer one frame in
533 memory and call twolame_set_DAB_scf_crc */
534
535 {
536 int gr, bl, ch;
537 /* New polyphase filter Combines windowing and filtering. Ricardo Feb'03 */
538 for (gr = 0; gr < 3; gr++)
539 for (bl = 0; bl < 12; bl++)
540 for (ch = 0; ch < nch; ch++)
541 twolame_window_filter_subband(&glopts->smem,
542 &glopts->buffer[ch][gr * 12 * 32 + 32 * bl], ch,
543 &(*glopts->sb_sample)[ch][gr][bl][0]);
544 }
545
546 twolame_scalefactor_calc(*glopts->sb_sample, glopts->scalar, nch, glopts->sblimit);
547 twolame_find_sf_max(glopts, glopts->scalar, glopts->max_sc);
548 if (glopts->mode == TWOLAME_JOINT_STEREO) {
549 // this way we calculate more mono than we need but it is cheap
550 twolame_combine_lr(*glopts->sb_sample, *glopts->j_sample, glopts->sblimit);
551 twolame_scalefactor_calc(glopts->j_sample, &glopts->j_scale, 1, glopts->sblimit);
552 }
553
554 if ((glopts->quickmode == TRUE) && (++glopts->psycount % glopts->quickcount != 0)) {
555 /* We're using quick mode, so we're only calculating the model every 'quickcount' frames.
556 Otherwise, just copy the old ones across */
557 for (ch = 0; ch < nch; ch++) {
558 for (sb = 0; sb < SBLIMIT; sb++) {
559 glopts->smr[ch][sb] = glopts->smrdef[ch][sb];
560 }
561 }
562 } else {
563 // calculate the psymodel
564 switch (glopts->psymodel) {
565 case -1:
566 twolame_psycho_n1(glopts, glopts->smr, nch);
567 break;
568 case 0: // Psy Model A
569 twolame_psycho_0(glopts, glopts->smr, glopts->scalar);
570 break;
571 case 1:
572 twolame_psycho_1(glopts, glopts->buffer, glopts->max_sc, glopts->smr);
573 break;
574 case 2:
575 twolame_psycho_2(glopts, glopts->buffer, sam, glopts->smr);
576 break;
577 case 3:
578 // Modified psy model 1
579 twolame_psycho_3(glopts, glopts->buffer, glopts->max_sc, glopts->smr);
580 break;
581 case 4:
582 // Modified psy model 2
583 twolame_psycho_4(glopts, glopts->buffer, sam, glopts->smr);
584 break;
585 default:
586 fprintf(stderr, "Invalid psy model specification: %i\n", glopts->psymodel);
587 return -1;
588 break;
589 }
590
591 if (glopts->quickmode == TRUE) {
592 // copy the smr values and reuse them later
593 for (ch = 0; ch < nch; ch++) {
594 for (sb = 0; sb < SBLIMIT; sb++)
595 glopts->smrdef[ch][sb] = glopts->smr[ch][sb];
596 }
597 }
598 }
599
600
601 twolame_sf_transmission_pattern(glopts, glopts->scalar, glopts->scfsi);
602 twolame_main_bit_allocation(glopts, glopts->smr, glopts->scfsi, glopts->bit_alloc, &adb);
603
604 twolame_write_header(glopts, bs);
605
606 // Leave space for 2 bytes of CRC to be filled in later
607 if (glopts->error_protection)
608 buffer_putbits(bs, 0, 16);
609
610 twolame_write_bit_alloc(glopts, glopts->bit_alloc, bs);
611 twolame_write_scalefactors(glopts, glopts->bit_alloc, glopts->scfsi, glopts->scalar, bs);
612
613 twolame_subband_quantization(glopts, glopts->scalar, *glopts->sb_sample, glopts->j_scale,
614 *glopts->j_sample, glopts->bit_alloc, *glopts->subband);
615 twolame_write_samples(glopts, *glopts->subband, glopts->bit_alloc, bs);
616
617 // If not all the bits were used, write out a stack of zeros
618 for (i = 0; i < adb; i++)
619 buffer_put1bit(bs, 0);
620
621
622 /* pad the current frame when needed */
623 if (glopts->header.padding)
624 // input file
625 buffer_putbits(bs, 0, 8);
626
627 if (glopts->do_dab) {
628 // Do the CRC calc for DAB stuff if required.
629 // It will be up to the frontend to insert it into the end of the
630 // previous frame.
631 for (i = glopts->dab_crc_len - 1; i >= 0; i--) {
632 twolame_dab_crc_calc(glopts, glopts->bit_alloc, glopts->scfsi, glopts->scalar,
633 &glopts->dab_crc[i], i);
634 }
635 }
636 // Allocate space for the reserved ancillary bits
637 for (i = 0; i < glopts->num_ancillary_bits; i++)
638 buffer_put1bit(bs, 0);
639
640
641 // Calulate the number of bits in this frame
642 frameBits = twolame_buffer_sstell(bs) - initial_bits;
643 if (frameBits % 8) { /* a program failure */
644 fprintf(stderr, "Sent %ld bits = %ld slots plus %ld\n", frameBits, frameBits / 8,
645 frameBits % 8);
646 fprintf(stderr, "If you are reading this, the program is broken\n");
647 fprintf(stderr, "email %s with the command line arguments and other info\n",
648 PACKAGE_BUGREPORT);
649 return -1;
650 }
651 // Store the energy levels at the end of the frame
652 if (glopts->do_energy_levels)
653 twolame_do_energy_levels(glopts, bs);
654
655 // MEANX: Recompute checksum from bitstream
656 if (glopts->error_protection) {
657 unsigned char *frame_ptr = bs->buf + (initial_bits >> 3);
658 twolame_crc_writeheader(frame_ptr, glopts->num_crc_bits);
659 }
660 // fprintf(stderr,"Frame size: %li\n\n",frameBits/8);
661
662 return frameBits / 8;
663 }
664
665
666
667 /*
668 glopts
669 leftpcm - holds left channel (or mono channel)
670 rightpcm - d'uh
671 num_samples - the number of samples in each channel
672 mp2buffer - a pointer to the place where we want the mpeg data to be written
673 mp2buffer_size - how much space the user allocated for this buffer
674 mp2fill_size - how much mpeg data the library has put into the mp2buffer
675 */
676
677 int twolame_encode_buffer(twolame_options * glopts,
678 const short int leftpcm[],
679 const short int rightpcm[],
680 int num_samples, unsigned char *mp2buffer, int mp2buffer_size)
681 {
682 int mp2_size = 0;
683 bit_stream *mybs;
684 int i;
685
686 if (num_samples == 0)
687 return 0;
688
689
690 // now would be a great time to validate the size of the buffer.
691 // samples/1152 * sizeof(frame) < mp2buffer_size
692 mybs = twolame_buffer_init(mp2buffer, mp2buffer_size);
693
694 if (mybs != NULL) {
695 // Use up all the samples in in_buffer
696 while (num_samples) {
697
698 // fill up glopts->buffer with as much as we can
699 int samples_to_copy = TWOLAME_SAMPLES_PER_FRAME - glopts->samples_in_buffer;
700 if (num_samples < samples_to_copy)
701 samples_to_copy = num_samples;
702
703 /* Copy across samples */
704 if (glopts->num_channels_in == 2)
705 for (i = 0; i < samples_to_copy; i++) {
706 glopts->buffer[0][glopts->samples_in_buffer + i] = *leftpcm++;
707 glopts->buffer[1][glopts->samples_in_buffer + i] = *rightpcm++;
708 }
709 else
710 for (i = 0; i < samples_to_copy; i++)
711 glopts->buffer[0][glopts->samples_in_buffer + i] = *leftpcm++;
712
713
714 /* Update sample counts */
715 glopts->samples_in_buffer += samples_to_copy;
716 num_samples -= samples_to_copy;
717
718
719 // is there enough to encode a whole frame ?
720 if (glopts->samples_in_buffer >= TWOLAME_SAMPLES_PER_FRAME) {
721 int bytes = encode_frame(glopts, mybs);
722 if (bytes <= 0) {
723 twolame_buffer_deinit(&mybs);
724 return bytes;
725 }
726 mp2_size += bytes;
727 glopts->samples_in_buffer -= TWOLAME_SAMPLES_PER_FRAME;
728 }
729 }
730
731 // free up the bit stream buffer structure
732 twolame_buffer_deinit(&mybs);
733 }
734
735 return (mp2_size);
736 }
737
738
739 int twolame_encode_buffer_interleaved(twolame_options * glopts,
740 const short int pcm[],
741 int num_samples, unsigned char *mp2buffer, int mp2buffer_size)
742 {
743 int mp2_size = 0;
744 bit_stream *mybs;
745 int i;
746
747 if (num_samples == 0)
748 return 0;
749
750
751 // now would be a great time to validate the size of the buffer.
752 // samples/1152 * sizeof(frame) < mp2buffer_size
753 mybs = twolame_buffer_init(mp2buffer, mp2buffer_size);
754
755 if (mybs != NULL) {
756 // Use up all the samples in in_buffer
757 while (num_samples) {
758
759 // fill up glopts->buffer with as much as we can
760 int samples_to_copy = TWOLAME_SAMPLES_PER_FRAME - glopts->samples_in_buffer;
761 if (num_samples < samples_to_copy)
762 samples_to_copy = num_samples;
763
764 /* Copy across samples */
765 if (glopts->num_channels_in == 2)
766 for (i = 0; i < samples_to_copy; i++) {
767 glopts->buffer[0][glopts->samples_in_buffer + i] = *pcm++;
768 glopts->buffer[1][glopts->samples_in_buffer + i] = *pcm++;
769 }
770 else
771 for (i = 0; i < samples_to_copy; i++)
772 glopts->buffer[0][glopts->samples_in_buffer + i] = *pcm++;
773
774
775 /* Update sample counts */
776 glopts->samples_in_buffer += samples_to_copy;
777 num_samples -= samples_to_copy;
778
779
780 // is there enough to encode a whole frame ?
781 if (glopts->samples_in_buffer >= TWOLAME_SAMPLES_PER_FRAME) {
782 int bytes = encode_frame(glopts, mybs);
783 if (bytes <= 0) {
784 twolame_buffer_deinit(&mybs);
785 return bytes;
786 }
787 mp2_size += bytes;
788 glopts->samples_in_buffer -= TWOLAME_SAMPLES_PER_FRAME;
789 }
790 }
791
792 // free up the bit stream buffer structure
793 twolame_buffer_deinit(&mybs);
794 }
795
796 return (mp2_size);
797 }
798
799
800 static void float32_to_short(const float in[], short out[], int num_samples, int stride)
801 {
802 int n;
803
804 for (n = 0; n < num_samples; n++) {
805 int tmp = lrintf(in[n * stride] * 32768.0f);
806 if (tmp > SHRT_MAX) {
807 out[n] = SHRT_MAX;
808 } else if (tmp < SHRT_MIN) {
809 out[n] = SHRT_MIN;
810 } else {
811 out[n] = (short) tmp;
812 }
813 }
814 }
815
816
817 /*
818 glopts
819 leftpcm - holds left channel (or mono channel)
820 rightpcm - d'uh
821 num_samples - the number of samples in each channel
822 mp2buffer - a pointer to the place where we want the mpeg data to be written
823 mp2buffer_size - how much space the user allocated for this buffer
824 mp2fill_size - how much mpeg data the library has put into the mp2buffer
825 */
826
827 int twolame_encode_buffer_float32(twolame_options * glopts,
828 const float leftpcm[],
829 const float rightpcm[],
830 int num_samples, unsigned char *mp2buffer, int mp2buffer_size)
831 {
832 int mp2_size = 0;
833 bit_stream *mybs;
834
835 if (num_samples == 0)
836 return 0;
837
838
839 // now would be a great time to validate the size of the buffer.
840 // samples/1152 * sizeof(frame) < mp2buffer_size
841 mybs = twolame_buffer_init(mp2buffer, mp2buffer_size);
842
843 if (mybs != NULL) {
844 // Use up all the samples in in_buffer
845 while (num_samples) {
846
847 // fill up glopts->buffer with as much as we can
848 int samples_to_copy = TWOLAME_SAMPLES_PER_FRAME - glopts->samples_in_buffer;
849 if (num_samples < samples_to_copy)
850 samples_to_copy = num_samples;
851
852 /* Copy across samples */
853 float32_to_short(leftpcm, &glopts->buffer[0][glopts->samples_in_buffer], samples_to_copy,
854 1);
855 if (glopts->num_channels_in == 2)
856 float32_to_short(rightpcm, &glopts->buffer[1][glopts->samples_in_buffer],
857 samples_to_copy, 1);
858 leftpcm += samples_to_copy;
859 rightpcm += samples_to_copy;
860
861 /* Update sample counts */
862 glopts->samples_in_buffer += samples_to_copy;
863 num_samples -= samples_to_copy;
864
865
866 // is there enough to encode a whole frame ?
867 if (glopts->samples_in_buffer >= TWOLAME_SAMPLES_PER_FRAME) {
868 int bytes = encode_frame(glopts, mybs);
869 if (bytes <= 0) {
870 twolame_buffer_deinit(&mybs);
871 return bytes;
872 }
873 mp2_size += bytes;
874 glopts->samples_in_buffer -= TWOLAME_SAMPLES_PER_FRAME;
875 }
876 }
877
878 // free up the bit stream buffer structure
879 twolame_buffer_deinit(&mybs);
880 }
881
882 return (mp2_size);
883 }
884
885
886 int twolame_encode_buffer_float32_interleaved(twolame_options * glopts,
887 const float pcm[],
888 int num_samples,
889 unsigned char *mp2buffer, int mp2buffer_size)
890 {
891 int mp2_size = 0;
892 bit_stream *mybs;
893
894 if (num_samples == 0)
895 return 0;
896
897
898 // now would be a great time to validate the size of the buffer.
899 // samples/1152 * sizeof(frame) < mp2buffer_size
900 mybs = twolame_buffer_init(mp2buffer, mp2buffer_size);
901
902 if (mybs != NULL) {
903 // Use up all the samples in in_buffer
904 while (num_samples) {
905
906 // fill up glopts->buffer with as much as we can
907 int samples_to_copy = TWOLAME_SAMPLES_PER_FRAME - glopts->samples_in_buffer;
908 if (num_samples < samples_to_copy)
909 samples_to_copy = num_samples;
910
911 /* Copy across samples */
912 float32_to_short(pcm, &glopts->buffer[0][glopts->samples_in_buffer], samples_to_copy,
913 glopts->num_channels_in);
914 if (glopts->num_channels_in == 2)
915 float32_to_short(pcm + 1, &glopts->buffer[1][glopts->samples_in_buffer],
916 samples_to_copy, glopts->num_channels_in);
917 pcm += (samples_to_copy * glopts->num_channels_in);
918
919
920 /* Update sample counts */
921 glopts->samples_in_buffer += samples_to_copy;
922 num_samples -= samples_to_copy;
923
924
925 // is there enough to encode a whole frame ?
926 if (glopts->samples_in_buffer >= TWOLAME_SAMPLES_PER_FRAME) {
927 int bytes = encode_frame(glopts, mybs);
928 if (bytes <= 0) {
929 twolame_buffer_deinit(&mybs);
930 return bytes;
931 }
932 mp2_size += bytes;
933 glopts->samples_in_buffer -= TWOLAME_SAMPLES_PER_FRAME;
934 }
935 }
936
937 // free up the bit stream buffer structure
938 twolame_buffer_deinit(&mybs);
939 }
940
941 return (mp2_size);
942 }
943
944
945
946 int twolame_encode_flush(twolame_options * glopts, unsigned char *mp2buffer, int mp2buffer_size)
947 {
948 bit_stream *mybs = NULL;
949 int mp2_size = 0;
950 int i;
951
952 if (glopts->samples_in_buffer == 0) {
953 // No samples left over
954 return 0;
955 }
956 // Create bit stream structure
957 mybs = twolame_buffer_init(mp2buffer, mp2buffer_size);
958
959 if (mybs != NULL) {
960 // Pad out the PCM buffers with 0 and encode the frame
961 for (i = glopts->samples_in_buffer; i < TWOLAME_SAMPLES_PER_FRAME; i++) {
962 glopts->buffer[0][i] = glopts->buffer[1][i] = 0;
963 }
964
965 // Encode the frame
966 mp2_size = encode_frame(glopts, mybs);
967 glopts->samples_in_buffer = 0;
968
969 // free up the bit stream buffer structure
970 twolame_buffer_deinit(&mybs);
971 }
972
973 return mp2_size;
974 }
975
976
977
978
979 void twolame_close(twolame_options ** glopts)
980 {
981 twolame_options *opts = NULL;
982
983 // Check input pointers aren't NULL
984 if (glopts == NULL)
985 return;
986 opts = *glopts;
987 if (opts == NULL)
988 return;
989
990 // free mem
991 twolame_psycho_4_deinit(&opts->p4mem);
992 twolame_psycho_3_deinit(&opts->p3mem);
993 twolame_psycho_2_deinit(&opts->p2mem);
994 twolame_psycho_1_deinit(&opts->p1mem);
995 twolame_psycho_0_deinit(&opts->p0mem);
996
997 TWOLAME_FREE(opts->subband);
998 TWOLAME_FREE(opts->j_sample);
999 TWOLAME_FREE(opts->sb_sample);
1000
1001 // Free the memory and zero the pointer
1002 TWOLAME_FREE(opts);
1003 }
1004
1005 // vim:ts=4:sw=4:nowrap: