1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002 Josh Coalson
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.
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.
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.
19 #include "operations.h"
22 #include "FLAC/assert.h"
23 #include "FLAC/metadata.h"
24 #include "share/grabbag.h"
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);
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);
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);
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);
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);
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, FLAC__bool cued_seekpoints);
58 FLAC__bool do_operations(const CommandLineOptions *options)
62 if(options->show_long_help) {
65 if(options->show_version) {
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);
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);
88 printf("metaflac %s\n", FLAC__VERSION_STRING);
91 FLAC__bool do_major_operation(const CommandLineOptions *options)
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);
103 FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
105 FLAC__bool ok = true, needs_write = false;
106 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
109 die("out of memory allocating chain");
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)]);
116 switch(options->ops.operations[0].type) {
118 ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
121 ok = do_major_operation__append(chain, options);
125 ok = do_major_operation__remove(chain, options);
129 ok = do_major_operation__remove_all(chain, options);
132 case OP__MERGE_PADDING:
133 FLAC__metadata_chain_merge_padding(chain);
136 case OP__SORT_PADDING:
137 FLAC__metadata_chain_sort_padding(chain);
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);
150 fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
153 FLAC__metadata_chain_delete(chain);
158 FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
160 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
161 FLAC__StreamMetadata *block;
162 FLAC__bool ok = true;
163 unsigned block_number;
166 die("out of memory allocating iterator");
168 FLAC__metadata_iterator_init(iterator, chain);
172 block = FLAC__metadata_iterator_get_block(iterator);
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);
179 } while(ok && FLAC__metadata_iterator_next(iterator));
181 FLAC__metadata_iterator_delete(iterator);
186 FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
188 (void) chain, (void) options;
189 fprintf(stderr, "ERROR: --append not implemented yet\n"); /*@@@*/
193 FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
195 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
196 FLAC__bool ok = true;
197 unsigned block_number;
200 die("out of memory allocating iterator");
202 FLAC__metadata_iterator_init(iterator, chain);
205 while(ok && FLAC__metadata_iterator_next(iterator)) {
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);
214 FLAC__metadata_iterator_delete(iterator);
219 FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
221 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
222 FLAC__bool ok = true;
225 die("out of memory allocating iterator");
227 FLAC__metadata_iterator_init(iterator, chain);
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);
235 FLAC__metadata_iterator_delete(iterator);
240 FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
243 FLAC__bool ok = true;
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);
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);
260 FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
263 FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
264 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
267 die("out of memory allocating chain");
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)]);
274 for(i = 0; i < options->ops.num_operations && ok; i++) {
275 ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
277 /* The following seems counterintuitive but the meaning
278 * of 'use_padding' is 'try to keep the overall metadata
279 * to its original size, adding or truncating extra
280 * padding if necessary' which is why we need to turn it
281 * off in this case. If we don't, the extra padding block
282 * will just be truncated.
284 if(options->ops.operations[i].type == OP__ADD_PADDING)
288 if(ok && needs_write) {
290 FLAC__metadata_chain_sort_padding(chain);
291 ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
293 fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
296 FLAC__metadata_chain_delete(chain);
301 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)
303 FLAC__bool ok = true;
305 switch(operation->type) {
306 case OP__SHOW_MD5SUM:
307 case OP__SHOW_MIN_BLOCKSIZE:
308 case OP__SHOW_MAX_BLOCKSIZE:
309 case OP__SHOW_MIN_FRAMESIZE:
310 case OP__SHOW_MAX_FRAMESIZE:
311 case OP__SHOW_SAMPLE_RATE:
312 case OP__SHOW_CHANNELS:
314 case OP__SHOW_TOTAL_SAMPLES:
316 case OP__SET_MIN_BLOCKSIZE:
317 case OP__SET_MAX_BLOCKSIZE:
318 case OP__SET_MIN_FRAMESIZE:
319 case OP__SET_MAX_FRAMESIZE:
320 case OP__SET_SAMPLE_RATE:
321 case OP__SET_CHANNELS:
323 case OP__SET_TOTAL_SAMPLES:
324 ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
326 case OP__SHOW_VC_VENDOR:
327 case OP__SHOW_VC_FIELD:
328 case OP__REMOVE_VC_ALL:
329 case OP__REMOVE_VC_FIELD:
330 case OP__REMOVE_VC_FIRSTFIELD:
331 case OP__SET_VC_FIELD:
332 case OP__IMPORT_VC_FROM:
333 case OP__EXPORT_VC_TO:
334 ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
336 case OP__IMPORT_CUESHEET_FROM:
337 case OP__EXPORT_CUESHEET_TO:
338 ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write, /*@@@@cued_seekpoints=*/true);
340 case OP__ADD_SEEKPOINT:
341 ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
343 case OP__ADD_REPLAY_GAIN:
344 /* this command is always executed last */
347 case OP__ADD_PADDING:
348 ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
359 FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime)
361 FLAC__StreamMetadata streaminfo;
362 float *title_gains = 0, *title_peaks = 0;
363 float album_gain, album_peak;
364 unsigned sample_rate = 0;
365 unsigned bits_per_sample = 0;
366 unsigned channels = 0;
369 FLAC__bool first = true;
371 FLAC__ASSERT(num_files > 0);
373 for(i = 0; i < num_files; i++) {
374 FLAC__ASSERT(0 != filenames[i]);
375 if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
376 fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
381 sample_rate = streaminfo.data.stream_info.sample_rate;
382 bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
383 channels = streaminfo.data.stream_info.channels;
386 if(sample_rate != streaminfo.data.stream_info.sample_rate) {
387 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);
390 if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
391 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);
394 if(channels != streaminfo.data.stream_info.channels) {
395 fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
399 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
400 fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
403 if(channels != 1 && channels != 2) {
404 fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
408 FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
410 if(!grabbag__replaygain_init(sample_rate))
414 0 == (title_gains = (float*)malloc(sizeof(float) * num_files)) ||
415 0 == (title_peaks = (float*)malloc(sizeof(float) * num_files))
417 die("out of memory allocating space for title gains/peaks");
419 for(i = 0; i < num_files; i++) {
420 if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
421 fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
427 grabbag__replaygain_get_album(&album_gain, &album_peak);
429 for(i = 0; i < num_files; i++) {
430 if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
431 fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
443 FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
445 FLAC__StreamMetadata *padding = 0;
446 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
449 die("out of memory allocating iterator");
451 FLAC__metadata_iterator_init(iterator, chain);
453 while(FLAC__metadata_iterator_next(iterator))
456 padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
458 die("out of memory allocating PADDING block");
460 padding->length = length;
462 if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
463 fprintf(stderr, "%s: ERROR: adding new PADDING block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
464 FLAC__metadata_object_delete(padding);
465 FLAC__metadata_iterator_delete(iterator);
469 FLAC__metadata_iterator_delete(iterator);
474 FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
477 FLAC__bool matches_number = false, matches_type = false;
478 FLAC__bool has_block_number_arg = false;
480 for(i = 0; i < options->args.num_arguments; i++) {
481 if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
482 for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
483 if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
484 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))
489 else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
490 has_block_number_arg = true;
491 for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
492 if(options->args.arguments[i].value.block_number.entries[j] == block_number)
493 matches_number = true;
498 if(!has_block_number_arg)
499 matches_number = true;
501 if(options->args.checks.has_block_type) {
502 FLAC__ASSERT(!options->args.checks.has_except_block_type);
504 else if(options->args.checks.has_except_block_type)
505 matches_type = !matches_type;
509 return matches_number && matches_type;
512 void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
516 /*@@@ yuck, should do this with a varargs function or something: */
517 #define PPR if(filename)printf("%s:",filename);
518 PPR; printf("METADATA block #%u\n", block_number);
519 PPR; printf(" type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
520 PPR; printf(" is last: %s\n", block->is_last? "true":"false");
521 PPR; printf(" length: %u\n", block->length);
523 switch(block->type) {
524 case FLAC__METADATA_TYPE_STREAMINFO:
525 PPR; printf(" minumum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
526 PPR; printf(" maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
527 PPR; printf(" minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
528 PPR; printf(" maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
529 PPR; printf(" sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
530 PPR; printf(" channels: %u\n", block->data.stream_info.channels);
531 PPR; printf(" bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
532 PPR; printf(" total samples: %llu\n", block->data.stream_info.total_samples);
533 PPR; printf(" MD5 signature: ");
534 for(i = 0; i < 16; i++) {
535 printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
539 case FLAC__METADATA_TYPE_PADDING:
540 /* nothing to print */
542 case FLAC__METADATA_TYPE_APPLICATION:
543 PPR; printf(" application ID: ");
544 for(i = 0; i < 4; i++) {
545 PPR; printf("%02x", block->data.application.id[i]);
548 PPR; printf(" data contents:\n");
549 if(0 != block->data.application.data) {
550 if(hexdump_application)
551 hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " ");
553 (void) fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
556 case FLAC__METADATA_TYPE_SEEKTABLE:
557 PPR; printf(" seek points: %u\n", block->data.seek_table.num_points);
558 for(i = 0; i < block->data.seek_table.num_points; i++) {
559 if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
560 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);
563 PPR; printf(" point %d: PLACEHOLDER\n", i);
567 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
568 PPR; printf(" vendor string: ");
569 write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
570 PPR; printf(" comments: %u\n", block->data.vorbis_comment.num_comments);
571 for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
572 PPR; printf(" comment[%u]: ", i);
573 write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
576 case FLAC__METADATA_TYPE_CUESHEET:
577 PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
581 PPR; printf("SKIPPING block of unknown type\n");