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