Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 5/8] lavc/encode: add an encoder-specific get_buffer() variant
Date: Wed, 23 Mar 2022 16:57:17 +0100
Message-ID: <20220323155720.20017-5-anton@khirnov.net> (raw)
In-Reply-To: <20220323155720.20017-1-anton@khirnov.net>

Several encoders (roqvideo, svq1, snow, and the mpegvideo family)
currently call ff_get_buffer(). However this function is written
assuming it is called by a decoder. Though nothing has been obviously
broken by this until now, this may change in the future.

To avoid potential future issues, introduce a simple encode-specific
wrapper around avcodec_default_get_buffer2() and enforce its use in
encoders.
---
 libavcodec/decode.c      |  2 ++
 libavcodec/encode.c      | 34 ++++++++++++++++++++++++++++++++++
 libavcodec/encode.h      |  5 +++++
 libavcodec/mpegpicture.c | 16 +++++++++-------
 libavcodec/roqvideoenc.c |  4 ++--
 libavcodec/snow.c        |  8 ++++++--
 libavcodec/svq1enc.c     |  4 ++--
 7 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index fffad5f700..3733e6d4b8 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1405,6 +1405,8 @@ int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
     int override_dimensions = 1;
     int ret;
 
+    av_assert0(av_codec_is_decoder(avctx->codec));
+
     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
         if ((unsigned)avctx->width > INT_MAX - STRIDE_ALIGN ||
             (ret = av_image_check_size2(FFALIGN(avctx->width, STRIDE_ALIGN), avctx->height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) < 0 || avctx->pix_fmt<0) {
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 837ffaa40d..ec8d06d068 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -572,3 +572,37 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
     return 0;
 }
+
+int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
+{
+    int ret;
+
+    switch (avctx->codec->type) {
+    case AVMEDIA_TYPE_VIDEO:
+        frame->format = avctx->pix_fmt;
+        if (frame->width <= 0 || frame->height <= 0) {
+            frame->width  = FFMAX(avctx->width,  avctx->coded_width);
+            frame->height = FFMAX(avctx->height, avctx->coded_height);
+        }
+
+        break;
+    case AVMEDIA_TYPE_AUDIO:
+        frame->sample_rate = avctx->sample_rate;
+        frame->format      = avctx->sample_fmt;
+        if (!frame->ch_layout.nb_channels) {
+            int ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
+            if (ret < 0)
+                return ret;
+        }
+        break;
+    }
+
+    ret = avcodec_default_get_buffer2(avctx, frame, 0);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        av_frame_unref(frame);
+        return ret;
+    }
+
+    return 0;
+}
diff --git a/libavcodec/encode.h b/libavcodec/encode.h
index 97b3acf9df..b2536bf0f3 100644
--- a/libavcodec/encode.h
+++ b/libavcodec/encode.h
@@ -44,6 +44,11 @@ int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame);
  */
 int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags);
 
+/**
+ * Allocate buffers for a frame. Encoder equivalent to ff_get_buffer().
+ */
+int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame);
+
 /**
  * Check AVPacket size and allocate data.
  *
diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index 681ccc2b82..aaa1df0bd8 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -26,6 +26,7 @@
 #include "libavutil/imgutils.h"
 
 #include "avcodec.h"
+#include "encode.h"
 #include "motion_est.h"
 #include "mpegpicture.h"
 #include "mpegutils.h"
@@ -123,14 +124,15 @@ static int alloc_frame_buffer(AVCodecContext *avctx,  Picture *pic,
     int r, ret;
 
     pic->tf.f = pic->f;
-    if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
-        avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
-        avctx->codec_id != AV_CODEC_ID_MSS2) {
-        if (edges_needed) {
-            pic->f->width  = avctx->width  + 2 * EDGE_WIDTH;
-            pic->f->height = avctx->height + 2 * EDGE_WIDTH;
-        }
 
+    if (edges_needed) {
+        pic->f->width  = avctx->width  + 2 * EDGE_WIDTH;
+        pic->f->height = avctx->height + 2 * EDGE_WIDTH;
+
+        r = ff_encode_alloc_frame(avctx, pic->f);
+    } else if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
+               avctx->codec_id != AV_CODEC_ID_VC1IMAGE  &&
+               avctx->codec_id != AV_CODEC_ID_MSS2) {
         r = ff_thread_get_ext_buffer(avctx, &pic->tf,
                                      pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
     } else {
diff --git a/libavcodec/roqvideoenc.c b/libavcodec/roqvideoenc.c
index ef7861088d..95db514140 100644
--- a/libavcodec/roqvideoenc.c
+++ b/libavcodec/roqvideoenc.c
@@ -1081,8 +1081,8 @@ static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     if (enc->first_frame) {
         /* Alloc memory for the reconstruction data (we must know the stride
          for that) */
-        if ((ret = ff_get_buffer(avctx, roq->current_frame, 0)) < 0 ||
-            (ret = ff_get_buffer(avctx, roq->last_frame,    0)) < 0)
+        if ((ret = ff_encode_alloc_frame(avctx, roq->current_frame)) < 0 ||
+            (ret = ff_encode_alloc_frame(avctx, roq->last_frame   )) < 0)
             return ret;
 
         /* Before the first video frame, write a "video info" chunk */
diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index 1224b95491..bc0d9a8983 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -23,6 +23,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/thread.h"
 #include "avcodec.h"
+#include "encode.h"
 #include "me_cmp.h"
 #include "snow_dwt.h"
 #include "internal.h"
@@ -76,8 +77,11 @@ int ff_snow_get_buffer(SnowContext *s, AVFrame *frame)
     if (edges_needed) {
         frame->width  += 2 * EDGE_WIDTH;
         frame->height += 2 * EDGE_WIDTH;
-    }
-    if ((ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+
+        ret = ff_encode_alloc_frame(s->avctx, frame);
+    } else
+        ret = ff_get_buffer(s->avctx, frame, AV_GET_BUFFER_FLAG_REF);
+    if (ret < 0)
         return ret;
     if (edges_needed) {
         for (i = 0; frame->data[i]; i++) {
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index 706bc2e42e..cb0d0308fc 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -595,12 +595,12 @@ static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     }
 
     if (!s->current_picture->data[0]) {
-        if ((ret = ff_get_buffer(avctx, s->current_picture, 0)) < 0) {
+        if ((ret = ff_encode_alloc_frame(avctx, s->current_picture)) < 0) {
             return ret;
         }
     }
     if (!s->last_picture->data[0]) {
-        ret = ff_get_buffer(avctx, s->last_picture, 0);
+        ret = ff_encode_alloc_frame(avctx, s->last_picture);
         if (ret < 0)
             return ret;
     }
-- 
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-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2022-03-23 15:57 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23 15:57 [FFmpeg-devel] [PATCH 1/8] lavc/avcodec: simplify codec id/type validity checking Anton Khirnov
2022-03-23 15:57 ` [FFmpeg-devel] [PATCH 2/8] lavc/avcodec: only allocate the encoding frame for encoders Anton Khirnov
2022-03-23 16:29   ` James Almer
2022-04-11  8:39     ` [FFmpeg-devel] [PATCH] lavc/encode: drop EncodeSimpleContext Anton Khirnov
2022-04-11  9:16       ` Paul B Mahol
2022-04-11 16:32       ` James Almer
2022-03-23 15:57 ` [FFmpeg-devel] [PATCH 3/8] lavc: move default get_buffer2() to its own file Anton Khirnov
2022-03-23 15:57 ` [FFmpeg-devel] [PATCH 4/8] lavc/snow: only allocate mconly_picture for decoding Anton Khirnov
2022-03-24 23:07   ` Michael Niedermayer
2022-04-11  8:49     ` [FFmpeg-devel] [PATCH] " Anton Khirnov
2022-04-11 19:28       ` Michael Niedermayer
2022-04-13 10:21         ` Anton Khirnov
2022-03-23 15:57 ` Anton Khirnov [this message]
2022-03-23 16:26   ` [FFmpeg-devel] [PATCH 5/8] lavc/encode: add an encoder-specific get_buffer() variant James Almer
2022-04-11  9:05     ` [FFmpeg-devel] [PATCH] " Anton Khirnov
2022-03-23 15:57 ` [FFmpeg-devel] [PATCH 6/8] lavc/avcodec: only allocate decoding packets for decoders Anton Khirnov
2022-04-13 14:51   ` Andreas Rheinhardt
2022-03-23 15:57 ` [FFmpeg-devel] [PATCH 7/8] lavc/pthread_frame: do not copy AVCodecInternal contents Anton Khirnov
2022-03-23 15:57 ` [FFmpeg-devel] [PATCH 8/8] lavc: drop a confusing message about "thread emulation" Anton Khirnov
2022-04-13 10:23 ` [FFmpeg-devel] [PATCH 1/8] lavc/avcodec: simplify codec id/type validity checking Anton Khirnov
2022-06-05  5:23 ` Soft Works
2022-06-05  7:01 ` Anton Khirnov
2022-06-05  7:54   ` Soft Works
2022-06-05  7:59     ` Soft Works
2022-06-05  8:20   ` Anton Khirnov
2022-06-05  8:55     ` Paul B Mahol
2022-06-05  8:55     ` Soft Works
2022-06-05  9:15     ` Soft Works
2022-06-05 10:42     ` Anton Khirnov
2022-06-05 10:55       ` Soft Works
2022-06-05 11:10       ` Soft Works
2022-06-05 13:20       ` Anton Khirnov
2022-06-05 14:06         ` Soft Works

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=20220323155720.20017-5-anton@khirnov.net \
    --to=anton@khirnov.net \
    --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