Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp522594imu; Fri, 16 Nov 2018 06:15:06 -0800 (PST) X-Google-Smtp-Source: AJdET5cOfXyohnuyS68IAj+qaROqjpH8WGyI/2BbMGZui0+Vy/YSfNDD4sNFQUqF6B9tZwajNsgZ X-Received: by 2002:a62:c60a:: with SMTP id m10-v6mr11425135pfg.15.1542377706188; Fri, 16 Nov 2018 06:15:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542377706; cv=none; d=google.com; s=arc-20160816; b=LZwecSV+P8JR+dwEhMXVaUto7iYMUxsrD+b2NtRbXOGI7CKAZ0j6QDpRsK3XiToS4R 1f8SFh0SLMrm1XNIF2PcwCONBO3Eou1LcNyYBEj8vhtKO87s2Fzw4Y8F+X8zAh058jMl gtcEPVFOnkPhQD6Euzjiu5OZpWW1yIZDlKMDlazliTaZgL+LSVZ/68Wo7fB6a2fMKO+g v6U4GBfeAYbvjH8SbzxVNobaB1iOahy0NM6Hxr6V36UtaWM97NwuGqwbsfrQi4v8N61K lIiXXNsHbIP8hvKk518jJf6UVhijp/AlvUtsrgrnHz0FKaLddliM1gqX3yf7bWrE8ilF IMfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=IbmqKP/mNnI2vYC8ij+js4X062fzHOuhGfwabywhHMk=; b=l441VjrNkIXqLdD3XOe4Xw0gT71Xp6ssgTfdosqp1GfH+q4rMxWEc8waYnmCIJMqiF ghQw2bc3gJudlRg9GV4bOhkhk5c5EkoZwrYOPqJKmHfo4v9XSgEr93wSxtir9z8/D/7G LQDZsJosZ0+iRGBRyjJ9zDSaJczqIiE0H+GipMAd7/3x6wdC+bEbREJ3EINDLxZ+jarv mQG48HZfarmbtIIGqx9VFQUE5XWb7KG2I107KjBnCp8aTlEk1pHGtQigJn1/Q9VNHy8S DXfRPh9ozoEto0mrZ8dGVt8TCm705PcT4zw5dA6zouPFNKwz9RBIgDo7qDpNZrCwtFqg 6rQA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u35si28361160pga.226.2018.11.16.06.14.51; Fri, 16 Nov 2018 06:15:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389807AbeKQAZc (ORCPT + 99 others); Fri, 16 Nov 2018 19:25:32 -0500 Received: from lb1-smtp-cloud8.xs4all.net ([194.109.24.21]:39549 "EHLO lb1-smtp-cloud8.xs4all.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728024AbeKQAZc (ORCPT ); Fri, 16 Nov 2018 19:25:32 -0500 Received: from [IPv6:2001:983:e9a7:1:b167:1e29:5b51:e4fb] ([IPv6:2001:983:e9a7:1:b167:1e29:5b51:e4fb]) by smtp-cloud8.xs4all.net with ESMTPA id NeregC7X9cvkSNerfgRIDj; Fri, 16 Nov 2018 15:12:55 +0100 Subject: Re: [PATCH v3] media: vivid: Improve timestamping To: Gabriel Francisco Mandaji , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Cc: lkcamp@lists.libreplanetbr.org References: <20181111223937.GA24963@gfm-note> From: Hans Verkuil Message-ID: <2623431d-fa96-30f9-2d1c-e5c8470d31ff@xs4all.nl> Date: Fri, 16 Nov 2018 15:12:54 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20181111223937.GA24963@gfm-note> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4wfLlFjzgkOqdMms5UJltKVRkM60VignrrURqRwNBJLuuHxvYXoNCdHPZUKz8STjghUdZ6+jSiHTtT/UmbP1g39SOCrpJK/yCmFFa2Wl1QQM09NDW/uf64 CoMDYUpW7Vca87QTsazYLBa8mipVAJ5ZJC9KO56iHDUvn+3KIVJIy3yHnaabDu9b5IS8ca/cf520LE+mihZ+P2ptkgDNBtBo4QN8FGtJbX0WXbEmbHMNwFBf VhiAVTkQcdMLIPsE8iF1KgI2gKILk7ws8XUkV+ZVdYnS62iqOnUoyO5aWhHSa0zFlLUCU4Q+ZvFvOvsecEQb0lMfvE1YFOl9HkxxJ7Ith2CgDxGd+oyKZpZk qtNZomRqt3tkxZ0pj+kWdw0FTndZqdwJGrBKRnqc7S7myLKGJMk1/zJ8qnDMQzuttVG8jpQi Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/11/2018 11:39 PM, Gabriel Francisco Mandaji wrote: > Simulate a more precise timestamp by calculating it based on the > current framerate. > > Signed-off-by: Gabriel Francisco Mandaji Something is still wrong: the frame period calculation is fine for input 0 (webcam), but if I switch to another input (S-Video/TV/HDMI), then it is totally wrong: for input 3 (HDMI) the fps seems to be off by a factor of 10000 (i.e. I see a framerate of over 60000 where I expected to see 60 Hz), and for inputs 2 and 3 I get 7000 fps for PAL and 110000 for NTSC. v4l2-ctl -P (== VIDIOC_G_PARM) returns the right thing here. This calculation is the problem: u64 f_period = dev->timeperframe_vid_cap.numerator * 1000000000 / dev->timeperframe_vid_cap.denominator; You need to cast dev->timeperframe_vid_cap.numerator to a u64 first otherwise the multiplication overflows the u32 if numerator is > 4. Also note that you cannot divide 64 bit values: not all architectures support that. Use do_div instead. With that change it should work. Regards, Hans > --- > Changes in v2: > - fix spelling > - end of exposure is offset by 90% of the frame period > - fix timestamp calculation for FIELD_ALTERNATE (untested) > - timestamp is now calculated and set from vivid_thread_cap_tick() > - capture vbi uses the same timestamp as non-vbi, but offset by 5% > - timestamp stays consistent even if the FPS changes > - tested with dropped frames > Changes in v3: > - fix calculating offset for 'End of Frame' > - fix changing 'Timestamp Source' mid-capture > - change order of operation when calculating the VBI's offset > --- > drivers/media/platform/vivid/vivid-core.h | 3 ++ > drivers/media/platform/vivid/vivid-kthread-cap.c | 47 +++++++++++++++++------- > drivers/media/platform/vivid/vivid-vbi-cap.c | 4 -- > 3 files changed, 36 insertions(+), 18 deletions(-) > > diff --git a/drivers/media/platform/vivid/vivid-core.h b/drivers/media/platform/vivid/vivid-core.h > index 1891254..f7b2a0b 100644 > --- a/drivers/media/platform/vivid/vivid-core.h > +++ b/drivers/media/platform/vivid/vivid-core.h > @@ -392,6 +392,9 @@ struct vivid_dev { > /* thread for generating video capture stream */ > struct task_struct *kthread_vid_cap; > unsigned long jiffies_vid_cap; > + u64 cap_stream_start; > + u64 cap_frame_period; > + u64 cap_frame_eof_offset; > u32 cap_seq_offset; > u32 cap_seq_count; > bool cap_seq_resync; > diff --git a/drivers/media/platform/vivid/vivid-kthread-cap.c b/drivers/media/platform/vivid/vivid-kthread-cap.c > index eebfff2..f97cb6a 100644 > --- a/drivers/media/platform/vivid/vivid-kthread-cap.c > +++ b/drivers/media/platform/vivid/vivid-kthread-cap.c > @@ -425,12 +425,6 @@ static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf) > is_loop = true; > > buf->vb.sequence = dev->vid_cap_seq_count; > - /* > - * Take the timestamp now if the timestamp source is set to > - * "Start of Exposure". > - */ > - if (dev->tstamp_src_is_soe) > - buf->vb.vb2_buf.timestamp = ktime_get_ns(); > if (dev->field_cap == V4L2_FIELD_ALTERNATE) { > /* > * 60 Hz standards start with the bottom field, 50 Hz standards > @@ -554,14 +548,6 @@ static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf) > } > } > } > - > - /* > - * If "End of Frame" is specified at the timestamp source, then take > - * the timestamp now. > - */ > - if (!dev->tstamp_src_is_soe) > - buf->vb.vb2_buf.timestamp = ktime_get_ns(); > - buf->vb.vb2_buf.timestamp += dev->time_wrap_offset; > } > > /* > @@ -667,10 +653,25 @@ static void vivid_overlay(struct vivid_dev *dev, struct vivid_buffer *buf) > } > } > > +static void vivid_cap_update_frame_period(struct vivid_dev *dev) > +{ > + u64 f_period = dev->timeperframe_vid_cap.numerator * 1000000000 / > + dev->timeperframe_vid_cap.denominator; > + if (dev->field_cap == V4L2_FIELD_ALTERNATE) > + f_period /= 2; > + /* > + * If "End of Frame", then offset the exposure time by 0.9 > + * of the frame period. > + */ > + dev->cap_frame_eof_offset = f_period * 9 / 10; > + dev->cap_frame_period = f_period; > +} > + > static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs) > { > struct vivid_buffer *vid_cap_buf = NULL; > struct vivid_buffer *vbi_cap_buf = NULL; > + u64 f_time = 0; > > dprintk(dev, 1, "Video Capture Thread Tick\n"); > > @@ -702,6 +703,11 @@ static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs) > if (!vid_cap_buf && !vbi_cap_buf) > goto update_mv; > > + f_time = dev->cap_frame_period * dev->vid_cap_seq_count + > + dev->cap_stream_start + dev->time_wrap_offset; > + if (!dev->tstamp_src_is_soe) > + f_time += dev->cap_frame_eof_offset; > + > if (vid_cap_buf) { > v4l2_ctrl_request_setup(vid_cap_buf->vb.vb2_buf.req_obj.req, > &dev->ctrl_hdl_vid_cap); > @@ -721,9 +727,13 @@ static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs) > VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); > dprintk(dev, 2, "vid_cap buffer %d done\n", > vid_cap_buf->vb.vb2_buf.index); > + > + vid_cap_buf->vb.vb2_buf.timestamp = f_time; > } > > if (vbi_cap_buf) { > + u64 vbi_period; > + > v4l2_ctrl_request_setup(vbi_cap_buf->vb.vb2_buf.req_obj.req, > &dev->ctrl_hdl_vbi_cap); > if (dev->stream_sliced_vbi_cap) > @@ -736,6 +746,10 @@ static void vivid_thread_vid_cap_tick(struct vivid_dev *dev, int dropped_bufs) > VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); > dprintk(dev, 2, "vbi_cap %d done\n", > vbi_cap_buf->vb.vb2_buf.index); > + > + /* If capturing a VBI, offset by 0.05 */ > + vbi_period = dev->cap_frame_period * 5 / 100; > + vbi_cap_buf->vb.vb2_buf.timestamp = f_time + vbi_period; > } > dev->dqbuf_error = false; > > @@ -767,6 +781,8 @@ static int vivid_thread_vid_cap(void *data) > dev->cap_seq_count = 0; > dev->cap_seq_resync = false; > dev->jiffies_vid_cap = jiffies; > + dev->cap_stream_start = ktime_get_ns(); > + vivid_cap_update_frame_period(dev); > > for (;;) { > try_to_freeze(); > @@ -779,6 +795,9 @@ static int vivid_thread_vid_cap(void *data) > dev->jiffies_vid_cap = cur_jiffies; > dev->cap_seq_offset = dev->cap_seq_count + 1; > dev->cap_seq_count = 0; > + dev->cap_stream_start += dev->cap_frame_period * > + dev->cap_seq_offset; > + vivid_cap_update_frame_period(dev); > dev->cap_seq_resync = false; > } > numerator = dev->timeperframe_vid_cap.numerator; > diff --git a/drivers/media/platform/vivid/vivid-vbi-cap.c b/drivers/media/platform/vivid/vivid-vbi-cap.c > index 903cebe..17aa4b0 100644 > --- a/drivers/media/platform/vivid/vivid-vbi-cap.c > +++ b/drivers/media/platform/vivid/vivid-vbi-cap.c > @@ -95,8 +95,6 @@ void vivid_raw_vbi_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf) > > if (!VIVID_INVALID_SIGNAL(dev->std_signal_mode)) > vivid_vbi_gen_raw(&dev->vbi_gen, &vbi, vbuf); > - > - buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset; > } > > > @@ -119,8 +117,6 @@ void vivid_sliced_vbi_cap_process(struct vivid_dev *dev, > for (i = 0; i < 25; i++) > vbuf[i] = dev->vbi_gen.data[i]; > } > - > - buf->vb.vb2_buf.timestamp = ktime_get_ns() + dev->time_wrap_offset; > } > > static int vbi_cap_queue_setup(struct vb2_queue *vq, >