2 * This code is based on dvdudf by:
3 * Christian Wolff <scarabaeus@convergence.de>.
6 * Billy Biggs <vektor@dumbterm.net>.
7 * Björn Englund <d4bjorn@dtek.chalmers.se>.
9 * Modified for use with MPlayer, changes contained in libdvdread_changes.diff.
10 * detailed changelog at http://svn.mplayerhq.hu/mplayer/trunk/
13 * dvdudf: parse and read the UDF volume information of a DVD Video
14 * Copyright (C) 1999 Christian Wolff for convergence integrated media
15 * GmbH The author can be reached at scarabaeus@convergence.de, the
16 * project's page is at http://linuxtv.org/dvd/
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or (at
21 * your option) any later version.
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 * 02111-1307, USA. Or, point your browser to
32 * http://www.gnu.org/copyleft/gpl.html
41 #include <sys/ioctl.h>
43 #include <sys/types.h>
48 #include "dvd_reader.h"
51 /* Private but located in/shared with dvd_reader.c */
52 extern int UDFReadBlocksRaw( dvd_reader_t
*device
, uint32_t lb_number
,
53 size_t block_count
, unsigned char *data
,
56 /* It's required to either fail or deliver all the blocks asked for. */
57 static int DVDReadLBUDF( dvd_reader_t
*device
, uint32_t lb_number
,
58 size_t block_count
, unsigned char *data
,
62 size_t count
= block_count
;
66 ret
= UDFReadBlocksRaw(device
, lb_number
, count
, data
, encrypted
);
69 /* One of the reads failed or nothing more to read, too bad.
70 * We won't even bother returning the reads that went ok. */
75 lb_number
+= (uint32_t)ret
;
83 #define NULL ((void *)0)
110 struct extent_ad mvds
;
111 struct extent_ad rvds
;
115 uint8_t VolumeIdentifier
[32];
116 uint8_t VolumeSetIdentifier
[128];
136 struct Partition partition
;
146 PartitionCache
, RootICBCache
, LBUDFCache
, MapCache
, AVDPCache
, PVDCache
149 extern void *GetUDFCacheHandle(dvd_reader_t
*device
);
150 extern void SetUDFCacheHandle(dvd_reader_t
*device
, void *cache
);
152 void FreeUDFCache(void *cache
)
154 struct udf_cache
*c
= (struct udf_cache
*)cache
;
168 static int GetUDFCache(dvd_reader_t
*device
, UDFCacheType type
,
169 uint32_t nr
, void *data
)
174 if(DVDUDFCacheLevel(device
, -1) <= 0) {
178 c
= (struct udf_cache
*)GetUDFCacheHandle(device
);
187 *(struct avdp_t
*)data
= c
->avdp
;
193 *(struct pvd_t
*)data
= c
->pvd
;
198 if(c
->partition_valid
) {
199 *(struct Partition
*)data
= c
->partition
;
204 if(c
->rooticb_valid
) {
205 *(struct AD
*)data
= c
->rooticb
;
210 for(n
= 0; n
< c
->lb_num
; n
++) {
211 if(c
->lbs
[n
].lb
== nr
) {
212 *(uint8_t **)data
= c
->lbs
[n
].data
;
218 for(n
= 0; n
< c
->map_num
; n
++) {
219 if(c
->maps
[n
].lbn
== nr
) {
220 *(struct icbmap
*)data
= c
->maps
[n
];
232 static int SetUDFCache(dvd_reader_t
*device
, UDFCacheType type
,
233 uint32_t nr
, void *data
)
238 if(DVDUDFCacheLevel(device
, -1) <= 0) {
242 c
= (struct udf_cache
*)GetUDFCacheHandle(device
);
245 c
= calloc(1, sizeof(struct udf_cache
));
246 // fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache));
250 SetUDFCacheHandle(device
, c
);
256 c
->avdp
= *(struct avdp_t
*)data
;
260 c
->pvd
= *(struct pvd_t
*)data
;
264 c
->partition
= *(struct Partition
*)data
;
265 c
->partition_valid
= 1;
268 c
->rooticb
= *(struct AD
*)data
;
269 c
->rooticb_valid
= 1;
272 for(n
= 0; n
< c
->lb_num
; n
++) {
273 if(c
->lbs
[n
].lb
== nr
) {
274 /* replace with new data */
275 c
->lbs
[n
].data
= *(uint8_t **)data
;
281 c
->lbs
= realloc(c
->lbs
, c
->lb_num
* sizeof(struct lbudf
));
283 fprintf(stderr, "realloc lb: %d * %d = %d\n",
284 c->lb_num, sizeof(struct lbudf),
285 c->lb_num * sizeof(struct lbudf));
291 c
->lbs
[n
].data
= *(uint8_t **)data
;
295 for(n
= 0; n
< c
->map_num
; n
++) {
296 if(c
->maps
[n
].lbn
== nr
) {
297 /* replace with new data */
298 c
->maps
[n
] = *(struct icbmap
*)data
;
304 c
->maps
= realloc(c
->maps
, c
->map_num
* sizeof(struct icbmap
));
306 fprintf(stderr, "realloc maps: %d * %d = %d\n",
307 c->map_num, sizeof(struct icbmap),
308 c->map_num * sizeof(struct icbmap));
310 if(c
->maps
== NULL
) {
314 c
->maps
[n
] = *(struct icbmap
*)data
;
325 /* For direct data access, LSB first */
326 #define GETN1(p) ((uint8_t)data[p])
327 #define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8))
328 #define GETN3(p) ((uint32_t)data[p] | ((uint32_t)data[(p) + 1] << 8) \
329 | ((uint32_t)data[(p) + 2] << 16))
330 #define GETN4(p) ((uint32_t)data[p] \
331 | ((uint32_t)data[(p) + 1] << 8) \
332 | ((uint32_t)data[(p) + 2] << 16) \
333 | ((uint32_t)data[(p) + 3] << 24))
334 /* This is wrong with regard to endianess */
335 #define GETN(p, n, target) memcpy(target, &data[p], n)
337 static int Unicodedecode( uint8_t *data
, int len
, char *target
)
341 if( ( data
[ 0 ] == 8 ) || ( data
[ 0 ] == 16 ) ) do {
342 if( data
[ 0 ] == 16 ) p
++; /* Ignore MSB of unicode16 */
344 target
[ i
++ ] = data
[ p
++ ];
352 static int UDFDescriptor( uint8_t *data
, uint16_t *TagID
)
355 // TODO: check CRC 'n stuff
359 static int UDFExtentAD( uint8_t *data
, uint32_t *Length
, uint32_t *Location
)
362 *Location
= GETN4(4);
366 static int UDFShortAD( uint8_t *data
, struct AD
*ad
,
367 struct Partition
*partition
)
369 ad
->Length
= GETN4(0);
370 ad
->Flags
= ad
->Length
>> 30;
371 ad
->Length
&= 0x3FFFFFFF;
372 ad
->Location
= GETN4(4);
373 ad
->Partition
= partition
->Number
; // use number of current partition
377 static int UDFLongAD( uint8_t *data
, struct AD
*ad
)
379 ad
->Length
= GETN4(0);
380 ad
->Flags
= ad
->Length
>> 30;
381 ad
->Length
&= 0x3FFFFFFF;
382 ad
->Location
= GETN4(4);
383 ad
->Partition
= GETN2(8);
388 static int UDFExtAD( uint8_t *data
, struct AD
*ad
)
390 ad
->Length
= GETN4(0);
391 ad
->Flags
= ad
->Length
>> 30;
392 ad
->Length
&= 0x3FFFFFFF;
393 ad
->Location
= GETN4(12);
394 ad
->Partition
= GETN2(16);
399 static int UDFICB( uint8_t *data
, uint8_t *FileType
, uint16_t *Flags
)
401 *FileType
= GETN1(11);
407 static int UDFPartition( uint8_t *data
, uint16_t *Flags
, uint16_t *Number
,
408 char *Contents
, uint32_t *Start
, uint32_t *Length
)
412 GETN(24, 32, Contents
);
414 *Length
= GETN4(192);
419 * Reads the volume descriptor and checks the parameters. Returns 0 on OK, 1
422 static int UDFLogVolume( uint8_t *data
, char *VolumeDescriptor
)
424 uint32_t lbsize
, MT_L
, N_PM
;
425 Unicodedecode(&data
[84], 128, VolumeDescriptor
);
426 lbsize
= GETN4(212); // should be 2048
427 MT_L
= GETN4(264); // should be 6
428 N_PM
= GETN4(268); // should be 1
429 if (lbsize
!= DVD_VIDEO_LB_LEN
) return 1;
433 static int UDFFileEntry( uint8_t *data
, uint8_t *FileType
,
434 struct Partition
*partition
, struct AD
*ad
)
440 UDFICB( &data
[ 16 ], FileType
, &flags
);
442 /* Init ad for an empty file (i.e. there isn't a AD, L_AD == 0 ) */
443 ad
->Length
= GETN4( 60 ); // Really 8 bytes a 56
445 ad
->Location
= 0; // what should we put here?
446 ad
->Partition
= partition
->Number
; // use number of current partition
451 while( p
< 176 + L_EA
+ L_AD
) {
452 switch( flags
& 0x0007 ) {
453 case 0: UDFShortAD( &data
[ p
], ad
, partition
); p
+= 8; break;
454 case 1: UDFLongAD( &data
[ p
], ad
); p
+= 16; break;
455 case 2: UDFExtAD( &data
[ p
], ad
); p
+= 20; break;
458 case 8: UDFShortAD( &data
[ p
], ad
, partition
); break;
459 case 16: UDFLongAD( &data
[ p
], ad
); break;
460 case 20: UDFExtAD( &data
[ p
], ad
); break;
471 static int UDFFileIdentifier( uint8_t *data
, uint8_t *FileCharacteristics
,
472 char *FileName
, struct AD
*FileICB
)
477 *FileCharacteristics
= GETN1(18);
479 UDFLongAD(&data
[20], FileICB
);
481 if (L_FI
) Unicodedecode(&data
[38 + L_IU
], L_FI
, FileName
);
482 else FileName
[0] = '\0';
483 return 4 * ((38 + L_FI
+ L_IU
+ 3) / 4);
488 * ICB: Location of ICB of directory to scan
489 * FileType: Type of the file
490 * File: Location of file the ICB is pointing to
491 * return 1 on success, 0 on error;
493 static int UDFMapICB( dvd_reader_t
*device
, struct AD ICB
, uint8_t *FileType
,
494 struct Partition
*partition
, struct AD
*File
)
496 uint8_t LogBlock
[DVD_VIDEO_LB_LEN
];
499 struct icbmap tmpmap
;
501 lbnum
= partition
->Start
+ ICB
.Location
;
503 if(GetUDFCache(device
, MapCache
, lbnum
, &tmpmap
)) {
504 *FileType
= tmpmap
.filetype
;
510 if( DVDReadLBUDF( device
, lbnum
++, 1, LogBlock
, 0 ) <= 0 ) {
513 UDFDescriptor( LogBlock
, &TagID
);
517 UDFFileEntry( LogBlock
, FileType
, partition
, File
);
519 tmpmap
.filetype
= *FileType
;
520 SetUDFCache(device
, MapCache
, tmpmap
.lbn
, &tmpmap
);
523 } while( ( lbnum
<= partition
->Start
+ ICB
.Location
+ ( ICB
.Length
- 1 )
524 / DVD_VIDEO_LB_LEN
) && ( TagID
!= 261 ) );
530 * Dir: Location of directory to scan
531 * FileName: Name of file to look for
532 * FileICB: Location of ICB of the found file
533 * return 1 on success, 0 on error;
535 static int UDFScanDir( dvd_reader_t
*device
, struct AD Dir
, char *FileName
,
536 struct Partition
*partition
, struct AD
*FileICB
,
539 char filename
[ MAX_UDF_FILE_NAME_LEN
];
540 uint8_t directory
[ 2 * DVD_VIDEO_LB_LEN
];
545 uint8_t *cached_dir
= NULL
;
551 /* Scan dir for ICB of file */
552 lbnum
= partition
->Start
+ Dir
.Location
;
554 if(DVDUDFCacheLevel(device
, -1) > 0) {
557 if(!GetUDFCache(device
, LBUDFCache
, lbnum
, &cached_dir
)) {
558 dir_lba
= (Dir
.Length
+ DVD_VIDEO_LB_LEN
) / DVD_VIDEO_LB_LEN
;
559 if((cached_dir
= malloc(dir_lba
* DVD_VIDEO_LB_LEN
)) == NULL
) {
562 if( DVDReadLBUDF( device
, lbnum
, dir_lba
, cached_dir
, 0) <= 0 ) {
568 fprintf(stderr, "malloc dir: %d\n",
569 dir_lba * DVD_VIDEO_LB_LEN);
572 SetUDFCache(device
, LBUDFCache
, lbnum
, &cached_dir
);
577 if(cached_dir
== NULL
) {
583 while( p
< Dir
.Length
) {
584 UDFDescriptor( &cached_dir
[ p
], &TagID
);
586 p
+= UDFFileIdentifier( &cached_dir
[ p
], &filechar
,
588 if(cache_file_info
&& !in_cache
) {
592 if( !strcasecmp( FileName
, filename
) ) {
597 UDFMapICB(device
, tmpICB
, &tmpFiletype
,
598 partition
, &tmpFile
);
600 if( !strcasecmp( FileName
, filename
) ) {
606 if(cache_file_info
&& (!in_cache
) && found
) {
612 if(cache_file_info
&& (!in_cache
) && found
) {
618 if( DVDReadLBUDF( device
, lbnum
, 2, directory
, 0 ) <= 0 ) {
623 while( p
< Dir
.Length
) {
624 if( p
> DVD_VIDEO_LB_LEN
) {
626 p
-= DVD_VIDEO_LB_LEN
;
627 Dir
.Length
-= DVD_VIDEO_LB_LEN
;
628 if( DVDReadLBUDF( device
, lbnum
, 2, directory
, 0 ) <= 0 ) {
632 UDFDescriptor( &directory
[ p
], &TagID
);
634 p
+= UDFFileIdentifier( &directory
[ p
], &filechar
,
636 if( !strcasecmp( FileName
, filename
) ) {
648 static int UDFGetAVDP( dvd_reader_t
*device
,
651 uint8_t Anchor
[ DVD_VIDEO_LB_LEN
];
652 uint32_t lbnum
, MVDS_location
, MVDS_length
;
658 if(GetUDFCache(device
, AVDPCache
, 0, avdp
)) {
664 lbnum
= 256; /* Try #1, prime anchor */
668 if( DVDReadLBUDF( device
, lbnum
, 1, Anchor
, 0 ) > 0 ) {
669 UDFDescriptor( Anchor
, &TagID
);
675 if( terminate
) return 0; /* Final try failed */
679 /* We already found the last sector. Try #3, alternative
680 * backup anchor. If that fails, don't try again.
685 /* TODO: Find last sector of the disc (this is optional). */
687 /* Try #2, backup anchor */
688 lbnum
= lastsector
- 256;
690 /* Unable to find last sector */
695 /* It's an anchor! We can leave */
699 /* Main volume descriptor */
700 UDFExtentAD( &Anchor
[ 16 ], &MVDS_length
, &MVDS_location
);
701 avdp
->mvds
.location
= MVDS_location
;
702 avdp
->mvds
.length
= MVDS_length
;
704 /* Backup volume descriptor */
705 UDFExtentAD( &Anchor
[ 24 ], &MVDS_length
, &MVDS_location
);
706 avdp
->rvds
.location
= MVDS_location
;
707 avdp
->rvds
.length
= MVDS_length
;
709 SetUDFCache(device
, AVDPCache
, 0, avdp
);
715 * Looks for partition on the disc. Returns 1 if partition found, 0 on error.
716 * partnum: Number of the partition, starting at 0.
717 * part: structure to fill with the partition information
719 static int UDFFindPartition( dvd_reader_t
*device
, int partnum
,
720 struct Partition
*part
)
722 uint8_t LogBlock
[ DVD_VIDEO_LB_LEN
];
723 uint32_t lbnum
, MVDS_location
, MVDS_length
;
729 if(!UDFGetAVDP(device
, &avdp
)) {
733 /* Main volume descriptor */
734 MVDS_location
= avdp
.mvds
.location
;
735 MVDS_length
= avdp
.mvds
.length
;
739 part
->VolumeDesc
[ 0 ] = '\0';
742 /* Find Volume Descriptor */
743 lbnum
= MVDS_location
;
746 if( DVDReadLBUDF( device
, lbnum
++, 1, LogBlock
, 0 ) <= 0 ) {
749 UDFDescriptor( LogBlock
, &TagID
);
752 if( ( TagID
== 5 ) && ( !part
->valid
) ) {
753 /* Partition Descriptor */
754 UDFPartition( LogBlock
, &part
->Flags
, &part
->Number
,
755 part
->Contents
, &part
->Start
, &part
->Length
);
756 part
->valid
= ( partnum
== part
->Number
);
757 } else if( ( TagID
== 6 ) && ( !volvalid
) ) {
758 /* Logical Volume Descriptor */
759 if( UDFLogVolume( LogBlock
, part
->VolumeDesc
) ) {
760 /* TODO: sector size wrong! */
766 } while( ( lbnum
<= MVDS_location
+ ( MVDS_length
- 1 )
767 / DVD_VIDEO_LB_LEN
) && ( TagID
!= 8 )
768 && ( ( !part
->valid
) || ( !volvalid
) ) );
770 if( ( !part
->valid
) || ( !volvalid
) ) {
771 /* Backup volume descriptor */
772 MVDS_location
= avdp
.mvds
.location
;
773 MVDS_length
= avdp
.mvds
.length
;
775 } while( i
-- && ( ( !part
->valid
) || ( !volvalid
) ) );
777 /* We only care for the partition, not the volume */
781 uint32_t UDFFindFile( dvd_reader_t
*device
, char *filename
,
784 uint8_t LogBlock
[ DVD_VIDEO_LB_LEN
];
787 struct Partition partition
;
788 struct AD RootICB
, File
, ICB
;
789 char tokenline
[ MAX_UDF_FILE_NAME_LEN
];
795 strcat( tokenline
, filename
);
798 if(!(GetUDFCache(device
, PartitionCache
, 0, &partition
) &&
799 GetUDFCache(device
, RootICBCache
, 0, &RootICB
))) {
800 /* Find partition, 0 is the standard location for DVD Video.*/
801 if( !UDFFindPartition( device
, 0, &partition
) ) return 0;
802 SetUDFCache(device
, PartitionCache
, 0, &partition
);
804 /* Find root dir ICB */
805 lbnum
= partition
.Start
;
807 if( DVDReadLBUDF( device
, lbnum
++, 1, LogBlock
, 0 ) <= 0 ) {
810 UDFDescriptor( LogBlock
, &TagID
);
813 /* File Set Descriptor */
814 if( TagID
== 256 ) { // File Set Descriptor
815 UDFLongAD( &LogBlock
[ 400 ], &RootICB
);
817 } while( ( lbnum
< partition
.Start
+ partition
.Length
)
818 && ( TagID
!= 8 ) && ( TagID
!= 256 ) );
821 if( TagID
!= 256 ) return 0;
822 if( RootICB
.Partition
!= 0 ) return 0;
823 SetUDFCache(device
, RootICBCache
, 0, &RootICB
);
827 if( !UDFMapICB( device
, RootICB
, &filetype
, &partition
, &File
) ) return 0;
828 if( filetype
!= 4 ) return 0; /* Root dir should be dir */
831 int cache_file_info
= 0;
832 /* Tokenize filepath */
833 token
= strtok(tokenline
, "/");
835 while( token
!= NULL
) {
837 if( !UDFScanDir( device
, File
, token
, &partition
, &ICB
,
841 if( !UDFMapICB( device
, ICB
, &filetype
, &partition
, &File
) ) {
844 if(!strcmp(token
, "VIDEO_TS")) {
847 token
= strtok( NULL
, "/" );
852 if( File
.Partition
!= 0 ) return 0;
854 *filesize
= File
.Length
;
855 /* Hack to not return partition.Start for empty files. */
859 return partition
.Start
+ File
.Location
;
865 * Gets a Descriptor .
866 * Returns 1 if descriptor found, 0 on error.
867 * id, tagid of descriptor
868 * bufsize, size of BlockBuf (must be >= DVD_VIDEO_LB_LEN).
870 static int UDFGetDescriptor( dvd_reader_t
*device
, int id
,
871 uint8_t *descriptor
, int bufsize
)
873 uint32_t lbnum
, MVDS_location
, MVDS_length
;
881 lbnum
= 256; /* Try #1, prime anchor */
883 if(bufsize
< DVD_VIDEO_LB_LEN
) {
887 if(!UDFGetAVDP(device
, &avdp
)) {
891 /* Main volume descriptor */
892 MVDS_location
= avdp
.mvds
.location
;
893 MVDS_length
= avdp
.mvds
.length
;
897 /* Find Descriptor */
898 lbnum
= MVDS_location
;
901 if( DVDReadLBUDF( device
, lbnum
++, 1, descriptor
, 0 ) <= 0 ) {
904 UDFDescriptor( descriptor
, &TagID
);
907 if( (TagID
== id
) && ( !desc_found
) ) {
911 } while( ( lbnum
<= MVDS_location
+ ( MVDS_length
- 1 )
912 / DVD_VIDEO_LB_LEN
) && ( TagID
!= 8 )
916 /* Backup volume descriptor */
917 MVDS_location
= avdp
.rvds
.location
;
918 MVDS_length
= avdp
.rvds
.length
;
920 } while( i
-- && ( !desc_found
) );
926 static int UDFGetPVD(dvd_reader_t
*device
, struct pvd_t
*pvd
)
928 uint8_t pvd_buf
[DVD_VIDEO_LB_LEN
];
930 if(GetUDFCache(device
, PVDCache
, 0, pvd
)) {
934 if(!UDFGetDescriptor( device
, 1, pvd_buf
, sizeof(pvd_buf
))) {
938 memcpy(pvd
->VolumeIdentifier
, &pvd_buf
[24], 32);
939 memcpy(pvd
->VolumeSetIdentifier
, &pvd_buf
[72], 128);
940 SetUDFCache(device
, PVDCache
, 0, pvd
);
946 * Gets the Volume Identifier string, in 8bit unicode (latin-1)
947 * volid, place to put the string
948 * volid_size, size of the buffer volid points to
949 * returns the size of buffer needed for all data
951 int UDFGetVolumeIdentifier(dvd_reader_t
*device
, char *volid
,
952 unsigned int volid_size
)
955 unsigned int volid_len
;
957 /* get primary volume descriptor */
958 if(!UDFGetPVD(device
, &pvd
)) {
962 volid_len
= pvd
.VolumeIdentifier
[31];
964 /* this field is only 32 bytes something is wrong */
967 if(volid_size
> volid_len
) {
968 volid_size
= volid_len
;
970 Unicodedecode(pvd
.VolumeIdentifier
, volid_size
, volid
);
976 * Gets the Volume Set Identifier, as a 128-byte dstring (not decoded)
977 * WARNING This is not a null terminated string
978 * volsetid, place to put the data
979 * volsetid_size, size of the buffer volsetid points to
980 * the buffer should be >=128 bytes to store the whole volumesetidentifier
981 * returns the size of the available volsetid information (128)
984 int UDFGetVolumeSetIdentifier(dvd_reader_t
*device
, uint8_t *volsetid
,
985 unsigned int volsetid_size
)
989 /* get primary volume descriptor */
990 if(!UDFGetPVD(device
, &pvd
)) {
995 if(volsetid_size
> 128) {
999 memcpy(volsetid
, pvd
.VolumeSetIdentifier
, volsetid_size
);