VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/HDAStreamPeriod.cpp@ 88163

Last change on this file since 88163 was 88139, checked in by vboxsync, 4 years ago

DevHDA: Reducing HDAStreamPeriod (will be eliminated). bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.8 KB
Line 
1/* $Id: HDAStreamPeriod.cpp 88139 2021-03-16 12:33:30Z vboxsync $ */
2/** @file
3 * HDAStreamPeriod.cpp - Stream period functions for HD Audio.
4 *
5 * Utility functions for handling HDA audio stream periods. Stream period
6 * handling is needed in order to keep track of a stream's timing
7 * and processed audio data.
8 *
9 * As the HDA device only has one bit clock (WALCLK) but audio streams can be
10 * processed at certain points in time, these functions can be used to estimate
11 * and schedule the wall clock (WALCLK) for all streams accordingly.
12 */
13
14/*
15 * Copyright (C) 2017-2020 Oracle Corporation
16 *
17 * This file is part of VirtualBox Open Source Edition (OSE), as
18 * available from http://www.virtualbox.org. This file is free software;
19 * you can redistribute it and/or modify it under the terms of the GNU
20 * General Public License (GPL) as published by the Free Software
21 * Foundation, in version 2 as it comes in the "COPYING" file of the
22 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
23 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
24 */
25
26
27/*********************************************************************************************************************************
28* Header Files *
29*********************************************************************************************************************************/
30#define LOG_GROUP LOG_GROUP_DEV_HDA
31#include <VBox/log.h>
32
33#include <iprt/asm-math.h> /* For ASMMultU64ByU32DivByU32(). */
34
35#include <VBox/vmm/pdmdev.h>
36#include <VBox/vmm/pdmaudioifs.h>
37#include <VBox/vmm/pdmaudioinline.h>
38
39#include "DrvAudio.h"
40#include "HDAStreamPeriod.h"
41
42
43#ifdef IN_RING3 /* entire file currently */
44
45/**
46 * Creates a stream period.
47 *
48 * @return IPRT status code.
49 * @param pPeriod Stream period to initialize.
50 */
51int hdaR3StreamPeriodCreate(PHDASTREAMPERIOD pPeriod)
52{
53 Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_F_VALID));
54
55 pPeriod->fStatus = HDASTREAMPERIOD_F_VALID;
56
57 return VINF_SUCCESS;
58}
59
60/**
61 * Destroys a formerly created stream period.
62 *
63 * @param pPeriod Stream period to destroy.
64 */
65void hdaR3StreamPeriodDestroy(PHDASTREAMPERIOD pPeriod)
66{
67 if (pPeriod->fStatus & HDASTREAMPERIOD_F_VALID)
68 pPeriod->fStatus = HDASTREAMPERIOD_F_NONE;
69}
70
71/**
72 * Initializes a given stream period with needed parameters.
73 *
74 * @return VBox status code.
75 * @param pPeriod Stream period to (re-)initialize. Must be created with hdaR3StreamPeriodCreate() first.
76 * @param u8SD Stream descriptor (serial data #) number to assign this stream period to.
77 * @param u16LVI The HDA stream's LVI value to use for the period calculation.
78 * @param u32CBL The HDA stream's CBL value to use for the period calculation.
79 * @param pStreamCfg Audio stream configuration to use for this period.
80 */
81int hdaR3StreamPeriodInit(PHDASTREAMPERIOD pPeriod,
82 uint8_t u8SD, uint16_t u16LVI, uint32_t u32CBL, PPDMAUDIOSTREAMCFG pStreamCfg)
83{
84 if ( !u16LVI
85 || !u32CBL
86 || !DrvAudioHlpPcmPropsAreValid(&pStreamCfg->Props))
87 {
88 return VERR_INVALID_PARAMETER;
89 }
90
91 /*
92 * Linux guests (at least Ubuntu):
93 * 17632 bytes (CBL) / 4 (frame size) = 4408 frames / 4 (LVI) = 1102 frames per period
94 *
95 * Windows guests (Win10 AU):
96 * 3584 bytes (CBL) / 4 (frame size) = 896 frames / 2 (LVI) = 448 frames per period
97 */
98 unsigned cTotalPeriods = u16LVI + 1;
99
100 if (cTotalPeriods <= 1)
101 cTotalPeriods = 2; /* At least two periods *must* be present (LVI >= 1). */
102
103 uint32_t cFramesToTransfer =
104 (u32CBL / (pStreamCfg->Props.cbSample * pStreamCfg->Props.cChannels /* Frame size */)) / cTotalPeriods;
105
106 pPeriod->u8SD = u8SD;
107 pPeriod->u64StartWalClk = 0;
108 pPeriod->u32Hz = pStreamCfg->Props.uHz;
109 pPeriod->u64DurationWalClk = hdaR3StreamPeriodFramesToWalClk(pPeriod, cFramesToTransfer);
110 pPeriod->u64ElapsedWalClk = 0;
111 pPeriod->i64DelayWalClk = 0;
112 pPeriod->cFramesToTransfer = cFramesToTransfer;
113 pPeriod->cFramesTransferred = 0;
114
115 Log3Func(("[SD%RU8] %RU64 long, Hz=%RU32, CBL=%RU32, LVI=%RU16 -> %u periods, %RU32 frames each\n",
116 pPeriod->u8SD, pPeriod->u64DurationWalClk, pPeriod->u32Hz, u32CBL, u16LVI,
117 cTotalPeriods, pPeriod->cFramesToTransfer));
118
119 return VINF_SUCCESS;
120}
121
122/**
123 * Resets a stream period to its initial state.
124 *
125 * @param pPeriod Stream period to reset.
126 */
127void hdaR3StreamPeriodReset(PHDASTREAMPERIOD pPeriod)
128{
129 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
130
131 pPeriod->fStatus &= ~HDASTREAMPERIOD_F_ACTIVE;
132 pPeriod->u64StartWalClk = 0;
133 pPeriod->u64ElapsedWalClk = 0;
134 pPeriod->cFramesTransferred = 0;
135# ifdef LOG_ENABLED
136 pPeriod->Dbg.tsStartNs = 0;
137# endif
138}
139
140/**
141 * Begins a new period life span of a given period.
142 *
143 * @return IPRT status code.
144 * @param pPeriod Stream period to begin new life span for.
145 * @param u64WalClk Wall clock (WALCLK) value to set for the period's starting point.
146 */
147int hdaR3StreamPeriodBegin(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
148{
149 Assert(!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE)); /* No nested calls. */
150
151 pPeriod->fStatus |= HDASTREAMPERIOD_F_ACTIVE;
152 pPeriod->u64StartWalClk = u64WalClk;
153 pPeriod->u64ElapsedWalClk = 0;
154 pPeriod->cFramesTransferred = 0;
155# ifdef LOG_ENABLED
156 pPeriod->Dbg.tsStartNs = RTTimeNanoTS();
157# endif
158
159 Log3Func(("[SD%RU8] Starting @ %RU64 (%RU64 long)\n", pPeriod->u8SD, pPeriod->u64StartWalClk, pPeriod->u64DurationWalClk));
160 return VINF_SUCCESS;
161}
162
163/**
164 * Resumes a formerly paused period.
165 *
166 * @param pPeriod Stream period to resume.
167 */
168void hdaR3StreamPeriodResume(PHDASTREAMPERIOD pPeriod)
169{
170 AssertMsg(!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE), ("Period %p already in active state\n", pPeriod));
171
172 pPeriod->fStatus |= HDASTREAMPERIOD_F_ACTIVE;
173
174 Log3Func(("[SD%RU8]\n", pPeriod->u8SD));
175}
176
177/**
178 * Returns the wall clock (WALCLK) value for a given amount of stream period audio frames.
179 *
180 * @return Calculated wall clock value.
181 * @param pPeriod Stream period to calculate wall clock value for.
182 * @param cFrames Number of audio frames to calculate wall clock value for.
183 *
184 * @remark Calculation depends on the given stream period and assumes a 24 MHz wall clock counter (WALCLK).
185 */
186uint64_t hdaR3StreamPeriodFramesToWalClk(PHDASTREAMPERIOD pPeriod, uint32_t cFrames)
187{
188 /* Prevent division by zero. */
189 const uint32_t uHz = pPeriod->u32Hz ? pPeriod->u32Hz : 1;
190
191 /* 24 MHz wall clock (WALCLK): 42ns resolution. */
192 return ASMMultU32ByU32DivByU32(cFrames, 24000000, uHz);
193}
194
195/**
196 * Returns the absolute wall clock (WALCLK) value for the already elapsed time of
197 * a given stream period.
198 *
199 * @return Absolute elapsed time as wall clock (WALCLK) value.
200 * @param pPeriod Stream period to use.
201 */
202uint64_t hdaR3StreamPeriodGetAbsElapsedWalClk(PHDASTREAMPERIOD pPeriod)
203{
204 return pPeriod->u64StartWalClk
205 + pPeriod->u64ElapsedWalClk
206 + pPeriod->i64DelayWalClk;
207}
208
209/**
210 * Returns the absolute wall clock (WALCLK) value for the calculated end time of
211 * a given stream period.
212 *
213 * @return Absolute end time as wall clock (WALCLK) value.
214 * @param pPeriod Stream period to use.
215 */
216uint64_t hdaR3StreamPeriodGetAbsEndWalClk(PHDASTREAMPERIOD pPeriod)
217{
218 return pPeriod->u64StartWalClk + pPeriod->u64DurationWalClk;
219}
220
221/**
222 * Returns the remaining audio frames to process for a given stream period.
223 *
224 * @return Number of remaining audio frames to process. 0 if all were processed.
225 * @param pPeriod Stream period to return value for.
226 */
227uint32_t hdaR3StreamPeriodGetRemainingFrames(PHDASTREAMPERIOD pPeriod)
228{
229 Assert(pPeriod->cFramesToTransfer >= pPeriod->cFramesTransferred);
230 return pPeriod->cFramesToTransfer - pPeriod->cFramesTransferred;
231}
232
233/**
234 * Tells whether a given stream period has elapsed (time-wise) or not.
235 *
236 * @return true if the stream period has elapsed, false if not.
237 * @param pPeriod Stream period to get status for.
238 */
239bool hdaR3StreamPeriodHasElapsed(PHDASTREAMPERIOD pPeriod)
240{
241 return (pPeriod->u64ElapsedWalClk >= pPeriod->u64DurationWalClk);
242}
243
244/**
245 * Tells whether a given stream period has passed the given absolute wall clock (WALCLK)
246 * time or not
247 *
248 * @return true if the stream period has passed the given time, false if not.
249 * @param pPeriod Stream period to get status for.
250 * @param u64WalClk Absolute wall clock (WALCLK) time to check for.
251 */
252bool hdaR3StreamPeriodHasPassedAbsWalClk(PHDASTREAMPERIOD pPeriod, uint64_t u64WalClk)
253{
254 /* Period not in use? */
255 if (!(pPeriod->fStatus & HDASTREAMPERIOD_F_ACTIVE))
256 return true; /* ... implies that it has passed. */
257
258 if (hdaR3StreamPeriodHasElapsed(pPeriod))
259 return true; /* Period already has elapsed. */
260
261 return (pPeriod->u64StartWalClk + pPeriod->u64ElapsedWalClk) >= u64WalClk;
262}
263
264/**
265 * Adds an amount of (processed) audio frames to a given stream period.
266 *
267 * @return IPRT status code.
268 * @param pPeriod Stream period to add audio frames to.
269 * @param framesInc Audio frames to add.
270 */
271void hdaR3StreamPeriodInc(PHDASTREAMPERIOD pPeriod, uint32_t framesInc)
272{
273 pPeriod->cFramesTransferred += framesInc;
274 Assert(pPeriod->cFramesTransferred <= pPeriod->cFramesToTransfer);
275
276 pPeriod->u64ElapsedWalClk = hdaR3StreamPeriodFramesToWalClk(pPeriod, pPeriod->cFramesTransferred);
277 Assert(pPeriod->u64ElapsedWalClk <= pPeriod->u64DurationWalClk);
278
279 Log3Func(("[SD%RU8] cbTransferred=%RU32, u64ElapsedWalClk=%RU64\n",
280 pPeriod->u8SD, pPeriod->cFramesTransferred, pPeriod->u64ElapsedWalClk));
281}
282
283#endif /* IN_RING3 */
284
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette