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