1 /* libFLAC - Free Lossless Audio Codec
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2013  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 #include <retro_miscellaneous.h>
44 
45 /***********************************************************************
46  *
47  * Public class methods
48  *
49  ***********************************************************************/
50 
FLAC__ogg_decoder_aspect_init(FLAC__OggDecoderAspect * aspect)51 FLAC__bool FLAC__ogg_decoder_aspect_init(FLAC__OggDecoderAspect *aspect)
52 {
53 	/* we will determine the serial number later if necessary */
54 	if(ogg_stream_init(&aspect->stream_state, aspect->serial_number) != 0)
55 		return false;
56 
57 	if(ogg_sync_init(&aspect->sync_state) != 0)
58 		return false;
59 
60 	aspect->version_major = ~(0u);
61 	aspect->version_minor = ~(0u);
62 
63 	aspect->need_serial_number = aspect->use_first_serial_number;
64 
65 	aspect->end_of_stream = false;
66 	aspect->have_working_page = false;
67 
68 	return true;
69 }
70 
FLAC__ogg_decoder_aspect_finish(FLAC__OggDecoderAspect * aspect)71 void FLAC__ogg_decoder_aspect_finish(FLAC__OggDecoderAspect *aspect)
72 {
73 	(void)ogg_sync_clear(&aspect->sync_state);
74 	(void)ogg_stream_clear(&aspect->stream_state);
75 }
76 
FLAC__ogg_decoder_aspect_set_serial_number(FLAC__OggDecoderAspect * aspect,long value)77 void FLAC__ogg_decoder_aspect_set_serial_number(FLAC__OggDecoderAspect *aspect, long value)
78 {
79 	aspect->use_first_serial_number = false;
80 	aspect->serial_number = value;
81 }
82 
FLAC__ogg_decoder_aspect_set_defaults(FLAC__OggDecoderAspect * aspect)83 void FLAC__ogg_decoder_aspect_set_defaults(FLAC__OggDecoderAspect *aspect)
84 {
85 	aspect->use_first_serial_number = true;
86 }
87 
FLAC__ogg_decoder_aspect_flush(FLAC__OggDecoderAspect * aspect)88 void FLAC__ogg_decoder_aspect_flush(FLAC__OggDecoderAspect *aspect)
89 {
90 	(void)ogg_stream_reset(&aspect->stream_state);
91 	(void)ogg_sync_reset(&aspect->sync_state);
92 	aspect->end_of_stream = false;
93 	aspect->have_working_page = false;
94 }
95 
FLAC__ogg_decoder_aspect_reset(FLAC__OggDecoderAspect * aspect)96 void FLAC__ogg_decoder_aspect_reset(FLAC__OggDecoderAspect *aspect)
97 {
98 	FLAC__ogg_decoder_aspect_flush(aspect);
99 
100 	if(aspect->use_first_serial_number)
101 		aspect->need_serial_number = true;
102 }
103 
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 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)
105 {
106 	static const size_t OGG_BYTES_CHUNK = 8192;
107 	const size_t bytes_requested = *bytes;
108 
109 	/*
110 	 * The FLAC decoding API uses pull-based reads, whereas Ogg decoding
111 	 * is push-based.  In libFLAC, when you ask to decode a frame, the
112 	 * decoder will eventually call the read callback to supply some data,
113 	 * but how much it asks for depends on how much free space it has in
114 	 * its internal buffer.  It does not try to grow its internal buffer
115 	 * to accomodate a whole frame because then the internal buffer size
116 	 * could not be limited, which is necessary in embedded applications.
117 	 *
118 	 * Ogg however grows its internal buffer until a whole page is present;
119 	 * only then can you get decoded data out.  So we can't just ask for
120 	 * the same number of bytes from Ogg, then pass what's decoded down to
121 	 * libFLAC.  If what libFLAC is asking for will not contain a whole
122 	 * page, then we will get no data from ogg_sync_pageout(), and at the
123 	 * same time cannot just read more data from the client for the purpose
124 	 * of getting a whole decoded page because the decoded size might be
125 	 * larger than libFLAC's internal buffer.
126 	 *
127 	 * Instead, whenever this read callback wrapper is called, we will
128 	 * continually request data from the client until we have at least one
129 	 * page, and manage pages internally so that we can send pieces of
130 	 * pages down to libFLAC in such a way that we obey its size
131 	 * requirement.  To limit the amount of callbacks, we will always try
132 	 * to read in enough pages to return the full number of bytes
133 	 * requested.
134 	 */
135 	*bytes = 0;
136 	while (*bytes < bytes_requested && !aspect->end_of_stream) {
137 		if (aspect->have_working_page) {
138 			if (aspect->have_working_packet) {
139 				size_t n = bytes_requested - *bytes;
140 				if ((size_t)aspect->working_packet.bytes <= n) {
141 					/* the rest of the packet will fit in the buffer */
142 					n = aspect->working_packet.bytes;
143 					memcpy(buffer, aspect->working_packet.packet, n);
144 					*bytes += n;
145 					buffer += n;
146 					aspect->have_working_packet = false;
147 				}
148 				else {
149 					/* only n bytes of the packet will fit in the buffer */
150 					memcpy(buffer, aspect->working_packet.packet, n);
151 					*bytes += n;
152 					buffer += n;
153 					aspect->working_packet.packet += n;
154 					aspect->working_packet.bytes -= n;
155 				}
156 			}
157 			else {
158 				/* try and get another packet */
159 				const int ret = ogg_stream_packetout(&aspect->stream_state, &aspect->working_packet);
160 				if (ret > 0) {
161 					aspect->have_working_packet = true;
162 					/* if it is the first header packet, check for magic and a supported Ogg FLAC mapping version */
163 					if (aspect->working_packet.bytes > 0 && aspect->working_packet.packet[0] == FLAC__OGG_MAPPING_FIRST_HEADER_PACKET_TYPE) {
164 						const FLAC__byte *b = aspect->working_packet.packet;
165 						const unsigned header_length =
166 							FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
167 							FLAC__OGG_MAPPING_MAGIC_LENGTH +
168 							FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
169 							FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
170 							FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH;
171 						if (aspect->working_packet.bytes < (long)header_length)
172 							return FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC;
173 						b += FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH;
174 						if (memcmp(b, FLAC__OGG_MAPPING_MAGIC, FLAC__OGG_MAPPING_MAGIC_LENGTH))
175 							return FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC;
176 						b += FLAC__OGG_MAPPING_MAGIC_LENGTH;
177 						aspect->version_major = (unsigned)(*b);
178 						b += FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH;
179 						aspect->version_minor = (unsigned)(*b);
180 						if (aspect->version_major != 1)
181 							return FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION;
182 						aspect->working_packet.packet += header_length;
183 						aspect->working_packet.bytes -= header_length;
184 					}
185 				}
186 				else if (ret == 0) {
187 					aspect->have_working_page = false;
188 				}
189 				else { /* ret < 0 */
190 					/* lost sync, we'll leave the working page for the next call */
191 					return FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC;
192 				}
193 			}
194 		}
195 		else {
196 			/* try and get another page */
197 			const int ret = ogg_sync_pageout(&aspect->sync_state, &aspect->working_page);
198 			if (ret > 0) {
199 				/* got a page, grab the serial number if necessary */
200 				if(aspect->need_serial_number) {
201 					aspect->stream_state.serialno = aspect->serial_number = ogg_page_serialno(&aspect->working_page);
202 					aspect->need_serial_number = false;
203 				}
204 				if(ogg_stream_pagein(&aspect->stream_state, &aspect->working_page) == 0) {
205 					aspect->have_working_page = true;
206 					aspect->have_working_packet = false;
207 				}
208 				/* else do nothing, could be a page from another stream */
209 			}
210 			else if (ret == 0) {
211 				/* need more data */
212 				const size_t ogg_bytes_to_read = MAX(bytes_requested - *bytes, OGG_BYTES_CHUNK);
213 				char *oggbuf = ogg_sync_buffer(&aspect->sync_state, ogg_bytes_to_read);
214 
215 				if(0 == oggbuf) {
216 					return FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR;
217 				}
218 				else {
219 					size_t ogg_bytes_read = ogg_bytes_to_read;
220 
221 					switch(read_callback(decoder, (FLAC__byte*)oggbuf, &ogg_bytes_read, client_data)) {
222 						case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
223 							break;
224 						case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
225 							aspect->end_of_stream = true;
226 							break;
227 						case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
228 							return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
229 						default:
230 							FLAC__ASSERT(0);
231 					}
232 
233 					if(ogg_sync_wrote(&aspect->sync_state, ogg_bytes_read) < 0) {
234 						/* double protection; this will happen if the read callback returns more bytes than the max requested, which would overflow Ogg's internal buffer */
235 						FLAC__ASSERT(0);
236 						return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR;
237 					}
238 				}
239 			}
240 			else { /* ret < 0 */
241 				/* lost sync, bail out */
242 				return FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC;
243 			}
244 		}
245 	}
246 
247 	if (aspect->end_of_stream && *bytes == 0) {
248 		return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
249 	}
250 
251 	return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
252 }
253