blob: 7e20ad5e9f7365ac92527904d7d9a09dd5395843 [file] [log] [blame]
Ronald S. Bultje08889d72003-04-22 14:55:12 +00001/* GStreamer xvid decoder plugin
2 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +00003 * (C) 2006 Mark Nauwelaerts <manauw@skynet.be>
Ronald S. Bultje08889d72003-04-22 14:55:12 +00004 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
Tim-Philipp Müller9e1b75f2012-11-03 20:38:00 +000017 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
Ronald S. Bultje08889d72003-04-22 14:55:12 +000019 */
20
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +000021
Ronald S. Bultje08889d72003-04-22 14:55:12 +000022#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include <string.h>
Ronald S. Bultje47d40102004-01-03 19:38:40 +000027#include <xvid.h>
28
Ronald S. Bultje08889d72003-04-22 14:55:12 +000029#include <gst/video/video.h>
Ronald S. Bultje47d40102004-01-03 19:38:40 +000030#include "gstxviddec.h"
Ronald S. Bultje08889d72003-04-22 14:55:12 +000031
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000032static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
33 GST_PAD_SINK,
34 GST_PAD_ALWAYS,
35 GST_STATIC_CAPS ("video/x-xvid, "
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +000036 "width = (int) [ 0, MAX ], "
Sebastian Dröge4e01ce32011-05-23 17:29:56 +020037 "height = (int) [ 0, MAX ], " "framerate = (fraction) [ 0/1, MAX ]; "
38 "video/mpeg, "
39 "mpegversion = (int) 4, "
40 "systemstream = (boolean) FALSE, "
41 "width = (int) [ 0, MAX ], "
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +000042 "height = (int) [ 0, MAX ], " "framerate = (fraction) [ 0/1, MAX ]")
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000043 );
Ronald S. Bultje08889d72003-04-22 14:55:12 +000044
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000045static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
46 GST_PAD_SRC,
47 GST_PAD_ALWAYS,
48 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YUY2, YV12, YVYU, UYVY }")
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +000049 "; " RGB_24_32_STATIC_CAPS (32, 0x00ff0000, 0x0000ff00,
50 0x000000ff) "; " RGB_24_32_STATIC_CAPS (32, 0xff000000, 0x00ff0000,
51 0x0000ff00) "; " RGB_24_32_STATIC_CAPS (32, 0x0000ff00, 0x00ff0000,
52 0xff000000) "; " RGB_24_32_STATIC_CAPS (32, 0x000000ff, 0x0000ff00,
53 0x00ff0000) "; " RGB_24_32_STATIC_CAPS (24, 0x0000ff, 0x00ff00,
54 0xff0000) "; " GST_VIDEO_CAPS_RGB_15 "; " GST_VIDEO_CAPS_RGB_16)
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000055 );
Ronald S. Bultje08889d72003-04-22 14:55:12 +000056
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +000057GST_DEBUG_CATEGORY_STATIC (xviddec_debug);
58#define GST_CAT_DEFAULT xviddec_debug
59
Edgard Lima96e74722005-12-14 22:41:47 +000060static void gst_xviddec_base_init (GstXvidDecClass * klass);
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000061static void gst_xviddec_class_init (GstXvidDecClass * klass);
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +000062static void gst_xviddec_init (GstXvidDec * dec);
63static void gst_xviddec_reset (GstXvidDec * dec);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +000064static gboolean gst_xviddec_handle_sink_event (GstPad * pad, GstEvent * event);
Edgard Lima96e74722005-12-14 22:41:47 +000065static GstFlowReturn gst_xviddec_chain (GstPad * pad, GstBuffer * buf);
66static gboolean gst_xviddec_setcaps (GstPad * pad, GstCaps * caps);
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +000067static void gst_xviddec_flush_buffers (GstXvidDec * dec, gboolean send);
Jan Schmidt012dfb82005-09-05 17:20:29 +000068static GstStateChangeReturn gst_xviddec_change_state (GstElement * element,
69 GstStateChange transition);
Ronald S. Bultje47d40102004-01-03 19:38:40 +000070
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +000071
Ronald S. Bultje08889d72003-04-22 14:55:12 +000072static GstElementClass *parent_class = NULL;
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000073
Ronald S. Bultje08889d72003-04-22 14:55:12 +000074GType
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000075gst_xviddec_get_type (void)
Ronald S. Bultje08889d72003-04-22 14:55:12 +000076{
77 static GType xviddec_type = 0;
78
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000079 if (!xviddec_type) {
Ronald S. Bultje08889d72003-04-22 14:55:12 +000080 static const GTypeInfo xviddec_info = {
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000081 sizeof (GstXvidDecClass),
Edgard Lima96e74722005-12-14 22:41:47 +000082 (GBaseInitFunc) gst_xviddec_base_init,
Ronald S. Bultje08889d72003-04-22 14:55:12 +000083 NULL,
84 (GClassInitFunc) gst_xviddec_class_init,
85 NULL,
86 NULL,
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000087 sizeof (GstXvidDec),
Ronald S. Bultje08889d72003-04-22 14:55:12 +000088 0,
89 (GInstanceInitFunc) gst_xviddec_init,
90 };
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +000091
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +000092 xviddec_type = g_type_register_static (GST_TYPE_ELEMENT,
Thomas Vander Stichele4fd57bb2004-03-15 19:32:27 +000093 "GstXvidDec", &xviddec_info, 0);
Ronald S. Bultje08889d72003-04-22 14:55:12 +000094 }
95 return xviddec_type;
96}
97
Iain Holmes343ee172003-11-02 02:31:04 +000098static void
Edgard Lima96e74722005-12-14 22:41:47 +000099gst_xviddec_base_init (GstXvidDecClass * klass)
Iain Holmes343ee172003-11-02 02:31:04 +0000100{
Edgard Lima96e74722005-12-14 22:41:47 +0000101 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
Iain Holmes343ee172003-11-02 02:31:04 +0000102
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +0000103 gst_element_class_add_pad_template (element_class,
104 gst_static_pad_template_get (&sink_template));
105 gst_element_class_add_pad_template (element_class,
106 gst_static_pad_template_get (&src_template));
Iain Holmes343ee172003-11-02 02:31:04 +0000107
Tim-Philipp Müller32ba17c2012-10-17 17:34:26 +0100108 gst_element_class_set_static_metadata (element_class, "XviD video decoder",
Benjamin Otte775c7582010-03-18 17:30:26 +0100109 "Codec/Decoder/Video",
110 "XviD decoder based on xvidcore",
111 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
Iain Holmes343ee172003-11-02 02:31:04 +0000112}
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000113
114static void
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +0000115gst_xviddec_class_init (GstXvidDecClass * klass)
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000116{
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000117 GstElementClass *gstelement_class = (GstElementClass *) klass;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000118
Edgard Lima96e74722005-12-14 22:41:47 +0000119 parent_class = g_type_class_peek_parent (klass);
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000120
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000121 GST_DEBUG_CATEGORY_INIT (xviddec_debug, "xviddec", 0, "XviD decoder");
122
123 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_xviddec_change_state);
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000124}
125
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000126
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000127static void
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000128gst_xviddec_init (GstXvidDec * dec)
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000129{
130 /* create the sink pad */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000131 dec->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
132 gst_pad_set_chain_function (dec->sinkpad,
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000133 GST_DEBUG_FUNCPTR (gst_xviddec_chain));
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000134 gst_pad_set_setcaps_function (dec->sinkpad,
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000135 GST_DEBUG_FUNCPTR (gst_xviddec_setcaps));
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000136 gst_pad_set_event_function (dec->sinkpad,
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000137 GST_DEBUG_FUNCPTR (gst_xviddec_handle_sink_event));
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000138 gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000139
140 /* create the src pad */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000141 dec->srcpad = gst_pad_new_from_static_template (&src_template, "src");
142 gst_pad_use_fixed_caps (dec->srcpad);
143 gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000144
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000145 gst_xviddec_reset (dec);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000146}
147
148
149static void
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000150gst_xviddec_reset (GstXvidDec * dec)
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000151{
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000152 /* size, etc. */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000153 dec->width = dec->height = dec->csp = -1;
154 dec->fps_n = dec->par_n = -1;
155 dec->fps_d = dec->par_d = 1;
156 dec->next_ts = dec->next_dur = GST_CLOCK_TIME_NONE;
157 dec->outbuf_size = 0;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000158
159 /* set xvid handle to NULL */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000160 dec->handle = NULL;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000161
162 /* no delayed timestamp to start with */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000163 dec->have_ts = FALSE;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000164
165 /* need keyframe to get going */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000166 dec->waiting_for_key = TRUE;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000167}
168
169
170static void
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000171gst_xviddec_unset (GstXvidDec * dec)
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000172{
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000173 /* release XviD decoder */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000174 xvid_decore (dec->handle, XVID_DEC_DESTROY, NULL, NULL);
175 dec->handle = NULL;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000176}
177
178
179static gboolean
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000180gst_xviddec_handle_sink_event (GstPad * pad, GstEvent * event)
181{
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000182 GstXvidDec *dec = GST_XVIDDEC (GST_PAD_PARENT (pad));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000183
184 switch (GST_EVENT_TYPE (event)) {
185 case GST_EVENT_EOS:
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000186 gst_xviddec_flush_buffers (dec, TRUE);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000187 break;
188 case GST_EVENT_FLUSH_STOP:
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000189 gst_xviddec_flush_buffers (dec, FALSE);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000190 break;
191 case GST_EVENT_NEWSEGMENT:
192 /* don't really mind about the actual segment info,
193 * but we do need to recover from this possible jump */
194 /* FIXME, NEWSEGMENT is not a discontinuity. A decoder
195 * should clip the output to the segment boundaries.
196 * Also the rate field of the segment can be used to
197 * optimize the decoding, like skipping B frames when
198 * playing at double speed.
199 * The DISCONT flag on buffers should be used to detect
200 * discontinuities.
201 */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000202 dec->waiting_for_key = TRUE;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000203 break;
204 default:
205 break;
206 }
207
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000208 return gst_pad_push_event (dec->srcpad, event);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000209}
210
211
212static gboolean
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000213gst_xviddec_setup (GstXvidDec * dec)
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000214{
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000215 xvid_dec_create_t xdec;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000216 gint ret;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000217
218 /* initialise parameters, see xvid documentation */
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000219 gst_xvid_init_struct (xdec);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000220 /* let the decoder handle this, don't trust the container */
221 xdec.width = 0;
222 xdec.height = 0;
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000223 xdec.handle = NULL;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000224
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000225 GST_DEBUG_OBJECT (dec, "Initializing xvid decoder with parameters "
226 "%dx%d@%d", dec->width, dec->height, dec->csp);
227
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +0000228 if ((ret = xvid_decore (NULL, XVID_DEC_CREATE, &xdec, NULL)) < 0) {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000229 GST_WARNING_OBJECT (dec, "Initializing xvid decoder failed: %s (%d)",
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000230 gst_xvid_error (ret), ret);
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000231 return FALSE;
232 }
233
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000234 dec->handle = xdec.handle;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000235
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000236 return TRUE;
237}
238
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000239
240static void
241gst_xviddec_add_par (GstStructure * structure,
242 gint mux_par_n, gint mux_par_d, gint dec_par_n, gint dec_par_d)
243{
244 /* muxer wins if decoder has nothing interesting to offer */
245 if (dec_par_n == dec_par_d) {
246 gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION,
247 mux_par_n, mux_par_d, NULL);
248 } else {
249 gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION,
250 dec_par_n, dec_par_d, NULL);
251 }
252}
253
254
255/* based on the decoder info, if provided, and xviddec info,
256 construct a caps and send on to src pad */
257static gboolean
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000258gst_xviddec_negotiate (GstXvidDec * dec, xvid_dec_stats_t * xstats)
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000259{
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000260 gboolean ret;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000261 gint par_width, par_height;
262 GstCaps *caps;
263
264 /* note: setcaps call with no xstats info,
265 so definitely need to negotiate then */
266 if (xstats && (xstats->type != XVID_TYPE_VOL
267 || (xstats->type == XVID_TYPE_VOL
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000268 && dec->width == xstats->data.vol.width
269 && dec->height == xstats->data.vol.height)))
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000270 return TRUE;
271
272 switch (xstats ? xstats->data.vol.par : XVID_PAR_11_VGA) {
273 case XVID_PAR_11_VGA:
274 par_width = par_height = 1;
275 break;
276 case XVID_PAR_43_PAL:
277 case XVID_PAR_43_NTSC:
278 par_width = 4;
279 par_height = 3;
280 break;
281 case XVID_PAR_169_PAL:
282 case XVID_PAR_169_NTSC:
283 par_width = 16;
284 par_height = 9;
285 break;
286 case XVID_PAR_EXT:
287 default:
288 par_width = xstats->data.vol.par_width;
289 par_height = xstats->data.vol.par_height;
290 }
291
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000292 caps = gst_xvid_csp_to_caps (dec->csp, dec->width, dec->height);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000293
294 /* can only provide framerate if we received one */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000295 if (dec->fps_n != -1) {
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000296 gst_structure_set (gst_caps_get_structure (caps, 0), "framerate",
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000297 GST_TYPE_FRACTION, dec->fps_n, dec->fps_d, NULL);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000298 }
299
300 gst_xviddec_add_par (gst_caps_get_structure (caps, 0),
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000301 dec->par_n, dec->par_d, par_width, par_height);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000302
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000303 GST_LOG ("setting caps on source pad: %" GST_PTR_FORMAT, caps);
304 ret = gst_pad_set_caps (dec->srcpad, caps);
305 gst_caps_unref (caps);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000306
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000307 return ret;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000308}
309
Edgard Lima96e74722005-12-14 22:41:47 +0000310static GstFlowReturn
311gst_xviddec_chain (GstPad * pad, GstBuffer * buf)
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000312{
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000313 GstXvidDec *dec;
Edgard Lima96e74722005-12-14 22:41:47 +0000314 GstBuffer *outbuf = NULL;
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000315 xvid_dec_frame_t xframe;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000316 xvid_dec_stats_t xstats;
317 gint ret;
Vincent Penquerc'hc696b542011-01-11 10:32:47 +0000318 guint8 *data, *dupe = NULL;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000319 guint size;
320 GstFlowReturn fret;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000321
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000322 dec = GST_XVIDDEC (GST_OBJECT_PARENT (pad));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000323
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000324 if (!dec->handle)
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000325 goto not_negotiated;
326
327 fret = GST_FLOW_OK;
328
Julien Moutte51ef42d2007-03-27 18:00:56 +0000329 GST_LOG_OBJECT (dec, "Received buffer of time %" GST_TIME_FORMAT
330 " duration %" GST_TIME_FORMAT ", size %d",
331 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
332 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_SIZE (buf));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000333
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000334 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT)) {
335 /* FIXME: should we do anything here, like flush the decoder? */
336 }
337
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000338 data = GST_BUFFER_DATA (buf);
339 size = GST_BUFFER_SIZE (buf);
340
Vincent Penquerc'hc696b542011-01-11 10:32:47 +0000341 /* xvidcore overreads the input buffer, we need to alloc some extra padding
342 * to make things work reliably */
343#define EXTRA_PADDING 16
344 if (EXTRA_PADDING > 0) {
345 dupe = g_malloc (size + EXTRA_PADDING);
346 memcpy (dupe, data, size);
347 memset (dupe + size, 0, EXTRA_PADDING);
348 data = dupe;
349 }
350
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000351 do { /* loop needed because xvidcore may return vol information */
352 /* decode and so ... */
353 gst_xvid_init_struct (xframe);
354 xframe.general = XVID_LOWDELAY;
355 xframe.bitstream = (void *) data;
356 xframe.length = size;
357
358 gst_xvid_init_struct (xstats);
359
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000360 if (outbuf == NULL) {
361 fret = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET_NONE,
362 dec->outbuf_size, GST_PAD_CAPS (dec->srcpad), &outbuf);
363 if (fret != GST_FLOW_OK)
364 goto done;
365 }
366
367 gst_xvid_image_fill (&xframe.output, GST_BUFFER_DATA (outbuf),
368 dec->csp, dec->width, dec->height);
369
370 ret = xvid_decore (dec->handle, XVID_DEC_DECODE, &xframe, &xstats);
371 if (ret < 0)
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000372 goto decode_error;
373
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000374 GST_LOG_OBJECT (dec, "xvid produced output, type %d, consumed %d",
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000375 xstats.type, ret);
376
377 if (xstats.type == XVID_TYPE_VOL)
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000378 gst_xviddec_negotiate (dec, &xstats);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000379
380 data += ret;
381 size -= ret;
382 } while (xstats.type <= 0 && size > 0);
383
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000384 /* 1 byte is frequently left over */
385 if (size > 1) {
386 GST_WARNING_OBJECT (dec, "decoder did not consume all input");
387 }
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000388
389 /* FIXME, reflow the multiple return exit points */
390 if (xstats.type > 0) { /* some real output was produced */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000391 if (G_UNLIKELY (dec->waiting_for_key)) {
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000392 if (xstats.type != XVID_TYPE_IVOP)
393 goto dropping;
394
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000395 dec->waiting_for_key = FALSE;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000396 }
397 /* bframes can cause a delay in frames being returned
398 non keyframe timestamps can permute a bit between
399 encode and display order, but should match for keyframes */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000400 if (dec->have_ts) {
401 GST_BUFFER_TIMESTAMP (outbuf) = dec->next_ts;
402 GST_BUFFER_DURATION (outbuf) = dec->next_dur;
403 dec->next_ts = GST_BUFFER_TIMESTAMP (buf);
404 dec->next_dur = GST_BUFFER_DURATION (buf);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000405 } else {
406 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
407 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
408 }
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000409 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dec->srcpad));
Julien Moutte51ef42d2007-03-27 18:00:56 +0000410 GST_LOG_OBJECT (dec, "pushing buffer with pts %" GST_TIME_FORMAT
411 " duration %" GST_TIME_FORMAT,
412 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
413 GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000414 fret = gst_pad_push (dec->srcpad, outbuf);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000415
416 } else { /* no real output yet, delay in frames being returned */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000417 if (G_UNLIKELY (dec->have_ts)) {
418 GST_WARNING_OBJECT (dec,
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000419 "xvid decoder produced no output, but timestamp %" GST_TIME_FORMAT
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000420 " already queued", GST_TIME_ARGS (dec->next_ts));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000421 } else {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000422 dec->have_ts = TRUE;
423 dec->next_ts = GST_BUFFER_TIMESTAMP (buf);
Julien Moutte51ef42d2007-03-27 18:00:56 +0000424 dec->next_dur = GST_BUFFER_DURATION (buf);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000425 }
426 gst_buffer_unref (outbuf);
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000427 }
428
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000429done:
Vincent Penquerc'hc696b542011-01-11 10:32:47 +0000430 g_free (dupe);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000431 gst_buffer_unref (buf);
Edgard Lima96e74722005-12-14 22:41:47 +0000432
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000433 return fret;
Edgard Lima96e74722005-12-14 22:41:47 +0000434
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000435 /* ERRORS */
Edgard Lima96e74722005-12-14 22:41:47 +0000436not_negotiated:
437 {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000438 GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
Edgard Lima96e74722005-12-14 22:41:47 +0000439 ("format wasn't negotiated before chain function"));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000440 fret = GST_FLOW_NOT_NEGOTIATED;
441 goto done;
Edgard Lima96e74722005-12-14 22:41:47 +0000442 }
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000443decode_error:
Edgard Lima96e74722005-12-14 22:41:47 +0000444 {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000445 /* FIXME: shouldn't error out fatally/properly after N decoding errors? */
446 GST_ELEMENT_WARNING (dec, STREAM, DECODE, (NULL),
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000447 ("Error decoding xvid frame: %s (%d)", gst_xvid_error (ret), ret));
448 if (outbuf)
449 gst_buffer_unref (outbuf);
450 goto done;
Edgard Lima96e74722005-12-14 22:41:47 +0000451 }
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000452dropping:
453 {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000454 GST_WARNING_OBJECT (dec, "Dropping non-keyframe (seek/init)");
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000455 if (outbuf)
456 gst_buffer_unref (outbuf);
457 goto done;
458 }
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000459}
460
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000461
462/* flush xvid encoder buffers caused by bframe usage;
463 not well tested */
464static void
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000465gst_xviddec_flush_buffers (GstXvidDec * dec, gboolean send)
Ronald S. Bultjeb55427f2004-01-04 09:55:16 +0000466{
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000467#if 0
468 gint ret;
469 GstBuffer *outbuf = NULL;
470 xvid_dec_frame_t xframe;
471 xvid_dec_stats_t xstats;
472#endif
473
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000474 GST_DEBUG_OBJECT (dec, "flushing buffers with send %d, have_ts %d",
475 send, dec->have_ts);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000476
477 /* no need to flush if there is no delayed time-stamp */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000478 if (!dec->have_ts)
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000479 return;
480
481 /* flushing must reset the timestamp keeping */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000482 dec->have_ts = FALSE;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000483
484 /* also no need to flush if no handle */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000485 if (!dec->handle)
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000486 return;
487
488 /* unlike encoder, decoder does not seem to like flushing, disable for now */
489#if 0
490 gst_xvid_init_struct (xframe);
491 gst_xvid_init_struct (xstats);
492
493 /* init a fake frame to force flushing */
494 xframe.bitstream = NULL;
495 xframe.length = -1;
496
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000497 ret = gst_xviddec_decode (dec, xframe, &outbuf, &xstats);
498 GST_DEBUG_OBJECT (dec, "received frame when flushing, type %d, size %d",
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000499 xstats.type, ret);
500
501 if (ret > 0 && send) {
502 /* we have some valid return frame, give it the delayed timestamp and send */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000503 GST_BUFFER_TIMESTAMP (outbuf) = dec->next_ts;
504 GST_BUFFER_DURATION (outbuf) = dec->next_dur;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000505
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000506 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dec->srcpad));
507 gst_pad_push (dec->srcpad, outbuf);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000508 return;
509 }
510
511 if (outbuf)
512 gst_buffer_unref (outbuf);
513#else
514 return;
515#endif
516}
517
518#if 0
519static GstCaps *
520gst_xviddec_src_getcaps (GstPad * pad)
521{
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000522 GstXvidDec *dec = GST_XVIDDEC (GST_PAD_PARENT (pad));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000523 GstCaps *caps;
Ronald S. Bultjeb55427f2004-01-04 09:55:16 +0000524 gint csp[] = {
525 XVID_CSP_I420,
526 XVID_CSP_YV12,
527 XVID_CSP_YUY2,
528 XVID_CSP_UYVY,
529 XVID_CSP_YVYU,
530 XVID_CSP_BGRA,
531 XVID_CSP_ABGR,
532 XVID_CSP_RGBA,
Jan Schmidt0051f0e2004-01-06 14:34:52 +0000533#ifdef XVID_CSP_ARGB
Ronald S. Bultjeb55427f2004-01-04 09:55:16 +0000534 XVID_CSP_ARGB,
Jan Schmidt0051f0e2004-01-06 14:34:52 +0000535#endif
Ronald S. Bultjeb55427f2004-01-04 09:55:16 +0000536 XVID_CSP_BGR,
537 XVID_CSP_RGB555,
538 XVID_CSP_RGB565,
539 0
540 }, i;
541
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000542 if (!GST_PAD_CAPS (dec->sinkpad)) {
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000543 GstPadTemplate *templ = gst_static_pad_template_get (&src_template);
Thomas Vander Stichele7a778ee2004-03-14 22:34:33 +0000544
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000545 return gst_caps_copy (gst_pad_template_get_caps (templ));
Ronald S. Bultjeb55427f2004-01-04 09:55:16 +0000546 }
547
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000548 caps = gst_caps_new_empty ();
549 for (i = 0; csp[i] != 0; i++) {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000550 GstCaps *one = gst_xvid_csp_to_caps (csp[i], dec->width,
551 dec->height, dec->fps, dec->par);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000552
553 gst_caps_append (caps, one);
554 }
555
556 return caps;
Ronald S. Bultjeb55427f2004-01-04 09:55:16 +0000557}
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000558#endif
559
Edgard Lima96e74722005-12-14 22:41:47 +0000560static gboolean
561gst_xviddec_setcaps (GstPad * pad, GstCaps * caps)
Ronald S. Bultje95011fd2003-07-06 20:49:52 +0000562{
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000563 GstXvidDec *dec = GST_XVIDDEC (GST_PAD_PARENT (pad));
David Schleefb144bc62003-12-22 01:47:09 +0000564 GstStructure *structure;
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000565 GstCaps *allowed_caps;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000566 const GValue *val;
567
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000568 GST_LOG_OBJECT (dec, "caps %" GST_PTR_FORMAT, caps);
Ronald S. Bultje95011fd2003-07-06 20:49:52 +0000569
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000570 /* if there's something old around, remove it */
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000571 if (dec->handle) {
572 gst_xviddec_unset (dec);
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000573 }
574
Edgard Lima96e74722005-12-14 22:41:47 +0000575 structure = gst_caps_get_structure (caps, 0);
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000576 gst_structure_get_int (structure, "width", &dec->width);
577 gst_structure_get_int (structure, "height", &dec->height);
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000578
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000579 /* perhaps some fps info */
580 val = gst_structure_get_value (structure, "framerate");
581 if ((val != NULL) && GST_VALUE_HOLDS_FRACTION (val)) {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000582 dec->fps_n = gst_value_get_fraction_numerator (val);
583 dec->fps_d = gst_value_get_fraction_denominator (val);
Edgard Lima96e74722005-12-14 22:41:47 +0000584 } else {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000585 dec->fps_n = -1;
586 dec->fps_d = 1;
Edgard Lima96e74722005-12-14 22:41:47 +0000587 }
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000588
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000589 /* perhaps some par info */
590 val = gst_structure_get_value (structure, "pixel-aspect-ratio");
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000591 if (val != NULL && GST_VALUE_HOLDS_FRACTION (val)) {
592 dec->par_n = gst_value_get_fraction_numerator (val);
593 dec->par_d = gst_value_get_fraction_denominator (val);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000594 } else {
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000595 dec->par_n = 1;
596 dec->par_d = 1;
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000597 }
Edgard Lima9a85a172005-12-15 14:39:00 +0000598
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000599 /* we try to find the preferred/accept csp */
600 allowed_caps = gst_pad_get_allowed_caps (dec->srcpad);
601 if (!allowed_caps) {
602 GST_DEBUG_OBJECT (dec, "... but no peer, using template caps");
603 /* need to copy because get_allowed_caps returns a ref,
604 and get_pad_template_caps doesn't */
605 allowed_caps = gst_caps_copy (gst_pad_get_pad_template_caps (dec->srcpad));
606 }
607 GST_LOG_OBJECT (dec, "allowed source caps %" GST_PTR_FORMAT, allowed_caps);
Edgard Lima9a85a172005-12-15 14:39:00 +0000608
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000609 /* pick the first one ... */
610 structure = gst_caps_get_structure (allowed_caps, 0);
611 val = gst_structure_get_value (structure, "format");
612 if (val != NULL && G_VALUE_TYPE (val) == GST_TYPE_LIST) {
613 GValue temp = { 0, };
614 gst_value_init_and_copy (&temp, gst_value_list_get_value (val, 0));
615 gst_structure_set_value (structure, "format", &temp);
616 g_value_unset (&temp);
617 }
Edgard Lima9a85a172005-12-15 14:39:00 +0000618
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000619 /* ... and use its info to get the csp */
620 dec->csp = gst_xvid_structure_to_csp (structure);
621 if (dec->csp == -1) {
622 GST_WARNING_OBJECT (dec, "failed to decide on colorspace, using I420");
623 dec->csp = XVID_CSP_I420;
624 }
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000625
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000626 dec->outbuf_size =
627 gst_xvid_image_get_size (dec->csp, dec->width, dec->height);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000628
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000629 GST_LOG_OBJECT (dec, "csp=%d, outbuf_size=%d", dec->csp, dec->outbuf_size);
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000630
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000631 gst_caps_unref (allowed_caps);
632
633 /* now set up xvid ... */
634 if (!gst_xviddec_setup (dec)) {
635 GST_ELEMENT_ERROR (GST_ELEMENT (dec), LIBRARY, INIT, (NULL), (NULL));
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000636 return FALSE;
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000637 }
638
639 return gst_xviddec_negotiate (dec, NULL);
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000640}
641
Jan Schmidt012dfb82005-09-05 17:20:29 +0000642static GstStateChangeReturn
643gst_xviddec_change_state (GstElement * element, GstStateChange transition)
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000644{
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000645 GstXvidDec *dec = GST_XVIDDEC (element);
Edgard Lima96e74722005-12-14 22:41:47 +0000646 GstStateChangeReturn ret;
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000647
Edgard Lima96e74722005-12-14 22:41:47 +0000648 switch (transition) {
649 case GST_STATE_CHANGE_NULL_TO_READY:
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000650 if (!gst_xvid_init ())
651 return GST_STATE_CHANGE_FAILURE;
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000652 break;
653 default:
654 break;
655 }
656
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000657 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
658 if (ret == GST_STATE_CHANGE_FAILURE)
659 goto done;
Ronald S. Bultje47d40102004-01-03 19:38:40 +0000660
Edgard Lima96e74722005-12-14 22:41:47 +0000661 switch (transition) {
Edgard Lima96e74722005-12-14 22:41:47 +0000662 case GST_STATE_CHANGE_PAUSED_TO_READY:
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000663 gst_xviddec_flush_buffers (dec, FALSE);
664 if (dec->handle) {
665 gst_xviddec_unset (dec);
Edgard Lima96e74722005-12-14 22:41:47 +0000666 }
Tim-Philipp Müllerd2f55672006-07-28 12:14:06 +0000667 gst_xviddec_reset (dec);
Edgard Lima96e74722005-12-14 22:41:47 +0000668 break;
669 default:
670 break;
671 }
672
Mark Nauwelaerts39f82f22006-05-12 09:28:15 +0000673done:
Edgard Lima96e74722005-12-14 22:41:47 +0000674 return ret;
Ronald S. Bultje08889d72003-04-22 14:55:12 +0000675}