Fix issues with VBR mp3s, detect duration by decoding
[ardour.git] / libs / ardour / mp3fileimportable.cc
1 /*
2  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #define MINIMP3_IMPLEMENTATION
21
22 #include <fcntl.h>
23
24 #ifdef PLATFORM_WINDOWS
25 #include <windows.h>
26 #else
27 #include <sys/mman.h>
28 #endif
29
30 #include <glib.h>
31 #include "pbd/gstdio_compat.h"
32
33 #include "pbd/error.h"
34 #include "ardour/mp3fileimportable.h"
35
36 using namespace ARDOUR;
37 using namespace std;
38
39 Mp3FileImportableSource::Mp3FileImportableSource (const string& path)
40         : _fd (-1)
41         , _map_addr (0)
42         , _map_length (0)
43         , _buffer (0)
44         , _remain (0)
45         , _read_position (0)
46         , _pcm_off (0)
47         , _n_frames (0)
48 {
49         mp3dec_init (&_mp3d);
50         memset (&_info, 0, sizeof (_info));
51
52         GStatBuf statbuf;
53         if (g_stat (path.c_str(), &statbuf) != 0) {
54                 throw failed_constructor ();
55         }
56
57         _fd = g_open (path.c_str (), O_RDONLY, 0444);
58         if (_fd == -1) {
59                 throw failed_constructor ();
60         }
61         _map_length = statbuf.st_size;
62
63 #ifdef PLATFORM_WINDOWS
64         HANDLE file_handle = (HANDLE) _get_osfhandle (int(_fd));
65
66         HANDLE map_handle = CreateFileMapping (file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
67         if (map_handle == NULL) {
68                 close (_fd);
69                 throw failed_constructor ();
70         }
71
72         LPVOID view_handle = MapViewOfFile (map_handle, FILE_MAP_READ, 0, 0, _map_length);
73         if (view_handle == NULL) {
74                 CloseHandle (map_handle);
75                 close (_fd);
76                 throw failed_constructor ();
77         }
78         _map_addr = (const uint8_t*)view_handle;
79         CloseHandle (map_handle);
80 #else
81         _map_addr = (const uint8_t *)mmap (NULL, _map_length, PROT_READ, MAP_PRIVATE, _fd, 0);
82         if (_map_addr == MAP_FAILED) {
83                 close (_fd);
84                 throw failed_constructor ();
85         }
86 #endif
87
88         _buffer = _map_addr;
89         _remain = _map_length;
90
91         if (!decode_mp3 ()) {
92                 unmap_mem ();
93                 throw failed_constructor ();
94         }
95
96         /* estimate length, fixed bitrate */
97         _length = _n_frames * _map_length / _info.frame_bytes;
98
99 #if 1 /* detect accurate length by parsing frame headers */
100         _length = _n_frames;
101         while (decode_mp3 (true)) {
102                 _length += _n_frames;
103         }
104         _read_position = _length;
105         seek (0);
106 #endif
107 }
108
109 Mp3FileImportableSource::~Mp3FileImportableSource ()
110 {
111         unmap_mem ();
112 }
113
114 void
115 Mp3FileImportableSource::unmap_mem ()
116 {
117 #ifdef PLATFORM_WINDOWS
118         if (_map_addr) {
119                 UnmapViewOfFile (_map_addr);
120         }
121 #else
122         munmap ((void*) _map_addr, _map_length);
123 #endif
124         close (_fd);
125         _map_addr = 0;
126 }
127
128 int
129 Mp3FileImportableSource::decode_mp3 (bool parse_only)
130 {
131         _pcm_off = 0;
132         do {
133                 _n_frames = mp3dec_decode_frame (&_mp3d, _buffer, _remain, parse_only ? NULL :_pcm, &_info);
134                 _buffer += _info.frame_bytes;
135                 _remain -= _info.frame_bytes;
136                 if (_n_frames) {
137                         break;
138                 }
139         } while (_info.frame_bytes);
140         return _n_frames;
141 }
142
143 void
144 Mp3FileImportableSource::seek (samplepos_t pos)
145 {
146         if (_read_position == pos) {
147                 return;
148         }
149
150         /* rewind, then decode to pos */
151         if (pos < _read_position) {
152                 _buffer = _map_addr;
153                 _remain = _map_length;
154                 _read_position = 0;
155                 _pcm_off = 0;
156                 mp3dec_init (&_mp3d);
157                 decode_mp3 ();
158         }
159
160         while (_read_position + _n_frames <= pos) {
161                 if (!decode_mp3 ()) {
162                         break;
163                 }
164                 _read_position += _n_frames;
165         }
166
167         if (_n_frames > 0) {
168                 _pcm_off = _info.channels * (pos - _read_position);
169                 _n_frames -= pos - _read_position;
170                 _read_position = pos;
171         }
172         assert (_pcm_off >= 0 && _pcm_off < MINIMP3_MAX_SAMPLES_PER_FRAME);
173 }
174
175 samplecnt_t
176 Mp3FileImportableSource::read (Sample* dst, samplecnt_t nframes)
177 {
178         size_t dst_off = 0;
179         int remain = nframes; // == samples * channels
180         while (remain > 0) {
181                 samplecnt_t samples_to_copy = std::min (remain, _n_frames * _info.channels);
182                 if (samples_to_copy > 0) {
183                         memcpy (&dst[dst_off], &_pcm[_pcm_off], samples_to_copy * sizeof (Sample));
184                         remain         -= samples_to_copy;
185                         dst_off        += samples_to_copy;
186                         _n_frames      -= samples_to_copy / _info.channels;
187                         _pcm_off       += samples_to_copy;
188                         _read_position += samples_to_copy / _info.channels;
189                 }
190                 assert (_n_frames >= 0);
191                 if (_n_frames <= 0 && !decode_mp3 ()) {
192                         /* EOF, or decode error */
193                         break;
194                 }
195         }
196         return dst_off;
197 }
198
199 samplecnt_t
200 Mp3FileImportableSource::read_unlocked (Sample* dst, samplepos_t start, samplecnt_t cnt, uint32_t chn)
201 {
202         const uint32_t n_chn = channels ();
203         if (chn > n_chn || cnt == 0) {
204                 return 0;
205         }
206         if (start != _read_position) {
207                 seek (start);
208         }
209
210         size_t dst_off = 0;
211         samplecnt_t remain = cnt;
212
213         while (remain > 0) {
214                 samplecnt_t samples_to_copy = std::min (remain, (samplecnt_t)_n_frames);
215
216                 for (samplecnt_t n = 0; n < samples_to_copy; ++n) {
217                         dst[dst_off] = _pcm[_pcm_off + chn];
218                         dst_off        += 1;
219                         remain         -= 1;
220                         _n_frames      -= 1;
221                         _pcm_off       += n_chn;
222                         _read_position += 1;
223                 }
224
225                 assert (_n_frames >= 0);
226                 if (_n_frames <= 0 && !decode_mp3 ()) {
227                         /* EOF, or decode error */
228                         break;
229                 }
230         }
231         return dst_off;
232 }