Skip to content

Commit

Permalink
Simplify decoder hwaccel flag
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptorian committed Nov 4, 2021
1 parent 11b7c06 commit 5504d7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 48 deletions.
2 changes: 1 addition & 1 deletion scratch/decode_avci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++ ) {
Expand Down
72 changes: 25 additions & 47 deletions src/decode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 5504d7b

Please sign in to comment.