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