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"
24 #include <gdk-pixbuf/gdk-pixbuf.h>
25 #include <glib-object.h>
27 #include <libffmpegthumbnailer/ffmpegthumbnailertypes.h>
28 #include <libffmpegthumbnailer/imagetypes.h>
29 #include <libffmpegthumbnailer/videothumbnailerc.h> //TODO Use videothumbnailer.h?
35 #include "image-load.h"
41 struct ImageLoaderFT : public ImageLoaderBackend
44 ~ImageLoaderFT() override;
46 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
47 void set_size(int width, int height) override;
48 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
49 GdkPixbuf *get_pixbuf() override;
50 gchar *get_format_name() override;
51 gchar **get_format_mime_types() override;
54 AreaUpdatedCb area_updated_cb;
55 SizePreparedCb size_prepared_cb;
56 AreaPreparedCb area_prepared_cb;
59 video_thumbnailer *vt;
62 guint requested_width;
63 guint requested_height;
66 #if HAVE_FFMPEGTHUMBNAILER_RGB
67 void image_loader_ft_log_cb(ThumbnailerLogLevel log_level, const char* msg)
69 if (log_level == ThumbnailerLogLevelError)
70 log_printf("ImageLoaderFFmpegthumbnailer: %s",msg);
72 DEBUG_1("ImageLoaderFFmpegthumbnailer: %s",msg);
76 void image_loader_ft_destroy_image_data(guchar *, gpointer data)
78 auto image = static_cast<image_data *>(data);
80 video_thumbnailer_destroy_image_data (image);
83 gchar *ImageLoaderFT::get_format_name()
85 return g_strdup("ffmpeg");
88 gchar **ImageLoaderFT::get_format_mime_types()
90 static const gchar *mime[] = {"video/mp4", nullptr};
91 return g_strdupv(const_cast<gchar **>(mime));
94 void ImageLoaderFT::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
96 this->area_updated_cb = area_updated_cb;
97 this->size_prepared_cb = size_prepared_cb;
98 this->area_prepared_cb = area_prepared_cb;
101 vt = video_thumbnailer_create();
102 vt->overlay_film_strip = 1;
103 vt->maintain_aspect_ratio = 1;
104 #if HAVE_FFMPEGTHUMBNAILER_RGB
105 video_thumbnailer_set_log_callback(vt, image_loader_ft_log_cb);
109 void ImageLoaderFT::set_size(int width, int height)
111 requested_width = width;
112 requested_height = height;
113 DEBUG_1("TG: setting size, w=%d, h=%d", width, height);
116 gboolean ImageLoaderFT::write(const guchar *, gsize &chunk_size, gsize count, GError **)
118 auto il = static_cast<ImageLoader *>(data);
120 image_data *image = video_thumbnailer_create_image_data();
122 #if HAVE_FFMPEGTHUMBNAILER_WH
123 video_thumbnailer_set_size(vt, requested_width, requested_height);
125 vt->thumbnail_size = MAX(requested_width, requested_height);
128 #if HAVE_FFMPEGTHUMBNAILER_METADATA
129 vt->prefer_embedded_metadata = options->thumbnails.use_ft_metadata ? 1 : 0;
132 #if HAVE_FFMPEGTHUMBNAILER_RGB
133 vt->thumbnail_image_type = Rgb;
135 vt->thumbnail_image_type = Png;
138 video_thumbnailer_generate_thumbnail_to_buffer (vt, il->fd->path, image);
140 #if HAVE_FFMPEGTHUMBNAILER_RGB
141 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);
142 size_prepared_cb(nullptr, image->image_data_width, image->image_data_height, data);
143 area_updated_cb(nullptr, 0, 0, image->image_data_width, image->image_data_height, data);
145 GInputStream *image_stream;
146 image_stream = g_memory_input_stream_new_from_data (image->image_data_ptr, image->image_data_size, nullptr);
148 if (image_stream == nullptr)
150 video_thumbnailer_destroy_image_data (image);
151 DEBUG_1("FFmpegthumbnailer: cannot open stream for %s", il->fd->path);
155 pixbuf = gdk_pixbuf_new_from_stream (image_stream, nullptr, nullptr);
156 size_prepared_cb(nullptr, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
157 g_object_unref (image_stream);
158 video_thumbnailer_destroy_image_data (image);
163 DEBUG_1("FFmpegthumbnailer: no frame generated for %s", il->fd->path);
167 /** See comment in image_loader_area_prepared_cb
168 * Geeqie uses area_prepared signal to fill pixbuf with background color.
169 * We can't do it here as pixbuf already contains the data
170 * area_prepared_cb(data);
172 area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
178 GdkPixbuf *ImageLoaderFT::get_pixbuf()
183 ImageLoaderFT::~ImageLoaderFT()
185 if (pixbuf) g_object_unref(pixbuf);
186 video_thumbnailer_destroy (vt);
191 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_ft()
193 return std::make_unique<ImageLoaderFT>();
196 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */