check that a cuesheet is legal before trying to encode with it so user will get a...
[flac.git] / src / metaflac / operations_shorthand_cuesheet.c
index 64fbf97..e9c54b5 100644 (file)
@@ -1,5 +1,5 @@
 /* metaflac - Command-line FLAC metadata editor
- * Copyright (C) 2001,2002  Josh Coalson
+ * Copyright (C) 2001,2002,2003,2004,2005  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include "share/grabbag.h"
 #include <string.h>
 
-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);
+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, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link);
+static FLAC__bool export_cs_to(const char *filename, const 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 ok = true;
        FLAC__StreamMetadata *cuesheet = 0;
        FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
-       FLAC__uint64 lead_out_offset;
+       FLAC__uint64 lead_out_offset = 0;
+       FLAC__bool is_cdda = false;
 
        if(0 == iterator)
                die("out of memory allocating iterator");
@@ -46,16 +47,18 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata
                                FLAC__metadata_iterator_delete(iterator);
                                return false;
                        }
-                       if(block->data.stream_info.sample_rate != 44100) {
-                               fprintf(stderr, "%s: ERROR: FLAC stream must currently be 44.1kHz in order to import/export cuesheet\n", filename);
-                               FLAC__metadata_iterator_delete(iterator);
-                               return false;
-                       }
+                       is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (block->data.stream_info.sample_rate == 44100);
                }
                else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
                        cuesheet = block;
        } while(FLAC__metadata_iterator_next(iterator));
 
+       if(lead_out_offset == 0) {
+               fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
+               FLAC__metadata_iterator_delete(iterator);
+               return false;
+       }
+
        switch(operation->type) {
                case OP__IMPORT_CUESHEET_FROM:
                        if(0 != cuesheet) {
@@ -63,13 +66,13 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata
                                ok = false;
                        }
                        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);
+                               ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link);
                                if(ok) {
                                        /* append CUESHEET block */
                                        while(FLAC__metadata_iterator_next(iterator))
                                                ;
                                        if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
-                                               fprintf(stderr, "%s: ERROR: adding new CUESHEET block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+                                               print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename);
                                                FLAC__metadata_object_delete(cuesheet);
                                                ok = false;
                                        }
@@ -98,7 +101,7 @@ 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 char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, Argument_AddSeekpoint *add_seekpoint_link)
+FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link)
 {
        FILE *f;
        const char *error_message;
@@ -119,7 +122,7 @@ FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet,
                return false;
        }
 
-       *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, /*is_cdda=*/true, lead_out_offset);
+       *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, is_cdda, lead_out_offset);
 
        if(f != stdin)
                fclose(f);
@@ -129,6 +132,17 @@ FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet,
                return false;
        }
 
+       if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
+               fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message);
+               return false;
+       }
+
+       /* if we're expecting CDDA, warn about non-compliance */
+       if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
+               fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message);
+               (*cuesheet)->data.cue_sheet.is_cd = false;
+       }
+
        /* add seekpoints for each index point if required */
        if(0 != seekpoint_specification) {
                char spec[128];
@@ -139,7 +153,11 @@ FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet,
                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++) {
+#ifdef _MSC_VER
+                               sprintf(spec, "%I64u;", tr->offset + tr->indices[index].offset);
+#else
                                sprintf(spec, "%llu;", tr->offset + tr->indices[index].offset);
+#endif
                                local_strcat(seekpoint_specification, spec);
                        }
                }
@@ -149,7 +167,7 @@ FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet,
        return true;
 }
 
-FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, const char *cs_filename)
+FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
 {
        FILE *f;
 
@@ -167,7 +185,7 @@ FLAC__bool export_cs_to(const char *filename, FLAC__StreamMetadata *cuesheet, co
                return false;
        }
 
-       grabbag__cuesheet_emit(f, cuesheet, "\"dummy.wav\" WAVE", /*is_cdda=*/true);
+       grabbag__cuesheet_emit(f, cuesheet, "\"dummy.wav\" WAVE");
 
        if(f != stdout)
                fclose(f);