1 | /* $Id: HDAStreamMap.cpp 88028 2021-03-08 19:31:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HDAStreamMap.cpp - Stream mapping functions for HD Audio.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DEV_HDA
|
---|
23 | #include <VBox/log.h>
|
---|
24 |
|
---|
25 | #include <iprt/mem.h>
|
---|
26 |
|
---|
27 | #include <VBox/vmm/pdmdev.h>
|
---|
28 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
29 | #include <VBox/vmm/pdmaudioinline.h>
|
---|
30 |
|
---|
31 | #include "DrvAudio.h"
|
---|
32 |
|
---|
33 | #include "HDAStreamChannel.h"
|
---|
34 | #include "HDAStreamMap.h"
|
---|
35 |
|
---|
36 | #ifdef IN_RING3
|
---|
37 |
|
---|
38 | static int hdaR3StreamMapSetup(PHDASTREAMMAP pMap, PPDMAUDIOPCMPROPS pProps);
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Initializes a stream mapping structure according to the given PCM properties.
|
---|
42 | *
|
---|
43 | * @return IPRT status code.
|
---|
44 | * @param pMap Pointer to mapping to initialize.
|
---|
45 | * @param pProps Pointer to PCM properties to use for initialization.
|
---|
46 | */
|
---|
47 | int hdaR3StreamMapInit(PHDASTREAMMAP pMap, PPDMAUDIOPCMPROPS pProps)
|
---|
48 | {
|
---|
49 | AssertPtrReturn(pMap, VERR_INVALID_POINTER);
|
---|
50 | AssertPtrReturn(pProps, VERR_INVALID_POINTER);
|
---|
51 |
|
---|
52 | if (!DrvAudioHlpPcmPropsAreValid(pProps))
|
---|
53 | return VERR_INVALID_PARAMETER;
|
---|
54 |
|
---|
55 | hdaR3StreamMapReset(pMap);
|
---|
56 |
|
---|
57 | int rc = hdaR3StreamMapSetup(pMap, pProps);
|
---|
58 | if (RT_FAILURE(rc))
|
---|
59 | return rc;
|
---|
60 |
|
---|
61 | #ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
|
---|
62 | if ( RT_SUCCESS(rc)
|
---|
63 | /* Create circular buffer if not created yet. */
|
---|
64 | && !pMap->pCircBuf)
|
---|
65 | {
|
---|
66 | rc = RTCircBufCreate(&pMap->pCircBuf, _4K); /** @todo Make size configurable? */
|
---|
67 | }
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | if (RT_SUCCESS(rc))
|
---|
71 | {
|
---|
72 | pMap->cbFrameSize = pProps->cChannels * pProps->cbSample;
|
---|
73 |
|
---|
74 | LogFunc(("cChannels=%RU8, cBytes=%RU8 -> cbFrameSize=%RU32\n",
|
---|
75 | pProps->cChannels, pProps->cbSample, pMap->cbFrameSize));
|
---|
76 |
|
---|
77 | Assert(pMap->cbFrameSize); /* Frame size must not be 0. */
|
---|
78 |
|
---|
79 | pMap->enmLayout = PDMAUDIOSTREAMLAYOUT_INTERLEAVED;
|
---|
80 | }
|
---|
81 |
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Destroys a given stream mapping.
|
---|
88 | *
|
---|
89 | * @param pMap Pointer to mapping to destroy.
|
---|
90 | */
|
---|
91 | void hdaR3StreamMapDestroy(PHDASTREAMMAP pMap)
|
---|
92 | {
|
---|
93 | hdaR3StreamMapReset(pMap);
|
---|
94 |
|
---|
95 | #ifdef VBOX_WITH_AUDIO_HDA_51_SURROUND
|
---|
96 | if (pMap->pCircBuf)
|
---|
97 | {
|
---|
98 | RTCircBufDestroy(pMap->pCircBuf);
|
---|
99 | pMap->pCircBuf = NULL;
|
---|
100 | }
|
---|
101 | #endif
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Resets a given stream mapping.
|
---|
107 | *
|
---|
108 | * @param pMap Pointer to mapping to reset.
|
---|
109 | */
|
---|
110 | void hdaR3StreamMapReset(PHDASTREAMMAP pMap)
|
---|
111 | {
|
---|
112 | AssertPtrReturnVoid(pMap);
|
---|
113 |
|
---|
114 | pMap->enmLayout = PDMAUDIOSTREAMLAYOUT_UNKNOWN;
|
---|
115 |
|
---|
116 | if (pMap->paMappings)
|
---|
117 | {
|
---|
118 | for (uint8_t i = 0; i < pMap->cMappings; i++)
|
---|
119 | hdaR3StreamChannelDataDestroy(&pMap->paMappings[i].Data);
|
---|
120 |
|
---|
121 | RTMemFree(pMap->paMappings);
|
---|
122 | pMap->paMappings = NULL;
|
---|
123 |
|
---|
124 | pMap->cMappings = 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | RT_ZERO(pMap->PCMProps);
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Sets up a stream mapping according to the given properties / configuration.
|
---|
133 | *
|
---|
134 | * @return VBox status code, or VERR_NOT_SUPPORTED if the channel setup is not supported (yet).
|
---|
135 | * @param pMap Pointer to mapping to set up.
|
---|
136 | * @param pProps PCM audio properties to use for lookup.
|
---|
137 | */
|
---|
138 | static int hdaR3StreamMapSetup(PHDASTREAMMAP pMap, PPDMAUDIOPCMPROPS pProps)
|
---|
139 | {
|
---|
140 | int rc;
|
---|
141 |
|
---|
142 | /** @todo We ASSUME that all channels in a stream ...
|
---|
143 | * - have the same format
|
---|
144 | * - are in a consecutive order with no gaps in between
|
---|
145 | * - have a simple (raw) data layout
|
---|
146 | * - work in a non-striped fashion, e.g. interleaved (only on one SDn, not spread over multiple SDns) */
|
---|
147 | if ( pProps->cChannels == 1 /* Mono */
|
---|
148 | || pProps->cChannels == 2 /* Stereo */
|
---|
149 | || pProps->cChannels == 4 /* Quadrophonic */
|
---|
150 | || pProps->cChannels == 6) /* Surround (5.1) */
|
---|
151 | {
|
---|
152 | /* For now we don't have anything other as mono / stereo channels being covered by the backends.
|
---|
153 | * So just set up one channel covering those and skipping the rest (like configured rear or center/LFE outputs). */
|
---|
154 | pMap->cMappings = 1;
|
---|
155 | pMap->paMappings = (PPDMAUDIOSTREAMMAP)RTMemAlloc(sizeof(PDMAUDIOSTREAMMAP) * pMap->cMappings);
|
---|
156 | if (!pMap->paMappings)
|
---|
157 | return VERR_NO_MEMORY;
|
---|
158 |
|
---|
159 | PPDMAUDIOSTREAMMAP pMapLR = &pMap->paMappings[0];
|
---|
160 |
|
---|
161 | pMapLR->aenmIDs[0] = PDMAUDIOSTREAMCHANNELID_FRONT_LEFT;
|
---|
162 | pMapLR->aenmIDs[1] = PDMAUDIOSTREAMCHANNELID_FRONT_RIGHT;
|
---|
163 | pMapLR->cbFrame = pProps->cbSample * pProps->cChannels;
|
---|
164 | pMapLR->cbStep = pProps->cbSample * 2 /* Front left + Front right channels */;
|
---|
165 | pMapLR->offFirst = 0;
|
---|
166 | pMapLR->offNext = pMapLR->offFirst;
|
---|
167 |
|
---|
168 | rc = hdaR3StreamChannelDataInit(&pMapLR->Data, PDMAUDIOSTREAMCHANNELDATA_FLAGS_NONE);
|
---|
169 | AssertRC(rc);
|
---|
170 |
|
---|
171 | memcpy(&pMap->PCMProps, pProps, sizeof(PDMAUDIOPCMPROPS));
|
---|
172 | }
|
---|
173 | else
|
---|
174 | rc = VERR_NOT_SUPPORTED; /** @todo r=andy Support more setups. */
|
---|
175 |
|
---|
176 | return rc;
|
---|
177 | }
|
---|
178 | #endif /* IN_RING3 */
|
---|
179 |
|
---|