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