diff mbox series

[FFmpeg-devel,5/6,v3] fftools/ffmpeg: support applying container level cropping

Message ID 20240702164936.6140-1-jamrial@gmail.com
State New
Headers show
Series None | expand

Commit Message

James Almer July 2, 2024, 4:49 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 doc/ffmpeg.texi         | 16 ++++++++++++++++
 fftools/ffmpeg.h        | 15 +++++++++++++++
 fftools/ffmpeg_demux.c  | 26 ++++++++++++++++++++++++++
 fftools/ffmpeg_filter.c | 10 ++++++++++
 fftools/ffmpeg_opt.c    | 25 +++++++++++++++++++++++++
 5 files changed, 92 insertions(+)
diff mbox series

Patch

diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index f25f6192eb..f75ed681cf 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -1262,6 +1262,22 @@  disabled, all output frames of filter graph might not be in the same resolution
 and may be inadequate for some encoder/muxer. Therefore, it is not recommended
 to disable it unless you really know what you are doing.
 Disable autoscale at your own risk.
+
+@item -apply_cropping
+Automatically crop the video according to file metadata. Default is @emph{all}.
+
+@table @option
+@item none (0)
+Don't apply any cropping metadata.
+@item all (1)
+Apply both codec and container level croppping. This is the default mode.
+@item codec (2)
+Apply codec level croppping.
+@item container (3)
+Apply container level croppping.
+
+@end table
+
 @end table
 
 @section Advanced Video options
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index fe75706afd..810a5fa66c 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -155,6 +155,7 @@  typedef struct OptionsContext {
     SpecifierOptList hwaccel_devices;
     SpecifierOptList hwaccel_output_formats;
     SpecifierOptList autorotate;
+    SpecifierOptList apply_cropping;
 
     /* output options */
     StreamMap *stream_maps;
@@ -239,6 +240,7 @@  enum IFilterFlags {
     IFILTER_FLAG_AUTOROTATE     = (1 << 0),
     IFILTER_FLAG_REINIT         = (1 << 1),
     IFILTER_FLAG_CFR            = (1 << 2),
+    IFILTER_FLAG_CROP           = (1 << 3),
 };
 
 typedef struct InputFilterOptions {
@@ -254,6 +256,11 @@  typedef struct InputFilterOptions {
      * accurate */
     AVRational          framerate;
 
+    unsigned            crop_top;
+    unsigned            crop_bottom;
+    unsigned            crop_left;
+    unsigned            crop_right;
+
     int                 sub2video_width;
     int                 sub2video_height;
 
@@ -539,6 +546,13 @@  typedef struct KeyframeForceCtx {
 
 typedef struct Encoder Encoder;
 
+enum CroppingType {
+    CROP_DISABLED = 0,
+    CROP_ALL,
+    CROP_CODEC,
+    CROP_CONTAINER,
+};
+
 typedef struct OutputStream {
     const AVClass *class;
 
@@ -715,6 +729,7 @@  AVDictionary *strip_specifiers(const AVDictionary *dict);
 int find_codec(void *logctx, const char *name,
                enum AVMediaType type, int encoder, const AVCodec **codec);
 int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
+int parse_and_set_cropping(const char *arg, int *out);
 
 int filtergraph_is_simple(const FilterGraph *fg);
 int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 1ca8d804ae..409d41eba7 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -66,6 +66,7 @@  typedef struct DemuxStream {
     int                      have_sub2video;
     int                      reinit_filters;
     int                      autorotate;
+    int                      apply_cropping;
 
 
     int                      wrap_correction_done;
@@ -1000,11 +1001,22 @@  int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
     ist->filters[ist->nb_filters - 1] = ifilter;
 
     if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
+        const AVPacketSideData *sd = av_packet_side_data_get(ist->par->coded_side_data,
+                                                             ist->par->nb_coded_side_data,
+                                                             AV_PKT_DATA_FRAME_CROPPING);
         if (ist->framerate.num > 0 && ist->framerate.den > 0) {
             opts->framerate = ist->framerate;
             opts->flags |= IFILTER_FLAG_CFR;
         } else
             opts->framerate = av_guess_frame_rate(d->f.ctx, ist->st, NULL);
+        if (sd && sd->size >= sizeof(uint32_t) * 4) {
+            opts->crop_top    = AV_RL32(sd->data +  0);
+            opts->crop_bottom = AV_RL32(sd->data +  4);
+            opts->crop_left   = AV_RL32(sd->data +  8);
+            opts->crop_right  = AV_RL32(sd->data + 12);
+            if (ds->apply_cropping && ds->apply_cropping != CROP_CODEC)
+                opts->flags |= IFILTER_FLAG_CROP;
+        }
     } else if (ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE) {
         /* Compute the size of the canvas for the subtitles stream.
            If the subtitles codecpar has set a size, use it. Otherwise use the
@@ -1215,6 +1227,7 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
     InputStream *ist;
     char *framerate = NULL, *hwaccel_device = NULL;
     const char *hwaccel = NULL;
+    const char *apply_cropping = NULL;
     char *hwaccel_output_format = NULL;
     char *codec_tag = NULL;
     char *bsfs = NULL;
@@ -1241,6 +1254,16 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
     ds->autorotate = 1;
     MATCH_PER_STREAM_OPT(autorotate, i, ds->autorotate, ic, st);
 
+    ds->apply_cropping = CROP_ALL;
+    MATCH_PER_STREAM_OPT(apply_cropping, str, apply_cropping, ic, st);
+    if (apply_cropping) {
+        ret = parse_and_set_cropping(apply_cropping, &ds->apply_cropping);
+        if (ret < 0) {
+            av_log(ist, AV_LOG_ERROR, "Invalid apply_cropping value '%s'.\n", apply_cropping);
+            return ret;
+        }
+    }
+
     MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
     if (codec_tag) {
         uint32_t tag = strtol(codec_tag, &next, 0);
@@ -1362,6 +1385,9 @@  static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
 
     ds->dec_opts.flags |= DECODER_FLAG_BITEXACT * !!o->bitexact;
 
+    av_dict_set_int(&ds->decoder_opts, "apply_cropping",
+                    ds->apply_cropping && ds->apply_cropping != CROP_CONTAINER, 0);
+
     /* Attached pics are sparse, therefore we would not want to delay their decoding
      * till EOF. */
     if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 12cca684b4..f3087afc88 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -1701,6 +1701,16 @@  static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     desc = av_pix_fmt_desc_get(ifp->format);
     av_assert0(desc);
 
+    if ((ifp->opts.flags & IFILTER_FLAG_CROP)) {
+        char crop_buf[64];
+        snprintf(crop_buf, sizeof(crop_buf), "w=iw-%d-%d:h=ih-%d-%d",
+                 ifp->opts.crop_left, ifp->opts.crop_right,
+                 ifp->opts.crop_top, ifp->opts.crop_bottom);
+        ret = insert_filter(&last_filter, &pad_idx, "crop", crop_buf);
+        if (ret < 0)
+            return ret;
+    }
+
     // TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
     ifp->displaymatrix_applied = 0;
     if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) &&
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 910e4a336b..fb104a356d 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -217,6 +217,28 @@  int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_id
     return 0;
 }
 
+int parse_and_set_cropping(const char *arg, int *out)
+{
+    const AVOption opts[] = {
+        { "apply_cropping", NULL, 0, AV_OPT_TYPE_INT,
+                { .i64 = CROP_ALL }, CROP_DISABLED, CROP_CONTAINER, AV_OPT_FLAG_DECODING_PARAM, .unit = "apply_cropping" },
+            { "none",      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_DISABLED  }, .unit = "apply_cropping" },
+            { "all",       NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_ALL       }, .unit = "apply_cropping" },
+            { "codec",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CODEC     }, .unit = "apply_cropping" },
+            { "container", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CROP_CONTAINER }, .unit = "apply_cropping" },
+        { NULL },
+    };
+    const AVClass class = {
+        .class_name = "apply_cropping",
+        .item_name  = av_default_item_name,
+        .option     = opts,
+        .version    = LIBAVUTIL_VERSION_INT,
+    };
+    const AVClass *pclass = &class;
+
+    return av_opt_eval_int(&pclass, opts, arg, out);
+}
+
 /* Correct input file start times based on enabled streams */
 static void correct_input_start_times(void)
 {
@@ -1732,6 +1754,9 @@  const OptionDef options[] = {
     { "autoscale",                  OPT_TYPE_BOOL,   OPT_PERSTREAM | OPT_EXPERT | OPT_OUTPUT,
         { .off = OFFSET(autoscale) },
         "automatically insert a scale filter at the end of the filter graph" },
+    { "apply_cropping",             OPT_TYPE_STRING, OPT_VIDEO | OPT_PERSTREAM | OPT_EXPERT | OPT_INPUT,
+        { .off = OFFSET(apply_cropping) },
+        "select the cropping to apply" },
     { "fix_sub_duration_heartbeat", OPT_TYPE_BOOL,   OPT_VIDEO | OPT_EXPERT | OPT_PERSTREAM | OPT_OUTPUT,
         { .off = OFFSET(fix_sub_duration_heartbeat) },
         "set this video output stream to be a heartbeat stream for "