qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Volker Rümelin" <vr_qemu@t-online.de>
To: "Gerd Hoffmann" <kraxel@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@gmail.com>
Cc: qemu-devel@nongnu.org,
	Christian Schoenebeck <qemu_oss@crudebyte.com>,
	Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Subject: [PATCH v2 09/17] audio/mixeng: calculate number of input frames
Date: Mon,  6 Feb 2023 19:52:29 +0100	[thread overview]
Message-ID: <20230206185237.8358-9-vr_qemu@t-online.de> (raw)
In-Reply-To: <df6510fe-1dfd-1585-8590-db230c71d367@t-online.de>

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 <mark.cave-ayland@ilande.co.uk>
Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
---
 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);
 }
 
+/**
+ * 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 = opaque;
+    uint64_t opos_start, opos_end;
+    uint32_t ipos_start, ipos_end;
+
+    if (rate->opos_inc == 1ULL << 32) {
+        return frames_out;
+    }
+
+    if (frames_out) {
+        opos_start = rate->opos;
+        ipos_start = rate->ipos;
+    } else {
+        uint64_t offset;
+
+        /* add offset = ceil(opos_inc) to opos and ipos to avoid an underflow */
+        offset = (rate->opos_inc + (1ULL << 32) - 1) & ~((1ULL << 32) - 1);
+        opos_start = rate->opos + offset;
+        ipos_start = rate->ipos + (offset >> 32);
+    }
+    /* last frame written was at opos_start - rate->opos_inc */
+    opos_end = opos_start - rate->opos_inc + rate->opos_inc * frames_out;
+    ipos_end = (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_sample *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);
 
-- 
2.35.3



  parent reply	other threads:[~2023-02-06 18:54 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-06 18:47 [PATCH v2 00/17] audio: improve callback interface for audio frontends Volker Rümelin
2023-02-06 18:52 ` [PATCH v2 01/17] audio: change type of mix_buf and conv_buf Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 02/17] audio: change type and name of the resample buffer Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 03/17] audio: make the resampling code greedy Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 04/17] audio: replace the resampling loop in audio_pcm_sw_write() Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 05/17] audio: remove sw == NULL check Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 06/17] audio: rename variables in audio_pcm_sw_write() Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 07/17] audio: don't misuse audio_pcm_sw_write() Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 08/17] audio: remove unused noop_conv() function Volker Rümelin
2023-02-22 10:49   ` Marc-André Lureau
2023-02-06 18:52 ` Volker Rümelin [this message]
2023-02-22 10:50   ` [PATCH v2 09/17] audio/mixeng: calculate number of input frames Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 10/17] audio: wire up st_rate_frames_in() Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 11/17] audio: replace the resampling loop in audio_pcm_sw_read() Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 12/17] audio: rename variables " Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 13/17] audio/mixeng: calculate number of output frames Volker Rümelin
2023-02-06 18:52 ` [PATCH v2 14/17] audio: wire up st_rate_frames_out() Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 15/17] audio: handle leftover audio frame from upsampling Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 16/17] audio/audio_template: substitute sw->hw with hw Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau
2023-02-06 18:52 ` [PATCH v2 17/17] audio: remove sw->ratio Volker Rümelin
2023-02-22 10:50   ` Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230206185237.8358-9-vr_qemu@t-online.de \
    --to=vr_qemu@t-online.de \
    --cc=kraxel@redhat.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu_oss@crudebyte.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).