2121
2222#include " decode.h"
2323
24- char * req_hw_type = nullptr ;
25- AVPixelFormat req_hw_pix_fmt = AV_PIX_FMT_NONE;
26-
2724AVPixelFormat get_format (AVCodecContext *s, const AVPixelFormat *pix_fmts)
2825{
29- AVPixelFormat result = AV_PIX_FMT_NONE;
3026 const AVPixelFormat *p;
3127 int i, err;
3228
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 ;
37-
38- if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
39- break ;
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 ;
4032
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 ;
49- }
33+ if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
34+ break ;
5035
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- }
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)
5843 break ;
59- }
6044 }
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);
45+
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);
52+ }
53+ break ;
6854 }
69- result = req_hw_pix_fmt;
7055 }
7156
72- return result ;
57+ return *p ;
7358}
7459
7560napi_value decoder (napi_env env, napi_callback_info info) {
@@ -85,8 +70,8 @@ napi_value decoder(napi_env env, napi_callback_info info) {
8570 AVCodecParameters* params = nullptr ;
8671 char * codecName = nullptr ;
8772 size_t codecNameLen = 0 ;
88- size_t hwTypeLen = 0 ;
8973 int32_t codecID = -1 ;
74+ bool hwaccel = false ;
9075
9176 size_t argc = 1 ;
9277 napi_value args[1 ];
@@ -205,16 +190,9 @@ napi_value decoder(napi_env env, napi_callback_info info) {
205190 if (hasHWaccel) {
206191 status = napi_get_named_property (env, args[0 ], " hwaccel" , &value);
207192 CHECK_STATUS;
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);
193+ status = napi_get_value_bool (env, value, &hwaccel);
212194 CHECK_STATUS;
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
195+ if (hwaccel)
218196 decoder->get_format = get_format;
219197 }
220198
0 commit comments