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