1
/* libOggFLAC - Free Lossless Audio Codec + Ogg library
2
* Copyright (C) 2002,2003,2004,2005 Josh Coalson
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
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.
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.
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.
32
#include <string.h> /* for memset() */
33
#include "FLAC/assert.h"
34
#include "private/ogg_encoder_aspect.h"
35
#include "private/ogg_mapping.h"
37
static const FLAC__byte OggFLAC__MAPPING_VERSION_MAJOR = 1;
38
static const FLAC__byte OggFLAC__MAPPING_VERSION_MINOR = 0;
40
/***********************************************************************
42
* Public class methods
44
***********************************************************************/
46
FLAC__bool OggFLAC__ogg_encoder_aspect_init(OggFLAC__OggEncoderAspect *aspect)
48
/* we will determine the serial number later if necessary */
49
if(ogg_stream_init(&aspect->stream_state, aspect->serial_number) != 0)
52
aspect->seen_magic = false;
53
aspect->is_first_packet = true;
54
aspect->samples_written = 0;
59
void OggFLAC__ogg_encoder_aspect_finish(OggFLAC__OggEncoderAspect *aspect)
61
(void)ogg_stream_clear(&aspect->stream_state);
62
/*@@@ what aobut the page? */
65
void OggFLAC__ogg_encoder_aspect_set_serial_number(OggFLAC__OggEncoderAspect *aspect, long value)
67
aspect->serial_number = value;
70
FLAC__bool OggFLAC__ogg_encoder_aspect_set_num_metadata(OggFLAC__OggEncoderAspect *aspect, unsigned value)
72
if(value < (1u << OggFLAC__MAPPING_NUM_HEADERS_LEN)) {
73
aspect->num_metadata = value;
80
void OggFLAC__ogg_encoder_aspect_set_defaults(OggFLAC__OggEncoderAspect *aspect)
82
aspect->serial_number = 0;
83
aspect->num_metadata = 0;
87
* The basic FLAC -> Ogg mapping goes like this:
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
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.
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.
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)
110
* This depends on the behavior of FLAC__StreamEncoder that 'samples'
111
* will be 0 for metadata writes.
113
const FLAC__bool is_metadata = (samples == 0);
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
119
if(aspect->seen_magic) {
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
132
memset(&packet, 0, sizeof(packet));
133
packet.granulepos = aspect->samples_written + samples;
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) {
139
* If we get here, our assumption about the way write callbacks happen
140
* explained above is wrong
143
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
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);
160
*b = (FLAC__byte)(aspect->num_metadata);
162
/* add native FLAC 'fLaC' magic */
163
memcpy(b, FLAC__STREAM_SYNC_STRING, FLAC__STREAM_SYNC_LENGTH);
164
b += FLAC__STREAM_SYNC_LENGTH;
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);
172
aspect->is_first_packet = false;
175
packet.packet = (unsigned char *)buffer;
176
packet.bytes = bytes;
179
if(total_samples_estimate > 0 && total_samples_estimate == aspect->samples_written + samples)
182
if(ogg_stream_packetin(&aspect->stream_state, &packet) != 0)
183
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
185
/*@@@ can't figure out a way to pass a useful number for 'samples' to the write_callback, so we'll just pass 0 */
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;
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;
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;
208
* If we get here, our assumption about the way write callbacks happen
209
* explained above is wrong
212
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
215
aspect->samples_written += samples;
217
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;