| 1 |
/* |
| 2 |
* Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2024 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 24 апр. 2020 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/common/types.h> |
| 23 |
|
| 24 |
#ifdef PLATFORM_WINDOWS |
| 25 |
|
| 26 |
#include <lsp-plug.in/common/alloc.h> |
| 27 |
#include <lsp-plug.in/common/debug.h> |
| 28 |
#include <lsp-plug.in/common/endian.h> |
| 29 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 30 |
#include <lsp-plug.in/mm/sample.h> |
| 31 |
#include <private/mm/ACMStream.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
#include <windows.h> |
| 35 |
#include <mmreg.h> |
| 36 |
#include <msacm.h> |
| 37 |
|
| 38 |
namespace lsp |
| 39 |
{ |
| 40 |
namespace mm |
| 41 |
{ |
| 42 |
|
| 43 |
ACMStream::ACMStream() |
| 44 |
{ |
| 45 |
pFmtIn = NULL; |
| 46 |
pFmtOut = NULL; |
| 47 |
hDriver = NULL; |
| 48 |
hStream = NULL; |
| 49 |
pHeader = NULL; |
| 50 |
} |
| 51 |
|
| 52 |
ACMStream::~ACMStream() |
| 53 |
{ |
| 54 |
close(); |
| 55 |
} |
| 56 |
|
| 57 |
void ACMStream::acm_query_formats(drv_lookup_t *s) |
| 58 |
{ |
| 59 |
ACMFORMATTAGDETAILSW *ftd = &s->ftd; |
| 60 |
ACMFORMATDETAILSW *fdw = &s->fd; |
| 61 |
|
| 62 |
// Iterate all formats |
| 63 |
for (size_t i=0, n=ftd->cStandardFormats; i < n; ++i) |
| 64 |
{ |
| 65 |
::ZeroMemory(fdw, sizeof(ACMFORMATDETAILSW)); |
| 66 |
|
| 67 |
fdw->cbStruct = sizeof(ACMFORMATDETAILSW); |
| 68 |
fdw->dwFormatTag = ftd->dwFormatTag; |
| 69 |
fdw->dwFormatIndex = i; |
| 70 |
fdw->pwfx = s->req; |
| 71 |
fdw->cbwfx = s->req_sz; |
| 72 |
|
| 73 |
if (::acmFormatDetailsW(s->hd, fdw, ACM_FORMATDETAILSF_INDEX) != 0) |
| 74 |
continue; |
| 75 |
|
| 76 |
// Create copy and add to driver list |
| 77 |
if ((s->pfmt = s->ptag->vfmt.add()) == NULL) |
| 78 |
continue; |
| 79 |
|
| 80 |
s->pfmt->id = fdw->dwFormatIndex; |
| 81 |
s->pfmt->fdw = fdw->fdwSupport; |
| 82 |
s->pfmt->wfex = copy_fmt(fdw->pwfx); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void ACMStream::acm_query_format_tags(drv_lookup_t *s) |
| 87 |
{ |
| 88 |
ACMFORMATTAGDETAILSW *ftd = &s->ftd; |
| 89 |
|
| 90 |
for (size_t i=0, n=s->dd.cFormatTags; i < n; ++i) |
| 91 |
{ |
| 92 |
// Prepare the structure |
| 93 |
::ZeroMemory(ftd, sizeof(ACMFORMATTAGDETAILSW)); |
| 94 |
ftd->cbStruct = sizeof(ACMFORMATTAGDETAILSW); |
| 95 |
ftd->dwFormatTagIndex = i; |
| 96 |
|
| 97 |
// Get information |
| 98 |
if (::acmFormatTagDetailsW(s->hd, ftd, ACM_FORMATTAGDETAILSF_INDEX) != 0) |
| 99 |
continue; |
| 100 |
|
| 101 |
// Create tag descriptor |
| 102 |
if ((s->ptag = new fmt_tag_t()) == NULL) |
| 103 |
continue; |
| 104 |
if (!s->pdrv->vtag.add(s->ptag)) |
| 105 |
{ |
| 106 |
delete s->ptag; |
| 107 |
continue; |
| 108 |
} |
| 109 |
s->ptag->id = ftd->dwFormatTag; |
| 110 |
s->ptag->fdw = ftd->fdwSupport; |
| 111 |
s->ptag->name.set_utf16(ftd->szFormatTag); |
| 112 |
|
| 113 |
// Query formats |
| 114 |
acm_query_formats(s); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
WINBOOL CALLBACK ACMStream::acm_driver_enum_cb(HACMDRIVERID hadid, DWORD_PTR dwInstance, DWORD fdwSupport) |
| 119 |
{ |
| 120 |
drv_lookup_t *s = reinterpret_cast<drv_lookup_t *>(dwInstance); |
| 121 |
|
| 122 |
// Get driver information |
| 123 |
ACMDRIVERDETAILSW *dd = &s->dd; |
| 124 |
::ZeroMemory(dd, sizeof(ACMDRIVERDETAILSW)); |
| 125 |
dd->cbStruct = sizeof(ACMDRIVERDETAILSW); |
| 126 |
if (::acmDriverDetailsW(hadid, dd, 0) != 0) |
| 127 |
return TRUE; |
| 128 |
|
| 129 |
// Open driver |
| 130 |
if (::acmDriverOpen(&s->hd, hadid, 0) != 0) |
| 131 |
return TRUE; |
| 132 |
|
| 133 |
// Allocate driver object |
| 134 |
if ((s->pdrv = new drv_t()) == NULL) |
| 135 |
{ |
| 136 |
::acmDriverClose(s->hd, 0); |
| 137 |
return TRUE; |
| 138 |
} |
| 139 |
if (!s->vdrv.add(s->pdrv)) |
| 140 |
{ |
| 141 |
delete s->pdrv; |
| 142 |
::acmDriverClose(s->hd, 0); |
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
s->pdrv->drv_id = hadid; |
| 146 |
s->pdrv->short_name.set_utf16(dd->szShortName); |
| 147 |
s->pdrv->full_name.set_utf16(dd->szLongName); |
| 148 |
s->pdrv->copyright.set_utf16(dd->szCopyright); |
| 149 |
s->pdrv->license.set_utf16(dd->szLicensing); |
| 150 |
s->pdrv->features.set_utf16(dd->szFeatures); |
| 151 |
|
| 152 |
// Query format metrics |
| 153 |
s->req_sz = 0; |
| 154 |
::acmMetrics((HACMOBJ)s->hd, ACM_METRIC_MAX_SIZE_FORMAT, &s->req_sz); |
| 155 |
s->req_sz = align_size(s->req_sz, DEFAULT_ALIGN); |
| 156 |
s->pdrv->pwfx_sz = s->req_sz; |
| 157 |
s->req = reinterpret_cast<WAVEFORMATEX *>(::malloc(s->req_sz)); |
| 158 |
if (s->req != NULL) |
| 159 |
{ |
| 160 |
// Query format tags |
| 161 |
acm_query_format_tags(s); |
| 162 |
|
| 163 |
::free(s->req); |
| 164 |
s->req = NULL; |
| 165 |
} |
| 166 |
|
| 167 |
// Close driver |
| 168 |
::acmDriverClose(s->hd, 0); |
| 169 |
s->hd = NULL; |
| 170 |
|
| 171 |
return TRUE; |
| 172 |
} |
| 173 |
|
| 174 |
status_t ACMStream::acm_enum_drivers(lltl::parray<drv_t> *res) |
| 175 |
{ |
| 176 |
drv_lookup_t lk; |
| 177 |
MMRESULT result = ::acmDriverEnum(acm_driver_enum_cb, (DWORD_PTR)&lk, ACM_DRIVERENUMF_DISABLED); |
| 178 |
if (result == 0) |
| 179 |
{ |
| 180 |
res->swap(&lk.vdrv); |
| 181 |
acm_destroy_drivers(&lk.vdrv); |
| 182 |
} |
| 183 |
|
| 184 |
#if 0 |
| 185 |
for (size_t i=0, n=res->size(); i<n; ++i) |
| 186 |
{ |
| 187 |
#ifdef LSP_TRACE |
| 188 |
LSPString info, idx; |
| 189 |
drv_t *drv = res->uget(i); |
| 190 |
info.fmt_utf8( |
| 191 |
"DRV #%-4d : %p\n" |
| 192 |
"Short Name: %s\n" |
| 193 |
"Long Name : %s\n" |
| 194 |
"Copyright : %s\n" |
| 195 |
"License : %s\n" |
| 196 |
"Features : %s\n" |
| 197 |
"Formats :\n", |
| 198 |
int(i), |
| 199 |
drv->drv_id, |
| 200 |
drv->short_name.get_utf8(), |
| 201 |
drv->full_name.get_utf8(), |
| 202 |
drv->copyright.get_utf8(), |
| 203 |
drv->license.get_utf8(), |
| 204 |
drv->features.get_utf8() |
| 205 |
); |
| 206 |
for (size_t j=0, m=drv->vtag.size(); j<m; ++j) |
| 207 |
{ |
| 208 |
fmt_tag_t *tag = drv->vtag.uget(j); |
| 209 |
info.fmt_append_utf8(" %s [0x%x]: %d items\n", |
| 210 |
tag->name.get_utf8(), |
| 211 |
int(tag->id), |
| 212 |
int(tag->vfmt.size()) |
| 213 |
); |
| 214 |
|
| 215 |
for (size_t k=0, l=tag->vfmt.size(); k<l; ++k) |
| 216 |
{ |
| 217 |
fmt_t *fmt = tag->vfmt.uget(k); |
| 218 |
idx.fmt_ascii("%d/%d", int(k), int(j)); |
| 219 |
info.fmt_append_utf8( |
| 220 |
"%10s: %s %d Hz/%d ch @%d bit [0x%x:%d]\n", |
| 221 |
idx.get_utf8(), |
| 222 |
tag->name.get_utf8(), |
| 223 |
int(fmt->wfex->nSamplesPerSec), |
| 224 |
int(fmt->wfex->nChannels), |
| 225 |
int(fmt->wfex->wBitsPerSample), |
| 226 |
int(fmt->wfex->wFormatTag), |
| 227 |
int(fmt->wfex->cbSize) |
| 228 |
); |
| 229 |
} |
| 230 |
} |
| 231 |
lsp_trace("\n%s\n", info.get_native()); |
| 232 |
#endif |
| 233 |
} |
| 234 |
#endif |
| 235 |
|
| 236 |
return (result == 0) ? STATUS_OK : STATUS_UNKNOWN_ERR; |
| 237 |
} |
| 238 |
|
| 239 |
void ACMStream::acm_destroy_drivers(lltl::parray<drv_t> *res) |
| 240 |
{ |
| 241 |
// Drop drivers |
| 242 |
for (size_t i=0, n=res->size(); i<n; ++i) |
| 243 |
{ |
| 244 |
drv_t *pdrv = res->uget(i); |
| 245 |
|
| 246 |
// Drop tags |
| 247 |
for (size_t j=0, m=pdrv->vtag.size(); j<m; ++j) |
| 248 |
{ |
| 249 |
fmt_tag_t *ptag = pdrv->vtag.uget(j); |
| 250 |
// Drop formats |
| 251 |
for (size_t k=0, l=ptag->vfmt.size(); k<l; ++k) |
| 252 |
{ |
| 253 |
fmt_t *pfmt = ptag->vfmt.uget(k); |
| 254 |
if (pfmt->wfex != NULL) |
| 255 |
{ |
| 256 |
::free(pfmt->wfex); |
| 257 |
pfmt->wfex = NULL; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
delete ptag; |
| 262 |
} |
| 263 |
|
| 264 |
delete pdrv; |
| 265 |
} |
| 266 |
|
| 267 |
res->flush(); |
| 268 |
} |
| 269 |
|
| 270 |
WAVEFORMATEX *ACMStream::copy_fmt(const WAVEFORMATEX *src) |
| 271 |
{ |
| 272 |
/* |
| 273 |
* cbSize |
| 274 |
* Size, in bytes, of extra format information appended to the end |
| 275 |
* of the WAVEFORMATEX structure. This information can be used by |
| 276 |
* non-PCM formats to store extra attributes for the wFormatTag. If |
| 277 |
* no extra information is required by the wFormatTag, this member must |
| 278 |
* be set to 0. For WAVE_FORMAT_PCM formats (and only WAVE_FORMAT_PCM formats), |
| 279 |
* this member is ignored. When this structure is included in a WAVEFORMATEXTENSIBLE |
| 280 |
* structure, this value must be at least 22. |
| 281 |
*/ |
| 282 |
size_t cbsize = (src->wFormatTag == WAVE_FORMAT_PCM) ? 0 : src->cbSize; |
| 283 |
size_t bytes = sizeof(WAVEFORMATEX) + cbsize; |
| 284 |
WAVEFORMATEX *res = static_cast<WAVEFORMATEX *>(::malloc(align_size(bytes, DEFAULT_ALIGN))); |
| 285 |
if (res != NULL) |
| 286 |
{ |
| 287 |
::memcpy(res, src, bytes); |
| 288 |
res->cbSize = cbsize; // Override original cbSize for PCM |
| 289 |
} |
| 290 |
return res; |
| 291 |
} |
| 292 |
|
| 293 |
ACMStream::fmt_tag_t *ACMStream::acm_find_tag(drv_t *drv, size_t fmt_tag) |
| 294 |
{ |
| 295 |
for (size_t i=0, n=drv->vtag.size(); i<n; ++i) |
| 296 |
{ |
| 297 |
fmt_tag_t *tag = drv->vtag.uget(i); |
| 298 |
if (tag == NULL) |
| 299 |
continue; |
| 300 |
if ((tag->id == fmt_tag) && (tag->fdw & ACMDRIVERDETAILS_SUPPORTF_CODEC)) |
| 301 |
return tag; |
| 302 |
} |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
status_t ACMStream::acm_configure_stream(WAVEFORMATEX *dst, WAVEFORMATEX *src) |
| 307 |
{ |
| 308 |
MMRESULT res; |
| 309 |
|
| 310 |
// Succeeded, initialize stream |
| 311 |
// Estimate size of conversion buffers |
| 312 |
DWORD src_length = IO_BUF_SIZE, dst_length = IO_BUF_SIZE; |
| 313 |
if ((dst->wFormatTag == WAVE_FORMAT_PCM) || (dst->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)) |
| 314 |
{ |
| 315 |
res = ::acmStreamSize(hStream, IO_BUF_SIZE, &dst_length, ACM_STREAMSIZEF_SOURCE); |
| 316 |
if ((res != 0) || (dst_length <= 0)) |
| 317 |
return STATUS_SKIP; |
| 318 |
|
| 319 |
res = ::acmStreamSize(hStream, dst_length, &src_length, ACM_STREAMSIZEF_DESTINATION); |
| 320 |
if ((res != 0) || (src_length <= 0)) |
| 321 |
return STATUS_SKIP; |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
res = ::acmStreamSize(hStream, IO_BUF_SIZE, &src_length, ACM_STREAMSIZEF_SOURCE); |
| 326 |
if ((res != 0) || (src_length <= 0)) |
| 327 |
return STATUS_SKIP; |
| 328 |
|
| 329 |
res = ::acmStreamSize(hStream, src_length, &dst_length, ACM_STREAMSIZEF_DESTINATION); |
| 330 |
if ((res != 0) || (src_length <= 0)) |
| 331 |
return STATUS_SKIP; |
| 332 |
} |
| 333 |
|
| 334 |
// Allocate stream header |
| 335 |
size_t hdr_alloc = align_size(sizeof(ACMSTREAMHEADER), DEFAULT_ALIGN); |
| 336 |
size_t src_alloc = align_size(src_length, DEFAULT_ALIGN); |
| 337 |
size_t dst_alloc = align_size(dst_length, DEFAULT_ALIGN); |
| 338 |
size_t to_alloc = hdr_alloc + src_alloc + dst_alloc; |
| 339 |
BYTE *buf = static_cast<BYTE *>(::malloc(to_alloc)); |
| 340 |
if (buf == NULL) |
| 341 |
return STATUS_NO_MEM; |
| 342 |
|
| 343 |
::ZeroMemory(buf, to_alloc); |
| 344 |
pHeader = reinterpret_cast<ACMSTREAMHEADER *>(buf); |
| 345 |
buf += hdr_alloc; |
| 346 |
pHeader->pbSrc = buf; |
| 347 |
buf += src_alloc; |
| 348 |
pHeader->pbDst = buf; |
| 349 |
buf += dst_alloc; |
| 350 |
|
| 351 |
pHeader->cbStruct = sizeof(ACMSTREAMHEADER); |
| 352 |
pHeader->dwSrcUser = src_length; // maximum size |
| 353 |
pHeader->cbSrcLength = src_length; // This is required for initialization |
| 354 |
pHeader->cbSrcLengthUsed = 0; |
| 355 |
pHeader->dwDstUser = 0; // read offset |
| 356 |
pHeader->cbDstLength = dst_length; |
| 357 |
pHeader->cbDstLengthUsed = 0; |
| 358 |
|
| 359 |
// Prepare stream header |
| 360 |
if ((res = ::acmStreamPrepareHeader(hStream, pHeader, 0)) != 0) |
| 361 |
return STATUS_UNKNOWN_ERR; |
| 362 |
|
| 363 |
// Reset size of input buffer |
| 364 |
pHeader->cbSrcLength = 0; |
| 365 |
|
| 366 |
return STATUS_OK; |
| 367 |
} |
| 368 |
|
| 369 |
status_t ACMStream::acm_try_format_dec(WAVEFORMATEX *to, WAVEFORMATEX *from) |
| 370 |
{ |
| 371 |
status_t res; |
| 372 |
size_t ftag = ((to->wFormatTag == WAVE_FORMAT_PCM) || (to->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)) ? |
| 373 |
from->wFormatTag : to->wFormatTag; |
| 374 |
|
| 375 |
for (size_t i=0, n=vDrv.size(); i<n; ++i) |
| 376 |
{ |
| 377 |
// Work with drivers that ONLY support the requested tag |
| 378 |
drv_t *drv = vDrv.uget(i); |
| 379 |
if (drv == NULL) |
| 380 |
continue; |
| 381 |
fmt_tag_t *tag = acm_find_tag(drv, ftag); |
| 382 |
if (tag == NULL) |
| 383 |
continue; |
| 384 |
|
| 385 |
// Open the specific ACM driver |
| 386 |
if (::acmDriverOpen(&hDriver, drv->drv_id, 0) != 0) |
| 387 |
continue; |
| 388 |
|
| 389 |
// Try to open stream |
| 390 |
if (::acmStreamOpen(&hStream, hDriver, from, to, NULL, 0, 0, ACM_STREAMOPENF_NONREALTIME) == 0) |
| 391 |
{ |
| 392 |
res = acm_configure_stream(to, from); |
| 393 |
if (res != STATUS_OK) |
| 394 |
return res; |
| 395 |
|
| 396 |
// Copy format descriptors |
| 397 |
if ((pFmtIn = copy_fmt(from)) == NULL) |
| 398 |
return STATUS_NO_MEM; |
| 399 |
if ((pFmtOut = copy_fmt(to)) == NULL) |
| 400 |
return STATUS_NO_MEM; |
| 401 |
|
| 402 |
return STATUS_OK; |
| 403 |
} |
| 404 |
|
| 405 |
// Close ACM driver |
| 406 |
::acmDriverClose(hDriver, 0); |
| 407 |
hDriver = NULL; |
| 408 |
} |
| 409 |
|
| 410 |
return STATUS_OK; |
| 411 |
} |
| 412 |
|
| 413 |
ACMStream::fmt_t *ACMStream::acm_select_format(fmt_t *best, fmt_t *curr, WAVEFORMATEX *req) |
| 414 |
{ |
| 415 |
if (curr == NULL) |
| 416 |
return best; |
| 417 |
if (best == NULL) |
| 418 |
return curr; |
| 419 |
|
| 420 |
// Minimize sample rate difference |
| 421 |
size_t d1 = lsp_abs(ssize_t(best->wfex->nSamplesPerSec) - ssize_t(req->nSamplesPerSec)); |
| 422 |
size_t d2 = lsp_abs(ssize_t(curr->wfex->nSamplesPerSec) - ssize_t(req->nSamplesPerSec)); |
| 423 |
if ((d2 < d1) && |
| 424 |
(curr->wfex->nSamplesPerSec > best->wfex->nSamplesPerSec) && |
| 425 |
(curr->wfex->nSamplesPerSec <= req->nSamplesPerSec)) |
| 426 |
return curr; |
| 427 |
|
| 428 |
// Optimize number of channels |
| 429 |
d1 = lsp_abs(ssize_t(best->wfex->nChannels) - ssize_t(req->nChannels)); |
| 430 |
d2 = lsp_abs(ssize_t(curr->wfex->nChannels) - ssize_t(req->nChannels)); |
| 431 |
if (d1 > d2) |
| 432 |
return curr; |
| 433 |
|
| 434 |
// Optimize sample format |
| 435 |
if ((best->wfex->wFormatTag != WAVE_FORMAT_IEEE_FLOAT) && |
| 436 |
(curr->wfex->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)) |
| 437 |
return curr; |
| 438 |
if (best->wfex->wBitsPerSample < curr->wfex->wBitsPerSample) |
| 439 |
return curr; |
| 440 |
|
| 441 |
return best; |
| 442 |
} |
| 443 |
|
| 444 |
status_t ACMStream::acm_find_standard_dec(WAVEFORMATEX *fmt) |
| 445 |
{ |
| 446 |
status_t res; |
| 447 |
lltl::parray<fmt_t> blist; // List of all best-matching solutions |
| 448 |
fmt_t *best = NULL; |
| 449 |
|
| 450 |
for (size_t i=0, n=vDrv.size(); i<n; ++i) |
| 451 |
{ |
| 452 |
// Work with drivers that ONLY support the requested tag |
| 453 |
drv_t *drv = vDrv.uget(i); |
| 454 |
if (drv == NULL) |
| 455 |
continue; |
| 456 |
fmt_tag_t *tag = acm_find_tag(drv, fmt->wFormatTag); |
| 457 |
if (tag == NULL) |
| 458 |
continue; |
| 459 |
|
| 460 |
best = NULL; |
| 461 |
|
| 462 |
// Possible conversion to floating-point PCM ? |
| 463 |
if ((tag = acm_find_tag(drv, WAVE_FORMAT_IEEE_FLOAT)) != NULL) |
| 464 |
{ |
| 465 |
for (size_t j=0, m=tag->vfmt.size(); j<m; ++j) |
| 466 |
best = acm_select_format(best, tag->vfmt.uget(j), fmt); |
| 467 |
} |
| 468 |
|
| 469 |
// Possible conversion to integer PCM ? |
| 470 |
if ((tag = acm_find_tag(drv, WAVE_FORMAT_PCM)) != NULL) |
| 471 |
{ |
| 472 |
for (size_t j=0, m=tag->vfmt.size(); j<m; ++j) |
| 473 |
best = acm_select_format(best, tag->vfmt.uget(j), fmt); |
| 474 |
} |
| 475 |
|
| 476 |
// Add to list |
| 477 |
if (best != NULL) |
| 478 |
blist.add(best); |
| 479 |
} |
| 480 |
|
| 481 |
// Select the best matching format across all and make it first for test |
| 482 |
best = NULL; |
| 483 |
for (size_t j=0, m=blist.size(); j<m; ++j) |
| 484 |
best = acm_select_format(best, blist.uget(j), fmt); |
| 485 |
if (best != NULL) |
| 486 |
{ |
| 487 |
size_t idx = blist.index_of(best); |
| 488 |
if (idx != 0) |
| 489 |
blist.xswap(0, idx); |
| 490 |
} |
| 491 |
|
| 492 |
// Try each format |
| 493 |
for (size_t i=0, n=blist.size(); i<n; ++i) |
| 494 |
{ |
| 495 |
fmt_t *curr = blist.uget(i); |
| 496 |
WAVEFORMATEX *pcm = (curr != NULL) ? curr->wfex : NULL; |
| 497 |
if (pcm == NULL) |
| 498 |
continue; |
| 499 |
|
| 500 |
if ((pcm->nChannels != fmt->nChannels) || |
| 501 |
(pcm->nSamplesPerSec != fmt->nSamplesPerSec)) |
| 502 |
{ |
| 503 |
WAVEFORMATEX tmp = *pcm; |
| 504 |
tmp.nChannels = fmt->nChannels; |
| 505 |
tmp.nSamplesPerSec = fmt->nSamplesPerSec; |
| 506 |
tmp.nBlockAlign = (tmp.wBitsPerSample * tmp.nChannels) >> 3; |
| 507 |
tmp.nAvgBytesPerSec = tmp.nBlockAlign * tmp.nSamplesPerSec; |
| 508 |
|
| 509 |
// Try modified mode |
| 510 |
if ((res = acm_try_format_dec(pcm, fmt)) != STATUS_OK) |
| 511 |
return res; |
| 512 |
if (hStream != NULL) |
| 513 |
return STATUS_OK; |
| 514 |
} |
| 515 |
|
| 516 |
// Try non-modified mode |
| 517 |
if ((res = acm_try_format_dec(pcm, fmt)) != STATUS_OK) |
| 518 |
return res; |
| 519 |
if (hStream != NULL) |
| 520 |
return STATUS_OK; |
| 521 |
} |
| 522 |
|
| 523 |
return STATUS_OK; |
| 524 |
} |
| 525 |
|
| 526 |
status_t ACMStream::acm_find_nonstandard_dec(WAVEFORMATEX *fmt) |
| 527 |
{ |
| 528 |
status_t res; |
| 529 |
WAVEFORMATEX pcm; |
| 530 |
|
| 531 |
pcm.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; |
| 532 |
pcm.nChannels = fmt->nChannels; |
| 533 |
pcm.nSamplesPerSec = fmt->nSamplesPerSec; |
| 534 |
pcm.nAvgBytesPerSec = sizeof(f32_t) * fmt->nChannels * fmt->nSamplesPerSec; |
| 535 |
pcm.nBlockAlign = sizeof(f32_t) * fmt->nChannels; |
| 536 |
pcm.wBitsPerSample = sizeof(f32_t) * 8; |
| 537 |
pcm.cbSize = 0; |
| 538 |
|
| 539 |
// Try IEEE Float first as the best output format |
| 540 |
if ((res = acm_try_format_dec(&pcm, fmt)) != STATUS_OK) |
| 541 |
return res; |
| 542 |
else if (hStream != NULL) |
| 543 |
return STATUS_OK; |
| 544 |
|
| 545 |
// No success, try 32, 24, 16, 8 bits per sample |
| 546 |
for (size_t bps=4; bps > 0; --bps) |
| 547 |
{ |
| 548 |
pcm.wFormatTag = WAVE_FORMAT_PCM; |
| 549 |
pcm.nChannels = fmt->nChannels; |
| 550 |
pcm.nSamplesPerSec = fmt->nSamplesPerSec; |
| 551 |
pcm.nAvgBytesPerSec = bps * fmt->nChannels * fmt->nSamplesPerSec; |
| 552 |
pcm.nBlockAlign = bps * fmt->nChannels; |
| 553 |
pcm.wBitsPerSample = bps * 8; |
| 554 |
pcm.cbSize = 0; |
| 555 |
|
| 556 |
// Try non-standard modes |
| 557 |
if ((res = acm_try_format_dec(&pcm, fmt)) != STATUS_OK) |
| 558 |
return res; |
| 559 |
if (hStream != NULL) |
| 560 |
return STATUS_OK; |
| 561 |
} |
| 562 |
|
| 563 |
return STATUS_OK; |
| 564 |
} |
| 565 |
|
| 566 |
status_t ACMStream::read_pcm(WAVEFORMATEX *in) |
| 567 |
{ |
| 568 |
status_t res = close(); |
| 569 |
if (res != STATUS_OK) |
| 570 |
return res; |
| 571 |
|
| 572 |
// We do not support PCM and IEEE FLOAT format since it doesn't require decoding/encoding |
| 573 |
if ((in->wFormatTag == WAVE_FORMAT_PCM) || (in->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)) |
| 574 |
return STATUS_UNSUPPORTED_FORMAT; |
| 575 |
|
| 576 |
// Enumerate all drivers available in the system |
| 577 |
acm_enum_drivers(&vDrv); |
| 578 |
|
| 579 |
if ((res = acm_find_nonstandard_dec(in)) != STATUS_OK) |
| 580 |
return close(res); |
| 581 |
if (hStream == NULL) |
| 582 |
{ |
| 583 |
if ((res = acm_find_standard_dec(in)) != STATUS_OK) |
| 584 |
return close(res); |
| 585 |
} |
| 586 |
|
| 587 |
acm_destroy_drivers(&vDrv); |
| 588 |
|
| 589 |
return (hStream != NULL) ? STATUS_OK : close(STATUS_UNSUPPORTED_FORMAT); |
| 590 |
} |
| 591 |
|
| 592 |
status_t ACMStream::acm_try_format_enc(HACMDRIVER drv, WAVEFORMATEX *to, WAVEFORMATEX *from, size_t szof) |
| 593 |
{ |
| 594 |
size_t flags = ACM_FORMATSUGGESTF_NCHANNELS | |
| 595 |
ACM_FORMATSUGGESTF_NSAMPLESPERSEC | |
| 596 |
ACM_FORMATSUGGESTF_WFORMATTAG; |
| 597 |
|
| 598 |
// Suggest format |
| 599 |
if ((::acmFormatSuggest(drv, from, to, szof, flags)) != 0) |
| 600 |
return STATUS_OK; |
| 601 |
|
| 602 |
// Try to open stream |
| 603 |
if (::acmStreamOpen(&hStream, hDriver, from, to, NULL, 0, 0, ACM_STREAMOPENF_NONREALTIME) != 0) |
| 604 |
return STATUS_OK; |
| 605 |
|
| 606 |
// Configure ACM stream |
| 607 |
status_t res = acm_configure_stream(to, from); |
| 608 |
if (res != STATUS_OK) |
| 609 |
return res; |
| 610 |
|
| 611 |
// Copy format descriptors |
| 612 |
if ((pFmtIn = copy_fmt(from)) == NULL) |
| 613 |
return STATUS_NO_MEM; |
| 614 |
pFmtOut = to; // reuse 'to' pointer; |
| 615 |
|
| 616 |
return STATUS_OK; |
| 617 |
} |
| 618 |
|
| 619 |
status_t ACMStream::acm_suggest_format_enc(WAVEFORMATEX *fmt) |
| 620 |
{ |
| 621 |
status_t res; |
| 622 |
|
| 623 |
for (size_t i=0, n=vDrv.size(); i<n; ++i) |
| 624 |
{ |
| 625 |
// Work with drivers that ONLY support the requested tag |
| 626 |
drv_t *drv = vDrv.uget(i); |
| 627 |
if (drv == NULL) |
| 628 |
continue; |
| 629 |
fmt_tag_t *tag = acm_find_tag(drv, fmt->wFormatTag); |
| 630 |
if (tag == NULL) |
| 631 |
continue; |
| 632 |
|
| 633 |
WAVEFORMATEX pcm, *dst; |
| 634 |
dst = reinterpret_cast<WAVEFORMATEX *>(::malloc(drv->pwfx_sz)); |
| 635 |
if (dst == NULL) |
| 636 |
return STATUS_NO_MEM; |
| 637 |
::ZeroMemory(dst, drv->pwfx_sz); |
| 638 |
dst->wFormatTag = fmt->wFormatTag; |
| 639 |
dst->nChannels = fmt->nChannels; |
| 640 |
dst->nSamplesPerSec = fmt->nSamplesPerSec; |
| 641 |
dst->cbSize = 0; |
| 642 |
|
| 643 |
// Open the specific ACM driver |
| 644 |
if (::acmDriverOpen(&hDriver, drv->drv_id, 0) != 0) |
| 645 |
{ |
| 646 |
::free(dst); |
| 647 |
continue; |
| 648 |
} |
| 649 |
|
| 650 |
// Try PCM IIIE Float first |
| 651 |
pcm.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; |
| 652 |
pcm.nChannels = fmt->nChannels; |
| 653 |
pcm.nSamplesPerSec = fmt->nSamplesPerSec; |
| 654 |
pcm.nAvgBytesPerSec = sizeof(f32_t) * fmt->nChannels * fmt->nSamplesPerSec; |
| 655 |
pcm.nBlockAlign = sizeof(f32_t) * fmt->nChannels; |
| 656 |
pcm.wBitsPerSample = sizeof(f32_t) * 8; |
| 657 |
pcm.cbSize = 0; |
| 658 |
|
| 659 |
if ((res = acm_try_format_enc(hDriver, dst, &pcm, drv->pwfx_sz)) != STATUS_OK) |
| 660 |
{ |
| 661 |
::free(dst); |
| 662 |
return res; |
| 663 |
} |
| 664 |
else if (hStream != NULL) |
| 665 |
return STATUS_OK; // dst will be saved as format descriptor |
| 666 |
|
| 667 |
// Try other PCM formats: 32, 24, 16, 8 bits per sample |
| 668 |
for (size_t bps=4; bps > 0; --bps) |
| 669 |
{ |
| 670 |
pcm.wFormatTag = WAVE_FORMAT_PCM; |
| 671 |
pcm.nChannels = fmt->nChannels; |
| 672 |
pcm.nSamplesPerSec = fmt->nSamplesPerSec; |
| 673 |
pcm.nAvgBytesPerSec = bps * fmt->nChannels * fmt->nSamplesPerSec; |
| 674 |
pcm.nBlockAlign = bps * fmt->nChannels; |
| 675 |
pcm.wBitsPerSample = bps * 8; |
| 676 |
pcm.cbSize = 0; |
| 677 |
|
| 678 |
// Try non-standard modes |
| 679 |
if ((res = acm_try_format_enc(hDriver, dst, &pcm, drv->pwfx_sz)) != STATUS_OK) |
| 680 |
{ |
| 681 |
::free(dst); |
| 682 |
return res; |
| 683 |
} |
| 684 |
if (hStream != NULL) |
| 685 |
return STATUS_OK; // dst will be saved as format descriptor |
| 686 |
} |
| 687 |
|
| 688 |
// Close ACM driver |
| 689 |
::free(dst); |
| 690 |
::acmDriverClose(hDriver, 0); |
| 691 |
hDriver = NULL; |
| 692 |
} |
| 693 |
|
| 694 |
return STATUS_OK; |
| 695 |
} |
| 696 |
|
| 697 |
status_t ACMStream::write_pcm(WAVEFORMATEX *out) |
| 698 |
{ |
| 699 |
status_t res = close(); |
| 700 |
if (res != STATUS_OK) |
| 701 |
return res; |
| 702 |
|
| 703 |
// Enumerate all drivers available in the system |
| 704 |
acm_enum_drivers(&vDrv); |
| 705 |
|
| 706 |
if ((res = acm_suggest_format_enc(out)) != STATUS_OK) |
| 707 |
return close(res); |
| 708 |
|
| 709 |
return (hStream != NULL) ? STATUS_OK : close(STATUS_UNSUPPORTED_FORMAT); |
| 710 |
} |
| 711 |
|
| 712 |
status_t ACMStream::close(status_t res) |
| 713 |
{ |
| 714 |
// Destroy information about all drivers |
| 715 |
acm_destroy_drivers(&vDrv); |
| 716 |
|
| 717 |
// Close stream |
| 718 |
if (hStream != NULL) |
| 719 |
{ |
| 720 |
// Unprepare ACM header first |
| 721 |
if ((pHeader != NULL) && (pHeader->fdwStatus != 0)) |
| 722 |
::acmStreamUnprepareHeader(hStream, pHeader, 0); |
| 723 |
|
| 724 |
::acmStreamClose(hStream, 0); |
| 725 |
hStream = NULL; |
| 726 |
} |
| 727 |
|
| 728 |
// Close driver |
| 729 |
if (hDriver != NULL) |
| 730 |
{ |
| 731 |
::acmDriverClose(hDriver, 0); |
| 732 |
hDriver = NULL; |
| 733 |
} |
| 734 |
|
| 735 |
// Free allocated memory |
| 736 |
if (pFmtIn != NULL) |
| 737 |
{ |
| 738 |
::free(pFmtIn); |
| 739 |
pFmtIn = NULL; |
| 740 |
} |
| 741 |
|
| 742 |
if (pFmtOut != NULL) |
| 743 |
{ |
| 744 |
::free(pFmtOut); |
| 745 |
pFmtOut = NULL; |
| 746 |
} |
| 747 |
|
| 748 |
if (pHeader != NULL) |
| 749 |
{ |
| 750 |
::free(pHeader); |
| 751 |
pHeader = NULL; |
| 752 |
} |
| 753 |
|
| 754 |
return res; |
| 755 |
} |
| 756 |
|
| 757 |
ssize_t ACMStream::push(void **buf) |
| 758 |
{ |
| 759 |
if (pHeader == NULL) |
| 760 |
return -STATUS_CLOSED; |
| 761 |
size_t avail = pHeader->dwSrcUser - pHeader->cbSrcLength; |
| 762 |
if (buf != NULL) |
| 763 |
*buf = &pHeader->pbSrc[pHeader->cbSrcLength]; |
| 764 |
return avail; |
| 765 |
} |
| 766 |
|
| 767 |
ssize_t ACMStream::pull(void **buf, size_t size, bool force) |
| 768 |
{ |
| 769 |
if (hStream == NULL) |
| 770 |
return -STATUS_CLOSED; |
| 771 |
|
| 772 |
// Check if there is enough data in data buffer |
| 773 |
size_t avail = pHeader->cbDstLengthUsed - pHeader->dwDstUser; |
| 774 |
if (avail <= 0) |
| 775 |
{ |
| 776 |
// Check buffer size |
| 777 |
if (pHeader->cbSrcLength <= 0) |
| 778 |
return (force) ? -STATUS_EOF : 0; |
| 779 |
pHeader->cbSrcLengthUsed = 0; |
| 780 |
pHeader->cbDstLengthUsed = 0; |
| 781 |
// We consider that output buffer is block-aligned and can perform conversion now |
| 782 |
// Perform conversion |
| 783 |
size_t flags = (force) ? ACM_STREAMCONVERTF_BLOCKALIGN : 0; |
| 784 |
if (::acmStreamConvert(hStream, pHeader, flags) != 0) |
| 785 |
return -STATUS_IO_ERROR; |
| 786 |
|
| 787 |
// Move the source buffer |
| 788 |
size_t tail = pHeader->cbSrcLength - pHeader->cbSrcLengthUsed; |
| 789 |
pHeader->cbSrcLength = tail; |
| 790 |
if (tail > 0) |
| 791 |
::memmove(pHeader->pbSrc, &pHeader->pbSrc[pHeader->cbSrcLengthUsed], tail); |
| 792 |
|
| 793 |
// Update buffer information and check again |
| 794 |
pHeader->dwDstUser = 0; |
| 795 |
avail = pHeader->cbDstLengthUsed; |
| 796 |
if (avail <= 0) |
| 797 |
return (force) ? -STATUS_EOF : 0; |
| 798 |
} |
| 799 |
|
| 800 |
// Copy data to the output buffer |
| 801 |
if (size > avail) |
| 802 |
size = avail; |
| 803 |
if (buf != NULL) |
| 804 |
*buf = &pHeader->pbDst[pHeader->dwDstUser]; |
| 805 |
pHeader->dwDstUser += size; |
| 806 |
return size; |
| 807 |
} |
| 808 |
|
| 809 |
size_t ACMStream::avail() |
| 810 |
{ |
| 811 |
if (pHeader == NULL) |
| 812 |
return 0; |
| 813 |
return pHeader->cbDstLengthUsed; |
| 814 |
} |
| 815 |
|
| 816 |
} /* namespace mm */ |
| 817 |
} /* namespace lsp */ |
| 818 |
|
| 819 |
#endif /* PLATFORM_WINDOWS */ |