]> git.tdb.fi Git - libs/al.git/blob - source/mp3decoder.cpp
Plug a memory leak in Mp3Decoder
[libs/al.git] / source / mp3decoder.cpp
1 #include <limits>
2 #include <mad.h>
3 #include <msp/strings/format.h>
4 #include "mp3decoder.h"
5
6 using namespace std;
7
8 namespace {
9
10 Msp::Int16 scale(mad_fixed_t sample)
11 {
12         if(sample<=-MAD_F_ONE)
13                 return numeric_limits<Msp::Int16>::min();
14         else if(sample>=MAD_F_ONE)
15                 return numeric_limits<Msp::Int16>::max();
16         else
17                 return (sample + (1<<(MAD_F_FRACBITS-16))) >> (MAD_F_FRACBITS-15);
18 }
19
20 } // namespace
21
22
23 namespace Msp {
24 namespace AL {
25
26 mp3_error::mp3_error(const std::string &func, int err):
27         runtime_error(format("%s: %s", func, get_message(err)))
28 { }
29
30 string mp3_error::get_message(int err)
31 {
32         return format("%d", err);
33 }
34
35
36 struct Mp3Decoder::Private
37 {
38         mad_stream stream;
39         mad_frame frame;
40         mad_synth synth;
41 };
42
43
44 Mp3Decoder::Mp3Decoder(IO::Seekable &io):
45         priv(new Private),
46         in(io),
47         in_buf_size(16384),
48         in_buffer(new char[in_buf_size]),
49         read_pos(0)
50 {
51         mad_stream_init(&priv->stream);
52         mad_frame_init(&priv->frame);
53         mad_synth_init(&priv->synth);
54
55         try
56         {
57                 decode_frame();
58                 format = (priv->frame.header.mode==MAD_MODE_SINGLE_CHANNEL ? MONO16 : STEREO16);
59                 freq = priv->frame.header.samplerate;
60         }
61         catch(...)
62         {
63                 delete[] in_buffer;
64                 throw;
65         }
66 }
67
68 Mp3Decoder::~Mp3Decoder()
69 {
70         delete[] in_buffer;
71         mad_synth_finish(&priv->synth);
72         mad_frame_finish(&priv->frame);
73         mad_stream_finish(&priv->stream);
74         delete priv;
75 }
76
77 bool Mp3Decoder::detect(const string &sig)
78 {
79         if(sig.size()>=2 && sig[0]=='\xFF' && (sig[1]&0xE0)==0xE0)
80                 return true;
81
82         static const char id3_sig[] = { 'I', 'D', '3' };
83         if(sig.size()<sizeof(id3_sig))
84                 return false;
85         return !sig.compare(0, sizeof(id3_sig), id3_sig);
86 }
87
88 void Mp3Decoder::rewind()
89 {
90         in.seek(0, IO::S_BEG);
91 }
92
93 unsigned Mp3Decoder::read(char *buf, unsigned len)
94 {
95         unsigned nchan = (format==STEREO16 ? 2 : 1);
96         mad_pcm &pcm = priv->synth.pcm;
97
98         unsigned pos = 0;
99         while(pos+2*nchan<=len)
100         {
101                 if(read_pos>=pcm.length)
102                 {
103                         if(!decode_frame())
104                                 break;
105                 }
106
107                 for(unsigned i=0; i<nchan; ++i)
108                 {
109                         Int16 sample = scale(pcm.samples[i][read_pos]);
110                         buf[pos++] = sample;
111                         buf[pos++] = sample>>8;
112                 }
113
114                 ++read_pos;
115         }
116
117         return pos;
118 }
119
120 bool Mp3Decoder::fill_input()
121 {
122         unsigned in_pos = 0;
123         if(priv->stream.next_frame && priv->stream.next_frame<priv->stream.bufend)
124         {
125                 copy(priv->stream.next_frame, priv->stream.bufend, in_buffer);
126                 in_pos = priv->stream.bufend-priv->stream.next_frame;
127         }
128
129         unsigned len = in.read(in_buffer+in_pos, in_buf_size-in_pos);
130         in_pos += len;
131
132         mad_stream_buffer(&priv->stream, reinterpret_cast<unsigned char *>(in_buffer), in_pos);
133         priv->stream.error = MAD_ERROR_NONE;
134
135         return len;
136 }
137
138 bool Mp3Decoder::decode_frame()
139 {
140         while(1)
141         {
142                 if(!priv->stream.buffer || priv->stream.error==MAD_ERROR_BUFLEN)
143                         if(!fill_input())
144                                 return false;
145
146                 if(!mad_frame_decode(&priv->frame, &priv->stream))
147                 {
148                         mad_synth_frame(&priv->synth, &priv->frame);
149                         read_pos = 0;
150                         return true;
151                 }
152
153                 if(priv->stream.error==MAD_ERROR_BUFLEN)
154                         continue;
155                 else if(!MAD_RECOVERABLE(priv->stream.error))
156                         throw mp3_error("mad_frame_decode", priv->stream.error);
157         }
158 }
159
160 } // namespace AL
161 } // namespace Msp