From 5504d7bb578363c34f436426fcef5f4e96f409a0 Mon Sep 17 00:00:00 2001 From: Simon Rogers Date: Thu, 4 Nov 2021 18:18:38 +0000 Subject: [PATCH] Simplify decoder hwaccel flag --- scratch/decode_avci.js | 2 +- src/decode.cc | 72 +++++++++++++++--------------------------- 2 files changed, 26 insertions(+), 48 deletions(-) diff --git a/scratch/decode_avci.js b/scratch/decode_avci.js index 3c341e5..1a6365b 100644 --- a/scratch/decode_avci.js +++ b/scratch/decode_avci.js @@ -28,7 +28,7 @@ async function run() { // let demuxer = await beamcoder.demuxer('M:/dpp/AS11.mxf'); demuxer.streams.forEach(s => s.discard = (0 == s.index) ? 'default' : 'all'); // let decoder = beamcoder.decoder({ name: 'h264', thread_count: 4, thread_type: { FRAME: false, SLICE: true } }); - let decoder = beamcoder.decoder({ name: 'h264', thread_count: 1, hwaccel: 'auto' }); + let decoder = beamcoder.decoder({ name: 'h264', thread_count: 1, hwaccel: true }); // console.dir(decoder, { getters: true, depth: 3 }); let packet = {}; for ( let x = 0 ; x < 2000 && packet != null; x++ ) { diff --git a/src/decode.cc b/src/decode.cc index 466659f..b196ddb 100644 --- a/src/decode.cc +++ b/src/decode.cc @@ -21,55 +21,40 @@ #include "decode.h" -char* req_hw_type = nullptr; -AVPixelFormat req_hw_pix_fmt = AV_PIX_FMT_NONE; - AVPixelFormat get_format(AVCodecContext *s, const AVPixelFormat *pix_fmts) { - AVPixelFormat result = AV_PIX_FMT_NONE; const AVPixelFormat *p; int i, err; - if (0 == strcmp("auto", req_hw_type)) { - for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); - const AVCodecHWConfig *config = NULL; - - if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) - break; + for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) { + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); + const AVCodecHWConfig *config = NULL; - for (i = 0;; i++) { - config = avcodec_get_hw_config(s->codec, i); - if (!config) - break; - if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) - continue; - if (config->pix_fmt == *p) - break; - } + if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) + break; - if (config) { - err = av_hwdevice_ctx_create(&s->hw_device_ctx, config->device_type, NULL, NULL, 0); - if (err < 0) { - char errstr[128]; - av_make_error_string(errstr, 128, err); - printf("Error in get_format \'auto\' av_hwdevice_ctx_create: %s\n", errstr); - } + for (i = 0;; i++) { + config = avcodec_get_hw_config(s->codec, i); + if (!config) + break; + if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) + continue; + if (config->pix_fmt == *p) break; - } } - result = *p; - } else { - err = av_hwdevice_ctx_create(&s->hw_device_ctx, av_hwdevice_find_type_by_name(req_hw_type), NULL, NULL, 0); - if (err < 0) { - char errstr[128]; - av_make_error_string(errstr, 128, err); - printf("Error in get_format \'%s\' av_hwdevice_ctx_create: %s\n", req_hw_type, errstr); + + if (config) { + err = av_hwdevice_ctx_create(&s->hw_device_ctx, config->device_type, NULL, NULL, 0); + if (err < 0) { + char errstr[128]; + av_make_error_string(errstr, 128, err); + printf("Error in get_format av_hwdevice_ctx_create: %s\n", errstr); + } + break; } - result = req_hw_pix_fmt; } - return result; + return *p; } napi_value decoder(napi_env env, napi_callback_info info) { @@ -85,8 +70,8 @@ napi_value decoder(napi_env env, napi_callback_info info) { AVCodecParameters* params = nullptr; char* codecName = nullptr; size_t codecNameLen = 0; - size_t hwTypeLen = 0; int32_t codecID = -1; + bool hwaccel = false; size_t argc = 1; napi_value args[1]; @@ -205,16 +190,9 @@ napi_value decoder(napi_env env, napi_callback_info info) { if (hasHWaccel) { status = napi_get_named_property(env, args[0], "hwaccel", &value); CHECK_STATUS; - hwTypeLen = 64; - req_hw_type = (char*) malloc(sizeof(char) * (hwTypeLen + 1)); - status = napi_get_value_string_utf8(env, value, req_hw_type, - 64, &hwTypeLen); + status = napi_get_value_bool(env, value, &hwaccel); CHECK_STATUS; - req_hw_pix_fmt = av_get_pix_fmt(req_hw_type); - - if (0 != strcmp("auto", req_hw_type) && req_hw_pix_fmt == AV_PIX_FMT_NONE) - printf("Decoder hwaccel name \'%s\' not recognised\n", req_hw_type); - else + if (hwaccel) decoder->get_format = get_format; }