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
2525 #include "libavutil/imgutils.h"
2626
2727 #include "avcodec.h"
28−#include "motion_est.h"
2928 #include "mpegpicture.h"
3029 #include "refstruct.h"
3130
@@ -136,8 +135,8 @@ void ff_mpv_workpic_from_pic(MPVWorkPicture *wpic, MPVPicture *pic) · 104 unmodified lines
136135 set_workpic_from_pic(wpic, pic);
137136 }
138137
139−int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
140− ScratchpadContext *sc, int linesize)
138+int ff_mpv_framesize_alloc(AVCodecContext *avctx,
139+ ScratchpadContext *sc, int linesize)
141140 {
142141 # define EMU_EDGE_HEIGHT (4 * 70)
143142 int linesizeabs = FFABS(linesize);
@@ -158,7 +157,7 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, · 14 unmodified lines
158157 return AVERROR(ENOMEM);
159158
160159 av_freep(&sc->edge_emu_buffer);
161− av_freep(&me->scratchpad);
160+ av_freep(&sc->scratchpad_buf);
162161
163162 // edge emu needs blocksize + filter length - 1
164163 // (= 17x17 for halfpel / 21x21 for H.264)
@@ -167,16 +166,14 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, · 2 unmodified lines
167166 // linesize * interlaced * MBsize
168167 // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
169168 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)) {
171170 sc->linesize = 0;
172171 av_freep(&sc->edge_emu_buffer);
173172 return AVERROR(ENOMEM);
174173 }
175174 sc->linesize = linesizeabs;
176175
177− me->temp = me->scratchpad;
178− sc->b_scratchpad = me->scratchpad;
179− sc->obmc_scratchpad = me->scratchpad + 16;
176+ sc->obmc_scratchpad = sc->scratchpad_buf + 16;
180177
181178 return 0;
182179 }
@@ -238,14 +235,13 @@ static int alloc_picture_tables(BufferPoolContext *pools, MPVPicture *pic, · 55 unmodified lines
238235 }
239236
240237 int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *wpic,
241− MotionEstContext *me, ScratchpadContext *sc,
238+ ScratchpadContext *sc,
242239 BufferPoolContext *pools, int mb_height)
243240 {
244241 MPVPicture *pic = wpic->ptr;
245242 int ret;
246243
247− ret = ff_mpeg_framesize_alloc(avctx, me, sc,
248− pic->f->linesize[0]);
244+ ret = ff_mpv_framesize_alloc(avctx, sc, pic->f->linesize[0]);
249245 if (ret < 0)
250246 goto fail;
251247
libavcodec/mpegpicture.h+4−4
@@ -25,7 +25,6 @@ · 24 unmodified lines
2525 #include <stdint.h>
2626
2727 #include "avcodec.h"
28−#include "motion_est.h"
2928 #include "threadprogress.h"
3029
3130 #define MPV_MAX_PLANES 3
@@ -35,6 +34,7 @@ typedef struct ScratchpadContext { · 3 unmodified lines
3534 uint8_t *edge_emu_buffer; ///< temporary buffer for if MVs point to out-of-frame data
3635 uint8_t *obmc_scratchpad;
3736 union {
37+ uint8_t *scratchpad_buf; ///< the other *_scratchpad point into this buffer
3838 uint8_t *b_scratchpad; ///< scratchpad used for writing into write only buffers
3939 uint8_t *rd_scratchpad; ///< scratchpad for rate distortion mb decision
4040 };
@@ -121,7 +121,7 @@ struct FFRefStructPool *ff_mpv_alloc_pic_pool(int init_progress); · 80 unmodified lines
121121 * and set the MPVWorkPicture's fields.
122122 */
123123 int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *pic,
124− MotionEstContext *me, ScratchpadContext *sc,
124+ ScratchpadContext *sc,
125125 BufferPoolContext *pools, int mb_height);
126126
127127 /**
@@ -133,8 +133,8 @@ int ff_mpv_alloc_pic_accessories(AVCodecContext *avctx, MPVWorkPicture *pic, · 5 unmodified lines
133133 int ff_mpv_pic_check_linesize(void *logctx, const struct AVFrame *f,
134134 ptrdiff_t *linesizep, ptrdiff_t *uvlinesizep);
135135
136−int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
137− ScratchpadContext *sc, int linesize);
136+int ff_mpv_framesize_alloc(AVCodecContext *avctx,
137+ ScratchpadContext *sc, int linesize);
138138
139139 void ff_mpv_unref_picture(MPVWorkPicture *pic);
140140 void 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
438438 return;
439439
440440 av_freep(&s->sc.edge_emu_buffer);
441− av_freep(&s->me.scratchpad);
442− s->me.temp =
443− s->sc.b_scratchpad =
441+ av_freep(&s->sc.scratchpad_buf);
442+ s->me.temp = s->me.scratchpad =
444443 s->sc.obmc_scratchpad = NULL;
445444 s->sc.linesize = 0;
446445
@@ -465,8 +464,6 @@ static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src) · 18 unmodified lines
465464 {
466465 #define COPY(a) bak->a = src->a
467466 COPY(sc);
468− COPY(me.scratchpad);
469− COPY(me.temp);
470467 COPY(me.map);
471468 COPY(me.score_map);
472469 COPY(blocks);
@@ -500,8 +497,7 @@ int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src) · 27 unmodified lines
500497 // exchange uv
501498 FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
502499 }
503− ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
504− &dst->sc, dst->linesize);
500+ ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize);
505501 if (ret < 0) {
506502 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
507503 "scratch buffers.\n");
libavcodec/mpegvideo_dec.c+2−3
@@ -155,8 +155,7 @@ int ff_mpeg_update_thread_context(AVCodecContext *dst, · 154 unmodified lines
155155 }
156156
157157 // linesize-dependent scratch buffer allocation
158− ret = ff_mpeg_framesize_alloc(s->avctx, &s->me,
159− &s->sc, s1->linesize);
158+ ret = ff_mpv_framesize_alloc(s->avctx, &s->sc, s1->linesize);
160159 if (ret < 0) {
161160 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
162161 "scratch buffers.\n");
@@ -264,7 +263,7 @@ static int alloc_picture(MpegEncContext *s, MPVWorkPicture *dst, int reference) · 101 unmodified lines
264263 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height ||
265264 FFALIGN(s->mb_height, 2) == s->buffer_pools.alloc_mb_height);
266265 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,
268267 &s->buffer_pools, s->mb_height);
269268 if (ret < 0)
270269 goto fail;
libavcodec/mpegvideo_enc.c+5−2
@@ -1663,12 +1663,13 @@ static int select_input_picture(MpegEncContext *s) · 1662 unmodified lines
16631663 av_assert1(s->mb_width == s->buffer_pools.alloc_mb_width);
16641664 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height);
16651665 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,
16671667 &s->sc, &s->buffer_pools, s->mb_height);
16681668 if (ret < 0) {
16691669 ff_mpv_unref_picture(&s->cur_pic);
16701670 return ret;
16711671 }
1672+ s->me.temp = s->me.scratchpad = s->sc.scratchpad_buf;
16721673 s->picture_number = s->cur_pic.ptr->display_picture_number;
16731674
16741675 }
@@ -3616,9 +3617,11 @@ static int encode_picture(MpegEncContext *s) · 1941 unmodified lines
36163617
36173618 s->mb_intra=0; //for the rate distortion & bit compare functions
36183619 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);
36203622 if (ret < 0)
36213623 return ret;
3624+ slice->me.temp = slice->me.scratchpad = slice->sc.scratchpad_buf;
36223625 }
36233626
36243627 /* 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
543543 s->rd_total / (double)(avctx->width * avctx->height *
544544 avctx->frame_num));
545545
546− s->m.mb_type = NULL;
547− ff_mpv_common_end(&s->m);
548−
549546 av_freep(&s->m.me.scratchpad);
550547 av_freep(&s->m.me.map);
551548 av_freep(&s->mb_type);
552549 av_freep(&s->dummy);
553550 av_freep(&s->scratchbuf);
554551
552+ s->m.mb_type = NULL;
553+ ff_mpv_common_end(&s->m);
554+
555555 for (i = 0; i < 3; i++) {
556556 av_freep(&s->motion_val8[i]);
557557 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