]> git.sesse.net Git - vlc/blob - modules/access/vcdx/cdrom.c
dba99e60a35acabdfb0c040c38e0a1a9be44b5fc
[vlc] / modules / access / vcdx / cdrom.c
1 /****************************************************************************
2  * cdrom.c: cdrom tools
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Johan Bilien <jobi@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *          Jon Lech Johansen <jon-vl@nanocrew.net>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef HAVE_UNISTD_H
35 #   include <unistd.h>
36 #endif
37
38 #include <string.h>
39 #include <errno.h>
40
41 #include "cdrom.h"
42
43 /*****************************************************************************
44  * Local Prototypes
45  *****************************************************************************/
46 static void cd_log_handler (cdio_log_level_t level, const char message[]);
47
48 /*****************************************************************************
49  * ioctl_Open: Opens a VCD device or file and returns an opaque handle
50  *****************************************************************************/
51 cddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
52 {
53     cddev_t *p_cddev;
54
55     if( !psz_dev ) return NULL;
56
57     /*
58      *  Initialize structure with default values
59      */
60     p_cddev = (cddev_t *)malloc( sizeof(cddev_t) );
61     if( p_cddev == NULL )
62     {
63         msg_Err( p_this, "out of memory" );
64         return NULL;
65     }
66
67     /* Set where to log errors messages from libcdio. */
68     cdio_log_set_handler ( cd_log_handler );
69
70     p_cddev->cdio = cdio_open(psz_dev, DRIVER_UNKNOWN);
71
72     if( p_cddev->cdio == NULL )
73     {
74         free( p_cddev );
75         p_cddev = NULL;
76     }
77
78     return p_cddev;
79 }
80
81 /*****************************************************************************
82  * ioctl_Close: Closes an already opened VCD device or file.
83  *****************************************************************************/
84 void ioctl_Close( cddev_t *p_cddev )
85 {
86     cdio_destroy(p_cddev->cdio);
87 }
88
89 /*****************************************************************************
90  * ioctl_GetTracksMap: Read the Table of Contents, fill in the pp_sectors map
91  *                     if pp_sectors is not null and return the number of
92  *                     tracks available.
93  *                     We allocate and fill one more track than are on 
94  *                     the CD. The last "track" is leadout track information.
95  *                     This makes finding the end of the last track uniform
96  *                     how it is done for other tracks.
97  *****************************************************************************/
98 track_t ioctl_GetTracksMap( vlc_object_t *p_this, const CdIo *cdio,
99                             lsn_t **pp_sectors )
100 {
101     track_t i_tracks     = cdio_get_num_tracks(cdio);
102     track_t first_track  = cdio_get_first_track_num(cdio);
103     track_t i;
104
105
106     *pp_sectors = malloc( (i_tracks + 1) * sizeof(lsn_t) );
107     if( *pp_sectors == NULL )
108       {
109         msg_Err( p_this, "out of memory" );
110         return 0;
111       }
112
113     /* Fill the p_sectors structure with the track/sector matches.
114        Note cdio_get_track_lsn when given num_tracks + 1 will return
115        the leadout LSN.
116      */
117     for( i = 0 ; i <= i_tracks ; i++ )
118       {
119         (*pp_sectors)[ i ] = cdio_get_track_lsn(cdio, first_track+i);
120       }
121     
122     return i_tracks;
123 }
124
125 /****************************************************************************
126  * ioctl_ReadSector: Read a sector (2324 bytes)
127  ****************************************************************************/
128 int ioctl_ReadSector( vlc_object_t *p_this, const cddev_t *p_cddev,
129                       int i_sector, byte_t * p_buffer )
130 {
131   typedef struct {
132     uint8_t subheader   [8];
133     uint8_t data        [M2F2_SECTOR_SIZE];
134   } vcdsector_t;
135   vcdsector_t vcd_sector;
136   
137   if( cdio_read_mode2_sector(p_cddev->cdio, &vcd_sector, i_sector, VLC_TRUE) 
138       != 0)
139   {
140       // msg_Err( p_this, "Could not read sector %d", i_sector );
141       return -1;
142   }
143     
144   memcpy (p_buffer, vcd_sector.data, M2F2_SECTOR_SIZE);
145   
146   return( 0 );
147 }
148
149 /****************************************************************************
150  * Private functions
151  ****************************************************************************/
152
153 /* For now we're going to just discard error messages from libcdio... */
154 static void
155 cd_log_handler (cdio_log_level_t level, const char message[])
156 {
157   return;
158 }