static k4a_result_t validate_configuration()

in src/sdk/k4a.c [580:841]


static k4a_result_t validate_configuration(k4a_context_t *device, const k4a_device_configuration_t *config)
{
    RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, config == NULL);
    RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, device == NULL);
    k4a_result_t result = K4A_RESULT_SUCCEEDED;
    bool depth_enabled = false;
    bool color_enabled = false;

    if (config->color_format != K4A_IMAGE_FORMAT_COLOR_MJPG && config->color_format != K4A_IMAGE_FORMAT_COLOR_YUY2 &&
        config->color_format != K4A_IMAGE_FORMAT_COLOR_NV12 && config->color_format != K4A_IMAGE_FORMAT_COLOR_BGRA32)
    {
        result = K4A_RESULT_FAILED;
        LOG_ERROR("The configured color_format is not a valid k4a_color_format_t value.", 0);
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->color_resolution < K4A_COLOR_RESOLUTION_OFF ||
            config->color_resolution > K4A_COLOR_RESOLUTION_3072P)
        {
            result = K4A_RESULT_FAILED;
            LOG_ERROR("The configured color_resolution is not a valid k4a_color_resolution_t value.", 0);
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->depth_mode < K4A_DEPTH_MODE_OFF || config->depth_mode > K4A_DEPTH_MODE_PASSIVE_IR)
        {
            result = K4A_RESULT_FAILED;
            LOG_ERROR("The configured depth_mode is not a valid k4a_depth_mode_t value.", 0);
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->camera_fps != K4A_FRAMES_PER_SECOND_5 && config->camera_fps != K4A_FRAMES_PER_SECOND_15 &&
            config->camera_fps != K4A_FRAMES_PER_SECOND_30)
        {
            result = K4A_RESULT_FAILED;
            LOG_ERROR("The configured camera_fps is not a valid k4a_fps_t value.", 0);
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->wired_sync_mode < K4A_WIRED_SYNC_MODE_STANDALONE ||
            config->wired_sync_mode > K4A_WIRED_SYNC_MODE_SUBORDINATE)
        {
            result = K4A_RESULT_FAILED;
            LOG_ERROR("The configured wired_sync_mode is not a valid k4a_wired_sync_mode_t value.", 0);
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_SUBORDINATE ||
            config->wired_sync_mode == K4A_WIRED_SYNC_MODE_MASTER)
        {
            bool sync_in_cable_present;
            bool sync_out_cable_present;

            result = colormcu_get_external_sync_jack_state(device->colormcu,
                                                           &sync_in_cable_present,
                                                           &sync_out_cable_present);

            if (K4A_SUCCEEDED(result))
            {
                if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_SUBORDINATE && !sync_in_cable_present)
                {
                    result = K4A_RESULT_FAILED;
                    LOG_ERROR("Failure to detect presence of sync in cable with wired sync mode "
                              "K4A_WIRED_SYNC_MODE_SUBORDINATE.",
                              0);
                }

                if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_MASTER)
                {
                    if (!sync_out_cable_present)
                    {
                        result = K4A_RESULT_FAILED;
                        LOG_ERROR("Failure to detect presence of sync out cable with wired sync mode "
                                  "K4A_WIRED_SYNC_MODE_MASTER.",
                                  0);
                    }

                    if (config->color_resolution == K4A_COLOR_RESOLUTION_OFF)
                    {
                        result = K4A_RESULT_FAILED;
                        LOG_ERROR(
                            "Device wired_sync_mode is set to K4A_WIRED_SYNC_MODE_MASTER, so color camera must be used "
                            "on master device. Color_resolution can not be set to K4A_COLOR_RESOLUTION_OFF.",
                            0);
                    }
                }
            }
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->wired_sync_mode == K4A_WIRED_SYNC_MODE_SUBORDINATE &&
            config->subordinate_delay_off_master_usec != 0)
        {
            uint32_t fps_in_usec = HZ_TO_PERIOD_US(k4a_convert_fps_to_uint(config->camera_fps));
            if (config->subordinate_delay_off_master_usec > fps_in_usec)
            {
                result = K4A_RESULT_FAILED;
                LOG_ERROR("The configured subordinate device delay from the master device cannot exceed one frame "
                          "interval of %d. User requested %d",
                          fps_in_usec,
                          config->subordinate_delay_off_master_usec);
            }
        }

        if (config->wired_sync_mode != K4A_WIRED_SYNC_MODE_SUBORDINATE &&
            config->subordinate_delay_off_master_usec != 0)
        {
            result = K4A_RESULT_FAILED;
            LOG_ERROR("When wired_sync_mode is K4A_WIRED_SYNC_MODE_STANDALONE or K4A_WIRED_SYNC_MODE_MASTER, the "
                      "subordinate_delay_off_master_usec must be 0.",
                      0);
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (config->depth_mode != K4A_DEPTH_MODE_OFF)
        {
            depth_enabled = true;
        }

        if (config->color_resolution != K4A_COLOR_RESOLUTION_OFF)
        {
            color_enabled = true;
        }

        if (depth_enabled && color_enabled)
        {
            int64_t fps = HZ_TO_PERIOD_US(k4a_convert_fps_to_uint(config->camera_fps));
            if (config->depth_delay_off_color_usec < -fps || config->depth_delay_off_color_usec > fps)
            {
                result = K4A_RESULT_FAILED;
                LOG_ERROR("The configured depth_delay_off_color_usec must be within +/- one frame interval of %d. User "
                          "requested %d",
                          fps,
                          config->depth_delay_off_color_usec);
            }
        }
        else if (!depth_enabled && !color_enabled)
        {
            result = K4A_RESULT_FAILED;
            LOG_ERROR("Neither depth camera nor color camera are enabled in the configuration, at least one needs to "
                      "be enabled.",
                      0);
        }
        else
        {
            if (config->depth_delay_off_color_usec != 0)
            {
                result = K4A_RESULT_FAILED;
                LOG_ERROR("If depth_delay_off_color_usec is not 0, both depth camera and color camera must be enabled.",
                          0);
            }

            if (config->synchronized_images_only)
            {
                result = K4A_RESULT_FAILED;
                LOG_ERROR(
                    "To enable synchronized_images_only, both depth camera and color camera must also be enabled.", 0);
            }
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (depth_enabled)
        {
            struct _depth_configuration
            {
                k4a_depth_mode_t mode;
                k4a_fps_t max_fps;
            } supported_depth_configs[] = {
                { K4A_DEPTH_MODE_NFOV_2X2BINNED, K4A_FRAMES_PER_SECOND_30 },
                { K4A_DEPTH_MODE_NFOV_UNBINNED, K4A_FRAMES_PER_SECOND_30 },
                { K4A_DEPTH_MODE_WFOV_2X2BINNED, K4A_FRAMES_PER_SECOND_30 },
                { K4A_DEPTH_MODE_WFOV_UNBINNED, K4A_FRAMES_PER_SECOND_15 },
                { K4A_DEPTH_MODE_PASSIVE_IR, K4A_FRAMES_PER_SECOND_30 },
            };

            bool depth_fps_and_mode_supported = false;
            for (unsigned int x = 0; x < COUNTOF(supported_depth_configs); x++)
            {
                if (supported_depth_configs[x].mode == config->depth_mode &&
                    supported_depth_configs[x].max_fps >= config->camera_fps)
                {
                    depth_fps_and_mode_supported = true;
                    break;
                }
            }

            if (!depth_fps_and_mode_supported)
            {
                result = K4A_RESULT_FAILED;
                LOG_ERROR("The configured depth_mode %s does not support the configured camera_fps %s.",
                          k4a_depth_mode_to_string(config->depth_mode),
                          k4a_fps_to_string(config->camera_fps));
            }
        }
    }

    if (K4A_SUCCEEDED(result))
    {
        if (color_enabled)
        {
            struct _color_configuration
            {
                k4a_color_resolution_t res;
                k4a_image_format_t format;
                k4a_fps_t max_fps;
            } supported_color_configs[] = {
                { K4A_COLOR_RESOLUTION_2160P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_1440P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_1080P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_YUY2, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_NV12, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_3072P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_15 },
                { K4A_COLOR_RESOLUTION_1536P, K4A_IMAGE_FORMAT_COLOR_MJPG, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_2160P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_1440P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_1080P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_720P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
                { K4A_COLOR_RESOLUTION_3072P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_15 },
                { K4A_COLOR_RESOLUTION_1536P, K4A_IMAGE_FORMAT_COLOR_BGRA32, K4A_FRAMES_PER_SECOND_30 },
            };

            bool color_fps_and_res_and_format_supported = false;
            for (unsigned int x = 0; x < COUNTOF(supported_color_configs); x++)
            {
                if (supported_color_configs[x].res == config->color_resolution &&
                    supported_color_configs[x].max_fps >= config->camera_fps &&
                    supported_color_configs[x].format == config->color_format)
                {
                    color_fps_and_res_and_format_supported = true;
                    break;
                }
            }

            if (!color_fps_and_res_and_format_supported)
            {
                result = K4A_RESULT_FAILED;
                LOG_ERROR("The combination of color_resolution at %s, color_format at %s, and camera_fps at %s is not "
                          "supported.",
                          k4a_color_resolution_to_string(config->color_resolution),
                          k4a_image_format_to_string(config->color_format),
                          k4a_fps_to_string(config->camera_fps));
            }
        }
    }
    return result;
}