Skip to content

Commit 59fc0fd

Browse files
lrgirdwolgirdwood
authored andcommitted
ffmpeg_dec: address G.726 review comments and fix EOS drain
Address PR #11462 review feedback and unframed compress EOS drain issues: - Reject explicit non-mono G.726 capture requests (req_ch > 1) with -EINVAL in both validate_params and configure while preserving stereo-to-mono downmix when mono or default channels are requested. - Add validate_params hook to ffmpeg_dec_codec_ops and reject explicit multichannel G.726 playback requests (req_channels > 1) with -EINVAL at both IPC init and configure_avctx time while still allowing mono streams to upmix to stereo PCM sinks. - Upgrade the 16->8 kHz stage of ffmpeg_dec_pcm_decimate_6to1_s16_mono() from a 3-tap [1, 2, 1]/4 filter to a 29-tap Kaiser-windowed linear-phase low-pass FIR (fc = 0.225 * 16 kHz = 3.6 kHz, beta = 5.2, unity Q15) with a persistent 27-sample delay line (FFMPEG_DECIM_6TO1_HIST_LEN = 39), achieving -22.9 dB at 4 kHz Nyquist, -72.6 dB at 5 kHz, and >63 dB stopband attenuation above 4.5 kHz. - Clear min_available on compressed decode sources in ffmpeg_dec_prepare() (with EXPORT_SYMBOL(source_set_min_available)) and gate ffmpeg_dec_is_ready_to_process() on eos_sent / final host_eos flush so comp_check_eos() does not inject trailing 0x00 silence blocks into unframed G.711/G.726/aptX bitstreams at compress drain. Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
1 parent 2c20c1a commit 59fc0fd

8 files changed

Lines changed: 155 additions & 9 deletions

File tree

‎src/audio/ffmpeg_dec/ffmpeg-enc-g726.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ static int ffmpeg_enc_g726_validate_params(struct processing_module *mod,
3434
const struct snd_codec *codec_params)
3535
{
3636
struct comp_dev *dev = mod->dev;
37+
uint32_t req_ch = codec_params->ch_in ?
38+
codec_params->ch_in : codec_params->ch_out;
39+
40+
if (req_ch > 1) {
41+
comp_err(dev, "G.726 encoder only supports mono (got %u ch)", req_ch);
42+
return -EINVAL;
43+
}
3744

3845
if (codec_params->format != SND_AUDIOSTREAMFORMAT_UNDEFINED &&
3946
codec_params->format != SND_AUDIOSTREAMFORMAT_RAW &&
@@ -104,6 +111,12 @@ static int ffmpeg_enc_g726_configure(struct processing_module *mod, struct ffmpe
104111
uint32_t cs;
105112
int ret;
106113

114+
if (e->req_channels > 1) {
115+
comp_err(dev, "G.726 encoder only supports mono (got %u ch)",
116+
e->req_channels);
117+
return -EINVAL;
118+
}
119+
107120
if (enc_rate == cd->out_rate) {
108121
g726->decimate = 1;
109122
} else if (cd->out_rate == 48000 && enc_rate == 16000) {

‎src/audio/ffmpeg_dec/ffmpeg_dec-convert.c‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,40 @@ void ffmpeg_dec_pcm_decimate_6to1_s16_mono(const uint8_t *in, int16_t *dst,
507507
int nb_out_samples,
508508
int16_t hist[FFMPEG_DECIM_6TO1_HIST_LEN])
509509
{
510+
/*
511+
* Second stage (16->8 kHz): 29-tap symmetric Kaiser-windowed linear-phase
512+
* low-pass FIR (fc = 0.225 * 16 kHz = 3.6 kHz, beta = 5.2, unity Q15 gain
513+
* 32768). Combined with decim_3to1_step(): -22.9 dB at 4 kHz Nyquist,
514+
* -72.6 dB at 5 kHz, >63 dB stopband rejection above 4.5 kHz.
515+
*/
516+
static const int32_t h[15] = {
517+
19, -24, -96, 27, 266, 62, -543, -364,
518+
899, 1086, -1263, -2781, 1537, 10184, 14750,
519+
};
520+
int16_t *hist2 = &hist[FFMPEG_DECIM_3TO1_HIST_LEN];
510521
int i;
511522

512523
for (i = 0; i < nb_out_samples; i++) {
513524
int16_t y0 = decim_3to1_step(in, 2 * i + 0, src_fmt, src_ch, hist);
514525
int16_t y1 = decim_3to1_step(in, 2 * i + 1, src_fmt, src_ch, hist);
515-
int32_t acc = (int32_t)hist[12] + 2 * (int32_t)y0 + (int32_t)y1 + 2;
526+
int32_t acc;
527+
int k;
528+
529+
acc = h[0] * ((int32_t)hist2[0] + (int32_t)y1) +
530+
h[1] * ((int32_t)hist2[1] + (int32_t)y0) +
531+
h[14] * (int32_t)hist2[14];
532+
for (k = 2; k < 14; k++) {
533+
acc += h[k] * ((int32_t)hist2[k] + (int32_t)hist2[28 - k]);
534+
}
535+
acc = (acc + 16384) >> 15;
536+
537+
for (k = 0; k < FFMPEG_DECIM_2TO1_HIST_LEN - 2; k++) {
538+
hist2[k] = hist2[k + 2];
539+
}
540+
hist2[FFMPEG_DECIM_2TO1_HIST_LEN - 2] = y0;
541+
hist2[FFMPEG_DECIM_2TO1_HIST_LEN - 1] = y1;
516542

517-
dst[i] = sat_int16(acc >> 2);
518-
hist[12] = y1;
543+
dst[i] = sat_int16(acc);
519544
}
520545
}
521546

‎src/audio/ffmpeg_dec/ffmpeg_dec-convert.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@
2828
int ffmpeg_dec_convert_frame(const AVFrame *frame, uint8_t *out, size_t out_size,
2929
int sink_fmt, int channels);
3030

31+
#ifndef FFMPEG_DECIM_3TO1_HIST_LEN
3132
#define FFMPEG_DECIM_3TO1_HIST_LEN 12
32-
#define FFMPEG_DECIM_6TO1_HIST_LEN 13
33+
#endif
34+
#ifndef FFMPEG_DECIM_2TO1_HIST_LEN
35+
#define FFMPEG_DECIM_2TO1_HIST_LEN 27
36+
#endif
37+
#ifndef FFMPEG_DECIM_6TO1_HIST_LEN
38+
#define FFMPEG_DECIM_6TO1_HIST_LEN (FFMPEG_DECIM_3TO1_HIST_LEN + FFMPEG_DECIM_2TO1_HIST_LEN)
39+
#endif
3340

3441
/**
3542
* ffmpeg_dec_pcm_to_frame - Convert interleaved SOF PCM into an AVFrame.
@@ -65,7 +72,7 @@ void ffmpeg_dec_pcm_decimate_3to1_s16_mono(const uint8_t *in, int16_t *dst,
6572
* @src_fmt: SOF valid sample format (SOF_IPC_FRAME_S16_LE / S24_4LE / S32_LE).
6673
* @src_ch: Input channel count.
6774
* @nb_out_samples: Number of 8 kHz output samples to produce.
68-
* @hist: Stateful 13-sample FIR delay line preserved across frames.
75+
* @hist: Stateful 39-sample two-stage FIR delay line preserved across frames.
6976
*/
7077
void ffmpeg_dec_pcm_decimate_6to1_s16_mono(const uint8_t *in, int16_t *dst,
7178
int src_fmt, int src_ch,

‎src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ static int ffmpeg_dec_ff_decode(struct processing_module *mod,
391391
}
392392
if (raw_len >= cap ||
393393
(cd->codec_ops && cd->codec_ops->limit_packet_size)) {
394-
comp_err(dev, "unparsed packet reassembly buffer full or invalid block");
394+
comp_err(dev, "unparsed decode failed %d (raw=%zu pkt=%zu prev=%zu)",
395+
ret, raw_len, pkt_len, prev_len);
395396
ff->pktbuf_len = 0;
396397
return -EIO;
397398
}

‎src/audio/ffmpeg_dec/ffmpeg_dec-g726.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ static int ffmpeg_dec_g726_code_size(const struct ffmpeg_dec_comp_data *cd, uint
3636
return (int)cs;
3737
}
3838

39+
static int ffmpeg_dec_g726_validate_params(struct processing_module *mod,
40+
const struct snd_codec *codec_params)
41+
{
42+
uint32_t req_ch = codec_params->ch_in ?
43+
codec_params->ch_in : codec_params->ch_out;
44+
45+
if (codec_params->profile > 1) {
46+
comp_err(mod->dev, "unsupported G.726 profile %u", codec_params->profile);
47+
return -EINVAL;
48+
}
49+
50+
if (req_ch > 1) {
51+
comp_err(mod->dev, "G.726 decoder only supports mono (got %u ch)", req_ch);
52+
return -EINVAL;
53+
}
54+
55+
return 0;
56+
}
57+
3958
static uint32_t ffmpeg_dec_g726_max_frame_samples(const struct ffmpeg_dec_comp_data *cd)
4059
{
4160
(void)cd;
@@ -82,6 +101,12 @@ static int ffmpeg_dec_g726_configure_avctx(struct processing_module *mod, void *
82101
return -EINVAL;
83102
}
84103

104+
if (cd->req_channels > 1) {
105+
comp_err(mod->dev, "G.726 decoder only supports mono (got %u ch)",
106+
cd->req_channels);
107+
return -EINVAL;
108+
}
109+
85110
cs = ffmpeg_dec_g726_code_size(cd, rate);
86111
if (cs < 0) {
87112
comp_err(mod->dev, "unsupported G.726 bitrate %u at %u Hz",
@@ -100,6 +125,7 @@ const struct ffmpeg_dec_codec_ops ffmpeg_dec_g726_ops = {
100125
.id = FFMPEG_DEC_CODEC_G726,
101126
.snd_codec_id = SND_AUDIOCODEC_G726,
102127
.name = "g726",
128+
.validate_params = ffmpeg_dec_g726_validate_params,
103129
.max_frame_samples = ffmpeg_dec_g726_max_frame_samples,
104130
#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB)
105131
.configure_avctx = ffmpeg_dec_g726_configure_avctx,

‎src/audio/ffmpeg_dec/ffmpeg_dec.c‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,17 @@ __cold static int ffmpeg_dec_init(struct processing_module *mod)
379379
comp_err(dev, "unsupported or disabled snd_codec id 0x%x",
380380
codec_params->id);
381381
mod_free(mod, cd);
382+
md->private = NULL;
382383
return -ENOTSUP;
383384
}
385+
if (ops->validate_params) {
386+
ret = ops->validate_params(mod, codec_params);
387+
if (ret) {
388+
mod_free(mod, cd);
389+
md->private = NULL;
390+
return ret;
391+
}
392+
}
384393
cd->codec_ops = ops;
385394
cd->codec = ops->id;
386395
cd->req_rate = codec_params->sample_rate;
@@ -492,6 +501,15 @@ static int ffmpeg_dec_prepare(struct processing_module *mod,
492501
}
493502
sink = sinks[0];
494503

504+
/*
505+
* Compressed inputs have no fixed frame-size alignment for EOS zero-fill:
506+
* clear min_available so comp_check_eos() does not inject 0x00 silence
507+
* blocks into unframed bitstreams (G.711/G.726/aptX) after host EOS.
508+
*/
509+
if (num_of_sources > 0 && sources[0]) {
510+
source_set_min_available(sources[0], 0);
511+
}
512+
495513
/* Cache the PCM format the decoder must produce for the pipeline. */
496514
cd->out_rate = sink_get_rate(sink);
497515
cd->out_channels = sink_get_channels(sink);
@@ -551,6 +569,9 @@ static int ffmpeg_dec_prepare(struct processing_module *mod,
551569
cd->unparsed_skip_to_eof = false;
552570
cd->draining = false;
553571
cd->drained = false;
572+
#if CONFIG_IPC_MAJOR_4
573+
cd->eos_sent = false;
574+
#endif
554575

555576
return 0;
556577
}
@@ -606,13 +627,35 @@ static bool ffmpeg_dec_is_ready_to_process(struct processing_module *mod,
606627
if (num_of_sources < 1 || num_of_sinks < 1)
607628
return false;
608629

630+
#if CONFIG_IPC_MAJOR_4
631+
/* Do not pull trailing EOS-flush silence blocks once compress drain completed. */
632+
if (cd->eos_sent) {
633+
return false;
634+
}
635+
#endif
636+
609637
if (sink_get_free_size(sinks[0]) == 0)
610638
return false;
611639

612640
if (cd->pcm_avail)
613641
return true;
614642

615-
return source_get_data_available(sources[0]) > 0;
643+
if (source_get_data_available(sources[0]) > 0) {
644+
return true;
645+
}
646+
647+
#if CONFIG_IPC_MAJOR_4
648+
{
649+
enum sof_audio_buffer_state src_state = source_get_state(sources[0]);
650+
651+
if (src_state == AUDIOBUF_STATE_END_OF_STREAM ||
652+
src_state == AUDIOBUF_STATE_END_OF_STREAM_FLUSH) {
653+
return true;
654+
}
655+
}
656+
#endif
657+
658+
return false;
616659
}
617660

618661
/**
@@ -669,6 +712,13 @@ static int ffmpeg_dec_process(struct processing_module *mod,
669712
return 0;
670713
}
671714

715+
#if CONFIG_IPC_MAJOR_4
716+
if (cd->eos_sent) {
717+
source_drop_data(src, source_get_data_available(src));
718+
return 0;
719+
}
720+
#endif
721+
672722
/* One-time: strip the container header so the parser sees frames. */
673723
if (!cd->hdr_done) {
674724
if (cd->codec_ops && cd->codec_ops->skip_container) {
@@ -779,6 +829,22 @@ static int ffmpeg_dec_process(struct processing_module *mod,
779829
if (ret) {
780830
if (direct_sink)
781831
sink_commit_buffer(sink, 0);
832+
if (host_eos) {
833+
bool all_zero = true;
834+
835+
for (size_t i = 0; i < req; i++) {
836+
if (cd->in_buf[i] != 0) {
837+
all_zero = false;
838+
break;
839+
}
840+
}
841+
if (all_zero) {
842+
source_drop_data(src, source_get_data_available(src));
843+
cd->drained = true;
844+
ffmpeg_dec_signal_eos(mod, sink);
845+
return 0;
846+
}
847+
}
782848
comp_err(dev, "decode failed %d", ret);
783849
return ret;
784850
}
@@ -812,7 +878,8 @@ static int ffmpeg_dec_process(struct processing_module *mod,
812878
* produced no PCM and no staged PCM remains, enter draining mode to
813879
* flush the parser and codec before signaling EOS.
814880
*/
815-
if (host_eos && !produced && !cd->pcm_avail) {
881+
if (host_eos && !cd->pcm_avail &&
882+
(!produced || source_get_data_available(src) == 0)) {
816883
if (!cd->drained) {
817884
size_t c_consumed = 0;
818885
size_t c_flushed = 0;

‎src/audio/ffmpeg_dec/ffmpeg_dec.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ struct ffmpeg_enc_codec_ops {
161161
void (*free)(struct processing_module *mod, struct ffmpeg_enc_data *e);
162162
};
163163

164+
#define FFMPEG_DECIM_3TO1_HIST_LEN 12
165+
#define FFMPEG_DECIM_2TO1_HIST_LEN 27
166+
#define FFMPEG_DECIM_6TO1_HIST_LEN (FFMPEG_DECIM_3TO1_HIST_LEN + FFMPEG_DECIM_2TO1_HIST_LEN)
167+
164168
/**
165169
* struct ffmpeg_enc_data - Common encoder state across all encode codecs.
166170
* @codec_ops: Selected per-codec encoder operations.
@@ -189,7 +193,7 @@ struct ffmpeg_enc_data {
189193
uint32_t req_channels;
190194
uint32_t req_ch_mode;
191195
uint32_t req_profile;
192-
int16_t decim_hist[13];
196+
int16_t decim_hist[FFMPEG_DECIM_6TO1_HIST_LEN];
193197
int src_fmt;
194198
int frame_bytes;
195199
int nb;
@@ -246,6 +250,8 @@ struct ffmpeg_dec_codec_ops {
246250
uint32_t snd_codec_id;
247251
const char *name;
248252
const char *decoder_name;
253+
int (*validate_params)(struct processing_module *mod,
254+
const struct snd_codec *codec_params);
249255
uint32_t (*max_frame_samples)(const struct ffmpeg_dec_comp_data *cd);
250256
int (*skip_container)(struct processing_module *mod, struct sof_source *src);
251257
void (*prepare_extradata)(struct processing_module *mod);

‎src/audio/source_api_helper.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@ void source_set_min_available(struct sof_source *source, size_t min_available)
105105
{
106106
source->min_available = min_available;
107107
}
108+
EXPORT_SYMBOL(source_set_min_available);

0 commit comments

Comments
 (0)