| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # File: DSF.pm | ||
| 3 | # | ||
| 4 | # Description: Read DSF meta information | ||
| 5 | # | ||
| 6 | # Revisions: 2025-09-24 - P. Harvey Created | ||
| 7 | # | ||
| 8 | # References: 1) https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf | ||
| 9 | #------------------------------------------------------------------------------ | ||
| 10 | |||
| 11 | package Image::ExifTool::DSF; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use vars qw($VERSION); | ||
| 15 | use Image::ExifTool qw(:DataAccess :Utils); | ||
| 16 | |||
| 17 | $VERSION = '1.00'; | ||
| 18 | |||
| 19 | # DSF header format | ||
| 20 | %Image::ExifTool::DSF::Main = ( | ||
| 21 | PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData, | ||
| 22 | GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Audio' }, | ||
| 23 | FORMAT => 'int32u', | ||
| 24 | NOTES => q{ | ||
| 25 | Tags read from the 'fmt ' header of a DSF (DSD Stream File) audio files. As | ||
| 26 | well, ID3 metadata may also exist in these files. See | ||
| 27 | L<https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf> | ||
| 28 | for the specification. | ||
| 29 | }, | ||
| 30 | 3 => 'FormatVersion', | ||
| 31 | 4 => { Name => 'FormatID', PrintConv => { 0 => 'DSD Raw' }}, | ||
| 32 | 5 => { | ||
| 33 | Name => 'ChannelType', | ||
| 34 | PrintConv => { | ||
| 35 | 1 => 'Mono', | ||
| 36 | 2 => 'Stereo (Left, Right)', | ||
| 37 | 3 => '3 Channels (Left, Right, Center)', | ||
| 38 | 4 => 'Quad (Left, Right, Back L, Back R)', | ||
| 39 | 5 => '4 Channels (Left, Right, Center, Bass)', | ||
| 40 | 6 => '5 Channels (Left, Right, Center, Back L, Back R)', | ||
| 41 | 7 => '5.1 Channels (Left, Right, Center, Bass, Back L, Back R)', | ||
| 42 | }, | ||
| 43 | }, | ||
| 44 | 6 => 'ChannelCount', | ||
| 45 | 7 => 'SampleRate', | ||
| 46 | 8 => 'BitsPerSample', | ||
| 47 | 9 => { Name => 'SampleCount', Format => 'int64u' }, | ||
| 48 | 11 => 'BlockSize', | ||
| 49 | ); | ||
| 50 | |||
| 51 | #------------------------------------------------------------------------------ | ||
| 52 | # Extract metadata from a DSF file | ||
| 53 | # Inputs: 0) ExifTool object reference, 1) dirInfo reference | ||
| 54 | # Returns: 1 on success, 0 if this wasn't a valid DSF file | ||
| 55 | sub ProcessDSF($$) | ||
| 56 | { | ||
| 57 | my ($et, $dirInfo) = @_; | ||
| 58 | my $raf = $$dirInfo{RAF}; | ||
| 59 | my ($buff, $buf2, $tagTablePtr); | ||
| 60 | |||
| 61 | # verify this is a valid DSF file | ||
| 62 | return 0 unless $raf->Read($buff, 40) == 40; | ||
| 63 | return 0 unless $buff =~ /^DSD \x1c\0{7}.{16}fmt /s; | ||
| 64 | $et->SetFileType(); # set the FileType tag | ||
| 65 | my $tagTbl = GetTagTable('Image::ExifTool::DSF::Main'); | ||
| 66 | SetByteOrder('II'); | ||
| 67 | my $fmtLen = Get64u(\$buff,32); | ||
| 68 | unless ($fmtLen > 12 and $fmtLen < 1000 and | ||
| 69 | $raf->Read($buf2, $fmtLen - 12) == $fmtLen - 12) | ||
| 70 | { | ||
| 71 | $et->Warn('Error reading DSF fmt chunk'); | ||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | my $fileSize = Get64u(\$buff, 12); | ||
| 75 | my $metaPos = Get64u(\$buff, 20); | ||
| 76 | $buff = substr($buff, 28) . $buf2; | ||
| 77 | # | ||
| 78 | # process the DSF 'fmt ' chunk | ||
| 79 | # | ||
| 80 | my %dirInfo = ( | ||
| 81 | DataPt => \$buff, | ||
| 82 | DirStart => 0, | ||
| 83 | DirLen => length($buff), | ||
| 84 | ); | ||
| 85 | $et->ProcessBinaryData(\%dirInfo, $tagTbl); | ||
| 86 | # | ||
| 87 | # process ID3v2 if it exists | ||
| 88 | # | ||
| 89 | my $metaLen = $fileSize - $metaPos; | ||
| 90 | if ($metaPos and $metaLen > 0 and $metaLen < 20000000 and | ||
| 91 | $raf->Seek($metaPos, 0) and $raf->Read($buff, $metaLen) == $metaLen) | ||
| 92 | { | ||
| 93 | $dirInfo{DataPos} = $metaPos; | ||
| 94 | $dirInfo{DirLen} = $metaLen; | ||
| 95 | my $id3Tbl = GetTagTable('Image::ExifTool::ID3::Main'); | ||
| 96 | $et->ProcessDirectory(\%dirInfo, $id3Tbl); | ||
| 97 | } | ||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; # end | ||
| 102 | |||
| 103 | __END__ |