Error decoding / encoding flac to / from wav

I added LibFlac to the xcode project. Then I added decode / main.c from Libflac to my project. I passed infile.flac and executed the project executable, but it gave the 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 am i getting this error?

+4
source share
1 answer

Opening your input file with "wb" will truncate your intrusion when you open it. It may not be what you want, right? I think you really mean:

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

There seems to be some confusion about how the FLAC pattern works.

 FLAC__stream_decoder_init_file 

opens the file that you give it the file name for decoding, and sets callbacks for decoding.

 FLAC__stream_decoder_process_until_end_of_stream 

decodes the file and for each decoded frame it calls the write_callback function provided in the FLAC__stream_decoder_init_file call, with the parameter specified as the last parameter.

In other words, all the work of writing a file is done in write_callback. This is where you get the decoded data, and you have to generate and write the output file, frame by frame. If you look at the sample http://flac.cvs.sourceforge.net/viewvc/flac/flac/examples/c/decode/file/main.c?view=markup , which seems to be what you copied from what exactly is he doing.

+3
source

Source: https://habr.com/ru/post/1388426/


All Articles