Let image loader backend decide how to process image buffer
[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 {
43         ImageLoaderBackendCbAreaUpdated area_updated_cb;
44         ImageLoaderBackendCbSize size_cb;
45         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
46
47         video_thumbnailer *vt;
48
49         gpointer data;
50
51         GdkPixbuf *pixbuf;
52         guint requested_width;
53         guint requested_height;
54
55 };
56
57 #if HAVE_FFMPEGTHUMBNAILER_RGB
58 void image_loader_ft_log_cb(ThumbnailerLogLevel log_level, const char* msg)
59 {
60         if (log_level == ThumbnailerLogLevelError)
61                 log_printf("ImageLoaderFFmpegthumbnailer: %s",msg);
62         else
63                 DEBUG_1("ImageLoaderFFmpegthumbnailer: %s",msg);
64 }
65 #endif
66
67 void image_loader_ft_destroy_image_data(guchar *, gpointer data)
68 {
69         auto image = static_cast<image_data *>(data);
70
71         video_thumbnailer_destroy_image_data (image);
72 }
73
74 gchar* image_loader_ft_get_format_name(gpointer)
75 {
76         return g_strdup("ffmpeg");
77 }
78
79 gchar** image_loader_ft_get_format_mime_types(gpointer)
80 {
81         static const gchar *mime[] = {"video/mp4", nullptr};
82         return g_strdupv(const_cast<gchar **>(mime));
83 }
84
85 gpointer image_loader_ft_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
86 {
87         auto loader = g_new0(ImageLoaderFT, 1);
88
89         loader->area_updated_cb = area_updated_cb;
90         loader->size_cb = size_cb;
91         loader->area_prepared_cb = area_prepared_cb;
92         loader->data = data;
93
94         loader->vt = video_thumbnailer_create();
95         loader->vt->overlay_film_strip = 1;
96         loader->vt->maintain_aspect_ratio = 1;
97 #if HAVE_FFMPEGTHUMBNAILER_RGB
98         video_thumbnailer_set_log_callback(loader->vt, image_loader_ft_log_cb);
99 #endif
100
101         return loader;
102 }
103
104 void image_loader_ft_set_size(gpointer loader, int width, int height)
105 {
106         auto lft = static_cast<ImageLoaderFT *>(loader);
107         lft->requested_width = width;
108         lft->requested_height = height;
109         DEBUG_1("TG: setting size, w=%d, h=%d", width, height);
110 }
111
112 gboolean image_loader_ft_write (gpointer loader, const guchar *, gsize &chunk_size, gsize count, GError **)
113 {
114         auto lft = static_cast<ImageLoaderFT *>(loader);
115         auto il = static_cast<ImageLoader *>(lft->data);
116
117         image_data *image = video_thumbnailer_create_image_data();
118
119 #if HAVE_FFMPEGTHUMBNAILER_WH
120         video_thumbnailer_set_size(lft->vt, lft->requested_width, lft->requested_height);
121 #else
122         lft->vt->thumbnail_size = MAX(lft->requested_width,lft->requested_height);
123 #endif
124
125 #if HAVE_FFMPEGTHUMBNAILER_METADATA
126         lft->vt->prefer_embedded_metadata = options->thumbnails.use_ft_metadata ? 1 : 0;
127 #endif
128
129 #if HAVE_FFMPEGTHUMBNAILER_RGB
130         lft->vt->thumbnail_image_type = Rgb;
131 #else
132         lft->vt->thumbnail_image_type = Png;
133 #endif
134
135         video_thumbnailer_generate_thumbnail_to_buffer (lft->vt, il->fd->path, image);
136
137 #if HAVE_FFMPEGTHUMBNAILER_RGB
138         lft->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);
139         lft->size_cb(loader, image->image_data_width, image->image_data_height, lft->data);
140         lft->area_updated_cb(loader, 0, 0, image->image_data_width, image->image_data_height, lft->data);
141 #else
142         GInputStream *image_stream;
143         image_stream = g_memory_input_stream_new_from_data (image->image_data_ptr, image->image_data_size, nullptr);
144
145         if (image_stream == nullptr)
146                 {
147                 video_thumbnailer_destroy_image_data (image);
148                 DEBUG_1("FFmpegthumbnailer: cannot open stream for %s", il->fd->path);
149                 return FALSE;
150                 }
151
152         lft->pixbuf  = gdk_pixbuf_new_from_stream (image_stream, nullptr, nullptr);
153         lft->size_cb(loader, gdk_pixbuf_get_width(lft->pixbuf), gdk_pixbuf_get_height(lft->pixbuf), lft->data);
154         g_object_unref (image_stream);
155         video_thumbnailer_destroy_image_data (image);
156 #endif
157
158         if (!lft->pixbuf)
159                 {
160                 DEBUG_1("FFmpegthumbnailer: no frame generated for %s", il->fd->path);
161                 return FALSE;
162                 }
163
164         /** See comment in image_loader_area_prepared_cb
165          * Geeqie uses area_prepared signal to fill pixbuf with background color.
166          * We can't do it here as pixbuf already contains the data
167          * lft->area_prepared_cb(loader, lft->data);
168          */
169         lft->area_updated_cb(loader, 0, 0, gdk_pixbuf_get_width(lft->pixbuf), gdk_pixbuf_get_height(lft->pixbuf), lft->data);
170
171         chunk_size = count;
172         return TRUE;
173 }
174
175 GdkPixbuf* image_loader_ft_get_pixbuf(gpointer loader)
176 {
177         auto lft = static_cast<ImageLoaderFT *>(loader);
178         return lft->pixbuf;
179 }
180
181 void image_loader_ft_abort(gpointer)
182 {
183 }
184
185 gboolean image_loader_ft_close(gpointer, GError **)
186 {
187         return TRUE;
188 }
189
190 void image_loader_ft_free(gpointer loader)
191 {
192         auto lft = static_cast<ImageLoaderFT *>(loader);
193         if (lft->pixbuf) g_object_unref(lft->pixbuf);
194         video_thumbnailer_destroy (lft->vt);
195
196         g_free(lft);
197 }
198
199 } // namespace
200
201 void image_loader_backend_set_ft(ImageLoaderBackend *funcs)
202 {
203         funcs->loader_new = image_loader_ft_new;
204         funcs->set_size = image_loader_ft_set_size;
205         funcs->write = image_loader_ft_write;
206         funcs->get_pixbuf = image_loader_ft_get_pixbuf;
207         funcs->close = image_loader_ft_close;
208         funcs->abort = image_loader_ft_abort;
209         funcs->free = image_loader_ft_free;
210
211         funcs->get_format_name = image_loader_ft_get_format_name;
212         funcs->get_format_mime_types = image_loader_ft_get_format_mime_types;
213 }
214
215 #endif
216 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */