/* * SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include "backend.h" #include "decode_params.h" #include "encode_params.h" #include "image.h" namespace nvimgcodec { namespace py = pybind11; using namespace py::literals; class ILogger; class CodeStream; class Encoder { public: Encoder(nvimgcodecInstance_t instance, ILogger* logger, int device_id, int max_num_cpu_threads, std::optional> backends, const std::string& options); ~Encoder(); py::object encode(const py::object& image_s, const std::string& codec, std::optional code_stream_s = std::nullopt, const std::optional& params = std::nullopt, intptr_t cuda_stream = 0); py::object write(const std::string& file_name, const py::object& image, const std::string& codec, const std::optional& params, intptr_t cuda_stream); std::vector write(const std::vector& file_names, const std::vector& images, const std::string& codec, const std::optional& params, intptr_t cuda_stream); py::object enter(); void exit(const std::optional& exc_type, const std::optional& exc_value, const std::optional& traceback); static void exportToPython(py::module& m, nvimgcodecInstance_t instance, ILogger* logger); private: void encode_batch_impl(const std::vector& images, const std::optional& params, intptr_t cuda_stream, std::function create_code_stream, std::function post_encode_call_back); py::object encode_image(const Image* image, const std::string& codec, std::optional code_stream, const std::optional& params = std::nullopt, intptr_t cuda_stream = 0); std::vector encode_batch(const std::vector& images, const std::string& codec, std::optional> code_streams, const std::optional& params = std::nullopt, intptr_t cuda_stream = 0); py::object write_image(const std::string& file_name, const Image* image, const std::string& codec, const std::optional& params, intptr_t cuda_stream); std::vector write_batch(const std::vector& file_names, const std::vector& images, const std::string& codec, const std::optional& params, intptr_t cuda_stream); // Helper function to convert Python objects to Image pointers with exception handling std::vector convertPyObjectsToImages(const std::vector& py_images, intptr_t cuda_stream, std::vector& image_raii); std::shared_ptr::type> encoder_; nvimgcodecInstance_t instance_; ILogger* logger_; }; } // namespace nvimgcodec