From c9c43a2511ec88bf40aa720ba712577ba85a6863 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 31 Dec 2015 17:52:14 +0100 Subject: [PATCH] Only load images once; add a cache. --- image_input.cpp | 19 ++++++++++++++++--- image_input.h | 5 +++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/image_input.cpp b/image_input.cpp index 090b6db..2bd147d 100644 --- a/image_input.cpp +++ b/image_input.cpp @@ -12,10 +12,19 @@ extern "C" { using namespace std; -ImageInput::ImageInput(const std::string &filename) +ImageInput::ImageInput(const string &filename) : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA, GL_UNSIGNED_BYTE, 1280, 720) // FIXME { + set_pixel_data(load_image(filename)); +} + +const uint8_t *ImageInput::load_image(const string &filename) +{ + if (all_images.count(filename)) { + return all_images[filename].get(); + } + AVFormatContext *format_ctx = nullptr; if (avformat_open_input(&format_ctx, filename.c_str(), nullptr, nullptr) != 0) { fprintf(stderr, "%s: Error opening file\n", filename.c_str()); @@ -88,12 +97,16 @@ ImageInput::ImageInput(const std::string &filename) sws_freeContext(sws_ctx); size_t len = frame->width * frame->height * 4; - image_data.reset(new uint8_t[len]); + unique_ptr image_data(new uint8_t[len]); av_image_copy_to_buffer(image_data.get(), len, pic.data, pic.linesize, PIX_FMT_RGBA, frame->width, frame->height, 1); - set_pixel_data(image_data.get()); avpicture_free(&pic); av_frame_free(&frame); avcodec_close(codec_ctx); avformat_close_input(&format_ctx); + + all_images[filename] = move(image_data); + return all_images[filename].get(); } + +map> ImageInput::all_images; diff --git a/image_input.h b/image_input.h index edc86c8..3fad845 100644 --- a/image_input.h +++ b/image_input.h @@ -1,12 +1,12 @@ #ifndef _IMAGE_INPUT_H #define _IMAGE_INPUT_H 1 +#include #include #include #include - // An output that takes its input from a static image, loaded with ffmpeg. // comes from a single 2D array with chunky pixels. class ImageInput : public movit::FlatInput { @@ -16,7 +16,8 @@ public: std::string effect_type_id() const override { return "ImageInput"; } private: - std::unique_ptr image_data; + static const uint8_t *load_image(const std::string &filename); + static std::map> all_images; }; #endif // !defined(_IMAGE_INPUT_H) -- 2.39.2