]> git.sesse.net Git - vlc/blob - modules/access/cdda.c
modules/access/cdda.c: write proper bitrate in the wav header.
[vlc] / modules / access / cdda.c
1 /*****************************************************************************
2  * cdda.c : CD digital audio input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "codecs.h"
34 #include "vcd/cdrom.h"
35
36 /*****************************************************************************
37  * Module descriptior
38  *****************************************************************************/
39 static int  AccessOpen ( vlc_object_t * );
40 static void AccessClose( vlc_object_t * );
41
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44     "Allows you to modify the default caching value for cdda streams. This " \
45     "value should be set in milliseconds units." )
46
47 vlc_module_begin();
48     set_description( _("Audio CD input") );
49
50     add_integer( "cdda-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
51                  CACHING_LONGTEXT, VLC_TRUE );
52
53     set_capability( "access", 70 );
54     set_callbacks( AccessOpen, AccessClose );
55     add_shortcut( "cdda" );
56     add_shortcut( "cddasimple" );
57 vlc_module_end();
58
59
60 /* how many blocks VCDRead will read in each loop */
61 #define CDDA_BLOCKS_ONCE 20
62 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDDA_DATA_SIZE)
63
64 /*****************************************************************************
65  * Access: local prototypes
66  *****************************************************************************/
67 struct access_sys_t
68 {
69     vcddev_t    *vcddev;                            /* vcd device descriptor */
70     int         i_nb_tracks;                        /* Nb of tracks (titles) */
71     int         i_track;                                    /* Current track */
72     int         i_sector;                                  /* Current Sector */
73     int *       p_sectors;                                  /* Track sectors */
74     vlc_bool_t  b_end_of_track;           /* If the end of track was reached */
75
76     WAVEHEADER  waveheader;               /* Wave header for the output data */
77     int         i_header_pos;
78 };
79
80 static int  Read      ( input_thread_t *, byte_t *, size_t );
81 static void Seek      ( input_thread_t *, off_t );
82 static int  SetArea   ( input_thread_t *, input_area_t * );
83 static int  SetProgram( input_thread_t *, pgrm_descriptor_t * );
84
85 /*****************************************************************************
86  * AccessOpen: open cdda
87  *****************************************************************************/
88 static int AccessOpen( vlc_object_t *p_this )
89 {
90     input_thread_t *        p_input = (input_thread_t *)p_this;
91     access_sys_t *          p_sys;
92
93     char *                  psz_orig;
94     char *                  psz_parser;
95     char *                  psz_source;
96     int                     i;
97     input_area_t *          p_area;
98     int                     i_title = 1;
99     vcddev_t                *vcddev;
100
101     /* parse the options passed in command line : */
102     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
103
104     if( !psz_orig )
105     {
106         return( -1 );
107     }
108
109     while( *psz_parser && *psz_parser != '@' )
110     {
111         psz_parser++;
112     }
113
114     if( *psz_parser == '@' )
115     {
116         /* Found options */
117         *psz_parser = '\0';
118         ++psz_parser;
119
120         i_title = (int)strtol( psz_parser, NULL, 10 );
121         i_title = i_title ? i_title : 1;
122     }
123
124     if( !*psz_source )
125     {
126         /* No source specified, so figure it out. */
127         if( !p_input->psz_access )
128         {
129             free( psz_orig );
130             return VLC_EGENERIC;
131         }
132         psz_source = config_GetPsz( p_input, "cd-audio" );
133         if( !psz_source ) return -1;
134     }
135
136     /* Open CDDA */
137     if( !(vcddev = ioctl_Open( p_this, psz_source )) )
138     {
139         msg_Warn( p_input, "could not open %s", psz_source );
140         free( psz_source );
141         return VLC_EGENERIC;
142     }
143     free( psz_source );
144
145     p_input->p_access_data = p_sys = malloc( sizeof(access_sys_t) );
146     if( p_sys == NULL )
147     {
148         msg_Err( p_input, "out of memory" );
149         free( psz_source );
150         return VLC_EGENERIC;
151     }
152
153     p_sys->vcddev = vcddev;
154
155     p_input->i_mtu = CDDA_DATA_ONCE;
156
157     /* We read the Table Of Content information */
158     p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
159                                              p_sys->vcddev, &p_sys->p_sectors );
160     if( p_sys->i_nb_tracks < 0 )
161     {
162         msg_Err( p_input, "unable to count tracks" );
163     }
164     else if( p_sys->i_nb_tracks <= 0 )
165     {
166         msg_Err( p_input, "no audio tracks found" );
167     }
168
169     if( p_sys->i_nb_tracks <= 0 )
170     {
171         ioctl_Close( p_this, p_sys->vcddev );
172         free( p_sys );
173         return VLC_EGENERIC;
174     }
175
176     if( i_title >= p_sys->i_nb_tracks || i_title < 1 )
177     {
178         i_title = 1;
179     }
180
181     /* Set stream and area data */
182     vlc_mutex_lock( &p_input->stream.stream_lock );
183
184     /* Initialize ES structures */
185     input_InitStream( p_input, 0 );
186
187     /* cdda input method */
188     p_input->stream.i_method = INPUT_METHOD_CDDA;
189
190     p_input->stream.b_pace_control = 1;
191     p_input->stream.b_seekable = 1;
192     p_input->stream.i_mux_rate = 44100 * 4 / 50;
193
194 #define area p_input->stream.pp_areas
195     for( i = 1 ; i <= p_sys->i_nb_tracks ; i++ )
196     {
197         input_AddArea( p_input, i, 1 );
198
199         /* Absolute start offset and size */
200         area[i]->i_start =
201             (off_t)p_sys->p_sectors[i-1] * (off_t)CDDA_DATA_SIZE;
202         area[i]->i_size =
203             (off_t)(p_sys->p_sectors[i] - p_sys->p_sectors[i-1])
204             * (off_t)CDDA_DATA_SIZE;
205     }
206 #undef area
207
208     p_area = p_input->stream.pp_areas[i_title];
209
210     SetArea( p_input, p_area );
211
212     vlc_mutex_unlock( &p_input->stream.stream_lock );
213
214     p_input->pf_read = Read;
215     p_input->pf_seek = Seek;
216     p_input->pf_set_area = SetArea;
217     p_input->pf_set_program = SetProgram;
218
219     /* Update default_pts to a suitable value for cdda access */
220     p_input->i_pts_delay = config_GetInt( p_input, "cdda-caching" ) * 1000;
221
222     /* Build a WAV header for the output data */
223     memset( &p_sys->waveheader, 0, sizeof(WAVEHEADER) );
224     SetWLE( &p_sys->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
225     SetWLE( &p_sys->waveheader.BitsPerSample, 16);
226     p_sys->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
227     p_sys->waveheader.Length = 0;                     /* we just don't know */
228     p_sys->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
229     p_sys->waveheader.SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
230     SetDWLE( &p_sys->waveheader.SubChunkLength, 16);
231     SetWLE( &p_sys->waveheader.Modus, 2);
232     SetDWLE( &p_sys->waveheader.SampleFreq, 44100);
233     SetWLE( &p_sys->waveheader.BytesPerSample,
234             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
235     SetDWLE( &p_sys->waveheader.BytesPerSec,
236              2*16/8 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
237     p_sys->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
238     p_sys->waveheader.DataLength = 0;                 /* we just don't know */
239     p_sys->i_header_pos = 0;
240
241     return VLC_SUCCESS;
242 }
243
244 /*****************************************************************************
245  * AccessClose: closes cdda
246  *****************************************************************************/
247 static void AccessClose( vlc_object_t *p_this )
248 {
249     input_thread_t *p_input = (input_thread_t *)p_this;
250     access_sys_t   *p_sys = p_input->p_access_data;
251
252     ioctl_Close( p_this, p_sys->vcddev );
253     free( p_sys );
254 }
255
256 /*****************************************************************************
257  * Read: reads from the CDDA into PES packets.
258  *****************************************************************************
259  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
260  * bytes.
261  *****************************************************************************/
262 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
263 {
264     access_sys_t *p_sys = p_input->p_access_data;
265     int          i_blocks = i_len / CDDA_DATA_SIZE;
266     int          i_read = 0;
267     int          i_index;
268
269     if( !p_sys->i_header_pos )
270     {
271         p_sys->i_header_pos = sizeof(WAVEHEADER);
272         i_blocks = (i_len - sizeof(WAVEHEADER)) / CDDA_DATA_SIZE;
273         memcpy( p_buffer, &p_sys->waveheader, sizeof(WAVEHEADER) );
274         p_buffer += sizeof(WAVEHEADER);
275         i_read += sizeof(WAVEHEADER);
276     }
277
278     if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev, p_sys->i_sector,
279                            p_buffer, i_blocks, CDDA_TYPE ) < 0 )
280     {
281         msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
282         return -1;
283     }
284
285     for( i_index = 0; i_index < i_blocks; i_index++ )
286     {
287         p_sys->i_sector ++;
288         if( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
289         {
290             input_area_t *p_area;
291
292             if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
293             {
294                 return 0; /* EOF */
295             }
296
297             vlc_mutex_lock( &p_input->stream.stream_lock );
298             p_area = p_input->stream.pp_areas[
299                     p_input->stream.p_selected_area->i_id + 1 ];
300
301             msg_Dbg( p_input, "new title" );
302
303             p_area->i_part = 1;
304             SetArea( p_input, p_area );
305             vlc_mutex_unlock( &p_input->stream.stream_lock );
306         }
307         i_read += CDDA_DATA_SIZE;
308     }
309
310     if ( i_len % CDDA_DATA_SIZE ) /* this should not happen */
311     {
312         msg_Err( p_input, "must read full sectors" );
313     }
314
315     return i_read;
316 }
317
318 /*****************************************************************************
319  * SetProgram: Does nothing since a CDDA is mono_program
320  *****************************************************************************/
321 static int SetProgram( input_thread_t * p_input,
322                            pgrm_descriptor_t * p_program)
323 {
324     return VLC_EGENERIC;
325 }
326
327 /*****************************************************************************
328  * SetArea: initialize input data for title x.
329  * It should be called for each user navigation request.
330  ****************************************************************************/
331 static int SetArea( input_thread_t * p_input, input_area_t * p_area )
332 {
333     access_sys_t *p_sys = p_input->p_access_data;
334     vlc_value_t  val;
335
336     /* we can't use the interface slider until initilization is complete */
337     p_input->stream.b_seekable = 0;
338
339     if( p_area != p_input->stream.p_selected_area )
340     {
341         /* Change the default area */
342         p_input->stream.p_selected_area = p_area;
343
344         /* Change the current track */
345         p_sys->i_track = p_area->i_id - 1;
346         p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
347
348         /* Update the navigation variables without triggering a callback */
349         val.i_int = p_area->i_id;
350         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
351     }
352
353     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
354
355     p_input->stream.p_selected_area->i_tell =
356         (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
357          - p_input->stream.p_selected_area->i_start;
358
359     /* warn interface that something has changed */
360     p_input->stream.b_seekable = 1;
361     p_input->stream.b_changed = 1;
362
363     return 0;
364 }
365
366 /****************************************************************************
367  * Seek
368  ****************************************************************************/
369 static void Seek( input_thread_t * p_input, off_t i_off )
370 {
371     access_sys_t * p_sys = p_input->p_access_data;
372
373     p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
374                        + i_off / (off_t)CDDA_DATA_SIZE;
375
376     vlc_mutex_lock( &p_input->stream.stream_lock );
377     p_input->stream.p_selected_area->i_tell =
378         (off_t)p_sys->i_sector * (off_t)CDDA_DATA_SIZE
379          - p_input->stream.p_selected_area->i_start;
380     vlc_mutex_unlock( &p_input->stream.stream_lock );
381 }