Mercurial > mplayer.hg
annotate libmpdvdkit2/dvd_reader.c @ 9778:da63c272425e
AAC section added.
author | diego |
---|---|
date | Tue, 01 Apr 2003 00:33:16 +0000 |
parents | f0f0f176d298 |
children | 535a53c058f8 |
rev | line source |
---|---|
7029 | 1 /* |
2 * Copyright (C) 2001, 2002 Billy Biggs <vektor@dumbterm.net>, | |
3 * Håkan Hjort <d95hjort@dtek.chalmers.se> | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or (at | |
8 * your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
18 */ | |
19 | |
7033 | 20 #include "config.h" |
21 | |
7029 | 22 #include <sys/types.h> |
23 #include <sys/stat.h> | |
24 #include <sys/time.h> /* For the timing of dvdcss_title crack. */ | |
25 #include <fcntl.h> | |
26 #include <stdlib.h> | |
27 #include <stdio.h> | |
28 #include <errno.h> | |
29 #include <string.h> | |
30 #include <unistd.h> | |
31 #include <limits.h> | |
32 #include <dirent.h> | |
33 | |
34 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)|| defined(__DARWIN__) | |
35 #define SYS_BSD 1 | |
36 #endif | |
37 | |
38 #if defined(__sun) | |
39 #include <sys/mnttab.h> | |
7423
ad967766679a
hpux DVD support fixes by Martin Gansser <mgansser@ngi.de>
arpi
parents:
7358
diff
changeset
|
40 #elif defined(hpux) |
ad967766679a
hpux DVD support fixes by Martin Gansser <mgansser@ngi.de>
arpi
parents:
7358
diff
changeset
|
41 #include </usr/conf/h/mnttab.h> |
7029 | 42 #elif defined(SYS_BSD) |
43 #include <fstab.h> | |
44 #elif defined(__linux__) | |
45 #include <mntent.h> | |
46 #endif | |
47 | |
48 #include "dvd_udf.h" | |
49 #include "dvd_input.h" | |
50 #include "dvd_reader.h" | |
51 | |
52 struct dvd_reader_s { | |
53 /* Basic information. */ | |
54 int isImageFile; | |
55 | |
56 /* Hack for keeping track of the css status. | |
57 * 0: no css, 1: perhaps (need init of keys), 2: have done init */ | |
58 int css_state; | |
59 int css_title; /* Last title that we have called DVDinpute_title for. */ | |
60 | |
61 /* Information required for an image file. */ | |
62 dvd_input_t dev; | |
63 | |
64 /* Information required for a directory path drive. */ | |
65 char *path_root; | |
66 }; | |
67 | |
68 struct dvd_file_s { | |
69 /* Basic information. */ | |
70 dvd_reader_t *dvd; | |
71 | |
72 /* Hack for selecting the right css title. */ | |
73 int css_title; | |
74 | |
75 /* Information required for an image file. */ | |
76 uint32_t lb_start; | |
77 uint32_t seek_pos; | |
78 | |
79 /* Information required for a directory path drive. */ | |
80 size_t title_sizes[ 9 ]; | |
81 dvd_input_t title_devs[ 9 ]; | |
82 | |
83 /* Calculated at open-time, size in blocks. */ | |
84 ssize_t filesize; | |
85 }; | |
86 | |
87 /* Loop over all titles and call dvdcss_title to crack the keys. */ | |
88 static int initAllCSSKeys( dvd_reader_t *dvd ) | |
89 { | |
90 struct timeval all_s, all_e; | |
91 struct timeval t_s, t_e; | |
92 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
93 uint32_t start, len; | |
94 int title; | |
95 | |
96 fprintf( stderr, "\n" ); | |
97 fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" ); | |
98 fprintf( stderr, "libdvdread: This can take a _long_ time, " | |
99 "please be patient\n\n" ); | |
100 | |
101 gettimeofday(&all_s, NULL); | |
102 | |
103 for( title = 0; title < 100; title++ ) { | |
104 gettimeofday( &t_s, NULL ); | |
105 if( title == 0 ) { | |
106 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
107 } else { | |
108 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 0 ); | |
109 } | |
110 start = UDFFindFile( dvd, filename, &len ); | |
111 if( start != 0 && len != 0 ) { | |
112 /* Perform CSS key cracking for this title. */ | |
113 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
114 filename, start ); | |
115 if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { | |
116 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start); | |
117 } | |
118 gettimeofday( &t_e, NULL ); | |
119 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
120 (long int) t_e.tv_sec - t_s.tv_sec ); | |
121 } | |
122 | |
123 if( title == 0 ) continue; | |
124 | |
125 gettimeofday( &t_s, NULL ); | |
126 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, 1 ); | |
127 start = UDFFindFile( dvd, filename, &len ); | |
128 if( start == 0 || len == 0 ) break; | |
129 | |
130 /* Perform CSS key cracking for this title. */ | |
131 fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", | |
132 filename, start ); | |
133 if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { | |
134 fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start); | |
135 } | |
136 gettimeofday( &t_e, NULL ); | |
137 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
138 (long int) t_e.tv_sec - t_s.tv_sec ); | |
139 } | |
140 title--; | |
141 | |
142 fprintf( stderr, "libdvdread: Found %d VTS's\n", title ); | |
143 gettimeofday(&all_e, NULL); | |
144 fprintf( stderr, "libdvdread: Elapsed time %ld\n", | |
145 (long int) all_e.tv_sec - all_s.tv_sec ); | |
146 | |
147 return 0; | |
148 } | |
149 | |
150 | |
7033 | 151 #ifndef HAVE_MPLAYER |
152 #include "get_path.c" | |
153 #else | |
154 extern char * get_path( char * filename ); | |
155 #endif | |
156 | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
8881
diff
changeset
|
157 //extern char * dvdcss_cache_dir; |
7029 | 158 |
159 /** | |
160 * Open a DVD image or block device file. | |
161 */ | |
162 static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) | |
163 { | |
164 dvd_reader_t *dvd; | |
165 dvd_input_t dev; | |
7033 | 166 |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
8881
diff
changeset
|
167 /* setup cache dir is no longer needed, it's now implemented in libdvdcss.c |
7033 | 168 if(!dvdcss_cache_dir){ |
169 dvdcss_cache_dir=get_path( "" ); | |
170 if ( dvdcss_cache_dir ) { mkdir( dvdcss_cache_dir,493 ); free( dvdcss_cache_dir ); } | |
171 dvdcss_cache_dir=get_path( "DVDKeys" ); | |
172 if(dvdcss_cache_dir) mkdir( dvdcss_cache_dir,493 ); | |
173 } | |
9333
f0f0f176d298
sync with libdvdcss 1.2.5 (including u8->uint8_t and whitespace cosmetics...)
arpi
parents:
8881
diff
changeset
|
174 */ |
7029 | 175 |
7033 | 176 /* open it */ |
7029 | 177 dev = DVDinput_open( location ); |
178 if( !dev ) { | |
179 fprintf( stderr, "libdvdread: Can't open %s for reading\n", location ); | |
180 return 0; | |
181 } | |
182 | |
183 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
184 if( !dvd ) return 0; | |
185 dvd->isImageFile = 1; | |
186 dvd->dev = dev; | |
187 dvd->path_root = 0; | |
188 | |
189 if( have_css ) { | |
190 /* Only if DVDCSS_METHOD = title, a bit if it's disc or if | |
191 * DVDCSS_METHOD = key but region missmatch. Unfortunaly we | |
192 * don't have that information. */ | |
193 | |
194 dvd->css_state = 1; /* Need key init. */ | |
195 } | |
196 | |
197 return dvd; | |
198 } | |
199 | |
200 static dvd_reader_t *DVDOpenPath( const char *path_root ) | |
201 { | |
202 dvd_reader_t *dvd; | |
203 | |
204 dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) ); | |
205 if( !dvd ) return 0; | |
206 dvd->isImageFile = 0; | |
207 dvd->dev = 0; | |
208 dvd->path_root = strdup( path_root ); | |
209 | |
210 return dvd; | |
211 } | |
212 | |
213 #if defined(__sun) | |
214 /* /dev/rdsk/c0t6d0s0 (link to /devices/...) | |
215 /vol/dev/rdsk/c0t6d0/?? | |
216 /vol/rdsk/<name> */ | |
217 static char *sun_block2char( const char *path ) | |
218 { | |
219 char *new_path; | |
220 | |
221 /* Must contain "/dsk/" */ | |
222 if( !strstr( path, "/dsk/" ) ) return (char *) strdup( path ); | |
223 | |
224 /* Replace "/dsk/" with "/rdsk/" */ | |
225 new_path = malloc( strlen(path) + 2 ); | |
226 strcpy( new_path, path ); | |
227 strcpy( strstr( new_path, "/dsk/" ), "" ); | |
228 strcat( new_path, "/rdsk/" ); | |
229 strcat( new_path, strstr( path, "/dsk/" ) + strlen( "/dsk/" ) ); | |
230 | |
231 return new_path; | |
232 } | |
233 #endif | |
234 | |
235 #if defined(SYS_BSD) | |
236 /* FreeBSD /dev/(r)(a)cd0c (a is for atapi), recomended to _not_ use r | |
237 OpenBSD /dev/rcd0c, it needs to be the raw device | |
238 NetBSD /dev/rcd0[d|c|..] d for x86, c (for non x86), perhaps others | |
239 Darwin /dev/rdisk0, it needs to be the raw device | |
240 BSD/OS /dev/sr0c (if not mounted) or /dev/rsr0c ('c' any letter will do) */ | |
241 static char *bsd_block2char( const char *path ) | |
242 { | |
243 char *new_path; | |
244 | |
245 /* If it doesn't start with "/dev/" or does start with "/dev/r" exit */ | |
246 if( !strncmp( path, "/dev/", 5 ) || strncmp( path, "/dev/r", 6 ) ) | |
247 return (char *) strdup( path ); | |
248 | |
249 /* Replace "/dev/" with "/dev/r" */ | |
250 new_path = malloc( strlen(path) + 2 ); | |
251 strcpy( new_path, "/dev/r" ); | |
252 strcat( new_path, path + strlen( "/dev/" ) ); | |
253 | |
254 return new_path; | |
255 } | |
256 #endif | |
257 | |
258 dvd_reader_t *DVDOpen( const char *path ) | |
259 { | |
260 struct stat fileinfo; | |
261 int ret, have_css; | |
262 char *dev_name = 0; | |
263 | |
264 if( !path ) return 0; | |
265 | |
266 ret = stat( path, &fileinfo ); | |
267 if( ret < 0 ) { | |
268 /* If we can't stat the file, give up */ | |
269 fprintf( stderr, "libdvdread: Can't stat %s\n", path ); | |
270 perror(""); | |
271 return 0; | |
272 } | |
273 | |
274 /* Try to open libdvdcss or fall back to standard functions */ | |
275 have_css = DVDInputSetup(); | |
276 | |
277 /* First check if this is a block/char device or a file*/ | |
278 if( S_ISBLK( fileinfo.st_mode ) || | |
279 S_ISCHR( fileinfo.st_mode ) || | |
280 S_ISREG( fileinfo.st_mode ) ) { | |
281 | |
282 /** | |
283 * Block devices and regular files are assumed to be DVD-Video images. | |
284 */ | |
285 #if defined(__sun) | |
286 return DVDOpenImageFile( sun_block2char( path ), have_css ); | |
287 #elif defined(SYS_BSD) | |
288 return DVDOpenImageFile( bsd_block2char( path ), have_css ); | |
289 #else | |
290 return DVDOpenImageFile( path, have_css ); | |
291 #endif | |
292 | |
293 } else if( S_ISDIR( fileinfo.st_mode ) ) { | |
294 dvd_reader_t *auth_drive = 0; | |
295 char *path_copy; | |
296 #if defined(SYS_BSD) | |
297 struct fstab* fe; | |
298 #elif defined(__sun) || defined(__linux__) | |
299 FILE *mntfile; | |
300 #endif | |
301 | |
302 /* XXX: We should scream real loud here. */ | |
303 if( !(path_copy = strdup( path ) ) ) return 0; | |
304 | |
305 /* Resolve any symlinks and get the absolut dir name. */ | |
306 { | |
307 char *new_path; | |
308 int cdir = open( ".", O_RDONLY ); | |
309 | |
310 if( cdir >= 0 ) { | |
311 chdir( path_copy ); | |
312 new_path = getcwd( NULL, PATH_MAX ); | |
313 fchdir( cdir ); | |
314 close( cdir ); | |
315 if( new_path ) { | |
316 free( path_copy ); | |
317 path_copy = new_path; | |
318 } | |
319 } | |
320 } | |
321 | |
322 /** | |
323 * If we're being asked to open a directory, check if that directory | |
324 * is the mountpoint for a DVD-ROM which we can use instead. | |
325 */ | |
326 | |
327 if( strlen( path_copy ) > 1 ) { | |
328 if( path_copy[ strlen( path_copy ) - 1 ] == '/' ) | |
329 path_copy[ strlen( path_copy ) - 1 ] = '\0'; | |
330 } | |
331 | |
332 if( strlen( path_copy ) > 9 ) { | |
333 if( !strcasecmp( &(path_copy[ strlen( path_copy ) - 9 ]), | |
334 "/video_ts" ) ) { | |
335 path_copy[ strlen( path_copy ) - 9 ] = '\0'; | |
336 } | |
337 } | |
338 | |
339 #if defined(SYS_BSD) | |
340 if( ( fe = getfsfile( path_copy ) ) ) { | |
341 dev_name = bsd_block2char( fe->fs_spec ); | |
342 fprintf( stderr, | |
343 "libdvdread: Attempting to use device %s" | |
344 " mounted on %s for CSS authentication\n", | |
345 dev_name, | |
346 fe->fs_file ); | |
347 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
348 } | |
349 #elif defined(__sun) | |
350 mntfile = fopen( MNTTAB, "r" ); | |
351 if( mntfile ) { | |
352 struct mnttab mp; | |
353 int res; | |
354 | |
355 while( ( res = getmntent( mntfile, &mp ) ) != -1 ) { | |
356 if( res == 0 && !strcmp( mp.mnt_mountp, path_copy ) ) { | |
357 dev_name = sun_block2char( mp.mnt_special ); | |
358 fprintf( stderr, | |
359 "libdvdread: Attempting to use device %s" | |
360 " mounted on %s for CSS authentication\n", | |
361 dev_name, | |
362 mp.mnt_mountp ); | |
363 auth_drive = DVDOpenImageFile( dev_name, have_css ); | |
364 break; | |
365 } | |
366 } | |
367 fclose( mntfile ); | |
368 } | |
369 #elif defined(__linux__) | |
370 mntfile = fopen( MOUNTED, "r" ); | |
371 if( mntfile ) { | |
372 struct mntent *me; | |
373 | |
374 while( ( me = getmntent( mntfile ) ) ) { | |
375 if( !strcmp( me->mnt_dir, path_copy ) ) { | |
376 fprintf( stderr, | |
377 "libdvdread: Attempting to use device %s" | |
378 " mounted on %s for CSS authentication\n", | |
379 me->mnt_fsname, | |
380 me->mnt_dir ); | |
381 auth_drive = DVDOpenImageFile( me->mnt_fsname, have_css ); | |
382 dev_name = strdup(me->mnt_fsname); | |
383 break; | |
384 } | |
385 } | |
386 fclose( mntfile ); | |
387 } | |
7033 | 388 #elif defined(WIN32) |
389 dev_name = strdup(path); | |
390 auth_drive = DVDOpenImageFile( path, have_css ); | |
7029 | 391 #endif |
392 if( !dev_name ) { | |
393 fprintf( stderr, "libdvdread: Couldn't find device name.\n" ); | |
394 } else if( !auth_drive ) { | |
395 fprintf( stderr, "libdvdread: Device %s inaccessible, " | |
396 "CSS authentication not available.\n", dev_name ); | |
397 } | |
398 | |
399 free( dev_name ); | |
400 free( path_copy ); | |
401 | |
402 /** | |
403 * If we've opened a drive, just use that. | |
404 */ | |
405 if( auth_drive ) return auth_drive; | |
406 | |
407 /** | |
408 * Otherwise, we now try to open the directory tree instead. | |
409 */ | |
410 return DVDOpenPath( path ); | |
411 } | |
412 | |
413 /* If it's none of the above, screw it. */ | |
414 fprintf( stderr, "libdvdread: Could not open %s\n", path ); | |
415 return 0; | |
416 } | |
417 | |
418 void DVDClose( dvd_reader_t *dvd ) | |
419 { | |
420 if( dvd ) { | |
421 if( dvd->dev ) DVDinput_close( dvd->dev ); | |
422 if( dvd->path_root ) free( dvd->path_root ); | |
423 free( dvd ); | |
424 dvd = 0; | |
425 } | |
426 } | |
427 | |
428 /** | |
429 * Open an unencrypted file on a DVD image file. | |
430 */ | |
431 static dvd_file_t *DVDOpenFileUDF( dvd_reader_t *dvd, char *filename ) | |
432 { | |
433 uint32_t start, len; | |
434 dvd_file_t *dvd_file; | |
435 | |
436 start = UDFFindFile( dvd, filename, &len ); | |
437 if( !start ) return 0; | |
438 | |
439 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
440 if( !dvd_file ) return 0; | |
441 dvd_file->dvd = dvd; | |
442 dvd_file->lb_start = start; | |
443 dvd_file->seek_pos = 0; | |
444 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
445 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
446 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
447 | |
448 return dvd_file; | |
449 } | |
450 | |
451 /** | |
452 * Searches for <file> in directory <path>, ignoring case. | |
453 * Returns 0 and full filename in <filename>. | |
454 * or -1 on file not found. | |
455 * or -2 on path not found. | |
456 */ | |
457 static int findDirFile( const char *path, const char *file, char *filename ) | |
458 { | |
459 DIR *dir; | |
460 struct dirent *ent; | |
461 | |
462 dir = opendir( path ); | |
463 if( !dir ) return -2; | |
464 | |
465 while( ( ent = readdir( dir ) ) != NULL ) { | |
466 if( !strcasecmp( ent->d_name, file ) ) { | |
467 sprintf( filename, "%s%s%s", path, | |
468 ( ( path[ strlen( path ) - 1 ] == '/' ) ? "" : "/" ), | |
469 ent->d_name ); | |
470 return 0; | |
471 } | |
472 } | |
473 | |
474 return -1; | |
475 } | |
476 | |
477 static int findDVDFile( dvd_reader_t *dvd, const char *file, char *filename ) | |
478 { | |
479 char video_path[ PATH_MAX + 1 ]; | |
480 const char *nodirfile; | |
481 int ret; | |
482 | |
483 /* Strip off the directory for our search */ | |
484 if( !strncasecmp( "/VIDEO_TS/", file, 10 ) ) { | |
485 nodirfile = &(file[ 10 ]); | |
486 } else { | |
487 nodirfile = file; | |
488 } | |
489 | |
490 ret = findDirFile( dvd->path_root, nodirfile, filename ); | |
491 if( ret < 0 ) { | |
492 /* Try also with adding the path, just in case. */ | |
493 sprintf( video_path, "%s/VIDEO_TS/", dvd->path_root ); | |
494 ret = findDirFile( video_path, nodirfile, filename ); | |
495 if( ret < 0 ) { | |
496 /* Try with the path, but in lower case. */ | |
497 sprintf( video_path, "%s/video_ts/", dvd->path_root ); | |
498 ret = findDirFile( video_path, nodirfile, filename ); | |
499 if( ret < 0 ) { | |
500 return 0; | |
501 } | |
502 } | |
503 } | |
504 | |
505 return 1; | |
506 } | |
507 | |
508 /** | |
509 * Open an unencrypted file from a DVD directory tree. | |
510 */ | |
511 static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename ) | |
512 { | |
513 char full_path[ PATH_MAX + 1 ]; | |
514 dvd_file_t *dvd_file; | |
515 struct stat fileinfo; | |
516 dvd_input_t dev; | |
517 | |
518 /* Get the full path of the file. */ | |
519 if( !findDVDFile( dvd, filename, full_path ) ) return 0; | |
520 | |
521 dev = DVDinput_open( full_path ); | |
522 if( !dev ) return 0; | |
523 | |
524 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
525 if( !dvd_file ) return 0; | |
526 dvd_file->dvd = dvd; | |
527 dvd_file->lb_start = 0; | |
528 dvd_file->seek_pos = 0; | |
529 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
530 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
531 dvd_file->filesize = 0; | |
532 | |
533 if( stat( full_path, &fileinfo ) < 0 ) { | |
534 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
535 free( dvd_file ); | |
536 return 0; | |
537 } | |
538 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
539 dvd_file->title_devs[ 0 ] = dev; | |
540 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
541 | |
542 return dvd_file; | |
543 } | |
544 | |
545 static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu ) | |
546 { | |
547 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
548 uint32_t start, len; | |
549 dvd_file_t *dvd_file; | |
550 | |
551 if( title == 0 ) { | |
552 sprintf( filename, "/VIDEO_TS/VIDEO_TS.VOB" ); | |
553 } else { | |
554 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, menu ? 0 : 1 ); | |
555 } | |
556 start = UDFFindFile( dvd, filename, &len ); | |
557 if( start == 0 ) return 0; | |
558 | |
559 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
560 if( !dvd_file ) return 0; | |
561 dvd_file->dvd = dvd; | |
562 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
563 dvd_file->lb_start = start; | |
564 dvd_file->seek_pos = 0; | |
565 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
566 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
567 dvd_file->filesize = len / DVD_VIDEO_LB_LEN; | |
568 | |
569 /* Calculate the complete file size for every file in the VOBS */ | |
570 if( !menu ) { | |
571 int cur; | |
572 | |
573 for( cur = 2; cur < 10; cur++ ) { | |
574 sprintf( filename, "/VIDEO_TS/VTS_%02d_%d.VOB", title, cur ); | |
575 if( !UDFFindFile( dvd, filename, &len ) ) break; | |
576 dvd_file->filesize += len / DVD_VIDEO_LB_LEN; | |
577 } | |
578 } | |
579 | |
580 if( dvd->css_state == 1 /* Need key init */ ) { | |
7033 | 581 // initAllCSSKeys( dvd ); |
582 // dvd->css_state = 2; | |
7029 | 583 } |
584 /* | |
585 if( DVDinput_seek( dvd_file->dvd->dev, | |
586 (int)start, DVDINPUT_SEEK_KEY ) < 0 ) { | |
587 fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n", | |
588 filename ); | |
589 } | |
590 */ | |
591 | |
592 return dvd_file; | |
593 } | |
594 | |
595 static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) | |
596 { | |
597 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
598 char full_path[ PATH_MAX + 1 ]; | |
599 struct stat fileinfo; | |
600 dvd_file_t *dvd_file; | |
601 int i; | |
602 | |
603 dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); | |
604 if( !dvd_file ) return 0; | |
605 dvd_file->dvd = dvd; | |
606 /*Hack*/ dvd_file->css_title = title << 1 | menu; | |
607 dvd_file->lb_start = 0; | |
608 dvd_file->seek_pos = 0; | |
609 memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); | |
610 memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); | |
611 dvd_file->filesize = 0; | |
612 | |
613 if( menu ) { | |
614 dvd_input_t dev; | |
615 | |
616 if( title == 0 ) { | |
617 sprintf( filename, "VIDEO_TS.VOB" ); | |
618 } else { | |
619 sprintf( filename, "VTS_%02i_0.VOB", title ); | |
620 } | |
621 if( !findDVDFile( dvd, filename, full_path ) ) { | |
622 free( dvd_file ); | |
623 return 0; | |
624 } | |
625 | |
626 dev = DVDinput_open( full_path ); | |
627 if( dev == NULL ) { | |
628 free( dvd_file ); | |
629 return 0; | |
630 } | |
631 | |
632 if( stat( full_path, &fileinfo ) < 0 ) { | |
633 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
634 free( dvd_file ); | |
635 return 0; | |
636 } | |
637 dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
638 dvd_file->title_devs[ 0 ] = dev; | |
639 DVDinput_seek( dvd_file->title_devs[0], 0, DVDINPUT_SEEK_KEY ); | |
640 dvd_file->filesize = dvd_file->title_sizes[ 0 ]; | |
641 | |
642 } else { | |
643 for( i = 0; i < 9; ++i ) { | |
644 | |
645 sprintf( filename, "VTS_%02i_%i.VOB", title, i + 1 ); | |
646 if( !findDVDFile( dvd, filename, full_path ) ) { | |
647 break; | |
648 } | |
649 | |
650 if( stat( full_path, &fileinfo ) < 0 ) { | |
651 fprintf( stderr, "libdvdread: Can't stat() %s.\n", filename ); | |
652 break; | |
653 } | |
654 | |
655 dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; | |
656 dvd_file->title_devs[ i ] = DVDinput_open( full_path ); | |
657 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
|
658 DVDinput_seek( dvd_file->title_devs[ i ], 0, DVDINPUT_SEEK_KEY ); |
7029 | 659 } |
8881
1e40d4a2466f
Function DVDOpenVOBPath only decrypts first VOB file and since each VOB file has
arpi
parents:
7423
diff
changeset
|
660 if( !dvd_file->title_devs[ 0 ] ) { |
7029 | 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 } |