21
21
22
22
#include " decode.h"
23
23
24
+ char * req_hw_type = nullptr ;
25
+ AVPixelFormat req_hw_pix_fmt = AV_PIX_FMT_NONE;
26
+
24
27
AVPixelFormat get_format (AVCodecContext *s, const AVPixelFormat *pix_fmts)
25
28
{
29
+ AVPixelFormat result = AV_PIX_FMT_NONE;
26
30
const AVPixelFormat *p;
27
31
int i, err;
28
32
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 ;
35
37
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))
43
39
break ;
44
- }
45
40
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 ;
52
49
}
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);
54
68
}
69
+ result = req_hw_pix_fmt;
55
70
}
56
71
57
- return *p ;
72
+ return result ;
58
73
}
59
74
60
75
napi_value decoder (napi_env env, napi_callback_info info) {
@@ -70,8 +85,8 @@ napi_value decoder(napi_env env, napi_callback_info info) {
70
85
AVCodecParameters* params = nullptr ;
71
86
char * codecName = nullptr ;
72
87
size_t codecNameLen = 0 ;
88
+ size_t hwTypeLen = 0 ;
73
89
int32_t codecID = -1 ;
74
- bool hwaccel = false ;
75
90
76
91
size_t argc = 1 ;
77
92
napi_value args[1 ];
@@ -190,9 +205,16 @@ napi_value decoder(napi_env env, napi_callback_info info) {
190
205
if (hasHWaccel) {
191
206
status = napi_get_named_property (env, args[0 ], " hwaccel" , &value);
192
207
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);
194
212
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
196
218
decoder->get_format = get_format;
197
219
}
198
220
0 commit comments