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