The only variable accessed from it is AVCodecContext, which we can pass directly. Also, this function currently logs into the AVCodecContext, which is wrong. Log to Encoder instead.
Unified · Split
| @@ -110,34 +110,35 @@ int enc_alloc(Encoder **penc, const AVCodec *codec, · 109 unmodified lines | ||
| 110 | 110 | return 0; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | −static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref) | |
| 113 | +static int hw_device_setup_for_encode(Encoder *e, AVCodecContext *enc_ctx, | |
| 114 | + AVBufferRef *frames_ref) | |
| 114 | 115 | { |
| 115 | 116 | const AVCodecHWConfig *config; |
| 116 | 117 | HWDevice *dev = NULL; |
| 117 | 118 | |
| 118 | 119 | if (frames_ref && |
| 119 | 120 | ((AVHWFramesContext*)frames_ref->data)->format == |
| 120 | − ost->enc_ctx->pix_fmt) { | |
| 121 | + enc_ctx->pix_fmt) { | |
| 121 | 122 | // Matching format, will try to use hw_frames_ctx. |
| 122 | 123 | } else { |
| 123 | 124 | frames_ref = NULL; |
| 124 | 125 | } |
| 125 | 126 | |
| 126 | 127 | for (int i = 0;; i++) { |
| 127 | − config = avcodec_get_hw_config(ost->enc_ctx->codec, i); | |
| 128 | + config = avcodec_get_hw_config(enc_ctx->codec, i); | |
| 128 | 129 | if (!config) |
| 129 | 130 | break; |
| 130 | 131 | |
| 131 | 132 | if (frames_ref && |
| 132 | 133 | config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX && |
| 133 | 134 | (config->pix_fmt == AV_PIX_FMT_NONE || |
| 134 | − config->pix_fmt == ost->enc_ctx->pix_fmt)) { | |
| 135 | − av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input " | |
| 135 | + config->pix_fmt == enc_ctx->pix_fmt)) { | |
| 136 | + av_log(e, AV_LOG_VERBOSE, "Using input " | |
| 136 | 137 | "frames context (format %s) with %s encoder.\n", |
| 137 | − av_get_pix_fmt_name(ost->enc_ctx->pix_fmt), | |
| 138 | − ost->enc_ctx->codec->name); | |
| 139 | − ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); | |
| 140 | − if (!ost->enc_ctx->hw_frames_ctx) | |
| 138 | + av_get_pix_fmt_name(enc_ctx->pix_fmt), | |
| 139 | + enc_ctx->codec->name); | |
| 140 | + enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); | |
| 141 | + if (!enc_ctx->hw_frames_ctx) | |
| 141 | 142 | return AVERROR(ENOMEM); |
| 142 | 143 | return 0; |
| 143 | 144 | } |
| @@ -148,11 +149,11 @@ static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref · 4 unmodified lines | ||
| 148 | 149 | } |
| 149 | 150 | |
| 150 | 151 | if (dev) { |
| 151 | − av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s " | |
| 152 | + av_log(e, AV_LOG_VERBOSE, "Using device %s " | |
| 152 | 153 | "(type %s) with %s encoder.\n", dev->name, |
| 153 | − av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name); | |
| 154 | − ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); | |
| 155 | − if (!ost->enc_ctx->hw_device_ctx) | |
| 154 | + av_hwdevice_get_type_name(dev->type), enc_ctx->codec->name); | |
| 155 | + enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); | |
| 156 | + if (!enc_ctx->hw_device_ctx) | |
| 156 | 157 | return AVERROR(ENOMEM); |
| 157 | 158 | } else { |
| 158 | 159 | // No device required, or no device available. |
| @@ -335,7 +336,7 @@ int enc_open(void *opaque, const AVFrame *frame) · 176 unmodified lines | ||
| 335 | 336 | |
| 336 | 337 | enc_ctx->flags |= AV_CODEC_FLAG_FRAME_DURATION; |
| 337 | 338 | |
| 338 | − ret = hw_device_setup_for_encode(ost, frame ? frame->hw_frames_ctx : NULL); | |
| 339 | + ret = hw_device_setup_for_encode(e, enc_ctx, frame ? frame->hw_frames_ctx : NULL); | |
| 339 | 340 | if (ret < 0) { |
| 340 | 341 | av_log(e, AV_LOG_ERROR, |
| 341 | 342 | "Encoding hardware device setup failed: %s\n", av_err2str(ret)); |