Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Subject: [FFmpeg-devel] [PATCH v2 07/71] avcodec/mpegpicture: Store linesize in ScratchpadContext
Date: Sat, 11 May 2024 22:50:31 +0200
Message-ID: <GV1P250MB0737DA5AD0AF89894DB929A18FE02@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <AS8P250MB074471DDEA29072B2586F0EF8FE02@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM>

The mpegvideo-based codecs currently require the linesize to be
constant (except when the frame dimensions change); one reason
for this is that certain scratch buffers whose size depend on
linesize are only allocated once and are presumed to be correctly
sized if the pointers are != NULL.

This commit changes this by storing the actual linesize these
buffers belong to and reallocating the buffers if it does not
suffice. This is not enough to actually support changing linesizes,
but it is a start. And it is a prerequisite for the next patch.

Also don't emit an error message in case the source ctx's
edge_emu_buffer is unset in ff_mpeg_update_thread_context().
It need not be an error at all; e.g. it is a perfectly normal
state in case a hardware acceleration is used as the scratch
buffers are not allocated in this case (it is easy to run into
this issue with MPEG-4) or if the src context was not initialized
at all (e.g. because the first packet contained garbage).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegpicture.c   | 19 ++++++++++++++-----
 libavcodec/mpegpicture.h   |  1 +
 libavcodec/mpegvideo.c     | 19 +++++++------------
 libavcodec/mpegvideo_dec.c | 19 +++++++------------
 4 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c
index 06b6daa01a..aa882cf747 100644
--- a/libavcodec/mpegpicture.c
+++ b/libavcodec/mpegpicture.c
@@ -89,12 +89,16 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
                             ScratchpadContext *sc, int linesize)
 {
 #   define EMU_EDGE_HEIGHT (4 * 70)
-    int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
+    int linesizeabs = FFABS(linesize);
+    int alloc_size = FFALIGN(linesizeabs + 64, 32);
+
+    if (linesizeabs <= sc->linesize)
+        return 0;
 
     if (avctx->hwaccel)
         return 0;
 
-    if (linesize < 24) {
+    if (linesizeabs < 24) {
         av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
         return AVERROR_PATCHWELCOME;
     }
@@ -102,6 +106,9 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
     if (av_image_check_size2(alloc_size, EMU_EDGE_HEIGHT, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx) < 0)
         return AVERROR(ENOMEM);
 
+    av_freep(&sc->edge_emu_buffer);
+    av_freep(&me->scratchpad);
+
     // edge emu needs blocksize + filter length - 1
     // (= 17x17 for  halfpel / 21x21 for H.264)
     // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
@@ -110,9 +117,11 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
     // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
     if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) ||
         !FF_ALLOCZ_TYPED_ARRAY(me->scratchpad,      alloc_size * 4 * 16 * 2)) {
+        sc->linesize = 0;
         av_freep(&sc->edge_emu_buffer);
         return AVERROR(ENOMEM);
     }
+    sc->linesize = linesizeabs;
 
     me->temp            = me->scratchpad;
     sc->rd_scratchpad   = me->scratchpad;
@@ -149,9 +158,9 @@ static int handle_pic_linesizes(AVCodecContext *avctx, Picture *pic,
         return -1;
     }
 
-    if (!sc->edge_emu_buffer &&
-        (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
-                                       pic->f->linesize[0])) < 0) {
+    ret = ff_mpeg_framesize_alloc(avctx, me, sc,
+                                  pic->f->linesize[0]);
+    if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR,
                "get_buffer() failed to allocate context scratch buffers.\n");
         ff_mpeg_unref_picture(pic);
diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h
index a457586be5..215e7388ef 100644
--- a/libavcodec/mpegpicture.h
+++ b/libavcodec/mpegpicture.h
@@ -38,6 +38,7 @@ typedef struct ScratchpadContext {
     uint8_t *rd_scratchpad;       ///< scratchpad for rate distortion mb decision
     uint8_t *obmc_scratchpad;
     uint8_t *b_scratchpad;        ///< scratchpad used for writing into write only buffers
+    int      linesize;            ///< linesize that the buffers in this context have been allocated for
 } ScratchpadContext;
 
 /**
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 7af823b8bd..130ccb4c97 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -443,6 +443,7 @@ static void free_duplicate_context(MpegEncContext *s)
     s->sc.rd_scratchpad =
     s->sc.b_scratchpad =
     s->sc.obmc_scratchpad = NULL;
+    s->sc.linesize = 0;
 
     av_freep(&s->dct_error_sum);
     av_freep(&s->me.map);
@@ -464,12 +465,9 @@ static void free_duplicate_contexts(MpegEncContext *s)
 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
 {
 #define COPY(a) bak->a = src->a
-    COPY(sc.edge_emu_buffer);
+    COPY(sc);
     COPY(me.scratchpad);
     COPY(me.temp);
-    COPY(sc.rd_scratchpad);
-    COPY(sc.b_scratchpad);
-    COPY(sc.obmc_scratchpad);
     COPY(me.map);
     COPY(me.score_map);
     COPY(blocks);
@@ -503,9 +501,9 @@ int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
         // exchange uv
         FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
     }
-    if (!dst->sc.edge_emu_buffer &&
-        (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
-                                       &dst->sc, dst->linesize)) < 0) {
+    ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
+                                  &dst->sc, dst->linesize);
+    if (ret < 0) {
         av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
                "scratch buffers.\n");
         return ret;
@@ -646,12 +644,9 @@ static void clear_context(MpegEncContext *s)
     s->ac_val[0] =
     s->ac_val[1] =
     s->ac_val[2] =NULL;
-    s->sc.edge_emu_buffer = NULL;
     s->me.scratchpad = NULL;
-    s->me.temp =
-    s->sc.rd_scratchpad =
-    s->sc.b_scratchpad =
-    s->sc.obmc_scratchpad = NULL;
+    s->me.temp = NULL;
+    memset(&s->sc, 0, sizeof(s->sc));
 
 
     s->bitstream_buffer = NULL;
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 4353f1fd68..31403d9acc 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -167,18 +167,13 @@ do {\
     }
 
     // linesize-dependent scratch buffer allocation
-    if (!s->sc.edge_emu_buffer)
-        if (s1->linesize) {
-            if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
-                                        &s->sc, s1->linesize) < 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
-                       "scratch buffers.\n");
-                return AVERROR(ENOMEM);
-            }
-        } else {
-            av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
-                   "be allocated due to unknown size.\n");
-        }
+    ret = ff_mpeg_framesize_alloc(s->avctx, &s->me,
+                                  &s->sc, s1->linesize);
+    if (ret < 0) {
+        av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
+               "scratch buffers.\n");
+        return ret;
+    }
 
     // MPEG-2/interlacing info
     memcpy(&s->progressive_sequence, &s1->progressive_sequence,
-- 
2.40.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:[~2024-05-11 20:52 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-11 20:23 [FFmpeg-devel] [PATCH v2 01/71] avcodec/ratecontrol: Fix double free on error Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 02/71] avcodec/ratecontrol: Pass RCContext directly in ff_rate_control_uninit() Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 03/71] avcodec/ratecontrol: Don't call ff_rate_control_uninit() ourselves Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 04/71] avcodec/mpegvideo, ratecontrol: Remove write-only skip_count Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 05/71] avcodec/ratecontrol: Avoid padding in RateControlEntry Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 06/71] avcodec/get_buffer: Remove redundant check Andreas Rheinhardt
2024-05-11 20:50 ` Andreas Rheinhardt [this message]
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 08/71] avcodec/mpegvideo_dec: Sync linesize and uvlinesize between threads Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 09/71] avcodec/mpegvideo_dec: Factor allocating dummy frames out Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 10/71] avcodec/mpegpicture: Mark dummy frames as such Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 11/71] avcodec/mpeg12dec: Allocate dummy frames for non-I fields Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 12/71] avcodec/mpegvideo_motion: Remove dead checks for existence of reference Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 13/71] avcodec/mpegvideo_motion: Optimize check away Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 14/71] " Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 15/71] avcodec/mpegvideo_motion: Avoid constant function argument Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 16/71] avcodec/msmpeg4enc: Only calculate coded_cbp when used Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 17/71] avcodec/mpegvideo: Only allocate coded_block when needed Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 18/71] avcodec/mpegvideo: Don't reset coded_block unnecessarily Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 19/71] avcodec/mpegvideo: Only allocate cbp_table, pred_dir_table when needed Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 20/71] avcodec/mpegpicture: Always reset motion val buffer Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 21/71] avcodec/mpegpicture: Always reset mbskip_table Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 22/71] avcodec/mpegvideo: Redo aligning mb_height for VC-1 Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 23/71] avcodec/mpegvideo, mpegpicture: Add buffer pool Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 24/71] avcodec/mpegpicture: Reindent after the previous commit Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 25/71] avcodec/mpegpicture: Use RefStruct-pool API Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 26/71] avcodec/h263: Move encoder-only part out of ff_h263_update_motion_val() Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 27/71] avcodec/h263, mpeg(picture|video): Only allocate mbskip_table for MPEG-4 Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 28/71] avcodec/mpegvideo: Reindent after the previous commit Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 29/71] avcodec/h263: Move setting mbskip_table to decoder/encoders Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 30/71] avcodec/mpegvideo: Restrict resetting mbskip_table to MPEG-4 decoder Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 31/71] avcodec/mpegvideo: Shorten variable names Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 32/71] avcodec/mpegpicture: Reduce value of MAX_PLANES define Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 33/71] avcodec/mpegpicture: Cache AVFrame.data and linesize values Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 34/71] avcodec/rv30, rv34, rv40: Avoid indirection Andreas Rheinhardt
2024-05-11 20:50 ` [FFmpeg-devel] [PATCH v2 35/71] avcodec/mpegvideo: Add const where appropriate Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 36/71] avcodec/vc1_pred: Remove unused function parameter Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 37/71] avcodec/mpegpicture: Improve error messages and code Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 38/71] avcodec/mpegpicture: Split ff_alloc_picture() into check and alloc part Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 39/71] avcodec/mpegvideo_enc: Pass AVFrame*, not Picture* to alloc_picture() Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 40/71] avcodec/mpegvideo_enc: Move copying properties " Andreas Rheinhardt
2024-05-12 19:55   ` Michael Niedermayer
2024-06-08 14:03     ` [FFmpeg-devel] [PATCH v3 " Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 41/71] avcodec/mpegpicture: Rename Picture->MPVPicture Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 42/71] avcodec/vc1_mc: Don't check AVFrame INTERLACE flags Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 43/71] avcodec/mpegpicture: Split MPVPicture into WorkPicture and ordinary Pic Andreas Rheinhardt
2024-06-23 22:28   ` Michael Niedermayer
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 44/71] avcodec/error_resilience: Deduplicate cleanup code Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 45/71] avcodec/mpegvideo_enc: Factor setting length of B frame chain out Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 46/71] avcodec/mpegvideo_enc: Return early when getting length of B frame chain Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 47/71] avcodec/mpegvideo_enc: Reindentation Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 48/71] avcodec/mpeg12dec: Don't initialize inter tables for IPU Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 49/71] avcodec/mpeg12dec: Only initialize IDCT " Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 50/71] avcodec/mpeg12dec: Remove write-only assignment Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 51/71] avcodec/mpeg12dec: Set out_format only once Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 52/71] avformat/riff: Declare VCR2 to be MPEG-2 Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 53/71] avcodec/mpegvideo_dec: Add close function for mpegvideo-decoders Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 54/71] avcodec/mpegpicture: Make MPVPicture refcounted Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 55/71] avcodec/mpeg4videoenc: Avoid branch for writing stuffing Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 56/71] avcodec/mpeg4videoenc: Simplify writing startcodes Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 57/71] avcodec/mpegpicture: Use ThreadProgress instead of ThreadFrame API Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 58/71] avcodec/mpegpicture: Avoid loop and branch when setting motion_val Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 59/71] avcodec/mpegpicture: Use union for b_scratchpad and rd_scratchpad Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 60/71] avcodec/mpegpicture: Avoid MotionEstContext in ff_mpeg_framesize_alloc() Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 61/71] avcodec/mpegvideo_enc: Unify initializing PutBitContexts Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 62/71] avcodec/mpeg12enc: Simplify writing startcodes Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 63/71] avcodec/mpegvideo_dec, rv34: Simplify check for "does pic exist?" Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 64/71] avcodec/mpegvideo_dec: Don't sync encoder-only coded_picture_number Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 65/71] avcodec/mpeg12dec: Pass Mpeg1Context* in mpeg_field_start() Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 66/71] avcodec/mpeg12dec: Don't initialize inter_scantable Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 67/71] avcodec/mpegvideo: Remove pblocks Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 68/71] avcodec/mpegvideo: Use enum for msmpeg4_version Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 69/71] avcodec/ituh263enc: Remove redundant check Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 70/71] avcodec/mpegvideo_enc: Binarize reference Andreas Rheinhardt
2024-05-11 20:51 ` [FFmpeg-devel] [PATCH v2 71/71] avcodec/vc1_pred: Fix indentation Andreas Rheinhardt
2024-06-11 20:59 ` [FFmpeg-devel] [PATCH v2 01/71] avcodec/ratecontrol: Fix double free on error Andreas Rheinhardt

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=GV1P250MB0737DA5AD0AF89894DB929A18FE02@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM \
    --to=andreas.rheinhardt@outlook.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