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 FLAC__bool do_operations(const CommandLineOptions *options)
58 if(options->show_long_help) {
61 if(options->show_version) {
64 else if(options->args.checks.num_major_ops > 0) {
65 FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0);
66 FLAC__ASSERT(options->args.checks.num_major_ops == 1);
67 FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations);
68 ok = do_major_operation(options);
70 else if(options->args.checks.num_shorthand_ops > 0) {
71 FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations);
72 ok = do_shorthand_operations(options);
84 printf("metaflac %s\n", FLAC__VERSION_STRING);
87 FLAC__bool do_major_operation(const CommandLineOptions *options)
92 /*@@@ to die after first error, v--- add '&& ok' here */
93 for(i = 0; i < options->num_files; i++)
94 ok &= do_major_operation_on_file(options->filenames[i], options);
99 FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
101 FLAC__bool ok = true, needs_write = false;
102 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
105 die("out of memory allocating chain");
107 if(!FLAC__metadata_chain_read(chain, filename)) {
108 fprintf(stderr, "%s: ERROR: reading metadata, status = \"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
112 switch(options->ops.operations[0].type) {
114 ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
117 ok = do_major_operation__append(chain, options);
121 ok = do_major_operation__remove(chain, options);
125 ok = do_major_operation__remove_all(chain, options);
128 case OP__MERGE_PADDING:
129 FLAC__metadata_chain_merge_padding(chain);
132 case OP__SORT_PADDING:
133 FLAC__metadata_chain_sort_padding(chain);
141 if(ok && needs_write) {
142 if(options->use_padding)
143 FLAC__metadata_chain_sort_padding(chain);
144 ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
146 fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
149 FLAC__metadata_chain_delete(chain);
154 FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
156 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
157 FLAC__StreamMetadata *block;
158 FLAC__bool ok = true;
159 unsigned block_number;
162 die("out of memory allocating iterator");
164 FLAC__metadata_iterator_init(iterator, chain);
168 block = FLAC__metadata_iterator_get_block(iterator);
171 fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
172 else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
173 write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
175 } while(ok && FLAC__metadata_iterator_next(iterator));
177 FLAC__metadata_iterator_delete(iterator);
182 FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
184 (void) chain, (void) options;
185 fprintf(stderr, "ERROR: --append not implemented yet\n"); /*@@@*/
189 FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
191 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
192 FLAC__bool ok = true;
193 unsigned block_number;
196 die("out of memory allocating iterator");
198 FLAC__metadata_iterator_init(iterator, chain);
201 while(ok && FLAC__metadata_iterator_next(iterator)) {
203 if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) {
204 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
205 if(options->use_padding)
206 ok &= FLAC__metadata_iterator_next(iterator);
210 FLAC__metadata_iterator_delete(iterator);
215 FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
217 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
218 FLAC__bool ok = true;
221 die("out of memory allocating iterator");
223 FLAC__metadata_iterator_init(iterator, chain);
225 while(ok && FLAC__metadata_iterator_next(iterator)) {
226 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
227 if(options->use_padding)
228 ok &= FLAC__metadata_iterator_next(iterator);
231 FLAC__metadata_iterator_delete(iterator);
236 FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
239 FLAC__bool ok = true;
241 /* to die after first error, v--- add '&& ok' here */
242 for(i = 0; i < options->num_files; i++)
243 ok &= do_shorthand_operations_on_file(options->filenames[i], options);
245 /* check if OP__ADD_REPLAY_GAIN requested */
246 if(ok && options->num_files > 0) {
247 for(i = 0; i < options->ops.num_operations; i++) {
248 if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
249 ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime);
256 FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
259 FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
260 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
263 die("out of memory allocating chain");
265 if(!FLAC__metadata_chain_read(chain, filename)) {
266 fprintf(stderr, "%s: ERROR: reading metadata, status = \"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
270 for(i = 0; i < options->ops.num_operations && ok; i++) {
271 ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
273 /* The following seems counterintuitive but the meaning
274 * of 'use_padding' is 'try to keep the overall metadata
275 * to its original size, adding or truncating extra
276 * padding if necessary' which is why we need to turn it
277 * off in this case. If we don't, the extra padding block
278 * will just be truncated.
280 if(options->ops.operations[i].type == OP__ADD_PADDING)
284 if(ok && needs_write) {
286 FLAC__metadata_chain_sort_padding(chain);
287 ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
289 fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
292 FLAC__metadata_chain_delete(chain);
297 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)
299 FLAC__bool ok = true;
301 switch(operation->type) {
302 case OP__SHOW_MD5SUM:
303 case OP__SHOW_MIN_BLOCKSIZE:
304 case OP__SHOW_MAX_BLOCKSIZE:
305 case OP__SHOW_MIN_FRAMESIZE:
306 case OP__SHOW_MAX_FRAMESIZE:
307 case OP__SHOW_SAMPLE_RATE:
308 case OP__SHOW_CHANNELS:
310 case OP__SHOW_TOTAL_SAMPLES:
312 case OP__SET_MIN_BLOCKSIZE:
313 case OP__SET_MAX_BLOCKSIZE:
314 case OP__SET_MIN_FRAMESIZE:
315 case OP__SET_MAX_FRAMESIZE:
316 case OP__SET_SAMPLE_RATE:
317 case OP__SET_CHANNELS:
319 case OP__SET_TOTAL_SAMPLES:
320 ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
322 case OP__SHOW_VC_VENDOR:
323 case OP__SHOW_VC_FIELD:
324 case OP__REMOVE_VC_ALL:
325 case OP__REMOVE_VC_FIELD:
326 case OP__REMOVE_VC_FIRSTFIELD:
327 case OP__SET_VC_FIELD:
328 case OP__IMPORT_VC_FROM:
329 case OP__EXPORT_VC_TO:
330 ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
332 case OP__ADD_SEEKPOINT:
333 ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
335 case OP__ADD_REPLAY_GAIN:
336 /* this command is always executed last */
339 case OP__ADD_PADDING:
340 ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
351 FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime)
353 FLAC__StreamMetadata streaminfo;
354 float *title_gains = 0, *title_peaks = 0;
355 float album_gain, album_peak;
356 unsigned sample_rate = 0;
357 unsigned bits_per_sample = 0;
358 unsigned channels = 0;
361 FLAC__bool first = true;
363 FLAC__ASSERT(num_files > 0);
365 for(i = 0; i < num_files; i++) {
366 FLAC__ASSERT(0 != filenames[i]);
367 if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
368 fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
373 sample_rate = streaminfo.data.stream_info.sample_rate;
374 bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
375 channels = streaminfo.data.stream_info.channels;
378 if(sample_rate != streaminfo.data.stream_info.sample_rate) {
379 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);
382 if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
383 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);
386 if(channels != streaminfo.data.stream_info.channels) {
387 fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
391 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
392 fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
395 if(channels != 1 && channels != 2) {
396 fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
400 FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
402 if(!grabbag__replaygain_init(sample_rate))
406 0 == (title_gains = (float*)malloc(sizeof(float) * num_files)) ||
407 0 == (title_peaks = (float*)malloc(sizeof(float) * num_files))
409 die("out of memory allocating space for title gains/peaks");
411 for(i = 0; i < num_files; i++) {
412 if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
413 fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
419 grabbag__replaygain_get_album(&album_gain, &album_peak);
421 for(i = 0; i < num_files; i++) {
422 if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
423 fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
435 FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
437 FLAC__StreamMetadata *padding = 0;
438 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
441 die("out of memory allocating iterator");
443 FLAC__metadata_iterator_init(iterator, chain);
445 while(FLAC__metadata_iterator_next(iterator))
448 padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
450 die("out of memory allocating PADDING block");
452 padding->length = length;
454 if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
455 fprintf(stderr, "%s: ERROR: adding new PADDING block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
456 FLAC__metadata_object_delete(padding);
457 FLAC__metadata_iterator_delete(iterator);
461 FLAC__metadata_iterator_delete(iterator);
466 FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
469 FLAC__bool matches_number = false, matches_type = false;
470 FLAC__bool has_block_number_arg = false;
472 for(i = 0; i < options->args.num_arguments; i++) {
473 if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
474 for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
475 if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
476 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))
481 else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
482 has_block_number_arg = true;
483 for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
484 if(options->args.arguments[i].value.block_number.entries[j] == block_number)
485 matches_number = true;
490 if(!has_block_number_arg)
491 matches_number = true;
493 if(options->args.checks.has_block_type) {
494 FLAC__ASSERT(!options->args.checks.has_except_block_type);
496 else if(options->args.checks.has_except_block_type)
497 matches_type = !matches_type;
501 return matches_number && matches_type;
504 void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
508 /*@@@ yuck, should do this with a varargs function or something: */
509 #define PPR if(filename)printf("%s:",filename);
510 PPR; printf("METADATA block #%u\n", block_number);
511 PPR; printf(" type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
512 PPR; printf(" is last: %s\n", block->is_last? "true":"false");
513 PPR; printf(" length: %u\n", block->length);
515 switch(block->type) {
516 case FLAC__METADATA_TYPE_STREAMINFO:
517 PPR; printf(" minumum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
518 PPR; printf(" maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
519 PPR; printf(" minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
520 PPR; printf(" maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
521 PPR; printf(" sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
522 PPR; printf(" channels: %u\n", block->data.stream_info.channels);
523 PPR; printf(" bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
524 PPR; printf(" total samples: %llu\n", block->data.stream_info.total_samples);
525 PPR; printf(" MD5 signature: ");
526 for(i = 0; i < 16; i++) {
527 printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
531 case FLAC__METADATA_TYPE_PADDING:
532 /* nothing to print */
534 case FLAC__METADATA_TYPE_APPLICATION:
535 PPR; printf(" application ID: ");
536 for(i = 0; i < 4; i++) {
537 PPR; printf("%02x", block->data.application.id[i]);
540 PPR; printf(" data contents:\n");
541 if(0 != block->data.application.data) {
542 if(hexdump_application)
543 hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " ");
545 (void) fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
548 case FLAC__METADATA_TYPE_SEEKTABLE:
549 PPR; printf(" seek points: %u\n", block->data.seek_table.num_points);
550 for(i = 0; i < block->data.seek_table.num_points; i++) {
551 if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
552 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);
555 PPR; printf(" point %d: PLACEHOLDER\n", i);
559 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
560 PPR; printf(" vendor string: ");
561 write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
562 PPR; printf(" comments: %u\n", block->data.vorbis_comment.num_comments);
563 for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
564 PPR; printf(" comment[%u]: ", i);
565 write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
569 PPR; printf("SKIPPING block of unknown type\n");