| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # File: Kandao.pm | ||
| 3 | # | ||
| 4 | # Description: Read Kandao MP4 metadata | ||
| 5 | # | ||
| 6 | # Revisions: 2025-12-10 - P. Harvey Created | ||
| 7 | # | ||
| 8 | # Notes: Tested with videos from the Kandao QooCam 3 Ultra | ||
| 9 | #------------------------------------------------------------------------------ | ||
| 10 | |||
| 11 | package Image::ExifTool::Kandao; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use vars qw($VERSION); | ||
| 15 | use Image::ExifTool qw(:DataAccess :Utils); | ||
| 16 | |||
| 17 | $VERSION = '1.00'; | ||
| 18 | |||
| 19 | sub ProcessKandao($$$); | ||
| 20 | sub ProcessKVAR($$); | ||
| 21 | |||
| 22 | # Kandao format codes | ||
| 23 | my %format = ( | ||
| 24 | CHAR => 'string', | ||
| 25 | BOOL => 'int8u', | ||
| 26 | U8 => 'int8u', | ||
| 27 | U16 => 'int16u', | ||
| 28 | U32 => 'int32u', | ||
| 29 | U64 => 'int64u', | ||
| 30 | S8 => 'int8s', | ||
| 31 | S16 => 'int16s', | ||
| 32 | S32 => 'int32s', | ||
| 33 | S64 => 'int64s', | ||
| 34 | FLOAT => 'float', | ||
| 35 | DOUBLE => 'double', | ||
| 36 | ); | ||
| 37 | |||
| 38 | # Kandao 'kvar' and 'kfix' information | ||
| 39 | %Image::ExifTool::Kandao::Main = ( | ||
| 40 | GROUPS => { 0 => 'Kandao', 1 => 'KVAR', 2 => 'Camera' }, | ||
| 41 | VARS => { NO_LOOKUP => 1 }, | ||
| 42 | NOTES => q{ | ||
| 43 | Tags extracted from Kandao KVAR files and the 'kvar', 'kfix' and 'kstb' | ||
| 44 | atoms in Kandao MP4 videos, with a family 1 group name of KVAR, KFIX or KSTB | ||
| 45 | depending on their location. | ||
| 46 | }, | ||
| 47 | PROCESS_PROC => \&ProcessKandao, | ||
| 48 | # | ||
| 49 | # 'kvar' tags | ||
| 50 | # | ||
| 51 | CPU_TEMP => { Name => 'CPUTemperature', ValueConv => '$val / 10' }, # U32[1] | ||
| 52 | BAT_TEMP => 'BatteryTemperature', # U32[1] | ||
| 53 | PTS_UNIT => { # U32[1] | ||
| 54 | Name => 'TimeStampUnit', | ||
| 55 | PrintConv => { 0 => 'ms', 1 => 'Subtle', 2 => 'ns' }, | ||
| 56 | }, | ||
| 57 | TOTAL_FRAME => 'TotalFrames', # U32[1] | ||
| 58 | TOTAL_TIME_MS => { # U32[1] | ||
| 59 | Name => 'TotalTime', | ||
| 60 | ValueConv => '$val / 1000', | ||
| 61 | }, | ||
| 62 | LENS => 'LensData', # CHAR[534] | ||
| 63 | DASHBOARD => 'Dashboard', # U8[1] | ||
| 64 | PROJECTION => 'Projection', # CHAR[16] | ||
| 65 | CENTER_SHIFT => 'CenterShift', # DOUBLE[1] | ||
| 66 | DISTORTION => 'Distortion', # DOUBLE[1] | ||
| 67 | INFO => 'Info', # CHAR[239] | ||
| 68 | PTS => { | ||
| 69 | Name => 'PresentationTimeStamp', | ||
| 70 | Notes => 'TimeStamp for each frame', | ||
| 71 | Format => 'undef', # U64[x] | ||
| 72 | Binary => 1, | ||
| 73 | }, | ||
| 74 | LENS_SN0 => 'Lens0SerialNumber', # CHAR[15] | ||
| 75 | LENS_SN1 => 'Lens1SerialNumber', # CHAR[15] | ||
| 76 | LENS_OTP_ID0 => 'Lens0OTP_ID', # U8[1] | ||
| 77 | LENS_OTP_ID1 => 'Lens1OTP_ID', # U8[1] | ||
| 78 | IMU => { | ||
| 79 | RecordSize => 20, | ||
| 80 | SubDirectory => { TagTable => 'Image::ExifTool::Kandao::IMU' }, | ||
| 81 | }, | ||
| 82 | GPS => [{ | ||
| 83 | Condition => '$$valPt =~ /^\xff{4}/', | ||
| 84 | RecordSize => 28, | ||
| 85 | SubDirectory => { | ||
| 86 | TagTable => 'Image::ExifTool::Kandao::GPS', | ||
| 87 | Start => 4, | ||
| 88 | }, | ||
| 89 | },{ | ||
| 90 | RecordSize => 20, | ||
| 91 | SubDirectory => { | ||
| 92 | TagTable => 'Image::ExifTool::Kandao::GPS', | ||
| 93 | }, | ||
| 94 | }], | ||
| 95 | GPSX => { | ||
| 96 | RecordSize => 36, | ||
| 97 | SubDirectory => { TagTable => 'Image::ExifTool::Kandao::GPSX' }, | ||
| 98 | }, | ||
| 99 | EXP => { # U8[x] | ||
| 100 | # actually int64u - monotonically increasing | ||
| 101 | Name => 'Exp', | ||
| 102 | }, | ||
| 103 | # (ISP = Image Signal Processor) | ||
| 104 | ISP => 'ISP', # U8 - 46-byte records (not very interesting looking) | ||
| 105 | FRAME_ISP => { | ||
| 106 | # U8 - 12-byte records: int32u-ts, float, float | ||
| 107 | RecordSize => 12, | ||
| 108 | SubDirectory => { TagTable => 'Image::ExifTool::Kandao::FrameISP' }, | ||
| 109 | }, | ||
| 110 | GAINMAP0 => 'GainMap0', | ||
| 111 | GAINMAP1 => 'GainMap1', | ||
| 112 | # | ||
| 113 | # 'kfix' tags | ||
| 114 | # | ||
| 115 | PRODUCT => 'Model', | ||
| 116 | PROJECT => 'Project', | ||
| 117 | SN => 'SerialNumber', | ||
| 118 | PR_VER => 'ProductVersion', | ||
| 119 | SW_VER => 'SoftwareVersion', | ||
| 120 | HW_VER => 'HardwareVersion', | ||
| 121 | VIDEO_CAPTIME => { | ||
| 122 | Name => 'VideoCaptureTime', | ||
| 123 | Notes => 'local camera time', | ||
| 124 | Groups => { 2 => 'Time' }, | ||
| 125 | ValueConv => '$val =~ s/^(\d{4})-(\d{2})-/$1:$2:/; $val', | ||
| 126 | PrintConv => '$self->ConvertDateTime($val)', | ||
| 127 | }, | ||
| 128 | GPS_CAPTIME => { | ||
| 129 | Name => 'GPSCaptureTime', | ||
| 130 | Groups => { 2 => 'Time' }, | ||
| 131 | ValueConv => 'ConvertUnixTime($val/1000, 0, 3) . "Z"', | ||
| 132 | PrintConv => '$self->ConvertDateTime($val)', | ||
| 133 | }, | ||
| 134 | LENS_INDEX => { Name => 'LensIndex', Format => 'int16u' }, | ||
| 135 | NEED_LSC => 'NeedLDSC', | ||
| 136 | # (also in 'kvar') CPU_TEMP => { Name => 'CPUTemperature', ValueConv => '$val / 10' }, | ||
| 137 | # (also in 'kvar') BAT_TEMP => 'BatteryTemperature', | ||
| 138 | INPUT_INSERT => 'InputInsert', | ||
| 139 | VIDEO_RESOLUTION => 'VideoResolution', | ||
| 140 | VIDEO_CODECTYPE => 'VideoCodec', | ||
| 141 | VIDEO_BITRATE => { Name => 'VideoBitrate', PrintConv => '($val / 1e6) . " Mbps"' }, | ||
| 142 | VIDEO_FORMAT => 'VideoFormat', | ||
| 143 | OUTPUT_INSERT => 'OutputInsert', | ||
| 144 | DYNAMIC_RANGE => 'DynamicRange', | ||
| 145 | AWB_CCT => 'AWB_CCT', | ||
| 146 | EV => 'EV', | ||
| 147 | ISO => 'ISO', | ||
| 148 | SHUTTER => 'Shutter', | ||
| 149 | IMAGE_STYLE => 'ImageStyle', | ||
| 150 | AE_METERING => 'AEMetering', | ||
| 151 | CAPTURE_MODE => 'CaptureMode', | ||
| 152 | HDR => 'HDR', | ||
| 153 | STITCHED => 'Stitched', | ||
| 154 | COVER_MODE => 'CoverMode', | ||
| 155 | STEREO => 'Stereo', | ||
| 156 | FOV => 'FOV', | ||
| 157 | AE_MODE => 'AEMode', | ||
| 158 | AF_FN => 'AF_FN', | ||
| 159 | AWB_MODE => 'AWBMode', | ||
| 160 | AUDIO_GAIN => 'AudioGain', | ||
| 161 | INTERVAL => 'Interval', | ||
| 162 | ISP_VER => 'ISPVersion', | ||
| 163 | YAW => 'Yaw', | ||
| 164 | FAN_LEVEL => 'FanLevel', | ||
| 165 | FAN_MODE => 'FanMode', | ||
| 166 | CUSTOMIZED => 'Customized', | ||
| 167 | ); | ||
| 168 | |||
| 169 | %Image::ExifTool::Kandao::GPS = ( | ||
| 170 | GROUPS => { 0 => 'Kandao', 1 => 'KVAR', 2 => 'Location' }, | ||
| 171 | PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData, | ||
| 172 | NOTES => q{ | ||
| 173 | These tags are in the family 1 KVAR group instead of a GPS group to allow | ||
| 174 | them to be distinguished from the duplicate GPS tags in the GPSX table. | ||
| 175 | }, | ||
| 176 | 0 => { Name => 'TimeStamp', Format => 'int32u' }, | ||
| 177 | 4 => { | ||
| 178 | Name => 'GPSLatitude', | ||
| 179 | Format => 'double', | ||
| 180 | ValueConv => 'Image::ExifTool::GPS::ToDegrees($val, 1)', | ||
| 181 | PrintConv => 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "N")', | ||
| 182 | }, | ||
| 183 | 12 => { | ||
| 184 | Name => 'GPSLongitude', | ||
| 185 | Format => 'double', | ||
| 186 | ValueConv => 'Image::ExifTool::GPS::ToDegrees($val, 1)', | ||
| 187 | PrintConv => 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "E")', | ||
| 188 | }, | ||
| 189 | 20 => { # (optional) | ||
| 190 | Name => 'GPSAltitude', | ||
| 191 | Format => 'double', | ||
| 192 | PrintConv => '$_ = sprintf("%.6f", $val); s/\.?0+$//; "$_ m"', | ||
| 193 | }, | ||
| 194 | ); | ||
| 195 | |||
| 196 | %Image::ExifTool::Kandao::GPSX = ( | ||
| 197 | GROUPS => { 0 => 'Kandao', 1 => 'GPS', 2 => 'Location' }, | ||
| 198 | PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData, | ||
| 199 | NOTES => q( | ||
| 200 | These tags are in the family 1 GPS group. They are duplicates of the tags | ||
| 201 | in the Kandao GPS table with the addition of GPSDateTime. | ||
| 202 | ), | ||
| 203 | 0 => { Name => 'TimeStamp', Format => 'int32u' }, | ||
| 204 | 4 => { | ||
| 205 | Name => 'GPSDateTime', | ||
| 206 | Description => 'GPS Date/Time', | ||
| 207 | Groups => { 2 => 'Time' }, | ||
| 208 | Format => 'int64u', | ||
| 209 | ValueConv => 'ConvertUnixTime($val/1000, 0, 3) . "Z"', | ||
| 210 | PrintConv => '$self->ConvertDateTime($val)', | ||
| 211 | }, | ||
| 212 | 12 => { | ||
| 213 | Name => 'GPSLatitude', | ||
| 214 | Format => 'double', | ||
| 215 | ValueConv => 'Image::ExifTool::GPS::ToDegrees($val, 1)', | ||
| 216 | PrintConv => 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "N")', | ||
| 217 | }, | ||
| 218 | 20 => { | ||
| 219 | Name => 'GPSLongitude', | ||
| 220 | Format => 'double', | ||
| 221 | ValueConv => 'Image::ExifTool::GPS::ToDegrees($val, 1)', | ||
| 222 | PrintConv => 'Image::ExifTool::GPS::ToDMS($self, $val, 1, "E")', | ||
| 223 | }, | ||
| 224 | 28 => { | ||
| 225 | Name => 'GPSAltitude', | ||
| 226 | Format => 'double', | ||
| 227 | PrintConv => '$_ = sprintf("%.6f", $val); s/\.?0+$//; "$_ m"', | ||
| 228 | }, | ||
| 229 | ); | ||
| 230 | |||
| 231 | # IMU gyroscope data | ||
| 232 | %Image::ExifTool::Kandao::IMU = ( | ||
| 233 | GROUPS => { 0 => 'Kandao', 1 => 'KVAR', 2 => 'Location' }, | ||
| 234 | PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData, | ||
| 235 | 0 => { Name => 'TimeStamp', Format => 'int64u' }, | ||
| 236 | 8 => { Name => 'Gyroscope', Format => 'int16s[3]' }, # 2G max range | ||
| 237 | 14=> { Name => 'Accelerometer', Format => 'int16s[3]' }, # 2000 deg/sec max range | ||
| 238 | # (Kandao docs mention optional 6-byte Magnetometer data here, but | ||
| 239 | # with no way to determine if it exists, and it isn't in my samples) | ||
| 240 | ); | ||
| 241 | |||
| 242 | # (ISP = Image Signal Processor?) | ||
| 243 | %Image::ExifTool::Kandao::FrameISP = ( | ||
| 244 | GROUPS => { 0 => 'Kandao', 1 => 'KVAR', 2 => 'Other' }, | ||
| 245 | PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData, | ||
| 246 | 0 => { Name => 'TimeStamp', Format => 'int32u', Unknown => 1 }, | ||
| 247 | 4 => { Name => 'FrameISP_4', Format => 'float[2]', Unknown => 1 }, | ||
| 248 | ); | ||
| 249 | |||
| 250 | #------------------------------------------------------------------------------ | ||
| 251 | # Extract information from a Kandao 'kfix' and 'kvar' atoms | ||
| 252 | # Inputs: 0) ExifTool ref, 1) dirInfo reference, 2) tag table ref | ||
| 253 | # Returns: 1 on success | ||
| 254 | sub ProcessKandao($$$) | ||
| 255 | { | ||
| 256 | my ($et, $dirInfo, $tagTbl) = @_; | ||
| 257 | my $raf = $$dirInfo{RAF}; | ||
| 258 | $raf or $raf = File::RandomAccess->new($$dirInfo{DataPt}); | ||
| 259 | my $dirName = $$dirInfo{DirName}; | ||
| 260 | my $dataPos = ($$dirInfo{DataPos} || 0) + ($$dirInfo{Base} || 0); | ||
| 261 | my $verbose = $et->Options('Verbose'); | ||
| 262 | my $ee = $et->Options('ExtractEmbedded'); | ||
| 263 | # extract as a block from MP4 if specified | ||
| 264 | if ($$dirInfo{BlockInfo}) { | ||
| 265 | my $blockName = $$dirInfo{BlockInfo}{Name}; | ||
| 266 | my $blockExtract = $et->Options('BlockExtract'); | ||
| 267 | if (($blockExtract or $$et{REQ_TAG_LOOKUP}{lc $blockName} or | ||
| 268 | ($$et{TAGS_FROM_FILE} and not $$et{EXCL_TAG_LOOKUP}{lc $blockName}))) | ||
| 269 | { | ||
| 270 | $et->FoundTag($$dirInfo{BlockInfo}, $$dirInfo{DataPt}); | ||
| 271 | return 1 if $blockExtract and $blockExtract > 1; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | my ($buff, $err, $i, $tag, $fmt); | ||
| 275 | SetByteOrder('II'); | ||
| 276 | $raf->Read($buff, 4) == 4 or return 0; | ||
| 277 | my $n = Get32u(\$buff, 0); | ||
| 278 | $et->VerboseDir($dirName, $n); | ||
| 279 | for ($i=0; $i<$n; ++$i) { | ||
| 280 | $raf->Read($buff, 0x2c) == 0x2c or $err = '', last; | ||
| 281 | ($tag = substr($buff, 0, 0x20)) =~ s/\0+$//; | ||
| 282 | ($fmt = substr($buff, 0x20, 8)) =~ s/\0+$//; | ||
| 283 | my $format = $format{$fmt}; | ||
| 284 | $format or $err = "Unknown Kandao format '${fmt}'", last; | ||
| 285 | my $num = Get32u(\$buff, 0x28); | ||
| 286 | my $size = $num * Image::ExifTool::FormatSize($format); | ||
| 287 | my %parms = ( | ||
| 288 | DataPt => \$buff, | ||
| 289 | DataPos => $dataPos + $raf->Tell(), | ||
| 290 | Index => $i, | ||
| 291 | Format => $format, | ||
| 292 | ); | ||
| 293 | $raf->Read($buff, $size) == $size or $err = '', last; | ||
| 294 | my $tagInfo = $et->GetTagInfo($tagTbl, $tag, \$buff); | ||
| 295 | unless ($tagInfo) { | ||
| 296 | my $name = ucfirst lc $tag; | ||
| 297 | $name =~ s/_([a-z])/_\u$1/g; | ||
| 298 | $name = Image::ExifTool::MakeTagName($name); | ||
| 299 | $et->VPrint(0, "$$et{INDENT}\[adding $dirName '${tag}']\n"); | ||
| 300 | $tagInfo = AddTagToTable($tagTbl, $tag, { Name => $name }); | ||
| 301 | } | ||
| 302 | my $recLen = $$tagInfo{RecordSize}; | ||
| 303 | if ($recLen) { | ||
| 304 | $verbose and $et->VerboseInfo($tag, $tagInfo, %parms); | ||
| 305 | my $tbl = GetTagTable($$tagInfo{SubDirectory}{TagTable}); | ||
| 306 | my $pt = $$tagInfo{SubDirectory}{Start} || 0; | ||
| 307 | my %dirInfo = ( | ||
| 308 | DataPt => \$buff,, | ||
| 309 | DataPos => $parms{DataPos}, | ||
| 310 | DirLen => $recLen, | ||
| 311 | DirName => $tag, | ||
| 312 | ); | ||
| 313 | my $nLimit = 10000; | ||
| 314 | my $sizeLimit = $size; | ||
| 315 | if ($ee and int(($size - $pt) / $recLen) > $nLimit and | ||
| 316 | $et->Warn("Extracting only the first $nLimit $$tagInfo{Name} records", 2)) | ||
| 317 | { | ||
| 318 | $sizeLimit = $pt + $recLen * $nLimit; | ||
| 319 | } | ||
| 320 | while ($pt+$recLen <= $sizeLimit) { | ||
| 321 | $$et{DOC_NUM} = ++$$et{DOC_COUNT}; | ||
| 322 | $dirInfo{DirStart} = $pt; | ||
| 323 | $et->ProcessDirectory(\%dirInfo, $tbl); | ||
| 324 | $ee or $et->Warn('Use the ExtractEmbedded option to extract all timed metadata',7), last; | ||
| 325 | $pt += $recLen; | ||
| 326 | } | ||
| 327 | delete $$et{DOC_NUM}; | ||
| 328 | } else { | ||
| 329 | my $val; | ||
| 330 | if ($fmt eq 'U8' and $num > 1 and not $$tagInfo{Format}) { | ||
| 331 | $val = \$buff; | ||
| 332 | } else { | ||
| 333 | # override format if necessary | ||
| 334 | if ($$tagInfo{Format}) { | ||
| 335 | $format = $$tagInfo{Format}; | ||
| 336 | $parms{Format} .= " read as $format"; | ||
| 337 | $num = int($size / Image::ExifTool::FormatSize($format)); | ||
| 338 | } | ||
| 339 | $val = ReadValue(\$buff, 0, $format, $num, $size); | ||
| 340 | } | ||
| 341 | my $key = $et->HandleTag($tagTbl, $tag, $val, %parms); | ||
| 342 | $et->SetGroup($key, $dirName) if $dirName ne 'KVAR'; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | $et->Warn($err || "Truncated $dirName record") if defined $err; | ||
| 346 | return 1; | ||
| 347 | } | ||
| 348 | |||
| 349 | #------------------------------------------------------------------------------ | ||
| 350 | # Process Kandao KVAR file | ||
| 351 | # Inputs: 0) ExifTool ref, 1) dirInfo reference | ||
| 352 | # Returns: 1 on success, 0 if this wasn't a valid KVAR file | ||
| 353 | sub ProcessKVAR($$) | ||
| 354 | { | ||
| 355 | my ($et, $dirInfo) = @_; | ||
| 356 | my $raf = $$dirInfo{RAF}; | ||
| 357 | my ($buff, $tagTablePtr); | ||
| 358 | |||
| 359 | # verify this is a valid KVAR file | ||
| 360 | return 0 unless $raf->Read($buff, 44) == 44; | ||
| 361 | return 0 unless $buff =~ /^.{2}\0\0[A-Z].{31}(CHAR|BOOL|[US](8|16|32|64)|FLOAT|DOUBLE)\0/s; | ||
| 362 | $et->SetFileType('KVAR', 'application/x-kandaostudio'); | ||
| 363 | $raf->Seek(0,0) or $et->Warn('Seek error'), return 1; | ||
| 364 | $$dirInfo{DirName} = 'KVAR'; | ||
| 365 | $tagTablePtr = GetTagTable('Image::ExifTool::Kandao::Main'); | ||
| 366 | return $et->ProcessDirectory($dirInfo, $tagTablePtr); | ||
| 367 | } | ||
| 368 | |||
| 369 | 1; # end | ||
| 370 | |||
| 371 | __END__ |