From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH v2 5/5] fftools/ffmpeg: support applying container level cropping
Date: Tue, 25 Jul 2023 14:09:39 -0300
Message-ID: <20230725170939.3285-1-jamrial@gmail.com> (raw)
In-Reply-To: <20230719222043.59743-1-jamrial@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
---
Now inserting a filter into the graph.
fftools/ffmpeg.h | 3 +++
fftools/ffmpeg_demux.c | 6 ++++++
fftools/ffmpeg_enc.c | 19 +++++++++++--------
fftools/ffmpeg_filter.c | 22 ++++++++++++++++++++++
fftools/ffmpeg_opt.c | 3 +++
5 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d53181e427..bb6d510f10 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -154,6 +154,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;
@@ -347,6 +349,7 @@ typedef struct InputStream {
int top_field_first;
int autorotate;
+ int apply_cropping;
int fix_sub_duration;
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 48edbd7f6b..1209cf2046 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};
@@ -1085,6 +1086,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 96424272bf..cd30986344 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -448,14 +448,17 @@ int enc_open(OutputStream *ost, AVFrame *frame)
int i;
for (i = 0; i < ist->st->nb_side_data; i++) {
AVPacketSideData *sd = &ist->st->side_data[i];
- if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
- uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
- if (!dst)
- return AVERROR(ENOMEM);
- memcpy(dst, sd->data, sd->size);
- if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
- av_display_rotation_set((int32_t *)dst, 0);
- }
+ uint8_t *dst;
+ if (sd->type == AV_PKT_DATA_CPB_PROPERTIES)
+ continue;
+ if (ist->apply_cropping && sd->type == AV_PKT_DATA_FRAME_CROPPING)
+ continue;
+ dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
+ if (!dst)
+ return AVERROR(ENOMEM);
+ memcpy(dst, sd->data, sd->size);
+ if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
+ av_display_rotation_set((int32_t *)dst, 0);
}
}
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 925b5116cc..8cadb4732c 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"
@@ -1418,6 +1419,27 @@ static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
return ret;
}
+ if (ist->apply_cropping && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
+ size_t cropping_size;
+ uint8_t *cropping = av_stream_get_side_data(ist->st, AV_PKT_DATA_FRAME_CROPPING, &cropping_size);
+
+ if (cropping && cropping_size == sizeof(uint32_t) * 4) {
+ char crop_buf[64];
+ int top = AV_RL32(cropping + 0);
+ int bottom = AV_RL32(cropping + 4);
+ int left = AV_RL32(cropping + 8);
+ int right = AV_RL32(cropping + 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;
+ }
+ }
+
snprintf(name, sizeof(name), "trim_in_%d_%d",
ist->file_index, ist->index);
if (copy_ts) {
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index dc6044120a..30bb919cb6 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1733,6 +1733,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.41.0
_______________________________________________
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".
next prev parent reply other threads:[~2023-07-25 17:10 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-19 22:20 [FFmpeg-devel] [PATCH 1/5] avcodec/packet: add a decoded frame cropping side data type James Almer
2023-07-19 22:20 ` [FFmpeg-devel] [PATCH 2/5] avformat/dump: print Frame Cropping side data info James Almer
2023-07-19 22:20 ` [FFmpeg-devel] [PATCH 3/5] avformat/matroskadec: export cropping values James Almer
2023-07-19 22:20 ` [FFmpeg-devel] [PATCH 4/5] avformat/matroskaenc: support writing " James Almer
2023-07-19 22:20 ` [FFmpeg-devel] [PATCH 5/5] fftools/ffmpeg: support applying container level cropping James Almer
2023-07-20 19:08 ` Anton Khirnov
2023-07-20 19:25 ` James Almer
2023-07-20 19:31 ` Anton Khirnov
2023-07-20 19:39 ` James Almer
2023-07-25 17:09 ` James Almer [this message]
2023-07-26 21:42 ` [FFmpeg-devel] [PATCH v2 " Tomas Härdin
2023-07-26 22:11 ` James Almer
2023-07-27 11:07 ` Tomas Härdin
2023-07-27 11:13 ` Anton Khirnov
2023-07-27 11:59 ` James Almer
2023-07-31 15:53 ` Tomas Härdin
[not found] ` <3EF017F1-A8DB-4934-A86C-8E17BC067DA0@cosmin.at>
2023-08-01 18:51 ` Cosmin Stejerean
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=20230725170939.3285-1-jamrial@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