]> git.sesse.net Git - nageru/blobdiff - image_input.cpp
Name the ImageInput update threads, too.
[nageru] / image_input.cpp
index c9f74f8847c4b5f297c512de57c6d9eed1f7201f..9f474c94b14ae2b8bf315e0d0a4b2308f153b8f3 100644 (file)
@@ -1,24 +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,8 +71,9 @@ 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
+         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");
@@ -84,7 +100,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)) {
@@ -94,55 +110,11 @@ 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];
 }
 
-// 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)
-{
-       avformat_close_input(&format_ctx);
-}
-
-unique_ptr<AVFormatContext, decltype(avformat_close_input_unique)*>
-avformat_open_input_unique(const char *pathname,
-                           AVInputFormat *fmt, AVDictionary **options)
-{
-       AVFormatContext *format_ctx = nullptr;
-       if (avformat_open_input(&format_ctx, pathname, fmt, options) != 0) {
-               format_ctx = nullptr;
-       }
-       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);
-}
-
-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);
-}
-
-void avcodec_free_context_unique(AVCodecContext *codec_ctx)
-{
-       avcodec_free_context(&codec_ctx);
-}
-
-}  // namespace
-
 shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pathname)
 {
        // Note: Call before open, not after; otherwise, there's a race.
@@ -179,10 +151,8 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
        }
 
        const AVCodecParameters *codecpar = format_ctx->streams[stream_index]->codecpar;
-       AVCodecContext *codec_ctx = avcodec_alloc_context3(nullptr);
-       unique_ptr<AVCodecContext, decltype(avcodec_free_context_unique)*> codec_ctx_free(
-               codec_ctx, avcodec_free_context_unique);
-       if (avcodec_parameters_to_context(codec_ctx, codecpar) < 0) {
+       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;
        }
@@ -191,16 +161,16 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
                return nullptr;
        }
-       if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
+       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 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;
@@ -213,7 +183,7 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                        if (pkt.stream_index != stream_index) {
                                continue;
                        }
-                       if (avcodec_send_packet(codec_ctx, &pkt) < 0) {
+                       if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
                                fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
                                return nullptr;
                        }
@@ -221,7 +191,7 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
                        eof = true;  // Or error, but ignore that for the time being.
                }
 
-               int err = avcodec_receive_frame(codec_ctx, frame.get());
+               int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
                if (err == 0) {
                        frame_finished = true;
                        break;
@@ -266,8 +236,12 @@ shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pat
 
 // 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 ( ;; ) {