ad_ffmpeg: return failure from init() if initial decode fails
[mplayer/greg.git] / stream / vcd_read_win32.h
blobf4ac384d1dd502202dda8747db8ee4ff1084f068
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_WIN32_H
20 #define MPLAYER_VCD_READ_WIN32_H
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ddk/ntddcdrm.h>
26 #include "mp_msg.h"
28 typedef struct mp_vcd_priv_st mp_vcd_priv_t;
31 Unlike Unices, upon reading TOC, Windows will retrieve information for
32 all tracks, so we don't need to call DeviceIoControl() in functions
33 like vcd_seek_to_track() and vcd_get_track_end() for each track. Instead
34 we cache the information in mp_vcd_priv_st.
36 struct mp_vcd_priv_st {
37 HANDLE hd;
38 CDROM_TOC toc;
39 unsigned sect;
40 char buf[VCD_SECTOR_SIZE];
43 static inline void vcd_set_msf(mp_vcd_priv_t* vcd, unsigned sect)
45 vcd->sect = sect;
48 static inline unsigned vcd_get_msf(mp_vcd_priv_t* vcd, int track){
49 int index = track - vcd->toc.FirstTrack;
50 /* -150 to compensate the 2-second pregap */
51 return vcd->toc.TrackData[index].Address[3] +
52 (vcd->toc.TrackData[index].Address[2] +
53 vcd->toc.TrackData[index].Address[1] * 60) * 75 - 150;
56 int vcd_seek_to_track(mp_vcd_priv_t* vcd, int track)
58 unsigned sect;
59 if (track < vcd->toc.FirstTrack || track > vcd->toc.LastTrack)
60 return -1;
61 sect = vcd_get_msf(vcd, track);
62 vcd_set_msf(vcd, sect);
63 return VCD_SECTOR_DATA * (sect + 2);
66 static int vcd_get_track_end(mp_vcd_priv_t* vcd, int track)
68 if (track < vcd->toc.FirstTrack || track > vcd->toc.LastTrack)
69 return -1;
70 return VCD_SECTOR_DATA * (vcd_get_msf(vcd, track + 1));
73 static mp_vcd_priv_t* vcd_read_toc(int fd)
75 DWORD dwBytesReturned;
76 HANDLE hd;
77 int i, min = 0, sec = 0, frame = 0;
78 mp_vcd_priv_t* vcd = malloc(sizeof(mp_vcd_priv_t));
79 if (!vcd)
80 return NULL;
82 hd = (HANDLE)_get_osfhandle(fd);
83 if (!DeviceIoControl(hd, IOCTL_CDROM_READ_TOC, NULL, 0, &vcd->toc,
84 sizeof(CDROM_TOC), &dwBytesReturned, NULL)) {
85 mp_msg(MSGT_OPEN, MSGL_ERR, "read CDROM toc header: %lu\n",
86 GetLastError());
87 free(vcd);
88 return NULL;
91 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_START_TRACK=%d\n",
92 vcd->toc.FirstTrack);
93 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_END_TRACK=%d\n",
94 vcd->toc.LastTrack);
96 for (i = vcd->toc.FirstTrack; i <= vcd->toc.LastTrack + 1; i++) {
97 int index = i - vcd->toc.FirstTrack;
98 if (i <= vcd->toc.LastTrack) {
99 mp_msg(MSGT_OPEN, MSGL_INFO, "track %02d: adr=%d ctrl=%d"
100 " %02d:%02d:%02d\n",
101 vcd->toc.TrackData[index].TrackNumber,
102 vcd->toc.TrackData[index].Adr,
103 vcd->toc.TrackData[index].Control,
104 vcd->toc.TrackData[index].Address[1],
105 vcd->toc.TrackData[index].Address[2],
106 vcd->toc.TrackData[index].Address[3]);
109 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
110 if (i > vcd->toc.FirstTrack) {
111 min = vcd->toc.TrackData[index].Address[1] - min;
112 sec = vcd->toc.TrackData[index].Address[2] - sec;
113 frame = vcd->toc.TrackData[index].Address[3] - frame;
114 if (frame < 0) {
115 frame += 75;
116 sec--;
118 if (sec < 0) {
119 sec += 60;
120 min--;
122 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_TRACK_%d_MSF="
123 "%02d:%02d:%02d\n", i - 1, min, sec, frame);
124 min = vcd->toc.TrackData[index].Address[1];
125 sec = vcd->toc.TrackData[index].Address[2];
126 frame = vcd->toc.TrackData[index].Address[3];
131 vcd->hd = hd;
132 return vcd;
135 static int vcd_end_track(mp_vcd_priv_t* vcd)
137 return vcd->toc.LastTrack;
140 static int vcd_read(mp_vcd_priv_t* vcd, char *mem)
142 DWORD dwBytesReturned;
143 RAW_READ_INFO cdrom_raw;
145 /* 2048 isn't a typo: it's the Windows way. */
146 cdrom_raw.DiskOffset.QuadPart = (long long)(2048 * vcd->sect);
147 cdrom_raw.SectorCount = 1;
148 cdrom_raw.TrackMode = XAForm2;
150 if (!DeviceIoControl(vcd->hd, IOCTL_CDROM_RAW_READ, &cdrom_raw,
151 sizeof(RAW_READ_INFO), vcd->buf, sizeof(vcd->buf),
152 &dwBytesReturned, NULL))
153 return 0;
155 vcd->sect++;
156 memcpy(mem, &vcd->buf[VCD_SECTOR_OFFS], VCD_SECTOR_DATA);
157 return VCD_SECTOR_DATA;
160 #endif /* MPLAYER_VCD_READ_WIN32_H */