History

avcodec/mpegpicture: Avoid MotionEstContext in ff_mpeg_framesize_alloc()

Only set the ScratchpadContext and let the users that need it (i.e. encoders) set the MotionEstContext stuff themselves. Also add an explicit pointer to ScratchpadContext to point to the allocated buffer so that none of the other scratchpad pointers is singled out as being used for the allocations.

Andreas Rheinhardt committed 2 years ago

Commit a05eebe1 parent d0f76e66 files · +24 −30Browse files

Unified · Split

6 files
  1. libavcodec/mpegpicture.c +7 −11
  2. libavcodec/mpegpicture.h +4 −4
  3. libavcodec/mpegvideo.c +3 −7
  4. libavcodec/mpegvideo_dec.c +2 −3
  5. libavcodec/mpegvideo_enc.c +5 −2
  6. libavcodec/svq1enc.c +3 −3
libavcodec/mpegpicture.c+7−11
@@ -25,7 +25,6 @@ · 24 unmodified lines
25#include "libavutil/imgutils.h"25#include "libavutil/imgutils.h"
26 26
27#include "avcodec.h"27#include "avcodec.h"
28#include "motion_est.h"
29#include "mpegpicture.h"28#include "mpegpicture.h"
30#include "refstruct.h"29#include "refstruct.h"
31 30
@@ -136,8 +135,8 @@ void ff_mpv_workpic_from_pic(MPVWorkPicture *wpic, MPVPicture *pic) · 104 unmodified lines
136 set_workpic_from_pic(wpic, pic);135 set_workpic_from_pic(wpic, pic);
137}136}
138 137
139int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,138int ff_mpv_framesize_alloc(AVCodecContext *avctx,
140 ScratchpadContext *sc, int linesize)139 ScratchpadContext *sc, int linesize)
141{140{
142# define EMU_EDGE_HEIGHT (4 * 70)141# define EMU_EDGE_HEIGHT (4 * 70)
143 int linesizeabs = FFABS(linesize);142 int linesizeabs = FFABS(linesize);
@@ -158,7 +157,7 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, · 14 unmodified lines
158 return AVERROR(ENOMEM);157 return AVERROR(ENOMEM);
159 158
160 av_freep(&sc->edge_emu_buffer);159 av_freep(&sc->edge_emu_buffer);
161 av_freep(&me->scratchpad);160 av_freep(&sc->scratchpad_buf);
162 161
163 // edge emu needs blocksize + filter length - 1162 // edge emu needs blocksize + filter length - 1
164 // (= 17x17 for halfpel / 21x21 for H.264)163 // (= 17x17 for halfpel / 21x21 for H.264)
@@ -167,16 +166,14 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, · 2 unmodified lines
167 // linesize * interlaced * MBsize166 // linesize * interlaced * MBsize
168 // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines167 // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
169 if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) ||168 if (!FF_ALLOCZ_TYPED_ARRAY(sc->edge_emu_buffer, alloc_size * EMU_EDGE_HEIGHT) ||
170 !FF_ALLOCZ_TYPED_ARRAY(me->scratchpad, alloc_size * 4 * 16 * 2)) {169 !FF_ALLOCZ_TYPED_ARRAY(sc->scratchpad_buf, alloc_size * 4 * 16 * 2)) {
171 sc->linesize = 0;170 sc->linesize = 0;
172 av_freep(&sc->edge_emu_buffer);171 av_freep(&sc->edge_emu_buffer);
173 return AVERROR(ENOMEM);172 return AVERROR(ENOMEM);
174 }173 }
175 sc->linesize = linesizeabs;174 sc->linesize = linesizeabs;
176 175
177 me->temp = me->scratchpad;176 sc->obmc_scratchpad = sc->scratchpad_buf + 16;
178 sc->b_scratchpad = me->scratchpad;
179 sc->obmc_scratchpad = me->scratchpad + 16;
180 177
181 return 0;178 return 0;
182}179}
@@ -238,14 +235,13 @@ static int alloc_picture_tables(BufferPoolContext *pools, MPVPicture *pic, · 55 unmodified lines
238}235}
239 236
240int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *wpic,237int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *wpic,
241 MotionEstContext *me, ScratchpadContext *sc,238 ScratchpadContext *sc,
242 BufferPoolContext *pools, int mb_height)239 BufferPoolContext *pools, int mb_height)
243{240{
244 MPVPicture *pic = wpic->ptr;241 MPVPicture *pic = wpic->ptr;
245 int ret;242 int ret;
246 243
247 ret = ff_mpeg_framesize_alloc(avctx, me, sc,244 ret = ff_mpv_framesize_alloc(avctx, sc, pic->f->linesize[0]);
248 pic->f->linesize[0]);
249 if (ret < 0)245 if (ret < 0)
250 goto fail;246 goto fail;
251 247
libavcodec/mpegpicture.h+4−4
@@ -25,7 +25,6 @@ · 24 unmodified lines
25#include <stdint.h>25#include <stdint.h>
26 26
27#include "avcodec.h"27#include "avcodec.h"
28#include "motion_est.h"
29#include "threadprogress.h"28#include "threadprogress.h"
30 29
31#define MPV_MAX_PLANES 330#define MPV_MAX_PLANES 3
@@ -35,6 +34,7 @@ typedef struct ScratchpadContext { · 3 unmodified lines
35 uint8_t *edge_emu_buffer; ///< temporary buffer for if MVs point to out-of-frame data34 uint8_t *edge_emu_buffer; ///< temporary buffer for if MVs point to out-of-frame data
36 uint8_t *obmc_scratchpad;35 uint8_t *obmc_scratchpad;
37 union {36 union {
37 uint8_t *scratchpad_buf; ///< the other *_scratchpad point into this buffer
38 uint8_t *b_scratchpad; ///< scratchpad used for writing into write only buffers38 uint8_t *b_scratchpad; ///< scratchpad used for writing into write only buffers
39 uint8_t *rd_scratchpad; ///< scratchpad for rate distortion mb decision39 uint8_t *rd_scratchpad; ///< scratchpad for rate distortion mb decision
40 };40 };
@@ -121,7 +121,7 @@ struct FFRefStructPool *ff_mpv_alloc_pic_pool(int init_progress); · 80 unmodified lines
121 * and set the MPVWorkPicture's fields.121 * and set the MPVWorkPicture's fields.
122 */122 */
123int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *pic,123int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *pic,
124 MotionEstContext *me, ScratchpadContext *sc,124 ScratchpadContext *sc,
125 BufferPoolContext *pools, int mb_height);125 BufferPoolContext *pools, int mb_height);
126 126
127/**127/**
@@ -133,8 +133,8 @@ int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *pic, · 5 unmodified lines
133int ff_mpv_pic_check_linesize(void *logctx, const struct AVFrame *f,133int ff_mpv_pic_check_linesize(void *logctx, const struct AVFrame *f,
134 ptrdiff_t *linesizep, ptrdiff_t *uvlinesizep);134 ptrdiff_t *linesizep, ptrdiff_t *uvlinesizep);
135 135
136int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,136int ff_mpv_framesize_alloc(AVCodecContext *avctx,
137 ScratchpadContext *sc, int linesize);137 ScratchpadContext *sc, int linesize);
138 138
139void ff_mpv_unref_picture(MPVWorkPicture *pic);139void ff_mpv_unref_picture(MPVWorkPicture *pic);
140void ff_mpv_workpic_from_pic(MPVWorkPicture *wpic, MPVPicture *pic);140void ff_mpv_workpic_from_pic(MPVWorkPicture *wpic, MPVPicture *pic);
libavcodec/mpegvideo.c+3−7
@@ -438,9 +438,8 @@ static void free_duplicate_context(MpegEncContext *s) · 437 unmodified lines
438 return;438 return;
439 439
440 av_freep(&s->sc.edge_emu_buffer);440 av_freep(&s->sc.edge_emu_buffer);
441 av_freep(&s->me.scratchpad);441 av_freep(&s->sc.scratchpad_buf);
442 s->me.temp =442 s->me.temp = s->me.scratchpad =
443 s->sc.b_scratchpad =
444 s->sc.obmc_scratchpad = NULL;443 s->sc.obmc_scratchpad = NULL;
445 s->sc.linesize = 0;444 s->sc.linesize = 0;
446 445
@@ -465,8 +464,6 @@ static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src) · 18 unmodified lines
465{464{
466#define COPY(a) bak->a = src->a465#define COPY(a) bak->a = src->a
467 COPY(sc);466 COPY(sc);
468 COPY(me.scratchpad);
469 COPY(me.temp);
470 COPY(me.map);467 COPY(me.map);
471 COPY(me.score_map);468 COPY(me.score_map);
472 COPY(blocks);469 COPY(blocks);
@@ -500,8 +497,7 @@ int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src) · 27 unmodified lines
500 // exchange uv497 // exchange uv
501 FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);498 FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
502 }499 }
503 ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,500 ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize);
504 &dst->sc, dst->linesize);
505 if (ret < 0) {501 if (ret < 0) {
506 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "502 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
507 "scratch buffers.\n");503 "scratch buffers.\n");
libavcodec/mpegvideo_dec.c+2−3
@@ -155,8 +155,7 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst, · 154 unmodified lines
155 }155 }
156 156
157 // linesize-dependent scratch buffer allocation157 // linesize-dependent scratch buffer allocation
158 ret = ff_mpeg_framesize_alloc(s->avctx, &s->me,158 ret = ff_mpv_framesize_alloc(s->avctx, &s->sc, s1->linesize);
159 &s->sc, s1->linesize);
160 if (ret < 0) {159 if (ret < 0) {
161 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "160 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
162 "scratch buffers.\n");161 "scratch buffers.\n");
@@ -264,7 +263,7 @@ static int alloc_picture(MpegEncContext *s, MPVWorkPicture *dst, int reference) · 101 unmodified lines
264 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height ||263 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height ||
265 FFALIGN(s->mb_height, 2) == s->buffer_pools.alloc_mb_height);264 FFALIGN(s->mb_height, 2) == s->buffer_pools.alloc_mb_height);
266 av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);265 av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);
267 ret = ff_mpv_alloc_pic_accessories(s->avctx, dst, &s->me, &s->sc,266 ret = ff_mpv_alloc_pic_accessories(s->avctx, dst, &s->sc,
268 &s->buffer_pools, s->mb_height);267 &s->buffer_pools, s->mb_height);
269 if (ret < 0)268 if (ret < 0)
270 goto fail;269 goto fail;
libavcodec/mpegvideo_enc.c+5−2
@@ -1663,12 +1663,13 @@ static int select_input_picture(MpegEncContext *s) · 1662 unmodified lines
1663 av_assert1(s->mb_width == s->buffer_pools.alloc_mb_width);1663 av_assert1(s->mb_width == s->buffer_pools.alloc_mb_width);
1664 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height);1664 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height);
1665 av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);1665 av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);
1666 ret = ff_mpv_alloc_pic_accessories(s->avctx, &s->cur_pic, &s->me,1666 ret = ff_mpv_alloc_pic_accessories(s->avctx, &s->cur_pic,
1667 &s->sc, &s->buffer_pools, s->mb_height);1667 &s->sc, &s->buffer_pools, s->mb_height);
1668 if (ret < 0) {1668 if (ret < 0) {
1669 ff_mpv_unref_picture(&s->cur_pic);1669 ff_mpv_unref_picture(&s->cur_pic);
1670 return ret;1670 return ret;
1671 }1671 }
1672 s->me.temp = s->me.scratchpad = s->sc.scratchpad_buf;
1672 s->picture_number = s->cur_pic.ptr->display_picture_number;1673 s->picture_number = s->cur_pic.ptr->display_picture_number;
1673 1674
1674 }1675 }
@@ -3616,9 +3617,11 @@ static int encode_picture(MpegEncContext *s) · 1941 unmodified lines
3616 3617
3617 s->mb_intra=0; //for the rate distortion & bit compare functions3618 s->mb_intra=0; //for the rate distortion & bit compare functions
3618 for(i=1; i<context_count; i++){3619 for(i=1; i<context_count; i++){
3619 ret = ff_update_duplicate_context(s->thread_context[i], s);3620 MpegEncContext *const slice = s->thread_context[i];
3621 ret = ff_update_duplicate_context(slice, s);
3620 if (ret < 0)3622 if (ret < 0)
3621 return ret;3623 return ret;
3624 slice->me.temp = slice->me.scratchpad = slice->sc.scratchpad_buf;
3622 }3625 }
3623 3626
3624 /* Estimate motion for every MB */3627 /* Estimate motion for every MB */
libavcodec/svq1enc.c+3−3
@@ -543,15 +543,15 @@ static av_cold int svq1_encode_end(AVCodecContext *avctx) · 542 unmodified lines
543 s->rd_total / (double)(avctx->width * avctx->height *543 s->rd_total / (double)(avctx->width * avctx->height *
544 avctx->frame_num));544 avctx->frame_num));
545 545
546 s->m.mb_type = NULL;
547 ff_mpv_common_end(&s->m);
548
549 av_freep(&s->m.me.scratchpad);546 av_freep(&s->m.me.scratchpad);
550 av_freep(&s->m.me.map);547 av_freep(&s->m.me.map);
551 av_freep(&s->mb_type);548 av_freep(&s->mb_type);
552 av_freep(&s->dummy);549 av_freep(&s->dummy);
553 av_freep(&s->scratchbuf);550 av_freep(&s->scratchbuf);
554 551
552 s->m.mb_type = NULL;
553 ff_mpv_common_end(&s->m);
554
555 for (i = 0; i < 3; i++) {555 for (i = 0; i < 3; i++) {
556 av_freep(&s->motion_val8[i]);556 av_freep(&s->motion_val8[i]);
557 av_freep(&s->motion_val16[i]);557 av_freep(&s->motion_val16[i]);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t