From: OvchinnikovDmitrii <ovchinnikov.dmitrii@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: OvchinnikovDmitrii <ovchinnikov.dmitrii@gmail.com>
Subject: [FFmpeg-devel] [crop support for matroska demuxer, V4 2/3] libavcodec: Public code to support container crop
Date: Fri, 4 Nov 2022 15:13:35 +0100
Message-ID: <20221104141336.1812-2-ovchinnikov.dmitrii@gmail.com> (raw)
In-Reply-To: <20221104141336.1812-1-ovchinnikov.dmitrii@gmail.com>
Support both simple and receive_frame api
The container crop information is applied additional to frame crop information
---
libavcodec/codec_par.c | 30 ++++++++++++++---------
libavcodec/decode.c | 54 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 11 deletions(-)
diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c
index abda649aa8..9738402434 100644
--- a/libavcodec/codec_par.c
+++ b/libavcodec/codec_par.c
@@ -115,17 +115,21 @@ int avcodec_parameters_from_context(AVCodecParameters *par,
switch (par->codec_type) {
case AVMEDIA_TYPE_VIDEO:
- par->format = codec->pix_fmt;
- par->width = codec->width;
- par->height = codec->height;
- par->field_order = codec->field_order;
- par->color_range = codec->color_range;
- par->color_primaries = codec->color_primaries;
- par->color_trc = codec->color_trc;
- par->color_space = codec->colorspace;
- par->chroma_location = codec->chroma_sample_location;
- par->sample_aspect_ratio = codec->sample_aspect_ratio;
- par->video_delay = codec->has_b_frames;
+ par->format = codec->pix_fmt;
+ par->width = codec->width;
+ par->height = codec->height;
+ par->container_crop_top = codec->container_crop_top;
+ par->container_crop_left = codec->container_crop_left;
+ par->container_crop_bottom = codec->container_crop_bottom;
+ par->container_crop_right = codec->container_crop_right;
+ par->field_order = codec->field_order;
+ par->color_range = codec->color_range;
+ par->color_primaries = codec->color_primaries;
+ par->color_trc = codec->color_trc;
+ par->color_space = codec->colorspace;
+ par->chroma_location = codec->chroma_sample_location;
+ par->sample_aspect_ratio = codec->sample_aspect_ratio;
+ par->video_delay = codec->has_b_frames;
break;
case AVMEDIA_TYPE_AUDIO:
par->format = codec->sample_fmt;
@@ -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->container_crop_top = par->container_crop_top;
+ codec->container_crop_left = par->container_crop_left;
+ codec->container_crop_bottom = par->container_crop_bottom;
+ codec->container_crop_right = par->container_crop_right;
codec->field_order = par->field_order;
codec->color_range = par->color_range;
codec->color_primaries = par->color_primaries;
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 6be2d3d6ed..9e44fcb293 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -693,6 +693,60 @@ static int apply_cropping(AVCodecContext *avctx, AVFrame *frame)
if (!avctx->apply_cropping)
return 0;
+ if (avctx->container_apply_cropping == FF_CONTAINER_CROPPING_ADDITION)
+ {
+ /*check if container parameter and elementary streaming cropping parameters are safe for applying */
+ if (avctx->container_crop_left + frame->crop_left>= INT_MAX - (avctx->container_crop_right + frame->crop_right) ||
+ avctx->container_crop_top + frame->crop_top >= INT_MAX - (avctx->container_crop_bottom + frame->crop_bottom) ||
+ (avctx->container_crop_left + frame->crop_left + avctx->container_crop_right + frame->crop_right) >= frame->width ||
+ (avctx->container_crop_top + frame->crop_top + avctx->container_crop_bottom + frame->crop_bottom) >= frame->height) {
+ av_log(avctx, AV_LOG_WARNING,
+ "Apply container and elementary stream corpping parametes error: "
+ "container cropping parameters"
+ "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
+ "elementary stream croping paramters"
+ "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
+ "(frame size %dx%d). This is a bug, please report it\n",
+ avctx->container_crop_left, avctx->container_crop_right, avctx->container_crop_top, avctx->container_crop_bottom,
+ frame->crop_left, frame->crop_right, frame->crop_top, frame->crop_bottom,
+ frame->width, frame->height);
+ frame->crop_left = 0;
+ frame->crop_right = 0;
+ frame->crop_top = 0;
+ frame->crop_bottom = 0;
+ return 0;
+ }
+
+ frame->crop_top += avctx->container_crop_top;
+ frame->crop_left += avctx->container_crop_left;
+ frame->crop_bottom += avctx->container_crop_bottom;
+ frame->crop_right += avctx->container_crop_right;
+ }else if (avctx->container_apply_cropping == FF_CONTAINER_CROPPING_OVERWRITE) {
+
+ /*check the croppping parameters from container are reasonable and correct*/
+ if (avctx->container_crop_left >= INT_MAX - avctx->container_crop_right ||
+ avctx->container_crop_top >= INT_MAX - avctx->container_crop_bottom ||
+ (avctx->container_crop_left + avctx->container_crop_right) >= frame->width ||
+ (avctx->container_crop_top + avctx->container_crop_bottom) >= frame->height) {
+ av_log(avctx, AV_LOG_WARNING,
+ "Invalid container cropping information set by a demuxer: "
+ "%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER"/%"SIZE_SPECIFIER" "
+ "(frame size %dx%d). This is a bug, please report it\n",
+ avctx->container_crop_left, avctx->container_crop_right, avctx->container_crop_top, avctx->container_crop_bottom,
+ frame->width, frame->height);
+ frame->crop_left = 0;
+ frame->crop_right = 0;
+ frame->crop_top = 0;
+ frame->crop_bottom = 0;
+ return 0;
+ }
+
+ frame->crop_top = avctx->container_crop_top;
+ frame->crop_left = avctx->container_crop_left;
+ frame->crop_bottom = avctx->container_crop_bottom;
+ frame->crop_right = avctx->container_crop_right;
+ }
+
return av_frame_apply_cropping(frame, avctx->flags & AV_CODEC_FLAG_UNALIGNED ?
AV_FRAME_CROP_UNALIGNED : 0);
}
--
2.30.0.windows.2
_______________________________________________
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:[~2022-11-04 14:13 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-04 14:13 [FFmpeg-devel] [crop support for matroska demuxer, V4 1/3] libavcodec: Add crop related fields to structure AVCodecContext and AVCodecParameters OvchinnikovDmitrii
2022-11-04 14:13 ` OvchinnikovDmitrii [this message]
2022-11-04 15:34 ` [FFmpeg-devel] [crop support for matroska demuxer, V4 2/3] libavcodec: Public code to support container crop Michael Niedermayer
2022-11-23 14:44 ` Dmitrii Ovchinnikov
2022-11-24 23:07 ` Michael Niedermayer
2022-11-04 14:13 ` [FFmpeg-devel] [crop support for matroska demuxer, V4 3/3] libavformat\matroskadec.c: crop support for matroska demuxer OvchinnikovDmitrii
2022-12-20 15:37 [FFmpeg-devel] [crop support for matroska demuxer, V4 1/3] libavcodec: Add crop related fields to structure AVCodecContext and AVCodecParameters Dmitrii Ovchinnikov
2022-12-20 15:37 ` [FFmpeg-devel] [crop support for matroska demuxer, V4 2/3] libavcodec: Public code to support container crop Dmitrii Ovchinnikov
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=20221104141336.1812-2-ovchinnikov.dmitrii@gmail.com \
--to=ovchinnikov.dmitrii@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