2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
26 /*#define DEBUG_ENABLED*/
27 #include "gstsmokedec.h"
28 #include <gst/video/video.h>
30 /* elementfactory information */
31 GstElementDetails gst_smokedec_details = {
32 "Smoke video decoder",
33 "Codec/Decoder/Image",
34 "Decode video from Smoke format",
35 "Wim Taymans <wim@fluendo.com>",
38 GST_DEBUG_CATEGORY (smokedec_debug);
39 #define GST_CAT_DEFAULT smokedec_debug
41 /* SmokeDec signals and args */
52 static void gst_smokedec_base_init (gpointer g_class);
53 static void gst_smokedec_class_init (GstSmokeDec * klass);
54 static void gst_smokedec_init (GstSmokeDec * smokedec);
56 static GstFlowReturn gst_smokedec_chain (GstPad * pad, GstBuffer * buf);
58 static GstElementClass *parent_class = NULL;
60 /*static guint gst_smokedec_signals[LAST_SIGNAL] = { 0 }; */
63 gst_smokedec_get_type (void)
65 static GType smokedec_type = 0;
68 static const GTypeInfo smokedec_info = {
69 sizeof (GstSmokeDecClass),
70 gst_smokedec_base_init,
72 (GClassInitFunc) gst_smokedec_class_init,
77 (GInstanceInitFunc) gst_smokedec_init,
81 g_type_register_static (GST_TYPE_ELEMENT, "GstSmokeDec", &smokedec_info,
87 static GstStaticPadTemplate gst_smokedec_src_pad_template =
88 GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
94 static GstStaticPadTemplate gst_smokedec_sink_pad_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS ("video/x-smoke, "
99 "width = (int) [ 16, 4096 ], "
100 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0/1, MAX ]")
104 gst_smokedec_base_init (gpointer g_class)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108 gst_element_class_add_pad_template (element_class,
109 gst_static_pad_template_get (&gst_smokedec_src_pad_template));
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&gst_smokedec_sink_pad_template));
112 gst_element_class_set_details (element_class, &gst_smokedec_details);
116 gst_smokedec_class_init (GstSmokeDec * klass)
118 GstElementClass *gstelement_class;
120 gstelement_class = (GstElementClass *) klass;
122 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124 GST_DEBUG_CATEGORY_INIT (smokedec_debug, "smokedec", 0, "Smoke decoder");
128 gst_smokedec_init (GstSmokeDec * smokedec)
130 GST_DEBUG_OBJECT (smokedec, "gst_smokedec_init: initializing");
131 /* create the sink and src pads */
134 gst_pad_new_from_template (gst_static_pad_template_get
135 (&gst_smokedec_sink_pad_template), "sink");
136 gst_pad_set_chain_function (smokedec->sinkpad, gst_smokedec_chain);
137 gst_element_add_pad (GST_ELEMENT (smokedec), smokedec->sinkpad);
140 gst_pad_new_from_template (gst_static_pad_template_get
141 (&gst_smokedec_src_pad_template), "src");
142 gst_pad_use_fixed_caps (smokedec->srcpad);
143 gst_element_add_pad (GST_ELEMENT (smokedec), smokedec->srcpad);
145 /* reset the initial video state */
146 smokedec->format = -1;
147 smokedec->width = -1;
148 smokedec->height = -1;
149 smokedec->fps_num = -1;
150 smokedec->fps_denom = -1;
151 smokedec->next_time = 0;
153 smokecodec_decode_new (&smokedec->info);
157 gst_smokedec_chain (GstPad * pad, GstBuffer * buf)
159 GstSmokeDec *smokedec;
160 guint8 *data, *outdata;
161 gulong size, outsize;
163 SmokeCodecFlags flags;
166 guint fps_num, fps_denom;
170 smokedec = GST_SMOKEDEC (gst_pad_get_parent (pad));
172 data = GST_BUFFER_DATA (buf);
173 size = GST_BUFFER_SIZE (buf);
174 time = GST_BUFFER_TIMESTAMP (buf);
176 GST_DEBUG_OBJECT (smokedec,
177 "gst_smokedec_chain: got buffer of %ld bytes in '%s'", size,
178 GST_OBJECT_NAME (smokedec));
180 /* have the ID packet. */
181 if (data[0] == SMOKECODEC_TYPE_ID) {
182 smokeret = smokecodec_parse_id (smokedec->info, data, size);
183 if (smokeret != SMOKECODEC_OK)
190 /* now handle data packets */
191 GST_DEBUG_OBJECT (smokedec, "gst_smokedec_chain: reading header %08lx",
193 smokecodec_parse_header (smokedec->info, data, size, &flags, &width, &height,
194 &fps_num, &fps_denom);
196 if (smokedec->height != height || smokedec->width != width ||
197 smokedec->fps_num != fps_num || smokedec->fps_denom != fps_denom) {
200 smokedec->height = height;
201 smokedec->width = width;
203 caps = gst_caps_new_simple ("video/x-raw-yuv",
204 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I', '4', '2', '0'),
205 "width", G_TYPE_INT, width,
206 "height", G_TYPE_INT, height,
207 "framerate", GST_TYPE_FRACTION, fps_num, fps_denom, NULL);
209 gst_pad_set_caps (smokedec->srcpad, caps);
210 gst_caps_unref (caps);
214 if (smokedec->need_keyframe) {
215 if (!(flags & SMOKECODEC_KEYFRAME))
218 smokedec->need_keyframe = FALSE;
221 outbuf = gst_buffer_new ();
222 outsize = GST_BUFFER_SIZE (outbuf) = width * height + width * height / 2;
223 outdata = g_malloc (outsize);
224 GST_BUFFER_DATA (outbuf) = outdata;
225 GST_BUFFER_MALLOCDATA (outbuf) = outdata;
227 GST_BUFFER_DURATION (outbuf) = GST_SECOND * fps_denom / fps_num;
228 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buf);
229 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (smokedec->srcpad));
231 if (time == GST_CLOCK_TIME_NONE) {
232 if (GST_BUFFER_OFFSET (buf) == -1) {
233 time = smokedec->next_time;
235 time = GST_BUFFER_OFFSET (buf) * GST_BUFFER_DURATION (outbuf);
238 GST_BUFFER_TIMESTAMP (outbuf) = time;
239 smokedec->next_time = time + GST_BUFFER_DURATION (outbuf);
241 smokeret = smokecodec_decode (smokedec->info, data, size, outdata);
242 if (smokeret != SMOKECODEC_OK)
245 GST_DEBUG_OBJECT (smokedec, "gst_smokedec_chain: sending buffer");
246 ret = gst_pad_push (smokedec->srcpad, outbuf);
249 gst_buffer_unref (buf);
250 gst_object_unref (smokedec);
257 GST_ELEMENT_ERROR (smokedec, STREAM, DECODE,
258 (NULL), ("Could not parse smoke header, reason: %d", smokeret));
259 ret = GST_FLOW_ERROR;
264 GST_DEBUG_OBJECT (smokedec, "dropping buffer while waiting for keyframe");
270 GST_ELEMENT_ERROR (smokedec, STREAM, DECODE,
271 (NULL), ("Could not decode smoke frame, reason: %d", smokeret));
272 ret = GST_FLOW_ERROR;