Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp849414imm; Wed, 10 Oct 2018 05:24:27 -0700 (PDT) X-Google-Smtp-Source: ACcGV610FezMaGhWh7RzpFXCJtTFbmh/Ar5lUqrvDtZl6EVMtWa0X9o6wLKwIzQnUcK3P2btPSFN X-Received: by 2002:a63:cb51:: with SMTP id m17-v6mr28933846pgi.105.1539174267336; Wed, 10 Oct 2018 05:24:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539174267; cv=none; d=google.com; s=arc-20160816; b=pZH7zD45sba1hp55Ldnm+rgdd4DpIB+f8sHPuU7sG43jJL1EjqOD29cv3RSnNjMBEg YVbRQpb2r290qjbLZa2L6KGnMed0rRVDx2YM/9LewwWKminvLiaVBNYlydz4YFb4Zatf TAKLeGoFWQ2WwoAflmhAwSHBmp+0Oee+wSytSex16cFH5+thzgnhzhXdubVTDvYhKQaQ 5rFpJf5ctuHkXEoeD+yZpAkd9MstV/dYJO6xjPTi+JkdoyClhJWbF7ye6ciLQ5/Pwgvp d0SocydqpyRfrWQy4UqWMOC0SoY9tSlpMvK+C5GrCT1m64Ca5ZPhRpNagx4kAOL6CQk2 AHbA== 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=Ew98eqyYMUN+METqjTsytUausXcEjYuFOn0fl1HQcQo=; b=Ui3oPFxlmG2gRPoUYcAXhTdnNRkkjzGkJw1NP30PKbymD0XzmG4Mep57ZujYGPKLmr N6sxWkYiX6VoW6cl49vfnekPARpEGbmIHafj3twqNzwp0JixlkXm2PRpdENmVduncJou Frh/15gi1oo9XVap5ZV+GSQ9ym3+tuj5rXhQKcO+BvKLFuZZbyfrXQWcrN8deNxjyRZh 1J5Rdw+EFzfZFR9zqTz9fCryfQxTf6jfz08/GUSt4kDZz+Z05dBowfCw+/gjnbNKjVJK plbhAghTZJXgdpit/XeOvSVTOm4SpdPF4BJiqixIN1rsMWtVn7vB6O6vMNzouT2lLN0t 21UA== 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 r6-v6si27693482pfc.253.2018.10.10.05.24.11; Wed, 10 Oct 2018 05:24:27 -0700 (PDT) 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 S1726712AbeJJToP (ORCPT + 99 others); Wed, 10 Oct 2018 15:44:15 -0400 Received: from lb3-smtp-cloud8.xs4all.net ([194.109.24.29]:47399 "EHLO lb3-smtp-cloud8.xs4all.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726579AbeJJToP (ORCPT ); Wed, 10 Oct 2018 15:44:15 -0400 Received: from [IPv6:2001:420:44c1:2579:144:1d36:11b8:bf53] ([IPv6:2001:420:44c1:2579:144:1d36:11b8:bf53]) by smtp-cloud8.xs4all.net with ESMTPA id ADVFg2NqZ0ZZEADVJgFMtb; Wed, 10 Oct 2018 14:22:17 +0200 Subject: Re: [PATCH] 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: <20181010004921.GA6532@gfm-note> From: Hans Verkuil Message-ID: Date: Wed, 10 Oct 2018 14:22:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20181010004921.GA6532@gfm-note> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4wfBEYAYsVsNlVn56T5cyvldktHGsYqXZPrxVixl2pzz7Ar5LCHjo3d8fuD9ZEnfdsR1dZGKFM7vg6gOl2F4/tYr/5igCgzSHtoqPXxjVRsvWWpXSVyD14 Evpvu+E064NHcdk/lokvfPb4/2eAPnpKIti60cBK1LDDBExioJugjfDDzjiKY0ZZkCERvHpyP2KaOKDmzaLVn2HRYNZzv5GIboXqNfKHHmYn6/SjISCK1gLi UtR9IYcXQXYiZuLWUxUDPtqD2BPm0yoS3xHHLEIN2lnya/xyHoBFeMFefddyNfGFmgQMQ1sKEyUqbGHpL8PbuT7ujX9OK2wqn69OiauH8gklw1p1lxjcp+0m GD6ywzpwqJyMAfR4Sz3gmDGRT6aqc8zWOGyYK8565+I09lEnDHAI+IfgF3MAop/83KLXUHq4juJb70NYuN0Q6T3noyRZAA== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Gabriel, Thank for you this patch! I do have some comments, see below... On 10/10/18 02:49, Gabriel Francisco Mandaji wrote: > Simulate a more precise timestamp by calculating it based on the > current framerate. > > Signed-off-by: Gabriel Francisco Mandaji > --- > drivers/media/platform/vivid/vivid-core.h | 1 + > drivers/media/platform/vivid/vivid-kthread-cap.c | 24 ++++++++++++++++-------- > 2 files changed, 17 insertions(+), 8 deletions(-) > > diff --git a/drivers/media/platform/vivid/vivid-core.h b/drivers/media/platform/vivid/vivid-core.h > index cd4c823..cbdadd8 100644 > --- a/drivers/media/platform/vivid/vivid-core.h > +++ b/drivers/media/platform/vivid/vivid-core.h > @@ -384,6 +384,7 @@ struct vivid_dev { > /* thread for generating video capture stream */ > struct task_struct *kthread_vid_cap; > unsigned long jiffies_vid_cap; > + u64 cap_stream_start; > 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 f06003b..0793b15 100644 > --- a/drivers/media/platform/vivid/vivid-kthread-cap.c > +++ b/drivers/media/platform/vivid/vivid-kthread-cap.c > @@ -416,6 +416,7 @@ static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf) > char str[100]; > s32 gain; > bool is_loop = false; > + u64 soe_time = 0; > > if (dev->loop_video && dev->can_loop_video && > ((vivid_is_svid_cap(dev) && > @@ -426,11 +427,11 @@ static void vivid_fillbuff(struct vivid_dev *dev, struct vivid_buffer *buf) > > buf->vb.sequence = dev->vid_cap_seq_count; > /* > - * Take the timestamp now if the timestamp source is set to > - * "Start of Exposure". > + * Store the current time to calculate the delta if source is set to > + * "End of Frame". > */ > - if (dev->tstamp_src_is_soe) > - buf->vb.vb2_buf.timestamp = ktime_get_ns(); > + if (!dev->tstamp_src_is_soe) > + soe_time = ktime_get_ns(); > if (dev->field_cap == V4L2_FIELD_ALTERNATE) { > /* > * 60 Hz standards start with the bottom field, 50 Hz standards > @@ -556,12 +557,18 @@ 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 "End of Frame", then calculate the "exposition time" and add exposition -> exposure > + * it to the timestamp. > */ > if (!dev->tstamp_src_is_soe) > - buf->vb.vb2_buf.timestamp = ktime_get_ns(); > - buf->vb.vb2_buf.timestamp += dev->time_wrap_offset; > + soe_time = ktime_get_ns() - soe_time; This isn't going to work. The soe_time is variable since the time it takes vivid to fill the buffer will be variable. And the whole purpose of this patch is to make it constant (since that's what happens in a real webcam). I would just set soe_time to 0.9 times the frame period, and then add this constant to the calculated timestamp. > + buf->vb.vb2_buf.timestamp = dev->timeperframe_vid_cap.numerator * > + 1000000000 / > + dev->timeperframe_vid_cap.denominator * I would first calculate the frame period in ns and store it in a temporary variable, then use that to calculate the timestamp. Note that the current code is wrong in case of FIELD_ALTERNATE (i.e. each buffer contains a top or bottom field: in that case you first need to divide the frame period by 2 to get the field period. > + dev->vid_cap_seq_count + > + dev->cap_stream_start + > + soe_time + > + dev->time_wrap_offset; > } > > /* > @@ -759,6 +766,7 @@ 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(); You also need to do this in the 'if (dev->cap_seq_resync) {' a few lines below this. cap_seq_resync is set to true whenever the framerate is modified by userspace. In fact, I think it would help if the timestamp calculation is moved from vivid_fillbuff() to vivid_thread_vid_cap_tick(), since the same timestamp should be used for both video and vbi (with the vbi timestamp slightly later compared to the video timestamp, 0.05*frame_period would be reasonable offset). That means that vivid_sliced_vbi_cap_process() and vivid_raw_vbi_cap_process() no longer set the timestamp there. The same exercise should also be done for video output, but let's get video capture right first. > > for (;;) { > try_to_freeze(); > Did you check what happens when you drop frames? And wrapping the timestamp around? I think those corner cases will work fine, but make sure you test it. Regards, Hans