gl_common: minor cleanup/refactor
[mplayer.git] / stream / vcd_read_fbsd.h
blob406cb902da7d492a6e4cda6944f15e86e1df40ce
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 <sys/cdio.h>
28 #include <sys/ioctl.h>
30 #include <libavutil/intreadwrite.h>
32 #if defined(__NetBSD__) || defined(__OpenBSD__)
33 #define VCD_NETBSD 1
34 #endif
35 #ifdef VCD_NETBSD
36 #include <sys/scsiio.h>
37 #define TOCADDR(te) ((te).data->addr)
38 #define READ_TOC CDIOREADTOCENTRYS
39 #else
40 #include <sys/cdrio.h>
41 #define TOCADDR(te) ((te).entry.addr)
42 #define READ_TOC CDIOREADTOCENTRY
43 #endif
44 #include "mp_msg.h"
46 //=================== VideoCD ==========================
47 #define CDROM_LEADOUT 0xAA
49 typedef struct {
50 uint8_t sync [12];
51 uint8_t header [4];
52 uint8_t subheader [8];
53 uint8_t data [2324];
54 uint8_t spare [4];
55 } cdsector_t;
57 #ifdef VCD_NETBSD
58 typedef struct ioc_read_toc_entry vcd_tocentry;
59 #else
60 typedef struct ioc_read_toc_single_entry vcd_tocentry;
61 #endif
63 typedef struct mp_vcd_priv_st {
64 int fd;
65 vcd_tocentry entry;
66 #ifdef VCD_NETBSD
67 struct cd_toc_entry entry_data;
68 #else
69 cdsector_t buf;
70 #endif
71 struct ioc_toc_header tochdr;
72 } mp_vcd_priv_t;
74 static inline void
75 vcd_set_msf(mp_vcd_priv_t* vcd, unsigned int sect)
77 #ifdef VCD_NETBSD
78 vcd->entry.data = &vcd->entry_data;
79 #endif
80 sect += 150;
81 TOCADDR(vcd->entry).msf.frame = sect % 75;
82 sect = sect / 75;
83 TOCADDR(vcd->entry).msf.second = sect % 60;
84 sect = sect / 60;
85 TOCADDR(vcd->entry).msf.minute = sect;
88 static inline void
89 vcd_inc_msf(mp_vcd_priv_t* vcd)
91 #ifdef VCD_NETBSD
92 vcd->entry.data = &vcd->entry_data;
93 #endif
94 TOCADDR(vcd->entry).msf.frame++;
95 if (TOCADDR(vcd->entry).msf.frame==75){
96 TOCADDR(vcd->entry).msf.frame=0;
97 TOCADDR(vcd->entry).msf.second++;
98 if (TOCADDR(vcd->entry).msf.second==60){
99 TOCADDR(vcd->entry).msf.second=0;
100 TOCADDR(vcd->entry).msf.minute++;
105 static inline unsigned int
106 vcd_get_msf(mp_vcd_priv_t* vcd)
108 #ifdef VCD_NETBSD
109 vcd->entry.data = &vcd->entry_data;
110 #endif
111 return TOCADDR(vcd->entry).msf.frame +
112 (TOCADDR(vcd->entry).msf.second +
113 TOCADDR(vcd->entry).msf.minute * 60) * 75 - 150;
117 * \brief read a TOC entry
118 * \param fd device to read from
119 * \param dst buffer to read data into
120 * \param nr track number to read info for
121 * \return 1 on success, 0 on failure
123 static int
124 read_toc_entry(mp_vcd_priv_t *vcd, int nr)
126 vcd->entry.address_format = CD_MSF_FORMAT;
127 #ifdef VCD_NETBSD
128 vcd->entry.data_len = sizeof(struct cd_toc_entry);
129 vcd->entry.data = &vcd->entry_data;
130 vcd->entry.starting_track = nr;
131 #else
132 vcd->entry.track = nr;
133 #endif
134 if (ioctl(vcd->fd, READ_TOC, &vcd->entry) == -1) {
135 mp_msg(MSGT_OPEN,MSGL_ERR,"read CDROM toc entry: %s\n",strerror(errno));
136 return 0;
138 return 1;
141 static int
142 vcd_seek_to_track(mp_vcd_priv_t* vcd, int track)
144 if (!read_toc_entry(vcd, track))
145 return -1;
146 return VCD_SECTOR_DATA * vcd_get_msf(vcd);
149 static int
150 vcd_get_track_end(mp_vcd_priv_t* vcd, int track)
152 if (!read_toc_entry(vcd,
153 track < vcd->tochdr.ending_track ? track + 1 : CDROM_LEADOUT))
154 return -1;
155 return VCD_SECTOR_DATA * vcd_get_msf(vcd);
158 static mp_vcd_priv_t*
159 vcd_read_toc(int fd)
161 struct ioc_toc_header tochdr;
162 mp_vcd_priv_t* vcd;
163 int i, last_startsect;
164 if (ioctl(fd, CDIOREADTOCHEADER, &tochdr) == -1) {
165 mp_msg(MSGT_OPEN,MSGL_ERR,"read CDROM toc header: %s\n",strerror(errno));
166 return NULL;
168 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_START_TRACK=%d\n", tochdr.starting_track);
169 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_END_TRACK=%d\n", tochdr.ending_track);
170 vcd = malloc(sizeof(mp_vcd_priv_t));
171 vcd->fd = fd;
172 vcd->tochdr = tochdr;
173 for (i = tochdr.starting_track; i <= tochdr.ending_track + 1; i++) {
174 if (!read_toc_entry(vcd,
175 i <= tochdr.ending_track ? i : CDROM_LEADOUT)) {
176 free(vcd);
177 return NULL;
180 if (i <= tochdr.ending_track)
181 mp_msg(MSGT_OPEN,MSGL_INFO,"track %02d: adr=%d ctrl=%d format=%d %02d:%02d:%02d\n",
182 #ifdef VCD_NETBSD
183 (int)vcd->entry.starting_track,
184 (int)vcd->entry.data->addr_type,
185 (int)vcd->entry.data->control,
186 #else
187 (int)vcd->entry.track,
188 (int)vcd->entry.entry.addr_type,
189 (int)vcd->entry.entry.control,
190 #endif
191 (int)vcd->entry.address_format,
192 (int)TOCADDR(vcd->entry).msf.minute,
193 (int)TOCADDR(vcd->entry).msf.second,
194 (int)TOCADDR(vcd->entry).msf.frame
197 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
199 int startsect = vcd_get_msf(vcd);
200 if (i > tochdr.starting_track)
202 // convert duraion to MSF
203 vcd_set_msf(vcd, startsect - last_startsect);
204 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
205 "ID_VCD_TRACK_%d_MSF=%02d:%02d:%02d\n",
206 i - 1,
207 TOCADDR(vcd->entry).msf.minute,
208 TOCADDR(vcd->entry).msf.second,
209 TOCADDR(vcd->entry).msf.frame);
211 last_startsect = startsect;
214 return vcd;
217 static int vcd_end_track(mp_vcd_priv_t* vcd)
219 return vcd->tochdr.ending_track;
222 static int
223 vcd_read(mp_vcd_priv_t* vcd, char *mem)
225 #ifdef VCD_NETBSD
226 struct scsireq sc;
227 int lba = vcd_get_msf(vcd);
228 int blocks;
229 int rc;
231 blocks = 1;
233 memset(&sc, 0, sizeof(sc));
234 sc.cmd[0] = 0xBE;
235 sc.cmd[1] = 5 << 2; // mode2/form2
236 AV_WB32(&sc.cmd[2], lba);
237 AV_WB24(&sc.cmd[6], blocks);
238 sc.cmd[9] = 1 << 4; // user data only
239 sc.cmd[10] = 0; // no subchannel
240 sc.cmdlen = 12;
241 sc.databuf = (caddr_t) mem;
242 sc.datalen = VCD_SECTOR_DATA;
243 sc.senselen = sizeof(sc.sense);
244 sc.flags = SCCMD_READ;
245 sc.timeout = 10000;
246 rc = ioctl(vcd->fd, SCIOCCOMMAND, &sc);
247 if (rc == -1) {
248 mp_msg(MSGT_STREAM,MSGL_ERR,"SCIOCCOMMAND: %s\n",strerror(errno));
249 return -1;
251 if (sc.retsts || sc.error) {
252 mp_msg(MSGT_STREAM,MSGL_ERR,"scsi command failed: status %d error %d\n",
253 sc.retsts,sc.error);
254 return -1;
256 #else
257 if (pread(vcd->fd,&vcd->buf,VCD_SECTOR_SIZE,vcd_get_msf(vcd)*VCD_SECTOR_SIZE)
258 != VCD_SECTOR_SIZE) return 0; // EOF?
260 memcpy(mem,vcd->buf.data,VCD_SECTOR_DATA);
261 #endif
262 vcd_inc_msf(vcd);
263 return VCD_SECTOR_DATA;
266 #endif /* MPLAYER_VCD_READ_FBSD_H */