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