[v3,1/4,media] v4l2-common: Add helper function for fourcc to string

Message ID e6dfbe4afd3f1db4c3f8a81c0813dc418896f5e1.1505916622.git.dave.stevenson@raspberrypi.org (mailing list archive)
State Obsoleted, archived
Delegated to: Hans Verkuil
Headers

Commit Message

Dave Stevenson Sept. 20, 2017, 4:07 p.m. UTC
  New helper function char *v4l2_fourcc2s(u32 fourcc, char *buf)
that converts a fourcc into a nice printable version.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
---

No changes from v2 to v3

 drivers/media/v4l2-core/v4l2-common.c | 18 ++++++++++++++++++
 include/media/v4l2-common.h           |  3 +++
 2 files changed, 21 insertions(+)
  

Comments

Sakari Ailus Oct. 13, 2017, 9:09 p.m. UTC | #1
Hi Dave,

On Wed, Sep 20, 2017 at 05:07:54PM +0100, Dave Stevenson wrote:
> New helper function char *v4l2_fourcc2s(u32 fourcc, char *buf)
> that converts a fourcc into a nice printable version.
> 
> Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
> ---
> 
> No changes from v2 to v3
> 
>  drivers/media/v4l2-core/v4l2-common.c | 18 ++++++++++++++++++
>  include/media/v4l2-common.h           |  3 +++
>  2 files changed, 21 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
> index a5ea1f5..0219895 100644
> --- a/drivers/media/v4l2-core/v4l2-common.c
> +++ b/drivers/media/v4l2-core/v4l2-common.c
> @@ -405,3 +405,21 @@ void v4l2_get_timestamp(struct timeval *tv)
>  	tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
>  }
>  EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
> +
> +char *v4l2_fourcc2s(u32 fourcc, char *buf)
> +{
> +	buf[0] = fourcc & 0x7f;
> +	buf[1] = (fourcc >> 8) & 0x7f;
> +	buf[2] = (fourcc >> 16) & 0x7f;
> +	buf[3] = (fourcc >> 24) & 0x7f;
> +	if (fourcc & (1 << 31)) {
> +		buf[4] = '-';
> +		buf[5] = 'B';
> +		buf[6] = 'E';
> +		buf[7] = '\0';
> +	} else {
> +		buf[4] = '\0';
> +	}
> +	return buf;
> +}
> +EXPORT_SYMBOL_GPL(v4l2_fourcc2s);
> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> index aac8b7b..5b0fff9 100644
> --- a/include/media/v4l2-common.h
> +++ b/include/media/v4l2-common.h
> @@ -264,4 +264,7 @@ const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
>  
>  void v4l2_get_timestamp(struct timeval *tv);
>  
> +#define V4L2_FOURCC_MAX_SIZE 8
> +char *v4l2_fourcc2s(u32 fourcc, char *buf);
> +
>  #endif /* V4L2_COMMON_H_ */

I like the idea but the use of a character pointer and assuming a length
looks a bit scary.

As this seems to be used uniquely for printing stuff, a couple of macros
could be used instead. Something like

#define V4L2_FOURCC_CONV "%c%c%c%c%s"
#define V4L2_FOURCC_TO_CONV(fourcc) \
	fourcc & 0x7f, (fourcc >> 8) & 0x7f, (fourcc >> 16) & 0x7f, \
	(fourcc >> 24) & 0x7f, fourcc & BIT(31) ? "-BE" : ""

You could use it with printk-style functions, e.g.

	printk("blah fourcc " V4L2_FOURCC_CONV " is a nice format",
	       V4L2_FOURCC_TO_CONV(fourcc));

I guess it'd be better to add more parentheses around "fourcc" but it'd be
less clear here that way.
  
Dave Stevenson Oct. 17, 2017, 9:17 a.m. UTC | #2
Hi Sakari.

On 13 October 2017 at 22:09, Sakari Ailus <sakari.ailus@iki.fi> wrote:
> Hi Dave,
>
> On Wed, Sep 20, 2017 at 05:07:54PM +0100, Dave Stevenson wrote:
>> New helper function char *v4l2_fourcc2s(u32 fourcc, char *buf)
>> that converts a fourcc into a nice printable version.
>>
>> Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
>> ---
>>
>> No changes from v2 to v3
>>
>>  drivers/media/v4l2-core/v4l2-common.c | 18 ++++++++++++++++++
>>  include/media/v4l2-common.h           |  3 +++
>>  2 files changed, 21 insertions(+)
>>
>> diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
>> index a5ea1f5..0219895 100644
>> --- a/drivers/media/v4l2-core/v4l2-common.c
>> +++ b/drivers/media/v4l2-core/v4l2-common.c
>> @@ -405,3 +405,21 @@ void v4l2_get_timestamp(struct timeval *tv)
>>       tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
>>  }
>>  EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
>> +
>> +char *v4l2_fourcc2s(u32 fourcc, char *buf)
>> +{
>> +     buf[0] = fourcc & 0x7f;
>> +     buf[1] = (fourcc >> 8) & 0x7f;
>> +     buf[2] = (fourcc >> 16) & 0x7f;
>> +     buf[3] = (fourcc >> 24) & 0x7f;
>> +     if (fourcc & (1 << 31)) {
>> +             buf[4] = '-';
>> +             buf[5] = 'B';
>> +             buf[6] = 'E';
>> +             buf[7] = '\0';
>> +     } else {
>> +             buf[4] = '\0';
>> +     }
>> +     return buf;
>> +}
>> +EXPORT_SYMBOL_GPL(v4l2_fourcc2s);
>> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
>> index aac8b7b..5b0fff9 100644
>> --- a/include/media/v4l2-common.h
>> +++ b/include/media/v4l2-common.h
>> @@ -264,4 +264,7 @@ const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
>>
>>  void v4l2_get_timestamp(struct timeval *tv);
>>
>> +#define V4L2_FOURCC_MAX_SIZE 8
>> +char *v4l2_fourcc2s(u32 fourcc, char *buf);
>> +
>>  #endif /* V4L2_COMMON_H_ */
>
> I like the idea but the use of a character pointer and assuming a length
> looks a bit scary.
>
> As this seems to be used uniquely for printing stuff, a couple of macros
> could be used instead. Something like
>
> #define V4L2_FOURCC_CONV "%c%c%c%c%s"
> #define V4L2_FOURCC_TO_CONV(fourcc) \
>         fourcc & 0x7f, (fourcc >> 8) & 0x7f, (fourcc >> 16) & 0x7f, \
>         (fourcc >> 24) & 0x7f, fourcc & BIT(31) ? "-BE" : ""
>
> You could use it with printk-style functions, e.g.
>
>         printk("blah fourcc " V4L2_FOURCC_CONV " is a nice format",
>                V4L2_FOURCC_TO_CONV(fourcc));
>
> I guess it'd be better to add more parentheses around "fourcc" but it'd be
> less clear here that way.

I was following Hans' suggestion made in
https://www.spinics.net/lists/linux-media/msg117046.html

Hans: Do you agree with Sakari here to make it to a macro?

  Dave
  
Hans Verkuil (hansverk) Oct. 17, 2017, 9:28 a.m. UTC | #3
On 10/17/17 11:17, Dave Stevenson wrote:
> Hi Sakari.
> 
> On 13 October 2017 at 22:09, Sakari Ailus <sakari.ailus@iki.fi> wrote:
>> Hi Dave,
>>
>> On Wed, Sep 20, 2017 at 05:07:54PM +0100, Dave Stevenson wrote:
>>> New helper function char *v4l2_fourcc2s(u32 fourcc, char *buf)
>>> that converts a fourcc into a nice printable version.
>>>
>>> Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
>>> ---
>>>
>>> No changes from v2 to v3
>>>
>>>  drivers/media/v4l2-core/v4l2-common.c | 18 ++++++++++++++++++
>>>  include/media/v4l2-common.h           |  3 +++
>>>  2 files changed, 21 insertions(+)
>>>
>>> diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
>>> index a5ea1f5..0219895 100644
>>> --- a/drivers/media/v4l2-core/v4l2-common.c
>>> +++ b/drivers/media/v4l2-core/v4l2-common.c
>>> @@ -405,3 +405,21 @@ void v4l2_get_timestamp(struct timeval *tv)
>>>       tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
>>>  }
>>>  EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
>>> +
>>> +char *v4l2_fourcc2s(u32 fourcc, char *buf)
>>> +{
>>> +     buf[0] = fourcc & 0x7f;
>>> +     buf[1] = (fourcc >> 8) & 0x7f;
>>> +     buf[2] = (fourcc >> 16) & 0x7f;
>>> +     buf[3] = (fourcc >> 24) & 0x7f;
>>> +     if (fourcc & (1 << 31)) {
>>> +             buf[4] = '-';
>>> +             buf[5] = 'B';
>>> +             buf[6] = 'E';
>>> +             buf[7] = '\0';
>>> +     } else {
>>> +             buf[4] = '\0';
>>> +     }
>>> +     return buf;
>>> +}
>>> +EXPORT_SYMBOL_GPL(v4l2_fourcc2s);
>>> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
>>> index aac8b7b..5b0fff9 100644
>>> --- a/include/media/v4l2-common.h
>>> +++ b/include/media/v4l2-common.h
>>> @@ -264,4 +264,7 @@ const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
>>>
>>>  void v4l2_get_timestamp(struct timeval *tv);
>>>
>>> +#define V4L2_FOURCC_MAX_SIZE 8
>>> +char *v4l2_fourcc2s(u32 fourcc, char *buf);
>>> +
>>>  #endif /* V4L2_COMMON_H_ */
>>
>> I like the idea but the use of a character pointer and assuming a length
>> looks a bit scary.
>>
>> As this seems to be used uniquely for printing stuff, a couple of macros
>> could be used instead. Something like
>>
>> #define V4L2_FOURCC_CONV "%c%c%c%c%s"
>> #define V4L2_FOURCC_TO_CONV(fourcc) \
>>         fourcc & 0x7f, (fourcc >> 8) & 0x7f, (fourcc >> 16) & 0x7f, \
>>         (fourcc >> 24) & 0x7f, fourcc & BIT(31) ? "-BE" : ""

'fourcc' should be in parenthesis '(fourcc)'.

>>
>> You could use it with printk-style functions, e.g.
>>
>>         printk("blah fourcc " V4L2_FOURCC_CONV " is a nice format",
>>                V4L2_FOURCC_TO_CONV(fourcc));
>>
>> I guess it'd be better to add more parentheses around "fourcc" but it'd be
>> less clear here that way.
> 
> I was following Hans' suggestion made in
> https://www.spinics.net/lists/linux-media/msg117046.html
> 
> Hans: Do you agree with Sakari here to make it to a macro?

Not a bad idea. But I think we should add it to the public videodev2.h
header in that case, allowing it to be used by applications as well.

How about calling the defines V4L2_FOURCC_FMT and V4L2_FOURCC_FMT_ARGS?

Regards,

	Hans
  
Sakari Ailus Oct. 17, 2017, 10:08 a.m. UTC | #4
Hi Hans,

On Tue, Oct 17, 2017 at 11:28:46AM +0200, Hans Verkuil wrote:
> On 10/17/17 11:17, Dave Stevenson wrote:
> > Hi Sakari.
> > 
> > On 13 October 2017 at 22:09, Sakari Ailus <sakari.ailus@iki.fi> wrote:
> >> Hi Dave,
> >>
> >> On Wed, Sep 20, 2017 at 05:07:54PM +0100, Dave Stevenson wrote:
> >>> New helper function char *v4l2_fourcc2s(u32 fourcc, char *buf)
> >>> that converts a fourcc into a nice printable version.
> >>>
> >>> Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
> >>> ---
> >>>
> >>> No changes from v2 to v3
> >>>
> >>>  drivers/media/v4l2-core/v4l2-common.c | 18 ++++++++++++++++++
> >>>  include/media/v4l2-common.h           |  3 +++
> >>>  2 files changed, 21 insertions(+)
> >>>
> >>> diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
> >>> index a5ea1f5..0219895 100644
> >>> --- a/drivers/media/v4l2-core/v4l2-common.c
> >>> +++ b/drivers/media/v4l2-core/v4l2-common.c
> >>> @@ -405,3 +405,21 @@ void v4l2_get_timestamp(struct timeval *tv)
> >>>       tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
> >>>  }
> >>>  EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
> >>> +
> >>> +char *v4l2_fourcc2s(u32 fourcc, char *buf)
> >>> +{
> >>> +     buf[0] = fourcc & 0x7f;
> >>> +     buf[1] = (fourcc >> 8) & 0x7f;
> >>> +     buf[2] = (fourcc >> 16) & 0x7f;
> >>> +     buf[3] = (fourcc >> 24) & 0x7f;
> >>> +     if (fourcc & (1 << 31)) {
> >>> +             buf[4] = '-';
> >>> +             buf[5] = 'B';
> >>> +             buf[6] = 'E';
> >>> +             buf[7] = '\0';
> >>> +     } else {
> >>> +             buf[4] = '\0';
> >>> +     }
> >>> +     return buf;
> >>> +}
> >>> +EXPORT_SYMBOL_GPL(v4l2_fourcc2s);
> >>> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> >>> index aac8b7b..5b0fff9 100644
> >>> --- a/include/media/v4l2-common.h
> >>> +++ b/include/media/v4l2-common.h
> >>> @@ -264,4 +264,7 @@ const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
> >>>
> >>>  void v4l2_get_timestamp(struct timeval *tv);
> >>>
> >>> +#define V4L2_FOURCC_MAX_SIZE 8
> >>> +char *v4l2_fourcc2s(u32 fourcc, char *buf);
> >>> +
> >>>  #endif /* V4L2_COMMON_H_ */
> >>
> >> I like the idea but the use of a character pointer and assuming a length
> >> looks a bit scary.
> >>
> >> As this seems to be used uniquely for printing stuff, a couple of macros
> >> could be used instead. Something like
> >>
> >> #define V4L2_FOURCC_CONV "%c%c%c%c%s"
> >> #define V4L2_FOURCC_TO_CONV(fourcc) \
> >>         fourcc & 0x7f, (fourcc >> 8) & 0x7f, (fourcc >> 16) & 0x7f, \
> >>         (fourcc >> 24) & 0x7f, fourcc & BIT(31) ? "-BE" : ""
> 
> 'fourcc' should be in parenthesis '(fourcc)'.

Yes, I omitted those for readability here.

> 
> >>
> >> You could use it with printk-style functions, e.g.
> >>
> >>         printk("blah fourcc " V4L2_FOURCC_CONV " is a nice format",
> >>                V4L2_FOURCC_TO_CONV(fourcc));
> >>
> >> I guess it'd be better to add more parentheses around "fourcc" but it'd be
> >> less clear here that way.
> > 
> > I was following Hans' suggestion made in
> > https://www.spinics.net/lists/linux-media/msg117046.html
> > 
> > Hans: Do you agree with Sakari here to make it to a macro?
> 
> Not a bad idea. But I think we should add it to the public videodev2.h
> header in that case, allowing it to be used by applications as well.

Sounds good.

> 
> How about calling the defines V4L2_FOURCC_FMT and V4L2_FOURCC_FMT_ARGS?

printf(3) describes conversion specifiers (%...) and I thought of using
"CONV" there for that purpose to make it explicit. Fourcc, implicitly,
already is about formats. What would you think of V4L2_FOURCC_CONV and
V4L2_FOURCC_CONV_ARGS (or just V4L2_FOURCC_ARGS)?
  

Patch

diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
index a5ea1f5..0219895 100644
--- a/drivers/media/v4l2-core/v4l2-common.c
+++ b/drivers/media/v4l2-core/v4l2-common.c
@@ -405,3 +405,21 @@  void v4l2_get_timestamp(struct timeval *tv)
 	tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
 }
 EXPORT_SYMBOL_GPL(v4l2_get_timestamp);
+
+char *v4l2_fourcc2s(u32 fourcc, char *buf)
+{
+	buf[0] = fourcc & 0x7f;
+	buf[1] = (fourcc >> 8) & 0x7f;
+	buf[2] = (fourcc >> 16) & 0x7f;
+	buf[3] = (fourcc >> 24) & 0x7f;
+	if (fourcc & (1 << 31)) {
+		buf[4] = '-';
+		buf[5] = 'B';
+		buf[6] = 'E';
+		buf[7] = '\0';
+	} else {
+		buf[4] = '\0';
+	}
+	return buf;
+}
+EXPORT_SYMBOL_GPL(v4l2_fourcc2s);
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index aac8b7b..5b0fff9 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -264,4 +264,7 @@  const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
 
 void v4l2_get_timestamp(struct timeval *tv);
 
+#define V4L2_FOURCC_MAX_SIZE 8
+char *v4l2_fourcc2s(u32 fourcc, char *buf);
+
 #endif /* V4L2_COMMON_H_ */