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