]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Fix so one can use images with other resolutions than 1280x720.
[nageru] / image_input.cpp
index b8e2b02942b4ea01993131f2ee73acf595479e05..a224af9d47e1d8fc5a0f16ac702f5ce66cbdd7d5 100644 (file)
@@ -1,25 +1,39 @@
 #include "image_input.h"
 
+#include <errno.h>
 #include <movit/image_format.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
+#include <libavutil/avutil.h>
+#include <libavutil/error.h>
+#include <libavutil/frame.h>
 #include <libavutil/imgutils.h>
+#include <libavutil/mem.h>
 #include <libavutil/pixfmt.h>
 #include <libswscale/swscale.h>
 }
 
-#include <sys/types.h>
+#include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <fcntl.h>
-
+#include <cstddef>
+#include <functional>
 #include <mutex>
 #include <thread>
+#include <utility>
+#include <vector>
 
 #include "ffmpeg_raii.h"
 #include "flags.h"
+#include "flat_input.h"
+
+struct SwsContext;
 
 using namespace std;
 
@@ -56,14 +70,17 @@ string search_for_file(const 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
+                          GL_UNSIGNED_BYTE, 1280, 720),  // Resolution will be overwritten.
+         filename(filename),
          pathname(search_for_file(filename)),
-         current_image(load_image(pathname))
+         current_image(load_image(filename, pathname))
 {
        if (current_image == nullptr) {  // Could happen even though search_for_file() returned.
                fprintf(stderr, "Couldn't load image, exiting.\n");
                exit(1);
        }
+       set_width(current_image->width);
+       set_height(current_image->height);
        set_pixel_data(current_image->pixels.get());
 }
 
@@ -85,7 +102,7 @@ void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, uns
        movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num);
 }
 
-shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &pathname)
+shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &filename, const string &pathname)
 {
        unique_lock<mutex> lock(all_images_lock);  // Held also during loading.
        if (all_images.count(pathname)) {
@@ -95,7 +112,7 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &pathnam
        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));
+               thread(bind(update_thread_func, filename, pathname, first_modified));
 
        return all_images[pathname];
 }
@@ -191,7 +208,6 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                return nullptr;
        }
 
-       // TODO: Scale down if needed!
        uint8_t *pic_data[4] = {nullptr};
        unique_ptr<uint8_t *, decltype(av_freep)*> pic_data_cleanup(
                &pic_data[0], av_freep);
@@ -215,14 +231,18 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
        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);
 
-       shared_ptr<Image> image(new Image{move(image_data), last_modified});
+       shared_ptr<Image> image(new Image{unsigned(frame->width), unsigned(frame->height), 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)
+void ImageInput::update_thread_func(const std::string &filename, const std::string &pathname, const timespec &first_modified)
 {
+       char thread_name[16];
+       snprintf(thread_name, sizeof(thread_name), "Update_%s", filename.c_str());
+       pthread_setname_np(pthread_self(), thread_name);
+
        timespec last_modified = first_modified;
        struct stat buf;
        for ( ;; ) {