Bulk update copyright dates
[flac.git] / src / metaflac / operations.c
index a9922de..119d497 100644 (file)
@@ -1,5 +1,6 @@
 /* metaflac - Command-line FLAC metadata editor
- * Copyright (C) 2001,2002  Josh Coalson
+ * Copyright (C) 2001-2009  Josh Coalson
+ * Copyright (C) 2011-2016  Xiph.Org Foundation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include "operations.h"
 #include "usage.h"
 #include "utils.h"
 #include "FLAC/assert.h"
 #include "FLAC/metadata.h"
+#include "share/alloc.h"
 #include "share/grabbag.h"
+#include "share/compat.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "operations_shorthand.h"
 
-static void show_version();
+static void show_version(void);
 static FLAC__bool do_major_operation(const CommandLineOptions *options);
 static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options);
 static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
@@ -35,8 +43,8 @@ static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const
 static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
 static FLAC__bool do_shorthand_operations(const CommandLineOptions *options);
 static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options);
-static FLAC__bool do_shorthand_operation(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert);
-static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime);
+static FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert);
+static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan);
 static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write);
 
 static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number);
@@ -46,10 +54,17 @@ static void write_metadata(const char *filename, FLAC__StreamMetadata *block, un
 extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
 
 /* from operations_shorthand_streaminfo.c */
-extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
+extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
 
 /* from operations_shorthand_vorbiscomment.c */
-extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw);
+extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw);
+
+/* from operations_shorthand_cuesheet.c */
+extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
+
+/* from operations_shorthand_picture.c */
+extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
+
 
 FLAC__bool do_operations(const CommandLineOptions *options)
 {
@@ -79,7 +94,7 @@ FLAC__bool do_operations(const CommandLineOptions *options)
  * local routines
  */
 
-void show_version()
+void show_version(void)
 {
        printf("metaflac %s\n", FLAC__VERSION_STRING);
 }
@@ -89,7 +104,7 @@ FLAC__bool do_major_operation(const CommandLineOptions *options)
        unsigned i;
        FLAC__bool ok = true;
 
-       /*@@@ to die after first error,  v---  add '&& ok' here */
+       /* to die after first error,     v---  add '&& ok' here */
        for(i = 0; i < options->num_files; i++)
                ok &= do_major_operation_on_file(options->filenames[i], options);
 
@@ -98,14 +113,19 @@ FLAC__bool do_major_operation(const CommandLineOptions *options)
 
 FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
 {
-       FLAC__bool ok = true, needs_write = false;
+       FLAC__bool ok = true, needs_write = false, is_ogg = false;
        FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
 
        if(0 == chain)
                die("out of memory allocating chain");
 
-       if(!FLAC__metadata_chain_read(chain, filename)) {
-               fprintf(stderr, "%s: ERROR: reading metadata, status = \"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+       /*@@@@ lame way of guessing the file type */
+       if(strlen(filename) >= 4 && (0 == strcmp(filename+strlen(filename)-4, ".oga") || 0 == strcmp(filename+strlen(filename)-4, ".ogg")))
+               is_ogg = true;
+
+       if(! (is_ogg? FLAC__metadata_chain_read_ogg(chain, filename) : FLAC__metadata_chain_read(chain, filename)) ) {
+               print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
+               FLAC__metadata_chain_delete(chain);
                return false;
        }
 
@@ -143,7 +163,7 @@ FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOpt
                        FLAC__metadata_chain_sort_padding(chain);
                ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
                if(!ok)
-                       fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+                       print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
        }
 
        FLAC__metadata_chain_delete(chain);
@@ -168,7 +188,7 @@ FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *
                block = FLAC__metadata_iterator_get_block(iterator);
                ok &= (0 != block);
                if(!ok)
-                       fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
+                       flac_fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
                else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
                        write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
                block_number++;
@@ -182,7 +202,7 @@ FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *
 FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
 {
        (void) chain, (void) options;
-       fprintf(stderr, "ERROR: --append not implemented yet\n"); /*@@@*/
+       flac_fprintf(stderr, "ERROR: --append not implemented yet\n");
        return false;
 }
 
@@ -246,7 +266,9 @@ FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
        if(ok && options->num_files > 0) {
                for(i = 0; i < options->ops.num_operations; i++) {
                        if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
-                               ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime);
+                               ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, false);
+                       else if(options->ops.operations[i].type == OP__SCAN_REPLAY_GAIN)
+                               ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, true);
                }
        }
 
@@ -263,12 +285,17 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi
                die("out of memory allocating chain");
 
        if(!FLAC__metadata_chain_read(chain, filename)) {
-               fprintf(stderr, "%s: ERROR: reading metadata, status = \"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+               print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
                return false;
        }
 
        for(i = 0; i < options->ops.num_operations && ok; i++) {
-               ok &= do_shorthand_operation(options->prefix_with_filename? filename : 0, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
+               /*
+                * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
+                * --add-seekpoint and --import-cuesheet-from are used.
+                */
+               if(options->ops.operations[i].type != OP__ADD_SEEKPOINT)
+                       ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
 
                /* The following seems counterintuitive but the meaning
                 * of 'use_padding' is 'try to keep the overall metadata
@@ -281,12 +308,21 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi
                        use_padding = false;
        }
 
+       /*
+        * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
+        * --add-seekpoint and --import-cuesheet-from are used.
+        */
+       for(i = 0; i < options->ops.num_operations && ok; i++) {
+               if(options->ops.operations[i].type == OP__ADD_SEEKPOINT)
+                       ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
+       }
+
        if(ok && needs_write) {
                if(use_padding)
                        FLAC__metadata_chain_sort_padding(chain);
                ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
                if(!ok)
-                       fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+                       print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
        }
 
        FLAC__metadata_chain_delete(chain);
@@ -294,7 +330,7 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi
        return ok;
 }
 
-FLAC__bool do_shorthand_operation(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert)
+FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert)
 {
        FLAC__bool ok = true;
 
@@ -317,7 +353,7 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__Metadata_Chain *ch
                case OP__SET_CHANNELS:
                case OP__SET_BPS:
                case OP__SET_TOTAL_SAMPLES:
-                       ok = do_shorthand_operation__streaminfo(filename, chain, operation, needs_write);
+                       ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
                        break;
                case OP__SHOW_VC_VENDOR:
                case OP__SHOW_VC_FIELD:
@@ -327,13 +363,22 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__Metadata_Chain *ch
                case OP__SET_VC_FIELD:
                case OP__IMPORT_VC_FROM:
                case OP__EXPORT_VC_TO:
-                       ok = do_shorthand_operation__vorbis_comment(filename, chain, operation, needs_write, !utf8_convert);
+                       ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
+                       break;
+               case OP__IMPORT_CUESHEET_FROM:
+               case OP__EXPORT_CUESHEET_TO:
+                       ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
+                       break;
+               case OP__IMPORT_PICTURE_FROM:
+               case OP__EXPORT_PICTURE_TO:
+                       ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
                        break;
                case OP__ADD_SEEKPOINT:
                        ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
                        break;
                case OP__ADD_REPLAY_GAIN:
-                       /* this command is always executed last */
+               case OP__SCAN_REPLAY_GAIN:
+                       /* these commands are always executed last */
                        ok = true;
                        break;
                case OP__ADD_PADDING:
@@ -348,7 +393,7 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__Metadata_Chain *ch
        return ok;
 }
 
-FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime)
+FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan)
 {
        FLAC__StreamMetadata streaminfo;
        float *title_gains = 0, *title_peaks = 0;
@@ -365,7 +410,7 @@ FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned nu
        for(i = 0; i < num_files; i++) {
                FLAC__ASSERT(0 != filenames[i]);
                if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
-                       fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
+                       flac_fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
                        return false;
                }
                if(first) {
@@ -376,41 +421,45 @@ FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned nu
                }
                else {
                        if(sample_rate != streaminfo.data.stream_info.sample_rate) {
-                               fprintf(stderr, "%s: ERROR: sample rate of %u Hz does not match previous files' %u Hz\n", filenames[i], streaminfo.data.stream_info.sample_rate, sample_rate);
+                               flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz does not match previous files' %u Hz\n", filenames[i], streaminfo.data.stream_info.sample_rate, sample_rate);
                                return false;
                        }
                        if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
-                               fprintf(stderr, "%s: ERROR: resolution of %u bps does not match previous files' %u bps\n", filenames[i], streaminfo.data.stream_info.bits_per_sample, bits_per_sample);
+                               flac_fprintf(stderr, "%s: ERROR: resolution of %u bps does not match previous files' %u bps\n", filenames[i], streaminfo.data.stream_info.bits_per_sample, bits_per_sample);
                                return false;
                        }
                        if(channels != streaminfo.data.stream_info.channels) {
-                               fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
+                               flac_fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
                                return false;
                        }
                }
                if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
-                       fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
+                       flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
                        return false;
                }
                if(channels != 1 && channels != 2) {
-                       fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
+                       flac_fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
                        return false;
                }
        }
        FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
 
-       if(!grabbag__replaygain_init(sample_rate))
+       if(!grabbag__replaygain_init(sample_rate)) {
                FLAC__ASSERT(0);
+               /* double protection */
+               flac_fprintf(stderr, "internal error\n");
+               return false;
+       }
 
        if(
-               0 == (title_gains = (float*)malloc(sizeof(float) * num_files)) ||
-               0 == (title_peaks = (float*)malloc(sizeof(float) * num_files))
+               0 == (title_gains = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) ||
+               0 == (title_peaks = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files))
        )
                die("out of memory allocating space for title gains/peaks");
 
        for(i = 0; i < num_files; i++) {
                if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
-                       fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
+                       flac_fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
                        free(title_gains);
                        free(title_peaks);
                        return false;
@@ -419,11 +468,15 @@ FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned nu
        grabbag__replaygain_get_album(&album_gain, &album_peak);
 
        for(i = 0; i < num_files; i++) {
-               if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
-                       fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
-                       free(title_gains);
-                       free(title_peaks);
-                       return false;
+               if(!scan) {
+                       if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
+                               flac_fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
+                               free(title_gains);
+                               free(title_peaks);
+                               return false;
+                       }
+               } else {
+                       flac_fprintf(stdout, "%s: %f %f %f %f\n", filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i]);
                }
        }
 
@@ -452,7 +505,7 @@ FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metad
        padding->length = length;
 
        if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
-               fprintf(stderr, "%s: ERROR: adding new PADDING block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+               print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename);
                FLAC__metadata_object_delete(padding);
                FLAC__metadata_iterator_delete(iterator);
                return false;
@@ -503,25 +556,25 @@ FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMe
 
 void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
 {
-       unsigned i;
+       unsigned i, j;
 
 /*@@@ yuck, should do this with a varargs function or something: */
-#define PPR if(filename)printf("%s:",filename);
+#define PPR if(filename) { if(raw) printf("%s:",filename); else flac_printf("%s:",filename); }
        PPR; printf("METADATA block #%u\n", block_number);
-       PPR; printf("  type: %u (%s)\n", (unsigned)block->type, block->type<=FLAC__METADATA_TYPE_VORBIS_COMMENT? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
+       PPR; printf("  type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
        PPR; printf("  is last: %s\n", block->is_last? "true":"false");
        PPR; printf("  length: %u\n", block->length);
 
        switch(block->type) {
                case FLAC__METADATA_TYPE_STREAMINFO:
-                       PPR; printf("  minumum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
+                       PPR; printf("  minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
                        PPR; printf("  maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
                        PPR; printf("  minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
                        PPR; printf("  maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
                        PPR; printf("  sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
                        PPR; printf("  channels: %u\n", block->data.stream_info.channels);
                        PPR; printf("  bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
-                       PPR; printf("  total samples: %llu\n", block->data.stream_info.total_samples);
+                       PPR; printf("  total samples: %" PRIu64 "\n", block->data.stream_info.total_samples);
                        PPR; printf("  MD5 signature: ");
                        for(i = 0; i < 16; i++) {
                                printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
@@ -533,26 +586,25 @@ void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned
                        break;
                case FLAC__METADATA_TYPE_APPLICATION:
                        PPR; printf("  application ID: ");
-                       for(i = 0; i < 4; i++) {
-                               PPR; printf("%02x", block->data.application.id[i]);
-                       }
-                       PPR; printf("\n");
+                       for(i = 0; i < 4; i++)
+                               printf("%02x", block->data.application.id[i]);
+                       printf("\n");
                        PPR; printf("  data contents:\n");
                        if(0 != block->data.application.data) {
                                if(hexdump_application)
                                        hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, "    ");
                                else
-                                       (void) fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
+                                       (void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
                        }
                        break;
                case FLAC__METADATA_TYPE_SEEKTABLE:
                        PPR; printf("  seek points: %u\n", block->data.seek_table.num_points);
                        for(i = 0; i < block->data.seek_table.num_points; i++) {
                                if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
-                                       PPR; printf("    point %d: sample_number=%llu, stream_offset=%llu, frame_samples=%u\n", i, block->data.seek_table.points[i].sample_number, block->data.seek_table.points[i].stream_offset, block->data.seek_table.points[i].frame_samples);
+                                       PPR; printf("    point %u: sample_number=%" PRIu64 ", stream_offset=%" PRIu64 ", frame_samples=%u\n", i, block->data.seek_table.points[i].sample_number, block->data.seek_table.points[i].stream_offset, block->data.seek_table.points[i].frame_samples);
                                }
                                else {
-                                       PPR; printf("    point %d: PLACEHOLDER\n", i);
+                                       PPR; printf("    point %u: PLACEHOLDER\n", i);
                                }
                        }
                        break;
@@ -565,8 +617,54 @@ void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned
                                write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
                        }
                        break;
+               case FLAC__METADATA_TYPE_CUESHEET:
+                       PPR; printf("  media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
+                       PPR; printf("  lead-in: %" PRIu64 "\n", block->data.cue_sheet.lead_in);
+                       PPR; printf("  is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false");
+                       PPR; printf("  number of tracks: %u\n", block->data.cue_sheet.num_tracks);
+                       for(i = 0; i < block->data.cue_sheet.num_tracks; i++) {
+                               const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i;
+                               const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1);
+                               const FLAC__bool is_leadout = is_last && track->num_indices == 0;
+                               PPR; printf("    track[%u]\n", i);
+                               PPR; printf("      offset: %" PRIu64 "\n", track->offset);
+                               if(is_last) {
+                                       PPR; printf("      number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID");
+                               }
+                               else {
+                                       PPR; printf("      number: %u\n", (unsigned)track->number);
+                               }
+                               if(!is_leadout) {
+                                       PPR; printf("      ISRC: %s\n", track->isrc);
+                                       PPR; printf("      type: %s\n", track->type == 1? "DATA" : "AUDIO");
+                                       PPR; printf("      pre-emphasis: %s\n", track->pre_emphasis? "true":"false");
+                                       PPR; printf("      number of index points: %u\n", track->num_indices);
+                                       for(j = 0; j < track->num_indices; j++) {
+                                               const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices+j;
+                                               PPR; printf("        index[%u]\n", j);
+                                               PPR; printf("          offset: %" PRIu64 "\n", indx->offset);
+                                               PPR; printf("          number: %u\n", (unsigned)indx->number);
+                                       }
+                               }
+                       }
+                       break;
+               case FLAC__METADATA_TYPE_PICTURE:
+                       PPR; printf("  type: %u (%s)\n", block->data.picture.type, block->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED? FLAC__StreamMetadata_Picture_TypeString[block->data.picture.type] : "UNDEFINED");
+                       PPR; printf("  MIME type: %s\n", block->data.picture.mime_type);
+                       PPR; printf("  description: %s\n", block->data.picture.description);
+                       PPR; printf("  width: %u\n", (unsigned)block->data.picture.width);
+                       PPR; printf("  height: %u\n", (unsigned)block->data.picture.height);
+                       PPR; printf("  depth: %u\n", (unsigned)block->data.picture.depth);
+                       PPR; printf("  colors: %u%s\n", (unsigned)block->data.picture.colors, block->data.picture.colors? "" : " (unindexed)");
+                       PPR; printf("  data length: %u\n", (unsigned)block->data.picture.data_length);
+                       PPR; printf("  data:\n");
+                       if(0 != block->data.picture.data)
+                               hexdump(filename, block->data.picture.data, block->data.picture.data_length, "    ");
+                       break;
                default:
-                       PPR; printf("SKIPPING block of unknown type\n");
+                       PPR; printf("  data contents:\n");
+                       if(0 != block->data.unknown.data)
+                               hexdump(filename, block->data.unknown.data, block->length, "    ");
                        break;
        }
 #undef PPR