avcodec/mpegpicture: Add function to completely free MPEG-Picture

Also use said function in mpegvideo.c and mpegvideo_enc.c;
and make ff_free_picture_tables() static as it isn't needed anymore
outside of mpegpicture.c.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-01-26 19:02:37 +01:00
parent d0ceb90f44
commit 5a04c6a269
4 changed files with 34 additions and 43 deletions

View File

@ -30,6 +30,24 @@
#include "mpegpicture.h" #include "mpegpicture.h"
#include "mpegutils.h" #include "mpegutils.h"
static void av_noinline free_picture_tables(Picture *pic)
{
pic->alloc_mb_width =
pic->alloc_mb_height = 0;
av_buffer_unref(&pic->mb_var_buf);
av_buffer_unref(&pic->mc_mb_var_buf);
av_buffer_unref(&pic->mb_mean_buf);
av_buffer_unref(&pic->mbskip_table_buf);
av_buffer_unref(&pic->qscale_table_buf);
av_buffer_unref(&pic->mb_type_buf);
for (int i = 0; i < 2; i++) {
av_buffer_unref(&pic->motion_val_buf[i]);
av_buffer_unref(&pic->ref_index_buf[i]);
}
}
static int make_tables_writable(Picture *pic) static int make_tables_writable(Picture *pic)
{ {
int ret, i; int ret, i;
@ -240,7 +258,7 @@ int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
if (pic->qscale_table_buf) if (pic->qscale_table_buf)
if ( pic->alloc_mb_width != mb_width if ( pic->alloc_mb_width != mb_width
|| pic->alloc_mb_height != mb_height) || pic->alloc_mb_height != mb_height)
ff_free_picture_tables(pic); free_picture_tables(pic);
if (shared) { if (shared) {
av_assert0(pic->f->data[0]); av_assert0(pic->f->data[0]);
@ -285,7 +303,7 @@ int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
fail: fail:
av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n"); av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
ff_mpeg_unref_picture(avctx, pic); ff_mpeg_unref_picture(avctx, pic);
ff_free_picture_tables(pic); free_picture_tables(pic);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
@ -310,7 +328,7 @@ void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
av_buffer_unref(&pic->hwaccel_priv_buf); av_buffer_unref(&pic->hwaccel_priv_buf);
if (pic->needs_realloc) if (pic->needs_realloc)
ff_free_picture_tables(pic); free_picture_tables(pic);
memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
} }
@ -331,7 +349,7 @@ int ff_update_picture_tables(Picture *dst, Picture *src)
} }
if (ret < 0) { if (ret < 0) {
ff_free_picture_tables(dst); free_picture_tables(dst);
return ret; return ret;
} }
@ -450,22 +468,9 @@ int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
return ret; return ret;
} }
void ff_free_picture_tables(Picture *pic) void av_cold ff_mpv_picture_free(AVCodecContext *avctx, Picture *pic)
{ {
int i; free_picture_tables(pic);
ff_mpeg_unref_picture(avctx, pic);
pic->alloc_mb_width = av_frame_free(&pic->f);
pic->alloc_mb_height = 0;
av_buffer_unref(&pic->mb_var_buf);
av_buffer_unref(&pic->mc_mb_var_buf);
av_buffer_unref(&pic->mb_mean_buf);
av_buffer_unref(&pic->mbskip_table_buf);
av_buffer_unref(&pic->qscale_table_buf);
av_buffer_unref(&pic->mb_type_buf);
for (i = 0; i < 2; i++) {
av_buffer_unref(&pic->motion_val_buf[i]);
av_buffer_unref(&pic->ref_index_buf[i]);
}
} }

View File

@ -108,7 +108,7 @@ int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src); int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src);
void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *picture); void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *picture);
void ff_free_picture_tables(Picture *pic); void ff_mpv_picture_free(AVCodecContext *avctx, Picture *pic);
int ff_update_picture_tables(Picture *dst, Picture *src); int ff_update_picture_tables(Picture *dst, Picture *src);
int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared); int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared);

View File

@ -874,8 +874,6 @@ void ff_mpv_free_context_frame(MpegEncContext *s)
/* init common structure for both encoder and decoder */ /* init common structure for both encoder and decoder */
void ff_mpv_common_end(MpegEncContext *s) void ff_mpv_common_end(MpegEncContext *s)
{ {
int i;
if (!s) if (!s)
return; return;
@ -895,25 +893,14 @@ void ff_mpv_common_end(MpegEncContext *s)
return; return;
if (s->picture) { if (s->picture) {
for (i = 0; i < MAX_PICTURE_COUNT; i++) { for (int i = 0; i < MAX_PICTURE_COUNT; i++)
ff_free_picture_tables(&s->picture[i]); ff_mpv_picture_free(s->avctx, &s->picture[i]);
ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
av_frame_free(&s->picture[i].f);
}
} }
av_freep(&s->picture); av_freep(&s->picture);
ff_free_picture_tables(&s->last_picture); ff_mpv_picture_free(s->avctx, &s->last_picture);
ff_mpeg_unref_picture(s->avctx, &s->last_picture); ff_mpv_picture_free(s->avctx, &s->current_picture);
av_frame_free(&s->last_picture.f); ff_mpv_picture_free(s->avctx, &s->next_picture);
ff_free_picture_tables(&s->current_picture); ff_mpv_picture_free(s->avctx, &s->new_picture);
ff_mpeg_unref_picture(s->avctx, &s->current_picture);
av_frame_free(&s->current_picture.f);
ff_free_picture_tables(&s->next_picture);
ff_mpeg_unref_picture(s->avctx, &s->next_picture);
av_frame_free(&s->next_picture.f);
ff_free_picture_tables(&s->new_picture);
ff_mpeg_unref_picture(s->avctx, &s->new_picture);
av_frame_free(&s->new_picture.f);
s->context_initialized = 0; s->context_initialized = 0;
s->context_reinit = 0; s->context_reinit = 0;

View File

@ -941,8 +941,7 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++) for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
av_frame_free(&s->tmp_frames[i]); av_frame_free(&s->tmp_frames[i]);
ff_free_picture_tables(&s->new_picture); ff_mpv_picture_free(avctx, &s->new_picture);
ff_mpeg_unref_picture(avctx, &s->new_picture);
av_freep(&avctx->stats_out); av_freep(&avctx->stats_out);
av_freep(&s->ac_stats); av_freep(&s->ac_stats);