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