Skip to content

libFLAC: make FLAC__stream_encoder_finish() restore the compression-level defaults - #954

Open
GeekQAQ wants to merge 1 commit into
xiph:masterfrom
GeekQAQ:encoder-finish-defaults
Open

GeekQAQ wants to merge 1 commit into
xiph:masterfrom
GeekQAQ:encoder-finish-defaults

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown

FLAC__stream_encoder_finish() is documented to reset all settings to the constructor defaults (stream_encoder.h: "FLAC__stream_encoder_finish() resets all settings to the constructor defaults", and the FLAC__stream_encoder_finish description). It calls set_defaults_(), which zeroes the compression-level settings and then calls FLAC__stream_encoder_set_compression_level(encoder, 5). At that point the state is still FLAC__STREAM_ENCODER_OK, so set_compression_level() returns false without doing anything, and the encoder is left with do_mid_side_stereo = false, max_lpc_order = 0, max_residual_partition_order = 0, etc. FLAC__stream_encoder_new() sets the state to UNINITIALIZED before calling set_defaults_(), so a new encoder gets level 5.

This is the same problem that #294 fixed for FLAC__stream_encoder_new(), where set_defaults_() used to run before the state was set to UNINITIALIZED (the comment there notes that the default became level 5 "as was intended"). The call in finish() still runs while the state is OK.

Reproduction

Tested on master 59fa340. The program encreuse.c in https://gist.github.com/GeekQAQ/4f489659d418c52779eff031d5654675 (with flac_common.h) encodes the same 8000 samples twice with one encoder, setting only channels, bits per sample and sample rate (no compression level):

cc -o encreuse encreuse.c $(pkg-config --cflags --libs flac ogg)
./encreuse defaults reuse   # second stream, after finish(): mid_side=0 max_lpc_order=0 blocksize=1152, 15713 bytes
./encreuse defaults fresh   # new encoder: mid_side=1 max_lpc_order=8 blocksize=4096, 11313 bytes

Calling FLAC__stream_encoder_set_compression_level(encoder, 5) after finish() gives the same result as a new encoder (explicit mode).

Change

finish() temporarily sets the state to FLAC__STREAM_ENCODER_UNINITIALIZED while calling set_defaults_() and restores it afterwards, so the existing state handling after it is unchanged. With the change, the encoder after finish() reports the same settings and produces byte-identical output to a new encoder. test_libFLAC passes (ctest libFLAC, ASan+UBSan build).

…evel defaults

`FLAC__stream_encoder_finish()` is documented to reset all settings to
the constructor defaults (stream_encoder.h:
"FLAC__stream_encoder_finish() resets all settings to the constructor
defaults", and the `FLAC__stream_encoder_finish` description). It calls
`set_defaults_()`, which zeroes the compression-level settings and then
calls `FLAC__stream_encoder_set_compression_level(encoder, 5)`. At that
point the state is still `FLAC__STREAM_ENCODER_OK`, so
`set_compression_level()` returns `false` without doing anything, and
the encoder is left with `do_mid_side_stereo = false`, `max_lpc_order =
0`, `max_residual_partition_order = 0`, etc.
`FLAC__stream_encoder_new()` sets the state to `UNINITIALIZED` before
calling `set_defaults_()`, so a new encoder gets level 5.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 19:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants