Mercurial > mplayer.hg
annotate libmpdvdkit2/dvd_reader.c @ 15614:a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
author | reimar |
---|---|
date | Thu, 02 Jun 2005 18:57:58 +0000 |
parents | 3758536dcef3 |
children | 483e955893b8 |
rev | line source |
---|---|
7029 | 1 /* |
2 * Copyright (C) 2001, 2002 Billy Biggs <vektor@dumbterm.net>, | |
3 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
4 * | |
14938
25df9508f9a8
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12431
diff
changeset
|
5 * Modified for use with MPlayer, changes contained in libdvdread_changes.diff. |
25df9508f9a8
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12431
diff
changeset
|
6 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
25df9508f9a8
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12431
diff
changeset
|
7 * $Id$ |
25df9508f9a8
Mark modified files as such to comply more closely with GPL ¡ø2a.
diego
parents:
12431
diff
changeset
|
8 * |
7029 | 9 * This program is free software; you can redistribute it and/or modify |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or (at | |
12 * your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
22 */ | |
23 | |
7033 | 24 #include "config.h" |
25 | |
7029 | 26 #include <sys/types.h> |
27 #include <sys/stat.h> | |
28 #include <sys/time.h> /* For the timing of dvdcss_title crack. */ | |
29 #include <fcntl.h> | |
30 #include <stdlib.h> | |
31 #include <stdio.h> | |
32 #include <errno.h> | |
33 #include <string.h> | |
34 #include <unistd.h> | |
35 #include <limits.h> | |
36 #include <dirent.h> | |
37 | |
15566 | 38 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)|| defined(__DARWIN__) || defined(__DragonFly__) |
7029 | 39 #define SYS_BSD 1 |
40 #endif | |
41 | |
42 #if defined(__sun) | |
43 #include <sys/mnttab.h> | |
7423
ad967766679a
hpux DVD support fixes by Martin Gansser <mgansser@ngi.de>
arpi
parents:
7358
diff
changeset
|
44 #elif defined(hpux) |
ad967766679a
hpux DVD support fixes by Martin Gansser <mgansser@ngi.de>
arpi
parents:
7358
diff
changeset
|
45 #include </usr/conf/h/mnttab.h> |
7029 | 46 #elif defined(SYS_BSD) |
47 #include <fstab.h> | |
15534 | 48 #elif defined(__linux__) || defined(__CYGWIN__) |
7029 | 49 #include <mntent.h> |
50 #endif | |
51 | |
10825 | 52 #ifdef __MINGW32__ |
53 #include <sys/timeb.h> | |
54 static void gettimeofday(struct timeval* t,void* timezone){ | |
55 struct timeb timebuffer; | |
56 ftime( &timebuffer ); | |
57 t->tv_sec=timebuffer.time; | |
58 t->tv_usec=1000*timebuffer.millitm; | |
59 } | |
60 #endif | |
61 | |
7029 | 62 #include "dvd_udf.h" |
63 #include "dvd_input.h" | |
64 #include "dvd_reader.h" | |
65 | |
66 struct dvd_reader_s { | |
67 /* Basic information. */ | |
68 int isImageFile; | |
69 | |
70 /* Hack for keeping track of the css status. | |
71 * 0: no css, 1: perhaps (need init of keys), 2: have done init */ | |
72 int css_state; | |
73 int css_title; /* Last title that we have called DVDinpute_title for. */ | |
74 | |
75 /* Information required for an image file. */ | |
76 dvd_input_t dev; | |
77 | |
78 /* Information required for a directory path drive. */ | |
79 char *path_root; | |
80 }; | |
81 | |
82 struct dvd_file_s { | |
83 /* Basic information. */ | |
84 dvd_reader_t *dvd; | |
85 | |
86 /* Hack for selecting the right css title. */ | |
87 int css_title; | |
88 | |
89 /* Information required for an image file. */ | |
90 uint32_t lb_start; | |
91 uint32_t seek_pos; | |
92 | |
93 /* Information required for a directory path drive. */ | |
94 size_t title_sizes[ 9 ]; | |
95 dvd_input_t title_devs[ 9 ]; | |
96 | |
97 /* Calculated at open-time, size in blocks. */ | |
98 ssize_t filesize; | |
99 }; | |
100 | |
101 /* Loop over all titles and call dvdcss_title to crack the keys. */ | |
102 static int initAllCSSKeys( dvd_reader_t *dvd ) | |
103 { | |
104 struct timeval all_s, all_e; | |
105 struct timeval t_s, t_e; | |
106 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
107 uint32_t start, len; | |
108 int title; | |
109 | |
110 fprintf( stderr, "\n" ); | |
111 fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" ); | |
112 fprintf( stderr, "libdvdread: This can take a _long_ time, " | |
113 "please be patient\n\n" ); | |
114 | |
115 gettimeofday(&all_s, NULL); | |
116 | |
117 for( title = 0; title < 100; title++ ) { | |
118 gettimeofday( &t_s, NULL ); | |
119 if( title == 0 ) { | |
120 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
121 } else { | |
122 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 ); | |
123 } | |
124 start = UDFFindFile( dvd, filename, &len ); | |
125 if( start != 0 && len != 0 ) { | |
126 /* Perform CSS key cracking for this title. */ | |
127 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
128 filename, start ); | |
129 if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { | |
130 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start); | |
131 } | |
132 gettimeofday( &t_e, NULL ); | |
133 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
134 (long int) t_e.tv_sec - t_s.tv_sec ); | |
135 } | |
136 | |
137 if( title == 0 ) continue; | |
138 | |
139 gettimeofday( &t_s, NULL ); | |
140 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 ); | |
141 start = UDFFindFile( dvd, filename, &len ); | |
142 if( start == 0 || len == 0 ) break; | |
143 | |
144 /* Perform CSS key cracking for this title. */ | |
145 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
146 filename, start ); | |
147 if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { | |
148 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start); | |
149 } | |
150 gettimeofday( &t_e, NULL ); | |
151 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
152 (long int) t_e.tv_sec - t_s.tv_sec ); | |
153 } | |
154 title--; | |
155 | |
156 fprintf( stderr, "libdvdread: Found %d VTS's\n", title ); | |
157 gettimeofday(&all_e, NULL); | |
158 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
159 (long int) all_e.tv_sec - all_s.tv_sec ); | |
160 | |
161 return 0; | |
162 } | |
163 | |
164 | |
7033 | 165 #ifndef HAVE_MPLAYER |
166 #include "get_path.c" | |
167 #else | |
168 extern char * get_path( char * filename ); | |
169 #endif | |
170 | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
8881
diff
changeset
|
171 //extern char * dvdcss_cache_dir; |
7029 | 172 |
173 /** | |
174 * Open a DVD image or block device file. | |
175 */ | |
176 static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) | |
177 { | |
178 dvd_reader_t *dvd; | |
179 dvd_input_t dev; | |
7033 | 180 |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
8881
diff
changeset
|
181 /* setup cache dir is no longer needed, it's now implemented in libdvdcss.c |
7033 | 182 if(!dvdcss_cache_dir){ |
183 dvdcss_cache_dir=get_path( "" ); | |
184 if ( dvdcss_cache_dir ) { mkdir( dvdcss_cache_dir,493 ); free( dvdcss_cache_dir ); } | |
185 dvdcss_cache_dir=get_path( "DVDKeys" ); | |
186 if(dvdcss_cache_dir) mkdir( dvdcss_cache_dir,493 ); | |
187 } | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
8881
diff
changeset
|
188 */ |
7029 | 189 |
7033 | 190 /* open it */ |
7029 | 191 dev = DVDinput_open( location ); |
192 if( !dev ) { | |
193 fprintf( stderr, "libdvdread: Can't open %s for reading\n", location ); | |
194 return 0; | |
195 } | |
196 | |
197 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
198 if( !dvd ) return 0; | |
199 dvd->isImageFile = 1; | |
200 dvd->dev = dev; | |
201 dvd->path_root = 0; | |
202 | |
203 if( have_css ) { | |
204 /* Only if DVDCSS_METHOD = title, a bit if it's disc or if | |
205 * DVDCSS_METHOD = key but region missmatch. Unfortunaly we | |
206 * don't have that information. */ | |
207 | |
208 dvd->css_state = 1; /* Need key init. */ | |
209 } | |
210 | |
211 return dvd; | |
212 } | |
213 | |
214 static dvd_reader_t *DVDOpenPath( const char *path_root ) | |
215 { | |
216 dvd_reader_t *dvd; | |
217 | |
218 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
219 if( !dvd ) return 0; | |
220 dvd->isImageFile = 0; | |
221 dvd->dev = 0; | |
222 dvd->path_root = strdup( path_root ); | |
223 | |
224 return dvd; | |
225 } | |
226 | |
227 #if defined(__sun) | |
228 /* /dev/rdsk/c0t6d0s0 (link to /devices/...) | |
229 /vol/dev/rdsk/c0t6d0/?? | |
230 /vol/rdsk/<name> */ | |
231 static char *sun_block2char( const char *path ) | |
232 { | |
233 char *new_path; | |
234 | |
235 /* Must contain "/dsk/" */ | |
236 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path ); | |
237 | |
238 /* Replace "/dsk/" with "/rdsk/" */ | |
239 new_path = malloc( strlen(path) + 2 ); | |
240 strcpy( new_path, path ); | |
241 strcpy( strstr( new_path, "/dsk/" ), "" ); | |
242 strcat( new_path, "/rdsk/" ); | |
243 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) ); | |
244 | |
245 return new_path; | |
246 } | |
247 #endif | |
248 | |
249 #if defined(SYS_BSD) | |
250 /* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recomended to _not_ use r | |
251 OpenBSD /dev/rcd0c, it needs to be the raw device | |
252 NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others | |
253 Darwin /dev/rdisk0, it needs to be the raw device | |
254 BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do) */ | |
255 static char *bsd_block2char( const char *path ) | |
256 { | |
257 char *new_path; | |
258 | |
259 /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */ | |
10511 | 260 if( strncmp( path, "/dev/", 5 ) || !strncmp( path, "/dev/r", 6 ) ) |
7029 | 261 return (char *) strdup( path ); |
262 | |
263 /* Replace "/dev/" with "/dev/r" */ | |
264 new_path = malloc( strlen(path) + 2 ); | |
265 strcpy( new_path, "/dev/r" ); | |
266 strcat( new_path, path + strlen( "/dev/" ) ); | |
267 | |
268 return new_path; | |
269 } | |
270 #endif | |
271 | |
272 dvd_reader_t *DVDOpen( const char *path ) | |
273 { | |
274 struct stat fileinfo; | |
275 int ret, have_css; | |
276 char *dev_name = 0; | |
277 | |
278 if( !path ) return 0; | |
279 | |
12431
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
280 #ifdef WIN32 |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
281 /* Stat doesn't work on devices under mingwin/cygwin. */ |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
282 if( path[0] && path[1] == ':' && path[2] == '\0' ) |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
283 { |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
284 /* Don't try to stat the file */ |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
285 fileinfo.st_mode = S_IFBLK; |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
286 } |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
287 else |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
288 #endif |
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
289 { |
7029 | 290 ret = stat( path, &fileinfo ); |
291 if( ret < 0 ) { | |
292 /* If we can't stat the file, give up */ | |
293 fprintf( stderr, "libdvdread: Can't stat %s\n", path ); | |
294 perror(""); | |
295 return 0; | |
296 } | |
12431
663fdd72e594
Encrypted dvd playback now accepts -dvd-drive e: on mingw. fix from libdvdread, left out the various cosmetics changes for now
faust3
parents:
10825
diff
changeset
|
297 } |
7029 | 298 |
299 /* Try to open libdvdcss or fall back to standard functions */ | |
300 have_css = DVDInputSetup(); | |
301 | |
302 /* First check if this is a block/char device or a file*/ | |
303 if( S_ISBLK( fileinfo.st_mode ) || | |
304 S_ISCHR( fileinfo.st_mode ) || | |
305 S_ISREG( fileinfo.st_mode ) ) { | |
306 | |
307 /** | |
308 * Block devices and regular files are assumed to be DVD-Video images. | |
309 */ | |
310 #if defined(__sun) | |
311 return DVDOpenImageFile( sun_block2char( path ), have_css ); | |
312 #elif defined(SYS_BSD) | |
313 return DVDOpenImageFile( bsd_block2char( path ), have_css ); | |
314 #else | |
315 return DVDOpenImageFile( path, have_css ); | |
316 #endif | |
317 | |
318 } else if( S_ISDIR( fileinfo.st_mode ) ) { | |
319 dvd_reader_t *auth_drive = 0; | |
320 char *path_copy; | |
321 #if defined(SYS_BSD) | |
322 struct fstab* fe; | |
15534 | 323 #elif defined(__sun) || defined(__linux__) || defined(__CYGWIN__) |
7029 | 324 FILE *mntfile; |
325 #endif | |
326 | |
327 /* XXX: We should scream real loud here. */ | |
328 if( !(path_copy = strdup( path ) ) ) return 0; | |
329 | |
330 /* Resolve any symlinks and get the absolut dir name. */ | |
331 { | |
332 char *new_path; | |
333 int cdir = open( ".", O_RDONLY ); | |
334 | |
335 if( cdir >= 0 ) { | |
336 chdir( path_copy ); | |
337 new_path = getcwd( NULL, PATH_MAX ); | |
10443 | 338 #ifndef __MINGW32__ |
7029 | 339 fchdir( cdir ); |
10443 | 340 #endif |
7029 | 341 close( cdir ); |
342 if( new_path ) { | |
343 free( path_copy ); | |
344 path_copy = new_path; | |
345 } | |
346 } | |
347 } | |
348 | |
349 /** | |
350 * If we're being asked to open a directory, check if that directory | |
351 * is the mountpoint for a DVD-ROM which we can use instead. | |
352 */ | |
353 | |
354 if( strlen( path_copy ) > 1 ) { | |
355 if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) | |
356 path_copy[ strlen( path_copy ) - 1 ] = '\0'; | |
357 } | |
358 | |
359 if( strlen( path_copy ) > 9 ) { | |
360 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]), | |
361 "/video_ts" ) ) { | |
362 path_copy[ strlen( path_copy ) - 9 ] = '\0'; | |
363 } | |
364 } | |
365 | |
366 #if defined(SYS_BSD) | |
367 if( ( fe = getfsfile( path_copy ) ) ) { | |
368 dev_name = bsd_block2char( fe->fs_spec ); | |
369 fprintf( stderr, | |
370 "libdvdread: Attempting to use device %s" | |
371 " mounted on %s for CSS authentication\n", | |
372 dev_name, | |
373 fe->fs_file ); | |
374 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
375 } | |
376 #elif defined(__sun) | |
377 mntfile = fopen( MNTTAB, "r" ); | |
378 if( mntfile ) { | |
379 struct mnttab mp; | |
380 int res; | |
381 | |
382 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) { | |
383 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) { | |
384 dev_name = sun_block2char( mp.mnt_special ); | |
385 fprintf( stderr, | |
386 "libdvdread: Attempting to use device %s" | |
387 " mounted on %s for CSS authentication\n", | |
388 dev_name, | |
389 mp.mnt_mountp ); | |
390 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
391 break; | |
392 } | |
393 } | |
394 fclose( mntfile ); | |
395 } | |
15534 | 396 #elif defined(__linux__) || defined(__CYGWIN__) |
7029 | 397 mntfile = fopen( MOUNTED, "r" ); |
398 if( mntfile ) { | |
399 struct mntent *me; | |
400 | |
401 while( ( me = getmntent( mntfile ) ) ) { | |
402 if( !strcmp( me->mnt_dir, path_copy ) ) { | |
403 fprintf( stderr, | |
404 "libdvdread: Attempting to use device %s" | |
405 " mounted on %s for CSS authentication\n", | |
406 me->mnt_fsname, | |
407 me->mnt_dir ); | |
408 auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css ); | |
409 dev_name = strdup(me->mnt_fsname); | |
410 break; | |
411 } | |
412 } | |
413 fclose( mntfile ); | |
414 } | |
15534 | 415 #elif defined(__MINGW32__) |
7033 | 416 dev_name = strdup(path); |
417 auth_drive = DVDOpenImageFile( path, have_css ); | |
7029 | 418 #endif |
419 if( !dev_name ) { | |
420 fprintf( stderr, "libdvdread: Couldn't find device name.\n" ); | |
421 } else if( !auth_drive ) { | |
422 fprintf( stderr, "libdvdread: Device %s inaccessible, " | |
423 "CSS authentication not available.\n", dev_name ); | |
424 } | |
425 | |
426 free( dev_name ); | |
427 free( path_copy ); | |
428 | |
429 /** | |
430 * If we've opened a drive, just use that. | |
431 */ | |
432 if( auth_drive ) return auth_drive; | |
433 | |
434 /** | |
435 * Otherwise, we now try to open the directory tree instead. | |
436 */ | |
437 return DVDOpenPath( path ); | |
438 } | |
439 | |
440 /* If it's none of the above, screw it. */ | |
441 fprintf( stderr, "libdvdread: Could not open %s\n", path ); | |
442 return 0; | |
443 } | |
444 | |
445 void DVDClose( dvd_reader_t *dvd ) | |
446 { | |
447 if( dvd ) { | |
448 if( dvd->dev ) DVDinput_close( dvd->dev ); | |
449 if( dvd->path_root ) free( dvd->path_root ); | |
450 free( dvd ); | |
451 dvd = 0; | |
452 } | |
453 } | |
454 | |
455 /** | |
456 * Open an unencrypted file on a DVD image file. | |
457 */ | |
458 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename ) | |
459 { | |
460 uint32_t start, len; | |
461 dvd_file_t *dvd_file; | |
462 | |
463 start = UDFFindFile( dvd, filename, &len ); | |
464 if( !start ) return 0; | |
465 | |
466 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
467 if( !dvd_file ) return 0; | |
468 dvd_file->dvd = dvd; | |
469 dvd_file->lb_start = start; | |
470 dvd_file->seek_pos = 0; | |
471 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
472 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
473 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
474 | |
475 return dvd_file; | |
476 } | |
477 | |
478 /** | |
479 * Searches for <file> in directory <path>, ignoring case. | |
480 * Returns 0 and full filename in <filename>. | |
481 * or -1 on file not found. | |
482 * or -2 on path not found. | |
483 */ | |
484 static int findDirFile( const char *path, const char *file, char *filename ) | |
485 { | |
486 DIR *dir; | |
487 struct dirent *ent; | |
488 | |
489 dir = opendir( path ); | |
490 if( !dir ) return -2; | |
491 | |
492 while( ( ent = readdir( dir ) ) != NULL ) { | |
493 if( !strcasecmp( ent->d_name, file ) ) { | |
494 sprintf( filename, "%s%s%s", path, | |
495 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ), | |
496 ent->d_name ); | |
497 return 0; | |
498 } | |
499 } | |
500 | |
501 return -1; | |
502 } | |
503 | |
504 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename ) | |
505 { | |
506 char video_path[ PATH_MAX + 1 ]; | |
507 const char *nodirfile; | |
508 int ret; | |
509 | |
510 /* Strip off the directory for our search */ | |
511 if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) { | |
512 nodirfile = &(file[ 10 ]); | |
513 } else { | |
514 nodirfile = file; | |
515 } | |
516 | |
517 ret = findDirFile( dvd->path_root, nodirfile, filename ); | |
518 if( ret < 0 ) { | |
519 /* Try also with adding the path, just in case. */ | |
520 sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root ); | |
521 ret = findDirFile( video_path, nodirfile, filename ); | |
522 if( ret < 0 ) { | |
523 /* Try with the path, but in lower case. */ | |
524 sprintf( video_path, "%s/video_ts/", dvd->path_root ); | |
525 ret = findDirFile( video_path, nodirfile, filename ); | |
526 if( ret < 0 ) { | |
527 return 0; | |
528 } | |
529 } | |
530 } | |
531 | |
532 return 1; | |
533 } | |
534 | |
535 /** | |
536 * Open an unencrypted file from a DVD directory tree. | |
537 */ | |
538 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename ) | |
539 { | |
540 char full_path[ PATH_MAX + 1 ]; | |
541 dvd_file_t *dvd_file; | |
542 struct stat fileinfo; | |
543 dvd_input_t dev; | |
544 | |
545 /* Get the full path of the file. */ | |
546 if( !findDVDFile( dvd, filename, full_path ) ) return 0; | |
547 | |
548 dev = DVDinput_open( full_path ); | |
549 if( !dev ) return 0; | |
550 | |
551 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
552 if( !dvd_file ) return 0; | |
553 dvd_file->dvd = dvd; | |
554 dvd_file->lb_start = 0; | |
555 dvd_file->seek_pos = 0; | |
556 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
557 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
558 dvd_file->filesize = 0; | |
559 | |
560 if( stat( full_path, &fileinfo ) < 0 ) { | |
561 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
562 free( dvd_file ); | |
563 return 0; | |
564 } | |
565 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
566 dvd_file->title_devs[ 0 ] = dev; | |
567 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
568 | |
569 return dvd_file; | |
570 } | |
571 | |
572 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu ) | |
573 { | |
574 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
575 uint32_t start, len; | |
576 dvd_file_t *dvd_file; | |
577 | |
578 if( title == 0 ) { | |
579 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
580 } else { | |
581 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 ); | |
582 } | |
583 start = UDFFindFile( dvd, filename, &len ); | |
584 if( start == 0 ) return 0; | |
585 | |
586 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
587 if( !dvd_file ) return 0; | |
588 dvd_file->dvd = dvd; | |
589 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
590 dvd_file->lb_start = start; | |
591 dvd_file->seek_pos = 0; | |
592 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
593 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
594 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
595 | |
596 /* Calculate the complete file size for every file in the VOBS */ | |
597 if( !menu ) { | |
598 int cur; | |
599 | |
600 for( cur = 2; cur < 10; cur++ ) { | |
601 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur ); | |
602 if( !UDFFindFile( dvd, filename, &len ) ) break; | |
603 dvd_file->filesize += len / DVD_VIDEO_LB_LEN; | |
604 } | |
605 } | |
606 | |
607 if( dvd->css_state == 1 /* Need key init */ ) { | |
7033 | 608 // initAllCSSKeys( dvd ); |
609 // dvd->css_state = 2; | |
7029 | 610 } |
611 /* | |
612 if( DVDinput_seek( dvd_file->dvd->dev, | |
613 (int)start, DVDINPUT_SEEK_KEY ) < 0 ) { | |
614 fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n", | |
615 filename ); | |
616 } | |
617 */ | |
618 | |
619 return dvd_file; | |
620 } | |
621 | |
622 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) | |
623 { | |
624 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
625 char full_path[ PATH_MAX + 1 ]; | |
626 struct stat fileinfo; | |
627 dvd_file_t *dvd_file; | |
628 int i; | |
629 | |
630 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
631 if( !dvd_file ) return 0; | |
632 dvd_file->dvd = dvd; | |
633 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
634 dvd_file->lb_start = 0; | |
635 dvd_file->seek_pos = 0; | |
636 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
637 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
638 dvd_file->filesize = 0; | |
639 | |
640 if( menu ) { | |
641 dvd_input_t dev; | |
642 | |
643 if( title == 0 ) { | |
644 sprintf( filename, "VIDEO_TS.VOB" ); | |
645 } else { | |
646 sprintf( filename, "VTS_%02i_0.VOB", title ); | |
647 } | |
648 if( !findDVDFile( dvd, filename, full_path ) ) { | |
649 free( dvd_file ); | |
650 return 0; | |
651 } | |
652 | |
653 dev = DVDinput_open( full_path ); | |
654 if( dev == NULL ) { | |
655 free( dvd_file ); | |
656 return 0; | |
657 } | |
658 | |
659 if( stat( full_path, &fileinfo ) < 0 ) { | |
660 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
661 free( dvd_file ); | |
662 return 0; | |
663 } | |
664 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
665 dvd_file->title_devs[ 0 ] = dev; | |
666 DVDinput_seek( dvd_file->title_devs[0], 0, DVDINPUT_SEEK_KEY ); | |
667 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
668 | |
669 } else { | |
670 for( i = 0; i < 9; ++i ) { | |
671 | |
672 sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 ); | |
673 if( !findDVDFile( dvd, filename, full_path ) ) { | |
674 break; | |
675 } | |
676 | |
677 if( stat( full_path, &fileinfo ) < 0 ) { | |
678 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
679 break; | |
680 } | |
681 | |
682 dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
683 dvd_file->title_devs[ i ] = DVDinput_open( full_path ); | |
684 dvd_file->filesize += dvd_file->title_sizes[ i ]; | |
8881
1e40d4a2466f
Function DVDOpenVOBPath only decrypts first VOB file and since each VOB file has
arpi
parents:
7423
diff
changeset
|
685 DVDinput_seek( dvd_file->title_devs[ i ], 0, DVDINPUT_SEEK_KEY ); |
7029 | 686 } |
8881
1e40d4a2466f
Function DVDOpenVOBPath only decrypts first VOB file and since each VOB file has
arpi
parents:
7423
diff
changeset
|
687 if( !dvd_file->title_devs[ 0 ] ) { |
7029 | 688 free( dvd_file ); |
689 return 0; | |
690 } | |
691 } | |
692 | |
693 return dvd_file; | |
694 } | |
695 | |
696 dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, | |
697 dvd_read_domain_t domain ) | |
698 { | |
699 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
700 | |
701 switch( domain ) { | |
702 case DVD_READ_INFO_FILE: | |
703 if( titlenum == 0 ) { | |
704 sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" ); | |
705 } else { | |
706 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum ); | |
707 } | |
708 break; | |
709 case DVD_READ_INFO_BACKUP_FILE: | |
710 if( titlenum == 0 ) { | |
711 sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" ); | |
712 } else { | |
713 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum ); | |
714 } | |
715 break; | |
716 case DVD_READ_MENU_VOBS: | |
717 if( dvd->isImageFile ) { | |
718 return DVDOpenVOBUDF( dvd, titlenum, 1 ); | |
719 } else { | |
720 return DVDOpenVOBPath( dvd, titlenum, 1 ); | |
721 } | |
722 break; | |
723 case DVD_READ_TITLE_VOBS: | |
724 if( titlenum == 0 ) return 0; | |
725 if( dvd->isImageFile ) { | |
726 return DVDOpenVOBUDF( dvd, titlenum, 0 ); | |
727 } else { | |
728 return DVDOpenVOBPath( dvd, titlenum, 0 ); | |
729 } | |
730 break; | |
731 default: | |
732 fprintf( stderr, "libdvdread: Invalid domain for file open.\n" ); | |
733 return 0; | |
734 } | |
735 | |
736 if( dvd->isImageFile ) { | |
737 return DVDOpenFileUDF( dvd, filename ); | |
738 } else { | |
739 return DVDOpenFilePath( dvd, filename ); | |
740 } | |
741 } | |
742 | |
743 void DVDCloseFile( dvd_file_t *dvd_file ) | |
744 { | |
745 int i; | |
746 | |
747 if( dvd_file ) { | |
748 if( dvd_file->dvd->isImageFile ) { | |
749 ; | |
750 } else { | |
751 for( i = 0; i < 9; ++i ) { | |
752 if( dvd_file->title_devs[ i ] ) { | |
753 DVDinput_close( dvd_file->title_devs[i] ); | |
754 } | |
755 } | |
756 } | |
757 | |
758 free( dvd_file ); | |
759 dvd_file = 0; | |
760 } | |
761 } | |
762 | |
763 /* Internal, but used from dvd_udf.c */ | |
764 int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, | |
765 size_t block_count, unsigned char *data, | |
766 int encrypted ) | |
767 { | |
768 int ret; | |
769 | |
770 if( !device->dev ) { | |
771 fprintf( stderr, "libdvdread: Fatal error in block read.\n" ); | |
772 return 0; | |
773 } | |
774 | |
775 ret = DVDinput_seek( device->dev, (int) lb_number, DVDINPUT_NOFLAGS ); | |
776 if( ret != (int) lb_number ) { | |
777 fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number ); | |
778 return 0; | |
779 } | |
780 | |
781 return DVDinput_read( device->dev, (char *) data, | |
782 (int) block_count, encrypted ); | |
783 } | |
784 | |
785 /* This is using a single input and starting from 'dvd_file->lb_start' offset. | |
786 * | |
787 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
788 * into the buffer located at 'data' and if 'encrypted' is set | |
789 * descramble the data if it's encrypted. Returning either an | |
790 * negative error or the number of blocks read. */ | |
791 static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset, | |
792 size_t block_count, unsigned char *data, | |
793 int encrypted ) | |
794 { | |
795 return DVDReadBlocksUDFRaw( dvd_file->dvd, dvd_file->lb_start + offset, | |
796 block_count, data, encrypted ); | |
797 } | |
798 | |
799 /* This is using possibly several inputs and starting from an offset of '0'. | |
800 * | |
801 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
802 * into the buffer located at 'data' and if 'encrypted' is set | |
803 * descramble the data if it's encrypted. Returning either an | |
804 * negative error or the number of blocks read. */ | |
805 static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, | |
806 size_t block_count, unsigned char *data, | |
807 int encrypted ) | |
808 { | |
809 int i; | |
810 int ret, ret2, off; | |
811 | |
812 ret = 0; | |
813 ret2 = 0; | |
814 for( i = 0; i < 9; ++i ) { | |
815 if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */ | |
816 | |
817 if( offset < dvd_file->title_sizes[ i ] ) { | |
818 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) { | |
819 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
820 (int)offset, DVDINPUT_NOFLAGS ); | |
821 if( off < 0 || off != (int)offset ) { | |
822 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
823 offset ); | |
824 return off < 0 ? off : 0; | |
825 } | |
826 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
827 (int)block_count, encrypted ); | |
828 break; | |
829 } else { | |
830 size_t part1_size = dvd_file->title_sizes[ i ] - offset; | |
831 /* FIXME: Really needs to be a while loop. | |
832 * (This is only true if you try and read >1GB at a time) */ | |
833 | |
834 /* Read part 1 */ | |
835 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
836 (int)offset, DVDINPUT_NOFLAGS ); | |
837 if( off < 0 || off != (int)offset ) { | |
838 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
839 offset ); | |
840 return off < 0 ? off : 0; | |
841 } | |
842 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
843 (int)part1_size, encrypted ); | |
844 if( ret < 0 ) return ret; | |
845 /* FIXME: This is wrong if i is the last file in the set. | |
846 * also error from this read will not show in ret. */ | |
7358
bb40478265df
I experienced several segfaults when trying to play (unencrypted) DVDs
arpi
parents:
7033
diff
changeset
|
847 |
bb40478265df
I experienced several segfaults when trying to play (unencrypted) DVDs
arpi
parents:
7033
diff
changeset
|
848 /* Does the next part exist? If not then return now. */ |
bb40478265df
I experienced several segfaults when trying to play (unencrypted) DVDs
arpi
parents:
7033
diff
changeset
|
849 if( !dvd_file->title_devs[ i + 1 ] ) return ret; |
bb40478265df
I experienced several segfaults when trying to play (unencrypted) DVDs
arpi
parents:
7033
diff
changeset
|
850 |
7029 | 851 /* Read part 2 */ |
852 off = DVDinput_seek( dvd_file->title_devs[ i + 1 ], | |
853 0, DVDINPUT_NOFLAGS ); | |
854 if( off < 0 || off != 0 ) { | |
855 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
856 0 ); | |
857 return off < 0 ? off : 0; | |
858 } | |
859 ret2 = DVDinput_read( dvd_file->title_devs[ i + 1 ], | |
860 data + ( part1_size | |
861 * (int64_t)DVD_VIDEO_LB_LEN ), | |
862 (int)(block_count - part1_size), | |
863 encrypted ); | |
864 if( ret2 < 0 ) return ret2; | |
865 break; | |
866 } | |
867 } else { | |
868 offset -= dvd_file->title_sizes[ i ]; | |
869 } | |
870 } | |
871 | |
872 return ret + ret2; | |
873 } | |
874 | |
875 /* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */ | |
876 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, | |
877 size_t block_count, unsigned char *data ) | |
878 { | |
879 int ret; | |
880 | |
881 /* Hack, and it will still fail for multiple opens in a threaded app ! */ | |
882 if( dvd_file->dvd->css_title != dvd_file->css_title ) { | |
883 dvd_file->dvd->css_title = dvd_file->css_title; | |
884 if( dvd_file->dvd->isImageFile ) { | |
885 DVDinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); | |
886 } else { | |
887 DVDinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); | |
888 } | |
889 } | |
890 | |
891 if( dvd_file->dvd->isImageFile ) { | |
892 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset, | |
893 block_count, data, DVDINPUT_READ_DECRYPT ); | |
894 } else { | |
895 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset, | |
896 block_count, data, DVDINPUT_READ_DECRYPT ); | |
897 } | |
898 | |
899 return (ssize_t)ret; | |
900 } | |
901 | |
10017
535a53c058f8
There are conflicting definitions for DVDFileSeek in the .c and .h file.
diego
parents:
9333
diff
changeset
|
902 int DVDFileSeek( dvd_file_t *dvd_file, int offset ) |
7029 | 903 { |
904 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { | |
905 return -1; | |
906 } | |
907 dvd_file->seek_pos = (uint32_t) offset; | |
908 return offset; | |
909 } | |
910 | |
911 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) | |
912 { | |
913 unsigned char *secbuf; | |
914 unsigned int numsec, seek_sector, seek_byte; | |
915 int ret; | |
916 | |
917 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN; | |
918 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN; | |
919 | |
920 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + 1; | |
921 secbuf = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN ); | |
922 if( !secbuf ) { | |
923 fprintf( stderr, "libdvdread: Can't allocate memory " | |
924 "for file read!\n" ); | |
925 return 0; | |
926 } | |
927 | |
928 if( dvd_file->dvd->isImageFile ) { | |
929 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector, | |
930 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
931 } else { | |
932 ret = DVDReadBlocksPath( dvd_file, seek_sector, | |
933 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
934 } | |
935 | |
936 if( ret != (int) numsec ) { | |
937 free( secbuf ); | |
938 return ret < 0 ? ret : 0; | |
939 } | |
940 | |
941 memcpy( data, &(secbuf[ seek_byte ]), byte_size ); | |
942 free( secbuf ); | |
943 | |
944 dvd_file->seek_pos += byte_size; | |
945 return byte_size; | |
946 } | |
947 | |
948 ssize_t DVDFileSize( dvd_file_t *dvd_file ) | |
949 { | |
950 return dvd_file->filesize; | |
951 } |