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