Skip to content

Commit

Permalink
automatically call aom_codec_destroy() at end of scope
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Feb 19, 2025
1 parent e190cd9 commit e172d6b
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions libheif/plugins/encoder_aom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,6 @@ static bool check_aom_error(aom_codec_err_t aom_error, const aom_codec_ctx_t* co

#define CHECK_ERROR \
if (check_aom_error(aom_error, &codec, encoder, &err)) { \
aom_codec_destroy(&codec); \
return err; \
}

Expand Down Expand Up @@ -1024,15 +1023,19 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
encoder_flags = (aom_codec_flags_t) (encoder_flags | AOM_CODEC_USE_HIGHBITDEPTH);
}

// allocate aom_codec_ctx_t
if (aom_codec_enc_init(&codec, iface, &cfg, encoder_flags)) {
// AOM makes sure that the error text returned by aom_codec_error_detail() is always a static
// text that is valid even through the codec allocation failed (#788).
// text that is valid even though the codec allocation failed (#788).
err = {heif_error_Encoder_plugin_error,
heif_suberror_Encoder_initialization,
encoder->set_aom_error(aom_codec_error_detail(&codec))};
return err;
}

// automatically destroy aom_codec_ctx_t when we leave the function
auto codec_ctx_deleter = std::unique_ptr<aom_codec_ctx_t, aom_codec_err_t (*)(aom_codec_ctx_t*)>(&codec, aom_codec_destroy);

aom_codec_err_t aom_error;

aom_error = aom_codec_control(&codec, AOME_SET_CPUUSED, encoder->cpu_used); CHECK_ERROR;
Expand Down Expand Up @@ -1105,7 +1108,6 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
heif_suberror_Unsupported_parameter,
encoder->set_aom_error(sstr.str().c_str())
};
aom_codec_destroy(&codec);
return err;
}
}
Expand All @@ -1124,7 +1126,6 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
heif_suberror_Encoder_encoding,
encoder->set_aom_error(aom_codec_error_detail(&codec))
};
aom_codec_destroy(&codec);
return err;
}

Expand Down Expand Up @@ -1161,7 +1162,6 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
err = {heif_error_Encoder_plugin_error,
heif_suberror_Encoder_encoding,
encoder->set_aom_error(aom_codec_error_detail(&codec))};
aom_codec_destroy(&codec);
return err;
}

Expand Down Expand Up @@ -1190,17 +1190,6 @@ struct heif_error aom_encode_image(void* encoder_raw, const struct heif_image* i
}
}


// --- clean up

if (aom_codec_destroy(&codec)) {
// Note: do not call aom_codec_error_detail(), because it is not set in aom_codec_destroy(). (see #788)
err = {heif_error_Encoder_plugin_error,
heif_suberror_Encoder_cleanup,
kError_undefined_error};
return err;
}

return heif_error_ok;
}

Expand Down

0 comments on commit e172d6b

Please sign in to comment.