~ubuntu-branches/ubuntu/trusty/flac/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libOggFLAC/ogg_encoder_aspect.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc 'HE' Brockschmidt
  • Date: 2008-03-16 18:02:56 UTC
  • mfrom: (1.1.5 upstream) (8.1.2 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080316180256-qhf3wk704rp165pm
Tags: 1.2.1-1.2
* Non-maintainer upload.
* Fix gcc-4.3 FTBFS, patch by KiBi (Closes: #455304)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* libOggFLAC - Free Lossless Audio Codec + Ogg library
2
 
 * Copyright (C) 2002,2003,2004,2005  Josh Coalson
3
 
 *
4
 
 * Redistribution and use in source and binary forms, with or without
5
 
 * modification, are permitted provided that the following conditions
6
 
 * are met:
7
 
 *
8
 
 * - Redistributions of source code must retain the above copyright
9
 
 * notice, this list of conditions and the following disclaimer.
10
 
 *
11
 
 * - Redistributions in binary form must reproduce the above copyright
12
 
 * notice, this list of conditions and the following disclaimer in the
13
 
 * documentation and/or other materials provided with the distribution.
14
 
 *
15
 
 * - Neither the name of the Xiph.org Foundation nor the names of its
16
 
 * contributors may be used to endorse or promote products derived from
17
 
 * this software without specific prior written permission.
18
 
 *
19
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
 
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
 
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23
 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 
 */
31
 
 
32
 
#include <string.h> /* for memset() */
33
 
#include "FLAC/assert.h"
34
 
#include "private/ogg_encoder_aspect.h"
35
 
#include "private/ogg_mapping.h"
36
 
 
37
 
static const FLAC__byte OggFLAC__MAPPING_VERSION_MAJOR = 1;
38
 
static const FLAC__byte OggFLAC__MAPPING_VERSION_MINOR = 0;
39
 
 
40
 
/***********************************************************************
41
 
 *
42
 
 * Public class methods
43
 
 *
44
 
 ***********************************************************************/
45
 
 
46
 
FLAC__bool OggFLAC__ogg_encoder_aspect_init(OggFLAC__OggEncoderAspect *aspect)
47
 
{
48
 
        /* we will determine the serial number later if necessary */
49
 
        if(ogg_stream_init(&aspect->stream_state, aspect->serial_number) != 0)
50
 
                return false;
51
 
 
52
 
        aspect->seen_magic = false;
53
 
        aspect->is_first_packet = true;
54
 
        aspect->samples_written = 0;
55
 
 
56
 
        return true;
57
 
}
58
 
 
59
 
void OggFLAC__ogg_encoder_aspect_finish(OggFLAC__OggEncoderAspect *aspect)
60
 
{
61
 
        (void)ogg_stream_clear(&aspect->stream_state);
62
 
        /*@@@ what aobut the page? */
63
 
}
64
 
 
65
 
void OggFLAC__ogg_encoder_aspect_set_serial_number(OggFLAC__OggEncoderAspect *aspect, long value)
66
 
{
67
 
        aspect->serial_number = value;
68
 
}
69
 
 
70
 
FLAC__bool OggFLAC__ogg_encoder_aspect_set_num_metadata(OggFLAC__OggEncoderAspect *aspect, unsigned value)
71
 
{
72
 
        if(value < (1u << OggFLAC__MAPPING_NUM_HEADERS_LEN)) {
73
 
                aspect->num_metadata = value;
74
 
                return true;
75
 
        }
76
 
        else
77
 
                return false;
78
 
}
79
 
 
80
 
void OggFLAC__ogg_encoder_aspect_set_defaults(OggFLAC__OggEncoderAspect *aspect)
81
 
{
82
 
        aspect->serial_number = 0;
83
 
        aspect->num_metadata = 0;
84
 
}
85
 
 
86
 
/*
87
 
 * The basic FLAC -> Ogg mapping goes like this:
88
 
 *
89
 
 * - 'fLaC' magic and STREAMINFO block get combined into the first
90
 
 *   packet.  The packet is prefixed with
91
 
 *   + the one-byte packet type 0x7F
92
 
 *   + 'FLAC' magic
93
 
 *   + the 2 byte Ogg FLAC mapping version number
94
 
 *   + tne 2 byte big-endian # of header packets
95
 
 * - The first packet is flushed to the first page.
96
 
 * - Each subsequent metadata block goes into its own packet.
97
 
 * - Each metadata packet is flushed to page (this is not required,
98
 
 *   the mapping only requires that a flush must occur after all
99
 
 *   metadata is written).
100
 
 * - Each subsequent FLAC audio frame goes into its own packet.
101
 
 *
102
 
 * WATCHOUT:
103
 
 * This depends on the behavior of FLAC__StreamEncoder that we get a
104
 
 * separate write callback for the fLaC magic, and then separate write
105
 
 * callbacks for each metadata block and audio frame.
106
 
 */
107
 
FLAC__StreamEncoderWriteStatus OggFLAC__ogg_encoder_aspect_write_callback_wrapper(OggFLAC__OggEncoderAspect *aspect, const FLAC__uint64 total_samples_estimate, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, OggFLAC__OggEncoderAspectWriteCallbackProxy write_callback, void *encoder, void *client_data)
108
 
{
109
 
        /* WATCHOUT:
110
 
         * This depends on the behavior of FLAC__StreamEncoder that 'samples'
111
 
         * will be 0 for metadata writes.
112
 
         */
113
 
        const FLAC__bool is_metadata = (samples == 0);
114
 
 
115
 
        /*
116
 
         * Treat fLaC magic packet specially.  We will note when we see it, then
117
 
         * wait until we get the STREAMINFO and prepend it in that packet
118
 
         */
119
 
        if(aspect->seen_magic) {
120
 
                ogg_packet packet;
121
 
                FLAC__byte synthetic_first_packet_body[
122
 
                        OggFLAC__MAPPING_PACKET_TYPE_LENGTH +
123
 
                        OggFLAC__MAPPING_MAGIC_LENGTH +
124
 
                        OggFLAC__MAPPING_VERSION_MAJOR_LENGTH +
125
 
                        OggFLAC__MAPPING_VERSION_MINOR_LENGTH +
126
 
                        OggFLAC__MAPPING_NUM_HEADERS_LENGTH +
127
 
                        FLAC__STREAM_SYNC_LENGTH +
128
 
                        FLAC__STREAM_METADATA_HEADER_LENGTH +
129
 
                        FLAC__STREAM_METADATA_STREAMINFO_LENGTH
130
 
                ];
131
 
 
132
 
                memset(&packet, 0, sizeof(packet));
133
 
                packet.granulepos = aspect->samples_written + samples;
134
 
 
135
 
                if(aspect->is_first_packet) {
136
 
                        FLAC__byte *b = synthetic_first_packet_body;
137
 
                        if(bytes != FLAC__STREAM_METADATA_HEADER_LENGTH + FLAC__STREAM_METADATA_STREAMINFO_LENGTH) {
138
 
                                /*
139
 
                                 * If we get here, our assumption about the way write callbacks happen
140
 
                                 * explained above is wrong
141
 
                                 */
142
 
                                FLAC__ASSERT(0);
143
 
                                return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
144
 
                        }
145
 
                        /* add first header packet type */
146
 
                        *b = OggFLAC__MAPPING_FIRST_HEADER_PACKET_TYPE;
147
 
                        b += OggFLAC__MAPPING_PACKET_TYPE_LENGTH;
148
 
                        /* add 'FLAC' mapping magic */
149
 
                        memcpy(b, OggFLAC__MAPPING_MAGIC, OggFLAC__MAPPING_MAGIC_LENGTH);
150
 
                        b += OggFLAC__MAPPING_MAGIC_LENGTH;
151
 
                        /* add Ogg FLAC mapping major version number */
152
 
                        memcpy(b, &OggFLAC__MAPPING_VERSION_MAJOR, OggFLAC__MAPPING_VERSION_MAJOR_LENGTH);
153
 
                        b += OggFLAC__MAPPING_VERSION_MAJOR_LENGTH;
154
 
                        /* add Ogg FLAC mapping minor version number */
155
 
                        memcpy(b, &OggFLAC__MAPPING_VERSION_MINOR, OggFLAC__MAPPING_VERSION_MINOR_LENGTH);
156
 
                        b += OggFLAC__MAPPING_VERSION_MINOR_LENGTH;
157
 
                        /* add number of header packets */
158
 
                        *b = (FLAC__byte)(aspect->num_metadata >> 8);
159
 
                        b++;
160
 
                        *b = (FLAC__byte)(aspect->num_metadata);
161
 
                        b++;
162
 
                        /* add native FLAC 'fLaC' magic */
163
 
                        memcpy(b, FLAC__STREAM_SYNC_STRING, FLAC__STREAM_SYNC_LENGTH);
164
 
                        b += FLAC__STREAM_SYNC_LENGTH;
165
 
                        /* add STREAMINFO */
166
 
                        memcpy(b, buffer, bytes);
167
 
                        FLAC__ASSERT(b + bytes - synthetic_first_packet_body == sizeof(synthetic_first_packet_body));
168
 
                        packet.packet = (unsigned char *)synthetic_first_packet_body;
169
 
                        packet.bytes = sizeof(synthetic_first_packet_body);
170
 
 
171
 
                        packet.b_o_s = 1;
172
 
                        aspect->is_first_packet = false;
173
 
                }
174
 
                else {
175
 
                        packet.packet = (unsigned char *)buffer;
176
 
                        packet.bytes = bytes;
177
 
                }
178
 
 
179
 
                if(total_samples_estimate > 0 && total_samples_estimate == aspect->samples_written + samples)
180
 
                        packet.e_o_s = 1;
181
 
 
182
 
                if(ogg_stream_packetin(&aspect->stream_state, &packet) != 0)
183
 
                        return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
184
 
 
185
 
                /*@@@ can't figure out a way to pass a useful number for 'samples' to the write_callback, so we'll just pass 0 */
186
 
                if(is_metadata) {
187
 
                        while(ogg_stream_flush(&aspect->stream_state, &aspect->page) != 0) {
188
 
                                if(write_callback(encoder, aspect->page.header, aspect->page.header_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
189
 
                                        return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
190
 
                                if(write_callback(encoder, aspect->page.body, aspect->page.body_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
191
 
                                        return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
192
 
                        }
193
 
                }
194
 
                else {
195
 
                        while(ogg_stream_pageout(&aspect->stream_state, &aspect->page) != 0) {
196
 
                                if(write_callback(encoder, aspect->page.header, aspect->page.header_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
197
 
                                        return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
198
 
                                if(write_callback(encoder, aspect->page.body, aspect->page.body_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
199
 
                                        return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
200
 
                        }
201
 
                }
202
 
        }
203
 
        else if(is_metadata && current_frame == 0 && samples == 0 && bytes == 4 && 0 == memcmp(buffer, FLAC__STREAM_SYNC_STRING, sizeof(FLAC__STREAM_SYNC_STRING))) {
204
 
                aspect->seen_magic = true;
205
 
        }
206
 
        else {
207
 
                /*
208
 
                 * If we get here, our assumption about the way write callbacks happen
209
 
                 * explained above is wrong
210
 
                 */
211
 
                FLAC__ASSERT(0);
212
 
                return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
213
 
        }
214
 
 
215
 
        aspect->samples_written += samples;
216
 
 
217
 
        return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
218
 
}