Skip to content

Conversation

@josuah
Copy link
Contributor

@josuah josuah commented Feb 24, 2025

This adds a macro to generate a C99 compound literal string, which allow to use it in debug log messages, such as:
LOG_DBG("The pixel format is '%s'", VIDEO_FOURCC_STR(fmt->pixelformat));

The previous proposal was more awkward: printf("video format: " PRIvfmt, PRIvfmt_arg(&fmt));

@josuah josuah added area: API Changes to public APIs area: Video Video subsystem labels Feb 24, 2025
@josuah josuah force-pushed the pr-video-string-format branch from 25ff916 to 4f51883 Compare February 28, 2025 15:16
@josuah
Copy link
Contributor Author

josuah commented Feb 28, 2025

Wrapping it in parenthesis seemed necessary in some cases.

@josuah josuah added the Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc. label Feb 28, 2025
@kartben
Copy link
Contributor

kartben commented Feb 28, 2025

* @param fcc 32-bit integer to be converted
* @return Four-character-string built out of this fourcc.
*/
#define VIDEO_FOURCC_TO_STR(u) ((char []){(u) >> 0, (u) >> 8, (u) >> 16, (u) >> 24, '\0'})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't each byte need to explicitly be cast to char? probably warning-prone otherwise?
also not sure about the naming u, and I think you might face endianness issues?

#define VIDEO_FOURCC_TO_STR(fourcc) ((char []){ \
    (char)((fourcc) & 0xFF), \
    (char)(((fourcc) >> 8) & 0xFF), \
    (char)(((fourcc) >> 16) & 0xFF), \
    (char)(((fourcc) >> 24) & 0xFF), \
    '\0' \
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might face endianness issues

The pixelformat used everywhere in video.h is what FOURCC are used for, and is always in the native endianess of the CPU. That can lead to surprises if logging something directly from the network, I added a comment.

Thank you for the improvement and reminding that clarity and correctness are better than compactness.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • little/big-endian (LSB/MSB first)
  • native-endian (what the CPU has)
  • confused-endian (the opposite) like FOURCCs

@kartben kartben removed the Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc. label Feb 28, 2025
@josuah josuah force-pushed the pr-video-string-format branch from 4f51883 to 2d55aa1 Compare February 28, 2025 16:48
This adds a macro to generate a C99 compound literal string, which allow
to use it in debug log messages, such as:
LOG_DBG("The pixel format is '%s'", VIDEO_FOURCC_STR(fmt->pixelformat));

Signed-off-by: Josuah Demangeon <[email protected]>
@josuah josuah force-pushed the pr-video-string-format branch from 2d55aa1 to 24b7230 Compare February 28, 2025 16:54
kartben
kartben previously approved these changes Feb 28, 2025
Copy link
Contributor

@ngphibang ngphibang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the PR should also include changes that uses this macro, at least in the video samples. This avoids adding additional PRs later for it (and avoid adding things that has no usage). Otherwise, LGTM

A macro introduced allows to log four-character-codes (FOURCC) as one
string instead of enumerating each individual character. This saves a bit
of room in the code and is less error-prone.

Signed-off-by: Josuah Demangeon <[email protected]>
@kartben kartben merged commit 364322e into zephyrproject-rtos:main Mar 8, 2025
23 checks passed
@josuah josuah deleted the pr-video-string-format branch March 13, 2025 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: API Changes to public APIs area: Samples Samples area: Video Video subsystem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants