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