Mercurial > mplayer.hg
annotate libmpdvdkit2/dvd_reader.c @ 9148:946b14a9e743
type autodetection (from filemask/filelist extension)
author | arpi |
---|---|
date | Tue, 28 Jan 2003 22:00:57 +0000 |
parents | 1e40d4a2466f |
children | f0f0f176d298 |
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 | |
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 |
167 /* setup cache dir */ | |
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 } | |
7029 | 174 |
7033 | 175 /* open it */ |
7029 | 176 dev = DVDinput_open( location ); |
177 if( !dev ) { | |
178 fprintf( stderr, "libdvdread: Can't open %s for reading\n", location ); | |
179 return 0; | |
180 } | |
181 | |
182 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
183 if( !dvd ) return 0; | |
184 dvd->isImageFile = 1; | |
185 dvd->dev = dev; | |
186 dvd->path_root = 0; | |
187 | |
188 if( have_css ) { | |
189 /* Only if DVDCSS_METHOD = title, a bit if it's disc or if | |
190 * DVDCSS_METHOD = key but region missmatch. Unfortunaly we | |
191 * don't have that information. */ | |
192 | |
193 dvd->css_state = 1; /* Need key init. */ | |
194 } | |
195 | |
196 return dvd; | |
197 } | |
198 | |
199 static dvd_reader_t *DVDOpenPath( const char *path_root ) | |
200 { | |
201 dvd_reader_t *dvd; | |
202 | |
203 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
204 if( !dvd ) return 0; | |
205 dvd->isImageFile = 0; | |
206 dvd->dev = 0; | |
207 dvd->path_root = strdup( path_root ); | |
208 | |
209 return dvd; | |
210 } | |
211 | |
212 #if defined(__sun) | |
213 /* /dev/rdsk/c0t6d0s0 (link to /devices/...) | |
214 /vol/dev/rdsk/c0t6d0/?? | |
215 /vol/rdsk/<name> */ | |
216 static char *sun_block2char( const char *path ) | |
217 { | |
218 char *new_path; | |
219 | |
220 /* Must contain "/dsk/" */ | |
221 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path ); | |
222 | |
223 /* Replace "/dsk/" with "/rdsk/" */ | |
224 new_path = malloc( strlen(path) + 2 ); | |
225 strcpy( new_path, path ); | |
226 strcpy( strstr( new_path, "/dsk/" ), "" ); | |
227 strcat( new_path, "/rdsk/" ); | |
228 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) ); | |
229 | |
230 return new_path; | |
231 } | |
232 #endif | |
233 | |
234 #if defined(SYS_BSD) | |
235 /* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recomended to _not_ use r | |
236 OpenBSD /dev/rcd0c, it needs to be the raw device | |
237 NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others | |
238 Darwin /dev/rdisk0, it needs to be the raw device | |
239 BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do) */ | |
240 static char *bsd_block2char( const char *path ) | |
241 { | |
242 char *new_path; | |
243 | |
244 /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */ | |
245 if( !strncmp( path, "/dev/", 5 ) || strncmp( path, "/dev/r", 6 ) ) | |
246 return (char *) strdup( path ); | |
247 | |
248 /* Replace "/dev/" with "/dev/r" */ | |
249 new_path = malloc( strlen(path) + 2 ); | |
250 strcpy( new_path, "/dev/r" ); | |
251 strcat( new_path, path + strlen( "/dev/" ) ); | |
252 | |
253 return new_path; | |
254 } | |
255 #endif | |
256 | |
257 dvd_reader_t *DVDOpen( const char *path ) | |
258 { | |
259 struct stat fileinfo; | |
260 int ret, have_css; | |
261 char *dev_name = 0; | |
262 | |
263 if( !path ) return 0; | |
264 | |
265 ret = stat( path, &fileinfo ); | |
266 if( ret < 0 ) { | |
267 /* If we can't stat the file, give up */ | |
268 fprintf( stderr, "libdvdread: Can't stat %s\n", path ); | |
269 perror(""); | |
270 return 0; | |
271 } | |
272 | |
273 /* Try to open libdvdcss or fall back to standard functions */ | |
274 have_css = DVDInputSetup(); | |
275 | |
276 /* First check if this is a block/char device or a file*/ | |
277 if( S_ISBLK( fileinfo.st_mode ) || | |
278 S_ISCHR( fileinfo.st_mode ) || | |
279 S_ISREG( fileinfo.st_mode ) ) { | |
280 | |
281 /** | |
282 * Block devices and regular files are assumed to be DVD-Video images. | |
283 */ | |
284 #if defined(__sun) | |
285 return DVDOpenImageFile( sun_block2char( path ), have_css ); | |
286 #elif defined(SYS_BSD) | |
287 return DVDOpenImageFile( bsd_block2char( path ), have_css ); | |
288 #else | |
289 return DVDOpenImageFile( path, have_css ); | |
290 #endif | |
291 | |
292 } else if( S_ISDIR( fileinfo.st_mode ) ) { | |
293 dvd_reader_t *auth_drive = 0; | |
294 char *path_copy; | |
295 #if defined(SYS_BSD) | |
296 struct fstab* fe; | |
297 #elif defined(__sun) || defined(__linux__) | |
298 FILE *mntfile; | |
299 #endif | |
300 | |
301 /* XXX: We should scream real loud here. */ | |
302 if( !(path_copy = strdup( path ) ) ) return 0; | |
303 | |
304 /* Resolve any symlinks and get the absolut dir name. */ | |
305 { | |
306 char *new_path; | |
307 int cdir = open( ".", O_RDONLY ); | |
308 | |
309 if( cdir >= 0 ) { | |
310 chdir( path_copy ); | |
311 new_path = getcwd( NULL, PATH_MAX ); | |
312 fchdir( cdir ); | |
313 close( cdir ); | |
314 if( new_path ) { | |
315 free( path_copy ); | |
316 path_copy = new_path; | |
317 } | |
318 } | |
319 } | |
320 | |
321 /** | |
322 * If we're being asked to open a directory, check if that directory | |
323 * is the mountpoint for a DVD-ROM which we can use instead. | |
324 */ | |
325 | |
326 if( strlen( path_copy ) > 1 ) { | |
327 if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) | |
328 path_copy[ strlen( path_copy ) - 1 ] = '\0'; | |
329 } | |
330 | |
331 if( strlen( path_copy ) > 9 ) { | |
332 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]), | |
333 "/video_ts" ) ) { | |
334 path_copy[ strlen( path_copy ) - 9 ] = '\0'; | |
335 } | |
336 } | |
337 | |
338 #if defined(SYS_BSD) | |
339 if( ( fe = getfsfile( path_copy ) ) ) { | |
340 dev_name = bsd_block2char( fe->fs_spec ); | |
341 fprintf( stderr, | |
342 "libdvdread: Attempting to use device %s" | |
343 " mounted on %s for CSS authentication\n", | |
344 dev_name, | |
345 fe->fs_file ); | |
346 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
347 } | |
348 #elif defined(__sun) | |
349 mntfile = fopen( MNTTAB, "r" ); | |
350 if( mntfile ) { | |
351 struct mnttab mp; | |
352 int res; | |
353 | |
354 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) { | |
355 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) { | |
356 dev_name = sun_block2char( mp.mnt_special ); | |
357 fprintf( stderr, | |
358 "libdvdread: Attempting to use device %s" | |
359 " mounted on %s for CSS authentication\n", | |
360 dev_name, | |
361 mp.mnt_mountp ); | |
362 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
363 break; | |
364 } | |
365 } | |
366 fclose( mntfile ); | |
367 } | |
368 #elif defined(__linux__) | |
369 mntfile = fopen( MOUNTED, "r" ); | |
370 if( mntfile ) { | |
371 struct mntent *me; | |
372 | |
373 while( ( me = getmntent( mntfile ) ) ) { | |
374 if( !strcmp( me->mnt_dir, path_copy ) ) { | |
375 fprintf( stderr, | |
376 "libdvdread: Attempting to use device %s" | |
377 " mounted on %s for CSS authentication\n", | |
378 me->mnt_fsname, | |
379 me->mnt_dir ); | |
380 auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css ); | |
381 dev_name = strdup(me->mnt_fsname); | |
382 break; | |
383 } | |
384 } | |
385 fclose( mntfile ); | |
386 } | |
7033 | 387 #elif defined(WIN32) |
388 dev_name = strdup(path); | |
389 auth_drive = DVDOpenImageFile( path, have_css ); | |
7029 | 390 #endif |
391 if( !dev_name ) { | |
392 fprintf( stderr, "libdvdread: Couldn't find device name.\n" ); | |
393 } else if( !auth_drive ) { | |
394 fprintf( stderr, "libdvdread: Device %s inaccessible, " | |
395 "CSS authentication not available.\n", dev_name ); | |
396 } | |
397 | |
398 free( dev_name ); | |
399 free( path_copy ); | |
400 | |
401 /** | |
402 * If we've opened a drive, just use that. | |
403 */ | |
404 if( auth_drive ) return auth_drive; | |
405 | |
406 /** | |
407 * Otherwise, we now try to open the directory tree instead. | |
408 */ | |
409 return DVDOpenPath( path ); | |
410 } | |
411 | |
412 /* If it's none of the above, screw it. */ | |
413 fprintf( stderr, "libdvdread: Could not open %s\n", path ); | |
414 return 0; | |
415 } | |
416 | |
417 void DVDClose( dvd_reader_t *dvd ) | |
418 { | |
419 if( dvd ) { | |
420 if( dvd->dev ) DVDinput_close( dvd->dev ); | |
421 if( dvd->path_root ) free( dvd->path_root ); | |
422 free( dvd ); | |
423 dvd = 0; | |
424 } | |
425 } | |
426 | |
427 /** | |
428 * Open an unencrypted file on a DVD image file. | |
429 */ | |
430 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename ) | |
431 { | |
432 uint32_t start, len; | |
433 dvd_file_t *dvd_file; | |
434 | |
435 start = UDFFindFile( dvd, filename, &len ); | |
436 if( !start ) return 0; | |
437 | |
438 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
439 if( !dvd_file ) return 0; | |
440 dvd_file->dvd = dvd; | |
441 dvd_file->lb_start = start; | |
442 dvd_file->seek_pos = 0; | |
443 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
444 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
445 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
446 | |
447 return dvd_file; | |
448 } | |
449 | |
450 /** | |
451 * Searches for <file> in directory <path>, ignoring case. | |
452 * Returns 0 and full filename in <filename>. | |
453 * or -1 on file not found. | |
454 * or -2 on path not found. | |
455 */ | |
456 static int findDirFile( const char *path, const char *file, char *filename ) | |
457 { | |
458 DIR *dir; | |
459 struct dirent *ent; | |
460 | |
461 dir = opendir( path ); | |
462 if( !dir ) return -2; | |
463 | |
464 while( ( ent = readdir( dir ) ) != NULL ) { | |
465 if( !strcasecmp( ent->d_name, file ) ) { | |
466 sprintf( filename, "%s%s%s", path, | |
467 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ), | |
468 ent->d_name ); | |
469 return 0; | |
470 } | |
471 } | |
472 | |
473 return -1; | |
474 } | |
475 | |
476 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename ) | |
477 { | |
478 char video_path[ PATH_MAX + 1 ]; | |
479 const char *nodirfile; | |
480 int ret; | |
481 | |
482 /* Strip off the directory for our search */ | |
483 if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) { | |
484 nodirfile = &(file[ 10 ]); | |
485 } else { | |
486 nodirfile = file; | |
487 } | |
488 | |
489 ret = findDirFile( dvd->path_root, nodirfile, filename ); | |
490 if( ret < 0 ) { | |
491 /* Try also with adding the path, just in case. */ | |
492 sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root ); | |
493 ret = findDirFile( video_path, nodirfile, filename ); | |
494 if( ret < 0 ) { | |
495 /* Try with the path, but in lower case. */ | |
496 sprintf( video_path, "%s/video_ts/", dvd->path_root ); | |
497 ret = findDirFile( video_path, nodirfile, filename ); | |
498 if( ret < 0 ) { | |
499 return 0; | |
500 } | |
501 } | |
502 } | |
503 | |
504 return 1; | |
505 } | |
506 | |
507 /** | |
508 * Open an unencrypted file from a DVD directory tree. | |
509 */ | |
510 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename ) | |
511 { | |
512 char full_path[ PATH_MAX + 1 ]; | |
513 dvd_file_t *dvd_file; | |
514 struct stat fileinfo; | |
515 dvd_input_t dev; | |
516 | |
517 /* Get the full path of the file. */ | |
518 if( !findDVDFile( dvd, filename, full_path ) ) return 0; | |
519 | |
520 dev = DVDinput_open( full_path ); | |
521 if( !dev ) return 0; | |
522 | |
523 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
524 if( !dvd_file ) return 0; | |
525 dvd_file->dvd = dvd; | |
526 dvd_file->lb_start = 0; | |
527 dvd_file->seek_pos = 0; | |
528 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
529 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
530 dvd_file->filesize = 0; | |
531 | |
532 if( stat( full_path, &fileinfo ) < 0 ) { | |
533 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
534 free( dvd_file ); | |
535 return 0; | |
536 } | |
537 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
538 dvd_file->title_devs[ 0 ] = dev; | |
539 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
540 | |
541 return dvd_file; | |
542 } | |
543 | |
544 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu ) | |
545 { | |
546 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
547 uint32_t start, len; | |
548 dvd_file_t *dvd_file; | |
549 | |
550 if( title == 0 ) { | |
551 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
552 } else { | |
553 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 ); | |
554 } | |
555 start = UDFFindFile( dvd, filename, &len ); | |
556 if( start == 0 ) return 0; | |
557 | |
558 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
559 if( !dvd_file ) return 0; | |
560 dvd_file->dvd = dvd; | |
561 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
562 dvd_file->lb_start = start; | |
563 dvd_file->seek_pos = 0; | |
564 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
565 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
566 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
567 | |
568 /* Calculate the complete file size for every file in the VOBS */ | |
569 if( !menu ) { | |
570 int cur; | |
571 | |
572 for( cur = 2; cur < 10; cur++ ) { | |
573 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur ); | |
574 if( !UDFFindFile( dvd, filename, &len ) ) break; | |
575 dvd_file->filesize += len / DVD_VIDEO_LB_LEN; | |
576 } | |
577 } | |
578 | |
579 if( dvd->css_state == 1 /* Need key init */ ) { | |
7033 | 580 // initAllCSSKeys( dvd ); |
581 // dvd->css_state = 2; | |
7029 | 582 } |
583 /* | |
584 if( DVDinput_seek( dvd_file->dvd->dev, | |
585 (int)start, DVDINPUT_SEEK_KEY ) < 0 ) { | |
586 fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n", | |
587 filename ); | |
588 } | |
589 */ | |
590 | |
591 return dvd_file; | |
592 } | |
593 | |
594 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) | |
595 { | |
596 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
597 char full_path[ PATH_MAX + 1 ]; | |
598 struct stat fileinfo; | |
599 dvd_file_t *dvd_file; | |
600 int i; | |
601 | |
602 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
603 if( !dvd_file ) return 0; | |
604 dvd_file->dvd = dvd; | |
605 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
606 dvd_file->lb_start = 0; | |
607 dvd_file->seek_pos = 0; | |
608 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
609 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
610 dvd_file->filesize = 0; | |
611 | |
612 if( menu ) { | |
613 dvd_input_t dev; | |
614 | |
615 if( title == 0 ) { | |
616 sprintf( filename, "VIDEO_TS.VOB" ); | |
617 } else { | |
618 sprintf( filename, "VTS_%02i_0.VOB", title ); | |
619 } | |
620 if( !findDVDFile( dvd, filename, full_path ) ) { | |
621 free( dvd_file ); | |
622 return 0; | |
623 } | |
624 | |
625 dev = DVDinput_open( full_path ); | |
626 if( dev == NULL ) { | |
627 free( dvd_file ); | |
628 return 0; | |
629 } | |
630 | |
631 if( stat( full_path, &fileinfo ) < 0 ) { | |
632 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
633 free( dvd_file ); | |
634 return 0; | |
635 } | |
636 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
637 dvd_file->title_devs[ 0 ] = dev; | |
638 DVDinput_seek( dvd_file->title_devs[0], 0, DVDINPUT_SEEK_KEY ); | |
639 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
640 | |
641 } else { | |
642 for( i = 0; i < 9; ++i ) { | |
643 | |
644 sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 ); | |
645 if( !findDVDFile( dvd, filename, full_path ) ) { | |
646 break; | |
647 } | |
648 | |
649 if( stat( full_path, &fileinfo ) < 0 ) { | |
650 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
651 break; | |
652 } | |
653 | |
654 dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
655 dvd_file->title_devs[ i ] = DVDinput_open( full_path ); | |
656 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
|
657 DVDinput_seek( dvd_file->title_devs[ i ], 0, DVDINPUT_SEEK_KEY ); |
7029 | 658 } |
8881
1e40d4a2466f
Function DVDOpenVOBPath only decrypts first VOB file and since each VOB file has
arpi
parents:
7423
diff
changeset
|
659 if( !dvd_file->title_devs[ 0 ] ) { |
7029 | 660 free( dvd_file ); |
661 return 0; | |
662 } | |
663 } | |
664 | |
665 return dvd_file; | |
666 } | |
667 | |
668 dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, | |
669 dvd_read_domain_t domain ) | |
670 { | |
671 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
672 | |
673 switch( domain ) { | |
674 case DVD_READ_INFO_FILE: | |
675 if( titlenum == 0 ) { | |
676 sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" ); | |
677 } else { | |
678 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum ); | |
679 } | |
680 break; | |
681 case DVD_READ_INFO_BACKUP_FILE: | |
682 if( titlenum == 0 ) { | |
683 sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" ); | |
684 } else { | |
685 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum ); | |
686 } | |
687 break; | |
688 case DVD_READ_MENU_VOBS: | |
689 if( dvd->isImageFile ) { | |
690 return DVDOpenVOBUDF( dvd, titlenum, 1 ); | |
691 } else { | |
692 return DVDOpenVOBPath( dvd, titlenum, 1 ); | |
693 } | |
694 break; | |
695 case DVD_READ_TITLE_VOBS: | |
696 if( titlenum == 0 ) return 0; | |
697 if( dvd->isImageFile ) { | |
698 return DVDOpenVOBUDF( dvd, titlenum, 0 ); | |
699 } else { | |
700 return DVDOpenVOBPath( dvd, titlenum, 0 ); | |
701 } | |
702 break; | |
703 default: | |
704 fprintf( stderr, "libdvdread: Invalid domain for file open.\n" ); | |
705 return 0; | |
706 } | |
707 | |
708 if( dvd->isImageFile ) { | |
709 return DVDOpenFileUDF( dvd, filename ); | |
710 } else { | |
711 return DVDOpenFilePath( dvd, filename ); | |
712 } | |
713 } | |
714 | |
715 void DVDCloseFile( dvd_file_t *dvd_file ) | |
716 { | |
717 int i; | |
718 | |
719 if( dvd_file ) { | |
720 if( dvd_file->dvd->isImageFile ) { | |
721 ; | |
722 } else { | |
723 for( i = 0; i < 9; ++i ) { | |
724 if( dvd_file->title_devs[ i ] ) { | |
725 DVDinput_close( dvd_file->title_devs[i] ); | |
726 } | |
727 } | |
728 } | |
729 | |
730 free( dvd_file ); | |
731 dvd_file = 0; | |
732 } | |
733 } | |
734 | |
735 /* Internal, but used from dvd_udf.c */ | |
736 int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, | |
737 size_t block_count, unsigned char *data, | |
738 int encrypted ) | |
739 { | |
740 int ret; | |
741 | |
742 if( !device->dev ) { | |
743 fprintf( stderr, "libdvdread: Fatal error in block read.\n" ); | |
744 return 0; | |
745 } | |
746 | |
747 ret = DVDinput_seek( device->dev, (int) lb_number, DVDINPUT_NOFLAGS ); | |
748 if( ret != (int) lb_number ) { | |
749 fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number ); | |
750 return 0; | |
751 } | |
752 | |
753 return DVDinput_read( device->dev, (char *) data, | |
754 (int) block_count, encrypted ); | |
755 } | |
756 | |
757 /* This is using a single input and starting from 'dvd_file->lb_start' offset. | |
758 * | |
759 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
760 * into the buffer located at 'data' and if 'encrypted' is set | |
761 * descramble the data if it's encrypted. Returning either an | |
762 * negative error or the number of blocks read. */ | |
763 static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset, | |
764 size_t block_count, unsigned char *data, | |
765 int encrypted ) | |
766 { | |
767 return DVDReadBlocksUDFRaw( dvd_file->dvd, dvd_file->lb_start + offset, | |
768 block_count, data, encrypted ); | |
769 } | |
770 | |
771 /* This is using possibly several inputs and starting from an offset of '0'. | |
772 * | |
773 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
774 * into the buffer located at 'data' and if 'encrypted' is set | |
775 * descramble the data if it's encrypted. Returning either an | |
776 * negative error or the number of blocks read. */ | |
777 static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, | |
778 size_t block_count, unsigned char *data, | |
779 int encrypted ) | |
780 { | |
781 int i; | |
782 int ret, ret2, off; | |
783 | |
784 ret = 0; | |
785 ret2 = 0; | |
786 for( i = 0; i < 9; ++i ) { | |
787 if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */ | |
788 | |
789 if( offset < dvd_file->title_sizes[ i ] ) { | |
790 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) { | |
791 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
792 (int)offset, DVDINPUT_NOFLAGS ); | |
793 if( off < 0 || off != (int)offset ) { | |
794 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
795 offset ); | |
796 return off < 0 ? off : 0; | |
797 } | |
798 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
799 (int)block_count, encrypted ); | |
800 break; | |
801 } else { | |
802 size_t part1_size = dvd_file->title_sizes[ i ] - offset; | |
803 /* FIXME: Really needs to be a while loop. | |
804 * (This is only true if you try and read >1GB at a time) */ | |
805 | |
806 /* Read part 1 */ | |
807 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
808 (int)offset, DVDINPUT_NOFLAGS ); | |
809 if( off < 0 || off != (int)offset ) { | |
810 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
811 offset ); | |
812 return off < 0 ? off : 0; | |
813 } | |
814 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
815 (int)part1_size, encrypted ); | |
816 if( ret < 0 ) return ret; | |
817 /* FIXME: This is wrong if i is the last file in the set. | |
818 * 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
|
819 |
bb40478265df
I experienced several segfaults when trying to play (unencrypted) DVDs
arpi
parents:
7033
diff
changeset
|
820 /* 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
|
821 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
|
822 |
7029 | 823 /* Read part 2 */ |
824 off = DVDinput_seek( dvd_file->title_devs[ i + 1 ], | |
825 0, DVDINPUT_NOFLAGS ); | |
826 if( off < 0 || off != 0 ) { | |
827 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
828 0 ); | |
829 return off < 0 ? off : 0; | |
830 } | |
831 ret2 = DVDinput_read( dvd_file->title_devs[ i + 1 ], | |
832 data + ( part1_size | |
833 * (int64_t)DVD_VIDEO_LB_LEN ), | |
834 (int)(block_count - part1_size), | |
835 encrypted ); | |
836 if( ret2 < 0 ) return ret2; | |
837 break; | |
838 } | |
839 } else { | |
840 offset -= dvd_file->title_sizes[ i ]; | |
841 } | |
842 } | |
843 | |
844 return ret + ret2; | |
845 } | |
846 | |
847 /* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */ | |
848 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, | |
849 size_t block_count, unsigned char *data ) | |
850 { | |
851 int ret; | |
852 | |
853 /* Hack, and it will still fail for multiple opens in a threaded app ! */ | |
854 if( dvd_file->dvd->css_title != dvd_file->css_title ) { | |
855 dvd_file->dvd->css_title = dvd_file->css_title; | |
856 if( dvd_file->dvd->isImageFile ) { | |
857 DVDinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); | |
858 } else { | |
859 DVDinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); | |
860 } | |
861 } | |
862 | |
863 if( dvd_file->dvd->isImageFile ) { | |
864 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset, | |
865 block_count, data, DVDINPUT_READ_DECRYPT ); | |
866 } else { | |
867 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset, | |
868 block_count, data, DVDINPUT_READ_DECRYPT ); | |
869 } | |
870 | |
871 return (ssize_t)ret; | |
872 } | |
873 | |
874 int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset ) | |
875 { | |
876 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { | |
877 return -1; | |
878 } | |
879 dvd_file->seek_pos = (uint32_t) offset; | |
880 return offset; | |
881 } | |
882 | |
883 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) | |
884 { | |
885 unsigned char *secbuf; | |
886 unsigned int numsec, seek_sector, seek_byte; | |
887 int ret; | |
888 | |
889 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN; | |
890 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN; | |
891 | |
892 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + 1; | |
893 secbuf = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN ); | |
894 if( !secbuf ) { | |
895 fprintf( stderr, "libdvdread: Can't allocate memory " | |
896 "for file read!\n" ); | |
897 return 0; | |
898 } | |
899 | |
900 if( dvd_file->dvd->isImageFile ) { | |
901 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector, | |
902 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
903 } else { | |
904 ret = DVDReadBlocksPath( dvd_file, seek_sector, | |
905 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
906 } | |
907 | |
908 if( ret != (int) numsec ) { | |
909 free( secbuf ); | |
910 return ret < 0 ? ret : 0; | |
911 } | |
912 | |
913 memcpy( data, &(secbuf[ seek_byte ]), byte_size ); | |
914 free( secbuf ); | |
915 | |
916 dvd_file->seek_pos += byte_size; | |
917 return byte_size; | |
918 } | |
919 | |
920 ssize_t DVDFileSize( dvd_file_t *dvd_file ) | |
921 { | |
922 return dvd_file->filesize; | |
923 } |