delete bundled libgsm
[sox.git] / src / mp3.c
blob1306f6368254f009f1e4f49b58ac707e525851a9
1 /* MP3 support for SoX
3 * Uses libmad for MP3 decoding
4 * libmp3lame for MP3 encoding
5 * and libtwolame for MP2 encoding
7 * Written by Fabrizio Gennari <fabrizio.ge@tiscali.it>
9 * The decoding part is based on the decoder-tutorial program madlld
10 * written by Bertrand Petit <madlld@phoe.fmug.org>,
13 #include "sox_i.h"
14 #include <string.h>
16 #if defined(HAVE_LAME_LAME_H) || defined(HAVE_LAME_H) || defined(DL_LAME)
17 #define HAVE_LAME 1
18 #endif
20 #if defined(HAVE_TWOLAME_H) || defined(DL_TWOLAME)
21 #define HAVE_TWOLAME 1
22 #endif
24 #if defined(HAVE_MAD_H) || defined(HAVE_LAME) || defined(HAVE_TWOLAME)
26 #ifdef HAVE_MAD_H
27 #include <mad.h>
28 #endif
30 #if defined(HAVE_LAME_LAME_H)
31 #include <lame/lame.h>
32 #elif defined(HAVE_LAME_H)
33 #include <lame.h>
34 #elif defined(DL_LAME)
35 typedef struct lame_global_struct lame_global_flags;
36 typedef enum {
37 vbr_off=0,
38 vbr_default=4
39 } vbr_mode;
40 #endif
42 #if defined(HAVE_ID3TAG) && (defined(HAVE_IO_H) || defined(HAVE_UNISTD_H))
43 #define USING_ID3TAG 1
44 #endif
46 #ifdef USING_ID3TAG
47 #include <id3tag.h>
48 #if defined(HAVE_UNISTD_H)
49 #include <unistd.h>
50 #elif defined(HAVE_IO_H)
51 #include <io.h>
52 #endif
53 #else
54 #define ID3_TAG_FLAG_FOOTERPRESENT 0x10
55 #endif
57 #ifdef HAVE_TWOLAME_H
58 #include <twolame.h>
59 #endif
61 #ifndef HAVE_LIBLTDL
62 #undef DL_LAME
63 #undef DL_MAD
64 #endif
66 /* Under Windows, importing data from DLLs is a dicey proposition. This is true
67 * when using dlopen, but also true if linking directly against the DLL if the
68 * header does not mark the data as __declspec(dllexport), which mad.h does not.
69 * Sidestep the issue by defining our own mad_timer_zero. This is needed because
70 * mad_timer_zero is used in some of the mad.h macros.
72 #ifdef HAVE_MAD_H
73 #define mad_timer_zero mad_timer_zero_stub
74 static mad_timer_t const mad_timer_zero_stub = {0, 0};
75 #endif
77 #define MAXFRAMESIZE 2880
78 #define ID3PADDING 128
80 /* LAME takes float values as input. */
81 #define MP3_LAME_PRECISION 24
83 /* MAD returns values with MAD_F_FRACBITS (28) bits of precision, though it's
84 not certain that all of them are meaningful. Default to 16 bits to
85 align with most users expectation of output file should be 16 bits. */
86 #define MP3_MAD_PRECISION 16
88 static const char* const mad_library_names[] =
90 #ifdef DL_MAD
91 "libmad",
92 "libmad-0",
93 "cygmad-0",
94 #endif
95 NULL
98 #ifdef DL_MAD
99 #define MAD_FUNC LSX_DLENTRY_DYNAMIC
100 #else
101 #define MAD_FUNC LSX_DLENTRY_STATIC
102 #endif
104 #define MAD_FUNC_ENTRIES(f,x) \
105 MAD_FUNC(f,x, void, mad_stream_buffer, (struct mad_stream *, unsigned char const *, unsigned long)) \
106 MAD_FUNC(f,x, void, mad_stream_skip, (struct mad_stream *, unsigned long)) \
107 MAD_FUNC(f,x, int, mad_stream_sync, (struct mad_stream *)) \
108 MAD_FUNC(f,x, void, mad_stream_init, (struct mad_stream *)) \
109 MAD_FUNC(f,x, void, mad_frame_init, (struct mad_frame *)) \
110 MAD_FUNC(f,x, void, mad_synth_init, (struct mad_synth *)) \
111 MAD_FUNC(f,x, int, mad_frame_decode, (struct mad_frame *, struct mad_stream *)) \
112 MAD_FUNC(f,x, void, mad_timer_add, (mad_timer_t *, mad_timer_t)) \
113 MAD_FUNC(f,x, void, mad_synth_frame, (struct mad_synth *, struct mad_frame const *)) \
114 MAD_FUNC(f,x, char const *, mad_stream_errorstr, (struct mad_stream const *)) \
115 MAD_FUNC(f,x, void, mad_frame_finish, (struct mad_frame *)) \
116 MAD_FUNC(f,x, void, mad_stream_finish, (struct mad_stream *)) \
117 MAD_FUNC(f,x, unsigned long, mad_bit_read, (struct mad_bitptr *, unsigned int)) \
118 MAD_FUNC(f,x, int, mad_header_decode, (struct mad_header *, struct mad_stream *)) \
119 MAD_FUNC(f,x, void, mad_header_init, (struct mad_header *)) \
120 MAD_FUNC(f,x, signed long, mad_timer_count, (mad_timer_t, enum mad_units)) \
121 MAD_FUNC(f,x, void, mad_timer_multiply, (mad_timer_t *, signed long))
123 static const char* const lame_library_names[] =
125 #ifdef DL_LAME
126 "libmp3lame",
127 "libmp3lame-0",
128 "lame-enc",
129 "cygmp3lame-0",
130 #endif
131 NULL
134 #ifdef DL_LAME
136 /* Expected to be present in all builds of LAME. */
137 #define LAME_FUNC LSX_DLENTRY_DYNAMIC
139 /* id3tag support is an optional component of LAME. Use if available. */
140 #define LAME_FUNC_ID3 LSX_DLENTRY_STUB
142 #else /* DL_LAME */
144 /* Expected to be present in all builds of LAME. */
145 #define LAME_FUNC LSX_DLENTRY_STATIC
147 /* id3tag support is an optional component of LAME. Use if available. */
148 #ifdef HAVE_LAME_ID3TAG
149 #define LAME_FUNC_ID3 LSX_DLENTRY_STATIC
150 #else
151 #define LAME_FUNC_ID3 LSX_DLENTRY_STUB
152 #endif
154 #endif /* DL_LAME */
156 #define LAME_FUNC_ENTRIES(f,x) \
157 LAME_FUNC(f,x, lame_global_flags*, lame_init, (void)) \
158 LAME_FUNC(f,x, int, lame_set_errorf, (lame_global_flags *, void (*)(const char *, va_list))) \
159 LAME_FUNC(f,x, int, lame_set_debugf, (lame_global_flags *, void (*)(const char *, va_list))) \
160 LAME_FUNC(f,x, int, lame_set_msgf, (lame_global_flags *, void (*)(const char *, va_list))) \
161 LAME_FUNC(f,x, int, lame_set_num_samples, (lame_global_flags *, unsigned long)) \
162 LAME_FUNC(f,x, int, lame_get_num_channels, (const lame_global_flags *)) \
163 LAME_FUNC(f,x, int, lame_set_num_channels, (lame_global_flags *, int)) \
164 LAME_FUNC(f,x, int, lame_set_in_samplerate, (lame_global_flags *, int)) \
165 LAME_FUNC(f,x, int, lame_set_out_samplerate, (lame_global_flags *, int)) \
166 LAME_FUNC(f,x, int, lame_set_bWriteVbrTag, (lame_global_flags *, int)) \
167 LAME_FUNC(f,x, int, lame_set_brate, (lame_global_flags *, int)) \
168 LAME_FUNC(f,x, int, lame_set_quality, (lame_global_flags *, int)) \
169 LAME_FUNC(f,x, vbr_mode, lame_get_VBR, (const lame_global_flags *)) \
170 LAME_FUNC(f,x, int, lame_set_VBR, (lame_global_flags *, vbr_mode)) \
171 LAME_FUNC(f,x, int, lame_set_VBR_q, (lame_global_flags *, int)) \
172 LAME_FUNC(f,x, int, lame_init_params, (lame_global_flags *)) \
173 LAME_FUNC(f,x, int, lame_encode_buffer_float, (lame_global_flags *, const float[], const float[], const int, unsigned char *, const int)) \
174 LAME_FUNC(f,x, int, lame_encode_flush, (lame_global_flags *, unsigned char *, int)) \
175 LAME_FUNC(f,x, int, lame_close, (lame_global_flags *)) \
176 LAME_FUNC(f,x, size_t, lame_get_lametag_frame, (const lame_global_flags *, unsigned char*, size_t)) \
177 LAME_FUNC_ID3(f,x, void, id3tag_init, (lame_global_flags *)) \
178 LAME_FUNC_ID3(f,x, void, id3tag_set_title, (lame_global_flags *, const char* title)) \
179 LAME_FUNC_ID3(f,x, void, id3tag_set_artist, (lame_global_flags *, const char* artist)) \
180 LAME_FUNC_ID3(f,x, void, id3tag_set_album, (lame_global_flags *, const char* album)) \
181 LAME_FUNC_ID3(f,x, void, id3tag_set_year, (lame_global_flags *, const char* year)) \
182 LAME_FUNC_ID3(f,x, void, id3tag_set_comment, (lame_global_flags *, const char* comment)) \
183 LAME_FUNC_ID3(f,x, int, id3tag_set_track, (lame_global_flags *, const char* track)) \
184 LAME_FUNC_ID3(f,x, int, id3tag_set_genre, (lame_global_flags *, const char* genre)) \
185 LAME_FUNC_ID3(f,x, size_t, id3tag_set_pad, (lame_global_flags *, size_t)) \
186 LAME_FUNC_ID3(f,x, size_t, lame_get_id3v2_tag, (lame_global_flags *, unsigned char*, size_t)) \
187 LAME_FUNC_ID3(f,x, int, id3tag_set_fieldvalue, (lame_global_flags *, const char *))
189 #ifdef HAVE_TWOLAME
190 static const char* const twolame_library_names[] =
192 #ifdef DL_TWOLAME
193 "libtwolame",
194 "libtwolame-0",
195 #endif
196 NULL
198 #endif
200 #ifdef DL_TWOLAME
201 #define TWOLAME_FUNC LSX_DLENTRY_DYNAMIC
202 #else
203 #define TWOLAME_FUNC LSX_DLENTRY_STATIC
204 #endif
206 #define TWOLAME_FUNC_ENTRIES(f,x) \
207 TWOLAME_FUNC(f,x, twolame_options*, twolame_init, (void)) \
208 TWOLAME_FUNC(f,x, int, twolame_get_num_channels, (twolame_options*)) \
209 TWOLAME_FUNC(f,x, int, twolame_set_num_channels, (twolame_options*, int)) \
210 TWOLAME_FUNC(f,x, int, twolame_set_in_samplerate, (twolame_options *, int)) \
211 TWOLAME_FUNC(f,x, int, twolame_set_out_samplerate, (twolame_options *, int)) \
212 TWOLAME_FUNC(f,x, int, twolame_set_brate, (twolame_options *, int)) \
213 TWOLAME_FUNC(f,x, int, twolame_init_params, (twolame_options *)) \
214 TWOLAME_FUNC(f,x, int, twolame_encode_buffer_float32_interleaved, (twolame_options *, const float [], int, unsigned char *, int)) \
215 TWOLAME_FUNC(f,x, int, twolame_encode_flush, (twolame_options *, unsigned char *, int)) \
216 TWOLAME_FUNC(f,x, void, twolame_close, (twolame_options **))
218 /* Private data */
219 typedef struct mp3_priv_t {
220 unsigned char *mp3_buffer;
221 size_t mp3_buffer_size;
223 #ifdef HAVE_MAD_H
224 struct mad_stream Stream;
225 struct mad_frame Frame;
226 struct mad_synth Synth;
227 mad_timer_t Timer;
228 ptrdiff_t cursamp;
229 size_t FrameCount;
230 LSX_DLENTRIES_TO_PTRS(MAD_FUNC_ENTRIES, mad_dl);
231 #endif /*HAVE_MAD_H*/
233 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
234 float *pcm_buffer;
235 size_t pcm_buffer_size;
236 char mp2;
237 #endif
239 #ifdef HAVE_LAME
240 lame_global_flags *gfp;
241 uint64_t num_samples;
242 int vbr_tag;
243 LSX_DLENTRIES_TO_PTRS(LAME_FUNC_ENTRIES, lame_dl);
244 #endif
246 #ifdef HAVE_TWOLAME
247 twolame_options *opt;
248 LSX_DLENTRIES_TO_PTRS(TWOLAME_FUNC_ENTRIES, twolame_dl);
249 #endif
250 } priv_t;
252 #ifdef HAVE_MAD_H
254 /* This function merges the functions tagtype() and id3_tag_query()
255 from MAD's libid3tag, so we don't have to link to it
256 Returns 0 if the frame is not an ID3 tag, tag length if it is */
258 static int tagtype(const unsigned char *data, size_t length)
260 if (length >= 3 && data[0] == 'T' && data[1] == 'A' && data[2] == 'G')
262 return 128; /* ID3V1 */
265 if (length >= 10 &&
266 (data[0] == 'I' && data[1] == 'D' && data[2] == '3') &&
267 data[3] < 0xff && data[4] < 0xff &&
268 data[6] < 0x80 && data[7] < 0x80 && data[8] < 0x80 && data[9] < 0x80)
269 { /* ID3V2 */
270 unsigned char flags;
271 unsigned int size;
272 flags = data[5];
273 size = 10 + (data[6]<<21) + (data[7]<<14) + (data[8]<<7) + data[9];
274 if (flags & ID3_TAG_FLAG_FOOTERPRESENT)
275 size += 10;
276 for (; size < length && !data[size]; ++size); /* Consume padding */
277 return size;
280 return 0;
283 #endif /*HAVE_MAD_H*/
285 #include "mp3-util.h"
287 #ifdef HAVE_MAD_H
290 * (Re)fill the stream buffer that is to be decoded. If any data
291 * still exists in the buffer then they are first shifted to be
292 * front of the stream buffer.
294 static int sox_mp3_input(sox_format_t * ft)
296 priv_t *p = (priv_t *) ft->priv;
297 size_t bytes_read;
298 size_t remaining;
300 remaining = p->Stream.bufend - p->Stream.next_frame;
302 /* libmad does not consume all the buffer it's given. Some
303 * data, part of a truncated frame, is left unused at the
304 * end of the buffer. That data must be put back at the
305 * beginning of the buffer and taken in account for
306 * refilling the buffer. This means that the input buffer
307 * must be large enough to hold a complete frame at the
308 * highest observable bit-rate (currently 448 kb/s).
309 * TODO: Is 2016 bytes the size of the largest frame?
310 * (448000*(1152/32000))/8
312 memmove(p->mp3_buffer, p->Stream.next_frame, remaining);
314 bytes_read = lsx_readbuf(ft, p->mp3_buffer+remaining,
315 p->mp3_buffer_size-remaining);
316 if (bytes_read == 0)
318 return SOX_EOF;
321 p->mad_stream_buffer(&p->Stream, p->mp3_buffer, bytes_read+remaining);
322 p->Stream.error = 0;
324 return SOX_SUCCESS;
327 /* Attempts to read an ID3 tag at the current location in stream and
328 * consume it all. Returns SOX_EOF if no tag is found. Its up to
329 * caller to recover.
330 * */
331 static int sox_mp3_inputtag(sox_format_t * ft)
333 priv_t *p = (priv_t *) ft->priv;
334 int rc = SOX_EOF;
335 size_t remaining;
336 size_t tagsize;
339 /* FIXME: This needs some more work if we are to ever
340 * look at the ID3 frame. This is because the Stream
341 * may not be able to hold the complete ID3 frame.
342 * We should consume the whole frame inside tagtype()
343 * instead of outside of tagframe(). That would support
344 * recovering when Stream contains less then 8-bytes (header)
345 * and also when ID3v2 is bigger then Stream buffer size.
346 * Need to pass in stream so that buffer can be
347 * consumed as well as letting additional data to be
348 * read in.
350 remaining = p->Stream.bufend - p->Stream.next_frame;
351 if ((tagsize = tagtype(p->Stream.this_frame, remaining)))
353 p->mad_stream_skip(&p->Stream, tagsize);
354 rc = SOX_SUCCESS;
357 /* We know that a valid frame hasn't been found yet
358 * so help libmad out and go back into frame seek mode.
359 * This is true whether an ID3 tag was found or not.
361 p->mad_stream_sync(&p->Stream);
363 return rc;
366 static sox_bool sox_mp3_vbrtag(sox_format_t *ft)
368 priv_t *p = ft->priv;
369 struct mad_bitptr *anc = &p->Stream.anc_ptr;
371 if (p->Frame.header.layer != MAD_LAYER_III)
372 return sox_false;
374 if (p->Stream.anc_bitlen < 32)
375 return sox_false;
377 if (!memcmp(anc->byte, "Xing", 4) ||
378 !memcmp(anc->byte, "Info", 4))
379 return sox_true;
381 return sox_false;
384 static int startread(sox_format_t * ft)
386 priv_t *p = (priv_t *) ft->priv;
387 size_t ReadSize;
388 sox_bool ignore_length = ft->signal.length == SOX_IGNORE_LENGTH;
389 int open_library_result;
391 LSX_DLLIBRARY_OPEN(
393 mad_dl,
394 MAD_FUNC_ENTRIES,
395 "MAD decoder library",
396 mad_library_names,
397 open_library_result);
398 if (open_library_result)
399 return SOX_EOF;
401 p->mp3_buffer_size = sox_globals.bufsiz;
402 p->mp3_buffer = lsx_malloc(p->mp3_buffer_size);
404 ft->signal.length = SOX_UNSPEC;
405 if (ft->seekable) {
406 #ifdef USING_ID3TAG
407 read_comments(ft);
408 lsx_rewind(ft);
409 if (!ft->signal.length)
410 #endif
411 if (!ignore_length)
412 ft->signal.length = mp3_duration(ft);
415 p->mad_stream_init(&p->Stream);
416 p->mad_frame_init(&p->Frame);
417 p->mad_synth_init(&p->Synth);
418 mad_timer_reset(&p->Timer);
420 ft->encoding.encoding = SOX_ENCODING_MP3;
422 /* Decode at least one valid frame to find out the input
423 * format. The decoded frame will be saved off so that it
424 * can be processed later.
426 ReadSize = lsx_readbuf(ft, p->mp3_buffer, p->mp3_buffer_size);
427 if (ReadSize != p->mp3_buffer_size && lsx_error(ft))
428 return SOX_EOF;
430 p->mad_stream_buffer(&p->Stream, p->mp3_buffer, ReadSize);
432 /* Find a valid frame before starting up. This makes sure
433 * that we have a valid MP3 and also skips past ID3v2 tags
434 * at the beginning of the audio file.
436 p->Stream.error = 0;
437 while (p->mad_frame_decode(&p->Frame,&p->Stream))
439 /* check whether input buffer needs a refill */
440 if (p->Stream.error == MAD_ERROR_BUFLEN)
442 if (sox_mp3_input(ft) == SOX_EOF)
443 return SOX_EOF;
445 continue;
448 /* Consume any ID3 tags */
449 sox_mp3_inputtag(ft);
451 /* FIXME: We should probably detect when we've read
452 * a bunch of non-ID3 data and still haven't found a
453 * frame. In that case we can abort early without
454 * scanning the whole file.
456 p->Stream.error = 0;
459 if (p->Stream.error)
461 lsx_fail_errno(ft,SOX_EOF,"No valid MP3 frame found");
462 return SOX_EOF;
465 switch(p->Frame.header.mode)
467 case MAD_MODE_SINGLE_CHANNEL:
468 case MAD_MODE_DUAL_CHANNEL:
469 case MAD_MODE_JOINT_STEREO:
470 case MAD_MODE_STEREO:
471 ft->signal.channels = MAD_NCHANNELS(&p->Frame.header);
472 break;
473 default:
474 lsx_fail_errno(ft, SOX_EFMT, "Cannot determine number of channels");
475 return SOX_EOF;
478 ft->signal.precision = MP3_MAD_PRECISION;
479 ft->signal.rate=p->Frame.header.samplerate;
480 if (ignore_length)
481 ft->signal.length = SOX_UNSPEC;
482 else {
483 ft->signal.length *= ft->signal.channels; /* Keep separate from line above! */
486 if (!sox_mp3_vbrtag(ft))
487 p->Stream.next_frame = p->Stream.this_frame;
489 p->mad_frame_init(&p->Frame);
490 sox_mp3_input(ft);
492 p->cursamp = 0;
494 return SOX_SUCCESS;
498 * Read up to len samples from p->Synth
499 * If needed, read some more MP3 data, decode them and synth them
500 * Place in buf[].
501 * Return number of samples read.
503 static size_t sox_mp3read(sox_format_t * ft, sox_sample_t *buf, size_t len)
505 priv_t *p = (priv_t *) ft->priv;
506 size_t donow,i,done=0;
507 mad_fixed_t sample;
508 size_t chan;
510 do {
511 size_t x = (p->Synth.pcm.length - p->cursamp)*ft->signal.channels;
512 donow=min(len, x);
513 i=0;
514 while(i<donow){
515 for(chan=0;chan<ft->signal.channels;chan++){
516 sample=p->Synth.pcm.samples[chan][p->cursamp];
517 if (sample < -MAD_F_ONE)
518 sample=-MAD_F_ONE;
519 else if (sample >= MAD_F_ONE)
520 sample=MAD_F_ONE-1;
521 *buf++=(sox_sample_t)(sample<<(32-1-MAD_F_FRACBITS));
522 i++;
524 p->cursamp++;
527 len-=donow;
528 done+=donow;
530 if (len==0) break;
532 /* check whether input buffer needs a refill */
533 if (p->Stream.error == MAD_ERROR_BUFLEN)
535 if (sox_mp3_input(ft) == SOX_EOF) {
536 lsx_debug("sox_mp3_input EOF");
537 break;
541 if (p->mad_frame_decode(&p->Frame,&p->Stream))
543 if(MAD_RECOVERABLE(p->Stream.error))
545 sox_mp3_inputtag(ft);
546 continue;
548 else
550 if (p->Stream.error == MAD_ERROR_BUFLEN)
551 continue;
552 else
554 lsx_report("unrecoverable frame level error (%s).",
555 p->mad_stream_errorstr(&p->Stream));
556 break;
560 p->FrameCount++;
561 p->mad_timer_add(&p->Timer,p->Frame.header.duration);
562 p->mad_synth_frame(&p->Synth,&p->Frame);
563 p->cursamp=0;
564 } while(1);
566 return done;
569 static int stopread(sox_format_t * ft)
571 priv_t *p=(priv_t*) ft->priv;
573 mad_synth_finish(&p->Synth);
574 p->mad_frame_finish(&p->Frame);
575 p->mad_stream_finish(&p->Stream);
577 free(p->mp3_buffer);
578 LSX_DLLIBRARY_CLOSE(p, mad_dl);
579 return SOX_SUCCESS;
582 static int sox_mp3seek(sox_format_t * ft, uint64_t offset)
584 priv_t * p = (priv_t *) ft->priv;
585 size_t initial_bitrate = p->Frame.header.bitrate;
586 size_t tagsize = 0, consumed = 0;
587 sox_bool vbr = sox_false; /* Variable Bit Rate */
588 sox_bool depadded = sox_false;
589 uint64_t to_skip_samples = 0;
591 /* Reset all */
592 lsx_rewind(ft);
593 mad_timer_reset(&p->Timer);
594 p->FrameCount = 0;
596 /* They where opened in startread */
597 mad_synth_finish(&p->Synth);
598 p->mad_frame_finish(&p->Frame);
599 p->mad_stream_finish(&p->Stream);
601 p->mad_stream_init(&p->Stream);
602 p->mad_frame_init(&p->Frame);
603 p->mad_synth_init(&p->Synth);
605 offset /= ft->signal.channels;
606 to_skip_samples = offset;
608 while(sox_true) { /* Read data from the MP3 file */
609 size_t padding = 0;
610 size_t read;
611 size_t leftover = p->Stream.bufend - p->Stream.next_frame;
613 memmove(p->mp3_buffer, p->Stream.this_frame, leftover);
614 read = lsx_readbuf(ft, p->mp3_buffer + leftover, p->mp3_buffer_size - leftover);
615 if (read == 0) {
616 lsx_debug("seek failure. unexpected EOF (frames=%" PRIuPTR " leftover=%" PRIuPTR ")", p->FrameCount, leftover);
617 break;
619 for (; !depadded && padding < read && !p->mp3_buffer[padding]; ++padding);
620 depadded = sox_true;
621 p->mad_stream_buffer(&p->Stream, p->mp3_buffer + padding, leftover + read - padding);
623 while (sox_true) { /* Decode frame headers */
624 static unsigned short samples;
625 p->Stream.error = MAD_ERROR_NONE;
627 /* Not an audio frame */
628 if (p->mad_header_decode(&p->Frame.header, &p->Stream) == -1) {
629 if (p->Stream.error == MAD_ERROR_BUFLEN)
630 break; /* Normal behaviour; get some more data from the file */
631 if (!MAD_RECOVERABLE(p->Stream.error)) {
632 lsx_warn("unrecoverable MAD error");
633 break;
635 if (p->Stream.error == MAD_ERROR_LOSTSYNC) {
636 unsigned available = (p->Stream.bufend - p->Stream.this_frame);
637 tagsize = tagtype(p->Stream.this_frame, (size_t) available);
638 if (tagsize) { /* It's some ID3 tags, so just skip */
639 if (tagsize >= available) {
640 lsx_seeki(ft, (off_t)(tagsize - available), SEEK_CUR);
641 depadded = sox_false;
643 p->mad_stream_skip(&p->Stream, min(tagsize, available));
645 else lsx_warn("MAD lost sync");
647 else lsx_warn("recoverable MAD error");
648 continue;
651 consumed += p->Stream.next_frame - p->Stream.this_frame;
652 vbr |= (p->Frame.header.bitrate != initial_bitrate);
654 samples = 32 * MAD_NSBSAMPLES(&p->Frame.header);
656 p->FrameCount++;
657 p->mad_timer_add(&p->Timer, p->Frame.header.duration);
659 if(to_skip_samples <= samples)
661 p->mad_frame_decode(&p->Frame,&p->Stream);
662 p->mad_synth_frame(&p->Synth, &p->Frame);
663 p->cursamp = to_skip_samples;
664 return SOX_SUCCESS;
666 else to_skip_samples -= samples;
668 /* If not VBR, we can extrapolate frame size */
669 if (p->FrameCount == 64 && !vbr) {
670 p->FrameCount = offset / samples;
671 to_skip_samples = offset % samples;
673 if (SOX_SUCCESS != lsx_seeki(ft, (off_t)(p->FrameCount * consumed / 64 + tagsize), SEEK_SET))
674 return SOX_EOF;
676 /* Reset Stream for refilling buffer */
677 p->mad_stream_finish(&p->Stream);
678 p->mad_stream_init(&p->Stream);
679 break;
684 return SOX_EOF;
686 #else /* !HAVE_MAD_H */
687 static int startread(sox_format_t * ft)
689 lsx_fail_errno(ft,SOX_EOF,"SoX was compiled without MP3 decoding support");
690 return SOX_EOF;
692 #define sox_mp3read NULL
693 #define stopread NULL
694 #define sox_mp3seek NULL
695 #endif /*HAVE_MAD_H*/
697 #ifdef HAVE_LAME
699 /* Adapters for lame message callbacks: */
701 static void errorf(const char* fmt, va_list va)
703 sox_globals.subsystem=__FILE__;
704 if (sox_globals.output_message_handler)
705 (*sox_globals.output_message_handler)(1,sox_globals.subsystem,fmt,va);
706 return;
709 static void debugf(const char* fmt, va_list va)
711 sox_globals.subsystem=__FILE__;
712 if (sox_globals.output_message_handler)
713 (*sox_globals.output_message_handler)(4,sox_globals.subsystem,fmt,va);
714 return;
717 static void msgf(const char* fmt, va_list va)
719 sox_globals.subsystem=__FILE__;
720 if (sox_globals.output_message_handler)
721 (*sox_globals.output_message_handler)(3,sox_globals.subsystem,fmt,va);
722 return;
725 /* These functions are considered optional. If they aren't present in the
726 library, the stub versions defined here will be used instead. */
728 UNUSED static void id3tag_init_stub(lame_global_flags * gfp UNUSED)
729 { return; }
730 UNUSED static void id3tag_set_title_stub(lame_global_flags * gfp UNUSED, const char* title UNUSED)
731 { return; }
732 UNUSED static void id3tag_set_artist_stub(lame_global_flags * gfp UNUSED, const char* artist UNUSED)
733 { return; }
734 UNUSED static void id3tag_set_album_stub(lame_global_flags * gfp UNUSED, const char* album UNUSED)
735 { return; }
736 UNUSED static void id3tag_set_year_stub(lame_global_flags * gfp UNUSED, const char* year UNUSED)
737 { return; }
738 UNUSED static void id3tag_set_comment_stub(lame_global_flags * gfp UNUSED, const char* comment UNUSED)
739 { return; }
740 UNUSED static void id3tag_set_track_stub(lame_global_flags * gfp UNUSED, const char* track UNUSED)
741 { return; }
742 UNUSED static int id3tag_set_genre_stub(lame_global_flags * gfp UNUSED, const char* genre UNUSED)
743 { return 0; }
744 UNUSED static size_t id3tag_set_pad_stub(lame_global_flags * gfp UNUSED, size_t n UNUSED)
745 { return 0; }
746 UNUSED static size_t lame_get_id3v2_tag_stub(lame_global_flags * gfp UNUSED, unsigned char * buffer UNUSED, size_t size UNUSED)
747 { return 0; }
748 UNUSED static int id3tag_set_fieldvalue_stub(lame_global_flags * gfp UNUSED, const char *fieldvalue UNUSED)
749 { return 0; }
751 static int get_id3v2_tag_size(sox_format_t * ft)
753 size_t bytes_read;
754 int id3v2_size;
755 unsigned char id3v2_header[10];
757 if (lsx_seeki(ft, (off_t)0, SEEK_SET) != 0) {
758 lsx_warn("cannot update id3 tag - failed to seek to beginning");
759 return SOX_EOF;
762 /* read 10 bytes in case there's an ID3 version 2 header here */
763 bytes_read = lsx_readbuf(ft, id3v2_header, sizeof(id3v2_header));
764 if (bytes_read != sizeof(id3v2_header)) {
765 lsx_warn("cannot update id3 tag - failed to read id3 header");
766 return SOX_EOF; /* not readable, maybe opened Write-Only */
769 /* does the stream begin with the ID3 version 2 file identifier? */
770 if (!strncmp((char *) id3v2_header, "ID3", (size_t)3)) {
771 /* the tag size (minus the 10-byte header) is encoded into four
772 * bytes where the most significant bit is clear in each byte */
773 id3v2_size = (((id3v2_header[6] & 0x7f) << 21)
774 | ((id3v2_header[7] & 0x7f) << 14)
775 | ((id3v2_header[8] & 0x7f) << 7)
776 | (id3v2_header[9] & 0x7f))
777 + sizeof(id3v2_header);
778 } else {
779 /* no ID3 version 2 tag in this stream */
780 id3v2_size = 0;
782 return id3v2_size;
785 static void rewrite_id3v2_tag(sox_format_t * ft, size_t id3v2_size, uint64_t num_samples)
787 priv_t *p = (priv_t *)ft->priv;
788 size_t new_size;
789 unsigned char * buffer;
791 if (LSX_DLFUNC_IS_STUB(p, lame_get_id3v2_tag))
793 if (p->num_samples)
794 lsx_warn("cannot update track length info - tag update not supported with this version of LAME. Track length will be incorrect.");
795 else
796 lsx_report("cannot update track length info - tag update not supported with this version of LAME. Track length will be unspecified.");
797 return;
800 buffer = lsx_malloc(id3v2_size);
801 if (!buffer)
803 lsx_warn("cannot update track length info - failed to allocate buffer");
804 return;
807 if (num_samples > ULONG_MAX)
809 lsx_warn("cannot accurately update track length info - file is too long");
810 num_samples = 0;
812 p->lame_set_num_samples(p->gfp, (unsigned long)num_samples);
813 lsx_debug("updated MP3 TLEN to %lu samples", (unsigned long)num_samples);
815 new_size = p->lame_get_id3v2_tag(p->gfp, buffer, id3v2_size);
817 if (new_size != id3v2_size && new_size-ID3PADDING <= id3v2_size) {
818 p->id3tag_set_pad(p->gfp, ID3PADDING + id3v2_size - new_size);
819 new_size = p->lame_get_id3v2_tag(p->gfp, buffer, id3v2_size);
822 if (new_size != id3v2_size) {
823 if (LSX_DLFUNC_IS_STUB(p, id3tag_set_pad))
825 if (p->num_samples)
826 lsx_warn("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be invalid.");
827 else
828 lsx_report("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be unspecified.");
830 else
831 lsx_warn("cannot update track length info - failed to adjust tag size");
832 } else {
833 lsx_seeki(ft, (off_t)0, SEEK_SET);
834 /* Overwrite the Id3v2 tag (this time TLEN should be accurate) */
835 if (lsx_writebuf(ft, buffer, id3v2_size) != 1) {
836 lsx_debug("Rewrote Id3v2 tag (%" PRIuPTR " bytes)", id3v2_size);
840 free(buffer);
843 static void rewrite_tags(sox_format_t * ft, uint64_t num_samples)
845 priv_t *p = (priv_t *)ft->priv;
847 off_t file_size;
848 size_t id3v2_size;
850 if (lsx_seeki(ft, (off_t)0, SEEK_END)) {
851 lsx_warn("cannot update tags - seek to end failed");
852 return;
855 /* Get file size */
856 file_size = lsx_tell(ft);
858 if (file_size == 0) {
859 lsx_warn("cannot update tags - file size is 0");
860 return;
863 id3v2_size = get_id3v2_tag_size(ft);
864 if (id3v2_size > 0 && num_samples != p->num_samples) {
865 rewrite_id3v2_tag(ft, id3v2_size, num_samples);
868 if (p->vbr_tag) {
869 size_t lametag_size;
870 uint8_t buffer[MAXFRAMESIZE];
872 if (lsx_seeki(ft, (off_t)id3v2_size, SEEK_SET)) {
873 lsx_warn("cannot write VBR tag - seek to tag block failed");
874 return;
877 lametag_size = p->lame_get_lametag_frame(p->gfp, buffer, sizeof(buffer));
878 if (lametag_size > sizeof(buffer)) {
879 lsx_warn("cannot write VBR tag - VBR tag too large for buffer");
880 return;
883 if (lametag_size < 1) {
884 return;
887 if (lsx_writebuf(ft, buffer, lametag_size) != lametag_size) {
888 lsx_warn("cannot write VBR tag - VBR tag write failed");
889 } else {
890 lsx_debug("rewrote VBR tag (%" PRIuPTR " bytes)", lametag_size);
895 #endif /* HAVE_LAME */
897 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
899 #define LAME_BUFFER_SIZE(num_samples) (((num_samples) + 3) / 4 * 5 + 7200)
901 static int startwrite(sox_format_t * ft)
903 priv_t *p = (priv_t *) ft->priv;
904 int openlibrary_result;
905 int fail = 0;
907 if (ft->encoding.encoding != SOX_ENCODING_MP3) {
908 if(ft->encoding.encoding != SOX_ENCODING_UNKNOWN)
909 lsx_report("Encoding forced to MP2/MP3");
910 ft->encoding.encoding = SOX_ENCODING_MP3;
913 if(strchr(ft->filetype, '2'))
914 p->mp2 = 1;
916 if (p->mp2) {
917 #ifdef HAVE_TWOLAME
918 LSX_DLLIBRARY_OPEN(
920 twolame_dl,
921 TWOLAME_FUNC_ENTRIES,
922 "Twolame encoder library",
923 twolame_library_names,
924 openlibrary_result);
925 #else
926 lsx_fail_errno(ft,SOX_EOF,"SoX was compiled without MP2 encoding support");
927 return SOX_EOF;
928 #endif
929 } else {
930 #ifdef HAVE_LAME
931 LSX_DLLIBRARY_OPEN(
933 lame_dl,
934 LAME_FUNC_ENTRIES,
935 "LAME encoder library",
936 lame_library_names,
937 openlibrary_result);
938 #else
939 lsx_fail_errno(ft,SOX_EOF,"SoX was compiled without MP3 encoding support");
940 return SOX_EOF;
941 #endif
943 if (openlibrary_result)
944 return SOX_EOF;
946 p->mp3_buffer_size = LAME_BUFFER_SIZE(sox_globals.bufsiz / max(ft->signal.channels, 1));
947 p->mp3_buffer = lsx_malloc(p->mp3_buffer_size);
949 p->pcm_buffer_size = sox_globals.bufsiz * sizeof(float);
950 p->pcm_buffer = lsx_malloc(p->pcm_buffer_size);
952 if (p->mp2) {
953 #ifdef HAVE_TWOLAME
954 p->opt = p->twolame_init();
956 if (p->opt == NULL){
957 lsx_fail_errno(ft,SOX_EOF,"Initialization of Twolame library failed");
958 return(SOX_EOF);
960 #endif
961 } else {
962 #ifdef HAVE_LAME
963 p->gfp = p->lame_init();
965 if (p->gfp == NULL){
966 lsx_fail_errno(ft,SOX_EOF,"Initialization of LAME library failed");
967 return(SOX_EOF);
970 /* First set message callbacks so we don't miss any messages: */
971 p->lame_set_errorf(p->gfp,errorf);
972 p->lame_set_debugf(p->gfp,debugf);
973 p->lame_set_msgf (p->gfp,msgf);
975 p->num_samples = ft->signal.length == SOX_IGNORE_LENGTH ? 0 : ft->signal.length / max(ft->signal.channels, 1);
976 p->lame_set_num_samples(p->gfp, p->num_samples > ULONG_MAX ? 0 : (unsigned long)p->num_samples);
977 #endif
980 ft->signal.precision = MP3_LAME_PRECISION;
982 if (ft->signal.channels != SOX_ENCODING_UNKNOWN) {
983 if (p->mp2) {
984 #ifdef HAVE_TWOLAME
985 fail = (p->twolame_set_num_channels(p->opt,(int)ft->signal.channels) != 0);
986 #endif
987 } else {
988 #ifdef HAVE_LAME
989 fail = (p->lame_set_num_channels(p->gfp,(int)ft->signal.channels) < 0);
990 #endif
992 if (fail) {
993 lsx_fail_errno(ft,SOX_EOF,"Unsupported number of channels");
994 return(SOX_EOF);
997 else {
998 if (p->mp2) {
999 #ifdef HAVE_TWOLAME
1000 ft->signal.channels = p->twolame_get_num_channels(p->opt); /* Twolame default */
1001 #endif
1002 } else {
1003 #ifdef HAVE_LAME
1004 ft->signal.channels = p->lame_get_num_channels(p->gfp); /* LAME default */
1005 #endif
1009 if (p->mp2) {
1010 #ifdef HAVE_TWOLAME
1011 p->twolame_set_in_samplerate(p->opt,(int)ft->signal.rate);
1012 p->twolame_set_out_samplerate(p->opt,(int)ft->signal.rate);
1013 #endif
1014 } else {
1015 #ifdef HAVE_LAME
1016 p->lame_set_in_samplerate(p->gfp,(int)ft->signal.rate);
1017 p->lame_set_out_samplerate(p->gfp,(int)ft->signal.rate);
1018 #endif
1021 if (!p->mp2) {
1022 #ifdef HAVE_LAME
1023 if (!LSX_DLFUNC_IS_STUB(p, id3tag_init))
1024 write_comments(ft);
1025 #endif
1028 /* The primary parameter to the LAME encoder is the bit rate. If the
1029 * value of encoding.compression is a positive integer, it's taken as
1030 * the bitrate in kbps (that is if you specify 128, it use 128 kbps).
1032 * The second most important parameter is probably "quality" (really
1033 * performance), which allows balancing encoding speed vs. quality.
1034 * In LAME, 0 specifies highest quality but is very slow, while
1035 * 9 selects poor quality, but is fast. (5 is the default and 2 is
1036 * recommended as a good trade-off for high quality encodes.)
1038 * Because encoding.compression is a float, the fractional part is used
1039 * to select quality. 128.2 selects 128 kbps encoding with a quality
1040 * of 2. There is one problem with this approach. We need 128 to specify
1041 * 128 kbps encoding with default quality, so .0 means use default. Instead
1042 * of .0 you have to use .01 to specify the highest quality (128.01).
1044 * LAME uses bitrate to specify a constant bitrate, but higher quality
1045 * can be achieved using Variable Bit Rate (VBR). VBR quality (really
1046 * size) is selected using a number from 0 to 9. Use a value of 0 for high
1047 * quality, larger files, and 9 for smaller files of lower quality. 4 is
1048 * the default.
1050 * In order to squeeze the selection of VBR into the encoding.compression
1051 * float we use negative numbers to select VRR. -4.2 would select default
1052 * VBR encoding (size) with high quality (speed). One special case is 0,
1053 * which is a valid VBR encoding parameter but not a valid bitrate.
1054 * Compression value of 0 is always treated as a high quality vbr, as a
1055 * result both -0.2 and 0.2 are treated as highest quality VBR (size) and
1056 * high quality (speed).
1058 * Note: It would have been nice to simply use low values, 0-9, to trigger
1059 * VBR mode, but 8 kbps is a valid bit rate, so negative values were
1060 * used instead.
1063 lsx_debug("-C option is %f", ft->encoding.compression);
1065 if (ft->encoding.compression == HUGE_VAL) {
1066 /* Do nothing, use defaults: */
1067 lsx_report("using %s encoding defaults", p->mp2? "MP2" : "MP3");
1068 } else {
1069 double abs_compression = fabs(ft->encoding.compression);
1070 double floor_compression = floor(abs_compression);
1071 double fraction_compression = abs_compression - floor_compression;
1072 int bitrate_q = (int)floor_compression;
1073 int encoder_q =
1074 fraction_compression == 0.0
1075 ? -1
1076 : (int)(fraction_compression * 10.0 + 0.5);
1078 if (ft->encoding.compression < 0.5) {
1079 if (p->mp2) {
1080 lsx_fail_errno(ft,SOX_EOF,"Variable bitrate encoding not supported for MP2 audio");
1081 return(SOX_EOF);
1083 #ifdef HAVE_LAME
1084 if (p->lame_get_VBR(p->gfp) == vbr_off)
1085 p->lame_set_VBR(p->gfp, vbr_default);
1087 if (ft->seekable) {
1088 p->vbr_tag = 1;
1089 } else {
1090 lsx_warn("unable to write VBR tag because we can't seek");
1093 if (p->lame_set_VBR_q(p->gfp, bitrate_q) < 0)
1095 lsx_fail_errno(ft, SOX_EOF,
1096 "lame_set_VBR_q(%d) failed (should be between 0 and 9)",
1097 bitrate_q);
1098 return(SOX_EOF);
1100 lsx_report("lame_set_VBR_q(%d)", bitrate_q);
1101 #endif
1102 } else {
1103 if (p->mp2) {
1104 #ifdef HAVE_TWOLAME
1105 fail = (p->twolame_set_brate(p->opt, bitrate_q) != 0);
1106 #endif
1107 } else {
1108 #ifdef HAVE_LAME
1109 fail = (p->lame_set_brate(p->gfp, bitrate_q) < 0);
1110 #endif
1112 if (fail) {
1113 lsx_fail_errno(ft, SOX_EOF,
1114 "%slame_set_brate(%d) failed", p->mp2? "two" : "", bitrate_q);
1115 return(SOX_EOF);
1117 lsx_report("(two)lame_set_brate(%d)", bitrate_q);
1120 /* Set Quality */
1122 if (encoder_q < 0 || p->mp2) {
1123 /* use default quality value */
1124 lsx_report("using %s default quality", p->mp2? "MP2" : "MP3");
1125 } else {
1126 #ifdef HAVE_LAME
1127 if (p->lame_set_quality(p->gfp, encoder_q) < 0) {
1128 lsx_fail_errno(ft, SOX_EOF,
1129 "lame_set_quality(%d) failed", encoder_q);
1130 return(SOX_EOF);
1132 lsx_report("lame_set_quality(%d)", encoder_q);
1133 #endif
1137 if (!p->mp2) {
1138 #ifdef HAVE_LAME
1139 p->lame_set_bWriteVbrTag(p->gfp, p->vbr_tag);
1140 #endif
1143 if (p->mp2) {
1144 #ifdef HAVE_TWOLAME
1145 fail = (p->twolame_init_params(p->opt) != 0);
1146 #endif
1147 } else {
1148 #ifdef HAVE_LAME
1149 fail = (p->lame_init_params(p->gfp) < 0);
1150 #endif
1152 if (fail) {
1153 lsx_fail_errno(ft,SOX_EOF,"%s initialization failed", p->mp2? "Twolame" : "LAME");
1154 return(SOX_EOF);
1157 return(SOX_SUCCESS);
1160 #define MP3_SAMPLE_TO_FLOAT(d) ((float)(32768*SOX_SAMPLE_TO_FLOAT_32BIT(d,)))
1162 static size_t sox_mp3write(sox_format_t * ft, const sox_sample_t *buf, size_t samp)
1164 priv_t *p = (priv_t *)ft->priv;
1165 size_t new_buffer_size;
1166 float *buffer_l, *buffer_r = NULL;
1167 int nsamples = samp/ft->signal.channels;
1168 int i,j;
1169 int written = 0;
1170 SOX_SAMPLE_LOCALS;
1172 new_buffer_size = samp * sizeof(float);
1173 if (p->pcm_buffer_size < new_buffer_size) {
1174 float *new_buffer = lsx_realloc(p->pcm_buffer, new_buffer_size);
1175 if (!new_buffer) {
1176 lsx_fail_errno(ft, SOX_ENOMEM, "Out of memory");
1177 return 0;
1179 p->pcm_buffer_size = new_buffer_size;
1180 p->pcm_buffer = new_buffer;
1183 buffer_l = p->pcm_buffer;
1185 if (p->mp2)
1187 size_t s;
1188 for(s = 0; s < samp; s++)
1189 buffer_l[s] = SOX_SAMPLE_TO_FLOAT_32BIT(buf[s],);
1191 else
1193 if (ft->signal.channels == 2)
1195 /* lame doesn't support interleaved samples for floats so we must break
1196 * them out into seperate buffers.
1198 buffer_r = p->pcm_buffer + nsamples;
1199 j=0;
1200 for (i = 0; i < nsamples; i++)
1202 buffer_l[i] = MP3_SAMPLE_TO_FLOAT(buf[j++]);
1203 buffer_r[i] = MP3_SAMPLE_TO_FLOAT(buf[j++]);
1206 else
1208 j=0;
1209 for (i = 0; i < nsamples; i++) {
1210 buffer_l[i] = MP3_SAMPLE_TO_FLOAT(buf[j++]);
1215 new_buffer_size = LAME_BUFFER_SIZE(nsamples);
1216 if (p->mp3_buffer_size < new_buffer_size) {
1217 unsigned char *new_buffer = lsx_realloc(p->mp3_buffer, new_buffer_size);
1218 if (!new_buffer) {
1219 lsx_fail_errno(ft, SOX_ENOMEM, "Out of memory");
1220 return 0;
1222 p->mp3_buffer_size = new_buffer_size;
1223 p->mp3_buffer = new_buffer;
1226 if(p->mp2) {
1227 #ifdef HAVE_TWOLAME
1228 written = p->twolame_encode_buffer_float32_interleaved(p->opt, buffer_l,
1229 nsamples, p->mp3_buffer, (int)p->mp3_buffer_size);
1230 #endif
1231 } else {
1232 #ifdef HAVE_LAME
1233 written = p->lame_encode_buffer_float(p->gfp, buffer_l, buffer_r,
1234 nsamples, p->mp3_buffer, (int)p->mp3_buffer_size);
1235 #endif
1237 if (written < 0) {
1238 lsx_fail_errno(ft,SOX_EOF,"Encoding failed");
1239 return 0;
1242 if (lsx_writebuf(ft, p->mp3_buffer, (size_t)written) < (size_t)written)
1244 lsx_fail_errno(ft,SOX_EOF,"File write failed");
1245 return 0;
1248 return samp;
1251 static int stopwrite(sox_format_t * ft)
1253 priv_t *p = (priv_t *) ft->priv;
1254 uint64_t num_samples = ft->olength == SOX_IGNORE_LENGTH ? 0 : ft->olength / max(ft->signal.channels, 1);
1255 int written = 0;
1257 if (p->mp2) {
1258 #ifdef HAVE_TWOLAME
1259 written = p->twolame_encode_flush(p->opt, p->mp3_buffer, (int)p->mp3_buffer_size);
1260 #endif
1261 } else {
1262 #ifdef HAVE_LAME
1263 written = p->lame_encode_flush(p->gfp, p->mp3_buffer, (int)p->mp3_buffer_size);
1264 #endif
1266 if (written < 0)
1267 lsx_fail_errno(ft, SOX_EOF, "Encoding failed");
1268 else if (lsx_writebuf(ft, p->mp3_buffer, (size_t)written) < (size_t)written)
1269 lsx_fail_errno(ft, SOX_EOF, "File write failed");
1270 else if (!p->mp2) {
1271 #ifdef HAVE_LAME
1272 if (ft->seekable && (num_samples != p->num_samples || p->vbr_tag))
1273 rewrite_tags(ft, num_samples);
1274 #endif
1277 free(p->mp3_buffer);
1278 free(p->pcm_buffer);
1280 if(p->mp2) {
1281 #ifdef HAVE_TWOLAME
1282 p->twolame_close(&p->opt);
1283 LSX_DLLIBRARY_CLOSE(p, twolame_dl);
1284 #endif
1285 } else {
1286 #ifdef HAVE_LAME
1287 p->lame_close(p->gfp);
1288 LSX_DLLIBRARY_CLOSE(p, lame_dl);
1289 #endif
1291 return SOX_SUCCESS;
1294 #else /* !(HAVE_LAME || HAVE_TWOLAME) */
1295 static int startwrite(sox_format_t * ft UNUSED)
1297 lsx_fail_errno(ft,SOX_EOF,"SoX was compiled with neither MP2 nor MP3 encoding support");
1298 return SOX_EOF;
1300 #define sox_mp3write NULL
1301 #define stopwrite NULL
1302 #endif /* HAVE_LAME || HAVE_TWOLAME */
1304 LSX_FORMAT_HANDLER(mp3)
1306 static char const * const names[] = {"mp3", "mp2", "audio/mpeg", NULL};
1307 static unsigned const write_encodings[] = {
1308 SOX_ENCODING_MP3, 0, 0};
1309 static sox_rate_t const write_rates[] = {
1310 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 0};
1311 static sox_format_handler_t const handler = {SOX_LIB_VERSION_CODE,
1312 "MPEG Layer 2/3 lossy audio compression", names, 0,
1313 startread, sox_mp3read, stopread,
1314 startwrite, sox_mp3write, stopwrite,
1315 sox_mp3seek, write_encodings, write_rates, sizeof(priv_t)
1317 return &handler;
1319 #endif /* defined(HAVE_MAD_H) || defined(HAVE_LAME) || defined(HAVE_TWOLAME) */