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