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