From patchwork Wed Mar 8 22:45:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 661363 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE3F5C678D5 for ; Wed, 8 Mar 2023 22:46:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230006AbjCHWqB (ORCPT ); Wed, 8 Mar 2023 17:46:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229943AbjCHWpv (ORCPT ); Wed, 8 Mar 2023 17:45:51 -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 EA0B0D08D0 for ; Wed, 8 Mar 2023 14:45:48 -0800 (PST) Received: by mail-ed1-x52b.google.com with SMTP id s11so71915955edy.8 for ; Wed, 08 Mar 2023 14:45:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678315547; 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=5nAAE1hEWYCr5Pmk00qC9NxfHeEjmY265EAWnttnUqA=; b=DrRi816pfrsFJZsHdqlKl9NY/Ojqj53zXsA6juTnL7As94dK+3MfqUGe6pNg5IcoAM QXjmOU3i4zOGv+E6dUaJ2Pd3o3UL1d1Ci2CLhso8Lfh+RHEzjVGnh8VSWO1oXadNysXW jn8JG32Fc/63Zm7VB+0xDTPmCHCy0jxMgQjDg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678315547; 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=5nAAE1hEWYCr5Pmk00qC9NxfHeEjmY265EAWnttnUqA=; b=sn4G+cpJiuA5yOmS93c/SSce/ztMthMwScx7wOKfpl0b9fU4QE9z9TQm/3bq5Mw660 kAfPc1lLeHWzaXm2cXF6DNrmYr+Gb0BJJTDmvTRdrfHf8nFk6jUvS3FXF+ttGKT2ZB7p NL30t0xoNljGi1OhCVJmxsZcnStWFI13PMY51k1QmWmnzqs6XYFH5/s6YTvlPBD5BvEA 43jQq7iUrXdzxVmvgmqkrifCo1JRqVXFi5OWllGCSVTN860XOlJlaCB8yO1yaASPjK+f AIfIkALBtoHaismeMw2YEh7qH8jgBK0jcuFiwIHCHjQIZFYsbqGRGtSQDgd3VEoM5Uiv se1Q== X-Gm-Message-State: AO0yUKXQEyAMBtdiCRxP7IIS2IHvW9/pS2UiHhfbcrdQNyYWXrlcT8Jj B2sCj3VjOPr3QwjqQhsYBhsFyQ== X-Google-Smtp-Source: AK7set9obC+TPt3MhU2dsJ75Dc72DIClOFTLEl3termnv2GIo96iQ+aDeb6mOTTHQX/76o5Wcw5RZA== X-Received: by 2002:a17:906:6c84:b0:8aa:33c4:87d5 with SMTP id s4-20020a1709066c8400b008aa33c487d5mr18188780ejr.10.1678315547309; Wed, 08 Mar 2023 14:45:47 -0800 (PST) Received: from alco.roam.corp.google.com (80.71.134.83.ipv4.parknet.dk. [80.71.134.83]) by smtp.gmail.com with ESMTPSA id ot18-20020a170906ccd200b008e40853a712sm8040167ejb.97.2023.03.08.14.45.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Mar 2023 14:45:47 -0800 (PST) From: Ricardo Ribalda Date: Wed, 08 Mar 2023 23:45:36 +0100 Subject: [PATCH RESEND v5 5/5] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-5-139d1a144614@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-139d1a144614@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-139d1a144614@chromium.org> To: Laurent Pinchart , Mauro Carvalho Chehab Cc: "hn.chen" , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, Ricardo Ribalda X-Mailer: b4 0.11.0-dev-696ae X-Developer-Signature: v=1; a=openpgp-sha256; l=2432; i=ribalda@chromium.org; h=from:subject:message-id; bh=8oy5jraGEK21FgogoPTHRpnEBLvJaCndlyuKgZhZ4ZM=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBkCRAWwM+vnZz8D7hi2QHfLJNIXdM2uj8xqqrGEuM8 GWfHQ0WJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCZAkQFgAKCRDRN9E+zzrEiGVnD/ 0ZZz+qXtOEMfCgF5HlAwc1Q+RLZCxf4ZO1YCYRL8HAtJRfGzegNQnua+/fEJDCLoroLU/vgRHINUw+ ev/eB7oK2cab4EFihmX1Lwu1F5VbMpMmgkGK1FjScREm2180fNAzYdfM6gh4c2B/iz5cKKWkfgljwP GnlZdnQ2mhu8VJj3URRAwNBtG6qDujoqq6e0Gf116eJPThgv5MK6KwH95gIO89xz35Lhy6kckvJF1K 4QxFFq21iP4+zogTXqKxwJOsfbPSLy7smLy/gv8uC2+AkK9Z7nC0XZJKpeCOP8Bqp7v6EMgYmCRpXV lnJ/t6z2r5jTazn3SHFqq+gtQQBNxZqVkFGTdTY4JqV7nxlEzxGFimwJmK315PO4mjYgyNB33myuM4 sGaOBYl8MyjzD8yDupsokZhCP5d4g64MO4Xe6zs3HlfEl7FJcniTvsdMtNq9omiRnvw4gkgw8EzzE4 kcMqamAOB8gcP5xuaimAKMFppqQknbymb+jFW2wPDzOd48qA8+jA3vw4PkRpQF1fWejvoX7aDV6Yop EmBYeitH54ZLLtZmAGzhVomyHEnr/uWFqGgM4ETNNX51/4tr6RLjmMRbtoQOjc3EfoGvD5vRvHdpHJ t2OMLBjrJuj2oIgK4b14TYNJ4LT7CRlOhgQCaJ/runUpCIEFrb9sSFn0Y6GQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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. Revome 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 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 15 +++++++++++++++ drivers/media/usb/uvc/uvcvideo.h | 1 + 2 files changed, 16 insertions(+) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 22255bfa7db9..ae692122d5dc 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -473,6 +473,20 @@ static void uvc_video_clock_add_sample(struct uvc_clock *clock, spin_lock_irqsave(&clock->lock, flags); + /* Delete last overflows */ + if (clock->head == clock->last_sof_overflow) + clock->last_sof_overflow = -1; + + /* Handle overflows */ + if (clock->count > 0 && clock->last_sof > sample->dev_sof) { + /* Remove data from the last^2 overflows */ + if (clock->last_sof_overflow != -1) + clock->count = (clock->head - clock->last_sof_overflow) + % clock->count; + clock->last_sof_overflow = clock->head; + } + + /* Add sample */ memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); clock->head = (clock->head + 1) % clock->size; clock->count = min(clock->count + 1, clock->size); @@ -600,6 +614,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 699c33bc24ed..f2a7e1507492 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -501,6 +501,7 @@ struct uvc_streaming { unsigned int head; unsigned int count; unsigned int size; + unsigned int last_sof_overflow; u16 last_sof; u16 sof_offset;