📄 seekable_stream_decoder.c
字号:
/* libFLAC - Free Lossless Audio Codec library
* Copyright (C) 2000,2001,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 <stdio.h>
#include <stdlib.h> /* for calloc() */
#include <string.h> /* for memcpy()/memcmp() */
#include "FLAC/assert.h"
#include "protected/seekable_stream_decoder.h"
#include "protected/stream_decoder.h"
#include "private/float.h" /* for FLAC__double */
#include "private/md5.h"
/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
#ifdef _MSC_VER
#define FLAC__U64L(x) x
#else
#define FLAC__U64L(x) x##LLU
#endif
/***********************************************************************
*
* Private class method prototypes
*
***********************************************************************/
static void set_defaults_(FLAC__SeekableStreamDecoder *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 FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
/***********************************************************************
*
* Private class data
*
***********************************************************************/
typedef struct FLAC__SeekableStreamDecoderPrivate {
FLAC__SeekableStreamDecoderReadCallback read_callback;
FLAC__SeekableStreamDecoderSeekCallback seek_callback;
FLAC__SeekableStreamDecoderTellCallback tell_callback;
FLAC__SeekableStreamDecoderLengthCallback length_callback;
FLAC__SeekableStreamDecoderEofCallback eof_callback;
FLAC__SeekableStreamDecoderWriteCallback write_callback;
FLAC__SeekableStreamDecoderMetadataCallback metadata_callback;
FLAC__SeekableStreamDecoderErrorCallback error_callback;
void *client_data;
FLAC__StreamDecoder *stream_decoder;
FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek */
struct FLAC__MD5Context md5context;
FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */
FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
/* the rest of these are only used for seeking: */
FLAC__StreamMetadata_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */
const FLAC__StreamMetadata_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */
/* Since we always want to see the STREAMINFO and SEEK_TABLE blocks at this level, we need some extra flags to keep track of whether they should be passed on up through the metadata_callback */
FLAC__bool ignore_stream_info_block;
FLAC__bool ignore_seek_table_block;
FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
FLAC__uint64 target_sample;
} FLAC__SeekableStreamDecoderPrivate;
/***********************************************************************
*
* Public static class data
*
***********************************************************************/
FLAC_API const char * const FLAC__SeekableStreamDecoderStateString[] = {
"FLAC__SEEKABLE_STREAM_DECODER_OK",
"FLAC__SEEKABLE_STREAM_DECODER_SEEKING",
"FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",
"FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
"FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR",
"FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",
"FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",
"FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",
"FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",
"FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"
};
FLAC_API const char * const FLAC__SeekableStreamDecoderReadStatusString[] = {
"FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK",
"FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR"
};
FLAC_API const char * const FLAC__SeekableStreamDecoderSeekStatusString[] = {
"FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK",
"FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR"
};
FLAC_API const char * const FLAC__SeekableStreamDecoderTellStatusString[] = {
"FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK",
"FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR"
};
FLAC_API const char * const FLAC__SeekableStreamDecoderLengthStatusString[] = {
"FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK",
"FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR"
};
/***********************************************************************
*
* Class constructor/destructor
*
***********************************************************************/
FLAC_API FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()
{
FLAC__SeekableStreamDecoder *decoder;
FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
decoder = (FLAC__SeekableStreamDecoder*)calloc(1, sizeof(FLAC__SeekableStreamDecoder));
if(decoder == 0) {
return 0;
}
decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)calloc(1, sizeof(FLAC__SeekableStreamDecoderProtected));
if(decoder->protected_ == 0) {
free(decoder);
return 0;
}
decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)calloc(1, sizeof(FLAC__SeekableStreamDecoderPrivate));
if(decoder->private_ == 0) {
free(decoder->protected_);
free(decoder);
return 0;
}
decoder->private_->stream_decoder = FLAC__stream_decoder_new();
if(0 == decoder->private_->stream_decoder) {
free(decoder->private_);
free(decoder->protected_);
free(decoder);
return 0;
}
set_defaults_(decoder);
decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
return decoder;
}
FLAC_API void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->protected_);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->private_->stream_decoder);
(void)FLAC__seekable_stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
free(decoder->private_);
free(decoder->protected_);
free(decoder);
}
/***********************************************************************
*
* Public class methods
*
***********************************************************************/
FLAC_API FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
{
FLAC__ASSERT(0 != decoder);
if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
if(0 == decoder->private_->read_callback || 0 == decoder->private_->seek_callback || 0 == decoder->private_->tell_callback || 0 == decoder->private_->length_callback || 0 == decoder->private_->eof_callback)
return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
decoder->private_->seek_table = 0;
decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
/* We initialize the FLAC__MD5Context even though we may never use it. This
* is because md5 checking may be turned on to start and then turned off if
* a seek occurs. So we always init the context here and finalize it in
* FLAC__seekable_stream_decoder_finish() to make sure things are always
* cleaned up properly.
*/
FLAC__MD5Init(&decoder->private_->md5context);
FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
/* We always want to see these blocks. Whether or not we pass them up
* through the metadata callback will be determined by flags set in our
* implementation of ..._set_metadata_respond/ignore...()
*/
FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_STREAMINFO);
FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_SEEKTABLE);
if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
}
FLAC_API FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
{
FLAC__bool md5_failed = false;
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->private_);
FLAC__ASSERT(0 != decoder->protected_);
if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
return true;
FLAC__ASSERT(0 != decoder->private_->stream_decoder);
/* see the comment in FLAC__seekable_stream_decoder_init() as to why we
* always call FLAC__MD5Final()
*/
FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
if(decoder->private_->do_md5_checking) {
if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
md5_failed = true;
}
set_defaults_(decoder);
decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
return !md5_failed;
}
FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
{
FLAC__ASSERT(0 != decoder);
FLAC__ASSERT(0 != decoder->protected_);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -