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