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