From a6cd154463bea7eb56d28192db4c8c6d83f67fd7 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 5 Apr 2015 20:51:00 +0200 Subject: [PATCH] h264: move the DPB init/uninit to init_context()/free_context() Currently, the DPB is initialized in alloc_tables() and uninitialized in free_tables(), but those functions manage frame size-dependent variables, so DPB management does not logically belong in there. Since we want the init/uninit to happen exactly once per the context lifetime, init_context()/free_context() are the proper place for this code. --- libavcodec/h264.c | 34 ++++++++++++++++------------------ libavcodec/h264_slice.c | 3 ++- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/libavcodec/h264.c b/libavcodec/h264.c index 5c676559e6..5acc4634d7 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -355,14 +355,6 @@ void ff_h264_free_tables(H264Context *h, int free_rbsp) av_buffer_pool_uninit(&h->motion_val_pool); av_buffer_pool_uninit(&h->ref_index_pool); - if (free_rbsp && h->DPB) { - for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) - ff_h264_unref_picture(h, &h->DPB[i]); - av_freep(&h->DPB); - } - - h->cur_pic_ptr = NULL; - for (i = 0; i < h->nb_slice_ctx; i++) { H264SliceContext *sl = &h->slice_ctx[i]; @@ -392,7 +384,7 @@ int ff_h264_alloc_tables(H264Context *h) { const int big_mb_num = h->mb_stride * (h->mb_height + 1); const int row_mb_num = h->mb_stride * 2 * h->avctx->thread_count; - int x, y, i; + int x, y; FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail) @@ -438,15 +430,6 @@ int ff_h264_alloc_tables(H264Context *h) if (!h->dequant4_coeff[0]) h264_init_dequant_tables(h); - if (!h->DPB) { - h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB)); - if (!h->DPB) - goto fail; - for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) - av_frame_unref(&h->DPB[i].f); - av_frame_unref(&h->cur_pic.f); - } - return 0; fail: @@ -613,6 +596,13 @@ static int h264_init_context(AVCodecContext *avctx, H264Context *h) return AVERROR(ENOMEM); } + h->DPB = av_mallocz_array(H264_MAX_PICTURE_COUNT, sizeof(*h->DPB)); + if (!h->DPB) + return AVERROR(ENOMEM); + for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) + av_frame_unref(&h->DPB[i].f); + av_frame_unref(&h->cur_pic.f); + for (i = 0; i < h->nb_slice_ctx; i++) h->slice_ctx[i].h264 = h; @@ -1756,6 +1746,14 @@ av_cold void ff_h264_free_context(H264Context *h) ff_h264_free_tables(h, 1); // FIXME cleanup init stuff perhaps + if (h->DPB) { + for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) + ff_h264_unref_picture(h, &h->DPB[i]); + av_freep(&h->DPB); + } + + h->cur_pic_ptr = NULL; + av_freep(&h->slice_ctx); h->nb_slice_ctx = 0; diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index d5b99ae817..01cfb9d06c 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -454,6 +454,7 @@ int ff_h264_update_thread_context(AVCodecContext *dst, if (!inited) { H264SliceContext *orig_slice_ctx = h->slice_ctx; + H264Picture *orig_DPB = h->DPB; for (i = 0; i < MAX_SPS_COUNT; i++) av_freep(h->sps_buffers + i); @@ -471,6 +472,7 @@ int ff_h264_update_thread_context(AVCodecContext *dst, h->cur_pic.tf.f = &h->cur_pic.f; h->slice_ctx = orig_slice_ctx; + h->DPB = orig_DPB; memset(&h->slice_ctx[0].er, 0, sizeof(h->slice_ctx[0].er)); memset(&h->slice_ctx[0].mb, 0, sizeof(h->slice_ctx[0].mb)); @@ -478,7 +480,6 @@ int ff_h264_update_thread_context(AVCodecContext *dst, memset(&h->slice_ctx[0].mb_padding, 0, sizeof(h->slice_ctx[0].mb_padding)); h->avctx = dst; - h->DPB = NULL; h->qscale_table_pool = NULL; h->mb_type_pool = NULL; h->ref_index_pool = NULL;