b0538cac7aaf697df516db3aceed07ade677f404
[platform/upstream/gst-plugins-good.git] / ext / gdk_pixbuf / gst_loader.c
1 /*
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gst_loader.c: Load GStreamer videos as gdkpixbufs
5  * 
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstgdkanimation.h"
27 #include <gst/gstinfo.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32
33
34 typedef struct {
35   /* stuff gdk throws at us and we're supposed to keep */
36   GdkPixbufModuleSizeFunc       size_func;
37   GdkPixbufModulePreparedFunc   prepared_func;
38   GdkPixbufModuleUpdatedFunc    updated_func;
39   gpointer                      user_data;
40   /* our own stuff - we're much better at keeping fields small :p */
41   GstGdkAnimation *             ani;
42   gboolean                      initialized;
43 } GstLoaderContext;
44
45 GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
46 #define GST_CAT_DEFAULT gst_loader_debug
47
48 static gboolean
49 gst_loader_init (GError **error)
50 {
51   static gboolean inited = FALSE;
52   
53   if (inited)
54     return TRUE;
55   
56   if (!g_thread_supported ()) {
57     g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
58                  "The GStreamer loader requires threading support.");
59     return FALSE;
60   }
61
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.");
65     return FALSE;
66   }
67
68   inited = TRUE;
69   GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0, "entry point debugging for the GStreamer gdk pixbuf loader");
70   return TRUE;
71 }
72 static gpointer
73 gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func, GdkPixbufModulePreparedFunc prepared_func,
74                        GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data, GError **error)
75 {
76   GstLoaderContext *context;
77   
78   if (!gst_loader_init (error))
79     return NULL;
80   
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;
88
89   if (!context->ani) {
90     GST_WARNING ("creating animation failed");
91     g_free (context);
92     return NULL;
93   }
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);
97     g_free (context);
98     return NULL;
99   }
100
101   GST_LOG_OBJECT (context->ani, "begin loading");
102   return context;
103 }
104 static gboolean
105 gst_loader_load_increment (gpointer context_pointer, const guchar *buf, guint size, GError **error)
106 {
107   GdkPixbufAnimationIter *iter;
108   GstLoaderContext *context = (GstLoaderContext *) context_pointer;
109
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));
117
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);
123     }
124
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);
128     }
129     
130     context->initialized = TRUE;
131   }
132   
133   return TRUE;
134 }
135 static gboolean
136 gst_loader_stop_load (gpointer context_pointer, GError **error)
137 {
138   GstLoaderContext *context = (GstLoaderContext *) context_pointer;
139
140   GST_LOG_OBJECT (context->ani, "stop loading");
141   gst_gdk_animation_done_adding (context->ani);
142   g_object_unref (context->ani);
143   g_free (context);
144
145   return TRUE;
146 }
147
148 static GdkPixbufAnimation *
149 gst_loader_load_animation (FILE *f, GError **error)
150 {
151   gchar *filename;
152   GstGdkAnimation *ani;
153   GdkPixbufAnimationIter *iter = NULL;
154   
155   if (!gst_loader_init (error))
156     return NULL;
157   
158   GST_LOG ("load_animation");
159   ani = gst_gdk_animation_new (error);
160   if (!ani)
161     return NULL;
162   
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);
167   } else {
168     GST_DEBUG ("open (\"%s\", 0) failed", filename);
169   }
170   g_free (filename);
171   if (iter == NULL) {
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");
176     return NULL; 
177   }
178   g_object_unref (iter);
179   GST_LOG_OBJECT (ani, "load_animation succeeded");
180   return GDK_PIXBUF_ANIMATION (ani);
181 }
182 static GdkPixbuf *
183 gst_loader_load (FILE *f, GError **error)
184 {
185   GdkPixbufAnimation *ani;
186   GdkPixbuf *pixbuf;
187   
188   ani = gst_loader_load_animation (f, error);
189   if (ani == NULL)
190     return NULL;
191
192   pixbuf = gdk_pixbuf_animation_get_static_image (ani);
193   if (!pixbuf) {
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.");
198     return NULL;
199   }
200
201   g_object_ref (pixbuf);
202   g_object_unref (ani);
203
204   return pixbuf;
205 }
206 void
207 fill_vtable (GdkPixbufModule *module)
208 {
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;
214 }
215 void
216 fill_info (GdkPixbufFormat *info)
217 {
218   static GdkPixbufModulePattern signature[] = {
219     /* AVI */
220     { "RIFF    AVI ", "    xxxx    ", 100 },
221     /* MPEG 1 */
222     { "xx\001\272", "zz  ", 100 },
223     /* MPEG 2 */
224     { "xx\001\263", "zz  ", 100 },
225     /* Quicktime */
226     { "    wide", "xxxx    ", 80 },
227     { "    moov", "xxxx    ", 80 },
228     { "    mdat", "xxxx    ", 80 },
229     { "    pnot", "xxxx    ", 80 },
230     { "    PICT", "xxxx    ", 80 },
231     { "    free", "xxxx    ", 80 },
232     /* ASF */
233     { "\060\046\262\165\216\146\317\021\246\331 \252 \142\316\154", "          z z   ", 100 },
234     { NULL, NULL, 0 }
235   };
236   
237   static gchar *mime_types[] = {
238     "video/avi", "video/x-avi", "video/x-msvideo",
239     "video/mpeg",
240     "video/quicktime", 
241     "video/x-ms-asf", 
242     NULL
243   };
244
245   static gchar *extensions[] = {
246     "avi",
247     "mpeg", "mpe", "mpg",
248     "mov",
249     "asf", "wmv", 
250     NULL
251   };
252   
253   info->name        = "GStreamer";
254   info->signature   = signature;
255   info->description = "GStreamer supported video";
256   info->mime_types  = mime_types;
257   info->extensions  = extensions;
258   info->flags       = 0;
259 }