From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709650166142.05127686514925; Mon, 6 Feb 2023 10:54:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bj-0004wJ-Fl; Mon, 06 Feb 2023 13:52:51 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6be-0004vk-Ec for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:50 -0500 Received: from mailout10.t-online.de ([194.25.134.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bb-0005RO-O7 for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:46 -0500 Received: from fwd83.dcpf.telekom.de (fwd83.aul.t-online.de [10.223.144.109]) by mailout10.t-online.de (Postfix) with SMTP id 67E7F16838; Mon, 6 Feb 2023 19:52:38 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd83.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bV-0pMZOb0; Mon, 6 Feb 2023 19:52:37 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 7245F2001FB; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 01/17] audio: change type of mix_buf and conv_buf Date: Mon, 6 Feb 2023 19:52:21 +0100 Message-Id: <20230206185237.8358-1-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709557-41ABE378-C5BEE7C2/0/0 CLEAN NORMAL X-TOI-MSGID: df4a4a22-81bf-424b-9b09-05aef69fc74e Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.21; envelope-from=volker.ruemelin@t-online.de; helo=mailout10.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709650531100003 Change the type of mix_buf in struct HWVoiceOut and conv_buf in struct HWVoiceIn from STSampleBuffer * to STSampleBuffer. However, a buffer pointer is still needed. For this reason in struct STSampleBuffer samples[] is changed to *buffer. This is a preparation for the next patch. The next patch will add this line, which is not possible with the current struct STSampleBuffer definition. + sw->resample_buf.buffer =3D hw->mix_buf.buffer + rpos2; There are no functional changes. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 106 ++++++++++++++++++++--------------------- audio/audio_int.h | 6 +-- audio/audio_template.h | 19 ++++---- 3 files changed, 67 insertions(+), 64 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 772c3cc320..a0b54e4a2e 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -523,8 +523,8 @@ static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw) static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw) { size_t live =3D hw->total_samples_captured - audio_pcm_hw_find_min_in = (hw); - if (audio_bug(__func__, live > hw->conv_buf->size)) { - dolog("live=3D%zu hw->conv_buf->size=3D%zu\n", live, hw->conv_buf-= >size); + if (audio_bug(__func__, live > hw->conv_buf.size)) { + dolog("live=3D%zu hw->conv_buf.size=3D%zu\n", live, hw->conv_buf.s= ize); return 0; } return live; @@ -533,13 +533,13 @@ static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw) static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t sa= mples) { size_t conv =3D 0; - STSampleBuffer *conv_buf =3D hw->conv_buf; + STSampleBuffer *conv_buf =3D &hw->conv_buf; =20 while (samples) { uint8_t *src =3D advance(pcm_buf, conv * hw->info.bytes_per_frame); size_t proc =3D MIN(samples, conv_buf->size - conv_buf->pos); =20 - hw->conv(conv_buf->samples + conv_buf->pos, src, proc); + hw->conv(conv_buf->buffer + conv_buf->pos, src, proc); conv_buf->pos =3D (conv_buf->pos + proc) % conv_buf->size; samples -=3D proc; conv +=3D proc; @@ -561,12 +561,12 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *= buf, size_t size) if (!live) { return 0; } - if (audio_bug(__func__, live > hw->conv_buf->size)) { - dolog("live_in=3D%zu hw->conv_buf->size=3D%zu\n", live, hw->conv_b= uf->size); + if (audio_bug(__func__, live > hw->conv_buf.size)) { + dolog("live_in=3D%zu hw->conv_buf.size=3D%zu\n", live, hw->conv_bu= f.size); return 0; } =20 - rpos =3D audio_ring_posb(hw->conv_buf->pos, live, hw->conv_buf->size); + rpos =3D audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size); =20 samples =3D size / sw->info.bytes_per_frame; =20 @@ -574,11 +574,11 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *= buf, size_t size) swlim =3D MIN (swlim, samples); =20 while (swlim) { - src =3D hw->conv_buf->samples + rpos; - if (hw->conv_buf->pos > rpos) { - isamp =3D hw->conv_buf->pos - rpos; + src =3D hw->conv_buf.buffer + rpos; + if (hw->conv_buf.pos > rpos) { + isamp =3D hw->conv_buf.pos - rpos; } else { - isamp =3D hw->conv_buf->size - rpos; + isamp =3D hw->conv_buf.size - rpos; } =20 if (!isamp) { @@ -588,7 +588,7 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *bu= f, size_t size) =20 st_rate_flow (sw->rate, src, dst, &isamp, &osamp); swlim -=3D osamp; - rpos =3D (rpos + isamp) % hw->conv_buf->size; + rpos =3D (rpos + isamp) % hw->conv_buf.size; dst +=3D osamp; ret +=3D osamp; total +=3D isamp; @@ -636,8 +636,8 @@ static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw= , int *nb_live) if (nb_live1) { size_t live =3D smin; =20 - if (audio_bug(__func__, live > hw->mix_buf->size)) { - dolog("live=3D%zu hw->mix_buf->size=3D%zu\n", live, hw->mix_bu= f->size); + if (audio_bug(__func__, live > hw->mix_buf.size)) { + dolog("live=3D%zu hw->mix_buf.size=3D%zu\n", live, hw->mix_buf= .size); return 0; } return live; @@ -654,17 +654,17 @@ static size_t audio_pcm_hw_get_free(HWVoiceOut *hw) static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t le= n) { size_t clipped =3D 0; - size_t pos =3D hw->mix_buf->pos; + size_t pos =3D hw->mix_buf.pos; =20 while (len) { - st_sample *src =3D hw->mix_buf->samples + pos; + st_sample *src =3D hw->mix_buf.buffer + pos; uint8_t *dst =3D advance(pcm_buf, clipped * hw->info.bytes_per_fra= me); - size_t samples_till_end_of_buf =3D hw->mix_buf->size - pos; + size_t samples_till_end_of_buf =3D hw->mix_buf.size - pos; size_t samples_to_clip =3D MIN(len, samples_till_end_of_buf); =20 hw->clip(dst, src, samples_to_clip); =20 - pos =3D (pos + samples_to_clip) % hw->mix_buf->size; + pos =3D (pos + samples_to_clip) % hw->mix_buf.size; len -=3D samples_to_clip; clipped +=3D samples_to_clip; } @@ -683,11 +683,11 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void= *buf, size_t size) return size; } =20 - hwsamples =3D sw->hw->mix_buf->size; + hwsamples =3D sw->hw->mix_buf.size; =20 live =3D sw->total_hw_samples_mixed; if (audio_bug(__func__, live > hwsamples)) { - dolog("live=3D%zu hw->mix_buf->size=3D%zu\n", live, hwsamples); + dolog("live=3D%zu hw->mix_buf.size=3D%zu\n", live, hwsamples); return 0; } =20 @@ -698,7 +698,7 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *= buf, size_t size) return 0; } =20 - wpos =3D (sw->hw->mix_buf->pos + live) % hwsamples; + wpos =3D (sw->hw->mix_buf.pos + live) % hwsamples; =20 dead =3D hwsamples - live; hw_free =3D audio_pcm_hw_get_free(sw->hw); @@ -725,7 +725,7 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *= buf, size_t size) st_rate_flow_mix ( sw->rate, sw->buf + pos, - sw->hw->mix_buf->samples + wpos, + sw->hw->mix_buf.buffer + wpos, &isamp, &osamp ); @@ -989,9 +989,9 @@ static size_t audio_get_avail (SWVoiceIn *sw) } =20 live =3D sw->hw->total_samples_captured - sw->total_hw_samples_acquire= d; - if (audio_bug(__func__, live > sw->hw->conv_buf->size)) { - dolog("live=3D%zu sw->hw->conv_buf->size=3D%zu\n", live, - sw->hw->conv_buf->size); + if (audio_bug(__func__, live > sw->hw->conv_buf.size)) { + dolog("live=3D%zu sw->hw->conv_buf.size=3D%zu\n", live, + sw->hw->conv_buf.size); return 0; } =20 @@ -1026,13 +1026,13 @@ static size_t audio_get_free(SWVoiceOut *sw) =20 live =3D sw->total_hw_samples_mixed; =20 - if (audio_bug(__func__, live > sw->hw->mix_buf->size)) { - dolog("live=3D%zu sw->hw->mix_buf->size=3D%zu\n", live, - sw->hw->mix_buf->size); + if (audio_bug(__func__, live > sw->hw->mix_buf.size)) { + dolog("live=3D%zu sw->hw->mix_buf.size=3D%zu\n", live, + sw->hw->mix_buf.size); return 0; } =20 - dead =3D sw->hw->mix_buf->size - live; + dead =3D sw->hw->mix_buf.size - live; =20 #ifdef DEBUG_OUT dolog("%s: get_free live %zu dead %zu frontend frames %zu\n", @@ -1056,12 +1056,12 @@ static void audio_capture_mix_and_clear(HWVoiceOut = *hw, size_t rpos, =20 n =3D samples; while (n) { - size_t till_end_of_hw =3D hw->mix_buf->size - rpos2; + size_t till_end_of_hw =3D hw->mix_buf.size - rpos2; size_t to_write =3D MIN(till_end_of_hw, n); size_t bytes =3D to_write * hw->info.bytes_per_frame; size_t written; =20 - sw->buf =3D hw->mix_buf->samples + rpos2; + sw->buf =3D hw->mix_buf.buffer + rpos2; written =3D audio_pcm_sw_write (sw, NULL, bytes); if (written - bytes) { dolog("Could not mix %zu bytes into a capture " @@ -1070,14 +1070,14 @@ static void audio_capture_mix_and_clear(HWVoiceOut = *hw, size_t rpos, break; } n -=3D to_write; - rpos2 =3D (rpos2 + to_write) % hw->mix_buf->size; + rpos2 =3D (rpos2 + to_write) % hw->mix_buf.size; } } } =20 - n =3D MIN(samples, hw->mix_buf->size - rpos); - mixeng_clear(hw->mix_buf->samples + rpos, n); - mixeng_clear(hw->mix_buf->samples, samples - n); + n =3D MIN(samples, hw->mix_buf.size - rpos); + mixeng_clear(hw->mix_buf.buffer + rpos, n); + mixeng_clear(hw->mix_buf.buffer, samples - n); } =20 static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live) @@ -1103,7 +1103,7 @@ static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, si= ze_t live) =20 live -=3D proc; clipped +=3D proc; - hw->mix_buf->pos =3D (hw->mix_buf->pos + proc) % hw->mix_buf->size; + hw->mix_buf.pos =3D (hw->mix_buf.pos + proc) % hw->mix_buf.size; =20 if (proc =3D=3D 0 || proc < decr) { break; @@ -1174,8 +1174,8 @@ static void audio_run_out (AudioState *s) live =3D 0; } =20 - if (audio_bug(__func__, live > hw->mix_buf->size)) { - dolog("live=3D%zu hw->mix_buf->size=3D%zu\n", live, hw->mix_bu= f->size); + if (audio_bug(__func__, live > hw->mix_buf.size)) { + dolog("live=3D%zu hw->mix_buf.size=3D%zu\n", live, hw->mix_buf= .size); continue; } =20 @@ -1203,13 +1203,13 @@ static void audio_run_out (AudioState *s) continue; } =20 - prev_rpos =3D hw->mix_buf->pos; + prev_rpos =3D hw->mix_buf.pos; played =3D audio_pcm_hw_run_out(hw, live); replay_audio_out(&played); - if (audio_bug(__func__, hw->mix_buf->pos >=3D hw->mix_buf->size)) { - dolog("hw->mix_buf->pos=3D%zu hw->mix_buf->size=3D%zu played= =3D%zu\n", - hw->mix_buf->pos, hw->mix_buf->size, played); - hw->mix_buf->pos =3D 0; + if (audio_bug(__func__, hw->mix_buf.pos >=3D hw->mix_buf.size)) { + dolog("hw->mix_buf.pos=3D%zu hw->mix_buf.size=3D%zu played=3D%= zu\n", + hw->mix_buf.pos, hw->mix_buf.size, played); + hw->mix_buf.pos =3D 0; } =20 #ifdef DEBUG_OUT @@ -1290,10 +1290,10 @@ static void audio_run_in (AudioState *s) =20 if (replay_mode !=3D REPLAY_MODE_PLAY) { captured =3D audio_pcm_hw_run_in( - hw, hw->conv_buf->size - audio_pcm_hw_get_live_in(hw)); + hw, hw->conv_buf.size - audio_pcm_hw_get_live_in(hw)); } - replay_audio_in(&captured, hw->conv_buf->samples, &hw->conv_buf->p= os, - hw->conv_buf->size); + replay_audio_in(&captured, hw->conv_buf.buffer, &hw->conv_buf.pos, + hw->conv_buf.size); =20 min =3D audio_pcm_hw_find_min_in (hw); hw->total_samples_captured +=3D captured - min; @@ -1326,14 +1326,14 @@ static void audio_run_capture (AudioState *s) SWVoiceOut *sw; =20 captured =3D live =3D audio_pcm_hw_get_live_out (hw, NULL); - rpos =3D hw->mix_buf->pos; + rpos =3D hw->mix_buf.pos; while (live) { - size_t left =3D hw->mix_buf->size - rpos; + size_t left =3D hw->mix_buf.size - rpos; size_t to_capture =3D MIN(live, left); struct st_sample *src; struct capture_callback *cb; =20 - src =3D hw->mix_buf->samples + rpos; + src =3D hw->mix_buf.buffer + rpos; hw->clip (cap->buf, src, to_capture); mixeng_clear (src, to_capture); =20 @@ -1341,10 +1341,10 @@ static void audio_run_capture (AudioState *s) cb->ops.capture (cb->opaque, cap->buf, to_capture * hw->info.bytes_per_frame); } - rpos =3D (rpos + to_capture) % hw->mix_buf->size; + rpos =3D (rpos + to_capture) % hw->mix_buf.size; live -=3D to_capture; } - hw->mix_buf->pos =3D rpos; + hw->mix_buf.pos =3D rpos; =20 for (sw =3D hw->sw_head.lh_first; sw; sw =3D sw->entries.le_next) { if (!sw->active && sw->empty) { @@ -1903,7 +1903,7 @@ CaptureVoiceOut *AUD_add_capture( =20 audio_pcm_init_info (&hw->info, as); =20 - cap->buf =3D g_malloc0_n(hw->mix_buf->size, hw->info.bytes_per_fra= me); + cap->buf =3D g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_fram= e); =20 if (hw->info.is_float) { hw->clip =3D mixeng_clip_float[hw->info.nchannels =3D=3D 2]; @@ -1955,7 +1955,7 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_= opaque) sw =3D sw1; } QLIST_REMOVE (cap, entries); - g_free (cap->hw.mix_buf); + g_free(cap->hw.mix_buf.buffer); g_free (cap->buf); g_free (cap); } diff --git a/audio/audio_int.h b/audio/audio_int.h index 5028f2354a..061845dcc2 100644 --- a/audio/audio_int.h +++ b/audio/audio_int.h @@ -58,7 +58,7 @@ typedef struct SWVoiceCap SWVoiceCap; =20 typedef struct STSampleBuffer { size_t pos, size; - st_sample samples[]; + st_sample *buffer; } STSampleBuffer; =20 typedef struct HWVoiceOut { @@ -71,7 +71,7 @@ typedef struct HWVoiceOut { f_sample *clip; uint64_t ts_helper; =20 - STSampleBuffer *mix_buf; + STSampleBuffer mix_buf; void *buf_emul; size_t pos_emul, pending_emul, size_emul; =20 @@ -93,7 +93,7 @@ typedef struct HWVoiceIn { size_t total_samples_captured; uint64_t ts_helper; =20 - STSampleBuffer *conv_buf; + STSampleBuffer conv_buf; void *buf_emul; size_t pos_emul, pending_emul, size_emul; =20 diff --git a/audio/audio_template.h b/audio/audio_template.h index 980e1f4bd0..dd87170cbd 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -71,8 +71,9 @@ static void glue(audio_init_nb_voices_, TYPE)(AudioState = *s, static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw) { g_free(hw->buf_emul); - g_free (HWBUF); - HWBUF =3D NULL; + g_free(HWBUF.buffer); + HWBUF.buffer =3D NULL; + HWBUF.size =3D 0; } =20 static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw) @@ -83,10 +84,12 @@ static void glue(audio_pcm_hw_alloc_resources_, TYPE)(H= W *hw) dolog("Attempted to allocate empty buffer\n"); } =20 - HWBUF =3D g_malloc0(sizeof(STSampleBuffer) + sizeof(st_sample) * s= amples); - HWBUF->size =3D samples; + HWBUF.buffer =3D g_new0(st_sample, samples); + HWBUF.size =3D samples; + HWBUF.pos =3D 0; } else { - HWBUF =3D NULL; + HWBUF.buffer =3D NULL; + HWBUF.size =3D 0; } } =20 @@ -111,9 +114,9 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (= SW *sw) } =20 #ifdef DAC - samples =3D ((int64_t) sw->HWBUF->size << 32) / sw->ratio; + samples =3D ((int64_t)sw->HWBUF.size << 32) / sw->ratio; #else - samples =3D (int64_t)sw->HWBUF->size * sw->ratio >> 32; + samples =3D (int64_t)sw->HWBUF.size * sw->ratio >> 32; #endif if (audio_bug(__func__, samples < 0)) { dolog("Can not allocate buffer for `%s' (%d samples)\n", @@ -126,7 +129,7 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (= SW *sw) size_t f_fe_min; =20 /* f_fe_min =3D ceil(1 [frames] * f_be [Hz] / size_be [frames]) */ - f_fe_min =3D (hw->info.freq + HWBUF->size - 1) / HWBUF->size; + f_fe_min =3D (hw->info.freq + HWBUF.size - 1) / HWBUF.size; qemu_log_mask(LOG_UNIMP, AUDIO_CAP ": The guest selected a " NAME " sample ra= te" " of %d Hz for %s. Only sample rates >=3D %zu Hz are" --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709597364633.1035286737466; Mon, 6 Feb 2023 10:53:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bk-0004x5-Ui; Mon, 06 Feb 2023 13:52:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bf-0004vl-3L for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:50 -0500 Received: from mailout09.t-online.de ([194.25.134.84]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bd-0005SY-7J for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:46 -0500 Received: from fwd76.dcpf.telekom.de (fwd76.aul.t-online.de [10.223.144.102]) by mailout09.t-online.de (Postfix) with SMTP id B4AD14A55; Mon, 6 Feb 2023 19:52:40 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd76.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bY-2RxiiH0; Mon, 6 Feb 2023 19:52:40 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 746D3200623; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 02/17] audio: change type and name of the resample buffer Date: Mon, 6 Feb 2023 19:52:22 +0100 Message-Id: <20230206185237.8358-2-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709560-E4D742C2-03257CA1/0/0 CLEAN NORMAL X-TOI-MSGID: 41bc744d-f515-417e-aa77-98919390c0f2 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.84; envelope-from=volker.ruemelin@t-online.de; helo=mailout09.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709598238100001 Change the type of the resample buffer from struct st_sample * to STSampleBuffer. Also change the name from buf to resample_buf for better readability. The new variables resample_buf.size and resample_buf.pos will be used after the next patches. There is no functional change. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 15 ++++++++------- audio/audio_int.h | 4 ++-- audio/audio_template.h | 10 ++++++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index a0b54e4a2e..a399147486 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -555,7 +555,7 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *bu= f, size_t size) { HWVoiceIn *hw =3D sw->hw; size_t samples, live, ret =3D 0, swlim, isamp, osamp, rpos, total =3D = 0; - struct st_sample *src, *dst =3D sw->buf; + struct st_sample *src, *dst =3D sw->resample_buf.buffer; =20 live =3D hw->total_samples_captured - sw->total_hw_samples_acquired; if (!live) { @@ -595,10 +595,10 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *= buf, size_t size) } =20 if (!hw->pcm_ops->volume_in) { - mixeng_volume (sw->buf, ret, &sw->vol); + mixeng_volume(sw->resample_buf.buffer, ret, &sw->vol); } =20 - sw->clip (buf, sw->buf, ret); + sw->clip(buf, sw->resample_buf.buffer, ret); sw->total_hw_samples_acquired +=3D total; return ret * sw->info.bytes_per_frame; } @@ -706,10 +706,10 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void= *buf, size_t size) samples =3D ((int64_t)MIN(dead, hw_free) << 32) / sw->ratio; samples =3D MIN(samples, size / sw->info.bytes_per_frame); if (samples) { - sw->conv(sw->buf, buf, samples); + sw->conv(sw->resample_buf.buffer, buf, samples); =20 if (!sw->hw->pcm_ops->volume_out) { - mixeng_volume(sw->buf, samples, &sw->vol); + mixeng_volume(sw->resample_buf.buffer, samples, &sw->vol); } } =20 @@ -724,7 +724,7 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *= buf, size_t size) osamp =3D blck; st_rate_flow_mix ( sw->rate, - sw->buf + pos, + sw->resample_buf.buffer + pos, sw->hw->mix_buf.buffer + wpos, &isamp, &osamp @@ -1061,7 +1061,8 @@ static void audio_capture_mix_and_clear(HWVoiceOut *h= w, size_t rpos, size_t bytes =3D to_write * hw->info.bytes_per_frame; size_t written; =20 - sw->buf =3D hw->mix_buf.buffer + rpos2; + sw->resample_buf.buffer =3D hw->mix_buf.buffer + rpos2; + sw->resample_buf.size =3D to_write; written =3D audio_pcm_sw_write (sw, NULL, bytes); if (written - bytes) { dolog("Could not mix %zu bytes into a capture " diff --git a/audio/audio_int.h b/audio/audio_int.h index 061845dcc2..8b163e1759 100644 --- a/audio/audio_int.h +++ b/audio/audio_int.h @@ -109,7 +109,7 @@ struct SWVoiceOut { struct audio_pcm_info info; t_sample *conv; int64_t ratio; - struct st_sample *buf; + STSampleBuffer resample_buf; void *rate; size_t total_hw_samples_mixed; int active; @@ -129,7 +129,7 @@ struct SWVoiceIn { int64_t ratio; void *rate; size_t total_hw_samples_acquired; - struct st_sample *buf; + STSampleBuffer resample_buf; f_sample *clip; HWVoiceIn *hw; char *name; diff --git a/audio/audio_template.h b/audio/audio_template.h index dd87170cbd..a0b653f52c 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -95,13 +95,13 @@ static void glue(audio_pcm_hw_alloc_resources_, TYPE)(H= W *hw) =20 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw) { - g_free (sw->buf); + g_free(sw->resample_buf.buffer); + sw->resample_buf.buffer =3D NULL; + sw->resample_buf.size =3D 0; =20 if (sw->rate) { st_rate_stop (sw->rate); } - - sw->buf =3D NULL; sw->rate =3D NULL; } =20 @@ -138,7 +138,9 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (= SW *sw) return -1; } =20 - sw->buf =3D g_new0(st_sample, samples); + sw->resample_buf.buffer =3D g_new0(st_sample, samples); + sw->resample_buf.size =3D samples; + sw->resample_buf.pos =3D 0; =20 #ifdef DAC sw->rate =3D st_rate_start (sw->info.freq, sw->hw->info.freq); --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16757098292471011.8625600017084; Mon, 6 Feb 2023 10:57:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bm-0004y4-QX; Mon, 06 Feb 2023 13:52:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bk-0004wX-4h for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:52 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bi-0005Tf-EP for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:51 -0500 Received: from fwd80.dcpf.telekom.de (fwd80.aul.t-online.de [10.223.144.106]) by mailout08.t-online.de (Postfix) with SMTP id E53A98080; Mon, 6 Feb 2023 19:52:42 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd80.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6ba-2AXabZ0; Mon, 6 Feb 2023 19:52:42 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 76B7920062A; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 03/17] audio: make the resampling code greedy Date: Mon, 6 Feb 2023 19:52:23 +0100 Message-Id: <20230206185237.8358-3-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709562-B8817A74-FEE2E169/0/0 CLEAN NORMAL X-TOI-MSGID: f9a5fea7-8344-49ca-9af9-97cdd5a43e4b Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709830082100005 Read the maximum possible number of audio frames instead of the minimum necessary number of frames when the audio stream is downsampled and the output buffer is limited. This makes the function symmetrical to upsampling when the input buffer is limited. The maximum possible number of frames is written here. With this change it's easier to calculate the exact number of audio frames the resample function will read or write. These two functions will be introduced later. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Acked-by: Marc-Andr=C3=A9 Lureau --- audio/rate_template.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/audio/rate_template.h b/audio/rate_template.h index b432719ebb..6648f0d2e5 100644 --- a/audio/rate_template.h +++ b/audio/rate_template.h @@ -40,8 +40,6 @@ void NAME (void *opaque, struct st_sample *ibuf, struct s= t_sample *obuf, int64_t t; #endif =20 - ilast =3D rate->ilast; - istart =3D ibuf; iend =3D ibuf + *isamp; =20 @@ -59,15 +57,17 @@ void NAME (void *opaque, struct st_sample *ibuf, struct= st_sample *obuf, return; } =20 - while (obuf < oend) { + /* without input samples, there's nothing to do */ + if (ibuf >=3D iend) { + *osamp =3D 0; + return; + } =20 - /* Safety catch to make sure we have input samples. */ - if (ibuf >=3D iend) { - break; - } + ilast =3D rate->ilast; =20 - /* read as many input samples so that ipos > opos */ + while (true) { =20 + /* read as many input samples so that ipos > opos */ while (rate->ipos <=3D (rate->opos >> 32)) { ilast =3D *ibuf++; rate->ipos++; @@ -78,6 +78,11 @@ void NAME (void *opaque, struct st_sample *ibuf, struct = st_sample *obuf, } } =20 + /* make sure that the next output sample can be written */ + if (obuf >=3D oend) { + break; + } + icur =3D *ibuf; =20 /* wrap ipos and opos around long before they overflow */ --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709758621168.12629886728962; Mon, 6 Feb 2023 10:55:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6c8-0005E9-8I; Mon, 06 Feb 2023 13:53:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c2-00053U-IG for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:10 -0500 Received: from mailout02.t-online.de ([194.25.134.17]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c0-0005W0-Pu for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:10 -0500 Received: from fwd88.dcpf.telekom.de (fwd88.aul.t-online.de [10.223.144.114]) by mailout02.t-online.de (Postfix) with SMTP id C3DA425E0C; Mon, 6 Feb 2023 19:52:46 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd88.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bd-4LvybJ0; Mon, 6 Feb 2023 19:52:45 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 7959A20062C; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 04/17] audio: replace the resampling loop in audio_pcm_sw_write() Date: Mon, 6 Feb 2023 19:52:24 +0100 Message-Id: <20230206185237.8358-4-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709565-70EA07D8-9CF8A3F1/0/0 CLEAN NORMAL X-TOI-MSGID: f3601ead-7c50-4406-9ba7-a522ccdb7adc Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.17; envelope-from=volker.ruemelin@t-online.de; helo=mailout02.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709759178100001 Replace the resampling loop in audio_pcm_sw_write() with the new function audio_pcm_sw_resample_out(). Unlike the old resample loop the new function will try to consume input frames even if the output buffer is full. This is necessary when downsampling to avoid reading less audio frames than calculated in advance. The loop was unrolled to avoid complicated loop control conditions in this case. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Acked-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 63 +++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index a399147486..4412b5fad8 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -673,11 +673,44 @@ static void audio_pcm_hw_clip_out(HWVoiceOut *hw, voi= d *pcm_buf, size_t len) /* * Soft voice (playback) */ +static void audio_pcm_sw_resample_out(SWVoiceOut *sw, + size_t frames_in_max, size_t frames_out_max, + size_t *total_in, size_t *total_out) +{ + HWVoiceOut *hw =3D sw->hw; + struct st_sample *src, *dst; + size_t live, wpos, frames_in, frames_out; + + live =3D sw->total_hw_samples_mixed; + wpos =3D (hw->mix_buf.pos + live) % hw->mix_buf.size; + + /* write to mix_buf from wpos to end of buffer */ + src =3D sw->resample_buf.buffer; + frames_in =3D frames_in_max; + dst =3D hw->mix_buf.buffer + wpos; + frames_out =3D MIN(frames_out_max, hw->mix_buf.size - wpos); + st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out); + wpos +=3D frames_out; + *total_in =3D frames_in; + *total_out =3D frames_out; + + /* write to mix_buf from start of buffer if there are input frames lef= t */ + if (frames_in_max - frames_in > 0 && wpos =3D=3D hw->mix_buf.size) { + src +=3D frames_in; + frames_in =3D frames_in_max - frames_in; + dst =3D hw->mix_buf.buffer; + frames_out =3D frames_out_max - frames_out; + st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out); + *total_in +=3D frames_in; + *total_out +=3D frames_out; + } +} + static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size) { - size_t hwsamples, samples, isamp, osamp, wpos, live, dead, left, blck; + size_t hwsamples, samples, live, dead; size_t hw_free; - size_t ret =3D 0, pos =3D 0, total =3D 0; + size_t ret, total; =20 if (!sw) { return size; @@ -698,8 +731,6 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *= buf, size_t size) return 0; } =20 - wpos =3D (sw->hw->mix_buf.pos + live) % hwsamples; - dead =3D hwsamples - live; hw_free =3D audio_pcm_hw_get_free(sw->hw); hw_free =3D hw_free > live ? hw_free - live : 0; @@ -713,29 +744,7 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void = *buf, size_t size) } } =20 - while (samples) { - dead =3D hwsamples - live; - left =3D hwsamples - wpos; - blck =3D MIN (dead, left); - if (!blck) { - break; - } - isamp =3D samples; - osamp =3D blck; - st_rate_flow_mix ( - sw->rate, - sw->resample_buf.buffer + pos, - sw->hw->mix_buf.buffer + wpos, - &isamp, - &osamp - ); - ret +=3D isamp; - samples -=3D isamp; - pos +=3D isamp; - live +=3D osamp; - wpos =3D (wpos + osamp) % hwsamples; - total +=3D osamp; - } + audio_pcm_sw_resample_out(sw, samples, MIN(dead, hw_free), &ret, &tota= l); =20 sw->total_hw_samples_mixed +=3D total; sw->empty =3D sw->total_hw_samples_mixed =3D=3D 0; --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709650008389.4482335735777; Mon, 6 Feb 2023 10:54:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bn-0004yZ-DB; Mon, 06 Feb 2023 13:52:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bl-0004xf-Rb for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:53 -0500 Received: from mailout07.t-online.de ([194.25.134.83]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bj-0005Tp-T4 for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:53 -0500 Received: from fwd79.dcpf.telekom.de (fwd79.aul.t-online.de [10.223.144.105]) by mailout07.t-online.de (Postfix) with SMTP id CC134C402; Mon, 6 Feb 2023 19:52:47 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd79.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bf-11EhJx0; Mon, 6 Feb 2023 19:52:47 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 7C16A200635; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 05/17] audio: remove sw == NULL check Date: Mon, 6 Feb 2023 19:52:25 +0100 Message-Id: <20230206185237.8358-5-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709567-D53B29DF-A63D0D90/0/0 CLEAN NORMAL X-TOI-MSGID: 11063a39-87cb-417f-bd0f-3aca6bac4306 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.83; envelope-from=volker.ruemelin@t-online.de; helo=mailout07.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709650468100001 All call sites of audio_pcm_sw_write() guarantee that sw is not NULL. Remove the unnecessary NULL check. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 4412b5fad8..8f1c0e77b0 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -712,10 +712,6 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void = *buf, size_t size) size_t hw_free; size_t ret, total; =20 - if (!sw) { - return size; - } - hwsamples =3D sw->hw->mix_buf.size; =20 live =3D sw->total_hw_samples_mixed; --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709711815170.13903318673567; Mon, 6 Feb 2023 10:55:11 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bo-0004yi-9e; Mon, 06 Feb 2023 13:52:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bl-0004xX-FI for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:53 -0500 Received: from mailout10.t-online.de ([194.25.134.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bj-0005Ts-HR for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:53 -0500 Received: from fwd86.dcpf.telekom.de (fwd86.aul.t-online.de [10.223.144.112]) by mailout10.t-online.de (Postfix) with SMTP id 0F2EE4F80; Mon, 6 Feb 2023 19:52:50 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd86.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bh-0vP60X0; Mon, 6 Feb 2023 19:52:49 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 7EC24200638; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 06/17] audio: rename variables in audio_pcm_sw_write() Date: Mon, 6 Feb 2023 19:52:26 +0100 Message-Id: <20230206185237.8358-6-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709569-92FEA95E-79178949/0/0 CLEAN NORMAL X-TOI-MSGID: 0fa32990-6943-464e-91db-6154b887a500 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.21; envelope-from=volker.ruemelin@t-online.de; helo=mailout10.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709712802100003 The audio_pcm_sw_write() function uses a lot of very unspecific variable names. Rename them for better readability. ret =3D> total_in total =3D> total_out size =3D> buf_len hwsamples =3D> hw->mix_buf.size samples =3D> frames_in_max Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 8f1c0e77b0..cd10f1ec10 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -706,56 +706,55 @@ static void audio_pcm_sw_resample_out(SWVoiceOut *sw, } } =20 -static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size) +static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len) { - size_t hwsamples, samples, live, dead; - size_t hw_free; - size_t ret, total; - - hwsamples =3D sw->hw->mix_buf.size; + HWVoiceOut *hw =3D sw->hw; + size_t live, dead, hw_free; + size_t frames_in_max, total_in, total_out; =20 live =3D sw->total_hw_samples_mixed; - if (audio_bug(__func__, live > hwsamples)) { - dolog("live=3D%zu hw->mix_buf.size=3D%zu\n", live, hwsamples); + if (audio_bug(__func__, live > hw->mix_buf.size)) { + dolog("live=3D%zu hw->mix_buf.size=3D%zu\n", live, hw->mix_buf.siz= e); return 0; } =20 - if (live =3D=3D hwsamples) { + if (live =3D=3D hw->mix_buf.size) { #ifdef DEBUG_OUT dolog ("%s is full %zu\n", sw->name, live); #endif return 0; } =20 - dead =3D hwsamples - live; - hw_free =3D audio_pcm_hw_get_free(sw->hw); + dead =3D hw->mix_buf.size - live; + hw_free =3D audio_pcm_hw_get_free(hw); hw_free =3D hw_free > live ? hw_free - live : 0; - samples =3D ((int64_t)MIN(dead, hw_free) << 32) / sw->ratio; - samples =3D MIN(samples, size / sw->info.bytes_per_frame); - if (samples) { - sw->conv(sw->resample_buf.buffer, buf, samples); + frames_in_max =3D ((int64_t)MIN(dead, hw_free) << 32) / sw->ratio; + frames_in_max =3D MIN(frames_in_max, buf_len / sw->info.bytes_per_fram= e); + if (frames_in_max) { + sw->conv(sw->resample_buf.buffer, buf, frames_in_max); =20 if (!sw->hw->pcm_ops->volume_out) { - mixeng_volume(sw->resample_buf.buffer, samples, &sw->vol); + mixeng_volume(sw->resample_buf.buffer, frames_in_max, &sw->vol= ); } } =20 - audio_pcm_sw_resample_out(sw, samples, MIN(dead, hw_free), &ret, &tota= l); + audio_pcm_sw_resample_out(sw, frames_in_max, MIN(dead, hw_free), + &total_in, &total_out); =20 - sw->total_hw_samples_mixed +=3D total; + sw->total_hw_samples_mixed +=3D total_out; sw->empty =3D sw->total_hw_samples_mixed =3D=3D 0; =20 #ifdef DEBUG_OUT dolog ( - "%s: write size %zu ret %zu total sw %zu\n", - SW_NAME (sw), - size / sw->info.bytes_per_frame, - ret, + "%s: write size %zu written %zu total mixed %zu\n", + SW_NAME(sw), + buf_len / sw->info.bytes_per_frame, + total_in, sw->total_hw_samples_mixed ); #endif =20 - return ret * sw->info.bytes_per_frame; + return total_in * sw->info.bytes_per_frame; } =20 #ifdef DEBUG_AUDIO --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709844899232.00654801633937; Mon, 6 Feb 2023 10:57:24 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bq-0004zi-5F; Mon, 06 Feb 2023 13:52:58 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bp-0004zG-28 for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:57 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bn-0005Ul-Fm for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:56 -0500 Received: from fwd88.dcpf.telekom.de (fwd88.aul.t-online.de [10.223.144.114]) by mailout08.t-online.de (Postfix) with SMTP id 9E7552B513; Mon, 6 Feb 2023 19:52:53 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd88.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bk-0h9Ior0; Mon, 6 Feb 2023 19:52:52 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 8166F20063B; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 07/17] audio: don't misuse audio_pcm_sw_write() Date: Mon, 6 Feb 2023 19:52:27 +0100 Message-Id: <20230206185237.8358-7-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709572-70EA07D8-7CE700D8/0/0 CLEAN NORMAL X-TOI-MSGID: d2ef10c9-2008-4a32-9861-5314ed268ebc Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709848179100009 The audio_pcm_sw_write() function is intended to convert a PCM audio stream to the internal representation, adjust the volume, and then mix it with the other audio streams with a possibly changed sample rate in mix_buf. In order for the audio_capture_mix_and_clear() function to use audio_pcm_sw_write(), it must bypass the first two tasks of audio_pcm_sw_write(). Since patch "audio: split out the resampling loop in audio_pcm_sw_write()" this is no longer necessary, because now the audio_pcm_sw_resample_out() function can be used instead of audio_pcm_sw_write(). Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Acked-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index cd10f1ec10..44eb7b63b4 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -1056,26 +1056,33 @@ static void audio_capture_mix_and_clear(HWVoiceOut = *hw, size_t rpos, =20 for (sc =3D hw->cap_head.lh_first; sc; sc =3D sc->entries.le_next)= { SWVoiceOut *sw =3D &sc->sw; - int rpos2 =3D rpos; + size_t rpos2 =3D rpos; =20 n =3D samples; while (n) { size_t till_end_of_hw =3D hw->mix_buf.size - rpos2; - size_t to_write =3D MIN(till_end_of_hw, n); - size_t bytes =3D to_write * hw->info.bytes_per_frame; - size_t written; + size_t to_read =3D MIN(till_end_of_hw, n); + size_t live, frames_in, frames_out; =20 sw->resample_buf.buffer =3D hw->mix_buf.buffer + rpos2; - sw->resample_buf.size =3D to_write; - written =3D audio_pcm_sw_write (sw, NULL, bytes); - if (written - bytes) { - dolog("Could not mix %zu bytes into a capture " + sw->resample_buf.size =3D to_read; + live =3D sw->total_hw_samples_mixed; + + audio_pcm_sw_resample_out(sw, + to_read, sw->hw->mix_buf.size - = live, + &frames_in, &frames_out); + + sw->total_hw_samples_mixed +=3D frames_out; + sw->empty =3D sw->total_hw_samples_mixed =3D=3D 0; + + if (to_read - frames_in) { + dolog("Could not mix %zu frames into a capture " "buffer, mixed %zu\n", - bytes, written); + to_read, frames_in); break; } - n -=3D to_write; - rpos2 =3D (rpos2 + to_write) % hw->mix_buf.size; + n -=3D to_read; + rpos2 =3D (rpos2 + to_read) % hw->mix_buf.size; } } } --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709777753277.1179845460423; Mon, 6 Feb 2023 10:56:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6br-0004zt-UL; Mon, 06 Feb 2023 13:52:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bp-0004za-Hp for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:57 -0500 Received: from mailout11.t-online.de ([194.25.134.85]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bo-0005Up-1M for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:52:57 -0500 Received: from fwd89.dcpf.telekom.de (fwd89.aul.t-online.de [10.223.144.115]) by mailout11.t-online.de (Postfix) with SMTP id A4B0314A18; Mon, 6 Feb 2023 19:52:54 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd89.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bm-396CO10; Mon, 6 Feb 2023 19:52:54 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 842FE20063C; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 08/17] audio: remove unused noop_conv() function Date: Mon, 6 Feb 2023 19:52:28 +0100 Message-Id: <20230206185237.8358-8-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709574-D671D046-DAC71AB9/0/0 CLEAN NORMAL X-TOI-MSGID: cb995642-b983-42f5-a80a-626e679a12e5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.85; envelope-from=volker.ruemelin@t-online.de; helo=mailout11.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709779592100007 The function audio_capture_mix_and_clear() no longer uses audio_pcm_sw_write() to resample audio frames from one internal buffer to another. For this reason, the noop_conv() function is now unused. Remove it. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 44eb7b63b4..556696b095 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -381,13 +381,6 @@ void audio_pcm_info_clear_buf (struct audio_pcm_info *= info, void *buf, int len) /* * Capture */ -static void noop_conv (struct st_sample *dst, const void *src, int samples) -{ - (void) src; - (void) dst; - (void) samples; -} - static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s, struct audsettings= *as) { @@ -485,7 +478,6 @@ static int audio_attach_capture (HWVoiceOut *hw) sw->info =3D hw->info; sw->empty =3D 1; sw->active =3D hw->enabled; - sw->conv =3D noop_conv; sw->ratio =3D ((int64_t) hw_cap->info.freq << 32) / sw->info.freq; sw->vol =3D nominal_volume; sw->rate =3D st_rate_start (sw->info.freq, hw_cap->info.freq); --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709680506326.59602232469035; Mon, 6 Feb 2023 10:54:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bw-00050v-2Y; Mon, 06 Feb 2023 13:53:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bu-00050f-IH for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:02 -0500 Received: from mailout03.t-online.de ([194.25.134.81]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bs-0005VE-NS for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:02 -0500 Received: from fwd71.dcpf.telekom.de (fwd71.aul.t-online.de [10.223.144.97]) by mailout03.t-online.de (Postfix) with SMTP id 7B78218B86; Mon, 6 Feb 2023 19:52:57 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd71.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bo-0DMnPF0; Mon, 6 Feb 2023 19:52:57 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 86DE820063D; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 09/17] audio/mixeng: calculate number of input frames Date: Mon, 6 Feb 2023 19:52:29 +0100 Message-Id: <20230206185237.8358-9-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709577-437ECC4D-ECB023D0/0/0 CLEAN NORMAL X-TOI-MSGID: 2dd0f31e-9e34-4b4c-96fd-3037ff24e60d Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.81; envelope-from=volker.ruemelin@t-online.de; helo=mailout03.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709682736100003 Calculate the exact number of audio input frames needed to get a given number of audio output frames. The exact number of frames depends only on the difference of opos - ipos and the number of output frames. When downsampling, this function returns the maximum number of input frames needed. This function will later replace the audio_frontend_frames_out() function, which calculates the average number of input frames rounded down to the nearest integer. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Acked-by: Marc-Andr=C3=A9 Lureau --- audio/mixeng.c | 36 ++++++++++++++++++++++++++++++++++++ audio/mixeng.h | 1 + 2 files changed, 37 insertions(+) diff --git a/audio/mixeng.c b/audio/mixeng.c index fe454e0725..6bb3d54f77 100644 --- a/audio/mixeng.c +++ b/audio/mixeng.c @@ -440,6 +440,42 @@ void st_rate_stop (void *opaque) g_free (opaque); } =20 +/** + * st_rate_frames_in() - returns the number of frames needed to + * get frames_out frames after resampling + * + * @opaque: pointer to struct rate + * @frames_out: number of frames + */ +uint32_t st_rate_frames_in(void *opaque, uint32_t frames_out) +{ + struct rate *rate =3D opaque; + uint64_t opos_start, opos_end; + uint32_t ipos_start, ipos_end; + + if (rate->opos_inc =3D=3D 1ULL << 32) { + return frames_out; + } + + if (frames_out) { + opos_start =3D rate->opos; + ipos_start =3D rate->ipos; + } else { + uint64_t offset; + + /* add offset =3D ceil(opos_inc) to opos and ipos to avoid an unde= rflow */ + offset =3D (rate->opos_inc + (1ULL << 32) - 1) & ~((1ULL << 32) - = 1); + opos_start =3D rate->opos + offset; + ipos_start =3D rate->ipos + (offset >> 32); + } + /* last frame written was at opos_start - rate->opos_inc */ + opos_end =3D opos_start - rate->opos_inc + rate->opos_inc * frames_out; + ipos_end =3D (opos_end >> 32) + 1; + + /* last frame read was at ipos_start - 1 */ + return ipos_end + 1 > ipos_start ? ipos_end + 1 - ipos_start : 0; +} + void mixeng_clear (struct st_sample *buf, int len) { memset (buf, 0, len * sizeof (struct st_sample)); diff --git a/audio/mixeng.h b/audio/mixeng.h index 2dcd6df245..64c1e231cc 100644 --- a/audio/mixeng.h +++ b/audio/mixeng.h @@ -52,6 +52,7 @@ void st_rate_flow(void *opaque, st_sample *ibuf, st_sampl= e *obuf, void st_rate_flow_mix(void *opaque, st_sample *ibuf, st_sample *obuf, size_t *isamp, size_t *osamp); void st_rate_stop (void *opaque); +uint32_t st_rate_frames_in(void *opaque, uint32_t frames_out); void mixeng_clear (struct st_sample *buf, int len); void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *= vol); =20 --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709859184985.2190382217835; Mon, 6 Feb 2023 10:57:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6bx-00051K-Mx; Mon, 06 Feb 2023 13:53:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bw-00050w-3I for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:04 -0500 Received: from mailout04.t-online.de ([194.25.134.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bu-0005VQ-Bn for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:03 -0500 Received: from fwd89.dcpf.telekom.de (fwd89.aul.t-online.de [10.223.144.115]) by mailout04.t-online.de (Postfix) with SMTP id AD4CF520B; Mon, 6 Feb 2023 19:52:59 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd89.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6br-3nW74z0; Mon, 6 Feb 2023 19:52:59 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 899E22006C1; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 10/17] audio: wire up st_rate_frames_in() Date: Mon, 6 Feb 2023 19:52:30 +0100 Message-Id: <20230206185237.8358-10-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709579-D571F046-7875955D/0/0 CLEAN NORMAL X-TOI-MSGID: c0a60082-f42e-429f-a42b-4e3a7b45a1b9 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.18; envelope-from=volker.ruemelin@t-online.de; helo=mailout04.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709860341100001 Wire up the st_rate_frames_in() function and replace audio_frontend_frames_out() to make audio packet length calculation exact. When upsampling, it's still possible that the audio frontends can't write the last audio frame. This will be fixed later. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin --- audio/audio.c | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 556696b095..e18b5e98c5 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -701,8 +701,8 @@ static void audio_pcm_sw_resample_out(SWVoiceOut *sw, static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len) { HWVoiceOut *hw =3D sw->hw; - size_t live, dead, hw_free; - size_t frames_in_max, total_in, total_out; + size_t live, dead, hw_free, sw_max, fe_max; + size_t frames_in_max, frames_out_max, total_in, total_out; =20 live =3D sw->total_hw_samples_mixed; if (audio_bug(__func__, live > hw->mix_buf.size)) { @@ -720,17 +720,21 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void= *buf, size_t buf_len) dead =3D hw->mix_buf.size - live; hw_free =3D audio_pcm_hw_get_free(hw); hw_free =3D hw_free > live ? hw_free - live : 0; - frames_in_max =3D ((int64_t)MIN(dead, hw_free) << 32) / sw->ratio; - frames_in_max =3D MIN(frames_in_max, buf_len / sw->info.bytes_per_fram= e); - if (frames_in_max) { - sw->conv(sw->resample_buf.buffer, buf, frames_in_max); + frames_out_max =3D MIN(dead, hw_free); + sw_max =3D st_rate_frames_in(sw->rate, frames_out_max); + fe_max =3D MIN(buf_len / sw->info.bytes_per_frame, sw->resample_buf.si= ze); + frames_in_max =3D MIN(sw_max, fe_max); =20 - if (!sw->hw->pcm_ops->volume_out) { - mixeng_volume(sw->resample_buf.buffer, frames_in_max, &sw->vol= ); - } + if (!frames_in_max) { + return 0; } =20 - audio_pcm_sw_resample_out(sw, frames_in_max, MIN(dead, hw_free), + sw->conv(sw->resample_buf.buffer, buf, frames_in_max); + if (!sw->hw->pcm_ops->volume_out) { + mixeng_volume(sw->resample_buf.buffer, frames_in_max, &sw->vol); + } + + audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max, &total_in, &total_out); =20 sw->total_hw_samples_mixed +=3D total_out; @@ -1000,18 +1004,6 @@ static size_t audio_get_avail (SWVoiceIn *sw) return live; } =20 -/** - * audio_frontend_frames_out() - returns the number of frames needed to - * get frames_out frames after resampling - * - * @sw: audio playback frontend - * @frames_out: number of frames - */ -static size_t audio_frontend_frames_out(SWVoiceOut *sw, size_t frames_out) -{ - return ((int64_t)frames_out << 32) / sw->ratio; -} - static size_t audio_get_free(SWVoiceOut *sw) { size_t live, dead; @@ -1031,8 +1023,8 @@ static size_t audio_get_free(SWVoiceOut *sw) dead =3D sw->hw->mix_buf.size - live; =20 #ifdef DEBUG_OUT - dolog("%s: get_free live %zu dead %zu frontend frames %zu\n", - SW_NAME(sw), live, dead, audio_frontend_frames_out(sw, dead)); + dolog("%s: get_free live %zu dead %zu frontend frames %u\n", + SW_NAME(sw), live, dead, st_rate_frames_in(sw->rate, dead)); #endif =20 return dead; @@ -1161,12 +1153,13 @@ static void audio_run_out (AudioState *s) size_t free; =20 if (hw_free > sw->total_hw_samples_mixed) { - free =3D audio_frontend_frames_out(sw, + free =3D st_rate_frames_in(sw->rate, MIN(sw_free, hw_free - sw->total_hw_samples_mixed)= ); } else { free =3D 0; } if (free > 0) { + free =3D MIN(free, sw->resample_buf.size); sw->callback.fn(sw->callback.opaque, free * sw->info.bytes_per_frame); } --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709620677840.9993165525724; Mon, 6 Feb 2023 10:53:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6c1-000521-JI; Mon, 06 Feb 2023 13:53:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bz-00051l-QR for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:08 -0500 Received: from mailout01.t-online.de ([194.25.134.80]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bx-0005Ve-CG for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:07 -0500 Received: from fwd72.dcpf.telekom.de (fwd72.aul.t-online.de [10.223.144.98]) by mailout01.t-online.de (Postfix) with SMTP id 1A1E5623; Mon, 6 Feb 2023 19:53:03 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd72.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bt-1AtKnB0; Mon, 6 Feb 2023 19:53:01 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 8C6962006C4; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 11/17] audio: replace the resampling loop in audio_pcm_sw_read() Date: Mon, 6 Feb 2023 19:52:31 +0100 Message-Id: <20230206185237.8358-11-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709581-81AEBD99-FAFFAF20/0/0 CLEAN NORMAL X-TOI-MSGID: 720c81c0-cb33-4ff7-aee6-58fb6544d500 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.80; envelope-from=volker.ruemelin@t-online.de; helo=mailout01.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709622265100001 Replace the resampling loop in audio_pcm_sw_read() with the new function audio_pcm_sw_resample_in(). Unlike the old resample loop the new function will try to consume input frames even if the output buffer is full. This is necessary when downsampling to avoid reading less audio frames than calculated in advance. The loop was unrolled to avoid complicated loop control conditions in this case. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Acked-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 59 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index e18b5e98c5..9e9c03a42e 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -543,11 +543,43 @@ static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, voi= d *pcm_buf, size_t samples) /* * Soft voice (capture) */ +static void audio_pcm_sw_resample_in(SWVoiceIn *sw, + size_t frames_in_max, size_t frames_out_max, + size_t *total_in, size_t *total_out) +{ + HWVoiceIn *hw =3D sw->hw; + struct st_sample *src, *dst; + size_t live, rpos, frames_in, frames_out; + + live =3D hw->total_samples_captured - sw->total_hw_samples_acquired; + rpos =3D audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size); + + /* resample conv_buf from rpos to end of buffer */ + src =3D hw->conv_buf.buffer + rpos; + frames_in =3D MIN(frames_in_max, hw->conv_buf.size - rpos); + dst =3D sw->resample_buf.buffer; + frames_out =3D frames_out_max; + st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out); + rpos +=3D frames_in; + *total_in =3D frames_in; + *total_out =3D frames_out; + + /* resample conv_buf from start of buffer if there are input frames le= ft */ + if (frames_in_max - frames_in && rpos =3D=3D hw->conv_buf.size) { + src =3D hw->conv_buf.buffer; + frames_in =3D frames_in_max - frames_in; + dst +=3D frames_out; + frames_out =3D frames_out_max - frames_out; + st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out); + *total_in +=3D frames_in; + *total_out +=3D frames_out; + } +} + static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size) { HWVoiceIn *hw =3D sw->hw; - size_t samples, live, ret =3D 0, swlim, isamp, osamp, rpos, total =3D = 0; - struct st_sample *src, *dst =3D sw->resample_buf.buffer; + size_t samples, live, ret, swlim, total; =20 live =3D hw->total_samples_captured - sw->total_hw_samples_acquired; if (!live) { @@ -558,33 +590,12 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *= buf, size_t size) return 0; } =20 - rpos =3D audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size); - samples =3D size / sw->info.bytes_per_frame; =20 swlim =3D (live * sw->ratio) >> 32; swlim =3D MIN (swlim, samples); =20 - while (swlim) { - src =3D hw->conv_buf.buffer + rpos; - if (hw->conv_buf.pos > rpos) { - isamp =3D hw->conv_buf.pos - rpos; - } else { - isamp =3D hw->conv_buf.size - rpos; - } - - if (!isamp) { - break; - } - osamp =3D swlim; - - st_rate_flow (sw->rate, src, dst, &isamp, &osamp); - swlim -=3D osamp; - rpos =3D (rpos + isamp) % hw->conv_buf.size; - dst +=3D osamp; - ret +=3D osamp; - total +=3D isamp; - } + audio_pcm_sw_resample_in(sw, live, swlim, &total, &ret); =20 if (!hw->pcm_ops->volume_in) { mixeng_volume(sw->resample_buf.buffer, ret, &sw->vol); --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709816547333.71160048776255; Mon, 6 Feb 2023 10:56:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6cB-0005U7-6S; Mon, 06 Feb 2023 13:53:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c0-00051u-Ve for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:09 -0500 Received: from mailout11.t-online.de ([194.25.134.85]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6bz-0005Vx-BI for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:08 -0500 Received: from fwd84.dcpf.telekom.de (fwd84.aul.t-online.de [10.223.144.110]) by mailout11.t-online.de (Postfix) with SMTP id 26EC714A1E; Mon, 6 Feb 2023 19:53:06 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd84.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6bw-0wOy0n0; Mon, 6 Feb 2023 19:53:04 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 8F0152006C5; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 12/17] audio: rename variables in audio_pcm_sw_read() Date: Mon, 6 Feb 2023 19:52:32 +0100 Message-Id: <20230206185237.8358-12-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709584-511B9A0B-D267D465/0/0 CLEAN NORMAL X-TOI-MSGID: b7a288fc-e0d0-4398-9589-b873797edc75 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.85; envelope-from=volker.ruemelin@t-online.de; helo=mailout11.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709820167100001 The audio_pcm_sw_read() function uses a few very unspecific variable names. Rename them for better readability. ret =3D> total_out total =3D> total_in size =3D> buf_len samples =3D> frames_out_max Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 9e9c03a42e..22c36d6660 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -576,10 +576,10 @@ static void audio_pcm_sw_resample_in(SWVoiceIn *sw, } } =20 -static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size) +static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t buf_len) { HWVoiceIn *hw =3D sw->hw; - size_t samples, live, ret, swlim, total; + size_t live, frames_out_max, swlim, total_in, total_out; =20 live =3D hw->total_samples_captured - sw->total_hw_samples_acquired; if (!live) { @@ -590,20 +590,20 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *= buf, size_t size) return 0; } =20 - samples =3D size / sw->info.bytes_per_frame; + frames_out_max =3D buf_len / sw->info.bytes_per_frame; =20 swlim =3D (live * sw->ratio) >> 32; - swlim =3D MIN (swlim, samples); + swlim =3D MIN(swlim, frames_out_max); =20 - audio_pcm_sw_resample_in(sw, live, swlim, &total, &ret); + audio_pcm_sw_resample_in(sw, live, swlim, &total_in, &total_out); =20 if (!hw->pcm_ops->volume_in) { - mixeng_volume(sw->resample_buf.buffer, ret, &sw->vol); + mixeng_volume(sw->resample_buf.buffer, total_out, &sw->vol); } + sw->clip(buf, sw->resample_buf.buffer, total_out); =20 - sw->clip(buf, sw->resample_buf.buffer, ret); - sw->total_hw_samples_acquired +=3D total; - return ret * sw->info.bytes_per_frame; + sw->total_hw_samples_acquired +=3D total_in; + return total_out * sw->info.bytes_per_frame; } =20 /* --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709777656207.36517983390536; Mon, 6 Feb 2023 10:56:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6cA-0005Rk-OM; Mon, 06 Feb 2023 13:53:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c1-000523-SV for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:09 -0500 Received: from mailout08.t-online.de ([194.25.134.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c0-0005W4-8Y for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:09 -0500 Received: from fwd80.dcpf.telekom.de (fwd80.aul.t-online.de [10.223.144.106]) by mailout08.t-online.de (Postfix) with SMTP id AF1B029E7A; Mon, 6 Feb 2023 19:53:06 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd80.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6by-3A7zjV0; Mon, 6 Feb 2023 19:53:06 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 91EF22006C6; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 13/17] audio/mixeng: calculate number of output frames Date: Mon, 6 Feb 2023 19:52:33 +0100 Message-Id: <20230206185237.8358-13-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709586-B8817A74-8C6EAFD2/0/0 CLEAN NORMAL X-TOI-MSGID: 8aac8eb5-823d-43cf-b481-3ad1367ebe27 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.20; envelope-from=volker.ruemelin@t-online.de; helo=mailout08.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709779264100002 Calculate the exact number of audio output frames the resampling code can generate from a given number of audio input frames. When upsampling, this function returns the maximum number of output frames. This function will later replace the audio_frontend_frames_in() function, which calculates the average number of output frames rounded down to the nearest integer. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin --- audio/mixeng.c | 37 +++++++++++++++++++++++++++++++++++++ audio/mixeng.h | 1 + 2 files changed, 38 insertions(+) diff --git a/audio/mixeng.c b/audio/mixeng.c index 6bb3d54f77..92a3a1ac58 100644 --- a/audio/mixeng.c +++ b/audio/mixeng.c @@ -440,6 +440,43 @@ void st_rate_stop (void *opaque) g_free (opaque); } =20 +/** + * st_rate_frames_out() - returns the number of frames the resampling code + * generates from frames_in frames + * + * @opaque: pointer to struct rate + * @frames_in: number of frames + */ +uint32_t st_rate_frames_out(void *opaque, uint32_t frames_in) +{ + struct rate *rate =3D opaque; + uint64_t opos_end, opos_delta; + uint32_t ipos_end; + uint32_t frames_out; + + if (rate->opos_inc =3D=3D 1ULL << 32) { + return frames_in; + } + + /* no output frame without at least one input frame */ + if (!frames_in) { + return 0; + } + + /* last frame read was at rate->ipos - 1 */ + ipos_end =3D rate->ipos - 1 + frames_in; + opos_end =3D (uint64_t)ipos_end << 32; + + /* last frame written was at rate->opos - rate->opos_inc */ + if (opos_end + rate->opos_inc <=3D rate->opos) { + return 0; + } + opos_delta =3D opos_end - rate->opos + rate->opos_inc; + frames_out =3D opos_delta / rate->opos_inc; + + return opos_delta % rate->opos_inc ? frames_out : frames_out - 1; +} + /** * st_rate_frames_in() - returns the number of frames needed to * get frames_out frames after resampling diff --git a/audio/mixeng.h b/audio/mixeng.h index 64c1e231cc..f9de7cffeb 100644 --- a/audio/mixeng.h +++ b/audio/mixeng.h @@ -52,6 +52,7 @@ void st_rate_flow(void *opaque, st_sample *ibuf, st_sampl= e *obuf, void st_rate_flow_mix(void *opaque, st_sample *ibuf, st_sample *obuf, size_t *isamp, size_t *osamp); void st_rate_stop (void *opaque); +uint32_t st_rate_frames_out(void *opaque, uint32_t frames_in); uint32_t st_rate_frames_in(void *opaque, uint32_t frames_out); void mixeng_clear (struct st_sample *buf, int len); void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *= vol); --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709827985878.0475993526127; Mon, 6 Feb 2023 10:57:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6cB-0005Wu-Rt; Mon, 06 Feb 2023 13:53:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c9-0005M2-E3 for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:17 -0500 Received: from mailout03.t-online.de ([194.25.134.81]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c4-0005Wi-ED for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:17 -0500 Received: from fwd71.dcpf.telekom.de (fwd71.aul.t-online.de [10.223.144.97]) by mailout03.t-online.de (Postfix) with SMTP id 22891177BB; Mon, 6 Feb 2023 19:53:09 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd71.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6c0-22PXxR0; Mon, 6 Feb 2023 19:53:08 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 94BE22006C8; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 14/17] audio: wire up st_rate_frames_out() Date: Mon, 6 Feb 2023 19:52:34 +0100 Message-Id: <20230206185237.8358-14-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709588-437ECC4D-00173077/0/0 CLEAN NORMAL X-TOI-MSGID: d6d7aca6-1e01-4365-93ee-3c806e7db691 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.81; envelope-from=volker.ruemelin@t-online.de; helo=mailout03.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709830090100006 Wire up the st_rate_frames_out() function and replace audio_frontend_frames_in() to make audio packet length calculation exact. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin --- audio/audio.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 22c36d6660..dad17e59b8 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -579,7 +579,7 @@ static void audio_pcm_sw_resample_in(SWVoiceIn *sw, static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t buf_len) { HWVoiceIn *hw =3D sw->hw; - size_t live, frames_out_max, swlim, total_in, total_out; + size_t live, frames_out_max, total_in, total_out; =20 live =3D hw->total_samples_captured - sw->total_hw_samples_acquired; if (!live) { @@ -590,12 +590,10 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *= buf, size_t buf_len) return 0; } =20 - frames_out_max =3D buf_len / sw->info.bytes_per_frame; + frames_out_max =3D MIN(buf_len / sw->info.bytes_per_frame, + sw->resample_buf.size); =20 - swlim =3D (live * sw->ratio) >> 32; - swlim =3D MIN(swlim, frames_out_max); - - audio_pcm_sw_resample_in(sw, live, swlim, &total_in, &total_out); + audio_pcm_sw_resample_in(sw, live, frames_out_max, &total_in, &total_o= ut); =20 if (!hw->pcm_ops->volume_in) { mixeng_volume(sw->resample_buf.buffer, total_out, &sw->vol); @@ -979,18 +977,6 @@ void AUD_set_active_in (SWVoiceIn *sw, int on) } } =20 -/** - * audio_frontend_frames_in() - returns the number of frames the resampling - * code generates from frames_in frames - * - * @sw: audio recording frontend - * @frames_in: number of frames - */ -static size_t audio_frontend_frames_in(SWVoiceIn *sw, size_t frames_in) -{ - return (int64_t)frames_in * sw->ratio >> 32; -} - static size_t audio_get_avail (SWVoiceIn *sw) { size_t live; @@ -1007,9 +993,9 @@ static size_t audio_get_avail (SWVoiceIn *sw) } =20 ldebug ( - "%s: get_avail live %zu frontend frames %zu\n", + "%s: get_avail live %zu frontend frames %u\n", SW_NAME (sw), - live, audio_frontend_frames_in(sw, live) + live, st_rate_frames_out(sw->rate, live) ); =20 return live; @@ -1314,8 +1300,9 @@ static void audio_run_in (AudioState *s) size_t sw_avail =3D audio_get_avail(sw); size_t avail; =20 - avail =3D audio_frontend_frames_in(sw, sw_avail); + avail =3D st_rate_frames_out(sw->rate, sw_avail); if (avail > 0) { + avail =3D MIN(avail, sw->resample_buf.size); sw->callback.fn(sw->callback.opaque, avail * sw->info.bytes_per_frame); } --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709827085754.3290779205854; Mon, 6 Feb 2023 10:57:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6cA-0005R9-Ja; Mon, 06 Feb 2023 13:53:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c8-0005Dr-1D for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:16 -0500 Received: from mailout01.t-online.de ([194.25.134.80]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c5-0005Wv-FS for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:14 -0500 Received: from fwd72.dcpf.telekom.de (fwd72.aul.t-online.de [10.223.144.98]) by mailout01.t-online.de (Postfix) with SMTP id 50B73BEB3; Mon, 6 Feb 2023 19:53:11 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd72.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6c3-2UCJMX0; Mon, 6 Feb 2023 19:53:11 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 977BF2006C9; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 15/17] audio: handle leftover audio frame from upsampling Date: Mon, 6 Feb 2023 19:52:35 +0100 Message-Id: <20230206185237.8358-15-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709591-81AEBD99-F64D8ECC/2/50744550798 SUSPECT URL-COUNT X-TOI-MSGID: 859d53c4-4527-4f4d-824e-61cc276b1897 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.80; envelope-from=volker.ruemelin@t-online.de; helo=mailout01.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709827983100001 Upsampling may leave one remaining audio frame in the input buffer. The emulated audio playback devices are currently resposible to write this audio frame again in the next write cycle. Push that task down to audio_pcm_sw_write. This is another step towards an audio callback interface that guarantees that when audio frontends are told they can write n audio frames, they can actually do so. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Acked-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 34 ++++++++++++++++++++++++++++------ audio/audio_template.h | 6 ++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index dad17e59b8..4836ab8ca8 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -731,16 +731,21 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void= *buf, size_t buf_len) hw_free =3D hw_free > live ? hw_free - live : 0; frames_out_max =3D MIN(dead, hw_free); sw_max =3D st_rate_frames_in(sw->rate, frames_out_max); - fe_max =3D MIN(buf_len / sw->info.bytes_per_frame, sw->resample_buf.si= ze); + fe_max =3D MIN(buf_len / sw->info.bytes_per_frame + sw->resample_buf.p= os, + sw->resample_buf.size); frames_in_max =3D MIN(sw_max, fe_max); =20 if (!frames_in_max) { return 0; } =20 - sw->conv(sw->resample_buf.buffer, buf, frames_in_max); - if (!sw->hw->pcm_ops->volume_out) { - mixeng_volume(sw->resample_buf.buffer, frames_in_max, &sw->vol); + if (frames_in_max > sw->resample_buf.pos) { + sw->conv(sw->resample_buf.buffer + sw->resample_buf.pos, + buf, frames_in_max - sw->resample_buf.pos); + if (!sw->hw->pcm_ops->volume_out) { + mixeng_volume(sw->resample_buf.buffer + sw->resample_buf.pos, + frames_in_max - sw->resample_buf.pos, &sw->vol); + } } =20 audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max, @@ -749,6 +754,22 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void = *buf, size_t buf_len) sw->total_hw_samples_mixed +=3D total_out; sw->empty =3D sw->total_hw_samples_mixed =3D=3D 0; =20 + /* + * Upsampling may leave one audio frame in the resample buffer. Decrem= ent + * total_in by one if there was a leftover frame from the previous res= ample + * pass in the resample buffer. Increment total_in by one if the curre= nt + * resample pass left one frame in the resample buffer. + */ + if (frames_in_max - total_in =3D=3D 1) { + /* copy one leftover audio frame to the beginning of the buffer */ + *sw->resample_buf.buffer =3D *(sw->resample_buf.buffer + total_in); + total_in +=3D 1 - sw->resample_buf.pos; + sw->resample_buf.pos =3D 1; + } else if (total_in >=3D sw->resample_buf.pos) { + total_in -=3D sw->resample_buf.pos; + sw->resample_buf.pos =3D 0; + } + #ifdef DEBUG_OUT dolog ( "%s: write size %zu written %zu total mixed %zu\n", @@ -1155,8 +1176,9 @@ static void audio_run_out (AudioState *s) } else { free =3D 0; } - if (free > 0) { - free =3D MIN(free, sw->resample_buf.size); + if (free > sw->resample_buf.pos) { + free =3D MIN(free, sw->resample_buf.size) + - sw->resample_buf.pos; sw->callback.fn(sw->callback.opaque, free * sw->info.bytes_per_frame); } diff --git a/audio/audio_template.h b/audio/audio_template.h index a0b653f52c..0d8aab6fad 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -138,6 +138,12 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) = (SW *sw) return -1; } =20 + /* + * Allocate one additional audio frame that is needed for upsampling + * if the resample buffer size is small. For large buffer sizes take + * care of overflows. + */ + samples =3D samples < INT_MAX ? samples + 1 : INT_MAX; sw->resample_buf.buffer =3D g_new0(st_sample, samples); sw->resample_buf.size =3D samples; sw->resample_buf.pos =3D 0; --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709669378421.209302200054; Mon, 6 Feb 2023 10:54:29 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6cF-0005eB-7y; Mon, 06 Feb 2023 13:53:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6cD-0005ak-Nq for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:21 -0500 Received: from mailout02.t-online.de ([194.25.134.17]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c8-0005XG-KH for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:21 -0500 Received: from fwd83.dcpf.telekom.de (fwd83.aul.t-online.de [10.223.144.109]) by mailout02.t-online.de (Postfix) with SMTP id 39E2D2A35C; Mon, 6 Feb 2023 19:53:15 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd83.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6c5-3ZIWHZ0; Mon, 6 Feb 2023 19:53:13 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 9A3F82006CE; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 16/17] audio/audio_template: substitute sw->hw with hw Date: Mon, 6 Feb 2023 19:52:36 +0100 Message-Id: <20230206185237.8358-16-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709593-432C3378-768373ED/0/0 CLEAN NORMAL X-TOI-MSGID: 3eac6bbd-7d12-43bc-8dd2-76cb3f4b2236 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.17; envelope-from=volker.ruemelin@t-online.de; helo=mailout02.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_NONE=0.001, T_SPF_HELO_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709670611100003 Substitute sw->hw with hw in the audio_pcm_sw_alloc_resources_* functions. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio_template.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/audio/audio_template.h b/audio/audio_template.h index 0d8aab6fad..7e116426c7 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -107,6 +107,7 @@ static void glue (audio_pcm_sw_free_resources_, TYPE) (= SW *sw) =20 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) { + HW *hw =3D sw->hw; int samples; =20 if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) { @@ -125,7 +126,6 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (= SW *sw) } =20 if (samples =3D=3D 0) { - HW *hw =3D sw->hw; size_t f_fe_min; =20 /* f_fe_min =3D ceil(1 [frames] * f_be [Hz] / size_be [frames]) */ @@ -149,9 +149,9 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (= SW *sw) sw->resample_buf.pos =3D 0; =20 #ifdef DAC - sw->rate =3D st_rate_start (sw->info.freq, sw->hw->info.freq); + sw->rate =3D st_rate_start(sw->info.freq, hw->info.freq); #else - sw->rate =3D st_rate_start (sw->hw->info.freq, sw->info.freq); + sw->rate =3D st_rate_start(hw->info.freq, sw->info.freq); #endif =20 return 0; --=20 2.35.3 From nobody Tue Apr 30 11:58:18 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675709842868374.4734201951143; Mon, 6 Feb 2023 10:57:22 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pP6cD-0005aC-MO; Mon, 06 Feb 2023 13:53:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6cB-0005WP-IN for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:19 -0500 Received: from mailout01.t-online.de ([194.25.134.80]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pP6c9-0005XR-Ok for qemu-devel@nongnu.org; Mon, 06 Feb 2023 13:53:19 -0500 Received: from fwd72.dcpf.telekom.de (fwd72.aul.t-online.de [10.223.144.98]) by mailout01.t-online.de (Postfix) with SMTP id 395ADBEB6; Mon, 6 Feb 2023 19:53:16 +0100 (CET) Received: from linpower.localnet ([79.208.25.151]) by fwd72.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1pP6c7-3AMqNF0; Mon, 6 Feb 2023 19:53:15 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 9D00C2006CF; Mon, 6 Feb 2023 19:52:37 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org, Christian Schoenebeck , Mark Cave-Ayland Subject: [PATCH v2 17/17] audio: remove sw->ratio Date: Mon, 6 Feb 2023 19:52:37 +0100 Message-Id: <20230206185237.8358-17-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1675709595-81AEBD99-64F0BB90/2/50744550798 SUSPECT URL-COUNT X-TOI-MSGID: 7057a69a-cd9a-4033-b082-81c90fcb8e0c Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=194.25.134.80; envelope-from=volker.ruemelin@t-online.de; helo=mailout01.t-online.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1675709844341100004 Simplify the resample buffer size calculation. For audio playback we have sw->ratio =3D ((int64_t)sw->hw->info.freq << 32) / sw->info.freq; samples =3D ((int64_t)sw->HWBUF.size << 32) / sw->ratio; This can be simplified to samples =3D muldiv64(sw->HWBUF.size, sw->info.freq, sw->hw->info.freq); For audio recording we have sw->ratio =3D ((int64_t)sw->info.freq << 32) / sw->hw->info.freq; samples =3D (int64_t)sw->HWBUF.size * sw->ratio >> 32; This can be simplified to samples =3D muldiv64(sw->HWBUF.size, sw->info.freq, sw->hw->info.freq); With hw =3D sw->hw this becomes in both cases samples =3D muldiv64(HWBUF.size, sw->info.freq, hw->info.freq); Now that sw->ratio is no longer needed, remove sw->ratio. Acked-by: Mark Cave-Ayland Signed-off-by: Volker R=C3=BCmelin Reviewed-by: Marc-Andr=C3=A9 Lureau --- audio/audio.c | 1 - audio/audio_int.h | 2 -- audio/audio_template.h | 30 +++++++++--------------------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 4836ab8ca8..70b096713c 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -478,7 +478,6 @@ static int audio_attach_capture (HWVoiceOut *hw) sw->info =3D hw->info; sw->empty =3D 1; sw->active =3D hw->enabled; - sw->ratio =3D ((int64_t) hw_cap->info.freq << 32) / sw->info.freq; sw->vol =3D nominal_volume; sw->rate =3D st_rate_start (sw->info.freq, hw_cap->info.freq); QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries); diff --git a/audio/audio_int.h b/audio/audio_int.h index 8b163e1759..d51d63f08d 100644 --- a/audio/audio_int.h +++ b/audio/audio_int.h @@ -108,7 +108,6 @@ struct SWVoiceOut { AudioState *s; struct audio_pcm_info info; t_sample *conv; - int64_t ratio; STSampleBuffer resample_buf; void *rate; size_t total_hw_samples_mixed; @@ -126,7 +125,6 @@ struct SWVoiceIn { AudioState *s; int active; struct audio_pcm_info info; - int64_t ratio; void *rate; size_t total_hw_samples_acquired; STSampleBuffer resample_buf; diff --git a/audio/audio_template.h b/audio/audio_template.h index 7e116426c7..e42326c20d 100644 --- a/audio/audio_template.h +++ b/audio/audio_template.h @@ -108,32 +108,23 @@ static void glue (audio_pcm_sw_free_resources_, TYPE)= (SW *sw) static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) { HW *hw =3D sw->hw; - int samples; + uint64_t samples; =20 if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) { return 0; } =20 -#ifdef DAC - samples =3D ((int64_t)sw->HWBUF.size << 32) / sw->ratio; -#else - samples =3D (int64_t)sw->HWBUF.size * sw->ratio >> 32; -#endif - if (audio_bug(__func__, samples < 0)) { - dolog("Can not allocate buffer for `%s' (%d samples)\n", - SW_NAME(sw), samples); - return -1; - } - + samples =3D muldiv64(HWBUF.size, sw->info.freq, hw->info.freq); if (samples =3D=3D 0) { - size_t f_fe_min; + uint64_t f_fe_min; + uint64_t f_be =3D (uint32_t)hw->info.freq; =20 /* f_fe_min =3D ceil(1 [frames] * f_be [Hz] / size_be [frames]) */ - f_fe_min =3D (hw->info.freq + HWBUF.size - 1) / HWBUF.size; + f_fe_min =3D (f_be + HWBUF.size - 1) / HWBUF.size; qemu_log_mask(LOG_UNIMP, AUDIO_CAP ": The guest selected a " NAME " sample ra= te" - " of %d Hz for %s. Only sample rates >=3D %zu Hz are" - " supported.\n", + " of %d Hz for %s. Only sample rates >=3D %" PRIu64 = " Hz" + " are supported.\n", sw->info.freq, sw->name, f_fe_min); return -1; } @@ -141,9 +132,9 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (= SW *sw) /* * Allocate one additional audio frame that is needed for upsampling * if the resample buffer size is small. For large buffer sizes take - * care of overflows. + * care of overflows and truncation. */ - samples =3D samples < INT_MAX ? samples + 1 : INT_MAX; + samples =3D samples < SIZE_MAX ? samples + 1 : SIZE_MAX; sw->resample_buf.buffer =3D g_new0(st_sample, samples); sw->resample_buf.size =3D samples; sw->resample_buf.pos =3D 0; @@ -170,11 +161,8 @@ static int glue (audio_pcm_sw_init_, TYPE) ( sw->hw =3D hw; sw->active =3D 0; #ifdef DAC - sw->ratio =3D ((int64_t) sw->hw->info.freq << 32) / sw->info.freq; sw->total_hw_samples_mixed =3D 0; sw->empty =3D 1; -#else - sw->ratio =3D ((int64_t) sw->info.freq << 32) / sw->hw->info.freq; #endif =20 if (sw->info.is_float) { --=20 2.35.3