Mercurial > mplayer.hg
annotate libmpdvdkit2/dvd_reader.c @ 8179:63a5e03f4346
Removed hard coded value for the length of the header separator.
Used the value previous detected.
author | bertrand |
---|---|
date | Wed, 13 Nov 2002 09:02:55 +0000 |
parents | ad967766679a |
children | 1e40d4a2466f |
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 ]; | |
657 } | |
658 if( dvd_file->title_devs[ 0 ] ) { | |
659 DVDinput_seek( dvd_file->title_devs[ 0 ], 0, DVDINPUT_SEEK_KEY ); | |
660 } else { | |
661 free( dvd_file ); | |
662 return 0; | |
663 } | |
664 } | |
665 | |
666 return dvd_file; | |
667 } | |
668 | |
669 dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, | |
670 dvd_read_domain_t domain ) | |
671 { | |
672 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
673 | |
674 switch( domain ) { | |
675 case DVD_READ_INFO_FILE: | |
676 if( titlenum == 0 ) { | |
677 sprintf( filename, "/VIDEO_TS/VIDEO_TS.IFO" ); | |
678 } else { | |
679 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.IFO", titlenum ); | |
680 } | |
681 break; | |
682 case DVD_READ_INFO_BACKUP_FILE: | |
683 if( titlenum == 0 ) { | |
684 sprintf( filename, "/VIDEO_TS/VIDEO_TS.BUP" ); | |
685 } else { | |
686 sprintf( filename, "/VIDEO_TS/VTS_%02i_0.BUP", titlenum ); | |
687 } | |
688 break; | |
689 case DVD_READ_MENU_VOBS: | |
690 if( dvd->isImageFile ) { | |
691 return DVDOpenVOBUDF( dvd, titlenum, 1 ); | |
692 } else { | |
693 return DVDOpenVOBPath( dvd, titlenum, 1 ); | |
694 } | |
695 break; | |
696 case DVD_READ_TITLE_VOBS: | |
697 if( titlenum == 0 ) return 0; | |
698 if( dvd->isImageFile ) { | |
699 return DVDOpenVOBUDF( dvd, titlenum, 0 ); | |
700 } else { | |
701 return DVDOpenVOBPath( dvd, titlenum, 0 ); | |
702 } | |
703 break; | |
704 default: | |
705 fprintf( stderr, "libdvdread: Invalid domain for file open.\n" ); | |
706 return 0; | |
707 } | |
708 | |
709 if( dvd->isImageFile ) { | |
710 return DVDOpenFileUDF( dvd, filename ); | |
711 } else { | |
712 return DVDOpenFilePath( dvd, filename ); | |
713 } | |
714 } | |
715 | |
716 void DVDCloseFile( dvd_file_t *dvd_file ) | |
717 { | |
718 int i; | |
719 | |
720 if( dvd_file ) { | |
721 if( dvd_file->dvd->isImageFile ) { | |
722 ; | |
723 } else { | |
724 for( i = 0; i < 9; ++i ) { | |
725 if( dvd_file->title_devs[ i ] ) { | |
726 DVDinput_close( dvd_file->title_devs[i] ); | |
727 } | |
728 } | |
729 } | |
730 | |
731 free( dvd_file ); | |
732 dvd_file = 0; | |
733 } | |
734 } | |
735 | |
736 /* Internal, but used from dvd_udf.c */ | |
737 int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, | |
738 size_t block_count, unsigned char *data, | |
739 int encrypted ) | |
740 { | |
741 int ret; | |
742 | |
743 if( !device->dev ) { | |
744 fprintf( stderr, "libdvdread: Fatal error in block read.\n" ); | |
745 return 0; | |
746 } | |
747 | |
748 ret = DVDinput_seek( device->dev, (int) lb_number, DVDINPUT_NOFLAGS ); | |
749 if( ret != (int) lb_number ) { | |
750 fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number ); | |
751 return 0; | |
752 } | |
753 | |
754 return DVDinput_read( device->dev, (char *) data, | |
755 (int) block_count, encrypted ); | |
756 } | |
757 | |
758 /* This is using a single input and starting from 'dvd_file->lb_start' offset. | |
759 * | |
760 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
761 * into the buffer located at 'data' and if 'encrypted' is set | |
762 * descramble the data if it's encrypted. Returning either an | |
763 * negative error or the number of blocks read. */ | |
764 static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset, | |
765 size_t block_count, unsigned char *data, | |
766 int encrypted ) | |
767 { | |
768 return DVDReadBlocksUDFRaw( dvd_file->dvd, dvd_file->lb_start + offset, | |
769 block_count, data, encrypted ); | |
770 } | |
771 | |
772 /* This is using possibly several inputs and starting from an offset of '0'. | |
773 * | |
774 * Reads 'block_count' blocks from 'dvd_file' at block offset 'offset' | |
775 * into the buffer located at 'data' and if 'encrypted' is set | |
776 * descramble the data if it's encrypted. Returning either an | |
777 * negative error or the number of blocks read. */ | |
778 static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, | |
779 size_t block_count, unsigned char *data, | |
780 int encrypted ) | |
781 { | |
782 int i; | |
783 int ret, ret2, off; | |
784 | |
785 ret = 0; | |
786 ret2 = 0; | |
787 for( i = 0; i < 9; ++i ) { | |
788 if( !dvd_file->title_sizes[ i ] ) return 0; /* Past end of file */ | |
789 | |
790 if( offset < dvd_file->title_sizes[ i ] ) { | |
791 if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) { | |
792 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
793 (int)offset, DVDINPUT_NOFLAGS ); | |
794 if( off < 0 || off != (int)offset ) { | |
795 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
796 offset ); | |
797 return off < 0 ? off : 0; | |
798 } | |
799 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
800 (int)block_count, encrypted ); | |
801 break; | |
802 } else { | |
803 size_t part1_size = dvd_file->title_sizes[ i ] - offset; | |
804 /* FIXME: Really needs to be a while loop. | |
805 * (This is only true if you try and read >1GB at a time) */ | |
806 | |
807 /* Read part 1 */ | |
808 off = DVDinput_seek( dvd_file->title_devs[ i ], | |
809 (int)offset, DVDINPUT_NOFLAGS ); | |
810 if( off < 0 || off != (int)offset ) { | |
811 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
812 offset ); | |
813 return off < 0 ? off : 0; | |
814 } | |
815 ret = DVDinput_read( dvd_file->title_devs[ i ], data, | |
816 (int)part1_size, encrypted ); | |
817 if( ret < 0 ) return ret; | |
818 /* FIXME: This is wrong if i is the last file in the set. | |
819 * 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
|
820 |
bb40478265df
I experienced several segfaults when trying to play (unencrypted) DVDs
arpi
parents:
7033
diff
changeset
|
821 /* 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
|
822 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
|
823 |
7029 | 824 /* Read part 2 */ |
825 off = DVDinput_seek( dvd_file->title_devs[ i + 1 ], | |
826 0, DVDINPUT_NOFLAGS ); | |
827 if( off < 0 || off != 0 ) { | |
828 fprintf( stderr, "libdvdread: Can't seek to block %d\n", | |
829 0 ); | |
830 return off < 0 ? off : 0; | |
831 } | |
832 ret2 = DVDinput_read( dvd_file->title_devs[ i + 1 ], | |
833 data + ( part1_size | |
834 * (int64_t)DVD_VIDEO_LB_LEN ), | |
835 (int)(block_count - part1_size), | |
836 encrypted ); | |
837 if( ret2 < 0 ) return ret2; | |
838 break; | |
839 } | |
840 } else { | |
841 offset -= dvd_file->title_sizes[ i ]; | |
842 } | |
843 } | |
844 | |
845 return ret + ret2; | |
846 } | |
847 | |
848 /* This is broken reading more than 2Gb at a time is ssize_t is 32-bit. */ | |
849 ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, | |
850 size_t block_count, unsigned char *data ) | |
851 { | |
852 int ret; | |
853 | |
854 /* Hack, and it will still fail for multiple opens in a threaded app ! */ | |
855 if( dvd_file->dvd->css_title != dvd_file->css_title ) { | |
856 dvd_file->dvd->css_title = dvd_file->css_title; | |
857 if( dvd_file->dvd->isImageFile ) { | |
858 DVDinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); | |
859 } else { | |
860 DVDinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); | |
861 } | |
862 } | |
863 | |
864 if( dvd_file->dvd->isImageFile ) { | |
865 ret = DVDReadBlocksUDF( dvd_file, (uint32_t)offset, | |
866 block_count, data, DVDINPUT_READ_DECRYPT ); | |
867 } else { | |
868 ret = DVDReadBlocksPath( dvd_file, (unsigned int)offset, | |
869 block_count, data, DVDINPUT_READ_DECRYPT ); | |
870 } | |
871 | |
872 return (ssize_t)ret; | |
873 } | |
874 | |
875 int32_t DVDFileSeek( dvd_file_t *dvd_file, int32_t offset ) | |
876 { | |
877 if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { | |
878 return -1; | |
879 } | |
880 dvd_file->seek_pos = (uint32_t) offset; | |
881 return offset; | |
882 } | |
883 | |
884 ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) | |
885 { | |
886 unsigned char *secbuf; | |
887 unsigned int numsec, seek_sector, seek_byte; | |
888 int ret; | |
889 | |
890 seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN; | |
891 seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN; | |
892 | |
893 numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + 1; | |
894 secbuf = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN ); | |
895 if( !secbuf ) { | |
896 fprintf( stderr, "libdvdread: Can't allocate memory " | |
897 "for file read!\n" ); | |
898 return 0; | |
899 } | |
900 | |
901 if( dvd_file->dvd->isImageFile ) { | |
902 ret = DVDReadBlocksUDF( dvd_file, (uint32_t) seek_sector, | |
903 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
904 } else { | |
905 ret = DVDReadBlocksPath( dvd_file, seek_sector, | |
906 (size_t) numsec, secbuf, DVDINPUT_NOFLAGS ); | |
907 } | |
908 | |
909 if( ret != (int) numsec ) { | |
910 free( secbuf ); | |
911 return ret < 0 ? ret : 0; | |
912 } | |
913 | |
914 memcpy( data, &(secbuf[ seek_byte ]), byte_size ); | |
915 free( secbuf ); | |
916 | |
917 dvd_file->seek_pos += byte_size; | |
918 return byte_size; | |
919 } | |
920 | |
921 ssize_t DVDFileSize( dvd_file_t *dvd_file ) | |
922 { | |
923 return dvd_file->filesize; | |
924 } |