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
61 /* Under Windows, importing data from DLLs is a dicey proposition. This is true
62 * when using dlopen, but also true if linking directly against the DLL if the
63 * header does not mark the data as __declspec(dllexport), which mad.h does not.
64 * Sidestep the issue by defining our own mad_timer_zero. This is needed because
65 * mad_timer_zero is used in some of the mad.h macros.
68 #define mad_timer_zero mad_timer_zero_stub
69 static mad_timer_t
const mad_timer_zero_stub
= {0, 0};
72 #define MAXFRAMESIZE 2880
73 #define ID3PADDING 128
75 /* LAME takes float values as input. */
76 #define MP3_LAME_PRECISION 24
78 /* MAD returns values with MAD_F_FRACBITS (28) bits of precision, though it's
79 not certain that all of them are meaningful. Default to 16 bits to
80 align with most users expectation of output file should be 16 bits. */
81 #define MP3_MAD_PRECISION 16
83 static const char* const mad_library_names
[] =
94 #define MAD_FUNC LSX_DLENTRY_DYNAMIC
96 #define MAD_FUNC LSX_DLENTRY_STATIC
99 #define MAD_FUNC_ENTRIES(f,x) \
100 MAD_FUNC(f,x, void, mad_stream_buffer, (struct mad_stream *, unsigned char const *, unsigned long)) \
101 MAD_FUNC(f,x, void, mad_stream_skip, (struct mad_stream *, unsigned long)) \
102 MAD_FUNC(f,x, int, mad_stream_sync, (struct mad_stream *)) \
103 MAD_FUNC(f,x, void, mad_stream_init, (struct mad_stream *)) \
104 MAD_FUNC(f,x, void, mad_frame_init, (struct mad_frame *)) \
105 MAD_FUNC(f,x, void, mad_synth_init, (struct mad_synth *)) \
106 MAD_FUNC(f,x, int, mad_frame_decode, (struct mad_frame *, struct mad_stream *)) \
107 MAD_FUNC(f,x, void, mad_timer_add, (mad_timer_t *, mad_timer_t)) \
108 MAD_FUNC(f,x, void, mad_synth_frame, (struct mad_synth *, struct mad_frame const *)) \
109 MAD_FUNC(f,x, char const *, mad_stream_errorstr, (struct mad_stream const *)) \
110 MAD_FUNC(f,x, void, mad_frame_finish, (struct mad_frame *)) \
111 MAD_FUNC(f,x, void, mad_stream_finish, (struct mad_stream *)) \
112 MAD_FUNC(f,x, unsigned long, mad_bit_read, (struct mad_bitptr *, unsigned int)) \
113 MAD_FUNC(f,x, int, mad_header_decode, (struct mad_header *, struct mad_stream *)) \
114 MAD_FUNC(f,x, void, mad_header_init, (struct mad_header *)) \
115 MAD_FUNC(f,x, signed long, mad_timer_count, (mad_timer_t, enum mad_units)) \
116 MAD_FUNC(f,x, void, mad_timer_multiply, (mad_timer_t *, signed long))
118 static const char* const lame_library_names
[] =
130 #define LAME_FUNC LSX_DLENTRY_DYNAMIC
132 #define LAME_FUNC LSX_DLENTRY_STATIC
135 #define LAME_FUNC_ENTRIES(f,x) \
136 LAME_FUNC(f,x, lame_global_flags*, lame_init, (void)) \
137 LAME_FUNC(f,x, int, lame_set_errorf, (lame_global_flags *, void (*)(const char *, va_list))) \
138 LAME_FUNC(f,x, int, lame_set_debugf, (lame_global_flags *, void (*)(const char *, va_list))) \
139 LAME_FUNC(f,x, int, lame_set_msgf, (lame_global_flags *, void (*)(const char *, va_list))) \
140 LAME_FUNC(f,x, int, lame_set_num_samples, (lame_global_flags *, unsigned long)) \
141 LAME_FUNC(f,x, int, lame_get_num_channels, (const lame_global_flags *)) \
142 LAME_FUNC(f,x, int, lame_set_num_channels, (lame_global_flags *, int)) \
143 LAME_FUNC(f,x, int, lame_set_in_samplerate, (lame_global_flags *, int)) \
144 LAME_FUNC(f,x, int, lame_set_out_samplerate, (lame_global_flags *, int)) \
145 LAME_FUNC(f,x, int, lame_set_bWriteVbrTag, (lame_global_flags *, int)) \
146 LAME_FUNC(f,x, int, lame_set_brate, (lame_global_flags *, int)) \
147 LAME_FUNC(f,x, int, lame_set_quality, (lame_global_flags *, int)) \
148 LAME_FUNC(f,x, vbr_mode, lame_get_VBR, (const lame_global_flags *)) \
149 LAME_FUNC(f,x, int, lame_set_VBR, (lame_global_flags *, vbr_mode)) \
150 LAME_FUNC(f,x, int, lame_set_VBR_q, (lame_global_flags *, int)) \
151 LAME_FUNC(f,x, int, lame_init_params, (lame_global_flags *)) \
152 LAME_FUNC(f,x, int, lame_encode_buffer_float, (lame_global_flags *, const float[], const float[], const int, unsigned char *, const int)) \
153 LAME_FUNC(f,x, int, lame_encode_flush, (lame_global_flags *, unsigned char *, int)) \
154 LAME_FUNC(f,x, int, lame_close, (lame_global_flags *)) \
155 LAME_FUNC(f,x, size_t, lame_get_lametag_frame, (const lame_global_flags *, unsigned char*, size_t)) \
156 LAME_FUNC(f,x, void, id3tag_init, (lame_global_flags *)) \
157 LAME_FUNC(f,x, void, id3tag_set_title, (lame_global_flags *, const char* title)) \
158 LAME_FUNC(f,x, void, id3tag_set_artist, (lame_global_flags *, const char* artist)) \
159 LAME_FUNC(f,x, void, id3tag_set_album, (lame_global_flags *, const char* album)) \
160 LAME_FUNC(f,x, void, id3tag_set_year, (lame_global_flags *, const char* year)) \
161 LAME_FUNC(f,x, void, id3tag_set_comment, (lame_global_flags *, const char* comment)) \
162 LAME_FUNC(f,x, int, id3tag_set_track, (lame_global_flags *, const char* track)) \
163 LAME_FUNC(f,x, int, id3tag_set_genre, (lame_global_flags *, const char* genre)) \
164 LAME_FUNC(f,x, size_t, id3tag_set_pad, (lame_global_flags *, size_t)) \
165 LAME_FUNC(f,x, size_t, lame_get_id3v2_tag, (lame_global_flags *, unsigned char*, size_t)) \
166 LAME_FUNC(f,x, int, id3tag_set_fieldvalue, (lame_global_flags *, const char *))
169 static const char* const twolame_library_names
[] =
180 #define TWOLAME_FUNC LSX_DLENTRY_DYNAMIC
182 #define TWOLAME_FUNC LSX_DLENTRY_STATIC
185 #define TWOLAME_FUNC_ENTRIES(f,x) \
186 TWOLAME_FUNC(f,x, twolame_options*, twolame_init, (void)) \
187 TWOLAME_FUNC(f,x, int, twolame_get_num_channels, (twolame_options*)) \
188 TWOLAME_FUNC(f,x, int, twolame_set_num_channels, (twolame_options*, int)) \
189 TWOLAME_FUNC(f,x, int, twolame_set_in_samplerate, (twolame_options *, int)) \
190 TWOLAME_FUNC(f,x, int, twolame_set_out_samplerate, (twolame_options *, int)) \
191 TWOLAME_FUNC(f,x, int, twolame_set_brate, (twolame_options *, int)) \
192 TWOLAME_FUNC(f,x, int, twolame_init_params, (twolame_options *)) \
193 TWOLAME_FUNC(f,x, int, twolame_encode_buffer_float32_interleaved, (twolame_options *, const float [], int, unsigned char *, int)) \
194 TWOLAME_FUNC(f,x, int, twolame_encode_flush, (twolame_options *, unsigned char *, int)) \
195 TWOLAME_FUNC(f,x, void, twolame_close, (twolame_options **))
198 typedef struct mp3_priv_t
{
199 unsigned char *mp3_buffer
;
200 size_t mp3_buffer_size
;
203 struct mad_stream Stream
;
204 struct mad_frame Frame
;
205 struct mad_synth Synth
;
209 LSX_DLENTRIES_TO_PTRS(MAD_FUNC_ENTRIES
, mad_dl
);
210 #endif /*HAVE_MAD_H*/
212 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
214 size_t pcm_buffer_size
;
219 lame_global_flags
*gfp
;
220 uint64_t num_samples
;
222 LSX_DLENTRIES_TO_PTRS(LAME_FUNC_ENTRIES
, lame_dl
);
226 twolame_options
*opt
;
227 LSX_DLENTRIES_TO_PTRS(TWOLAME_FUNC_ENTRIES
, twolame_dl
);
233 /* This function merges the functions tagtype() and id3_tag_query()
234 from MAD's libid3tag, so we don't have to link to it
235 Returns 0 if the frame is not an ID3 tag, tag length if it is */
237 static int tagtype(const unsigned char *data
, size_t length
)
239 if (length
>= 3 && data
[0] == 'T' && data
[1] == 'A' && data
[2] == 'G')
241 return 128; /* ID3V1 */
245 (data
[0] == 'I' && data
[1] == 'D' && data
[2] == '3') &&
246 data
[3] < 0xff && data
[4] < 0xff &&
247 data
[6] < 0x80 && data
[7] < 0x80 && data
[8] < 0x80 && data
[9] < 0x80)
252 size
= 10 + (data
[6]<<21) + (data
[7]<<14) + (data
[8]<<7) + data
[9];
253 if (flags
& ID3_TAG_FLAG_FOOTERPRESENT
)
255 for (; size
< length
&& !data
[size
]; ++size
); /* Consume padding */
262 #endif /*HAVE_MAD_H*/
264 #include "mp3-util.h"
269 * (Re)fill the stream buffer that is to be decoded. If any data
270 * still exists in the buffer then they are first shifted to be
271 * front of the stream buffer.
273 static int sox_mp3_input(sox_format_t
* ft
)
275 priv_t
*p
= (priv_t
*) ft
->priv
;
279 remaining
= p
->Stream
.bufend
- p
->Stream
.next_frame
;
281 /* libmad does not consume all the buffer it's given. Some
282 * data, part of a truncated frame, is left unused at the
283 * end of the buffer. That data must be put back at the
284 * beginning of the buffer and taken in account for
285 * refilling the buffer. This means that the input buffer
286 * must be large enough to hold a complete frame at the
287 * highest observable bit-rate (currently 448 kb/s).
288 * TODO: Is 2016 bytes the size of the largest frame?
289 * (448000*(1152/32000))/8
291 memmove(p
->mp3_buffer
, p
->Stream
.next_frame
, remaining
);
293 bytes_read
= lsx_readbuf(ft
, p
->mp3_buffer
+remaining
,
294 p
->mp3_buffer_size
-remaining
);
300 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
, bytes_read
+remaining
);
306 /* Attempts to read an ID3 tag at the current location in stream and
307 * consume it all. Returns SOX_EOF if no tag is found. Its up to
310 static int sox_mp3_inputtag(sox_format_t
* ft
)
312 priv_t
*p
= (priv_t
*) ft
->priv
;
318 /* FIXME: This needs some more work if we are to ever
319 * look at the ID3 frame. This is because the Stream
320 * may not be able to hold the complete ID3 frame.
321 * We should consume the whole frame inside tagtype()
322 * instead of outside of tagframe(). That would support
323 * recovering when Stream contains less then 8-bytes (header)
324 * and also when ID3v2 is bigger then Stream buffer size.
325 * Need to pass in stream so that buffer can be
326 * consumed as well as letting additional data to be
329 remaining
= p
->Stream
.bufend
- p
->Stream
.next_frame
;
330 if ((tagsize
= tagtype(p
->Stream
.this_frame
, remaining
)))
332 p
->mad_stream_skip(&p
->Stream
, tagsize
);
336 /* We know that a valid frame hasn't been found yet
337 * so help libmad out and go back into frame seek mode.
338 * This is true whether an ID3 tag was found or not.
340 p
->mad_stream_sync(&p
->Stream
);
345 static sox_bool
sox_mp3_vbrtag(sox_format_t
*ft
)
347 priv_t
*p
= ft
->priv
;
348 struct mad_bitptr
*anc
= &p
->Stream
.anc_ptr
;
350 if (p
->Frame
.header
.layer
!= MAD_LAYER_III
)
353 if (p
->Stream
.anc_bitlen
< 32)
356 if (!memcmp(anc
->byte
, "Xing", 4) ||
357 !memcmp(anc
->byte
, "Info", 4))
363 static int startread(sox_format_t
* ft
)
365 priv_t
*p
= (priv_t
*) ft
->priv
;
367 sox_bool ignore_length
= ft
->signal
.length
== SOX_IGNORE_LENGTH
;
368 int open_library_result
;
374 "MAD decoder library",
376 open_library_result
);
377 if (open_library_result
)
380 p
->mp3_buffer_size
= sox_globals
.bufsiz
;
381 p
->mp3_buffer
= lsx_malloc(p
->mp3_buffer_size
);
383 ft
->signal
.length
= SOX_UNSPEC
;
388 if (!ft
->signal
.length
)
391 ft
->signal
.length
= mp3_duration(ft
);
394 p
->mad_stream_init(&p
->Stream
);
395 p
->mad_frame_init(&p
->Frame
);
396 p
->mad_synth_init(&p
->Synth
);
397 mad_timer_reset(&p
->Timer
);
399 ft
->encoding
.encoding
= SOX_ENCODING_MP3
;
401 /* Decode at least one valid frame to find out the input
402 * format. The decoded frame will be saved off so that it
403 * can be processed later.
405 ReadSize
= lsx_readbuf(ft
, p
->mp3_buffer
, p
->mp3_buffer_size
);
406 if (ReadSize
!= p
->mp3_buffer_size
&& lsx_error(ft
))
409 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
, ReadSize
);
411 /* Find a valid frame before starting up. This makes sure
412 * that we have a valid MP3 and also skips past ID3v2 tags
413 * at the beginning of the audio file.
416 while (p
->mad_frame_decode(&p
->Frame
,&p
->Stream
))
418 /* check whether input buffer needs a refill */
419 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
421 if (sox_mp3_input(ft
) == SOX_EOF
)
427 /* Consume any ID3 tags */
428 sox_mp3_inputtag(ft
);
430 /* FIXME: We should probably detect when we've read
431 * a bunch of non-ID3 data and still haven't found a
432 * frame. In that case we can abort early without
433 * scanning the whole file.
440 lsx_fail_errno(ft
,SOX_EOF
,"No valid MP3 frame found");
444 switch(p
->Frame
.header
.mode
)
446 case MAD_MODE_SINGLE_CHANNEL
:
447 case MAD_MODE_DUAL_CHANNEL
:
448 case MAD_MODE_JOINT_STEREO
:
449 case MAD_MODE_STEREO
:
450 ft
->signal
.channels
= MAD_NCHANNELS(&p
->Frame
.header
);
453 lsx_fail_errno(ft
, SOX_EFMT
, "Cannot determine number of channels");
457 ft
->signal
.precision
= MP3_MAD_PRECISION
;
458 ft
->signal
.rate
=p
->Frame
.header
.samplerate
;
460 ft
->signal
.length
= SOX_UNSPEC
;
462 ft
->signal
.length
*= ft
->signal
.channels
; /* Keep separate from line above! */
465 if (!sox_mp3_vbrtag(ft
))
466 p
->Stream
.next_frame
= p
->Stream
.this_frame
;
468 p
->mad_frame_init(&p
->Frame
);
477 * Read up to len samples from p->Synth
478 * If needed, read some more MP3 data, decode them and synth them
480 * Return number of samples read.
482 static size_t sox_mp3read(sox_format_t
* ft
, sox_sample_t
*buf
, size_t len
)
484 priv_t
*p
= (priv_t
*) ft
->priv
;
485 size_t donow
,i
,done
=0;
490 size_t x
= (p
->Synth
.pcm
.length
- p
->cursamp
)*ft
->signal
.channels
;
494 for(chan
=0;chan
<ft
->signal
.channels
;chan
++){
495 sample
=p
->Synth
.pcm
.samples
[chan
][p
->cursamp
];
496 if (sample
< -MAD_F_ONE
)
498 else if (sample
>= MAD_F_ONE
)
500 *buf
++=(sox_sample_t
)(sample
<<(32-1-MAD_F_FRACBITS
));
511 /* check whether input buffer needs a refill */
512 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
514 if (sox_mp3_input(ft
) == SOX_EOF
) {
515 lsx_debug("sox_mp3_input EOF");
520 if (p
->mad_frame_decode(&p
->Frame
,&p
->Stream
))
522 if(MAD_RECOVERABLE(p
->Stream
.error
))
524 sox_mp3_inputtag(ft
);
529 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
533 lsx_report("unrecoverable frame level error (%s).",
534 p
->mad_stream_errorstr(&p
->Stream
));
540 p
->mad_timer_add(&p
->Timer
,p
->Frame
.header
.duration
);
541 p
->mad_synth_frame(&p
->Synth
,&p
->Frame
);
548 static int stopread(sox_format_t
* ft
)
550 priv_t
*p
=(priv_t
*) ft
->priv
;
552 mad_synth_finish(&p
->Synth
);
553 p
->mad_frame_finish(&p
->Frame
);
554 p
->mad_stream_finish(&p
->Stream
);
557 LSX_DLLIBRARY_CLOSE(p
, mad_dl
);
561 static int sox_mp3seek(sox_format_t
* ft
, uint64_t offset
)
563 priv_t
* p
= (priv_t
*) ft
->priv
;
564 size_t initial_bitrate
= p
->Frame
.header
.bitrate
;
565 size_t tagsize
= 0, consumed
= 0;
566 sox_bool vbr
= sox_false
; /* Variable Bit Rate */
567 sox_bool depadded
= sox_false
;
568 uint64_t to_skip_samples
= 0;
572 mad_timer_reset(&p
->Timer
);
575 /* They where opened in startread */
576 mad_synth_finish(&p
->Synth
);
577 p
->mad_frame_finish(&p
->Frame
);
578 p
->mad_stream_finish(&p
->Stream
);
580 p
->mad_stream_init(&p
->Stream
);
581 p
->mad_frame_init(&p
->Frame
);
582 p
->mad_synth_init(&p
->Synth
);
584 offset
/= ft
->signal
.channels
;
585 to_skip_samples
= offset
;
587 while(sox_true
) { /* Read data from the MP3 file */
590 size_t leftover
= p
->Stream
.bufend
- p
->Stream
.next_frame
;
592 memmove(p
->mp3_buffer
, p
->Stream
.this_frame
, leftover
);
593 read
= lsx_readbuf(ft
, p
->mp3_buffer
+ leftover
, p
->mp3_buffer_size
- leftover
);
595 lsx_debug("seek failure. unexpected EOF (frames=%" PRIuPTR
" leftover=%" PRIuPTR
")", p
->FrameCount
, leftover
);
598 for (; !depadded
&& padding
< read
&& !p
->mp3_buffer
[padding
]; ++padding
);
600 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
+ padding
, leftover
+ read
- padding
);
602 while (sox_true
) { /* Decode frame headers */
603 static unsigned short samples
;
604 p
->Stream
.error
= MAD_ERROR_NONE
;
606 /* Not an audio frame */
607 if (p
->mad_header_decode(&p
->Frame
.header
, &p
->Stream
) == -1) {
608 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
609 break; /* Normal behaviour; get some more data from the file */
610 if (!MAD_RECOVERABLE(p
->Stream
.error
)) {
611 lsx_warn("unrecoverable MAD error");
614 if (p
->Stream
.error
== MAD_ERROR_LOSTSYNC
) {
615 unsigned available
= (p
->Stream
.bufend
- p
->Stream
.this_frame
);
616 tagsize
= tagtype(p
->Stream
.this_frame
, (size_t) available
);
617 if (tagsize
) { /* It's some ID3 tags, so just skip */
618 if (tagsize
>= available
) {
619 lsx_seeki(ft
, (off_t
)(tagsize
- available
), SEEK_CUR
);
620 depadded
= sox_false
;
622 p
->mad_stream_skip(&p
->Stream
, min(tagsize
, available
));
624 else lsx_warn("MAD lost sync");
626 else lsx_warn("recoverable MAD error");
630 consumed
+= p
->Stream
.next_frame
- p
->Stream
.this_frame
;
631 vbr
|= (p
->Frame
.header
.bitrate
!= initial_bitrate
);
633 samples
= 32 * MAD_NSBSAMPLES(&p
->Frame
.header
);
636 p
->mad_timer_add(&p
->Timer
, p
->Frame
.header
.duration
);
638 if(to_skip_samples
<= samples
)
640 p
->mad_frame_decode(&p
->Frame
,&p
->Stream
);
641 p
->mad_synth_frame(&p
->Synth
, &p
->Frame
);
642 p
->cursamp
= to_skip_samples
;
645 else to_skip_samples
-= samples
;
647 /* If not VBR, we can extrapolate frame size */
648 if (p
->FrameCount
== 64 && !vbr
) {
649 p
->FrameCount
= offset
/ samples
;
650 to_skip_samples
= offset
% samples
;
652 if (SOX_SUCCESS
!= lsx_seeki(ft
, (off_t
)(p
->FrameCount
* consumed
/ 64 + tagsize
), SEEK_SET
))
655 /* Reset Stream for refilling buffer */
656 p
->mad_stream_finish(&p
->Stream
);
657 p
->mad_stream_init(&p
->Stream
);
665 #else /* !HAVE_MAD_H */
666 static int startread(sox_format_t
* ft
)
668 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP3 decoding support");
671 #define sox_mp3read NULL
672 #define stopread NULL
673 #define sox_mp3seek NULL
674 #endif /*HAVE_MAD_H*/
678 /* Adapters for lame message callbacks: */
680 static void errorf(const char* fmt
, va_list va
)
682 sox_globals
.subsystem
=__FILE__
;
683 if (sox_globals
.output_message_handler
)
684 (*sox_globals
.output_message_handler
)(1,sox_globals
.subsystem
,fmt
,va
);
688 static void debugf(const char* fmt
, va_list va
)
690 sox_globals
.subsystem
=__FILE__
;
691 if (sox_globals
.output_message_handler
)
692 (*sox_globals
.output_message_handler
)(4,sox_globals
.subsystem
,fmt
,va
);
696 static void msgf(const char* fmt
, va_list va
)
698 sox_globals
.subsystem
=__FILE__
;
699 if (sox_globals
.output_message_handler
)
700 (*sox_globals
.output_message_handler
)(3,sox_globals
.subsystem
,fmt
,va
);
704 /* These functions are considered optional. If they aren't present in the
705 library, the stub versions defined here will be used instead. */
707 UNUSED
static void id3tag_init_stub(lame_global_flags
* gfp UNUSED
)
709 UNUSED
static void id3tag_set_title_stub(lame_global_flags
* gfp UNUSED
, const char* title UNUSED
)
711 UNUSED
static void id3tag_set_artist_stub(lame_global_flags
* gfp UNUSED
, const char* artist UNUSED
)
713 UNUSED
static void id3tag_set_album_stub(lame_global_flags
* gfp UNUSED
, const char* album UNUSED
)
715 UNUSED
static void id3tag_set_year_stub(lame_global_flags
* gfp UNUSED
, const char* year UNUSED
)
717 UNUSED
static void id3tag_set_comment_stub(lame_global_flags
* gfp UNUSED
, const char* comment UNUSED
)
719 UNUSED
static void id3tag_set_track_stub(lame_global_flags
* gfp UNUSED
, const char* track UNUSED
)
721 UNUSED
static int id3tag_set_genre_stub(lame_global_flags
* gfp UNUSED
, const char* genre UNUSED
)
723 UNUSED
static size_t id3tag_set_pad_stub(lame_global_flags
* gfp UNUSED
, size_t n UNUSED
)
725 UNUSED
static size_t lame_get_id3v2_tag_stub(lame_global_flags
* gfp UNUSED
, unsigned char * buffer UNUSED
, size_t size UNUSED
)
727 UNUSED
static int id3tag_set_fieldvalue_stub(lame_global_flags
* gfp UNUSED
, const char *fieldvalue UNUSED
)
730 static int get_id3v2_tag_size(sox_format_t
* ft
)
734 unsigned char id3v2_header
[10];
736 if (lsx_seeki(ft
, (off_t
)0, SEEK_SET
) != 0) {
737 lsx_warn("cannot update id3 tag - failed to seek to beginning");
741 /* read 10 bytes in case there's an ID3 version 2 header here */
742 bytes_read
= lsx_readbuf(ft
, id3v2_header
, sizeof(id3v2_header
));
743 if (bytes_read
!= sizeof(id3v2_header
)) {
744 lsx_warn("cannot update id3 tag - failed to read id3 header");
745 return SOX_EOF
; /* not readable, maybe opened Write-Only */
748 /* does the stream begin with the ID3 version 2 file identifier? */
749 if (!strncmp((char *) id3v2_header
, "ID3", (size_t)3)) {
750 /* the tag size (minus the 10-byte header) is encoded into four
751 * bytes where the most significant bit is clear in each byte */
752 id3v2_size
= (((id3v2_header
[6] & 0x7f) << 21)
753 | ((id3v2_header
[7] & 0x7f) << 14)
754 | ((id3v2_header
[8] & 0x7f) << 7)
755 | (id3v2_header
[9] & 0x7f))
756 + sizeof(id3v2_header
);
758 /* no ID3 version 2 tag in this stream */
764 static void rewrite_id3v2_tag(sox_format_t
* ft
, size_t id3v2_size
, uint64_t num_samples
)
766 priv_t
*p
= (priv_t
*)ft
->priv
;
768 unsigned char * buffer
;
770 if (LSX_DLFUNC_IS_STUB(p
, lame_get_id3v2_tag
))
773 lsx_warn("cannot update track length info - tag update not supported with this version of LAME. Track length will be incorrect.");
775 lsx_report("cannot update track length info - tag update not supported with this version of LAME. Track length will be unspecified.");
779 buffer
= lsx_malloc(id3v2_size
);
782 lsx_warn("cannot update track length info - failed to allocate buffer");
786 if (num_samples
> ULONG_MAX
)
788 lsx_warn("cannot accurately update track length info - file is too long");
791 p
->lame_set_num_samples(p
->gfp
, (unsigned long)num_samples
);
792 lsx_debug("updated MP3 TLEN to %lu samples", (unsigned long)num_samples
);
794 new_size
= p
->lame_get_id3v2_tag(p
->gfp
, buffer
, id3v2_size
);
796 if (new_size
!= id3v2_size
&& new_size
-ID3PADDING
<= id3v2_size
) {
797 p
->id3tag_set_pad(p
->gfp
, ID3PADDING
+ id3v2_size
- new_size
);
798 new_size
= p
->lame_get_id3v2_tag(p
->gfp
, buffer
, id3v2_size
);
801 if (new_size
!= id3v2_size
) {
802 if (LSX_DLFUNC_IS_STUB(p
, id3tag_set_pad
))
805 lsx_warn("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be invalid.");
807 lsx_report("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be unspecified.");
810 lsx_warn("cannot update track length info - failed to adjust tag size");
812 lsx_seeki(ft
, (off_t
)0, SEEK_SET
);
813 /* Overwrite the Id3v2 tag (this time TLEN should be accurate) */
814 if (lsx_writebuf(ft
, buffer
, id3v2_size
) != 1) {
815 lsx_debug("Rewrote Id3v2 tag (%" PRIuPTR
" bytes)", id3v2_size
);
822 static void rewrite_tags(sox_format_t
* ft
, uint64_t num_samples
)
824 priv_t
*p
= (priv_t
*)ft
->priv
;
829 if (lsx_seeki(ft
, (off_t
)0, SEEK_END
)) {
830 lsx_warn("cannot update tags - seek to end failed");
835 file_size
= lsx_tell(ft
);
837 if (file_size
== 0) {
838 lsx_warn("cannot update tags - file size is 0");
842 id3v2_size
= get_id3v2_tag_size(ft
);
843 if (id3v2_size
> 0 && num_samples
!= p
->num_samples
) {
844 rewrite_id3v2_tag(ft
, id3v2_size
, num_samples
);
849 uint8_t buffer
[MAXFRAMESIZE
];
851 if (lsx_seeki(ft
, (off_t
)id3v2_size
, SEEK_SET
)) {
852 lsx_warn("cannot write VBR tag - seek to tag block failed");
856 lametag_size
= p
->lame_get_lametag_frame(p
->gfp
, buffer
, sizeof(buffer
));
857 if (lametag_size
> sizeof(buffer
)) {
858 lsx_warn("cannot write VBR tag - VBR tag too large for buffer");
862 if (lametag_size
< 1) {
866 if (lsx_writebuf(ft
, buffer
, lametag_size
) != lametag_size
) {
867 lsx_warn("cannot write VBR tag - VBR tag write failed");
869 lsx_debug("rewrote VBR tag (%" PRIuPTR
" bytes)", lametag_size
);
874 #endif /* HAVE_LAME */
876 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
878 #define LAME_BUFFER_SIZE(num_samples) (((num_samples) + 3) / 4 * 5 + 7200)
880 static int startwrite(sox_format_t
* ft
)
882 priv_t
*p
= (priv_t
*) ft
->priv
;
883 int openlibrary_result
;
886 if (ft
->encoding
.encoding
!= SOX_ENCODING_MP3
) {
887 if(ft
->encoding
.encoding
!= SOX_ENCODING_UNKNOWN
)
888 lsx_report("Encoding forced to MP2/MP3");
889 ft
->encoding
.encoding
= SOX_ENCODING_MP3
;
892 if(strchr(ft
->filetype
, '2'))
900 TWOLAME_FUNC_ENTRIES
,
901 "Twolame encoder library",
902 twolame_library_names
,
905 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP2 encoding support");
914 "LAME encoder library",
918 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP3 encoding support");
922 if (openlibrary_result
)
925 p
->mp3_buffer_size
= LAME_BUFFER_SIZE(sox_globals
.bufsiz
/ max(ft
->signal
.channels
, 1));
926 p
->mp3_buffer
= lsx_malloc(p
->mp3_buffer_size
);
928 p
->pcm_buffer_size
= sox_globals
.bufsiz
* sizeof(float);
929 p
->pcm_buffer
= lsx_malloc(p
->pcm_buffer_size
);
933 p
->opt
= p
->twolame_init();
936 lsx_fail_errno(ft
,SOX_EOF
,"Initialization of Twolame library failed");
942 p
->gfp
= p
->lame_init();
945 lsx_fail_errno(ft
,SOX_EOF
,"Initialization of LAME library failed");
949 /* First set message callbacks so we don't miss any messages: */
950 p
->lame_set_errorf(p
->gfp
,errorf
);
951 p
->lame_set_debugf(p
->gfp
,debugf
);
952 p
->lame_set_msgf (p
->gfp
,msgf
);
954 p
->num_samples
= ft
->signal
.length
== SOX_IGNORE_LENGTH
? 0 : ft
->signal
.length
/ max(ft
->signal
.channels
, 1);
955 p
->lame_set_num_samples(p
->gfp
, p
->num_samples
> ULONG_MAX
? 0 : (unsigned long)p
->num_samples
);
959 ft
->signal
.precision
= MP3_LAME_PRECISION
;
961 if (ft
->signal
.channels
!= SOX_ENCODING_UNKNOWN
) {
964 fail
= (p
->twolame_set_num_channels(p
->opt
,(int)ft
->signal
.channels
) != 0);
968 fail
= (p
->lame_set_num_channels(p
->gfp
,(int)ft
->signal
.channels
) < 0);
972 lsx_fail_errno(ft
,SOX_EOF
,"Unsupported number of channels");
979 ft
->signal
.channels
= p
->twolame_get_num_channels(p
->opt
); /* Twolame default */
983 ft
->signal
.channels
= p
->lame_get_num_channels(p
->gfp
); /* LAME default */
990 p
->twolame_set_in_samplerate(p
->opt
,(int)ft
->signal
.rate
);
991 p
->twolame_set_out_samplerate(p
->opt
,(int)ft
->signal
.rate
);
995 p
->lame_set_in_samplerate(p
->gfp
,(int)ft
->signal
.rate
);
996 p
->lame_set_out_samplerate(p
->gfp
,(int)ft
->signal
.rate
);
1002 if (!LSX_DLFUNC_IS_STUB(p
, id3tag_init
))
1007 /* The primary parameter to the LAME encoder is the bit rate. If the
1008 * value of encoding.compression is a positive integer, it's taken as
1009 * the bitrate in kbps (that is if you specify 128, it use 128 kbps).
1011 * The second most important parameter is probably "quality" (really
1012 * performance), which allows balancing encoding speed vs. quality.
1013 * In LAME, 0 specifies highest quality but is very slow, while
1014 * 9 selects poor quality, but is fast. (5 is the default and 2 is
1015 * recommended as a good trade-off for high quality encodes.)
1017 * Because encoding.compression is a float, the fractional part is used
1018 * to select quality. 128.2 selects 128 kbps encoding with a quality
1019 * of 2. There is one problem with this approach. We need 128 to specify
1020 * 128 kbps encoding with default quality, so .0 means use default. Instead
1021 * of .0 you have to use .01 to specify the highest quality (128.01).
1023 * LAME uses bitrate to specify a constant bitrate, but higher quality
1024 * can be achieved using Variable Bit Rate (VBR). VBR quality (really
1025 * size) is selected using a number from 0 to 9. Use a value of 0 for high
1026 * quality, larger files, and 9 for smaller files of lower quality. 4 is
1029 * In order to squeeze the selection of VBR into the encoding.compression
1030 * float we use negative numbers to select VRR. -4.2 would select default
1031 * VBR encoding (size) with high quality (speed). One special case is 0,
1032 * which is a valid VBR encoding parameter but not a valid bitrate.
1033 * Compression value of 0 is always treated as a high quality vbr, as a
1034 * result both -0.2 and 0.2 are treated as highest quality VBR (size) and
1035 * high quality (speed).
1037 * Note: It would have been nice to simply use low values, 0-9, to trigger
1038 * VBR mode, but 8 kbps is a valid bit rate, so negative values were
1042 lsx_debug("-C option is %f", ft
->encoding
.compression
);
1044 if (ft
->encoding
.compression
== HUGE_VAL
) {
1045 /* Do nothing, use defaults: */
1046 lsx_report("using %s encoding defaults", p
->mp2
? "MP2" : "MP3");
1048 double abs_compression
= fabs(ft
->encoding
.compression
);
1049 double floor_compression
= floor(abs_compression
);
1050 double fraction_compression
= abs_compression
- floor_compression
;
1051 int bitrate_q
= (int)floor_compression
;
1053 fraction_compression
== 0.0
1055 : (int)(fraction_compression
* 10.0 + 0.5);
1057 if (ft
->encoding
.compression
< 0.5) {
1059 lsx_fail_errno(ft
,SOX_EOF
,"Variable bitrate encoding not supported for MP2 audio");
1063 if (p
->lame_get_VBR(p
->gfp
) == vbr_off
)
1064 p
->lame_set_VBR(p
->gfp
, vbr_default
);
1069 lsx_warn("unable to write VBR tag because we can't seek");
1072 if (p
->lame_set_VBR_q(p
->gfp
, bitrate_q
) < 0)
1074 lsx_fail_errno(ft
, SOX_EOF
,
1075 "lame_set_VBR_q(%d) failed (should be between 0 and 9)",
1079 lsx_report("lame_set_VBR_q(%d)", bitrate_q
);
1084 fail
= (p
->twolame_set_brate(p
->opt
, bitrate_q
) != 0);
1088 fail
= (p
->lame_set_brate(p
->gfp
, bitrate_q
) < 0);
1092 lsx_fail_errno(ft
, SOX_EOF
,
1093 "%slame_set_brate(%d) failed", p
->mp2
? "two" : "", bitrate_q
);
1096 lsx_report("(two)lame_set_brate(%d)", bitrate_q
);
1101 if (encoder_q
< 0 || p
->mp2
) {
1102 /* use default quality value */
1103 lsx_report("using %s default quality", p
->mp2
? "MP2" : "MP3");
1106 if (p
->lame_set_quality(p
->gfp
, encoder_q
) < 0) {
1107 lsx_fail_errno(ft
, SOX_EOF
,
1108 "lame_set_quality(%d) failed", encoder_q
);
1111 lsx_report("lame_set_quality(%d)", encoder_q
);
1118 p
->lame_set_bWriteVbrTag(p
->gfp
, p
->vbr_tag
);
1124 fail
= (p
->twolame_init_params(p
->opt
) != 0);
1128 fail
= (p
->lame_init_params(p
->gfp
) < 0);
1132 lsx_fail_errno(ft
,SOX_EOF
,"%s initialization failed", p
->mp2
? "Twolame" : "LAME");
1136 return(SOX_SUCCESS
);
1139 #define MP3_SAMPLE_TO_FLOAT(d) ((float)(32768*SOX_SAMPLE_TO_FLOAT_32BIT(d,)))
1141 static size_t sox_mp3write(sox_format_t
* ft
, const sox_sample_t
*buf
, size_t samp
)
1143 priv_t
*p
= (priv_t
*)ft
->priv
;
1144 size_t new_buffer_size
;
1145 float *buffer_l
, *buffer_r
= NULL
;
1146 int nsamples
= samp
/ft
->signal
.channels
;
1151 new_buffer_size
= samp
* sizeof(float);
1152 if (p
->pcm_buffer_size
< new_buffer_size
) {
1153 float *new_buffer
= lsx_realloc(p
->pcm_buffer
, new_buffer_size
);
1155 lsx_fail_errno(ft
, SOX_ENOMEM
, "Out of memory");
1158 p
->pcm_buffer_size
= new_buffer_size
;
1159 p
->pcm_buffer
= new_buffer
;
1162 buffer_l
= p
->pcm_buffer
;
1167 for(s
= 0; s
< samp
; s
++)
1168 buffer_l
[s
] = SOX_SAMPLE_TO_FLOAT_32BIT(buf
[s
],);
1172 if (ft
->signal
.channels
== 2)
1174 /* lame doesn't support interleaved samples for floats so we must break
1175 * them out into seperate buffers.
1177 buffer_r
= p
->pcm_buffer
+ nsamples
;
1179 for (i
= 0; i
< nsamples
; i
++)
1181 buffer_l
[i
] = MP3_SAMPLE_TO_FLOAT(buf
[j
++]);
1182 buffer_r
[i
] = MP3_SAMPLE_TO_FLOAT(buf
[j
++]);
1188 for (i
= 0; i
< nsamples
; i
++) {
1189 buffer_l
[i
] = MP3_SAMPLE_TO_FLOAT(buf
[j
++]);
1194 new_buffer_size
= LAME_BUFFER_SIZE(nsamples
);
1195 if (p
->mp3_buffer_size
< new_buffer_size
) {
1196 unsigned char *new_buffer
= lsx_realloc(p
->mp3_buffer
, new_buffer_size
);
1198 lsx_fail_errno(ft
, SOX_ENOMEM
, "Out of memory");
1201 p
->mp3_buffer_size
= new_buffer_size
;
1202 p
->mp3_buffer
= new_buffer
;
1207 written
= p
->twolame_encode_buffer_float32_interleaved(p
->opt
, buffer_l
,
1208 nsamples
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1212 written
= p
->lame_encode_buffer_float(p
->gfp
, buffer_l
, buffer_r
,
1213 nsamples
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1217 lsx_fail_errno(ft
,SOX_EOF
,"Encoding failed");
1221 if (lsx_writebuf(ft
, p
->mp3_buffer
, (size_t)written
) < (size_t)written
)
1223 lsx_fail_errno(ft
,SOX_EOF
,"File write failed");
1230 static int stopwrite(sox_format_t
* ft
)
1232 priv_t
*p
= (priv_t
*) ft
->priv
;
1233 uint64_t num_samples
= ft
->olength
== SOX_IGNORE_LENGTH
? 0 : ft
->olength
/ max(ft
->signal
.channels
, 1);
1238 written
= p
->twolame_encode_flush(p
->opt
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1242 written
= p
->lame_encode_flush(p
->gfp
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1246 lsx_fail_errno(ft
, SOX_EOF
, "Encoding failed");
1247 else if (lsx_writebuf(ft
, p
->mp3_buffer
, (size_t)written
) < (size_t)written
)
1248 lsx_fail_errno(ft
, SOX_EOF
, "File write failed");
1251 if (ft
->seekable
&& (num_samples
!= p
->num_samples
|| p
->vbr_tag
))
1252 rewrite_tags(ft
, num_samples
);
1256 free(p
->mp3_buffer
);
1257 free(p
->pcm_buffer
);
1261 p
->twolame_close(&p
->opt
);
1262 LSX_DLLIBRARY_CLOSE(p
, twolame_dl
);
1266 p
->lame_close(p
->gfp
);
1267 LSX_DLLIBRARY_CLOSE(p
, lame_dl
);
1273 #else /* !(HAVE_LAME || HAVE_TWOLAME) */
1274 static int startwrite(sox_format_t
* ft UNUSED
)
1276 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled with neither MP2 nor MP3 encoding support");
1279 #define sox_mp3write NULL
1280 #define stopwrite NULL
1281 #endif /* HAVE_LAME || HAVE_TWOLAME */
1283 LSX_FORMAT_HANDLER(mp3
)
1285 static char const * const names
[] = {"mp3", "mp2", "audio/mpeg", NULL
};
1286 static unsigned const write_encodings
[] = {
1287 SOX_ENCODING_MP3
, 0, 0};
1288 static sox_rate_t
const write_rates
[] = {
1289 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 0};
1290 static sox_format_handler_t
const handler
= {SOX_LIB_VERSION_CODE
,
1291 "MPEG Layer 2/3 lossy audio compression", names
, 0,
1292 startread
, sox_mp3read
, stopread
,
1293 startwrite
, sox_mp3write
, stopwrite
,
1294 sox_mp3seek
, write_encodings
, write_rates
, sizeof(priv_t
)
1298 #endif /* defined(HAVE_MAD_H) || defined(HAVE_LAME) || defined(HAVE_TWOLAME) */