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>
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
51 #ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h>
55 #if defined( SYS_BSDI )
57 #elif defined ( __APPLE__ )
58 # include <CoreFoundation/CFBase.h>
59 # include <IOKit/IOKitLib.h>
60 # include <IOKit/storage/IOCDTypes.h>
61 # include <IOKit/storage/IOCDMedia.h>
62 # include <IOKit/storage/IOCDMediaBSDClient.h>
63 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
64 # include <inttypes.h>
65 # include <sys/cdio.h>
66 # include <sys/scsiio.h>
67 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
68 # include <sys/cdio.h>
69 # include <sys/cdrio.h>
70 #elif defined( WIN32 )
72 # include <winioctl.h>
73 #elif defined (__linux__)
74 # include <sys/ioctl.h>
75 # include <linux/cdrom.h>
80 #include "cdrom_internals.h"
82 #include <vlc_charset.h>
85 /*****************************************************************************
86 * ioctl_Open: Opens a VCD device or file and returns an opaque handle
87 *****************************************************************************/
88 vcddev_t *ioctl_Open( vlc_object_t *p_this, const char *psz_dev )
97 if( !psz_dev ) return NULL;
100 * Initialize structure with default values
102 p_vcddev = malloc( sizeof(*p_vcddev) );
103 if( p_vcddev == NULL )
105 p_vcddev->i_vcdimage_handle = -1;
106 p_vcddev->psz_dev = NULL;
110 * Check if we are dealing with a device or a file (vcd image)
113 if( (strlen( psz_dev ) == 2 && psz_dev[1] == ':') )
119 if( stat( psz_dev, &fileinfo ) < 0 )
125 /* Check if this is a block/char device */
126 if( S_ISBLK( fileinfo.st_mode ) || S_ISCHR( fileinfo.st_mode ) )
132 i_ret = OpenVCDImage( p_this, psz_dev, p_vcddev );
137 * open the vcd device
141 i_ret = win32_vcd_open( p_this, psz_dev, p_vcddev );
143 p_vcddev->i_device_handle = -1;
144 p_vcddev->i_device_handle = open( psz_dev, O_RDONLY | O_NONBLOCK );
145 i_ret = (p_vcddev->i_device_handle == -1) ? -1 : 0;
151 p_vcddev->psz_dev = (char *)strdup( psz_dev );
162 /*****************************************************************************
163 * ioctl_Close: Closes an already opened VCD device or file.
164 *****************************************************************************/
165 void ioctl_Close( vlc_object_t * p_this, vcddev_t *p_vcddev )
167 free( p_vcddev->psz_dev );
169 if( p_vcddev->i_vcdimage_handle != -1 )
175 CloseVCDImage( p_this, p_vcddev );
184 if( p_vcddev->h_device_handle )
185 CloseHandle( p_vcddev->h_device_handle );
187 if( p_vcddev->i_device_handle != -1 )
188 close( p_vcddev->i_device_handle );
193 /*****************************************************************************
194 * ioctl_GetTracksMap: Read the Table of Content, fill in the pp_sectors map
195 * if pp_sectors is not null and return the number of
197 *****************************************************************************/
198 int ioctl_GetTracksMap( vlc_object_t *p_this, const vcddev_t *p_vcddev,
203 if( p_vcddev->i_vcdimage_handle != -1 )
209 i_tracks = p_vcddev->i_tracks;
213 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
214 if( *pp_sectors == NULL )
216 memcpy( *pp_sectors, p_vcddev->p_sectors,
217 (i_tracks + 1) * sizeof(**pp_sectors) );
229 #if defined( __APPLE__ )
234 if( ( pTOC = darwin_getTOC( p_this, p_vcddev ) ) == NULL )
236 msg_Err( p_this, "failed to get the TOC" );
240 i_descriptors = CDTOCGetDescriptorCount( pTOC );
241 i_tracks = darwin_getNumberOfTracks( pTOC, i_descriptors );
245 int i, i_leadout = -1;
246 CDTOCDescriptor *pTrackDescriptors;
249 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
250 if( *pp_sectors == NULL )
252 darwin_freeTOC( pTOC );
256 pTrackDescriptors = pTOC->descriptors;
258 for( i_tracks = 0, i = 0; i < i_descriptors; i++ )
260 track = pTrackDescriptors[i].point;
265 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
268 (*pp_sectors)[i_tracks++] =
269 CDConvertMSFToLBA( pTrackDescriptors[i].p );
272 if( i_leadout == -1 )
274 msg_Err( p_this, "leadout not found" );
276 darwin_freeTOC( pTOC );
280 /* set leadout sector */
281 (*pp_sectors)[i_tracks] =
282 CDConvertMSFToLBA( pTrackDescriptors[i_leadout].p );
285 darwin_freeTOC( pTOC );
287 #elif defined( WIN32 )
288 DWORD dwBytesReturned;
291 if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC,
292 NULL, 0, &cdrom_toc, sizeof(CDROM_TOC),
293 &dwBytesReturned, NULL ) == 0 )
295 msg_Err( p_this, "could not read TOCHDR" );
299 i_tracks = cdrom_toc.LastTrack - cdrom_toc.FirstTrack + 1;
303 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
304 if( *pp_sectors == NULL )
307 for( int i = 0 ; i <= i_tracks ; i++ )
309 (*pp_sectors)[ i ] = MSF_TO_LBA2(
310 cdrom_toc.TrackData[i].Address[1],
311 cdrom_toc.TrackData[i].Address[2],
312 cdrom_toc.TrackData[i].Address[3] );
313 msg_Dbg( p_this, "p_sectors: %i, %i", i, (*pp_sectors)[i]);
317 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) \
318 || defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
319 struct ioc_toc_header tochdr;
320 struct ioc_read_toc_entry toc_entries;
322 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCHEADER, &tochdr )
325 msg_Err( p_this, "could not read TOCHDR" );
329 i_tracks = tochdr.ending_track - tochdr.starting_track + 1;
335 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
336 if( *pp_sectors == NULL )
339 toc_entries.address_format = CD_LBA_FORMAT;
340 toc_entries.starting_track = 0;
341 toc_entries.data_len = ( i_tracks + 1 ) *
342 sizeof( struct cd_toc_entry );
343 toc_entries.data = (struct cd_toc_entry *)
344 malloc( toc_entries.data_len );
345 if( toc_entries.data == NULL )
352 if( ioctl( p_vcddev->i_device_handle, CDIOREADTOCENTRYS,
353 &toc_entries ) == -1 )
355 msg_Err( p_this, "could not read the TOC" );
357 free( toc_entries.data );
361 /* Fill the p_sectors structure with the track/sector matches */
362 for( i = 0 ; i <= i_tracks ; i++ )
364 #if defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
365 /* FIXME: is this ok? */
366 (*pp_sectors)[ i ] = toc_entries.data[i].addr.lba;
368 (*pp_sectors)[ i ] = ntohl( toc_entries.data[i].addr.lba );
373 struct cdrom_tochdr tochdr;
374 struct cdrom_tocentry tocent;
376 /* First we read the TOC header */
377 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCHDR, &tochdr )
380 msg_Err( p_this, "could not read TOCHDR" );
384 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
390 *pp_sectors = calloc( i_tracks + 1, sizeof(**pp_sectors) );
391 if( *pp_sectors == NULL )
394 /* Fill the p_sectors structure with the track/sector matches */
395 for( i = 0 ; i <= i_tracks ; i++ )
397 tocent.cdte_format = CDROM_LBA;
399 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
401 if( ioctl( p_vcddev->i_device_handle, CDROMREADTOCENTRY,
404 msg_Err( p_this, "could not read TOCENTRY" );
409 (*pp_sectors)[ i ] = tocent.cdte_addr.lba;
418 /****************************************************************************
419 * ioctl_ReadSector: Read VCD or CDDA sectors
420 ****************************************************************************/
421 int ioctl_ReadSectors( vlc_object_t *p_this, const vcddev_t *p_vcddev,
422 int i_sector, uint8_t *p_buffer, int i_nb, int i_type )
427 if( i_type == VCD_TYPE )
428 p_block = malloc( VCD_SECTOR_SIZE * i_nb );
432 if( p_vcddev->i_vcdimage_handle != -1 )
437 if( lseek( p_vcddev->i_vcdimage_handle, i_sector * VCD_SECTOR_SIZE,
440 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
441 if( i_type == VCD_TYPE ) free( p_block );
445 if( read( p_vcddev->i_vcdimage_handle, p_block, VCD_SECTOR_SIZE * i_nb)
448 msg_Err( p_this, "Could not read sector %d", i_sector );
449 if( i_type == VCD_TYPE ) free( p_block );
461 #if defined( __APPLE__ )
462 dk_cd_read_t cd_read;
464 memset( &cd_read, 0, sizeof(cd_read) );
466 cd_read.offset = i_sector * VCD_SECTOR_SIZE;
467 cd_read.sectorArea = kCDSectorAreaSync | kCDSectorAreaHeader |
468 kCDSectorAreaSubHeader | kCDSectorAreaUser |
469 kCDSectorAreaAuxiliary;
470 cd_read.sectorType = kCDSectorTypeUnknown;
472 cd_read.buffer = p_block;
473 cd_read.bufferLength = VCD_SECTOR_SIZE * i_nb;
475 if( ioctl( p_vcddev->i_device_handle, DKIOCCDREAD, &cd_read ) == -1 )
477 msg_Err( p_this, "could not read block %d", i_sector );
478 if( i_type == VCD_TYPE ) free( p_block );
482 #elif defined( WIN32 )
483 DWORD dwBytesReturned;
484 RAW_READ_INFO cdrom_raw;
486 /* Initialize CDROM_RAW_READ structure */
487 cdrom_raw.DiskOffset.QuadPart = CD_SECTOR_SIZE * i_sector;
488 cdrom_raw.SectorCount = i_nb;
489 cdrom_raw.TrackMode = i_type == VCD_TYPE ? XAForm2 : CDDA;
491 if( DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_RAW_READ,
492 &cdrom_raw, sizeof(RAW_READ_INFO), p_block,
493 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
496 if( i_type == VCD_TYPE )
498 /* Retry in YellowMode2 */
499 cdrom_raw.TrackMode = YellowMode2;
500 if( DeviceIoControl( p_vcddev->h_device_handle,
501 IOCTL_CDROM_RAW_READ, &cdrom_raw,
502 sizeof(RAW_READ_INFO), p_block,
503 VCD_SECTOR_SIZE * i_nb, &dwBytesReturned,
513 #elif defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
517 memset( &sc, 0, sizeof(sc) );
519 sc.cmd[1] = i_type == VCD_TYPE ? SECTOR_TYPE_MODE2_FORM2:
521 sc.cmd[2] = (i_sector >> 24) & 0xff;
522 sc.cmd[3] = (i_sector >> 16) & 0xff;
523 sc.cmd[4] = (i_sector >> 8) & 0xff;
524 sc.cmd[5] = (i_sector >> 0) & 0xff;
525 sc.cmd[6] = (i_nb >> 16) & 0xff;
526 sc.cmd[7] = (i_nb >> 8) & 0xff;
527 sc.cmd[8] = (i_nb ) & 0xff;
528 sc.cmd[9] = i_type == VCD_TYPE ? READ_CD_RAW_MODE2 : READ_CD_USERDATA;
529 sc.cmd[10] = 0; /* sub channel */
531 sc.databuf = (caddr_t)p_block;
532 sc.datalen = VCD_SECTOR_SIZE * i_nb;
533 sc.senselen = sizeof( sc.sense );
534 sc.flags = SCCMD_READ;
537 i_ret = ioctl( p_vcddev->i_device_handle, SCIOCCOMMAND, &sc );
540 msg_Err( p_this, "SCIOCCOMMAND failed" );
541 if( i_type == VCD_TYPE ) free( p_block );
544 if( sc.retsts || sc.error )
546 msg_Err( p_this, "SCSI command failed: status %d error %d",
547 sc.retsts, sc.error );
548 if( i_type == VCD_TYPE ) free( p_block );
552 #elif defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H )
553 int i_size = VCD_SECTOR_SIZE;
555 if( ioctl( p_vcddev->i_device_handle, CDRIOCSETBLOCKSIZE, &i_size )
558 msg_Err( p_this, "Could not set block size" );
559 if( i_type == VCD_TYPE ) free( p_block );
563 if( lseek( p_vcddev->i_device_handle,
564 i_sector * VCD_SECTOR_SIZE, SEEK_SET ) == -1 )
566 msg_Err( p_this, "Could not lseek to sector %d", i_sector );
567 if( i_type == VCD_TYPE ) free( p_block );
571 if( read( p_vcddev->i_device_handle,
572 p_block, VCD_SECTOR_SIZE * i_nb ) == -1 )
574 msg_Err( p_this, "Could not read sector %d", i_sector );
575 if( i_type == VCD_TYPE ) free( p_block );
580 for( i = 0; i < i_nb; i++ )
582 int i_dummy = i_sector + i + 2 * CD_FRAMES;
584 #define p_msf ((struct cdrom_msf0 *)(p_block + i * VCD_SECTOR_SIZE))
585 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
586 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
587 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
590 if( ioctl( p_vcddev->i_device_handle, CDROMREADRAW,
591 p_block + i * VCD_SECTOR_SIZE ) == -1 )
593 msg_Err( p_this, "could not read block %i from disc",
598 if( i_type == VCD_TYPE ) free( p_block );
607 /* For VCDs, we don't want to keep the header and footer of the
609 if( i_type == VCD_TYPE )
611 for( i = 0; i < i_nb; i++ )
613 memcpy( p_buffer + i * VCD_DATA_SIZE,
614 p_block + i * VCD_SECTOR_SIZE + VCD_DATA_START,
623 /****************************************************************************
625 ****************************************************************************/
627 /****************************************************************************
628 * OpenVCDImage: try to open a vcd image from a .cue file
629 ****************************************************************************/
630 static int OpenVCDImage( vlc_object_t * p_this, const char *psz_dev,
635 char *psz_vcdfile = NULL;
636 char *psz_cuefile = NULL;
637 FILE *cuefile = NULL;
638 int *p_sectors = NULL;
640 bool b_found = false;
642 /* Check if we are dealing with a .cue file */
643 p_pos = strrchr( psz_dev, '.' );
644 if( p_pos && !strcmp( p_pos, ".cue" ) )
646 /* psz_dev must be the cue file. Let's assume there's a .bin
647 * file with the same filename */
648 psz_vcdfile = malloc( p_pos - psz_dev + 5 /* ".bin" */ );
649 strncpy( psz_vcdfile, psz_dev, p_pos - psz_dev );
650 strcpy( psz_vcdfile + (p_pos - psz_dev), ".bin");
651 psz_cuefile = strdup( psz_dev );
655 /* psz_dev must be the actual vcd file. Let's assume there's a .cue
656 * file with the same filename */
659 psz_cuefile = malloc( p_pos - psz_dev + 5 /* ".cue" */ );
660 strncpy( psz_cuefile, psz_dev, p_pos - psz_dev );
661 strcpy( psz_cuefile + (p_pos - psz_dev), ".cue");
665 if( asprintf( &psz_cuefile, "%s.cue", psz_dev ) == -1 )
668 /* If we need to look up the .cue file, then we don't have to look for the vcd */
669 psz_vcdfile = strdup( psz_dev );
672 /* Open the cue file and try to parse it */
673 msg_Dbg( p_this,"trying .cue file: %s", psz_cuefile );
674 cuefile = utf8_fopen( psz_cuefile, "rt" );
675 if( cuefile == NULL )
677 msg_Dbg( p_this, "could not find .cue file" );
681 msg_Dbg( p_this,"guessing vcd image file: %s", psz_vcdfile );
682 p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
683 O_RDONLY | O_NONBLOCK | O_BINARY );
685 while( fgets( line, 1024, cuefile ) && !b_found )
687 /* We have a cue file, but no valid vcd file yet */
690 int i_temp = sscanf( line, "FILE \"%1023[^\"]\" %15s", filename, type );
695 msg_Dbg( p_this, "the cue file says the data file is %s", type );
696 if( strcasecmp( type, "BINARY" ) )
697 goto error; /* Error if not binary, otherwise treat as case 1 */
699 if( p_vcddev->i_vcdimage_handle == -1 )
701 msg_Dbg( p_this, "we could not find the data file, but we found a new path" );
703 if( *filename != '/' && ((p_pos = strrchr( psz_cuefile, '/' ))
704 || (p_pos = strrchr( psz_cuefile, '\\' ) )) )
706 psz_vcdfile = malloc( strlen(filename) +
707 (p_pos - psz_cuefile + 1) + 1 );
708 strncpy( psz_vcdfile, psz_cuefile, (p_pos - psz_cuefile + 1) );
709 strcpy( psz_vcdfile + (p_pos - psz_cuefile + 1), filename );
710 } else psz_vcdfile = strdup( filename );
711 msg_Dbg( p_this,"using vcd image file: %s", psz_vcdfile );
712 p_vcddev->i_vcdimage_handle = utf8_open( psz_vcdfile,
713 O_RDONLY | O_NONBLOCK | O_BINARY );
721 if( p_vcddev->i_vcdimage_handle == -1)
724 /* Try to parse the i_tracks and p_sectors info so we can just forget
725 * about the cuefile */
728 while( fgets( line, 1024, cuefile ) && i_tracks < INT_MAX-1 )
730 /* look for a TRACK line */
732 if( !sscanf( line, "%9s", psz_dummy ) || strcmp(psz_dummy, "TRACK") )
735 /* look for an INDEX line */
736 while( fgets( line, 1024, cuefile ) )
738 int i_num, i_min, i_sec, i_frame;
740 if( (sscanf( line, "%*9s %2u %2u:%2u:%2u", &i_num,
741 &i_min, &i_sec, &i_frame ) != 4) || (i_num != 1) )
744 int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
748 p_sectors[i_tracks] = MSF_TO_LBA(i_min, i_sec, i_frame);
749 msg_Dbg( p_this, "vcd track %i begins at sector:%i",
750 (int)i_tracks, (int)p_sectors[i_tracks] );
756 /* fill in the last entry */
757 int *buf = realloc (p_sectors, (i_tracks + 1) * sizeof (*buf));
761 p_sectors[i_tracks] = lseek(p_vcddev->i_vcdimage_handle, 0, SEEK_END)
763 msg_Dbg( p_this, "vcd track %i, begins at sector:%i",
764 (int)i_tracks, (int)p_sectors[i_tracks] );
765 p_vcddev->i_tracks = ++i_tracks;
766 p_vcddev->p_sectors = p_sectors;
770 if( cuefile ) fclose( cuefile );
778 /****************************************************************************
779 * CloseVCDImage: closes a vcd image opened by OpenVCDImage
780 ****************************************************************************/
781 static void CloseVCDImage( vlc_object_t * p_this, vcddev_t *p_vcddev )
783 VLC_UNUSED( p_this );
784 if( p_vcddev->i_vcdimage_handle != -1 )
785 close( p_vcddev->i_vcdimage_handle );
789 free( p_vcddev->p_sectors );
792 #if defined( __APPLE__ )
793 /****************************************************************************
794 * darwin_getTOC: get the TOC
795 ****************************************************************************/
796 static CDTOC *darwin_getTOC( vlc_object_t * p_this, const vcddev_t *p_vcddev )
802 io_iterator_t iterator;
803 io_registry_entry_t service;
804 CFMutableDictionaryRef properties;
807 /* get the device name */
808 if( ( psz_devname = strrchr( p_vcddev->psz_dev, '/') ) != NULL )
811 psz_devname = p_vcddev->psz_dev;
813 /* unraw the device name */
814 if( *psz_devname == 'r' )
817 /* get port for IOKit communication */
818 if( ( ret = IOMasterPort( MACH_PORT_NULL, &port ) ) != KERN_SUCCESS )
820 msg_Err( p_this, "IOMasterPort: 0x%08x", ret );
824 /* get service iterator for the device */
825 if( ( ret = IOServiceGetMatchingServices(
826 port, IOBSDNameMatching( port, 0, psz_devname ),
827 &iterator ) ) != KERN_SUCCESS )
829 msg_Err( p_this, "IOServiceGetMatchingServices: 0x%08x", ret );
834 service = IOIteratorNext( iterator );
835 IOObjectRelease( iterator );
837 /* search for kIOCDMediaClass */
838 while( service && !IOObjectConformsTo( service, kIOCDMediaClass ) )
840 if( ( ret = IORegistryEntryGetParentIterator( service,
841 kIOServicePlane, &iterator ) ) != KERN_SUCCESS )
843 msg_Err( p_this, "IORegistryEntryGetParentIterator: 0x%08x", ret );
844 IOObjectRelease( service );
848 IOObjectRelease( service );
849 service = IOIteratorNext( iterator );
850 IOObjectRelease( iterator );
853 if( service == NULL )
855 msg_Err( p_this, "search for kIOCDMediaClass came up empty" );
859 /* create a CF dictionary containing the TOC */
860 if( ( ret = IORegistryEntryCreateCFProperties( service, &properties,
861 kCFAllocatorDefault, kNilOptions ) ) != KERN_SUCCESS )
863 msg_Err( p_this, "IORegistryEntryCreateCFProperties: 0x%08x", ret );
864 IOObjectRelease( service );
868 /* get the TOC from the dictionary */
869 if( ( data = (CFDataRef) CFDictionaryGetValue( properties,
870 CFSTR(kIOCDMediaTOCKey) ) ) != NULL )
875 buf_len = CFDataGetLength( data ) + 1;
876 range = CFRangeMake( 0, buf_len );
878 if( ( pTOC = malloc( buf_len ) ) != NULL )
880 CFDataGetBytes( data, range, (u_char *)pTOC );
885 msg_Err( p_this, "CFDictionaryGetValue failed" );
888 CFRelease( properties );
889 IOObjectRelease( service );
894 /****************************************************************************
895 * darwin_getNumberOfTracks: get number of tracks in TOC
896 ****************************************************************************/
897 static int darwin_getNumberOfTracks( CDTOC *pTOC, int i_descriptors )
901 CDTOCDescriptor *pTrackDescriptors = NULL;
903 pTrackDescriptors = (CDTOCDescriptor *)pTOC->descriptors;
905 for( i = i_descriptors; i > 0; i-- )
907 track = pTrackDescriptors[i].point;
909 if( track > CD_MAX_TRACK_NO || track < CD_MIN_TRACK_NO )
917 #endif /* __APPLE__ */
920 /*****************************************************************************
921 * win32_vcd_open: open vcd drive
922 *****************************************************************************
923 * Use IOCTLs on WinNT/2K/XP.
924 *****************************************************************************/
925 static int win32_vcd_open( vlc_object_t * p_this, const char *psz_dev,
928 /* Initializations */
929 p_vcddev->h_device_handle = NULL;
931 char psz_win32_drive[7];
933 msg_Dbg( p_this, "using winNT/2K/XP ioctl layer" );
935 sprintf( psz_win32_drive, "\\\\.\\%c:", psz_dev[0] );
937 p_vcddev->h_device_handle = CreateFile( psz_win32_drive, GENERIC_READ,
938 FILE_SHARE_READ | FILE_SHARE_WRITE,
940 FILE_FLAG_NO_BUFFERING |
941 FILE_FLAG_RANDOM_ACCESS, NULL );
942 return (p_vcddev->h_device_handle == NULL) ? -1 : 0;
948 static void astrcat( char **ppsz_dst, char *psz_src )
950 char *psz_old = *ppsz_dst;
954 *ppsz_dst = strdup( psz_src );
958 if( asprintf( ppsz_dst, "%s%s", psz_old, psz_src ) < 0 )
966 static int CdTextParse( vlc_meta_t ***ppp_tracks, int *pi_tracks,
967 const uint8_t *p_buffer, int i_buffer )
969 char *pppsz_info[128][0x10];
970 int i_track_last = -1;
974 memset( pppsz_info, 0, sizeof(pppsz_info) );
976 for( int i = 0; i < (i_buffer-4)/18; i++ )
978 const uint8_t *p_block = &p_buffer[4 + 18*i];
981 const int i_pack_type = p_block[0];
982 if( i_pack_type < 0x80 || i_pack_type > 0x8f )
985 const int i_track_number = (p_block[1] >> 0)&0x7f;
986 const int i_extension_flag = ( p_block[1] >> 7)& 0x01;
987 if( i_extension_flag )
990 //const int i_sequence_number = p_block[2];
991 //const int i_charater_position = (p_block[3] >> 0) &0x0f;
992 //const int i_block_number = (p_block[3] >> 4) &0x07;
993 /* TODO unicode support
995 //const int i_unicode = ( p_block[3] >> 7)&0x01;
996 //const int i_crc = (p_block[4+12] << 8) | (p_block[4+13] << 0);
999 memcpy( psz_text, &p_block[4], 12 );
1000 psz_text[12] = '\0';
1003 int i_track = i_track_number;
1004 char *psz_track = &psz_text[0];
1005 while( i_track <= 127 && psz_track < &psz_text[12] )
1007 //fprintf( stderr, "t=%d psz_track=%p end=%p", i_track, psz_track, &psz_text[12] );
1010 astrcat( &pppsz_info[i_track][i_pack_type-0x80], psz_track );
1011 i_track_last = __MAX( i_track_last, i_track );
1015 psz_track += 1 + strlen(psz_track);
1019 if( i_track_last < 0 )
1022 vlc_meta_t **pp_tracks = calloc( i_track_last+1, sizeof(*pp_tracks) );
1026 for( int j = 0; j < 0x10; j++ )
1028 for( int i = 0; i <= i_track_last; i++ )
1031 if( pppsz_info[i][j] )
1032 EnsureUTF8( pppsz_info[i][j] );
1035 const char *psz_default = pppsz_info[0][j];
1036 const char *psz_value = pppsz_info[i][j];
1038 if( !psz_value && !psz_default )
1040 vlc_meta_t *p_track = pp_tracks[i];
1043 p_track = pp_tracks[i] = vlc_meta_New();
1049 case 0x00: /* Album/Title */
1052 vlc_meta_SetAlbum( p_track, psz_value );
1057 vlc_meta_SetTitle( p_track, psz_value );
1059 vlc_meta_SetAlbum( p_track, psz_default );
1062 case 0x01: /* Performer */
1063 vlc_meta_SetArtist( p_track,
1064 psz_value ? psz_value : psz_default );
1066 case 0x05: /* Messages */
1067 vlc_meta_SetDescription( p_track,
1068 psz_value ? psz_value : psz_default );
1070 case 0x07: /* Genre */
1071 vlc_meta_SetGenre( p_track,
1072 psz_value ? psz_value : psz_default );
1074 /* FIXME unsupported:
1084 for( int j = 0; j < 0x10; j++ )
1085 for( int i = 0; i <= i_track_last; i++ )
1086 free( pppsz_info[i][j] );
1088 *ppp_tracks = pp_tracks;
1089 *pi_tracks = i_track_last+1;
1090 return pp_tracks ? 0 : -1;
1093 #if defined( __APPLE__ ) || \
1094 defined( HAVE_IOC_TOC_HEADER_IN_SYS_CDIO_H ) || \
1095 defined( HAVE_SCSIREQ_IN_SYS_SCSIIO_H )
1096 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1097 uint8_t **pp_buffer, int *pi_buffer )
1099 VLC_UNUSED( p_object );
1102 #elif defined( WIN32 )
1103 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1104 uint8_t **pp_buffer, int *pi_buffer )
1106 VLC_UNUSED( p_object );
1108 CDROM_READ_TOC_EX TOCEx;
1109 memset(&TOCEx, 0, sizeof(TOCEx));
1110 TOCEx.Format = CDROM_READ_TOC_EX_FORMAT_CDTEXT;
1112 const int i_header_size = __MAX( 4, MINIMUM_CDROM_READ_TOC_EX_SIZE );
1113 uint8_t header[i_header_size];
1115 if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1116 &TOCEx, sizeof(TOCEx), header, i_header_size, &i_read, 0 ) )
1119 const int i_text = 2 + (header[0] << 8) + header[1];
1123 /* Read complete CD-TEXT */
1124 uint8_t *p_text = calloc( 1, i_text );
1126 return VLC_EGENERIC;
1128 if( !DeviceIoControl( p_vcddev->h_device_handle, IOCTL_CDROM_READ_TOC_EX,
1129 &TOCEx, sizeof(TOCEx), p_text, i_text, &i_read, 0 ) )
1132 return VLC_EGENERIC;
1136 *pp_buffer = p_text;
1137 *pi_buffer = i_text;
1141 static int CdTextRead( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1142 uint8_t **pp_buffer, int *pi_buffer )
1144 VLC_UNUSED( p_object );
1146 if( p_vcddev->i_device_handle == -1 )
1149 struct cdrom_generic_command gc;
1152 /* Read CD-TEXT size */
1153 memset( header, 0, sizeof(header) );
1154 memset( &gc, 0, sizeof(gc) );
1155 gc.cmd[0] = 0x43; /* Read TOC */
1156 gc.cmd[1] = 0x02; /* MSF */
1157 gc.cmd[2] = 5; /* CD-Text */
1158 gc.cmd[7] = ( sizeof(header) >> 8 ) & 0xff;
1159 gc.cmd[8] = ( sizeof(header) >> 0 ) & 0xff;
1161 gc.buflen = sizeof(header);
1163 gc.data_direction = CGC_DATA_READ;
1166 if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1167 return VLC_EGENERIC;
1169 /* If the size is less than 4 it is an error, if it 4 then
1170 * it means no text data */
1171 const int i_text = 2 + (header[0] << 8) + header[1];
1173 return VLC_EGENERIC;
1175 /* Read complete CD-TEXT */
1176 uint8_t *p_text = calloc( 1, i_text );
1178 return VLC_EGENERIC;
1180 memset( &gc, 0, sizeof(gc) );
1181 gc.cmd[0] = 0x43; /* Read TOC */
1182 gc.cmd[1] = 0x02; /* MSF */
1183 gc.cmd[2] = 5; /* CD-Text */
1184 gc.cmd[7] = ( i_text >> 8 ) & 0xff;
1185 gc.cmd[8] = ( i_text >> 0 ) & 0xff;
1189 gc.data_direction = CGC_DATA_READ;
1192 if( ioctl( p_vcddev->i_device_handle, CDROM_SEND_PACKET, &gc ) == -1 )
1195 return VLC_EGENERIC;
1199 *pp_buffer = p_text;
1200 *pi_buffer = i_text;
1205 int ioctl_GetCdText( vlc_object_t *p_object, const vcddev_t *p_vcddev,
1206 vlc_meta_t ***ppp_tracks, int *pi_tracks )
1211 if( p_vcddev->i_vcdimage_handle != -1 )
1214 if( CdTextRead( p_object, p_vcddev, &p_text, &i_text ) )
1217 CdTextParse( ppp_tracks, pi_tracks, p_text, i_text );