From fe6a2c0efb866b758f81801fc2347e9e7f35130a Mon Sep 17 00:00:00 2001
From: "zorro.ma" <12685137@qq.com>
Date: Sun, 7 Jul 2024 17:19:31 +0800
Subject: [PATCH] Fix HLG 10bit color transfer

---
 libavcodec/videotoolboxenc.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index b0e827d14a..899fa3ae12 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -323,11 +323,14 @@ static void clear_frame_queue(VTEncContext *vtctx)
     set_async_error(vtctx, 0);
 }
 
-static void vtenc_reset(VTEncContext *vtctx)
+static void vtenc_reset(VTEncContext *vtctx, bool resetSessionOnly)
 {
-    if (vtctx->session) {
-        CFRelease(vtctx->session);
-        vtctx->session = NULL;
+    if (resetSessionOnly) {
+        if (vtctx->session) {
+            CFRelease(vtctx->session);
+            vtctx->session = NULL;
+        }
+        return;
     }
 
     if (vtctx->supported_props) {
@@ -2494,7 +2497,7 @@ static int create_cv_pixel_buffer(AVCodecContext   *avctx,
 
         vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
         if (vtstatus == kVTInvalidSessionErr) {
-            vtenc_reset(vtctx);
+            vtenc_reset(vtctx, true);
 
             status = vtenc_configure_encoder(avctx);
             if (status == 0)
@@ -2751,7 +2754,7 @@ static int vtenc_populate_extradata(AVCodecContext   *avctx,
 
 pe_cleanup:
     CVPixelBufferRelease(pix_buf);
-    vtenc_reset(vtctx);
+    vtenc_reset(vtctx, true);
     vtctx->frame_ct_out = 0;
 
     av_assert0(status != 0 || (avctx->extradata && avctx->extradata_size > 0));
@@ -2775,7 +2778,7 @@ static av_cold int vtenc_close(AVCodecContext *avctx)
     pthread_cond_destroy(&vtctx->cv_sample_sent);
     pthread_mutex_destroy(&vtctx->lock);
 
-    vtenc_reset(vtctx);
+    vtenc_reset(vtctx, false);
 
     return 0;
 }
-- 
2.40.1.windows.1

