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

« back to all changes in this revision

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