From patchwork Fri Jan 13 13:48:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 642367 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 9FC34C61DB3 for ; Fri, 13 Jan 2023 13:52:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233066AbjAMNwp (ORCPT ); Fri, 13 Jan 2023 08:52:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241778AbjAMNwT (ORCPT ); Fri, 13 Jan 2023 08:52:19 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2712625F2 for ; Fri, 13 Jan 2023 05:49:04 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id u9so52502828ejo.0 for ; Fri, 13 Jan 2023 05:49:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=i7Gw/Wt0kH+cB2xtvU6KPFDByh+yOvFcci+3eI5EXXQ=; b=mhHk1EqH9xVIqkhKsNdXdmh+OuuxL+KEI0D3zaPzlYzcIRSGiOdglM9YSA1sntKCDV TdcndgS11/KSuipZ9sM0JS35hiZwO3gTXYpyNET0v1E/paQcf6dUjJV93fnsiqPHCtgb AXIzQQKVb3P5RjADuj0UsumKUrSQeC3MXftN8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=i7Gw/Wt0kH+cB2xtvU6KPFDByh+yOvFcci+3eI5EXXQ=; b=JuXZQfOdhyso6aWvAtQvIO2L/RIBqylr4DNdaBsxU09SJl+wmaKU3RJC0epfHSP6G7 enSIFlVHVNzWzI0tfoWw58Oc2H3Pj5lhfBC8i7HBk1aIGPs1F6h7kOqkd71TmdGwwQzr Kj0yOHnSI9NYO2TWmprpvSD4qFTjTO/28oawS68boCVCVTxMGzwL4r/miVNyYSqOxD98 XEqguxfM4XO3IwQINCPUrFT5jit1rg26PSRNGo9HNrbTuPRuz18Zz4CdowYqJ5zLo/hd DB+Y5/l0g1xT7+Yrf56Hz/XkJc/Krn0W+cpzefz/3Rh2eng1rTeICal9L9mSkkYAeLOd 9Lgw== X-Gm-Message-State: AFqh2kqNwU5uG2/9uTx7b6pl8F89Ax1+CYHnTHwqIHTRA9hs4doKxzot kHjJy0wONCSq6OaMjK7tnd632Q== X-Google-Smtp-Source: AMrXdXv1qSKuQwl1dZhC4gndWE4r/T9aYIOPX5ZP0aW9IQHkUeNfgR91Xeojx4UdzY7Y2O4PfIhRog== X-Received: by 2002:a17:907:c208:b0:84c:e73c:d224 with SMTP id ti8-20020a170907c20800b0084ce73cd224mr30979496ejc.10.1673617743591; Fri, 13 Jan 2023 05:49:03 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:03 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:47 +0100 Subject: [PATCH v5 1/5] media: uvc: Ignore empty TS packets MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-1-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart 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=3557; i=ribalda@chromium.org; h=from:subject:message-id; bh=2f5YmRNpaCruZQpy69ufXYbW1rPh5r91J7dE4mEBR5A=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFFfFL5GkVzMO4ITDR9HY0Ml5keCj9kLRkwObQR aIM9lUuJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhRQAKCRDRN9E+zzrEiIrQD/ 9rs93+/3QWU+vZSQemr3EkZfXx0Gqyr0rMzn/AVumh4gCMbq9wILo/9I9oJ60p/uE8cst5TNrmkeB1 HF+8Sx3XzFRryL0enPuJprCuliI6UkW4gSI1KYWI36yUpQx9OgY1oo8915eEpukvsWGahHG+JUiWzT dKoMu4PsUpusBKTAMOOvszl+R7SmCe00+CSjoO75R2RVtFT8I3UgEjj0uXOvq4MHbLKmRdGlqXZkvG KCmgGRqJc8+5/oIbwKdbmIAtpsNZF/YDXkoZxVCUAj9w44cyESeyRMZ2KX1cxRsVOqZWV1Bd+VeUGy /tsocLM3YVH7sKfgGWLvuKe9y8LA5gSp5y74thXPl47sfhQtdr2Psb4JPEaej0Sa0HzxZm+jmu9D4d BzQYtYuM0LIDKV/1Idj9ApRdRXkyXR/PoBWxkMXtf0eOvow4HKFO0NLhoX/3kfX1S6qxe83saKZF2k g7Xge2kfkuRByU+ERzYUJqtqI4+2Ddvo309CrfEGbyBPwRNjnjLr2LONPxKeSkiwCI+TNfWV+I1PMk GPby9Awa6R0mMNduKKjDj+dmDYyDFWvVMEVkrhJy+MOR2t4yDmevCD1Pz2MhI3a594kfCgkuCRZR2p yOFf3HwGdJvKgbGPfhgGASWuiFzINWuSXedjFfuvCgcBYiL6lRQHTHjtnX2w== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Some SunplusIT cameras took a borderline interpretation of the UVC 1.5 standard, and fill the PTS and SCR fields with invalid data if the package does not contain data. "STC must be captured when the first video data of a video frame is put on the USB bus." Eg: buffer: 0xa7755c00 len 000012 header:0x8c stc 00000000 sof 0000 pts 00000000 buffer: 0xa7755c00 len 000012 header:0x8c stc 00000000 sof 0000 pts 00000000 buffer: 0xa7755c00 len 000668 header:0x8c stc 73779dba sof 070c pts 7376d37a This borderline/buggy interpretation has been implemented in a variety of devices, from directly SunplusIT and from other OEMs that rebrand SunplusIT products. So quirking based on VID:PID will be problematic. All the affected modules have the following extension unit: VideoControl Interface Descriptor: guidExtensionCode {82066163-7050-ab49-b8cc-b3855e8d221d} But the vendor plans to use that GUID in the future and fix the bug, this means that we should use heuristic to figure out the broken packets. This patch takes care of this. lsusb of one of the affected cameras: Bus 001 Device 003: ID 1bcf:2a01 Sunplus Innovation Technology Inc. Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.01 bDeviceClass 239 Miscellaneous Device bDeviceSubClass 2 ? bDeviceProtocol 1 Interface Association bMaxPacketSize0 64 idVendor 0x1bcf Sunplus Innovation Technology Inc. idProduct 0x2a01 bcdDevice 0.02 iManufacturer 1 SunplusIT Inc iProduct 2 HanChen Wise Camera iSerial 3 01.00.00 bNumConfigurations 1 Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index d4b023d4de7c..488c58bc9919 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -478,6 +478,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, ktime_t time; u16 host_sof; u16 dev_sof; + u32 dev_stc; switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -526,6 +527,18 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, if (dev_sof == stream->clock.last_sof) return; + dev_stc = get_unaligned_le32(&data[header_size - 6]); + + /* + * Some devices make a borderline interpretation of the UVC 1.5 standard + * and sends packets with no data contain undefined timestamps. Ignore + * such packages to avoid interfering with the clock interpolation + * algorithm. + */ + if (buf && buf->bytesused == 0 && len == header_size && + dev_stc == 0 && dev_sof == 0) + return; + stream->clock.last_sof = dev_sof; host_sof = usb_get_current_frame_number(stream->dev->udev); @@ -564,7 +577,7 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, spin_lock_irqsave(&stream->clock.lock, flags); sample = &stream->clock.samples[stream->clock.head]; - sample->dev_stc = get_unaligned_le32(&data[header_size - 6]); + sample->dev_stc = dev_stc; sample->dev_sof = dev_sof; sample->host_sof = host_sof; sample->host_time = time; From patchwork Fri Jan 13 13:48:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 642830 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 03689C67871 for ; Fri, 13 Jan 2023 13:52:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241800AbjAMNwr (ORCPT ); Fri, 13 Jan 2023 08:52:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241577AbjAMNwV (ORCPT ); Fri, 13 Jan 2023 08:52:21 -0500 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 123B363F51 for ; Fri, 13 Jan 2023 05:49:06 -0800 (PST) Received: by mail-ej1-x62a.google.com with SMTP id u9so52502943ejo.0 for ; Fri, 13 Jan 2023 05:49:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=qJgtpHz+rrTvZ2HTdfCXZ2CyubMLbgrhsqS57oo+fLs=; b=ND08wZXSDHaD26Y0TCZhButoS5O8WaoV7uieUsnmBfhZHWPpncmTUCs4PJn3K1qSeg zmUzvLwj9sNFB6KtMVa44DjavpJ2SZ+wLrZyXmBA/dkQDh3FXuqNIA1fi8HIhR1rDCaG dKc0juQpNzQk2G1W7Q8wPK3AVhFEv9G+IVROs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=qJgtpHz+rrTvZ2HTdfCXZ2CyubMLbgrhsqS57oo+fLs=; b=0qFpnTNiF33HaqHNEu00nL/cj7zARFHgLcu7dKJHz0XumvPAe4WksgMRu/QJ0VrJQA ntJvsU+wg2ciX4CqB/4V4fiG2QLrc9Pza1nKeNgSiTnkyi8Ly8u2zTrzgIeJCKt405cy XM+PJ866CNDptjaW8Q5QgCa3h7hJG9RaiYnOzSkI85VFauKoF4OUjnHahAj0RUZ2Q5nB wBACcn4kHhtsmpc4E9tKnHoaj1wITuFdgIU2Rkt7m20Uta4gixwEchXpuGwkuHVtExjX j6ehZ5YiUxfMTCowEjcBl4aJu2IbjrGTwXESzN31Y2uBMe2kC6NVdiPFkMb5QhzekTp2 zAog== X-Gm-Message-State: AFqh2kpsGj5QzXq/WsgQ9ZDO0ofQ2WHwNB0RC6W2D30aZtZ8gVf2Yqn1 cBBw0EXMjG9vheiRX7DOWXSajsoyK9WcaCJjwQA= X-Google-Smtp-Source: AMrXdXuvUT2nUagFQ3RxwsERWFTj/c8VzQ2I015/czkHLsi3sWDCgyyPZx+Rd/orzHlp2onPt//yyA== X-Received: by 2002:a17:907:8a24:b0:846:95f9:5b4d with SMTP id sc36-20020a1709078a2400b0084695f95b4dmr90060532ejc.42.1673617744658; Fri, 13 Jan 2023 05:49:04 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:04 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:48 +0100 Subject: [PATCH v5 2/5] media: uvcvideo: Quirk for invalid dev_sof in Logitech C922 MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-2-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart 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=5225; i=ribalda@chromium.org; h=from:subject:message-id; bh=hnq1PFU+jUes6McsuTh1jrRR6iZpqXb1FFM1WIqCsy0=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFHk2w4RhlH18GhP3/ZqGa/Lgl662roB7gJW0jX tE+jkN6JAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhRwAKCRDRN9E+zzrEiFuHD/ 0T+asPS2gJL5WRZcRw41N5tD5bz49be8iTGWEr5yjsM7e/dLMj2gA6jD6+PZE7rAlvHGcXUItOxY7D Z/aYo8edrIpPc+EqSGRuoUvQILn3xFn85iukIjts/l+76aPKp1M7BbxVNHYl7vsJTSHtctB5Rq6XtH 49Kdnk+HPfngySAvC8GouO6GbDsCD04uSN5fsMbh7xcM93NTqMgoCHqTJvdEPCMW6LirNs1MYmTAGM KANqrIB+8sqZi+5khKW4pmuGXxYRNnUQFgM3TqeJXxYVkY6G9UUhk1btaFvNw7/hcdznHoSuEwwf0j CNm/vsAmRQ2OTCUMHG0pZyA1S4BkvLXDGcYx35i93QZbVhrCgT27ZemDky82lZqU2SpoL4Zg/fMUhP vGTrw6sV+JDqzh9JfdETbo9XogvEu6hzlWQZ5CJnUFD9Wuk7xmSXs1wg7NoVx1RTUVq2DjPQGHQl1i ZSsMWfF9uCc6ymrEMo1WKL8ZiXYpFaSlzmYiTlU0cmdkLRVWreLOLnLr1UCPOQ7wLFSH35+Usql0vB SevSDJTpskAE42jfK8Gd9JLb/qjnWHTN3X7jiFyMazMu69B3ESB5ViZ8oIViK3xLGLlp0PXgfJ3N9O 6iRhtHlsD1mBez4uTqHeyddhTupgsUeWXE0TQp+o+fUbf4PjirUCjKx3ludg== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Logitech C922 internal SOF does not increases at a stable rate of 1kHz. This causes that the device_sof and the host_sof run at different rates, breaking the clock domain conversion algorithm. Eg: 30 (6) [-] none 30 614400 B 21.245557 21.395214 34.133 fps ts mono/SoE 31 (7) [-] none 31 614400 B 21.275327 21.427246 33.591 fps ts mono/SoE 32 (0) [-] none 32 614400 B 21.304739 21.459256 34.000 fps ts mono/SoE 33 (1) [-] none 33 614400 B 21.334324 21.495274 33.801 fps ts mono/SoE * 34 (2) [-] none 34 614400 B 21.529237 21.527297 5.130 fps ts mono/SoE * 35 (3) [-] none 35 614400 B 21.649416 21.559306 8.321 fps ts mono/SoE 36 (4) [-] none 36 614400 B 21.678789 21.595320 34.045 fps ts mono/SoE ... 99 (3) [-] none 99 614400 B 23.542226 23.696352 33.541 fps ts mono/SoE 100 (4) [-] none 100 614400 B 23.571578 23.728404 34.069 fps ts mono/SoE 101 (5) [-] none 101 614400 B 23.601425 23.760420 33.504 fps ts mono/SoE * 102 (6) [-] none 102 614400 B 23.798324 23.796428 5.079 fps ts mono/SoE * 103 (7) [-] none 103 614400 B 23.916271 23.828450 8.478 fps ts mono/SoE 104 (0) [-] none 104 614400 B 23.945720 23.860479 33.957 fps ts mono/SoE Instead of disabling completely the hardware timestamping for such hardware we take the assumption that the packet handling jitter is under 2ms and use the host_sof as dev_sof. We can think of the UVC hardware clock as a system with a coarse clock (the SOF) and a fine clock (the PTS). The coarse clock can be replaced with a clock on the same frequency, if the jitter of such clock is smaller than its sampling rate. That way we can save some of the precision of the fine clock. To probe this point we have run three experiments on the Logitech C922. On that experiment we run the camera at 33fps and we analyse the difference in msec between a frame and its predecessor. If we display the histogram of that value, a thinner histogram will mean a better meassurement. The results for: - original hw timestamp: https://ibb.co/D1HJJ4x - pure software timestamp: https://ibb.co/QC9MgVK - modified hw timestamp: https://ibb.co/8s9dBdk This bug in the camera firmware has been confirmed by the vendor. lsusb -v Bus 001 Device 044: ID 046d:085c Logitech, Inc. C922 Pro Stream Webcam Device Descriptor: bLength 18 bDescriptorType 1 bcdUSB 2.00 bDeviceClass 239 Miscellaneous Device bDeviceSubClass 2 bDeviceProtocol 1 Interface Association bMaxPacketSize0 64 idVendor 0x046d Logitech, Inc. idProduct 0x085c C922 Pro Stream Webcam bcdDevice 0.16 iManufacturer 0 iProduct 2 C922 Pro Stream Webcam iSerial 1 80B912DF bNumConfigurations 1 Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_driver.c | 9 +++++++++ drivers/media/usb/uvc/uvc_video.c | 8 +++++++- drivers/media/usb/uvc/uvcvideo.h | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index c1e3c1fab976..eae88d0f420e 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -2569,6 +2569,15 @@ static const struct usb_device_id uvc_ids[] = { .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) }, + /* Logitech HD Pro Webcam C922 */ + { .match_flags = USB_DEVICE_ID_MATCH_DEVICE + | USB_DEVICE_ID_MATCH_INT_INFO, + .idVendor = 0x046d, + .idProduct = 0x085c, + .bInterfaceClass = USB_CLASS_VIDEO, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 0, + .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_INVALID_DEVICE_SOF) }, /* Chicony CNF7129 (Asus EEE 100HE) */ { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO, diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 488c58bc9919..f4470f6df45c 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -541,7 +541,13 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, stream->clock.last_sof = dev_sof; - host_sof = usb_get_current_frame_number(stream->dev->udev); + /* + * On some devices, like the Logitech C922, the device SOF does not run + * at a stable rate of 1kHz. For those devices use the host SOF instead. + */ + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) + dev_sof = usb_get_current_frame_number(stream->dev->udev); + time = uvc_video_get_time(); /* diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 22656755a801..699c33bc24ed 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -73,6 +73,7 @@ #define UVC_QUIRK_FORCE_Y8 0x00000800 #define UVC_QUIRK_FORCE_BPP 0x00001000 #define UVC_QUIRK_WAKE_AUTOSUSPEND 0x00002000 +#define UVC_QUIRK_INVALID_DEVICE_SOF 0x00004000 /* Format flags */ #define UVC_FMT_FLAG_COMPRESSED 0x00000001 From patchwork Fri Jan 13 13:48:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 642366 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 A901CC678D4 for ; Fri, 13 Jan 2023 13:52:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241859AbjAMNws (ORCPT ); Fri, 13 Jan 2023 08:52:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241482AbjAMNwU (ORCPT ); Fri, 13 Jan 2023 08:52:20 -0500 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 156F668C84 for ; Fri, 13 Jan 2023 05:49:07 -0800 (PST) Received: by mail-ej1-x62f.google.com with SMTP id vm8so52445668ejc.2 for ; Fri, 13 Jan 2023 05:49:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=BIYSq+SG4WtbilzNOWYagt4DvEBoOwZmBVCbkxWrmdg=; b=Psf7tBaJ4kUzf0xvoPiWTIUp/xnmYxZevQ6ZlF2RYXkljmHGkAyZ+3rU/Ykwng7MeL GFT4jRKvJT/LUY5LDmcTukJ5b68E0ywQG+6EQDR6J4Rw9zbm2ROUc7by6meEkx+aOmTE /q17Fpsz/BPRntTDr6/imThxAf80cbNjjx+hs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=BIYSq+SG4WtbilzNOWYagt4DvEBoOwZmBVCbkxWrmdg=; b=LOPpLU1Xo3KKgX/anzBH8eMbXCkpRdFuFHcGSTxPaCsNjlDvrI9X2ffkyVnllkBKvi JmLGmmolBscTabxktBiHmKcTLeyvG6hqeaf7eiNTo0UQArXjNWFwFd8tLYkk7UO3Qf4O IcwCzzcUdsHU9FcIdUcWivTFiRz44wUg6yzhrt6OnhbbzvGjRcqxUNVSghSZUulahpQs 9PdqpbHkWrGgARR40fz5BFG4DZuG0clFWIIFx8UtdTf4+Eu3DZAoeHLKmfPnqvkakLX8 tmjZYypxqx/NG+vXUbD5bJoBnnJw10OviDK6scGVmakHv+NKkxF9S58N1koqwZK/PshU iiLA== X-Gm-Message-State: AFqh2krFY5FKdY6rqk+KRNJ25O9cKoPN80vZ1E7EAljZa0DNapZsaUpE pD4xVBgwesswdzsuN+reMFzcSg== X-Google-Smtp-Source: AMrXdXvVSF0aOYTYEszDFBVHm+AelHcJuzDS6lm5uyEPGb3LPwSyG+qOfiy2Gr75vPbH5JS4td16ZQ== X-Received: by 2002:a17:907:8b1a:b0:84d:34a4:46fd with SMTP id sz26-20020a1709078b1a00b0084d34a446fdmr3495226ejc.71.1673617745694; Fri, 13 Jan 2023 05:49:05 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:05 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:49 +0100 Subject: [PATCH v5 3/5] media: uvcvideo: Allow hw clock updates with buffers not full MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-3-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart 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=1843; i=ribalda@chromium.org; h=from:subject:message-id; bh=ze5myykY7IWxFl+shu/B7fsc0sNffBzVSl+RqFnttm0=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFJsRwW22gkHsLnSkRocTuSoJA32f8kqplaGddG N4YwKziJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhSQAKCRDRN9E+zzrEiN9VD/ 0XTWp1wD61yuxVx7AJoIHnpjTRZO9X/JAVF2G2p0qj14/oeLVkL9YnLryvrkdYmxnPAIzJUtkroCiN X/eegUJgccziHt2GXqQVm7oBEcOehw2R5WKkoUT5WY7cXOv5rqfedIya76MxqGcpAQfa4KVkX4eBQm QqUvRqBKcBqKryviL9KXUA1zKfRx6PaMMIV/5/OXU2pztfzXjlFIKlntYu08rLeKVR/8upJlEdN0+1 XrfjPHP/PKM2qCY5bTV2siH7/y/QJJ68VFxFLkylkRWjIwgIZP3k0BAYiiTAStlQAz5Lqu7ia/Xg6p g+tc0xduyHVY1fOTWTf2TYdeaaPiBVFdP0R1m+8ubQ+23SNo56/0TEqywOgDkrgT6cIoZWE4XKyI/Y 0cEkwuMnBbL7rGmTi4HMFsNKapWZrtBZW2YRe9MhUNQEtl0UFt3SpQiCimuu1ECSX3N3hQkofgop8B DqOKbGlbhdoyeNgk/HSx1AX6/4Zn7NHUC4YYoFYw235Xr5NCItFs7aUESZl5U3r8nMPsubCpsWHjCV rZRpLurXrSJISxwTCX7OH32f45YY4ImJQWhKJlAQEBsBugv7ClLGfvi9nQybHr+VNTMZmJVUK5w4HO 1MZIksNsbbQ7l+iYvFa4z2exrsAbDlPWclFqxV0GCwCZI9ysjawGZrsZ3k9w== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index f4470f6df45c..21985598ff5b 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -747,10 +747,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]; last = &clock->samples[(clock->head - 1) % clock->size]; /* First step, PTS to SOF conversion. */ @@ -765,6 +765,15 @@ 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. + */ + if ((y2 - y1) < (256 << 16)) + goto done; + y = (u64)(y2 - y1) * (1ULL << 31) + (u64)y1 * (u64)x2 - (u64)y2 * (u64)x1; y = div_u64(y, x2 - x1); From patchwork Fri Jan 13 13:48:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 642829 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 BC172C61DB3 for ; Fri, 13 Jan 2023 13:52:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241882AbjAMNwv (ORCPT ); Fri, 13 Jan 2023 08:52:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241780AbjAMNwW (ORCPT ); Fri, 13 Jan 2023 08:52:22 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5644B69B0E for ; Fri, 13 Jan 2023 05:49:08 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id u19so52398522ejm.8 for ; Fri, 13 Jan 2023 05:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=2JdQ79/DG3w/NnYpFjhrKQiF9vf+P0kD+HDc/SwBkDU=; b=SPSWEpVlZTdei5NmTNUgjxxFp8CYsjWO5wHQilWmjHPTTHVb4NWA73Yd/jt8L6iGNg 4OFqL9ivs7qwbnHHenui8Ue13qsjG7/8c2ZczkdyZOUlVYljfqhFuetUCbJg4GEpYyD7 MzD350qHCe99aghjFjFso9blEJvSuuma/OgO4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=2JdQ79/DG3w/NnYpFjhrKQiF9vf+P0kD+HDc/SwBkDU=; b=2UZyd7Yf21Vjf5NzOK4g6WarFeO2/C2aOwjo71mxT5dwxDXgZA5ak4lVFhPSak6kbT 9GhM0z5HT9jTHZ1iuKH1GE+r62bWCRPB/9z8zYWhj1FUJ5JDqHsvE1eMskY+7dvZs8yB yKSOMjcl6o1hHl9whT2vQYh8z+cBXJruwYZb06QLi/XCjZ9B5eQ4WCXsV2zC5CAncvdL 1/equgv/ir+nqboaCDHpwaUTcUHDHlrBwpEMvLkb+1DxH/1sOSi2xbtoc5bjqsgaRTXN Lf/PlP7bk/q9Px1Q9WQyVMFVT6jtJiubH1zfDCyPDwoq8j9gnyOMcey09CO1V2JGBlLb 55zA== X-Gm-Message-State: AFqh2kqZn7So4cHJ3XNWVyI95IQ7WbfPJmvB4ZYsRoygT9uotQ2fnZZ8 xKNtw3CNaS9nYMvnCpT2W2peAA== X-Google-Smtp-Source: AMrXdXsRa7oXYSt5dEKlb3mHA3ByJcdxO7ZlcTVT0IrUuh7tZkMPORQi+v7lArSWLosoi1eRAoQ9Ig== X-Received: by 2002:a17:907:a485:b0:7c1:709d:fa49 with SMTP id vp5-20020a170907a48500b007c1709dfa49mr87888466ejc.18.1673617746803; Fri, 13 Jan 2023 05:49:06 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:06 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:50 +0100 Subject: [PATCH v5 4/5] media: uvcvideo: Refactor clock circular buffer MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-4-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart 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=6614; i=ribalda@chromium.org; h=from:subject:message-id; bh=r2Bi8jzAlegYJ34KV7vhsUT4PU3/z0Uyq8nK4oWIpCU=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFKV4PZvGmcZmD3pUGhHBoq/4lKBwYpLBlCb1Og KVnqGqCJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhSgAKCRDRN9E+zzrEiLPLD/ sHCLfMibRlpo7NDo/NG17fdXhmRU63hrmgnPJ3Z1cbh6sfQyLIMRXi3A5vxJJ8gHRtcma3Vo95HR5H 8MGyiIfblutzocaLdP/PjlraBskXJ4oudncy9SdzJ0dnVKmb3y9Pij+X2onf0q/WPT6oHoDpJiIe2F UZuvqBxRiMMdMhaphG4OQdFN5zEz5uWVX+geqxkZs+Mw9gei0ZlgKSQJdeTJSjdWRgQnQB0IatGP/g fE0T/JBIg6lMxUhNphIdXeCMXdvdLgdjYHvtdmijw/iKYJRMcoD+h6f2tKN8ZxnQyC1CTBmxKfxqc0 9CKpdMwi3TX4nnOi1hqgn3FVxl7nAnhMwYUj6ingAGt08V/9v2sshrOkRz1gqwhyA/kH9salQ84U5M xD0ZbymF1jKzxKsqQX+OtrbmmJi+9CxGVSlm29lBCFsBpeuiqn+ndlYhJ103e3hzlb2fZWEkDbl9Hf VQDlWgZNs+WMuyg80SJJCAttMxn2UPZUfDQBjLpmmTv46l5lkUljBR5wTe4HHXy8b4L1mJdThdYW+/ bs2MSMmVOSgrm10aQcvjQm98BiXT5L6eXuVhtjFde9ieLlhLbxjjP/92Gl8jsdbXqajKYtFe8lw25C vWGazIMErHMVn7MEBJcPU6SsFaW60mGJrH9GLCQkVAPiWUllr7+Aav3THNkQ== X-Developer-Key: i=ribalda@chromium.org; a=openpgp; fpr=9EC3BB66E2FC129A6F90B39556A0D81F9F782DA9 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Isolate all the changes related to the clock circular buffer to its own function, that way we can make changes easier to the buffer logic. Also simplify the lock, by removing the circular buffer clock handling from uvc_video_clock_decode(). And now that we are at it, unify the API of the clock functions. Tested-by: HungNien Chen Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/uvc_video.c | 84 ++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c index 21985598ff5b..22255bfa7db9 100644 --- a/drivers/media/usb/uvc/uvc_video.c +++ b/drivers/media/usb/uvc/uvc_video.c @@ -466,19 +466,28 @@ static inline ktime_t uvc_video_get_time(void) return ktime_get_real(); } +static void uvc_video_clock_add_sample(struct uvc_clock *clock, + const struct uvc_clock_sample *sample) +{ + unsigned long flags; + + spin_lock_irqsave(&clock->lock, flags); + + memcpy(&clock->samples[clock->head], sample, sizeof(*sample)); + clock->head = (clock->head + 1) % clock->size; + clock->count = min(clock->count + 1, clock->size); + + spin_unlock_irqrestore(&clock->lock, flags); +} + static void uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, const u8 *data, int len) { - struct uvc_clock_sample *sample; + struct uvc_clock_sample sample; unsigned int header_size; bool has_pts = false; bool has_scr = false; - unsigned long flags; - ktime_t time; - u16 host_sof; - u16 dev_sof; - u32 dev_stc; switch (data[1] & (UVC_STREAM_PTS | UVC_STREAM_SCR)) { case UVC_STREAM_PTS | UVC_STREAM_SCR: @@ -523,11 +532,11 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * all the data packets of the same frame contains the same SOF. In that * case only the first one will match the host_sof. */ - dev_sof = get_unaligned_le16(&data[header_size - 2]); - if (dev_sof == stream->clock.last_sof) + sample.dev_sof = get_unaligned_le16(&data[header_size - 2]); + if (sample.dev_sof == stream->clock.last_sof) return; - dev_stc = get_unaligned_le32(&data[header_size - 6]); + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); /* * Some devices make a borderline interpretation of the UVC 1.5 standard @@ -536,19 +545,21 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * algorithm. */ if (buf && buf->bytesused == 0 && len == header_size && - dev_stc == 0 && dev_sof == 0) + sample.dev_stc == 0 && sample.dev_sof == 0) return; - stream->clock.last_sof = dev_sof; + stream->clock.last_sof = sample.dev_sof; /* * On some devices, like the Logitech C922, the device SOF does not run * at a stable rate of 1kHz. For those devices use the host SOF instead. */ - if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) - dev_sof = usb_get_current_frame_number(stream->dev->udev); + if (stream->dev->quirks & UVC_QUIRK_INVALID_DEVICE_SOF) { + sample.dev_sof = + usb_get_current_frame_number(stream->dev->udev); + } - time = uvc_video_get_time(); + sample.host_time = uvc_video_get_time(); /* * The UVC specification allows device implementations that can't obtain @@ -571,46 +582,29 @@ uvc_video_clock_decode(struct uvc_streaming *stream, struct uvc_buffer *buf, * the 8 LSBs of the delta are kept. */ if (stream->clock.sof_offset == (u16)-1) { - u16 delta_sof = (host_sof - dev_sof) & 255; + u16 delta_sof = (sample.host_sof - sample.dev_sof) & 255; if (delta_sof >= 10) stream->clock.sof_offset = delta_sof; else stream->clock.sof_offset = 0; } - dev_sof = (dev_sof + stream->clock.sof_offset) & 2047; - - spin_lock_irqsave(&stream->clock.lock, flags); - - sample = &stream->clock.samples[stream->clock.head]; - sample->dev_stc = dev_stc; - sample->dev_sof = dev_sof; - sample->host_sof = host_sof; - sample->host_time = time; - - /* Update the sliding window head and count. */ - stream->clock.head = (stream->clock.head + 1) % stream->clock.size; + sample.dev_sof = (sample.dev_sof + stream->clock.sof_offset) & 2047; + sample.dev_stc = get_unaligned_le32(&data[header_size - 6]); - if (stream->clock.count < stream->clock.size) - stream->clock.count++; - - spin_unlock_irqrestore(&stream->clock.lock, flags); + uvc_video_clock_add_sample(&stream->clock, &sample); } -static void uvc_video_clock_reset(struct uvc_streaming *stream) +static void uvc_video_clock_reset(struct uvc_clock *clock) { - struct uvc_clock *clock = &stream->clock; - clock->head = 0; clock->count = 0; clock->last_sof = -1; clock->sof_offset = -1; } -static int uvc_video_clock_init(struct uvc_streaming *stream) +static int uvc_video_clock_init(struct uvc_clock *clock) { - struct uvc_clock *clock = &stream->clock; - spin_lock_init(&clock->lock); clock->size = 32; @@ -619,15 +613,15 @@ static int uvc_video_clock_init(struct uvc_streaming *stream) if (clock->samples == NULL) return -ENOMEM; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(clock); return 0; } -static void uvc_video_clock_cleanup(struct uvc_streaming *stream) +static void uvc_video_clock_cleanup(struct uvc_clock *clock) { - kfree(stream->clock.samples); - stream->clock.samples = NULL; + kfree(clock->samples); + clock->samples = NULL; } /* @@ -2099,7 +2093,7 @@ int uvc_video_resume(struct uvc_streaming *stream, int reset) stream->frozen = 0; - uvc_video_clock_reset(stream); + uvc_video_clock_reset(&stream->clock); if (!uvc_queue_streaming(&stream->queue)) return 0; @@ -2248,7 +2242,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) { int ret; - ret = uvc_video_clock_init(stream); + ret = uvc_video_clock_init(&stream->clock); if (ret < 0) return ret; @@ -2266,7 +2260,7 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) error_video: usb_set_interface(stream->dev->udev, stream->intfnum, 0); error_commit: - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); return ret; } @@ -2294,5 +2288,5 @@ void uvc_video_stop_streaming(struct uvc_streaming *stream) usb_clear_halt(stream->dev->udev, pipe); } - uvc_video_clock_cleanup(stream); + uvc_video_clock_cleanup(&stream->clock); } From patchwork Fri Jan 13 13:48:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda X-Patchwork-Id: 642365 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 575D4C54EBE for ; Fri, 13 Jan 2023 13:52:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241566AbjAMNwx (ORCPT ); Fri, 13 Jan 2023 08:52:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233155AbjAMNwX (ORCPT ); Fri, 13 Jan 2023 08:52:23 -0500 Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 042BC687AE for ; Fri, 13 Jan 2023 05:49:09 -0800 (PST) Received: by mail-ej1-x62e.google.com with SMTP id l22so22742856eja.12 for ; Fri, 13 Jan 2023 05:49:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=dDfjDeTst6OQxRVhR2qypSB7k7XxFsYPOvv4qs9VTxY=; b=knAfm9vHU6HiEfv+gWrWbwIGU0C9RXL7SSqRpLkf//zZYBgXkmztEsQYrNOvz6nSuU Mnc9MPPtOhSg+BV/ZLQP05GW1CQRoEIjJYyUZUVyyyT9qaajgokS1qEcINf3xr6eOjoB ekkr2py3iWWrMxt39pNGi55cyzez2HzjOUQXE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=dDfjDeTst6OQxRVhR2qypSB7k7XxFsYPOvv4qs9VTxY=; b=CR9Vt9schT1EhaHGVQQwRLNNOA70M/fseWuIbpAV3KWQe3S4dWejuoZ+JfillPUWWk EqF1o346wbfOfhmkRqHGmb02Ii7/ggV9MF01nQNpLZbB5U23oi3kmQthkBPZZf/AOHzE 7/Irx8SiN9GLkuDYDvjOv8+ZaPE1iHxfviScjbMdetcUKvr+Tqaf1RtDS33+leM80+xu FXRjUIY0y3llvPq/cskGiU/Q8K4sDONrp69CH9iliDAxHPF38rrbHXlpOBiqHqfrLTxR l6geLrLvaez3RIzGTNPTmTn8x6zgCM4gtm9LEcTkrBbHaLLCD3d/sjRDXoBsMn2zBYkO 4pMg== X-Gm-Message-State: AFqh2koNQjExonUha9is+BdSvI6acX6UjUaRjSFIrDzwzE3aAakSdCRv IaMgGQIpLRG/2LzXnkVboacvag== X-Google-Smtp-Source: AMrXdXvBPvBk9VIhr4SxUH4aKpo9zMpYGavW7m8bpOGxZyp5UOcKx++aEXxOYY14/dZka+TAryI32g== X-Received: by 2002:a17:907:d312:b0:84d:3543:fa7b with SMTP id vg18-20020a170907d31200b0084d3543fa7bmr22367374ejc.54.1673617747573; Fri, 13 Jan 2023 05:49:07 -0800 (PST) Received: from alco.roam.corp.google.com ([2620:0:1059:10:b023:4272:bddc:acf2]) by smtp.gmail.com with ESMTPSA id w23-20020a1709062f9700b0084d3bf44996sm7072888eji.142.2023.01.13.05.49.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 05:49:07 -0800 (PST) From: Ricardo Ribalda Date: Fri, 13 Jan 2023 14:48:51 +0100 Subject: [PATCH v5 5/5] media: uvcvideo: Fix hw timestamp handling for slow FPS MIME-Version: 1.0 Message-Id: <20220920-resend-hwtimestamp-v5-5-660679c6e148@chromium.org> References: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> In-Reply-To: <20220920-resend-hwtimestamp-v5-0-660679c6e148@chromium.org> To: Mauro Carvalho Chehab , Laurent Pinchart 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=2429; i=ribalda@chromium.org; h=from:subject:message-id; bh=78AsqBPCn9SSTg96NmP9pgJexa1mmA+uxtg7QUEhcrg=; b=owEBbQKS/ZANAwAKAdE30T7POsSIAcsmYgBjwWFLQMt/hm9dgwBbvtkC6/YYapOI4tQlTEdEbj/n BT8kx8uJAjMEAAEKAB0WIQREDzjr+/4oCDLSsx7RN9E+zzrEiAUCY8FhSwAKCRDRN9E+zzrEiKteD/ 4pR6X/2GxhnuA2bKPD6ZrG9DiHOPaonuT/jZsm6Wj7UCTOJsQ41RCFqfLkdl3DQ4xuERRvjkHcTznj 0CcJ8p6yhO6GGa4NctZGXAj3MBSsRUVkeeHhCd5VCGaYfBcuhLbHJIljtgValjqnoh7IuTqxgJPWKw j172qlY7ZysYOcQGX5Pj/WZMyHA3ZBQTVx3voDEmG0StmdlbCq8d0D/C829plELKJ7DFlsqNapEFQC uKauAa/6tgSpd9QEyeqTJ+eGVGO4YD1CAikeQlTOVSujLFUUeMkOFQwlGyi+m8ROcHm2E9bUMn6TOy xhgIO0F14fClachp79yCkO+uRvhNq51OlcLDZ5uOtlY4CaSN9NfFnI3FSjqJRcDko//WRW4Sru9hBP jwKONpryM3GhC4VUbW5r5gnwB4OPSEZ2sHKxL5i7Fkzoapzi5R7SjA8dyrMBL6u14apAA7I12IAA9J NJzOc781CxMGOZh2gP0WwFZs6Yb7BKY0nbS2BYCPYyFzYy0i8uf26GCB7SmXu4WKhcYEZ9zpHm5yqT QeO1vfzwk4WTbBTs6IpW4kswrKJ2Wc4V/m76Mlh/LH/tccCnBgOY+LL+hMphKwep4Zf1ZUV9debsUB vy0hwGe5pDX/ZNdJRE6Yt0F1pBASpnMVcyur7+vWzz6nvB/G4coLVIaSbqwQ== 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;