Move third-party sources to separate sub-directory
[geeqie.git] / src / image-load-ffmpegthumbnailer.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Tomasz Golinski <tomaszg@math.uwb.edu.pl>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "image-load-ffmpegthumbnailer.h"
23
24 #include <config.h>
25
26 #if HAVE_FFMPEGTHUMBNAILER
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <glib-object.h>
29 #include <glib.h>
30 #include <libffmpegthumbnailer/ffmpegthumbnailertypes.h>
31 #include <libffmpegthumbnailer/imagetypes.h>
32 #include <libffmpegthumbnailer/videothumbnailerc.h> //TODO Use videothumbnailer.h?
33
34 #include "debug.h"
35 #include "filedata.h"
36 #include "image-load.h"
37 #include "options.h"
38
39 namespace
40 {
41
42 struct ImageLoaderFT : public ImageLoaderBackend
43 {
44 public:
45         ~ImageLoaderFT() override;
46
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;
53
54 private:
55         AreaUpdatedCb area_updated_cb;
56         SizePreparedCb size_prepared_cb;
57         AreaPreparedCb area_prepared_cb;
58         gpointer data;
59
60         video_thumbnailer *vt;
61
62         GdkPixbuf *pixbuf;
63         guint requested_width;
64         guint requested_height;
65 };
66
67 #if HAVE_FFMPEGTHUMBNAILER_RGB
68 void image_loader_ft_log_cb(ThumbnailerLogLevel log_level, const char* msg)
69 {
70         if (log_level == ThumbnailerLogLevelError)
71                 log_printf("ImageLoaderFFmpegthumbnailer: %s",msg);
72         else
73                 DEBUG_1("ImageLoaderFFmpegthumbnailer: %s",msg);
74 }
75 #endif
76
77 void image_loader_ft_destroy_image_data(guchar *, gpointer data)
78 {
79         auto image = static_cast<image_data *>(data);
80
81         video_thumbnailer_destroy_image_data (image);
82 }
83
84 gchar *ImageLoaderFT::get_format_name()
85 {
86         return g_strdup("ffmpeg");
87 }
88
89 gchar **ImageLoaderFT::get_format_mime_types()
90 {
91         static const gchar *mime[] = {"video/mp4", nullptr};
92         return g_strdupv(const_cast<gchar **>(mime));
93 }
94
95 void ImageLoaderFT::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
96 {
97         this->area_updated_cb = area_updated_cb;
98         this->size_prepared_cb = size_prepared_cb;
99         this->area_prepared_cb = area_prepared_cb;
100         this->data = data;
101
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);
107 #endif
108 }
109
110 void ImageLoaderFT::set_size(int width, int height)
111 {
112         requested_width = width;
113         requested_height = height;
114         DEBUG_1("TG: setting size, w=%d, h=%d", width, height);
115 }
116
117 gboolean ImageLoaderFT::write(const guchar *, gsize &chunk_size, gsize count, GError **)
118 {
119         auto il = static_cast<ImageLoader *>(data);
120
121         image_data *image = video_thumbnailer_create_image_data();
122
123 #if HAVE_FFMPEGTHUMBNAILER_WH
124         video_thumbnailer_set_size(vt, requested_width, requested_height);
125 #else
126         vt->thumbnail_size = MAX(requested_width, requested_height);
127 #endif
128
129 #if HAVE_FFMPEGTHUMBNAILER_METADATA
130         vt->prefer_embedded_metadata = options->thumbnails.use_ft_metadata ? 1 : 0;
131 #endif
132
133 #if HAVE_FFMPEGTHUMBNAILER_RGB
134         vt->thumbnail_image_type = Rgb;
135 #else
136         vt->thumbnail_image_type = Png;
137 #endif
138
139         video_thumbnailer_generate_thumbnail_to_buffer (vt, il->fd->path, image);
140
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);
145 #else
146         GInputStream *image_stream;
147         image_stream = g_memory_input_stream_new_from_data (image->image_data_ptr, image->image_data_size, nullptr);
148
149         if (image_stream == nullptr)
150                 {
151                 video_thumbnailer_destroy_image_data (image);
152                 DEBUG_1("FFmpegthumbnailer: cannot open stream for %s", il->fd->path);
153                 return FALSE;
154                 }
155
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);
160 #endif
161
162         if (!pixbuf)
163                 {
164                 DEBUG_1("FFmpegthumbnailer: no frame generated for %s", il->fd->path);
165                 return FALSE;
166                 }
167
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);
172          */
173         area_updated_cb(nullptr, 0, 0, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), data);
174
175         chunk_size = count;
176         return TRUE;
177 }
178
179 GdkPixbuf *ImageLoaderFT::get_pixbuf()
180 {
181         return pixbuf;
182 }
183
184 ImageLoaderFT::~ImageLoaderFT()
185 {
186         if (pixbuf) g_object_unref(pixbuf);
187         video_thumbnailer_destroy (vt);
188 }
189
190 } // namespace
191
192 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_ft()
193 {
194         return std::make_unique<ImageLoaderFT>();
195 }
196
197 #endif
198 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */