annotate dvd_input.c @ 183:b0aa6f7931c0 src

merging patch from xine-lib's libdvdread * this mostly aligns all data blocks in memory to make raw device reads work * some MSVC related patches that went into xine-lib cvs are also included
author mroi
date Tue, 29 Apr 2003 15:14:00 +0000
parents 7993d22f6487
children 36777e6346c5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
168
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
1 /*
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
2 * Copyright (C) 2002 Samuel Hocevar <sam@zoy.org>,
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
3 * Håkan Hjort <d95hjort@dtek.chalmers.se>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
4 *
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
8 * (at your option) any later version.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
9 *
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
13 * GNU General Public License for more details.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
14 *
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
18 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
19
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
20 #include "config.h"
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
21
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
22 #include <stdio.h>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
23 #include <stdlib.h>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
24 #include <fcntl.h>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
25 #include <unistd.h>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
26
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
27 #include "dvd_reader.h"
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
28 #include "dvd_input.h"
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
29
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
30 /* The function pointers that is the exported interface of this file. */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
31 dvd_input_t (*dvdinput_open) (const char *);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
32 int (*dvdinput_close) (dvd_input_t);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
33 int (*dvdinput_seek) (dvd_input_t, int);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
34 int (*dvdinput_title) (dvd_input_t, int);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
35 int (*dvdinput_read) (dvd_input_t, void *, int, int);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
36 char * (*dvdinput_error) (dvd_input_t);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
37
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
38 #ifdef HAVE_DVDCSS_DVDCSS_H
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
39 /* linking to libdvdcss */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
40 #include <dvdcss/dvdcss.h>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
41 #define DVDcss_open(a) dvdcss_open((char*)(a))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
42 #define DVDcss_close dvdcss_close
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
43 #define DVDcss_seek dvdcss_seek
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
44 #define DVDcss_title dvdcss_title
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
45 #define DVDcss_read dvdcss_read
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
46 #define DVDcss_error dvdcss_error
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
47 #else
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
48 /* dlopening libdvdcss */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
49 #include <dlfcn.h>
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
50 typedef struct dvdcss_s *dvdcss_handle;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
51 static dvdcss_handle (*DVDcss_open) (const char *);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
52 static int (*DVDcss_close) (dvdcss_handle);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
53 static int (*DVDcss_seek) (dvdcss_handle, int, int);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
54 static int (*DVDcss_title) (dvdcss_handle, int);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
55 static int (*DVDcss_read) (dvdcss_handle, void *, int, int);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
56 static char * (*DVDcss_error) (dvdcss_handle);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
57 #endif
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
58
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
59 /* The DVDinput handle, add stuff here for new input methods. */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
60 struct dvd_input_s {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
61 /* libdvdcss handle */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
62 dvdcss_handle dvdcss;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
63
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
64 /* dummy file input */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
65 int fd;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
66 };
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
67
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
68
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
69 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
70 * initialize and open a DVD device or file.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
71 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
72 static dvd_input_t css_open(const char *target)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
73 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
74 dvd_input_t dev;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
75
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
76 /* Allocate the handle structure */
183
b0aa6f7931c0 merging patch from xine-lib's libdvdread
mroi
parents: 178
diff changeset
77 dev = (dvd_input_t) malloc(sizeof(*dev));
168
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
78 if(dev == NULL) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
79 fprintf(stderr, "libdvdread: Could not allocate memory.\n");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
80 return NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
81 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
82
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
83 /* Really open it with libdvdcss */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
84 dev->dvdcss = DVDcss_open(target);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
85 if(dev->dvdcss == 0) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
86 fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
87 free(dev);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
88 return NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
89 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
90
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
91 return dev;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
92 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
93
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
94 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
95 * return the last error message
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
96 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
97 static char *css_error(dvd_input_t dev)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
98 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
99 return DVDcss_error(dev->dvdcss);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
100 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
101
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
102 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
103 * seek into the device.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
104 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
105 static int css_seek(dvd_input_t dev, int blocks)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
106 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
107 /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
108 return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
109 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
110
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
111 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
112 * set the block for the begining of a new title (key).
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
113 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
114 static int css_title(dvd_input_t dev, int block)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
115 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
116 return DVDcss_title(dev->dvdcss, block);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
117 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
118
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
119 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
120 * read data from the device.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
121 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
122 static int css_read(dvd_input_t dev, void *buffer, int blocks, int flags)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
123 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
124 return DVDcss_read(dev->dvdcss, buffer, blocks, flags);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
125 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
126
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
127 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
128 * close the DVD device and clean up the library.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
129 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
130 static int css_close(dvd_input_t dev)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
131 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
132 int ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
133
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
134 ret = DVDcss_close(dev->dvdcss);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
135
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
136 if(ret < 0)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
137 return ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
138
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
139 free(dev);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
140
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
141 return 0;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
142 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
143
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
144
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
145
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
146
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
147
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
148
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
149 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
150 * initialize and open a DVD device or file.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
151 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
152 static dvd_input_t file_open(const char *target)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
153 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
154 dvd_input_t dev;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
155
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
156 /* Allocate the library structure */
183
b0aa6f7931c0 merging patch from xine-lib's libdvdread
mroi
parents: 178
diff changeset
157 dev = (dvd_input_t) malloc(sizeof(*dev));
168
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
158 if(dev == NULL) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
159 fprintf(stderr, "libdvdread: Could not allocate memory.\n");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
160 return NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
161 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
162
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
163 /* Open the device */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
164 dev->fd = open(target, O_RDONLY);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
165 if(dev->fd < 0) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
166 perror("libdvdread: Could not open input");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
167 free(dev);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
168 return NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
169 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
170
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
171 return dev;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
172 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
173
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
174 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
175 * return the last error message
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
176 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
177 static char *file_error(dvd_input_t dev)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
178 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
179 /* use strerror(errno)? */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
180 return (char *)"unknown error";
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
181 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
182
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
183 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
184 * seek into the device.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
185 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
186 static int file_seek(dvd_input_t dev, int blocks)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
187 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
188 off_t pos;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
189
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
190 pos = lseek(dev->fd, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN, SEEK_SET);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
191 if(pos < 0) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
192 return pos;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
193 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
194 /* assert pos % DVD_VIDEO_LB_LEN == 0 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
195 return (int) (pos / DVD_VIDEO_LB_LEN);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
196 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
197
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
198 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
199 * set the block for the begining of a new title (key).
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
200 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
201 static int file_title(dvd_input_t dev, int block)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
202 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
203 return -1;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
204 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
205
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
206 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
207 * read data from the device.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
208 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
209 static int file_read(dvd_input_t dev, void *buffer, int blocks, int flags)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
210 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
211 size_t len;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
212 ssize_t ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
213
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
214 len = (size_t)blocks * DVD_VIDEO_LB_LEN;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
215
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
216 while(len > 0) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
217
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
218 ret = read(dev->fd, buffer, len);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
219
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
220 if(ret < 0) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
221 /* One of the reads failed, too bad. We won't even bother
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
222 * returning the reads that went ok, and as in the posix spec
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
223 * the file postition is left unspecified after a failure. */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
224 return ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
225 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
226
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
227 if(ret == 0) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
228 /* Nothing more to read. Return the whole blocks, if any, that we got.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
229 and adjust the file possition back to the previous block boundary. */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
230 size_t bytes = (size_t)blocks * DVD_VIDEO_LB_LEN - len;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
231 off_t over_read = -(bytes % DVD_VIDEO_LB_LEN);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
232 /*off_t pos =*/ lseek(dev->fd, over_read, SEEK_CUR);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
233 /* should have pos % 2048 == 0 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
234 return (int) (bytes / DVD_VIDEO_LB_LEN);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
235 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
236
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
237 len -= ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
238 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
239
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
240 return blocks;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
241 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
242
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
243 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
244 * close the DVD device and clean up.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
245 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
246 static int file_close(dvd_input_t dev)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
247 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
248 int ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
249
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
250 ret = close(dev->fd);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
251
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
252 if(ret < 0)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
253 return ret;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
254
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
255 free(dev);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
256
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
257 return 0;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
258 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
259
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
260
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
261 /**
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
262 * Setup read functions with either libdvdcss or minimal DVD access.
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
263 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
264 int dvdinput_setup(void)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
265 {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
266 void *dvdcss_library = NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
267 char **dvdcss_version = NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
268
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
269 #ifdef HAVE_DVDCSS_DVDCSS_H
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
270 /* linking to libdvdcss */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
271 dvdcss_library = &dvdcss_library; /* Give it some value != NULL */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
272 /* the DVDcss_* functions have been #defined at the top */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
273 dvdcss_version = &dvdcss_interface_2;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
274
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
275 #else
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
276 /* dlopening libdvdcss */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
277 dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
278
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
279 if(dvdcss_library != NULL) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
280 #if defined(__OpenBSD__) && !defined(__ELF__)
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
281 #define U_S "_"
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
282 #else
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
283 #define U_S
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
284 #endif
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
285 DVDcss_open = (dvdcss_handle (*)(const char*))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
286 dlsym(dvdcss_library, U_S "dvdcss_open");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
287 DVDcss_close = (int (*)(dvdcss_handle))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
288 dlsym(dvdcss_library, U_S "dvdcss_close");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
289 DVDcss_title = (int (*)(dvdcss_handle, int))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
290 dlsym(dvdcss_library, U_S "dvdcss_title");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
291 DVDcss_seek = (int (*)(dvdcss_handle, int, int))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
292 dlsym(dvdcss_library, U_S "dvdcss_seek");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
293 DVDcss_read = (int (*)(dvdcss_handle, void*, int, int))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
294 dlsym(dvdcss_library, U_S "dvdcss_read");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
295 DVDcss_error = (char* (*)(dvdcss_handle))
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
296 dlsym(dvdcss_library, U_S "dvdcss_error");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
297
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
298 dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
299
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
300 if(dlsym(dvdcss_library, U_S "dvdcss_crack")) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
301 fprintf(stderr,
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
302 "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n"
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
303 "libdvdread: You should get the latest version from "
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
304 "http://www.videolan.org/\n" );
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
305 dlclose(dvdcss_library);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
306 dvdcss_library = NULL;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
307 } else if(!DVDcss_open || !DVDcss_close || !DVDcss_title || !DVDcss_seek
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
308 || !DVDcss_read || !DVDcss_error || !dvdcss_version) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
309 fprintf(stderr, "libdvdread: Missing symbols in libdvdcss.so.2, "
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
310 "this shouldn't happen !\n");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
311 dlclose(dvdcss_library);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
312 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
313 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
314 #endif /* HAVE_DVDCSS_DVDCSS_H */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
315
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
316 if(dvdcss_library != NULL) {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
317 /*
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
318 char *psz_method = getenv( "DVDCSS_METHOD" );
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
319 char *psz_verbose = getenv( "DVDCSS_VERBOSE" );
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
320 fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
321 fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
322 */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
323 fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n",
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
324 *dvdcss_version);
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
325
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
326 /* libdvdcss wrapper functions */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
327 dvdinput_open = css_open;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
328 dvdinput_close = css_close;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
329 dvdinput_seek = css_seek;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
330 dvdinput_title = css_title;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
331 dvdinput_read = css_read;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
332 dvdinput_error = css_error;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
333 return 1;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
334
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
335 } else {
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
336 fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n");
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
337
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
338 /* libdvdcss replacement functions */
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
339 dvdinput_open = file_open;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
340 dvdinput_close = file_close;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
341 dvdinput_seek = file_seek;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
342 dvdinput_title = file_title;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
343 dvdinput_read = file_read;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
344 dvdinput_error = file_error;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
345 return 0;
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
346 }
330ef38ad968 Add files from libdvdread.
jcdutton
parents:
diff changeset
347 }