add support for new PICTURE metadata block
[flac.git] / src / metaflac / operations.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "operations.h"
24 #include "usage.h"
25 #include "utils.h"
26 #include "FLAC/assert.h"
27 #include "FLAC/metadata.h"
28 #include "share/grabbag.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 static void show_version();
34 static FLAC__bool do_major_operation(const CommandLineOptions *options);
35 static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options);
36 static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
37 static FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
38 static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
39 static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
40 static FLAC__bool do_shorthand_operations(const CommandLineOptions *options);
41 static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options);
42 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);
43 static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime);
44 static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write);
45
46 static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number);
47 static void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application);
48
49 /* from operations_shorthand_seektable.c */
50 extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
51
52 /* from operations_shorthand_streaminfo.c */
53 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);
54
55 /* from operations_shorthand_vorbiscomment.c */
56 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);
57
58 /* from operations_shorthand_cuesheet.c */
59 extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
60
61
62 FLAC__bool do_operations(const CommandLineOptions *options)
63 {
64         FLAC__bool ok = true;
65
66         if(options->show_long_help) {
67                 long_usage(0);
68         }
69         if(options->show_version) {
70                 show_version();
71         }
72         else if(options->args.checks.num_major_ops > 0) {
73                 FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0);
74                 FLAC__ASSERT(options->args.checks.num_major_ops == 1);
75                 FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations);
76                 ok = do_major_operation(options);
77         }
78         else if(options->args.checks.num_shorthand_ops > 0) {
79                 FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations);
80                 ok = do_shorthand_operations(options);
81         }
82
83         return ok;
84 }
85
86 /*
87  * local routines
88  */
89
90 void show_version()
91 {
92         printf("metaflac %s\n", FLAC__VERSION_STRING);
93 }
94
95 FLAC__bool do_major_operation(const CommandLineOptions *options)
96 {
97         unsigned i;
98         FLAC__bool ok = true;
99
100         /*@@@ to die after first error,  v---  add '&& ok' here */
101         for(i = 0; i < options->num_files; i++)
102                 ok &= do_major_operation_on_file(options->filenames[i], options);
103
104         return ok;
105 }
106
107 FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
108 {
109         FLAC__bool ok = true, needs_write = false;
110         FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
111
112         if(0 == chain)
113                 die("out of memory allocating chain");
114
115         if(!FLAC__metadata_chain_read(chain, filename)) {
116                 print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
117                 FLAC__metadata_chain_delete(chain);
118                 return false;
119         }
120
121         switch(options->ops.operations[0].type) {
122                 case OP__LIST:
123                         ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
124                         break;
125                 case OP__APPEND:
126                         ok = do_major_operation__append(chain, options);
127                         needs_write = true;
128                         break;
129                 case OP__REMOVE:
130                         ok = do_major_operation__remove(chain, options);
131                         needs_write = true;
132                         break;
133                 case OP__REMOVE_ALL:
134                         ok = do_major_operation__remove_all(chain, options);
135                         needs_write = true;
136                         break;
137                 case OP__MERGE_PADDING:
138                         FLAC__metadata_chain_merge_padding(chain);
139                         needs_write = true;
140                         break;
141                 case OP__SORT_PADDING:
142                         FLAC__metadata_chain_sort_padding(chain);
143                         needs_write = true;
144                         break;
145                 default:
146                         FLAC__ASSERT(0);
147                         return false;
148         }
149
150         if(ok && needs_write) {
151                 if(options->use_padding)
152                         FLAC__metadata_chain_sort_padding(chain);
153                 ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
154                 if(!ok)
155                         print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
156         }
157
158         FLAC__metadata_chain_delete(chain);
159
160         return ok;
161 }
162
163 FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
164 {
165         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
166         FLAC__StreamMetadata *block;
167         FLAC__bool ok = true;
168         unsigned block_number;
169
170         if(0 == iterator)
171                 die("out of memory allocating iterator");
172
173         FLAC__metadata_iterator_init(iterator, chain);
174
175         block_number = 0;
176         do {
177                 block = FLAC__metadata_iterator_get_block(iterator);
178                 ok &= (0 != block);
179                 if(!ok)
180                         fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
181                 else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
182                         write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
183                 block_number++;
184         } while(ok && FLAC__metadata_iterator_next(iterator));
185
186         FLAC__metadata_iterator_delete(iterator);
187
188         return ok;
189 }
190
191 FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
192 {
193         (void) chain, (void) options;
194         fprintf(stderr, "ERROR: --append not implemented yet\n"); /*@@@*/
195         return false;
196 }
197
198 FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
199 {
200         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
201         FLAC__bool ok = true;
202         unsigned block_number;
203
204         if(0 == iterator)
205                 die("out of memory allocating iterator");
206
207         FLAC__metadata_iterator_init(iterator, chain);
208
209         block_number = 0;
210         while(ok && FLAC__metadata_iterator_next(iterator)) {
211                 block_number++;
212                 if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) {
213                         ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
214                         if(options->use_padding)
215                                 ok &= FLAC__metadata_iterator_next(iterator);
216                 }
217         }
218
219         FLAC__metadata_iterator_delete(iterator);
220
221         return ok;
222 }
223
224 FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
225 {
226         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
227         FLAC__bool ok = true;
228
229         if(0 == iterator)
230                 die("out of memory allocating iterator");
231
232         FLAC__metadata_iterator_init(iterator, chain);
233
234         while(ok && FLAC__metadata_iterator_next(iterator)) {
235                 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
236                 if(options->use_padding)
237                         ok &= FLAC__metadata_iterator_next(iterator);
238         }
239
240         FLAC__metadata_iterator_delete(iterator);
241
242         return ok;
243 }
244
245 FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
246 {
247         unsigned i;
248         FLAC__bool ok = true;
249
250         /* to die after first error,     v---  add '&& ok' here */
251         for(i = 0; i < options->num_files; i++)
252                 ok &= do_shorthand_operations_on_file(options->filenames[i], options);
253
254         /* check if OP__ADD_REPLAY_GAIN requested */
255         if(ok && options->num_files > 0) {
256                 for(i = 0; i < options->ops.num_operations; i++) {
257                         if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
258                                 ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime);
259                 }
260         }
261
262         return ok;
263 }
264
265 FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
266 {
267         unsigned i;
268         FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
269         FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
270
271         if(0 == chain)
272                 die("out of memory allocating chain");
273
274         if(!FLAC__metadata_chain_read(chain, filename)) {
275                 print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
276                 return false;
277         }
278
279         for(i = 0; i < options->ops.num_operations && ok; i++) {
280                 /*
281                  * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
282                  * --add-seekpoint and --import-cuesheet-from are used.
283                  */
284                 if(options->ops.operations[i].type != OP__ADD_SEEKPOINT)
285                         ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
286
287                 /* The following seems counterintuitive but the meaning
288                  * of 'use_padding' is 'try to keep the overall metadata
289                  * to its original size, adding or truncating extra
290                  * padding if necessary' which is why we need to turn it
291                  * off in this case.  If we don't, the extra padding block
292                  * will just be truncated.
293                  */
294                 if(options->ops.operations[i].type == OP__ADD_PADDING)
295                         use_padding = false;
296         }
297
298         /*
299          * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
300          * --add-seekpoint and --import-cuesheet-from are used.
301          */
302         for(i = 0; i < options->ops.num_operations && ok; i++) {
303                 if(options->ops.operations[i].type == OP__ADD_SEEKPOINT)
304                         ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
305         }
306
307         if(ok && needs_write) {
308                 if(use_padding)
309                         FLAC__metadata_chain_sort_padding(chain);
310                 ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
311                 if(!ok)
312                         print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
313         }
314
315         FLAC__metadata_chain_delete(chain);
316
317         return ok;
318 }
319
320 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)
321 {
322         FLAC__bool ok = true;
323
324         switch(operation->type) {
325                 case OP__SHOW_MD5SUM:
326                 case OP__SHOW_MIN_BLOCKSIZE:
327                 case OP__SHOW_MAX_BLOCKSIZE:
328                 case OP__SHOW_MIN_FRAMESIZE:
329                 case OP__SHOW_MAX_FRAMESIZE:
330                 case OP__SHOW_SAMPLE_RATE:
331                 case OP__SHOW_CHANNELS:
332                 case OP__SHOW_BPS:
333                 case OP__SHOW_TOTAL_SAMPLES:
334                 case OP__SET_MD5SUM:
335                 case OP__SET_MIN_BLOCKSIZE:
336                 case OP__SET_MAX_BLOCKSIZE:
337                 case OP__SET_MIN_FRAMESIZE:
338                 case OP__SET_MAX_FRAMESIZE:
339                 case OP__SET_SAMPLE_RATE:
340                 case OP__SET_CHANNELS:
341                 case OP__SET_BPS:
342                 case OP__SET_TOTAL_SAMPLES:
343                         ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
344                         break;
345                 case OP__SHOW_VC_VENDOR:
346                 case OP__SHOW_VC_FIELD:
347                 case OP__REMOVE_VC_ALL:
348                 case OP__REMOVE_VC_FIELD:
349                 case OP__REMOVE_VC_FIRSTFIELD:
350                 case OP__SET_VC_FIELD:
351                 case OP__IMPORT_VC_FROM:
352                 case OP__EXPORT_VC_TO:
353                         ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
354                         break;
355                 case OP__IMPORT_CUESHEET_FROM:
356                 case OP__EXPORT_CUESHEET_TO:
357                         ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
358                         break;
359                 case OP__ADD_SEEKPOINT:
360                         ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
361                         break;
362                 case OP__ADD_REPLAY_GAIN:
363                         /* this command is always executed last */
364                         ok = true;
365                         break;
366                 case OP__ADD_PADDING:
367                         ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
368                         break;
369                 default:
370                         ok = false;
371                         FLAC__ASSERT(0);
372                         break;
373         };
374
375         return ok;
376 }
377
378 FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime)
379 {
380         FLAC__StreamMetadata streaminfo;
381         float *title_gains = 0, *title_peaks = 0;
382         float album_gain, album_peak;
383         unsigned sample_rate = 0;
384         unsigned bits_per_sample = 0;
385         unsigned channels = 0;
386         unsigned i;
387         const char *error;
388         FLAC__bool first = true;
389
390         FLAC__ASSERT(num_files > 0);
391
392         for(i = 0; i < num_files; i++) {
393                 FLAC__ASSERT(0 != filenames[i]);
394                 if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
395                         fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
396                         return false;
397                 }
398                 if(first) {
399                         first = false;
400                         sample_rate = streaminfo.data.stream_info.sample_rate;
401                         bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
402                         channels = streaminfo.data.stream_info.channels;
403                 }
404                 else {
405                         if(sample_rate != streaminfo.data.stream_info.sample_rate) {
406                                 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);
407                                 return false;
408                         }
409                         if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
410                                 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);
411                                 return false;
412                         }
413                         if(channels != streaminfo.data.stream_info.channels) {
414                                 fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
415                                 return false;
416                         }
417                 }
418                 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
419                         fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
420                         return false;
421                 }
422                 if(channels != 1 && channels != 2) {
423                         fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
424                         return false;
425                 }
426         }
427         FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
428
429         if(!grabbag__replaygain_init(sample_rate)) {
430                 FLAC__ASSERT(0);
431                 /* double protection */
432                 fprintf(stderr, "internal error\n");
433                 return false;
434         }
435
436         if(
437                 0 == (title_gains = (float*)malloc(sizeof(float) * num_files)) ||
438                 0 == (title_peaks = (float*)malloc(sizeof(float) * num_files))
439         )
440                 die("out of memory allocating space for title gains/peaks");
441
442         for(i = 0; i < num_files; i++) {
443                 if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
444                         fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
445                         free(title_gains);
446                         free(title_peaks);
447                         return false;
448                 }
449         }
450         grabbag__replaygain_get_album(&album_gain, &album_peak);
451
452         for(i = 0; i < num_files; i++) {
453                 if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
454                         fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
455                         free(title_gains);
456                         free(title_peaks);
457                         return false;
458                 }
459         }
460
461         free(title_gains);
462         free(title_peaks);
463         return true;
464 }
465
466 FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
467 {
468         FLAC__StreamMetadata *padding = 0;
469         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
470
471         if(0 == iterator)
472                 die("out of memory allocating iterator");
473
474         FLAC__metadata_iterator_init(iterator, chain);
475
476         while(FLAC__metadata_iterator_next(iterator))
477                 ;
478
479         padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
480         if(0 == padding)
481                 die("out of memory allocating PADDING block");
482
483         padding->length = length;
484
485         if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
486                 print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename);
487                 FLAC__metadata_object_delete(padding);
488                 FLAC__metadata_iterator_delete(iterator);
489                 return false;
490         }
491
492         FLAC__metadata_iterator_delete(iterator);
493         *needs_write = true;
494         return true;
495 }
496
497 FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
498 {
499         unsigned i, j;
500         FLAC__bool matches_number = false, matches_type = false;
501         FLAC__bool has_block_number_arg = false;
502
503         for(i = 0; i < options->args.num_arguments; i++) {
504                 if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
505                         for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
506                                 if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
507                                         if(block->type != FLAC__METADATA_TYPE_APPLICATION || !options->args.arguments[i].value.block_type.entries[j].filter_application_by_id || 0 == memcmp(options->args.arguments[i].value.block_type.entries[j].application_id, block->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
508                                                 matches_type = true;
509                                 }
510                         }
511                 }
512                 else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
513                         has_block_number_arg = true;
514                         for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
515                                 if(options->args.arguments[i].value.block_number.entries[j] == block_number)
516                                         matches_number = true;
517                         }
518                 }
519         }
520
521         if(!has_block_number_arg)
522                 matches_number = true;
523
524         if(options->args.checks.has_block_type) {
525                 FLAC__ASSERT(!options->args.checks.has_except_block_type);
526         }
527         else if(options->args.checks.has_except_block_type)
528                 matches_type = !matches_type;
529         else
530                 matches_type = true;
531
532         return matches_number && matches_type;
533 }
534
535 void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
536 {
537         unsigned i, j;
538
539 /*@@@ yuck, should do this with a varargs function or something: */
540 #define PPR if(filename)printf("%s:",filename);
541         PPR; printf("METADATA block #%u\n", block_number);
542         PPR; printf("  type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
543         PPR; printf("  is last: %s\n", block->is_last? "true":"false");
544         PPR; printf("  length: %u\n", block->length);
545
546         switch(block->type) {
547                 case FLAC__METADATA_TYPE_STREAMINFO:
548                         PPR; printf("  minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
549                         PPR; printf("  maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
550                         PPR; printf("  minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
551                         PPR; printf("  maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
552                         PPR; printf("  sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
553                         PPR; printf("  channels: %u\n", block->data.stream_info.channels);
554                         PPR; printf("  bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
555 #ifdef _MSC_VER
556                         PPR; printf("  total samples: %I64u\n", block->data.stream_info.total_samples);
557 #else
558                         PPR; printf("  total samples: %llu\n", block->data.stream_info.total_samples);
559 #endif
560                         PPR; printf("  MD5 signature: ");
561                         for(i = 0; i < 16; i++) {
562                                 printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
563                         }
564                         printf("\n");
565                         break;
566                 case FLAC__METADATA_TYPE_PADDING:
567                         /* nothing to print */
568                         break;
569                 case FLAC__METADATA_TYPE_APPLICATION:
570                         PPR; printf("  application ID: ");
571                         for(i = 0; i < 4; i++)
572                                 printf("%02x", block->data.application.id[i]);
573                         printf("\n");
574                         PPR; printf("  data contents:\n");
575                         if(0 != block->data.application.data) {
576                                 if(hexdump_application)
577                                         hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, "    ");
578                                 else
579                                         (void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
580                         }
581                         break;
582                 case FLAC__METADATA_TYPE_SEEKTABLE:
583                         PPR; printf("  seek points: %u\n", block->data.seek_table.num_points);
584                         for(i = 0; i < block->data.seek_table.num_points; i++) {
585                                 if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
586 #ifdef _MSC_VER
587                                         PPR; printf("    point %u: sample_number=%I64u, stream_offset=%I64u, 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);
588 #else
589                                         PPR; printf("    point %u: 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);
590 #endif
591                                 }
592                                 else {
593                                         PPR; printf("    point %u: PLACEHOLDER\n", i);
594                                 }
595                         }
596                         break;
597                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
598                         PPR; printf("  vendor string: ");
599                         write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
600                         PPR; printf("  comments: %u\n", block->data.vorbis_comment.num_comments);
601                         for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
602                                 PPR; printf("    comment[%u]: ", i);
603                                 write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
604                         }
605                         break;
606                 case FLAC__METADATA_TYPE_CUESHEET:
607                         PPR; printf("  media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
608 #ifdef _MSC_VER
609                         PPR; printf("  lead-in: %I64u\n", block->data.cue_sheet.lead_in);
610 #else
611                         PPR; printf("  lead-in: %llu\n", block->data.cue_sheet.lead_in);
612 #endif
613                         PPR; printf("  is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false");
614                         PPR; printf("  number of tracks: %u\n", block->data.cue_sheet.num_tracks);
615                         for(i = 0; i < block->data.cue_sheet.num_tracks; i++) {
616                                 const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i;
617                                 const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1);
618                                 const FLAC__bool is_leadout = is_last && track->num_indices == 0;
619                                 PPR; printf("    track[%u]\n", i);
620 #ifdef _MSC_VER
621                                 PPR; printf("      offset: %I64u\n", track->offset);
622 #else
623                                 PPR; printf("      offset: %llu\n", track->offset);
624 #endif
625                                 if(is_last) {
626                                         PPR; printf("      number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID");
627                                 }
628                                 else {
629                                         PPR; printf("      number: %u\n", (unsigned)track->number);
630                                 }
631                                 if(!is_leadout) {
632                                         PPR; printf("      ISRC: %s\n", track->isrc);
633                                         PPR; printf("      type: %s\n", track->type == 1? "DATA" : "AUDIO");
634                                         PPR; printf("      pre-emphasis: %s\n", track->pre_emphasis? "true":"false");
635                                         PPR; printf("      number of index points: %u\n", track->num_indices);
636                                         for(j = 0; j < track->num_indices; j++) {
637                                                 const FLAC__StreamMetadata_CueSheet_Index *index = track->indices+j;
638                                                 PPR; printf("        index[%u]\n", j);
639 #ifdef _MSC_VER
640                                                 PPR; printf("          offset: %I64u\n", index->offset);
641 #else
642                                                 PPR; printf("          offset: %llu\n", index->offset);
643 #endif
644                                                 PPR; printf("          number: %u\n", (unsigned)index->number);
645                                         }
646                                 }
647                         }
648                         break;
649                 case FLAC__METADATA_TYPE_PICTURE:
650                         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");
651                         PPR; printf("  MIME type: %s\n", block->data.picture.mime_type);
652                         PPR; printf("  description: %s\n", block->data.picture.description);
653                         PPR; printf("  width: %u\n", (unsigned)block->data.picture.width);
654                         PPR; printf("  height: %u\n", (unsigned)block->data.picture.height);
655                         PPR; printf("  depth: %u\n", (unsigned)block->data.picture.depth);
656                         PPR; printf("  data length: %u\n", (unsigned)block->data.picture.data_length);
657                         PPR; printf("  data:\n");
658                         if(0 != block->data.picture.data)
659                                 hexdump(filename, block->data.picture.data, block->data.picture.data_length, "    ");
660                         break;
661                 default:
662                         PPR; printf("  data contents:\n");
663                         if(0 != block->data.unknown.data)
664                                 hexdump(filename, block->data.unknown.data, block->length, "    ");
665                         break;
666         }
667 #undef PPR
668 }