diff mbox series

[PATCHv5,5/8] videodev2.h: add v4l2_timeval_to_ns inline function

Message ID 20181212123901.34109-6-hverkuil-cisco@xs4all.nl (mailing list archive)
State New, archived
Headers show
Series vb2/cedrus: use timestamps to identify buffers | expand

Commit Message

Hans Verkuil Dec. 12, 2018, 12:38 p.m. UTC
From: Hans Verkuil <hverkuil-cisco@xs4all.nl>

We want to be able to uniquely identify buffers for stateless
codecs. The internal timestamp (a u64) as stored internally in the
kernel is a suitable candidate for that, but in struct v4l2_buffer
it is represented as a struct timeval.

Add a v4l2_timeval_to_ns() function that converts the struct timeval
into a u64 in the same way that the kernel does. This makes it possible
to use this u64 elsewhere as a unique identifier of the buffer.

Since timestamps are also copied from the output buffer to the
corresponding capture buffer(s) by M2M devices, the u64 can be
used to refer to both output and capture buffers.

The plan is that in the future we redesign struct v4l2_buffer and use
u64 for the timestamp instead of a struct timeval (which has lots of
problems with 32 vs 64 bit and y2038 layout changes), and then there
is no more need to use this function.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
---
 include/uapi/linux/videodev2.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Jonas Karlman Dec. 16, 2018, 9:47 p.m. UTC | #1
On 2018-12-12 13:38, hverkuil-cisco@xs4all.nl wrote:
> From: Hans Verkuil <hverkuil-cisco@xs4all.nl>
>
> We want to be able to uniquely identify buffers for stateless
> codecs. The internal timestamp (a u64) as stored internally in the
> kernel is a suitable candidate for that, but in struct v4l2_buffer
> it is represented as a struct timeval.
>
> Add a v4l2_timeval_to_ns() function that converts the struct timeval
> into a u64 in the same way that the kernel does. This makes it possible
> to use this u64 elsewhere as a unique identifier of the buffer.
>
> Since timestamps are also copied from the output buffer to the
> corresponding capture buffer(s) by M2M devices, the u64 can be
> used to refer to both output and capture buffers.
>
> The plan is that in the future we redesign struct v4l2_buffer and use
> u64 for the timestamp instead of a struct timeval (which has lots of
> problems with 32 vs 64 bit and y2038 layout changes), and then there
> is no more need to use this function.
>
> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
> ---
>  include/uapi/linux/videodev2.h | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
> index 2db1635de956..3580c1ea4fba 100644
> --- a/include/uapi/linux/videodev2.h
> +++ b/include/uapi/linux/videodev2.h
> @@ -971,6 +971,18 @@ struct v4l2_buffer {
>  	};
>  };
>  
> +/**
> + * v4l2_timeval_to_ns - Convert timeval to nanoseconds
> + * @ts:		pointer to the timeval variable to be converted
> + *
> + * Returns the scalar nanosecond representation of the timeval
> + * parameter.
> + */
> +static inline u64 v4l2_timeval_to_ns(const struct timeval *tv)
> +{
> +	return (__u64)tv->tv_sec * 1000000000ULL + tv->tv_usec * 1000;
> +}
This is causing a compile issue in userspace application, replacing u64
with __u64 solves the compile issue below.

In file included from libavcodec/v4l2_request.h:22,
                 from libavcodec/v4l2_request.c:28:
/home/docker/LibreELEC/build.LibreELEC-H3.arm-9.0-devel/toolchain/armv7ve-libreelec-linux-gnueabi/sysroot/usr/include/linux/videodev2.h:975:19:
error: unknown type name 'u64'
 static __inline__ u64 v4l2_timeval_to_ns(const struct timeval *tv)
                   ^~~

Regards,
Jonas
> +
>  /*  Flags for 'flags' field */
>  /* Buffer is mapped (flag) */
>  #define V4L2_BUF_FLAG_MAPPED			0x00000001
Hans Verkuil Dec. 17, 2018, 9:24 a.m. UTC | #2
On 12/16/18 10:47 PM, Jonas Karlman wrote:
> 
> On 2018-12-12 13:38, hverkuil-cisco@xs4all.nl wrote:
>> From: Hans Verkuil <hverkuil-cisco@xs4all.nl>
>>
>> We want to be able to uniquely identify buffers for stateless
>> codecs. The internal timestamp (a u64) as stored internally in the
>> kernel is a suitable candidate for that, but in struct v4l2_buffer
>> it is represented as a struct timeval.
>>
>> Add a v4l2_timeval_to_ns() function that converts the struct timeval
>> into a u64 in the same way that the kernel does. This makes it possible
>> to use this u64 elsewhere as a unique identifier of the buffer.
>>
>> Since timestamps are also copied from the output buffer to the
>> corresponding capture buffer(s) by M2M devices, the u64 can be
>> used to refer to both output and capture buffers.
>>
>> The plan is that in the future we redesign struct v4l2_buffer and use
>> u64 for the timestamp instead of a struct timeval (which has lots of
>> problems with 32 vs 64 bit and y2038 layout changes), and then there
>> is no more need to use this function.
>>
>> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
>> ---
>>  include/uapi/linux/videodev2.h | 12 ++++++++++++
>>  1 file changed, 12 insertions(+)
>>
>> diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
>> index 2db1635de956..3580c1ea4fba 100644
>> --- a/include/uapi/linux/videodev2.h
>> +++ b/include/uapi/linux/videodev2.h
>> @@ -971,6 +971,18 @@ struct v4l2_buffer {
>>  	};
>>  };
>>  
>> +/**
>> + * v4l2_timeval_to_ns - Convert timeval to nanoseconds
>> + * @ts:		pointer to the timeval variable to be converted
>> + *
>> + * Returns the scalar nanosecond representation of the timeval
>> + * parameter.
>> + */
>> +static inline u64 v4l2_timeval_to_ns(const struct timeval *tv)
>> +{
>> +	return (__u64)tv->tv_sec * 1000000000ULL + tv->tv_usec * 1000;
>> +}
> This is causing a compile issue in userspace application, replacing u64
> with __u64 solves the compile issue below.
> 
> In file included from libavcodec/v4l2_request.h:22,
>                  from libavcodec/v4l2_request.c:28:
> /home/docker/LibreELEC/build.LibreELEC-H3.arm-9.0-devel/toolchain/armv7ve-libreelec-linux-gnueabi/sysroot/usr/include/linux/videodev2.h:975:19:
> error: unknown type name 'u64'
>  static __inline__ u64 v4l2_timeval_to_ns(const struct timeval *tv)
>                    ^~~

Oops, I missed that one. Fixed in my git branch.

Thanks for reporting this!

	Hans

> 
> Regards,
> Jonas
>> +
>>  /*  Flags for 'flags' field */
>>  /* Buffer is mapped (flag) */
>>  #define V4L2_BUF_FLAG_MAPPED			0x00000001
diff mbox series

Patch

diff --git a/include/uapi/linux/videodev2.h b/include/uapi/linux/videodev2.h
index 2db1635de956..3580c1ea4fba 100644
--- a/include/uapi/linux/videodev2.h
+++ b/include/uapi/linux/videodev2.h
@@ -971,6 +971,18 @@  struct v4l2_buffer {
 	};
 };
 
+/**
+ * v4l2_timeval_to_ns - Convert timeval to nanoseconds
+ * @ts:		pointer to the timeval variable to be converted
+ *
+ * Returns the scalar nanosecond representation of the timeval
+ * parameter.
+ */
+static inline u64 v4l2_timeval_to_ns(const struct timeval *tv)
+{
+	return (__u64)tv->tv_sec * 1000000000ULL + tv->tv_usec * 1000;
+}
+
 /*  Flags for 'flags' field */
 /* Buffer is mapped (flag) */
 #define V4L2_BUF_FLAG_MAPPED			0x00000001