Question

I have added LibFlac in xcode project . Then I added decode/main.c from Libflac in my project. I passed infile.flac and run executable of project but it is giving following error

decoding: FAILED state: FLAC__STREAM_DECODER_END_OF_STREAM logou

t

Here is main.c

int main(int argc, char *argv[])
{
    FLAC__bool ok = true;
    FLAC__StreamDecoder *decoder = 0;
    FLAC__StreamDecoderInitStatus init_status;
    FILE *fout;

    const char *infile = "infile.flac";
    const char *outfile = "outfile.wav";

    /*
    if(argc != 3) {
        fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
        return 1;
    }
    */

    if((fout = fopen("infile.flac", "wb")) == NULL) {
        fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
        return 1;
    }

    if((decoder = FLAC__stream_decoder_new()) == NULL) {
        fprintf(stderr, "ERROR: allocating decoder\n");
        fclose(fout);
        return 1;
    }

    (void)FLAC__stream_decoder_set_md5_checking(decoder, true);

    init_status = FLAC__stream_decoder_init_file(decoder, infile, write_callback, metadata_callback, error_callback, /*client_data=*/fout);
    if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
        fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
        ok = false;
    }

    if(ok) {
        ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
        fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
        fprintf(stderr, "   state: %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
    }

    FLAC__stream_decoder_delete(decoder);
    fclose(fout);

    return 0;
}

Please help me. why I am getting this error ?

Was it helpful?

Solution

fopening your input file with "wb" will truncate your infile on opening it. That can't be what you want, right? I think you really mean;

if((fout = fopen(outfile, "wb")) == NULL) {

There seems to be some confusion how the FLAC sample works.

FLAC__stream_decoder_init_file

opens the file you give it the filename to for decoding and sets up callbacks for the decoding.

FLAC__stream_decoder_process_until_end_of_stream

decodes the file and for every decoded frame it calls the write_callback function provided in the call to FLAC__stream_decoder_init_file with the parameter given as last parameter to it.

In other words, all the work of writing the file is done in write_callback. That's where you are provided with the decoded data and you should generate and write the output file, frame by frame. If you look at the sample at http://flac.cvs.sourceforge.net/viewvc/flac/flac/examples/c/decode/file/main.c?view=markup which seems to be what you've copied to start with, that's exactly what it does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top