Commit 7d273b4b authored by Josh Coalson's avatar Josh Coalson

add support for read-only operations for ogg flac to chain interface

parent d037e887
......@@ -119,6 +119,7 @@
<li>
metaflac:
<ul>
<li>Added support for read-only operations on Ogg FLAC files.</li>
<li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_set_tag_from_file">--set-tag-from-file</a></span> for setting a tag from file (e.g. for importing a cuesheet as a tag).</li>
<li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_import_picture_from">--import-picture-from</a></span> for importing pictures.</li>
<li>Added a new option <span class="argument"><a href="documentation.html#metaflac_shorthand_export_picture_to">--export-picture-to</a></span> for exporting pictures.</li>
......@@ -191,6 +192,7 @@
<li><b>Added</b> FLAC__metadata_object_cuesheet_calculate_cddb_id()</li>
<li><b>Added</b> FLAC__metadata_get_cuesheet()</li>
<li><b>Added</b> FLAC__metadata_get_picture()</li>
<li><b>Added</b> FLAC__metadata_chain_read_ogg() and FLAC__metadata_chain_read_ogg_with_callbacks()</li>
<li><b>Changed</b> FLAC__stream_encoder_finish() now returns a FLAC__bool to signal a verify failure.</li>
<li><b>Changed</b> FLAC__StreamDecoderState: removed state FLAC__STREAM_DECODER_UNPARSEABLE_STREAM</li>
<li><b>Changed</b> FLAC__StreamDecoderErrorStatus: new error code FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM</li>
......@@ -206,6 +208,7 @@
<li><b>Added</b> FLAC::Metadata::CueSheet::calculate_cddb_id()</li>
<li><b>Added</b> FLAC::Metadata::get_cuesheet()</li>
<li><b>Added</b> FLAC::Metadata::get_picture()</li>
<li><b>Changed</b> FLAC::Metadata::Chain::read() to accept a flag denoting Ogg FLAC input</li>
<li><b>Changed</b> FLAC::Decoder::Stream::finish() now returns a bool to signal an MD5 failure like FLAC__stream_decoder_finish() does.</li>
<li><b>Changed</b> FLAC::Encoder::Stream::finish() now returns a bool to signal a verify failure.</li>
</ul>
......
......@@ -1095,8 +1095,8 @@ namespace FLAC {
Status status(); ///< See FLAC__metadata_chain_status().
bool read(const char *filename); ///< See FLAC__metadata_chain_read().
bool read(FLAC__IOHandle handle, FLAC__IOCallbacks callbacks); ///< See FLAC__metadata_chain_read_with_callbacks().
bool read(const char *filename, bool is_ogg = false); ///< See FLAC__metadata_chain_read(), FLAC__metadata_chain_read_ogg().
bool read(FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, bool is_ogg = false); ///< See FLAC__metadata_chain_read_with_callbacks(), FLAC__metadata_chain_read_ogg_with_callbacks().
bool check_if_tempfile_needed(bool use_padding); ///< See FLAC__metadata_chain_check_if_tempfile_needed().
......
......@@ -584,12 +584,17 @@ FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_S
* all metadata is read into memory, operated on in memory, and then written
* to file, which is more efficient than level 1 when editing multiple blocks.
*
* Currently Ogg FLAC is supported for read only, via
* FLAC__metadata_chain_read_ogg() but a subsequent
* FLAC__metadata_chain_write() will fail.
*
* The general usage of this interface is:
*
* - Create a new chain using FLAC__metadata_chain_new(). A chain is a
* linked list of FLAC metadata blocks.
* - Read all metadata into the the chain from a FLAC file using
* FLAC__metadata_chain_read() and check the status.
* FLAC__metadata_chain_read() or FLAC__metadata_chain_read_ogg() and
* check the status.
* - Optionally, consolidate the padding using
* FLAC__metadata_chain_merge_padding() or
* FLAC__metadata_chain_sort_padding().
......@@ -611,8 +616,8 @@ FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_S
* Even though the FLAC file is not open while the chain is being
* manipulated, you must not alter the file externally during
* this time. The chain assumes the FLAC file will not change
* between the time of FLAC__metadata_chain_read() and
* FLAC__metadata_chain_write().
* between the time of FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg()
* and FLAC__metadata_chain_write().
*
* \note
* Do not modify the is_last, length, or type fields of returned
......@@ -683,11 +688,12 @@ typedef enum {
FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH,
/**< FLAC__metadata_chain_write() was called on a chain read by
* FLAC__metadata_chain_read_with_callbacks(), or
* FLAC__metadata_chain_write_with_callbacks() or
* FLAC__metadata_chain_write_with_callbacks_and_tempfile() was
* called on a chain read by FLAC__metadata_chain_read(). Matching
* read/write methods must always be used. */
* FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(),
* or
* FLAC__metadata_chain_write_with_callbacks()/FLAC__metadata_chain_write_with_callbacks_and_tempfile()
* was called on a chain read by
* FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg().
* Matching read/write methods must always be used. */
FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL
/**< FLAC__metadata_chain_write_with_callbacks() was called when the
......@@ -751,6 +757,24 @@ FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_C
*/
FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename);
/*@@@@ add to unit tests*/
/** Read all metadata from an Ogg FLAC file into the chain.
*
* \note Ogg FLAC metadata data writing is not supported yet and
* FLAC__metadata_chain_write() will fail.
*
* \param chain A pointer to an existing chain.
* \param filename The path to the Ogg FLAC file to read.
* \assert
* \code chain != NULL \endcode
* \code filename != NULL \endcode
* \retval FLAC__bool
* \c true if a valid list of metadata blocks was read from
* \a filename, else \c false. On failure, check the status with
* FLAC__metadata_chain_status().
*/
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename);
/** Read all metadata from a FLAC stream into the chain via I/O callbacks.
*
* The \a handle need only be open for reading, but must be seekable.
......@@ -773,6 +797,32 @@ FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const
*/
FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);
/*@@@@ add to unit tests*/
/** Read all metadata from an Ogg FLAC stream into the chain via I/O callbacks.
*
* The \a handle need only be open for reading, but must be seekable.
* The equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb"
* for Windows).
*
* \note Ogg FLAC metadata data writing is not supported yet and
* FLAC__metadata_chain_write() will fail.
*
* \param chain A pointer to an existing chain.
* \param handle The I/O handle of the Ogg FLAC stream to read. The
* handle will NOT be closed after the metadata is read;
* that is the duty of the caller.
* \param callbacks
* A set of callbacks to use for I/O. The mandatory
* callbacks are \a read, \a seek, and \a tell.
* \assert
* \code chain != NULL \endcode
* \retval FLAC__bool
* \c true if a valid list of metadata blocks was read from
* \a handle, else \c false. On failure, check the status with
* FLAC__metadata_chain_status().
*/
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);
/** Checks if writing the given chain would require the use of a
* temporary file, or if it could be written in place.
*
......@@ -833,7 +883,8 @@ FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata
* be preserved even if the FLAC file is written.
*
* For this write function to be used, the chain must have been read with
* FLAC__metadata_chain_read(), not FLAC__metadata_chain_read_with_callbacks().
* FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg(), not
* FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks().
*
* \param chain A pointer to an existing chain.
* \param use_padding See above.
......@@ -856,7 +907,8 @@ FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC
* for Windows).
*
* For this write function to be used, the chain must have been read with
* FLAC__metadata_chain_read_with_callbacks(), not FLAC__metadata_chain_read().
* FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(),
* not FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg().
* Also, FLAC__metadata_chain_check_if_tempfile_needed() must have returned
* \c false.
*
......@@ -899,7 +951,8 @@ FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Cha
* truncate it on return).
*
* For this write function to be used, the chain must have been read with
* FLAC__metadata_chain_read_with_callbacks(), not FLAC__metadata_chain_read().
* FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(),
* not FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg().
* Also, FLAC__metadata_chain_check_if_tempfile_needed() must have returned
* \c true.
*
......
......@@ -1402,17 +1402,23 @@ namespace FLAC {
return Status(::FLAC__metadata_chain_status(chain_));
}
bool Chain::read(const char *filename)
bool Chain::read(const char *filename, bool is_ogg)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(is_valid());
return (bool)::FLAC__metadata_chain_read(chain_, filename);
return is_ogg?
(bool)::FLAC__metadata_chain_read_ogg(chain_, filename) :
(bool)::FLAC__metadata_chain_read(chain_, filename)
;
}
bool Chain::read(FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks)
bool Chain::read(FLAC__IOHandle handle, ::FLAC__IOCallbacks callbacks, bool is_ogg)
{
FLAC__ASSERT(is_valid());
return (bool)::FLAC__metadata_chain_read_with_callbacks(chain_, handle, callbacks);
return is_ogg?
(bool)::FLAC__metadata_chain_read_ogg_with_callbacks(chain_, handle, callbacks) :
(bool)::FLAC__metadata_chain_read_with_callbacks(chain_, handle, callbacks)
;
}
bool Chain::check_if_tempfile_needed(bool use_padding)
......
......@@ -839,6 +839,7 @@ typedef struct FLAC__Metadata_Node {
struct FLAC__Metadata_Chain {
char *filename; /* will be NULL if using callbacks */
FLAC__bool is_ogg;
FLAC__Metadata_Node *head;
FLAC__Metadata_Node *tail;
unsigned nodes;
......@@ -850,6 +851,9 @@ struct FLAC__Metadata_Chain {
* or not the whole file has to be rewritten.
*/
off_t initial_length;
/* @@@ hacky, these are currently only needed by ogg reader */
FLAC__IOHandle handle;
FLAC__IOCallback_Read read_cb;
};
struct FLAC__Metadata_Iterator {
......@@ -895,10 +899,12 @@ static void chain_init_(FLAC__Metadata_Chain *chain)
FLAC__ASSERT(0 != chain);
chain->filename = 0;
chain->is_ogg = false;
chain->head = chain->tail = 0;
chain->nodes = 0;
chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
chain->initial_length = 0;
chain->read_cb = 0;
}
static void chain_clear_(FLAC__Metadata_Chain *chain)
......@@ -1188,6 +1194,95 @@ static FLAC__bool chain_read_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle han
return true;
}
FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
(void)decoder;
if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) {
*bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle);
if(*bytes == 0)
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
else
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
else
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
(void)decoder, (void)frame, (void)buffer, (void)client_data;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
(void)decoder;
FLAC__Metadata_Node *node = node_new_();
if(0 == node) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return;
}
node->data = FLAC__metadata_object_clone(metadata);
if(0 == node->data) {
node_delete_(node);
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return;
}
chain_append_node_(chain, node);
}
static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
(void)decoder, (void)status;
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
}
static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb)
{
FLAC__StreamDecoder *decoder;
FLAC__ASSERT(0 != chain);
/* we assume we're already at the beginning of the file */
chain->handle = handle;
chain->read_cb = read_cb;
if(0 == (decoder = FLAC__stream_decoder_new())) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
FLAC__stream_decoder_set_metadata_respond_all(decoder);
if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
FLAC__stream_decoder_delete(decoder);
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
return false;
}
chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
FLAC__stream_decoder_delete(decoder);
return false;
}
FLAC__stream_decoder_delete(decoder);
chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
chain->initial_length = chain_calculate_length_(chain);
return true;
}
static FLAC__bool chain_rewrite_metadata_in_place_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, FLAC__IOCallback_Seek seek_cb)
{
FLAC__Metadata_Node *node;
......@@ -1369,7 +1464,7 @@ FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_C
return status;
}
FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg)
{
FILE *file;
FLAC__bool ret;
......@@ -1384,21 +1479,38 @@ FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const
return false;
}
chain->is_ogg = is_ogg;
if(0 == (file = fopen(filename, "rb"))) {
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
return false;
}
/* chain_read_cb_() sets chain->status for us */
ret = chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_);
/* the function also sets chain->status for us */
ret = is_ogg?
chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) :
chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_)
;
fclose(file);
return ret;
}
FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
{
return chain_read_(chain, filename, /*is_ogg=*/false);
}
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename)
{
return chain_read_(chain, filename, /*is_ogg=*/true);
}
static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg)
{
FLAC__bool ret;
FLAC__ASSERT(0 != chain);
chain_clear_(chain);
......@@ -1408,16 +1520,31 @@ FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chai
return false;
}
chain->is_ogg = is_ogg;
/* rewind */
if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
return false;
}
if(!chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell))
return false; /* chain->status is already set by chain_read_cb_ */
/* the function also sets chain->status for us */
ret = is_ogg?
chain_read_ogg_cb_(chain, handle, callbacks.read) :
chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell)
;
return true;
return ret;
}
FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
{
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false);
}
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
{
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
}
FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
......@@ -1462,6 +1589,11 @@ FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC
FLAC__ASSERT(0 != chain);
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
return false;
}
if (0 == chain->filename) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
return false;
......@@ -1506,6 +1638,11 @@ FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Cha
FLAC__ASSERT(0 != chain);
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
return false;
}
if (0 != chain->filename) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
return false;
......@@ -1538,6 +1675,11 @@ FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC_
FLAC__ASSERT(0 != chain);
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
return false;
}
if (0 != chain->filename) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
return false;
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment