4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (C) 4Front Technologies 1996-2008.
24 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Purpose: Virtual mixing audio output routines
30 * This file contains the actual mixing and resampling engine for output.
34 #include <sys/sunddi.h>
35 #include <sys/sysmacros.h>
36 #include "audio_impl.h"
38 #define DECL_AUDIO_EXPORT(NAME, TYPE, SAMPLE) \
40 auimpl_export_##NAME(audio_engine_t *eng, uint_t nfr, uint_t froff) \
42 int nch = eng->e_nchan; \
43 uint_t hidx = eng->e_hidx; \
44 TYPE *out = (void *)eng->e_data; \
47 do { /* for each channel */ \
51 int incr = eng->e_chincr[ch]; \
53 /* get value and adjust next channel offset */ \
54 op = out + eng->e_choffs[ch] + (hidx * incr); \
55 ip = eng->e_chbufs[ch]; \
60 do { /* for each frame */ \
61 int32_t sample = *ip; \
73 DECL_AUDIO_EXPORT(16ne
, int16_t, sample
>> 8)
74 DECL_AUDIO_EXPORT(16oe
, int16_t, ddi_swap16(sample
>> 8))
75 DECL_AUDIO_EXPORT(32ne
, int32_t, sample
<< 8)
76 DECL_AUDIO_EXPORT(32oe
, int32_t, ddi_swap32(sample
<< 8))
77 DECL_AUDIO_EXPORT(24ne
, int32_t, sample
)
78 DECL_AUDIO_EXPORT(24oe
, int32_t, ddi_swap32(sample
))
81 * Simple limiter to prevent overflows when using fixed point computations
84 auimpl_output_limiter(audio_engine_t
*eng
)
88 int nchan
= eng
->e_nchan
;
89 uint_t fragfr
= eng
->e_fragfr
;
90 int32_t **chbufs
= eng
->e_chbufs
;
91 uint_t statevar
= eng
->e_limiter_state
;
93 for (t
= 0; t
< fragfr
; t
++) {
95 amp
= (uint_t
)ABS(chbufs
[0][t
]);
97 for (k
= 1; k
< nchan
; k
++) {
98 amp2
= (uint_t
)ABS(chbufs
[k
][t
]);
107 q
= 0x7FFF0000 / amp
;
115 * Simplier (linear) tracking algo
116 * (gives less distortion, but more pumping)
119 if (statevar
> 0x10000)
123 * Classic tracking algo
124 * gives more distortion with no-lookahead
125 * statevar=0x10000-((0x10000-statevar)*0xFFF4>>16);
129 for (k
= 0; k
< nchan
; k
++) {
130 int32_t in
= chbufs
[k
][t
];
136 p
= ((p
& 0xFFFF) * (q
>> 4) >> 12) +
141 p
= ((p
& 0xFFFF) * (q
>> 4) >> 12) +
147 * if output after limiter is clamped, then it
152 else if (out
< -0x7FFFFF)
159 eng
->e_limiter_state
= statevar
;
163 * Output mixing function. Assumption: all work is done in 24-bit native PCM.
166 auimpl_output_mix(audio_stream_t
*sp
, int offset
, int nfr
)
168 audio_engine_t
*eng
= sp
->s_engine
;
179 choffs
= sp
->s_choffs
;
180 nch
= sp
->s_cnv_dst_nchan
;
181 vol
= sp
->s_gain_eff
;
184 * Do the mixing. We de-interleave the source stream at the
187 for (int ch
= 0; ch
< nch
; ch
++) {
193 op
= eng
->e_chbufs
[ch
+ choffs
];
196 for (int i
= nfr
; i
; i
--) {
202 samp
/= AUDIO_VOL_SCALE
;
205 *op
+= (int32_t)samp
;
210 sp
->s_cnv_cnt
-= nfr
;
211 sp
->s_cnv_ptr
+= (nch
* nfr
);
215 * Consume a fragment's worth of data. This is called when the data in
216 * the conversion buffer is exhausted, and we need to refill it from the
217 * source buffer. We always consume data from the client in quantities of
218 * a fragment at a time (assuming that a fragment is available.)
221 auimpl_consume_fragment(audio_stream_t
*sp
)
230 sp
->s_cnv_src
= sp
->s_cnv_buf0
;
231 sp
->s_cnv_dst
= sp
->s_cnv_buf1
;
233 fragfr
= sp
->s_fragfr
;
234 nframes
= sp
->s_nframes
;
235 framesz
= sp
->s_framesz
;
237 ASSERT(sp
->s_head
>= sp
->s_tail
);
239 avail
= sp
->s_head
- sp
->s_tail
;
240 cnvbuf
= sp
->s_cnv_src
;
242 count
= min(avail
, fragfr
);
245 * Copy data. We deal properly with wraps. Done as a
246 * do...while to minimize the number of tests.
252 n
= min(nframes
- sp
->s_tidx
, count
);
253 nbytes
= framesz
* n
;
254 bcopy(sp
->s_data
+ (sp
->s_tidx
* framesz
), cnvbuf
, nbytes
);
260 if (sp
->s_tidx
>= nframes
)
261 sp
->s_tidx
-= nframes
;
264 /* Note: data conversion is optional! */
265 count
= min(avail
, fragfr
);
266 if (sp
->s_converter
!= NULL
) {
267 sp
->s_cnv_cnt
= sp
->s_converter(sp
, count
);
269 sp
->s_cnv_cnt
= count
;
274 auimpl_output_callback_impl(audio_engine_t
*eng
, audio_client_t
**output
,
275 audio_client_t
**drain
)
277 uint_t fragfr
= eng
->e_fragfr
;
280 /* clear any preexisting mix results */
281 for (int i
= 0; i
< eng
->e_nchan
; i
++)
282 bzero(eng
->e_chbufs
[i
], AUDIO_CHBUFS
* sizeof (int32_t));
284 for (audio_stream_t
*sp
= list_head(&eng
->e_streams
);
286 sp
= list_next(&eng
->e_streams
, sp
)) {
292 boolean_t drained
= B_FALSE
;
293 audio_client_t
*c
= sp
->s_client
;
296 * We need/want a full fragment. If the client has
297 * less than that available, it will cause a client
298 * underrun in auimpl_consume_fragment, but in such a
299 * case we should get silence bytes. Assignments done
300 * ahead of the lock to minimize lock contention.
305 mutex_enter(&sp
->s_lock
);
306 /* skip over streams not running or paused */
307 if ((!sp
->s_running
) || (sp
->s_paused
)) {
308 mutex_exit(&sp
->s_lock
);
313 /* make sure we have data to chew on */
314 if ((avail
= sp
->s_cnv_cnt
) == 0) {
315 auimpl_consume_fragment(sp
);
316 sp
->s_cnv_ptr
= sp
->s_cnv_src
;
317 avail
= sp
->s_cnv_cnt
;
321 * We might have got more data than we need
322 * right now. (E.g. 8kHz expanding to 48kHz.)
323 * Take only what we need.
325 used
= min(avail
, need
);
328 * Mix the results, as much data as we can use
331 auimpl_output_mix(sp
, offset
, used
);
334 * Save the offset for the next round, so we don't
335 * remix into the same location.
340 * Okay, we mixed some data, but it might not
341 * have been all we need. This can happen
342 * either because we just mixed up some
343 * partial/residual data, or because the
344 * client has a fragment size which expands to
345 * less than a full fragment for us. (Such as
346 * a client wanting to operate at a higher
347 * data rate than the engine.)
351 } while (need
&& avail
);
354 /* underrun or end of data */
355 if (sp
->s_draining
) {
356 if (sp
->s_drain_idx
== 0) {
357 sp
->s_drain_idx
= eng
->e_head
;
359 if (eng
->e_tail
>= sp
->s_drain_idx
) {
361 sp
->s_draining
= B_FALSE
;
363 * After draining, stop the
364 * stream cleanly. This
365 * prevents underrun errors.
367 * (Stream will auto-start if
368 * client submits more data to
371 * AC3: When an AC3 stream
372 * drains we should probably
373 * stop the actual hardware
376 ASSERT(mutex_owned(&eng
->e_lock
));
377 sp
->s_running
= B_FALSE
;
381 sp
->s_errors
+= need
;
382 eng
->e_stream_underruns
++;
386 /* wake threads waiting for stream (blocking writes, etc.) */
387 cv_broadcast(&sp
->s_cv
);
389 mutex_exit(&sp
->s_lock
);
393 * Asynchronously notify clients. We do as much as
394 * possible of this outside of the lock, it avoids
395 * s_lock and c_lock contention and eliminates any
396 * chance of deadlock.
400 * NB: The only lock we are holding now is the engine
401 * lock. But the client can't go away because the
402 * closer would have to get the engine lock to remove
403 * the client's stream from engine. So we're safe.
406 if (output
&& (c
->c_output
!= NULL
) &&
407 (c
->c_next_output
== NULL
)) {
409 c
->c_next_output
= *output
;
413 if (drain
&& drained
&& (c
->c_drain
!= NULL
) &&
414 (c
->c_next_drain
== NULL
)) {
416 c
->c_next_drain
= *drain
;
422 * Deal with 24-bit overflows (from mixing) gracefully.
424 auimpl_output_limiter(eng
);
427 * Export the data (a whole fragment) to the device. Deal
428 * properly with wraps. Note that the test and subtraction is
429 * faster for dealing with wrap than modulo.
433 uint_t part
= min(resid
, eng
->e_nframes
- eng
->e_hidx
);
434 eng
->e_export(eng
, part
, fragfr
- resid
);
437 if (eng
->e_hidx
== eng
->e_nframes
)
443 * Consider doing the SYNC outside of the lock.
445 ENG_SYNC(eng
, fragfr
);
449 * Outer loop attempts to keep playing until we hit maximum playahead.
453 auimpl_output_callback(void *arg
)
455 audio_engine_t
*e
= arg
;
458 audio_client_t
*output
= NULL
;
459 audio_client_t
*drain
= NULL
;
462 mutex_enter(&e
->e_lock
);
464 if (e
->e_suspended
|| e
->e_failed
|| !e
->e_periodic
) {
465 mutex_exit(&e
->e_lock
);
469 if (e
->e_need_start
) {
471 if ((rv
= ENG_START(e
)) != 0) {
472 e
->e_failed
= B_TRUE
;
473 mutex_exit(&e
->e_lock
);
474 audio_dev_warn(e
->e_dev
,
475 "failed starting output, rv = %d", rv
);
478 e
->e_need_start
= B_FALSE
;
484 * This is a sign of a serious bug. We should
485 * probably offline the device via FMA, if we ever
486 * support FMA for audio devices.
488 e
->e_failed
= B_TRUE
;
490 mutex_exit(&e
->e_lock
);
491 audio_dev_warn(e
->e_dev
,
492 "device malfunction: broken play back sample counter");
498 if (e
->e_tail
> e
->e_head
) {
499 /* want more than we have */
504 cnt
= e
->e_head
- e
->e_tail
;
506 /* stay a bit ahead */
507 while (cnt
< e
->e_playahead
) {
508 auimpl_output_callback_impl(e
, &output
, &drain
);
509 cnt
= e
->e_head
- e
->e_tail
;
511 mutex_exit(&e
->e_lock
);
514 * Notify client personalities.
516 while ((c
= output
) != NULL
) {
518 output
= c
->c_next_output
;
519 c
->c_next_output
= NULL
;
524 while ((c
= drain
) != NULL
) {
526 drain
= c
->c_next_drain
;
527 c
->c_next_drain
= NULL
;
535 auimpl_output_preload(audio_engine_t
*e
)
539 ASSERT(mutex_owned(&e
->e_lock
));
541 if (e
->e_tail
> e
->e_head
) {
542 /* want more than we have */
545 e
->e_tail
= e
->e_head
;
547 cnt
= e
->e_head
- e
->e_tail
;
549 /* stay a bit ahead */
550 while (cnt
< e
->e_playahead
) {
551 auimpl_output_callback_impl(e
, NULL
, NULL
);
552 cnt
= e
->e_head
- e
->e_tail
;