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>,
16 #if defined(HAVE_LAME_LAME_H) || defined(HAVE_LAME_H) || defined(DL_LAME)
20 #if defined(HAVE_TWOLAME_H) || defined(DL_TWOLAME)
21 #define HAVE_TWOLAME 1
24 #if defined(HAVE_MAD_H) || defined(HAVE_LAME) || defined(HAVE_TWOLAME)
30 #if defined(HAVE_LAME_LAME_H)
31 #include <lame/lame.h>
32 #elif defined(HAVE_LAME_H)
34 #elif defined(DL_LAME)
35 typedef struct lame_global_struct lame_global_flags
;
42 #if defined(HAVE_ID3TAG) && (defined(HAVE_IO_H) || defined(HAVE_UNISTD_H))
43 #define USING_ID3TAG 1
48 #if defined(HAVE_UNISTD_H)
50 #elif defined(HAVE_IO_H)
54 #define ID3_TAG_FLAG_FOOTERPRESENT 0x10
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.
73 #define mad_timer_zero mad_timer_zero_stub
74 static mad_timer_t
const mad_timer_zero_stub
= {0, 0};
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
[] =
99 #define MAD_FUNC LSX_DLENTRY_DYNAMIC
101 #define MAD_FUNC LSX_DLENTRY_STATIC
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
[] =
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
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
151 #define LAME_FUNC_ID3 LSX_DLENTRY_STUB
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 *))
190 static const char* const twolame_library_names
[] =
201 #define TWOLAME_FUNC LSX_DLENTRY_DYNAMIC
203 #define TWOLAME_FUNC LSX_DLENTRY_STATIC
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 **))
219 typedef struct mp3_priv_t
{
220 unsigned char *mp3_buffer
;
221 size_t mp3_buffer_size
;
224 struct mad_stream Stream
;
225 struct mad_frame Frame
;
226 struct mad_synth Synth
;
230 LSX_DLENTRIES_TO_PTRS(MAD_FUNC_ENTRIES
, mad_dl
);
231 #endif /*HAVE_MAD_H*/
233 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
235 size_t pcm_buffer_size
;
240 lame_global_flags
*gfp
;
241 uint64_t num_samples
;
243 LSX_DLENTRIES_TO_PTRS(LAME_FUNC_ENTRIES
, lame_dl
);
247 twolame_options
*opt
;
248 LSX_DLENTRIES_TO_PTRS(TWOLAME_FUNC_ENTRIES
, twolame_dl
);
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 */
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)
273 size
= 10 + (data
[6]<<21) + (data
[7]<<14) + (data
[8]<<7) + data
[9];
274 if (flags
& ID3_TAG_FLAG_FOOTERPRESENT
)
276 for (; size
< length
&& !data
[size
]; ++size
); /* Consume padding */
283 #endif /*HAVE_MAD_H*/
285 #include "mp3-util.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
;
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
);
321 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
, bytes_read
+remaining
);
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
331 static int sox_mp3_inputtag(sox_format_t
* ft
)
333 priv_t
*p
= (priv_t
*) ft
->priv
;
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
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
);
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
);
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
)
374 if (p
->Stream
.anc_bitlen
< 32)
377 if (!memcmp(anc
->byte
, "Xing", 4) ||
378 !memcmp(anc
->byte
, "Info", 4))
384 static int startread(sox_format_t
* ft
)
386 priv_t
*p
= (priv_t
*) ft
->priv
;
388 sox_bool ignore_length
= ft
->signal
.length
== SOX_IGNORE_LENGTH
;
389 int open_library_result
;
395 "MAD decoder library",
397 open_library_result
);
398 if (open_library_result
)
401 p
->mp3_buffer_size
= sox_globals
.bufsiz
;
402 p
->mp3_buffer
= lsx_malloc(p
->mp3_buffer_size
);
404 ft
->signal
.length
= SOX_UNSPEC
;
409 if (!ft
->signal
.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
))
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.
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
)
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.
461 lsx_fail_errno(ft
,SOX_EOF
,"No valid MP3 frame found");
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
);
474 lsx_fail_errno(ft
, SOX_EFMT
, "Cannot determine number of channels");
478 ft
->signal
.precision
= MP3_MAD_PRECISION
;
479 ft
->signal
.rate
=p
->Frame
.header
.samplerate
;
481 ft
->signal
.length
= SOX_UNSPEC
;
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
);
498 * Read up to len samples from p->Synth
499 * If needed, read some more MP3 data, decode them and synth them
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;
511 size_t x
= (p
->Synth
.pcm
.length
- p
->cursamp
)*ft
->signal
.channels
;
515 for(chan
=0;chan
<ft
->signal
.channels
;chan
++){
516 sample
=p
->Synth
.pcm
.samples
[chan
][p
->cursamp
];
517 if (sample
< -MAD_F_ONE
)
519 else if (sample
>= MAD_F_ONE
)
521 *buf
++=(sox_sample_t
)(sample
<<(32-1-MAD_F_FRACBITS
));
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");
541 if (p
->mad_frame_decode(&p
->Frame
,&p
->Stream
))
543 if(MAD_RECOVERABLE(p
->Stream
.error
))
545 sox_mp3_inputtag(ft
);
550 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
554 lsx_report("unrecoverable frame level error (%s).",
555 p
->mad_stream_errorstr(&p
->Stream
));
561 p
->mad_timer_add(&p
->Timer
,p
->Frame
.header
.duration
);
562 p
->mad_synth_frame(&p
->Synth
,&p
->Frame
);
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
);
578 LSX_DLLIBRARY_CLOSE(p
, mad_dl
);
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;
593 mad_timer_reset(&p
->Timer
);
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 */
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
);
616 lsx_debug("seek failure. unexpected EOF (frames=%" PRIuPTR
" leftover=%" PRIuPTR
")", p
->FrameCount
, leftover
);
619 for (; !depadded
&& padding
< read
&& !p
->mp3_buffer
[padding
]; ++padding
);
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");
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");
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
);
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
;
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
))
676 /* Reset Stream for refilling buffer */
677 p
->mad_stream_finish(&p
->Stream
);
678 p
->mad_stream_init(&p
->Stream
);
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");
692 #define sox_mp3read NULL
693 #define stopread NULL
694 #define sox_mp3seek NULL
695 #endif /*HAVE_MAD_H*/
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
);
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
);
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
);
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
)
730 UNUSED
static void id3tag_set_title_stub(lame_global_flags
* gfp UNUSED
, const char* title UNUSED
)
732 UNUSED
static void id3tag_set_artist_stub(lame_global_flags
* gfp UNUSED
, const char* artist UNUSED
)
734 UNUSED
static void id3tag_set_album_stub(lame_global_flags
* gfp UNUSED
, const char* album UNUSED
)
736 UNUSED
static void id3tag_set_year_stub(lame_global_flags
* gfp UNUSED
, const char* year UNUSED
)
738 UNUSED
static void id3tag_set_comment_stub(lame_global_flags
* gfp UNUSED
, const char* comment UNUSED
)
740 UNUSED
static void id3tag_set_track_stub(lame_global_flags
* gfp UNUSED
, const char* track UNUSED
)
742 UNUSED
static int id3tag_set_genre_stub(lame_global_flags
* gfp UNUSED
, const char* genre UNUSED
)
744 UNUSED
static size_t id3tag_set_pad_stub(lame_global_flags
* gfp UNUSED
, size_t n UNUSED
)
746 UNUSED
static size_t lame_get_id3v2_tag_stub(lame_global_flags
* gfp UNUSED
, unsigned char * buffer UNUSED
, size_t size UNUSED
)
748 UNUSED
static int id3tag_set_fieldvalue_stub(lame_global_flags
* gfp UNUSED
, const char *fieldvalue UNUSED
)
751 static int get_id3v2_tag_size(sox_format_t
* ft
)
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");
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
);
779 /* no ID3 version 2 tag in this stream */
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
;
789 unsigned char * buffer
;
791 if (LSX_DLFUNC_IS_STUB(p
, lame_get_id3v2_tag
))
794 lsx_warn("cannot update track length info - tag update not supported with this version of LAME. Track length will be incorrect.");
796 lsx_report("cannot update track length info - tag update not supported with this version of LAME. Track length will be unspecified.");
800 buffer
= lsx_malloc(id3v2_size
);
803 lsx_warn("cannot update track length info - failed to allocate buffer");
807 if (num_samples
> ULONG_MAX
)
809 lsx_warn("cannot accurately update track length info - file is too long");
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
))
826 lsx_warn("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be invalid.");
828 lsx_report("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be unspecified.");
831 lsx_warn("cannot update track length info - failed to adjust tag size");
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
);
843 static void rewrite_tags(sox_format_t
* ft
, uint64_t num_samples
)
845 priv_t
*p
= (priv_t
*)ft
->priv
;
850 if (lsx_seeki(ft
, (off_t
)0, SEEK_END
)) {
851 lsx_warn("cannot update tags - seek to end failed");
856 file_size
= lsx_tell(ft
);
858 if (file_size
== 0) {
859 lsx_warn("cannot update tags - file size is 0");
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
);
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");
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");
883 if (lametag_size
< 1) {
887 if (lsx_writebuf(ft
, buffer
, lametag_size
) != lametag_size
) {
888 lsx_warn("cannot write VBR tag - VBR tag write failed");
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
;
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'))
921 TWOLAME_FUNC_ENTRIES
,
922 "Twolame encoder library",
923 twolame_library_names
,
926 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP2 encoding support");
935 "LAME encoder library",
939 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP3 encoding support");
943 if (openlibrary_result
)
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
);
954 p
->opt
= p
->twolame_init();
957 lsx_fail_errno(ft
,SOX_EOF
,"Initialization of Twolame library failed");
963 p
->gfp
= p
->lame_init();
966 lsx_fail_errno(ft
,SOX_EOF
,"Initialization of LAME library failed");
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
);
980 ft
->signal
.precision
= MP3_LAME_PRECISION
;
982 if (ft
->signal
.channels
!= SOX_ENCODING_UNKNOWN
) {
985 fail
= (p
->twolame_set_num_channels(p
->opt
,(int)ft
->signal
.channels
) != 0);
989 fail
= (p
->lame_set_num_channels(p
->gfp
,(int)ft
->signal
.channels
) < 0);
993 lsx_fail_errno(ft
,SOX_EOF
,"Unsupported number of channels");
1000 ft
->signal
.channels
= p
->twolame_get_num_channels(p
->opt
); /* Twolame default */
1004 ft
->signal
.channels
= p
->lame_get_num_channels(p
->gfp
); /* LAME default */
1011 p
->twolame_set_in_samplerate(p
->opt
,(int)ft
->signal
.rate
);
1012 p
->twolame_set_out_samplerate(p
->opt
,(int)ft
->signal
.rate
);
1016 p
->lame_set_in_samplerate(p
->gfp
,(int)ft
->signal
.rate
);
1017 p
->lame_set_out_samplerate(p
->gfp
,(int)ft
->signal
.rate
);
1023 if (!LSX_DLFUNC_IS_STUB(p
, id3tag_init
))
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
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
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");
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
;
1074 fraction_compression
== 0.0
1076 : (int)(fraction_compression
* 10.0 + 0.5);
1078 if (ft
->encoding
.compression
< 0.5) {
1080 lsx_fail_errno(ft
,SOX_EOF
,"Variable bitrate encoding not supported for MP2 audio");
1084 if (p
->lame_get_VBR(p
->gfp
) == vbr_off
)
1085 p
->lame_set_VBR(p
->gfp
, vbr_default
);
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)",
1100 lsx_report("lame_set_VBR_q(%d)", bitrate_q
);
1105 fail
= (p
->twolame_set_brate(p
->opt
, bitrate_q
) != 0);
1109 fail
= (p
->lame_set_brate(p
->gfp
, bitrate_q
) < 0);
1113 lsx_fail_errno(ft
, SOX_EOF
,
1114 "%slame_set_brate(%d) failed", p
->mp2
? "two" : "", bitrate_q
);
1117 lsx_report("(two)lame_set_brate(%d)", bitrate_q
);
1122 if (encoder_q
< 0 || p
->mp2
) {
1123 /* use default quality value */
1124 lsx_report("using %s default quality", p
->mp2
? "MP2" : "MP3");
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
);
1132 lsx_report("lame_set_quality(%d)", encoder_q
);
1139 p
->lame_set_bWriteVbrTag(p
->gfp
, p
->vbr_tag
);
1145 fail
= (p
->twolame_init_params(p
->opt
) != 0);
1149 fail
= (p
->lame_init_params(p
->gfp
) < 0);
1153 lsx_fail_errno(ft
,SOX_EOF
,"%s initialization failed", p
->mp2
? "Twolame" : "LAME");
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
;
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
);
1176 lsx_fail_errno(ft
, SOX_ENOMEM
, "Out of memory");
1179 p
->pcm_buffer_size
= new_buffer_size
;
1180 p
->pcm_buffer
= new_buffer
;
1183 buffer_l
= p
->pcm_buffer
;
1188 for(s
= 0; s
< samp
; s
++)
1189 buffer_l
[s
] = SOX_SAMPLE_TO_FLOAT_32BIT(buf
[s
],);
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
;
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
++]);
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
);
1219 lsx_fail_errno(ft
, SOX_ENOMEM
, "Out of memory");
1222 p
->mp3_buffer_size
= new_buffer_size
;
1223 p
->mp3_buffer
= new_buffer
;
1228 written
= p
->twolame_encode_buffer_float32_interleaved(p
->opt
, buffer_l
,
1229 nsamples
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1233 written
= p
->lame_encode_buffer_float(p
->gfp
, buffer_l
, buffer_r
,
1234 nsamples
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1238 lsx_fail_errno(ft
,SOX_EOF
,"Encoding failed");
1242 if (lsx_writebuf(ft
, p
->mp3_buffer
, (size_t)written
) < (size_t)written
)
1244 lsx_fail_errno(ft
,SOX_EOF
,"File write failed");
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);
1259 written
= p
->twolame_encode_flush(p
->opt
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1263 written
= p
->lame_encode_flush(p
->gfp
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
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");
1272 if (ft
->seekable
&& (num_samples
!= p
->num_samples
|| p
->vbr_tag
))
1273 rewrite_tags(ft
, num_samples
);
1277 free(p
->mp3_buffer
);
1278 free(p
->pcm_buffer
);
1282 p
->twolame_close(&p
->opt
);
1283 LSX_DLLIBRARY_CLOSE(p
, twolame_dl
);
1287 p
->lame_close(p
->gfp
);
1288 LSX_DLLIBRARY_CLOSE(p
, lame_dl
);
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");
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
)
1319 #endif /* defined(HAVE_MAD_H) || defined(HAVE_LAME) || defined(HAVE_TWOLAME) */