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