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