From patchwork Fri Mar 10 09:01:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 67285 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp763605wrd; Fri, 10 Mar 2023 01:08:24 -0800 (PST) X-Google-Smtp-Source: AK7set/mwmUeBbPG/h7m9FU4+JxZj2Bx2yDXebC9gNqnnw+w6EWamNDDxczhqo8aMhukgZGPHNd/ X-Received: by 2002:a17:90b:1b10:b0:237:40a5:7acf with SMTP id nu16-20020a17090b1b1000b0023740a57acfmr26218640pjb.33.1678439304145; Fri, 10 Mar 2023 01:08:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678439304; cv=none; d=google.com; s=arc-20160816; b=gFhDdAVFGIdjRzR/e8WIgMb7SMX1P64sNV/w+MXBO4BAPUe+dk2ySNGhc55Nn2r+V8 9EQ3pbNsQekr5XWc+j21B5Iasal4xWKIdB7LDD9kFeje9DrlMMsMpbaMg918+BfvnTVl flWOuJX99WSE/9k3Z0Fnz6t/gamPl1PrWioUm61kn2LFkpxw8ZuHoW4ADGhrp1sNLbf0 CKOtccgpG+wdKdKTDrasASQ2m7avEnxu5BOHTb85nPwOrQoRhRNpMWDHELeRLwxcVeWo RDbb8xvNSO/5SOOejPMhYL81vYSIbS0HIr3YV8KQWz8cYlzEgAMBy3tpyeSxsRmD+PPw f+ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=GIJ4NYsOVV8RZeylhHPPojbsvAFmmoT5ppiz3o3U3kxO00C1cXjFGrr7rM6/9URmFG JQcy5uYR5ouddgtBC+QdqD34rXf34zFwKM2av+W3KCsXpHsjiiDD3rjAzJkdn8y9E0IW YiXUMqc7LUA9r7aRV2l5Ug47zPPJ2uZ6Oy9CA50sTA0s7S1/EKACPF1ZIL5P2EsZK9Rw LFIDWzj8aT6eENWbgDFev+sIsiHO8PIwsWzKUmwaFwiSfU4wTJS1Rdx618SighrUBMzH 1s6TCbFiDvYSZp/IjctSrTfOLn1Ehp4lrJpW6uWNQc2/oQ9CR/8+VqSUN0L8O4Q2y6zS 7z1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=auahc3+p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v2-20020a17090a898200b00230ca3efcf3si1436228pjn.158.2023.03.10.01.08.10; Fri, 10 Mar 2023 01:08:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=auahc3+p; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231538AbjCJJHg (ORCPT + 99 others); Fri, 10 Mar 2023 04:07:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230098AbjCJJGk (ORCPT ); Fri, 10 Mar 2023 04:06:40 -0500 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AF0E7DF81 for ; Fri, 10 Mar 2023 01:02:35 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id ec29so17630540edb.6 for ; Fri, 10 Mar 2023 01:02:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678438934; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=auahc3+pJeuaf50PijN97Rc1bShULdg8glH6J/F2ecGmAk6HjZ2P6FY++By0j9Wzpy nzGKYmfnG32/bfAFFgwu0GVMYVf50UTIlIs6CrBFKzbRse2c1VFHSkaL9ewdJ0O6xAc+ ws4F4we3XAVtEiiqgXTiluJxDHoCiZQwoYrGc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678438934; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RN9dk0TN64Mo7WHVhpVGbABaRx7Q/q6Pwknupu3adDk=; b=w+asR4fAr4gSvuZMVGlOR5D/2OWNxZxqKKeH3bfzU/XullmdQSNN/iPEwDlLNVai3s TJbztDiGIVWwSw12NP+tOkDNrHGGo1y5dH7Bz9FQB25iKll+SCIYJ5/cAF8YYfXd2NNq iF6g3KTt16Oi5dZplWHBQXWZk5F1z0jRPp8JjMt392llJBa+G5OcdwT/cRdKFqDXjcmV p68HTvJRgpQKkxMDXpSaZs7C3GElJRIQVH1663z9kUTmOFGiL6WVxmp1sETqQo3XA6aI 0G+IA6PHBH6VIbGniEmRgECaH6H83LJ3JuxiNPo292rTylM1Y+2oN45x7vbCFXO0NWZV AvRg== X-Gm-Message-State: AO0yUKXk1kRUQS9On1p/1CWWrdppd+nV+iDgsQeorjXXXVrO8cCvrh3n 1aX4Mb7Pi8qtZRgVoV1QO9V2MQ== X-Received: by 2002:a17:907:a808:b0:8aa:c35b:a34b with SMTP id vo8-20020a170907a80800b008aac35ba34bmr25771623ejc.20.1678438934536; Fri, 10 Mar 2023 01:02:14 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:ef69:7ab6:87ac:99f]) by smtp.gmail.com with ESMTPSA id s5-20020a170906454500b008b23b22b062sm692931ejq.114.2023.03.10.01.02.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 01:02:14 -0800 (PST) From: Ricardo Ribalda Date: Fri, 10 Mar 2023 10:01:31 +0100 Subject: [PATCH v7 4/6] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v7-4-cf1d78bb8821@chromium.org> References: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v7-0-cf1d78bb8821@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: Ricardo Ribalda , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Sergey Senozhatsky , "hn.chen" X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=1966; i=ribalda@chromium.org; h=from:subject:message-id; bh=Ka10ItjemOVlrqrWVbbdhzTBAJcpymfvLrHEyyU17oM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCvIMxQ6NquFZoQQOCgrRDaCRelepwRKDHcV6hDD7 +ALRcGmJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAryDAAKCRDRN9E+zzrEiBQUEA CJkPTC56XQ5o/bYlLjZwg+MwUglsKtjo3UStrY7DZ82nwL3w0vF6A8FBKOODmvShxR17gikEdhhgi+ 4E5NUybANa4TrbHQ5T+dUmZFRvvk0kRLUkGcl6s4VDDNmD7a9FDmMhgOjkuK6VV6/f+XjhZVj2Dx3V x+8Gb5HcGdpUT3rYIzJPW8ASyVC6o9tUz+VQiJt8EyO0RAKNCeJVwxXsdvZ7NxyX5qEQNVZ/IO27bV FDxPm7w/5eAF5gd7CbJRZ/y7TCWRse6xU0bxM53OfJnXbBFOSLe8aYaXoyYFo828PTIYnSD+SH4NyV SSVVQ+dTW2k0918lsL8Fm6OvFCpeM1hQALewDDWE5jG70NWY2yrb0H+6FNhX7Zu5T2sVQztFCZKSRx j+c0QMLZ7EnSrHsDTKvLn8kP8kDDPTZw5s30PPoxfQ+qA29/83RUbueaAryTJF06kMPYz9DuFiqFBc yqdxdFYvj7yWgiogpjWZJzcYzYcsZxHkSrnQx8ZLahAQmbKb1vU0ArOeE2S+wxFs4sNT0a4a+pvGaP ItfdV7jSgdGo6mIGf3mFvL+FUXsTgrRPF768VzNpdXp3WDc4+7Sn2oVI0sfC+lgYFBZrf9hg9IylZ0 I2t8DzZMMnzLKQ6UWbazGtMJLPwE2xnE4txXjYaLfqrSqVI/NuDGzCS5heZg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759971171724612492?= X-GMAIL-MSGID: =?utf-8?q?1759971171724612492?= With UVC 1.5 we get as little as one clock sample per frame. Which means that it takes 32 frames to move from the software timestamp to the hardware timestamp method. This results in abrupt changes in the timestamping after 32 frames (~1 second), resulting in noticeable artifacts when used for encoding. With this patch we modify the update algorithm to work with whatever amount of values are available. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 4d566edb73e7..6d0243ea0e07 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -754,10 +754,10 @@ void uvc_video_clock_update(struct uvc_streaming *stream, spin_lock_irqsave(&clock->lock, flags); - if (clock->count < clock->size) + if (clock->count < 2) goto done; - first = &clock->samples[clock->head]; + first = &clock->samples[(clock->head - clock->count + clock->size) % clock->size]; last = &clock->samples[(clock->head - 1 + clock->size) % clock->size]; /* First step, PTS to SOF conversion. */ @@ -772,6 +772,17 @@ void uvc_video_clock_update(struct uvc_streaming *stream, if (y2 < y1) y2 += 2048 << 16; + /* + * Have at least 1/4 of a second of timestamps before we + * try to do any calculation. Otherwise we do not have enough + * precision. This value was determined by running Android CTS + * on different devices. + * Dev_sof runs at 1KHz, and we have a fixed point precision of + * 16 bits. + */ + if ((y2 - y1) < ( (1000 / 4) << 16)) + goto done; + y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 - (u64)y2 * (u64)x1; y = div_u64(y, x2 - x1);