From eb3c1b5cedfc1b7d35a2d261780979b00170946e Mon Sep 17 00:00:00 2001 From: aurel Date: Thu, 30 Jun 2005 22:48:26 +0000 Subject: update libdvdread to v0.9.4 git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@15875 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libmpdvdkit2/Makefile | 4 +- libmpdvdkit2/README | 6 +- libmpdvdkit2/bswap.h | 26 ++- libmpdvdkit2/config.h | 2 +- libmpdvdkit2/dvd_input.c | 257 ++++++++++++++++++++---- libmpdvdkit2/dvd_input.h | 24 +-- libmpdvdkit2/dvd_reader.c | 311 +++++++++++++++++++++++------ libmpdvdkit2/dvd_reader.h | 208 ++++++++++++++----- libmpdvdkit2/dvd_udf.c | 369 +++++++++++++++++++++++++++++++++- libmpdvdkit2/dvd_udf.h | 8 +- libmpdvdkit2/ifo_print.c | 245 ++++++++++++++++------- libmpdvdkit2/ifo_print.h | 1 + libmpdvdkit2/ifo_read.c | 498 +++++++++++++++++++++++++++++++++------------- libmpdvdkit2/ifo_read.h | 16 +- libmpdvdkit2/ifo_types.h | 313 +++++++++++++++++++++-------- libmpdvdkit2/nav_print.c | 20 +- libmpdvdkit2/nav_print.h | 12 +- libmpdvdkit2/nav_read.c | 120 ++++++----- libmpdvdkit2/nav_read.h | 16 +- libmpdvdkit2/nav_types.h | 161 ++++++++------- 20 files changed, 1997 insertions(+), 620 deletions(-) (limited to 'libmpdvdkit2') diff --git a/libmpdvdkit2/Makefile b/libmpdvdkit2/Makefile index 7df8e6fa43..602309557d 100644 --- a/libmpdvdkit2/Makefile +++ b/libmpdvdkit2/Makefile @@ -15,6 +15,7 @@ SRCS = css.c \ libdvdcss.c \ nav_print.c \ nav_read.c \ + md5.c \ #bsdi_ioctl.c @@ -34,7 +35,8 @@ endif # -funroll-loops removed, triggered gcc 3.0.4 (3.x?) bug CFLAGS= -I. $(OPTFLAGS) $(EXTRA_INC)\ - -DSYS_LINUX -D__USE_UNIX98 -D_REENTRANT -D_GNU_SOURCE + -DSYS_LINUX -D__USE_UNIX98 -D_REENTRANT -D_GNU_SOURCE \ + -DHAVE_DVDCSS_DVDCSS_H -DSTDC_HEADERS .c.o: $(CC) $(CFLAGS) -c -o $@ $< diff --git a/libmpdvdkit2/README b/libmpdvdkit2/README index 3a84c1b195..b1b55bfa87 100644 --- a/libmpdvdkit2/README +++ b/libmpdvdkit2/README @@ -1,12 +1,12 @@ -MPlayer DVD-kit v2.2 :) +MPlayer DVD-kit v2.3 :) made by Pontscho & A'rpi for the MPlayer project What the hell is this? ====================== Nothing special, just a collection of sources and patches and fixes: -- dvdread 0.9.3 + static libdvdcss (removed dlopen code) -- libdvdcss 1.2.5 +- dvdread 0.9.4 + static libdvdcss +- libdvdcss 1.2.8 - optimizations enabled, asserts disabled everything packed together with _static_ linking to maximize performance. diff --git a/libmpdvdkit2/bswap.h b/libmpdvdkit2/bswap.h index 240f06e16f..7b5e7448f2 100644 --- a/libmpdvdkit2/bswap.h +++ b/libmpdvdkit2/bswap.h @@ -32,6 +32,11 @@ #else +/* For __FreeBSD_version */ +#if defined(HAVE_SYS_PARAM_H) +#include +#endif + #if defined(__linux__) #include #define B2N_16(x) x = bswap_16(x) @@ -50,6 +55,18 @@ #define B2N_32(x) x = swap32(x) #define B2N_64(x) x = swap64(x) +#elif defined(__FreeBSD__) && __FreeBSD_version >= 470000 +#include +#define B2N_16(x) x = be16toh(x) +#define B2N_32(x) x = be32toh(x) +#define B2N_64(x) x = be64toh(x) + +#elif defined(__DragonFly__) +#include +#define B2N_16(x) x = be16toh(x) +#define B2N_32(x) x = be32toh(x) +#define B2N_64(x) x = be64toh(x) + #elif defined(ARCH_X86) inline static unsigned short bswap_16(unsigned short x) { @@ -88,17 +105,12 @@ inline static unsigned long long int bswap_64(unsigned long long int x) } #define B2N_64(x) x = bswap_64(x) -#elif defined(__DragonFly__) -#include -#define B2N_16(x) x = be16toh(x) -#define B2N_32(x) x = be32toh(x) -#define B2N_64(x) x = be64toh(x) - /* This is a slow but portable implementation, it has multiple evaluation * problems so beware. - * FreeBSD and Solaris don't have or any other such + * Old FreeBSD's and Solaris don't have or any other such * functionality! */ + #elif defined(__FreeBSD__) || defined(__sun) || defined(__bsdi__) || defined(__CYGWIN__) #define B2N_16(x) \ x = ((((x) & 0xff00) >> 8) | \ diff --git a/libmpdvdkit2/config.h b/libmpdvdkit2/config.h index 1520ef34e9..43e624c7c9 100644 --- a/libmpdvdkit2/config.h +++ b/libmpdvdkit2/config.h @@ -1,6 +1,6 @@ /* Version number of package */ -#define VERSION "1.2.2" +#define VERSION "1.2.3" #define HAVE_UNISTD_H 1 /* Define if your processor stores words with the most significant diff --git a/libmpdvdkit2/dvd_input.c b/libmpdvdkit2/dvd_input.c index a5c0e7f0d4..840d8e2086 100644 --- a/libmpdvdkit2/dvd_input.c +++ b/libmpdvdkit2/dvd_input.c @@ -21,6 +21,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ +#include "config.h" + #include #include #include @@ -29,21 +31,34 @@ #include "dvd_reader.h" #include "dvd_input.h" -#include "dvdcss.h" - -dvdcss_handle (*DVDcss_open) (const char *); -int (*DVDcss_close) (dvdcss_handle); -int (*DVDcss_seek) (dvdcss_handle, int, int); -int (*DVDcss_title) (dvdcss_handle, int); -int (*DVDcss_read) (dvdcss_handle, void *, int, int); -char * (*DVDcss_error) (dvdcss_handle); +/* The function pointers that is the exported interface of this file. */ +dvd_input_t (*dvdinput_open) (const char *); +int (*dvdinput_close) (dvd_input_t); +int (*dvdinput_seek) (dvd_input_t, int); +int (*dvdinput_title) (dvd_input_t, int); +int (*dvdinput_read) (dvd_input_t, void *, int, int); +char * (*dvdinput_error) (dvd_input_t); -dvd_input_t (*DVDinput_open) (const char *); -int (*DVDinput_close) (dvd_input_t); -int (*DVDinput_seek) (dvd_input_t, int, int); -int (*DVDinput_title) (dvd_input_t, int); -int (*DVDinput_read) (dvd_input_t, void *, int, int); -char * (*DVDinput_error) (dvd_input_t); +#ifdef HAVE_DVDCSS_DVDCSS_H +/* linking to libdvdcss */ +#include "dvdcss.h" +#define DVDcss_open(a) dvdcss_open((char*)(a)) +#define DVDcss_close dvdcss_close +#define DVDcss_seek dvdcss_seek +#define DVDcss_title dvdcss_title +#define DVDcss_read dvdcss_read +#define DVDcss_error dvdcss_error +#else +/* dlopening libdvdcss */ +#include +typedef struct dvdcss_s *dvdcss_handle; +static dvdcss_handle (*DVDcss_open) (const char *); +static int (*DVDcss_close) (dvdcss_handle); +static int (*DVDcss_seek) (dvdcss_handle, int, int); +static int (*DVDcss_title) (dvdcss_handle, int); +static int (*DVDcss_read) (dvdcss_handle, void *, int, int); +static char * (*DVDcss_error) (dvdcss_handle); +#endif /* The DVDinput handle, add stuff here for new input methods. */ struct dvd_input_s { @@ -61,7 +76,7 @@ struct dvd_input_s { static dvd_input_t css_open(const char *target) { dvd_input_t dev; - + /* Allocate the handle structure */ dev = (dvd_input_t) malloc(sizeof(struct dvd_input_s)); if(dev == NULL) { @@ -72,7 +87,7 @@ static dvd_input_t css_open(const char *target) /* Really open it with libdvdcss */ dev->dvdcss = DVDcss_open(target); if(dev->dvdcss == 0) { - fprintf(stderr, "libdvdread: Could not open device with libdvdcss.\n"); + fprintf(stderr, "libdvdread: Could not open %s with libdvdcss.\n", target); free(dev); return NULL; } @@ -91,9 +106,10 @@ static char *css_error(dvd_input_t dev) /** * seek into the device. */ -static int css_seek(dvd_input_t dev, int blocks, int flags) +static int css_seek(dvd_input_t dev, int blocks) { - return DVDcss_seek(dev->dvdcss, blocks, flags); + /* DVDINPUT_NOFLAGS should match the DVDCSS_NOFLAGS value. */ + return DVDcss_seek(dev->dvdcss, blocks, DVDINPUT_NOFLAGS); } /** @@ -131,34 +147,207 @@ static int css_close(dvd_input_t dev) + + + +/** + * initialize and open a DVD device or file. + */ +static dvd_input_t file_open(const char *target) +{ + dvd_input_t dev; + + /* Allocate the library structure */ + dev = (dvd_input_t) malloc(sizeof(dvd_input_t)); + if(dev == NULL) { + fprintf(stderr, "libdvdread: Could not allocate memory.\n"); + return NULL; + } + + /* Open the device */ + dev->fd = open(target, O_RDONLY); + if(dev->fd < 0) { + perror("libdvdread: Could not open input"); + free(dev); + return NULL; + } + + return dev; +} + +/** + * return the last error message + */ +static char *file_error(dvd_input_t dev) +{ + /* use strerror(errno)? */ + return (char *)"unknown error"; +} + +/** + * seek into the device. + */ +static int file_seek(dvd_input_t dev, int blocks) +{ + off_t pos; + + pos = lseek(dev->fd, (off_t)blocks * (off_t)DVD_VIDEO_LB_LEN, SEEK_SET); + if(pos < 0) { + return pos; + } + /* assert pos % DVD_VIDEO_LB_LEN == 0 */ + return (int) (pos / DVD_VIDEO_LB_LEN); +} + +/** + * set the block for the begining of a new title (key). + */ +static int file_title(dvd_input_t dev, int block) +{ + return -1; +} + +/** + * read data from the device. + */ +static int file_read(dvd_input_t dev, void *buffer, int blocks, int flags) +{ + size_t len; + ssize_t ret; + + len = (size_t)blocks * DVD_VIDEO_LB_LEN; + + while(len > 0) { + + ret = read(dev->fd, buffer, len); + + if(ret < 0) { + /* One of the reads failed, too bad. We won't even bother + * returning the reads that went ok, and as in the posix spec + * the file postition is left unspecified after a failure. */ + return ret; + } + + if(ret == 0) { + /* Nothing more to read. Return the whole blocks, if any, that we got. + and adjust the file possition back to the previous block boundary. */ + size_t bytes = (size_t)blocks * DVD_VIDEO_LB_LEN - len; + off_t over_read = -(bytes % DVD_VIDEO_LB_LEN); + /*off_t pos =*/ lseek(dev->fd, over_read, SEEK_CUR); + /* should have pos % 2048 == 0 */ + return (int) (bytes / DVD_VIDEO_LB_LEN); + } + + len -= ret; + } + + return blocks; +} + +/** + * close the DVD device and clean up. + */ +static int file_close(dvd_input_t dev) +{ + int ret; + + ret = close(dev->fd); + + if(ret < 0) + return ret; + + free(dev); + + return 0; +} + + /** * Setup read functions with either libdvdcss or minimal DVD access. */ -int DVDInputSetup(void) +int dvdinput_setup(void) { - DVDcss_open = dvdcss_open; - DVDcss_close = dvdcss_close; - DVDcss_title = dvdcss_title; - DVDcss_seek = dvdcss_seek; - DVDcss_read = dvdcss_read; - DVDcss_error = dvdcss_error; + void *dvdcss_library = NULL; + char **dvdcss_version = NULL; + +#ifdef HAVE_DVDCSS_DVDCSS_H + /* linking to libdvdcss */ + dvdcss_library = &dvdcss_library; /* Give it some value != NULL */ + /* the DVDcss_* functions have been #defined at the top */ + dvdcss_version = &dvdcss_interface_2; + +#else + /* dlopening libdvdcss */ + dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY); + + if(dvdcss_library != NULL) { +#if defined(__OpenBSD__) && !defined(__ELF__) +#define U_S "_" +#else +#define U_S +#endif + DVDcss_open = (dvdcss_handle (*)(const char*)) + dlsym(dvdcss_library, U_S "dvdcss_open"); + DVDcss_close = (int (*)(dvdcss_handle)) + dlsym(dvdcss_library, U_S "dvdcss_close"); + DVDcss_title = (int (*)(dvdcss_handle, int)) + dlsym(dvdcss_library, U_S "dvdcss_title"); + DVDcss_seek = (int (*)(dvdcss_handle, int, int)) + dlsym(dvdcss_library, U_S "dvdcss_seek"); + DVDcss_read = (int (*)(dvdcss_handle, void*, int, int)) + dlsym(dvdcss_library, U_S "dvdcss_read"); + DVDcss_error = (char* (*)(dvdcss_handle)) + dlsym(dvdcss_library, U_S "dvdcss_error"); + dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2"); + + if(dlsym(dvdcss_library, U_S "dvdcss_crack")) { + fprintf(stderr, + "libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n" + "libdvdread: You should get the latest version from " + "http://www.videolan.org/\n" ); + dlclose(dvdcss_library); + dvdcss_library = NULL; + } else if(!DVDcss_open || !DVDcss_close || !DVDcss_title || !DVDcss_seek + || !DVDcss_read || !DVDcss_error || !dvdcss_version) { + fprintf(stderr, "libdvdread: Missing symbols in libdvdcss.so.2, " + "this shouldn't happen !\n"); + dlclose(dvdcss_library); + } + } +#endif /* HAVE_DVDCSS_DVDCSS_H */ + + if(dvdcss_library != NULL) { /* char *psz_method = getenv( "DVDCSS_METHOD" ); char *psz_verbose = getenv( "DVDCSS_VERBOSE" ); fprintf(stderr, "DVDCSS_METHOD %s\n", psz_method); fprintf(stderr, "DVDCSS_VERBOSE %s\n", psz_verbose); */ -// fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n", -// *dvdcss_version); + /* + fprintf(stderr, "libdvdread: Using libdvdcss version %s for DVD access\n", + *dvdcss_version); + */ - /* libdvdcss wraper functions */ - DVDinput_open = css_open; - DVDinput_close = css_close; - DVDinput_seek = css_seek; - DVDinput_title = css_title; - DVDinput_read = css_read; - DVDinput_error = css_error; + /* libdvdcss wrapper functions */ + dvdinput_open = css_open; + dvdinput_close = css_close; + dvdinput_seek = css_seek; + dvdinput_title = css_title; + dvdinput_read = css_read; + dvdinput_error = css_error; return 1; + } else { + fprintf(stderr, "libdvdread: Encrypted DVD support unavailable.\n"); + + /* libdvdcss replacement functions */ + dvdinput_open = file_open; + dvdinput_close = file_close; + dvdinput_seek = file_seek; + dvdinput_title = file_title; + dvdinput_read = file_read; + dvdinput_error = file_error; + return 0; + } } diff --git a/libmpdvdkit2/dvd_input.h b/libmpdvdkit2/dvd_input.h index ee28348995..e9e84917ea 100644 --- a/libmpdvdkit2/dvd_input.h +++ b/libmpdvdkit2/dvd_input.h @@ -5,10 +5,6 @@ * Copyright (C) 2001, 2002 Samuel Hocevar , * Håkan Hjort * - * Modified for use with MPlayer, changes contained in libdvdread_changes.diff. - * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ - * $Id$ - * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -31,25 +27,21 @@ #define DVDINPUT_READ_DECRYPT (1 << 0) -#define DVDINPUT_SEEK_MPEG (1 << 0) -#define DVDINPUT_SEEK_KEY (1 << 1) - - typedef struct dvd_input_s *dvd_input_t; /** - * Pointers which will be filled either the input meathods functions. + * Pointers which will be filled either the input methods functions. */ -extern dvd_input_t (*DVDinput_open) (const char *); -extern int (*DVDinput_close) (dvd_input_t); -extern int (*DVDinput_seek) (dvd_input_t, int, int); -extern int (*DVDinput_title) (dvd_input_t, int); -extern int (*DVDinput_read) (dvd_input_t, void *, int, int); -extern char * (*DVDinput_error) (dvd_input_t); +extern dvd_input_t (*dvdinput_open) (const char *); +extern int (*dvdinput_close) (dvd_input_t); +extern int (*dvdinput_seek) (dvd_input_t, int); +extern int (*dvdinput_title) (dvd_input_t, int); +extern int (*dvdinput_read) (dvd_input_t, void *, int, int); +extern char * (*dvdinput_error) (dvd_input_t); /** * Setup function accessed by dvd_reader.c. Returns 1 if there is CSS support. */ -int DVDInputSetup(void); +int dvdinput_setup(void); #endif /* DVD_INPUT_H_INCLUDED */ diff --git a/libmpdvdkit2/dvd_reader.c b/libmpdvdkit2/dvd_reader.c index f4da80a7de..4e5db5cc15 100644 --- a/libmpdvdkit2/dvd_reader.c +++ b/libmpdvdkit2/dvd_reader.c @@ -1,6 +1,7 @@ /* - * Copyright (C) 2001, 2002 Billy Biggs , - * Håkan Hjort + * Copyright (C) 2001, 2002, 2003 Billy Biggs , + * Håkan Hjort , + * Björn Englund * * Modified for use with MPlayer, changes contained in libdvdread_changes.diff. * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ @@ -62,6 +63,9 @@ static void gettimeofday(struct timeval* t,void* timezone){ #include "dvd_udf.h" #include "dvd_input.h" #include "dvd_reader.h" +#include "md5.h" + +#define DEFAULT_UDF_CACHE_LEVEL 0 struct dvd_reader_s { /* Basic information. */ @@ -70,13 +74,17 @@ struct dvd_reader_s { /* Hack for keeping track of the css status. * 0: no css, 1: perhaps (need init of keys), 2: have done init */ int css_state; - int css_title; /* Last title that we have called DVDinpute_title for. */ + int css_title; /* Last title that we have called dvdinpute_title for. */ /* Information required for an image file. */ dvd_input_t dev; /* Information required for a directory path drive. */ char *path_root; + + /* Filesystem cache */ + int udfcache_level; /* 0 - turned off, 1 - on */ + void *udfcache; }; struct dvd_file_s { @@ -98,6 +106,42 @@ struct dvd_file_s { ssize_t filesize; }; +/** + * Set the level of caching on udf + * level = 0 (no caching) + * level = 1 (caching filesystem info) + */ +int DVDUDFCacheLevel(dvd_reader_t *device, int level) +{ + struct dvd_reader_s *dev = (struct dvd_reader_s *)device; + + if(level > 0) { + level = 1; + } else if(level < 0) { + return dev->udfcache_level; + } + + dev->udfcache_level = level; + + return level; +} + +void *GetUDFCacheHandle(dvd_reader_t *device) +{ + struct dvd_reader_s *dev = (struct dvd_reader_s *)device; + + return dev->udfcache; +} + +void SetUDFCacheHandle(dvd_reader_t *device, void *cache) +{ + struct dvd_reader_s *dev = (struct dvd_reader_s *)device; + + dev->udfcache = cache; +} + + + /* Loop over all titles and call dvdcss_title to crack the keys. */ static int initAllCSSKeys( dvd_reader_t *dvd ) { @@ -107,6 +151,10 @@ static int initAllCSSKeys( dvd_reader_t *dvd ) uint32_t start, len; int title; + char *nokeys_str = getenv("DVDREAD_NOKEYS"); + if(nokeys_str != NULL) + return 0; + fprintf( stderr, "\n" ); fprintf( stderr, "libdvdread: Attempting to retrieve all CSS keys\n" ); fprintf( stderr, "libdvdread: This can take a _long_ time, " @@ -126,7 +174,7 @@ static int initAllCSSKeys( dvd_reader_t *dvd ) /* Perform CSS key cracking for this title. */ fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", filename, start ); - if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { + if( dvdinput_title( dvd->dev, (int)start ) < 0 ) { fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)\n", filename, start); } gettimeofday( &t_e, NULL ); @@ -144,7 +192,7 @@ static int initAllCSSKeys( dvd_reader_t *dvd ) /* Perform CSS key cracking for this title. */ fprintf( stderr, "libdvdread: Get key for %s at 0x%08x\n", filename, start ); - if( DVDinput_title( dvd->dev, (int)start ) < 0 ) { + if( dvdinput_title( dvd->dev, (int)start ) < 0 ) { fprintf( stderr, "libdvdread: Error cracking CSS key for %s (0x%08x)!!\n", filename, start); } gettimeofday( &t_e, NULL ); @@ -177,18 +225,8 @@ static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) { dvd_reader_t *dvd; dvd_input_t dev; - - /* setup cache dir is no longer needed, it's now implemented in libdvdcss.c - if(!dvdcss_cache_dir){ - dvdcss_cache_dir=get_path( "" ); - if ( dvdcss_cache_dir ) { mkdir( dvdcss_cache_dir,493 ); free( dvdcss_cache_dir ); } - dvdcss_cache_dir=get_path( "DVDKeys" ); - if(dvdcss_cache_dir) mkdir( dvdcss_cache_dir,493 ); - } - */ - /* open it */ - dev = DVDinput_open( location ); + dev = dvdinput_open( location ); if( !dev ) { fprintf( stderr, "libdvdread: Can't open %s for reading\n", location ); return 0; @@ -200,6 +238,9 @@ static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) dvd->dev = dev; dvd->path_root = 0; + dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL; + dvd->udfcache = NULL; + if( have_css ) { /* Only if DVDCSS_METHOD = title, a bit if it's disc or if * DVDCSS_METHOD = key but region missmatch. Unfortunaly we @@ -207,6 +248,7 @@ static dvd_reader_t *DVDOpenImageFile( const char *location, int have_css ) dvd->css_state = 1; /* Need key init. */ } + dvd->css_title = 0; return dvd; } @@ -221,6 +263,12 @@ static dvd_reader_t *DVDOpenPath( const char *path_root ) dvd->dev = 0; dvd->path_root = strdup( path_root ); + dvd->udfcache_level = DEFAULT_UDF_CACHE_LEVEL; + dvd->udfcache = NULL; + + dvd->css_state = 0; /* Only used in the UDF path */ + dvd->css_title = 0; /* Only matters in the UDF path */ + return dvd; } @@ -275,7 +323,8 @@ dvd_reader_t *DVDOpen( const char *path ) int ret, have_css; char *dev_name = 0; - if( !path ) return 0; + if( path == NULL ) + return 0; #ifdef WIN32 /* Stat doesn't work on devices under mingwin/cygwin. */ @@ -297,7 +346,7 @@ dvd_reader_t *DVDOpen( const char *path ) } /* Try to open libdvdcss or fall back to standard functions */ - have_css = DVDInputSetup(); + have_css = dvdinput_setup(); /* First check if this is a block/char device or a file*/ if( S_ISBLK( fileinfo.st_mode ) || @@ -445,10 +494,10 @@ dvd_reader_t *DVDOpen( const char *path ) void DVDClose( dvd_reader_t *dvd ) { if( dvd ) { - if( dvd->dev ) DVDinput_close( dvd->dev ); + if( dvd->dev ) dvdinput_close( dvd->dev ); if( dvd->path_root ) free( dvd->path_root ); + if( dvd->udfcache ) FreeUDFCache( dvd->udfcache ); free( dvd ); - dvd = 0; } } @@ -545,7 +594,7 @@ static dvd_file_t *DVDOpenFilePath( dvd_reader_t *dvd, char *filename ) /* Get the full path of the file. */ if( !findDVDFile( dvd, filename, full_path ) ) return 0; - dev = DVDinput_open( full_path ); + dev = dvdinput_open( full_path ); if( !dev ) return 0; dvd_file = (dvd_file_t *) malloc( sizeof( dvd_file_t ) ); @@ -609,8 +658,7 @@ static dvd_file_t *DVDOpenVOBUDF( dvd_reader_t *dvd, int title, int menu ) // dvd->css_state = 2; } /* - if( DVDinput_seek( dvd_file->dvd->dev, - (int)start, DVDINPUT_SEEK_KEY ) < 0 ) { + if( dvdinput_title( dvd_file->dvd->dev, (int)start ) < 0 ) { fprintf( stderr, "libdvdread: Error cracking CSS key for %s\n", filename ); } @@ -636,7 +684,7 @@ static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) memset( dvd_file->title_sizes, 0, sizeof( dvd_file->title_sizes ) ); memset( dvd_file->title_devs, 0, sizeof( dvd_file->title_devs ) ); dvd_file->filesize = 0; - + if( menu ) { dvd_input_t dev; @@ -650,7 +698,7 @@ static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) return 0; } - dev = DVDinput_open( full_path ); + dev = dvdinput_open( full_path ); if( dev == NULL ) { free( dvd_file ); return 0; @@ -663,7 +711,7 @@ static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) } dvd_file->title_sizes[ 0 ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; dvd_file->title_devs[ 0 ] = dev; - DVDinput_seek( dvd_file->title_devs[0], 0, DVDINPUT_SEEK_KEY ); + dvdinput_title( dvd_file->title_devs[0], 0); dvd_file->filesize = dvd_file->title_sizes[ 0 ]; } else { @@ -680,9 +728,9 @@ static dvd_file_t *DVDOpenVOBPath( dvd_reader_t *dvd, int title, int menu ) } dvd_file->title_sizes[ i ] = fileinfo.st_size / DVD_VIDEO_LB_LEN; - dvd_file->title_devs[ i ] = DVDinput_open( full_path ); + dvd_file->title_devs[ i ] = dvdinput_open( full_path ); + dvdinput_title( dvd_file->title_devs[ i ], 0 ); dvd_file->filesize += dvd_file->title_sizes[ i ]; - DVDinput_seek( dvd_file->title_devs[ i ], 0, DVDINPUT_SEEK_KEY ); } if( !dvd_file->title_devs[ 0 ] ) { free( dvd_file ); @@ -697,6 +745,10 @@ dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, dvd_read_domain_t domain ) { char filename[ MAX_UDF_FILE_NAME_LEN ]; + + /* Check arguments. */ + if( dvd == NULL || titlenum < 0 ) + return NULL; switch( domain ) { case DVD_READ_INFO_FILE: @@ -730,7 +782,7 @@ dvd_file_t *DVDOpenFile( dvd_reader_t *dvd, int titlenum, break; default: fprintf( stderr, "libdvdread: Invalid domain for file open.\n" ); - return 0; + return NULL; } if( dvd->isImageFile ) { @@ -750,7 +802,7 @@ void DVDCloseFile( dvd_file_t *dvd_file ) } else { for( i = 0; i < 9; ++i ) { if( dvd_file->title_devs[ i ] ) { - DVDinput_close( dvd_file->title_devs[i] ); + dvdinput_close( dvd_file->title_devs[i] ); } } } @@ -761,7 +813,7 @@ void DVDCloseFile( dvd_file_t *dvd_file ) } /* Internal, but used from dvd_udf.c */ -int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, +int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number, size_t block_count, unsigned char *data, int encrypted ) { @@ -772,13 +824,13 @@ int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, return 0; } - ret = DVDinput_seek( device->dev, (int) lb_number, DVDINPUT_NOFLAGS ); + ret = dvdinput_seek( device->dev, (int) lb_number ); if( ret != (int) lb_number ) { fprintf( stderr, "libdvdread: Can't seek to block %u\n", lb_number ); return 0; } - return DVDinput_read( device->dev, (char *) data, + return dvdinput_read( device->dev, (char *) data, (int) block_count, encrypted ); } @@ -792,8 +844,8 @@ static int DVDReadBlocksUDF( dvd_file_t *dvd_file, uint32_t offset, size_t block_count, unsigned char *data, int encrypted ) { - return DVDReadBlocksUDFRaw( dvd_file->dvd, dvd_file->lb_start + offset, - block_count, data, encrypted ); + return UDFReadBlocksRaw( dvd_file->dvd, dvd_file->lb_start + offset, + block_count, data, encrypted ); } /* This is using possibly several inputs and starting from an offset of '0'. @@ -816,14 +868,13 @@ static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, if( offset < dvd_file->title_sizes[ i ] ) { if( ( offset + block_count ) <= dvd_file->title_sizes[ i ] ) { - off = DVDinput_seek( dvd_file->title_devs[ i ], - (int)offset, DVDINPUT_NOFLAGS ); + off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset ); if( off < 0 || off != (int)offset ) { fprintf( stderr, "libdvdread: Can't seek to block %d\n", offset ); return off < 0 ? off : 0; } - ret = DVDinput_read( dvd_file->title_devs[ i ], data, + ret = dvdinput_read( dvd_file->title_devs[ i ], data, (int)block_count, encrypted ); break; } else { @@ -832,31 +883,29 @@ static int DVDReadBlocksPath( dvd_file_t *dvd_file, unsigned int offset, * (This is only true if you try and read >1GB at a time) */ /* Read part 1 */ - off = DVDinput_seek( dvd_file->title_devs[ i ], - (int)offset, DVDINPUT_NOFLAGS ); + off = dvdinput_seek( dvd_file->title_devs[ i ], (int)offset ); if( off < 0 || off != (int)offset ) { fprintf( stderr, "libdvdread: Can't seek to block %d\n", offset ); return off < 0 ? off : 0; } - ret = DVDinput_read( dvd_file->title_devs[ i ], data, + ret = dvdinput_read( dvd_file->title_devs[ i ], data, (int)part1_size, encrypted ); if( ret < 0 ) return ret; /* FIXME: This is wrong if i is the last file in the set. * also error from this read will not show in ret. */ - - /* Does the next part exist? If not then return now. */ - if( !dvd_file->title_devs[ i + 1 ] ) return ret; + + /* Does the next part exist? If not then return now. */ + if( !dvd_file->title_devs[ i + 1 ] ) return ret; /* Read part 2 */ - off = DVDinput_seek( dvd_file->title_devs[ i + 1 ], - 0, DVDINPUT_NOFLAGS ); + off = dvdinput_seek( dvd_file->title_devs[ i + 1 ], 0 ); if( off < 0 || off != 0 ) { fprintf( stderr, "libdvdread: Can't seek to block %d\n", 0 ); return off < 0 ? off : 0; } - ret2 = DVDinput_read( dvd_file->title_devs[ i + 1 ], + ret2 = dvdinput_read( dvd_file->title_devs[ i + 1 ], data + ( part1_size * (int64_t)DVD_VIDEO_LB_LEN ), (int)(block_count - part1_size), @@ -878,14 +927,20 @@ ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, { int ret; + /* Check arguments. */ + if( dvd_file == NULL || offset < 0 || data == NULL ) + return -1; + /* Hack, and it will still fail for multiple opens in a threaded app ! */ if( dvd_file->dvd->css_title != dvd_file->css_title ) { dvd_file->dvd->css_title = dvd_file->css_title; if( dvd_file->dvd->isImageFile ) { - DVDinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); - } else { - DVDinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); - } + dvdinput_title( dvd_file->dvd->dev, (int)dvd_file->lb_start ); + } + /* Here each vobu has it's own dvdcss handle, so no need to update + else { + dvdinput_title( dvd_file->title_devs[ 0 ], (int)dvd_file->lb_start ); + }*/ } if( dvd_file->dvd->isImageFile ) { @@ -901,11 +956,15 @@ ssize_t DVDReadBlocks( dvd_file_t *dvd_file, int offset, int DVDFileSeek( dvd_file_t *dvd_file, int offset ) { - if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { + /* Check arguments. */ + if( dvd_file == NULL || offset < 0 ) return -1; - } - dvd_file->seek_pos = (uint32_t) offset; - return offset; + + if( offset > dvd_file->filesize * DVD_VIDEO_LB_LEN ) { + return -1; + } + dvd_file->seek_pos = (uint32_t) offset; + return offset; } ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) @@ -914,10 +973,16 @@ ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) unsigned int numsec, seek_sector, seek_byte; int ret; + /* Check arguments. */ + if( dvd_file == NULL || data == NULL ) + return -1; + seek_sector = dvd_file->seek_pos / DVD_VIDEO_LB_LEN; seek_byte = dvd_file->seek_pos % DVD_VIDEO_LB_LEN; - numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + 1; + numsec = ( ( seek_byte + byte_size ) / DVD_VIDEO_LB_LEN ) + + ( ( ( seek_byte + byte_size ) % DVD_VIDEO_LB_LEN ) ? 1 : 0 ); + secbuf = (unsigned char *) malloc( numsec * DVD_VIDEO_LB_LEN ); if( !secbuf ) { fprintf( stderr, "libdvdread: Can't allocate memory " @@ -947,5 +1012,139 @@ ssize_t DVDReadBytes( dvd_file_t *dvd_file, void *data, size_t byte_size ) ssize_t DVDFileSize( dvd_file_t *dvd_file ) { + /* Check arguments. */ + if( dvd_file == NULL ) + return -1; + return dvd_file->filesize; } + +int DVDDiscID( dvd_reader_t *dvd, unsigned char *discid ) +{ + struct md5_ctx ctx; + int title; + + /* Check arguments. */ + if( dvd == NULL || discid == NULL ) + return 0; + + /* Go through the first 10 IFO:s, in order, + * and md5sum them, i.e VIDEO_TS.IFO and VTS_0?_0.IFO */ + md5_init_ctx( &ctx ); + for( title = 0; title < 10; title++ ) { + dvd_file_t *dvd_file = DVDOpenFile( dvd, title, DVD_READ_INFO_FILE ); + if( dvd_file != NULL ) { + ssize_t bytes_read; + size_t file_size = dvd_file->filesize * DVD_VIDEO_LB_LEN; + char *buffer = malloc( file_size ); + + if( buffer == NULL ) { + fprintf( stderr, "libdvdread: DVDDiscId, failed to " + "allocate memory for file read!\n" ); + return -1; + } + bytes_read = DVDReadBytes( dvd_file, buffer, file_size ); + if( bytes_read != file_size ) { + fprintf( stderr, "libdvdread: DVDDiscId read returned %d bytes" + ", wanted %d\n", bytes_read, file_size ); + DVDCloseFile( dvd_file ); + return -1; + } + + md5_process_bytes( buffer, file_size, &ctx ); + + DVDCloseFile( dvd_file ); + free( buffer ); + } + } + md5_finish_ctx( &ctx, discid ); + + return 0; +} + + +int DVDISOVolumeInfo( dvd_reader_t *dvd, + char *volid, unsigned int volid_size, + unsigned char *volsetid, unsigned int volsetid_size ) +{ + unsigned char *buffer; + int ret; + + /* Check arguments. */ + if( dvd == NULL ) + return 0; + + if( dvd->dev == NULL ) { + /* No block access, so no ISO... */ + return -1; + } + + buffer = malloc( DVD_VIDEO_LB_LEN ); + if( buffer == NULL ) { + fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to " + "allocate memory for file read!\n" ); + return -1; + } + + ret = UDFReadBlocksRaw( dvd, 16, 1, buffer, 0 ); + if( ret != 1 ) { + fprintf( stderr, "libdvdread: DVDISOVolumeInfo, failed to " + "read ISO9660 Primary Volume Descriptor!\n" ); + return -1; + } + + if( (volid != NULL) && (volid_size > 0) ) { + unsigned int n; + for(n = 0; n < 32; n++) { + if(buffer[40+n] == 0x20) { + break; + } + } + + if(volid_size > n+1) { + volid_size = n+1; + } + + memcpy(volid, &buffer[40], volid_size-1); + volid[volid_size-1] = '\0'; + } + + if( (volsetid != NULL) && (volsetid_size > 0) ) { + if(volsetid_size > 128) { + volsetid_size = 128; + } + memcpy(volsetid, &buffer[190], volsetid_size); + } + return 0; +} + + +int DVDUDFVolumeInfo( dvd_reader_t *dvd, + char *volid, unsigned int volid_size, + unsigned char *volsetid, unsigned int volsetid_size ) +{ + int ret; + /* Check arguments. */ + if( dvd == NULL ) + return -1; + + if( dvd->dev == NULL ) { + /* No block access, so no UDF VolumeSet Identifier */ + return -1; + } + + if( (volid != NULL) && (volid_size > 0) ) { + ret = UDFGetVolumeIdentifier(dvd, volid, volid_size); + if(!ret) { + return -1; + } + } + if( (volsetid != NULL) && (volsetid_size > 0) ) { + ret = UDFGetVolumeSetIdentifier(dvd, volsetid, volsetid_size); + if(!ret) { + return -1; + } + } + + return 0; +} diff --git a/libmpdvdkit2/dvd_reader.h b/libmpdvdkit2/dvd_reader.h index 75f7182747..cc4ba54895 100644 --- a/libmpdvdkit2/dvd_reader.h +++ b/libmpdvdkit2/dvd_reader.h @@ -3,7 +3,8 @@ /* * Copyright (C) 2001, 2002 Billy Biggs , - * Håkan Hjort + * Håkan Hjort , + * Björn Englund * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,128 +24,241 @@ #include /** - * The length of one Logical Block of a DVD Video. + * The DVD access interface. + * + * This file contains the functions that form the interface to to + * reading files located on a DVD. + */ + +/** + * The current version. + */ +#define DVDREAD_VERSION 904 + +/** + * The length of one Logical Block of a DVD. */ #define DVD_VIDEO_LB_LEN 2048 /** - * Maximum length of filenames for UDF. + * Maximum length of filenames allowed in UDF. */ #define MAX_UDF_FILE_NAME_LEN 2048 #ifdef __cplusplus extern "C" { #endif - + +/** + * Opaque type that is used as a handle for one instance of an opened DVD. + */ typedef struct dvd_reader_s dvd_reader_t; + +/** + * Opaque type for a file read handle, much like a normal fd or FILE *. + */ typedef struct dvd_file_s dvd_file_t; /** - * dvd = DVDOpen(path); - * * Opens a block device of a DVD-ROM file, or an image file, or a directory - * name for a mounted DVD or HD copy of a DVD. Returns 0 if we can't get any - * of those methods to work. + * name for a mounted DVD or HD copy of a DVD. * * If the given file is a block device, or is the mountpoint for a block * device, then that device is used for CSS authentication using libdvdcss. * If no device is available, then no CSS authentication is performed, * and we hope that the image is decrypted. * - * If the path given is a directory, then the files in that directory may be in - * any one of these formats: + * If the path given is a directory, then the files in that directory may be + * in any one of these formats: * * path/VIDEO_TS/VTS_01_1.VOB * path/video_ts/vts_01_1.vob * path/VTS_01_1.VOB * path/vts_01_1.vob + * + * @param path Specifies the the device, file or directory to be used. + * @return If successful a a read handle is returned. Otherwise 0 is returned. + * + * dvd = DVDOpen(path); */ dvd_reader_t *DVDOpen( const char * ); /** - * DVDClose(dvd); + * Closes and cleans up the DVD reader object. * - * Closes and cleans up the DVD reader object. You must close all open files - * before calling this function. + * You must close all open files before calling this function. + * + * @param dvd A read handle that should be closed. + * + * DVDClose(dvd); */ void DVDClose( dvd_reader_t * ); /** - * INFO_FILE : VIDEO_TS.IFO (manager) - * VTS_XX_0.IFO (title) - * - * INFO_BACKUP_FILE: VIDEO_TS.BUP (manager) - * VTS_XX_0.BUP (title) - * - * MENU_VOBS : VIDEO_TS.VOB (manager) - * VTS_XX_0.VOB (title) - * - * TITLE_VOBS : VTS_XX_[1-9].VOB (title) - * All files in the title set are opened and - * read as a single file. + * */ typedef enum { - DVD_READ_INFO_FILE, - DVD_READ_INFO_BACKUP_FILE, - DVD_READ_MENU_VOBS, - DVD_READ_TITLE_VOBS + DVD_READ_INFO_FILE, /**< VIDEO_TS.IFO or VTS_XX_0.IFO (title) */ + DVD_READ_INFO_BACKUP_FILE, /**< VIDEO_TS.BUP or VTS_XX_0.BUP (title) */ + DVD_READ_MENU_VOBS, /**< VIDEO_TS.VOB or VTS_XX_0.VOB (title) */ + DVD_READ_TITLE_VOBS /**< VTS_XX_[1-9].VOB (title). All files in + the title set are opened and read as a + single file. */ } dvd_read_domain_t; /** - * dvd_file = DVDOpenFile(dvd, titlenum, domain); + * Opens a file on the DVD given the title number and domain. * - * Opens a file on the DVD given the title number and domain. If the title - * number is 0, the video manager information is opened - * (VIDEO_TS.[IFO,BUP,VOB]). Returns a file structure which may be used for - * reads, or 0 if the file was not found. - */ -dvd_file_t *DVDOpenFile( dvd_reader_t *, int, - dvd_read_domain_t ); + * If the title number is 0, the video manager information is opened + * (VIDEO_TS.[IFO,BUP,VOB]). Returns a file structure which may be + * used for reads, or 0 if the file was not found. + * + * @param dvd A dvd read handle. + * @param titlenum Which Video Title Set should be used, VIDEO_TS is 0. + * @param domain Which domain. + * @return If successful a a file read handle is returned, otherwise 0. + * + * dvd_file = DVDOpenFile(dvd, titlenum, domain); */ +dvd_file_t *DVDOpenFile( dvd_reader_t *, int, dvd_read_domain_t ); /** - * DVDCloseFile(dvd_file); - * * Closes a file and frees the associated structure. + * + * @param dvd_file The file read handle to be closed. + * + * DVDCloseFile(dvd_file); */ void DVDCloseFile( dvd_file_t * ); /** - * blocks_read = DVDReadBlocks(dvd_file, offset, block_count, data); - * * Reads block_count number of blocks from the file at the given block offset. * Returns number of blocks read on success, -1 on error. This call is only * for reading VOB data, and should not be used when reading the IFO files. * When reading from an encrypted drive, blocks are decrypted using libdvdcss * where required. + * + * @param dvd_file A file read handle. + * @param offset Block offset from the start of the file to start reading at. + * @param block_count Number of block to read. + * @param data Pointer to a buffer to write the data into. + * @return Returns number of blocks read on success, -1 on error. + * + * blocks_read = DVDReadBlocks(dvd_file, offset, block_count, data); */ ssize_t DVDReadBlocks( dvd_file_t *, int, size_t, unsigned char * ); /** - * offset_set = DVDFileSeek(dvd_file, seek_offset); - * * Seek to the given position in the file. Returns the resulting position in * bytes from the beginning of the file. The seek position is only used for * byte reads from the file, the block read call always reads from the given * offset. + * + * @param dvd_file A file read handle. + * @param seek_offset Byte offset from the start of the file to seek to. + * @return The resulting position in bytes from the beginning of the file. + * + * offset_set = DVDFileSeek(dvd_file, seek_offset); */ int DVDFileSeek( dvd_file_t *, int ); /** - * bytes_read = DVDReadBytes(dvd_file, data, bytes); - * * Reads the given number of bytes from the file. This call can only be used * on the information files, and may not be used for reading from a VOB. This * reads from and increments the currrent seek position for the file. + * + * @param dvd_file A file read handle. + * @param data Pointer to a buffer to write the data into. + * @param bytes Number of bytes to read. + * @return Returns number of bytes read on success, -1 on error. + * + * bytes_read = DVDReadBytes(dvd_file, data, bytes); */ ssize_t DVDReadBytes( dvd_file_t *, void *, size_t ); /** - * blocks = DVDFileSize(dvd_file); - * * Returns the file size in blocks. + * + * @param dvd_file A file read handle. + * @return The size of the file in blocks, -1 on error. + * + * blocks = DVDFileSize(dvd_file); */ ssize_t DVDFileSize( dvd_file_t * ); +/** + * Get a unique 128 bit disc ID. + * This is the MD5 sum of VIDEO_TS.IFO and the VTS_0?_0.IFO files + * in title order (those that exist). + * If you need a 'text' representation of the id, print it as a + * hexadecimal number, using lowercase letters, discid[0] first. + * I.e. the same format as the command-line 'md5sum' program uses. + * + * @param dvd A read handle to get the disc ID from + * @param discid The buffer to put the disc ID into. The buffer must + * have room for 128 bits (16 chars). + * @return 0 on success, -1 on error. + */ +int DVDDiscID( dvd_reader_t *, unsigned char * ); + +/** + * Get the UDF VolumeIdentifier and VolumeSetIdentifier + * from the PrimaryVolumeDescriptor. + * + * @param dvd A read handle to get the disc ID from + * @param volid The buffer to put the VolumeIdentifier into. + * The VolumeIdentifier is latin-1 encoded (8bit unicode) + * null terminated and max 32 bytes (including '\0') + * @param volid_size No more than volid_size bytes will be copied to volid. + * If the VolumeIdentifier is truncated because of this + * it will still be null terminated. + * @param volsetid The buffer to put the VolumeSetIdentifier into. + * The VolumeIdentifier is 128 bytes as + * stored in the UDF PrimaryVolumeDescriptor. + * Note that this is not a null terminated string. + * @param volsetid_size At most volsetid_size bytes will be copied to volsetid. + * @return 0 on success, -1 on error. + */ +int DVDUDFVolumeInfo( dvd_reader_t *, char *, unsigned int, + unsigned char *, unsigned int ); + +/** + * Get the ISO9660 VolumeIdentifier and VolumeSetIdentifier + * + * * Only use this function as fallback if DVDUDFVolumeInfo returns 0 * + * * this will happen on a disc mastered only with a iso9660 filesystem * + * * All video DVD discs have UDF filesystem * + * + * @param dvd A read handle to get the disc ID from + * @param volid The buffer to put the VolumeIdentifier into. + * The VolumeIdentifier is coded with '0-9','A-Z','_' + * null terminated and max 33 bytes (including '\0') + * @param volid_size No more than volid_size bytes will be copied to volid. + * If the VolumeIdentifier is truncated because of this + * it will still be null terminated. + * @param volsetid The buffer to put the VolumeSetIdentifier into. + * The VolumeIdentifier is 128 bytes as + * stored in the ISO9660 PrimaryVolumeDescriptor. + * Note that this is not a null terminated string. + * @param volsetid_size At most volsetid_size bytes will be copied to volsetid. + * @return 0 on success, -1 on error. + */ +int DVDISOVolumeInfo( dvd_reader_t *, char *, unsigned int, + unsigned char *, unsigned int ); + +/** + * Sets the level of caching that is done when reading from a device + * + * @param dvd A read handle to get the disc ID from + * @param level The level of caching wanted. + * -1 - returns the current setting. + * 0 - UDF Cache turned off. + * 1 - (default level) Pointers to IFO files and some data from + * PrimaryVolumeDescriptor are cached. + * + * @return The level of caching. + */ +int DVDUDFCacheLevel( dvd_reader_t *, int ); + #ifdef __cplusplus }; #endif diff --git a/libmpdvdkit2/dvd_udf.c b/libmpdvdkit2/dvd_udf.c index a01aba790a..f2144451ee 100644 --- a/libmpdvdkit2/dvd_udf.c +++ b/libmpdvdkit2/dvd_udf.c @@ -5,8 +5,6 @@ * Modifications by: * Billy Biggs . * Björn Englund . - * Joey Parrish . - * - updated from libdvdread 0.9.4 and removed udf caching * * Modified for use with MPlayer, changes contained in libdvdread_changes.diff. * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ @@ -34,10 +32,11 @@ * http://www.gnu.org/copyleft/gpl.html */ +#include "config.h" + #include #include #include -//#include #ifndef __MINGW32__ #include #endif @@ -50,7 +49,7 @@ #include "dvd_udf.h" /* Private but located in/shared with dvd_reader.c */ -extern int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number, +extern int UDFReadBlocksRaw( dvd_reader_t *device, uint32_t lb_number, size_t block_count, unsigned char *data, int encrypted ); @@ -64,7 +63,7 @@ static int DVDReadLBUDF( dvd_reader_t *device, uint32_t lb_number, while(count > 0) { - ret = DVDReadBlocksUDFRaw(device, lb_number, count, data, encrypted); + ret = UDFReadBlocksRaw(device, lb_number, count, data, encrypted); if(ret <= 0) { /* One of the reads failed or nothing more to read, too bad. @@ -128,6 +127,201 @@ struct icbmap { uint8_t filetype; }; +struct udf_cache { + int avdp_valid; + struct avdp_t avdp; + int pvd_valid; + struct pvd_t pvd; + int partition_valid; + struct Partition partition; + int rooticb_valid; + struct AD rooticb; + int lb_num; + struct lbudf *lbs; + int map_num; + struct icbmap *maps; +}; + +typedef enum { + PartitionCache, RootICBCache, LBUDFCache, MapCache, AVDPCache, PVDCache +} UDFCacheType; + +extern void *GetUDFCacheHandle(dvd_reader_t *device); +extern void SetUDFCacheHandle(dvd_reader_t *device, void *cache); + +void FreeUDFCache(void *cache) +{ + struct udf_cache *c = (struct udf_cache *)cache; + if(c == NULL) { + return; + } + if(c->lbs) { + free(c->lbs); + } + if(c->maps) { + free(c->maps); + } + free(c); +} + + +static int GetUDFCache(dvd_reader_t *device, UDFCacheType type, + uint32_t nr, void *data) +{ + int n; + struct udf_cache *c; + + if(DVDUDFCacheLevel(device, -1) <= 0) { + return 0; + } + + c = (struct udf_cache *)GetUDFCacheHandle(device); + + if(c == NULL) { + return 0; + } + + switch(type) { + case AVDPCache: + if(c->avdp_valid) { + *(struct avdp_t *)data = c->avdp; + return 1; + } + break; + case PVDCache: + if(c->pvd_valid) { + *(struct pvd_t *)data = c->pvd; + return 1; + } + break; + case PartitionCache: + if(c->partition_valid) { + *(struct Partition *)data = c->partition; + return 1; + } + break; + case RootICBCache: + if(c->rooticb_valid) { + *(struct AD *)data = c->rooticb; + return 1; + } + break; + case LBUDFCache: + for(n = 0; n < c->lb_num; n++) { + if(c->lbs[n].lb == nr) { + *(uint8_t **)data = c->lbs[n].data; + return 1; + } + } + break; + case MapCache: + for(n = 0; n < c->map_num; n++) { + if(c->maps[n].lbn == nr) { + *(struct icbmap *)data = c->maps[n]; + return 1; + } + } + break; + default: + break; + } + + return 0; +} + +static int SetUDFCache(dvd_reader_t *device, UDFCacheType type, + uint32_t nr, void *data) +{ + int n; + struct udf_cache *c; + + if(DVDUDFCacheLevel(device, -1) <= 0) { + return 0; + } + + c = (struct udf_cache *)GetUDFCacheHandle(device); + + if(c == NULL) { + c = calloc(1, sizeof(struct udf_cache)); + // fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache)); + if(c == NULL) { + return 0; + } + SetUDFCacheHandle(device, c); + } + + + switch(type) { + case AVDPCache: + c->avdp = *(struct avdp_t *)data; + c->avdp_valid = 1; + break; + case PVDCache: + c->pvd = *(struct pvd_t *)data; + c->pvd_valid = 1; + break; + case PartitionCache: + c->partition = *(struct Partition *)data; + c->partition_valid = 1; + break; + case RootICBCache: + c->rooticb = *(struct AD *)data; + c->rooticb_valid = 1; + break; + case LBUDFCache: + for(n = 0; n < c->lb_num; n++) { + if(c->lbs[n].lb == nr) { + /* replace with new data */ + c->lbs[n].data = *(uint8_t **)data; + c->lbs[n].lb = nr; + return 1; + } + } + c->lb_num++; + c->lbs = realloc(c->lbs, c->lb_num * sizeof(struct lbudf)); + /* + fprintf(stderr, "realloc lb: %d * %d = %d\n", + c->lb_num, sizeof(struct lbudf), + c->lb_num * sizeof(struct lbudf)); + */ + if(c->lbs == NULL) { + c->lb_num = 0; + return 0; + } + c->lbs[n].data = *(uint8_t **)data; + c->lbs[n].lb = nr; + break; + case MapCache: + for(n = 0; n < c->map_num; n++) { + if(c->maps[n].lbn == nr) { + /* replace with new data */ + c->maps[n] = *(struct icbmap *)data; + c->maps[n].lbn = nr; + return 1; + } + } + c->map_num++; + c->maps = realloc(c->maps, c->map_num * sizeof(struct icbmap)); + /* + fprintf(stderr, "realloc maps: %d * %d = %d\n", + c->map_num, sizeof(struct icbmap), + c->map_num * sizeof(struct icbmap)); + */ + if(c->maps == NULL) { + c->map_num = 0; + return 0; + } + c->maps[n] = *(struct icbmap *)data; + c->maps[n].lbn = nr; + break; + default: + return 0; + } + + return 1; +} + + /* For direct data access, LSB first */ #define GETN1(p) ((uint8_t)data[p]) #define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8)) @@ -302,8 +496,16 @@ static int UDFMapICB( dvd_reader_t *device, struct AD ICB, uint8_t *FileType, uint8_t LogBlock[DVD_VIDEO_LB_LEN]; uint32_t lbnum; uint16_t TagID; + struct icbmap tmpmap; lbnum = partition->Start + ICB.Location; + tmpmap.lbn = lbnum; + if(GetUDFCache(device, MapCache, lbnum, &tmpmap)) { + *FileType = tmpmap.filetype; + *File = tmpmap.file; + return 1; + } + do { if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) { TagID = 0; @@ -313,6 +515,9 @@ static int UDFMapICB( dvd_reader_t *device, struct AD ICB, uint8_t *FileType, if( TagID == 261 ) { UDFFileEntry( LogBlock, FileType, partition, File ); + tmpmap.file = *File; + tmpmap.filetype = *FileType; + SetUDFCache(device, MapCache, tmpmap.lbn, &tmpmap); return 1; }; } while( ( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 ) @@ -328,7 +533,8 @@ static int UDFMapICB( dvd_reader_t *device, struct AD ICB, uint8_t *FileType, * return 1 on success, 0 on error; */ static int UDFScanDir( dvd_reader_t *device, struct AD Dir, char *FileName, - struct Partition *partition, struct AD *FileICB ) + struct Partition *partition, struct AD *FileICB, + int cache_file_info) { char filename[ MAX_UDF_FILE_NAME_LEN ]; uint8_t directory[ 2 * DVD_VIDEO_LB_LEN ]; @@ -336,9 +542,78 @@ static int UDFScanDir( dvd_reader_t *device, struct AD Dir, char *FileName, uint16_t TagID; uint8_t filechar; unsigned int p; + uint8_t *cached_dir = NULL; + uint32_t dir_lba; + struct AD tmpICB; + int found = 0; + int in_cache = 0; /* Scan dir for ICB of file */ lbnum = partition->Start + Dir.Location; + + if(DVDUDFCacheLevel(device, -1) > 0) { + /* caching */ + + if(!GetUDFCache(device, LBUDFCache, lbnum, &cached_dir)) { + dir_lba = (Dir.Length + DVD_VIDEO_LB_LEN) / DVD_VIDEO_LB_LEN; + if((cached_dir = malloc(dir_lba * DVD_VIDEO_LB_LEN)) == NULL) { + return 0; + } + if( DVDReadLBUDF( device, lbnum, dir_lba, cached_dir, 0) <= 0 ) { + free(cached_dir); + cached_dir = NULL; + } + /* + if(cached_dir) { + fprintf(stderr, "malloc dir: %d\n", + dir_lba * DVD_VIDEO_LB_LEN); + } + */ + SetUDFCache(device, LBUDFCache, lbnum, &cached_dir); + } else { + in_cache = 1; + } + + if(cached_dir == NULL) { + return 0; + } + + p = 0; + + while( p < Dir.Length ) { + UDFDescriptor( &cached_dir[ p ], &TagID ); + if( TagID == 257 ) { + p += UDFFileIdentifier( &cached_dir[ p ], &filechar, + filename, &tmpICB ); + if(cache_file_info && !in_cache) { + uint8_t tmpFiletype; + struct AD tmpFile; + + if( !strcasecmp( FileName, filename ) ) { + *FileICB = tmpICB; + found = 1; + + } + UDFMapICB(device, tmpICB, &tmpFiletype, + partition, &tmpFile); + } else { + if( !strcasecmp( FileName, filename ) ) { + *FileICB = tmpICB; + return 1; + } + } + } else { + if(cache_file_info && (!in_cache) && found) { + return 1; + } + return 0; + } + } + if(cache_file_info && (!in_cache) && found) { + return 1; + } + return 0; + } if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) { return 0; @@ -380,6 +655,10 @@ static int UDFGetAVDP( dvd_reader_t *device, int terminate; struct avdp_t; + if(GetUDFCache(device, AVDPCache, 0, avdp)) { + return 1; + } + /* Find Anchor */ lastsector = 0; lbnum = 256; /* Try #1, prime anchor */ @@ -427,6 +706,8 @@ static int UDFGetAVDP( dvd_reader_t *device, avdp->rvds.location = MVDS_location; avdp->rvds.length = MVDS_length; + SetUDFCache(device, AVDPCache, 0, avdp); + return 1; } @@ -514,8 +795,11 @@ uint32_t UDFFindFile( dvd_reader_t *device, char *filename, strcat( tokenline, filename ); + if(!(GetUDFCache(device, PartitionCache, 0, &partition) && + GetUDFCache(device, RootICBCache, 0, &RootICB))) { /* Find partition, 0 is the standard location for DVD Video.*/ if( !UDFFindPartition( device, 0, &partition ) ) return 0; + SetUDFCache(device, PartitionCache, 0, &partition); /* Find root dir ICB */ lbnum = partition.Start; @@ -536,22 +820,29 @@ uint32_t UDFFindFile( dvd_reader_t *device, char *filename, /* Sanity checks. */ if( TagID != 256 ) return 0; if( RootICB.Partition != 0 ) return 0; + SetUDFCache(device, RootICBCache, 0, &RootICB); + } /* Find root dir */ if( !UDFMapICB( device, RootICB, &filetype, &partition, &File ) ) return 0; if( filetype != 4 ) return 0; /* Root dir should be dir */ { + int cache_file_info = 0; /* Tokenize filepath */ token = strtok(tokenline, "/"); while( token != NULL ) { - if( !UDFScanDir( device, File, token, &partition, &ICB)) { + if( !UDFScanDir( device, File, token, &partition, &ICB, + cache_file_info)) { return 0; } if( !UDFMapICB( device, ICB, &filetype, &partition, &File ) ) { return 0; + } + if(!strcmp(token, "VIDEO_TS")) { + cache_file_info = 1; } token = strtok( NULL, "/" ); } @@ -636,12 +927,76 @@ static int UDFGetPVD(dvd_reader_t *device, struct pvd_t *pvd) { uint8_t pvd_buf[DVD_VIDEO_LB_LEN]; + if(GetUDFCache(device, PVDCache, 0, pvd)) { + return 1; + } + if(!UDFGetDescriptor( device, 1, pvd_buf, sizeof(pvd_buf))) { return 0; } memcpy(pvd->VolumeIdentifier, &pvd_buf[24], 32); memcpy(pvd->VolumeSetIdentifier, &pvd_buf[72], 128); + SetUDFCache(device, PVDCache, 0, pvd); return 1; } + +/** + * Gets the Volume Identifier string, in 8bit unicode (latin-1) + * volid, place to put the string + * volid_size, size of the buffer volid points to + * returns the size of buffer needed for all data + */ +int UDFGetVolumeIdentifier(dvd_reader_t *device, char *volid, + unsigned int volid_size) +{ + struct pvd_t pvd; + unsigned int volid_len; + + /* get primary volume descriptor */ + if(!UDFGetPVD(device, &pvd)) { + return 0; + } + + volid_len = pvd.VolumeIdentifier[31]; + if(volid_len > 31) { + /* this field is only 32 bytes something is wrong */ + volid_len = 31; + } + if(volid_size > volid_len) { + volid_size = volid_len; + } + Unicodedecode(pvd.VolumeIdentifier, volid_size, volid); + + return volid_len; +} + +/** + * Gets the Volume Set Identifier, as a 128-byte dstring (not decoded) + * WARNING This is not a null terminated string + * volsetid, place to put the data + * volsetid_size, size of the buffer volsetid points to + * the buffer should be >=128 bytes to store the whole volumesetidentifier + * returns the size of the available volsetid information (128) + * or 0 on error + */ +int UDFGetVolumeSetIdentifier(dvd_reader_t *device, uint8_t *volsetid, + unsigned int volsetid_size) +{ + struct pvd_t pvd; + + /* get primary volume descriptor */ + if(!UDFGetPVD(device, &pvd)) { + return 0; + } + + + if(volsetid_size > 128) { + volsetid_size = 128; + } + + memcpy(volsetid, pvd.VolumeSetIdentifier, volsetid_size); + + return 128; +} diff --git a/libmpdvdkit2/dvd_udf.h b/libmpdvdkit2/dvd_udf.h index 960ccf4055..b600983d9a 100644 --- a/libmpdvdkit2/dvd_udf.h +++ b/libmpdvdkit2/dvd_udf.h @@ -7,7 +7,8 @@ * * Modifications by: * Billy Biggs . - * + * Björn Englund . + * * dvdudf: parse and read the UDF volume information of a DVD Video * Copyright (C) 1999 Christian Wolff for convergence integrated media * GmbH The author can be reached at scarabaeus@convergence.de, the @@ -47,6 +48,11 @@ extern "C" { */ uint32_t UDFFindFile( dvd_reader_t *device, char *filename, uint32_t *size ); +void FreeUDFCache(void *cache); +int UDFGetVolumeIdentifier(dvd_reader_t *device, + char *volid, unsigned int volid_size); +int UDFGetVolumeSetIdentifier(dvd_reader_t *device, + uint8_t *volsetid, unsigned int volsetid_size); #ifdef __cplusplus }; #endif diff --git a/libmpdvdkit2/ifo_print.c b/libmpdvdkit2/ifo_print.c index 5da0ebcd61..6d5b54acfa 100644 --- a/libmpdvdkit2/ifo_print.c +++ b/libmpdvdkit2/ifo_print.c @@ -1,10 +1,7 @@ /* - * Copyright (C) 2000, 2001, 2002 Björn Englund , - * Håkan Hjort - * - * Modified for use with MPlayer, changes contained in libdvdread_changes.diff. - * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ - * $Id$ + * Copyright (C) 2000, 2001, 2002, 2003 + * Björn Englund , + * Håkan Hjort * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,26 +18,27 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "config.h" + #include #include #include #include #include #include -//#include -#include "config.h" // Needed for WORDS_BIGENDIAN #include "ifo_types.h" #include "ifo_read.h" #include "ifo_print.h" +#include "dvdread_internal.h" /* Put this in some other file / package? It's used in nav_print too. */ -static void ifoPrint_time(int level, dvd_time_t *dtime) { +static void ifoPrint_time(dvd_time_t *dtime) { const char *rate; - assert((dtime->hour>>4) < 0xa && (dtime->hour&0xf) < 0xa); - assert((dtime->minute>>4) < 0x7 && (dtime->minute&0xf) < 0xa); - assert((dtime->second>>4) < 0x7 && (dtime->second&0xf) < 0xa); - assert((dtime->frame_u&0xf) < 0xa); + CHECK_VALUE((dtime->hour>>4) < 0xa && (dtime->hour&0xf) < 0xa); + CHECK_VALUE((dtime->minute>>4) < 0x7 && (dtime->minute&0xf) < 0xa); + CHECK_VALUE((dtime->second>>4) < 0x7 && (dtime->second&0xf) < 0xa); + CHECK_VALUE((dtime->frame_u&0xf) < 0xa); printf("%02x:%02x:%02x.%02x", dtime->hour, @@ -79,7 +77,7 @@ static void ifoPrint_CMD(int row, vm_cmd_t *command) { printf("\n"); } -static void ifoPrint_video_attributes(int level, video_attr_t *attr) { +static void ifoPrint_video_attributes(video_attr_t *attr) { /* The following test is shorter but not correct ISO C, memcmp(attr,my_friendly_zeros, sizeof(video_attr_t)) */ @@ -90,6 +88,7 @@ static void ifoPrint_video_attributes(int level, video_attr_t *attr) { && attr->unknown1 == 0 && attr->line21_cc_1 == 0 && attr->line21_cc_2 == 0 + && attr->bit_rate == 0 && attr->video_format == 0 && attr->letterboxed == 0 && attr->film_mode == 0) { @@ -149,7 +148,7 @@ static void ifoPrint_video_attributes(int level, video_attr_t *attr) { } printf("U%x ", attr->unknown1); - assert(!attr->unknown1); + CHECK_VALUE(!attr->unknown1); if(attr->line21_cc_1 || attr->line21_cc_2) { printf("NTSC CC "); @@ -158,6 +157,17 @@ static void ifoPrint_video_attributes(int level, video_attr_t *attr) { if(attr->line21_cc_2) printf("2 "); } + + switch(attr->bit_rate) { + case 0: + printf("Variable Bit Rate "); + break; + case 1: + printf("Constant Bit Rate "); + break; + default: + printf("(please send a bug report)"); + } { int height = 480; @@ -192,7 +202,7 @@ static void ifoPrint_video_attributes(int level, video_attr_t *attr) { } } -static void ifoPrint_audio_attributes(int level, audio_attr_t *attr) { +static void ifoPrint_audio_attributes(audio_attr_t *attr) { if(attr->audio_format == 0 && attr->multichannel_extension == 0 @@ -201,8 +211,10 @@ static void ifoPrint_audio_attributes(int level, audio_attr_t *attr) { && attr->quantization == 0 && attr->sample_frequency == 0 && attr->channels == 0 + && attr->lang_code == 0 && attr->lang_extension == 0 - && attr->unknown1 == 0 + && attr->code_extension == 0 + && attr->unknown3 == 0 && attr->unknown1 == 0) { printf("-- Unspecified --"); return; @@ -240,10 +252,11 @@ static void ifoPrint_audio_attributes(int level, audio_attr_t *attr) { switch(attr->lang_type) { case 0: // not specified - assert(attr->lang_code == 0 || attr->lang_code == 0xffff); + CHECK_VALUE(attr->lang_code == 0 || attr->lang_code == 0xffff); break; case 1: - printf("%c%c ", attr->lang_code>>8, attr->lang_code & 0xff); + printf("%c%c (%c) ", attr->lang_code>>8, attr->lang_code & 0xff, + attr->lang_extension ? attr->lang_extension : ' '); break; default: printf("(please send a bug report) "); @@ -294,7 +307,7 @@ static void ifoPrint_audio_attributes(int level, audio_attr_t *attr) { printf("%dCh ", attr->channels + 1); - switch(attr->lang_extension) { + switch(attr->code_extension) { case 0: printf("Not specified "); break; @@ -315,34 +328,64 @@ static vo