From patchwork Thu Mar 26 20:14:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kaaira Gupta X-Patchwork-Id: 3343 Return-Path: Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 14D9960412 for ; Thu, 26 Mar 2020 21:14:10 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=es-iitr-ac-in.20150623.gappssmtp.com header.i=@es-iitr-ac-in.20150623.gappssmtp.com header.b="YE76cruy"; dkim-atps=neutral Received: by mail-pg1-x52b.google.com with SMTP id a32so3458932pga.4 for ; Thu, 26 Mar 2020 13:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=es-iitr-ac-in.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=+WwGuhGPhcludT0HCtl5ArqUofjOoNPxiNxH8uvRPE8=; b=YE76cruyAzRVPF7slyMCSAk5SoY3Cf+gsgQ/bqlc6ykVfsGZXqkI9brvwapuBgSqJZ HXwN5ZQ7b9qVqRPKO/kz/V7eS1keEXxiXKpET7fWGURyct+mT83t0ljRREWqYWn/4gG5 VBzXcAkpvWZVTns+QPU9mBvqCz6Z/UC1ANUo6heEqnYta1tn5QltoLLSm1xOMgV27zQJ gmBNecEe/cVNz8TmnImO6wslbJZi5wxBeZD8/ae9nXsSZGxFmGBMqqituj9ty3jpgFsZ eFLvLG0tVu3SPSXD4XWaYl6CGSObvCBP0z8rtJKpr2LV0f03hjHx0QIoxsjRVePVMG5X pvuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=+WwGuhGPhcludT0HCtl5ArqUofjOoNPxiNxH8uvRPE8=; b=E1PLNE3ShtxsbS4x1PbaEptVK0UXmaz3UpqWHyBGbRw38xTlEwprYZoIkj0CVGzDwW 0OmnMSNQ0ECkR0u31axU8WIxDtQStBLFnRgWnhHgZk2w7U/+yO4clzUtRnAhsf/lT9bf l1zIjl2xZ1FEQfQlFKOVGvxeGiKSaxJhTo2YmBidWSBQv9TKJjjqPVp7N/upZjDXt3lG wZ0OPqeH8pcHGwG5gRqMo5MQM+icZAX6GcPnfZJ0mTu4fPAykLBTWw1JXfFuv7DiZLUT CdiJHY2QFfoQN80kLFsYVJ4jJKl9P9Tzi7BOLkJVkukor9xN3ek7/znQBDMBeHig7jTJ NxVw== X-Gm-Message-State: ANhLgQ2TbBNKA27f2Ha9TEFkDJdyKpAwVL7XGEpoWSzyn5KQu+OoR1O/ W18+YgJY1i3018dcvuglslsmvmuw7hgiwQ== X-Google-Smtp-Source: ADFU+vv3VXOZCAeyopcZ3v3+OG/oONCtYHo2gnvAA2OrwEpL6rKQyKyuSZKJE5Xa++lVFUdj+kq2HQ== X-Received: by 2002:a63:b557:: with SMTP id u23mr10301653pgo.160.1585253647875; Thu, 26 Mar 2020 13:14:07 -0700 (PDT) Received: from kaaira-HP-Pavilion-Notebook ([103.113.213.154]) by smtp.gmail.com with ESMTPSA id j38sm2231342pgi.51.2020.03.26.13.14.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Mar 2020 13:14:07 -0700 (PDT) Date: Fri, 27 Mar 2020 01:44:00 +0530 From: Kaaira Gupta To: libcamera-devel@lists.libcamera.org, Kieran Bingham , Helen Koike , Vaishali Thakkar Message-ID: <20200326201400.GA29067@kaaira-HP-Pavilion-Notebook> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Subject: [libcamera-devel] [PATCH v4] libcamera: v4l2PixelFormat: Replace hex with fourCC X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 Mar 2020 20:14:10 -0000 Print fourCC characters instead of the hex value in toString() as they are more informative. Also, write the tests for this in formats.cpp Signed-off-by: Kaaira Gupta Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- Changes since v3: - Reformatted the code - Changed 'invalid' to "" - Changed cerr message. Changes since v2: - reformatted the code - added cerr messages before TestFail - Changed test case to accomodate edge case - Increased buffer size of char array - Changed the maximum length of for loop. Changes since v1: - Add tests for checking this function. - use char[] instead of stringstream. - add checks for default value. - Print '.' for non-printable characters. src/libcamera/v4l2_videodevice.cpp | 20 +++++++++++++++++--- test/v4l2_videodevice/formats.cpp | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index b778181..eb33a68 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -336,9 +336,23 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const */ std::string V4L2PixelFormat::toString() const { - char str[11]; - snprintf(str, 11, "0x%08x", fourcc_); - return str; + if (fourcc_ == 0) + return ""; + + char ss[8] = { static_cast(fourcc_ & 0x7f), + static_cast((fourcc_ >> 8) & 0x7f), + static_cast((fourcc_ >> 16) & 0x7f), + static_cast((fourcc_ >> 24) & 0x7f) }; + + for (unsigned int i = 0; i < 4; i++) { + if (!isprint(ss[i])) + ss[i] = '.'; + } + + if (fourcc_ & (1 << 31)) + strcat(ss, "-BE"); + + return ss; } /** diff --git a/test/v4l2_videodevice/formats.cpp b/test/v4l2_videodevice/formats.cpp index d504d17..d01eccd 100644 --- a/test/v4l2_videodevice/formats.cpp +++ b/test/v4l2_videodevice/formats.cpp @@ -47,6 +47,28 @@ protected: return TestFail; } + std::vector> formats{ + { V4L2_PIX_FMT_YUYV, "YUYV" }, + { 0, "" }, + { v4l2_fourcc(0, 1, 2, 3), "...." }, + { V4L2_PIX_FMT_Y16_BE, "Y16 -BE" } + }; + + for (const auto &format : formats) { + if (V4L2PixelFormat(format.first).toString() != format.second) { + cerr << "Failed to convert V4L2PixelFormat" + << utils::hex(format.first) << "to string" + << endl; + return TestFail; + } + } + + if (V4L2PixelFormat().toString() != "") { + cerr << "Failed to convert default V4L2PixelFormat to string" + << endl; + return TestFail; + } + return TestPass; } };