change back to unix-format line endings
[flac.git] / src / libFLAC / file_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  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 <stdio.h>
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for strcmp() */
23 #include <sys/stat.h> /* for stat() */
24 #include "FLAC/assert.h"
25 #include "protected/file_decoder.h"
26 #include "protected/seekable_stream_decoder.h"
27 #include "private/md5.h"
28
29 /***********************************************************************
30  *
31  * Private class method prototypes
32  *
33  ***********************************************************************/
34
35 static FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
36 static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
37 static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
38 static FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
39 static FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data);
40 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
41 static void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
42 static void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
43
44 /***********************************************************************
45  *
46  * Private class data
47  *
48  ***********************************************************************/
49
50 typedef struct FLAC__FileDecoderPrivate {
51         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
52         void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
53         void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
54         void *client_data;
55         FILE *file;
56         char *filename; /* == NULL if stdin */
57         FLAC__SeekableStreamDecoder *seekable_stream_decoder;
58         struct {
59                 FLAC__bool md5_checking;
60         } init_values_for_superclass;
61 } FLAC__FileDecoderPrivate;
62
63 /***********************************************************************
64  *
65  * Public static class data
66  *
67  ***********************************************************************/
68
69 const char *FLAC__FileDecoderStateString[] = {
70         "FLAC__FILE_DECODER_OK",
71         "FLAC__FILE_DECODER_SEEKING",
72         "FLAC__FILE_DECODER_END_OF_FILE",
73         "FLAC__FILE_DECODER_ERROR_OPENING_FILE",
74         "FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",
75         "FLAC__FILE_DECODER_SEEK_ERROR",
76         "FLAC__FILE_DECODER_STREAM_ERROR",
77         "FLAC__FILE_DECODER_STREAM_DECODER_ERROR",
78         "FLAC__FILE_DECODER_ALREADY_INITIALIZED",
79         "FLAC__FILE_DECODER_INVALID_CALLBACK",
80         "FLAC__FILE_DECODER_UNINITIALIZED"
81 };
82
83 /***********************************************************************
84  *
85  * Class constructor/destructor
86  *
87  ***********************************************************************/
88
89 FLAC__FileDecoder *FLAC__file_decoder_new()
90 {
91         FLAC__FileDecoder *decoder;
92
93         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
94
95         decoder = (FLAC__FileDecoder*)malloc(sizeof(FLAC__FileDecoder));
96         if(decoder == 0) {
97                 return 0;
98         }
99         decoder->protected_ = (FLAC__FileDecoderProtected*)malloc(sizeof(FLAC__FileDecoderProtected));
100         if(decoder->protected_ == 0) {
101                 free(decoder);
102                 return 0;
103         }
104         decoder->private_ = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));
105         if(decoder->private_ == 0) {
106                 free(decoder->protected_);
107                 free(decoder);
108                 return 0;
109         }
110
111         decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;
112
113         decoder->private_->filename = 0;
114         decoder->private_->write_callback = 0;
115         decoder->private_->metadata_callback = 0;
116         decoder->private_->error_callback = 0;
117         decoder->private_->client_data = 0;
118         decoder->private_->init_values_for_superclass.md5_checking = false;
119
120         return decoder;
121 }
122
123 void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder)
124 {
125         FLAC__ASSERT(decoder != 0);
126         FLAC__ASSERT(decoder->protected_ != 0);
127         FLAC__ASSERT(decoder->private_ != 0);
128
129         free(decoder->private_);
130         free(decoder->protected_);
131         free(decoder);
132 }
133
134 /***********************************************************************
135  *
136  * Public class methods
137  *
138  ***********************************************************************/
139
140 FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder)
141 {
142         FLAC__ASSERT(decoder != 0);
143
144         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
145                 return decoder->protected_->state = FLAC__FILE_DECODER_ALREADY_INITIALIZED;
146
147         decoder->protected_->state = FLAC__FILE_DECODER_OK;
148
149         if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
150                 return decoder->protected_->state = FLAC__FILE_DECODER_INVALID_CALLBACK;
151
152         decoder->private_->file = 0;
153         decoder->private_->seekable_stream_decoder = 0;
154
155         if(0 == decoder->private_->filename)
156                 decoder->private_->file = stdin;
157         else
158                 decoder->private_->file = fopen(decoder->private_->filename, "rb");
159
160         if(decoder->private_->file == 0)
161                 return decoder->protected_->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;
162
163         decoder->private_->seekable_stream_decoder = FLAC__seekable_stream_decoder_new();
164
165         if(0 == decoder->private_->seekable_stream_decoder)
166                 return decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
167
168         FLAC__seekable_stream_decoder_set_read_callback(decoder->private_->seekable_stream_decoder, read_callback_);
169         FLAC__seekable_stream_decoder_set_seek_callback(decoder->private_->seekable_stream_decoder, seek_callback_);
170         FLAC__seekable_stream_decoder_set_tell_callback(decoder->private_->seekable_stream_decoder, tell_callback_);
171         FLAC__seekable_stream_decoder_set_length_callback(decoder->private_->seekable_stream_decoder, length_callback_);
172         FLAC__seekable_stream_decoder_set_eof_callback(decoder->private_->seekable_stream_decoder, eof_callback_);
173         FLAC__seekable_stream_decoder_set_write_callback(decoder->private_->seekable_stream_decoder, write_callback_);
174         FLAC__seekable_stream_decoder_set_metadata_callback(decoder->private_->seekable_stream_decoder, metadata_callback_);
175         FLAC__seekable_stream_decoder_set_error_callback(decoder->private_->seekable_stream_decoder, error_callback_);
176         FLAC__seekable_stream_decoder_set_client_data(decoder->private_->seekable_stream_decoder, decoder);
177
178         /*
179          * Unfortunately, because of the "_new() ... _set_() ... _init()" order of
180          * decoder initialization, settings that are 'inherited' from the superclass
181          * have to be passed up this way, because the superclass has not even been
182          * created yet when the value is set in the subclass.
183          */
184         (void)FLAC__seekable_stream_decoder_set_md5_checking(decoder->private_->seekable_stream_decoder, decoder->private_->init_values_for_superclass.md5_checking);
185
186         if(FLAC__seekable_stream_decoder_init(decoder->private_->seekable_stream_decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)
187                 return decoder->protected_->state = FLAC__FILE_DECODER_STREAM_DECODER_ERROR; /*@@@ change this to FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR in next minor-revision */
188
189         return decoder->protected_->state;
190 }
191
192 FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
193 {
194         FLAC__bool ok = true;
195
196         FLAC__ASSERT(decoder != 0);
197         if(decoder->protected_->state == FLAC__FILE_DECODER_UNINITIALIZED)
198                 return true;
199         if(decoder->private_->file != 0 && decoder->private_->file != stdin)
200                 fclose(decoder->private_->file);
201         if(0 != decoder->private_->filename)
202                 free(decoder->private_->filename);
203         if(decoder->private_->seekable_stream_decoder != 0) {
204                 ok = FLAC__seekable_stream_decoder_finish(decoder->private_->seekable_stream_decoder);
205                 FLAC__seekable_stream_decoder_delete(decoder->private_->seekable_stream_decoder);
206         }
207         decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;
208         return ok;
209 }
210
211 FLAC__bool FLAC__file_decoder_set_md5_checking(const FLAC__FileDecoder *decoder, FLAC__bool value)
212 {
213         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
214                 return false;
215         decoder->private_->init_values_for_superclass.md5_checking = value;
216         return true;
217 }
218
219 FLAC__bool FLAC__file_decoder_set_filename(const FLAC__FileDecoder *decoder, const char *value)
220 {
221         FLAC__ASSERT(value != 0);
222         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
223                 return false;
224         if(0 != decoder->private_->filename)
225                 free(decoder->private_->filename);
226         if(0 == strcmp(value, "-")) {
227                 decoder->private_->filename = 0;
228         }
229         else  {
230                 if(0 == (decoder->private_->filename = (char*)malloc(strlen(value)+1))) {
231                         decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
232                         return false;
233                 }
234                 strcpy(decoder->private_->filename, value);
235         }
236         return true;
237 }
238
239 FLAC__bool FLAC__file_decoder_set_write_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
240 {
241         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
242                 return false;
243         decoder->private_->write_callback = value;
244         return true;
245 }
246
247 FLAC__bool FLAC__file_decoder_set_metadata_callback(const FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
248 {
249         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
250                 return false;
251         decoder->private_->metadata_callback = value;
252         return true;
253 }
254
255 FLAC__bool FLAC__file_decoder_set_error_callback(const FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
256 {
257         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
258                 return false;
259         decoder->private_->error_callback = value;
260         return true;
261 }
262
263 FLAC__bool FLAC__file_decoder_set_client_data(const FLAC__FileDecoder *decoder, void *value)
264 {
265         if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
266                 return false;
267         decoder->private_->client_data = value;
268         return true;
269 }
270
271 FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder)
272 {
273         return decoder->protected_->state;
274 }
275
276 FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder)
277 {
278         return FLAC__seekable_stream_decoder_get_md5_checking(decoder->private_->seekable_stream_decoder);
279 }
280
281 FLAC__bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder)
282 {
283         FLAC__bool ret;
284         FLAC__ASSERT(decoder != 0);
285
286         if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
287                 decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
288
289         if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
290                 return true;
291
292         FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
293
294         ret = FLAC__seekable_stream_decoder_process_whole_stream(decoder->private_->seekable_stream_decoder);
295         if(!ret)
296                 decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
297
298         return ret;
299 }
300
301 FLAC__bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder)
302 {
303         FLAC__bool ret;
304         FLAC__ASSERT(decoder != 0);
305
306         if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
307                 decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
308
309         if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
310                 return true;
311
312         FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
313
314         ret = FLAC__seekable_stream_decoder_process_metadata(decoder->private_->seekable_stream_decoder);
315         if(!ret)
316                 decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
317
318         return ret;
319 }
320
321 FLAC__bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder)
322 {
323         FLAC__bool ret;
324         FLAC__ASSERT(decoder != 0);
325
326         if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
327                 decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
328
329         if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
330                 return true;
331
332         FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
333
334         ret = FLAC__seekable_stream_decoder_process_one_frame(decoder->private_->seekable_stream_decoder);
335         if(!ret)
336                 decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
337
338         return ret;
339 }
340
341 FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder)
342 {
343         FLAC__bool ret;
344         FLAC__ASSERT(decoder != 0);
345
346         if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
347                 decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
348
349         if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
350                 return true;
351
352         FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
353
354         ret = FLAC__seekable_stream_decoder_process_remaining_frames(decoder->private_->seekable_stream_decoder);
355         if(!ret)
356                 decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
357
358         return ret;
359 }
360
361 /***********************************************************************
362  *
363  * Private class methods
364  *
365  ***********************************************************************/
366
367 FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample)
368 {
369         FLAC__ASSERT(decoder != 0);
370         FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
371
372         if(decoder->private_->filename == 0) { /* means the file is stdin... */
373                 decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;
374                 return false;
375         }
376
377         if(!FLAC__seekable_stream_decoder_seek_absolute(decoder->private_->seekable_stream_decoder, sample)) {
378                 decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;
379                 return false;
380         }
381         else
382                 return true;
383 }
384
385 FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
386 {
387         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
388         (void)decoder;
389
390         if(*bytes > 0) {
391                 size_t bytes_read = fread(buffer, sizeof(FLAC__byte), *bytes, file_decoder->private_->file);
392                 if(bytes_read == 0 && !feof(file_decoder->private_->file)) {
393                         return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
394                 }
395                 else {
396                         *bytes = (unsigned)bytes_read;
397                         return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
398                 }
399         }
400         else
401                 return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; /* abort to avoid a deadlock */
402 }
403
404 FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
405 {
406         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
407         (void)decoder;
408
409         if(fseek(file_decoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
410                 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
411         else
412                 return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
413 }
414
415 FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
416 {
417         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
418         long pos;
419         (void)decoder;
420
421         if((pos = ftell(file_decoder->private_->file)) < 0)
422                 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
423         else {
424                 *absolute_byte_offset = (FLAC__uint64)pos;
425                 return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
426         }
427 }
428
429 FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
430 {
431         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
432         struct stat filestats;
433         (void)decoder;
434
435         if(stat(file_decoder->private_->filename, &filestats) != 0)
436                 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
437         else {
438                 *stream_length = (FLAC__uint64)filestats.st_size;
439                 return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
440         }
441 }
442
443 FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data)
444 {
445         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
446         (void)decoder;
447
448         return feof(file_decoder->private_->file);
449 }
450
451 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
452 {
453         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
454         (void)decoder;
455
456         return file_decoder->private_->write_callback(file_decoder, frame, buffer, file_decoder->private_->client_data);
457 }
458
459 void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
460 {
461         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
462         (void)decoder;
463
464         file_decoder->private_->metadata_callback(file_decoder, metadata, file_decoder->private_->client_data);
465 }
466
467 void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
468 {
469         FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
470         (void)decoder;
471
472         file_decoder->private_->error_callback(file_decoder, status, file_decoder->private_->client_data);
473 }