xref: /aosp_15_r20/external/flac/examples/c/encode/file/main.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1*600f14f4SXin Li /* example_c_encode_file - Simple FLAC file encoder 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 encode a WAVE file to a FLAC
22*600f14f4SXin Li  * file.  It only supports 16-bit stereo files in canonical WAVE format.
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 <string.h>
35*600f14f4SXin Li #include "share/compat.h"
36*600f14f4SXin Li #include "FLAC/metadata.h"
37*600f14f4SXin Li #include "FLAC/stream_encoder.h"
38*600f14f4SXin Li 
39*600f14f4SXin Li static void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
40*600f14f4SXin Li 
41*600f14f4SXin Li #define READSIZE 1024
42*600f14f4SXin Li 
43*600f14f4SXin Li static unsigned total_samples = 0; /* can use a 32-bit number due to WAVE size limitations */
44*600f14f4SXin Li static FLAC__byte buffer[READSIZE/*samples*/ * 2/*bytes_per_sample*/ * 2/*channels*/]; /* we read the WAVE data into here */
45*600f14f4SXin Li static FLAC__int32 pcm[READSIZE/*samples*/ * 2/*channels*/];
46*600f14f4SXin Li 
main(int argc,char * argv[])47*600f14f4SXin Li int main(int argc, char *argv[])
48*600f14f4SXin Li {
49*600f14f4SXin Li 	FLAC__bool ok = true;
50*600f14f4SXin Li 	FLAC__StreamEncoder *encoder = 0;
51*600f14f4SXin Li 	FLAC__StreamEncoderInitStatus init_status;
52*600f14f4SXin Li 	FLAC__StreamMetadata *metadata[2];
53*600f14f4SXin Li 	FLAC__StreamMetadata_VorbisComment_Entry entry;
54*600f14f4SXin Li 	FILE *fin;
55*600f14f4SXin Li 	unsigned sample_rate = 0;
56*600f14f4SXin Li 	unsigned channels = 0;
57*600f14f4SXin Li 	unsigned bps = 0;
58*600f14f4SXin Li 
59*600f14f4SXin Li 	if(argc != 3) {
60*600f14f4SXin Li 		fprintf(stderr, "usage: %s infile.wav outfile.flac\n", argv[0]);
61*600f14f4SXin Li 		return 1;
62*600f14f4SXin Li 	}
63*600f14f4SXin Li 
64*600f14f4SXin Li 	if((fin = fopen(argv[1], "rb")) == NULL) {
65*600f14f4SXin Li 		fprintf(stderr, "ERROR: opening %s for output\n", argv[1]);
66*600f14f4SXin Li 		return 1;
67*600f14f4SXin Li 	}
68*600f14f4SXin Li 
69*600f14f4SXin Li 	/* read wav header and validate it */
70*600f14f4SXin Li 	if(
71*600f14f4SXin Li 		fread(buffer, 1, 44, fin) != 44 ||
72*600f14f4SXin Li 		memcmp(buffer, "RIFF", 4) ||
73*600f14f4SXin Li 		memcmp(buffer+8, "WAVEfmt \020\000\000\000\001\000\002\000", 16) ||
74*600f14f4SXin Li 		memcmp(buffer+32, "\004\000\020\000data", 8)
75*600f14f4SXin Li 	) {
76*600f14f4SXin Li 		fprintf(stderr, "ERROR: invalid/unsupported WAVE file, only 16bps stereo WAVE in canonical form allowed\n");
77*600f14f4SXin Li 		fclose(fin);
78*600f14f4SXin Li 		return 1;
79*600f14f4SXin Li 	}
80*600f14f4SXin Li 	sample_rate = ((((((unsigned)buffer[27] << 8) | buffer[26]) << 8) | buffer[25]) << 8) | buffer[24];
81*600f14f4SXin Li 	channels = 2;
82*600f14f4SXin Li 	bps = 16;
83*600f14f4SXin Li 	total_samples = (((((((unsigned)buffer[43] << 8) | buffer[42]) << 8) | buffer[41]) << 8) | buffer[40]) / 4;
84*600f14f4SXin Li 
85*600f14f4SXin Li 	/* allocate the encoder */
86*600f14f4SXin Li 	if((encoder = FLAC__stream_encoder_new()) == NULL) {
87*600f14f4SXin Li 		fprintf(stderr, "ERROR: allocating encoder\n");
88*600f14f4SXin Li 		fclose(fin);
89*600f14f4SXin Li 		return 1;
90*600f14f4SXin Li 	}
91*600f14f4SXin Li 
92*600f14f4SXin Li 	ok &= FLAC__stream_encoder_set_verify(encoder, true);
93*600f14f4SXin Li 	ok &= FLAC__stream_encoder_set_compression_level(encoder, 5);
94*600f14f4SXin Li 	ok &= FLAC__stream_encoder_set_channels(encoder, channels);
95*600f14f4SXin Li 	ok &= FLAC__stream_encoder_set_bits_per_sample(encoder, bps);
96*600f14f4SXin Li 	ok &= FLAC__stream_encoder_set_sample_rate(encoder, sample_rate);
97*600f14f4SXin Li 	ok &= FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples);
98*600f14f4SXin Li 
99*600f14f4SXin Li 	/* now add some metadata; we'll add some tags and a padding block */
100*600f14f4SXin Li 	if(ok) {
101*600f14f4SXin Li 		if(
102*600f14f4SXin Li 			(metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL ||
103*600f14f4SXin Li 			(metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL ||
104*600f14f4SXin Li 			/* there are many tag (vorbiscomment) functions but these are convenient for this particular use: */
105*600f14f4SXin Li 			!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", "Some Artist") ||
106*600f14f4SXin Li 			!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false) || /* copy=false: let metadata object take control of entry's allocated string */
107*600f14f4SXin Li 			!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "YEAR", "1984") ||
108*600f14f4SXin Li 			!FLAC__metadata_object_vorbiscomment_append_comment(metadata[0], entry, /*copy=*/false)
109*600f14f4SXin Li 		) {
110*600f14f4SXin Li 			fprintf(stderr, "ERROR: out of memory or tag error\n");
111*600f14f4SXin Li 			ok = false;
112*600f14f4SXin Li 		} else {
113*600f14f4SXin Li 
114*600f14f4SXin Li 			metadata[1]->length = 1234; /* set the padding length */
115*600f14f4SXin Li 
116*600f14f4SXin Li 			ok = FLAC__stream_encoder_set_metadata(encoder, metadata, 2);
117*600f14f4SXin Li 
118*600f14f4SXin Li                 }
119*600f14f4SXin Li 	}
120*600f14f4SXin Li 
121*600f14f4SXin Li 	/* initialize encoder */
122*600f14f4SXin Li 	if(ok) {
123*600f14f4SXin Li 		init_status = FLAC__stream_encoder_init_file(encoder, argv[2], progress_callback, /*client_data=*/NULL);
124*600f14f4SXin Li 		if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
125*600f14f4SXin Li 			fprintf(stderr, "ERROR: initializing encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]);
126*600f14f4SXin Li 			ok = false;
127*600f14f4SXin Li 		}
128*600f14f4SXin Li 	}
129*600f14f4SXin Li 
130*600f14f4SXin Li 	/* read blocks of samples from WAVE file and feed to encoder */
131*600f14f4SXin Li 	if(ok) {
132*600f14f4SXin Li 		size_t left = (size_t)total_samples;
133*600f14f4SXin Li 		while(ok && left) {
134*600f14f4SXin Li 			size_t need = (left>READSIZE? (size_t)READSIZE : (size_t)left);
135*600f14f4SXin Li 			if(fread(buffer, channels*(bps/8), need, fin) != need) {
136*600f14f4SXin Li 				fprintf(stderr, "ERROR: reading from WAVE file\n");
137*600f14f4SXin Li 				ok = false;
138*600f14f4SXin Li 			}
139*600f14f4SXin Li 			else {
140*600f14f4SXin Li 				/* convert the packed little-endian 16-bit PCM samples from WAVE into an interleaved FLAC__int32 buffer for libFLAC */
141*600f14f4SXin Li 				size_t i;
142*600f14f4SXin Li 				for(i = 0; i < need*channels; i++) {
143*600f14f4SXin Li 					/* inefficient but simple and works on big- or little-endian machines */
144*600f14f4SXin Li 					pcm[i] = (FLAC__int32)(((FLAC__int16)(FLAC__int8)buffer[2*i+1] << 8) | (FLAC__int16)buffer[2*i]);
145*600f14f4SXin Li 				}
146*600f14f4SXin Li 				/* feed samples to encoder */
147*600f14f4SXin Li 				ok = FLAC__stream_encoder_process_interleaved(encoder, pcm, need);
148*600f14f4SXin Li 			}
149*600f14f4SXin Li 			left -= need;
150*600f14f4SXin Li 		}
151*600f14f4SXin Li 	}
152*600f14f4SXin Li 
153*600f14f4SXin Li 	ok &= FLAC__stream_encoder_finish(encoder);
154*600f14f4SXin Li 
155*600f14f4SXin Li 	fprintf(stderr, "encoding: %s\n", ok? "succeeded" : "FAILED");
156*600f14f4SXin Li 	fprintf(stderr, "   state: %s\n", FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder)]);
157*600f14f4SXin Li 
158*600f14f4SXin Li 	/* now that encoding is finished, the metadata can be freed */
159*600f14f4SXin Li 	FLAC__metadata_object_delete(metadata[0]);
160*600f14f4SXin Li 	FLAC__metadata_object_delete(metadata[1]);
161*600f14f4SXin Li 
162*600f14f4SXin Li 	FLAC__stream_encoder_delete(encoder);
163*600f14f4SXin Li 	fclose(fin);
164*600f14f4SXin Li 
165*600f14f4SXin Li 	return 0;
166*600f14f4SXin Li }
167*600f14f4SXin Li 
progress_callback(const FLAC__StreamEncoder * encoder,FLAC__uint64 bytes_written,FLAC__uint64 samples_written,unsigned frames_written,unsigned total_frames_estimate,void * client_data)168*600f14f4SXin Li void progress_callback(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
169*600f14f4SXin Li {
170*600f14f4SXin Li 	(void)encoder, (void)client_data;
171*600f14f4SXin Li 
172*600f14f4SXin Li 	fprintf(stderr, "wrote %" PRIu64 " bytes, %" PRIu64 "/%u samples, %u/%u frames\n", bytes_written, samples_written, total_samples, frames_written, total_frames_estimate);
173*600f14f4SXin Li }
174