Using libFlac from my Delphi program. Decoding works perfectly fine. But I'm having major trouble with encoding. Checked my code multiple times and I don't see what I could be doing wrong. Here's what I'm getting:
(1) If I enable verifying, the first FLAC__stream_encoder_process_interleaved fails with FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA.
(2) If I disable verifying, encoding kinda works. But the resulting file is MUCH too big with 2.6MB. WAV file is 2.8MB. Correct FLAC encoded by flac.exe is 1.5MB. However, testing the too big FLAC file succeeds and decoding succeeds, too. And the decoded WAV file is bit perfect! So the FLAC file is correct. It's just much too big. Setting compression level to 8 reduces file size a bit, but it's nowhere near the file size flac.exe produces. I've checked and number of channels, bitdepth, samplingrate etc are all correct.
I've also tried earlier libFlac versions and they all seem to behave the same way.
Logged In: YES
user_id=535287
Originator: YES
Here's a sample:
http://madshi.net/libFlacBug.zip
It contains two FLAC files. Both originate from the same WAV file. One was encoded with flac.exe. The other one with my Delphi libFlac encoder. As you can see, they have identical parameters, decode to the same bit perfect WAV file. But the one encoded with libFlac is much too big.
Logged In: YES
user_id=78173
Originator: NO
this is highly unlikely to be a libFLAC problem, most likely you are formatting the PCM data incorrectly before passing to FLAC__stream_encoder_process(). the size of your resulting FLAC file is the dead giveaway. the samples must be converted to 32-bit signed, see
http://flac.sourceforge.net/api/group__flac__stream__encoder.html#ga63
and example code:
http://flac.cvs.sourceforge.net/flac/flac/examples/c/encode/file/main.c?revision=HEAD
Logged In: YES
user_id=535287
Originator: YES
If my PCM formatting is incorrect, then why does my too big FLAC file decode to a WAV file which is bit perfect compared to the original source WAV? That doesn't make any sense to me.
But just to be sure I've replaced my PCM formatting code with the same logic you used in your sample code - no change!
Is there any other (fully compiled and linked) encoder available on win32 which uses libFlac so that I can double check?
Thanks!
Logged In: YES
user_id=535287
Originator: YES
I've done two more tests:
(1) I've swapped the two bytes of the signed 16bit PCM data. The resulting FLAC was was even bigger than before and the decode does not match the WAV source, anymore (of course).
(2) I've stored the two PCM bytes in the other half of the 4-byte-integer libFlac gets from me. As a result the final FLAC was only 8KB big because libFlac only saw 0 all the time.
So as far as I can see this proves that my PCM formatting is correct. But I think it was already proven before because if my PCM formatting was incorrect, my FLAC file wouldn't decode to a bit perfect WAV.
Logged In: YES
user_id=78173
Originator: NO
I'll need to see your code to figure out what's going on... one thing I can tell from the zipped files is that you're using a blocksize of 1152 where flac.exe is letting the encoder pick the blocksize (defaulting to 4096). I also don't see any LPC subframes in yours which should not be the case if you called FLAC__stream_encoder_set_compression_level(encoder, 8)
Logged In: YES
user_id=535287
Originator: YES
Here's the code:
http://madshi.net/madFlacEncoder.zip
I think the "big" sample is not with compression level 8, but I've left the default there.
Thanks for your help!!
Logged In: YES
user_id=78173
Originator: NO
(moving to support requests)
your formatting code is incorrect. you are zeroing out FHelperBuf, then packing it by byte. this does not sign-extend negative values. for the FLAC process calls, the buffer is an array of 32-bit signed ints with range (for 16bps) of [-32768,32767]. so if you read a 16-bit "-1" you pack bytes 0xff 0xff 0x00 0x00 (65535) when it should be 0xff 0xff 0xff 0xff (-1)
it just happens to work losslessly due to clipping in the integer stage, but the analysis stages are thrown off by the bad signal and so code most of the frames as VERBATIM (try flac -a toobig.flac to see)
make sure your formatting code works first by printing the signed dword values in FHelperBuf and making sure they match what is in the WAV file.
BTW you should always set a compression level as currently in libFLAC it will default to suboptimal settings (will be fixed in the next release)
Logged In: YES
user_id=535287
Originator: YES
Ouch. I'm embarrased!! :-O Didn't believe this could be my fault. The decoder giving me bit perfect results threw me off.
Anyway, thanks so much for your help! It's working perfectly fine now.
Logged In: YES
user_id=535287
Originator: YES
P.S: I've released an open source DirectShow FLAC decoder filter, btw. Maybe you can add it to your links section? I'm handling download, support etc through Doom9:
http://forum.doom9.org/showthread.php?t=130498
All other FLAC DirectShow projects seem either dead or the filters aren't working well (or both).