]> git.sesse.net Git - vlc/blob - modules/access/vcd/cdrom.c
* Fixed AOUT_CHAN_CHANNEL2 trivial mixer,
[vlc] / modules / access / vcd / cdrom.c
1 /****************************************************************************
2  * cdrom.c: cdrom tools
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: cdrom.c,v 1.5 2002/10/16 23:12:46 massiot Exp $
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 <fcntl.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <string.h>
42 #include <errno.h>
43
44 #ifdef HAVE_SYS_IOCTL_H
45 #   include <sys/ioctl.h>
46 #endif
47
48 #if defined( SYS_BSDI )
49 #   include <dvd.h>
50 #elif defined ( SYS_DARWIN )
51 #   include <CoreFoundation/CFBase.h>
52 #   include <IOKit/IOKitLib.h>
53 #   include <IOKit/storage/IOCDTypes.h>
54 #   include <IOKit/storage/IOCDMedia.h>
55 #   include <IOKit/storage/IOCDMediaBSDClient.h>
56 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
57 #   include <sys/cdio.h>
58 #   include <sys/cdrio.h>
59 #elif defined( WIN32 )
60 #   include <windows.h>
61 #   include <winioctl.h>
62 #else
63 #   include <linux/cdrom.h>
64 #endif
65
66 #include "cdrom.h"
67 #include "vcd.h"
68
69 /*****************************************************************************
70  * ioctl_Open: Opens a VCD device or file and returns an opaque handle
71  *****************************************************************************/
72 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
73 {
74     int i_ret;
75     int b_is_file;
76     vcddev_t *p_vcddev;
77 #ifndef WIN32
78     struct stat fileinfo;
79 #endif
80
81     if( !psz_dev ) return NULL;
82
83     /*
84      *  Initialize structure with default values
85      */
86     p_vcddev = (vcddev_t *)malloc( sizeof(vcddev_t) );
87     if( p_vcddev == NULL )
88     {
89         msg_Err( p_this, "out of memory" );
90         return NULL;
91     }
92     p_vcddev->i_vcdimage_handle = -1;
93     p_vcddev->psz_dev = NULL;
94     b_is_file = 1;
95
96     /*
97      *  Check if we are dealing with a device or a file (vcd image)
98      */
99 #ifdef WIN32
100     if( strlen( psz_dev ) == 1 ||
101         (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
102     {
103         b_is_file = 0;
104     }
105
106 #else
107     if( stat( psz_dev, &fileinfo ) < 0 )
108     {
109         free( p_vcddev );
110         return NULL;
111     }
112
113     /* Check if this is a block/char device */
114     if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
115         b_is_file = 0;
116 #endif
117
118     if( b_is_file )
119     {
120         i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
121     }
122     else
123     {
124         /*
125          *  open the vcd device
126          */
127
128 #ifdef WIN32
129         i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
130 #else
131         p_vcddev->i_device_handle = -1;
132         p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
133         i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
134 #endif
135     }
136
137     if( i_ret == 0 )
138     {
139         p_vcddev->psz_dev = (char *)strdup( psz_dev );
140     }
141     else
142     {
143         free( p_vcddev );
144         p_vcddev = NULL;
145     }
146
147     return p_vcddev;
148 }
149
150 /*****************************************************************************
151  * ioctl_Close: Closes an already opened VCD device or file.
152  *****************************************************************************/
153 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
154 {
155     if( p_vcddev->psz_dev ) free( p_vcddev->psz_dev );
156
157     if( p_vcddev->i_vcdimage_handle != -1 )
158     {
159         /*
160          *  vcd image mode
161          */
162
163         CloseVCDImage( p_this, p_vcddev );
164         return;
165     }
166
167     /*
168      *  vcd device mode
169      */
170
171 #ifdef WIN32
172     if( p_vcddev->h_device_handle )
173         CloseHandle( p_vcddev->h_device_handle );
174     if( p_vcddev->hASPI )
175         FreeLibrary( (HMODULE)p_vcddev->hASPI );
176 #else
177     if( p_vcddev->i_device_handle != -1 )
178         close( p_vcddev->i_device_handle );
179 #endif
180 }
181
182 /*****************************************************************************
183  * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
184  *                     if pp_sectors is not null and return the number of
185  *                     tracks available.
186  *****************************************************************************/
187 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
188                         int **pp_sectors )
189 {
190     int i_tracks = 0;
191
192     if( p_vcddev->i_vcdimage_handle != -1 )
193     {
194         /*
195          *  vcd image mode
196          */
197
198         i_tracks = p_vcddev->i_tracks;
199
200         if( pp_sectors )
201         {
202             *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
203             if( *pp_sectors == NULL )
204             {
205                 msg_Err( p_this, "out of memory" );
206                 return 0;
207             }
208             memcpy( *pp_sectors, p_vcddev->p_sectors,
209                     (i_tracks + 1) * sizeof(int) );
210         }
211
212         return i_tracks;
213     }
214     else
215     {
216
217         /*
218          *  vcd device mode
219          */
220
221 #if defined( SYS_DARWIN )
222
223         CDTOC *pTOC;
224         int i_descriptors;
225
226         if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
227         {
228             msg_Err( p_this, "failed to get the TOC" );
229             return 0;
230         }
231
232         i_descriptors = darwin_getNumberOfDescriptors( pTOC );
233         i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
234
235         if( pp_sectors )
236         {
237             int i, i_leadout = -1;
238             CDTOCDescriptor *pTrackDescriptors;
239             u_char track;
240
241             *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
242             if( *pp_sectors == NULL )
243             {
244                 msg_Err( p_this, "out of memory" );
245                 darwin_freeTOC( pTOC );
246                 return 0;
247             }
248
249             pTrackDescriptors = pTOC->descriptors;
250
251             for( i_tracks = 0, i = 0; i <= i_descriptors; i++ )
252             {
253                 track = pTrackDescriptors[i].point;
254
255                 if( track == 0xA2 )
256                     i_leadout = i;
257
258                 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
259                     continue;
260
261                 (*pp_sectors)[i_tracks++] = 
262                     CDConvertMSFToLBA( pTrackDescriptors[i].p );
263             }
264
265             if( i_leadout == -1 )
266             {
267                 msg_Err( p_this, "leadout not found" );
268                 free( *pp_sectors );
269                 darwin_freeTOC( pTOC );
270                 return 0;
271             }
272
273             /* set leadout sector */
274             (*pp_sectors)[i_tracks] = 
275                 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
276         }
277
278         darwin_freeTOC( pTOC );
279
280 #elif defined( WIN32 )
281         if( p_vcddev->hASPI )
282         {
283             HANDLE hEvent;
284             struct SRB_ExecSCSICmd ssc;
285             byte_t p_tocheader[ 4 ];
286
287             /* Create the transfer completion event */
288             hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
289             if( hEvent == NULL )
290             {
291                 return -1;
292             }
293
294             memset( &ssc, 0, sizeof( ssc ) );
295
296             ssc.SRB_Cmd         = SC_EXEC_SCSI_CMD;
297             ssc.SRB_Flags       = SRB_DIR_IN | SRB_EVENT_NOTIFY;
298             ssc.SRB_HaId        = LOBYTE( p_vcddev->i_sid );
299             ssc.SRB_Target      = HIBYTE( p_vcddev->i_sid );
300             ssc.SRB_SenseLen    = SENSE_LEN;
301
302             ssc.SRB_PostProc = (LPVOID) hEvent;
303             ssc.SRB_CDBLen      = 10;
304
305             /* Operation code */
306             ssc.CDBByte[ 0 ] = READ_TOC;
307
308             /* Format */
309             ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;
310
311             /* Starting track */
312             ssc.CDBByte[ 6 ] = 0;
313
314             /* Allocation length and buffer */
315             ssc.SRB_BufLen = sizeof( p_tocheader );
316             ssc.SRB_BufPointer  = p_tocheader;
317             ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >>  8 ) & 0xff;
318             ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen       ) & 0xff;
319
320             /* Initiate transfer */
321             ResetEvent( hEvent );
322             p_vcddev->lpSendCommand( (void*) &ssc );
323
324             /* If the command has still not been processed, wait until it's
325              * finished */
326             if( ssc.SRB_Status == SS_PENDING )
327                 WaitForSingleObject( hEvent, INFINITE );
328
329             /* check that the transfer went as planned */
330             if( ssc.SRB_Status != SS_COMP )
331             {
332                 CloseHandle( hEvent );
333                 return 0;
334             }
335
336             i_tracks = p_tocheader[3] - p_tocheader[2] + 1;
337
338             if( pp_sectors )
339             {
340                 int i, i_toclength;
341                 byte_t *p_fulltoc;
342
343                 i_toclength = 4 /* header */ + p_tocheader[0] +
344                               ((unsigned int)p_tocheader[1] << 8);
345
346                 p_fulltoc = malloc( i_toclength );
347                 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
348
349                 if( *pp_sectors == NULL || p_fulltoc == NULL )
350                 {
351                     if( *pp_sectors ) free( *pp_sectors );
352                     if( p_fulltoc ) free( p_fulltoc );
353                     msg_Err( p_this, "out of memory" );
354                     CloseHandle( hEvent );
355                     return 0;
356                 }
357
358                 /* Allocation length and buffer */
359                 ssc.SRB_BufLen = i_toclength;
360                 ssc.SRB_BufPointer  = p_fulltoc;
361                 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >>  8 ) & 0xff;
362                 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen       ) & 0xff;
363
364                 /* Initiate transfer */
365                 ResetEvent( hEvent );
366                 p_vcddev->lpSendCommand( (void*) &ssc );
367
368                 /* If the command has still not been processed, wait until it's
369                  * finished */
370                 if( ssc.SRB_Status == SS_PENDING )
371                     WaitForSingleObject( hEvent, INFINITE );
372
373                 /* check that the transfer went as planned */
374                 if( ssc.SRB_Status != SS_COMP )
375                     i_tracks = 0;
376
377                 for( i = 0 ; i <= i_tracks ; i++ )
378                 {
379                     int i_index = 8 + 8 * i;
380                     (*pp_sectors)[ i ] = ((int)p_fulltoc[ i_index ] << 24) +
381                                          ((int)p_fulltoc[ i_index+1 ] << 16) +
382                                          ((int)p_fulltoc[ i_index+2 ] << 8) +
383                                          (int)p_fulltoc[ i_index+3 ];
384
385                     msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
386                 }
387
388                 free( p_fulltoc );
389             }
390
391             CloseHandle( hEvent );
392
393         }
394         else
395         {
396             DWORD dwBytesReturned;
397             CDROM_TOC cdrom_toc;
398
399             if( DeviceIoControl( p_vcddev->h_device_handle,
400                                  IOCTL_CDROM_READ_TOC,
401                                  NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
402                                  &dwBytesReturned, NULL ) == 0 )
403             {
404                 msg_Err( p_this, "could not read TOCHDR" );
405                 return 0;
406             }
407
408             i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
409
410             if( pp_sectors )
411             {
412                 int i;
413
414                 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
415                 if( *pp_sectors == NULL )
416                 {
417                     msg_Err( p_this, "out of memory" );
418                     return 0;
419                 }
420
421                 for( i = 0 ; i <= i_tracks ; i++ )
422                 {
423                     (*pp_sectors)[ i ] = MSF_TO_LBA2(
424                                            cdrom_toc.TrackData[i].Address[1],
425                                            cdrom_toc.TrackData[i].Address[2],
426                                            cdrom_toc.TrackData[i].Address[3] );
427                     msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
428                 }
429             }
430         }
431
432 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
433         struct ioc_toc_header tochdr;
434         struct ioc_read_toc_entry toc_entries;
435
436         if( ioctl( p_vcddev->i_devicd_handle, CDIOREADTOCHEADER, &tochdr )
437             == -1 )
438         {
439             msg_Err( p_this, "could not read TOCHDR" );
440             return 0;
441         }
442
443         i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
444
445         if( pp_sectors )
446         {
447              int i;
448
449              *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
450              if( *pp_sectors == NULL )
451              {
452                  msg_Err( p_this, "out of memory" );
453                  return NULL;
454              }
455
456              toc_entries.address_format = CD_LBA_FORMAT;
457              toc_entries.starting_track = 0;
458              toc_entries.data_len = ( i_tracks + 1 ) *
459                                         sizeof( struct cd_toc_entry ); 
460              toc_entries.data = (struct cd_toc_entry *)
461                                     malloc( toc_entries.data_len );
462              if( toc_entries.data == NULL )
463              {
464                  msg_Err( p_this, "out of memory" );
465                  free( *pp_sectors );
466                  return 0;
467              }
468  
469              /* Read the TOC */
470              if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
471                         &toc_entries ) == -1 )
472              {
473                  msg_Err( p_this, "could not read the TOC" );
474                  free( *pp_sectors );
475                  free( toc_entries.data );
476                  return 0;
477              }
478     
479              /* Fill the p_sectors structure with the track/sector matches */
480              for( i = 0 ; i <= i_tracks ; i++ )
481              {
482                  (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
483              }
484         }
485 #else
486         struct cdrom_tochdr   tochdr;
487         struct cdrom_tocentry tocent;
488
489         /* First we read the TOC header */
490         if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
491             == -1 )
492         {
493             msg_Err( p_this, "could not read TOCHDR" );
494             return 0;
495         }
496
497         i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
498
499         if( pp_sectors )
500         {
501             int i;
502
503             *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
504             if( *pp_sectors == NULL )
505             {
506                 msg_Err( p_this, "out of memory" );
507                 return 0;
508             }
509
510             /* Fill the p_sectors structure with the track/sector matches */
511             for( i = 0 ; i <= i_tracks ; i++ )
512             {
513                 tocent.cdte_format = CDROM_LBA;
514                 tocent.cdte_track =
515                     ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
516
517                 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
518                            &tocent ) == -1 )
519                 {
520                     msg_Err( p_this, "could not read TOCENTRY" );
521                     free( *pp_sectors );
522                     return 0;
523                 }
524
525                 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
526             }
527         }
528 #endif
529
530         return i_tracks;
531     }
532 }
533
534 /****************************************************************************
535  * ioctl_ReadSector: Read a sector (2324 bytes)
536  ****************************************************************************/
537 int ioctl_ReadSector( vlc_object_t *p_this, const vcddev_t *p_vcddev,
538                       int i_sector, byte_t * p_buffer )
539 {
540     byte_t p_block[ VCD_SECTOR_SIZE ];
541
542     if( p_vcddev->i_vcdimage_handle != -1 )
543     {
544         /*
545          *  vcd image mode
546          */
547         if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
548                    SEEK_SET ) == -1 )
549         {
550             msg_Err( p_this, "Could not lseek to sector %d", i_sector );
551             return -1;
552         }
553
554         if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE )
555             == -1 )
556         {
557             // msg_Err( p_this, "Could not read sector %d", i_sector );
558             return -1;
559         }
560
561         /* We don't want to keep the header of the read sector */
562         memcpy( p_buffer, p_block + VCD_DATA_START, VCD_DATA_SIZE );
563
564         return 0;
565     }
566     else
567     {
568
569         /*
570          *  vcd device mode
571          */
572
573 #if defined( SYS_DARWIN )
574         dk_cd_read_t cd_read;
575
576         memset( &cd_read, 0, sizeof(cd_read) );
577
578         cd_read.offset = i_sector * VCD_SECTOR_SIZE;
579         cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
580                              kCDSectorAreaSubHeader | kCDSectorAreaUser |
581                              kCDSectorAreaAuxiliary;
582         cd_read.sectorType = kCDSectorTypeUnknown;
583
584         cd_read.buffer = p_block;
585         cd_read.bufferLength = sizeof(p_block);
586
587         if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
588         {
589             msg_Err( p_this, "could not read block %d", i_sector );
590             return -1;
591         }
592
593 #elif defined( WIN32 )
594         if( p_vcddev->hASPI )
595         {
596             HANDLE hEvent;
597             struct SRB_ExecSCSICmd ssc;
598
599             /* Create the transfer completion event */
600             hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
601             if( hEvent == NULL )
602             {
603                 return -1;
604             }
605
606             memset( &ssc, 0, sizeof( ssc ) );
607
608             ssc.SRB_Cmd         = SC_EXEC_SCSI_CMD;
609             ssc.SRB_Flags       = SRB_DIR_IN | SRB_EVENT_NOTIFY;
610             ssc.SRB_HaId        = LOBYTE( p_vcddev->i_sid );
611             ssc.SRB_Target      = HIBYTE( p_vcddev->i_sid );
612             ssc.SRB_SenseLen    = SENSE_LEN;
613
614             ssc.SRB_PostProc = (LPVOID) hEvent;
615             ssc.SRB_CDBLen      = 12;
616
617             /* Operation code */
618             ssc.CDBByte[ 0 ] = READ_CD;
619
620             /* Start of LBA */
621             ssc.CDBByte[ 2 ] = ( i_sector >> 24 ) & 0xff;
622             ssc.CDBByte[ 3 ] = ( i_sector >> 16 ) & 0xff;
623             ssc.CDBByte[ 4 ] = ( i_sector >>  8 ) & 0xff;
624             ssc.CDBByte[ 5 ] = ( i_sector       ) & 0xff;
625
626             /* Transfer length */
627             ssc.CDBByte[ 6 ] = 0;
628             ssc.CDBByte[ 7 ] = 0;
629             ssc.CDBByte[ 8 ] = 1;
630
631             /* Data selection */
632             ssc.CDBByte[ 9 ] = READ_CD_USERDATA_MODE2;
633
634             /* Result buffer */
635             ssc.SRB_BufPointer  = p_block;
636             ssc.SRB_BufLen = VCD_SECTOR_SIZE;
637
638             /* Initiate transfer */
639             ResetEvent( hEvent );
640             p_vcddev->lpSendCommand( (void*) &ssc );
641
642             /* If the command has still not been processed, wait until it's
643              * finished */
644             if( ssc.SRB_Status == SS_PENDING )
645             {
646                 WaitForSingleObject( hEvent, INFINITE );
647             }
648             CloseHandle( hEvent );
649
650             /* check that the transfer went as planned */
651             if( ssc.SRB_Status != SS_COMP )
652             {
653                 return -1;
654             }
655
656             /* We don't want to keep the footer of the read sector */
657             memcpy( p_buffer, p_block, VCD_DATA_SIZE );
658             return 0;
659         }
660         else
661         {
662             DWORD dwBytesReturned;
663             RAW_READ_INFO cdrom_raw;
664
665             /* Initialize CDROM_RAW_READ structure */
666             cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
667             cdrom_raw.SectorCount = 1;
668             cdrom_raw.TrackMode = XAForm2;
669
670             if( DeviceIoControl( p_vcddev->h_device_handle,
671                                  IOCTL_CDROM_RAW_READ, &cdrom_raw,
672                                  sizeof(RAW_READ_INFO), p_block,
673                                  sizeof(p_block), &dwBytesReturned, NULL )
674                 == 0 )
675             {
676                 return -1;
677             }
678  
679             /* We don't want to keep the header of the read sector */
680             memcpy( p_buffer, p_block + VCD_DATA_START, VCD_DATA_SIZE );
681
682             return 0;
683         }
684
685 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
686
687         int i_size = VCD_SECTOR_SIZE;
688
689         if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
690             == -1 )
691         {
692             msg_Err( p_this, "Could not set block size" );
693             return( -1 );
694         }
695
696         if( lseek( p_vcddev->i_device_handle,
697                    i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
698         {
699             msg_Err( p_this, "Could not lseek to sector %d", i_sector );
700             return( -1 );
701         }
702
703         if( read( p_vcddev->i_device_handle, p_block, VCD_SECTOR_SIZE ) == -1 )
704         {
705             msg_Err( p_this, "Could not read sector %d", i_sector );
706             return( -1 );
707         }
708
709 #else
710         int i_dummy = i_sector + 2 * CD_FRAMES;
711
712 #define p_msf ((struct cdrom_msf0 *)p_block)
713         p_msf->minute =   i_dummy / (CD_FRAMES * CD_SECS);
714         p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
715         p_msf->frame =  ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
716 #undef p_msf
717
718         if( ioctl(p_vcddev->i_device_handle, CDROMREADRAW, p_block) == -1 )
719         {
720             msg_Err( p_this, "could not read block %i from disc", i_sector );
721             return( -1 );
722         }
723 #endif
724
725         /* We don't want to keep the header of the read sector */
726         memcpy( p_buffer, p_block + VCD_DATA_START, VCD_DATA_SIZE );
727
728         return( 0 );
729     }
730 }
731
732 /****************************************************************************
733  * Private functions
734  ****************************************************************************/
735
736 /****************************************************************************
737  * OpenVCDImage: try to open a vcd image from a .cue file
738  ****************************************************************************/
739 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
740                          vcddev_t *p_vcddev )
741 {
742     int i_ret = -1;
743     char *p_pos;
744     char *psz_vcdfile = NULL;
745     char *psz_cuefile = NULL;
746     FILE *cuefile;
747     char line[1024];
748
749     /* Check if we are dealing with a .cue file */
750     p_pos = strrchr( psz_dev, '.' );
751     if( p_pos && !strcmp( p_pos, ".cue" ) )
752     {
753         psz_cuefile = strdup( psz_dev );
754     }
755     else
756     {
757         /* psz_dev must be the actual vcd file. Let's assume there's a .cue
758          * file with the same filename */
759         if( p_pos )
760         {
761             psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
762             strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
763             strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
764         }
765         else
766         {
767             psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
768             sprintf( psz_cuefile, "%s.cue", psz_dev );
769         }
770     }
771
772     /* Open the cue file and try to parse it */
773     msg_Dbg( p_this,"using .cue file: %s", psz_cuefile );
774     cuefile = fopen( psz_cuefile, "rt" );
775     if( cuefile && fscanf( cuefile, "FILE %c", line ) &&
776         fgets( line, 1024, cuefile ) )
777     {
778         p_pos = strchr( line, '"' );
779         if( p_pos )
780         {
781             *p_pos = 0;
782
783             /* Take care of path standardization */
784             if( *line != '/' && (p_pos = strrchr( psz_cuefile, '/' )) )
785             {
786                 psz_vcdfile = malloc( strlen(line) +
787                                       (p_pos - psz_cuefile + 1) + 1 );
788                 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
789                 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), line );
790             }
791             else psz_vcdfile = strdup( line );
792         }
793     }
794
795     if( psz_vcdfile )
796     {
797         msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
798         p_vcddev->i_vcdimage_handle = open( psz_vcdfile,
799                                         O_RDONLY | O_NONBLOCK | O_BINARY );
800         i_ret = (p_vcddev->i_vcdimage_handle == -1) ? -1 : 0;
801     }
802
803     /* Try to parse the i_tracks and p_sectors info so we can just forget
804      * about the cuefile */
805     if( i_ret == 0 )
806     {
807         int p_sectors[100];
808         int i_tracks = 0;
809         int i_num;
810         char psz_dummy[10];
811
812         while( fgets( line, 1024, cuefile ) )
813         {
814             /* look for a TRACK line */
815             if( !sscanf( line, "%9s", psz_dummy ) ||
816                 strcmp(psz_dummy, "TRACK") )
817                 continue;
818
819             /* look for an INDEX line */
820             while( fgets( line, 1024, cuefile ) )
821             {
822                 int i_min, i_sec, i_frame;
823
824                 if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
825                             &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
826                     continue;
827
828                 i_tracks++;
829                 p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
830                 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
831                          i_tracks - 1, p_sectors[i_tracks - 1] );
832                 break;
833             }
834         }
835
836         /* fill in the last entry */
837         p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
838                                 / VCD_SECTOR_SIZE;
839         msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
840                  i_tracks, p_sectors[i_tracks] );
841         p_vcddev->i_tracks = i_tracks;
842         p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
843         memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
844
845     }
846
847     if( cuefile ) fclose( cuefile );
848     if( psz_cuefile ) free( psz_cuefile );
849     if( psz_vcdfile ) free( psz_vcdfile );
850
851     return i_ret;
852 }
853
854 /****************************************************************************
855  * CloseVCDImage: closes a vcd image opened by OpenVCDImage
856  ****************************************************************************/
857 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
858 {
859     if( p_vcddev->i_vcdimage_handle != -1 )
860         close( p_vcddev->i_vcdimage_handle );
861     else
862         return;
863
864     if( p_vcddev->p_sectors )
865         free( p_vcddev->p_sectors );
866 }
867
868 #if defined( SYS_DARWIN )
869 /****************************************************************************
870  * darwin_getTOC: get the TOC
871  ****************************************************************************/
872 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
873 {
874     mach_port_t port;
875     char *psz_devname;
876     kern_return_t ret;
877     CDTOC *pTOC = NULL;
878     io_iterator_t iterator;
879     io_registry_entry_t service;
880     CFMutableDictionaryRef properties;
881     CFDataRef data;
882
883     /* get the device name */
884     if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
885         ++psz_devname;
886     else
887         psz_devname = p_vcddev->psz_dev;
888
889     /* unraw the device name */
890     if( *psz_devname == 'r' )
891         ++psz_devname;
892
893     /* get port for IOKit communication */
894     if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
895     {
896         msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
897         return( NULL );
898     }
899
900     /* get service iterator for the device */
901     if( ( ret = IOServiceGetMatchingServices( 
902                     port, IOBSDNameMatching( port, 0, psz_devname ),
903                     &iterator ) ) != KERN_SUCCESS )
904     {
905         msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
906         return( NULL );
907     }
908
909     /* first service */
910     service = IOIteratorNext( iterator );
911     IOObjectRelease( iterator );
912
913     /* search for kIOCDMediaClass */ 
914     while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
915     {
916         if( ( ret = IORegistryEntryGetParentIterator( service, 
917                         kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
918         {
919             msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
920             IOObjectRelease( service );
921             return( NULL );
922         }
923
924         IOObjectRelease( service );
925         service = IOIteratorNext( iterator );
926         IOObjectRelease( iterator );
927     }
928
929     if( service == NULL )
930     {
931         msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
932         return( NULL );
933     }
934
935     /* create a CF dictionary containing the TOC */
936     if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
937                     kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
938     {
939         msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
940         IOObjectRelease( service );
941         return( NULL );
942     }
943
944     /* get the TOC from the dictionary */
945     if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
946                                     CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
947     {
948         CFRange range;
949         CFIndex buf_len;
950
951         buf_len = CFDataGetLength( data ) + 1;
952         range = CFRangeMake( 0, buf_len );
953
954         if( ( pTOC = (CDTOC *)malloc( buf_len ) ) != NULL )
955         {
956             CFDataGetBytes( data, range, (u_char *)pTOC );
957         }
958     }
959     else
960     {
961         msg_Err( p_this, "CFDictionaryGetValue failed" );
962     }
963
964     CFRelease( properties );
965     IOObjectRelease( service ); 
966
967     return( pTOC ); 
968 }
969
970 /****************************************************************************
971  * darwin_getNumberOfDescriptors: get number of descriptors in TOC 
972  ****************************************************************************/
973 static int darwin_getNumberOfDescriptors( CDTOC *pTOC )
974 {
975     int i_descriptors;
976
977     /* get TOC length */
978     i_descriptors = pTOC->length;
979
980     /* remove the first and last session */
981     i_descriptors -= ( sizeof(pTOC->sessionFirst) +
982                        sizeof(pTOC->sessionLast) );
983
984     /* divide the length by the size of a single descriptor */
985     i_descriptors /= sizeof(CDTOCDescriptor);
986
987     return( i_descriptors );
988 }
989
990 /****************************************************************************
991  * darwin_getNumberOfTracks: get number of tracks in TOC 
992  ****************************************************************************/
993 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
994 {
995     u_char track;
996     int i, i_tracks = 0; 
997     CDTOCDescriptor *pTrackDescriptors;
998
999     pTrackDescriptors = pTOC->descriptors;
1000
1001     for( i = i_descriptors; i >= 0; i-- )
1002     {
1003         track = pTrackDescriptors[i].point;
1004
1005         if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1006             continue;
1007
1008         i_tracks++; 
1009     }
1010
1011     return( i_tracks );
1012 }
1013 #endif /* SYS_DARWIN */
1014
1015 #if defined( WIN32 )
1016 /*****************************************************************************
1017  * win32_vcd_open: open vcd drive
1018  *****************************************************************************
1019  * Load and use aspi if it is available, otherwise use IOCTLs on WinNT/2K/XP.
1020  *****************************************************************************/
1021 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1022                            vcddev_t *p_vcddev )
1023 {
1024     /* Initializations */
1025     p_vcddev->h_device_handle = NULL;
1026     p_vcddev->i_sid = 0;
1027     p_vcddev->hASPI = 0;
1028     p_vcddev->lpSendCommand = 0;
1029
1030     if( WIN_NT )
1031     {
1032         char psz_win32_drive[7];
1033
1034         msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1035
1036         sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1037
1038         p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
1039                                             FILE_SHARE_READ | FILE_SHARE_WRITE,
1040                                             NULL, OPEN_EXISTING,
1041                                             FILE_FLAG_NO_BUFFERING |
1042                                             FILE_FLAG_RANDOM_ACCESS, NULL );
1043         return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1044     }
1045     else
1046     {
1047         HMODULE hASPI = NULL;
1048         long (*lpGetSupport)( void ) = NULL;
1049         long (*lpSendCommand)( void* ) = NULL;
1050         DWORD dwSupportInfo;
1051         int i, j, i_hostadapters;
1052         char c_drive = psz_dev[0];
1053
1054         hASPI = LoadLibrary( "wnaspi32.dll" );
1055         if( hASPI != NULL )
1056         {
1057             (FARPROC) lpGetSupport = GetProcAddress( hASPI,
1058                                                      "GetASPI32SupportInfo" );
1059             (FARPROC) lpSendCommand = GetProcAddress( hASPI,
1060                                                       "SendASPI32Command" );
1061         }
1062
1063         if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL )
1064         {
1065             msg_Dbg( p_this,
1066                      "unable to load aspi or get aspi function pointers" );
1067             if( hASPI ) FreeLibrary( hASPI );
1068             return -1;
1069         }
1070
1071         /* ASPI support seems to be there */
1072
1073         dwSupportInfo = lpGetSupport();
1074
1075         if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
1076         {
1077             msg_Dbg( p_this, "no host adapters found (aspi)" );
1078             FreeLibrary( hASPI );
1079             return -1;
1080         }
1081
1082         if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
1083         {
1084             msg_Dbg( p_this, "unable to initalize aspi layer" );
1085             FreeLibrary( hASPI );
1086             return -1;
1087         }
1088
1089         i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
1090         if( i_hostadapters == 0 )
1091         {
1092             FreeLibrary( hASPI );
1093             return -1;
1094         }
1095
1096         c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
1097
1098         for( i = 0; i < i_hostadapters; i++ )
1099         {
1100           for( j = 0; j < 15; j++ )
1101           {
1102               struct SRB_GetDiskInfo srbDiskInfo;
1103
1104               srbDiskInfo.SRB_Cmd         = SC_GET_DISK_INFO;
1105               srbDiskInfo.SRB_HaId        = i;
1106               srbDiskInfo.SRB_Flags       = 0;
1107               srbDiskInfo.SRB_Hdr_Rsvd    = 0;
1108               srbDiskInfo.SRB_Target      = j;
1109               srbDiskInfo.SRB_Lun         = 0;
1110
1111               lpSendCommand( (void*) &srbDiskInfo );
1112
1113               if( (srbDiskInfo.SRB_Status == SS_COMP) &&
1114                   (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
1115               {
1116                   /* Make sure this is a cdrom device */
1117                   struct SRB_GDEVBlock   srbGDEVBlock;
1118
1119                   memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
1120                   srbGDEVBlock.SRB_Cmd    = SC_GET_DEV_TYPE;
1121                   srbGDEVBlock.SRB_HaId   = i;
1122                   srbGDEVBlock.SRB_Target = j;
1123
1124                   lpSendCommand( (void*) &srbGDEVBlock );
1125
1126                   if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
1127                       ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) )
1128                   {
1129                       p_vcddev->i_sid = MAKEWORD( i, j );
1130                       p_vcddev->hASPI = (long)hASPI;
1131                       p_vcddev->lpSendCommand = lpSendCommand;
1132                       msg_Dbg( p_this, "using aspi layer" );
1133
1134                       return 0;
1135                   }
1136                   else
1137                   {
1138                       FreeLibrary( hASPI );
1139                       msg_Dbg( p_this, "%s: is not a cdrom drive",
1140                                psz_dev[0] );
1141                       return -1;
1142                   }
1143               }
1144           }
1145         }
1146
1147         FreeLibrary( hASPI );
1148         msg_Dbg( p_this, "unable to get haid and target (aspi)" );
1149
1150     }
1151
1152     return -1;
1153 }
1154
1155 #endif /* WIN32 */