avcodec/videotoolbox: extract videotoolbox_{start,stop} helpers
authorAman Gupta <aman@tmm1.net>
Mon, 25 Sep 2017 23:32:11 +0000 (16:32 -0700)
committerAman Gupta <aman@tmm1.net>
Mon, 13 Nov 2017 22:31:44 +0000 (14:31 -0800)
These helpers will be used in later commits to automatically restart
the decoder session when SPS changes are encountered.

Signed-off-by: Aman Gupta <aman@tmm1.net>
libavcodec/videotoolbox.c

index 5011f52..f3d5f0a 100644 (file)
@@ -756,7 +756,7 @@ static CMVideoFormatDescriptionRef videotoolbox_format_desc_create(CMVideoCodecT
     return cm_fmt_desc;
 }
 
-static int videotoolbox_default_init(AVCodecContext *avctx)
+static int videotoolbox_start(AVCodecContext *avctx)
 {
     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
     OSStatus status;
@@ -794,6 +794,11 @@ static int videotoolbox_default_init(AVCodecContext *avctx)
 
     decoder_spec = videotoolbox_decoder_config_create(videotoolbox->cm_codec_type, avctx);
 
+    if (!decoder_spec) {
+        av_log(avctx, AV_LOG_ERROR, "decoder specification creation failed\n");
+        return -1;
+    }
+
     videotoolbox->cm_fmt_desc = videotoolbox_format_desc_create(videotoolbox->cm_codec_type,
                                                                 decoder_spec,
                                                                 avctx->width,
@@ -846,18 +851,21 @@ static int videotoolbox_default_init(AVCodecContext *avctx)
     }
 }
 
-static void videotoolbox_default_free(AVCodecContext *avctx)
+static void videotoolbox_stop(AVCodecContext *avctx)
 {
     AVVideotoolboxContext *videotoolbox = videotoolbox_get_context(avctx);
     if (!videotoolbox)
         return;
 
-    if (videotoolbox->cm_fmt_desc)
+    if (videotoolbox->cm_fmt_desc) {
         CFRelease(videotoolbox->cm_fmt_desc);
+        videotoolbox->cm_fmt_desc = NULL;
+    }
 
     if (videotoolbox->session) {
         VTDecompressionSessionInvalidate(videotoolbox->session);
         CFRelease(videotoolbox->session);
+        videotoolbox->session = NULL;
     }
 }
 
@@ -870,7 +878,7 @@ static int videotoolbox_uninit(AVCodecContext *avctx)
     ff_videotoolbox_uninit(avctx);
 
     if (vtctx->vt_ctx)
-        videotoolbox_default_free(avctx);
+        videotoolbox_stop(avctx);
 
     av_buffer_unref(&vtctx->cached_hw_frames_ctx);
     av_freep(&vtctx->vt_ctx);
@@ -936,7 +944,7 @@ static int videotoolbox_common_init(AVCodecContext *avctx)
         goto fail;
     }
 
-    err = videotoolbox_default_init(avctx);
+    err = videotoolbox_start(avctx);
     if (err < 0)
         goto fail;
 
@@ -1072,13 +1080,13 @@ int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *
     avctx->hwaccel_context = vtctx ?: av_videotoolbox_alloc_context();
     if (!avctx->hwaccel_context)
         return AVERROR(ENOMEM);
-    return videotoolbox_default_init(avctx);
+    return videotoolbox_start(avctx);
 }
 
 void av_videotoolbox_default_free(AVCodecContext *avctx)
 {
 
-    videotoolbox_default_free(avctx);
+    videotoolbox_stop(avctx);
     av_freep(&avctx->hwaccel_context);
 }
 #endif /* CONFIG_VIDEOTOOLBOX */