From c23da380a386e2cae2c757f06a2aebfd72451413 Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Tue, 3 Oct 2023 19:30:12 -0700 Subject: [PATCH] VP8: Allocate cpi->mt_current_mb_col array lazily Add the mt_current_mb_col_size field to VP8_COMP to record the size of the mt_current_mb_col array. Move the allocation of the mt_current_mb_col array from vp8_alloc_compressor_data() to vp8_encode_frame(), where the use of mt_current_mb_col starts. Allocate mt_current_mb_col right before use if mt_current_mb_col hasn't been allocated or if the current size is incorrect. Move the deallocation of the mt_current_mb_col array from dealloc_compressor_data() to vp8cx_remove_encoder_threads(). Move the TODO(https://crbug.com/1486441) comment from vp8/encoder/onyx_if.c to vp8/vp8_cx_iface.c. Change-Id: Ic5a0793278c2cc94876669aaa0dd732412876673 --- vp8/encoder/encodeframe.c | 9 +++++++++ vp8/encoder/ethreading.c | 3 +++ vp8/encoder/onyx_if.c | 21 --------------------- vp8/encoder/onyx_int.h | 1 + vp8/vp8_cx_iface.c | 2 ++ 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/vp8/encoder/encodeframe.c b/vp8/encoder/encodeframe.c index dc29945..3f0319a 100644 --- a/vp8/encoder/encodeframe.c +++ b/vp8/encoder/encodeframe.c @@ -750,6 +750,15 @@ void vp8_encode_frame(VP8_COMP *cpi) { vp8cx_init_mbrthread_data(cpi, x, cpi->mb_row_ei, cpi->encoding_thread_count); + if (cpi->mt_current_mb_col_size != cm->mb_rows) { + vpx_free(cpi->mt_current_mb_col); + cpi->mt_current_mb_col = NULL; + cpi->mt_current_mb_col_size = 0; + CHECK_MEM_ERROR( + &cpi->common.error, cpi->mt_current_mb_col, + vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows)); + cpi->mt_current_mb_col_size = cm->mb_rows; + } for (i = 0; i < cm->mb_rows; ++i) vpx_atomic_store_release(&cpi->mt_current_mb_col[i], -1); diff --git a/vp8/encoder/ethreading.c b/vp8/encoder/ethreading.c index 353496c..3362755 100644 --- a/vp8/encoder/ethreading.c +++ b/vp8/encoder/ethreading.c @@ -644,6 +644,9 @@ void vp8cx_remove_encoder_threads(VP8_COMP *cpi) { cpi->b_lpf_running = 0; /* free thread related resources */ + vpx_free(cpi->mt_current_mb_col); + cpi->mt_current_mb_col = NULL; + cpi->mt_current_mb_col_size = 0; vpx_free(cpi->h_event_start_encoding); cpi->h_event_start_encoding = NULL; vpx_free(cpi->h_event_end_encoding); diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c index c5e9970..890237f 100644 --- a/vp8/encoder/onyx_if.c +++ b/vp8/encoder/onyx_if.c @@ -442,11 +442,6 @@ static void dealloc_compressor_data(VP8_COMP *cpi) { vpx_free(cpi->mb.pip); cpi->mb.pip = 0; - -#if CONFIG_MULTITHREAD - vpx_free(cpi->mt_current_mb_col); - cpi->mt_current_mb_col = NULL; -#endif } static void enable_segmentation(VP8_COMP *cpi) { @@ -1224,17 +1219,6 @@ void vp8_alloc_compressor_data(VP8_COMP *cpi) { } else { cpi->mt_sync_range = 16; } - - if (cpi->oxcf.multi_threaded > 1) { - int i; - - vpx_free(cpi->mt_current_mb_col); - CHECK_MEM_ERROR(&cpi->common.error, cpi->mt_current_mb_col, - vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows)); - for (i = 0; i < cm->mb_rows; ++i) - vpx_atomic_init(&cpi->mt_current_mb_col[i], 0); - } - #endif vpx_free(cpi->tplist); @@ -1447,11 +1431,6 @@ void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) { last_h = cpi->oxcf.Height; prev_number_of_layers = cpi->oxcf.number_of_layers; - if (cpi->initial_width) { - // TODO(https://crbug.com/1486441): Allow changing thread counts; the - // allocation is done once in vp8_create_compressor(). - oxcf->multi_threaded = cpi->oxcf.multi_threaded; - } cpi->oxcf = *oxcf; switch (cpi->oxcf.Mode) { diff --git a/vp8/encoder/onyx_int.h b/vp8/encoder/onyx_int.h index 4304f05..2c6a55a 100644 --- a/vp8/encoder/onyx_int.h +++ b/vp8/encoder/onyx_int.h @@ -526,6 +526,7 @@ typedef struct VP8_COMP { #if CONFIG_MULTITHREAD /* multithread data */ vpx_atomic_int *mt_current_mb_col; + int mt_current_mb_col_size; int mt_sync_range; vpx_atomic_int b_multi_threaded; int encoding_thread_count; diff --git a/vp8/vp8_cx_iface.c b/vp8/vp8_cx_iface.c index 8950de0..470fe9e 100644 --- a/vp8/vp8_cx_iface.c +++ b/vp8/vp8_cx_iface.c @@ -488,6 +488,8 @@ static vpx_codec_err_t vp8e_set_config(vpx_codec_alg_priv_t *ctx, ctx->cfg = *cfg; set_vp8e_config(&ctx->oxcf, ctx->cfg, ctx->vp8_cfg, NULL); vp8_change_config(ctx->cpi, &ctx->oxcf); + // TODO(https://crbug.com/1486441): Change thread counts; + // vp8cx_create_encoder_threads() is called once in vp8_create_compressor(). ctx->cpi->common.error.setjmp = 0; return VPX_CODEC_OK; } -- 2.7.4