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