blob: cd31a3d4f76dd37bb424f26e899a54969a73841b [file] [log] [blame]
Josh Coalsonee44de42002-11-08 06:07:20 +00001/* metaflac - Command-line FLAC metadata editor
Josh Coalsondea0f5a2009-01-07 07:31:28 +00002 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
Josh Coalsonee44de42002-11-08 06:07:20 +00003 *
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 *
Miroslav Lichvar6a1f59b2012-12-04 17:36:05 +010014 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Josh Coalsonee44de42002-11-08 06:07:20 +000017 */
18
Josh Coalsonb1ec7962006-05-24 04:41:36 +000019#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
Josh Coalsonee44de42002-11-08 06:07:20 +000023#include "operations.h"
24#include "usage.h"
25#include "utils.h"
26#include "FLAC/assert.h"
27#include "FLAC/metadata.h"
Josh Coalson0f008d22007-09-11 04:49:56 +000028#include "share/alloc.h"
Josh Coalsonee44de42002-11-08 06:07:20 +000029#include "share/grabbag.h"
Erik de Castro Lopo59afe362012-02-04 16:13:37 +110030#include "share/compat.h"
Josh Coalsonee44de42002-11-08 06:07:20 +000031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
Josh Coalsonf9cf27f2007-07-25 03:11:00 +000034#include "operations_shorthand.h"
Josh Coalsonee44de42002-11-08 06:07:20 +000035
Josh Coalsone3ec2ad2007-01-31 03:53:22 +000036static void show_version(void);
Josh Coalsonee44de42002-11-08 06:07:20 +000037static FLAC__bool do_major_operation(const CommandLineOptions *options);
38static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options);
39static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
40static FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
41static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
42static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options);
43static FLAC__bool do_shorthand_operations(const CommandLineOptions *options);
44static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options);
Josh Coalson3c18ade2002-11-08 22:15:08 +000045static 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);
Josh Coalsonee44de42002-11-08 06:07:20 +000046static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime);
47static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write);
48
49static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number);
50static void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application);
51
52/* from operations_shorthand_seektable.c */
53extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write);
54
55/* from operations_shorthand_streaminfo.c */
Josh Coalson3c18ade2002-11-08 22:15:08 +000056extern 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 +000057
58/* from operations_shorthand_vorbiscomment.c */
Josh Coalson3c18ade2002-11-08 22:15:08 +000059extern 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 +000060
Josh Coalson303123f2002-11-26 06:21:06 +000061/* from operations_shorthand_cuesheet.c */
Josh Coalsoncfae6cd2002-11-27 04:44:54 +000062extern 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 +000063
Josh Coalsonb02574e2006-09-26 00:43:48 +000064/* from operations_shorthand_picture.c */
65extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write);
66
Josh Coalson303123f2002-11-26 06:21:06 +000067
Josh Coalsonee44de42002-11-08 06:07:20 +000068FLAC__bool do_operations(const CommandLineOptions *options)
69{
70 FLAC__bool ok = true;
71
72 if(options->show_long_help) {
73 long_usage(0);
74 }
75 if(options->show_version) {
76 show_version();
77 }
78 else if(options->args.checks.num_major_ops > 0) {
79 FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0);
80 FLAC__ASSERT(options->args.checks.num_major_ops == 1);
81 FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations);
82 ok = do_major_operation(options);
83 }
84 else if(options->args.checks.num_shorthand_ops > 0) {
85 FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations);
86 ok = do_shorthand_operations(options);
87 }
88
89 return ok;
90}
91
92/*
93 * local routines
94 */
95
Josh Coalsone3ec2ad2007-01-31 03:53:22 +000096void show_version(void)
Josh Coalsonee44de42002-11-08 06:07:20 +000097{
98 printf("metaflac %s\n", FLAC__VERSION_STRING);
99}
100
101FLAC__bool do_major_operation(const CommandLineOptions *options)
102{
103 unsigned i;
104 FLAC__bool ok = true;
105
Josh Coalsonbeb12842006-10-03 01:04:41 +0000106 /* to die after first error, v--- add '&& ok' here */
Josh Coalsonee44de42002-11-08 06:07:20 +0000107 for(i = 0; i < options->num_files; i++)
108 ok &= do_major_operation_on_file(options->filenames[i], options);
109
110 return ok;
111}
112
113FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options)
114{
Josh Coalson6fba8472006-11-15 06:13:16 +0000115 FLAC__bool ok = true, needs_write = false, is_ogg = false;
Josh Coalsonee44de42002-11-08 06:07:20 +0000116 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
117
118 if(0 == chain)
119 die("out of memory allocating chain");
120
Josh Coalson6fba8472006-11-15 06:13:16 +0000121 /*@@@@ lame way of guessing the file type */
Josh Coalson12d3a412007-08-01 03:02:00 +0000122 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 +0000123 is_ogg = true;
124
125 if(! (is_ogg? FLAC__metadata_chain_read_ogg(chain, filename) : FLAC__metadata_chain_read(chain, filename)) ) {
Josh Coalson9454b412004-07-23 05:11:52 +0000126 print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
Josh Coalson07be2912004-10-19 04:23:57 +0000127 FLAC__metadata_chain_delete(chain);
Josh Coalsonee44de42002-11-08 06:07:20 +0000128 return false;
129 }
130
131 switch(options->ops.operations[0].type) {
132 case OP__LIST:
133 ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options);
134 break;
135 case OP__APPEND:
136 ok = do_major_operation__append(chain, options);
137 needs_write = true;
138 break;
139 case OP__REMOVE:
140 ok = do_major_operation__remove(chain, options);
141 needs_write = true;
142 break;
143 case OP__REMOVE_ALL:
144 ok = do_major_operation__remove_all(chain, options);
145 needs_write = true;
146 break;
147 case OP__MERGE_PADDING:
148 FLAC__metadata_chain_merge_padding(chain);
149 needs_write = true;
150 break;
151 case OP__SORT_PADDING:
152 FLAC__metadata_chain_sort_padding(chain);
153 needs_write = true;
154 break;
155 default:
156 FLAC__ASSERT(0);
157 return false;
158 }
159
160 if(ok && needs_write) {
161 if(options->use_padding)
162 FLAC__metadata_chain_sort_padding(chain);
163 ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime);
164 if(!ok)
Josh Coalson9454b412004-07-23 05:11:52 +0000165 print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000166 }
167
168 FLAC__metadata_chain_delete(chain);
169
170 return ok;
171}
172
173FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
174{
175 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
176 FLAC__StreamMetadata *block;
177 FLAC__bool ok = true;
178 unsigned block_number;
179
180 if(0 == iterator)
181 die("out of memory allocating iterator");
182
183 FLAC__metadata_iterator_init(iterator, chain);
184
185 block_number = 0;
186 do {
187 block = FLAC__metadata_iterator_get_block(iterator);
188 ok &= (0 != block);
189 if(!ok)
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100190 flac_fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000191 else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number))
192 write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump);
193 block_number++;
194 } while(ok && FLAC__metadata_iterator_next(iterator));
195
196 FLAC__metadata_iterator_delete(iterator);
197
198 return ok;
199}
200
201FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
202{
203 (void) chain, (void) options;
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100204 flac_fprintf(stderr, "ERROR: --append not implemented yet\n");
Josh Coalsonee44de42002-11-08 06:07:20 +0000205 return false;
206}
207
208FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
209{
210 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
211 FLAC__bool ok = true;
212 unsigned block_number;
213
214 if(0 == iterator)
215 die("out of memory allocating iterator");
216
217 FLAC__metadata_iterator_init(iterator, chain);
218
219 block_number = 0;
220 while(ok && FLAC__metadata_iterator_next(iterator)) {
221 block_number++;
222 if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) {
223 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
224 if(options->use_padding)
225 ok &= FLAC__metadata_iterator_next(iterator);
226 }
227 }
228
229 FLAC__metadata_iterator_delete(iterator);
230
231 return ok;
232}
233
234FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options)
235{
236 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
237 FLAC__bool ok = true;
238
239 if(0 == iterator)
240 die("out of memory allocating iterator");
241
242 FLAC__metadata_iterator_init(iterator, chain);
243
244 while(ok && FLAC__metadata_iterator_next(iterator)) {
245 ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding);
246 if(options->use_padding)
247 ok &= FLAC__metadata_iterator_next(iterator);
248 }
249
250 FLAC__metadata_iterator_delete(iterator);
251
252 return ok;
253}
254
255FLAC__bool do_shorthand_operations(const CommandLineOptions *options)
256{
257 unsigned i;
258 FLAC__bool ok = true;
259
260 /* to die after first error, v--- add '&& ok' here */
261 for(i = 0; i < options->num_files; i++)
262 ok &= do_shorthand_operations_on_file(options->filenames[i], options);
263
264 /* check if OP__ADD_REPLAY_GAIN requested */
265 if(ok && options->num_files > 0) {
266 for(i = 0; i < options->ops.num_operations; i++) {
267 if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN)
268 ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime);
269 }
270 }
271
272 return ok;
273}
274
275FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options)
276{
277 unsigned i;
278 FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding;
279 FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new();
280
281 if(0 == chain)
282 die("out of memory allocating chain");
283
284 if(!FLAC__metadata_chain_read(chain, filename)) {
Josh Coalson9454b412004-07-23 05:11:52 +0000285 print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000286 return false;
287 }
288
289 for(i = 0; i < options->ops.num_operations && ok; i++) {
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000290 /*
291 * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
292 * --add-seekpoint and --import-cuesheet-from are used.
293 */
294 if(options->ops.operations[i].type != OP__ADD_SEEKPOINT)
295 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 +0000296
297 /* The following seems counterintuitive but the meaning
298 * of 'use_padding' is 'try to keep the overall metadata
299 * to its original size, adding or truncating extra
300 * padding if necessary' which is why we need to turn it
301 * off in this case. If we don't, the extra padding block
302 * will just be truncated.
303 */
304 if(options->ops.operations[i].type == OP__ADD_PADDING)
305 use_padding = false;
306 }
307
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000308 /*
309 * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both
310 * --add-seekpoint and --import-cuesheet-from are used.
311 */
312 for(i = 0; i < options->ops.num_operations && ok; i++) {
313 if(options->ops.operations[i].type == OP__ADD_SEEKPOINT)
314 ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert);
315 }
316
Josh Coalsonee44de42002-11-08 06:07:20 +0000317 if(ok && needs_write) {
318 if(use_padding)
319 FLAC__metadata_chain_sort_padding(chain);
320 ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime);
321 if(!ok)
Josh Coalson9454b412004-07-23 05:11:52 +0000322 print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000323 }
324
325 FLAC__metadata_chain_delete(chain);
326
327 return ok;
328}
329
Josh Coalson3c18ade2002-11-08 22:15:08 +0000330FLAC__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 +0000331{
332 FLAC__bool ok = true;
333
334 switch(operation->type) {
335 case OP__SHOW_MD5SUM:
336 case OP__SHOW_MIN_BLOCKSIZE:
337 case OP__SHOW_MAX_BLOCKSIZE:
338 case OP__SHOW_MIN_FRAMESIZE:
339 case OP__SHOW_MAX_FRAMESIZE:
340 case OP__SHOW_SAMPLE_RATE:
341 case OP__SHOW_CHANNELS:
342 case OP__SHOW_BPS:
343 case OP__SHOW_TOTAL_SAMPLES:
344 case OP__SET_MD5SUM:
345 case OP__SET_MIN_BLOCKSIZE:
346 case OP__SET_MAX_BLOCKSIZE:
347 case OP__SET_MIN_FRAMESIZE:
348 case OP__SET_MAX_FRAMESIZE:
349 case OP__SET_SAMPLE_RATE:
350 case OP__SET_CHANNELS:
351 case OP__SET_BPS:
352 case OP__SET_TOTAL_SAMPLES:
Josh Coalson3c18ade2002-11-08 22:15:08 +0000353 ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write);
Josh Coalsonee44de42002-11-08 06:07:20 +0000354 break;
355 case OP__SHOW_VC_VENDOR:
356 case OP__SHOW_VC_FIELD:
357 case OP__REMOVE_VC_ALL:
358 case OP__REMOVE_VC_FIELD:
359 case OP__REMOVE_VC_FIRSTFIELD:
360 case OP__SET_VC_FIELD:
361 case OP__IMPORT_VC_FROM:
362 case OP__EXPORT_VC_TO:
Josh Coalson3c18ade2002-11-08 22:15:08 +0000363 ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert);
Josh Coalsonee44de42002-11-08 06:07:20 +0000364 break;
Josh Coalson303123f2002-11-26 06:21:06 +0000365 case OP__IMPORT_CUESHEET_FROM:
366 case OP__EXPORT_CUESHEET_TO:
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000367 ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write);
Josh Coalson303123f2002-11-26 06:21:06 +0000368 break;
Josh Coalsonaec8bb32006-11-02 04:06:56 +0000369 case OP__IMPORT_PICTURE_FROM:
Josh Coalson989f2672006-11-02 03:40:44 +0000370 case OP__EXPORT_PICTURE_TO:
Josh Coalsonb02574e2006-09-26 00:43:48 +0000371 ok = do_shorthand_operation__picture(filename, chain, operation, needs_write);
372 break;
Josh Coalsonee44de42002-11-08 06:07:20 +0000373 case OP__ADD_SEEKPOINT:
374 ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write);
375 break;
376 case OP__ADD_REPLAY_GAIN:
377 /* this command is always executed last */
378 ok = true;
379 break;
380 case OP__ADD_PADDING:
381 ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write);
382 break;
383 default:
384 ok = false;
385 FLAC__ASSERT(0);
386 break;
387 };
388
389 return ok;
390}
391
392FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime)
393{
394 FLAC__StreamMetadata streaminfo;
395 float *title_gains = 0, *title_peaks = 0;
396 float album_gain, album_peak;
397 unsigned sample_rate = 0;
398 unsigned bits_per_sample = 0;
399 unsigned channels = 0;
400 unsigned i;
401 const char *error;
402 FLAC__bool first = true;
403
404 FLAC__ASSERT(num_files > 0);
405
406 for(i = 0; i < num_files; i++) {
407 FLAC__ASSERT(0 != filenames[i]);
408 if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100409 flac_fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]);
Josh Coalsonee44de42002-11-08 06:07:20 +0000410 return false;
411 }
412 if(first) {
413 first = false;
414 sample_rate = streaminfo.data.stream_info.sample_rate;
415 bits_per_sample = streaminfo.data.stream_info.bits_per_sample;
416 channels = streaminfo.data.stream_info.channels;
417 }
418 else {
419 if(sample_rate != streaminfo.data.stream_info.sample_rate) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100420 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 +0000421 return false;
422 }
423 if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100424 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 +0000425 return false;
426 }
427 if(channels != streaminfo.data.stream_info.channels) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100428 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 +0000429 return false;
430 }
431 }
432 if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100433 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 +0000434 return false;
435 }
436 if(channels != 1 && channels != 2) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100437 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 +0000438 return false;
439 }
440 }
441 FLAC__ASSERT(bits_per_sample >= FLAC__MIN_BITS_PER_SAMPLE && bits_per_sample <= FLAC__MAX_BITS_PER_SAMPLE);
442
Josh Coalsonc191ab42002-12-23 19:52:20 +0000443 if(!grabbag__replaygain_init(sample_rate)) {
Josh Coalsonee44de42002-11-08 06:07:20 +0000444 FLAC__ASSERT(0);
Josh Coalsonc191ab42002-12-23 19:52:20 +0000445 /* double protection */
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100446 flac_fprintf(stderr, "internal error\n");
Josh Coalsonc191ab42002-12-23 19:52:20 +0000447 return false;
448 }
Josh Coalsonee44de42002-11-08 06:07:20 +0000449
450 if(
Erik de Castro Lopo6c2040d2012-04-04 21:29:25 +1000451 0 == (title_gains = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) ||
452 0 == (title_peaks = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files))
Josh Coalsonee44de42002-11-08 06:07:20 +0000453 )
454 die("out of memory allocating space for title gains/peaks");
455
456 for(i = 0; i < num_files; i++) {
457 if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100458 flac_fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error);
Josh Coalsonee44de42002-11-08 06:07:20 +0000459 free(title_gains);
460 free(title_peaks);
461 return false;
462 }
463 }
464 grabbag__replaygain_get_album(&album_gain, &album_peak);
465
466 for(i = 0; i < num_files; i++) {
467 if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) {
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100468 flac_fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error);
Josh Coalsonee44de42002-11-08 06:07:20 +0000469 free(title_gains);
470 free(title_peaks);
471 return false;
472 }
473 }
474
475 free(title_gains);
476 free(title_peaks);
477 return true;
478}
479
480FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write)
481{
482 FLAC__StreamMetadata *padding = 0;
483 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
484
485 if(0 == iterator)
486 die("out of memory allocating iterator");
487
488 FLAC__metadata_iterator_init(iterator, chain);
489
490 while(FLAC__metadata_iterator_next(iterator))
491 ;
492
493 padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
494 if(0 == padding)
495 die("out of memory allocating PADDING block");
496
497 padding->length = length;
498
499 if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) {
Josh Coalson9454b412004-07-23 05:11:52 +0000500 print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000501 FLAC__metadata_object_delete(padding);
502 FLAC__metadata_iterator_delete(iterator);
503 return false;
504 }
505
506 FLAC__metadata_iterator_delete(iterator);
507 *needs_write = true;
508 return true;
509}
510
511FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number)
512{
513 unsigned i, j;
514 FLAC__bool matches_number = false, matches_type = false;
515 FLAC__bool has_block_number_arg = false;
516
517 for(i = 0; i < options->args.num_arguments; i++) {
518 if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) {
519 for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) {
520 if(options->args.arguments[i].value.block_type.entries[j].type == block->type) {
521 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))
522 matches_type = true;
523 }
524 }
525 }
526 else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) {
527 has_block_number_arg = true;
528 for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) {
529 if(options->args.arguments[i].value.block_number.entries[j] == block_number)
530 matches_number = true;
531 }
532 }
533 }
534
535 if(!has_block_number_arg)
536 matches_number = true;
537
538 if(options->args.checks.has_block_type) {
539 FLAC__ASSERT(!options->args.checks.has_except_block_type);
540 }
541 else if(options->args.checks.has_except_block_type)
542 matches_type = !matches_type;
543 else
544 matches_type = true;
545
546 return matches_number && matches_type;
547}
548
549void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application)
550{
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000551 unsigned i, j;
Josh Coalsonee44de42002-11-08 06:07:20 +0000552
553/*@@@ yuck, should do this with a varargs function or something: */
Erik de Castro Lopo5705b4d2013-03-21 19:18:49 +1100554#define PPR if(filename)flac_printf("%s:",filename);
Josh Coalsonee44de42002-11-08 06:07:20 +0000555 PPR; printf("METADATA block #%u\n", block_number);
Josh Coalsond91f8fa2002-11-15 05:44:26 +0000556 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 +0000557 PPR; printf(" is last: %s\n", block->is_last? "true":"false");
558 PPR; printf(" length: %u\n", block->length);
559
560 switch(block->type) {
561 case FLAC__METADATA_TYPE_STREAMINFO:
Josh Coalson14502c62006-08-28 03:33:01 +0000562 PPR; printf(" minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize);
Josh Coalsonee44de42002-11-08 06:07:20 +0000563 PPR; printf(" maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize);
564 PPR; printf(" minimum framesize: %u bytes\n", block->data.stream_info.min_framesize);
565 PPR; printf(" maximum framesize: %u bytes\n", block->data.stream_info.max_framesize);
566 PPR; printf(" sample_rate: %u Hz\n", block->data.stream_info.sample_rate);
567 PPR; printf(" channels: %u\n", block->data.stream_info.channels);
568 PPR; printf(" bits-per-sample: %u\n", block->data.stream_info.bits_per_sample);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100569 PPR; printf(" total samples: %" PRIu64 "\n", block->data.stream_info.total_samples);
Josh Coalsonee44de42002-11-08 06:07:20 +0000570 PPR; printf(" MD5 signature: ");
571 for(i = 0; i < 16; i++) {
572 printf("%02x", (unsigned)block->data.stream_info.md5sum[i]);
573 }
574 printf("\n");
575 break;
576 case FLAC__METADATA_TYPE_PADDING:
577 /* nothing to print */
578 break;
579 case FLAC__METADATA_TYPE_APPLICATION:
580 PPR; printf(" application ID: ");
Josh Coalson292d20f2003-01-11 06:25:20 +0000581 for(i = 0; i < 4; i++)
582 printf("%02x", block->data.application.id[i]);
583 printf("\n");
Josh Coalsonee44de42002-11-08 06:07:20 +0000584 PPR; printf(" data contents:\n");
585 if(0 != block->data.application.data) {
586 if(hexdump_application)
587 hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " ");
588 else
Josh Coalsonf91251d2002-12-28 07:04:49 +0000589 (void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout);
Josh Coalsonee44de42002-11-08 06:07:20 +0000590 }
591 break;
592 case FLAC__METADATA_TYPE_SEEKTABLE:
593 PPR; printf(" seek points: %u\n", block->data.seek_table.num_points);
594 for(i = 0; i < block->data.seek_table.num_points; i++) {
595 if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100596 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 +0000597 }
598 else {
Josh Coalsonfec4a772004-03-22 05:47:25 +0000599 PPR; printf(" point %u: PLACEHOLDER\n", i);
Josh Coalsonee44de42002-11-08 06:07:20 +0000600 }
601 }
602 break;
603 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
604 PPR; printf(" vendor string: ");
605 write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout);
606 PPR; printf(" comments: %u\n", block->data.vorbis_comment.num_comments);
607 for(i = 0; i < block->data.vorbis_comment.num_comments; i++) {
608 PPR; printf(" comment[%u]: ", i);
609 write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout);
610 }
611 break;
Josh Coalson303123f2002-11-26 06:21:06 +0000612 case FLAC__METADATA_TYPE_CUESHEET:
613 PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100614 PPR; printf(" lead-in: %" PRIu64 "\n", block->data.cue_sheet.lead_in);
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000615 PPR; printf(" is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false");
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000616 PPR; printf(" number of tracks: %u\n", block->data.cue_sheet.num_tracks);
617 for(i = 0; i < block->data.cue_sheet.num_tracks; i++) {
618 const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i;
619 const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1);
620 const FLAC__bool is_leadout = is_last && track->num_indices == 0;
621 PPR; printf(" track[%u]\n", i);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100622 PPR; printf(" offset: %" PRIu64 "\n", track->offset);
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000623 if(is_last) {
624 PPR; printf(" number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID");
625 }
626 else {
627 PPR; printf(" number: %u\n", (unsigned)track->number);
628 }
629 if(!is_leadout) {
630 PPR; printf(" ISRC: %s\n", track->isrc);
631 PPR; printf(" type: %s\n", track->type == 1? "DATA" : "AUDIO");
Josh Coalson8f0c71b2002-12-05 06:37:46 +0000632 PPR; printf(" pre-emphasis: %s\n", track->pre_emphasis? "true":"false");
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000633 PPR; printf(" number of index points: %u\n", track->num_indices);
634 for(j = 0; j < track->num_indices; j++) {
635 const FLAC__StreamMetadata_CueSheet_Index *index = track->indices+j;
636 PPR; printf(" index[%u]\n", j);
Erik de Castro Lopo9c2290a2012-02-01 21:46:35 +1100637 PPR; printf(" offset: %" PRIu64 "\n", index->offset);
Josh Coalsoncfae6cd2002-11-27 04:44:54 +0000638 PPR; printf(" number: %u\n", (unsigned)index->number);
639 }
640 }
641 }
Josh Coalson303123f2002-11-26 06:21:06 +0000642 break;
Josh Coalsone343ab22006-09-23 19:21:19 +0000643 case FLAC__METADATA_TYPE_PICTURE:
644 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");
645 PPR; printf(" MIME type: %s\n", block->data.picture.mime_type);
646 PPR; printf(" description: %s\n", block->data.picture.description);
647 PPR; printf(" width: %u\n", (unsigned)block->data.picture.width);
648 PPR; printf(" height: %u\n", (unsigned)block->data.picture.height);
649 PPR; printf(" depth: %u\n", (unsigned)block->data.picture.depth);
Josh Coalson74ed2942006-09-23 23:15:05 +0000650 PPR; printf(" colors: %u%s\n", (unsigned)block->data.picture.colors, block->data.picture.colors? "" : " (unindexed)");
Josh Coalsone343ab22006-09-23 19:21:19 +0000651 PPR; printf(" data length: %u\n", (unsigned)block->data.picture.data_length);
652 PPR; printf(" data:\n");
653 if(0 != block->data.picture.data)
654 hexdump(filename, block->data.picture.data, block->data.picture.data_length, " ");
655 break;
Josh Coalsonee44de42002-11-08 06:07:20 +0000656 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000657 PPR; printf(" data contents:\n");
658 if(0 != block->data.unknown.data)
659 hexdump(filename, block->data.unknown.data, block->length, " ");
Josh Coalsonee44de42002-11-08 06:07:20 +0000660 break;
661 }
662#undef PPR
663}