Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH 3/6] avformat/matroskadec: export cropping values
Date: Fri, 5 Jul 2024 17:51:09 -0300
Message-ID: <275f5bf7-dce1-44d7-90a2-52107439f5cc@gmail.com> (raw)
In-Reply-To: <CABPLASQNUVihE8PZofM04YbwZRQ2PGA5p60Xi3Rb5PeCG67Ywg@mail.gmail.com>

On 6/1/2024 8:24 AM, Kacper Michajlow wrote:
> On Wed, 29 May 2024 at 23:47, James Almer <jamrial@gmail.com> wrote:
>>
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   libavformat/matroskadec.c | 53 +++++++++++++++++++++++++++++++--------
>>   1 file changed, 43 insertions(+), 10 deletions(-)
>>
>> diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
>> index 2f07e11d87..a30bac786b 100644
>> --- a/libavformat/matroskadec.c
>> +++ b/libavformat/matroskadec.c
>> @@ -213,7 +213,13 @@ typedef struct MatroskaTrackVideo {
>>       uint64_t display_height;
>>       uint64_t pixel_width;
>>       uint64_t pixel_height;
>> +    uint64_t cropped_width;
>> +    uint64_t cropped_height;
>>       EbmlBin  color_space;
>> +    uint64_t pixel_cropt;
>> +    uint64_t pixel_cropl;
>> +    uint64_t pixel_cropb;
>> +    uint64_t pixel_cropr;
>>       uint64_t display_unit;
>>       uint64_t interlaced;
>>       uint64_t field_order;
>> @@ -527,10 +533,10 @@ static EbmlSyntax matroska_track_video[] = {
>>       { MATROSKA_ID_VIDEOALPHAMODE,      EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, alpha_mode), { .u = 0 } },
>>       { MATROSKA_ID_VIDEOCOLOR,          EBML_NEST,  0, sizeof(MatroskaTrackVideoColor), offsetof(MatroskaTrackVideo, color), { .n = matroska_track_video_color } },
>>       { MATROSKA_ID_VIDEOPROJECTION,     EBML_NEST,  0, 0, offsetof(MatroskaTrackVideo, projection), { .n = matroska_track_video_projection } },
>> -    { MATROSKA_ID_VIDEOPIXELCROPB,     EBML_NONE },
>> -    { MATROSKA_ID_VIDEOPIXELCROPT,     EBML_NONE },
>> -    { MATROSKA_ID_VIDEOPIXELCROPL,     EBML_NONE },
>> -    { MATROSKA_ID_VIDEOPIXELCROPR,     EBML_NONE },
>> +    { MATROSKA_ID_VIDEOPIXELCROPB,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_cropb), {.u = 0 } },
>> +    { MATROSKA_ID_VIDEOPIXELCROPT,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_cropt), {.u = 0 } },
>> +    { MATROSKA_ID_VIDEOPIXELCROPL,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_cropl), {.u = 0 } },
>> +    { MATROSKA_ID_VIDEOPIXELCROPR,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_cropr), {.u = 0 } },
>>       { MATROSKA_ID_VIDEODISPLAYUNIT,    EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, display_unit), { .u= MATROSKA_VIDEO_DISPLAYUNIT_PIXELS } },
>>       { MATROSKA_ID_VIDEOFLAGINTERLACED, EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, interlaced),  { .u = MATROSKA_VIDEO_INTERLACE_FLAG_UNDETERMINED } },
>>       { MATROSKA_ID_VIDEOFIELDORDER,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, field_order), { .u = MATROSKA_VIDEO_FIELDORDER_UNDETERMINED } },
>> @@ -2963,14 +2969,30 @@ static int mkv_parse_video(MatroskaTrack *track, AVStream *st,
>>
>>       if (track->video.display_unit < MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN) {
>>           if (track->video.display_width && track->video.display_height &&
>> -            par->height  < INT64_MAX / track->video.display_width  / display_width_mul &&
>> -            par->width   < INT64_MAX / track->video.display_height / display_height_mul)
>> +            track->video.cropped_height < INT64_MAX / track->video.display_width  / display_width_mul &&
>> +            track->video.cropped_width  < INT64_MAX / track->video.display_height / display_height_mul)
>>               av_reduce(&st->sample_aspect_ratio.num,
>>                         &st->sample_aspect_ratio.den,
>> -                      par->height * track->video.display_width  * display_width_mul,
>> -                      par->width  * track->video.display_height * display_height_mul,
>> +                      track->video.cropped_height * track->video.display_width  * display_width_mul,
>> +                      track->video.cropped_width  * track->video.display_height * display_height_mul,
>>                         INT_MAX);
>>       }
>> +    if (track->video.cropped_width  != track->video.pixel_width ||
>> +        track->video.cropped_height != track->video.pixel_height) {
>> +        uint8_t *cropping;
>> +        AVPacketSideData *sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
>> +                                                       &st->codecpar->nb_coded_side_data,
>> +                                                       AV_PKT_DATA_FRAME_CROPPING,
>> +                                                       sizeof(uint32_t) * 4, 0);
>> +        if (!sd)
>> +            return AVERROR(ENOMEM);
>> +
>> +        cropping = sd->data;
>> +        bytestream_put_le32(&cropping, track->video.pixel_cropt);
>> +        bytestream_put_le32(&cropping, track->video.pixel_cropb);
>> +        bytestream_put_le32(&cropping, track->video.pixel_cropl);
>> +        bytestream_put_le32(&cropping, track->video.pixel_cropr);
>> +    }
>>       if (par->codec_id != AV_CODEC_ID_HEVC)
>>           sti->need_parsing = AVSTREAM_PARSE_HEADERS;
>>
>> @@ -3136,10 +3158,21 @@ static int matroska_parse_tracks(AVFormatContext *s)
>>                       track->default_duration = default_duration;
>>                   }
>>               }
>> +            track->video.cropped_width  = track->video.pixel_width;
>> +            track->video.cropped_height = track->video.pixel_height;
>> +            if (track->video.display_unit == 0) {
>> +                if (track->video.pixel_cropl >= INT_MAX - track->video.pixel_cropr ||
>> +                    track->video.pixel_cropt >= INT_MAX - track->video.pixel_cropb ||
>> +                    (track->video.pixel_cropl + track->video.pixel_cropr) >= track->video.pixel_width ||
>> +                    (track->video.pixel_cropt + track->video.pixel_cropb) >= track->video.pixel_height)
>> +                    return AVERROR_INVALIDDATA;
>> +                track->video.cropped_width  -= track->video.pixel_cropl + track->video.pixel_cropr;
>> +                track->video.cropped_height -= track->video.pixel_cropt + track->video.pixel_cropb;
>> +            }
>>               if (track->video.display_width == -1)
>> -                track->video.display_width = track->video.pixel_width;
>> +                track->video.display_width = track->video.cropped_width;
>>               if (track->video.display_height == -1)
>> -                track->video.display_height = track->video.pixel_height;
>> +                track->video.display_height = track->video.cropped_height;
>>           } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
>>               if (!track->audio.out_samplerate)
>>                   track->audio.out_samplerate = track->audio.samplerate;
>> --
>> 2.45.1
> 
> Have you seen the discussion over the MKVToolNix issue tracker?
> https://gitlab.com/mbunkus/mkvtoolnix/-/issues/2389
> 
> I think it is the reason it wasn't implemented in ffmpeg sooner. The
> issue summarizes the problem and current status quo, quite well I
> think. I think the main problem is that vast majority of Matroska
> files doesn't are not following the standard with regards to
> DisplayWidth/Height.

The only thing i can think to do about this is adding an option to 
ignore display dimension fields and have the demuxer derive them as if 
they were not coded in. There's no amount of heuristics one could do to 
figure out if the values in display dimension fields are correct and 
intended (taking into account any cropping value that may be present, 
and/or trying to achieve a certain DAR), or if they are bogus.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  reply	other threads:[~2024-07-05 20:50 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-29 21:46 [FFmpeg-devel] [PATCH 1/6] avcodec/packet: add a decoded frame cropping side data type James Almer
2024-05-29 21:46 ` [FFmpeg-devel] [PATCH 2/6] avformat/dump: print Frame Cropping side data info James Almer
2024-05-29 21:46 ` [FFmpeg-devel] [PATCH 3/6] avformat/matroskadec: export cropping values James Almer
2024-06-01 11:24   ` Kacper Michajlow
2024-07-05 20:51     ` James Almer [this message]
2024-05-29 21:46 ` [FFmpeg-devel] [PATCH 4/6] avformat/matroskaenc: support writing " James Almer
2024-05-29 21:46 ` [FFmpeg-devel] [PATCH 5/6] fftools/ffmpeg: support applying container level cropping James Almer
2024-05-30  1:01   ` Lynne via ffmpeg-devel
2024-05-30  1:04     ` James Almer
2024-05-31  1:13       ` Lynne via ffmpeg-devel
2024-05-30 22:57   ` Michael Niedermayer
2024-05-30 23:22     ` [FFmpeg-devel] [PATCH 1/2 v2] " James Almer
2024-06-25 10:25       ` Anton Khirnov
2024-06-25 12:38         ` James Almer
2024-06-25 13:12           ` Anton Khirnov
2024-06-25 13:23             ` Paul B Mahol
2024-06-25 13:54               ` Anton Khirnov
2024-06-25 10:19   ` [FFmpeg-devel] [PATCH 5/6] " Anton Khirnov
2024-07-02 16:49     ` [FFmpeg-devel] [PATCH 5/6 v3] " James Almer
2024-07-02 17:55       ` Anton Khirnov
2024-07-02 18:43         ` James Almer
2024-07-02 19:00           ` Anton Khirnov
2024-05-29 21:46 ` [FFmpeg-devel] [PATCH 6/6] fftools/ffplay: " James Almer
2024-05-30  1:02 ` [FFmpeg-devel] [PATCH 1/6] avcodec/packet: add a decoded frame cropping side data type Lynne via ffmpeg-devel
2024-05-30  1:08   ` James Almer
2024-06-25 18:13 ` Andreas Rheinhardt
2024-06-25 18:17   ` James Almer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=275f5bf7-dce1-44d7-90a2-52107439f5cc@gmail.com \
    --to=jamrial@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git