]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Remove an (easily-predictable) branch from the StereoCompressor inner loop.
[nageru] / image_input.cpp
index ad193ff008213f7c4b8112d1e735f4567ebeef6e..b8e2b02942b4ea01993131f2ee73acf595479e05 100644 (file)
@@ -10,104 +10,153 @@ extern "C" {
 #include <libswscale/swscale.h>
 }
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <mutex>
+#include <thread>
+
+#include "ffmpeg_raii.h"
+#include "flags.h"
+
 using namespace std;
 
+namespace {
+
+string search_for_file(const string &filename)
+{
+       // Look for the file in all theme_dirs until we find one;
+       // that will be the permanent resolution of this file, whether
+       // it is actually valid or not.
+       // We store errors from all the attempts, and show them
+       // once we know we can't find any of them.
+       vector<string> errors;
+       for (const string &dir : global_flags.theme_dirs) {
+               string pathname = dir + "/" + filename;
+               if (access(pathname.c_str(), O_RDONLY) == 0) {
+                       return pathname;
+               } else {
+                       char buf[512];
+                       snprintf(buf, sizeof(buf), "%s: %s", pathname.c_str(), strerror(errno));
+                       errors.push_back(buf);
+               }
+       }
+
+       for (const string &error : errors) {
+               fprintf(stderr, "%s\n", error.c_str());
+       }
+       fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n",
+               filename.c_str());
+       exit(1);
+}
+
+}  // namespace
+
 ImageInput::ImageInput(const string &filename)
        : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
-                          GL_UNSIGNED_BYTE, 1280, 720)  // FIXME
+                          GL_UNSIGNED_BYTE, 1280, 720),  // FIXME
+         pathname(search_for_file(filename)),
+         current_image(load_image(pathname))
 {
-       const uint8_t *pixel_data = load_image(filename);
-       if (pixel_data == nullptr) {
+       if (current_image == nullptr) {  // Could happen even though search_for_file() returned.
                fprintf(stderr, "Couldn't load image, exiting.\n");
                exit(1);
        }
-       set_pixel_data(pixel_data);
+       set_pixel_data(current_image->pixels.get());
 }
 
-// Some helpers to make RAII versions of FFmpeg objects.
-// The cleanup functions don't interact all that well with unique_ptr,
-// so things get a bit messy and verbose, but overall it's worth it to ensure
-// we never leak things by accident in error paths.
-
-namespace {
-
-void avformat_close_input_unique(AVFormatContext *format_ctx)
+void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
 {
-       avformat_close_input(&format_ctx);
+       // See if the background thread has given us a new version of our image.
+       // Note: The old version might still be lying around in other ImageInputs
+       // (in fact, it's likely), but at least the total amount of memory used
+       // is bounded. Currently we don't even share textures between them,
+       // so there's a fair amount of OpenGL memory waste anyway (the cache
+       // is mostly there to save startup time, not RAM).
+       {
+               unique_lock<mutex> lock(all_images_lock);
+               if (all_images[pathname] != current_image) {
+                       current_image = all_images[pathname];
+                       set_pixel_data(current_image->pixels.get());
+               }
+       }
+       movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num);
 }
 
-unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>
-avformat_open_input_unique(const char *filename,
-                           AVInputFormat *fmt, AVDictionary **options)
+shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &pathname)
 {
-       AVFormatContext *format_ctx = nullptr;
-       if (avformat_open_input(&format_ctx, filename, fmt, options) != 0) {
-               format_ctx = nullptr;
+       unique_lock<mutex> lock(all_images_lock);  // Held also during loading.
+       if (all_images.count(pathname)) {
+               return all_images[pathname];
        }
-       return unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>(
-               format_ctx, avformat_close_input_unique);
-}
 
-void av_frame_free_unique(AVFrame *frame)
-{
-       av_frame_free(&frame);
-}
+       all_images[pathname] = load_image_raw(pathname);
+       timespec first_modified = all_images[pathname]->last_modified;
+       update_threads[pathname] =
+               thread(bind(update_thread_func, pathname, first_modified));
 
-unique_ptr<AVFrame, decltype(av_frame_free_unique)*>
-av_frame_alloc_unique()
-{
-       AVFrame *frame = av_frame_alloc();
-       return unique_ptr<AVFrame, decltype(av_frame_free_unique)*>(
-               frame, av_frame_free_unique);
+       return all_images[pathname];
 }
 
-}  // namespace
-
-const uint8_t *ImageInput::load_image(const string &filename)
+shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pathname)
 {
-       if (all_images.count(filename)) {
-               return all_images[filename].get();
+       // Note: Call before open, not after; otherwise, there's a race.
+       // (There is now, too, but it tips the correct way. We could use fstat()
+       // if we had the file descriptor.)
+       struct stat buf;
+       if (stat(pathname.c_str(), &buf) != 0) {
+               fprintf(stderr, "%s: Error stat-ing file\n", pathname.c_str());
+               return nullptr;
        }
+       timespec last_modified = buf.st_mtim;
 
-       auto format_ctx = avformat_open_input_unique(filename.c_str(), nullptr, nullptr);
+       auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr);
        if (format_ctx == nullptr) {
-               fprintf(stderr, "%s: Error opening file\n", filename.c_str());
+               fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
                return nullptr;
        }
 
        if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
-               fprintf(stderr, "%s: Error finding stream info\n", filename.c_str());
+               fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
                return nullptr;
        }
 
        int stream_index = -1;
        for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
-               if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+               if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
                        stream_index = i;
                        break;
                }
        }
        if (stream_index == -1) {
-               fprintf(stderr, "%s: No video stream found\n", filename.c_str());
+               fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
                return nullptr;
        }
 
-       AVCodecContext *codec_ctx = format_ctx->streams[stream_index]->codec;
-       AVCodec *codec = avcodec_find_decoder(codec_ctx->codec_id);
+       const AVCodecParameters *codecpar = format_ctx->streams[stream_index]->codecpar;
+       AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr);
+       if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) {
+               fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str());
+               return nullptr;
+       }
+       AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
        if (codec == nullptr) {
-               fprintf(stderr, "%s: Cannot find decoder\n", filename.c_str());
+               fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
                return nullptr;
        }
-       if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
-               fprintf(stderr, "%s: Cannot open decoder\n", filename.c_str());
+       if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
+               fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
                return nullptr;
        }
        unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
-               codec_ctx, avcodec_close);
+               codec_ctx.get(), avcodec_close);
 
-       // Read packets until we have a frame.
+       // Read packets until we have a frame or there are none left.
        int frame_finished = 0;
-       auto frame = av_frame_alloc_unique();
+       AVFrameWithDeleter frame = av_frame_alloc_unique();
+       bool eof = false;
        do {
                AVPacket pkt;
                unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
@@ -115,19 +164,32 @@ const uint8_t *ImageInput::load_image(const string &filename)
                av_init_packet(&pkt);
                pkt.data = nullptr;
                pkt.size = 0;
-               if (av_read_frame(format_ctx.get(), &pkt) < 0) {
-                       fprintf(stderr, "%s: Cannot read frame\n", filename.c_str());
-                       return nullptr;
-               }
-               if (pkt.stream_index != stream_index) {
-                       continue;
+               if (av_read_frame(format_ctx.get(), &pkt) == 0) {
+                       if (pkt.stream_index != stream_index) {
+                               continue;
+                       }
+                       if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
+                               fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
+                               return nullptr;
+                       }
+               } else {
+                       eof = true;  // Or error, but ignore that for the time being.
                }
 
-               if (avcodec_decode_video2(codec_ctx, frame.get(), &frame_finished, &pkt) < 0) {
-                       fprintf(stderr, "%s: Cannot decode frame\n", filename.c_str());
+               int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
+               if (err == 0) {
+                       frame_finished = true;
+                       break;
+               } else if (err != AVERROR(EAGAIN)) {
+                       fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
                        return nullptr;
                }
-       } while (!frame_finished);
+       } while (!eof);
+
+       if (!frame_finished) {
+               fprintf(stderr, "%s: Decoder did not output frame.\n", pathname.c_str());
+               return nullptr;
+       }
 
        // TODO: Scale down if needed!
        uint8_t *pic_data[4] = {nullptr};
@@ -135,7 +197,7 @@ const uint8_t *ImageInput::load_image(const string &filename)
                &pic_data[0], av_freep);
        int linesizes[4];
        if (av_image_alloc(pic_data, linesizes, frame->width, frame->height, AV_PIX_FMT_RGBA, 1) < 0) {
-               fprintf(stderr, "%s: Could not allocate picture data\n", filename.c_str());
+               fprintf(stderr, "%s: Could not allocate picture data\n", pathname.c_str());
                return nullptr;
        }
        unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(
@@ -144,7 +206,7 @@ const uint8_t *ImageInput::load_image(const string &filename)
                        AV_PIX_FMT_RGBA, SWS_BICUBIC, nullptr, nullptr, nullptr),
                sws_freeContext);
        if (sws_ctx == nullptr) {
-               fprintf(stderr, "%s: Could not create scaler context\n", filename.c_str());
+               fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
                return nullptr;
        }
        sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
@@ -153,8 +215,55 @@ const uint8_t *ImageInput::load_image(const string &filename)
        unique_ptr<uint8_t[]> image_data(new uint8_t[len]);
        av_image_copy_to_buffer(image_data.get(), len, pic_data, linesizes, AV_PIX_FMT_RGBA, frame->width, frame->height, 1);
 
-       all_images[filename] = move(image_data);
-       return all_images[filename].get();
+       shared_ptr<Image> image(new Image{move(image_data), last_modified});
+       return image;
+}
+
+// Fire up a thread to update the image every second.
+// We could do inotify, but this is good enough for now.
+void ImageInput::update_thread_func(const std::string &pathname, const timespec &first_modified)
+{
+       timespec last_modified = first_modified;
+       struct stat buf;
+       for ( ;; ) {
+               sleep(1);
+
+               if (threads_should_quit) {
+                       return;
+               }
+
+               if (stat(pathname.c_str(), &buf) != 0) {
+                       fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", pathname.c_str());
+                       continue;
+               }
+               if (buf.st_mtim.tv_sec == last_modified.tv_sec &&
+                   buf.st_mtim.tv_nsec == last_modified.tv_nsec) {
+                       // Not changed.
+                       continue;
+               }
+               shared_ptr<const Image> image = load_image_raw(pathname);
+               if (image == nullptr) {
+                       fprintf(stderr, "Couldn't load image, leaving the old in place.\n");
+                       continue;
+               }
+               fprintf(stderr, "Loaded new version of %s from disk.\n", pathname.c_str());
+               unique_lock<mutex> lock(all_images_lock);
+               all_images[pathname] = image;
+               last_modified = image->last_modified;
+       }
+}
+
+void ImageInput::shutdown_updaters()
+{
+       // TODO: Kick these out of the sleep before one second?
+       threads_should_quit = true;
+
+       for (auto &it : update_threads) {
+               it.second.join();
+       }
 }
 
-map<string, unique_ptr<uint8_t[]>> ImageInput::all_images;
+mutex ImageInput::all_images_lock;
+map<string, shared_ptr<const ImageInput::Image>> ImageInput::all_images;
+map<string, thread> ImageInput::update_threads;
+volatile bool ImageInput::threads_should_quit = false;