diff --git a/src/metaflac/operations.c b/src/metaflac/operations.c index a105c88a..8b711b3c 100644 --- a/src/metaflac/operations.c +++ b/src/metaflac/operations.c @@ -52,7 +52,7 @@ extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC_ 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); /* from operations_shorthand_cuesheet.c */ -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); +extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); FLAC__bool do_operations(const CommandLineOptions *options) @@ -272,7 +272,12 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi } for(i = 0; i < options->ops.num_operations && ok; i++) { - ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert); + /* + * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both + * --add-seekpoint and --import-cuesheet-from are used. + */ + if(options->ops.operations[i].type != OP__ADD_SEEKPOINT) + ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert); /* The following seems counterintuitive but the meaning * of 'use_padding' is 'try to keep the overall metadata @@ -285,6 +290,15 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi use_padding = false; } + /* + * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both + * --add-seekpoint and --import-cuesheet-from are used. + */ + for(i = 0; i < options->ops.num_operations && ok; i++) { + if(options->ops.operations[i].type == OP__ADD_SEEKPOINT) + ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert); + } + if(ok && needs_write) { if(use_padding) FLAC__metadata_chain_sort_padding(chain); @@ -335,7 +349,7 @@ FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_f break; case OP__IMPORT_CUESHEET_FROM: case OP__EXPORT_CUESHEET_TO: - ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write, /*@@@@cued_seekpoints=*/true); + ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write); break; case OP__ADD_SEEKPOINT: ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write); @@ -511,7 +525,7 @@ FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMe void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application) { - unsigned i; + unsigned i, j; /*@@@ yuck, should do this with a varargs function or something: */ #define PPR if(filename)printf("%s:",filename); @@ -575,7 +589,33 @@ void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned break; case FLAC__METADATA_TYPE_CUESHEET: PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number); - //@@@@ finish + PPR; printf(" lead-in: %llu\n", block->data.cue_sheet.lead_in); + PPR; printf(" number of tracks: %u\n", block->data.cue_sheet.num_tracks); + for(i = 0; i < block->data.cue_sheet.num_tracks; i++) { + const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i; + const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1); + const FLAC__bool is_leadout = is_last && track->num_indices == 0; + PPR; printf(" track[%u]\n", i); + PPR; printf(" offset: %llu\n", track->offset); + if(is_last) { + PPR; printf(" number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID"); + } + else { + PPR; printf(" number: %u\n", (unsigned)track->number); + } + if(!is_leadout) { + PPR; printf(" ISRC: %s\n", track->isrc); + PPR; printf(" type: %s\n", track->type == 1? "DATA" : "AUDIO"); + PPR; printf(" pre-emphasis: %s\n", track->pre_emphasis? "true" : "false"); + PPR; printf(" number of index points: %u\n", track->num_indices); + for(j = 0; j < track->num_indices; j++) { + const FLAC__StreamMetadata_CueSheet_Index *index = track->indices+j; + PPR; printf(" index[%u]\n", j); + PPR; printf(" offset: %llu\n", index->offset); + PPR; printf(" number: %u\n", (unsigned)index->number); + } + } + } break; default: PPR; printf("SKIPPING block of unknown type\n"); diff --git a/src/metaflac/operations_shorthand_cuesheet.c b/src/metaflac/operations_shorthand_cuesheet.c index ae17b8c0..64fbf972 100644 --- a/src/metaflac/operations_shorthand_cuesheet.c +++ b/src/metaflac/operations_shorthand_cuesheet.c @@ -22,13 +22,13 @@ #include "share/grabbag.h" #include -static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const Argument_Filename *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__StreamMetadata *seektable); -static FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const Argument_Filename *cs_filename); +static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, Argument_AddSeekpoint *add_seekpoint_link); +static FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const char *cs_filename); -FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool cued_seekpoints) +FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write) { FLAC__bool ok = true; - FLAC__StreamMetadata *cuesheet = 0, *seektable = 0; + FLAC__StreamMetadata *cuesheet = 0; FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); FLAC__uint64 lead_out_offset; @@ -54,8 +54,6 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata } else if(block->type == FLAC__METADATA_TYPE_CUESHEET) cuesheet = block; - else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE) - seektable = block; } while(FLAC__metadata_iterator_next(iterator)); switch(operation->type) { @@ -65,26 +63,8 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata ok = false; } else { - /* create a new SEEKTABLE block if necessary */ - if(cued_seekpoints && 0 == seektable) { - seektable = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); - if(0 == seektable) - die("out of memory allocating SEEKTABLE block"); - while(FLAC__metadata_iterator_prev(iterator)) - ; - if(!FLAC__metadata_iterator_insert_block_after(iterator, seektable)) { - fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); - FLAC__metadata_object_delete(seektable); - FLAC__metadata_object_delete(cuesheet); - ok = false; - } - } - else - seektable = 0; - ok = import_cs_from(filename, &cuesheet, &operation->argument.filename, needs_write, lead_out_offset, seektable); - if(!ok && 0 != seektable) - FLAC__metadata_object_delete(seektable); - else { + ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, operation->argument.import_cuesheet_from.add_seekpoint_link); + if(ok) { /* append CUESHEET block */ while(FLAC__metadata_iterator_next(iterator)) ; @@ -102,7 +82,7 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata ok = false; } else - ok = export_cs_to(filename, cuesheet, &operation->argument.filename); + ok = export_cs_to(filename, cuesheet, operation->argument.filename.value); break; default: ok = false; @@ -118,23 +98,24 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata * local routines */ -FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const Argument_Filename *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__StreamMetadata *seektable) +FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, Argument_AddSeekpoint *add_seekpoint_link) { FILE *f; const char *error_message; + char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0; unsigned last_line_read; - if(0 == cs_filename->value || strlen(cs_filename->value) == 0) { + if(0 == cs_filename || strlen(cs_filename) == 0) { fprintf(stderr, "%s: ERROR: empty import file name\n", filename); return false; } - if(0 == strcmp(cs_filename->value, "-")) + if(0 == strcmp(cs_filename, "-")) f = stdin; else - f = fopen(cs_filename->value, "r"); + f = fopen(cs_filename, "r"); if(0 == f) { - fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, cs_filename->value); + fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, cs_filename); return false; } @@ -144,38 +125,49 @@ FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, fclose(f); if(0 == *cuesheet) { - fprintf(stderr, "%s: ERROR: while parsing cuesheet, line %u, %s\n", filename, last_line_read, error_message); + fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message); return false; } /* add seekpoints for each index point if required */ - if(0 != seektable) { - //@@@@ + if(0 != seekpoint_specification) { + char spec[128]; + unsigned track, index; + const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet; + if(0 == *seekpoint_specification) + *seekpoint_specification = local_strdup(""); + for(track = 0; track < cs->num_tracks; track++) { + const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track; + for(index = 0; index < tr->num_indices; index++) { + sprintf(spec, "%llu;", tr->offset + tr->indices[index].offset); + local_strcat(seekpoint_specification, spec); + } + } } *needs_write = true; return true; } -FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const Argument_Filename *cs_filename) +FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const char *cs_filename) { FILE *f; - if(0 == cs_filename->value || strlen(cs_filename->value) == 0) { + if(0 == cs_filename || strlen(cs_filename) == 0) { fprintf(stderr, "%s: ERROR: empty export file name\n", filename); return false; } - if(0 == strcmp(cs_filename->value, "-")) + if(0 == strcmp(cs_filename, "-")) f = stdout; else - f = fopen(cs_filename->value, "w"); + f = fopen(cs_filename, "w"); if(0 == f) { - fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, cs_filename->value); + fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, cs_filename); return false; } - grabbag__cuesheet_emit(f, cuesheet, "DUMMY.WAV", /*is_cdda=*/true); + grabbag__cuesheet_emit(f, cuesheet, "\"dummy.wav\" WAVE", /*is_cdda=*/true); if(f != stdout) fclose(f); diff --git a/src/metaflac/operations_shorthand_seektable.c b/src/metaflac/operations_shorthand_seektable.c index c3ad4075..8ec55872 100644 --- a/src/metaflac/operations_shorthand_seektable.c +++ b/src/metaflac/operations_shorthand_seektable.c @@ -61,6 +61,7 @@ FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Me ; if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) { fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + FLAC__metadata_object_delete(block); return false; } /* iterator is left pointing to new block */ diff --git a/src/metaflac/options.c b/src/metaflac/options.c index 5155719c..51582641 100644 --- a/src/metaflac/options.c +++ b/src/metaflac/options.c @@ -36,6 +36,7 @@ struct share__option long_options_[] = { { "no-filename", 0, 0, 0 }, { "no-utf8-convert", 0, 0, 0 }, { "dont-use-padding", 0, 0, 0 }, + { "no-cued-seekpoints", 0, 0, 0 }, /* shorthand operations */ { "show-md5sum", 0, 0, 0 }, { "show-min-blocksize", 0, 0, 0 }, @@ -118,6 +119,7 @@ void init_options(CommandLineOptions *options) options->utf8_convert = true; options->use_padding = true; + options->cued_seekpoints = true; options->show_long_help = false; options->show_version = false; options->application_data_format_is_hexdump = false; @@ -190,7 +192,11 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options) } } - //@@@@ check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to + /* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */ + if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) { + fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n"); + had_error = true; + } if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) { fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n"); @@ -200,6 +206,21 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options) if(had_error) short_usage(0); + /* + * We need to create an OP__ADD_SEEKPOINT operation if there is + * not one already, and --import-cuesheet-from was specified but + * --no-cued-seekpoints was not: + */ + if(options->cued_seekpoints) { + Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM); + if(0 != op) { + Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT); + if(0 == op2) + op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT); + op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint); + } + } + return !had_error; } @@ -230,11 +251,14 @@ void free_options(CommandLineOptions *options) break; case OP__IMPORT_VC_FROM: case OP__EXPORT_VC_TO: - case OP__IMPORT_CUESHEET_FROM: case OP__EXPORT_CUESHEET_TO: if(0 != op->argument.filename.value) free(op->argument.filename.value); break; + case OP__IMPORT_CUESHEET_FROM: + if(0 != op->argument.import_cuesheet_from.filename) + free(op->argument.import_cuesheet_from.filename); + break; case OP__ADD_SEEKPOINT: if(0 != op->argument.add_seekpoint.specification) free(op->argument.add_seekpoint.specification); @@ -300,6 +324,9 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi else if(0 == strcmp(opt, "dont-use-padding")) { options->use_padding = false; } + else if(0 == strcmp(opt, "no-cued-seekpoints")) { + options->cued_seekpoints = false; + } else if(0 == strcmp(opt, "show-md5sum")) { (void) append_shorthand_operation(options, OP__SHOW_MD5SUM); } @@ -472,9 +499,13 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi } } else if(0 == strcmp(opt, "import-cuesheet-from")) { + if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) { + fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt); + ok = false; + } op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM); FLAC__ASSERT(0 != option_argument); - if(!parse_filename(option_argument, &(op->argument.filename.value))) { + if(!parse_filename(option_argument, &(op->argument.import_cuesheet_from.filename))) { fprintf(stderr, "ERROR (--%s): missing filename\n", opt); ok = false; } @@ -926,6 +957,9 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out) else if(0 == strcmp(q, "VORBIS_COMMENT")) { out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT; } + else if(0 == strcmp(q, "CUESHEET")) { + out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET; + } else { free(s); return false; diff --git a/src/metaflac/options.h b/src/metaflac/options.h index f92d00ab..ddbd291b 100644 --- a/src/metaflac/options.h +++ b/src/metaflac/options.h @@ -135,6 +135,11 @@ typedef struct { char *specification; } Argument_AddSeekpoint; +typedef struct { + char *filename; + Argument_AddSeekpoint *add_seekpoint_link; +} Argument_ImportCuesheetFrom; + typedef struct { unsigned length; } Argument_AddPadding; @@ -148,6 +153,7 @@ typedef struct { Argument_VcFieldName vc_field_name; Argument_VcField vc_field; Argument_Filename filename; + Argument_ImportCuesheetFrom import_cuesheet_from; Argument_AddSeekpoint add_seekpoint; Argument_AddPadding add_padding; } argument; @@ -168,6 +174,7 @@ typedef struct { FLAC__bool prefix_with_filename; FLAC__bool utf8_convert; FLAC__bool use_padding; + FLAC__bool cued_seekpoints; FLAC__bool show_long_help; FLAC__bool show_version; FLAC__bool application_data_format_is_hexdump; diff --git a/src/metaflac/usage.c b/src/metaflac/usage.c index e8a7d11f..6bcbf0ad 100644 --- a/src/metaflac/usage.c +++ b/src/metaflac/usage.c @@ -136,9 +136,9 @@ int long_usage(const char *message, ...) fprintf(out, " Each line will be of the form NAME=VALUE. Specify\n"); fprintf(out, " --no-utf8-convert if necessary.\n"); fprintf(out, "--import-cuesheet-from=file Import a cuesheet from a file. Only one FLAC\n"); - fprintf(out, " file may be specified. Seekpoints for all indices in\n"); - fprintf(out, " the cuesheet will be added unless --no-cued-seekpoints\n"); - fprintf(out, " is specified.\n"); + fprintf(out, " file may be specified. A seekpoint will be added for\n"); + fprintf(out, " each index point in the cuesheet to the SEEKTABLE unless\n"); + fprintf(out, " --no-cued-seekpoints is specified.\n"); fprintf(out, "--export-cuesheet-to=file Export CUESHEET block to a cuesheet file, suitable\n"); fprintf(out, " for use by CD authoring software. Use '-' for stdin.\n"); fprintf(out, " Only one FLAC file may be specified on the command line.\n");