1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #ifndef FLAC__METADATA_H
21 #define FLAC__METADATA_H
25 /******************************************************************************
26 (For an example of how all these routines are used, see the source
27 code for the unit tests in src/test_libFLAC/metadata_*.c, or metaflac
29 ******************************************************************************/
31 /** \file include/FLAC/metadata.h
34 * This module provides functions for creating and manipulating FLAC
35 * metadata blocks in memory, and three progressively more powerful
36 * interfaces for traversing and editing metadata in FLAC files.
38 * See the detailed documentation for each interface in the
39 * \link flac_metadata metadata \endlink module.
42 /** \defgroup flac_metadata FLAC/metadata.h: metadata interfaces
46 * This module provides functions for creating and manipulating FLAC
47 * metadata blocks in memory, and three progressively more powerful
48 * interfaces for traversing and editing metadata in FLAC files.
50 * There are three metadata interfaces of increasing complexity:
53 * Read-only access to the STREAMINFO block.
56 * Read-write access to all metadata blocks. This level is write-
57 * efficient in most cases (more on this below), and uses less memory
61 * Read-write access to all metadata blocks. This level is write-
62 * efficient in all cases, but uses more memory since all metadata for
63 * the whole file is read into memory and manipulated before writing
66 * What do we mean by efficient? When writing metadata back to a FLAC
67 * file it is possible to grow or shrink the metadata such that the entire
68 * file must be rewritten. However, if the size remains the same during
69 * changes or PADDING blocks are utilized, only the metadata needs to be
70 * overwritten, which is much faster.
72 * Efficient means the whole file is rewritten at most one time, and only
73 * when necessary. Level 1 is not efficient only in the case that you
74 * cause more than one metadata block to grow or shrink beyond what can
75 * be accomodated by padding. In this case you should probably use level
76 * 2, which allows you to edit all the metadata for a file in memory and
77 * write it out all at once.
79 * All levels know how to skip over and not disturb an ID3v2 tag at the
82 * In addition to the three interfaces, this module defines functions for
83 * creating and manipulating various metadata objects in memory. As we see
84 * from the Format module, FLAC metadata blocks in memory are very primitive
85 * structures for storing information in an efficient way. Reading
86 * information from the structures is easy but creating or modifying them
87 * directly is more complex. The metadata object routines here facilitate
88 * this by taking care of the consistency and memory management drudgery.
90 * Unless you will be using the level 1 or 2 interfaces to modify existing
91 * metadata however, you will not probably not need these.
93 * From a dependency standpoint, none of the encoders or decoders require
94 * the metadata module. This is so that embedded users can strip out the
95 * metadata module from libFLAC to reduce the size and complexity.
103 /** \defgroup flac_metadata_level0 FLAC/metadata.h: metadata level 0 interface
104 * \ingroup flac_metadata
107 * The level 0 interface consists of a single routine to read the
110 * It skips any ID3v2 tag at the head of the file.
115 /** Read the STREAMINFO metadata block of the given FLAC file. This function
116 * will skip any ID3v2 tag at the head of the file.
118 * \param filename The path to the FLAC file to read.
119 * \param streaminfo A pointer to space for the STREAMINFO block.
121 * \code filename != NULL \endcode
122 * \code streaminfo != NULL \endcode
124 * \c true if a valid STREAMINFO block was read from \a filename. Returns
125 * \c false if there was a memory allocation error, a file decoder error,
126 * or the file contained no STREAMINFO block.
128 FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo);
133 /** \defgroup flac_metadata_level1 FLAC/metadata.h: metadata level 1 interface
134 * \ingroup flac_metadata
137 * The level 1 interface provides read-write access to FLAC file metadata and
138 * operates directly on the FLAC file.
140 * The general usage of this interface is:
142 * - Create an iterator using FLAC__metadata_simple_iterator_new()
143 * - Attach it to a file using FLAC__metadata_simple_iterator_init() and check
144 * the exit code. Call FLAC__metadata_simple_iterator_is_writable() to
145 * see if the file is writable, or read-only access is allowed.
146 * - Use FLAC__metadata_simple_iterator_next() and
147 * FLAC__metadata_simple_iterator_prev() to move around the blocks.
148 * This is does not read the actual blocks themselves.
149 * FLAC__metadata_simple_iterator_next() is relatively fast.
150 * FLAC__metadata_simple_iterator_prev() is slower since it needs to search
151 * forward from the front of the file.
152 * - Use FLAC__metadata_simple_iterator_get_block_type() or
153 * FLAC__metadata_simple_iterator_get_block() to access the actual data at
154 * the current iterator position. The returned object is yours to modify
156 * - Use FLAC__metadata_simple_iterator_set_block() to write a modified block
157 * back. You must have write permission to the original file. Make sure to
158 * read the whole comment to FLAC__metadata_simple_iterator_set_block()
160 * - Use FLAC__metadata_simple_iterator_insert_block_after() to add new blocks.
161 * Use the object creation functions from
162 * \link flac_metadata_object here \endlink to generate new objects.
163 * - Use FLAC__metadata_simple_iterator_delete_block() to remove the block
164 * currently referred to by the iterator, or replace it with padding.
165 * - Destroy the iterator with FLAC__metadata_simple_iterator_delete() when
169 * The FLAC file remains open the whole time between
170 * FLAC__metadata_simple_iterator_init() and
171 * FLAC__metadata_simple_iterator_delete(), so make sure you are not altering
172 * the file during this time.
175 * Do not modify the \a is_last, \a length, or \a type fields of returned
176 * FLAC__MetadataType objects. These are managed automatically.
179 * If any of the modification functions
180 * (FLAC__metadata_simple_iterator_set_block(),
181 * FLAC__metadata_simple_iterator_delete_block(),
182 * FLAC__metadata_simple_iterator_insert_block_after(), etc.) return \c false,
183 * you should delete the iterator as it may no longer be valid.
188 struct FLAC__Metadata_SimpleIterator;
189 /** The opaque structure definition for the level 1 iterator type.
191 * \link flac_metadata_level1 metadata level 1 module \endlink
192 * for a detailed description.
194 typedef struct FLAC__Metadata_SimpleIterator FLAC__Metadata_SimpleIterator;
196 /** Status type for FLAC__Metadata_SimpleIterator.
198 * The iterator's current status can be obtained by calling FLAC__metadata_simple_iterator_status().
202 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK = 0,
203 /**< The iterator is in the normal OK state */
205 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT,
206 /**< The data passed into a function violated the function's usage criteria */
208 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE,
209 /**< The iterator could not open the target file */
211 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE,
212 /**< The iterator could not find the FLAC signature at the start of the file */
214 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE,
215 /**< The iterator tried to write to a file that was not writable */
217 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA,
218 /**< The iterator encountered input that does not conform to the FLAC metadata specification */
220 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR,
221 /**< The iterator encountered an error while reading the FLAC file */
223 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR,
224 /**< The iterator encountered an error while seeking in the FLAC file */
226 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR,
227 /**< The iterator encountered an error while writing the FLAC file */
229 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR,
230 /**< The iterator encountered an error renaming the FLAC file */
232 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR,
233 /**< The iterator encountered an error removing the temporary file */
235 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR,
236 /**< Memory allocation failed */
238 FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR
239 /**< The caller violated an assertion or an unexpected error occurred */
241 } FLAC__Metadata_SimpleIteratorStatus;
243 /** Maps a FLAC__Metadata_SimpleIteratorStatus to a C string.
245 * Using a FLAC__Metadata_SimpleIteratorStatus as the index to this array
246 * will give the string equivalent. The contents should not be modified.
248 extern const char * const FLAC__Metadata_SimpleIteratorStatusString[];
251 /** Create a new iterator instance.
253 * \retval FLAC__Metadata_SimpleIterator*
254 * \c NULL if there was an error allocating memory, else the new instance.
256 FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new();
258 /** Free an iterator instance. Deletes the object pointed to by \a iterator.
260 * \param iterator A pointer to an existing iterator.
262 * \code iterator != NULL \endcode
264 void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator);
266 /** Get the current status of the iterator. Call this after a function
267 * returns \c false to get the reason for the error. Also resets the status
268 * to FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK.
270 * \param iterator A pointer to an existing iterator.
272 * \code iterator != NULL \endcode
273 * \retval FLAC__Metadata_SimpleIteratorStatus
274 * The current status of the iterator.
276 FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator);
278 /** Initialize the iterator to point to the first metadata block in the
281 * \param iterator A pointer to an existing iterator.
282 * \param filename The path to the FLAC file.
283 * \param read_only If \c true, the FLAC file will be opened
284 * in read-only mode; if \c false, the FLAC
285 * file will be opened for edit even if no
286 * edits are performed.
287 * \param preserve_file_stats If \c true, the owner and modification
288 * time will be preserved even if the FLAC
289 * file is written to.
291 * \code iterator != NULL \endcode
292 * \code filename != NULL \endcode
294 * \c false if a memory allocation error occurs, the file can't be
295 * opened, or another error occurs, else \c true.
297 FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats);
299 /** Returns \c true if the FLAC file is writable. If \c false, calls to
300 * FLAC__metadata_simple_iterator_set_block() and
301 * FLAC__metadata_simple_iterator_insert_block_after() will fail.
303 * \param iterator A pointer to an existing iterator.
305 * \code iterator != NULL \endcode
309 FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator);
311 /** Moves the iterator forward one metadata block, returning \c false if
312 * already at the end.
314 * \param iterator A pointer to an existing initialized iterator.
316 * \code iterator != NULL \endcode
317 * \a iterator has been successfully initialized with
318 * FLAC__metadata_simple_iterator_init()
320 * \c false if already at the last metadata block of the chain, else
323 FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator);
325 /** Moves the iterator backward one metadata block, returning \c false if
326 * already at the beginning.
328 * \param iterator A pointer to an existing initialized iterator.
330 * \code iterator != NULL \endcode
331 * \a iterator has been successfully initialized with
332 * FLAC__metadata_simple_iterator_init()
334 * \c false if already at the first metadata block of the chain, else
337 FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator);
339 /** Get the type of the metadata block at the current position. This
340 * avoids reading the actual block data which can save time for large
343 * \param iterator A pointer to an existing initialized iterator.
345 * \code iterator != NULL \endcode
346 * \a iterator has been successfully initialized with
347 * FLAC__metadata_simple_iterator_init()
348 * \retval FLAC__MetadataType
349 * The type of the metadata block at the current iterator position.
352 FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator);
354 /** Get the metadata block at the current position. You can modify the
355 * block but must use FLAC__metadata_simple_iterator_set_block() to
356 * write it back to the FLAC file.
358 * You must call FLAC__metadata_object_delete() on the returned object
359 * when you are finished with it.
361 * \param iterator A pointer to an existing initialized iterator.
363 * \code iterator != NULL \endcode
364 * \a iterator has been successfully initialized with
365 * FLAC__metadata_simple_iterator_init()
366 * \retval FLAC__StreamMetadata*
367 * The current metadata block.
369 FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator);
371 /** Write a block back to the FLAC file. This function tries to be
372 * as efficient as possible; how the block is actually written is
373 * shown by the following:
375 * Existing block is a STREAMINFO block and the new block is a
376 * STREAMINFO block: the new block is written in place. Make sure
377 * you know what you're doing when changing the values of a
380 * Existing block is a STREAMINFO block and the new block is a
381 * not a STREAMINFO block: this is an error since the first block
382 * must be a STREAMINFO block. Returns \c false without altering the
385 * Existing block is not a STREAMINFO block and the new block is a
386 * STREAMINFO block: this is an error since there may be only one
387 * STREAMINFO block. Returns \c false without altering the file.
389 * Existing block and new block are the same length: the existing
390 * block will be replaced by the new block, written in place.
392 * Existing block is longer than new block: if use_padding is \c true,
393 * the existing block will be overwritten in place with the new
394 * block followed by a PADDING block, if possible, to make the total
395 * size the same as the existing block. Remember that a padding
396 * block requires at least four bytes so if the difference in size
397 * between the new block and existing block is less than that, the
398 * entire file will have to be rewritten, using the new block's
399 * exact size. If use_padding is \c false, the entire file will be
400 * rewritten, replacing the existing block by the new block.
402 * Existing block is shorter than new block: if use_padding is \c true,
403 * the function will try and expand the new block into the following
404 * PADDING block, if it exists and doing so won't shrink the PADDING
405 * block to less than 4 bytes. If there is no following PADDING
406 * block, or it will shrink to less than 4 bytes, or use_padding is
407 * \c false, the entire file is rewritten, replacing the existing block
408 * with the new block. Note that in this case any following PADDING
409 * block is preserved as is.
411 * After writing the block, the iterator will remain in the same
412 * place, i.e. pointing to the new block.
414 * \param iterator A pointer to an existing initialized iterator.
415 * \param block The block to set.
416 * \param use_padding See above.
418 * \code iterator != NULL \endcode
419 * \a iterator has been successfully initialized with
420 * FLAC__metadata_simple_iterator_init()
421 * \code block != NULL \endcode
423 * \c true if successful, else \c false.
425 FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding);
427 /** This is similar to FLAC__metadata_simple_iterator_set_block()
428 * except that instead of writing over an existing block, it appends
429 * a block after the existing block. \a use_padding is again used to
430 * tell the function to try an expand into following padding in an
431 * attempt to avoid rewriting the entire file.
433 * This function will fail and return \c false if given a STREAMINFO
436 * After writing the block, the iterator will be pointing to the
439 * \param iterator A pointer to an existing initialized iterator.
440 * \param block The block to set.
441 * \param use_padding See above.
443 * \code iterator != NULL \endcode
444 * \a iterator has been successfully initialized with
445 * FLAC__metadata_simple_iterator_init()
446 * \code block != NULL \endcode
448 * \c true if successful, else \c false.
450 FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding);
452 /** Deletes the block at the current position. This will cause the
453 * entire FLAC file to be rewritten, unless \a use_padding is \c true,
454 * in which case the block will be replaced by an equal-sized PADDING
455 * block. The iterator will be left pointing to the block before the
458 * You may not delete the STREAMINFO block.
460 * \param iterator A pointer to an existing initialized iterator.
461 * \param use_padding See above.
463 * \code iterator != NULL \endcode
464 * \a iterator has been successfully initialized with
465 * FLAC__metadata_simple_iterator_init()
467 * \c true if successful, else \c false.
469 FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding);
474 /** \defgroup flac_metadata_level2 FLAC/metadata.h: metadata level 2 interface
475 * \ingroup flac_metadata
478 * The level 2 interface provides read-write access to FLAC file metadata;
479 * all metadata is read into memory, operated on in memory, and then written
480 * to file, which is more efficient than level 1 when editing multiple blocks.
482 * The general usage of this interface is:
484 * - Create a new chain using FLAC__metadata_chain_new(). A chain is a
485 * linked list of FLAC metadata blocks.
486 * - Read all metadata into the the chain from a FLAC file using
487 * FLAC__metadata_chain_read() and check the status.
488 * - Optionally, consolidate the padding using
489 * FLAC__metadata_chain_merge_padding() or
490 * FLAC__metadata_chain_sort_padding().
491 * - Create a new iterator using FLAC__metadata_iterator_new()
492 * - Initialize the iterator to point to the first element in the chain
493 * using FLAC__metadata_iterator_init()
494 * - Traverse the chain using FLAC__metadata_iterator_next and
495 * FLAC__metadata_iterator_prev().
496 * - Get a block for reading or modification using
497 * FLAC__metadata_iterator_get_block(). The pointer to the object
498 * inside the chain is returned, so the block is yours to modify.
499 * Changes will be reflected in the FLAC file when you write the
500 * chain. You can also add and delete blocks (see functions below).
501 * - When done, write out the chain using FLAC__metadata_chain_write().
502 * Make sure to read the whole comment to the function below.
503 * - Delete the chain using FLAC__metadata_chain_delete().
506 * Even though the FLAC file is not open while the chain is being
507 * manipulated, you must not alter the file externally during
508 * this time. The chain assumes the FLAC file will not change
509 * between the time of FLAC__metadata_chain_read() and
510 * FLAC__metadata_chain_write().
513 * Do not modify the is_last, length, or type fields of returned
514 * FLAC__MetadataType objects. These are managed automatically.
517 * The metadata objects returned by FLAC__metadata_iterator_get_block()
518 * are owned by the chain; do not FLAC__metadata_object_delete() them.
519 * In the same way, blocks passed to FLAC__metadata_iterator_set_block()
520 * become owned by the chain and they will be deleted when the chain is
526 struct FLAC__Metadata_Chain;
527 /** The opaque structure definition for the level 2 chain type.
529 typedef struct FLAC__Metadata_Chain FLAC__Metadata_Chain;
531 struct FLAC__Metadata_Iterator;
532 /** The opaque structure definition for the level 2 iterator type.
534 typedef struct FLAC__Metadata_Iterator FLAC__Metadata_Iterator;
537 FLAC__METADATA_CHAIN_STATUS_OK = 0,
538 /**< The chain is in the normal OK state */
540 FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT,
541 /**< The data passed into a function violated the function's usage criteria */
543 FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE,
544 /**< The chain could not open the target file */
546 FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE,
547 /**< The chain could not find the FLAC signature at the start of the file */
549 FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE,
550 /**< The chain tried to write to a file that was not writable */
552 FLAC__METADATA_CHAIN_STATUS_BAD_METADATA,
553 /**< The chain encountered input that does not conform to the FLAC metadata specification */
555 FLAC__METADATA_CHAIN_STATUS_READ_ERROR,
556 /**< The chain encountered an error while reading the FLAC file */
558 FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR,
559 /**< The chain encountered an error while seeking in the FLAC file */
561 FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR,
562 /**< The chain encountered an error while writing the FLAC file */
564 FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR,
565 /**< The chain encountered an error renaming the FLAC file */
567 FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR,
568 /**< The chain encountered an error removing the temporary file */
570 FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR,
571 /**< Memory allocation failed */
573 FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR
574 /**< The caller violated an assertion or an unexpected error occurred */
576 } FLAC__Metadata_ChainStatus;
578 /** Maps a FLAC__Metadata_ChainStatus to a C string.
580 * Using a FLAC__Metadata_ChainStatus as the index to this array
581 * will give the string equivalent. The contents should not be modified.
583 extern const char * const FLAC__Metadata_ChainStatusString[];
585 /*********** FLAC__Metadata_Chain ***********/
587 /** Create a new chain instance.
589 * \retval FLAC__Metadata_Chain*
590 * \c NULL if there was an error allocating memory, else the new instance.
592 FLAC__Metadata_Chain *FLAC__metadata_chain_new();
594 /** Free a chain instance. Deletes the object pointed to by \a chain.
596 * \param chain A pointer to an existing chain.
598 * \code chain != NULL \endcode
600 void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain);
602 /** Get the current status of the chain. Call this after a function
603 * returns \c false to get the reason for the error. Also resets the
604 * status to FLAC__METADATA_CHAIN_STATUS_OK.
606 * \param chain A pointer to an existing chain.
608 * \code chain != NULL \endcode
609 * \retval FLAC__Metadata_ChainStatus
610 * The current status of the chain.
612 FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain);
614 /** Read all metadata from a FLAC file into the chain.
616 * \param chain A pointer to an existing chain.
617 * \param filename The path to the FLAC file to read.
619 * \code chain != NULL \endcode
620 * \code filename != NULL \endcode
622 * \c true if a valid list of metadata blocks was read from
623 * \a filename, else \c false. On failure, check the status with
624 * FLAC__metadata_chain_status().
626 FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename);
628 /** Write all metadata out to the FLAC file. This function tries to be as
629 * efficient as possible; how the metadata is actually written is shown by
632 * If the current chain is the same size as the existing metadata, the new
633 * data is written in place.
635 * If the current chain is longer than the existing metadata, and
636 * \a use_padding is \c true, and the last block is a PADDING block of
637 * sufficient length, the function will truncate the final padding block
638 * so that the overall size of the metadata is the same as the existing
639 * metadata, and then just rewrite the metadata. Otherwise, if not all of
640 * the above conditions are met, the entire FLAC file must be rewritten.
641 * If you want to use padding this way it is a good idea to call
642 * FLAC__metadata_chain_sort_padding() first so that you have the maximum
643 * amount of padding to work with, unless you need to preserve ordering
644 * of the PADDING blocks for some reason.
646 * If the current chain is shorter than the existing metadata, and
647 * \a use_padding is \c true, and the final block is a PADDING block, the padding
648 * is extended to make the overall size the same as the existing data. If
649 * \a use_padding is \c true and the last block is not a PADDING block, a new
650 * PADDING block is added to the end of the new data to make it the same
651 * size as the existing data (if possible, see the note to
652 * FLAC__metadata_simple_iterator_set_block() about the four byte limit)
653 * and the new data is written in place. If none of the above apply or
654 * \a use_padding is \c false, the entire FLAC file is rewritten.
656 * If \a preserve_file_stats is \c true, the owner and modification time will
657 * be preserved even if the FLAC file is written.
659 * \param chain A pointer to an existing chain.
660 * \param use_padding See above.
661 * \param preserve_file_stats See above.
663 * \code chain != NULL \endcode
665 * \c true if the write succeeded, else \c false. On failure,
666 * check the status with FLAC__metadata_chain_status().
668 FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats);
670 /** Merge adjacent PADDING blocks into a single block.
672 * \note This function does not write to the FLAC file, it only
673 * modifies the chain.
675 * \warning Any iterator on the current chain will become invalid after this
676 * call. You should delete the iterator and get a new one.
678 * \param chain A pointer to an existing chain.
680 * \code chain != NULL \endcode
682 void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain);
684 /** This function will move all PADDING blocks to the end on the metadata,
685 * then merge them into a single block.
687 * \note This function does not write to the FLAC file, it only
688 * modifies the chain.
690 * \warning Any iterator on the current chain will become invalid after this
691 * call. You should delete the iterator and get a new one.
693 * \param chain A pointer to an existing chain.
695 * \code chain != NULL \endcode
697 void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain);
700 /*********** FLAC__Metadata_Iterator ***********/
702 /** Create a new iterator instance.
704 * \retval FLAC__Metadata_Iterator*
705 * \c NULL if there was an error allocating memory, else the new instance.
707 FLAC__Metadata_Iterator *FLAC__metadata_iterator_new();
709 /** Free an iterator instance. Deletes the object pointed to by \a iterator.
711 * \param iterator A pointer to an existing iterator.
713 * \code iterator != NULL \endcode
715 void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator);
717 /** Initialize the iterator to point to the first metadata block in the
720 * \param iterator A pointer to an existing iterator.
721 * \param chain A pointer to an existing and initialized (read) chain.
723 * \code iterator != NULL \endcode
724 * \code chain != NULL \endcode
726 void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain);
728 /** Moves the iterator forward one metadata block, returning \c false if
729 * already at the end.
731 * \param iterator A pointer to an existing initialized iterator.
733 * \code iterator != NULL \endcode
734 * \a iterator has been successfully initialized with
735 * FLAC__metadata_iterator_init()
737 * \c false if already at the last metadata block of the chain, else
740 FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator);
742 /** Moves the iterator backward one metadata block, returning \c false if
743 * already at the beginning.
745 * \param iterator A pointer to an existing initialized iterator.
747 * \code iterator != NULL \endcode
748 * \a iterator has been successfully initialized with
749 * FLAC__metadata_iterator_init()
751 * \c false if already at the first metadata block of the chain, else
754 FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator);
756 /** Get the type of the metadata block at the current position.
758 * \param iterator A pointer to an existing initialized iterator.
760 * \code iterator != NULL \endcode
761 * \a iterator has been successfully initialized with
762 * FLAC__metadata_iterator_init()
763 * \retval FLAC__MetadataType
764 * The type of the metadata block at the current iterator position.
766 FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator);
768 /** Get the metadata block at the current position. You can modify
769 * the block in place but must write the chain before the changes
770 * are reflected to the FLAC file. You do not need to call
771 * FLAC__metadata_iterator_set_block() to reflect the changes;
772 * the pointer returned by FLAC__metadata_iterator_get_block()
773 * points directly into the chain.
776 * Do not call FLAC__metadata_object_delete() on the returned object;
777 * to delete a block use FLAC__metadata_iterator_delete_block().
779 * \param iterator A pointer to an existing initialized iterator.
781 * \code iterator != NULL \endcode
782 * \a iterator has been successfully initialized with
783 * FLAC__metadata_iterator_init()
784 * \retval FLAC__StreamMetadata*
785 * The current metadata block.
787 FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator);
789 /** Set the metadata block at the current position, replacing the existing
790 * block. The new block passed in becomes owned by the chain and it will be
791 * deleted when the chain is deleted.
793 * \param iterator A pointer to an existing initialized iterator.
794 * \param block A pointer to a metadata block.
796 * \code iterator != NULL \endcode
797 * \a iterator has been successfully initialized with
798 * FLAC__metadata_iterator_init()
799 * \code block != NULL \endcode
801 * \c false if the conditions in the above description are not met, or
802 * a memory allocation error occurs, otherwise \c true.
804 FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block);
806 /** Removes the current block from the chain. If \a replace_with_padding is
807 * \c true, the block will instead be replaced with a padding block of equal
808 * size. You can not delete the STREAMINFO block. The iterator will be
809 * left pointing to the block before the one just "deleted", even if
810 * \a replace_with_padding is \c true.
812 * \param iterator A pointer to an existing initialized iterator.
813 * \param replace_with_padding See above.
815 * \code iterator != NULL \endcode
816 * \a iterator has been successfully initialized with
817 * FLAC__metadata_iterator_init()
819 * \c false if the conditions in the above description are not met,
822 FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding);
824 /** Insert a new block before the current block. You cannot insert a block
825 * before the first STREAMINFO block. You cannot insert a STREAMINFO block
826 * as there can be only one, the one that already exists at the head when you
827 * read in a chain. The chain takes ownership of the new block and it will be
828 * deleted when the chain is deleted. The iterator will be left pointing to
831 * \param iterator A pointer to an existing initialized iterator.
832 * \param block A pointer to a metadata block to insert.
834 * \code iterator != NULL \endcode
835 * \a iterator has been successfully initialized with
836 * FLAC__metadata_iterator_init()
838 * \c false if the conditions in the above description are not met, or
839 * a memory allocation error occurs, otherwise \c true.
841 FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block);
843 /** Insert a new block after the current block. You cannot insert a STREAMINFO
844 * block as there can be only one, the one that already exists at the head when
845 * you read in a chain. The chain takes ownership of the new block and it will
846 * be deleted when the chain is deleted. The iterator will be left pointing to
849 * \param iterator A pointer to an existing initialized iterator.
850 * \param block A pointer to a metadata block to insert.
852 * \code iterator != NULL \endcode
853 * \a iterator has been successfully initialized with
854 * FLAC__metadata_iterator_init()
856 * \c false if the conditions in the above description are not met, or
857 * a memory allocation error occurs, otherwise \c true.
859 FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block);
864 /** \defgroup flac_metadata_object FLAC/metadata.h: metadata object methods
865 * \ingroup flac_metadata
868 * This module contains methods for manipulating FLAC metadata objects.
870 * Since many are variable length we have to be careful about the memory
871 * management. We decree that all pointers to data in the object are
872 * owned by the object and memory-managed by the object.
874 * Use the FLAC__metadata_object_new() and FLAC__metadata_object_delete()
875 * functions to create all instances. When using the
876 * FLAC__metadata_object_set_*() functions to set pointers to data, set
877 * \a copy to \c true to have the function make it's own copy of the data, or
878 * to \c false to give the object ownership of your data. In the latter case
879 * your pointer must be freeable by free() and will be free()d when the object
880 * is FLAC__metadata_object_delete()d. It is legal to pass a null pointer as
881 * the data pointer to a FLAC__metadata_object_set_*() function as long as
882 * the length argument is 0 and the \a copy argument is \c false.
884 * The FLAC__metadata_object_new() and FLAC__metadata_object_clone() function
885 * will return \c NULL in the case of a memory allocation error, otherwise a new
886 * object. The FLAC__metadata_object_set_*() functions return \c false in the
887 * case of a memory allocation error.
889 * We don't have the convenience of C++ here, so note that the library relies
890 * on you to keep the types straight. In other words, if you pass, for
891 * example, a FLAC__StreamMetadata* that represents a STREAMINFO block to
892 * FLAC__metadata_object_application_set_data(), you will get an assertion
895 * There is no need to recalculate the length field on metadata blocks you
896 * have modified. They will be calculated automatically before they are
897 * written back to a file.
903 /** Create a new metadata object instance of the given type.
905 * The object will be "empty"; i.e. values and data pointers will be \c 0.
907 * \param type Type of object to create
908 * \retval FLAC__StreamMetadata*
909 * \c NULL if there was an error allocating memory, else the new instance.
911 FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type);
913 /** Create a copy of an existing metadata object.
915 * The copy is a "deep" copy, i.e. dynamically allocated data within the
916 * object is also copied. The caller takes ownership of the new block and
917 * is responsible for freeing it with FLAC__metadata_object_delete().
919 * \param object Pointer to object to copy.
921 * \code object != NULL \endcode
922 * \retval FLAC__StreamMetadata*
923 * \c NULL if there was an error allocating memory, else the new instance.
925 FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMetadata *object);
927 /** Free a metadata object. Deletes the object pointed to by \a object.
929 * The delete is a "deep" delete, i.e. dynamically allocated data within the
930 * object is also deleted.
932 * \param object A pointer to an existing object.
934 * \code object != NULL \endcode
936 void FLAC__metadata_object_delete(FLAC__StreamMetadata *object);
938 /** Compares two metadata objects.
940 * The compare is "deep", i.e. dynamically allocated data within the
941 * object is also compared.
943 * \param block1 A pointer to an existing object.
944 * \param block2 A pointer to an existing object.
946 * \code block1 != NULL \endcode
947 * \code block2 != NULL \endcode
949 * \c true if objects are identical, else \c false.
951 FLAC__bool FLAC__metadata_object_is_equal(const FLAC__StreamMetadata *block1, const FLAC__StreamMetadata *block2);
953 /** Sets the application data of an APPLICATION block.
955 * If \a copy is \c true, a copy of the data is stored; otherwise, the object
956 * takes ownership of the pointer. Returns \c false if \a copy == \c true
959 * \param object A pointer to an existing APPLICATION object.
960 * \param data A pointer to the data to set.
961 * \param length The length of \a data in bytes.
962 * \param copy See above.
964 * \code object != NULL \endcode
965 * \code object->type == FLAC__METADATA_TYPE_APPLICATION \endcode
966 * \code (data != NULL && length > 0) ||
967 * (data == NULL && length == 0 && copy == false) \endcode
969 * \c false if \a copy is \c true and malloc fails, else \c true.
971 FLAC__bool FLAC__metadata_object_application_set_data(FLAC__StreamMetadata *object, FLAC__byte *data, unsigned length, FLAC__bool copy);
973 /** Resize the seekpoint array.
975 * If the size shrinks, elements will truncated; if it grows, new placeholder
976 * points will be added to the end.
978 * \param object A pointer to an existing SEEKTABLE object.
979 * \param new_num_points The desired length of the array; may be \c 0.
981 * \code object != NULL \endcode
982 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
983 * \code (object->data.seek_table.points == NULL && object->data.seek_table.num_points == 0) ||
984 * (object->data.seek_table.points != NULL && object->data.seek_table.num_points > 0) \endcode
986 * \c false if memory allocation error, else \c true.
988 FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMetadata *object, unsigned new_num_points);
990 /** Set a seekpoint in a seektable.
992 * \param object A pointer to an existing SEEKTABLE object.
993 * \param point_num Index into seekpoint array to set.
994 * \param point The point to set.
996 * \code object != NULL \endcode
997 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
998 * \code object->data.seek_table.num_points > point_num \endcode
1000 void FLAC__metadata_object_seektable_set_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point);
1002 /** Insert a seekpoint into a seektable.
1004 * \param object A pointer to an existing SEEKTABLE object.
1005 * \param point_num Index into seekpoint array to set.
1006 * \param point The point to set.
1008 * \code object != NULL \endcode
1009 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1010 * \code object->data.seek_table.num_points >= point_num \endcode
1011 * \retval FLAC__bool
1012 * \c false if memory allocation error, else \c true.
1014 FLAC__bool FLAC__metadata_object_seektable_insert_point(FLAC__StreamMetadata *object, unsigned point_num, FLAC__StreamMetadata_SeekPoint point);
1016 /** Delete a seekpoint from a seektable.
1018 * \param object A pointer to an existing SEEKTABLE object.
1019 * \param point_num Index into seekpoint array to set.
1021 * \code object != NULL \endcode
1022 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1023 * \code object->data.seek_table.num_points > point_num \endcode
1024 * \retval FLAC__bool
1025 * \c false if memory allocation error, else \c true.
1027 FLAC__bool FLAC__metadata_object_seektable_delete_point(FLAC__StreamMetadata *object, unsigned point_num);
1029 /** Check a seektable to see if it conforms to the FLAC specification.
1030 * See the format specification for limits on the contents of the
1033 * \param object A pointer to an existing SEEKTABLE object.
1035 * \code object != NULL \endcode
1036 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1037 * \retval FLAC__bool
1038 * \c false if seek table is illegal, else \c true.
1040 FLAC__bool FLAC__metadata_object_seektable_is_legal(const FLAC__StreamMetadata *object);
1042 /** Append a number of placeholder points to the end of a seek table.
1045 * As with the other ..._seektable_template_... functions, you should
1046 * call FLAC__metadata_object_seektable_template_sort() when finished
1047 * to make the seek table legal.
1049 * \param object A pointer to an existing SEEKTABLE object.
1050 * \param num The number of placeholder points to append.
1052 * \code object != NULL \endcode
1053 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1054 * \retval FLAC__bool
1055 * \c false if memory allocation fails, else \c true.
1057 FLAC__bool FLAC__metadata_object_seektable_template_append_placeholders(FLAC__StreamMetadata *object, unsigned num);
1059 /** Append a specific seek point template to the end of a seek table.
1062 * As with the other ..._seektable_template_... functions, you should
1063 * call FLAC__metadata_object_seektable_template_sort() when finished
1064 * to make the seek table legal.
1066 * \param object A pointer to an existing SEEKTABLE object.
1067 * \param sample_number The sample number of the seek point template.
1069 * \code object != NULL \endcode
1070 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1071 * \retval FLAC__bool
1072 * \c false if memory allocation fails, else \c true.
1074 FLAC__bool FLAC__metadata_object_seektable_template_append_point(FLAC__StreamMetadata *object, FLAC__uint64 sample_number);
1076 /** Append specific seek point templates to the end of a seek table.
1079 * As with the other ..._seektable_template_... functions, you should
1080 * call FLAC__metadata_object_seektable_template_sort() when finished
1081 * to make the seek table legal.
1083 * \param object A pointer to an existing SEEKTABLE object.
1084 * \param sample_numbers An array of sample numbers for the seek points.
1085 * \param num The number of seek point templates to append.
1087 * \code object != NULL \endcode
1088 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1089 * \retval FLAC__bool
1090 * \c false if memory allocation fails, else \c true.
1092 FLAC__bool FLAC__metadata_object_seektable_template_append_points(FLAC__StreamMetadata *object, FLAC__uint64 sample_numbers[], unsigned num);
1094 /** Append a set of evenly-spaced seek point templates to the end of a
1098 * As with the other ..._seektable_template_... functions, you should
1099 * call FLAC__metadata_object_seektable_template_sort() when finished
1100 * to make the seek table legal.
1102 * \param object A pointer to an existing SEEKTABLE object.
1103 * \param num The number of placeholder points to append.
1104 * \param total_samples The total number of samples to be encoded;
1105 * the seekpoints will be spaced approximately
1106 * \a total_samples / \a num samples apart.
1108 * \code object != NULL \endcode
1109 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1110 * \retval FLAC__bool
1111 * \c false if memory allocation fails, else \c true.
1113 FLAC__bool FLAC__metadata_object_seektable_template_append_spaced_points(FLAC__StreamMetadata *object, unsigned num, FLAC__uint64 total_samples);
1115 /** Sort a seek table's seek points according to the format specification,
1116 * removing duplicates.
1118 * \param object A pointer to a seek table to be sorted.
1119 * \param compact If \c false, behaves like FLAC__format_seektable_sort().
1120 * If \c true, duplicates are deleted and the seek table is
1121 * shrunk appropriately; the number of placeholder points
1122 * present in the seek table will be the same after the call
1125 * \code object != NULL \endcode
1126 * \code object->type == FLAC__METADATA_TYPE_SEEKTABLE \endcode
1127 * \retval FLAC__bool
1128 * \c false if realloc fails, else \c true.
1130 FLAC__bool FLAC__metadata_object_seektable_template_sort(FLAC__StreamMetadata *object, FLAC__bool compact);
1132 /** Sets the vendor string in a VORBIS_COMMENT block.
1134 * If \a copy is \c true, a copy of the entry is stored; otherwise, the object
1135 * takes ownership of the \c entry->entry pointer. Returns \c false if
1136 * \a copy == \c true and malloc fails.
1138 * \param object A pointer to an existing VORBIS_COMMENT object.
1139 * \param entry The entry to set the vendor string to.
1140 * \param copy See above.
1142 * \code object != NULL \endcode
1143 * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode
1144 * \code (entry->entry != NULL && entry->length > 0) ||
1145 * (entry->entry == NULL && entry->length == 0 && copy == false) \endcode
1146 * \retval FLAC__bool
1147 * \c false if \a copy is \c true and malloc fails, else \c true.
1149 FLAC__bool FLAC__metadata_object_vorbiscomment_set_vendor_string(FLAC__StreamMetadata *object, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);
1151 /** Resize the comment array.
1153 * If the size shrinks, elements will truncated; if it grows, new empty
1154 * fields will be added to the end.
1156 * \param object A pointer to an existing VORBIS_COMMENT object.
1157 * \param new_num_comments The desired length of the array; may be \c 0.
1159 * \code object != NULL \endcode
1160 * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode
1161 * \code (object->data.vorbis_comment.comments == NULL && object->data.vorbis_comment.num_comments == 0) ||
1162 * (object->data.vorbis_comment.comments != NULL && object->data.vorbis_comment.num_comments > 0) \endcode
1163 * \retval FLAC__bool
1164 * \c false if memory allocation error, else \c true.
1166 FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__StreamMetadata *object, unsigned new_num_comments);
1168 /** Sets a comment in a VORBIS_COMMENT block.
1170 * If \a copy is \c true, a copy of the entry is stored; otherwise, the object
1171 * takes ownership of the \c entry->entry pointer. Returns \c false if
1172 * \a copy == \c true and malloc fails.
1174 * \param object A pointer to an existing VORBIS_COMMENT object.
1175 * \param comment_num Index into comment array to set.
1176 * \param entry The entry to set the comment to.
1177 * \param copy See above.
1179 * \code object != NULL \endcode
1180 * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode
1181 * \code (entry->entry != NULL && entry->length > 0) ||
1182 * (entry->entry == NULL && entry->length == 0 && copy == false) \endcode
1183 * \retval FLAC__bool
1184 * \c false if \a copy is \c true and malloc fails, else \c true.
1186 FLAC__bool FLAC__metadata_object_vorbiscomment_set_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);
1188 /** Insert a comment in a VORBIS_COMMENT block at the given index.
1190 * If \a copy is \c true, a copy of the entry is stored; otherwise, the object
1191 * takes ownership of the \c entry->entry pointer. Returns \c false if
1192 * \a copy == \c true and malloc fails.
1194 * \param object A pointer to an existing VORBIS_COMMENT object.
1195 * \param comment_num The index at which to insert the comment. The comments
1196 * at and after \a comment_num move right one position.
1197 * To append a comment to the end, set \a comment_num to
1198 * \c object->data.vorbis_comment.num_comments .
1199 * \param entry The comment to insert.
1200 * \param copy See above.
1202 * \code object != NULL \endcode
1203 * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode
1204 * \code object->data.vorbis_comment.num_comments >= comment_num \endcode
1205 * \code (entry->entry != NULL && entry->length > 0) ||
1206 * (entry->entry == NULL && entry->length == 0 && copy == false) \endcode
1207 * \retval FLAC__bool
1208 * \c false if \a copy is \c true and malloc fails, else \c true.
1210 FLAC__bool FLAC__metadata_object_vorbiscomment_insert_comment(FLAC__StreamMetadata *object, unsigned comment_num, FLAC__StreamMetadata_VorbisComment_Entry entry, FLAC__bool copy);
1212 /** Delete a comment in a VORBIS_COMMENT block at the given index.
1214 * If \a copy is \c true, a copy of the entry is stored; otherwise, the object
1215 * takes ownership of the \c entry->entry pointer. Returns \c false if
1216 * \a copy == \c true and malloc fails.
1218 * \param object A pointer to an existing VORBIS_COMMENT object.
1219 * \param comment_num The index of the comment to delete.
1221 * \code object != NULL \endcode
1222 * \code object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT \endcode
1223 * \code object->data.vorbis_comment.num_comments > comment_num \endcode
1224 * \code (entry->entry != NULL && entry->length > 0) ||
1225 * (entry->entry == NULL && entry->length == 0 && copy == false) \endcode
1226 * \retval FLAC__bool
1227 * \c false if realloc fails, else \c true.
1229 FLAC__bool FLAC__metadata_object_vorbiscomment_delete_comment(FLAC__StreamMetadata *object, unsigned comment_num);