📄 stream_decoder.c
字号:
/* libOggFLAC - Free Lossless Audio Codec + Ogg library
* Copyright (C) 2002,2003,2004,2005 Josh Coalson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Xiph.org Foundation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h> /* for calloc() */
#include "FLAC/assert.h"
#include "protected/stream_decoder.h"
/***********************************************************************
*
* Private class method prototypes
*
***********************************************************************/
static void set_defaults_(OggFLAC__StreamDecoder *decoder);
static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static OggFLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
/***********************************************************************
*
* Private class data
*
***********************************************************************/
typedef struct OggFLAC__StreamDecoderPrivate {
OggFLAC__StreamDecoderReadCallback read_callback;
OggFLAC__StreamDecoderWriteCallback write_callback;
OggFLAC__StreamDecoderMetadataCallback metadata_callback;
OggFLAC__StreamDecoderErrorCallback error_callback;
void *client_data;
FLAC__StreamDecoder *FLAC_stream_decoder;
} OggFLAC__StreamDecoderPrivate;
/***********************************************************************
*
* Public static class data
*
***********************************************************************/
OggFLAC_API const char * const OggFLAC__StreamDecoderStateString[] = {
"OggFLAC__STREAM_DECODER_OK",
"OggFLAC__STREAM_DECODER_END_OF_STREAM",
"OggFLAC__STREAM_DECODER_OGG_ERROR",
"OggFLAC__STREAM_DECODER_READ_ERROR",
"OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR",
"OggFLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
"OggFLAC__STREAM_DECODER_ALREADY_INITIALIZED",
"OggFLAC__STREAM_DECODER_INVALID_CALLBACK",
"OggFLAC__STREAM_DECODER_UNINITIALIZED"
};
/***********************************************************************
*
* Class constructor/destructor
*
***********************************************************************/
OggFLAC_API OggFLAC__StreamDecoder *OggFLAC__stream_decoder_new()
{
OggFLAC__StreamDecoder *decoder;
decoder = (OggFLAC__StreamDecoder*)calloc(1, sizeof(OggFLAC__StreamDecoder));
if(decoder == 0) {
return 0;
}
decoder->protected_ = (OggFLAC__StreamDecoderProtected*)calloc(1, sizeof(OggFLAC__StreamDecoderProtected));
if(decoder->protected_ == 0) {
free(decoder);
return 0;
}
decoder->private_ = (OggFLAC__StreamDecoderPrivate*)calloc(1, sizeof(OggFLAC__StreamDecoderPrivate));
if(decoder->private_ == 0) {
free(decoder->protected_);
free(decoder);
return 0;
}
decoder->private_->FLAC_stream_decoder = FLAC__stream_decoder_new();
if(0 == decoder->private_->FLAC_stream_decoder) {
free(decoder->private_);
free(decoder->protected_);
free(decoder);
return 0;
}
set_defaults_(decoder);
decoder->protected_->state = OggFLAC__STREAM_DECODER_UNINITIALIZED;
return decoder;
}
OggFLAC_API void OggFLAC__stream_decoder_delete(OggFLAC__StreamDecoder *decoder)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->protected_);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->private_->FLAC_stream_decoder);
OggFLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder->private_->FLAC_stream_decoder);
free(decoder->private_);
free(decoder->protected_);
free(decoder);
}
/***********************************************************************
*
* Public class methods
*
***********************************************************************/
OggFLAC_API OggFLAC__StreamDecoderState OggFLAC__stream_decoder_init(OggFLAC__StreamDecoder *decoder)
{
FLAC__ASSERT(0 != decoder);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return decoder->protected_->state = OggFLAC__STREAM_DECODER_ALREADY_INITIALIZED;
if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
return decoder->protected_->state = OggFLAC__STREAM_DECODER_INVALID_CALLBACK;
if(!OggFLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
return decoder->protected_->state = OggFLAC__STREAM_DECODER_OGG_ERROR;
FLAC__stream_decoder_set_read_callback(decoder->private_->FLAC_stream_decoder, read_callback_);
FLAC__stream_decoder_set_write_callback(decoder->private_->FLAC_stream_decoder, write_callback_);
FLAC__stream_decoder_set_metadata_callback(decoder->private_->FLAC_stream_decoder, metadata_callback_);
FLAC__stream_decoder_set_error_callback(decoder->private_->FLAC_stream_decoder, error_callback_);
FLAC__stream_decoder_set_client_data(decoder->private_->FLAC_stream_decoder, decoder);
if(FLAC__stream_decoder_init(decoder->private_->FLAC_stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
return decoder->protected_->state = OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR;
return decoder->protected_->state = OggFLAC__STREAM_DECODER_OK;
}
OggFLAC_API void OggFLAC__stream_decoder_finish(OggFLAC__StreamDecoder *decoder)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state == OggFLAC__STREAM_DECODER_UNINITIALIZED)
return;
FLAC__ASSERT(0 != decoder->private_->FLAC_stream_decoder);
FLAC__stream_decoder_finish(decoder->private_->FLAC_stream_decoder);
OggFLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
set_defaults_(decoder);
decoder->protected_->state = OggFLAC__STREAM_DECODER_UNINITIALIZED;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_read_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderReadCallback value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
decoder->private_->read_callback = value;
return true;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_write_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderWriteCallback value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
decoder->private_->write_callback = value;
return true;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderMetadataCallback value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
decoder->private_->metadata_callback = value;
return true;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_error_callback(OggFLAC__StreamDecoder *decoder, OggFLAC__StreamDecoderErrorCallback value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
decoder->private_->error_callback = value;
return true;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_client_data(OggFLAC__StreamDecoder *decoder, void *value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
decoder->private_->client_data = value;
return true;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_serial_number(OggFLAC__StreamDecoder *decoder, long value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
OggFLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
return true;
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_respond(OggFLAC__StreamDecoder *decoder, FLAC__MetadataType type)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
return FLAC__stream_decoder_set_metadata_respond(decoder->private_->FLAC_stream_decoder, type);
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_respond_application(OggFLAC__StreamDecoder *decoder, const FLAC__byte id[4])
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state != OggFLAC__STREAM_DECODER_UNINITIALIZED)
return false;
return FLAC__stream_decoder_set_metadata_respond_application(decoder->private_->FLAC_stream_decoder, id);
}
OggFLAC_API FLAC__bool OggFLAC__stream_decoder_set_metadata_respond_all(OggFLAC__StreamDecoder *decoder)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -