close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
[platform/upstream/gstreamer.git] / ext / libmng / gstmngdec.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is distributed in the hope that it will be useful,
5  * but WITHOUT ANY WARRANTY; without even the implied warranty of
6  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7  * Library General Public License for more details.
8  *
9  * You should have received a copy of the GNU Library General Public
10  * License along with this library; if not, write to the
11  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
12  * Boston, MA 02111-1307, USA.
13  *
14  */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 #include <string.h>
20 #include <gst/gst.h>
21 #include "gstmngdec.h"
22 #include <gst/video/video.h>
23
24 static GstElementDetails gst_mngdec_details = {
25   "MNG decoder",
26   "Codec/Decoder/Image",
27   "Decode a mng video to raw images",
28   "Wim Taymans <wim@fluendo.com>",
29 };
30
31
32 /* Filter signals and args */
33 enum
34 {
35   /* FILL ME */
36   LAST_SIGNAL
37 };
38
39 enum
40 {
41   ARG_0
42 };
43
44 static void gst_mngdec_base_init (gpointer g_class);
45 static void gst_mngdec_class_init (GstMngDecClass * klass);
46 static void gst_mngdec_init (GstMngDec * mngdec);
47
48 static void gst_mngdec_get_property (GObject * object,
49     guint prop_id, GValue * value, GParamSpec * pspec);
50 static void gst_mngdec_set_property (GObject * object,
51     guint prop_id, const GValue * value, GParamSpec * pspec);
52
53 static GstStateChangeReturn gst_mngdec_change_state (GstElement * element,
54     GstStateChange transition);
55
56 static void gst_mngdec_loop (GstElement * element);
57
58 static GstCaps *gst_mngdec_src_getcaps (GstPad * pad);
59
60 static GstElementClass *parent_class = NULL;
61
62
63 GType
64 gst_mngdec_get_type (void)
65 {
66   static GType mngdec_type = 0;
67
68   if (!mngdec_type) {
69     static const GTypeInfo mngdec_info = {
70       sizeof (GstMngDecClass),
71       gst_mngdec_base_init,
72       NULL,
73       (GClassInitFunc) gst_mngdec_class_init,
74       NULL,
75       NULL,
76       sizeof (GstMngDec),
77       0,
78       (GInstanceInitFunc) gst_mngdec_init,
79     };
80
81     mngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstMngDec",
82         &mngdec_info, 0);
83   }
84   return mngdec_type;
85 }
86
87 static GstStaticPadTemplate gst_mngdec_src_pad_template =
88     GST_STATIC_PAD_TEMPLATE ("src",
89     GST_PAD_SRC,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
92     );
93
94 static GstStaticPadTemplate gst_mngdec_sink_pad_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
96     GST_PAD_SINK,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS ("video/x-mng, "
99         "width = (int) [ 16, 4096 ], "
100         "height = (int) [ 16, 4096 ], " "framerate = (double) [ 0.0, MAX ]")
101     );
102
103 static void
104 gst_mngdec_base_init (gpointer g_class)
105 {
106   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
107
108   gst_element_class_add_pad_template (element_class,
109       gst_static_pad_template_get (&gst_mngdec_src_pad_template));
110   gst_element_class_add_pad_template (element_class,
111       gst_static_pad_template_get (&gst_mngdec_sink_pad_template));
112   gst_element_class_set_details (element_class, &gst_mngdec_details);
113 }
114
115 static void
116 gst_mngdec_class_init (GstMngDecClass * klass)
117 {
118   GObjectClass *gobject_class;
119   GstElementClass *gstelement_class;
120
121   gobject_class = (GObjectClass *) klass;
122   gstelement_class = (GstElementClass *) klass;
123
124   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
125
126   gstelement_class->change_state = gst_mngdec_change_state;
127
128   gstelement_class->get_property = gst_mngdec_get_property;
129   gstelement_class->set_property = gst_mngdec_set_property;
130
131 }
132
133
134 static GstPadLinkReturn
135 gst_mngdec_sinklink (GstPad * pad, const GstCaps * caps)
136 {
137   GstMngDec *mngdec;
138   GstStructure *structure;
139
140   mngdec = GST_MNGDEC (gst_pad_get_parent (pad));
141
142   structure = gst_caps_get_structure (caps, 0);
143   gst_structure_get_double (structure, "framerate", &mngdec->fps);
144   gst_object_unref (mngdec);
145
146   return TRUE;
147 }
148
149 static void
150 gst_mngdec_init (GstMngDec * mngdec)
151 {
152   mngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
153       (&gst_mngdec_sink_pad_template), "sink");
154   gst_element_add_pad (GST_ELEMENT (mngdec), mngdec->sinkpad);
155
156   mngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
157       (&gst_mngdec_src_pad_template), "src");
158   gst_element_add_pad (GST_ELEMENT (mngdec), mngdec->srcpad);
159
160   gst_pad_set_link_function (mngdec->sinkpad, gst_mngdec_sinklink);
161
162   gst_pad_set_getcaps_function (mngdec->srcpad, gst_mngdec_src_getcaps);
163
164   mngdec->mng = NULL;
165   mngdec->buffer_out = NULL;
166
167   mngdec->color_type = -1;
168   mngdec->width = -1;
169   mngdec->height = -1;
170   mngdec->fps = -1;
171
172   gst_element_set_loop_function (GST_ELEMENT (mngdec), gst_mngdec_loop);
173 }
174
175 static GstCaps *
176 gst_mngdec_src_getcaps (GstPad * pad)
177 {
178   GstMngDec *mngdec;
179   GstCaps *caps;
180   gint i;
181   GstPadTemplate *templ;
182   GstCaps *inter;
183
184   mngdec = GST_MNGDEC (gst_pad_get_parent (pad));
185   templ = gst_static_pad_template_get (&gst_mngdec_src_pad_template);
186   caps = gst_caps_copy (gst_pad_template_get_caps (templ));
187
188   if (mngdec->color_type != -1) {
189     GstCaps *to_inter = NULL;
190
191     switch (mngdec->color_type) {
192       case MNG_COLORTYPE_RGB:
193         to_inter = gst_caps_new_simple ("video/x-raw-rgb",
194             "bpp", G_TYPE_INT, 24, NULL);
195         break;
196       case MNG_COLORTYPE_RGBA:
197         to_inter = gst_caps_new_simple ("video/x-raw-rgb",
198             "bpp", G_TYPE_INT, 32, NULL);
199         break;
200       case MNG_COLORTYPE_GRAY:
201       case MNG_COLORTYPE_INDEXED:
202       case MNG_COLORTYPE_GRAYA:
203       default:
204         GST_ELEMENT_ERROR (mngdec, STREAM, NOT_IMPLEMENTED, (NULL),
205             ("mngdec does not support grayscale or paletted data yet"));
206         break;
207     }
208     inter = gst_caps_intersect (caps, to_inter);
209     gst_caps_free (caps);
210     gst_caps_free (to_inter);
211     caps = inter;
212   }
213
214   for (i = 0; i < gst_caps_get_size (caps); i++) {
215     GstStructure *structure = gst_caps_get_structure (caps, i);
216
217     if (mngdec->width != -1) {
218       gst_structure_set (structure, "width", G_TYPE_INT, mngdec->width, NULL);
219     }
220
221     if (mngdec->height != -1) {
222       gst_structure_set (structure, "height", G_TYPE_INT, mngdec->height, NULL);
223     }
224
225     if (mngdec->fps != -1) {
226       gst_structure_set (structure,
227           "framerate", G_TYPE_DOUBLE, mngdec->fps, NULL);
228     }
229   }
230
231   gst_object_unref (mngdec);
232
233   return caps;
234 }
235
236
237 static void
238 gst_mngdec_loop (GstElement * element)
239 {
240   GstMngDec *mngdec;
241   mng_retcode ret;
242
243   mngdec = GST_MNGDEC (element);
244
245   if (mngdec->first) {
246     GST_DEBUG ("display");
247     ret = mng_readdisplay (mngdec->mng);
248     mngdec->first = FALSE;
249   } else {
250     GST_DEBUG ("resume");
251     ret = mng_display_resume (mngdec->mng);
252   }
253   if (ret == MNG_NEEDTIMERWAIT) {
254     /* libmng needs more data later on */
255   } else {
256     /* assume EOS here */
257     gst_pad_push (mngdec->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
258     gst_element_set_eos (element);
259   }
260 }
261
262 static void
263 gst_mngdec_get_property (GObject * object,
264     guint prop_id, GValue * value, GParamSpec * pspec)
265 {
266   GstMngDec *mngdec;
267
268   mngdec = GST_MNGDEC (object);
269
270   switch (prop_id) {
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274   }
275 }
276
277
278 static void
279 gst_mngdec_set_property (GObject * object,
280     guint prop_id, const GValue * value, GParamSpec * pspec)
281 {
282   GstMngDec *mngdec;
283
284   mngdec = GST_MNGDEC (object);
285
286   switch (prop_id) {
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290   }
291 }
292
293 static mng_bool
294 mngdec_error (mng_handle mng, mng_int32 code, mng_int8 severity,
295     mng_chunkid chunktype, mng_uint32 chunkseq,
296     mng_int32 extra1, mng_int32 extra2, mng_pchar text)
297 {
298   GstMngDec *mngdec;
299
300   mngdec = GST_MNGDEC (mng_get_userdata (mng));
301
302   GST_ERROR_OBJECT (mngdec, "error in chunk %4.4s (%d): %s",
303       (gchar *) & chunktype, chunkseq, text);
304
305   return FALSE;
306 }
307
308 static mng_bool
309 mngdec_openstream (mng_handle mng)
310 {
311   GstMngDec *mngdec;
312
313   mngdec = GST_MNGDEC (mng_get_userdata (mng));
314
315   mngdec->bs = gst_bytestream_new (mngdec->sinkpad);
316
317   return MNG_TRUE;
318 }
319
320 static mng_bool
321 mngdec_closestream (mng_handle mng)
322 {
323   GstMngDec *mngdec;
324
325   mngdec = GST_MNGDEC (mng_get_userdata (mng));
326
327   gst_bytestream_destroy (mngdec->bs);
328
329   return MNG_TRUE;
330 }
331
332 static gboolean
333 mngdec_handle_sink_event (GstMngDec * mngdec)
334 {
335   guint32 remaining;
336   GstEvent *event;
337   GstEventType type;
338
339   gst_bytestream_get_status (mngdec->bs, &remaining, &event);
340
341   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
342   GST_DEBUG ("mngdec: event %p %d", event, type);
343
344   switch (type) {
345     case GST_EVENT_EOS:
346       gst_bytestream_flush (mngdec->bs, remaining);
347       gst_pad_event_default (mngdec->sinkpad, event);
348       return FALSE;
349     case GST_EVENT_FLUSH:
350       break;
351     case GST_EVENT_DISCONTINUOUS:
352       GST_DEBUG ("discontinuous event");
353       break;
354     default:
355       g_warning ("unhandled event %d", type);
356       break;
357   }
358
359   gst_event_unref (event);
360   return TRUE;
361 }
362
363 static mng_bool
364 mngdec_readdata (mng_handle mng, mng_ptr buffer,
365     mng_uint32 size, mng_uint32 * bytesread)
366 {
367   GstMngDec *mngdec;
368   guint8 *bytes;
369   guint32 read;
370
371   mngdec = GST_MNGDEC (mng_get_userdata (mng));
372
373   GST_DEBUG ("read data %d", size);
374
375   do {
376     read = gst_bytestream_peek_bytes (mngdec->bs, &bytes, size);
377     if (read != size) {
378       if (!mngdec_handle_sink_event (mngdec)) {
379         /* EOS */
380         *bytesread = 0;
381         return MNG_FALSE;
382       }
383     } else {
384       break;
385     }
386   } while (TRUE);
387
388   memcpy (buffer, bytes, size);
389   gst_bytestream_flush_fast (mngdec->bs, read);
390   *bytesread = size;
391
392   return MNG_TRUE;
393 }
394
395 static mng_uint32
396 mngdec_gettickcount (mng_handle mng)
397 {
398   GTimeVal time;
399   guint32 val;
400
401   g_get_current_time (&time);
402
403   val = time.tv_sec * 1000 + time.tv_usec;
404   GST_DEBUG ("get tick count %d", val);
405
406   return val;
407 }
408
409 static mng_bool
410 mngdec_settimer (mng_handle mng, mng_uint32 msecs)
411 {
412   GstMngDec *mngdec;
413
414   mngdec = GST_MNGDEC (mng_get_userdata (mng));
415   //mymng->delay = msecs;
416   GST_DEBUG ("set timer %d", msecs);
417
418   return MNG_TRUE;
419 }
420
421 static mng_bool
422 mngdec_processheader (mng_handle mng, mng_uint32 width, mng_uint32 height)
423 {
424   GstMngDec *mngdec;
425   guint32 playtime;
426   guint32 framecount;
427   guint32 ticks;
428
429   mngdec = GST_MNGDEC (mng_get_userdata (mng));
430
431   GST_DEBUG ("process header %d %d", width, height);
432
433   playtime = mng_get_playtime (mng);
434   framecount = mng_get_framecount (mng);
435   ticks = mng_get_ticks (mng);
436
437   if (playtime == 0) {
438     mngdec->fps = ticks;
439   } else {
440     mngdec->fps = ((gfloat) ticks) / playtime;
441   }
442
443   if (mngdec->width != width || mngdec->height != height) {
444     mngdec->width = width;
445     mngdec->stride = ((width + 3) & ~3) * 4;
446     mngdec->height = height;
447
448     if (GST_PAD_LINK_FAILED (gst_pad_renegotiate (mngdec->srcpad))) {
449       GST_ELEMENT_ERROR (mngdec, CORE, NEGOTIATION, (NULL), (NULL));
450       return MNG_FALSE;
451     }
452
453     mngdec->buffer_out =
454         gst_buffer_new_and_alloc (mngdec->height * mngdec->stride);
455   }
456   return MNG_TRUE;
457 }
458
459 static mng_ptr
460 mngdec_getcanvasline (mng_handle mng, mng_uint32 line)
461 {
462   GstMngDec *mngdec;
463
464   mngdec = GST_MNGDEC (mng_get_userdata (mng));
465
466   GST_DEBUG ("get canvas line %d", line);
467
468   return (mng_ptr) (GST_BUFFER_DATA (mngdec->buffer_out) +
469       (line * mngdec->stride));
470 }
471
472 static mng_bool
473 mngdec_refresh (mng_handle mng, mng_uint32 x, mng_uint32 y,
474     mng_uint32 w, mng_uint32 h)
475 {
476   GstMngDec *mngdec;
477   guint32 current;
478
479   mngdec = GST_MNGDEC (mng_get_userdata (mng));
480
481   current = mng_get_currentplaytime (mng);
482
483   GST_DEBUG ("refresh %d %d %d %d", x, y, w, h);
484   if (h == mngdec->height) {
485     GstBuffer *out = gst_buffer_copy (mngdec->buffer_out);
486
487     gst_pad_push (mngdec->srcpad, GST_DATA (out));
488   }
489
490   return MNG_TRUE;
491 }
492
493 static GstStateChangeReturn
494 gst_mngdec_change_state (GstElement * element, GstStateChange transition)
495 {
496   GstMngDec *mngdec = GST_MNGDEC (element);
497
498   switch (transition) {
499     case GST_STATE_CHANGE_NULL_TO_READY:
500       /* init library, make sure to pass an alloc function that sets the
501        * memory to 0 */
502       mngdec->mng =
503           mng_initialize (mngdec, (mng_memalloc) g_malloc0,
504           (mng_memfree) g_free, MNG_NULL);
505       if (mngdec->mng == MNG_NULL) {
506         return GST_STATE_CHANGE_FAILURE;
507       }
508       /* set the callbacks */
509       mng_setcb_errorproc (mngdec->mng, mngdec_error);
510       mng_setcb_openstream (mngdec->mng, mngdec_openstream);
511       mng_setcb_closestream (mngdec->mng, mngdec_closestream);
512       mng_setcb_readdata (mngdec->mng, mngdec_readdata);
513       mng_setcb_gettickcount (mngdec->mng, mngdec_gettickcount);
514       mng_setcb_settimer (mngdec->mng, mngdec_settimer);
515       mng_setcb_processheader (mngdec->mng, mngdec_processheader);
516       mng_setcb_getcanvasline (mngdec->mng, mngdec_getcanvasline);
517       mng_setcb_refresh (mngdec->mng, mngdec_refresh);
518       mng_set_canvasstyle (mngdec->mng, MNG_CANVAS_RGBA8);
519       mng_set_doprogressive (mngdec->mng, MNG_FALSE);
520       break;
521     case GST_STATE_CHANGE_READY_TO_PAUSED:
522       mngdec->first = TRUE;
523       break;
524     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
525       break;
526     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
527       break;
528     case GST_STATE_CHANGE_PAUSED_TO_READY:
529       break;
530     case GST_STATE_CHANGE_READY_TO_NULL:
531       mng_cleanup (&mngdec->mng);
532       break;
533     default:
534       break;
535   }
536
537   if (GST_ELEMENT_CLASS (parent_class)->change_state)
538     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
539
540   return GST_STATE_CHANGE_SUCCESS;
541 }