[FFmpeg-devel] [PATCH] libavformat\matroskadec.c: crop support for matroska demuxer.

OvchinnikovDmitrii ovchinnikov.dmitrii at gmail.com
Wed Sep 21 14:29:05 EEST 2022


In webm specification, it supports cropping information. (https://www.webmproject.org/docs/container/)
In ffmpeg, the implementation of webm is a subset of matroska. In matroskadec.c, those cropping related four fields are forced to 0.

for the sample file with crop (crop_bottom =8, crop_top=crop_left=crop_right=0.)
ffmpeg.exe -i  test_with_container_crop.webm -pix_fmt yuv420p -y output.yuv

original ffmpeg code - the output.yuv resolution is 1920x1088
changed code - the output.yuv resolution is 1920x1080"
---
 libavcodec/avcodec.h      |  8 ++++++++
 libavcodec/codec_par.c    |  8 ++++++++
 libavcodec/codec_par.h    |  8 ++++++++
 libavcodec/decode.c       |  9 +++++++++
 libavformat/matroskadec.c | 17 +++++++++++++----
 5 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 7db5d1b1c5..4420cfa0c9 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -585,6 +585,14 @@ typedef struct AVCodecContext {
      */
     int coded_width, coded_height;
 
+    /**
+     * The dimensions of the crop, usually from container.
+     */
+    int crop_top;
+    int crop_left;
+    int crop_bottom;
+    int crop_right;
+
     /**
      * the number of pictures in a group of pictures, or 0 for intra_only
      * - encoding: Set by user.
diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c
index abda649aa8..f74964a817 100644
--- a/libavcodec/codec_par.c
+++ b/libavcodec/codec_par.c
@@ -118,6 +118,10 @@ int avcodec_parameters_from_context(AVCodecParameters *par,
         par->format              = codec->pix_fmt;
         par->width               = codec->width;
         par->height              = codec->height;
+        par->crop_top           = codec->crop_top;
+        par->crop_left          = codec->crop_left;
+        par->crop_bottom        = codec->crop_bottom;
+        par->crop_right         = codec->crop_right;
         par->field_order         = codec->field_order;
         par->color_range         = codec->color_range;
         par->color_primaries     = codec->color_primaries;
@@ -199,6 +203,10 @@ int avcodec_parameters_to_context(AVCodecContext *codec,
         codec->pix_fmt                = par->format;
         codec->width                  = par->width;
         codec->height                 = par->height;
+        codec->crop_top               = par->crop_top;
+        codec->crop_left              = par->crop_left;
+        codec->crop_bottom            = par->crop_bottom;
+        codec->crop_right             = par->crop_right;
         codec->field_order            = par->field_order;
         codec->color_range            = par->color_range;
         codec->color_primaries        = par->color_primaries;
diff --git a/libavcodec/codec_par.h b/libavcodec/codec_par.h
index 7660791a12..6671f1810b 100644
--- a/libavcodec/codec_par.h
+++ b/libavcodec/codec_par.h
@@ -127,6 +127,14 @@ typedef struct AVCodecParameters {
     int width;
     int height;
 
+    /**
+     * The dimensions of the crop, usually from container.
+     */
+    int crop_top;
+    int crop_left;
+    int crop_bottom;
+    int crop_right;
+
     /**
      * Video only. The aspect ratio (width / height) which a single pixel
      * should have when displayed.
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 2961705c9d..905eb8401e 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -324,6 +324,15 @@ static inline int decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,
     emms_c();
     actual_got_frame = got_frame;
 
+    if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
+        if (avctx->crop_top != 0 || avctx->crop_left != 0 || avctx->crop_right != 0 || avctx->crop_bottom != 0){
+            frame->crop_top     = avctx->crop_top;
+            frame->crop_left    = avctx->crop_left;
+            frame->crop_right   = avctx->crop_right;
+            frame->crop_bottom  = avctx->crop_bottom;
+        }
+    }
+
     if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
         if (frame->flags & AV_FRAME_FLAG_DISCARD)
             got_frame = 0;
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index 16a3e93611..6e05b36e58 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -209,6 +209,10 @@ typedef struct MatroskaTrackVideo {
     uint64_t pixel_width;
     uint64_t pixel_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;
@@ -516,10 +520,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_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_VIDEOPIXELCROPB,     EBML_UINT,  0, 0, offsetof(MatroskaTrackVideo, pixel_cropb), {.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 } },
@@ -2878,6 +2882,11 @@ static int matroska_parse_tracks(AVFormatContext *s)
             st->codecpar->width      = track->video.pixel_width;
             st->codecpar->height     = track->video.pixel_height;
 
+            st->codecpar->crop_top   = track->video.pixel_cropt;
+            st->codecpar->crop_left  = track->video.pixel_cropl;
+            st->codecpar->crop_bottom= track->video.pixel_cropb;
+            st->codecpar->crop_right = track->video.pixel_cropr;
+
             if (track->video.interlaced == MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED)
                 st->codecpar->field_order = mkv_field_order(matroska, track->video.field_order);
             else if (track->video.interlaced == MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE)
-- 
2.30.0.windows.2



More information about the ffmpeg-devel mailing list