[MPlayer-dev-eng] [PATCH] VCD support for Windows (except Win9x)
Zuxy Meng
zuxy.meng at gmail.com
Wed Feb 28 09:10:15 CET 2007
Hi,
2007/2/27, Gianluigi Tiesi <mplayer at netfarm.it>:
> On Mon, Feb 26, 2007 at 03:22:47PM +0100, Reimar D?ffinger wrote:
> > Hello,
> > On Mon, Feb 26, 2007 at 09:49:10PM +0800, Zuxy Meng wrote:
> > > 2007/2/26, Compn <tempn at twmi.rr.com>:
> > > >On Mon, 26 Feb 2007 21:37:40 +0800,
> > > >Zuxy Meng scribed:
> > > >
> > > >> +#ifdef WIN32
> > > >> + HANDLE hd;
> > > >> + char device[] = "\\\\.\\D:";
> > > >> +#endif
> > > >
> > > >does this hardcode the device name?
> > > >D: is not the dvd/cdrom drive on all systems.
> > >
> > > D is only a stub here. The actual letter is filled by:
> > > + device[4] = p->device[0];
> >
> > With any sane combination of compiler and operating system this should
> > crash. String constants are supposed to be in readonly memory.
> >
>
> char * are r/w, const char * "should be" r/o (but most compiler still
> keep it as r/w).
> msvc6 is able to have a #define MYSTRING "test1", with the string
> reference r/w, obiviously vs2005 crashes as it should :)
>
> with char device[] you are saying to the compiler to allocate
> a char array sized as the length of the provided string + trailing \0
> it's the same of char device[6] (or whatever) and then filling the
> content.
>
> Anyway a space or a ? or a x should be more readable
I updated the patch to use a '?' instead 'D' to avoid confusion. In
addition, I removed in the new patch the code to skip trailing margin,
because I'm planning to strip leading & trailing margins in a more
general way in stream_vcd.c so that VCD will be supported even with
-demuxer lavf; I'll send that patch until this one gets applied.
--
Zuxy
Beauty is truth,
While truth is beauty.
PGP KeyID: E8555ED6
-------------- next part --------------
Index: configure
===================================================================
--- configure ???????? 22351??
+++ configure ????????????
@@ -5145,7 +5150,7 @@
echocheck "VCD support"
-if linux || bsdos || freebsd || netbsd || sunos || darwin ; then
+if linux || bsdos || freebsd || netbsd || sunos || darwin || win32; then
_inputmodules="vcd $_inputmodules"
_def_vcd='#define HAVE_VCD 1'
_vcd="yes"
Index: stream/stream_vcd.c
===================================================================
--- stream/stream_vcd.c ???????? 22373??
+++ stream/stream_vcd.c ????????????
@@ -1,6 +1,10 @@
#include "config.h"
+#ifdef WIN32
+#include <windows.h>
+#endif
+
#include "mp_msg.h"
#include "stream.h"
#include "help_mp.h"
@@ -10,7 +14,9 @@
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
+#ifndef WIN32
#include <sys/ioctl.h>
+#endif
#include <errno.h>
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
@@ -20,6 +26,8 @@
#include "vcd_read_nbsd.h"
#elif defined(SYS_DARWIN)
#include "vcd_read_darwin.h"
+#elif defined(WIN32)
+#include "vcd_read_win32.h"
#else
#include "vcd_read.h"
#endif
@@ -76,8 +84,16 @@
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
int bsize = VCD_SECTOR_SIZE;
#endif
+#ifdef WIN32
+ HANDLE hd;
+ char device[] = "\\\\.\\?:";
+#endif
- if(mode != STREAM_READ) {
+ if(mode != STREAM_READ
+#ifdef WIN32
+ || GetVersion() > 0x80000000 // Win9x
+#endif
+ ) {
m_struct_free(&stream_opts,opts);
return STREAM_UNSUPORTED;
}
@@ -89,7 +105,15 @@
p->device = strdup(DEFAULT_CDROM_DEVICE);
}
+#ifdef WIN32
+ device[4] = p->device[0];
+ hd = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
+ OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
+ /* It's safe here: when f gets close()'ed, hd will be released too. */
+ f = _open_osfhandle((long)hd, _O_RDONLY);
+#else
f=open(p->device,O_RDONLY);
+#endif
if(f<0){
mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CdDevNotfound,p->device);
m_struct_free(&stream_opts,opts);
Index: stream/vcd_read_win32.h
===================================================================
--- stream/vcd_read_win32.h ???????? 0??
+++ stream/vcd_read_win32.h ???????? 0??
@@ -0,0 +1,130 @@
+#include <ddk/ntddcdrm.h>
+
+typedef struct mp_vcd_priv_st mp_vcd_priv_t;
+
+/*
+ Unlike Unices, upon reading TOC, Windows will retrieve information for
+ all tracks, so we don't need to call DeviceIoControl() in functions
+ like vcd_seek_to_track() and vcd_get_track_end() for each track. Instead
+ we cache it in mp_vcd_priv_st.
+*/
+struct mp_vcd_priv_st {
+ HANDLE hd;
+ CDROM_TOC toc;
+ unsigned sect;
+ char buf[VCD_SECTOR_SIZE];
+};
+
+static inline void vcd_set_msf(mp_vcd_priv_t* vcd, unsigned sect)
+{
+ vcd->sect = sect;
+}
+
+static inline unsigned vcd_get_msf(mp_vcd_priv_t* vcd, int track){
+ int index = track + vcd->toc.FirstTrack - 1;
+ /* -150 to compensate the 2-second pregap */
+ return vcd->toc.TrackData[index].Address[3] +
+ (vcd->toc.TrackData[index].Address[2] +
+ vcd->toc.TrackData[index].Address[1] * 60) * 75 - 150;
+}
+
+int vcd_seek_to_track(mp_vcd_priv_t* vcd, int track)
+{
+ unsigned sect;
+ if (track < vcd->toc.FirstTrack || track > vcd->toc.LastTrack)
+ return -1;
+ sect = vcd_get_msf(vcd, track);
+ vcd_set_msf(vcd, sect);
+ return VCD_SECTOR_DATA * (sect + 2);
+}
+
+int vcd_get_track_end(mp_vcd_priv_t* vcd, int track)
+{
+ if (track < vcd->toc.FirstTrack || track > vcd->toc.LastTrack)
+ return -1;
+ return VCD_SECTOR_DATA * (vcd_get_msf(vcd, track + 1));
+}
+
+mp_vcd_priv_t* vcd_read_toc(int fd)
+{
+ DWORD dwBytesReturned;
+ HANDLE hd;
+ int i, min = 0, sec = 0, frame = 0;
+ mp_vcd_priv_t* vcd = malloc(sizeof(mp_vcd_priv_t));
+ if (!vcd)
+ return NULL;
+
+ hd = (HANDLE)_get_osfhandle(fd);
+ if (!DeviceIoControl(hd, IOCTL_CDROM_READ_TOC, NULL, 0, &vcd->toc,
+ sizeof(CDROM_TOC), &dwBytesReturned, NULL)) {
+ mp_msg(MSGT_OPEN, MSGL_ERR, "read CDROM toc header: %u\n",
+ GetLastError());
+ free(vcd);
+ return NULL;
+ }
+
+ mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_START_TRACK=%d\n",
+ vcd->toc.FirstTrack);
+ mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_END_TRACK=%d\n",
+ vcd->toc.LastTrack);
+
+ for (i = vcd->toc.FirstTrack; i <= vcd->toc.LastTrack + 1; i++) {
+ int index = i - vcd->toc.FirstTrack;
+ if (i <= vcd->toc.LastTrack) {
+ mp_msg(MSGT_OPEN, MSGL_INFO, "track %02d: adr=%d ctrl=%d"
+ " %02d:%02d:%02d\n",
+ vcd->toc.TrackData[index].TrackNumber,
+ vcd->toc.TrackData[index].Adr,
+ vcd->toc.TrackData[index].Control,
+ vcd->toc.TrackData[index].Address[1],
+ vcd->toc.TrackData[index].Address[2],
+ vcd->toc.TrackData[index].Address[3]);
+ }
+
+ if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
+ if (i > vcd->toc.FirstTrack) {
+ min = vcd->toc.TrackData[index].Address[1] - min;
+ if (frame < 0) {
+ frame += 75;
+ sec--;
+ }
+ if (sec < 0) {
+ sec += 60;
+ min--;
+ }
+ mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VCD_TRACK_%d_MSF="
+ "%02d:%02d:%02d\n", i - 1, min, sec, frame);
+ min = vcd->toc.TrackData[index].Address[1];
+ sec = vcd->toc.TrackData[index].Address[2];
+ frame = vcd->toc.TrackData[index].Address[3];
+ }
+ }
+ }
+
+ vcd->hd = hd;
+ return vcd;
+}
+
+static int vcd_read(mp_vcd_priv_t* vcd, char *mem)
+{
+ DWORD dwBytesReturned;
+ RAW_READ_INFO cdrom_raw;
+
+ /* 2048 isn't a typo: it's the Windows way */
+ cdrom_raw.DiskOffset.QuadPart = (long long)(2048 * vcd->sect);
+ cdrom_raw.SectorCount = 1;
+ cdrom_raw.TrackMode = XAForm2;
+
+ if (!DeviceIoControl(vcd->hd, IOCTL_CDROM_RAW_READ, &cdrom_raw,
+ sizeof(RAW_READ_INFO), vcd->buf, sizeof(vcd->buf),
+ &dwBytesReturned, NULL))
+ return 0;
+
+ vcd->sect++;
+ memcpy(mem, &vcd->buf[VCD_SECTOR_OFFS], VCD_SECTOR_DATA);
+ return VCD_SECTOR_DATA;
+}
+
+/*
+vim:noet:sw=4:cino=\:0,g0
+*/
More information about the MPlayer-dev-eng
mailing list