1 /* example_c_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 #include "share/compat.h"
35 #include "FLAC/stream_decoder.h"
36 
37 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
38 static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
39 static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
40 
41 static FLAC__uint64 total_samples = 0;
42 static unsigned sample_rate = 0;
43 static unsigned channels = 0;
44 static unsigned bps = 0;
45 
write_little_endian_uint16(FILE * f,FLAC__uint16 x)46 static FLAC__bool write_little_endian_uint16(FILE *f, FLAC__uint16 x)
47 {
48 	return
49 		fputc(x, f) != EOF &&
50 		fputc(x >> 8, f) != EOF
51 	;
52 }
53 
write_little_endian_int16(FILE * f,FLAC__int16 x)54 static FLAC__bool write_little_endian_int16(FILE *f, FLAC__int16 x)
55 {
56 	return write_little_endian_uint16(f, (FLAC__uint16)x);
57 }
58 
write_little_endian_uint32(FILE * f,FLAC__uint32 x)59 static FLAC__bool write_little_endian_uint32(FILE *f, FLAC__uint32 x)
60 {
61 	return
62 		fputc(x, f) != EOF &&
63 		fputc(x >> 8, f) != EOF &&
64 		fputc(x >> 16, f) != EOF &&
65 		fputc(x >> 24, f) != EOF
66 	;
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 	FLAC__bool ok = true;
72 	FLAC__StreamDecoder *decoder = 0;
73 	FLAC__StreamDecoderInitStatus init_status;
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 	if((decoder = FLAC__stream_decoder_new()) == NULL) {
87 		fprintf(stderr, "ERROR: allocating decoder\n");
88 		fclose(fout);
89 		return 1;
90 	}
91 
92 	(void)FLAC__stream_decoder_set_md5_checking(decoder, true);
93 
94 	init_status = FLAC__stream_decoder_init_file(decoder, argv[1], write_callback, metadata_callback, error_callback, /*client_data=*/fout);
95 	if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
96 		fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
97 		ok = false;
98 	}
99 
100 	if(ok) {
101 		ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
102 		fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
103 		fprintf(stderr, "   state: %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
104 	}
105 
106 	FLAC__stream_decoder_delete(decoder);
107 	fclose(fout);
108 
109 	return 0;
110 }
111 
write_callback(const FLAC__StreamDecoder * decoder,const FLAC__Frame * frame,const FLAC__int32 * const buffer[],void * client_data)112 FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
113 {
114 	FILE *f = (FILE*)client_data;
115 	const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps/8));
116 	size_t i;
117 
118 	(void)decoder;
119 
120 	if(total_samples == 0) {
121 		fprintf(stderr, "ERROR: this example only works for FLAC files that have a total_samples count in STREAMINFO\n");
122 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
123 	}
124 	if(channels != 2 || bps != 16) {
125 		fprintf(stderr, "ERROR: this example only supports 16bit stereo streams\n");
126 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
127 	}
128 	if(frame->header.channels != 2) {
129 		fprintf(stderr, "ERROR: This frame contains %u channels (should be 2)\n", frame->header.channels);
130 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
131 	}
132 	if(buffer [0] == NULL) {
133 		fprintf(stderr, "ERROR: buffer [0] is NULL\n");
134 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
135 	}
136 	if(buffer [1] == NULL) {
137 		fprintf(stderr, "ERROR: buffer [1] is NULL\n");
138 		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
139 	}
140 
141 	/* write WAVE header before we write the first frame */
142 	if(frame->header.number.sample_number == 0) {
143 		if(
144 			fwrite("RIFF", 1, 4, f) < 4 ||
145 			!write_little_endian_uint32(f, total_size + 36) ||
146 			fwrite("WAVEfmt ", 1, 8, f) < 8 ||
147 			!write_little_endian_uint32(f, 16) ||
148 			!write_little_endian_uint16(f, 1) ||
149 			!write_little_endian_uint16(f, (FLAC__uint16)channels) ||
150 			!write_little_endian_uint32(f, sample_rate) ||
151 			!write_little_endian_uint32(f, sample_rate * channels * (bps/8)) ||
152 			!write_little_endian_uint16(f, (FLAC__uint16)(channels * (bps/8))) || /* block align */
153 			!write_little_endian_uint16(f, (FLAC__uint16)bps) ||
154 			fwrite("data", 1, 4, f) < 4 ||
155 			!write_little_endian_uint32(f, total_size)
156 		) {
157 			fprintf(stderr, "ERROR: write error\n");
158 			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
159 		}
160 	}
161 
162 	/* write decoded PCM samples */
163 	for(i = 0; i < frame->header.blocksize; i++) {
164 		if(
165 			!write_little_endian_int16(f, (FLAC__int16)buffer[0][i]) ||  /* left channel */
166 			!write_little_endian_int16(f, (FLAC__int16)buffer[1][i])     /* right channel */
167 		) {
168 			fprintf(stderr, "ERROR: write error\n");
169 			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
170 		}
171 	}
172 
173 	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
174 }
175 
metadata_callback(const FLAC__StreamDecoder * decoder,const FLAC__StreamMetadata * metadata,void * client_data)176 void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
177 {
178 	(void)decoder, (void)client_data;
179 
180 	/* print some stats */
181 	if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
182 		/* save for later */
183 		total_samples = metadata->data.stream_info.total_samples;
184 		sample_rate = metadata->data.stream_info.sample_rate;
185 		channels = metadata->data.stream_info.channels;
186 		bps = metadata->data.stream_info.bits_per_sample;
187 
188 		fprintf(stderr, "sample rate    : %u Hz\n", sample_rate);
189 		fprintf(stderr, "channels       : %u\n", channels);
190 		fprintf(stderr, "bits per sample: %u\n", bps);
191 		fprintf(stderr, "total samples  : %" PRIu64 "\n", total_samples);
192 	}
193 }
194 
error_callback(const FLAC__StreamDecoder * decoder,FLAC__StreamDecoderErrorStatus status,void * client_data)195 void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
196 {
197 	(void)decoder, (void)client_data;
198 
199 	fprintf(stderr, "Got error callback: %s\n", FLAC__StreamDecoderErrorStatusString[status]);
200 }
201