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 */
47 GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
48 #define GST_CAT_DEFAULT gst_loader_debug
51 gst_loader_init (GError ** error)
53 static gboolean inited = FALSE;
58 if (!g_thread_supported ()) {
59 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
60 "The GStreamer loader requires threading support.");
64 if (!gst_init_check (0, NULL)) {
65 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
66 "GStreamer could not be initialized.");
71 GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0,
72 "entry point debugging for the GStreamer gdk pixbuf loader");
77 gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func,
78 GdkPixbufModulePreparedFunc prepared_func,
79 GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data,
82 GstLoaderContext *context;
84 if (!gst_loader_init (error))
87 context = g_new (GstLoaderContext, 1);
88 context->size_func = size_func;
89 context->prepared_func = prepared_func;
90 context->updated_func = updated_func;
91 context->user_data = user_data;
92 context->ani = gst_gdk_animation_new (error);
93 context->initialized = FALSE;
96 GST_WARNING ("creating animation failed");
100 context->ani->temp_fd =
101 g_file_open_tmp (NULL, &context->ani->temp_location, error);
102 if (context->ani->temp_fd == 0) {
103 g_object_unref (context->ani);
108 GST_LOG_OBJECT (context->ani, "begin loading");
113 gst_loader_load_increment (gpointer context_pointer, const guchar * buf,
114 guint size, GError ** error)
116 GdkPixbufAnimationIter *iter;
117 GstLoaderContext *context = (GstLoaderContext *) context_pointer;
119 GST_LOG_OBJECT (context->ani, "load increment: %u bytes", size);
120 gst_gdk_animation_add_data (context->ani, buf, size);
121 if (!context->initialized
123 gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (context->ani),
126 gdk_pixbuf_animation_get_width (GDK_PIXBUF_ANIMATION (context->ani));
128 gdk_pixbuf_animation_get_height (GDK_PIXBUF_ANIMATION (context->ani));
130 gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->
133 g_object_unref (iter);
134 GST_LOG_OBJECT (context->ani, "initializing loader");
135 if (context->size_func) {
136 GST_LOG_OBJECT (context->ani, "calling size_func %p", context->size_func);
137 context->size_func (&width, &height, context->user_data);
140 if (context->prepared_func) {
141 GST_LOG_OBJECT (context->ani, "calling prepared_func %p",
142 context->prepared_func);
143 context->prepared_func (pixbuf, GDK_PIXBUF_ANIMATION (context->ani),
147 context->initialized = TRUE;
154 gst_loader_stop_load (gpointer context_pointer, GError ** error)
156 GstLoaderContext *context = (GstLoaderContext *) context_pointer;
158 GST_LOG_OBJECT (context->ani, "stop loading");
159 gst_gdk_animation_done_adding (context->ani);
160 g_object_unref (context->ani);
166 static GdkPixbufAnimation *
167 gst_loader_load_animation (FILE * f, GError ** error)
170 GstGdkAnimation *ani;
171 GdkPixbufAnimationIter *iter = NULL;
173 if (!gst_loader_init (error))
176 GST_LOG ("load_animation");
177 ani = gst_gdk_animation_new (error);
181 filename = g_strdup_printf ("/proc/self/fd/%d", fileno (f));
182 ani->temp_fd = open (filename, 0);
183 if (ani->temp_fd >= 0) {
184 iter = gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (ani), NULL);
186 GST_DEBUG ("open (\"%s\", 0) failed", filename);
190 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
191 "could not create an image");
192 g_object_unref (ani);
193 GST_INFO ("could not create an image");
196 g_object_unref (iter);
197 GST_LOG_OBJECT (ani, "load_animation succeeded");
198 return GDK_PIXBUF_ANIMATION (ani);
201 gst_loader_load (FILE * f, GError ** error)
203 GdkPixbufAnimation *ani;
206 ani = gst_loader_load_animation (f, error);
210 pixbuf = gdk_pixbuf_animation_get_static_image (ani);
212 GST_ERROR_OBJECT (ani, "Could not get an image in _pixbuf_load");
213 g_object_unref (ani);
214 g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
215 "Could not get an image from file.");
219 g_object_ref (pixbuf);
220 g_object_unref (ani);
226 fill_vtable (GdkPixbufModule * module)
228 module->load = gst_loader_load;
229 module->begin_load = gst_loader_begin_load;
230 module->load_increment = gst_loader_load_increment;
231 module->stop_load = gst_loader_stop_load;
232 module->load_animation = gst_loader_load_animation;
236 fill_info (GdkPixbufFormat * info)
238 static GdkPixbufModulePattern signature[] = {
240 {"RIFF AVI ", " xxxx ", 100},
242 {"xx\001\272", "zz ", 100},
244 {"xx\001\263", "zz ", 100},
246 {" wide", "xxxx ", 80},
247 {" moov", "xxxx ", 80},
248 {" mdat", "xxxx ", 80},
249 {" pnot", "xxxx ", 80},
250 {" PICT", "xxxx ", 80},
251 {" free", "xxxx ", 80},
253 {"\060\046\262\165\216\146\317\021\246\331 \252 \142\316\154",
258 static gchar *mime_types[] = {
259 "video/avi", "video/x-avi", "video/x-msvideo",
266 static gchar *extensions[] = {
268 "mpeg", "mpe", "mpg",
274 info->name = "GStreamer";
275 info->signature = signature;
276 info->description = "GStreamer supported video";
277 info->mime_types = mime_types;
278 info->extensions = extensions;