From patchwork Thu Mar 26 09:33:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kaaira Gupta X-Patchwork-Id: 3324 Return-Path: Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 895886040F for ; Thu, 26 Mar 2020 10:33:37 +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="St+VX9RY"; dkim-atps=neutral Received: by mail-pj1-x102b.google.com with SMTP id m15so2161422pje.3 for ; Thu, 26 Mar 2020 02:33:37 -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:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=WFlzpCI76ANsb4EigRR1xgFyxwpl3Mnx/68zZ2RkrL4=; b=St+VX9RYCNx73tPhWfaMdBiid45ZshNpkKqW7ks6GGZ0gcL5r58N9waCCO4VryObWm u4Q/moVcN4yrK7q9mDodTOdTKEogyJ6t+QcmcvCysYScn8l9qp9Jc/4t7xqXf1iKR2Kv SIDWOy7fld31j/B1m/x+2MluLhogJ3rjNxnMmt/NLcWbRndR5fRVqJH2a4IE+sFZxK5N NXAa9xIur/zLs0IAqVMDWmEZ7NXxpARclLHZEIyA4puH3uL7GPRnF54DNXfuGvLveQxy mcbPpoJuZFF34+8W/fdDWa0lTMYct+AOFvKDiJeQK0aiadRussgESaHQYIQCrI7dSpKF y+PA== 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:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=WFlzpCI76ANsb4EigRR1xgFyxwpl3Mnx/68zZ2RkrL4=; b=c3waZrNtMdEzcFBvFK8AheErBqOIoJWHvI7OY2rUQe8INVKhOCUL2k2pneDEWi++VL N3F47qa9dNZ70iDn71NNMZZFHjDavHAsdfvoESIL95CNc/AzCj6TtBGh8BLp0lp8dmec QbcjnskFSzXQPF3XADukeEPv6SI4RVNPoNJu6aJh67HirRh2wBiSQDZe+pU1hRQznw26 Q/5zs1Sjo7DMoOioDzI6wgKI168/Z7T6KvhVO7QjUx0eZPK6LhBLjFBvONgZPls90hWT wdGbjbw9MJXRVMx9Oyy/ultmmebk7fsb6eBlNFSIwpvBsuyPJr1t+cPOuzHaUVNtTDtM nFrA== X-Gm-Message-State: ANhLgQ1mLnwn80vNF9jdijbagLxLXQs18GkBt2+SmD+2ZnHc6H9CgW+P BrmsrTXbLuULQsnAExYDqXNgHDKKYtEAjg== X-Google-Smtp-Source: ADFU+vvbQAOIhiPuhQahXJV51Idf2+7Nh8hG3IcLPRwwXDrNoWB4qi2NY/Y1BH4ebUlJSqpInSVXEQ== X-Received: by 2002:a17:90b:3ca:: with SMTP id go10mr2205822pjb.9.1585215215354; Thu, 26 Mar 2020 02:33:35 -0700 (PDT) Received: from kaaira-HP-Pavilion-Notebook ([103.113.213.154]) by smtp.gmail.com with ESMTPSA id z12sm1295368pfj.144.2020.03.26.02.33.32 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Mar 2020 02:33:34 -0700 (PDT) Date: Thu, 26 Mar 2020 15:03:28 +0530 From: Kaaira Gupta To: libcamera-devel@lists.libcamera.org, Kieran Bingham , Helen Koike , Vaishali Thakkar Cc: kgupta@es.iitr.ac.in Message-ID: <20200326093327.GA9015@kaaira-HP-Pavilion-Notebook> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Subject: [libcamera-devel] [PATCH v3] 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 09:33:37 -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 --- 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 | 19 ++++++++++++++++--- test/v4l2_videodevice/formats.cpp | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index b778181..bb049ec 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -336,9 +336,22 @@ 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 "Invalid"; + + 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..4e02afc 100644 --- a/test/v4l2_videodevice/formats.cpp +++ b/test/v4l2_videodevice/formats.cpp @@ -47,6 +47,26 @@ protected: return TestFail; } + std::vector> formats{ + { V4L2_PIX_FMT_YUYV, "YUYV" }, { 0, "Invalid" }, + { 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 to its fourcc" + << " string" << endl; + return TestFail; + } + } + + if (V4L2PixelFormat().toString() != "Invalid") { + cerr << "Failed to convert default V4L2PixelFormat to 'Invalid'" + << " string" << endl; + return TestFail; + } + return TestPass; } };