Added a tarkin encoder/decoder plugin.
[platform/upstream/gstreamer.git] / ext / tarkin / gsttarkindec.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "gsttarkindec.h"
25
26 extern GstPadTemplate *dec_src_template, *dec_sink_template;
27
28 /* elementfactory information */
29 GstElementDetails tarkindec_details = {
30   "Ogg Tarkin decoder",
31   "Filter/Video/Decoder",
32   "Decodes video in OGG Tarkin format",
33   VERSION,
34   "Monty <monty@xiph.org>, " 
35   "Wim Taymans <wim.taymans@chello.be>",
36   "(C) 2002",
37 };
38
39 /* TarkinDec signals and args */
40 enum
41 {
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum
47 {
48   ARG_0,
49   ARG_BITRATE,
50 };
51
52 static void     gst_tarkindec_class_init        (TarkinDecClass *klass);
53 static void     gst_tarkindec_init              (TarkinDec *arkindec);
54
55 static void     gst_tarkindec_chain             (GstPad *pad, GstBuffer *buf);
56 static void     gst_tarkindec_setup             (TarkinDec *tarkindec);
57 static GstElementStateReturn
58                 gst_tarkindec_change_state      (GstElement *element);
59
60 static void     gst_tarkindec_get_property      (GObject *object, guint prop_id, GValue *value,
61                                                  GParamSpec *pspec);
62 static void     gst_tarkindec_set_property      (GObject *object, guint prop_id, const GValue *value,
63                                                  GParamSpec *pspec);
64
65 static GstElementClass *parent_class = NULL;
66 /*static guint gst_tarkindec_signals[LAST_SIGNAL] = { 0 }; */
67
68 GType
69 tarkindec_get_type (void)
70 {
71   static GType tarkindec_type = 0;
72
73   if (!tarkindec_type) {
74     static const GTypeInfo tarkindec_info = {
75       sizeof (TarkinDecClass), 
76       NULL,
77       NULL,
78       (GClassInitFunc) gst_tarkindec_class_init,
79       NULL,
80       NULL,
81       sizeof (TarkinDec),
82       0,
83       (GInstanceInitFunc) gst_tarkindec_init,
84     };
85
86     tarkindec_type = g_type_register_static (GST_TYPE_ELEMENT, "TarkinDec", &tarkindec_info, 0);
87   }
88   return tarkindec_type;
89 }
90
91 static void
92 gst_tarkindec_class_init (TarkinDecClass *klass)
93 {
94   GObjectClass *gobject_class;
95   GstElementClass *gstelement_class;
96
97   gobject_class = (GObjectClass *) klass;
98   gstelement_class = (GstElementClass *) klass;
99
100   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE, 
101     g_param_spec_int ("bitrate", "bitrate", "bitrate", 
102             G_MININT, G_MAXINT, 3000, G_PARAM_READWRITE));
103
104   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
105
106   gobject_class->set_property = gst_tarkindec_set_property;
107   gobject_class->get_property = gst_tarkindec_get_property;
108
109   gstelement_class->change_state = gst_tarkindec_change_state;
110 }
111
112 static void
113 gst_tarkindec_init (TarkinDec * tarkindec)
114 {
115   tarkindec->sinkpad = gst_pad_new_from_template (dec_sink_template, "sink");
116   gst_element_add_pad (GST_ELEMENT (tarkindec), tarkindec->sinkpad);
117   gst_pad_set_chain_function (tarkindec->sinkpad, gst_tarkindec_chain);
118
119   tarkindec->srcpad = gst_pad_new_from_template (dec_src_template, "src");
120   gst_element_add_pad (GST_ELEMENT (tarkindec), tarkindec->srcpad);
121
122   tarkindec->bitrate = 3000;
123   tarkindec->setup = FALSE;
124   tarkindec->nheader = 0;
125
126   /* we're chained and we can deal with events */
127   GST_FLAG_SET (tarkindec, GST_ELEMENT_EVENT_AWARE);
128 }
129
130 static void
131 gst_tarkindec_setup (TarkinDec *tarkindec)
132 {
133   tarkindec->tarkin_stream = tarkin_stream_new ();
134   
135   ogg_sync_init (&tarkindec->oy);
136   ogg_stream_init (&tarkindec->os, 1);
137   tarkin_info_init (&tarkindec->ti);
138   tarkin_comment_init (&tarkindec->tc);
139
140   tarkindec->setup = TRUE;
141 }
142
143 static void
144 gst_tarkindec_chain (GstPad *pad, GstBuffer *buf)
145 {
146   TarkinDec *tarkindec;
147
148   g_return_if_fail (pad != NULL);
149   g_return_if_fail (GST_IS_PAD (pad));
150   g_return_if_fail (buf != NULL);
151
152   tarkindec = GST_TARKINDEC (gst_pad_get_parent (pad));
153
154   if (!tarkindec->setup) {
155     gst_element_error (GST_ELEMENT (tarkindec), "decoder not initialized (input is not audio?)");
156     if (GST_IS_BUFFER (buf))
157       gst_buffer_unref (buf);
158     else
159       gst_pad_event_default (pad, GST_EVENT (buf));
160     return;
161   }
162
163   if (GST_IS_EVENT (buf)) {
164     switch (GST_EVENT_TYPE (buf)) {
165       case GST_EVENT_EOS:
166       default:
167         gst_pad_event_default (pad, GST_EVENT (buf));
168         break;
169     }
170   }
171   else {
172     gchar *data;
173     gulong size;
174     gchar *buffer;
175     guchar *rgb;
176     TarkinTime date;
177     TarkinVideoLayerDesc *layer;
178   
179     /* data to decode */
180     data = GST_BUFFER_DATA (buf);
181     size = GST_BUFFER_SIZE (buf);
182
183     buffer = ogg_sync_buffer(&tarkindec->oy, size);
184     memcpy (buffer, data, size);
185     ogg_sync_wrote(&tarkindec->oy, size);
186
187     if (ogg_sync_pageout (&tarkindec->oy, &tarkindec->og)) {
188       ogg_stream_pagein (&tarkindec->os, &tarkindec->og);
189
190       while (ogg_stream_packetout (&tarkindec->os, &tarkindec->op)) {
191         if (tarkindec->op.e_o_s)
192           break;
193         if (tarkindec->nheader < 3) { /* 3 first packets to headerin */
194           tarkin_synthesis_headerin (&tarkindec->ti, &tarkindec->tc, &tarkindec->op);
195
196           if (tarkindec->nheader == 2) {
197             tarkin_synthesis_init (tarkindec->tarkin_stream, &tarkindec->ti);
198           }
199           tarkindec->nheader++;
200         } else {
201           tarkin_synthesis_packetin (tarkindec->tarkin_stream, &tarkindec->op);
202           
203           while (tarkin_synthesis_frameout (tarkindec->tarkin_stream, &rgb, 0, &date) == 0) {
204             GstBuffer *outbuf;
205
206             layer = &tarkindec->tarkin_stream->layer->desc;
207
208             if (!GST_PAD_CAPS (tarkindec->srcpad)) {
209               if (!gst_pad_try_set_caps (tarkindec->srcpad,
210                                       GST_CAPS_NEW (
211                                         "tarkin_raw",
212                                         "video/raw",
213                                         "format",     GST_PROPS_FOURCC (GST_STR_FOURCC ("RGB ")),
214                                         "bpp",        GST_PROPS_INT (24),
215                                         "depth",      GST_PROPS_INT (24),
216                                         "endianness", GST_PROPS_INT (G_BYTE_ORDER),
217                                         "red_mask",   GST_PROPS_INT (0xff0000),
218                                         "green_mask", GST_PROPS_INT (0xff00),
219                                         "blue_mask",  GST_PROPS_INT (0xff),
220                                         "width",      GST_PROPS_INT (layer->width),
221                                         "height",     GST_PROPS_INT (layer->height)
222                                        )))
223               {
224                 gst_element_error (GST_ELEMENT (tarkindec), "could not output format");
225                 gst_buffer_unref (buf);
226                 return;
227               }
228             }
229             outbuf = gst_buffer_new ();
230             GST_BUFFER_DATA (outbuf) = rgb;
231             GST_BUFFER_SIZE (outbuf) = layer->width * layer->height * 3;
232             GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_DONTFREE);
233             gst_pad_push (tarkindec->srcpad, outbuf);
234             
235             tarkin_synthesis_freeframe (tarkindec->tarkin_stream, rgb);
236           }
237         }
238       }
239     }
240     gst_buffer_unref (buf);
241   }
242 }
243
244 static GstElementStateReturn
245 gst_tarkindec_change_state (GstElement *element)
246 {
247   TarkinDec *tarkindec;
248
249   tarkindec = GST_TARKINDEC (element);
250
251   switch (GST_STATE_TRANSITION (element)) {
252     case GST_STATE_READY_TO_PAUSED:
253       gst_tarkindec_setup (tarkindec);
254       break;
255     case GST_STATE_PAUSED_TO_READY:
256       break;
257     default:
258       break;
259   }
260   
261   return parent_class->change_state (element);
262 }
263
264 static void
265 gst_tarkindec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
266 {
267   TarkinDec *tarkindec;
268
269   /* it's not null if we got it, but it might not be ours */
270   g_return_if_fail (GST_IS_TARKINDEC (object));
271
272   tarkindec = GST_TARKINDEC (object);
273
274   switch (prop_id) {
275     case ARG_BITRATE:
276       g_value_set_int (value, tarkindec->bitrate);
277       break;
278     default:
279       break;
280   }
281 }
282
283 static void
284 gst_tarkindec_set_property (GObject *object, guint prop_id, const GValue *value,
285                             GParamSpec *pspec)
286 {
287   TarkinDec *tarkindec;
288
289   /* it's not null if we got it, but it might not be ours */
290   g_return_if_fail (GST_IS_TARKINDEC (object));
291
292   tarkindec = GST_TARKINDEC (object);
293
294   switch (prop_id) {
295     case ARG_BITRATE:
296       tarkindec->bitrate = g_value_get_int (value);
297       break;
298     default:
299       break;
300   }
301 }