[FFmpeg-devel] [PATCH v2 5/7] avcodec/mediacodecenc: remove the strategy to create DTS

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

Use input PTS as DTS has multiple problems:
1. If there is no reordering, it's better to just use the output
PTS as DTS, since encoder may change the timestamp value (do it
on purpose or rounding error).

2. If there is reordering, input PTS should be shift a few frames
as DTS to satisfy the requirement of PTS >= DTS. I can't find a
reliable way to determine how many frames to be shift. For example,
we don't known if the encoder use hierarchical B frames. The
max_num_reorder_frames can be get from VUI, but VUI is optional.

3. Encoder dropping frames makes the case worse. Android has an
BITRATE_MODE_CBR_FD option to allow it explicitly.
---
 libavcodec/mediacodecenc.c | 28 +---
 1 file changed, 1 insertion(+), 27 deletions(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 7f2ae88285..8e28a50e0d 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -64,18 +64,6 @@ typedef struct MediaCodecEncContext {
 
 uint8_t *extradata;
 int extradata_size;
-
-// Since MediaCodec doesn't output DTS, use a timestamp queue to save pts
-// of AVFrame and generate DTS for AVPacket.
-//
-// This doesn't work when use Surface as input, in that case frames can be
-// sent to encoder without our notice. One exception is frames come from
-// our MediaCodec decoder wrapper, since we can control it's render by
-// av_mediacodec_release_buffer.
-int64_t timestamps[32];
-int ts_head;
-int ts_tail;
-
 int eof_sent;
 
 AVFrame *frame;
@@ -368,11 +356,6 @@ static int mediacodec_receive(AVCodecContext *avctx,
 }
 memcpy(pkt->data + extradata_size, out_buf + out_info.offset, 
out_info.size);
 pkt->pts = av_rescale_q(out_info.presentationTimeUs, AV_TIME_BASE_Q, 
avctx->time_base);
-if (s->ts_tail != s->ts_head) {
-pkt->dts = s->timestamps[s->ts_tail];
-s->ts_tail = (s->ts_tail + 1) % FF_ARRAY_ELEMS(s->timestamps);
-}
-
 if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec))
 pkt->flags |= AV_PKT_FLAG_KEY;
 ret = 0;
@@ -437,14 +420,8 @@ static int mediacodec_send(AVCodecContext *avctx,
 return ff_AMediaCodec_signalEndOfInputStream(codec);
 }
 
-
-if (frame->data[3]) {
-pts = av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q);
-s->timestamps[s->ts_head] = frame->pts;
-s->ts_head = (s->ts_head + 1) % FF_ARRAY_ELEMS(s->timestamps);
-
+if (frame->data[3])
 av_mediacodec_release_buffer((AVMediaCodecBuffer *)frame->data[3], 
1);
-}
 return 0;
 }
 
@@ -463,9 +440,6 @@ static int mediacodec_send(AVCodecContext *avctx,
 copy_frame_to_buffer(avctx, frame, input_buf, input_size);
 
 pts = av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q);
-
-s->timestamps[s->ts_head] = frame->pts;
-s->ts_head = (s->ts_head + 1) % FF_ARRAY_ELEMS(s->timestamps);
 } else {
 flags |= ff_AMediaCodec_getBufferFlagEndOfStream(codec);
 s->eof_sent = 1;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 2/7] avcodec/mediacodecenc: add bitrate_mode option

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

---
 libavcodec/mediacodecenc.c | 25 +
 libavcodec/version.h   |  2 +-
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 4f9185342f..ec0e0b3a86 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -39,6 +39,17 @@
 #define INPUT_DEQUEUE_TIMEOUT_US 8000
 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
 
+enum BitrateMode {
+/* Constant quality mode */
+BITRATE_MODE_CQ = 0,
+/* Variable bitrate mode */
+BITRATE_MODE_VBR = 1,
+/* Constant bitrate mode */
+BITRATE_MODE_CBR = 2,
+/* Constant bitrate mode with frame drops */
+BITRATE_MODE_CBR_FD = 3,
+};
+
 typedef struct MediaCodecEncContext {
 AVClass *avclass;
 FFAMediaCodec *codec;
@@ -67,6 +78,8 @@ typedef struct MediaCodecEncContext {
 int eof_sent;
 
 AVFrame *frame;
+
+int bitrate_mode;
 } MediaCodecEncContext;
 
 enum {
@@ -193,6 +206,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 
 if (avctx->bit_rate)
 ff_AMediaFormat_setInt32(format, "bitrate", avctx->bit_rate);
+if (s->bitrate_mode >= 0)
+ff_AMediaFormat_setInt32(format, "bitrate-mode", s->bitrate_mode);
 // frame-rate and i-frame-interval are required to configure codec
 if (avctx->framerate.num >= avctx->framerate.den && avctx->framerate.den > 
0) {
 s->fps = avctx->framerate.num / avctx->framerate.den;
@@ -485,6 +500,16 @@ static const AVCodecHWConfigInternal *const 
mediacodec_hw_configs[] = {
 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 
1, VE },  \
 { "codec_name", "Select codec by name",
 \
 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, 
 \
+{ "bitrate_mode", "Bitrate control method",
 \
+OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 
INT_MAX, VE, "bitrate_mode" },  \
+{ "cq", "Constant quality mode",   
 \
+0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, 
"bitrate_mode" }, \
+{ "vbr", "Variable bitrate mode",  
 \
+0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, 
"bitrate_mode" },\
+{ "cbr", "Constant bitrate mode",  
 \
+0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, 
"bitrate_mode" },\
+{ "cbr_fd", "Constant bitrate mode with frame drops",  
 \
+0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, 
VE, "bitrate_mode" }, \
 
 
 #define MEDIACODEC_ENCODER_CLASS(name)  \
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 9e66920593..527b4dbd0b 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
 #include "version_major.h"
 
 #define LIBAVCODEC_VERSION_MINOR  54
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MICRO 102
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 3/7] avcodec/mediacodecenc: add level option

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

---
 libavcodec/mediacodecenc.c | 143 -
 libavcodec/version.h   |   2 +-
 2 files changed, 142 insertions(+), 3 deletions(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index ec0e0b3a86..2f78567451 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -80,6 +80,7 @@ typedef struct MediaCodecEncContext {
 AVFrame *frame;
 
 int bitrate_mode;
+int level;
 } MediaCodecEncContext;
 
 enum {
@@ -233,6 +234,10 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 av_log(avctx, AV_LOG_DEBUG, "set profile to 0x%x\n", ret);
 ff_AMediaFormat_setInt32(format, "profile", ret);
 }
+if (s->level > 0) {
+av_log(avctx, AV_LOG_DEBUG, "set level to 0x%x\n", s->level);
+ff_AMediaFormat_setInt32(format, "level", s->level);
+}
 
 ret = ff_AMediaCodec_getConfigureFlagEncode(s->codec);
 ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
@@ -541,17 +546,151 @@ const FFCodec ff_ ## short_name ## _mediacodec_encoder = 
{  \
 };  \
 
 #if CONFIG_H264_MEDIACODEC_ENCODER
+
+enum MediaCodecAvcLevel {
+AVCLevel1   = 0x01,
+AVCLevel1b  = 0x02,
+AVCLevel11  = 0x04,
+AVCLevel12  = 0x08,
+AVCLevel13  = 0x10,
+AVCLevel2   = 0x20,
+AVCLevel21  = 0x40,
+AVCLevel22  = 0x80,
+AVCLevel3   = 0x100,
+AVCLevel31  = 0x200,
+AVCLevel32  = 0x400,
+AVCLevel4   = 0x800,
+AVCLevel41  = 0x1000,
+AVCLevel42  = 0x2000,
+AVCLevel5   = 0x4000,
+AVCLevel51  = 0x8000,
+AVCLevel52  = 0x1,
+AVCLevel6   = 0x2,
+AVCLevel61  = 0x4,
+AVCLevel62  = 0x8,
+};
+
 static const AVOption h264_options[] = {
 COMMON_OPTION
+{ "level", "Specify level",
+OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, 
"level" },
+{ "1",  "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1  },  0, 0, VE, 
"level" },
+{ "1b", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1b }, 0, 0, VE, 
"level" },
+{ "1.1","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel11 }, 0, 0, VE, 
"level" },
+{ "1.2","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel12 }, 0, 0, VE, 
"level" },
+{ "1.3","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel13 }, 0, 0, VE, 
"level" },
+{ "2",  "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel2  },  0, 0, VE, 
"level" },
+{ "2.1","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel21 }, 0, 0, VE, 
"level" },
+{ "2.2","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel22 }, 0, 0, VE, 
"level" },
+{ "3",  "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel3  },  0, 0, VE, 
"level" },
+{ "3.1","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel31 }, 0, 0, VE, 
"level" },
+{ "3.2","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel32 }, 0, 0, VE, 
"level" },
+{ "4",  "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel4  },  0, 0, VE, 
"level" },
+{ "4.1","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel41 }, 0, 0, VE, 
"level" },
+{ "4.2","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel42 }, 0, 0, VE, 
"level" },
+{ "5",  "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel5  },  0, 0, VE, 
"level" },
+{ "5.1","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel51 }, 0, 0, VE, 
"level" },
+{ "5.2","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel52 }, 0, 0, VE, 
"level" },
+{ "6.0","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel6  }, 0, 0, VE, 
"level" },
+{ "6.1","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel61 }, 0, 0, VE, 
"level" },
+{ "6.2","", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel62 }, 0, 0, VE, 
"level" },
 { NULL, }
 };
+
 DECLARE_MEDIACODEC_ENCODER(h264, "H.264", AV_CODEC_ID_H264)
-#endif
+
+#endif  // CONFIG_H264_MEDIACODEC_ENCODER
 
 #if CONFIG_HEVC_MEDIACODEC_ENCODER
+
+enum MediaCodecHevcLevel {
+HEVCMainTierLevel1  = 0x1,
+HEVCHighTierLevel1  = 0x2,
+HEVCMainTierLevel2  = 0x4,
+HEVCHighTierLevel2  = 0x8,
+HEVCMainTierLevel21 = 0x10,
+HEVCHighTierLevel21 = 0x20,
+HEVCMainTierLevel3  = 0x40,
+HEVCHighTierLevel3  = 0x80,
+HEVCMainTierLevel31 = 0x100,
+HEVCHighTierLevel31 = 0x200,
+HEVCMainTierLevel4  = 0x400,
+HEVCHighTierLevel4  = 0x800,
+HEVCMainTierLevel41 = 0x1000,
+HEVCHighTierLevel41 = 0x2000,
+HEVCMainTierLevel5  = 0x4000,
+HEVCHighTierLevel5  = 0x8000,
+HEVCMainTierLevel51 = 0x1,
+HEVCHighTierLevel51 = 0x2,
+HEVCMainTierLevel52 = 0x4,
+HEVCHighTierLevel52 = 0x8,
+HEVCMainTierLevel6  = 0x10,
+HEVCHighTierLevel6  = 0x20,
+HEVCMainTierLevel61 = 0x40,
+HEVCHighTierLevel61 = 0x80,
+HEVCMainTierLevel62 = 0x100,
+HEVCHighTierLevel62 = 0x200,
+};
+
 static const AVOption hevc_options[] = {
 COMMON_OPTION
+

[FFmpeg-devel] [PATCH v2 0/7] MediaCodec encoder: Fix width/height alignment issue and add more options

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

v2:
Reorder 1/7 and 2/7.

Zhao Zhili (7):
  avcodec/mediacodecenc: make each encoder has its own option
  avcodec/mediacodecenc: add bitrate_mode option
  avcodec/mediacodecenc: add level option
  avcodec/mediacodecenc: use bsf to handle crop
  avcodec/mediacodecenc: remove the strategy to create DTS
  avcodec/mediacodecenc: add max-bframes support
  avcodec/mediacodecenc: add pts_as_dts option

 configure  |   2 +
 libavcodec/mediacodecenc.c | 294 +++--
 libavcodec/version.h   |   2 +-
 3 files changed, 256 insertions(+), 42 deletions(-)

-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 1/7] avcodec/mediacodecenc: make each encoder has its own option

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

---
 libavcodec/mediacodecenc.c | 23 +++
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 69246ad693..4f9185342f 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -480,19 +480,18 @@ static const AVCodecHWConfigInternal *const 
mediacodec_hw_configs[] = {
 
 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
-static const AVOption common_options[] = {
-{ "ndk_codec", "Use MediaCodec from NDK",
-OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 
1, VE },
-{ "codec_name", "Select codec by name",
-OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE },
-{ NULL },
-};
+#define COMMON_OPTION  
 \
+{ "ndk_codec", "Use MediaCodec from NDK",  
 \
+OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 
1, VE },  \
+{ "codec_name", "Select codec by name",
 \
+OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, 
 \
+
 
 #define MEDIACODEC_ENCODER_CLASS(name)  \
 static const AVClass name ## _mediacodec_class = {  \
 .class_name = #name "_mediacodec",  \
 .item_name  = av_default_item_name, \
-.option = common_options,   \
+.option = name ## _options, \
 .version= LIBAVUTIL_VERSION_INT,\
 };  \
 
@@ -517,9 +516,17 @@ const FFCodec ff_ ## short_name ## _mediacodec_encoder = { 
 \
 };  \
 
 #if CONFIG_H264_MEDIACODEC_ENCODER
+static const AVOption h264_options[] = {
+COMMON_OPTION
+{ NULL, }
+};
 DECLARE_MEDIACODEC_ENCODER(h264, "H.264", AV_CODEC_ID_H264)
 #endif
 
 #if CONFIG_HEVC_MEDIACODEC_ENCODER
+static const AVOption hevc_options[] = {
+COMMON_OPTION
+{ NULL, }
+};
 DECLARE_MEDIACODEC_ENCODER(hevc, "H.265", AV_CODEC_ID_HEVC)
 #endif
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 6/7] avcodec/mediacodecenc: add max-bframes support

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

---
 libavcodec/mediacodecenc.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 8e28a50e0d..e9cff8167a 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -264,6 +264,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 av_log(avctx, AV_LOG_DEBUG, "set level to 0x%x\n", s->level);
 ff_AMediaFormat_setInt32(format, "level", s->level);
 }
+if (avctx->max_b_frames > 0)
+ff_AMediaFormat_setInt32(format, "max-bframes", avctx->max_b_frames);
 
 ret = ff_AMediaCodec_getConfigureFlagEncode(s->codec);
 ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 7/7] avcodec/mediacodecenc: add pts_as_dts option

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

It works since most of Android devices don't output B frames by
default. The behavior is documented by Android now, although there
is some exception in history, which should have been fixed now.
---
 libavcodec/mediacodecenc.c | 8 
 libavcodec/version.h   | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index e9cff8167a..41b7afe23d 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -71,6 +71,7 @@ typedef struct MediaCodecEncContext {
 
 int bitrate_mode;
 int level;
+int pts_as_dts;
 } MediaCodecEncContext;
 
 enum {
@@ -266,6 +267,8 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 }
 if (avctx->max_b_frames > 0)
 ff_AMediaFormat_setInt32(format, "max-bframes", avctx->max_b_frames);
+if (s->pts_as_dts == -1)
+s->pts_as_dts = avctx->max_b_frames <= 0;
 
 ret = ff_AMediaCodec_getConfigureFlagEncode(s->codec);
 ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
@@ -358,6 +361,8 @@ static int mediacodec_receive(AVCodecContext *avctx,
 }
 memcpy(pkt->data + extradata_size, out_buf + out_info.offset, 
out_info.size);
 pkt->pts = av_rescale_q(out_info.presentationTimeUs, AV_TIME_BASE_Q, 
avctx->time_base);
+if (s->pts_as_dts)
+pkt->dts = pkt->pts;
 if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec))
 pkt->flags |= AV_PKT_FLAG_KEY;
 ret = 0;
@@ -548,6 +553,9 @@ static const AVCodecHWConfigInternal *const 
mediacodec_hw_configs[] = {
 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, 
"bitrate_mode" },\
 { "cbr_fd", "Constant bitrate mode with frame drops",  
 \
 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, 
VE, "bitrate_mode" }, \
+{ "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx 
max_b_frames <= 0, "  \
+"since most of Android devices don't output B frames by 
default.", \
+OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, 
VE }, \
 
 
 #define MEDIACODEC_ENCODER_CLASS(name)  \
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 61bdf5806b..dd90cd1335 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
 #include "version_major.h"
 
 #define LIBAVCODEC_VERSION_MINOR  54
-#define LIBAVCODEC_VERSION_MICRO 103
+#define LIBAVCODEC_VERSION_MICRO 104
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2 4/7] avcodec/mediacodecenc: use bsf to handle crop

2022-12-07 Thread Zhao Zhili
From: Zhao Zhili 

It's well known that mediacodec encoder requires 16x16 alignment.
Use our bsf to fix the crop info.
---
 configure  |  2 ++
 libavcodec/mediacodecenc.c | 65 +++---
 2 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/configure b/configure
index f4eedfc207..2180ebb4f1 100755
--- a/configure
+++ b/configure
@@ -3169,6 +3169,7 @@ h264_mediacodec_decoder_extralibs="-landroid"
 h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_parser"
 h264_mediacodec_encoder_deps="mediacodec"
 h264_mediacodec_encoder_extralibs="-landroid"
+h264_mediacodec_encoder_select="h264_metadata"
 h264_mf_encoder_deps="mediafoundation"
 h264_mmal_decoder_deps="mmal"
 h264_nvenc_encoder_deps="nvenc"
@@ -3190,6 +3191,7 @@ hevc_mediacodec_decoder_extralibs="-landroid"
 hevc_mediacodec_decoder_select="hevc_mp4toannexb_bsf hevc_parser"
 hevc_mediacodec_encoder_deps="mediacodec"
 hevc_mediacodec_encoder_extralibs="-landroid"
+hevc_mediacodec_encoder_select="hevc_metadata"
 hevc_mf_encoder_deps="mediafoundation"
 hevc_nvenc_encoder_deps="nvenc"
 hevc_nvenc_encoder_select="atsc_a53"
diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 2f78567451..7f2ae88285 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -28,6 +28,7 @@
 #include "libavutil/opt.h"
 
 #include "avcodec.h"
+#include "bsf.h"
 #include "codec_internal.h"
 #include "encode.h"
 #include "hwconfig.h"
@@ -78,6 +79,7 @@ typedef struct MediaCodecEncContext {
 int eof_sent;
 
 AVFrame *frame;
+AVBSFContext *bsf;
 
 int bitrate_mode;
 int level;
@@ -119,6 +121,42 @@ static void mediacodec_output_format(AVCodecContext *avctx)
 ff_AMediaFormat_delete(out_format);
 }
 
+static int mediacodec_init_bsf(AVCodecContext *avctx)
+{
+MediaCodecEncContext *s = avctx->priv_data;
+char str[128];
+int ret;
+int crop_right = s->width - avctx->width;
+int crop_bottom = s->height - avctx->height;
+
+if (!crop_right && !crop_bottom)
+return 0;
+
+if (avctx->codec_id == AV_CODEC_ID_H264)
+ret = snprintf(str, sizeof(str), 
"h264_metadata=crop_right=%d:crop_bottom=%d",
+ crop_right, crop_bottom);
+else if (avctx->codec_id == AV_CODEC_ID_HEVC)
+ret = snprintf(str, sizeof(str), 
"hevc_metadata=crop_right=%d:crop_bottom=%d",
+ crop_right, crop_bottom);
+else
+return 0;
+
+if (ret >= sizeof(str))
+return AVERROR_BUFFER_TOO_SMALL;
+
+ret = av_bsf_list_parse_str(str, &s->bsf);
+if (ret < 0)
+return ret;
+
+ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
+if (ret < 0)
+return ret;
+s->bsf->time_base_in = avctx->time_base;
+ret = av_bsf_init(s->bsf);
+
+return ret;
+}
+
 static av_cold int mediacodec_init(AVCodecContext *avctx)
 {
 const char *codec_mime = NULL;
@@ -159,7 +197,7 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 
 ff_AMediaFormat_setString(format, "mime", codec_mime);
 s->width = FFALIGN(avctx->width, 16);
-s->height = avctx->height;
+s->height = FFALIGN(avctx->height, 16);
 ff_AMediaFormat_setInt32(format, "width", s->width);
 ff_AMediaFormat_setInt32(format, "height", s->height);
 
@@ -252,6 +290,10 @@ static av_cold int mediacodec_init(AVCodecContext *avctx)
 goto bailout;
 }
 
+ret = mediacodec_init_bsf(avctx);
+if (ret)
+goto bailout;
+
 mediacodec_output_format(avctx);
 
 s->frame = av_frame_alloc();
@@ -444,10 +486,24 @@ static int mediacodec_encode(AVCodecContext *avctx, 
AVPacket *pkt)
 // 2. Got a packet success
 // 3. No AVFrame is available yet (don't return if get_frame return EOF)
 while (1) {
+if (s->bsf) {
+ret = av_bsf_receive_packet(s->bsf, pkt);
+if (!ret)
+return 0;
+if (ret != AVERROR(EAGAIN))
+return ret;
+}
+
 ret = mediacodec_receive(avctx, pkt, &got_packet);
-if (!ret)
-return 0;
-else if (ret != AVERROR(EAGAIN))
+if (s->bsf) {
+if (!ret || ret == AVERROR_EOF)
+ret = av_bsf_send_packet(s->bsf, pkt);
+} else {
+if (!ret)
+return 0;
+}
+
+if (ret != AVERROR(EAGAIN))
 return ret;
 
 if (!s->frame->buf[0]) {
@@ -480,6 +536,7 @@ static av_cold int mediacodec_close(AVCodecContext *avctx)
 s->window = NULL;
 }
 
+av_bsf_free(&s->bsf);
 av_frame_free(&s->frame);
 
 return 0;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avfilter: add corr video filter

2022-12-07 Thread Paul B Mahol
Will apply in 5 minutes.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [FFmpeg-cvslog] avfilter/framesync: add a new option to set how to sync streams based on secondary input timestamps

2022-12-07 Thread Paul B Mahol
Ping. doc entry is still missing.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 2/2] avcodec/mjpegdec: add support for frame threading

2022-12-07 Thread Timo Rothenpieler
In my tests, this lead to a notable speed increase with the amount
of threads used. Decoding a 720p sample gave the following results:

1 Thread: 1428 FPS
2 Threads: 2501 FPS
8 Threads: 7575 FPS
Automatic: 11326 FPS (On a 16 Core/32 Threads system)
---
 libavcodec/jpeglsdec.c |  2 +-
 libavcodec/mjpegdec.c  | 11 ++-
 libavcodec/sp5xdec.c   |  4 ++--
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index 2e6d018ea6..c0642e8e30 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -559,7 +559,7 @@ const FFCodec ff_jpegls_decoder = {
 .init   = ff_mjpeg_decode_init,
 .close  = ff_mjpeg_decode_end,
 FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
-.p.capabilities = AV_CODEC_CAP_DR1,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
   FF_CODEC_CAP_SETS_PKT_DTS,
 };
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 9b7465abe7..54605e04cb 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -54,6 +54,7 @@
 #include "exif.h"
 #include "bytestream.h"
 #include "tiff_common.h"
+#include "thread.h"
 
 
 static int init_default_huffman_tables(MJpegDecodeContext *s)
@@ -713,7 +714,7 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 s->avctx->pix_fmt,
 AV_PIX_FMT_NONE,
 };
-s->hwaccel_pix_fmt = ff_get_format(s->avctx, pix_fmts);
+s->hwaccel_pix_fmt = ff_thread_get_format(s->avctx, pix_fmts);
 if (s->hwaccel_pix_fmt < 0)
 return AVERROR(EINVAL);
 
@@ -729,7 +730,7 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 av_frame_unref(s->picture_ptr);
-if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 
0)
+if (ff_thread_get_buffer(s->avctx, s->picture_ptr, 
AV_GET_BUFFER_FLAG_REF) < 0)
 return -1;
 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
 s->picture_ptr->key_frame = 1;
@@ -3020,7 +3021,7 @@ const FFCodec ff_mjpeg_decoder = {
 .close  = ff_mjpeg_decode_end,
 FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
 .flush  = decode_flush,
-.p.capabilities = AV_CODEC_CAP_DR1,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 .p.max_lowres   = 3,
 .p.priv_class   = &mjpegdec_class,
 .p.profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
@@ -3050,7 +3051,7 @@ const FFCodec ff_thp_decoder = {
 .close  = ff_mjpeg_decode_end,
 FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
 .flush  = decode_flush,
-.p.capabilities = AV_CODEC_CAP_DR1,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 .p.max_lowres   = 3,
 .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
   FF_CODEC_CAP_SETS_PKT_DTS,
@@ -3068,7 +3069,7 @@ const FFCodec ff_smvjpeg_decoder = {
 .close  = ff_mjpeg_decode_end,
 FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
 .flush  = decode_flush,
-.p.capabilities = AV_CODEC_CAP_DR1,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 .caps_internal  = FF_CODEC_CAP_EXPORTS_CROPPING |
   FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_CLEANUP,
 };
diff --git a/libavcodec/sp5xdec.c b/libavcodec/sp5xdec.c
index 394448c5a9..8b08dc672a 100644
--- a/libavcodec/sp5xdec.c
+++ b/libavcodec/sp5xdec.c
@@ -101,7 +101,7 @@ const FFCodec ff_sp5x_decoder = {
 .init   = ff_mjpeg_decode_init,
 .close  = ff_mjpeg_decode_end,
 FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
-.p.capabilities = AV_CODEC_CAP_DR1,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 .p.max_lowres   = 3,
 .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
   FF_CODEC_CAP_SETS_PKT_DTS,
@@ -118,7 +118,7 @@ const FFCodec ff_amv_decoder = {
 .close  = ff_mjpeg_decode_end,
 FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
 .p.max_lowres   = 3,
-.p.capabilities = AV_CODEC_CAP_DR1,
+.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
 .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
   FF_CODEC_CAP_SETS_PKT_DTS,
 };
-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 1/2] lavc: convert frame threading to the receive_frame() pattern

2022-12-07 Thread Timo Rothenpieler
From: Anton Khirnov 

Reorganize the code such that the frame threading code does not call the
decoders directly, but instead calls back into the generic decoding
code. This avoids duplicating the logic that wraps the decoder
invocation and will be useful in the following commits.
---
 libavcodec/decode.c|  57 +---
 libavcodec/decode.h|   7 +
 libavcodec/internal.h  |   7 +
 libavcodec/pthread_frame.c | 276 -
 libavcodec/thread.h|  18 +--
 5 files changed, 241 insertions(+), 124 deletions(-)

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 6be2d3d6ed..bf3c0cbe0a 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -202,6 +202,10 @@ fail:
 return ret;
 }
 
+#if !HAVE_THREADS
+#define ff_thread_get_packet(avctx, pkt) (AVERROR_BUG)
+#endif
+
 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
 {
 AVCodecInternal *avci = avctx->internal;
@@ -210,7 +214,14 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket 
*pkt)
 if (avci->draining)
 return AVERROR_EOF;
 
-ret = av_bsf_receive_packet(avci->bsf, pkt);
+/* If we are a worker thread, get the next packet from the threading
+ * context. Otherwise we are the main (user-facing) context, so we get the
+ * next packet from the input filterchain.
+ */
+if (avctx->internal->is_frame_mt)
+ret = ff_thread_get_packet(avctx, pkt);
+else
+ret = av_bsf_receive_packet(avci->bsf, pkt);
 if (ret == AVERROR_EOF)
 avci->draining = 1;
 if (ret < 0)
@@ -295,30 +306,25 @@ static inline int decode_simple_internal(AVCodecContext 
*avctx, AVFrame *frame,
 return AVERROR_EOF;
 
 if (!pkt->data &&
-!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
-  avctx->active_thread_type & FF_THREAD_FRAME))
+!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
 return AVERROR_EOF;
 
 got_frame = 0;
 
-if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
-ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
-} else {
-ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
-
-if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
-frame->pkt_dts = pkt->dts;
-if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
-if(!avctx->has_b_frames)
-frame->pkt_pos = pkt->pos;
-//FIXME these should be under if(!avctx->has_b_frames)
-/* get_buffer is supposed to set frame parameters */
-if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
-if (!frame->sample_aspect_ratio.num)  
frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
-if (!frame->width)frame->width 
  = avctx->width;
-if (!frame->height)   frame->height
  = avctx->height;
-if (frame->format == AV_PIX_FMT_NONE) frame->format
  = avctx->pix_fmt;
-}
+ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
+
+if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
+frame->pkt_dts = pkt->dts;
+if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
+if(!avctx->has_b_frames)
+frame->pkt_pos = pkt->pos;
+//FIXME these should be under if(!avctx->has_b_frames)
+/* get_buffer is supposed to set frame parameters */
+if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
+if (!frame->sample_aspect_ratio.num)  frame->sample_aspect_ratio = 
avctx->sample_aspect_ratio;
+if (!frame->width)frame->width   = 
avctx->width;
+if (!frame->height)   frame->height  = 
avctx->height;
+if (frame->format == AV_PIX_FMT_NONE) frame->format  = 
avctx->pix_fmt;
 }
 }
 emms_c();
@@ -568,7 +574,7 @@ static int decode_simple_receive_frame(AVCodecContext 
*avctx, AVFrame *frame)
 return 0;
 }
 
-static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
+int ff_decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 {
 AVCodecInternal *avci = avctx->internal;
 const FFCodec *const codec = ffcodec(avctx->codec);
@@ -634,6 +640,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
 return ret;
 }
 
+static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
+{
+if (avctx->active_thread_type & FF_THREAD_FRAME)
+return ff_thread_receive_frame(avctx, frame);
+return ff_decode_receive_frame_internal(avctx, frame);
+}
+
 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const 
AVPacket *avpkt)
 {
 AVCodecInternal *avci = avctx->internal;
diff --git a/libavcodec/decode.h b/libavcodec/decode.h
index 5d95369b5e..34beb70f97 100644
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@ -58,6 +58,13 @@

[FFmpeg-devel] [PATCH v2] lavc: convert frame threading to the receive_frame() pattern

2022-12-07 Thread Timo Rothenpieler
From: Anton Khirnov 

Reorganize the code such that the frame threading code does not call the
decoders directly, but instead calls back into the generic decoding
code. This avoids duplicating the logic that wraps the decoder
invocation and will be useful in the following commits.
---
 libavcodec/decode.c|  57 +---
 libavcodec/decode.h|   7 +
 libavcodec/internal.h  |   7 +
 libavcodec/pthread_frame.c | 269 -
 libavcodec/thread.h|  18 +--
 5 files changed, 235 insertions(+), 123 deletions(-)

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index b184c3f55b..ce0b2830bd 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -180,6 +180,10 @@ fail:
 return ret;
 }
 
+#if !HAVE_THREADS
+#define ff_thread_get_packet(avctx, pkt) (AVERROR_BUG)
+#endif
+
 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
 {
 AVCodecInternal *avci = avctx->internal;
@@ -188,7 +192,14 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket 
*pkt)
 if (avci->draining)
 return AVERROR_EOF;
 
-ret = av_bsf_receive_packet(avci->bsf, pkt);
+/* If we are a worker thread, get the next packet from the threading
+ * context. Otherwise we are the main (user-facing) context, so we get the
+ * next packet from the input filterchain.
+ */
+if (avctx->internal->is_frame_mt)
+ret = ff_thread_get_packet(avctx, pkt);
+else
+ret = av_bsf_receive_packet(avci->bsf, pkt);
 if (ret == AVERROR_EOF)
 avci->draining = 1;
 if (ret < 0)
@@ -273,30 +284,25 @@ static inline int decode_simple_internal(AVCodecContext 
*avctx, AVFrame *frame,
 return AVERROR_EOF;
 
 if (!pkt->data &&
-!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
-  avctx->active_thread_type & FF_THREAD_FRAME))
+!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
 return AVERROR_EOF;
 
 got_frame = 0;
 
-if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
-ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
-} else {
-ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
-
-if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
-frame->pkt_dts = pkt->dts;
-if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
-if(!avctx->has_b_frames)
-frame->pkt_pos = pkt->pos;
-//FIXME these should be under if(!avctx->has_b_frames)
-/* get_buffer is supposed to set frame parameters */
-if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
-if (!frame->sample_aspect_ratio.num)  
frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
-if (!frame->width)frame->width 
  = avctx->width;
-if (!frame->height)   frame->height
  = avctx->height;
-if (frame->format == AV_PIX_FMT_NONE) frame->format
  = avctx->pix_fmt;
-}
+ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
+
+if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
+frame->pkt_dts = pkt->dts;
+if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
+if(!avctx->has_b_frames)
+frame->pkt_pos = pkt->pos;
+//FIXME these should be under if(!avctx->has_b_frames)
+/* get_buffer is supposed to set frame parameters */
+if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
+if (!frame->sample_aspect_ratio.num)  frame->sample_aspect_ratio = 
avctx->sample_aspect_ratio;
+if (!frame->width)frame->width   = 
avctx->width;
+if (!frame->height)   frame->height  = 
avctx->height;
+if (frame->format == AV_PIX_FMT_NONE) frame->format  = 
avctx->pix_fmt;
 }
 }
 emms_c();
@@ -546,7 +552,7 @@ static int decode_simple_receive_frame(AVCodecContext 
*avctx, AVFrame *frame)
 return 0;
 }
 
-static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
+int ff_decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 {
 AVCodecInternal *avci = avctx->internal;
 const FFCodec *const codec = ffcodec(avctx->codec);
@@ -604,6 +610,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
 return ret;
 }
 
+static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
+{
+if (avctx->active_thread_type & FF_THREAD_FRAME)
+return ff_thread_receive_frame(avctx, frame);
+return ff_decode_receive_frame_internal(avctx, frame);
+}
+
 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const 
AVPacket *avpkt)
 {
 AVCodecInternal *avci = avctx->internal;
diff --git a/libavcodec/decode.h b/libavcodec/decode.h
index 5d95369b5e..34beb70f97 100644
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@ -58,6 +58,13 @@

[FFmpeg-devel] [PATCH v3] lavc: convert frame threading to the receive_frame() pattern

2022-12-07 Thread Timo Rothenpieler
From: Anton Khirnov 

Reorganize the code such that the frame threading code does not call the
decoders directly, but instead calls back into the generic decoding
code. This avoids duplicating the logic that wraps the decoder
invocation and will be useful in the following commits.
---
 libavcodec/decode.c|  57 +
 libavcodec/decode.h|   7 +
 libavcodec/internal.h  |   7 +
 libavcodec/pthread_frame.c | 256 -
 libavcodec/thread.h|  18 +--
 5 files changed, 222 insertions(+), 123 deletions(-)

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index b184c3f55b..ce0b2830bd 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -180,6 +180,10 @@ fail:
 return ret;
 }
 
+#if !HAVE_THREADS
+#define ff_thread_get_packet(avctx, pkt) (AVERROR_BUG)
+#endif
+
 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
 {
 AVCodecInternal *avci = avctx->internal;
@@ -188,7 +192,14 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket 
*pkt)
 if (avci->draining)
 return AVERROR_EOF;
 
-ret = av_bsf_receive_packet(avci->bsf, pkt);
+/* If we are a worker thread, get the next packet from the threading
+ * context. Otherwise we are the main (user-facing) context, so we get the
+ * next packet from the input filterchain.
+ */
+if (avctx->internal->is_frame_mt)
+ret = ff_thread_get_packet(avctx, pkt);
+else
+ret = av_bsf_receive_packet(avci->bsf, pkt);
 if (ret == AVERROR_EOF)
 avci->draining = 1;
 if (ret < 0)
@@ -273,30 +284,25 @@ static inline int decode_simple_internal(AVCodecContext 
*avctx, AVFrame *frame,
 return AVERROR_EOF;
 
 if (!pkt->data &&
-!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY ||
-  avctx->active_thread_type & FF_THREAD_FRAME))
+!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
 return AVERROR_EOF;
 
 got_frame = 0;
 
-if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
-ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
-} else {
-ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
-
-if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
-frame->pkt_dts = pkt->dts;
-if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
-if(!avctx->has_b_frames)
-frame->pkt_pos = pkt->pos;
-//FIXME these should be under if(!avctx->has_b_frames)
-/* get_buffer is supposed to set frame parameters */
-if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
-if (!frame->sample_aspect_ratio.num)  
frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
-if (!frame->width)frame->width 
  = avctx->width;
-if (!frame->height)   frame->height
  = avctx->height;
-if (frame->format == AV_PIX_FMT_NONE) frame->format
  = avctx->pix_fmt;
-}
+ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
+
+if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
+frame->pkt_dts = pkt->dts;
+if (avctx->codec->type == AVMEDIA_TYPE_VIDEO) {
+if(!avctx->has_b_frames)
+frame->pkt_pos = pkt->pos;
+//FIXME these should be under if(!avctx->has_b_frames)
+/* get_buffer is supposed to set frame parameters */
+if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
+if (!frame->sample_aspect_ratio.num)  frame->sample_aspect_ratio = 
avctx->sample_aspect_ratio;
+if (!frame->width)frame->width   = 
avctx->width;
+if (!frame->height)   frame->height  = 
avctx->height;
+if (frame->format == AV_PIX_FMT_NONE) frame->format  = 
avctx->pix_fmt;
 }
 }
 emms_c();
@@ -546,7 +552,7 @@ static int decode_simple_receive_frame(AVCodecContext 
*avctx, AVFrame *frame)
 return 0;
 }
 
-static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
+int ff_decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
 {
 AVCodecInternal *avci = avctx->internal;
 const FFCodec *const codec = ffcodec(avctx->codec);
@@ -604,6 +610,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
 return ret;
 }
 
+static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
+{
+if (avctx->active_thread_type & FF_THREAD_FRAME)
+return ff_thread_receive_frame(avctx, frame);
+return ff_decode_receive_frame_internal(avctx, frame);
+}
+
 int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const 
AVPacket *avpkt)
 {
 AVCodecInternal *avci = avctx->internal;
diff --git a/libavcodec/decode.h b/libavcodec/decode.h
index 5d95369b5e..34beb70f97 100644
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@ -58,6 +58,13 @

[FFmpeg-devel] [PATCH] configure: support lsan as toolchain

2022-12-07 Thread James Darnley
---
 configure | 5 +
 1 file changed, 5 insertions(+)

diff --git a/configure b/configure
index f4eedfc207..eaa5ef6b20 100755
--- a/configure
+++ b/configure
@@ -4315,6 +4315,11 @@ case "$toolchain" in
 add_cflags  -fsanitize=address
 add_ldflags -fsanitize=address
 ;;
+*-lsan)
+cc_default="${toolchain%-lsan}"
+add_cflags  -fsanitize=leak
+add_ldflags -fsanitize=leak
+;;
 *-msan)
 cc_default="${toolchain%-msan}"
 add_cflags  -fsanitize=memory -fsanitize-memory-track-origins
-- 
2.38.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-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] Defect: MediaCodec Encoder - Pixel ColorSpace of many Adreno GPUs not supported

2022-12-07 Thread Zhao Zhili
> From: ffmpeg-devel  On Behalf Of Ben Temple
> Sent: 2022年12月7日 6:53
> To: ffmpeg-devel@ffmpeg.org; Zhao Zhili 
> Subject: [FFmpeg-devel] Defect: MediaCodec Encoder - Pixel ColorSpace of many 
> Adreno GPUs not supported
> 
> MediaCodec Encoder: Pixel ColorSpace of many Adreno GPUs is not supported
> (<= Adreno 650 in our testing)What you were trying to accomplish?
> 
> I am trying to transcode an MP4 file using the MediaCodec encoder support
> that was recently added by Zhao Zhili  here
> 
> (
> https://git.ffmpeg.org/gitweb/ffmpeg.git/commit/0ff18a7d6d496f89d9e007ddd1cad7116baf5c7c
> )
> The problem you encountered:
> 
> On multiple phones containing an Adreno GPU, the encoding process
> immediately fails because the color space expected by the GPU is
> unsupported.
> 
> The exact error output: “does not support color format 19”
> 
...
> 
> The exact command line you were using:
> 
> -hwaccel mediacodec -hwaccel_output_format mediacodec -i
> "/data/user/0/com.test.ffmpeg_kit_android_demo/cache/test_input.mp4"  -c:v
> h264_mediacodec  -ndk_codec 1  -y
> "/data/user/0/com.test.ffmpeg_kit_android_demo/cache/test_output.mp4"
> -loglevel trace

Thank you for your valuable test!

There is an unexpected behavior in the test. The test was conducted with 
ffmpeg-kit, not ffmpeg cmdline.
With FFmpeg cmdline and -hwcontext mediacodec, the encoder should have 
"-pix_fmt mediacodec" by
default, and have an AVMediaCodecDeviceContext (with Android 8.0 or higher). 
The encoder doesn't
setup properly with ffmpeg-kit.

pix_fmt mediacodec should be supported by most of Android devices. It do have 
its limitation, e.g., it needs
a Surface object or require Android 8.0, and it doesn't work with avfilter.

The above issue can be fixed on the ffmpeg-kit's side. Now comes the dark 
corner of Mediacodec. It should
work without pix_fmt mediacodec. Can you try specify the encoder pix_fmt as 
nv12, that's

-hwaccel mediacodec -hwaccel_output_format mediacodec -i test_input.mp4 -c:v 
h264_mediacodec -ndk_codec 1 -pix_fmt nv12 -y test_output.mp4

I guess these devices work with nv12.

There is an old story "MediaCodec and device compatibility"

https://groups.google.com/g/android-platform/c/p_MoSk0JPNM

Ten years on, the situation doesn't improved much:
1. There is an ugly API to get color format supported by a codec.
2. It's only available in Java.
3. Different codec component have different color format.

Yes, we can use a bunch of JNI to get the supported color format. It must be 
done before create
AVCodecContext. But different mediacodec component have different color format, 
user can select 
mediacodec by name (see the following patch), we have only one AVCodec.pix_fmts.

avcodec/mediacodecenc: add option to select codec by name
https://patchwork.ffmpeg.org/project/ffmpeg/patch/tencent_729f52abe34e4770972db89af840549a8...@qq.com/

We can add query the supported color format and add some hints when mediacodec 
configure failed.
Or we can add a helper function for user to query the color format. It will be 
a bunch of JNI code, limited
by the ugly MediaCodecInfo.CodecCapabilities API. It's easy to be done with 
Java. TBH, it's much easier with
trial and error, try nv12 then yuv420.

We can do something with FFCodec.init_static_data, but we need a strategy 
first. I don't want to go with a long
blacklist/whitelist.

** We only need one pixel format which supported by all devices, 
COLOR_FormatSurface doesn't count, and
COLOR_FormatYUV420Flexible doesn't count. **

Maybe Google can fix these issues with another 10 year.

> Console output:
> 
> See attached console_output.log
> Input Files:
> 
> See attached test_input.mp4

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] Defect: MediaCodec Encoder - Pixel ColorSpace of many Adreno GPUs not supported

2022-12-07 Thread Ben Temple
My apologies for using the incorrect channel. I mis-read the new ticket
description that states that *patches* must come through the mailing list,
not new tickets.

I've created a ticket here: https://trac.ffmpeg.org/ticket/10096 where we
can continue the discussion.

As a quick side-note, using -px_fmt nv12 does resolve the issue on a quick
test that I ran on a phone that was failing. I'll respond further on the
ticket so as to not add noise to this mailing list. Thank you for your
response!

On Wed, Dec 7, 2022 at 9:16 AM Zhao Zhili  wrote:

> > From: ffmpeg-devel  On Behalf Of Ben
> Temple
> > Sent: 2022年12月7日 6:53
> > To: ffmpeg-devel@ffmpeg.org; Zhao Zhili 
> > Subject: [FFmpeg-devel] Defect: MediaCodec Encoder - Pixel ColorSpace of
> many Adreno GPUs not supported
> >
> > MediaCodec Encoder: Pixel ColorSpace of many Adreno GPUs is not supported
> > (<= Adreno 650 in our testing)What you were trying to accomplish?
> >
> > I am trying to transcode an MP4 file using the MediaCodec encoder support
> > that was recently added by Zhao Zhili  here
> > <
> https://urldefense.com/v3/__https://git.ffmpeg.org/gitweb/ffmpeg.git/commit/0ff18a7d6d496f89d9e007ddd1cad7116baf5c7c__;!!N1d2nkBXpQ!K7sceiwL4SR1PIFjy4TY99mSRBvCQZi78fR4-CH4X5H9Q_KXbtmpW22WH9y-eagaazoZSyHLeITHcSseZdyLWTgl$
> >
> > (
> >
> https://urldefense.com/v3/__https://git.ffmpeg.org/gitweb/ffmpeg.git/commit/0ff18a7d6d496f89d9e007ddd1cad7116baf5c7c__;!!N1d2nkBXpQ!K7sceiwL4SR1PIFjy4TY99mSRBvCQZi78fR4-CH4X5H9Q_KXbtmpW22WH9y-eagaazoZSyHLeITHcSseZdyLWTgl$
> > )
> > The problem you encountered:
> >
> > On multiple phones containing an Adreno GPU, the encoding process
> > immediately fails because the color space expected by the GPU is
> > unsupported.
> >
> > The exact error output: “does not support color format 19”
> >
> ...
> >
> > The exact command line you were using:
> >
> > -hwaccel mediacodec -hwaccel_output_format mediacodec -i
> > "/data/user/0/com.test.ffmpeg_kit_android_demo/cache/test_input.mp4"
> -c:v
> > h264_mediacodec  -ndk_codec 1  -y
> > "/data/user/0/com.test.ffmpeg_kit_android_demo/cache/test_output.mp4"
> > -loglevel trace
>
> Thank you for your valuable test!
>
> There is an unexpected behavior in the test. The test was conducted with
> ffmpeg-kit, not ffmpeg cmdline.
> With FFmpeg cmdline and -hwcontext mediacodec, the encoder should have
> "-pix_fmt mediacodec" by
> default, and have an AVMediaCodecDeviceContext (with Android 8.0 or
> higher). The encoder doesn't
> setup properly with ffmpeg-kit.
>
> pix_fmt mediacodec should be supported by most of Android devices. It do
> have its limitation, e.g., it needs
> a Surface object or require Android 8.0, and it doesn't work with avfilter.
>
> The above issue can be fixed on the ffmpeg-kit's side. Now comes the dark
> corner of Mediacodec. It should
> work without pix_fmt mediacodec. Can you try specify the encoder pix_fmt
> as nv12, that's
>
> -hwaccel mediacodec -hwaccel_output_format mediacodec -i test_input.mp4
> -c:v h264_mediacodec -ndk_codec 1 -pix_fmt nv12 -y test_output.mp4
>
> I guess these devices work with nv12.
>
> There is an old story "MediaCodec and device compatibility"
>
>
> https://urldefense.com/v3/__https://groups.google.com/g/android-platform/c/p_MoSk0JPNM__;!!N1d2nkBXpQ!K7sceiwL4SR1PIFjy4TY99mSRBvCQZi78fR4-CH4X5H9Q_KXbtmpW22WH9y-eagaazoZSyHLeITHcSseZZeyTDJ9$
>
> Ten years on, the situation doesn't improved much:
> 1. There is an ugly API to get color format supported by a codec.
> 2. It's only available in Java.
> 3. Different codec component have different color format.
>
> Yes, we can use a bunch of JNI to get the supported color format. It must
> be done before create
> AVCodecContext. But different mediacodec component have different color
> format, user can select
> mediacodec by name (see the following patch), we have only one
> AVCodec.pix_fmts.
>
> avcodec/mediacodecenc: add option to select codec by name
>
> https://urldefense.com/v3/__https://patchwork.ffmpeg.org/project/ffmpeg/patch/tencent_729f52abe34e4770972db89af840549a8...@qq.com/__;!!N1d2nkBXpQ!K7sceiwL4SR1PIFjy4TY99mSRBvCQZi78fR4-CH4X5H9Q_KXbtmpW22WH9y-eagaazoZSyHLeITHcSseZTsxLMxB$
>
> We can add query the supported color format and add some hints when
> mediacodec configure failed.
> Or we can add a helper function for user to query the color format. It
> will be a bunch of JNI code, limited
> by the ugly MediaCodecInfo.CodecCapabilities API. It's easy to be done
> with Java. TBH, it's much easier with
> trial and error, try nv12 then yuv420.
>
> We can do something with FFCodec.init_static_data, but we need a strategy
> first. I don't want to go with a long
> blacklist/whitelist.
>
> ** We only need one pixel format which supported by all devices,
> COLOR_FormatSurface doesn't count, and
> COLOR_FormatYUV420Flexible doesn't count. **
>
> Maybe Google can fix these issues with another 10 year.
>
> > Console output:
> >
> > See attached console_output.log
> > Input Files:
> >
> > See attached test

[FFmpeg-devel] [PATCH 1/5] avcodec/mjpegdec: Restrict AVID MJPEG to non-SMVJPEG

2022-12-07 Thread Andreas Rheinhardt
AVID content is not supposed to be SMVJPEG; given that
both these codecs involve manipulating image dimensions
and cropping dimensions, it makes sense to restrict
the AVID codepaths to non-SMVJPEG codecs in order not
to have to think about what if SMVJPEG happens to
have a codec tag indicating AVID.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mjpegdec.c | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 9b7465abe7..28e2839072 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -452,7 +452,8 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 if (ret < 0)
 return ret;
 
-if ((s->avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
+if (s->avctx->codec_id != AV_CODEC_ID_SMVJPEG &&
+(s->avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
  s->avctx->codec_tag == MKTAG('A', 'V', 'D', 'J')) &&
 s->orig_height < height)
 s->avctx->height = AV_CEIL_RSHIFT(s->orig_height, 
s->avctx->lowres);
@@ -2927,7 +2928,8 @@ the_end:
 return ret;
 }
 }
-if ((avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
+if (avctx->codec_id != AV_CODEC_ID_SMVJPEG &&
+(avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
  avctx->codec_tag == MKTAG('A', 'V', 'D', 'J')) &&
 avctx->coded_height > s->orig_height) {
 frame->height   = AV_CEIL_RSHIFT(avctx->coded_height, avctx->lowres);
-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 2/5] avcodec/mjpegdec: Move special SMVJPEG-code to SMVJPEG-only function

2022-12-07 Thread Andreas Rheinhardt
This automatically avoids runtime checks for whether
the decoder is SMVJPEG.

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mjpegdec.c | 27 ---
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 28e2839072..b88d2ab889 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -2422,9 +2422,6 @@ int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame 
*frame)
 
 s->force_pal8 = 0;
 
-if (avctx->codec_id == AV_CODEC_ID_SMVJPEG && s->smv_next_frame > 0)
-return smv_process_frame(avctx, frame);
-
 av_dict_free(&s->exif_metadata);
 av_freep(&s->stereo3d);
 s->adobe_transform = -1;
@@ -2921,13 +2918,6 @@ the_end:
 av_dict_copy(&frame->metadata, s->exif_metadata, 0);
 av_dict_free(&s->exif_metadata);
 
-if (avctx->codec_id == AV_CODEC_ID_SMVJPEG) {
-ret = smv_process_frame(avctx, frame);
-if (ret < 0) {
-av_frame_unref(frame);
-return ret;
-}
-}
 if (avctx->codec_id != AV_CODEC_ID_SMVJPEG &&
 (avctx->codec_tag == MKTAG('A', 'V', 'R', 'n') ||
  avctx->codec_tag == MKTAG('A', 'V', 'D', 'J')) &&
@@ -3060,6 +3050,21 @@ const FFCodec ff_thp_decoder = {
 #endif
 
 #if CONFIG_SMVJPEG_DECODER
+static int smvjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+{
+MJpegDecodeContext *s = avctx->priv_data;
+int ret;
+
+if (s->smv_next_frame > 0)
+return smv_process_frame(avctx, frame);
+
+ret = ff_mjpeg_receive_frame(avctx, frame);
+if (ret < 0)
+return ret;
+
+return smv_process_frame(avctx, frame);
+}
+
 const FFCodec ff_smvjpeg_decoder = {
 .p.name = "smvjpeg",
 CODEC_LONG_NAME("SMV JPEG"),
@@ -3068,7 +3073,7 @@ const FFCodec ff_smvjpeg_decoder = {
 .priv_data_size = sizeof(MJpegDecodeContext),
 .init   = ff_mjpeg_decode_init,
 .close  = ff_mjpeg_decode_end,
-FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+FF_CODEC_RECEIVE_FRAME_CB(smvjpeg_receive_frame),
 .flush  = decode_flush,
 .p.capabilities = AV_CODEC_CAP_DR1,
 .caps_internal  = FF_CODEC_CAP_EXPORTS_CROPPING |
-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 3/5] avcodec/mjpegdec: Avoid checks whose results are known at compile-time

2022-12-07 Thread Andreas Rheinhardt
Namely the result of the check for smv_next_frame > 0 in
smv_process_frame().

Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mjpegdec.c | 39 ++-
 1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index b88d2ab889..2abc42a082 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -2349,24 +2349,9 @@ static void reset_icc_profile(MJpegDecodeContext *s)
 
 // SMV JPEG just stacks several output frames into one JPEG picture
 // we handle that by setting up the cropping parameters appropriately
-static int smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
+static void smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
 {
 MJpegDecodeContext *s = avctx->priv_data;
-int ret;
-
-if (s->smv_next_frame > 0) {
-av_assert0(s->smv_frame->buf[0]);
-av_frame_unref(frame);
-ret = av_frame_ref(frame, s->smv_frame);
-if (ret < 0)
-return ret;
-} else {
-av_assert0(frame->buf[0]);
-av_frame_unref(s->smv_frame);
-ret = av_frame_ref(s->smv_frame, frame);
-if (ret < 0)
-return ret;
-}
 
 av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
 
@@ -2379,8 +2364,6 @@ static int smv_process_frame(AVCodecContext *avctx, 
AVFrame *frame)
 
 if (s->smv_next_frame == 0)
 av_frame_unref(s->smv_frame);
-
-return 0;
 }
 
 static int mjpeg_get_packet(AVCodecContext *avctx)
@@ -3055,14 +3038,28 @@ static int smvjpeg_receive_frame(AVCodecContext *avctx, 
AVFrame *frame)
 MJpegDecodeContext *s = avctx->priv_data;
 int ret;
 
-if (s->smv_next_frame > 0)
-return smv_process_frame(avctx, frame);
+if (s->smv_next_frame > 0) {
+av_assert0(s->smv_frame->buf[0]);
+ret = av_frame_ref(frame, s->smv_frame);
+if (ret < 0)
+return ret;
+
+smv_process_frame(avctx, frame);
+return 0;
+}
 
 ret = ff_mjpeg_receive_frame(avctx, frame);
 if (ret < 0)
 return ret;
 
-return smv_process_frame(avctx, frame);
+av_assert0(frame->buf[0]);
+av_frame_unref(s->smv_frame);
+ret = av_frame_ref(s->smv_frame, frame);
+if (ret < 0)
+return ret;
+
+smv_process_frame(avctx, frame);
+return 0;
 }
 
 const FFCodec ff_smvjpeg_decoder = {
-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 4/5] avcodec/mjpegdec: Only use receive_frame for SMVJPEG

2022-12-07 Thread Andreas Rheinhardt
Only one codec using mjpegdec.c actually creates multiple
frames from a single packet, namely SMVJPEG. The other can
use the ordinary decode callback just fine. This e.g. has
the advantage of confining the special SP5X/AMV code to sp5xdec.c.

This reverts most of commit e9a2a8777317d91af658f774c68442ac4aa726ec;
of course it is not a simple revert: Way too much has changed;
furthermore, outright reverting the sp5xdec.c changes would readd
a stack packet to sp5x_decode_frame() which is not desired.
In order to avoid this without modifying the given AVPacket,
a variant of ff_mjpeg_decode_frame() with explicit buf and size
parameters has been added.

Signed-off-by: Andreas Rheinhardt 
---
Weird that these receive_frame decoders have FF_CODEC_CAP_SETS_PKT_DTS
set, despite this flag only being used for decode_frame decoders.

 libavcodec/jpeglsdec.c |  5 ++-
 libavcodec/mjpegdec.c  | 80 ++
 libavcodec/mjpegdec.h  | 11 +++---
 libavcodec/sp5xdec.c   | 32 -
 4 files changed, 59 insertions(+), 69 deletions(-)

diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index 2e6d018ea6..ec163b8964 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -558,8 +558,7 @@ const FFCodec ff_jpegls_decoder = {
 .priv_data_size = sizeof(MJpegDecodeContext),
 .init   = ff_mjpeg_decode_init,
 .close  = ff_mjpeg_decode_end,
-FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame),
 .p.capabilities = AV_CODEC_CAP_DR1,
-.caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
-  FF_CODEC_CAP_SETS_PKT_DTS,
+.caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 2abc42a082..aa1a6b5208 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -131,8 +131,6 @@ av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
 s->picture_ptr = s->picture;
 }
 
-s->pkt = avctx->internal->in_pkt;
-
 s->avctx = avctx;
 ff_blockdsp_init(&s->bdsp);
 ff_hpeldsp_init(&s->hdsp, avctx->flags);
@@ -2366,31 +2364,9 @@ static void smv_process_frame(AVCodecContext *avctx, 
AVFrame *frame)
 av_frame_unref(s->smv_frame);
 }
 
-static int mjpeg_get_packet(AVCodecContext *avctx)
-{
-MJpegDecodeContext *s = avctx->priv_data;
-int ret;
-
-av_packet_unref(s->pkt);
-ret = ff_decode_get_packet(avctx, s->pkt);
-if (ret < 0)
-return ret;
-
-#if CONFIG_SP5X_DECODER || CONFIG_AMV_DECODER
-if (avctx->codec_id == AV_CODEC_ID_SP5X ||
-avctx->codec_id == AV_CODEC_ID_AMV) {
-ret = ff_sp5x_process_packet(avctx, s->pkt);
-if (ret < 0)
-return ret;
-}
-#endif
-
-s->buf_size = s->pkt->size;
-
-return 0;
-}
-
-int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx, AVFrame *frame,
+   int *got_frame, const AVPacket *avpkt,
+   const uint8_t *buf, const int buf_size)
 {
 MJpegDecodeContext *s = avctx->priv_data;
 const uint8_t *buf_end, *buf_ptr;
@@ -2405,6 +2381,8 @@ int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame 
*frame)
 
 s->force_pal8 = 0;
 
+s->buf_size = buf_size;
+
 av_dict_free(&s->exif_metadata);
 av_freep(&s->stereo3d);
 s->adobe_transform = -1;
@@ -2412,12 +2390,9 @@ int ff_mjpeg_receive_frame(AVCodecContext *avctx, 
AVFrame *frame)
 if (s->iccnum != 0)
 reset_icc_profile(s);
 
-ret = mjpeg_get_packet(avctx);
-if (ret < 0)
-return ret;
 redo_for_pal8:
-buf_ptr = s->pkt->data;
-buf_end = s->pkt->data + s->pkt->size;
+buf_ptr = buf;
+buf_end = buf + buf_size;
 while (buf_ptr < buf_end) {
 /* find start next marker */
 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
@@ -2429,7 +2404,7 @@ redo_for_pal8:
 } else if (unescaped_buf_size > INT_MAX / 8) {
 av_log(avctx, AV_LOG_ERROR,
"MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
-   start_code, unescaped_buf_size, s->pkt->size);
+   start_code, unescaped_buf_size, buf_size);
 return AVERROR_INVALIDDATA;
 }
 av_log(avctx, AV_LOG_DEBUG, "marker=%x 
avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
@@ -2568,7 +2543,6 @@ eoi_parser:
 }
 if (avctx->skip_frame == AVDISCARD_ALL) {
 s->got_picture = 0;
-ret = AVERROR(EAGAIN);
 goto the_end_no_picture;
 }
 if (s->avctx->hwaccel) {
@@ -2580,10 +2554,9 @@ eoi_parser:
 }
 if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
 return ret;
+*got_frame = 1;
 s->got_picture = 0;
 
-frame->pkt_dts = s->pkt->dts;
-
 if (!s->lossless &

[FFmpeg-devel] [PATCH 5/5] avcodec/mjpegdec: Move smv_process_frame() to other SMV stuff

2022-12-07 Thread Andreas Rheinhardt
Signed-off-by: Andreas Rheinhardt 
---
 libavcodec/mjpegdec.c | 38 +++---
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index aa1a6b5208..ea6f724049 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -2345,25 +2345,6 @@ static void reset_icc_profile(MJpegDecodeContext *s)
 s->iccnum  = 0;
 }
 
-// SMV JPEG just stacks several output frames into one JPEG picture
-// we handle that by setting up the cropping parameters appropriately
-static void smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
-{
-MJpegDecodeContext *s = avctx->priv_data;
-
-av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
-
-frame->width   = avctx->coded_width;
-frame->height  = avctx->coded_height;
-frame->crop_top= FFMIN(s->smv_next_frame * avctx->height, 
frame->height);
-frame->crop_bottom = frame->height - (s->smv_next_frame + 1) * 
avctx->height;
-
-s->smv_next_frame = (s->smv_next_frame + 1) % s->smv_frames_per_jpeg;
-
-if (s->smv_next_frame == 0)
-av_frame_unref(s->smv_frame);
-}
-
 int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, const AVPacket *avpkt,
const uint8_t *buf, const int buf_size)
@@ -3009,6 +2990,25 @@ const FFCodec ff_thp_decoder = {
 #endif
 
 #if CONFIG_SMVJPEG_DECODER
+// SMV JPEG just stacks several output frames into one JPEG picture
+// we handle that by setting up the cropping parameters appropriately
+static void smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
+{
+MJpegDecodeContext *s = avctx->priv_data;
+
+av_assert0((s->smv_next_frame + 1) * avctx->height <= avctx->coded_height);
+
+frame->width   = avctx->coded_width;
+frame->height  = avctx->coded_height;
+frame->crop_top= FFMIN(s->smv_next_frame * avctx->height, 
frame->height);
+frame->crop_bottom = frame->height - (s->smv_next_frame + 1) * 
avctx->height;
+
+s->smv_next_frame = (s->smv_next_frame + 1) % s->smv_frames_per_jpeg;
+
+if (s->smv_next_frame == 0)
+av_frame_unref(s->smv_frame);
+}
+
 static int smvjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 {
 MJpegDecodeContext *s = avctx->priv_data;
-- 
2.34.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v3] lavc: convert frame threading to the receive_frame() pattern

2022-12-07 Thread Michael Niedermayer
On Wed, Dec 07, 2022 at 02:20:23PM +0100, Timo Rothenpieler wrote:
> From: Anton Khirnov 
> 
> Reorganize the code such that the frame threading code does not call the
> decoders directly, but instead calls back into the generic decoding
> code. This avoids duplicating the logic that wraps the decoder
> invocation and will be useful in the following commits.
> ---
>  libavcodec/decode.c|  57 +
>  libavcodec/decode.h|   7 +
>  libavcodec/internal.h  |   7 +
>  libavcodec/pthread_frame.c | 256 -
>  libavcodec/thread.h|  18 +--
>  5 files changed, 222 insertions(+), 123 deletions(-)

This breaks on arm (probably lack of pthread support) in this env

libavcodec/libavcodec.a(decode.o): In function `decode_receive_frame_internal':
arm/src/libavcodec/decode.c:616: undefined reference to 
`ff_thread_receive_frame'
arm/src/libavcodec/decode.c:616: undefined reference to 
`ff_thread_receive_frame'
collect2: error: ld returned 1 exit status
Makefile:131: recipe for target 'ffprobe_g' failed
make: *** [ffprobe_g] Error 1

thx

[...]
-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Many that live deserve death. And some that die deserve life. Can you give
it to them? Then do not be too eager to deal out death in judgement. For
even the very wise cannot see all ends. -- Gandalf


signature.asc
Description: PGP signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] A question about http connections

2022-12-07 Thread Steven Liu
Basel Sayeh  于2022年12月6日周二 22:48写道:
>
> Hello
>
> I'm thinking of implementing code to check the server response for the 
> hls/dash encoders (incase the output was a URL).
Hi Basel,

What about check the message in libavformat/http?
>
> Is it ok to implement it inside hlsenc_io_close/dashenc_io_close, and using 
> blocking ffurl_read?

Thanks
Steven
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavfi/vf_scale_qsv: remove PI, PHI and E

2022-12-07 Thread Xiang, Haihao
On Wo, 2022-12-07 at 10:51 +0530, Gyan Doshi wrote:
> 
> On 2022-12-07 08:13 am, Xiang, Haihao wrote:
> > From: Haihao Xiang 
> > 
> > PI, PHI and E are defined in libavutil/eval.c, and user may use these
> > constants for scale_qsv filter, so we needn't re-define these variables
> > in vf_scale_qsv.c
> 
> LGTM.

Thanks for review, applied. 

-Haihao

> 
> 
> > Signed-off-by: Haihao Xiang 
> > ---
> >   libavfilter/vf_scale_qsv.c | 9 -
> >   1 file changed, 9 deletions(-)
> > 
> > diff --git a/libavfilter/vf_scale_qsv.c b/libavfilter/vf_scale_qsv.c
> > index 758e730f78..fa37e95429 100644
> > --- a/libavfilter/vf_scale_qsv.c
> > +++ b/libavfilter/vf_scale_qsv.c
> > @@ -44,9 +44,6 @@
> >   #include "video.h"
> >   
> >   static const char *const var_names[] = {
> > -"PI",
> > -"PHI",
> > -"E",
> >   "in_w",   "iw",
> >   "in_h",   "ih",
> >   "out_w",  "ow",
> > @@ -57,9 +54,6 @@ static const char *const var_names[] = {
> >   };
> >   
> >   enum var_name {
> > -VAR_PI,
> > -VAR_PHI,
> > -VAR_E,
> >   VAR_IN_W,   VAR_IW,
> >   VAR_IN_H,   VAR_IH,
> >   VAR_OUT_W,  VAR_OW,
> > @@ -470,9 +464,6 @@ static int qsvscale_config_props(AVFilterLink *outlink)
> >   char *expr;
> >   int ret;
> >   
> > -var_values[VAR_PI]= M_PI;
> > -var_values[VAR_PHI]   = M_PHI;
> > -var_values[VAR_E] = M_E;
> >   var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
> >   var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
> >   var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
> 
> ___
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 2/3] avcodec/vqcdec: Check width & 15

2022-12-07 Thread Peter Ross
On Sun, Nov 27, 2022 at 11:34:34PM +0100, Michael Niedermayer wrote:
> Various parts of the code assume that width can be divided by various powers 
> of 2
> without rounding
> 
> Fixes: out of array access
> Fixes: 
> 53623/clusterfuzz-testcase-minimized-ffmpeg_AV_CODEC_ID_VQC_fuzzer-6209269924233216
> 
> Found-by: continuous fuzzing process 
> https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer 
> ---
>  libavcodec/vqcdec.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/libavcodec/vqcdec.c b/libavcodec/vqcdec.c
> index 18cd99462e..c3bce87974 100644
> --- a/libavcodec/vqcdec.c
> +++ b/libavcodec/vqcdec.c
> @@ -71,6 +71,9 @@ static av_cold int vqc_decode_init(AVCodecContext * avctx)
>  static AVOnce init_static_once = AV_ONCE_INIT;
>  VqcContext *s = avctx->priv_data;
>  
> +if (avctx->width & 15)
> +return AVERROR_PATCHWELCOME;
> +
>  s->vectors = av_malloc((avctx->width * avctx->height * 3) / 2);
>  if (!s->vectors)
>  return AVERROR(ENOMEM);
> -- 
> 2.17.1

ok. please apply.

-- Peter
(A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B)


signature.asc
Description: PGP signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] lavd/v4l2: add HEVC streams support

2022-12-07 Thread Dmitry Buzdyk
Reminder

On Thu, 2022-11-10 at 01:40 +0600, Dima Buzdyk wrote:
> Signed-off-by: Dima Buzdyk 
> ---
>  libavcodec/hevc_parser.c  | 4 
>  libavdevice/v4l2-common.c | 3 +++
>  libavdevice/v4l2.c    | 2 +-
>  3 files changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/libavcodec/hevc_parser.c b/libavcodec/hevc_parser.c
> index 59f9a0ff3e..8f99a277a1 100644
> --- a/libavcodec/hevc_parser.c
> +++ b/libavcodec/hevc_parser.c
> @@ -330,6 +330,10 @@ static int hevc_parse(AVCodecParserContext *s,
> AVCodecContext *avctx,
>  if (!is_dummy_buf)
>  parse_nal_units(s, buf, buf_size, avctx);
>  
> +    if (s->flags & PARSER_FLAG_ONCE) {
> +    s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
> +    }
> +
>  *poutbuf  = buf;
>  *poutbuf_size = buf_size;
>  return next;
> diff --git a/libavdevice/v4l2-common.c b/libavdevice/v4l2-common.c
> index b5b4448a31..353e83efdd 100644
> --- a/libavdevice/v4l2-common.c
> +++ b/libavdevice/v4l2-common.c
> @@ -55,6 +55,9 @@ const struct fmt_map ff_fmt_conversion_table[] = {
>  #ifdef V4L2_PIX_FMT_H264
>  { AV_PIX_FMT_NONE,    AV_CODEC_ID_H264, V4L2_PIX_FMT_H264   
> },
>  #endif
> +#ifdef V4L2_PIX_FMT_HEVC
> +    { AV_PIX_FMT_NONE,    AV_CODEC_ID_HEVC, V4L2_PIX_FMT_HEVC   
> },
> +#endif
>  #ifdef V4L2_PIX_FMT_MPEG4
>  { AV_PIX_FMT_NONE,    AV_CODEC_ID_MPEG4,    V4L2_PIX_FMT_MPEG4  
> },
>  #endif
> diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
> index 5e85d1a2b3..5558435827 100644
> --- a/libavdevice/v4l2.c
> +++ b/libavdevice/v4l2.c
> @@ -972,7 +972,7 @@ static int v4l2_read_header(AVFormatContext *ctx)
>  if (codec_id == AV_CODEC_ID_RAWVIDEO)
>  st->codecpar->codec_tag =
>  avcodec_pix_fmt_to_codec_tag(st->codecpar->format);
> -    else if (codec_id == AV_CODEC_ID_H264) {
> +    else if (codec_id == AV_CODEC_ID_H264 || codec_id ==
> AV_CODEC_ID_HEVC) {
>  avpriv_stream_set_need_parsing(st,
> AVSTREAM_PARSE_FULL_ONCE);
>  }
>  if (desired_format == V4L2_PIX_FMT_YVU420)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [RFC PATCH] lavd/v4l2: cover all bufer if bytesperline is set by driver

2022-12-07 Thread Dmitry Buzdyk
Reminder

On Thu, 2022-11-10 at 01:36 +0600, Dima Buzdyk wrote:
> Some drivers may set bytesperline if hardware use padding bytes for
> alignment. In this case lavd/v4l2 will expect W*H bytes per frame,
> but
> driver will provide Pitch*H bytes which makes v4l2 unhappy.
> 
> This change adjusts frame width to cover entire data buffer aligning
> lavd/v4l2 expectations with data provided by the driver. As a result
> user will be able to get image stream from device, albeit having
> garbage in padding bytes.
> 
> Signed-off-by: Dima Buzdyk 
> ---
>  libavdevice/v4l2.c | 32 +++-
>  1 file changed, 27 insertions(+), 5 deletions(-)
> 
> diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
> index 5e85d1a2b3..b1e837f740 100644
> --- a/libavdevice/v4l2.c
> +++ b/libavdevice/v4l2.c
> @@ -83,7 +83,7 @@ struct video_data {
>  AVClass *class;
>  int fd;
>  int pixelformat; /* V4L2_PIX_FMT_* */
> -    int width, height;
> +    int width, height, pitch;
>  int frame_size;
>  int interlaced;
>  int top_field_first;
> @@ -202,7 +202,7 @@ fail:
>  }
>  
>  static int device_init(AVFormatContext *ctx, int *width, int
> *height,
> -   uint32_t pixelformat)
> +   int *pitch, uint32_t pixelformat)
>  {
>  struct video_data *s = ctx->priv_data;
>  struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE
> };
> @@ -224,6 +224,7 @@ static int device_init(AVFormatContext *ctx, int
> *width, int *height,
>     *width, *height, fmt.fmt.pix.width,
> fmt.fmt.pix.height);
>  *width = fmt.fmt.pix.width;
>  *height = fmt.fmt.pix.height;
> +    *pitch = fmt.fmt.pix.bytesperline;
>  }
>  
>  if (pixelformat != fmt.fmt.pix.pixelformat) {
> @@ -779,6 +780,7 @@ static int device_try_init(AVFormatContext *ctx,
>     enum AVPixelFormat pix_fmt,
>     int *width,
>     int *height,
> +   int *pitch,
>     uint32_t *desired_format,
>     enum AVCodecID *codec_id)
>  {
> @@ -787,7 +789,7 @@ static int device_try_init(AVFormatContext *ctx,
>  *desired_format = ff_fmt_ff2v4l(pix_fmt, ctx->video_codec_id);
>  
>  if (*desired_format) {
> -    ret = device_init(ctx, width, height, *desired_format);
> +    ret = device_init(ctx, width, height, pitch,
> *desired_format);
>  if (ret < 0) {
>  *desired_format = 0;
>  if (ret != AVERROR(EINVAL))
> @@ -804,7 +806,7 @@ static int device_try_init(AVFormatContext *ctx,
>     (char
> *)av_x_if_null(av_get_pix_fmt_name(ff_fmt_conversion_table[i].ff_fmt)
> , "none"));
>  
>  *desired_format =
> ff_fmt_conversion_table[i].v4l2_fmt;
> -    ret = device_init(ctx, width, height,
> *desired_format);
> +    ret = device_init(ctx, width, height, pitch,
> *desired_format);
>  if (ret >= 0)
>  break;
>  else if (ret != AVERROR(EINVAL))
> @@ -933,11 +935,13 @@ static int v4l2_read_header(AVFormatContext
> *ctx)
>  
>  s->width  = fmt.fmt.pix.width;
>  s->height = fmt.fmt.pix.height;
> +    s->pitch  = fmt.fmt.pix.bytesperline;
>  av_log(ctx, AV_LOG_VERBOSE,
>     "Setting frame size to %dx%d\n", s->width, s-
> >height);
>  }
>  
> -    res = device_try_init(ctx, pix_fmt, &s->width, &s->height,
> &desired_format, &codec_id);
> +    res = device_try_init(ctx, pix_fmt, &s->width, &s->height, &s-
> >pitch,
> +  &desired_format, &codec_id);
>  if (res < 0)
>  goto fail;
>  
> @@ -948,6 +952,24 @@ static int v4l2_read_header(AVFormatContext
> *ctx)
>  if (codec_id != AV_CODEC_ID_NONE && ctx->video_codec_id ==
> AV_CODEC_ID_NONE)
>  ctx->video_codec_id = codec_id;
>  
> +    /* If bytesperpixel is set by driver then set width co cover
> full
> + * buffer area even if there are garbage data to be displayed.
> + * It is better to display padding bytes and give application
> ability
> + * to crop image later than fail to display image stream
> completely */
> +    if (s->pitch) {
> +    int linesize;
> +
> +    pix_fmt = ff_fmt_v4l2ff(desired_format, codec_id);
> +    linesize = av_image_get_linesize(pix_fmt, s->width, 0);
> +    if (linesize > 0) {
> +    s->width = s->pitch * s->width / linesize;
> +
> +    av_log(ctx, AV_LOG_INFO,
> +   "Expand frame width to %dx%d to cover full
> buffer\n",
> +   s->width, s->height);
> +    }
> +    }
> +
>  if ((res = av_image_check_size(s->width, s->height, 0, ctx)) <
> 0)
>  goto fail;
>  

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above,