Huge Windows utf8 I/O patch.
[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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "FLAC++/decoder.h"
35 #include "share/compat.h"
36
37 static FLAC__uint64 total_samples = 0;
38 static unsigned sample_rate = 0;
39 static unsigned channels = 0;
40 static unsigned bps = 0;
41
42 static bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
43 {
44         return
45                 fputc(x, f) != EOF &&
46                 fputc(x >> 8, f) != EOF
47         ;
48 }
49
50 static bool write_little_endian_int16(FILE *f, FLAC__int16 x)
51 {
52         return write_little_endian_uint16(f, (FLAC__uint16)x);
53 }
54
55 static bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
56 {
57         return
58                 fputc(x, f) != EOF &&
59                 fputc(x >> 8, f) != EOF &&
60                 fputc(x >> 16, f) != EOF &&
61                 fputc(x >> 24, f) != EOF
62         ;
63 }
64
65 class OurDecoder: public FLAC::Decoder::File {
66 public:
67         OurDecoder(FILE *f_): FLAC::Decoder::File(), f(f_) { }
68 protected:
69         FILE *f;
70
71         virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
72         virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata);
73         virtual void error_callback(::FLAC__StreamDecoderErrorStatus status);
74 };
75
76 int main(int argc, char *argv[])
77 {
78         bool ok = true;
79         FILE *fout;
80
81         if(argc != 3) {
82                 fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
83                 return 1;
84         }
85
86         if((fout = fopen(argv[2], "wb")) == NULL) {
87                 fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
88                 return 1;
89         }
90
91         OurDecoder decoder(fout);
92
93         if(!decoder) {
94                 fprintf(stderr, "ERROR: allocating decoder\n");
95                 fclose(fout);
96                 return 1;
97         }
98
99         (void)decoder.set_md5_checking(true);
100
101         FLAC__StreamDecoderInitStatus init_status = decoder.init(argv[1]);
102         if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
103                 fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
104                 ok = false;
105         }
106
107         if(ok) {
108                 ok = decoder.process_until_end_of_stream();
109                 fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
110                 fprintf(stderr, "   state: %s\n", decoder.get_state().resolved_as_cstring(decoder));
111         }
112
113         fclose(fout);
114
115         return 0;
116 }
117
118 ::FLAC__StreamDecoderWriteStatus OurDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
119 {
120         const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
121         size_t i;
122
123         if(total_samples == 0) {
124                 fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
125                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
126         }
127         if(channels != 2 || bps != 16) {
128                 fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
129                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
130         }
131
132         /* write WAVE header before we write the first frame */
133         if(frame->header.number.sample_number == 0) {
134                 if(
135                         fwrite("RIFF", 1, 4, f) < 4 ||
136                         !write_little_endian_uint32(f, total_size + 36) ||
137                         fwrite("WAVEfmt ", 1, 8, f) < 8 ||
138                         !write_little_endian_uint32(f, 16) ||
139                         !write_little_endian_uint16(f, 1) ||
140                         !write_little_endian_uint16(f, (FLAC__uint16)channels) ||
141                         !write_little_endian_uint32(f, sample_rate) ||
142                         !write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
143                         !write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
144                         !write_little_endian_uint16(f, (FLAC__uint16)bps) ||
145                         fwrite("data", 1, 4, f) < 4 ||
146                         !write_little_endian_uint32(f, total_size)
147                 ) {
148                         fprintf(stderr, "ERROR: write error\n");
149                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
150                 }
151         }
152
153         /* write decoded PCM samples */
154         for(i = 0; i < frame->header.blocksize; i++) {
155                 if(
156                         !write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) ||  /* left channel */
157                         !write_little_endian_int16(f, (FLAC__int16)buffer[1][i])     /* right channel */
158                 ) {
159                         fprintf(stderr, "ERROR: write error\n");
160                         return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
161                 }
162         }
163
164         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
165 }
166
167 void OurDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
168 {
169         /* print some stats */
170         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
171                 /* save for later */
172                 total_samples = metadata->data.stream_info.total_samples;
173                 sample_rate = metadata->data.stream_info.sample_rate;
174                 channels = metadata->data.stream_info.channels;
175                 bps = metadata->data.stream_info.bits_per_sample;
176
177                 fprintf(stderr, "sample rate    : %u Hz\n", sample_rate);
178                 fprintf(stderr, "channels       : %u\n", channels);
179                 fprintf(stderr, "bits per sample: %u\n", bps);
180                 fprintf(stderr, "total samples  : %" PRIu64 "\n", total_samples);
181         }
182 }
183
184 void OurDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
185 {
186         fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
187 }