annotate src/flac-1.2.1/examples/c/decode/file/main.c @ 1:05aa0afa9217

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