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
49 #if defined(HAVE_UNISTD_H)
51 #elif defined(HAVE_IO_H)
55 #define ID3_TAG_FLAG_FOOTERPRESENT 0x10
62 /* Under Windows, importing data from DLLs is a dicey proposition. This is true
63 * when using dlopen, but also true if linking directly against the DLL if the
64 * header does not mark the data as __declspec(dllexport), which mad.h does not.
65 * Sidestep the issue by defining our own mad_timer_zero. This is needed because
66 * mad_timer_zero is used in some of the mad.h macros.
69 #define mad_timer_zero mad_timer_zero_stub
70 static mad_timer_t
const mad_timer_zero_stub
= {0, 0};
73 #define MAXFRAMESIZE 2880
74 #define ID3PADDING 128
76 /* LAME takes float values as input. */
77 #define MP3_LAME_PRECISION 24
79 /* MAD returns values with MAD_F_FRACBITS (28) bits of precision, though it's
80 not certain that all of them are meaningful. Default to 16 bits to
81 align with most users expectation of output file should be 16 bits. */
82 #define MP3_MAD_PRECISION 16
84 static const char* const mad_library_names
[] =
95 #define MAD_FUNC LSX_DLENTRY_DYNAMIC
97 #define MAD_FUNC LSX_DLENTRY_STATIC
100 #define MAD_FUNC_ENTRIES(f,x) \
101 MAD_FUNC(f,x, void, mad_stream_buffer, (struct mad_stream *, unsigned char const *, unsigned long)) \
102 MAD_FUNC(f,x, void, mad_stream_skip, (struct mad_stream *, unsigned long)) \
103 MAD_FUNC(f,x, int, mad_stream_sync, (struct mad_stream *)) \
104 MAD_FUNC(f,x, void, mad_stream_init, (struct mad_stream *)) \
105 MAD_FUNC(f,x, void, mad_frame_init, (struct mad_frame *)) \
106 MAD_FUNC(f,x, void, mad_synth_init, (struct mad_synth *)) \
107 MAD_FUNC(f,x, int, mad_frame_decode, (struct mad_frame *, struct mad_stream *)) \
108 MAD_FUNC(f,x, void, mad_timer_add, (mad_timer_t *, mad_timer_t)) \
109 MAD_FUNC(f,x, void, mad_synth_frame, (struct mad_synth *, struct mad_frame const *)) \
110 MAD_FUNC(f,x, char const *, mad_stream_errorstr, (struct mad_stream const *)) \
111 MAD_FUNC(f,x, void, mad_frame_finish, (struct mad_frame *)) \
112 MAD_FUNC(f,x, void, mad_stream_finish, (struct mad_stream *)) \
113 MAD_FUNC(f,x, unsigned long, mad_bit_read, (struct mad_bitptr *, unsigned int)) \
114 MAD_FUNC(f,x, int, mad_header_decode, (struct mad_header *, struct mad_stream *)) \
115 MAD_FUNC(f,x, void, mad_header_init, (struct mad_header *)) \
116 MAD_FUNC(f,x, signed long, mad_timer_count, (mad_timer_t, enum mad_units)) \
117 MAD_FUNC(f,x, void, mad_timer_multiply, (mad_timer_t *, signed long))
119 static const char* const lame_library_names
[] =
131 #define LAME_FUNC LSX_DLENTRY_DYNAMIC
133 #define LAME_FUNC LSX_DLENTRY_STATIC
136 #define LAME_FUNC_ENTRIES(f,x) \
137 LAME_FUNC(f,x, lame_global_flags*, lame_init, (void)) \
138 LAME_FUNC(f,x, int, lame_set_errorf, (lame_global_flags *, void (*)(const char *, va_list))) \
139 LAME_FUNC(f,x, int, lame_set_debugf, (lame_global_flags *, void (*)(const char *, va_list))) \
140 LAME_FUNC(f,x, int, lame_set_msgf, (lame_global_flags *, void (*)(const char *, va_list))) \
141 LAME_FUNC(f,x, int, lame_set_num_samples, (lame_global_flags *, unsigned long)) \
142 LAME_FUNC(f,x, int, lame_get_num_channels, (const lame_global_flags *)) \
143 LAME_FUNC(f,x, int, lame_set_num_channels, (lame_global_flags *, int)) \
144 LAME_FUNC(f,x, int, lame_set_in_samplerate, (lame_global_flags *, int)) \
145 LAME_FUNC(f,x, int, lame_set_out_samplerate, (lame_global_flags *, int)) \
146 LAME_FUNC(f,x, int, lame_set_bWriteVbrTag, (lame_global_flags *, int)) \
147 LAME_FUNC(f,x, int, lame_set_brate, (lame_global_flags *, int)) \
148 LAME_FUNC(f,x, int, lame_set_quality, (lame_global_flags *, int)) \
149 LAME_FUNC(f,x, vbr_mode, lame_get_VBR, (const lame_global_flags *)) \
150 LAME_FUNC(f,x, int, lame_set_VBR, (lame_global_flags *, vbr_mode)) \
151 LAME_FUNC(f,x, int, lame_set_VBR_q, (lame_global_flags *, int)) \
152 LAME_FUNC(f,x, int, lame_init_params, (lame_global_flags *)) \
153 LAME_FUNC(f,x, int, lame_encode_buffer_float, (lame_global_flags *, const float[], const float[], const int, unsigned char *, const int)) \
154 LAME_FUNC(f,x, int, lame_encode_flush, (lame_global_flags *, unsigned char *, int)) \
155 LAME_FUNC(f,x, int, lame_close, (lame_global_flags *)) \
156 LAME_FUNC(f,x, size_t, lame_get_lametag_frame, (const lame_global_flags *, unsigned char*, size_t)) \
157 LAME_FUNC(f,x, void, id3tag_init, (lame_global_flags *)) \
158 LAME_FUNC(f,x, void, id3tag_set_title, (lame_global_flags *, const char* title)) \
159 LAME_FUNC(f,x, void, id3tag_set_artist, (lame_global_flags *, const char* artist)) \
160 LAME_FUNC(f,x, void, id3tag_set_album, (lame_global_flags *, const char* album)) \
161 LAME_FUNC(f,x, void, id3tag_set_year, (lame_global_flags *, const char* year)) \
162 LAME_FUNC(f,x, void, id3tag_set_comment, (lame_global_flags *, const char* comment)) \
163 LAME_FUNC(f,x, int, id3tag_set_track, (lame_global_flags *, const char* track)) \
164 LAME_FUNC(f,x, int, id3tag_set_genre, (lame_global_flags *, const char* genre)) \
165 LAME_FUNC(f,x, size_t, id3tag_set_pad, (lame_global_flags *, size_t)) \
166 LAME_FUNC(f,x, size_t, lame_get_id3v2_tag, (lame_global_flags *, unsigned char*, size_t)) \
167 LAME_FUNC(f,x, int, id3tag_set_fieldvalue, (lame_global_flags *, const char *))
170 static const char* const twolame_library_names
[] =
181 #define TWOLAME_FUNC LSX_DLENTRY_DYNAMIC
183 #define TWOLAME_FUNC LSX_DLENTRY_STATIC
186 #define TWOLAME_FUNC_ENTRIES(f,x) \
187 TWOLAME_FUNC(f,x, twolame_options*, twolame_init, (void)) \
188 TWOLAME_FUNC(f,x, int, twolame_get_num_channels, (twolame_options*)) \
189 TWOLAME_FUNC(f,x, int, twolame_set_num_channels, (twolame_options*, int)) \
190 TWOLAME_FUNC(f,x, int, twolame_set_in_samplerate, (twolame_options *, int)) \
191 TWOLAME_FUNC(f,x, int, twolame_set_out_samplerate, (twolame_options *, int)) \
192 TWOLAME_FUNC(f,x, int, twolame_set_brate, (twolame_options *, int)) \
193 TWOLAME_FUNC(f,x, int, twolame_init_params, (twolame_options *)) \
194 TWOLAME_FUNC(f,x, int, twolame_encode_buffer_float32_interleaved, (twolame_options *, const float [], int, unsigned char *, int)) \
195 TWOLAME_FUNC(f,x, int, twolame_encode_flush, (twolame_options *, unsigned char *, int)) \
196 TWOLAME_FUNC(f,x, void, twolame_close, (twolame_options **))
199 typedef struct mp3_priv_t
{
200 unsigned char *mp3_buffer
;
201 size_t mp3_buffer_size
;
204 struct mad_stream Stream
;
205 struct mad_frame Frame
;
206 struct mad_synth Synth
;
210 LSX_DLENTRIES_TO_PTRS(MAD_FUNC_ENTRIES
, mad_dl
);
211 #endif /*HAVE_MAD_H*/
213 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
215 size_t pcm_buffer_size
;
220 lame_global_flags
*gfp
;
221 uint64_t num_samples
;
223 LSX_DLENTRIES_TO_PTRS(LAME_FUNC_ENTRIES
, lame_dl
);
227 twolame_options
*opt
;
228 LSX_DLENTRIES_TO_PTRS(TWOLAME_FUNC_ENTRIES
, twolame_dl
);
234 /* This function merges the functions tagtype() and id3_tag_query()
235 from MAD's libid3tag, so we don't have to link to it
236 Returns 0 if the frame is not an ID3 tag, tag length if it is */
238 static int tagtype(const unsigned char *data
, size_t length
)
240 if (length
>= 3 && data
[0] == 'T' && data
[1] == 'A' && data
[2] == 'G')
242 return 128; /* ID3V1 */
246 (data
[0] == 'I' && data
[1] == 'D' && data
[2] == '3') &&
247 data
[3] < 0xff && data
[4] < 0xff &&
248 data
[6] < 0x80 && data
[7] < 0x80 && data
[8] < 0x80 && data
[9] < 0x80)
253 size
= 10 + (data
[6]<<21) + (data
[7]<<14) + (data
[8]<<7) + data
[9];
254 if (flags
& ID3_TAG_FLAG_FOOTERPRESENT
)
256 for (; size
< length
&& !data
[size
]; ++size
); /* Consume padding */
263 #endif /*HAVE_MAD_H*/
265 #include "mp3-util.h"
270 * (Re)fill the stream buffer that is to be decoded. If any data
271 * still exists in the buffer then they are first shifted to be
272 * front of the stream buffer.
274 static int sox_mp3_input(sox_format_t
* ft
)
276 priv_t
*p
= (priv_t
*) ft
->priv
;
280 remaining
= p
->Stream
.bufend
- p
->Stream
.next_frame
;
282 /* libmad does not consume all the buffer it's given. Some
283 * data, part of a truncated frame, is left unused at the
284 * end of the buffer. That data must be put back at the
285 * beginning of the buffer and taken in account for
286 * refilling the buffer. This means that the input buffer
287 * must be large enough to hold a complete frame at the
288 * highest observable bit-rate (currently 448 kb/s).
289 * TODO: Is 2016 bytes the size of the largest frame?
290 * (448000*(1152/32000))/8
292 memmove(p
->mp3_buffer
, p
->Stream
.next_frame
, remaining
);
294 bytes_read
= lsx_readbuf(ft
, p
->mp3_buffer
+remaining
,
295 p
->mp3_buffer_size
-remaining
);
301 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
, bytes_read
+remaining
);
307 /* Attempts to read an ID3 tag at the current location in stream and
308 * consume it all. Returns SOX_EOF if no tag is found. Its up to
311 static int sox_mp3_inputtag(sox_format_t
* ft
)
313 priv_t
*p
= (priv_t
*) ft
->priv
;
319 /* FIXME: This needs some more work if we are to ever
320 * look at the ID3 frame. This is because the Stream
321 * may not be able to hold the complete ID3 frame.
322 * We should consume the whole frame inside tagtype()
323 * instead of outside of tagframe(). That would support
324 * recovering when Stream contains less then 8-bytes (header)
325 * and also when ID3v2 is bigger then Stream buffer size.
326 * Need to pass in stream so that buffer can be
327 * consumed as well as letting additional data to be
330 remaining
= p
->Stream
.bufend
- p
->Stream
.next_frame
;
331 if ((tagsize
= tagtype(p
->Stream
.this_frame
, remaining
)))
333 p
->mad_stream_skip(&p
->Stream
, tagsize
);
337 /* We know that a valid frame hasn't been found yet
338 * so help libmad out and go back into frame seek mode.
339 * This is true whether an ID3 tag was found or not.
341 p
->mad_stream_sync(&p
->Stream
);
346 static sox_bool
sox_mp3_vbrtag(sox_format_t
*ft
)
348 priv_t
*p
= ft
->priv
;
349 struct mad_bitptr
*anc
= &p
->Stream
.anc_ptr
;
351 if (p
->Frame
.header
.layer
!= MAD_LAYER_III
)
354 if (p
->Stream
.anc_bitlen
< 32)
357 if (!memcmp(anc
->byte
, "Xing", 4) ||
358 !memcmp(anc
->byte
, "Info", 4))
364 static int startread(sox_format_t
* ft
)
366 priv_t
*p
= (priv_t
*) ft
->priv
;
368 sox_bool ignore_length
= ft
->signal
.length
== SOX_IGNORE_LENGTH
;
369 int open_library_result
;
375 "MAD decoder library",
377 open_library_result
);
378 if (open_library_result
)
381 p
->mp3_buffer_size
= sox_globals
.bufsiz
;
382 p
->mp3_buffer
= lsx_malloc(p
->mp3_buffer_size
);
384 ft
->signal
.length
= SOX_UNSPEC
;
387 lsx_id3_read_tag(ft
, sox_true
);
389 if (!ft
->signal
.length
)
392 ft
->signal
.length
= mp3_duration(ft
);
395 p
->mad_stream_init(&p
->Stream
);
396 p
->mad_frame_init(&p
->Frame
);
397 p
->mad_synth_init(&p
->Synth
);
398 mad_timer_reset(&p
->Timer
);
400 ft
->encoding
.encoding
= SOX_ENCODING_MP3
;
402 /* Decode at least one valid frame to find out the input
403 * format. The decoded frame will be saved off so that it
404 * can be processed later.
406 ReadSize
= lsx_readbuf(ft
, p
->mp3_buffer
, p
->mp3_buffer_size
);
407 if (ReadSize
!= p
->mp3_buffer_size
&& lsx_error(ft
))
410 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
, ReadSize
);
412 /* Find a valid frame before starting up. This makes sure
413 * that we have a valid MP3 and also skips past ID3v2 tags
414 * at the beginning of the audio file.
417 while (p
->mad_frame_decode(&p
->Frame
,&p
->Stream
))
419 /* check whether input buffer needs a refill */
420 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
422 if (sox_mp3_input(ft
) == SOX_EOF
)
428 /* Consume any ID3 tags */
429 sox_mp3_inputtag(ft
);
431 /* FIXME: We should probably detect when we've read
432 * a bunch of non-ID3 data and still haven't found a
433 * frame. In that case we can abort early without
434 * scanning the whole file.
441 lsx_fail_errno(ft
,SOX_EOF
,"No valid MP3 frame found");
445 switch(p
->Frame
.header
.mode
)
447 case MAD_MODE_SINGLE_CHANNEL
:
448 case MAD_MODE_DUAL_CHANNEL
:
449 case MAD_MODE_JOINT_STEREO
:
450 case MAD_MODE_STEREO
:
451 ft
->signal
.channels
= MAD_NCHANNELS(&p
->Frame
.header
);
454 lsx_fail_errno(ft
, SOX_EFMT
, "Cannot determine number of channels");
458 ft
->signal
.precision
= MP3_MAD_PRECISION
;
459 ft
->signal
.rate
=p
->Frame
.header
.samplerate
;
461 ft
->signal
.length
= SOX_UNSPEC
;
463 ft
->signal
.length
*= ft
->signal
.channels
; /* Keep separate from line above! */
466 if (!sox_mp3_vbrtag(ft
))
467 p
->Stream
.next_frame
= p
->Stream
.this_frame
;
469 p
->mad_frame_init(&p
->Frame
);
478 * Read up to len samples from p->Synth
479 * If needed, read some more MP3 data, decode them and synth them
481 * Return number of samples read.
483 static size_t sox_mp3read(sox_format_t
* ft
, sox_sample_t
*buf
, size_t len
)
485 priv_t
*p
= (priv_t
*) ft
->priv
;
486 size_t donow
,i
,done
=0;
491 size_t x
= (p
->Synth
.pcm
.length
- p
->cursamp
)*ft
->signal
.channels
;
495 for(chan
=0;chan
<ft
->signal
.channels
;chan
++){
496 sample
=p
->Synth
.pcm
.samples
[chan
][p
->cursamp
];
497 if (sample
< -MAD_F_ONE
)
499 else if (sample
>= MAD_F_ONE
)
501 *buf
++=(sox_sample_t
)(sample
<<(32-1-MAD_F_FRACBITS
));
512 /* check whether input buffer needs a refill */
513 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
515 if (sox_mp3_input(ft
) == SOX_EOF
) {
516 lsx_debug("sox_mp3_input EOF");
521 if (p
->mad_frame_decode(&p
->Frame
,&p
->Stream
))
523 if(MAD_RECOVERABLE(p
->Stream
.error
))
525 sox_mp3_inputtag(ft
);
530 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
534 lsx_report("unrecoverable frame level error (%s).",
535 p
->mad_stream_errorstr(&p
->Stream
));
541 p
->mad_timer_add(&p
->Timer
,p
->Frame
.header
.duration
);
542 p
->mad_synth_frame(&p
->Synth
,&p
->Frame
);
549 static int stopread(sox_format_t
* ft
)
551 priv_t
*p
=(priv_t
*) ft
->priv
;
553 mad_synth_finish(&p
->Synth
);
554 p
->mad_frame_finish(&p
->Frame
);
555 p
->mad_stream_finish(&p
->Stream
);
558 LSX_DLLIBRARY_CLOSE(p
, mad_dl
);
562 static int sox_mp3seek(sox_format_t
* ft
, uint64_t offset
)
564 priv_t
* p
= (priv_t
*) ft
->priv
;
565 size_t initial_bitrate
= p
->Frame
.header
.bitrate
;
566 size_t tagsize
= 0, consumed
= 0;
567 sox_bool vbr
= sox_false
; /* Variable Bit Rate */
568 sox_bool depadded
= sox_false
;
569 uint64_t to_skip_samples
= 0;
573 mad_timer_reset(&p
->Timer
);
576 /* They where opened in startread */
577 mad_synth_finish(&p
->Synth
);
578 p
->mad_frame_finish(&p
->Frame
);
579 p
->mad_stream_finish(&p
->Stream
);
581 p
->mad_stream_init(&p
->Stream
);
582 p
->mad_frame_init(&p
->Frame
);
583 p
->mad_synth_init(&p
->Synth
);
585 offset
/= ft
->signal
.channels
;
586 to_skip_samples
= offset
;
588 while(sox_true
) { /* Read data from the MP3 file */
591 size_t leftover
= p
->Stream
.bufend
- p
->Stream
.next_frame
;
593 memmove(p
->mp3_buffer
, p
->Stream
.this_frame
, leftover
);
594 read
= lsx_readbuf(ft
, p
->mp3_buffer
+ leftover
, p
->mp3_buffer_size
- leftover
);
596 lsx_debug("seek failure. unexpected EOF (frames=%" PRIuPTR
" leftover=%" PRIuPTR
")", p
->FrameCount
, leftover
);
599 for (; !depadded
&& padding
< read
&& !p
->mp3_buffer
[padding
]; ++padding
);
601 p
->mad_stream_buffer(&p
->Stream
, p
->mp3_buffer
+ padding
, leftover
+ read
- padding
);
603 while (sox_true
) { /* Decode frame headers */
604 static unsigned short samples
;
605 p
->Stream
.error
= MAD_ERROR_NONE
;
607 /* Not an audio frame */
608 if (p
->mad_header_decode(&p
->Frame
.header
, &p
->Stream
) == -1) {
609 if (p
->Stream
.error
== MAD_ERROR_BUFLEN
)
610 break; /* Normal behaviour; get some more data from the file */
611 if (!MAD_RECOVERABLE(p
->Stream
.error
)) {
612 lsx_warn("unrecoverable MAD error");
615 if (p
->Stream
.error
== MAD_ERROR_LOSTSYNC
) {
616 unsigned available
= (p
->Stream
.bufend
- p
->Stream
.this_frame
);
617 tagsize
= tagtype(p
->Stream
.this_frame
, (size_t) available
);
618 if (tagsize
) { /* It's some ID3 tags, so just skip */
619 if (tagsize
>= available
) {
620 lsx_seeki(ft
, (off_t
)(tagsize
- available
), SEEK_CUR
);
621 depadded
= sox_false
;
623 p
->mad_stream_skip(&p
->Stream
, min(tagsize
, available
));
625 else lsx_warn("MAD lost sync");
627 else lsx_warn("recoverable MAD error");
631 consumed
+= p
->Stream
.next_frame
- p
->Stream
.this_frame
;
632 vbr
|= (p
->Frame
.header
.bitrate
!= initial_bitrate
);
634 samples
= 32 * MAD_NSBSAMPLES(&p
->Frame
.header
);
637 p
->mad_timer_add(&p
->Timer
, p
->Frame
.header
.duration
);
639 if(to_skip_samples
<= samples
)
641 p
->mad_frame_decode(&p
->Frame
,&p
->Stream
);
642 p
->mad_synth_frame(&p
->Synth
, &p
->Frame
);
643 p
->cursamp
= to_skip_samples
;
646 else to_skip_samples
-= samples
;
648 /* If not VBR, we can extrapolate frame size */
649 if (p
->FrameCount
== 64 && !vbr
) {
650 p
->FrameCount
= offset
/ samples
;
651 to_skip_samples
= offset
% samples
;
653 if (SOX_SUCCESS
!= lsx_seeki(ft
, (off_t
)(p
->FrameCount
* consumed
/ 64 + tagsize
), SEEK_SET
))
656 /* Reset Stream for refilling buffer */
657 p
->mad_stream_finish(&p
->Stream
);
658 p
->mad_stream_init(&p
->Stream
);
666 #else /* !HAVE_MAD_H */
667 static int startread(sox_format_t
* ft
)
669 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP3 decoding support");
672 #define sox_mp3read NULL
673 #define stopread NULL
674 #define sox_mp3seek NULL
675 #endif /*HAVE_MAD_H*/
679 /* Adapters for lame message callbacks: */
681 static void errorf(const char* fmt
, va_list va
)
683 sox_globals
.subsystem
=__FILE__
;
684 if (sox_globals
.output_message_handler
)
685 (*sox_globals
.output_message_handler
)(1,sox_globals
.subsystem
,fmt
,va
);
689 static void debugf(const char* fmt
, va_list va
)
691 sox_globals
.subsystem
=__FILE__
;
692 if (sox_globals
.output_message_handler
)
693 (*sox_globals
.output_message_handler
)(4,sox_globals
.subsystem
,fmt
,va
);
697 static void msgf(const char* fmt
, va_list va
)
699 sox_globals
.subsystem
=__FILE__
;
700 if (sox_globals
.output_message_handler
)
701 (*sox_globals
.output_message_handler
)(3,sox_globals
.subsystem
,fmt
,va
);
705 /* These functions are considered optional. If they aren't present in the
706 library, the stub versions defined here will be used instead. */
708 UNUSED
static void id3tag_init_stub(lame_global_flags
* gfp UNUSED
)
710 UNUSED
static void id3tag_set_title_stub(lame_global_flags
* gfp UNUSED
, const char* title UNUSED
)
712 UNUSED
static void id3tag_set_artist_stub(lame_global_flags
* gfp UNUSED
, const char* artist UNUSED
)
714 UNUSED
static void id3tag_set_album_stub(lame_global_flags
* gfp UNUSED
, const char* album UNUSED
)
716 UNUSED
static void id3tag_set_year_stub(lame_global_flags
* gfp UNUSED
, const char* year UNUSED
)
718 UNUSED
static void id3tag_set_comment_stub(lame_global_flags
* gfp UNUSED
, const char* comment UNUSED
)
720 UNUSED
static void id3tag_set_track_stub(lame_global_flags
* gfp UNUSED
, const char* track UNUSED
)
722 UNUSED
static int id3tag_set_genre_stub(lame_global_flags
* gfp UNUSED
, const char* genre UNUSED
)
724 UNUSED
static size_t id3tag_set_pad_stub(lame_global_flags
* gfp UNUSED
, size_t n UNUSED
)
726 UNUSED
static size_t lame_get_id3v2_tag_stub(lame_global_flags
* gfp UNUSED
, unsigned char * buffer UNUSED
, size_t size UNUSED
)
728 UNUSED
static int id3tag_set_fieldvalue_stub(lame_global_flags
* gfp UNUSED
, const char *fieldvalue UNUSED
)
731 static int get_id3v2_tag_size(sox_format_t
* ft
)
735 unsigned char id3v2_header
[10];
737 if (lsx_seeki(ft
, (off_t
)0, SEEK_SET
) != 0) {
738 lsx_warn("cannot update id3 tag - failed to seek to beginning");
742 /* read 10 bytes in case there's an ID3 version 2 header here */
743 bytes_read
= lsx_readbuf(ft
, id3v2_header
, sizeof(id3v2_header
));
744 if (bytes_read
!= sizeof(id3v2_header
)) {
745 lsx_warn("cannot update id3 tag - failed to read id3 header");
746 return SOX_EOF
; /* not readable, maybe opened Write-Only */
749 /* does the stream begin with the ID3 version 2 file identifier? */
750 if (!strncmp((char *) id3v2_header
, "ID3", (size_t)3)) {
751 /* the tag size (minus the 10-byte header) is encoded into four
752 * bytes where the most significant bit is clear in each byte */
753 id3v2_size
= (((id3v2_header
[6] & 0x7f) << 21)
754 | ((id3v2_header
[7] & 0x7f) << 14)
755 | ((id3v2_header
[8] & 0x7f) << 7)
756 | (id3v2_header
[9] & 0x7f))
757 + sizeof(id3v2_header
);
759 /* no ID3 version 2 tag in this stream */
765 static void rewrite_id3v2_tag(sox_format_t
* ft
, size_t id3v2_size
, uint64_t num_samples
)
767 priv_t
*p
= (priv_t
*)ft
->priv
;
769 unsigned char * buffer
;
771 if (LSX_DLFUNC_IS_STUB(p
, lame_get_id3v2_tag
))
774 lsx_warn("cannot update track length info - tag update not supported with this version of LAME. Track length will be incorrect.");
776 lsx_report("cannot update track length info - tag update not supported with this version of LAME. Track length will be unspecified.");
780 buffer
= lsx_malloc(id3v2_size
);
783 lsx_warn("cannot update track length info - failed to allocate buffer");
787 if (num_samples
> ULONG_MAX
)
789 lsx_warn("cannot accurately update track length info - file is too long");
792 p
->lame_set_num_samples(p
->gfp
, (unsigned long)num_samples
);
793 lsx_debug("updated MP3 TLEN to %lu samples", (unsigned long)num_samples
);
795 new_size
= p
->lame_get_id3v2_tag(p
->gfp
, buffer
, id3v2_size
);
797 if (new_size
!= id3v2_size
&& new_size
-ID3PADDING
<= id3v2_size
) {
798 p
->id3tag_set_pad(p
->gfp
, ID3PADDING
+ id3v2_size
- new_size
);
799 new_size
= p
->lame_get_id3v2_tag(p
->gfp
, buffer
, id3v2_size
);
802 if (new_size
!= id3v2_size
) {
803 if (LSX_DLFUNC_IS_STUB(p
, id3tag_set_pad
))
806 lsx_warn("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be invalid.");
808 lsx_report("cannot update track length info - tag size adjustment not supported with this version of LAME. Track length will be unspecified.");
811 lsx_warn("cannot update track length info - failed to adjust tag size");
813 lsx_seeki(ft
, (off_t
)0, SEEK_SET
);
814 /* Overwrite the Id3v2 tag (this time TLEN should be accurate) */
815 if (lsx_writebuf(ft
, buffer
, id3v2_size
) != 1) {
816 lsx_debug("Rewrote Id3v2 tag (%" PRIuPTR
" bytes)", id3v2_size
);
823 static void rewrite_tags(sox_format_t
* ft
, uint64_t num_samples
)
825 priv_t
*p
= (priv_t
*)ft
->priv
;
830 if (lsx_seeki(ft
, (off_t
)0, SEEK_END
)) {
831 lsx_warn("cannot update tags - seek to end failed");
836 file_size
= lsx_tell(ft
);
838 if (file_size
== 0) {
839 lsx_warn("cannot update tags - file size is 0");
843 id3v2_size
= get_id3v2_tag_size(ft
);
844 if (id3v2_size
> 0 && num_samples
!= p
->num_samples
) {
845 rewrite_id3v2_tag(ft
, id3v2_size
, num_samples
);
850 uint8_t buffer
[MAXFRAMESIZE
];
852 if (lsx_seeki(ft
, (off_t
)id3v2_size
, SEEK_SET
)) {
853 lsx_warn("cannot write VBR tag - seek to tag block failed");
857 lametag_size
= p
->lame_get_lametag_frame(p
->gfp
, buffer
, sizeof(buffer
));
858 if (lametag_size
> sizeof(buffer
)) {
859 lsx_warn("cannot write VBR tag - VBR tag too large for buffer");
863 if (lametag_size
< 1) {
867 if (lsx_writebuf(ft
, buffer
, lametag_size
) != lametag_size
) {
868 lsx_warn("cannot write VBR tag - VBR tag write failed");
870 lsx_debug("rewrote VBR tag (%" PRIuPTR
" bytes)", lametag_size
);
875 #endif /* HAVE_LAME */
877 #if defined(HAVE_LAME) || defined(HAVE_TWOLAME)
879 #define LAME_BUFFER_SIZE(num_samples) (((num_samples) + 3) / 4 * 5 + 7200)
881 static int startwrite(sox_format_t
* ft
)
883 priv_t
*p
= (priv_t
*) ft
->priv
;
884 int openlibrary_result
;
887 if (ft
->encoding
.encoding
!= SOX_ENCODING_MP3
) {
888 if(ft
->encoding
.encoding
!= SOX_ENCODING_UNKNOWN
)
889 lsx_report("Encoding forced to MP2/MP3");
890 ft
->encoding
.encoding
= SOX_ENCODING_MP3
;
893 if(strchr(ft
->filetype
, '2'))
901 TWOLAME_FUNC_ENTRIES
,
902 "Twolame encoder library",
903 twolame_library_names
,
906 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP2 encoding support");
915 "LAME encoder library",
919 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled without MP3 encoding support");
923 if (openlibrary_result
)
926 p
->mp3_buffer_size
= LAME_BUFFER_SIZE(sox_globals
.bufsiz
/ max(ft
->signal
.channels
, 1));
927 p
->mp3_buffer
= lsx_malloc(p
->mp3_buffer_size
);
929 p
->pcm_buffer_size
= sox_globals
.bufsiz
* sizeof(float);
930 p
->pcm_buffer
= lsx_malloc(p
->pcm_buffer_size
);
934 p
->opt
= p
->twolame_init();
937 lsx_fail_errno(ft
,SOX_EOF
,"Initialization of Twolame library failed");
943 p
->gfp
= p
->lame_init();
946 lsx_fail_errno(ft
,SOX_EOF
,"Initialization of LAME library failed");
950 /* First set message callbacks so we don't miss any messages: */
951 p
->lame_set_errorf(p
->gfp
,errorf
);
952 p
->lame_set_debugf(p
->gfp
,debugf
);
953 p
->lame_set_msgf (p
->gfp
,msgf
);
955 p
->num_samples
= ft
->signal
.length
== SOX_IGNORE_LENGTH
? 0 : ft
->signal
.length
/ max(ft
->signal
.channels
, 1);
956 p
->lame_set_num_samples(p
->gfp
, p
->num_samples
> ULONG_MAX
? 0 : (unsigned long)p
->num_samples
);
960 ft
->signal
.precision
= MP3_LAME_PRECISION
;
962 if (ft
->signal
.channels
!= SOX_ENCODING_UNKNOWN
) {
965 fail
= (p
->twolame_set_num_channels(p
->opt
,(int)ft
->signal
.channels
) != 0);
969 fail
= (p
->lame_set_num_channels(p
->gfp
,(int)ft
->signal
.channels
) < 0);
973 lsx_fail_errno(ft
,SOX_EOF
,"Unsupported number of channels");
980 ft
->signal
.channels
= p
->twolame_get_num_channels(p
->opt
); /* Twolame default */
984 ft
->signal
.channels
= p
->lame_get_num_channels(p
->gfp
); /* LAME default */
991 p
->twolame_set_in_samplerate(p
->opt
,(int)ft
->signal
.rate
);
992 p
->twolame_set_out_samplerate(p
->opt
,(int)ft
->signal
.rate
);
996 p
->lame_set_in_samplerate(p
->gfp
,(int)ft
->signal
.rate
);
997 p
->lame_set_out_samplerate(p
->gfp
,(int)ft
->signal
.rate
);
1003 if (!LSX_DLFUNC_IS_STUB(p
, id3tag_init
))
1008 /* The primary parameter to the LAME encoder is the bit rate. If the
1009 * value of encoding.compression is a positive integer, it's taken as
1010 * the bitrate in kbps (that is if you specify 128, it use 128 kbps).
1012 * The second most important parameter is probably "quality" (really
1013 * performance), which allows balancing encoding speed vs. quality.
1014 * In LAME, 0 specifies highest quality but is very slow, while
1015 * 9 selects poor quality, but is fast. (5 is the default and 2 is
1016 * recommended as a good trade-off for high quality encodes.)
1018 * Because encoding.compression is a float, the fractional part is used
1019 * to select quality. 128.2 selects 128 kbps encoding with a quality
1020 * of 2. There is one problem with this approach. We need 128 to specify
1021 * 128 kbps encoding with default quality, so .0 means use default. Instead
1022 * of .0 you have to use .01 to specify the highest quality (128.01).
1024 * LAME uses bitrate to specify a constant bitrate, but higher quality
1025 * can be achieved using Variable Bit Rate (VBR). VBR quality (really
1026 * size) is selected using a number from 0 to 9. Use a value of 0 for high
1027 * quality, larger files, and 9 for smaller files of lower quality. 4 is
1030 * In order to squeeze the selection of VBR into the encoding.compression
1031 * float we use negative numbers to select VRR. -4.2 would select default
1032 * VBR encoding (size) with high quality (speed). One special case is 0,
1033 * which is a valid VBR encoding parameter but not a valid bitrate.
1034 * Compression value of 0 is always treated as a high quality vbr, as a
1035 * result both -0.2 and 0.2 are treated as highest quality VBR (size) and
1036 * high quality (speed).
1038 * Note: It would have been nice to simply use low values, 0-9, to trigger
1039 * VBR mode, but 8 kbps is a valid bit rate, so negative values were
1043 lsx_debug("-C option is %f", ft
->encoding
.compression
);
1045 if (ft
->encoding
.compression
== HUGE_VAL
) {
1046 /* Do nothing, use defaults: */
1047 lsx_report("using %s encoding defaults", p
->mp2
? "MP2" : "MP3");
1049 double abs_compression
= fabs(ft
->encoding
.compression
);
1050 double floor_compression
= floor(abs_compression
);
1051 double fraction_compression
= abs_compression
- floor_compression
;
1052 int bitrate_q
= (int)floor_compression
;
1054 fraction_compression
== 0.0
1056 : (int)(fraction_compression
* 10.0 + 0.5);
1058 if (ft
->encoding
.compression
< 0.5) {
1060 lsx_fail_errno(ft
,SOX_EOF
,"Variable bitrate encoding not supported for MP2 audio");
1064 if (p
->lame_get_VBR(p
->gfp
) == vbr_off
)
1065 p
->lame_set_VBR(p
->gfp
, vbr_default
);
1070 lsx_warn("unable to write VBR tag because we can't seek");
1073 if (p
->lame_set_VBR_q(p
->gfp
, bitrate_q
) < 0)
1075 lsx_fail_errno(ft
, SOX_EOF
,
1076 "lame_set_VBR_q(%d) failed (should be between 0 and 9)",
1080 lsx_report("lame_set_VBR_q(%d)", bitrate_q
);
1085 fail
= (p
->twolame_set_brate(p
->opt
, bitrate_q
) != 0);
1089 fail
= (p
->lame_set_brate(p
->gfp
, bitrate_q
) < 0);
1093 lsx_fail_errno(ft
, SOX_EOF
,
1094 "%slame_set_brate(%d) failed", p
->mp2
? "two" : "", bitrate_q
);
1097 lsx_report("(two)lame_set_brate(%d)", bitrate_q
);
1102 if (encoder_q
< 0 || p
->mp2
) {
1103 /* use default quality value */
1104 lsx_report("using %s default quality", p
->mp2
? "MP2" : "MP3");
1107 if (p
->lame_set_quality(p
->gfp
, encoder_q
) < 0) {
1108 lsx_fail_errno(ft
, SOX_EOF
,
1109 "lame_set_quality(%d) failed", encoder_q
);
1112 lsx_report("lame_set_quality(%d)", encoder_q
);
1119 p
->lame_set_bWriteVbrTag(p
->gfp
, p
->vbr_tag
);
1125 fail
= (p
->twolame_init_params(p
->opt
) != 0);
1129 fail
= (p
->lame_init_params(p
->gfp
) < 0);
1133 lsx_fail_errno(ft
,SOX_EOF
,"%s initialization failed", p
->mp2
? "Twolame" : "LAME");
1137 return(SOX_SUCCESS
);
1140 #define MP3_SAMPLE_TO_FLOAT(d) ((float)(32768*SOX_SAMPLE_TO_FLOAT_32BIT(d,)))
1142 static size_t sox_mp3write(sox_format_t
* ft
, const sox_sample_t
*buf
, size_t samp
)
1144 priv_t
*p
= (priv_t
*)ft
->priv
;
1145 size_t new_buffer_size
;
1146 float *buffer_l
, *buffer_r
= NULL
;
1147 int nsamples
= samp
/ft
->signal
.channels
;
1152 new_buffer_size
= samp
* sizeof(float);
1153 if (p
->pcm_buffer_size
< new_buffer_size
) {
1154 float *new_buffer
= lsx_realloc(p
->pcm_buffer
, new_buffer_size
);
1156 lsx_fail_errno(ft
, SOX_ENOMEM
, "Out of memory");
1159 p
->pcm_buffer_size
= new_buffer_size
;
1160 p
->pcm_buffer
= new_buffer
;
1163 buffer_l
= p
->pcm_buffer
;
1168 for(s
= 0; s
< samp
; s
++)
1169 buffer_l
[s
] = SOX_SAMPLE_TO_FLOAT_32BIT(buf
[s
],);
1173 if (ft
->signal
.channels
== 2)
1175 /* lame doesn't support interleaved samples for floats so we must break
1176 * them out into seperate buffers.
1178 buffer_r
= p
->pcm_buffer
+ nsamples
;
1180 for (i
= 0; i
< nsamples
; i
++)
1182 buffer_l
[i
] = MP3_SAMPLE_TO_FLOAT(buf
[j
++]);
1183 buffer_r
[i
] = MP3_SAMPLE_TO_FLOAT(buf
[j
++]);
1189 for (i
= 0; i
< nsamples
; i
++) {
1190 buffer_l
[i
] = MP3_SAMPLE_TO_FLOAT(buf
[j
++]);
1195 new_buffer_size
= LAME_BUFFER_SIZE(nsamples
);
1196 if (p
->mp3_buffer_size
< new_buffer_size
) {
1197 unsigned char *new_buffer
= lsx_realloc(p
->mp3_buffer
, new_buffer_size
);
1199 lsx_fail_errno(ft
, SOX_ENOMEM
, "Out of memory");
1202 p
->mp3_buffer_size
= new_buffer_size
;
1203 p
->mp3_buffer
= new_buffer
;
1208 written
= p
->twolame_encode_buffer_float32_interleaved(p
->opt
, buffer_l
,
1209 nsamples
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1213 written
= p
->lame_encode_buffer_float(p
->gfp
, buffer_l
, buffer_r
,
1214 nsamples
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1218 lsx_fail_errno(ft
,SOX_EOF
,"Encoding failed");
1222 if (lsx_writebuf(ft
, p
->mp3_buffer
, (size_t)written
) < (size_t)written
)
1224 lsx_fail_errno(ft
,SOX_EOF
,"File write failed");
1231 static int stopwrite(sox_format_t
* ft
)
1233 priv_t
*p
= (priv_t
*) ft
->priv
;
1234 uint64_t num_samples
= ft
->olength
== SOX_IGNORE_LENGTH
? 0 : ft
->olength
/ max(ft
->signal
.channels
, 1);
1239 written
= p
->twolame_encode_flush(p
->opt
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1243 written
= p
->lame_encode_flush(p
->gfp
, p
->mp3_buffer
, (int)p
->mp3_buffer_size
);
1247 lsx_fail_errno(ft
, SOX_EOF
, "Encoding failed");
1248 else if (lsx_writebuf(ft
, p
->mp3_buffer
, (size_t)written
) < (size_t)written
)
1249 lsx_fail_errno(ft
, SOX_EOF
, "File write failed");
1252 if (ft
->seekable
&& (num_samples
!= p
->num_samples
|| p
->vbr_tag
))
1253 rewrite_tags(ft
, num_samples
);
1257 free(p
->mp3_buffer
);
1258 free(p
->pcm_buffer
);
1262 p
->twolame_close(&p
->opt
);
1263 LSX_DLLIBRARY_CLOSE(p
, twolame_dl
);
1267 p
->lame_close(p
->gfp
);
1268 LSX_DLLIBRARY_CLOSE(p
, lame_dl
);
1274 #else /* !(HAVE_LAME || HAVE_TWOLAME) */
1275 static int startwrite(sox_format_t
* ft UNUSED
)
1277 lsx_fail_errno(ft
,SOX_EOF
,"SoX was compiled with neither MP2 nor MP3 encoding support");
1280 #define sox_mp3write NULL
1281 #define stopwrite NULL
1282 #endif /* HAVE_LAME || HAVE_TWOLAME */
1284 LSX_FORMAT_HANDLER(mp3
)
1286 static char const * const names
[] = {"mp3", "mp2", "audio/mpeg", NULL
};
1287 static unsigned const write_encodings
[] = {
1288 SOX_ENCODING_MP3
, 0, 0};
1289 static sox_rate_t
const write_rates
[] = {
1290 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 0};
1291 static sox_format_handler_t
const handler
= {SOX_LIB_VERSION_CODE
,
1292 "MPEG Layer 2/3 lossy audio compression", names
, 0,
1293 startread
, sox_mp3read
, stopread
,
1294 startwrite
, sox_mp3write
, stopwrite
,
1295 sox_mp3seek
, write_encodings
, write_rates
, sizeof(priv_t
)
1299 #endif /* defined(HAVE_MAD_H) || defined(HAVE_LAME) || defined(HAVE_TWOLAME) */