add string arrays for enums
[flac.git] / src / libFLAC / file_decoder.c
1 /* libFLAC - Free Lossless Audio Coder library
2  * Copyright (C) 2000  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h> /* for malloc() */
23 #include <string.h> /* for strcmp() */
24 #include "FLAC/file_decoder.h"
25 #include "protected/stream_decoder.h"
26
27 typedef struct FLAC__FileDecoderPrivate {
28         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
29         void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
30         void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
31         void *client_data;
32         FILE *file;
33         FLAC__StreamDecoder *stream;
34         /* the rest of these are only used for seeking: */
35         FLAC__StreamMetaData_Encoding metadata; /* we keep this around so we can figure out how to seek quickly */
36         FLAC__FrameHeader last_frame_header; /* holds the info of the last frame we seeked to */
37         uint64 target_sample;
38 } FLAC__FileDecoderPrivate;
39
40 static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
41 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
42 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
43 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
44 static bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64 target_sample);
45
46 const char *FLAC__FileDecoderStateString[] = {
47         "FLAC__FILE_DECODER_OK",
48         "FLAC__FILE_DECODER_SEEKING",
49         "FLAC__FILE_DECODER_END_OF_FILE",
50         "FLAC__FILE_DECODER_ERROR_OPENING_FILE",
51         "FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",
52         "FLAC__FILE_DECODER_SEEK_ERROR",
53         "FLAC__FILE_DECODER_STREAM_ERROR",
54         "FLAC__FILE_DECODER_UNINITIALIZED"
55 };
56
57 FLAC__FileDecoder *FLAC__file_decoder_get_new_instance()
58 {
59         FLAC__FileDecoder *decoder = (FLAC__FileDecoder*)malloc(sizeof(FLAC__FileDecoder));
60         if(decoder != 0) {
61                 decoder->state = FLAC__FILE_DECODER_UNINITIALIZED;
62                 decoder->guts = 0;
63         }
64         return decoder;
65 }
66
67 void FLAC__file_decoder_free_instance(FLAC__FileDecoder *decoder)
68 {
69         free(decoder);
70 }
71
72 FLAC__FileDecoderState FLAC__file_decoder_init(
73         FLAC__FileDecoder *decoder,
74         const char *filename,
75         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
76         void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
77         void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
78         void *client_data
79 )
80 {
81         assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
82         assert(decoder != 0);
83         assert(write_callback != 0);
84         assert(metadata_callback != 0);
85         assert(error_callback != 0);
86         assert(decoder->state == FLAC__FILE_DECODER_UNINITIALIZED);
87         assert(decoder->guts == 0);
88
89         decoder->state = FLAC__FILE_DECODER_OK;
90
91         decoder->guts = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));
92         if(decoder->guts == 0)
93                 return decoder->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
94
95         decoder->guts->write_callback = write_callback;
96         decoder->guts->metadata_callback = metadata_callback;
97         decoder->guts->error_callback = error_callback;
98         decoder->guts->client_data = client_data;
99         decoder->guts->stream = 0;
100
101         if(0 == strcmp(filename, "-"))
102                 decoder->guts->file = stdin;
103         else
104                 decoder->guts->file = fopen(filename, "rb");
105         if(decoder->guts->file == 0)
106                 return decoder->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;
107
108         decoder->guts->stream = FLAC__stream_decoder_get_new_instance();
109         if(FLAC__stream_decoder_init(decoder->guts->stream, read_callback_, write_callback_, metadata_callback_, error_callback_, decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
110                 return decoder->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR; /* this is based on internal knowledge of FLAC__stream_decoder_init() */
111
112         return decoder->state;
113 }
114
115 void FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
116 {
117         assert(decoder != 0);
118         if(decoder->state == FLAC__FILE_DECODER_UNINITIALIZED)
119                 return;
120         if(decoder->guts != 0) {
121                 if(decoder->guts->file != 0 && decoder->guts->file != stdin)
122                         fclose(decoder->guts->file);
123                 if(decoder->guts->stream != 0) {
124                         FLAC__stream_decoder_finish(decoder->guts->stream);
125                         FLAC__stream_decoder_free_instance(decoder->guts->stream);
126                 }
127                 free(decoder->guts);
128                 decoder->guts = 0;
129         }
130         decoder->state = FLAC__FILE_DECODER_UNINITIALIZED;
131 }
132
133 bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder)
134 {
135         bool ret;
136         assert(decoder != 0);
137
138         if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
139                 return true;
140
141         assert(decoder->state == FLAC__FILE_DECODER_OK);
142
143         ret = FLAC__stream_decoder_process_whole_stream(decoder->guts->stream);
144         if(!ret)
145                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
146
147         return ret;
148 }
149
150 bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder)
151 {
152         bool ret;
153         assert(decoder != 0);
154
155         if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
156                 return true;
157
158         assert(decoder->state == FLAC__FILE_DECODER_OK);
159
160         ret = FLAC__stream_decoder_process_metadata(decoder->guts->stream);
161         if(!ret)
162                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
163
164         return ret;
165 }
166
167 bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder)
168 {
169         bool ret;
170         assert(decoder != 0);
171
172         if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
173                 return true;
174
175         assert(decoder->state == FLAC__FILE_DECODER_OK);
176
177         ret = FLAC__stream_decoder_process_one_frame(decoder->guts->stream);
178         if(!ret)
179                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
180
181         return ret;
182 }
183
184 bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder)
185 {
186         bool ret;
187         assert(decoder != 0);
188
189         if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE)
190                 return true;
191
192         assert(decoder->state == FLAC__FILE_DECODER_OK);
193
194         ret = FLAC__stream_decoder_process_remaining_frames(decoder->guts->stream);
195         if(!ret)
196                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
197
198         return ret;
199 }
200
201 bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, uint64 sample)
202 {
203         long filesize;
204
205         assert(decoder != 0);
206         assert(decoder->state == FLAC__FILE_DECODER_OK);
207
208         decoder->state = FLAC__FILE_DECODER_SEEKING;
209
210         if(!FLAC__stream_decoder_reset(decoder->guts->stream)) {
211                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
212                 return false;
213         }
214         /* get the file length */
215         if(0 != fseek(decoder->guts->file, 0, SEEK_END)) {
216                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
217                 return false;
218         }
219         fflush(decoder->guts->file);
220         if(-1 == (filesize = ftell(decoder->guts->file))) {
221                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
222                 return false;
223         }
224         /* rewind */
225         if(0 != fseek(decoder->guts->file, 0, SEEK_SET)) {
226                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
227                 return false;
228         }
229         if(!FLAC__stream_decoder_process_metadata(decoder->guts->stream)) {
230                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
231                 return false;
232         }
233         if(sample > decoder->guts->metadata.total_samples) {
234                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
235                 return false;
236         }
237
238         return seek_to_absolute_sample_(decoder, filesize, sample);
239 }
240
241 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
242 {
243         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
244         (void)decoder;
245         if(feof(file_decoder->guts->file)) {
246                 file_decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
247                 return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
248         }
249         else if(*bytes > 0) {
250                 size_t bytes_read = fread(buffer, sizeof(byte), *bytes, file_decoder->guts->file);
251                 if(bytes_read == 0) {
252                         if(feof(file_decoder->guts->file)) {
253                                 file_decoder->state = FLAC__FILE_DECODER_END_OF_FILE;
254                                 return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
255                         }
256                         else
257                                 return FLAC__STREAM_DECODER_READ_ABORT;
258                 }
259                 else {
260                         *bytes = (unsigned)bytes_read;
261                         return FLAC__STREAM_DECODER_READ_CONTINUE;
262                 }
263         }
264         else
265                 return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */
266 }
267
268 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data)
269 {
270         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
271         (void)decoder;
272
273         if(file_decoder->state == FLAC__FILE_DECODER_SEEKING) {
274                 uint64 this_frame_sample = header->number.sample_number;
275                 uint64 next_frame_sample = this_frame_sample + (uint64)header->blocksize;
276                 uint64 target_sample = file_decoder->guts->target_sample;
277
278                 file_decoder->guts->last_frame_header = *header; /* save the header in the guts */
279                 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
280                         unsigned delta = (unsigned)(target_sample - this_frame_sample);
281                         /* kick out of seek mode */
282                         file_decoder->state = FLAC__FILE_DECODER_OK;
283                         /* shift out the samples before target_sample */
284                         if(delta > 0) {
285                                 unsigned channel;
286                                 const int32 *newbuffer[FLAC__MAX_CHANNELS];
287                                 for(channel = 0; channel < header->channels; channel++)
288                                         newbuffer[channel] = buffer[channel] + delta;
289                                 file_decoder->guts->last_frame_header.blocksize -= delta;
290                                 file_decoder->guts->last_frame_header.number.sample_number += (uint64)delta;
291                                 /* write the relevant samples */
292                                 return file_decoder->guts->write_callback(file_decoder, &file_decoder->guts->last_frame_header, newbuffer, file_decoder->guts->client_data);
293                         }
294                         else {
295                                 /* write the relevant samples */
296                                 return file_decoder->guts->write_callback(file_decoder, header, buffer, file_decoder->guts->client_data);
297                         }
298                 }
299                 else {
300                         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
301                 }
302         }
303         else {
304                 return file_decoder->guts->write_callback(file_decoder, header, buffer, file_decoder->guts->client_data);
305         }
306 }
307
308 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
309 {
310         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
311         (void)decoder;
312
313         if(metadata->type == FLAC__METADATA_TYPE_ENCODING)
314                 file_decoder->guts->metadata = metadata->data.encoding;
315         if(file_decoder->state != FLAC__FILE_DECODER_SEEKING)
316                 file_decoder->guts->metadata_callback(file_decoder, metadata, file_decoder->guts->client_data);
317 }
318
319 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
320 {
321         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
322         (void)decoder;
323
324         if(file_decoder->state != FLAC__FILE_DECODER_SEEKING)
325                 file_decoder->guts->error_callback(file_decoder, status, file_decoder->guts->client_data);
326 }
327
328 bool seek_to_absolute_sample_(FLAC__FileDecoder *decoder, long filesize, uint64 target_sample)
329 {
330         long l, r, pos, last_pos = -1;
331         unsigned approx_bytes_per_frame;
332         uint64 last_frame_sample = 0xffffffffffffffff;
333         bool needs_seek;
334         const bool is_variable_blocksize_stream = (decoder->guts->metadata.min_blocksize != decoder->guts->metadata.max_blocksize);
335
336         if(!is_variable_blocksize_stream) {
337                 /* we are just guessing here, but we want to guess high, not low */
338                 /* note there are no () around 'decoder->guts->metadata.bits_per_sample/8' to keep precision up since it's an integer calulation */
339                 approx_bytes_per_frame = decoder->guts->metadata.min_blocksize * decoder->guts->metadata.channels * decoder->guts->metadata.bits_per_sample/8 + 64;
340         }
341         else
342                 approx_bytes_per_frame = 1152 * decoder->guts->metadata.channels * decoder->guts->metadata.bits_per_sample/8 + 64;
343
344         /* Now we need to use the metadata and the filelength to search to the frame with the correct sample */
345         if(-1 == (l = ftell(decoder->guts->file))) {
346                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
347                 return false;
348         }
349         l -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->guts->stream);
350 #ifdef _MSC_VER
351         /* with VC++ you have to spoon feed it the casting */
352         pos = l + (long)((double)(int64)target_sample / (double)(int64)decoder->guts->metadata.total_samples * (double)(filesize-l+1)) - approx_bytes_per_frame;
353 #else
354         pos = l + (long)((double)target_sample / (double)decoder->guts->metadata.total_samples * (double)(filesize-l+1)) - approx_bytes_per_frame;
355 #endif
356         r = filesize - ((decoder->guts->metadata.channels * decoder->guts->metadata.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 64);
357         if(pos >= r)
358                 pos = r-1;
359         if(pos < l)
360                 pos = l;
361         needs_seek = true;
362
363         decoder->guts->target_sample = target_sample;
364         while(1) {
365                 if(needs_seek) {
366                         if(-1 == fseek(decoder->guts->file, pos, SEEK_SET)) {
367                                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
368                                 return false;
369                         }
370                         if(!FLAC__stream_decoder_flush(decoder->guts->stream)) {
371                                 decoder->state = FLAC__FILE_DECODER_STREAM_ERROR;
372                                 return false;
373                         }
374                 }
375                 if(!FLAC__stream_decoder_process_one_frame(decoder->guts->stream)) {
376                         decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
377                         return false;
378                 }
379                 /* our write callback will change the state when it gets to the target frame */
380                 if(decoder->state != FLAC__FILE_DECODER_SEEKING) {
381                         break;
382                 }
383                 else { /* we need to narrow the search */
384                         uint64 this_frame_sample = decoder->guts->last_frame_header.number.sample_number;
385                         if(this_frame_sample == last_frame_sample) {
386                                 /* our last move backwards wasn't big enough */
387                                 pos -= (last_pos - pos);
388                                 needs_seek = true;
389                         }
390                         else {
391                                 if(target_sample < this_frame_sample) {
392                                         last_pos = pos;
393                                         approx_bytes_per_frame = decoder->guts->last_frame_header.blocksize * decoder->guts->last_frame_header.channels * decoder->guts->last_frame_header.bits_per_sample/8 + 64;
394                                         pos -= approx_bytes_per_frame;
395                                         needs_seek = true;
396                                 }
397                                 else {
398                                         last_pos = pos;
399                                         if(-1 == (pos = ftell(decoder->guts->file))) {
400                                                 decoder->state = FLAC__FILE_DECODER_SEEK_ERROR;
401                                                 return false;
402                                         }
403                                         pos -= FLAC__stream_decoder_input_bytes_unconsumed(decoder->guts->stream);
404                                         needs_seek = false;
405                                 }
406                         }
407                         if(pos < l)
408                                 pos = l;
409                         last_frame_sample = this_frame_sample;
410                 }
411         }
412
413         return true;
414 }