Fix more compiler warnings.
[platform/upstream/flac.git] / examples / cpp / decode / file / main.cpp
1 /* example_cpp_decode_file - Simple FLAC file decoder using libFLAC
2  * Copyright (C) 2007,2008,2009  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*
20  * This example shows how to use libFLAC++ to decode a FLAC file to a WAVE
21  * file.  It only supports 16-bit stereo files.
22  *
23  * Complete API documentation can be found at:
24  *   http://flac.sourceforge.net/api/
25  */
26
27 #if HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #define __STDC_FORMAT_MACROS
35 #include <inttypes.h>
36
37 #include "FLAC++/decoder.h"
38
39 static FLAC__uint64 total_samples = 0;
40 static unsigned sample_rate = 0;
41 static unsigned channels = 0;
42 static unsigned bps = 0;
43
44 static bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
45 {
46         return
47                 fputc(x, f) != EOF &&
48                 fputc(x >> 8, f) != EOF
49         ;
50 }
51
52 static bool write_little_endian_int16(FILE *f, FLAC__int16 x)
53 {
54         return write_little_endian_uint16(f, (FLAC__uint16)x);
55 }
56
57 static bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
58 {
59         return
60                 fputc(x, f) != EOF &&
61                 fputc(x >> 8, f) != EOF &&
62                 fputc(x >> 16, f) != EOF &&
63                 fputc(x >> 24, f) != EOF
64         ;
65 }
66
67 class OurDecoder: public FLAC::Decoder::File {
68 public:
69         OurDecoder(FILE *f_): FLAC::Decoder::File(), f(f_) { }
70 protected:
71         FILE *f;
72
73         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
74         virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
75         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status);
76 };
77
78 int main(int argc, char *argv[])
79 {
80         bool ok = true;
81         FILE *fout;
82
83         if(argc != 3) {
84                 fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
85                 return 1;
86         }
87
88         if((fout = fopen(argv[2], "wb")) == NULL) {
89                 fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
90                 return 1;
91         }
92
93         OurDecoder decoder(fout);
94
95         if(!decoder) {
96                 fprintf(stderr, "ERROR: allocating decoder\n");
97                 fclose(fout);
98                 return 1;
99         }
100
101         (void)decoder.set_md5_checking(true);
102
103         FLAC__StreamDecoderInitStatus init_status = decoder.init(argv[1]);
104         if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
105                 fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
106                 ok = false;
107         }
108
109         if(ok) {
110                 ok = decoder.process_until_end_of_stream();
111                 fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
112                 fprintf(stderr, "   state: %s\n", decoder.get_state().resolved_as_cstring(decoder));
113         }
114
115         fclose(fout);
116
117         return 0;
118 }
119
120 ::FLAC__StreamDecoderWriteStatus OurDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
121 {
122         const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
123         size_t i;
124
125         if(total_samples == 0) {
126                 fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
127                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
128         }
129         if(channels != 2 || bps != 16) {
130                 fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
131                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
132         }
133
134         /* write WAVE header before we write the first frame */
135         if(frame->header.number.sample_number == 0) {
136                 if(
137                         fwrite("RIFF", 1, 4, f) < 4 ||
138                         !write_little_endian_uint32(f, total_size + 36) ||
139                         fwrite("WAVEfmt ", 1, 8, f) < 8 ||
140                         !write_little_endian_uint32(f, 16) ||
141                         !write_little_endian_uint16(f, 1) ||
142                         !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
143                         !write_little_endian_uint32(f, sample_rate) ||
144                         !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
145                         !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
146                         !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
147                         fwrite("data", 1, 4, f) < 4 ||
148                         !write_little_endian_uint32(f, total_size)
149                 ) {
150                         fprintf(stderr, "ERROR: write error\n");
151                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
152                 }
153         }
154
155         /* write decoded PCM samples */
156         for(i = 0; i < frame->header.blocksize; i++) {
157                 if(
158                         !write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) ||  /* left channel */
159                         !write_little_endian_int16(f, (FLAC__int16)buffer[1][i])     /* right channel */
160                 ) {
161                         fprintf(stderr, "ERROR: write error\n");
162                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
163                 }
164         }
165
166         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
167 }
168
169 void OurDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
170 {
171         /* print some stats */
172         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
173                 /* save for later */
174                 total_samples = metadata->data.stream_info.total_samples;
175                 sample_rate = metadata->data.stream_info.sample_rate;
176                 channels = metadata->data.stream_info.channels;
177                 bps = metadata->data.stream_info.bits_per_sample;
178
179                 fprintf(stderr, "sample rate    : %u Hz\n", sample_rate);
180                 fprintf(stderr, "channels       : %u\n", channels);
181                 fprintf(stderr, "bits per sample: %u\n", bps);
182                 fprintf(stderr, "total samples  : %" PRIu64 "\n", total_samples);
183         }
184 }
185
186 void OurDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
187 {
188         fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
189 }