~ubuntu-branches/ubuntu/precise/flac/precise-updates

« back to all changes in this revision

Viewing changes to src/test_libFLAC++/file_utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-05-29 22:56:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529225636-ljeff8xxip09qaap
Tags: 1.1.4-1
* New upstream release. closes: #405167, #411311
  - libOggFLAC and libOggFLAC++ have been merged into libFLAC, so
    remove their corresponding packages.
  - Because of the API changes required to effect the above, there has
    been yet another soname bump. libflac7 -> libflac8 and
    libflac++5 -> libflac++6. Emails have been dispatched to the
    maintainers of dependent packages.
* Some notes on patches that were removed:
  - 02_stdin_stdout, 06_manpage_mention_utf8_convert: merged upstream
  - 08_manpage_warnings: Upstream has changed the manpage so it defintely
    can't fit in in 80 cols, so just forget about it. We'll live.
  - 05_eof_warnings_are_errors: Upstream decided to add a -w option to
    flac to treat all warnings as errors. I am going to defer to that
    for now, but if people think it's stupid let me know and I'll port
    the patch forward.
  - 04_stack_smasher: was a backport from 1.1.3, so it's obsolete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* test_libFLAC - Unit tester for libFLAC
2
 
 * Copyright (C) 2002,2003,2004,2005  Josh Coalson
3
 
 *
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 "FLAC/stream_encoder.h"
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <sys/stat.h> /* for stat() */
25
 
 
26
 
#ifdef min
27
 
#undef min
28
 
#endif
29
 
#define min(a,b) ((a)<(b)?(a):(b))
30
 
 
31
 
#ifdef FLAC__VALGRIND_TESTING
32
 
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
33
 
{
34
 
        size_t ret = fwrite(ptr, size, nmemb, stream);
35
 
        if(!ferror(stream))
36
 
                fflush(stream);
37
 
        return ret;
38
 
}
39
 
#else
40
 
#define local__fwrite fwrite
41
 
#endif
42
 
 
43
 
typedef struct {
44
 
        FILE *file;
45
 
} encoder_client_struct;
46
 
 
47
 
static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
48
 
{
49
 
        encoder_client_struct *ecd = (encoder_client_struct*)client_data;
50
 
 
51
 
        (void)encoder, (void)samples, (void)current_frame;
52
 
 
53
 
        if(local__fwrite(buffer, 1, bytes, ecd->file) != bytes)
54
 
                return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
55
 
        else
56
 
                return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
57
 
}
58
 
 
59
 
static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
60
 
{
61
 
        (void)encoder, (void)metadata, (void)client_data;
62
 
}
63
 
 
64
 
FLAC__bool file_utils__generate_flacfile(const char *output_filename, unsigned *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
65
 
{
66
 
        FLAC__int32 samples[1024];
67
 
        FLAC__StreamEncoder *encoder;
68
 
        encoder_client_struct encoder_client_data;
69
 
        unsigned i, n;
70
 
 
71
 
        FLAC__ASSERT(0 != output_filename);
72
 
        FLAC__ASSERT(0 != streaminfo);
73
 
        FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
74
 
        FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
75
 
 
76
 
        if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
77
 
                return false;
78
 
 
79
 
        encoder = FLAC__stream_encoder_new();
80
 
        if(0 == encoder) {
81
 
                fclose(encoder_client_data.file);
82
 
                return false;
83
 
        }
84
 
 
85
 
        FLAC__stream_encoder_set_verify(encoder, true);
86
 
        FLAC__stream_encoder_set_streamable_subset(encoder, true);
87
 
        FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
88
 
        FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
89
 
        FLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
90
 
        FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
91
 
        FLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
92
 
        FLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
93
 
        FLAC__stream_encoder_set_max_lpc_order(encoder, 0);
94
 
        FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
95
 
        FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
96
 
        FLAC__stream_encoder_set_do_escape_coding(encoder, false);
97
 
        FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
98
 
        FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
99
 
        FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
100
 
        FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
101
 
        FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
102
 
        FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
103
 
        FLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
104
 
        FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_);
105
 
        FLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
106
 
 
107
 
        if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK) {
108
 
                fclose(encoder_client_data.file);
109
 
                return false;
110
 
        }
111
 
 
112
 
        /* init the dummy sample buffer */
113
 
        for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
114
 
                samples[i] = i & 7;
115
 
 
116
 
        while(length > 0) {
117
 
                n = min(length, sizeof(samples) / sizeof(FLAC__int32));
118
 
 
119
 
                if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
120
 
                        fclose(encoder_client_data.file);
121
 
                        return false;
122
 
                }
123
 
 
124
 
                length -= n;
125
 
        }
126
 
 
127
 
        FLAC__stream_encoder_finish(encoder);
128
 
 
129
 
        fclose(encoder_client_data.file);
130
 
 
131
 
        FLAC__stream_encoder_delete(encoder);
132
 
 
133
 
        if(0 != output_filesize) {
134
 
                struct stat filestats;
135
 
 
136
 
                if(stat(output_filename, &filestats) != 0)
137
 
                        return false;
138
 
                else
139
 
                        *output_filesize = (unsigned)filestats.st_size;
140
 
        }
141
 
 
142
 
        return true;
143
 
}