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