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>
35 /* stuff gdk throws at us and we're supposed to keep */
36 GdkPixbufModuleSizeFunc size_func;
37 GdkPixbufModulePreparedFunc prepared_func;
38 GdkPixbufModuleUpdatedFunc updated_func;
40 /* our own stuff - we're much better at keeping fields small :p */
41 GstGdkAnimation * ani;
45 GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
46 #define GST_CAT_DEFAULT gst_loader_debug
49 gst_loader_init (GError **error)
51 static gboolean inited = FALSE;
56 if (!g_thread_supported ()) {
57 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
58 "The GStreamer loader requires threading support.");
62 if (!gst_init_check (0, NULL)) {
63 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
64 "GStreamer could not be initialized.");
69 GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0, "entry point debugging for the GStreamer gdk pixbuf loader");
73 gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func, GdkPixbufModulePreparedFunc prepared_func,
74 GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data, GError **error)
76 GstLoaderContext *context;
78 if (!gst_loader_init (error))
81 context = g_new (GstLoaderContext, 1);
82 context->size_func = size_func;
83 context->prepared_func = prepared_func;
84 context->updated_func = updated_func;
85 context->user_data = user_data;
86 context->ani = gst_gdk_animation_new (error);
87 context->initialized = FALSE;
90 GST_WARNING ("creating animation failed");
94 context->ani->temp_fd = g_file_open_tmp (NULL, &context->ani->temp_location, error);
95 if (context->ani->temp_fd == 0) {
96 g_object_unref (context->ani);
101 GST_LOG_OBJECT (context->ani, "begin loading");
105 gst_loader_load_increment (gpointer context_pointer, const guchar *buf, guint size, GError **error)
107 GdkPixbufAnimationIter *iter;
108 GstLoaderContext *context = (GstLoaderContext *) context_pointer;
110 GST_LOG_OBJECT (context->ani, "load increment: %u bytes", size);
111 gst_gdk_animation_add_data (context->ani, buf, size);
112 if (!context->initialized && (iter = gdk_pixbuf_animation_get_iter (
113 GDK_PIXBUF_ANIMATION (context->ani), NULL)) != NULL) {
114 int width = gdk_pixbuf_animation_get_width (GDK_PIXBUF_ANIMATION (context->ani));
115 int height = gdk_pixbuf_animation_get_height (GDK_PIXBUF_ANIMATION (context->ani));
116 GdkPixbuf *pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->ani));
118 g_object_unref (iter);
119 GST_LOG_OBJECT (context->ani, "initializing loader");
120 if (context->size_func) {
121 GST_LOG_OBJECT (context->ani, "calling size_func %p", context->size_func);
122 context->size_func (&width, &height, context->user_data);
125 if (context->prepared_func) {
126 GST_LOG_OBJECT (context->ani, "calling prepared_func %p", context->prepared_func);
127 context->prepared_func (pixbuf, GDK_PIXBUF_ANIMATION (context->ani), context->user_data);
130 context->initialized = TRUE;
136 gst_loader_stop_load (gpointer context_pointer, GError **error)
138 GstLoaderContext *context = (GstLoaderContext *) context_pointer;
140 GST_LOG_OBJECT (context->ani, "stop loading");
141 gst_gdk_animation_done_adding (context->ani);
142 g_object_unref (context->ani);
148 static GdkPixbufAnimation *
149 gst_loader_load_animation (FILE *f, GError **error)
152 GstGdkAnimation *ani;
153 GdkPixbufAnimationIter *iter = NULL;
155 if (!gst_loader_init (error))
158 GST_LOG ("load_animation");
159 ani = gst_gdk_animation_new (error);
163 filename = g_strdup_printf ("/proc/self/fd/%d", fileno (f));
164 ani->temp_fd = open (filename, 0);
165 if (ani->temp_fd >= 0) {
166 iter = gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (ani), NULL);
168 GST_DEBUG ("open (\"%s\", 0) failed", filename);
172 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
173 "could not create an image");
174 g_object_unref (ani);
175 GST_INFO ("could not create an image");
178 g_object_unref (iter);
179 GST_LOG_OBJECT (ani, "load_animation succeeded");
180 return GDK_PIXBUF_ANIMATION (ani);
183 gst_loader_load (FILE *f, GError **error)
185 GdkPixbufAnimation *ani;
188 ani = gst_loader_load_animation (f, error);
192 pixbuf = gdk_pixbuf_animation_get_static_image (ani);
194 GST_ERROR_OBJECT (ani, "Could not get an image in _pixbuf_load");
195 g_object_unref (ani);
196 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
197 "Could not get an image from file.");
201 g_object_ref (pixbuf);
202 g_object_unref (ani);
207 fill_vtable (GdkPixbufModule *module)
209 module->load = gst_loader_load;
210 module->begin_load = gst_loader_begin_load;
211 module->load_increment = gst_loader_load_increment;
212 module->stop_load = gst_loader_stop_load;
213 module->load_animation = gst_loader_load_animation;
216 fill_info (GdkPixbufFormat *info)
218 static GdkPixbufModulePattern signature[] = {
220 { "RIFF AVI ", " xxxx ", 100 },
222 { "xx\001\272", "zz ", 100 },
224 { "xx\001\263", "zz ", 100 },
226 { " wide", "xxxx ", 80 },
227 { " moov", "xxxx ", 80 },
228 { " mdat", "xxxx ", 80 },
229 { " pnot", "xxxx ", 80 },
230 { " PICT", "xxxx ", 80 },
231 { " free", "xxxx ", 80 },
233 { "\060\046\262\165\216\146\317\021\246\331 \252 \142\316\154", " z z ", 100 },
237 static gchar *mime_types[] = {
238 "video/avi", "video/x-avi", "video/x-msvideo",
245 static gchar *extensions[] = {
247 "mpeg", "mpe", "mpg",
253 info->name = "GStreamer";
254 info->signature = signature;
255 info->description = "GStreamer supported video";
256 info->mime_types = mime_types;
257 info->extensions = extensions;