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