sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / stream / vcd_read_fbsd.h
bloba9555f5b9e2059f3dad1abfe25f5b9dad3af5e5e
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef MPLAYER_VCD_READ_FBSD_H
20 #define MPLAYER_VCD_READ_FBSD_H
22 #define _XOPEN_SOURCE 500
24 #include <sys/types.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27 #include "libavutil/intreadwrite.h"
28 #include <sys/cdio.h>
29 #include <sys/ioctl.h>
30 #if defined(__NetBSD__) || defined(__OpenBSD__)
31 #define VCD_NETBSD 1
32 #endif
33 #ifdef VCD_NETBSD
34 #include <sys/scsiio.h>
35 #define TOCADDR(te) ((te).data->addr)
36 #define READ_TOC CDIOREADTOCENTRYS
37 #else
38 #include <sys/cdrio.h>
39 #define TOCADDR(te) ((te).entry.addr)
40 #define READ_TOC CDIOREADTOCENTRY
41 #endif
42 #include "mp_msg.h"
44 //=================== VideoCD ==========================
45 #define CDROM_LEADOUT 0xAA
47 typedef struct {
48 uint8_t sync [12];
49 uint8_t header [4];
50 uint8_t subheader [8];
51 uint8_t data [2324];
52 uint8_t spare [4];
53 } cdsector_t;
55 #ifdef VCD_NETBSD
56 typedef struct ioc_read_toc_entry vcd_tocentry;
57 #else
58 typedef struct ioc_read_toc_single_entry vcd_tocentry;
59 #endif
61 typedef struct mp_vcd_priv_st {
62 int fd;
63 vcd_tocentry entry;
64 #ifdef VCD_NETBSD
65 struct cd_toc_entry entry_data;
66 #else
67 cdsector_t buf;
68 #endif
69 struct ioc_toc_header tochdr;
70 } mp_vcd_priv_t;
72 static inline void
73 vcd_set_msf(mp_vcd_priv_t* vcd, unsigned int sect)
75 #ifdef VCD_NETBSD
76 vcd->entry.data = &vcd->entry_data;
77 #endif
78 sect += 150;
79 TOCADDR(vcd->entry).msf.frame = sect % 75;
80 sect = sect / 75;
81 TOCADDR(vcd->entry).msf.second = sect % 60;
82 sect = sect / 60;
83 TOCADDR(vcd->entry).msf.minute = sect;
86 static inline void
87 vcd_inc_msf(mp_vcd_priv_t* vcd)
89 #ifdef VCD_NETBSD
90 vcd->entry.data = &vcd->entry_data;
91 #endif
92 TOCADDR(vcd->entry).msf.frame++;
93 if (TOCADDR(vcd->entry).msf.frame==75){
94 TOCADDR(vcd->entry).msf.frame=0;
95 TOCADDR(vcd->entry).msf.second++;
96 if (TOCADDR(vcd->entry).msf.second==60){
97 TOCADDR(vcd->entry).msf.second=0;
98 TOCADDR(vcd->entry).msf.minute++;
103 static inline unsigned int
104 vcd_get_msf(mp_vcd_priv_t* vcd)
106 #ifdef VCD_NETBSD
107 vcd->entry.data = &vcd->entry_data;
108 #endif
109 return TOCADDR(vcd->entry).msf.frame +
110 (TOCADDR(vcd->entry).msf.second +
111 TOCADDR(vcd->entry).msf.minute * 60) * 75 - 150;
115 * \brief read a TOC entry
116 * \param fd device to read from
117 * \param dst buffer to read data into
118 * \param nr track number to read info for
119 * \return 1 on success, 0 on failure
121 static int
122 read_toc_entry(mp_vcd_priv_t *vcd, int nr)
124 vcd->entry.address_format = CD_MSF_FORMAT;
125 #ifdef VCD_NETBSD
126 vcd->entry.data_len = sizeof(struct cd_toc_entry);
127 vcd->entry.data = &vcd->entry_data;
128 vcd->entry.starting_track = nr;
129 #else
130 vcd->entry.track = nr;
131 #endif
132 if (ioctl(vcd->fd, READ_TOC, &vcd->entry) == -1) {
133 mp_msg(MSGT_OPEN,MSGL_ERR,"read CDROM toc entry: %s\n",strerror(errno));
134 return 0;
136 return 1;
140 vcd_seek_to_track(mp_vcd_priv_t* vcd, int track)
142 if (!read_toc_entry(vcd, track))
143 return -1;
144 return VCD_SECTOR_DATA * vcd_get_msf(vcd);
147 static int
148 vcd_get_track_end(mp_vcd_priv_t* vcd, int track)
150 if (!read_toc_entry(vcd,
151 track < vcd->tochdr.ending_track ? track + 1 : CDROM_LEADOUT))
152 return -1;
153 return VCD_SECTOR_DATA * vcd_get_msf(vcd);
156 static mp_vcd_priv_t*
157 vcd_read_toc(int fd)
159 struct ioc_toc_header tochdr;
160 mp_vcd_priv_t* vcd;
161 int i, last_startsect;
162 if (ioctl(fd, CDIOREADTOCHEADER, &tochdr) == -1) {
163 mp_msg(MSGT_OPEN,MSGL_ERR,"read CDROM toc header: %s\n",strerror(errno));
164 return NULL;
166 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_START_TRACK=%d\n", tochdr.starting_track);
167 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_END_TRACK=%d\n", tochdr.ending_track);
168 vcd = malloc(sizeof(mp_vcd_priv_t));
169 vcd->fd = fd;
170 vcd->tochdr = tochdr;
171 for (i = tochdr.starting_track; i <= tochdr.ending_track + 1; i++) {
172 if (!read_toc_entry(vcd,
173 i <= tochdr.ending_track ? i : CDROM_LEADOUT)) {
174 free(vcd);
175 return NULL;
178 if (i <= tochdr.ending_track)
179 mp_msg(MSGT_OPEN,MSGL_INFO,"track %02d: adr=%d ctrl=%d format=%d %02d:%02d:%02d\n",
180 #ifdef VCD_NETBSD
181 (int)vcd->entry.starting_track,
182 (int)vcd->entry.data->addr_type,
183 (int)vcd->entry.data->control,
184 #else
185 (int)vcd->entry.track,
186 (int)vcd->entry.entry.addr_type,
187 (int)vcd->entry.entry.control,
188 #endif
189 (int)vcd->entry.address_format,
190 (int)TOCADDR(vcd->entry).msf.minute,
191 (int)TOCADDR(vcd->entry).msf.second,
192 (int)TOCADDR(vcd->entry).msf.frame
195 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
197 int startsect = vcd_get_msf(vcd);
198 if (i > tochdr.starting_track)
200 // convert duraion to MSF
201 vcd_set_msf(vcd, startsect - last_startsect);
202 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
203 "ID_VCD_TRACK_%d_MSF=%02d:%02d:%02d\n",
204 i - 1,
205 TOCADDR(vcd->entry).msf.minute,
206 TOCADDR(vcd->entry).msf.second,
207 TOCADDR(vcd->entry).msf.frame);
209 last_startsect = startsect;
212 return vcd;
215 static int
216 vcd_read(mp_vcd_priv_t* vcd, char *mem)
218 #ifdef VCD_NETBSD
219 struct scsireq sc;
220 int lba = vcd_get_msf(vcd);
221 int blocks;
222 int rc;
224 blocks = 1;
226 memset(&sc, 0, sizeof(sc));
227 sc.cmd[0] = 0xBE;
228 sc.cmd[1] = 5 << 2; // mode2/form2
229 AV_WB32(&sc.cmd[2], lba);
230 AV_WB24(&sc.cmd[6], blocks);
231 sc.cmd[9] = 1 << 4; // user data only
232 sc.cmd[10] = 0; // no subchannel
233 sc.cmdlen = 12;
234 sc.databuf = (caddr_t) mem;
235 sc.datalen = VCD_SECTOR_DATA;
236 sc.senselen = sizeof(sc.sense);
237 sc.flags = SCCMD_READ;
238 sc.timeout = 10000;
239 rc = ioctl(vcd->fd, SCIOCCOMMAND, &sc);
240 if (rc == -1) {
241 mp_msg(MSGT_STREAM,MSGL_ERR,"SCIOCCOMMAND: %s\n",strerror(errno));
242 return -1;
244 if (sc.retsts || sc.error) {
245 mp_msg(MSGT_STREAM,MSGL_ERR,"scsi command failed: status %d error %d\n",
246 sc.retsts,sc.error);
247 return -1;
249 #else
250 if (pread(vcd->fd,&vcd->buf,VCD_SECTOR_SIZE,vcd_get_msf(vcd)*VCD_SECTOR_SIZE)
251 != VCD_SECTOR_SIZE) return 0; // EOF?
253 memcpy(mem,vcd->buf.data,VCD_SECTOR_DATA);
254 #endif
255 vcd_inc_msf(vcd);
256 return VCD_SECTOR_DATA;
259 #endif /* MPLAYER_VCD_READ_FBSD_H */