1 /* libFLAC - Free Lossless Audio Codec
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2022 Xiph.Org Foundation
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36
37 #include <string.h> /* for memcpy() */
38 #include "FLAC/assert.h"
39 #include "private/ogg_decoder_aspect.h"
40 #include "private/ogg_mapping.h"
41 #include "private/macros.h"
42
43
44 /***********************************************************************
45 *
46 * Public class methods
47 *
48 ***********************************************************************/
49
FLAC__ogg_decoder_aspect_init(FLAC__OggDecoderAspect * aspect)50 FLAC__bool FLAC__ogg_decoder_aspect_init(FLAC__OggDecoderAspect *aspect)
51 {
52 /* we will determine the serial number later if necessary */
53 if(ogg_stream_init(&aspect->stream_state, aspect->serial_number) != 0)
54 return false;
55
56 if(ogg_sync_init(&aspect->sync_state) != 0)
57 return false;
58
59 aspect->version_major = ~(0u);
60 aspect->version_minor = ~(0u);
61
62 aspect->need_serial_number = aspect->use_first_serial_number;
63
64 aspect->end_of_stream = false;
65 aspect->have_working_page = false;
66
67 return true;
68 }
69
FLAC__ogg_decoder_aspect_finish(FLAC__OggDecoderAspect * aspect)70 void FLAC__ogg_decoder_aspect_finish(FLAC__OggDecoderAspect *aspect)
71 {
72 (void)ogg_sync_clear(&aspect->sync_state);
73 (void)ogg_stream_clear(&aspect->stream_state);
74 }
75
FLAC__ogg_decoder_aspect_set_serial_number(FLAC__OggDecoderAspect * aspect,long value)76 void FLAC__ogg_decoder_aspect_set_serial_number(FLAC__OggDecoderAspect *aspect, long value)
77 {
78 aspect->use_first_serial_number = false;
79 aspect->serial_number = value;
80 }
81
FLAC__ogg_decoder_aspect_set_defaults(FLAC__OggDecoderAspect * aspect)82 void FLAC__ogg_decoder_aspect_set_defaults(FLAC__OggDecoderAspect *aspect)
83 {
84 aspect->use_first_serial_number = true;
85 }
86
FLAC__ogg_decoder_aspect_flush(FLAC__OggDecoderAspect * aspect)87 void FLAC__ogg_decoder_aspect_flush(FLAC__OggDecoderAspect *aspect)
88 {
89 (void)ogg_stream_reset(&aspect->stream_state);
90 (void)ogg_sync_reset(&aspect->sync_state);
91 aspect->end_of_stream = false;
92 aspect->have_working_page = false;
93 }
94
FLAC__ogg_decoder_aspect_reset(FLAC__OggDecoderAspect * aspect)95 void FLAC__ogg_decoder_aspect_reset(FLAC__OggDecoderAspect *aspect)
96 {
97 FLAC__ogg_decoder_aspect_flush(aspect);
98
99 if(aspect->use_first_serial_number)
100 aspect->need_serial_number = true;
101 }
102
FLAC__ogg_decoder_aspect_read_callback_wrapper(FLAC__OggDecoderAspect * aspect,FLAC__byte buffer[],size_t * bytes,FLAC__OggDecoderAspectReadCallbackProxy read_callback,const FLAC__StreamDecoder * decoder,void * client_data)103 FLAC__OggDecoderAspectReadStatus FLAC__ogg_decoder_aspect_read_callback_wrapper(FLAC__OggDecoderAspect *aspect, FLAC__byte buffer[], size_t *bytes, FLAC__OggDecoderAspectReadCallbackProxy read_callback, const FLAC__StreamDecoder *decoder, void *client_data)
104 {
105 static const size_t OGG_BYTES_CHUNK = 8192;
106 const size_t bytes_requested = *bytes;
107
108 /*
109 * The FLAC decoding API uses pull-based reads, whereas Ogg decoding
110 * is push-based. In libFLAC, when you ask to decode a frame, the
111 * decoder will eventually call the read callback to supply some data,
112 * but how much it asks for depends on how much free space it has in
113 * its internal buffer. It does not try to grow its internal buffer
114 * to accommodate a whole frame because then the internal buffer size
115 * could not be limited, which is necessary in embedded applications.
116 *
117 * Ogg however grows its internal buffer until a whole page is present;
118 * only then can you get decoded data out. So we can't just ask for
119 * the same number of bytes from Ogg, then pass what's decoded down to
120 * libFLAC. If what libFLAC is asking for will not contain a whole
121 * page, then we will get no data from ogg_sync_pageout(), and at the
122 * same time cannot just read more data from the client for the purpose
123 * of getting a whole decoded page because the decoded size might be
124 * larger than libFLAC's internal buffer.
125 *
126 * Instead, whenever this read callback wrapper is called, we will
127 * continually request data from the client until we have at least one
128 * page, and manage pages internally so that we can send pieces of
129 * pages down to libFLAC in such a way that we obey its size
130 * requirement. To limit the amount of callbacks, we will always try
131 * to read in enough pages to return the full number of bytes
132 * requested.
133 */
134 *bytes = 0;
135 while (*bytes < bytes_requested && !aspect->end_of_stream) {
136 if (aspect->have_working_page) {
137 if (aspect->have_working_packet) {
138 size_t n = bytes_requested - *bytes;
139 if ((size_t)aspect->working_packet.bytes <= n) {
140 /* the rest of the packet will fit in the buffer */
141 n = aspect->working_packet.bytes;
142 memcpy(buffer, aspect->working_packet.packet, n);
143 *bytes += n;
144 buffer += n;
145 aspect->have_working_packet = false;
146 }
147 else {
148 /* only n bytes of the packet will fit in the buffer */
149 memcpy(buffer, aspect->working_packet.packet, n);
150 *bytes += n;
151 buffer += n;
152 aspect->working_packet.packet += n;
153 aspect->working_packet.bytes -= n;
154 }
155 }
156 else {
157 /* try and get another packet */
158 const int ret = ogg_stream_packetout(&aspect->stream_state, &aspect->working_packet);
159 if (ret > 0) {
160 aspect->have_working_packet = true;
161 /* if it is the first header packet, check for magic and a supported Ogg FLAC mapping version */
162 if (aspect->working_packet.bytes > 0 && aspect->working_packet.packet[0] == FLAC__OGG_MAPPING_FIRST_HEADER_PACKET_TYPE) {
163 const FLAC__byte *b = aspect->working_packet.packet;
164 const uint32_t header_length =
165 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
166 FLAC__OGG_MAPPING_MAGIC_LENGTH +
167 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
168 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
169 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH;
170 if (aspect->working_packet.bytes < (long)header_length)
171 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC;
172 b += FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH;
173 if (memcmp(b, FLAC__OGG_MAPPING_MAGIC, FLAC__OGG_MAPPING_MAGIC_LENGTH))
174 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC;
175 b += FLAC__OGG_MAPPING_MAGIC_LENGTH;
176 aspect->version_major = (uint32_t)(*b);
177 b += FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH;
178 aspect->version_minor = (uint32_t)(*b);
179 if (aspect->version_major != 1)
180 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION;
181 aspect->working_packet.packet += header_length;
182 aspect->working_packet.bytes -= header_length;
183 }
184 }
185 else if (ret == 0) {
186 aspect->have_working_page = false;
187 }
188 else { /* ret < 0 */
189 /* lost sync, we'll leave the working page for the next call */
190 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC;
191 }
192 }
193 }
194 else {
195 /* try and get another page */
196 const int ret = ogg_sync_pageout(&aspect->sync_state, &aspect->working_page);
197 if (ret > 0) {
198 /* got a page, grab the serial number if necessary */
199 if(aspect->need_serial_number) {
200 aspect->stream_state.serialno = aspect->serial_number = ogg_page_serialno(&aspect->working_page);
201 aspect->need_serial_number = false;
202 }
203 if(ogg_stream_pagein(&aspect->stream_state, &aspect->working_page) == 0) {
204 aspect->have_working_page = true;
205 aspect->have_working_packet = false;
206 }
207 /* else do nothing, could be a page from another stream */
208 }
209 else if (ret == 0) {
210 /* need more data */
211 const size_t ogg_bytes_to_read = flac_max(bytes_requested - *bytes, OGG_BYTES_CHUNK);
212 char *oggbuf = ogg_sync_buffer(&aspect->sync_state, ogg_bytes_to_read);
213
214 if(0 == oggbuf) {
215 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR;
216 }
217 else {
218 size_t ogg_bytes_read = ogg_bytes_to_read;
219
220 switch(read_callback(decoder, (FLAC__byte*)oggbuf, &ogg_bytes_read, client_data)) {
221 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
222 break;
223 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
224 aspect->end_of_stream = true;
225 break;
226 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
227 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
228 default:
229 FLAC__ASSERT(0);
230 }
231
232 if(ogg_sync_wrote(&aspect->sync_state, ogg_bytes_read) < 0) {
233 /* double protection; this will happen if the read callback returns more bytes than the max requested, which would overflow Ogg's internal buffer */
234 FLAC__ASSERT(0);
235 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR;
236 }
237 }
238 }
239 else { /* ret < 0 */
240 /* lost sync, bail out */
241 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC;
242 }
243 }
244 }
245
246 if (aspect->end_of_stream && *bytes == 0) {
247 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
248 }
249
250 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
251 }
252