[FFmpeg-devel] [PATCH 3/5] avformat/matroskadec: export cropping values

James Almer jamrial at gmail.com
Thu Jul 20 01:20:41 EEST 2023


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/matroskadec.c | 49 +++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 49950956b6..46459b3dc8 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -211,7 +211,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;
@@ -525,10 +531,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 } },
@@ -2527,10 +2533,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;
             if (track->video.color_space.size == 4)
                 fourcc = AV_RL32(track->video.color_space.data);
         } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
@@ -2955,14 +2972,26 @@ static int matroska_parse_tracks(AVFormatContext *s)
 
             if (track->video.display_unit < MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN) {
                 if (track->video.display_width && track->video.display_height &&
-                    st->codecpar->height  < INT64_MAX / track->video.display_width  / display_width_mul &&
-                    st->codecpar->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,
-                              st->codecpar->height * track->video.display_width  * display_width_mul,
-                              st->codecpar->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 = av_stream_new_side_data(st, AV_PKT_DATA_FRAME_CROPPING, sizeof(uint32_t) * 4);
+                if (!cropping)
+                    return AVERROR(ENOMEM);
+
+                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 (st->codecpar->codec_id != AV_CODEC_ID_HEVC)
                 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
 
-- 
2.41.0



More information about the ffmpeg-devel mailing list