blob: 40816f5aec51f9ebaeed7ccc6696596b84a35150 [file] [log] [blame]
Josh Coalsona86f8702002-08-20 04:03:24 +00001/* test_libOggFLAC - Unit tester for libOggFLAC
Josh Coalson95643902004-01-17 04:14:43 +00002 * Copyright (C) 2002,2003,2004 Josh Coalson
Josh Coalsona86f8702002-08-20 04:03:24 +00003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include "file_utils.h"
20#include "FLAC/assert.h"
21#include "OggFLAC/stream_encoder.h"
22#include <stdio.h>
23#include <stdlib.h>
Josh Coalson5843fc22002-10-30 06:18:13 +000024#include <sys/stat.h> /* for stat() */
Josh Coalsona86f8702002-08-20 04:03:24 +000025
26#ifdef min
27#undef min
28#endif
29#define min(a,b) ((a)<(b)?(a):(b))
30
Josh Coalson8304c8f2002-09-04 07:52:58 +000031const long file_utils__serial_number = 12345;
32
Josh Coalsonf91251d2002-12-28 07:04:49 +000033#ifdef FLAC__VALGRIND_TESTING
34static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
35{
36 size_t ret = fwrite(ptr, size, nmemb, stream);
37 if(!ferror(stream))
38 fflush(stream);
39 return ret;
40}
41#else
42#define local__fwrite fwrite
43#endif
44
Josh Coalsona86f8702002-08-20 04:03:24 +000045typedef struct {
46 FILE *file;
47} encoder_client_struct;
48
49static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
50{
51 encoder_client_struct *ecd = (encoder_client_struct*)client_data;
52
53 (void)encoder, (void)samples, (void)current_frame;
54
Josh Coalsonf91251d2002-12-28 07:04:49 +000055 if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
Josh Coalsona86f8702002-08-20 04:03:24 +000056 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
57 else
58 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
59}
60
Josh Coalsone7e56752003-09-25 04:08:46 +000061static void encoder_metadata_callback_(const OggFLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
62{
63 (void)encoder, (void)metadata, (void)client_data;
64}
65
Josh Coalsona86f8702002-08-20 04:03:24 +000066FLAC__bool file_utils__generate_oggflacfile(const char *output_filename, unsigned *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
67{
68 FLAC__int32 samples[1024];
69 OggFLAC__StreamEncoder *encoder;
70 encoder_client_struct encoder_client_data;
71 unsigned i, n;
72
73 FLAC__ASSERT(0 != output_filename);
74 FLAC__ASSERT(0 != streaminfo);
75 FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
76 FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
77
78 if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
79 return false;
80
81 encoder = OggFLAC__stream_encoder_new();
82 if(0 == encoder) {
83 fclose(encoder_client_data.file);
84 return false;
85 }
86
Josh Coalson8304c8f2002-09-04 07:52:58 +000087 OggFLAC__stream_encoder_set_serial_number(encoder, file_utils__serial_number);
Josh Coalsona86f8702002-08-20 04:03:24 +000088 OggFLAC__stream_encoder_set_verify(encoder, true);
89 OggFLAC__stream_encoder_set_streamable_subset(encoder, true);
90 OggFLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
91 OggFLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
92 OggFLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
93 OggFLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
94 OggFLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
95 OggFLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
96 OggFLAC__stream_encoder_set_max_lpc_order(encoder, 0);
97 OggFLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
98 OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
99 OggFLAC__stream_encoder_set_do_escape_coding(encoder, false);
100 OggFLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
101 OggFLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
102 OggFLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
103 OggFLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
104 OggFLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
105 OggFLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
106 OggFLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
Josh Coalsone7e56752003-09-25 04:08:46 +0000107 OggFLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_);
Josh Coalsona86f8702002-08-20 04:03:24 +0000108 OggFLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
109
110 if(OggFLAC__stream_encoder_init(encoder) != OggFLAC__STREAM_ENCODER_OK) {
111 fclose(encoder_client_data.file);
112 return false;
113 }
114
115 /* init the dummy sample buffer */
116 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
117 samples[i] = i & 7;
118
119 while(length > 0) {
120 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
121
122 if(!OggFLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
123 fclose(encoder_client_data.file);
124 return false;
125 }
126
127 length -= n;
128 }
129
130 OggFLAC__stream_encoder_finish(encoder);
131
132 fclose(encoder_client_data.file);
133
134 OggFLAC__stream_encoder_delete(encoder);
135
136 if(0 != output_filesize) {
137 struct stat filestats;
138
139 if(stat(output_filename, &filestats) != 0)
140 return false;
141 else
142 *output_filesize = (unsigned)filestats.st_size;
143 }
144
145 return true;
146}