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