2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gst_loader.c: Load GStreamer videos as gdkpixbufs
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include "gstgdkanimation.h"
27 #include <gst/gstinfo.h>
29 #include <sys/types.h>
36 /* stuff gdk throws at us and we're supposed to keep */
37 GdkPixbufModuleSizeFunc size_func;
38 GdkPixbufModulePreparedFunc prepared_func;
39 GdkPixbufModuleUpdatedFunc updated_func;
41 /* our own stuff - we're much better at keeping fields small :p */
46 GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
47 #define GST_CAT_DEFAULT gst_loader_debug
50 gst_loader_init (GError ** error)
52 static gboolean inited = FALSE;
57 if (!g_thread_supported ()) {
58 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
59 "The GStreamer loader requires threading support.");
63 if (!gst_init_check (0, NULL)) {
64 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
65 "GStreamer could not be initialized.");
70 GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0,
71 "entry point debugging for the GStreamer gdk pixbuf loader");
76 gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func,
77 GdkPixbufModulePreparedFunc prepared_func,
78 GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data,
81 GstLoaderContext *context;
83 if (!gst_loader_init (error))
86 context = g_new (GstLoaderContext, 1);
87 context->size_func = size_func;
88 context->prepared_func = prepared_func;
89 context->updated_func = updated_func;
90 context->user_data = user_data;
91 context->ani = gst_gdk_animation_new (error);
92 context->initialized = FALSE;
95 GST_WARNING ("creating animation failed");
99 context->ani->temp_fd =
100 g_file_open_tmp (NULL, &context->ani->temp_location, error);
101 if (context->ani->temp_fd == 0) {
102 g_object_unref (context->ani);
107 GST_LOG_OBJECT (context->ani, "begin loading");
112 gst_loader_load_increment (gpointer context_pointer, const guchar * buf,
113 guint size, GError ** error)
115 GdkPixbufAnimationIter *iter;
116 GstLoaderContext *context = (GstLoaderContext *) context_pointer;
118 GST_LOG_OBJECT (context->ani, "load increment: %u bytes", size);
119 gst_gdk_animation_add_data (context->ani, buf, size);
120 if (!context->initialized
122 gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (context->ani),
125 gdk_pixbuf_animation_get_width (GDK_PIXBUF_ANIMATION (context->ani));
127 gdk_pixbuf_animation_get_height (GDK_PIXBUF_ANIMATION (context->ani));
129 gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->
132 g_object_unref (iter);
133 GST_LOG_OBJECT (context->ani, "initializing loader");
134 if (context->size_func) {
135 GST_LOG_OBJECT (context->ani, "calling size_func %p", context->size_func);
136 context->size_func (&width, &height, context->user_data);
139 if (context->prepared_func) {
140 GST_LOG_OBJECT (context->ani, "calling prepared_func %p",
141 context->prepared_func);
142 context->prepared_func (pixbuf, GDK_PIXBUF_ANIMATION (context->ani),
146 context->initialized = TRUE;
153 gst_loader_stop_load (gpointer context_pointer, GError ** error)
155 GstLoaderContext *context = (GstLoaderContext *) context_pointer;
157 GST_LOG_OBJECT (context->ani, "stop loading");
158 gst_gdk_animation_done_adding (context->ani);
159 g_object_unref (context->ani);
165 static GdkPixbufAnimation *
166 gst_loader_load_animation (FILE * f, GError ** error)
169 GstGdkAnimation *ani;
170 GdkPixbufAnimationIter *iter = NULL;
172 if (!gst_loader_init (error))
175 GST_LOG ("load_animation");
176 ani = gst_gdk_animation_new (error);
180 filename = g_strdup_printf ("/proc/self/fd/%d", fileno (f));
181 ani->temp_fd = open (filename, 0);
182 if (ani->temp_fd >= 0) {
183 iter = gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (ani), NULL);
185 GST_DEBUG ("open (\"%s\", 0) failed", filename);
189 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
190 "could not create an image");
191 g_object_unref (ani);
192 GST_INFO ("could not create an image");
195 g_object_unref (iter);
196 GST_LOG_OBJECT (ani, "load_animation succeeded");
197 return GDK_PIXBUF_ANIMATION (ani);
200 gst_loader_load (FILE * f, GError ** error)
202 GdkPixbufAnimation *ani;
205 ani = gst_loader_load_animation (f, error);
209 pixbuf = gdk_pixbuf_animation_get_static_image (ani);
211 GST_ERROR_OBJECT (ani, "Could not get an image in _pixbuf_load");
212 g_object_unref (ani);
213 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
214 "Could not get an image from file.");
218 g_object_ref (pixbuf);
219 g_object_unref (ani);
225 fill_vtable (GdkPixbufModule * module)
227 module->load = gst_loader_load;
228 module->begin_load = gst_loader_begin_load;
229 module->load_increment = gst_loader_load_increment;
230 module->stop_load = gst_loader_stop_load;
231 module->load_animation = gst_loader_load_animation;
235 fill_info (GdkPixbufFormat * info)
237 static GdkPixbufModulePattern signature[] = {
239 {"RIFF AVI ", " xxxx ", 100},
241 {"xx\001\272", "zz ", 100},
243 {"xx\001\263", "zz ", 100},
245 {" wide", "xxxx ", 80},
246 {" moov", "xxxx ", 80},
247 {" mdat", "xxxx ", 80},
248 {" pnot", "xxxx ", 80},
249 {" PICT", "xxxx ", 80},
250 {" free", "xxxx ", 80},
252 {"\060\046\262\165\216\146\317\021\246\331 \252 \142\316\154",
257 static gchar *mime_types[] = {
258 "video/avi", "video/x-avi", "video/x-msvideo",
265 static gchar *extensions[] = {
267 "mpeg", "mpe", "mpg",
273 info->name = "GStreamer";
274 info->signature = signature;
275 info->description = "GStreamer supported video";
276 info->mime_types = mime_types;
277 info->extensions = extensions;