annotate src/flac-1.2.1/src/libFLAC/ogg_encoder_aspect.c @ 7:cd13f7cd9bc3

Add portaudio
author Chris Cannam
date Wed, 20 Mar 2013 15:18:57 +0000
parents 05aa0afa9217
children
rev   line source
Chris@1 1 /* libFLAC - Free Lossless Audio Codec
Chris@1 2 * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson
Chris@1 3 *
Chris@1 4 * Redistribution and use in source and binary forms, with or without
Chris@1 5 * modification, are permitted provided that the following conditions
Chris@1 6 * are met:
Chris@1 7 *
Chris@1 8 * - Redistributions of source code must retain the above copyright
Chris@1 9 * notice, this list of conditions and the following disclaimer.
Chris@1 10 *
Chris@1 11 * - Redistributions in binary form must reproduce the above copyright
Chris@1 12 * notice, this list of conditions and the following disclaimer in the
Chris@1 13 * documentation and/or other materials provided with the distribution.
Chris@1 14 *
Chris@1 15 * - Neither the name of the Xiph.org Foundation nor the names of its
Chris@1 16 * contributors may be used to endorse or promote products derived from
Chris@1 17 * this software without specific prior written permission.
Chris@1 18 *
Chris@1 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@1 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@1 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@1 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
Chris@1 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@1 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@1 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Chris@1 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Chris@1 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Chris@1 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@1 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@1 30 */
Chris@1 31
Chris@1 32 #if HAVE_CONFIG_H
Chris@1 33 # include <config.h>
Chris@1 34 #endif
Chris@1 35
Chris@1 36 #include <string.h> /* for memset() */
Chris@1 37 #include "FLAC/assert.h"
Chris@1 38 #include "private/ogg_encoder_aspect.h"
Chris@1 39 #include "private/ogg_mapping.h"
Chris@1 40
Chris@1 41 static const FLAC__byte FLAC__OGG_MAPPING_VERSION_MAJOR = 1;
Chris@1 42 static const FLAC__byte FLAC__OGG_MAPPING_VERSION_MINOR = 0;
Chris@1 43
Chris@1 44 /***********************************************************************
Chris@1 45 *
Chris@1 46 * Public class methods
Chris@1 47 *
Chris@1 48 ***********************************************************************/
Chris@1 49
Chris@1 50 FLAC__bool FLAC__ogg_encoder_aspect_init(FLAC__OggEncoderAspect *aspect)
Chris@1 51 {
Chris@1 52 /* we will determine the serial number later if necessary */
Chris@1 53 if(ogg_stream_init(&aspect->stream_state, aspect->serial_number) != 0)
Chris@1 54 return false;
Chris@1 55
Chris@1 56 aspect->seen_magic = false;
Chris@1 57 aspect->is_first_packet = true;
Chris@1 58 aspect->samples_written = 0;
Chris@1 59
Chris@1 60 return true;
Chris@1 61 }
Chris@1 62
Chris@1 63 void FLAC__ogg_encoder_aspect_finish(FLAC__OggEncoderAspect *aspect)
Chris@1 64 {
Chris@1 65 (void)ogg_stream_clear(&aspect->stream_state);
Chris@1 66 /*@@@ what about the page? */
Chris@1 67 }
Chris@1 68
Chris@1 69 void FLAC__ogg_encoder_aspect_set_serial_number(FLAC__OggEncoderAspect *aspect, long value)
Chris@1 70 {
Chris@1 71 aspect->serial_number = value;
Chris@1 72 }
Chris@1 73
Chris@1 74 FLAC__bool FLAC__ogg_encoder_aspect_set_num_metadata(FLAC__OggEncoderAspect *aspect, unsigned value)
Chris@1 75 {
Chris@1 76 if(value < (1u << FLAC__OGG_MAPPING_NUM_HEADERS_LEN)) {
Chris@1 77 aspect->num_metadata = value;
Chris@1 78 return true;
Chris@1 79 }
Chris@1 80 else
Chris@1 81 return false;
Chris@1 82 }
Chris@1 83
Chris@1 84 void FLAC__ogg_encoder_aspect_set_defaults(FLAC__OggEncoderAspect *aspect)
Chris@1 85 {
Chris@1 86 aspect->serial_number = 0;
Chris@1 87 aspect->num_metadata = 0;
Chris@1 88 }
Chris@1 89
Chris@1 90 /*
Chris@1 91 * The basic FLAC -> Ogg mapping goes like this:
Chris@1 92 *
Chris@1 93 * - 'fLaC' magic and STREAMINFO block get combined into the first
Chris@1 94 * packet. The packet is prefixed with
Chris@1 95 * + the one-byte packet type 0x7F
Chris@1 96 * + 'FLAC' magic
Chris@1 97 * + the 2 byte Ogg FLAC mapping version number
Chris@1 98 * + tne 2 byte big-endian # of header packets
Chris@1 99 * - The first packet is flushed to the first page.
Chris@1 100 * - Each subsequent metadata block goes into its own packet.
Chris@1 101 * - Each metadata packet is flushed to page (this is not required,
Chris@1 102 * the mapping only requires that a flush must occur after all
Chris@1 103 * metadata is written).
Chris@1 104 * - Each subsequent FLAC audio frame goes into its own packet.
Chris@1 105 *
Chris@1 106 * WATCHOUT:
Chris@1 107 * This depends on the behavior of FLAC__StreamEncoder that we get a
Chris@1 108 * separate write callback for the fLaC magic, and then separate write
Chris@1 109 * callbacks for each metadata block and audio frame.
Chris@1 110 */
Chris@1 111 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)
Chris@1 112 {
Chris@1 113 /* WATCHOUT:
Chris@1 114 * This depends on the behavior of FLAC__StreamEncoder that 'samples'
Chris@1 115 * will be 0 for metadata writes.
Chris@1 116 */
Chris@1 117 const FLAC__bool is_metadata = (samples == 0);
Chris@1 118
Chris@1 119 /*
Chris@1 120 * Treat fLaC magic packet specially. We will note when we see it, then
Chris@1 121 * wait until we get the STREAMINFO and prepend it in that packet
Chris@1 122 */
Chris@1 123 if(aspect->seen_magic) {
Chris@1 124 ogg_packet packet;
Chris@1 125 FLAC__byte synthetic_first_packet_body[
Chris@1 126 FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
Chris@1 127 FLAC__OGG_MAPPING_MAGIC_LENGTH +
Chris@1 128 FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
Chris@1 129 FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
Chris@1 130 FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
Chris@1 131 FLAC__STREAM_SYNC_LENGTH +
Chris@1 132 FLAC__STREAM_METADATA_HEADER_LENGTH +
Chris@1 133 FLAC__STREAM_METADATA_STREAMINFO_LENGTH
Chris@1 134 ];
Chris@1 135
Chris@1 136 memset(&packet, 0, sizeof(packet));
Chris@1 137 packet.granulepos = aspect->samples_written + samples;
Chris@1 138
Chris@1 139 if(aspect->is_first_packet) {
Chris@1 140 FLAC__byte *b = synthetic_first_packet_body;
Chris@1 141 if(bytes != FLAC__STREAM_METADATA_HEADER_LENGTH + FLAC__STREAM_METADATA_STREAMINFO_LENGTH) {
Chris@1 142 /*
Chris@1 143 * If we get here, our assumption about the way write callbacks happen
Chris@1 144 * (explained above) is wrong
Chris@1 145 */
Chris@1 146 FLAC__ASSERT(0);
Chris@1 147 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 148 }
Chris@1 149 /* add first header packet type */
Chris@1 150 *b = FLAC__OGG_MAPPING_FIRST_HEADER_PACKET_TYPE;
Chris@1 151 b += FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH;
Chris@1 152 /* add 'FLAC' mapping magic */
Chris@1 153 memcpy(b, FLAC__OGG_MAPPING_MAGIC, FLAC__OGG_MAPPING_MAGIC_LENGTH);
Chris@1 154 b += FLAC__OGG_MAPPING_MAGIC_LENGTH;
Chris@1 155 /* add Ogg FLAC mapping major version number */
Chris@1 156 memcpy(b, &FLAC__OGG_MAPPING_VERSION_MAJOR, FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH);
Chris@1 157 b += FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH;
Chris@1 158 /* add Ogg FLAC mapping minor version number */
Chris@1 159 memcpy(b, &FLAC__OGG_MAPPING_VERSION_MINOR, FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH);
Chris@1 160 b += FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH;
Chris@1 161 /* add number of header packets */
Chris@1 162 *b = (FLAC__byte)(aspect->num_metadata >> 8);
Chris@1 163 b++;
Chris@1 164 *b = (FLAC__byte)(aspect->num_metadata);
Chris@1 165 b++;
Chris@1 166 /* add native FLAC 'fLaC' magic */
Chris@1 167 memcpy(b, FLAC__STREAM_SYNC_STRING, FLAC__STREAM_SYNC_LENGTH);
Chris@1 168 b += FLAC__STREAM_SYNC_LENGTH;
Chris@1 169 /* add STREAMINFO */
Chris@1 170 memcpy(b, buffer, bytes);
Chris@1 171 FLAC__ASSERT(b + bytes - synthetic_first_packet_body == sizeof(synthetic_first_packet_body));
Chris@1 172 packet.packet = (unsigned char *)synthetic_first_packet_body;
Chris@1 173 packet.bytes = sizeof(synthetic_first_packet_body);
Chris@1 174
Chris@1 175 packet.b_o_s = 1;
Chris@1 176 aspect->is_first_packet = false;
Chris@1 177 }
Chris@1 178 else {
Chris@1 179 packet.packet = (unsigned char *)buffer;
Chris@1 180 packet.bytes = bytes;
Chris@1 181 }
Chris@1 182
Chris@1 183 if(is_last_block) {
Chris@1 184 /* we used to check:
Chris@1 185 * FLAC__ASSERT(total_samples_estimate == 0 || total_samples_estimate == aspect->samples_written + samples);
Chris@1 186 * but it's really not useful since total_samples_estimate is an estimate and can be inexact
Chris@1 187 */
Chris@1 188 packet.e_o_s = 1;
Chris@1 189 }
Chris@1 190
Chris@1 191 if(ogg_stream_packetin(&aspect->stream_state, &packet) != 0)
Chris@1 192 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 193
Chris@1 194 /*@@@ can't figure out a way to pass a useful number for 'samples' to the write_callback, so we'll just pass 0 */
Chris@1 195 if(is_metadata) {
Chris@1 196 while(ogg_stream_flush(&aspect->stream_state, &aspect->page) != 0) {
Chris@1 197 if(write_callback(encoder, aspect->page.header, aspect->page.header_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
Chris@1 198 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 199 if(write_callback(encoder, aspect->page.body, aspect->page.body_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
Chris@1 200 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 201 }
Chris@1 202 }
Chris@1 203 else {
Chris@1 204 while(ogg_stream_pageout(&aspect->stream_state, &aspect->page) != 0) {
Chris@1 205 if(write_callback(encoder, aspect->page.header, aspect->page.header_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
Chris@1 206 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 207 if(write_callback(encoder, aspect->page.body, aspect->page.body_len, 0, current_frame, client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK)
Chris@1 208 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 209 }
Chris@1 210 }
Chris@1 211 }
Chris@1 212 else if(is_metadata && current_frame == 0 && samples == 0 && bytes == 4 && 0 == memcmp(buffer, FLAC__STREAM_SYNC_STRING, sizeof(FLAC__STREAM_SYNC_STRING))) {
Chris@1 213 aspect->seen_magic = true;
Chris@1 214 }
Chris@1 215 else {
Chris@1 216 /*
Chris@1 217 * If we get here, our assumption about the way write callbacks happen
Chris@1 218 * explained above is wrong
Chris@1 219 */
Chris@1 220 FLAC__ASSERT(0);
Chris@1 221 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
Chris@1 222 }
Chris@1 223
Chris@1 224 aspect->samples_written += samples;
Chris@1 225
Chris@1 226 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
Chris@1 227 }