Skip to content

Commit ec11659

Browse files
author
Mateusz Garbowski
committed
Revert "Simplify decoder hwaccel flag"
This reverts commit 5504d7b.
1 parent 2debf61 commit ec11659

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

scratch/decode_avci.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function run() {
2828
// let demuxer = await beamcoder.demuxer('M:/dpp/AS11.mxf');
2929
demuxer.streams.forEach(s => s.discard = (0 == s.index) ? 'default' : 'all');
3030
// let decoder = beamcoder.decoder({ name: 'h264', thread_count: 4, thread_type: { FRAME: false, SLICE: true } });
31-
let decoder = beamcoder.decoder({ name: 'h264', thread_count: 1, hwaccel: true });
31+
let decoder = beamcoder.decoder({ name: 'h264', thread_count: 1, hwaccel: 'auto' });
3232
// console.dir(decoder, { getters: true, depth: 3 });
3333
let packet = {};
3434
for ( let x = 0 ; x < 2000 && packet != null; x++ ) {

src/decode.cc

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,55 @@
2121

2222
#include "decode.h"
2323

24+
char* req_hw_type = nullptr;
25+
AVPixelFormat req_hw_pix_fmt = AV_PIX_FMT_NONE;
26+
2427
AVPixelFormat get_format(AVCodecContext *s, const AVPixelFormat *pix_fmts)
2528
{
29+
AVPixelFormat result = AV_PIX_FMT_NONE;
2630
const AVPixelFormat *p;
2731
int i, err;
2832

29-
for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
30-
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
31-
const AVCodecHWConfig *config = NULL;
32-
33-
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
34-
break;
33+
if (0 == strcmp("auto", req_hw_type)) {
34+
for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
35+
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
36+
const AVCodecHWConfig *config = NULL;
3537

36-
for (i = 0;; i++) {
37-
config = avcodec_get_hw_config(s->codec, i);
38-
if (!config)
39-
break;
40-
if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
41-
continue;
42-
if (config->pix_fmt == *p)
38+
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
4339
break;
44-
}
4540

46-
if (config) {
47-
err = av_hwdevice_ctx_create(&s->hw_device_ctx, config->device_type, NULL, NULL, 0);
48-
if (err < 0) {
49-
char errstr[128];
50-
av_make_error_string(errstr, 128, err);
51-
printf("Error in get_format av_hwdevice_ctx_create: %s\n", errstr);
41+
for (i = 0;; i++) {
42+
config = avcodec_get_hw_config(s->codec, i);
43+
if (!config)
44+
break;
45+
if (!(config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
46+
continue;
47+
if (config->pix_fmt == *p)
48+
break;
5249
}
53-
break;
50+
51+
if (config) {
52+
err = av_hwdevice_ctx_create(&s->hw_device_ctx, config->device_type, NULL, NULL, 0);
53+
if (err < 0) {
54+
char errstr[128];
55+
av_make_error_string(errstr, 128, err);
56+
printf("Error in get_format \'auto\' av_hwdevice_ctx_create: %s\n", errstr);
57+
}
58+
break;
59+
}
60+
}
61+
result = *p;
62+
} else {
63+
err = av_hwdevice_ctx_create(&s->hw_device_ctx, av_hwdevice_find_type_by_name(req_hw_type), NULL, NULL, 0);
64+
if (err < 0) {
65+
char errstr[128];
66+
av_make_error_string(errstr, 128, err);
67+
printf("Error in get_format \'%s\' av_hwdevice_ctx_create: %s\n", req_hw_type, errstr);
5468
}
69+
result = req_hw_pix_fmt;
5570
}
5671

57-
return *p;
72+
return result;
5873
}
5974

6075
napi_value decoder(napi_env env, napi_callback_info info) {
@@ -70,8 +85,8 @@ napi_value decoder(napi_env env, napi_callback_info info) {
7085
AVCodecParameters* params = nullptr;
7186
char* codecName = nullptr;
7287
size_t codecNameLen = 0;
88+
size_t hwTypeLen = 0;
7389
int32_t codecID = -1;
74-
bool hwaccel = false;
7590

7691
size_t argc = 1;
7792
napi_value args[1];
@@ -190,9 +205,16 @@ napi_value decoder(napi_env env, napi_callback_info info) {
190205
if (hasHWaccel) {
191206
status = napi_get_named_property(env, args[0], "hwaccel", &value);
192207
CHECK_STATUS;
193-
status = napi_get_value_bool(env, value, &hwaccel);
208+
hwTypeLen = 64;
209+
req_hw_type = (char*) malloc(sizeof(char) * (hwTypeLen + 1));
210+
status = napi_get_value_string_utf8(env, value, req_hw_type,
211+
64, &hwTypeLen);
194212
CHECK_STATUS;
195-
if (hwaccel)
213+
req_hw_pix_fmt = av_get_pix_fmt(req_hw_type);
214+
215+
if (0 != strcmp("auto", req_hw_type) && req_hw_pix_fmt == AV_PIX_FMT_NONE)
216+
printf("Decoder hwaccel name \'%s\' not recognised\n", req_hw_type);
217+
else
196218
decoder->get_format = get_format;
197219
}
198220

0 commit comments

Comments
 (0)