#------------------------------------------------------------------------------
# File:         frameCount.config
#
# Description:  ExifTool config file to extract MP4 video FrameCount
#
# Usage:        exiftool -config frameCount.config -frameCount FILE
#
# Requires:     ExifTool version 7.99 or later
#
# Revisions:    2022-09-22 - P. Harvey Created
#
# Notes:        Enables Unknown option to extract the required SampleSizes atom
#------------------------------------------------------------------------------

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        'FrameCount' => {
            Require => {
                0 => 'HandlerType',
            },
            Desire => {
                1 => 'SampleSizes',
                2 => 'CompactSampleSizes',
            },
            Groups => { 2 => 'Video' },
            ValueConv => q{
                my ($i, $tag, $ptr);
                my $key = 'HandlerType';
                # find video track number
                for ($i=1; defined $val[0]; ++$i) {
                    last if $val[0] eq 'Video Track';
                    $key = "HandlerType ($i)";
                    $val[0] = $self->GetValue($key);
                }
                my $trk = $self->GetGroup($key, 1);
                # search for SampleSizes or CompactSampleSizes for this track
                foreach $tag ('SampleSizes', 'CompactSampleSizes') {
                    $key = $tag;
                    for ($i=1; ; ++$i) {
                        $ptr = $self->GetValue($key);
                        last unless $ptr;
                        last if $self->GetGroup($key, 1) eq $trk;
                        $key = "$tag ($i)";
                    }
                    last if $ptr;
                }
                return undef unless $ptr;
                return unpack('x8N', $$ptr);
            },
        },
    },
);

%Image::ExifTool::UserDefined::Options = (
    Unknown => 1,   # (otherwise SampleSizes won't be extracted)
);
