+// Return the AVVideotoolboxContext that matters currently. Where it comes from
+// depends on the API used.
+static AVVideotoolboxContext *videotoolbox_get_context(AVCodecContext *avctx)
+{
+ // Somewhat tricky because the user can call av_videotoolbox_default_free()
+ // at any time, even when the codec is closed.
+ if (avctx->internal && avctx->internal->hwaccel_priv_data) {
+ VTContext *vtctx = avctx->internal->hwaccel_priv_data;
+ if (vtctx->vt_ctx)
+ return vtctx->vt_ctx;
+ }
+ return avctx->hwaccel_context;
+}
+
+static int videotoolbox_buffer_create(AVCodecContext *avctx, AVFrame *frame)
+{
+ VTContext *vtctx = avctx->internal->hwaccel_priv_data;
+ CVPixelBufferRef pixbuf = (CVPixelBufferRef)vtctx->frame;
+ OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
+ enum AVPixelFormat sw_format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
+ int width = CVPixelBufferGetWidth(pixbuf);
+ int height = CVPixelBufferGetHeight(pixbuf);
+ AVHWFramesContext *cached_frames;
+ int ret;
+
+ ret = ff_videotoolbox_buffer_create(vtctx, frame);
+ if (ret < 0)
+ return ret;
+
+ // Old API code path.
+ if (!vtctx->cached_hw_frames_ctx)
+ return 0;
+
+ cached_frames = (AVHWFramesContext*)vtctx->cached_hw_frames_ctx->data;
+
+ if (cached_frames->sw_format != sw_format ||
+ cached_frames->width != width ||
+ cached_frames->height != height) {
+ AVBufferRef *hw_frames_ctx = av_hwframe_ctx_alloc(cached_frames->device_ref);
+ AVHWFramesContext *hw_frames;
+ if (!hw_frames_ctx)
+ return AVERROR(ENOMEM);
+
+ hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
+ hw_frames->format = cached_frames->format;
+ hw_frames->sw_format = sw_format;
+ hw_frames->width = width;
+ hw_frames->height = height;
+
+ ret = av_hwframe_ctx_init(hw_frames_ctx);
+ if (ret < 0) {
+ av_buffer_unref(&hw_frames_ctx);
+ return ret;
+ }
+
+ av_buffer_unref(&vtctx->cached_hw_frames_ctx);
+ vtctx->cached_hw_frames_ctx = hw_frames_ctx;
+ }
+
+ av_assert0(!frame->hw_frames_ctx);
+ frame->hw_frames_ctx = av_buffer_ref(vtctx->cached_hw_frames_ctx);
+ if (!frame->hw_frames_ctx)
+ return AVERROR(ENOMEM);
+
+ return 0;
+}
+