Merge commit from fork

Co-authored-by: Andrey Volk <andywolk@gmail.com>
This commit is contained in:
Dmitry VerenitsinandAndrey Volk authored and GitHub committed 2026-11-18 20:34:52 +03:00
1 parent e4325bc8c1
commit bffffa40a1
2 files changed
+149 -4

No files matched your search

+19 -4
View File
@@ -1602,6 +1602,7 @@ static switch_status_t open_input_file(av_file_context_t *context, switch_file_h
if (context->has_audio) {
AVCodecContext *c[2] = { NULL };
int in_channels;
if (!(c[0] = av_get_codec_context(&context->audio_st[0]))) {
switch_goto_status(SWITCH_STATUS_FALSE, err);
@@ -1634,7 +1635,14 @@ static switch_status_t open_input_file(av_file_context_t *context, switch_file_h
context->audio_st[0].sample_rate = handle->samplerate;
context->audio_st[1].sample_rate = handle->samplerate;
if (c[0]->sample_fmt != AV_SAMPLE_FMT_S16 || c[0]->sample_rate != handle->samplerate) {
#if (LIBAVCODEC_VERSION_MAJOR < LIBAVFORMAT_V)
in_channels = c[0]->channels;
#else
in_channels = c[0]->ch_layout.nb_channels;
#endif
/* The read thread sizes its buffers by audio_st[0].channels, so a channel count mismatch must go through the resampler too */
if (c[0]->sample_fmt != AV_SAMPLE_FMT_S16 || c[0]->sample_rate != handle->samplerate || in_channels != context->audio_st[0].channels) {
int x;
for (x = 0; x < context->has_audio && x < 2 && c[x]; x++) {
@@ -1653,15 +1661,22 @@ static switch_status_t open_input_file(av_file_context_t *context, switch_file_h
av_opt_set_int(resample_ctx, "in_sample_fmt", c[x]->sample_fmt, 0);
av_opt_set_int(resample_ctx, "in_channel_layout",
(c[x]->channel_layout == 0 && c[x]->channels == 2) ? AV_CH_LAYOUT_STEREO : c[x]->channel_layout, 0);
av_opt_set_int(resample_ctx, "out_channel_count", handle->channels, 0);
av_opt_set_int(resample_ctx, "out_channel_count", context->audio_st[x].channels, 0);
#else
av_opt_set_sample_fmt(resample_ctx, "in_sample_fmt", c[x]->sample_fmt, 0);
av_opt_set_chlayout(resample_ctx, "out_chlayout", &c[x]->ch_layout, 0);
{
/* Output must match the channel count the read thread allocates for (capped at 2), not the source layout */
AVChannelLayout out_ch_layout;
av_channel_layout_default(&out_ch_layout, context->audio_st[x].channels);
av_opt_set_chlayout(resample_ctx, "out_chlayout", &out_ch_layout, 0);
av_channel_layout_uninit(&out_ch_layout);
}
#endif
av_opt_set_int(resample_ctx, "out_sample_rate", handle->samplerate,0);
#if (LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59,27,100))
av_opt_set_int(resample_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
av_opt_set_int(resample_ctx, "out_channel_layout", handle->channels == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO, 0);
av_opt_set_int(resample_ctx, "out_channel_layout", context->audio_st[x].channels == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO, 0);
#else
av_opt_set_sample_fmt(resample_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
#endif
@@ -32,6 +32,69 @@
#define SAMPLES 160
#define SURROUND_CHANNELS 6
static void put_le16(FILE *f, uint16_t v)
{
fputc(v & 0xff, f);
fputc((v >> 8) & 0xff, f);
}
static void put_le32(FILE *f, uint32_t v)
{
put_le16(f, v & 0xffff);
put_le16(f, (v >> 16) & 0xffff);
}
/* Write a 5.1 (channel mask 0x3f) 16-bit PCM WAVE_FORMAT_EXTENSIBLE file with a
* square wave on FL/FR and silence on FC/LFE/BL/BR, so that reading the 6-channel
* frames as if they were stereo leaves two of every three output frames silent. */
static switch_status_t write_surround_wav(const char *path, int rate, int seconds)
{
static const uint8_t pcm_guid[16] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 };
uint32_t frames = rate * seconds;
uint32_t data_len = frames * SURROUND_CHANNELS * 2;
uint32_t i;
int ch;
FILE *f;
if (!(f = fopen(path, "wb"))) {
return SWITCH_STATUS_FALSE;
}
fwrite("RIFF", 1, 4, f);
put_le32(f, 4 + (8 + 40) + (8 + data_len));
fwrite("WAVE", 1, 4, f);
fwrite("fmt ", 1, 4, f);
put_le32(f, 40);
put_le16(f, 0xfffe); /* WAVE_FORMAT_EXTENSIBLE */
put_le16(f, SURROUND_CHANNELS);
put_le32(f, rate);
put_le32(f, rate * SURROUND_CHANNELS * 2); /* byte rate */
put_le16(f, SURROUND_CHANNELS * 2); /* block align */
put_le16(f, 16); /* bits per sample */
put_le16(f, 22); /* extension size */
put_le16(f, 16); /* valid bits per sample */
put_le32(f, 0x3f); /* FL FR FC LFE BL BR */
fwrite(pcm_guid, 1, sizeof(pcm_guid), f);
fwrite("data", 1, 4, f);
put_le32(f, data_len);
for (i = 0; i < frames; i++) {
uint16_t s = ((i / 20) % 2) ? (uint16_t)8000 : (uint16_t)-8000;
for (ch = 0; ch < SURROUND_CHANNELS; ch++) {
put_le16(f, ch < 2 ? s : 0);
}
}
fclose(f);
return SWITCH_STATUS_SUCCESS;
}
FST_CORE_BEGIN("conf")
{
FST_MODULE_BEGIN(mod_av, mod_av_test)
@@ -250,6 +313,73 @@ FST_CORE_BEGIN("conf")
}
FST_TEST_END()
/* A >2 channel source must be resampled down to the (capped) playback
* channel count. The resampler used to keep the 5.1 source layout and
* write 6 channels into a buffer sized for 2 (heap overflow, caught by ASan).
* Without ASan the old code still fails here: the read thread took the
* 6-channel frames as stereo, so about 2/3 of the output was silence.
* 16kHz starts the resampler for the rate change; 8kHz (the open rate)
* starts it only for the channel count mismatch. */
FST_TEST_BEGIN(avformat_test_play_surround_51)
{
int rates[] = { 16000, 8000 };
int r;
for (r = 0; r < 2; r++) {
char wav[1024];
char path[1024];
switch_status_t status;
switch_file_handle_t fh = { 0 };
int16_t data[SAMPLES * 2] = { 0 };
switch_size_t len;
switch_size_t total = 0, nonzero = 0, n;
int i;
switch_snprintf(wav, sizeof(wav), "%s%savformat_test_surround_51_%d.wav", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, rates[r]);
status = write_surround_wav(wav, rates[r], 1);
fst_check(status == SWITCH_STATUS_SUCCESS);
if (status != SWITCH_STATUS_SUCCESS) {
continue;
}
switch_snprintf(path, sizeof(path), "av://%s", wav);
status = switch_core_file_open(&fh, path, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, fst_pool);
fst_check(status == SWITCH_STATUS_SUCCESS);
if (status != SWITCH_STATUS_SUCCESS) {
switch_file_remove(wav, fst_pool);
continue;
}
fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN));
fst_check(fh.real_channels <= 2);
/* 1s of audio at 8kHz; read at real-time pace, bounded at ~4s */
for (i = 0; i < 200; i++) {
len = SAMPLES;
status = switch_core_file_read(&fh, data, &len);
if (status != SWITCH_STATUS_SUCCESS) {
break;
}
total += len;
for (n = 0; n < len; n++) {
if (data[n]) {
nonzero++;
}
}
switch_yield(20000);
}
switch_core_file_close(&fh);
switch_file_remove(wav, fst_pool);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "5.1 wav at %dHz read %d samples, %d non-silent\n", rates[r], (int)total, (int)nonzero);
fst_xcheck(nonzero >= 4000, "Expected at least 0.5s of decoded audio from a 5.1 source");
}
}
FST_TEST_END()
FST_TEARDOWN_BEGIN()
{
//const char *err = NULL;