<div dir="ltr"><div dir="ltr">Hi David,<div><br></div><div>Thank you for your feedback</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 1 Nov 2022 at 12:01, David Plowman <<a href="mailto:david.plowman@raspberrypi.com">david.plowman@raspberrypi.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Naush<br>
<br>
Thanks for the patch!<br>
<br>
On Fri, 14 Oct 2022 at 14:19, Naushir Patuck via libcamera-devel<br>
<<a href="mailto:libcamera-devel@lists.libcamera.org" target="_blank">libcamera-devel@lists.libcamera.org</a>> wrote:<br>
><br>
> Read the platform configuration parameters from default.json config file. Use<br>
> the PipelineHandler::configurationFile() helper to determine the full path of<br>
> the file. The default.json filename can be overridden by the user setting the<br>
> LIBCAMERA_RPI_CONFIG_FILE environment variable giving the full path of the<br>
> custom file.<br>
><br>
> Add config parameter validation to ensure bad parameters cannot cause the<br>
> pipeline handler to stop working.<br>
><br>
> Currently three parameters are available through the json file:<br>
><br>
> "min_unicam_buffers"<br>
> The minimum number of internal Unicam buffers to allocate.<br>
><br>
> "min_total_unicam_buffers"<br>
> The minimum number of internal + external Unicam buffers that must be allocated.<br>
><br>
> "num_output0_buffers"<br>
> Number of internal ISP Output0 buffers to allocate.<br>
><br>
> Signed-off-by: Naushir Patuck <<a href="mailto:naush@raspberrypi.com" target="_blank">naush@raspberrypi.com</a>><br>
> ---<br>
>  .../pipeline/raspberrypi/data/default.json    | 20 +++++++<br>
>  .../pipeline/raspberrypi/data/meson.build     |  8 +++<br>
>  .../pipeline/raspberrypi/meson.build          |  2 +<br>
>  .../pipeline/raspberrypi/raspberrypi.cpp      | 56 +++++++++++++++++++<br>
>  4 files changed, 86 insertions(+)<br>
>  create mode 100644 src/libcamera/pipeline/raspberrypi/data/default.json<br>
>  create mode 100644 src/libcamera/pipeline/raspberrypi/data/meson.build<br>
><br>
> diff --git a/src/libcamera/pipeline/raspberrypi/data/default.json b/src/libcamera/pipeline/raspberrypi/data/default.json<br>
> new file mode 100644<br>
> index 000000000000..d709e31850af<br>
> --- /dev/null<br>
> +++ b/src/libcamera/pipeline/raspberrypi/data/default.json<br>
> @@ -0,0 +1,20 @@<br>
> +{<br>
> +        "version": 1.0,<br>
> +        "target": "bcm2835",<br>
> +<br>
> +        "pipeline_handler":<br>
> +        {<br>
> +                # The minimum number of internal buffers to be allocated for Unicam.<br>
> +                # This value must be greater than 0, but less than or equal to min_total_unicam_buffers.<br>
<br>
Are we allowed comments in json files now? That's nice! Perhaps we<br>
should put some in our tuning files...<br>
<br>
> +                "min_unicam_buffers": 2,<br>
> +<br>
> +                # The minimum total (internal + external) buffer count used for Unicam.<br>
> +                # The number of internal buffers allocated for Unicam is given by:<br>
> +                # internal buffer count = max(min_unicam_buffers,<br>
> +                #                             min_total_unicam_buffers - external buffer count)<br>
> +                "min_total_unicam_buffers": 4,<br>
> +<br>
> +                # The number of internal buffers used for ISP Output0. This must be set to 1.<br>
> +                "num_output0_buffers": 1<br>
> +        }<br>
> +}<br>
> diff --git a/src/libcamera/pipeline/raspberrypi/data/meson.build b/src/libcamera/pipeline/raspberrypi/data/meson.build<br>
> new file mode 100644<br>
> index 000000000000..232f8b43c5fd<br>
> --- /dev/null<br>
> +++ b/src/libcamera/pipeline/raspberrypi/data/meson.build<br>
> @@ -0,0 +1,8 @@<br>
> +# SPDX-License-Identifier: CC0-1.0<br>
> +<br>
> +conf_files = files([<br>
> +    'default.json',<br>
> +])<br>
> +<br>
> +install_data(conf_files,<br>
> +             install_dir : libcamera_datadir / 'pipeline/raspberrypi')<br>
> diff --git a/src/libcamera/pipeline/raspberrypi/meson.build b/src/libcamera/pipeline/raspberrypi/meson.build<br>
> index f1a2f5ee72c2..48235f887501 100644<br>
> --- a/src/libcamera/pipeline/raspberrypi/meson.build<br>
> +++ b/src/libcamera/pipeline/raspberrypi/meson.build<br>
> @@ -5,3 +5,5 @@ libcamera_sources += files([<br>
>      'raspberrypi.cpp',<br>
>      'rpi_stream.cpp',<br>
>  ])<br>
> +<br>
> +subdir('data')<br>
> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp<br>
> index 450029197b11..bac7a66ba900 100644<br>
> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp<br>
> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp<br>
> @@ -14,6 +14,7 @@<br>
>  #include <unordered_set><br>
>  #include <utility><br>
><br>
> +#include <libcamera/base/file.h><br>
>  #include <libcamera/base/shared_fd.h><br>
>  #include <libcamera/base/utils.h><br>
><br>
> @@ -40,6 +41,7 @@<br>
>  #include "libcamera/internal/media_device.h"<br>
>  #include "libcamera/internal/pipeline_handler.h"<br>
>  #include "libcamera/internal/v4l2_videodevice.h"<br>
> +#include "libcamera/internal/yaml_parser.h"<br>
><br>
>  #include "dma_heaps.h"<br>
>  #include "rpi_stream.h"<br>
> @@ -301,6 +303,7 @@ public:<br>
>         };<br>
><br>
>         Config config_;<br>
> +       unsigned int numUnicamBuffers;<br>
><br>
>  private:<br>
>         void checkRequestCompleted();<br>
> @@ -1140,6 +1143,19 @@ int PipelineHandlerRPi::queueRequestDevice(Camera *camera, Request *request)<br>
>                          */<br>
>                         stream->setExternalBuffer(buffer);<br>
>                 }<br>
> +<br>
> +               if (!buffer) {<br>
> +                       if (stream == &data->isp_[Isp::Output0] && !data->config_.numOutput0Buffers) {<br>
> +                               LOG(RPI, Error) << "Output0 buffer must be provided in the request.";<br>
> +                               return -EINVAL;<br>
> +                       }<br>
> +<br>
> +                       if (stream == &data->unicam_[Unicam::Image] && !data->numUnicamBuffers) {<br>
> +                               LOG(RPI, Error) << "Unicam Image buffer must be provided in the request.";<br>
> +                               return -EINVAL;<br>
> +                       }<br>
> +               }<br>
> +<br>
>                 /*<br>
>                  * If no buffer is provided by the request for this stream, we<br>
>                  * queue a nullptr to the stream to signify that it must use an<br>
> @@ -1398,6 +1414,7 @@ int PipelineHandlerRPi::registerCamera(MediaDevice *unicam, MediaDevice *isp, Me<br>
>  int PipelineHandlerRPi::configurePipelineHandler(RPiCameraData *data)<br>
>  {<br>
>         RPiCameraData::Config &config = data->config_;<br>
> +       std::string filename;<br>
><br>
>         config = {<br>
>                 .minUnicamBuffers = 2,<br>
> @@ -1405,6 +1422,44 @@ int PipelineHandlerRPi::configurePipelineHandler(RPiCameraData *data)<br>
>                 .numOutput0Buffers = 1,<br>
>         };<br>
><br>
> +       char const *configFromEnv = utils::secure_getenv("LIBCAMERA_RPI_CONFIG_FILE");<br>
> +       if (!configFromEnv || *configFromEnv == '\0')<br>
> +               filename = configurationFile("raspberrypi/default.json");<br>
<br>
I was a teeny bit nervous that in the non-installed case it might come<br>
back with "libcamera/src/ipa/raspberrypi/data/raspberrypi/default.json"<br>
which would be the wrong thing? Maybe you could just put my mind at<br>
ease here...<br></blockquote><div><br></div><div>It should load things from "libcamera/src/libcamera/pipeline/data/raspberrypi/default.json",</div><div>but I ought to check this again to make sure!</div><div><br></div><div>However, that does lead to some awkwardness, perhaps it should be read from</div><div> "libcamera/src/libcamera/pipeline/raspberrypi/data/default.json" to be consistent with</div><div>the ipa config files...?</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> +       else<br>
> +               filename = std::string(configFromEnv);<br>
> +<br>
> +       if (filename.empty())<br>
> +               return -EINVAL;<br>
> +<br>
> +       File file(filename);<br>
> +       if (!file.open(File::OpenModeFlag::ReadOnly)) {<br>
> +               LOG(RPI, Error) << "Failed to open configuration file '" << filename << "'";<br>
> +               return -EIO;<br>
> +       }<br>
> +<br>
> +       LOG(RPI, Info) << "Using configuration file '" << filename << "'";<br>
> +<br>
> +       std::unique_ptr<YamlObject> root = YamlParser::parse(file);<br>
> +       if (!root) {<br>
> +               LOG(RPI, Error) << "Failed to parse configuration file, using defaults!";<br>
> +               return -EINVAL;<br>
> +       }<br>
> +<br>
> +       ASSERT((*root)["version"].get<double>() == 1.0);<br>
> +<br>
> +       const YamlObject &phConfig = (*root)["pipeline_handler"];<br>
> +       config.minUnicamBuffers =<br>
> +               phConfig["min_unicam_buffers"].get<unsigned int>(config.minUnicamBuffers);<br>
> +       config.minTotalUnicamBuffers =<br>
> +               phConfig["min_total_unicam_buffers"].get<unsigned int>(config.minTotalUnicamBuffers);<br>
> +       config.numOutput0Buffers =<br>
> +               phConfig["num_output0_buffers"].get<unsigned int>(config.numOutput0Buffers);<br>
> +<br>
> +       if (config.minTotalUnicamBuffers < config.minUnicamBuffers || config.minTotalUnicamBuffers < 1) {<br>
<br>
I wonder a little bit about splitting up all these little checks so<br>
that we can be really specific in the error message? But I'm not too<br>
fussed.<br></blockquote><div><br></div><div>Good point, I will split this up.</div><div><br></div><div>Regards,</div><div>Naush</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Subject to a check on that pathname question I had:<br>
<br>
Reviewed-by: David Plowman <<a href="mailto:david.plowman@raspberrypi.com" target="_blank">david.plowman@raspberrypi.com</a>><br>
<br>
Thanks!<br>
David<br>
<br>
> +               LOG(RPI, Error) << "Invalid Unicam buffer configuration used!";<br>
> +               return -EINVAL;<br>
> +       }<br>
> +<br>
>         return 0;<br>
>  }<br>
><br>
> @@ -1471,6 +1526,7 @@ int PipelineHandlerRPi::prepareBuffers(Camera *camera)<br>
>                          */<br>
>                         numBuffers = std::max<int>(data->config_.minUnicamBuffers,<br>
>                                                    minBuffers - numRawBuffers);<br>
> +                       data->numUnicamBuffers = numBuffers;<br>
>                 } else if (stream == &data->isp_[Isp::Input]) {<br>
>                         /*<br>
>                          * ISP input buffers are imported from Unicam, so follow<br>
> --<br>
> 2.25.1<br>
><br>
</blockquote></div></div>