encoder: clear cost and SMS tree pointers with their buffers alloc_compressor_data() frees td.mv_costs_alloc and, when the encoder uses only key frames (kf_max_dist 0), does not allocate it again, but td.mb.mv_costs kept pointing at the freed block. av1_change_config() also frees the simple-motion-search tree before calling it, and av1_setup_sms_tree() returns early for key-frame-only encoding, leaving td.sms_root pointing into the freed tree. Growing the frame size with aom_codec_enc_config_set() while switching to kf_max_dist 0 made the next frame write through both pointers. Make the pointers follow their buffers: assign mb.mv_costs from mv_costs_alloc unconditionally, clear mb.mv_costs, mb.dv_costs and sms_root wherever the buffers they point into are freed, and free the tree in av1_setup_sms_tree() when it is not needed. Worker threads copy the main thread's costs only when they exist; the code using them already handles NULL. The buffers are freed rather than allocated again, so the encoder cannot go back to inter frames afterwards: this relies on https://aomedia-review.googlesource.com/c/aom/+/218983, which rejects raising kf_max_dist from 0. Without it, raising kf_max_dist again still crashes. TAG=agy CONV=74ece8ab-3f39-4cda-9df8-8325c945d9d9 Bug: 573427857 Change-Id: I50b614033fda784ad76bedbeeddac86a13abf21d
diff --git a/av1/encoder/context_tree.c b/av1/encoder/context_tree.c index 3310384..0075fcc 100644 --- a/av1/encoder/context_tree.c +++ b/av1/encoder/context_tree.c
@@ -235,7 +235,10 @@ // The structure 'sms_tree' is used to store the simple motion search data for // partition pruning in inter frames. Hence, the memory allocations and // initializations related to it are avoided for allintra encoding mode. - if (cpi->oxcf.kf_cfg.key_freq_max == 0) return 0; + if (cpi->oxcf.kf_cfg.key_freq_max == 0) { + av1_free_sms_tree(td); + return 0; + } AV1_COMMON *const cm = &cpi->common; const int stat_generation_stage = is_stat_generation_stage(cpi); @@ -291,4 +294,5 @@ void av1_free_sms_tree(ThreadData *td) { aom_free(td->sms_tree); td->sms_tree = NULL; + td->sms_root = NULL; }
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h index 5d14e5d..1295aed 100644 --- a/av1/encoder/encoder_alloc.h +++ b/av1/encoder/encoder_alloc.h
@@ -73,8 +73,9 @@ if (cpi->oxcf.kf_cfg.key_freq_max != 0) { CHECK_MEM_ERROR(cm, cpi->td.mv_costs_alloc, (MvCosts *)aom_calloc(1, sizeof(*cpi->td.mv_costs_alloc))); - cpi->td.mb.mv_costs = cpi->td.mv_costs_alloc; } + // mb.mv_costs must not keep pointing to the buffer freed above. + cpi->td.mb.mv_costs = cpi->td.mv_costs_alloc; av1_setup_shared_coeff_buffer(cm->seq_params, &cpi->td.shared_coeff_buf, cm->error); @@ -338,8 +339,10 @@ aom_free(cpi->td.mv_costs_alloc); cpi->td.mv_costs_alloc = NULL; + cpi->td.mb.mv_costs = NULL; aom_free(cpi->td.dv_costs_alloc); cpi->td.dv_costs_alloc = NULL; + cpi->td.mb.dv_costs = NULL; aom_free(cpi->td.mb.sb_stats_cache); cpi->td.mb.sb_stats_cache = NULL; @@ -601,8 +604,10 @@ } aom_free(td->mv_costs_alloc); td->mv_costs_alloc = NULL; + td->mb.mv_costs = NULL; aom_free(td->dv_costs_alloc); td->dv_costs_alloc = NULL; + td->mb.dv_costs = NULL; aom_free(td->counts); av1_free_pmc(td->firstpass_ctx, num_planes); td->firstpass_ctx = NULL;
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c index 8ca1efc..5f90a06 100644 --- a/av1/encoder/ethread.c +++ b/av1/encoder/ethread.c
@@ -1545,10 +1545,12 @@ if (cpi->sf.inter_sf.mv_cost_upd_level != INTERNAL_COST_UPD_OFF) { aom_free(thread_data->td->mv_costs_alloc); thread_data->td->mv_costs_alloc = NULL; + thread_data->td->mb.mv_costs = NULL; } if (cpi->sf.intra_sf.dv_cost_upd_level != INTERNAL_COST_UPD_OFF) { aom_free(thread_data->td->dv_costs_alloc); thread_data->td->dv_costs_alloc = NULL; + thread_data->td->mb.dv_costs = NULL; } } av1_dealloc_mb_data(&thread_data->td->mb, av1_num_planes(&cpi->common)); @@ -1622,11 +1624,15 @@ // Keep these conditional expressions in sync with the corresponding ones // in accumulate_counters_enc_workers(). if (cpi->sf.inter_sf.mv_cost_upd_level != INTERNAL_COST_UPD_OFF) { - CHECK_MEM_ERROR( - cm, thread_data->td->mv_costs_alloc, - (MvCosts *)aom_malloc(sizeof(*thread_data->td->mv_costs_alloc))); - thread_data->td->mb.mv_costs = thread_data->td->mv_costs_alloc; - *thread_data->td->mb.mv_costs = *cpi->td.mb.mv_costs; + // mv_costs is not allocated when every frame is a key frame. + thread_data->td->mb.mv_costs = NULL; + if (cpi->td.mb.mv_costs != NULL) { + CHECK_MEM_ERROR( + cm, thread_data->td->mv_costs_alloc, + (MvCosts *)aom_malloc(sizeof(*thread_data->td->mv_costs_alloc))); + thread_data->td->mb.mv_costs = thread_data->td->mv_costs_alloc; + *thread_data->td->mb.mv_costs = *cpi->td.mb.mv_costs; + } } if (cpi->sf.intra_sf.dv_cost_upd_level != INTERNAL_COST_UPD_OFF) { // Reset dv_costs to NULL for worker threads when dv cost update is
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc index c0eb48f..c0db30a 100644 --- a/test/encode_api_test.cc +++ b/test/encode_api_test.cc
@@ -6118,4 +6118,49 @@ } #endif // !CONFIG_REALTIME_ONLY +TEST(EncodeAPI, ConfigSetAllKeyFramesAndLargerSize) { + // Growing the frame size reallocates the compressor data and frees the + // motion vector costs and the simple-motion-search tree; reconfiguring to all + // key frames (kf_max_dist 0) means the costs are not reallocated. A later + // frame, on the main thread and on worker threads, must not use the freed + // buffers. + for (const unsigned int threads : { 1u, 4u }) { + SCOPED_TRACE(threads); + aom_codec_iface_t *iface = aom_codec_av1_cx(); + aom_codec_enc_cfg_t cfg; + ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, kUsage), AOM_CODEC_OK); + cfg.g_w = 16; + cfg.g_h = 16; + cfg.g_lag_in_frames = 0; + cfg.g_threads = threads; + aom_codec_ctx_t enc; + ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK); + ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, 6), AOM_CODEC_OK); + ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ROW_MT, 1), AOM_CODEC_OK); + ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_TILE_COLUMNS, 1), AOM_CODEC_OK); + + aom_image_t *img = + aom_img_alloc(nullptr, AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h, 1); + ASSERT_NE(img, nullptr); + FillImageRandom(img); + ASSERT_EQ(aom_codec_encode(&enc, img, 0, 1, 0), AOM_CODEC_OK) + << aom_codec_error_detail(&enc); + aom_img_free(img); + + cfg.g_w = 320; + cfg.g_h = 240; + cfg.kf_max_dist = 0; + ASSERT_EQ(aom_codec_enc_config_set(&enc, &cfg), AOM_CODEC_OK) + << aom_codec_error_detail(&enc); + img = aom_img_alloc(nullptr, AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h, 1); + ASSERT_NE(img, nullptr); + FillImageRandom(img); + EXPECT_EQ(aom_codec_encode(&enc, img, 1, 1, 0), AOM_CODEC_OK) + << aom_codec_error_detail(&enc); + aom_img_free(img); + EXPECT_EQ(aom_codec_encode(&enc, nullptr, 0, 0, 0), AOM_CODEC_OK) + << aom_codec_error_detail(&enc); + ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK); + } +} } // namespace