1 #include "image_input.h"
5 #include <condition_variable>
11 #include <movit/image_format.h>
12 #include <movit/util.h>
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>
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"
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.
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))
62 if (current_image == nullptr) { // Could happen even though search_for_file() returned.
63 fprintf(stderr, "Couldn't load image, exiting.\n");
66 set_width(current_image->width);
67 set_height(current_image->height);
68 set_texture_num(*current_image->tex);
71 void ImageInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
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).
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);
87 sRGBSwitchingFlatInput::set_gl_state(glsl_program_num, prefix, sampler_num);
90 shared_ptr<const ImageInput::Image> ImageInput::load_image(const string &filename, const string &pathname)
92 lock_guard<mutex> lock(all_images_lock); // Held also during loading.
93 if (all_images.count(pathname)) {
94 return all_images[pathname];
97 all_images[pathname] = load_image_raw(pathname);
98 return all_images[pathname];
101 shared_ptr<const ImageInput::Image> ImageInput::load_image_raw(const string &pathname)
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.)
107 if (stat(pathname.c_str(), &buf) != 0) {
108 fprintf(stderr, "%s: Error stat-ing file\n", pathname.c_str());
111 timespec last_modified = buf.st_mtim;
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());
119 if (avformat_find_stream_info(format_ctx.get(), nullptr) < 0) {
120 fprintf(stderr, "%s: Error finding stream info\n", pathname.c_str());
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());
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());
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());
141 if (avcodec_open2(codec_ctx.get(), codec, nullptr) < 0) {
142 fprintf(stderr, "%s: Cannot open decoder\n", pathname.c_str());
145 unique_ptr<AVCodecContext, decltype(avcodec_close)*> codec_ctx_cleanup(
146 codec_ctx.get(), avcodec_close);
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();
153 AVPacketWithDeleter pkt = av_packet_alloc_unique();
156 if (av_read_frame(format_ctx.get(), pkt.get()) == 0) {
157 if (pkt->stream_index != stream_index) {
160 if (avcodec_send_packet(codec_ctx.get(), pkt.get()) < 0) {
161 fprintf(stderr, "%s: Cannot send packet to codec.\n", pathname.c_str());
165 eof = true; // Or error, but ignore that for the time being.
168 int err = avcodec_receive_frame(codec_ctx.get(), frame.get());
170 frame_finished = true;
172 } else if (err != AVERROR(EAGAIN)) {
173 fprintf(stderr, "%s: Cannot receive frame from codec.\n", pathname.c_str());
178 if (!frame_finished) {
179 fprintf(stderr, "%s: Decoder did not output frame.\n", pathname.c_str());
183 uint8_t *pic_data[4] = {nullptr};
184 unique_ptr<uint8_t *, decltype(av_freep)*> pic_data_cleanup(
185 &pic_data[0], av_freep);
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());
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),
196 if (sws_ctx == nullptr) {
197 fprintf(stderr, "%s: Could not create scaler context\n", pathname.c_str());
200 sws_scale(sws_ctx.get(), frame->data, frame->linesize, 0, frame->height, pic_data, linesizes);
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);
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.
209 glGenTextures(1, &tex);
211 glBindTexture(GL_TEXTURE_2D, tex);
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
215 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
221 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
223 glPixelStorei(GL_UNPACK_ROW_LENGTH, linesizes[0] / 4);
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());
227 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
230 glGenerateMipmap(GL_TEXTURE_2D);
232 glBindTexture(GL_TEXTURE_2D, 0);
235 shared_ptr<Image> image(new Image{unsigned(frame->width), unsigned(frame->height), UniqueTexture(new GLuint(tex)), last_modified});
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)
243 pthread_setname_np(pthread_self(), "Update_Images");
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");
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; });
258 if (update_thread_should_quit) {
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;
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);
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;
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());
283 if (buf.st_mtim.tv_sec == last_modified.tv_sec &&
284 buf.st_mtim.tv_nsec == last_modified.tv_nsec) {
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");
295 unique_lock<mutex> lock(all_images_lock);
296 all_images[pathname] = image;
301 void ImageInput::switch_image(const string &pathname)
304 lock_guard<mutex> lock(all_images_lock);
305 assert(all_images.count(pathname));
307 this->pathname = pathname;
310 void ImageInput::start_update_thread(QSurface *surface)
312 update_thread = thread(update_thread_func, surface);
315 void ImageInput::end_update_thread()
319 lock_guard<mutex> lock(update_thread_should_quit_mu);
320 update_thread_should_quit = true;
321 update_thread_should_quit_modified.notify_all();
323 update_thread.join();
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;