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