From patchwork Mon Sep 17 16:03:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 10602987 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A7F515E8 for ; Mon, 17 Sep 2018 16:03:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0AC3920134 for ; Mon, 17 Sep 2018 16:03:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F34F228F89; Mon, 17 Sep 2018 16:03:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 937BA20134 for ; Mon, 17 Sep 2018 16:03:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728323AbeIQVbc (ORCPT ); Mon, 17 Sep 2018 17:31:32 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:39411 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726979AbeIQVbb (ORCPT ); Mon, 17 Sep 2018 17:31:31 -0400 Received: by mail-pg1-f194.google.com with SMTP id i190-v6so7884068pgc.6 for ; Mon, 17 Sep 2018 09:03:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kCcb60wn8QagEZx2g18V1rCatI1b1/sLJ76nxpm49vE=; b=nXid3L9O9EZDZG+niMbWPIPgD2bhwbYRboGUuZK+viaqs60ewgOM7lcwYXJFXWwCN2 B1/6b6L7X6wg2CLgTkbpzbh/Kd8LXw21aXnnci0soqk8+agbZWpSXGwzO2RsBZlYR0GR mVThhWHcBD3NVh/qq1j224WtqOsUcFki4bWANiKipXK3KXN5f0VdrtbRmzWndICQc71Q bw06W9XSX49yt6I2Op6Pn+i32vlmYLzk3hcQUnDV4n0nlgtOzg5TJ4U7ZzLJH2vjujUl d3uF+xdiR1vQJrRlmOIG0XfjJtvyFUSfuwswN2SZeqRDC3NBDn9EedJhBAWIl1azO4KK DNtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kCcb60wn8QagEZx2g18V1rCatI1b1/sLJ76nxpm49vE=; b=byvZh5e6ZXLIVEEA+E6H+OpS9fKqbbbVK1/NzUPScGS2Wxq2ZmJ7md8BjEZF7hEPef B8b5clDZv3Qena6/1+oEUiOB1bXKATSmrJQQ78T4+8HKPxExM4pCybbYEPRao1HZdOLB 99dPhUw/tT2bhz/Uaog+mAKtOWIEInIs5Ke5LJB+c07Q99SGdMa6fjNn0e4k0R4kv2Uk giDrmGDBNjqjqPpEUYF2+yLWqSx90Gr5pphLF0MxiL3P279q3l6TBuhufYQ4dMeYRVe0 pkouXmtfIcps8l7HGsQjEPpzpFkRdVI9gTCWIsMTZY/FXW7GeGcyshTkKjRa6Eh9OgVx IlLA== X-Gm-Message-State: APzg51D51yXXzHaiVfZgmLovyRflynfa4AbO8vWO65uOr27cwdSDi2Cc naxtawEUL2fRgXbQtWUmPFatZ99H X-Google-Smtp-Source: ANB0VdbBrzw1/C2toTKkyRTvCsn00Cu7kc7SELJQifb6gVcsrx10g7PyE2ECDVc6jqotcYiNRdOTxQ== X-Received: by 2002:a63:3741:: with SMTP id g1-v6mr24240266pgn.59.1537200212235; Mon, 17 Sep 2018 09:03:32 -0700 (PDT) Received: from mita-MS-7A45.lan ([240f:34:212d:1:f447:4113:b862:c2f5]) by smtp.gmail.com with ESMTPSA id p3-v6sm20088607pfo.130.2018.09.17.09.03.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Sep 2018 09:03:31 -0700 (PDT) From: Akinobu Mita To: linux-media@vger.kernel.org Cc: Akinobu Mita , Matt Ranostay , Sakari Ailus , Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH 3/5] media: v4l2-common: add v4l2_find_closest_fract() Date: Tue, 18 Sep 2018 01:03:09 +0900 Message-Id: <1537200191-17956-4-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1537200191-17956-1-git-send-email-akinobu.mita@gmail.com> References: <1537200191-17956-1-git-send-email-akinobu.mita@gmail.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a function to locate the closest element in a sorted v4l2_fract array. The implementation is based on find_closest() macro in linux/util_macros.h and the way to compare two v4l2_fract in vivid_vid_cap_s_parm in drivers/media/platform/vivid/vivid-vid-cap.c. Cc: Matt Ranostay Cc: Sakari Ailus Cc: Hans Verkuil Cc: Mauro Carvalho Chehab Signed-off-by: Akinobu Mita --- drivers/media/v4l2-core/v4l2-common.c | 26 ++++++++++++++++++++++++++ include/media/v4l2-common.h | 12 ++++++++++++ 2 files changed, 38 insertions(+) diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c index b518b92..91bd460 100644 --- a/drivers/media/v4l2-core/v4l2-common.c +++ b/drivers/media/v4l2-core/v4l2-common.c @@ -387,6 +387,32 @@ __v4l2_find_nearest_size(const void *array, size_t array_size, } EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size); +#define FRACT_CMP(a, OP, b) \ + ((u64)(a).numerator * (b).denominator OP \ + (u64)(b).numerator * (a).denominator) + +int v4l2_find_closest_fract(struct v4l2_fract x, const struct v4l2_fract *array, + size_t num) +{ + int i; + + for (i = 0; i < num - 1; i++) { + struct v4l2_fract a = array[i]; + struct v4l2_fract b = array[i + 1]; + struct v4l2_fract midpoint = { + .numerator = a.numerator * b.denominator + + b.numerator * a.denominator, + .denominator = 2 * a.denominator * b.denominator, + }; + + if (FRACT_CMP(x, <=, midpoint)) + break; + } + + return i; +} +EXPORT_SYMBOL_GPL(v4l2_find_closest_fract); + void v4l2_get_timestamp(struct timeval *tv) { struct timespec ts; diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h index cdc87ec..e388f4e 100644 --- a/include/media/v4l2-common.h +++ b/include/media/v4l2-common.h @@ -350,6 +350,18 @@ __v4l2_find_nearest_size(const void *array, size_t array_size, size_t height_offset, s32 width, s32 height); /** + * v4l2_find_closest_fract - locate the closest element in a sorted array + * @x: The reference value. + * @array: The array in which to look for the closest element. Must be sorted + * in ascending order. + * @num: number of elements in 'array'. + * + * Returns the index of the element closest to 'x'. + */ +int v4l2_find_closest_fract(struct v4l2_fract x, const struct v4l2_fract *array, + size_t num); + +/** * v4l2_get_timestamp - helper routine to get a timestamp to be used when * filling streaming metadata. Internally, it uses ktime_get_ts(), * which is the recommended way to get it.