mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	spa: libcamera: source: do not make expensive queries multiple times
`StreamFormats::pixelformats()` and `StreamFormats::sizes()` both
return newly created `std::vector`s, so do not call them multiple
times.
(cherry picked from commit 311b3cc37f)
			
			
This commit is contained in:
		
							parent
							
								
									bdf904ebad
								
							
						
					
					
						commit
						bf1c57928b
					
				
					 1 changed files with 24 additions and 30 deletions
				
			
		| 
						 | 
					@ -4,9 +4,9 @@
 | 
				
			||||||
/* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans <wim.taymans@gmail.com> */
 | 
					/* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans <wim.taymans@gmail.com> */
 | 
				
			||||||
/* SPDX-License-Identifier: MIT */
 | 
					/* SPDX-License-Identifier: MIT */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <climits>
 | 
					#include <cstddef>
 | 
				
			||||||
#include <stddef.h>
 | 
					 | 
				
			||||||
#include <deque>
 | 
					#include <deque>
 | 
				
			||||||
 | 
					#include <limits>
 | 
				
			||||||
#include <optional>
 | 
					#include <optional>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/mman.h>
 | 
					#include <sys/mman.h>
 | 
				
			||||||
| 
						 | 
					@ -95,9 +95,9 @@ struct port {
 | 
				
			||||||
#define N_PORT_PARAMS	7
 | 
					#define N_PORT_PARAMS	7
 | 
				
			||||||
	struct spa_param_info params[N_PORT_PARAMS];
 | 
						struct spa_param_info params[N_PORT_PARAMS];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	uint32_t fmt_index = 0;
 | 
						std::size_t fmt_index = 0;
 | 
				
			||||||
	PixelFormat enum_fmt;
 | 
						PixelFormat enum_fmt;
 | 
				
			||||||
	uint32_t size_index = 0;
 | 
						std::size_t size_index = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	port(struct impl *impl)
 | 
						port(struct impl *impl)
 | 
				
			||||||
		: impl(impl)
 | 
							: impl(impl)
 | 
				
			||||||
| 
						 | 
					@ -437,7 +437,7 @@ static const struct format_info *find_format_info_by_media_type(
 | 
				
			||||||
	return nullptr;
 | 
						return nullptr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int score_size(Size &a, Size &b)
 | 
					static int score_size(const Size &a, const Size &b)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int x, y;
 | 
						int x, y;
 | 
				
			||||||
	x = (int)a.width - (int)b.width;
 | 
						x = (int)a.width - (int)b.width;
 | 
				
			||||||
| 
						 | 
					@ -505,23 +505,18 @@ static int
 | 
				
			||||||
spa_libcamera_enum_format(struct impl *impl, struct port *port, int seq,
 | 
					spa_libcamera_enum_format(struct impl *impl, struct port *port, int seq,
 | 
				
			||||||
		     uint32_t start, uint32_t num, const struct spa_pod *filter)
 | 
							     uint32_t start, uint32_t num, const struct spa_pod *filter)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int res;
 | 
					 | 
				
			||||||
	const struct format_info *info;
 | 
					 | 
				
			||||||
	uint8_t buffer[1024];
 | 
						uint8_t buffer[1024];
 | 
				
			||||||
	struct spa_pod_builder b = { 0 };
 | 
						struct spa_pod_builder b = { 0 };
 | 
				
			||||||
	struct spa_pod_frame f[2];
 | 
						struct spa_pod_frame f[2];
 | 
				
			||||||
	struct spa_result_node_params result;
 | 
						struct spa_result_node_params result;
 | 
				
			||||||
	struct spa_video_colorimetry colorimetry = {};
 | 
						struct spa_video_colorimetry colorimetry = {};
 | 
				
			||||||
	struct spa_pod *fmt;
 | 
						uint32_t count = 0;
 | 
				
			||||||
	uint32_t i, count = 0, num_sizes;
 | 
					 | 
				
			||||||
	PixelFormat format;
 | 
					 | 
				
			||||||
	Size frameSize;
 | 
					 | 
				
			||||||
	SizeRange sizeRange = SizeRange();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	spa_libcamera_get_config(impl);
 | 
						spa_libcamera_get_config(impl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const StreamConfiguration& streamConfig = impl->config->at(0);
 | 
						const StreamConfiguration& streamConfig = impl->config->at(0);
 | 
				
			||||||
	const StreamFormats &formats = streamConfig.formats();
 | 
						const StreamFormats &formats = streamConfig.formats();
 | 
				
			||||||
 | 
						const auto &pixel_formats = formats.pixelformats();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (streamConfig.colorSpace)
 | 
						if (streamConfig.colorSpace)
 | 
				
			||||||
		parse_colorimetry(*streamConfig.colorSpace, &colorimetry);
 | 
							parse_colorimetry(*streamConfig.colorSpace, &colorimetry);
 | 
				
			||||||
| 
						 | 
					@ -537,28 +532,30 @@ next:
 | 
				
			||||||
	result.index = result.next++;
 | 
						result.index = result.next++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
next_fmt:
 | 
					next_fmt:
 | 
				
			||||||
	if (port->fmt_index >= formats.pixelformats().size())
 | 
						if (port->fmt_index >= pixel_formats.size())
 | 
				
			||||||
		goto enum_end;
 | 
							return 0;
 | 
				
			||||||
 | 
					 | 
				
			||||||
	format = formats.pixelformats()[port->fmt_index];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						auto format = pixel_formats[port->fmt_index];
 | 
				
			||||||
	spa_log_debug(impl->log, "format: %s", format.toString().c_str());
 | 
						spa_log_debug(impl->log, "format: %s", format.toString().c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	info = video_format_to_info(format);
 | 
						const auto *info = video_format_to_info(format);
 | 
				
			||||||
	if (info == NULL) {
 | 
						if (info == NULL) {
 | 
				
			||||||
		spa_log_debug(impl->log, "unknown format");
 | 
							spa_log_debug(impl->log, "unknown format");
 | 
				
			||||||
		port->fmt_index++;
 | 
							port->fmt_index++;
 | 
				
			||||||
		goto next_fmt;
 | 
							goto next_fmt;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	num_sizes = formats.sizes(format).size();
 | 
						const auto& sizes = formats.sizes(format);
 | 
				
			||||||
	if (num_sizes > 0 && port->size_index <= num_sizes) {
 | 
						SizeRange sizeRange;
 | 
				
			||||||
 | 
						Size frameSize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!sizes.empty() && port->size_index <= sizes.size()) {
 | 
				
			||||||
		if (port->size_index == 0) {
 | 
							if (port->size_index == 0) {
 | 
				
			||||||
			Size wanted = Size(640, 480), test;
 | 
								Size wanted = Size(640, 480);
 | 
				
			||||||
			int score, best = INT_MAX;
 | 
								int best = std::numeric_limits<int>::max();
 | 
				
			||||||
			for (i = 0; i < num_sizes; i++) {
 | 
					
 | 
				
			||||||
				test = formats.sizes(format)[i];
 | 
								for (const auto& test : sizes) {
 | 
				
			||||||
				score = score_size(wanted, test);
 | 
									int score = score_size(wanted, test);
 | 
				
			||||||
				if (score < best) {
 | 
									if (score < best) {
 | 
				
			||||||
					best = score;
 | 
										best = score;
 | 
				
			||||||
					frameSize = test;
 | 
										frameSize = test;
 | 
				
			||||||
| 
						 | 
					@ -566,7 +563,7 @@ next_fmt:
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else {
 | 
							else {
 | 
				
			||||||
			frameSize = formats.sizes(format)[port->size_index - 1];
 | 
								frameSize = sizes[port->size_index - 1];
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else if (port->size_index < 1) {
 | 
						} else if (port->size_index < 1) {
 | 
				
			||||||
		sizeRange = formats.range(format);
 | 
							sizeRange = formats.range(format);
 | 
				
			||||||
| 
						 | 
					@ -632,8 +629,7 @@ next_fmt:
 | 
				
			||||||
				SPA_POD_Id(colorimetry.primaries), 0);
 | 
									SPA_POD_Id(colorimetry.primaries), 0);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt = (struct spa_pod*) spa_pod_builder_pop(&b, &f[0]);
 | 
						const auto *fmt = reinterpret_cast<spa_pod *>(spa_pod_builder_pop(&b, &f[0]));
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (spa_pod_filter(&b, &result.param, fmt, filter) < 0)
 | 
						if (spa_pod_filter(&b, &result.param, fmt, filter) < 0)
 | 
				
			||||||
		goto next;
 | 
							goto next;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -642,9 +638,7 @@ next_fmt:
 | 
				
			||||||
	if (++count != num)
 | 
						if (++count != num)
 | 
				
			||||||
		goto next;
 | 
							goto next;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      enum_end:
 | 
						return 0;
 | 
				
			||||||
	res = 0;
 | 
					 | 
				
			||||||
	return res;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int spa_libcamera_set_format(struct impl *impl, struct port *port,
 | 
					static int spa_libcamera_set_format(struct impl *impl, struct port *port,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue