Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp501660lqb; Wed, 29 May 2024 02:04:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhtPi6wOaGFt8iyAlvnPt5n732gKBTFFoC0sMAwhoO/cva6sOo/JYSMZd+Y/hsOT2lCvWTZkihuZEVu+OhBI5XcC1M9IB74jSUkU3tYg== X-Google-Smtp-Source: AGHT+IHJWEpOdviPUB/IqW9tYJHezsuXYA/U23LMqPyH3IfDozwgJnZLtB7sbYfGE5j0wnXlRulr X-Received: by 2002:a05:6871:82c:b0:24c:b884:ac7d with SMTP id 586e51a60fabf-24cb884cce7mr14861433fac.12.1716973472558; Wed, 29 May 2024 02:04:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716973472; cv=pass; d=google.com; s=arc-20160816; b=jxn+2R/MIqVZD8qgID+x7LZqq9DrhxAQHazVyBRRdhFZ5+28h7BKdQUAVQegOvb282 GBBbr0sMdj4FkwoSZJXSlVFzCKHYTwkVRTCkQ4xKRkQ8beww1cmlTovKpjip3BdEbYGP QlZT+iIeasLjxnmGTbcP6v5BenHBe7UjbAfAY+G+7vjD5cYYHwgn/zWXTCRjFCY5kaFf GloC1jq1xLyuOS3AjpPCFdc2t/+us6A61bUCGNKdF69azTLLw48XDJNVLIIpBqh46sgq jwmkse1FrOQ4XlkOrfawW72dOwUsWARbEjLwR8QXDATBx58mw/VsVoo2YvHLYwyknXs8 4i8w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=3qenqQXhZZCyagreSZ+ZOYO//Et9NuVOjmhUJ/lmfY0=; fh=DTavfcyZYFqVgigjwyGIq7WZw3bokGz6Yi9K6AWHQlc=; b=VJ0Qv1YVdHhm1eHkujxfI3iqacv+KsfKPi7e0SO+/Igz+GEfnHuFyvTniaI7eX6eEU cxTpXYV64x688ULdFGaXGv4CxlyA3Q4/x7Jj6JGGAbpSc/nWd7sO5O7drYBm7/r/0Z3t pNRPpI65JI8z4OSpxEG0REUtiN+nfAr3KRZJr6psCvL6LwKhLw7yfnGVMuAB9v3MZqC3 cjtdJuV52eDL5ZSvx0yQbrgsANXBRzTca90filU8YH3JZQrr8dbY4etHJffZzFEvXNgC T0r9uc96geICjvBAWLgYcDek3gjaf2dCzzW6QUaEi6CBKAwtzH9mPEAhWl75ej2pDQlx L7Nw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AG4zH0A9; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-193753-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193753-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f8fc046e72si9771232b3a.64.2024.05.29.02.04.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 02:04:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-193753-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AG4zH0A9; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-193753-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193753-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id B250FB2ECD8 for ; Wed, 29 May 2024 08:36:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E357A169361; Wed, 29 May 2024 08:30:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AG4zH0A9" Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CD2615CD50 for ; Wed, 29 May 2024 08:30:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716971433; cv=none; b=nlx9qZToPAFyp8nutAfZ5lBNxaPMNXQwtdAXCurmCP4NZxLM92pA2df+UGdzKkzmzifdPdRmErWTaM8R9tKKlxnUiBQRJHQGHSx1ALWRAL1dHFXVxx8j3aI0xFaTdj2UiXsVbq9stBx5eMCucp2zlD5CX7drHOEsZ1FrRsOPpYk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716971433; c=relaxed/simple; bh=KalOd4VNB9ddnqI5SJEA/fxZiA7LJ85CstT18qQ+mEg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GiMM0bXE33VA1AgygymoQQt6bhOgG+8JDf8Beytt5roZB/rzP5QR7bD8+GK7p3M3ok/3n21whCmfpWxTMT6Gf5r6H/QPZlzK0pLe7l6oVTXjRI0EMyNpv2eRObeDWlpIHiDaPRp8tXhxoNTlJPEqYmF81Vwuy4v5sp1/hrd3VYE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AG4zH0A9; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-70224f928edso39611b3a.0 for ; Wed, 29 May 2024 01:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1716971431; x=1717576231; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=3qenqQXhZZCyagreSZ+ZOYO//Et9NuVOjmhUJ/lmfY0=; b=AG4zH0A9IWb1LVOZAVTzjwqJpHpt9d4/DKxdxn7HS1+aelMJ0zXazylpAUxuw82fxE 8xMBPOpyFW9ERkZ3cSSYc5aGcUiPJXoIJ8uY3wrOyRddUseAe5vO73oIPeNykgz/1P72 iGZUHkFMUTWEPhm/5NJPAXaztaaiNoLUiVSrI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716971431; x=1717576231; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=3qenqQXhZZCyagreSZ+ZOYO//Et9NuVOjmhUJ/lmfY0=; b=l/G6S+mdF0Lt3pXhyoxY8bwZUq8Y4wC7pChujjsvY72t6DeQQBC/QYSl8WtnOurQ4Q nkK86bHtHgE6EFe17udX6v3GmRi3NVzc1UJ1R//CvIJ4WXpOuF79iVo30udgFLbftjF5 8OzyfZvjX/20cCaljWYMha1TznmIMQx0F6JyxIdFl+XWWDJwEF3tbhpkQkZv6wWXtPYE YicKQqjro5rDTpNFOpi6rs8AkpfsctB0je97+BXySuRi0yu7iV/4wUrXgbmye5M754uK Xf7bjy0QcOVjpJIO3aWPzs1D0uMIgohVO5uViOqqzDAaSDfmfFQ/kZodpGfAW/M8Yi9q WvBw== X-Forwarded-Encrypted: i=1; AJvYcCVj1nY1eNP/Tz/8lD0EV1E7YYmk2HONa4bVejoEcqWSiGEFaoUVCS8GXJOzmq5zo9oFn+ponsWGErowKg9PDXztrs2ufSa+Iu6fkqwf X-Gm-Message-State: AOJu0Yx5yXILhgxpQ7OzaCRldMxKu2r3PbxihSGpgfFqcN81vdpPMaeo o1WH3witU2KBcx+f6M3/5HI6bmSoAhcdQhd6Zkgmpn1Fea9eqZzMezHnqAqrnw== X-Received: by 2002:a05:6a00:4405:b0:6f4:3fe7:7aa9 with SMTP id d2e1a72fcca58-6f8f2d6e5f3mr18821491b3a.10.1716971430731; Wed, 29 May 2024 01:30:30 -0700 (PDT) Received: from chromium.org (174.71.80.34.bc.googleusercontent.com. [34.80.71.174]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f8fbf2e3c8sm7576786b3a.41.2024.05.29.01.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 01:30:30 -0700 (PDT) Date: Wed, 29 May 2024 17:30:27 +0900 From: Tomasz Figa To: Ricardo Ribalda Cc: Laurent Pinchart , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, "hn.chen" , Hans Verkuil , Sergey Senozhatsky Subject: Re: [PATCH v10 6/6] media: uvcvideo: Fix hw timestamp handling for slow FPS Message-ID: References: <20240323-resend-hwtimestamp-v10-0-b08e590d97c7@chromium.org> <20240323-resend-hwtimestamp-v10-6-b08e590d97c7@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240323-resend-hwtimestamp-v10-6-b08e590d97c7@chromium.org> On Sat, Mar 23, 2024 at 10:48:07AM +0000, Ricardo Ribalda wrote: > In UVC 1.5 we get a single clock value per frame. With the current > buffer size of 32, FPS slowers than 32 might roll-over twice. > > The current code cannot handle two roll-over and provide invalid > timestamps. > > Remove all the samples from the circular buffer that are more than two > rollovers old, so the algorithm always provides good timestamps. > > Note that we are removing values that are more than one second old, > which means that there is enough distance between the two points that > we use for the interpolation to provide good values. > > Tested-by: HungNien Chen > Reviewed-by: Sergey Senozhatsky > Signed-off-by: Ricardo Ribalda > --- > drivers/media/usb/uvc/uvc_video.c | 24 ++++++++++++++++++++++++ > drivers/media/usb/uvc/uvcvideo.h | 1 + > 2 files changed, 25 insertions(+) > > diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c > index 5df8f61d39cd1..900b57afac93a 100644 > --- a/drivers/media/usb/uvc/uvc_video.c > +++ b/drivers/media/usb/uvc/uvc_video.c > @@ -471,8 +471,31 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock, > { > unsigned long flags; > > + /* > + * If we write new data on the position where we had the last > + * overflow, remove the overflow pointer. There is no overflow > + * on the whole circular buffer. > + */ > + if (clock->head == clock->last_sof_overflow) > + clock->last_sof_overflow = -1; > + > spin_lock_irqsave(&clock->lock, flags); > > + /* Handle overflows */ > + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { > + /* > + * Remove data from the circular buffer that is older than the > + * last overflow. We only support one overflow per circular > + * buffer. > + */ > + if (clock->last_sof_overflow != -1) { > + clock->count = (clock->head - clock->last_sof_overflow > + + clock->count) % clock->count; > + } > + clock->last_sof_overflow = clock->head; > + } > + > + /* Add sample */ > clock->samples[clock->head] = *sample; > clock->head = (clock->head + 1) % clock->size; > clock->count = min(clock->count + 1, clock->size); > @@ -616,6 +639,7 @@ static void uvc_video_clock_reset(struct uvc_clock *clock) > clock->head = 0; > clock->count = 0; > clock->last_sof = -1; > + clock->last_sof_overflow = -1; > clock->sof_offset = -1; > } > > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h > index cb9dd50bba8ac..fb9f9771131ac 100644 > --- a/drivers/media/usb/uvc/uvcvideo.h > +++ b/drivers/media/usb/uvc/uvcvideo.h > @@ -499,6 +499,7 @@ struct uvc_streaming { > unsigned int head; > unsigned int count; > unsigned int size; > + unsigned int last_sof_overflow; > > u16 last_sof; > u16 sof_offset; > > -- > 2.44.0.396.g6e790dbe36-goog > > Given that majority of cameras kind of run ~30 fps, this should improve the timestamps for quite a lot of the users. Thanks. Reviewed-by: Tomasz Figa Best regards, Tomasz