[FFmpeg-devel] [PATCH 6/7] fftools/ffmpeg: support applying container level cropping
James Almer
jamrial at gmail.com
Sat Oct 7 19:25:02 EEST 2023
Signed-off-by: James Almer <jamrial at gmail.com>
---
fftools/ffmpeg.h | 3 +++
fftools/ffmpeg_demux.c | 6 ++++++
fftools/ffmpeg_enc.c | 24 +++++++++++++-----------
fftools/ffmpeg_filter.c | 23 +++++++++++++++++++++++
fftools/ffmpeg_opt.c | 3 +++
5 files changed, 48 insertions(+), 11 deletions(-)
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index b059ecbb9f..94ada9d586 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -155,6 +155,8 @@ typedef struct OptionsContext {
int nb_hwaccel_output_formats;
SpecifierOpt *autorotate;
int nb_autorotate;
+ SpecifierOpt *apply_cropping;
+ int nb_apply_cropping;
/* output options */
StreamMap *stream_maps;
@@ -352,6 +354,7 @@ typedef struct InputStream {
#endif
int autorotate;
+ int apply_cropping;
int fix_sub_duration;
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index c71edf01a5..53fe584959 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -48,6 +48,7 @@ static const char *const opt_name_hwaccels[] = {"hwaccel", NULL
static const char *const opt_name_hwaccel_devices[] = {"hwaccel_device", NULL};
static const char *const opt_name_hwaccel_output_formats[] = {"hwaccel_output_format", NULL};
static const char *const opt_name_autorotate[] = {"autorotate", NULL};
+static const char *const opt_name_apply_cropping[] = {"apply_cropping", NULL};
static const char *const opt_name_display_rotations[] = {"display_rotation", NULL};
static const char *const opt_name_display_hflips[] = {"display_hflip", NULL};
static const char *const opt_name_display_vflips[] = {"display_vflip", NULL};
@@ -1068,6 +1069,11 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
ist->autorotate = 1;
MATCH_PER_STREAM_OPT(autorotate, i, ist->autorotate, ic, st);
+ ist->apply_cropping = 1;
+ MATCH_PER_STREAM_OPT(apply_cropping, i, ist->apply_cropping, ic, st);
+
+ av_dict_set_int(&o->g->codec_opts, "apply_cropping", ist->apply_cropping, 0);
+
MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, ic, st);
if (codec_tag) {
uint32_t tag = strtol(codec_tag, &next, 0);
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index be35465c28..27dd28172c 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -467,17 +467,19 @@ int enc_open(OutputStream *ost, AVFrame *frame)
if (ist) {
int i;
for (i = 0; i < ist->st->codecpar->nb_coded_side_data; i++) {
- AVPacketSideData *sd_src = &ist->st->codecpar->coded_side_data[i];
- if (sd_src->type != AV_PKT_DATA_CPB_PROPERTIES) {
- AVPacketSideData *sd_dst = av_packet_side_data_new(&ost->par_in->coded_side_data,
- &ost->par_in->nb_coded_side_data,
- sd_src->type, sd_src->size, 0);
- if (!sd_dst)
- return AVERROR(ENOMEM);
- memcpy(sd_dst->data, sd_src->data, sd_src->size);
- if (ist->autorotate && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
- av_display_rotation_set((int32_t *)sd_dst->data, 0);
- }
+ AVPacketSideData *sd_dst, *sd_src = &ist->st->codecpar->coded_side_data[i];
+ if (sd_src->type == AV_PKT_DATA_CPB_PROPERTIES)
+ continue;
+ if (ist->apply_cropping && sd_src->type == AV_PKT_DATA_FRAME_CROPPING)
+ continue;
+ sd_dst = av_packet_side_data_new(&ost->par_in->coded_side_data,
+ &ost->par_in->nb_coded_side_data,
+ sd_src->type, sd_src->size, 0);
+ if (!sd_dst)
+ return AVERROR(ENOMEM);
+ memcpy(sd_dst->data, sd_src->data, sd_src->size);
+ if (ist->autorotate && sd_src->type == AV_PKT_DATA_DISPLAYMATRIX)
+ av_display_rotation_set((int32_t *)sd_dst->data, 0);
}
}
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index b71a84185a..d17b9fc45a 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -31,6 +31,7 @@
#include "libavutil/bprint.h"
#include "libavutil/channel_layout.h"
#include "libavutil/display.h"
+#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/pixfmt.h"
@@ -1381,6 +1382,28 @@ static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
desc = av_pix_fmt_desc_get(ifp->format);
av_assert0(desc);
+ if (ist->apply_cropping) {
+ AVPacketSideData *sd = av_packet_side_data_get(ist->st->codecpar->coded_side_data,
+ ist->st->codecpar->nb_coded_side_data,
+ AV_PKT_DATA_FRAME_CROPPING);
+
+ if (sd->data && sd->size == sizeof(uint32_t) * 4) {
+ char crop_buf[64];
+ int top = AV_RL32(sd->data + 0);
+ int bottom = AV_RL32(sd->data + 4);
+ int left = AV_RL32(sd->data + 8);
+ int right = AV_RL32(sd->data + 12);
+
+ if (top < 0 || bottom < 0 || left < 0 || right < 0)
+ return AVERROR(EINVAL);
+
+ snprintf(crop_buf, sizeof(crop_buf), "w=iw-%d-%d:h=ih-%d-%d", left, right, top, 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
if (ist->autorotate && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
const AVPacketSideData *sd = NULL;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 304471dd03..1ef131aa3a 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1737,6 +1737,9 @@ const OptionDef options[] = {
{ "autorotate", HAS_ARG | OPT_BOOL | OPT_SPEC |
OPT_EXPERT | OPT_INPUT, { .off = OFFSET(autorotate) },
"automatically insert correct rotate filters" },
+ { "apply_cropping", HAS_ARG | OPT_BOOL | OPT_SPEC |
+ OPT_EXPERT | OPT_INPUT, { .off = OFFSET(apply_cropping) },
+ "Apply frame cropping instead of exporting it" },
{ "autoscale", HAS_ARG | OPT_BOOL | OPT_SPEC |
OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(autoscale) },
"automatically insert a scale filter at the end of the filter graph" },
--
2.42.0
More information about the ffmpeg-devel
mailing list