From c074bdfd418b7f8e42f194d514d5730d0edd52c7 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Wed, 24 Jan 2024 14:13:47 +0100 Subject: [PATCH] vcap/dshow: GetSubtypeName - handle non-printable characters added resolve_fourcc to utils/string - fixed wrong signal.h include guard (it should be definitely included in mac, it was perhaps meant to be MSW, but it is needed there too) - removed config*h --- src/utils/string.c | 52 ++++++++++++++++++++----- src/utils/string.h | 3 +- src/video_capture/DirectShowGrabber.cpp | 5 +-- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/utils/string.c b/src/utils/string.c index dfb89eb72..b63b76928 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2014-2023 CESNET, z. s. p. o. + * Copyright (c) 2014-2024 CESNET, z. s. p. o. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,17 +35,18 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#include "config_unix.h" -#include "config_win32.h" -#endif - -#ifndef __APPLE__ +#include +#include +#include #include -#endif #include +#ifdef _WIN32 +#include +#else +#include +#endif + #include "debug.h" #include "utils/string.h" @@ -185,3 +186,36 @@ append_sig_desc(char **ptr, const char *ptr_end, int signum) #endif strappend(ptr, ptr_end, ")"); } + +/** + * Returns string representation of input FourCC in a fashion FFmpeg does - + * non-printable character value is printed as a number in '[]'. + * + * @returns NUL-terminated FourCC, which will be longer than 4B + '\0' if + * non-printable characters are present + */ + +const char * +pretty_print_fourcc(const void *fcc) +{ + enum { + CHAR_LEN = 5, // at worst [XXX] + CHARS = sizeof(uint32_t), + MAX_LEN = CHARS * CHAR_LEN + 1 /* '\0' */, + }; + _Thread_local static char out[MAX_LEN]; + char *out_ptr = out; + const unsigned char *fourcc = fcc; + + for (int i = 0; i < CHARS; ++i) { + if (isprint(fourcc[i])) { + *out_ptr++ = (char) fourcc[i]; + } else { + const int written = + snprintf(out_ptr, CHAR_LEN + 1, "[%hhu]", fourcc[i]); + out_ptr+= written; + } + } + *out_ptr = '\0'; + return out; +} diff --git a/src/utils/string.h b/src/utils/string.h index 7eccd180c..140380ab1 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -3,7 +3,7 @@ * @author Martin Pulec */ /* - * Copyright (c) 2014-2023 CESNET z.s.p.o. + * Copyright (c) 2014-2024 CESNET z.s.p.o. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -57,6 +57,7 @@ char *strrpbrk(char *s, const char *accept); void strappend(char **ptr, const char *ptr_end, const char *src); void append_sig_desc(char **ptr, const char *ptr_end, int signum); void write_all(int fd, size_t len, const char *msg); +const char *pretty_print_fourcc(const void *fcc); #ifdef __cplusplus } diff --git a/src/video_capture/DirectShowGrabber.cpp b/src/video_capture/DirectShowGrabber.cpp index 06adab511..29f7bcd36 100644 --- a/src/video_capture/DirectShowGrabber.cpp +++ b/src/video_capture/DirectShowGrabber.cpp @@ -24,6 +24,7 @@ #include "tv.h" #include "utils/color_out.h" #include "utils/macros.h" +#include "utils/string.h" #include "utils/windows.h" #include "video.h" #include "video_capture.h" @@ -1363,11 +1364,9 @@ static const CHAR * GetSubtypeNameA(const GUID *pSubtype) // would use the header file that picks the A or W version. static const CHAR * GetSubtypeName(const GUID *pSubtype) { - thread_local char fourcc[5] = ""; // the type is unknown to us, so print FourCC if (LocateSubtype(pSubtype) == sizeof BitCountMap / sizeof BitCountMap[0] - 1) { - memcpy(fourcc, &pSubtype->Data1, 4); - return fourcc; + return pretty_print_fourcc(&pSubtype->Data1); } return GetSubtypeNameA(pSubtype); }