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