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