2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * Author: Tomasz Golinski <tomaszg@math.uwb.edu.pl>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "image-load-ffmpegthumbnailer.h"
26 #if HAVE_FFMPEGTHUMBNAILER
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <glib-object.h>
30 #include <libffmpegthumbnailer/ffmpegthumbnailertypes.h>
31 #include <libffmpegthumbnailer/imagetypes.h>
32 #include <libffmpegthumbnailer/videothumbnailerc.h> //TODO Use videothumbnailer.h?
36 #include "image-load.h"
42 struct ImageLoaderFT : public ImageLoaderBackend
45 ~ImageLoaderFT() override;
47 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
48 void set_size(int width, int height) override;
49 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
50 GdkPixbuf *get_pixbuf() override;
51 gchar *get_format_name() override;
52 gchar **get_format_mime_types() override;
55 AreaUpdatedCb area_updated_cb;
56 SizePreparedCb size_prepared_cb;
57 AreaPreparedCb area_prepared_cb;
60 video_thumbnailer *vt;
63 guint requested_width;
64 guint requested_height;
67 #if HAVE_FFMPEGTHUMBNAILER_RGB
68 void image_loader_ft_log_cb(ThumbnailerLogLevel log_level, const char* msg)
70 if (log_level == ThumbnailerLogLevelError)
71 log_printf("ImageLoaderFFmpegthumbnailer: %s",msg);
73 DEBUG_1("ImageLoaderFFmpegthumbnailer: %s",msg);
77 void image_loader_ft_destroy_image_data(guchar *, gpointer data)
79 auto image = static_cast<image_data *>(data);
81 video_thumbnailer_destroy_image_data (image);
84 gchar *ImageLoaderFT::get_format_name()
86 return g_strdup("ffmpeg");
89 gchar **ImageLoaderFT::get_format_mime_types()
91 static const gchar *mime[] = {"video/mp4", nullptr};
92 return g_strdupv(const_cast<gchar **>(mime));
95 void ImageLoaderFT::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
97 this->area_updated_cb = area_updated_cb;
98 this->size_prepared_cb = size_prepared_cb;
99 this->area_prepared_cb = area_prepared_cb;
102 vt = video_thumbnailer_create();
103 vt->overlay_film_strip = 1;
104 vt->maintain_aspect_ratio = 1;
105 #if HAVE_FFMPEGTHUMBNAILER_RGB
106 video_thumbnailer_set_log_callback(vt, image_loader_ft_log_cb);
110 void ImageLoaderFT::set_size(int width, int height)
112 requested_width = width;
113 requested_height = height;
114 DEBUG_1("TG: setting size, w=%d, h=%d", width, height);
117 gboolean ImageLoaderFT::write(const guchar *, gsize &chunk_size, gsize count, GError **)
119 auto il = static_cast<ImageLoader *>(data);
121 image_data *image = video_thumbnailer_create_image_data();
123 #if HAVE_FFMPEGTHUMBNAILER_WH
124 video_thumbnailer_set_size(vt, requested_width, requested_height);
126 vt->thumbnail_size = MAX(requested_width, requested_height);
129 #if HAVE_FFMPEGTHUMBNAILER_METADATA
130 vt->prefer_embedded_metadata = options->thumbnails.use_ft_metadata ? 1 : 0;
133 #if HAVE_FFMPEGTHUMBNAILER_RGB
134 vt->thumbnail_image_type = Rgb;
136 vt->thumbnail_image_type = Png;
139 video_thumbnailer_generate_thumbnail_to_buffer (vt, il->fd->path, image);
141 #if HAVE_FFMPEGTHUMBNAILER_RGB
142 pixbuf = gdk_pixbuf_new_from_data (image->image_data_ptr, GDK_COLORSPACE_RGB, FALSE, 8, image->image_data_width, image->image_data_height, image->image_data_width*3, image_loader_ft_destroy_image_data, image);
143 size_prepared_cb(nullptr, image->image_data_width, image->image_data_height, data);
144 area_updated_cb(nullptr, 0, 0, image->image_data_width, image->image_data_height, data);
146 GInputStream *image_stream;
147 image_stream = g_memory_input_stream_new_from_data (image->image_data_ptr, image->image_data_size, nullptr);
149 if (image_stream == nullptr)
151 video_thumbnailer_destroy_image_data (image);
152 DEBUG_1("FFmpegthumbnailer: cannot open stream for %s", il->fd->path);
156 pixbuf = gdk_pixbuf_new_from_stream (image_stream, nullptr, nullptr);
157 size_prepared_cb(nullptr, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
158 g_object_unref (image_stream);
159 video_thumbnailer_destroy_image_data (image);
164 DEBUG_1("FFmpegthumbnailer: no frame generated for %s", il->fd->path);
168 /** See comment in image_loader_area_prepared_cb
169 * Geeqie uses area_prepared signal to fill pixbuf with background color.
170 * We can't do it here as pixbuf already contains the data
171 * area_prepared_cb(data);
173 area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
179 GdkPixbuf *ImageLoaderFT::get_pixbuf()
184 ImageLoaderFT::~ImageLoaderFT()
186 if (pixbuf) g_object_unref(pixbuf);
187 video_thumbnailer_destroy (vt);
192 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_ft()
194 return std::make_unique<ImageLoaderFT>();
198 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */