]> git.sesse.net Git - nageru/blob - image_input.cpp
Let settings follow buses when editing the mapping.
[nageru] / image_input.cpp
1 #include "image_input.h"
2
3 #include <movit/image_format.h>
4
5 extern "C" {
6 #include <libavcodec/avcodec.h>
7 #include <libavformat/avformat.h>
8 #include <libavutil/imgutils.h>
9 #include <libavutil/pixfmt.h>
10 #include <libswscale/swscale.h>
11 }
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17
18 #include <mutex>
19 #include <thread>
20
21 #include "ffmpeg_raii.h"
22 #include "flags.h"
23
24 using namespace std;
25
26 namespace {
27
28 string search_for_file(const string &filename)
29 {
30         // Look for the file in all theme_dirs until we find one;
31         // that will be the permanent resolution of this file, whether
32         // it is actually valid or not.
33         // We store errors from all the attempts, and show them
34         // once we know we can't find any of them.
35         vector<string> errors;
36         for (const string &dir : global_flags.theme_dirs) {
37                 string pathname = dir + "/" + filename;
38                 if (access(pathname.c_str(), O_RDONLY) == 0) {
39                         return pathname;
40                 } else {
41                         char buf[512];
42                         snprintf(buf, sizeof(buf), "%s: %s", pathname.c_str(), strerror(errno));
43                         errors.push_back(buf);
44                 }
45         }
46
47         for (const string &error : errors) {
48                 fprintf(stderr, "%s\n", error.c_str());
49         }
50         fprintf(stderr, "Couldn't find %s in any directory in --theme-dirs, exiting.\n",
51                 filename.c_str());
52         exit(1);
53 }
54
55 }  // namespace
56
57 ImageInput::ImageInput(const string &filename)
58         : movit::FlatInput({movit::COLORSPACE_sRGB, movit::GAMMA_sRGB}, movit::FORMAT_RGBA_POSTMULTIPLIED_ALPHA,
59                            GL_UNSIGNED_BYTE, 1280, 720),  // FIXME
60           pathname(search_for_file(filename)),
61           current_image(load_image(pathname))
62 {
63         if (current_image == nullptr) {  // Could happen even though search_for_file() returned.
64                 fprintf(stderr, "Couldn't load image, exiting.\n");
65                 exit(1);
66         }
67         set_pixel_data(current_image->pixels.get());
68 }
69
70 void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
71 {
72         // See if the background thread has given us a new version of our image.
73         // Note: The old version might still be lying around in other ImageInputs
74         // (in fact, it's likely), but at least the total amount of memory used
75         // is bounded. Currently we don't even share textures between them,
76         // so there's a fair amount of OpenGL memory waste anyway (the cache
77         // is mostly there to save startup time, not RAM).
78         {
79                 unique_lock<mutex> lock(all_images_lock);
80                 if (all_images[pathname] != current_image) {
81                         current_image = all_images[pathname];
82                         set_pixel_data(current_image->pixels.get());
83                 }
84         }
85         movit::FlatInput::set_gl_state(glsl_program_num, prefix, sampler_num);
86 }
87
88 shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &pathname)
89 {
90         unique_lock<mutex> lock(all_images_lock);  // Held also during loading.
91         if (all_images.count(pathname)) {
92                 return all_images[pathname];
93         }
94
95         all_images[pathname] = load_image_raw(pathname);
96         timespec first_modified = all_images[pathname]->last_modified;
97         update_threads[pathname] =
98                 thread(bind(update_thread_func, pathname, first_modified));
99
100         return all_images[pathname];
101 }
102
103 shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pathname)
104 {
105         // Note: Call before open, not after; otherwise, there's a race.
106         // (There is now, too, but it tips the correct way. We could use fstat()
107         // if we had the file descriptor.)
108         struct stat buf;
109         if (stat(pathname.c_str(), &buf) != 0) {
110                 fprintf(stderr, "%s: Error stat-ing file\n", pathname.c_str());
111                 return nullptr;
112         }
113         timespec last_modified = buf.st_mtim;
114
115         auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr);
116         if (format_ctx == nullptr) {
117                 fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
118                 return nullptr;
119         }
120
121         if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
122                 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
123                 return nullptr;
124         }
125
126         int stream_index = -1;
127         for (unsigned i = 0; i < format_ctx->nb_streams; ++i) {
128                 if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
129                         stream_index = i;
130                         break;
131                 }
132         }
133         if (stream_index == -1) {
134                 fprintf(stderr, "%s: No video stream found\n", pathname.c_str());
135                 return nullptr;
136         }
137
138         const AVCodecParameters *codecpar = format_ctx->streams[stream_index]->codecpar;
139         AVCodecContextWithDeleter codec_ctx = avcodec_alloc_context3_unique(nullptr);
140         if (avcodec_parameters_to_context(codec_ctx.get(), codecpar) < 0) {
141                 fprintf(stderr, "%s: Cannot fill codec parameters\n", pathname.c_str());
142                 return nullptr;
143         }
144         AVCodec *codec = avcodec_find_decoder(codecpar->codec_id);
145         if (codec == nullptr) {
146                 fprintf(stderr, "%s: Cannot find decoder\n", pathname.c_str());
147                 return nullptr;
148         }
149         if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
150                 fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
151                 return nullptr;
152         }
153         unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
154                 codec_ctx.get(), avcodec_close);
155
156         // Read packets until we have a frame or there are none left.
157         int frame_finished = 0;
158         AVFrameWithDeleter frame = av_frame_alloc_unique();
159         bool eof = false;
160         do {
161                 AVPacket pkt;
162                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
163                         &pkt, av_packet_unref);
164                 av_init_packet(&pkt);
165                 pkt.data = nullptr;
166                 pkt.size = 0;
167                 if (av_read_frame(format_ctx.get(), &pkt) == 0) {
168                         if (pkt.stream_index != stream_index) {
169                                 continue;
170                         }
171                         if (avcodec_send_packet(codec_ctx.get(), &pkt) < 0) {
172                                 fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
173                                 return nullptr;
174                         }
175                 } else {
176                         eof = true;  // Or error, but ignore that for the time being.
177                 }
178
179                 int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
180                 if (err == 0) {
181                         frame_finished = true;
182                         break;
183                 } else if (err != AVERROR(EAGAIN)) {
184                         fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
185                         return nullptr;
186                 }
187         } while (!eof);
188
189         if (!frame_finished) {
190                 fprintf(stderr, "%s: Decoder did not output frame.\n", pathname.c_str());
191                 return nullptr;
192         }
193
194         // TODO: Scale down if needed!
195         uint8_t *pic_data[4] = {nullptr};
196         unique_ptr<uint8_t *, decltype(av_freep)*> pic_data_cleanup(
197                 &pic_data[0], av_freep);
198         int linesizes[4];
199         if (av_image_alloc(pic_data, linesizes, frame->width, frame->height, AV_PIX_FMT_RGBA, 1) < 0) {
200                 fprintf(stderr, "%s: Could not allocate picture data\n", pathname.c_str());
201                 return nullptr;
202         }
203         unique_ptr<SwsContext, decltype(sws_freeContext)*> sws_ctx(
204                 sws_getContext(frame->width, frame->height,
205                         (AVPixelFormat)frame->format, frame->width, frame->height,
206                         AV_PIX_FMT_RGBA, SWS_BICUBIC, nullptr, nullptr, nullptr),
207                 sws_freeContext);
208         if (sws_ctx == nullptr) {
209                 fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
210                 return nullptr;
211         }
212         sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
213
214         size_t len = frame->width * frame->height * 4;
215         unique_ptr<uint8_t[]> image_data(new uint8_t[len]);
216         av_image_copy_to_buffer(image_data.get(), len, pic_data, linesizes, AV_PIX_FMT_RGBA, frame->width, frame->height, 1);
217
218         shared_ptr<Image> image(new Image{move(image_data), last_modified});
219         return image;
220 }
221
222 // Fire up a thread to update the image every second.
223 // We could do inotify, but this is good enough for now.
224 void ImageInput::update_thread_func(const std::string &pathname, const timespec &first_modified)
225 {
226         timespec last_modified = first_modified;
227         struct stat buf;
228         for ( ;; ) {
229                 sleep(1);
230
231                 if (threads_should_quit) {
232                         return;
233                 }
234
235                 if (stat(pathname.c_str(), &buf) != 0) {
236                         fprintf(stderr, "%s: Couldn't check for new version, leaving the old in place.\n", pathname.c_str());
237                         continue;
238                 }
239                 if (buf.st_mtim.tv_sec == last_modified.tv_sec &&
240                     buf.st_mtim.tv_nsec == last_modified.tv_nsec) {
241                         // Not changed.
242                         continue;
243                 }
244                 shared_ptr<const Image> image = load_image_raw(pathname);
245                 if (image == nullptr) {
246                         fprintf(stderr, "Couldn't load image, leaving the old in place.\n");
247                         continue;
248                 }
249                 fprintf(stderr, "Loaded new version of %s from disk.\n", pathname.c_str());
250                 unique_lock<mutex> lock(all_images_lock);
251                 all_images[pathname] = image;
252                 last_modified = image->last_modified;
253         }
254 }
255
256 void ImageInput::shutdown_updaters()
257 {
258         // TODO: Kick these out of the sleep before one second?
259         threads_should_quit = true;
260
261         for (auto &it : update_threads) {
262                 it.second.join();
263         }
264 }
265
266 mutex ImageInput::all_images_lock;
267 map<string, shared_ptr<const ImageInput::Image>> ImageInput::all_images;
268 map<string, thread> ImageInput::update_threads;
269 volatile bool ImageInput::threads_should_quit = false;