Merge remote branch 'mplayer/master'
[mplayer/glamo.git] / stream / stream_dvd_common.c
blobd5c4cc14a67f57cce0f17841bf0920d71dbfd1c2
1 #include "config.h"
2 #include <fcntl.h>
3 #include <inttypes.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <dvdread/ifo_types.h>
8 #ifdef __FreeBSD__
9 #include <sys/cdrio.h>
10 #endif
12 #ifdef __linux__
13 #include <linux/cdrom.h>
14 #include <scsi/sg.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/ioctl.h>
18 #endif
20 #include "mp_msg.h"
21 #include "help_mp.h"
22 #include "stream_dvd_common.h"
23 #include "ffmpeg_files/intreadwrite.h"
25 const char * const dvd_audio_stream_types[8] = { "ac3","unknown","mpeg1","mpeg2ext","lpcm","unknown","dts" };
26 const char * const dvd_audio_stream_channels[6] = { "mono", "stereo", "unknown", "unknown", "5.1/6.1", "5.1" };
28 int dvd_speed=0; /* 0 => don't touch speed */
30 void dvd_set_speed(char *device, unsigned speed)
32 #if defined(__linux__) && defined(SG_IO) && defined(GPCMD_SET_STREAMING)
33 int fd;
34 unsigned char buffer[28];
35 unsigned char cmd[12];
36 struct sg_io_hdr sghdr;
37 struct stat st;
39 memset(&st, 0, sizeof(st));
41 if (stat(device, &st) == -1) return;
43 if (!S_ISBLK(st.st_mode)) return; /* not a block device */
45 switch (speed) {
46 case 0: /* don't touch speed setting */
47 return;
48 case -1: /* restore default value */
49 if (dvd_speed == 0) return; /* we haven't touched the speed setting */
50 mp_tmsg(MSGT_OPEN, MSGL_INFO, "Restoring DVD speed... ");
51 break;
52 default: /* limit to <speed> KB/s */
53 // speed < 100 is multiple of DVD single speed (1350KB/s)
54 if (speed < 100)
55 speed *= 1350;
56 mp_tmsg(MSGT_OPEN, MSGL_INFO, "Limiting DVD speed to %dKB/s... ", speed);
57 break;
60 memset(&sghdr, 0, sizeof(sghdr));
61 sghdr.interface_id = 'S';
62 sghdr.timeout = 5000;
63 sghdr.dxfer_direction = SG_DXFER_TO_DEV;
64 sghdr.dxfer_len = sizeof(buffer);
65 sghdr.dxferp = buffer;
66 sghdr.cmd_len = sizeof(cmd);
67 sghdr.cmdp = cmd;
69 memset(cmd, 0, sizeof(cmd));
70 cmd[0] = GPCMD_SET_STREAMING;
71 cmd[10] = sizeof(buffer);
73 memset(buffer, 0, sizeof(buffer));
74 /* first sector 0, last sector 0xffffffff */
75 AV_WB32(buffer + 8, 0xffffffff);
76 if (speed == -1)
77 buffer[0] = 4; /* restore default */
78 else {
79 /* <speed> kilobyte */
80 AV_WB32(buffer + 12, speed);
81 AV_WB32(buffer + 20, speed);
83 /* 1 second */
84 AV_WB16(buffer + 18, 1000);
85 AV_WB16(buffer + 26, 1000);
87 fd = open(device, O_RDWR | O_NONBLOCK);
88 if (fd == -1) {
89 mp_tmsg(MSGT_OPEN, MSGL_INFO, "Couldn't open DVD device for writing, changing DVD speed needs write access.\n");
90 return;
93 if (ioctl(fd, SG_IO, &sghdr) < 0)
94 mp_tmsg(MSGT_OPEN, MSGL_INFO, "failed\n");
95 else
96 mp_tmsg(MSGT_OPEN, MSGL_INFO, "successful\n");
98 close(fd);
99 #endif
103 \brief Converts DVD time structure to milliseconds.
104 \param *dev the DVD time structure to convert
105 \return returns the time in milliseconds
107 int mp_dvdtimetomsec(dvd_time_t *dt)
109 static int framerates[4] = {0, 2500, 0, 2997};
110 int framerate = framerates[(dt->frame_u & 0xc0) >> 6];
111 int msec = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
112 msec += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
113 msec += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
114 if(framerate > 0)
115 msec += (((dt->frame_u & 0x30) >> 3) * 5 + (dt->frame_u & 0x0f)) * 100000 / framerate;
116 return msec;