ret = avcodec_open2(avctx, codec, options);
- ff_lock_avcodec(avctx);
+ ff_lock_avcodec(avctx, codec);
return ret;
}
if (options)
av_dict_copy(&tmp, *options, 0);
- ret = ff_lock_avcodec(avctx);
+ ret = ff_lock_avcodec(avctx, codec);
if (ret < 0)
return ret;
if (CONFIG_FRAME_THREAD_ENCODER) {
ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
- ff_lock_avcodec(avctx);
+ ff_lock_avcodec(avctx, codec);
if (ret < 0)
goto free_and_end;
}
return ret;
free_and_end:
+ if (avctx->codec &&
+ (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
+ avctx->codec->close(avctx);
+
av_dict_free(&tmp);
if (codec->priv_class && codec->priv_data_size)
av_opt_free(avctx->priv_data);
return 0;
}
-int ff_lock_avcodec(AVCodecContext *log_ctx)
+int ff_lock_avcodec(AVCodecContext *log_ctx, const AVCodec *codec)
{
if (lockmgr_cb) {
if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
return -1;
}
- entangled_thread_counter++;
- if (entangled_thread_counter != 1) {
+
+ if (avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, 1) != 1 &&
+ !(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE)) {
av_log(log_ctx, AV_LOG_ERROR,
"Insufficient thread locking. At least %d threads are "
"calling avcodec_open2() at the same time right now.\n",
{
av_assert0(ff_avcodec_locked);
ff_avcodec_locked = 0;
- entangled_thread_counter--;
+ avpriv_atomic_int_add_and_fetch(&entangled_thread_counter, -1);
if (lockmgr_cb) {
if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
return -1;