[{"id":34550,"web_url":"https://patchwork.libcamera.org/comment/34550/","msgid":"<20250618160916.GG22102@pendragon.ideasonboard.com>","date":"2025-06-18T16:09:16","subject":"Re: [PATCH] EncoderLibJpeg: Fix struct\n\tJPEGPixelFormatInfo::PixelFormatInfo","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Harvey,\n\nThank you for the patch.\n\nOn Wed, Jun 18, 2025 at 12:01:59PM +0000, Harvey Yang wrote:\n> Previously, JPEGPixelFormatInfo::PixelFormatInfo is a const reference.\n> However, the use cases of it are mostly from temporarily constructed\n> instances, which will be destructed right after setting up\n> JPEGPixelFormatInfo.\n\nCould you please explain in more details where those temporary instances\nare destroyed ? JPEGPixelFormatInfo::pixelFormatInfo is populated from\nPixelFormatInfo::info() calls, which should always return a reference to\na const global instance with a lifetime matching the libcamera lifetime.\n\n> To avoid data corruption, JPEGPixelFormatInfo::PixelFormatInfo should be\n> a pure constant member variable instead.\n> \n> Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> ---\n>  src/android/jpeg/encoder_libjpeg.cpp | 2 +-\n>  1 file changed, 1 insertion(+), 1 deletion(-)\n> \n> diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp\n> index cb242b5ec6a8..0c088535f0be 100644\n> --- a/src/android/jpeg/encoder_libjpeg.cpp\n> +++ b/src/android/jpeg/encoder_libjpeg.cpp\n> @@ -34,7 +34,7 @@ namespace {\n>  \n>  struct JPEGPixelFormatInfo {\n>  \tJ_COLOR_SPACE colorSpace;\n> -\tconst PixelFormatInfo &pixelFormatInfo;\n> +\tconst PixelFormatInfo pixelFormatInfo;\n>  \tbool nvSwap;\n>  };\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id E0142BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 18 Jun 2025 16:09:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F1EDE68DC1;\n\tWed, 18 Jun 2025 18:09:35 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F0CEC68DC1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 18 Jun 2025 18:09:33 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 84442752;\n\tWed, 18 Jun 2025 18:09:20 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ZOSnFswu\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1750262960;\n\tbh=O1m6G8WWAYHdtMnQ/hJo88vhEesf+VuoQQ/zZJYzyZE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=ZOSnFswuxHeQ4qDuXRXRKoutUobRRh7uP1Ns9BBmTqV/ztNfKDEIBU8yLagHVOpDQ\n\tx50a4L8Bsx4vvf/hRbwg3g7dLEJS+lahdPNFfHi6IANYquqW8AaTy8WtZkfdNZplHL\n\tdq2SUQMelOsc8b8TJGW26lbNjc+5Kv7qwuwrUM30=","Date":"Wed, 18 Jun 2025 19:09:16 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Harvey Yang <chenghaoyang@chromium.org>","Cc":"libcamera-devel@lists.libcamera.org,\n\tHarvey Yang <chenghaoyang@google.com>","Subject":"Re: [PATCH] EncoderLibJpeg: Fix struct\n\tJPEGPixelFormatInfo::PixelFormatInfo","Message-ID":"<20250618160916.GG22102@pendragon.ideasonboard.com>","References":"<20250618120204.3047218-1-chenghaoyang@google.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20250618120204.3047218-1-chenghaoyang@google.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":34553,"web_url":"https://patchwork.libcamera.org/comment/34553/","msgid":"<CAEB1ahuzDD+Pkdq10k-PBRbC0OMF3aTJRpVtRH7uUhu-_7rqQw@mail.gmail.com>","date":"2025-06-19T05:39:52","subject":"Re: [PATCH] EncoderLibJpeg: Fix struct\n\tJPEGPixelFormatInfo::PixelFormatInfo","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"content":"Hi Laurent,\n\nOn Thu, Jun 19, 2025 at 12:09 AM Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n>\n> Hi Harvey,\n>\n> Thank you for the patch.\n>\n> On Wed, Jun 18, 2025 at 12:01:59PM +0000, Harvey Yang wrote:\n> > Previously, JPEGPixelFormatInfo::PixelFormatInfo is a const reference.\n> > However, the use cases of it are mostly from temporarily constructed\n> > instances, which will be destructed right after setting up\n> > JPEGPixelFormatInfo.\n>\n> Could you please explain in more details where those temporary instances\n> are destroyed ? JPEGPixelFormatInfo::pixelFormatInfo is populated from\n> PixelFormatInfo::info() calls, which should always return a reference to\n> a const global instance with a lifetime matching the libcamera lifetime.\n\nAh yes, you're right. I misunderstood the failure I encountered:\nI'm running libcamera on Android, and it's that const global\n`pixelInfo` in `encoder_libjpeg.cpp` is initialized before\nconst global `pixelFormatInfo` in `formats.cpp`, even if\n`formats.cpp` is wrapped in the base libcamera shared library,\nand the android adapter (which contains `encoder_libjpeg.cpp`)\ndepends on the base libcamera shared library.\nIt seems that C++ initialization order doesn't follow the linking\nprocess...\n\nPlease check if it makes sense to you, and I'll upload another\npatch to put `pixelFormatInfo` in a static function in PixelFormatInfo.\nLet me know if there's a more proper fix.\n\nThanks,\nHarvey\n\n>\n> > To avoid data corruption, JPEGPixelFormatInfo::PixelFormatInfo should be\n> > a pure constant member variable instead.\n> >\n> > Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>\n> > ---\n> >  src/android/jpeg/encoder_libjpeg.cpp | 2 +-\n> >  1 file changed, 1 insertion(+), 1 deletion(-)\n> >\n> > diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp\n> > index cb242b5ec6a8..0c088535f0be 100644\n> > --- a/src/android/jpeg/encoder_libjpeg.cpp\n> > +++ b/src/android/jpeg/encoder_libjpeg.cpp\n> > @@ -34,7 +34,7 @@ namespace {\n> >\n> >  struct JPEGPixelFormatInfo {\n> >       J_COLOR_SPACE colorSpace;\n> > -     const PixelFormatInfo &pixelFormatInfo;\n> > +     const PixelFormatInfo pixelFormatInfo;\n> >       bool nvSwap;\n> >  };\n> >\n>\n> --\n> Regards,\n>\n> Laurent Pinchart","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 74D7AC3237\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 19 Jun 2025 05:40:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E940F68DCC;\n\tThu, 19 Jun 2025 07:40:06 +0200 (CEST)","from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com\n\t[IPv6:2a00:1450:4864:20::22f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6ABAA61535\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 19 Jun 2025 07:40:04 +0200 (CEST)","by mail-lj1-x22f.google.com with SMTP id\n\t38308e7fff4ca-32b43846e8cso3194681fa.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 18 Jun 2025 22:40:04 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"j9TnBd5G\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1750311603; x=1750916403;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:cc:to:subject:message-id:date:from\n\t:in-reply-to:references:mime-version:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=vMtGvr5MqoIl2nd1Y9bCHgHMI1dZXn7N5nRa5MkdqO8=;\n\tb=j9TnBd5G0wTFjo/lombrMWxxBavnuD8/4tPKbC1ap/G2W8ioxeJyAjmr7Oc9untp+o\n\tkCTtO2Ug306dIhTulwjwngowufdNbFpSTRzQplcZDGfohqFTH97nu1DmOBxIs5vvuDSM\n\tfU+7Ek6GJASjKNFcZDK3bcKYpEdhbF44yjvHo=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1750311603; x=1750916403;\n\th=content-transfer-encoding:cc:to:subject:message-id:date:from\n\t:in-reply-to:references:mime-version:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=vMtGvr5MqoIl2nd1Y9bCHgHMI1dZXn7N5nRa5MkdqO8=;\n\tb=GmqonlBrFuVZcb8fyPBk0t61wh/GdThOVfuHGq7Ax4KePy7x4c8gq8Fl0+edRJ2lRy\n\t8qsAl3r3x9/nylD6+4NqQOSOHFlgdfQrKUk52nxovjbE3Oem/HumJxFFC8WsqbAOXQlg\n\tnMyp6FJ3Id1PGZf5T/aUyb2pb0pCG1tKrnQ7Lh4+3EZdSXZlt/59d0FqWfOZiQ02KWcu\n\tm2yFrDS42Z2yX2zif5Y+B9uORZmXwh0VI9gBghbNtwuiBWeA1CdRlgxjZalI1wKRzQcC\n\tP4N0wB8I+HZ9dSZsmgsseiPdMhpsAFVmRObpiifqvp4JfdMZZ31cgz66RYk7wUMJpNKJ\n\twJjA==","X-Gm-Message-State":"AOJu0YwmwL9Y7aRrVNZH5Flu9AJXHNiTnmYLSjdQ7HfSFQqtGiHe00nu\n\tnbNf//49Bs6PVfbKu5vWQSxJgZTUUOpREJLmng9Lz6lLfDsEhS67NmHKa9slx3stj7eoN+UCTiQ\n\t/cxUJHEZJhxYtknpj3bT2r1h/PigjBTktgnY7TOskCjwlJQwsT7g=","X-Gm-Gg":"ASbGncvZNACJMVg7xDlyZ2Tp2BlGSfD2HvojmJf8k6raM9Q88+SKte4aLcFL2tgq7+b\n\tYlN4hqrvqA1PtfxRvZMzn73WSgcJ1J8p60oXTfOQiv9LuGAiR0SGkYpisNsMuhcMopvojEWY7Ze\n\tXyW4ugK3/XaK/HHvNctD2JGkzgr5Vwm3r5w/JLXjm5nf9iFvkbluK8dMdz/7RO/Rqi6nvWhA==","X-Google-Smtp-Source":"AGHT+IHLF7wYN1O9PIIivJraKNGpsFqGbKIiqLko5m7FQJ+/sKLo7ZkCKOauXCRo4J+rVt23sBEITFH3YG8ifLiGGH4=","X-Received":"by 2002:a05:651c:1a0a:b0:32b:50c5:2593 with SMTP id\n\t38308e7fff4ca-32b50c52793mr46279681fa.18.1750311603538;\n\tWed, 18 Jun 2025 22:40:03 -0700 (PDT)","MIME-Version":"1.0","References":"<20250618120204.3047218-1-chenghaoyang@google.com>\n\t<20250618160916.GG22102@pendragon.ideasonboard.com>","In-Reply-To":"<20250618160916.GG22102@pendragon.ideasonboard.com>","From":"Cheng-Hao Yang <chenghaoyang@chromium.org>","Date":"Thu, 19 Jun 2025 13:39:52 +0800","X-Gm-Features":"AX0GCFvPHjfQnyVKrLwpc4XsMSwBJnJLs13VVjJtc69D6F0WyKpg0YvK5DVGIfE","Message-ID":"<CAEB1ahuzDD+Pkdq10k-PBRbC0OMF3aTJRpVtRH7uUhu-_7rqQw@mail.gmail.com>","Subject":"Re: [PATCH] EncoderLibJpeg: Fix struct\n\tJPEGPixelFormatInfo::PixelFormatInfo","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tHarvey Yang <chenghaoyang@google.com>","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]