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