1 /****************************************************************************
2 * linux_cdrom_tools.c: linux cdrom tools
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
6 * Author: Johan Bilien <jobi@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
29 #include <videolan/vlc.h>
36 #include <sys/types.h>
40 #include <sys/ioctl.h>
42 #include "linux_cdrom_tools.h"
44 /*****************************************************************************
45 * ioctl_ReadTocHeader: Read the TOC header and return the track number.
46 *****************************************************************************/
47 int ioctl_GetTrackCount( int i_fd )
49 struct cdrom_tochdr tochdr;
51 /* First we read the TOC header */
52 if( ioctl( i_fd, CDROMREADTOCHDR, &tochdr ) == -1 )
54 intf_ErrMsg( "vcd error: could not read TOCHDR" );
58 return tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
61 /*****************************************************************************
62 * ioctl_GetSectors: Read the Table of Contents and fill p_vcd.
63 *****************************************************************************/
64 int * ioctl_GetSectors( int i_fd )
68 struct cdrom_tochdr tochdr;
69 struct cdrom_tocentry tocent;
71 /* First we read the TOC header */
72 if( ioctl( i_fd, CDROMREADTOCHDR, &tochdr ) == -1 )
74 intf_ErrMsg( "vcd error: could not read TOCHDR" );
78 i_tracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
80 p_sectors = malloc( (i_tracks + 1) * sizeof(int) );
81 if( p_sectors == NULL )
83 intf_ErrMsg( "vcd error: could not allocate p_sectors" );
87 /* Fill the p_sectors structure with the track/sector matches */
88 for( i = 0 ; i <= i_tracks ; i++ )
90 tocent.cdte_format = CDROM_LBA;
92 ( i == i_tracks ) ? CDROM_LEADOUT : tochdr.cdth_trk0 + i;
94 if( ioctl( i_fd, CDROMREADTOCENTRY, &tocent ) == -1 )
96 intf_ErrMsg( "vcd error: could not read TOCENTRY" );
101 p_sectors[ i ] = tocent.cdte_addr.lba;
107 /****************************************************************************
108 * ioctl_ReadSector: Read a sector (2324 bytes)
109 ****************************************************************************/
110 int ioctl_ReadSector( int i_fd, int i_sector, byte_t * p_buffer )
112 byte_t p_block[ VCD_SECTOR_SIZE ];
113 int i_dummy = i_sector + 2 * CD_FRAMES;
115 #define p_msf ((struct cdrom_msf0 *)p_block)
116 p_msf->minute = i_dummy / (CD_FRAMES * CD_SECS);
117 p_msf->second = ( i_dummy % (CD_FRAMES * CD_SECS) ) / CD_FRAMES;
118 p_msf->frame = ( i_dummy % (CD_FRAMES * CD_SECS) ) % CD_FRAMES;
120 intf_DbgMsg( "vcd debug: playing frame %d:%d-%d",
121 p_msf->minute, p_msf->second, p_msf->frame);
124 if( ioctl(i_fd, CDROMREADRAW, p_block) == -1 )
126 intf_ErrMsg( "vcd error: could not read block %i from disc",
131 /* We don't want to keep the header of the read sector */
132 FAST_MEMCPY( p_buffer, p_block + VCD_DATA_START, VCD_DATA_SIZE );