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