gst-indent
[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 {
36   /* stuff gdk throws at us and we're supposed to keep */
37   GdkPixbufModuleSizeFunc size_func;
38   GdkPixbufModulePreparedFunc prepared_func;
39   GdkPixbufModuleUpdatedFunc updated_func;
40   gpointer user_data;
41   /* our own stuff - we're much better at keeping fields small :p */
42   GstGdkAnimation *ani;
43   gboolean initialized;
44 } GstLoaderContext;
45
46 GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
47 #define GST_CAT_DEFAULT gst_loader_debug
48
49 static gboolean
50 gst_loader_init (GError ** error)
51 {
52   static gboolean inited = FALSE;
53
54   if (inited)
55     return TRUE;
56
57   if (!g_thread_supported ()) {
58     g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
59         "The GStreamer loader requires threading support.");
60     return FALSE;
61   }
62
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.");
66     return FALSE;
67   }
68
69   inited = TRUE;
70   GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0,
71       "entry point debugging for the GStreamer gdk pixbuf loader");
72   return TRUE;
73 }
74
75 static gpointer
76 gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func,
77     GdkPixbufModulePreparedFunc prepared_func,
78     GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data,
79     GError ** error)
80 {
81   GstLoaderContext *context;
82
83   if (!gst_loader_init (error))
84     return NULL;
85
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;
93
94   if (!context->ani) {
95     GST_WARNING ("creating animation failed");
96     g_free (context);
97     return NULL;
98   }
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);
103     g_free (context);
104     return NULL;
105   }
106
107   GST_LOG_OBJECT (context->ani, "begin loading");
108   return context;
109 }
110
111 static gboolean
112 gst_loader_load_increment (gpointer context_pointer, const guchar * buf,
113     guint size, GError ** error)
114 {
115   GdkPixbufAnimationIter *iter;
116   GstLoaderContext *context = (GstLoaderContext *) context_pointer;
117
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
121       && (iter =
122           gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (context->ani),
123               NULL)) != NULL) {
124     int width =
125         gdk_pixbuf_animation_get_width (GDK_PIXBUF_ANIMATION (context->ani));
126     int height =
127         gdk_pixbuf_animation_get_height (GDK_PIXBUF_ANIMATION (context->ani));
128     GdkPixbuf *pixbuf =
129         gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->
130             ani));
131
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);
137     }
138
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),
143           context->user_data);
144     }
145
146     context->initialized = TRUE;
147   }
148
149   return TRUE;
150 }
151
152 static gboolean
153 gst_loader_stop_load (gpointer context_pointer, GError ** error)
154 {
155   GstLoaderContext *context = (GstLoaderContext *) context_pointer;
156
157   GST_LOG_OBJECT (context->ani, "stop loading");
158   gst_gdk_animation_done_adding (context->ani);
159   g_object_unref (context->ani);
160   g_free (context);
161
162   return TRUE;
163 }
164
165 static GdkPixbufAnimation *
166 gst_loader_load_animation (FILE * f, GError ** error)
167 {
168   gchar *filename;
169   GstGdkAnimation *ani;
170   GdkPixbufAnimationIter *iter = NULL;
171
172   if (!gst_loader_init (error))
173     return NULL;
174
175   GST_LOG ("load_animation");
176   ani = gst_gdk_animation_new (error);
177   if (!ani)
178     return NULL;
179
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);
184   } else {
185     GST_DEBUG ("open (\"%s\", 0) failed", filename);
186   }
187   g_free (filename);
188   if (iter == NULL) {
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");
193     return NULL;
194   }
195   g_object_unref (iter);
196   GST_LOG_OBJECT (ani, "load_animation succeeded");
197   return GDK_PIXBUF_ANIMATION (ani);
198 }
199 static GdkPixbuf *
200 gst_loader_load (FILE * f, GError ** error)
201 {
202   GdkPixbufAnimation *ani;
203   GdkPixbuf *pixbuf;
204
205   ani = gst_loader_load_animation (f, error);
206   if (ani == NULL)
207     return NULL;
208
209   pixbuf = gdk_pixbuf_animation_get_static_image (ani);
210   if (!pixbuf) {
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.");
215     return NULL;
216   }
217
218   g_object_ref (pixbuf);
219   g_object_unref (ani);
220
221   return pixbuf;
222 }
223
224 void
225 fill_vtable (GdkPixbufModule * module)
226 {
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;
232 }
233
234 void
235 fill_info (GdkPixbufFormat * info)
236 {
237   static GdkPixbufModulePattern signature[] = {
238     /* AVI */
239     {"RIFF    AVI ", "    xxxx    ", 100},
240     /* MPEG 1 */
241     {"xx\001\272", "zz  ", 100},
242     /* MPEG 2 */
243     {"xx\001\263", "zz  ", 100},
244     /* Quicktime */
245     {"    wide", "xxxx    ", 80},
246     {"    moov", "xxxx    ", 80},
247     {"    mdat", "xxxx    ", 80},
248     {"    pnot", "xxxx    ", 80},
249     {"    PICT", "xxxx    ", 80},
250     {"    free", "xxxx    ", 80},
251     /* ASF */
252     {"\060\046\262\165\216\146\317\021\246\331 \252 \142\316\154",
253         "          z z   ", 100},
254     {NULL, NULL, 0}
255   };
256
257   static gchar *mime_types[] = {
258     "video/avi", "video/x-avi", "video/x-msvideo",
259     "video/mpeg",
260     "video/quicktime",
261     "video/x-ms-asf",
262     NULL
263   };
264
265   static gchar *extensions[] = {
266     "avi",
267     "mpeg", "mpe", "mpg",
268     "mov",
269     "asf", "wmv",
270     NULL
271   };
272
273   info->name = "GStreamer";
274   info->signature = signature;
275   info->description = "GStreamer supported video";
276   info->mime_types = mime_types;
277   info->extensions = extensions;
278   info->flags = 0;
279 }