gst_loader.c 6.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
 *
 * gst_loader.c: Load GStreamer videos as gdkpixbufs
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstgdkanimation.h"
27
#include <gst/gstinfo.h>
28
29
#include <stdio.h>

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
typedef struct {
  /* stuff gdk throws at us and we're supposed to keep */
  GdkPixbufModuleSizeFunc	size_func;
  GdkPixbufModulePreparedFunc	prepared_func;
  GdkPixbufModuleUpdatedFunc	updated_func;
  gpointer			user_data;
  /* our own stuff - we're much better at keeping fields small :p */
  GstGdkAnimation *		ani;
  gboolean			initialized;
} GstLoaderContext;

GST_DEBUG_CATEGORY_STATIC (gst_loader_debug);
#define GST_CAT_DEFAULT gst_loader_debug

static gboolean
gst_loader_init (GError **error)
{
  static gboolean inited = FALSE;
  
  if (inited)
    return TRUE;
  
  if (!g_thread_supported ()) {
    g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
		 "The GStreamer loader requires threading support.");
    return FALSE;
  }

  if (!gst_init_check (0, NULL)) {
    g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
		 "GStreamer could not be initialized.");
    return FALSE;
  }

  inited = TRUE;
  GST_DEBUG_CATEGORY_INIT (gst_loader_debug, "gstloader", 0, "entry point debugging for the GStreamer gdk pixbuf loader");
  return TRUE;
}
static gpointer
gst_loader_begin_load (GdkPixbufModuleSizeFunc size_func, GdkPixbufModulePreparedFunc prepared_func,
		       GdkPixbufModuleUpdatedFunc updated_func, gpointer user_data, GError **error)
{
  GstLoaderContext *context;
  
  if (!gst_loader_init (error))
    return NULL;
  
  context = g_new (GstLoaderContext, 1);
  context->size_func = size_func;
  context->prepared_func = prepared_func;
  context->updated_func = updated_func;
  context->user_data = user_data;
  context->ani = gst_gdk_animation_new (error);
  context->initialized = FALSE;

  if (!context->ani) {
    GST_WARNING ("creating animation failed");
    g_free (context);
    return NULL;
  }
  GST_LOG_OBJECT (context->ani, "begin loading");
  return context;
}
static gboolean
gst_loader_load_increment (gpointer context_pointer, const guchar *buf, guint size, GError **error)
{
  GdkPixbufAnimationIter *iter;
  GstLoaderContext *context = (GstLoaderContext *) context_pointer;

  GST_LOG_OBJECT (context->ani, "load increment: %u bytes", size);
  gst_gdk_animation_add_data (context->ani, buf, size);
  if (!context->initialized && (iter = gdk_pixbuf_animation_get_iter (
	      GDK_PIXBUF_ANIMATION (context->ani), NULL)) != NULL) {
    int width = gdk_pixbuf_animation_get_width (GDK_PIXBUF_ANIMATION (context->ani));
    int height = gdk_pixbuf_animation_get_height (GDK_PIXBUF_ANIMATION (context->ani));
    GdkPixbuf *pixbuf = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION (context->ani));

    g_object_unref (iter);
    GST_LOG_OBJECT (context->ani, "initializing loader");
    if (context->size_func) {
      GST_LOG_OBJECT (context->ani, "calling size_func %p", context->size_func);
      context->size_func (&width, &height, context->user_data);
    }

    if (context->prepared_func) {
      GST_LOG_OBJECT (context->ani, "calling prepared_func %p", context->prepared_func);
      context->prepared_func (pixbuf, GDK_PIXBUF_ANIMATION (context->ani), context->user_data);
    }
    
    context->initialized = TRUE;
  }
  
  return TRUE;
}
static gboolean
gst_loader_stop_load (gpointer context_pointer, GError **error)
{
  GstLoaderContext *context = (GstLoaderContext *) context_pointer;

  GST_LOG_OBJECT (context->ani, "stop loading");
  gst_gdk_animation_done_adding (context->ani);
  g_object_unref (context->ani);
  g_free (context);

  return TRUE;
}

137
138
139
static GdkPixbufAnimation *
gst_loader_load_animation (FILE *f, GError **error)
{
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  guchar data[4096];
  guint size;
  GdkPixbufAnimationIter *iter;
  GstGdkAnimation *ani;
  
  if (!gst_loader_init (error))
    return NULL;
  
  GST_LOG ("load_animation");
  ani = gst_gdk_animation_new (error);
  if (!ani)
    return NULL;
  
  while ((size = fread (data, 1, 4096, f)) > 0) {
    if (!gst_gdk_animation_add_data (ani, data, size)) {
      g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
		   "could not add more data to animation"); /* our errors suck ;) */
      g_object_unref (ani);
      GST_WARNING ("load_animation failed");
      return NULL;
    }
  }
  gst_gdk_animation_done_adding (ani);
  iter = gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (ani), NULL);
  if (iter == NULL) {
      g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
		   "could not create an image");
      g_object_unref (ani);
      GST_INFO ("could not create an image");
      return NULL; 
  }
  g_object_unref (iter);
  GST_LOG_OBJECT (ani, "load_animation succeeded");
  return GDK_PIXBUF_ANIMATION (ani);
174
175
176
177
}
void
fill_vtable (GdkPixbufModule *module)
{
178
179
180
181
  module->begin_load = gst_loader_begin_load;
  module->load_increment = gst_loader_load_increment;
  module->stop_load = gst_loader_stop_load;
  module->load_animation = gst_loader_load_animation;
182
183
184
185
186
187
}
void
fill_info (GdkPixbufFormat *info)
{
  static GdkPixbufModulePattern signature[] = {
    { "RIFF    AVI ", "    xxxx    ", 100 },
Iain Holmes's avatar
Iain Holmes committed
188
    { "xx\001\272", "zz  ", 100 },
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    { NULL, NULL, 0 }
  };
  
  static gchar *mime_types[] = {
    "video/avi",
    "video/mpeg",
    NULL
  };

  static gchar *extensions[] = {
    "avi",
    "mpeg", "mpe", "mpg",
    NULL
  };
  
  info->name        = "GStreamer";
  info->signature   = signature;
  info->description = "GStreamer supported video";
  info->mime_types  = mime_types;
  info->extensions  = extensions;
  info->flags       = 0;
}