source: gs2-extensions/parallel-building/trunk/src/perllib/cpan/Image/ExifTool/DV.pm@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

  • Property svn:executable set to *
File size: 9.9 KB
Line 
1#------------------------------------------------------------------------------
2# File: DV.pm
3#
4# Description: Read DV meta information
5#
6# Revisions: 2010/12/24 - P. Harvey Created
7#
8# References: 1) http://www.ffmpeg.org/
9# 2) http://dvswitch.alioth.debian.org/wiki/DV_format/
10#------------------------------------------------------------------------------
11
12package Image::ExifTool::DV;
13
14use strict;
15use vars qw($VERSION);
16use Image::ExifTool qw(:DataAccess :Utils);
17
18$VERSION = '1.00';
19
20# DV profiles (ref 1)
21my @dvProfiles = (
22 {
23 DSF => 0,
24 VideoSType => 0x0,
25 FrameSize => 120000,
26 VideoFormat => 'IEC 61834, SMPTE-314M - 525/60 (NTSC)',
27 VideoScanType => 'Progressive',
28 Colorimetry => '4:1:1',
29 FrameRate => 30000/1001,
30 ImageHeight => 480,
31 ImageWidth => 720,
32 },{
33 DSF => 1,
34 VideoSType => 0x0,
35 FrameSize => 144000,
36 VideoFormat => 'IEC 61834 - 625/50 (PAL)',
37 VideoScanType => 'Progressive',
38 Colorimetry => '4:2:0',
39 FrameRate => 25/1,
40 ImageHeight => 576,
41 ImageWidth => 720,
42 },{
43 DSF => 1,
44 VideoSType => 0x0,
45 FrameSize => 144000,
46 VideoFormat => 'SMPTE-314M - 625/50 (PAL)',
47 VideoScanType => 'Progressive',
48 Colorimetry => '4:1:1',
49 FrameRate => 25/1,
50 ImageHeight => 576,
51 ImageWidth => 720,
52 },{
53 DSF => 0,
54 VideoSType => 0x4,
55 FrameSize => 240000,
56 VideoFormat => 'DVCPRO50: SMPTE-314M - 525/60 (NTSC) 50 Mbps',
57 VideoScanType => 'Progressive',
58 Colorimetry => '4:2:2',
59 FrameRate => 30000/1001,
60 ImageHeight => 480,
61 ImageWidth => 720,
62 },{
63 DSF => 1,
64 VideoSType => 0x4,
65 FrameSize => 288000,
66 VideoFormat => 'DVCPRO50: SMPTE-314M - 625/50 (PAL) 50 Mbps',
67 VideoScanType => 'Progressive',
68 Colorimetry => '4:2:2',
69 FrameRate => 25/1,
70 ImageHeight => 576,
71 ImageWidth => 720,
72 },{
73 DSF => 0,
74 VideoSType => 0x14,
75 FrameSize => 480000,
76 VideoFormat => 'DVCPRO HD: SMPTE-370M - 1080i60 100 Mbps',
77 VideoScanType => 'Interlaced',
78 Colorimetry => '4:2:2',
79 FrameRate => 30000/1001,
80 ImageHeight => 1080,
81 ImageWidth => 1280,
82 },{
83 DSF => 1,
84 VideoSType => 0x14,
85 FrameSize => 576000,
86 VideoFormat => 'DVCPRO HD: SMPTE-370M - 1080i50 100 Mbps',
87 VideoScanType => 'Interlaced',
88 Colorimetry => '4:2:2',
89 FrameRate => 25/1,
90 ImageHeight => 1080,
91 ImageWidth => 1440,
92 },{
93 DSF => 0,
94 VideoSType => 0x18,
95 FrameSize => 240000,
96 VideoFormat => 'DVCPRO HD: SMPTE-370M - 720p60 100 Mbps',
97 VideoScanType => 'Progressive',
98 Colorimetry => '4:2:2',
99 FrameRate => 60000/1001,
100 ImageHeight => 720,
101 ImageWidth => 960,
102 },{
103 DSF => 1,
104 VideoSType => 0x18,
105 FrameSize => 288000,
106 VideoFormat => 'DVCPRO HD: SMPTE-370M - 720p50 100 Mbps',
107 VideoScanType => 'Progressive',
108 Colorimetry => '4:2:2',
109 FrameRate => 50/1,
110 ImageHeight => 720,
111 ImageWidth => 960,
112 },{
113 DSF => 1,
114 VideoSType => 0x1,
115 FrameSize => 144000,
116 VideoFormat => 'IEC 61883-5 - 625/50 (PAL)',
117 VideoScanType => 'Progressive',
118 Colorimetry => '4:2:0',
119 FrameRate => 25/1,
120 ImageHeight => 576,
121 ImageWidth => 720,
122 },
123);
124
125# tags to extract, in the order we want to extract them
126my @dvTags = (
127 'DateTimeOriginal', 'ImageWidth', 'ImageHeight', 'Duration',
128 'TotalBitrate', 'VideoFormat', 'VideoScanType', 'FrameRate',
129 'AspectRatio', 'Colorimetry', 'AudioChannels', 'AudioSampleRate',
130 'AudioBitsPerSample',
131);
132
133# DV tags
134%Image::ExifTool::DV::Main = (
135 GROUPS => { 2 => 'Video' },
136 VARS => { NO_ID => 1 },
137 NOTES => 'The following tags are extracted from DV videos.',
138 DateTimeOriginal => {
139 Groups => { 2 => 'Time' },
140 PrintConv => '$self->ConvertDateTime($val)',
141 },
142 ImageWidth => { },
143 ImageHeight => { },
144 Duration => { PrintConv => 'ConvertDuration($val)' },
145 TotalBitrate => { PrintConv => 'ConvertBitrate($val)' },
146 VideoFormat => { },
147 VideoScanType => { },
148 FrameRate => { PrintConv => 'int($val * 1000 + 0.5) / 1000' },
149 AspectRatio => { },
150 Colorimetry => { },
151 AudioChannels => { Groups => { 2 => 'Audio' } },
152 AudioSampleRate => { Groups => { 2 => 'Audio' } },
153 AudioBitsPerSample => { Groups => { 2 => 'Audio' } },
154);
155
156#------------------------------------------------------------------------------
157# Read information in a DV file (ref 1)
158# Inputs: 0) ExifTool ref, 1) dirInfo ref
159# Returns: 1 on success, 0 if this wasn't a valid DV file
160sub ProcessDV($$)
161{
162 my ($exifTool, $dirInfo) = @_;
163 local $_;
164 my $raf = $$dirInfo{RAF};
165 my ($buff, $start, $profile, $tag, $i, $j);
166
167 $raf->Read($buff, 12000) or return 0;
168 if ($buff =~ /\x1f\x07\0[\x3f\xbf]/sg) {
169 $start = pos($buff) - 4;
170 } else {
171 while ($buff =~ /[\0\xff]\x3f\x07\0.{76}\xff\x3f\x07\x01/sg) {
172 next if pos($buff) - 163 < 0;
173 $start = pos($buff) - 163;
174 last;
175 }
176 return 0 unless defined $start;
177 }
178 my $len = length $buff;
179 # must at least have a full DIF header
180 return 0 if $start + 80 * 6 > $len;
181
182 $exifTool->SetFileType();
183
184 my $pos = $start;
185 my $dsf = Get8u(\$buff, $pos + 3) & 0x80 >> 7;
186 my $stype = Get8u(\$buff, $pos + 80*5 + 48 + 3) & 0x1f;
187
188 # 576i50 25Mbps 4:1:1 is a special case
189 if ($dsf == 1 && $stype == 0 && Get8u(\$buff, 4) & 0x07) {
190 $profile = $dvProfiles[2];
191 } else {
192 foreach (@dvProfiles) {
193 next unless $dsf == $$_{DSF} and $stype == $$_{VideoSType};
194 $profile = $_;
195 last;
196 }
197 $profile or $exifTool->Warn("Unrecognized DV profile"), return 1;
198 }
199 my $tagTablePtr = GetTagTable('Image::ExifTool::DV::Main');
200
201 # calculate total bit rate and duration
202 my $byteRate = $$profile{FrameSize} * $$profile{FrameRate};
203 my $fileSize = $$exifTool{VALUE}{FileSize};
204 $$profile{TotalBitrate} = 8 * $byteRate;
205 $$profile{Duration} = $fileSize / $byteRate if defined $fileSize;
206
207 # read DVPack metadata from the VAUX DIF's to extract video tags
208 delete $$profile{DateTimeOriginal};
209 delete $$profile{AspectRatio};
210 my ($date, $time, $is16_9);
211 for ($i=1; $i<6; ++$i) {
212 $pos += 80;
213 my $type = Get8u(\$buff, $pos);
214 next unless ($type & 0xf0) == 0x50; # look for VAUX types
215 for ($j=0; $j<15; ++$j) {
216 my $p = $pos + $j * 5 + 3;
217 $type = Get8u(\$buff, $p);
218 if ($type == 0x61) { # video control
219 my $apt = Get8u(\$buff, $start + 4) & 0x07;
220 my $t = Get8u(\$buff, $p + 2);
221 $is16_9 = (($t & 0x07) == 0x02 or (not $apt and ($t & 0x07) == 0x07));
222 } elsif ($type == 0x62) { # date
223 # mask off unused bits
224 my @d = unpack('C*', substr($buff, $p + 1, 4));
225 # (ignore timezone in byte 0 until we can test this properly - see ref 2)
226 $date = sprintf('%.2x:%.2x:%.2x', $d[3], $d[2] & 0x1f, $d[1] & 0x3f);
227 if ($date =~ /[a-f]/) {
228 undef $date; # invalid date
229 } else {
230 # add century (this will work until 2089)
231 $date = ($date lt '9' ? '20' : '19') . $date;
232 }
233 undef $time;
234 } elsif ($type == 0x63 and $date) { # time
235 # (ignore frames past second in byte 0 for now - see ref 2)
236 my $val = Get32u(\$buff, $p + 1) & 0x007f7f3f;
237 my @t = unpack('C*', substr($buff, $p + 1, 4));
238 $time = sprintf('%.2x:%.2x:%.2x', $t[3] & 0x3f, $t[2] & 0x7f, $t[1] & 0x7f);
239 last;
240 } else {
241 undef $time; # must be consecutive
242 }
243 }
244 }
245 if ($date and $time) {
246 $$profile{DateTimeOriginal} = "$date $time";
247 $$profile{AspectRatio} = $is16_9 ? '16:9' : '5:4' if defined $is16_9;
248 }
249
250 # read audio tags if available
251 delete $$profile{AudioSampleRate};
252 delete $$profile{AudioBitsPerSample};
253 delete $$profile{AudioChannels};
254 $pos = $start + 80*6 + 80*16*3 + 3;
255 if ($pos + 4 < $len and Get8u(\$buff, $pos) == 0x50) {
256 my $smpls = Get8u(\$buff, $pos + 1);
257 my $freq = (Get8u(\$buff, $pos + 4) >> 3) & 0x07;
258 my $stype = Get8u(\$buff, $pos + 3) & 0x1f;
259 my $quant = Get8u(\$buff, $pos + 4) & 0x07;
260 if ($freq < 3) {
261 $$profile{AudioSampleRate} = {0=>48000, 1=>44100, 2=>32000}->{$freq};
262 }
263 if ($stype < 3) {
264 $stype = 2 if $stype == 0 and $quant and $freq == 2;
265 $$profile{AudioChannels} = {0=>2, 1=>0, 2=>4, 3=>8}->{$stype};
266 }
267 $$profile{AudioBitsPerSample} = $quant ? 12 : 16;
268 }
269
270 # save our metadata
271 foreach $tag (@dvTags) {
272 next unless defined $$profile{$tag};
273 $exifTool->HandleTag($tagTablePtr, $tag, $$profile{$tag});
274 }
275
276 return 1;
277}
278
2791; # end
280
281__END__
282
283=head1 NAME
284
285Image::ExifTool::DV - Read DV meta information
286
287=head1 SYNOPSIS
288
289This module is used by Image::ExifTool
290
291=head1 DESCRIPTION
292
293This module contains definitions required by Image::ExifTool to read meta
294information from DV (raw Digital Video) files.
295
296=head1 AUTHOR
297
298Copyright 2003-2011, Phil Harvey (phil at owl.phy.queensu.ca)
299
300This library is free software; you can redistribute it and/or modify it
301under the same terms as Perl itself.
302
303=head1 REFERENCES
304
305=over 4
306
307=item L<http://ffmpeg.org/>
308
309=item L<http://dvswitch.alioth.debian.org/wiki/DV_format/>
310
311=back
312
313=head1 SEE ALSO
314
315L<Image::ExifTool::TagNames/DV Tags>,
316L<Image::ExifTool(3pm)|Image::ExifTool>
317
318=cut
319
Note: See TracBrowser for help on using the repository browser.