From 68e18d3a1cf7c8c83360260d7025de4a03d8a3b7 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Mon, 9 Mar 2026 08:40:53 +0800 Subject: [PATCH] doc/examples/encode_audio: fix hardcoded stereo sample stride The sample generation loop hardcodes a stride of 2 (stereo) with samples[2*j], but the channel count is dynamically selected by select_channel_layout() which picks the layout with the highest channel count. If the encoder supports more than 2 channels, samples will be written at wrong offsets. Use c->ch_layout.nb_channels as the stride instead. Signed-off-by: Jun Zhao --- doc/examples/encode_audio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/examples/encode_audio.c b/doc/examples/encode_audio.c index bb16683d94..8be9e8a82a 100644 --- a/doc/examples/encode_audio.c +++ b/doc/examples/encode_audio.c @@ -218,10 +218,10 @@ int main(int argc, char **argv) samples = (uint16_t*)frame->data[0]; for (j = 0; j < c->frame_size; j++) { - samples[2*j] = (int)(sin(t) * 10000); + samples[c->ch_layout.nb_channels*j] = (int)(sin(t) * 10000); for (k = 1; k < c->ch_layout.nb_channels; k++) - samples[2*j + k] = samples[2*j]; + samples[c->ch_layout.nb_channels*j + k] = samples[c->ch_layout.nb_channels*j]; t += tincr; } encode(c, frame, pkt, f);