1 /****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1998-2001 the VideoLAN team
7 * Authors: Johan Bilien <jobi@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_access.h>
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
51 #if defined( SYS_BSDI )
53 #elif defined ( __APPLE__ )
54 # include <CoreFoundation/CFBase.h>
55 # include <IOKit/IOKitLib.h>
56 # include <IOKit/storage/IOCDTypes.h>
57 # include <IOKit/storage/IOCDMedia.h>
58 # include <IOKit/storage/IOCDMediaBSDClient.h>
59 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
60 # include <sys/inttypes.h>
61 # include <sys/cdio.h>
62 # include <sys/scsiio.h>
63 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
64 # include <sys/cdio.h>
65 # include <sys/cdrio.h>
66 #elif defined( WIN32 )
68 # include <winioctl.h>
69 #elif defined (__linux__)
70 # include <sys/ioctl.h>
71 # include <linux/cdrom.h>
76 #include "cdrom_internals.h"
78 #include <vlc_charset.h>
80 /*****************************************************************************
81 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
82 *****************************************************************************/
83 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
92 if( !psz_dev ) return NULL;
95 * Initialize structure with default values
97 p_vcddev = (vcddev_t *)malloc( sizeof(vcddev_t) );
98 if( p_vcddev == NULL )
100 p_vcddev->i_vcdimage_handle = -1;
101 p_vcddev->psz_dev = NULL;
105 * Check if we are dealing with a device or a file (vcd image)
108 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
114 if( stat( psz_dev, &fileinfo ) < 0 )
120 /* Check if this is a block/char device */
121 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
127 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
132 * open the vcd device
136 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
138 p_vcddev->i_device_handle = -1;
139 p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
140 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
146 p_vcddev->psz_dev = (char *)strdup( psz_dev );
157 /*****************************************************************************
158 * ioctl_Close: Closes an already opened VCD device or file.
159 *****************************************************************************/
160 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
162 free( p_vcddev->psz_dev );
164 if( p_vcddev->i_vcdimage_handle != -1 )
170 CloseVCDImage( p_this, p_vcddev );
179 if( p_vcddev->h_device_handle )
180 CloseHandle( p_vcddev->h_device_handle );
181 if( p_vcddev->hASPI )
182 FreeLibrary( (HMODULE)p_vcddev->hASPI );
184 if( p_vcddev->i_device_handle != -1 )
185 close( p_vcddev->i_device_handle );
189 /*****************************************************************************
190 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
191 * if pp_sectors is not null and return the number of
193 *****************************************************************************/
194 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
199 if( p_vcddev->i_vcdimage_handle != -1 )
205 i_tracks = p_vcddev->i_tracks;
209 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
210 if( *pp_sectors == NULL )
212 memcpy( *pp_sectors, p_vcddev->p_sectors,
213 (i_tracks + 1) * sizeof(int) );
225 #if defined( __APPLE__ )
230 if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
232 msg_Err( p_this, "failed to get the TOC" );
236 i_descriptors = CDTOCGetDescriptorCount( pTOC );
237 i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
241 int i, i_leadout = -1;
242 CDTOCDescriptor *pTrackDescriptors;
245 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
246 if( *pp_sectors == NULL )
248 darwin_freeTOC( pTOC );
252 pTrackDescriptors = pTOC->descriptors;
254 for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
256 track = pTrackDescriptors[i].point;
261 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
264 (*pp_sectors)[i_tracks++] =
265 CDConvertMSFToLBA( pTrackDescriptors[i].p );
268 if( i_leadout == -1 )
270 msg_Err( p_this, "leadout not found" );
272 darwin_freeTOC( pTOC );
276 /* set leadout sector */
277 (*pp_sectors)[i_tracks] =
278 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
281 darwin_freeTOC( pTOC );
283 #elif defined( WIN32 )
284 if( p_vcddev->hASPI )
287 struct SRB_ExecSCSICmd ssc;
288 uint8_t p_tocheader[ 4 ];
290 /* Create the transfer completion event */
291 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
297 memset( &ssc, 0, sizeof( ssc ) );
299 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
300 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
301 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
302 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
303 ssc.SRB_SenseLen = SENSE_LEN;
305 ssc.SRB_PostProc = (LPVOID) hEvent;
309 ssc.CDBByte[ 0 ] = READ_TOC;
312 ssc.CDBByte[ 2 ] = READ_TOC_FORMAT_TOC;
315 ssc.CDBByte[ 6 ] = 0;
317 /* Allocation length and buffer */
318 ssc.SRB_BufLen = sizeof( p_tocheader );
319 ssc.SRB_BufPointer = p_tocheader;
320 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
321 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
323 /* Initiate transfer */
324 ResetEvent( hEvent );
325 p_vcddev->lpSendCommand( (void*) &ssc );
327 /* If the command has still not been processed, wait until it's
329 if( ssc.SRB_Status == SS_PENDING )
330 WaitForSingleObject( hEvent, INFINITE );
332 /* check that the transfer went as planned */
333 if( ssc.SRB_Status != SS_COMP )
335 CloseHandle( hEvent );
339 i_tracks = p_tocheader[3] - p_tocheader[2] + 1;
346 i_toclength = 4 /* header */ + p_tocheader[0] +
347 ((unsigned int)p_tocheader[1] << 8);
349 p_fulltoc = malloc( i_toclength );
350 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
352 if( *pp_sectors == NULL || p_fulltoc == NULL )
356 CloseHandle( hEvent );
360 /* Allocation length and buffer */
361 ssc.SRB_BufLen = i_toclength;
362 ssc.SRB_BufPointer = p_fulltoc;
363 ssc.CDBByte[ 7 ] = ( ssc.SRB_BufLen >> 8 ) & 0xff;
364 ssc.CDBByte[ 8 ] = ( ssc.SRB_BufLen ) & 0xff;
366 /* Initiate transfer */
367 ResetEvent( hEvent );
368 p_vcddev->lpSendCommand( (void*) &ssc );
370 /* If the command has still not been processed, wait until it's
372 if( ssc.SRB_Status == SS_PENDING )
373 WaitForSingleObject( hEvent, INFINITE );
375 /* check that the transfer went as planned */
376 if( ssc.SRB_Status != SS_COMP )
379 for( i = 0 ; i <= i_tracks ; i++ )
381 int i_index = 8 + 8 * i;
382 (*pp_sectors)[ i ] = ((int)p_fulltoc[ i_index ] << 24) +
383 ((int)p_fulltoc[ i_index+1 ] << 16) +
384 ((int)p_fulltoc[ i_index+2 ] << 8) +
385 (int)p_fulltoc[ i_index+3 ];
387 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
393 CloseHandle( hEvent );
398 DWORD dwBytesReturned;
401 if( DeviceIoControl( p_vcddev->h_device_handle,
402 IOCTL_CDROM_READ_TOC,
403 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
404 &dwBytesReturned, NULL ) == 0 )
406 msg_Err( p_this, "could not read TOCHDR" );
410 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
416 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
417 if( *pp_sectors == NULL )
420 for( i = 0 ; i <= i_tracks ; i++ )
422 (*pp_sectors)[ i ] = MSF_TO_LBA2(
423 cdrom_toc.TrackData[i].Address[1],
424 cdrom_toc.TrackData[i].Address[2],
425 cdrom_toc.TrackData[i].Address[3] );
426 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
431 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
432 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
433 struct ioc_toc_header tochdr;
434 struct ioc_read_toc_entry toc_entries;
436 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
439 msg_Err( p_this, "could not read TOCHDR" );
443 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
449 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
450 if( *pp_sectors == NULL )
453 toc_entries.address_format = CD_LBA_FORMAT;
454 toc_entries.starting_track = 0;
455 toc_entries.data_len = ( i_tracks + 1 ) *
456 sizeof( struct cd_toc_entry );
457 toc_entries.data = (struct cd_toc_entry *)
458 malloc( toc_entries.data_len );
459 if( toc_entries.data == NULL )
466 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
467 &toc_entries ) == -1 )
469 msg_Err( p_this, "could not read the TOC" );
471 free( toc_entries.data );
475 /* Fill the p_sectors structure with the track/sector matches */
476 for( i = 0 ; i <= i_tracks ; i++ )
478 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
479 /* FIXME: is this ok? */
480 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
482 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
487 struct cdrom_tochdr tochdr;
488 struct cdrom_tocentry tocent;
490 /* First we read the TOC header */
491 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
494 msg_Err( p_this, "could not read TOCHDR" );
498 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
504 *pp_sectors = malloc( (i_tracks + 1) * sizeof(int) );
505 if( *pp_sectors == NULL )
508 /* Fill the p_sectors structure with the track/sector matches */
509 for( i = 0 ; i <= i_tracks ; i++ )
511 tocent.cdte_format = CDROM_LBA;
513 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
515 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
518 msg_Err( p_this, "could not read TOCENTRY" );
523 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
532 /****************************************************************************
533 * ioctl_ReadSector: Read VCD or CDDA sectors
534 ****************************************************************************/
535 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
536 int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
541 if( i_type == VCD_TYPE ) p_block = malloc( VCD_SECTOR_SIZE * i_nb );
542 else p_block = p_buffer;
544 if( p_vcddev->i_vcdimage_handle != -1 )
549 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
552 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
553 if( i_type == VCD_TYPE ) free( p_block );
557 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
560 msg_Err( p_this, "Could not read sector %d", i_sector );
561 if( i_type == VCD_TYPE ) free( p_block );
573 #if defined( __APPLE__ )
574 dk_cd_read_t cd_read;
576 memset( &cd_read, 0, sizeof(cd_read) );
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;
584 cd_read.buffer = p_block;
585 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
587 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
589 msg_Err( p_this, "could not read block %d", i_sector );
590 if( i_type == VCD_TYPE ) free( p_block );
594 #elif defined( WIN32 )
595 if( p_vcddev->hASPI )
598 struct SRB_ExecSCSICmd ssc;
600 /* Create the transfer completion event */
601 hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
604 if( i_type == VCD_TYPE ) free( p_block );
608 memset( &ssc, 0, sizeof( ssc ) );
610 ssc.SRB_Cmd = SC_EXEC_SCSI_CMD;
611 ssc.SRB_Flags = SRB_DIR_IN | SRB_EVENT_NOTIFY;
612 ssc.SRB_HaId = LOBYTE( p_vcddev->i_sid );
613 ssc.SRB_Target = HIBYTE( p_vcddev->i_sid );
614 ssc.SRB_SenseLen = SENSE_LEN;
616 ssc.SRB_PostProc = (LPVOID) hEvent;
620 ssc.CDBByte[ 0 ] = READ_CD;
623 ssc.CDBByte[ 1 ] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2 :
627 ssc.CDBByte[ 2 ] = ( i_sector >> 24 ) & 0xff;
628 ssc.CDBByte[ 3 ] = ( i_sector >> 16 ) & 0xff;
629 ssc.CDBByte[ 4 ] = ( i_sector >> 8 ) & 0xff;
630 ssc.CDBByte[ 5 ] = ( i_sector ) & 0xff;
632 /* Transfer length */
633 ssc.CDBByte[ 6 ] = ( i_nb >> 16 ) & 0xff;
634 ssc.CDBByte[ 7 ] = ( i_nb >> 8 ) & 0xff;
635 ssc.CDBByte[ 8 ] = ( i_nb ) & 0xff;
638 ssc.CDBByte[ 9 ] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 :
642 ssc.SRB_BufPointer = p_block;
643 ssc.SRB_BufLen = VCD_SECTOR_SIZE * i_nb;
645 /* Initiate transfer */
646 ResetEvent( hEvent );
647 p_vcddev->lpSendCommand( (void*) &ssc );
649 /* If the command has still not been processed, wait until it's
651 if( ssc.SRB_Status == SS_PENDING )
653 WaitForSingleObject( hEvent, INFINITE );
655 CloseHandle( hEvent );
657 /* check that the transfer went as planned */
658 if( ssc.SRB_Status != SS_COMP )
660 if( i_type == VCD_TYPE ) free( p_block );
666 DWORD dwBytesReturned;
667 RAW_READ_INFO cdrom_raw;
669 /* Initialize CDROM_RAW_READ structure */
670 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
671 cdrom_raw.SectorCount = i_nb;
672 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
674 if( DeviceIoControl( p_vcddev->h_device_handle,
675 IOCTL_CDROM_RAW_READ, &cdrom_raw,
676 sizeof(RAW_READ_INFO), p_block,
677 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
680 if( i_type == VCD_TYPE )
682 /* Retry in YellowMode2 */
683 cdrom_raw.TrackMode = YellowMode2;
684 if( DeviceIoControl( p_vcddev->h_device_handle,
685 IOCTL_CDROM_RAW_READ, &cdrom_raw,
686 sizeof(RAW_READ_INFO), p_block,
687 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
698 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
702 memset( &sc, 0, sizeof(sc) );
704 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
706 sc.cmd[2] = (i_sector >> 24) & 0xff;
707 sc.cmd[3] = (i_sector >> 16) & 0xff;
708 sc.cmd[4] = (i_sector >> 8) & 0xff;
709 sc.cmd[5] = (i_sector >> 0) & 0xff;
710 sc.cmd[6] = (i_nb >> 16) & 0xff;
711 sc.cmd[7] = (i_nb >> 8) & 0xff;
712 sc.cmd[8] = (i_nb ) & 0xff;
713 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
714 sc.cmd[10] = 0; /* sub channel */
716 sc.databuf = (caddr_t)p_block;
717 sc.datalen = VCD_SECTOR_SIZE * i_nb;
718 sc.senselen = sizeof( sc.sense );
719 sc.flags = SCCMD_READ;
722 i_ret = ioctl( i_fd, SCIOCCOMMAND, &sc );
725 msg_Err( p_this, "SCIOCCOMMAND failed" );
726 if( i_type == VCD_TYPE ) free( p_block );
729 if( sc.retsts || sc.error )
731 msg_Err( p_this, "SCSI command failed: status %d error %d\n",
732 sc.retsts, sc.error );
733 if( i_type == VCD_TYPE ) free( p_block );
737 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
738 int i_size = VCD_SECTOR_SIZE;
740 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
743 msg_Err( p_this, "Could not set block size" );
744 if( i_type == VCD_TYPE ) free( p_block );
748 if( lseek( p_vcddev->i_device_handle,
749 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
751 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
752 if( i_type == VCD_TYPE ) free( p_block );
756 if( read( p_vcddev->i_device_handle,
757 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
759 msg_Err( p_this, "Could not read sector %d", i_sector );
760 if( i_type == VCD_TYPE ) free( p_block );
765 for( i = 0; i < i_nb; i++ )
767 int i_dummy = i_sector + i + 2 * CD_FRAMES;
769 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
770 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
771 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
772 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
775 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
776 p_block + i * VCD_SECTOR_SIZE ) == -1 )
778 msg_Err( p_this, "could not read block %i from disc",
783 if( i_type == VCD_TYPE ) free( p_block );
792 /* For VCDs, we don't want to keep the header and footer of the
794 if( i_type == VCD_TYPE )
796 for( i = 0; i < i_nb; i++ )
798 memcpy( p_buffer + i * VCD_DATA_SIZE,
799 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
808 /****************************************************************************
810 ****************************************************************************/
812 /****************************************************************************
813 * OpenVCDImage: try to open a vcd image from a .cue file
814 ****************************************************************************/
815 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
820 char *psz_vcdfile = NULL;
821 char *psz_cuefile = NULL;
822 FILE *cuefile = NULL;
825 /* Check if we are dealing with a .cue file */
826 p_pos = strrchr( psz_dev, '.' );
827 if( p_pos && !strcmp( p_pos, ".cue" ) )
829 /* psz_dev must be the cue file. Let's assume there's a .bin
830 * file with the same filename */
833 psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
834 strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
835 strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
839 psz_vcdfile = malloc( strlen(psz_dev) + 5 /* ".bin" */ );
840 sprintf( psz_vcdfile, "%s.bin", psz_dev );
842 psz_cuefile = strdup( psz_dev );
846 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
847 * file with the same filename */
850 psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
851 strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
852 strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
856 psz_cuefile = malloc( strlen(psz_dev) + 5 /* ".cue" */ );
857 sprintf( psz_cuefile, "%s.cue", psz_dev );
859 /* If we need to look up the .cue file, then we don't have to look for the vcd */
860 psz_vcdfile = strdup( psz_dev );
863 /* Open the cue file and try to parse it */
864 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
865 cuefile = utf8_fopen( psz_cuefile, "rt" );
866 if( cuefile == NULL )
869 msg_Dbg( p_this, "could not find .cue file" );
873 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
874 p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
875 O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
877 if( p_vcddev->i_vcdimage_handle == -1 &&
878 fscanf( cuefile, "FILE %c", line ) &&
879 fgets( line, 1024, cuefile ) )
881 /* We have a cue file, but no valid vcd file yet */
883 p_pos = strchr( line, '"' );
888 /* Take care of path standardization */
889 if( *line != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
890 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
892 psz_vcdfile = malloc( strlen(line) +
893 (p_pos - psz_cuefile + 1) + 1 );
894 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
895 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), line );
897 else psz_vcdfile = strdup( line );
899 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
900 p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
901 O_RDONLY | O_NONBLOCK | O_BINARY, 0666 );
904 if( p_vcddev->i_vcdimage_handle == -1)
911 /* Try to parse the i_tracks and p_sectors info so we can just forget
912 * about the cuefile */
920 while( fgets( line, 1024, cuefile ) )
922 /* look for a TRACK line */
923 if( !sscanf( line, "%9s", psz_dummy ) ||
924 strcmp(psz_dummy, "TRACK") )
927 /* look for an INDEX line */
928 while( fgets( line, 1024, cuefile ) )
930 int i_min, i_sec, i_frame;
932 if( (sscanf( line, "%9s %2u %2u:%2u:%2u", psz_dummy, &i_num,
933 &i_min, &i_sec, &i_frame ) != 5) || (i_num != 1) )
937 p_sectors[i_tracks - 1] = MSF_TO_LBA(i_min, i_sec, i_frame);
938 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
939 i_tracks - 1, p_sectors[i_tracks - 1] );
944 /* fill in the last entry */
945 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
947 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
948 i_tracks, p_sectors[i_tracks] );
949 p_vcddev->i_tracks = i_tracks;
950 p_vcddev->p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
951 memcpy( p_vcddev->p_sectors, p_sectors, (i_tracks + 1) * sizeof(int) );
956 if( cuefile ) fclose( cuefile );
963 /****************************************************************************
964 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
965 ****************************************************************************/
966 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
968 if( p_vcddev->i_vcdimage_handle != -1 )
969 close( p_vcddev->i_vcdimage_handle );
973 free( p_vcddev->p_sectors );
976 #if defined( __APPLE__ )
977 /****************************************************************************
978 * darwin_getTOC: get the TOC
979 ****************************************************************************/
980 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
986 io_iterator_t iterator;
987 io_registry_entry_t service;
988 CFMutableDictionaryRef properties;
991 /* get the device name */
992 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
995 psz_devname = p_vcddev->psz_dev;
997 /* unraw the device name */
998 if( *psz_devname == 'r' )
1001 /* get port for IOKit communication */
1002 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
1004 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
1008 /* get service iterator for the device */
1009 if( ( ret = IOServiceGetMatchingServices(
1010 port, IOBSDNameMatching( port, 0, psz_devname ),
1011 &iterator ) ) != KERN_SUCCESS )
1013 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
1018 service = IOIteratorNext( iterator );
1019 IOObjectRelease( iterator );
1021 /* search for kIOCDMediaClass */
1022 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
1024 if( ( ret = IORegistryEntryGetParentIterator( service,
1025 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
1027 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
1028 IOObjectRelease( service );
1032 IOObjectRelease( service );
1033 service = IOIteratorNext( iterator );
1034 IOObjectRelease( iterator );
1037 if( service == NULL )
1039 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
1043 /* create a CF dictionary containing the TOC */
1044 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
1045 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
1047 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
1048 IOObjectRelease( service );
1052 /* get the TOC from the dictionary */
1053 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
1054 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
1059 buf_len = CFDataGetLength( data ) + 1;
1060 range = CFRangeMake( 0, buf_len );
1062 if( ( pTOC = (CDTOC *)malloc( buf_len ) ) != NULL )
1064 CFDataGetBytes( data, range, (u_char *)pTOC );
1069 msg_Err( p_this, "CFDictionaryGetValue failed" );
1072 CFRelease( properties );
1073 IOObjectRelease( service );
1078 /****************************************************************************
1079 * darwin_getNumberOfTracks: get number of tracks in TOC
1080 ****************************************************************************/
1081 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
1084 int i, i_tracks = 0;
1085 CDTOCDescriptor *pTrackDescriptors = NULL;
1087 pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
1089 for( i = i_descriptors; i > 0; i-- )
1091 track = pTrackDescriptors[i].point;
1093 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
1101 #endif /* __APPLE__ */
1103 #if defined( WIN32 )
1104 /*****************************************************************************
1105 * win32_vcd_open: open vcd drive
1106 *****************************************************************************
1107 * Load and use aspi if it is available, otherwise use IOCTLs on WinNT/2K/XP.
1108 *****************************************************************************/
1109 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
1110 vcddev_t *p_vcddev )
1112 /* Initializations */
1113 p_vcddev->h_device_handle = NULL;
1114 p_vcddev->i_sid = 0;
1115 p_vcddev->hASPI = 0;
1116 p_vcddev->lpSendCommand = 0;
1120 char psz_win32_drive[7];
1122 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
1124 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
1126 p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
1127 FILE_SHARE_READ | FILE_SHARE_WRITE,
1128 NULL, OPEN_EXISTING,
1129 FILE_FLAG_NO_BUFFERING |
1130 FILE_FLAG_RANDOM_ACCESS, NULL );
1131 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
1135 HMODULE hASPI = NULL;
1136 long (*lpGetSupport)( void ) = NULL;
1137 long (*lpSendCommand)( void* ) = NULL;
1138 DWORD dwSupportInfo;
1139 int i, j, i_hostadapters;
1140 char c_drive = psz_dev[0];
1142 hASPI = LoadLibrary( "wnaspi32.dll" );
1145 lpGetSupport = (void *)GetProcAddress( hASPI, "GetASPI32SupportInfo" );
1146 lpSendCommand = (void *)GetProcAddress( hASPI, "SendASPI32Command" );
1149 if( hASPI == NULL || lpGetSupport == NULL || lpSendCommand == NULL )
1152 "unable to load aspi or get aspi function pointers" );
1153 if( hASPI ) FreeLibrary( hASPI );
1157 /* ASPI support seems to be there */
1159 dwSupportInfo = lpGetSupport();
1161 if( HIBYTE( LOWORD ( dwSupportInfo ) ) == SS_NO_ADAPTERS )
1163 msg_Dbg( p_this, "no host adapters found (aspi)" );
1164 FreeLibrary( hASPI );
1168 if( HIBYTE( LOWORD ( dwSupportInfo ) ) != SS_COMP )
1170 msg_Dbg( p_this, "unable to initalize aspi layer" );
1171 FreeLibrary( hASPI );
1175 i_hostadapters = LOBYTE( LOWORD( dwSupportInfo ) );
1176 if( i_hostadapters == 0 )
1178 FreeLibrary( hASPI );
1182 c_drive = c_drive > 'Z' ? c_drive - 'a' : c_drive - 'A';
1184 for( i = 0; i < i_hostadapters; i++ )
1186 for( j = 0; j < 15; j++ )
1188 struct SRB_GetDiskInfo srbDiskInfo;
1190 srbDiskInfo.SRB_Cmd = SC_GET_DISK_INFO;
1191 srbDiskInfo.SRB_HaId = i;
1192 srbDiskInfo.SRB_Flags = 0;
1193 srbDiskInfo.SRB_Hdr_Rsvd = 0;
1194 srbDiskInfo.SRB_Target = j;
1195 srbDiskInfo.SRB_Lun = 0;
1197 lpSendCommand( (void*) &srbDiskInfo );
1199 if( (srbDiskInfo.SRB_Status == SS_COMP) &&
1200 (srbDiskInfo.SRB_Int13HDriveInfo == c_drive) )
1202 /* Make sure this is a cdrom device */
1203 struct SRB_GDEVBlock srbGDEVBlock;
1205 memset( &srbGDEVBlock, 0, sizeof(struct SRB_GDEVBlock) );
1206 srbGDEVBlock.SRB_Cmd = SC_GET_DEV_TYPE;
1207 srbGDEVBlock.SRB_HaId = i;
1208 srbGDEVBlock.SRB_Target = j;
1210 lpSendCommand( (void*) &srbGDEVBlock );
1212 if( ( srbGDEVBlock.SRB_Status == SS_COMP ) &&
1213 ( srbGDEVBlock.SRB_DeviceType == DTYPE_CDROM ) )
1215 p_vcddev->i_sid = MAKEWORD( i, j );
1216 p_vcddev->hASPI = (long)hASPI;
1217 p_vcddev->lpSendCommand = lpSendCommand;
1218 msg_Dbg( p_this, "using aspi layer" );
1224 FreeLibrary( hASPI );
1225 msg_Dbg( p_this, "%c: is not a cdrom drive",
1233 FreeLibrary( hASPI );
1234 msg_Dbg( p_this, "unable to get haid and target (aspi)" );