blob: 952f1d60b59d0b6349232ec9698a6fec4e1ada67 [file] [log] [blame]
Josh Coalsonee44de42002-11-08 06:07:20 +00001/* metaflac - Command-line FLAC metadata editor
Erik de Castro Lopob1982fb2013-05-25 17:11:19 +10002 * Copyright (C) 2001-2009 Josh Coalson
Erik de Castro Lopo6a5fe432016-12-05 06:35:39 +11003 * Copyright (C) 2011-2016 Xiph.Org Foundation
Josh Coalsonee44de42002-11-08 06:07:20 +00004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Miroslav Lichvar6a1f59b2012-12-04 17:36:05 +010015 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Josh Coalsonee44de42002-11-08 06:07:20 +000018 */
19
Erik de Castro Lopo006b8352014-03-23 21:59:46 +110020#ifdef HAVE_CONFIG_H
Josh Coalsonb1ec7962006-05-24 04:41:36 +000021# include <config.h>
22#endif
23
Josh Coalsonee44de42002-11-08 06:07:20 +000024#include "operations.h"
25#include "usage.h"
26#include "utils.h"
27#include "FLAC/assert.h"
28#include "FLAC/metadata.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000029#include "share/alloc.h"
Josh Coalsonee44de42002-11-08 06:07:20 +000030#include "share/grabbag.h"
Erik de Castro Lopo59afe362012-02-04 16:13:37 +110031#include "share/compat.h"
Josh Coalsonee44de42002-11-08 06:07:20 +000032#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
Josh Coalsonf9cf27f2007-07-25 03:11:00 +000035#include "operations_shorthand.h"
Josh Coalsonee44de42002-11-08 06:07:20 +000036
Josh Coalsone3ec2ad2007-01-31 03:53:22 +000037static void show_version(void);
Josh Coalsonee44de42002-11-08 06:07:20 +000038static FLAC__bool do_major_operation(const CommandLineOptions *options);
39static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options);
40static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
41static FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
42static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
43static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
44static FLAC__bool do_shorthand_operations(const CommandLineOptions *options);
45static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options);
Josh Coalson3c18ade2002-11-08 22:15:08 +000046static 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);
Ben Boeckelf7c52c82015-08-12 21:45:04 -040047static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan);
Josh Coalsonee44de42002-11-08 06:07:20 +000048static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write);
49
50static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number);
51static void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application);
52
53/* from operations_shorthand_seektable.c */
54extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
55
56/* from operations_shorthand_streaminfo.c */
Josh Coalson3c18ade2002-11-08 22:15:08 +000057extern 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);
Josh Coalsonee44de42002-11-08 06:07:20 +000058
59/* from operations_shorthand_vorbiscomment.c */
Josh Coalson3c18ade2002-11-08 22:15:08 +000060extern 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);
Josh Coalsonee44de42002-11-08 06:07:20 +000061
Josh Coalson303123f2002-11-26 06:21:06 +000062/* from operations_shorthand_cuesheet.c */
Josh Coalsoncfae6cd2002-11-27 04:44:54 +000063extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
Josh Coalson303123f2002-11-26 06:21:06 +000064
Josh Coalsonb02574e2006-09-26 00:43:48 +000065/* from operations_shorthand_picture.c */
66extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
67
Josh Coalson303123f2002-11-26 06:21:06 +000068
Josh Coalsonee44de42002-11-08 06:07:20 +000069FLAC__bool do_operations(const CommandLineOptions *options)
70{
71 FLAC__bool ok = true;
72
73 if(options->show_long_help) {
74 long_usage(0);
75 }
76 if(options->show_version) {
77 show_version();
78 }
79 else if(options->args.checks.num_major_ops > 0) {
80 FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0);
81 FLAC__ASSERT(options->args.checks.num_major_ops == 1);
82 FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations);
83 ok = do_major_operation(options);
84 }
85 else if(options->args.checks.num_shorthand_ops > 0) {
86 FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations);
87 ok = do_shorthand_operations(options);
88 }
89
90 return ok;
91}
92
93/*
94 * local routines
95 */
96
Josh Coalsone3ec2ad2007-01-31 03:53:22 +000097void show_version(void)
Josh Coalsonee44de42002-11-08 06:07:20 +000098{
99 printf("metaflac %s\n", FLAC__VERSION_STRING);
100}
101
102FLAC__bool do_major_operation(const CommandLineOptions *options)
103{
104 unsigned i;
105 FLAC__bool ok = true;
106
Josh Coalsonbeb12842006-10-03 01:04:41 +0000107 /* to die after first error, v--- add '&& ok' here */
Josh Coalsonee44de42002-11-08 06:07:20 +0000108 for(i = 0; i < options->num_files; i++)
109 ok &= do_major_operation_on_file(options->filenames[i], options);
110
111 return ok;
112}
113
114FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
115{
Josh Coalson6fba8472006-11-15 06:13:16 +0000116 FLAC__bool ok = true, needs_write = false, is_ogg = false;
Josh Coalsonee44de42002-11-08 06:07:20 +0000117 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
118
119 if(0 == chain)
120 die("out of memory allocating chain");
121
Josh Coalson6fba8472006-11-15 06:13:16 +0000122 /*@@@@ lame way of guessing the file type */
Josh Coalson12d3a412007-08-01 03:02:00 +0000123 if(strlen(filename) >= 4 && (0 == strcmp(filename+strlen(filename)-4, ".oga") || 0 == strcmp(filename+strlen(filename)-4, ".ogg")))
Josh Coalson6fba8472006-11-15 06:13:16 +0000124 is_ogg = true;
125
126 if(! (is_ogg? FLAC__metadata_chain_read_ogg(chain, filename) : FLAC__metadata_chain_read(chain, filename)) ) {
Josh Coalson9454b412004-07-23 05:11:52 +0000127 print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
Josh Coalson07be2912004-10-19 04:23:57 +0000128 FLAC__metadata_chain_delete(chain);
Josh Coalsonee44de42002-11-08 06:07:20 +0000129 return false;
130 }
131
132 switch(options->ops.operations[0].type) {
133 case OP__LIST:
134 ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
135 break;
136 case OP__APPEND:
137 ok = do_major_operation__append(chain, options);
138 needs_write = true;
139 break;
140 case OP__REMOVE:
141 ok = do_major_operation__remove(chain, options);
142 needs_write = true;
143 break;
144 case OP__REMOVE_ALL:
145 ok = do_major_operation__remove_all(chain, options);
146 needs_write = true;
147 break;
148 case OP__MERGE_PADDING:
149 FLAC__metadata_chain_merge_padding(chain);
150 needs_write = true;
151 break;
152 case OP__SORT_PADDING:
153 FLAC__metadata_chain_sort_padding(chain);
154 needs_write = true;
155 break;
156 default:
157 FLAC__ASSERT(0);
158 return false;
159 }
160
161 if(ok && needs_write) {
162 if(options->use_padding)
163 FLAC__metadata_chain_sort_padding(chain);
164 ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
165 if(!ok)
Josh Coalson9454b412004-07-23 05:11:52 +0000166 print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000167 }
168
169 FLAC__metadata_chain_delete(chain);
170
171 return ok;
172}
173
174FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
175{
176 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
177 FLAC__StreamMetadata *block;
178 FLAC__bool ok = true;
179 unsigned block_number;
180
181 if(0 == iterator)
182 die("out of memory allocating iterator");
183
184 FLAC__metadata_iterator_init(iterator, chain);
185
186 block_number = 0;
187 do {
188 block = FLAC__metadata_iterator_get_block(iterator);
189 ok &= (0 != block);
190 if(!ok)
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100191 flac_fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000192 else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
193 write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
194 block_number++;
195 } while(ok && FLAC__metadata_iterator_next(iterator));
196
197 FLAC__metadata_iterator_delete(iterator);
198
199 return ok;
200}
201
202FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
203{
204 (void) chain, (void) options;
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100205 flac_fprintf(stderr, "ERROR: --append not implemented yet\n");
Josh Coalsonee44de42002-11-08 06:07:20 +0000206 return false;
207}
208
209FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
210{
211 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
212 FLAC__bool ok = true;
213 unsigned block_number;
214
215 if(0 == iterator)
216 die("out of memory allocating iterator");
217
218 FLAC__metadata_iterator_init(iterator, chain);
219
220 block_number = 0;
221 while(ok && FLAC__metadata_iterator_next(iterator)) {
222 block_number++;
223 if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) {
224 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
225 if(options->use_padding)
226 ok &= FLAC__metadata_iterator_next(iterator);
227 }
228 }
229
230 FLAC__metadata_iterator_delete(iterator);
231
232 return ok;
233}
234
235FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
236{
237 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
238 FLAC__bool ok = true;
239
240 if(0 == iterator)
241 die("out of memory allocating iterator");
242
243 FLAC__metadata_iterator_init(iterator, chain);
244
245 while(ok && FLAC__metadata_iterator_next(iterator)) {
246 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
247 if(options->use_padding)
248 ok &= FLAC__metadata_iterator_next(iterator);
249 }
250
251 FLAC__metadata_iterator_delete(iterator);
252
253 return ok;
254}
255
256FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
257{
258 unsigned i;
259 FLAC__bool ok = true;
260
261 /* to die after first error, v--- add '&& ok' here */
262 for(i = 0; i < options->num_files; i++)
263 ok &= do_shorthand_operations_on_file(options->filenames[i], options);
264
265 /* check if OP__ADD_REPLAY_GAIN requested */
266 if(ok && options->num_files > 0) {
267 for(i = 0; i < options->ops.num_operations; i++) {
268 if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
Ben Boeckelf7c52c82015-08-12 21:45:04 -0400269 ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, false);
270 else if(options->ops.operations[i].type == OP__SCAN_REPLAY_GAIN)
271 ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, true);
Josh Coalsonee44de42002-11-08 06:07:20 +0000272 }
273 }
274
275 return ok;
276}
277
278FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
279{
280 unsigned i;
281 FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
282 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
283
284 if(0 == chain)
285 die("out of memory allocating chain");
286
287 if(!FLAC__metadata_chain_read(chain, filename)) {
Josh Coalson9454b412004-07-23 05:11:52 +0000288 print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
Erik de Castro Lopo952c93b2017-04-09 15:17:06 +1000289 ok = false;
290 goto cleanup;
Josh Coalsonee44de42002-11-08 06:07:20 +0000291 }
292
293 for(i = 0; i < options->ops.num_operations && ok; i++) {
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000294 /*
295 * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
296 * --add-seekpoint and --import-cuesheet-from are used.
297 */
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);
Josh Coalsonee44de42002-11-08 06:07:20 +0000300
301 /* The following seems counterintuitive but the meaning
302 * of 'use_padding' is 'try to keep the overall metadata
303 * to its original size, adding or truncating extra
304 * padding if necessary' which is why we need to turn it
305 * off in this case. If we don't, the extra padding block
306 * will just be truncated.
307 */
308 if(options->ops.operations[i].type == OP__ADD_PADDING)
309 use_padding = false;
310 }
311
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000312 /*
313 * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
314 * --add-seekpoint and --import-cuesheet-from are used.
315 */
316 for(i = 0; i < options->ops.num_operations && ok; i++) {
317 if(options->ops.operations[i].type == OP__ADD_SEEKPOINT)
318 ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
319 }
320
Josh Coalsonee44de42002-11-08 06:07:20 +0000321 if(ok && needs_write) {
322 if(use_padding)
323 FLAC__metadata_chain_sort_padding(chain);
324 ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
325 if(!ok)
Josh Coalson9454b412004-07-23 05:11:52 +0000326 print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000327 }
328
Erik de Castro Lopo952c93b2017-04-09 15:17:06 +1000329 cleanup :
Josh Coalsonee44de42002-11-08 06:07:20 +0000330 FLAC__metadata_chain_delete(chain);
331
332 return ok;
333}
334
Josh Coalson3c18ade2002-11-08 22:15:08 +0000335FLAC__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)
Josh Coalsonee44de42002-11-08 06:07:20 +0000336{
337 FLAC__bool ok = true;
338
339 switch(operation->type) {
340 case OP__SHOW_MD5SUM:
341 case OP__SHOW_MIN_BLOCKSIZE:
342 case OP__SHOW_MAX_BLOCKSIZE:
343 case OP__SHOW_MIN_FRAMESIZE:
344 case OP__SHOW_MAX_FRAMESIZE:
345 case OP__SHOW_SAMPLE_RATE:
346 case OP__SHOW_CHANNELS:
347 case OP__SHOW_BPS:
348 case OP__SHOW_TOTAL_SAMPLES:
349 case OP__SET_MD5SUM:
350 case OP__SET_MIN_BLOCKSIZE:
351 case OP__SET_MAX_BLOCKSIZE:
352 case OP__SET_MIN_FRAMESIZE:
353 case OP__SET_MAX_FRAMESIZE:
354 case OP__SET_SAMPLE_RATE:
355 case OP__SET_CHANNELS:
356 case OP__SET_BPS:
357 case OP__SET_TOTAL_SAMPLES:
Josh Coalson3c18ade2002-11-08 22:15:08 +0000358 ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
Josh Coalsonee44de42002-11-08 06:07:20 +0000359 break;
360 case OP__SHOW_VC_VENDOR:
361 case OP__SHOW_VC_FIELD:
362 case OP__REMOVE_VC_ALL:
363 case OP__REMOVE_VC_FIELD:
364 case OP__REMOVE_VC_FIRSTFIELD:
365 case OP__SET_VC_FIELD:
366 case OP__IMPORT_VC_FROM:
367 case OP__EXPORT_VC_TO:
Josh Coalson3c18ade2002-11-08 22:15:08 +0000368 ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
Josh Coalsonee44de42002-11-08 06:07:20 +0000369 break;
Josh Coalson303123f2002-11-26 06:21:06 +0000370 case OP__IMPORT_CUESHEET_FROM:
371 case OP__EXPORT_CUESHEET_TO:
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000372 ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
Josh Coalson303123f2002-11-26 06:21:06 +0000373 break;
Josh Coalsonaec8bb32006-11-02 04:06:56 +0000374 case OP__IMPORT_PICTURE_FROM:
Josh Coalson989f2672006-11-02 03:40:44 +0000375 case OP__EXPORT_PICTURE_TO:
Josh Coalsonb02574e2006-09-26 00:43:48 +0000376 ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
377 break;
Josh Coalsonee44de42002-11-08 06:07:20 +0000378 case OP__ADD_SEEKPOINT:
379 ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
380 break;
381 case OP__ADD_REPLAY_GAIN:
Ben Boeckelf7c52c82015-08-12 21:45:04 -0400382 case OP__SCAN_REPLAY_GAIN:
383 /* these commands are always executed last */
Josh Coalsonee44de42002-11-08 06:07:20 +0000384 ok = true;
385 break;
386 case OP__ADD_PADDING:
387 ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
388 break;
389 default:
390 ok = false;
391 FLAC__ASSERT(0);
392 break;
393 };
394
395 return ok;
396}
397
Ben Boeckelf7c52c82015-08-12 21:45:04 -0400398FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan)
Josh Coalsonee44de42002-11-08 06:07:20 +0000399{
400 FLAC__StreamMetadata streaminfo;
401 float *title_gains = 0, *title_peaks = 0;
402 float album_gain, album_peak;
403 unsigned sample_rate = 0;
404 unsigned bits_per_sample = 0;
405 unsigned channels = 0;
406 unsigned i;
407 const char *error;
408 FLAC__bool first = true;
409
410 FLAC__ASSERT(num_files > 0);
411
412 for(i = 0; i < num_files; i++) {
413 FLAC__ASSERT(0 != filenames[i]);
414 if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100415 flac_fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
Josh Coalsonee44de42002-11-08 06:07:20 +0000416 return false;
417 }
418 if(first) {
419 first = false;
420 sample_rate = streaminfo.data.stream_info.sample_rate;
421 bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
422 channels = streaminfo.data.stream_info.channels;
423 }
424 else {
425 if(sample_rate != streaminfo.data.stream_info.sample_rate) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100426 flac_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);
Josh Coalsonee44de42002-11-08 06:07:20 +0000427 return false;
428 }
429 if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100430 flac_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);
Josh Coalsonee44de42002-11-08 06:07:20 +0000431 return false;
432 }
433 if(channels != streaminfo.data.stream_info.channels) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100434 flac_fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels);
Josh Coalsonee44de42002-11-08 06:07:20 +0000435 return false;
436 }
437 }
438 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100439 flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate);
Josh Coalsonee44de42002-11-08 06:07:20 +0000440 return false;
441 }
442 if(channels != 1 && channels != 2) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100443 flac_fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels);
Josh Coalsonee44de42002-11-08 06:07:20 +0000444 return false;
445 }
446 }
447 FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
448
Josh Coalsonc191ab42002-12-23 19:52:20 +0000449 if(!grabbag__replaygain_init(sample_rate)) {
Josh Coalsonee44de42002-11-08 06:07:20 +0000450 FLAC__ASSERT(0);
Josh Coalsonc191ab42002-12-23 19:52:20 +0000451 /* double protection */
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100452 flac_fprintf(stderr, "internal error\n");
Josh Coalsonc191ab42002-12-23 19:52:20 +0000453 return false;
454 }
Josh Coalsonee44de42002-11-08 06:07:20 +0000455
456 if(
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000457 0 == (title_gains = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) ||
458 0 == (title_peaks = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files))
Josh Coalsonee44de42002-11-08 06:07:20 +0000459 )
460 die("out of memory allocating space for title gains/peaks");
461
462 for(i = 0; i < num_files; i++) {
463 if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100464 flac_fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
Josh Coalsonee44de42002-11-08 06:07:20 +0000465 free(title_gains);
466 free(title_peaks);
467 return false;
468 }
469 }
470 grabbag__replaygain_get_album(&album_gain, &album_peak);
471
472 for(i = 0; i < num_files; i++) {
Ben Boeckelf7c52c82015-08-12 21:45:04 -0400473 if(!scan) {
474 if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
475 flac_fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
476 free(title_gains);
477 free(title_peaks);
478 return false;
479 }
480 } else {
481 flac_fprintf(stdout, "%s: %f %f %f %f\n", filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i]);
Josh Coalsonee44de42002-11-08 06:07:20 +0000482 }
483 }
484
485 free(title_gains);
486 free(title_peaks);
487 return true;
488}
489
490FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
491{
492 FLAC__StreamMetadata *padding = 0;
493 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
494
495 if(0 == iterator)
496 die("out of memory allocating iterator");
497
498 FLAC__metadata_iterator_init(iterator, chain);
499
500 while(FLAC__metadata_iterator_next(iterator))
501 ;
502
503 padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
504 if(0 == padding)
505 die("out of memory allocating PADDING block");
506
507 padding->length = length;
508
509 if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
Josh Coalson9454b412004-07-23 05:11:52 +0000510 print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000511 FLAC__metadata_object_delete(padding);
512 FLAC__metadata_iterator_delete(iterator);
513 return false;
514 }
515
516 FLAC__metadata_iterator_delete(iterator);
517 *needs_write = true;
518 return true;
519}
520
521FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
522{
523 unsigned i, j;
524 FLAC__bool matches_number = false, matches_type = false;
525 FLAC__bool has_block_number_arg = false;
526
527 for(i = 0; i < options->args.num_arguments; i++) {
528 if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
529 for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
530 if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
531 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))
532 matches_type = true;
533 }
534 }
535 }
536 else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
537 has_block_number_arg = true;
538 for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
539 if(options->args.arguments[i].value.block_number.entries[j] == block_number)
540 matches_number = true;
541 }
542 }
543 }
544
545 if(!has_block_number_arg)
546 matches_number = true;
547
548 if(options->args.checks.has_block_type) {
549 FLAC__ASSERT(!options->args.checks.has_except_block_type);
550 }
551 else if(options->args.checks.has_except_block_type)
552 matches_type = !matches_type;
553 else
554 matches_type = true;
555
556 return matches_number && matches_type;
557}
558
559void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
560{
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000561 unsigned i, j;
Josh Coalsonee44de42002-11-08 06:07:20 +0000562
563/*@@@ yuck, should do this with a varargs function or something: */
Janne Hyvärinencc9f3922013-04-25 08:58:08 +1000564#define PPR if(filename) { if(raw) printf("%s:",filename); else flac_printf("%s:",filename); }
Josh Coalsonee44de42002-11-08 06:07:20 +0000565 PPR; printf("METADATA block #%u\n", block_number);
Josh Coalsond91f8fa2002-11-15 05:44:26 +0000566 PPR; printf(" type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN");
Josh Coalsonee44de42002-11-08 06:07:20 +0000567 PPR; printf(" is last: %s\n", block->is_last? "true":"false");
568 PPR; printf(" length: %u\n", block->length);
569
570 switch(block->type) {
571 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalson14502c62006-08-28 03:33:01 +0000572 PPR; printf(" minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
Josh Coalsonee44de42002-11-08 06:07:20 +0000573 PPR; printf(" maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
574 PPR; printf(" minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
575 PPR; printf(" maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
576 PPR; printf(" sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
577 PPR; printf(" channels: %u\n", block->data.stream_info.channels);
578 PPR; printf(" bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100579 PPR; printf(" total samples: %" PRIu64 "\n", block->data.stream_info.total_samples);
Josh Coalsonee44de42002-11-08 06:07:20 +0000580 PPR; printf(" MD5 signature: ");
581 for(i = 0; i < 16; i++) {
582 printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
583 }
584 printf("\n");
585 break;
586 case FLAC__METADATA_TYPE_PADDING:
587 /* nothing to print */
588 break;
589 case FLAC__METADATA_TYPE_APPLICATION:
590 PPR; printf(" application ID: ");
Josh Coalson292d20f2003-01-11 06:25:20 +0000591 for(i = 0; i < 4; i++)
592 printf("%02x", block->data.application.id[i]);
593 printf("\n");
Josh Coalsonee44de42002-11-08 06:07:20 +0000594 PPR; printf(" data contents:\n");
595 if(0 != block->data.application.data) {
596 if(hexdump_application)
597 hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " ");
598 else
Josh Coalsonf91251d2002-12-28 07:04:49 +0000599 (void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
Josh Coalsonee44de42002-11-08 06:07:20 +0000600 }
601 break;
602 case FLAC__METADATA_TYPE_SEEKTABLE:
603 PPR; printf(" seek points: %u\n", block->data.seek_table.num_points);
604 for(i = 0; i < block->data.seek_table.num_points; i++) {
605 if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100606 PPR; printf(" point %u: sample_number=%" PRIu64 ", stream_offset=%" PRIu64 ", 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);
Josh Coalsonee44de42002-11-08 06:07:20 +0000607 }
608 else {
Josh Coalsonfec4a772004-03-22 05:47:25 +0000609 PPR; printf(" point %u: PLACEHOLDER\n", i);
Josh Coalsonee44de42002-11-08 06:07:20 +0000610 }
611 }
612 break;
613 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
614 PPR; printf(" vendor string: ");
615 write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
616 PPR; printf(" comments: %u\n", block->data.vorbis_comment.num_comments);
617 for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
618 PPR; printf(" comment[%u]: ", i);
619 write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
620 }
621 break;
Josh Coalson303123f2002-11-26 06:21:06 +0000622 case FLAC__METADATA_TYPE_CUESHEET:
623 PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100624 PPR; printf(" lead-in: %" PRIu64 "\n", block->data.cue_sheet.lead_in);
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000625 PPR; printf(" is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false");
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000626 PPR; printf(" number of tracks: %u\n", block->data.cue_sheet.num_tracks);
627 for(i = 0; i < block->data.cue_sheet.num_tracks; i++) {
628 const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i;
629 const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1);
630 const FLAC__bool is_leadout = is_last && track->num_indices == 0;
631 PPR; printf(" track[%u]\n", i);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100632 PPR; printf(" offset: %" PRIu64 "\n", track->offset);
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000633 if(is_last) {
634 PPR; printf(" number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID");
635 }
636 else {
637 PPR; printf(" number: %u\n", (unsigned)track->number);
638 }
639 if(!is_leadout) {
640 PPR; printf(" ISRC: %s\n", track->isrc);
641 PPR; printf(" type: %s\n", track->type == 1? "DATA" : "AUDIO");
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000642 PPR; printf(" pre-emphasis: %s\n", track->pre_emphasis? "true":"false");
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000643 PPR; printf(" number of index points: %u\n", track->num_indices);
644 for(j = 0; j < track->num_indices; j++) {
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +1100645 const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices+j;
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000646 PPR; printf(" index[%u]\n", j);
Erik de Castro Lopoae7eda12013-04-05 20:21:22 +1100647 PPR; printf(" offset: %" PRIu64 "\n", indx->offset);
648 PPR; printf(" number: %u\n", (unsigned)indx->number);
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000649 }
650 }
651 }
Josh Coalson303123f2002-11-26 06:21:06 +0000652 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000653 case FLAC__METADATA_TYPE_PICTURE:
654 PPR; printf(" type: %u (%s)\n", block->data.picture.type, block->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED? FLAC__StreamMetadata_Picture_TypeString[block->data.picture.type] : "UNDEFINED");
655 PPR; printf(" MIME type: %s\n", block->data.picture.mime_type);
656 PPR; printf(" description: %s\n", block->data.picture.description);
657 PPR; printf(" width: %u\n", (unsigned)block->data.picture.width);
658 PPR; printf(" height: %u\n", (unsigned)block->data.picture.height);
659 PPR; printf(" depth: %u\n", (unsigned)block->data.picture.depth);
Josh Coalson74ed2942006-09-23 23:15:05 +0000660 PPR; printf(" colors: %u%s\n", (unsigned)block->data.picture.colors, block->data.picture.colors? "" : " (unindexed)");
Josh Coalsone343ab22006-09-23 19:21:19 +0000661 PPR; printf(" data length: %u\n", (unsigned)block->data.picture.data_length);
662 PPR; printf(" data:\n");
663 if(0 != block->data.picture.data)
664 hexdump(filename, block->data.picture.data, block->data.picture.data_length, " ");
665 break;
Josh Coalsonee44de42002-11-08 06:07:20 +0000666 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000667 PPR; printf(" data contents:\n");
668 if(0 != block->data.unknown.data)
669 hexdump(filename, block->data.unknown.data, block->length, " ");
Josh Coalsonee44de42002-11-08 06:07:20 +0000670 break;
671 }
672#undef PPR
673}