Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec |
Erik de Castro Lopo | b1982fb | 2013-05-25 17:11:19 +1000 | [diff] [blame] | 2 | * Copyright (C) 2002-2009 Josh Coalson |
Erik de Castro Lopo | 6a5fe43 | 2016-12-05 06:35:39 +1100 | [diff] [blame^] | 3 | * Copyright (C) 2011-2016 Xiph.Org Foundation |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 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 | |
Erik de Castro Lopo | 006b835 | 2014-03-23 21:59:46 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 34 | # include <config.h> |
| 35 | #endif |
| 36 | |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 37 | #include <string.h> /* for memset() */ |
| 38 | #include "FLAC/assert.h" |
| 39 | #include "private/ogg_encoder_aspect.h" |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 40 | #include "private/ogg_mapping.h" |
| 41 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 42 | static const FLAC__byte FLAC__OGG_MAPPING_VERSION_MAJOR = 1; |
| 43 | static const FLAC__byte FLAC__OGG_MAPPING_VERSION_MINOR = 0; |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 44 | |
| 45 | /*********************************************************************** |
| 46 | * |
| 47 | * Public class methods |
| 48 | * |
| 49 | ***********************************************************************/ |
| 50 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 51 | FLAC__bool FLAC__ogg_encoder_aspect_init(FLAC__OggEncoderAspect *aspect) |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 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 | |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 57 | aspect->seen_magic = false; |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 58 | aspect->is_first_packet = true; |
| 59 | aspect->samples_written = 0; |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 64 | void FLAC__ogg_encoder_aspect_finish(FLAC__OggEncoderAspect *aspect) |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 65 | { |
| 66 | (void)ogg_stream_clear(&aspect->stream_state); |
Josh Coalson | beb1284 | 2006-10-03 01:04:41 +0000 | [diff] [blame] | 67 | /*@@@ what about the page? */ |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 70 | void FLAC__ogg_encoder_aspect_set_serial_number(FLAC__OggEncoderAspect *aspect, long value) |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 71 | { |
| 72 | aspect->serial_number = value; |
| 73 | } |
| 74 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 75 | FLAC__bool FLAC__ogg_encoder_aspect_set_num_metadata(FLAC__OggEncoderAspect *aspect, unsigned value) |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 76 | { |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 77 | if(value < (1u << FLAC__OGG_MAPPING_NUM_HEADERS_LEN)) { |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 78 | aspect->num_metadata = value; |
| 79 | return true; |
| 80 | } |
| 81 | else |
| 82 | return false; |
| 83 | } |
| 84 | |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 85 | void FLAC__ogg_encoder_aspect_set_defaults(FLAC__OggEncoderAspect *aspect) |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 86 | { |
| 87 | aspect->serial_number = 0; |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 88 | aspect->num_metadata = 0; |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 91 | /* |
| 92 | * The basic FLAC -> Ogg mapping goes like this: |
| 93 | * |
| 94 | * - 'fLaC' magic and STREAMINFO block get combined into the first |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 95 | * packet. The packet is prefixed with |
| 96 | * + the one-byte packet type 0x7F |
| 97 | * + 'FLAC' magic |
| 98 | * + the 2 byte Ogg FLAC mapping version number |
| 99 | * + tne 2 byte big-endian # of header packets |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 100 | * - The first packet is flushed to the first page. |
| 101 | * - Each subsequent metadata block goes into its own packet. |
| 102 | * - Each metadata packet is flushed to page (this is not required, |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 103 | * the mapping only requires that a flush must occur after all |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 104 | * metadata is written). |
| 105 | * - Each subsequent FLAC audio frame goes into its own packet. |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 106 | * |
| 107 | * WATCHOUT: |
| 108 | * This depends on the behavior of FLAC__StreamEncoder that we get a |
| 109 | * separate write callback for the fLaC magic, and then separate write |
| 110 | * callbacks for each metadata block and audio frame. |
| 111 | */ |
Josh Coalson | acd4a43 | 2006-11-11 22:43:25 +0000 | [diff] [blame] | 112 | FLAC__StreamEncoderWriteStatus FLAC__ogg_encoder_aspect_write_callback_wrapper(FLAC__OggEncoderAspect *aspect, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, FLAC__bool is_last_block, FLAC__OggEncoderAspectWriteCallbackProxy write_callback, void *encoder, void *client_data) |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 113 | { |
Josh Coalson | 0433c8e | 2004-03-16 23:01:58 +0000 | [diff] [blame] | 114 | /* WATCHOUT: |
| 115 | * This depends on the behavior of FLAC__StreamEncoder that 'samples' |
| 116 | * will be 0 for metadata writes. |
| 117 | */ |
| 118 | const FLAC__bool is_metadata = (samples == 0); |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 119 | |
Josh Coalson | 112ed6c | 2004-01-16 00:10:37 +0000 | [diff] [blame] | 120 | /* |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 121 | * Treat fLaC magic packet specially. We will note when we see it, then |
| 122 | * wait until we get the STREAMINFO and prepend it in that packet |
Josh Coalson | 112ed6c | 2004-01-16 00:10:37 +0000 | [diff] [blame] | 123 | */ |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 124 | if(aspect->seen_magic) { |
| 125 | ogg_packet packet; |
Josh Coalson | 1fba8fb | 2004-09-14 00:24:12 +0000 | [diff] [blame] | 126 | FLAC__byte synthetic_first_packet_body[ |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 127 | FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH + |
| 128 | FLAC__OGG_MAPPING_MAGIC_LENGTH + |
| 129 | FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH + |
| 130 | FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH + |
| 131 | FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH + |
Josh Coalson | 1fba8fb | 2004-09-14 00:24:12 +0000 | [diff] [blame] | 132 | FLAC__STREAM_SYNC_LENGTH + |
| 133 | FLAC__STREAM_METADATA_HEADER_LENGTH + |
| 134 | FLAC__STREAM_METADATA_STREAMINFO_LENGTH |
| 135 | ]; |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 136 | |
| 137 | memset(&packet, 0, sizeof(packet)); |
| 138 | packet.granulepos = aspect->samples_written + samples; |
| 139 | |
| 140 | if(aspect->is_first_packet) { |
Josh Coalson | 1fba8fb | 2004-09-14 00:24:12 +0000 | [diff] [blame] | 141 | FLAC__byte *b = synthetic_first_packet_body; |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 142 | if(bytes != FLAC__STREAM_METADATA_HEADER_LENGTH + FLAC__STREAM_METADATA_STREAMINFO_LENGTH) { |
| 143 | /* |
| 144 | * If we get here, our assumption about the way write callbacks happen |
Josh Coalson | 6b21f66 | 2006-09-13 01:42:27 +0000 | [diff] [blame] | 145 | * (explained above) is wrong |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 146 | */ |
| 147 | FLAC__ASSERT(0); |
Josh Coalson | 112ed6c | 2004-01-16 00:10:37 +0000 | [diff] [blame] | 148 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 149 | } |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 150 | /* add first header packet type */ |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 151 | *b = FLAC__OGG_MAPPING_FIRST_HEADER_PACKET_TYPE; |
| 152 | b += FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH; |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 153 | /* add 'FLAC' mapping magic */ |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 154 | memcpy(b, FLAC__OGG_MAPPING_MAGIC, FLAC__OGG_MAPPING_MAGIC_LENGTH); |
| 155 | b += FLAC__OGG_MAPPING_MAGIC_LENGTH; |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 156 | /* add Ogg FLAC mapping major version number */ |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 157 | memcpy(b, &FLAC__OGG_MAPPING_VERSION_MAJOR, FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH); |
| 158 | b += FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH; |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 159 | /* add Ogg FLAC mapping minor version number */ |
Josh Coalson | 8da98c8 | 2006-10-15 04:24:05 +0000 | [diff] [blame] | 160 | memcpy(b, &FLAC__OGG_MAPPING_VERSION_MINOR, FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH); |
| 161 | b += FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH; |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 162 | /* add number of header packets */ |
| 163 | *b = (FLAC__byte)(aspect->num_metadata >> 8); |
| 164 | b++; |
| 165 | *b = (FLAC__byte)(aspect->num_metadata); |
| 166 | b++; |
Josh Coalson | 4598b1c | 2004-09-03 01:03:23 +0000 | [diff] [blame] | 167 | /* add native FLAC 'fLaC' magic */ |
| 168 | memcpy(b, FLAC__STREAM_SYNC_STRING, FLAC__STREAM_SYNC_LENGTH); |
| 169 | b += FLAC__STREAM_SYNC_LENGTH; |
| 170 | /* add STREAMINFO */ |
| 171 | memcpy(b, buffer, bytes); |
Josh Coalson | 1fba8fb | 2004-09-14 00:24:12 +0000 | [diff] [blame] | 172 | FLAC__ASSERT(b + bytes - synthetic_first_packet_body == sizeof(synthetic_first_packet_body)); |
| 173 | packet.packet = (unsigned char *)synthetic_first_packet_body; |
| 174 | packet.bytes = sizeof(synthetic_first_packet_body); |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 175 | |
| 176 | packet.b_o_s = 1; |
| 177 | aspect->is_first_packet = false; |
Josh Coalson | 112ed6c | 2004-01-16 00:10:37 +0000 | [diff] [blame] | 178 | } |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 179 | else { |
| 180 | packet.packet = (unsigned char *)buffer; |
| 181 | packet.bytes = bytes; |
| 182 | } |
| 183 | |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 184 | if(is_last_block) { |
| 185 | /* we used to check: |
| 186 | * FLAC__ASSERT(total_samples_estimate == 0 || total_samples_estimate == aspect->samples_written + samples); |
| 187 | * but it's really not useful since total_samples_estimate is an estimate and can be inexact |
| 188 | */ |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 189 | packet.e_o_s = 1; |
Josh Coalson | 49f2f16 | 2006-11-09 16:54:52 +0000 | [diff] [blame] | 190 | } |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 191 | |
| 192 | if(ogg_stream_packetin(&aspect->stream_state, &packet) != 0) |
| 193 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 194 | |
Josh Coalson | 69cfda7 | 2004-09-10 00:38:21 +0000 | [diff] [blame] | 195 | /*@@@ can't figure out a way to pass a useful number for 'samples' to the write_callback, so we'll just pass 0 */ |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 196 | if(is_metadata) { |
| 197 | while(ogg_stream_flush(&aspect->stream_state, &aspect->page) != 0) { |
| 198 | if(write_callback(encoder, aspect->page.header, aspect->page.header_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) |
| 199 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 200 | if(write_callback(encoder, aspect->page.body, aspect->page.body_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) |
| 201 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 202 | } |
| 203 | } |
| 204 | else { |
| 205 | while(ogg_stream_pageout(&aspect->stream_state, &aspect->page) != 0) { |
| 206 | if(write_callback(encoder, aspect->page.header, aspect->page.header_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) |
| 207 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 208 | if(write_callback(encoder, aspect->page.body, aspect->page.body_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) |
| 209 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | else if(is_metadata && current_frame == 0 && samples == 0 && bytes == 4 && 0 == memcmp(buffer, FLAC__STREAM_SYNC_STRING, sizeof(FLAC__STREAM_SYNC_STRING))) { |
| 214 | aspect->seen_magic = true; |
Josh Coalson | 112ed6c | 2004-01-16 00:10:37 +0000 | [diff] [blame] | 215 | } |
| 216 | else { |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 217 | /* |
| 218 | * If we get here, our assumption about the way write callbacks happen |
| 219 | * explained above is wrong |
| 220 | */ |
| 221 | FLAC__ASSERT(0); |
| 222 | return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; |
Josh Coalson | 112ed6c | 2004-01-16 00:10:37 +0000 | [diff] [blame] | 223 | } |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 224 | |
Josh Coalson | 55ddee5 | 2004-08-20 22:09:02 +0000 | [diff] [blame] | 225 | aspect->samples_written += samples; |
| 226 | |
Josh Coalson | e9b38b4 | 2003-09-26 01:51:44 +0000 | [diff] [blame] | 227 | return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; |
| 228 | } |