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