contribs: update to fribidi2 beta (0.19.1) to hopefully solve problems with arabic...
[vlc.git] / modules / audio_output / hd1000a.cpp
blob38d71773f685893bad5035943ed5b8bf4e3ba7b9
1 /*****************************************************************************
2 * hd1000a.cpp : Roku HD1000 audio output
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * $Id$
7 * Author: Jon Lech Johansen <jon-vl@nanocrew.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 extern "C"
29 #include <errno.h>
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <vlc/vlc.h>
36 #include <vlc_aout.h>
38 #include "aout_internal.h"
41 #include <deschutes/libraries/hdmachinex225/PCMAudioPlayer.h>
43 #define FRAME_SIZE 4096
45 /*****************************************************************************
46 * aout_sys_t: audio output method descriptor
47 *****************************************************************************
48 * This structure is part of the audio output thread descriptor.
49 * It describes the direct sound specific properties of an audio device.
50 *****************************************************************************/
51 struct aout_sys_t
53 u32 nAlignment;
54 u32 nSizeMultiple;
55 u32 nBuffers;
56 u32 nBufferSize;
57 void ** ppBuffers;
58 u32 nNextBufferIndex;
59 PCMAudioPlayer * pPlayer;
62 /*****************************************************************************
63 * Local prototypes.
64 *****************************************************************************/
65 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
68 static void Play ( aout_instance_t * );
69 static int Thread ( aout_instance_t * );
71 static void InterleaveS16( int16_t *, int16_t * );
73 /*****************************************************************************
74 * Module descriptor
75 *****************************************************************************/
76 vlc_module_begin();
77 set_shortname( "Roku HD1000" );
78 set_description( _("Roku HD1000 audio output") );
79 set_capability( "audio output", 100 );
80 set_category( CAT_AUDIO );
81 set_subcategory( SUBCAT_AUDIO_AOUT );
82 set_callbacks( Open, Close );
83 vlc_module_end();
85 /*****************************************************************************
86 * Open: open a dummy audio device
87 *****************************************************************************/
88 static int Open( vlc_object_t * p_this )
90 aout_instance_t * p_aout = (aout_instance_t *)p_this;
91 struct aout_sys_t * p_sys;
92 PCMAudioPlayer * pPlayer;
93 int i_volume;
95 /* Allocate structure */
96 p_aout->output.p_sys = p_sys =
97 (aout_sys_t *)malloc( sizeof( aout_sys_t ) );
98 if( p_aout->output.p_sys == NULL )
100 msg_Err( p_aout, "out of memory" );
101 return VLC_EGENERIC;
104 /* New PCMAudioPlayer */
105 p_sys->pPlayer = pPlayer = new PCMAudioPlayer();
106 if( p_sys->pPlayer == NULL )
108 msg_Err( p_aout, "out of memory" );
109 free( p_sys );
110 return VLC_EGENERIC;
113 /* Get Buffer Requirements */
114 if( !pPlayer->GetBufferRequirements( p_sys->nAlignment,
115 p_sys->nSizeMultiple,
116 p_sys->nBuffers ) )
118 msg_Err( p_aout, "GetBufferRequirements failed" );
119 delete pPlayer;
120 free( p_sys );
121 return VLC_EGENERIC;
124 p_sys->nBuffers = __MIN( p_sys->nBuffers, 4 );
126 p_sys->ppBuffers = (void **)malloc( p_sys->nBuffers * sizeof( void * ) );
127 if( p_sys->ppBuffers == NULL )
129 msg_Err( p_aout, "out of memory" );
130 delete pPlayer;
131 free( p_sys );
132 return VLC_EGENERIC;
135 /* Open PCMAudioPlayer */
136 p_sys->nBufferSize = FRAME_SIZE * 4;
137 if( !pPlayer->Open( p_sys->nBuffers, p_sys->nBufferSize,
138 p_sys->ppBuffers ) )
140 msg_Err( p_aout, "Open failed" );
141 delete pPlayer;
142 free( p_sys->ppBuffers );
143 free( p_sys );
144 return VLC_EGENERIC;
147 p_sys->nNextBufferIndex = 0;
149 if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
151 p_aout->output.output.i_rate = 44100;
152 if( !pPlayer->SetSampleRate( p_aout->output.output.i_rate ) )
154 msg_Err( p_aout, "SetSampleRate failed" );
155 pPlayer->Close();
156 delete pPlayer;
157 free( p_sys->ppBuffers );
158 free( p_sys );
159 return VLC_EGENERIC;
163 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
164 p_aout->output.i_nb_samples = FRAME_SIZE;
165 p_aout->output.output.i_physical_channels
166 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
167 p_aout->output.pf_play = Play;
168 aout_VolumeSoftInit( p_aout );
170 i_volume = config_GetInt( p_aout->p_libvlc, "volume" );
171 pPlayer->SetVolume( (u32)__MIN( i_volume * 64, 0xFFFF ) );
173 /* Create thread and wait for its readiness. */
174 if( vlc_thread_create( p_aout, "aout", Thread,
175 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
177 msg_Err( p_aout, "cannot create OSS thread (%m)" );
178 pPlayer->Close();
179 delete pPlayer;
180 free( p_sys->ppBuffers );
181 free( p_sys );
182 return VLC_ETHREAD;
185 return VLC_SUCCESS;
188 /*****************************************************************************
189 * Close: close our file
190 *****************************************************************************/
191 static void Close( vlc_object_t * p_this )
193 u32 i;
194 aout_instance_t * p_aout = (aout_instance_t *)p_this;
195 struct aout_sys_t * p_sys = p_aout->output.p_sys;
197 vlc_object_kill( p_aout );
198 vlc_thread_join( p_aout );
199 p_aout->b_die = VLC_FALSE;
203 i = p_sys->pPlayer->WaitForBuffer();
204 } while( i != 0 && i != p_sys->nBuffers );
206 p_sys->pPlayer->Close();
207 delete p_sys->pPlayer;
209 free( p_sys->ppBuffers );
210 free( p_sys );
213 /*****************************************************************************
214 * Play: do nothing
215 *****************************************************************************/
216 static void Play( aout_instance_t * p_aout )
220 /*****************************************************************************
221 * Thread: thread used to DMA the data to the device
222 *****************************************************************************/
223 static int Thread( aout_instance_t * p_aout )
225 aout_buffer_t * p_buffer;
226 struct aout_sys_t * p_sys = p_aout->output.p_sys;
227 PCMAudioPlayer * pPlayer = p_sys->pPlayer;
229 while( !p_aout->b_die )
231 pPlayer->WaitForBuffer();
233 vlc_mutex_lock( &p_aout->output_fifo_lock );
234 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
235 vlc_mutex_unlock( &p_aout->output_fifo_lock );
237 #define i p_sys->nNextBufferIndex
238 if( p_buffer == NULL )
240 p_aout->p_libvlc->pf_memset( p_sys->ppBuffers[ i ], 0,
241 p_sys->nBufferSize );
243 else
245 InterleaveS16( (int16_t *)p_buffer->p_buffer,
246 (int16_t *)p_sys->ppBuffers[ i ] );
247 aout_BufferFree( p_buffer );
250 if( !pPlayer->QueueBuffer( (s16 *)p_sys->ppBuffers[ i ],
251 p_sys->nBufferSize / 2 ) )
253 msg_Err( p_aout, "QueueBuffer failed" );
256 i = (i + 1) % p_sys->nBuffers;
257 #undef i
260 return VLC_SUCCESS;
263 /*****************************************************************************
264 * InterleaveS16: interleave samples
265 *****************************************************************************/
266 static void InterleaveS16( int16_t * p_in, int16_t * p_out )
268 for( int i = 0; i < FRAME_SIZE; i++ )
270 p_out[ i * 2 + 0 ] = p_in[ i * 2 + 1 ];
271 p_out[ i * 2 + 1 ] = p_in[ i * 2 + 0 ];