1 /* GStreamer divx decoder plugin
2 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
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.
25 #include "gstdivxdec.h"
26 #include <gst/video/video.h>
28 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
31 GST_STATIC_CAPS ("video/x-divx, "
32 "divxversion = (int) [ 3, 5 ], "
33 "width = (int) [ 16, 4096 ], "
34 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [0/1, MAX]")
37 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
40 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YUY2, YV12, UYVY }")
41 /* FIXME: 15/16/24/32bpp RGB */
46 /* DivxDec signals and args */
60 static void gst_divxdec_base_init (GstDivxDecClass * klass);
61 static void gst_divxdec_class_init (GstDivxDecClass * klass);
62 static void gst_divxdec_init (GstDivxDec * divxdec);
63 static void gst_divxdec_dispose (GObject * object);
64 static GstFlowReturn gst_divxdec_chain (GstPad * pad, GstBuffer * buf);
65 static gboolean gst_divxdec_connect (GstPad * pad, GstCaps * vscapslist);
66 static gboolean gst_divxdec_negotiate (GstDivxDec * divxdec);
67 static GstStateChangeReturn
68 gst_divxdec_change_state (GstElement * element, GstStateChange transition);
69 static GstElementClass *parent_class = NULL;
71 /* static guint gst_divxdec_signals[LAST_SIGNAL] = { 0 }; */
75 gst_divxdec_error (int errorcode)
84 error = "Invalid memory";
87 error = "Invalid format";
89 case DEC_INVALID_ARGUMENT:
90 error = "Invalid argument";
92 case DEC_NOT_IMPLEMENTED:
93 error = "Not implemented";
96 error = "Unknown error";
104 gst_divxdec_get_type (void)
106 static GType divxdec_type = 0;
109 static const GTypeInfo divxdec_info = {
110 sizeof (GstDivxDecClass),
111 (GBaseInitFunc) gst_divxdec_base_init,
113 (GClassInitFunc) gst_divxdec_class_init,
118 (GInstanceInitFunc) gst_divxdec_init,
121 divxdec_type = g_type_register_static (GST_TYPE_ELEMENT,
122 "GstDivxDec", &divxdec_info, 0);
129 gst_divxdec_base_init (GstDivxDecClass * klass)
131 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133 gst_element_class_add_static_pad_template (element_class,
135 gst_element_class_add_static_pad_template (element_class, &src_template);
137 gst_element_class_set_details_simple (element_class,
138 "Divx4linux video decoder", "Codec/Decoder/Video",
139 "Divx decoder based on divxdecore",
140 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
145 gst_divxdec_class_init (GstDivxDecClass * klass)
147 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
148 GObjectClass *gobject_class = (GObjectClass *) klass;
150 parent_class = g_type_class_peek_parent (klass);
152 gstelement_class->change_state = gst_divxdec_change_state;
153 gobject_class->dispose = gst_divxdec_dispose;
158 gst_divxdec_init (GstDivxDec * divxdec)
160 /* create the sink pad */
161 divxdec->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
162 gst_element_add_pad (GST_ELEMENT (divxdec), divxdec->sinkpad);
163 gst_pad_set_chain_function (divxdec->sinkpad, gst_divxdec_chain);
164 gst_pad_set_setcaps_function (divxdec->sinkpad, gst_divxdec_connect);
166 /* create the src pad */
167 divxdec->srcpad = gst_pad_new_from_static_template (&src_template, "src");
168 gst_element_add_pad (GST_ELEMENT (divxdec), divxdec->srcpad);
169 gst_pad_use_fixed_caps (divxdec->srcpad);
172 divxdec->width = divxdec->height = divxdec->csp = divxdec->bitcnt = -1;
173 divxdec->version = 0;
175 /* set divx handle to NULL */
176 divxdec->handle = NULL;
181 gst_divxdec_unset (GstDivxDec * divxdec)
183 if (divxdec->handle) {
184 /* unref this instance */
185 decore (divxdec->handle, DEC_OPT_RELEASE, NULL, NULL);
186 divxdec->handle = NULL;
192 gst_divxdec_setup (GstDivxDec * divxdec)
196 DivXBitmapInfoHeader output;
199 /* initialize the handle */
200 memset (&xinit, 0, sizeof (DEC_INIT));
201 xinit.smooth_playback = 0;
202 switch (divxdec->version) {
204 xinit.codec_version = 311;
207 xinit.codec_version = 400;
210 xinit.codec_version = 500;
213 xinit.codec_version = 0;
216 if ((ret = decore (&handle, DEC_OPT_INIT, &xinit, NULL)) != 0) {
217 GST_ELEMENT_ERROR (divxdec, LIBRARY, INIT, (NULL),
218 ("divx library error: %s (%d)", gst_divxdec_error (ret), ret));
222 /* we've got a handle now */
223 divxdec->handle = handle;
225 /* initialise parameters, see divx documentation */
226 memset (&output, 0, sizeof (DivXBitmapInfoHeader));
227 output.biSize = sizeof (DivXBitmapInfoHeader);
228 output.biWidth = divxdec->width;
229 output.biHeight = divxdec->height;
230 output.biBitCount = divxdec->bitcnt;
231 output.biCompression = divxdec->csp;
233 if ((ret = decore (divxdec->handle, DEC_OPT_SETOUT, &output, NULL)) != 0) {
234 GST_ELEMENT_ERROR (divxdec, LIBRARY, SETTINGS, (NULL),
235 ("error setting output: %s (%d)", gst_divxdec_error (ret), ret));
236 gst_divxdec_unset (divxdec);
243 gst_divxdec_dispose (GObject * object)
245 GstDivxDec *divxdec = GST_DIVXDEC (object);
247 gst_divxdec_unset (divxdec);
248 G_OBJECT_CLASS (parent_class)->dispose (object);
252 gst_divxdec_chain (GstPad * pad, GstBuffer * buf)
260 divxdec = GST_DIVXDEC (gst_pad_get_parent (pad));
261 if (!divxdec->handle) {
262 if (gst_divxdec_negotiate (divxdec) <= 0) {
267 outbuf = gst_buffer_new_and_alloc (divxdec->width *
268 divxdec->height * divxdec->bpp / 8);
269 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
270 GST_BUFFER_SIZE (outbuf) = divxdec->width *
271 divxdec->height * divxdec->bpp / 8;
273 /* encode and so ... */
274 xframe.bitstream = (void *) GST_BUFFER_DATA (buf);
275 xframe.bmp = (void *) GST_BUFFER_DATA (outbuf);
276 xframe.length = GST_BUFFER_SIZE (buf);
278 xframe.render_flag = 1;
280 if ((res = decore (divxdec->handle, DEC_OPT_FRAME, &xframe, NULL))) {
284 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (divxdec->srcpad));
285 ret = gst_pad_push (divxdec->srcpad, outbuf);
290 GST_ELEMENT_ERROR (divxdec, CORE, TOO_LAZY, (NULL),
291 ("No format set - aborting"));
292 ret = GST_FLOW_NOT_NEGOTIATED;
298 GST_ELEMENT_ERROR (divxdec, STREAM, DECODE, (NULL),
299 ("Error decoding divx frame: %s (%d)", gst_divxdec_error (res), res));
300 gst_buffer_unref (outbuf);
301 ret = GST_FLOW_ERROR;
307 gst_buffer_unref (buf);
308 gst_object_unref (divxdec);
314 /* FIXME: moved all the bits out here that are broken so the syntax
319 GST_MAKE_FOURCC ('R', 'G', 'B', ' '), 32, 32,
320 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
321 GST_MAKE_FOURCC ('A', 'B', 'G', 'R'), 32}
330 GST_MAKE_FOURCC ('R', 'G', 'B', ' '), 24, 24,
331 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
332 GST_MAKE_FOURCC ('A', 'B', 'G', 'R'), 24}
341 GST_MAKE_FOURCC ('R', 'G', 'B', ' '), 16, 16, 3, 16}
344 GST_MAKE_FOURCC ('R', 'G', 'B', ' '), 15, 16, 0, 16}
348 if (fmt_list[i].fourcc == GST_MAKE_FOURCC ('R', 'G', 'B', ' ')) {
349 guint32 r_mask = 0, b_mask = 0, g_mask = 0;
352 switch (fmt_list[i].depth) {
354 endianness = G_BYTE_ORDER;
360 endianness = G_BYTE_ORDER;
366 endianness = G_BIG_ENDIAN;
367 r_mask = GST_VIDEO_BYTE1_MASK_24_INT;
368 g_mask = GST_VIDEO_BYTE2_MASK_24_INT;
369 b_mask = GST_VIDEO_BYTE3_MASK_24_INT break;
371 endianness = G_BIG_ENDIAN;
372 r_mask = GST_VIDEO_BYTE1_MASK_32_INT;
373 g_mask = GST_VIDEO_BYTE2_MASK_32_INT;
374 b_mask = GST_VIDEO_BYTE3_MASK_32_INT break;
376 caps = GST_CAPS_NEW ("divxdec_src_pad_rgb",
378 "width", GST_PROPS_INT (divxdec->width),
379 "height", GST_PROPS_INT (divxdec->height),
380 "framerate", GST_PROPS_FLOAT (divxdec->fps),
381 "depth", GST_PROPS_INT (fmt_list[i].depth),
382 "bpp", GST_PROPS_INT (fmt_list[i].bpp),
383 "endianness", GST_PROPS_INT (endianness),
384 "red_mask", GST_PROPS_INT (r_mask),
385 "green_mask", GST_PROPS_INT (g_mask),
386 "blue_mask", GST_PROPS_INT (b_mask));
394 gst_divxdec_negotiate (GstDivxDec * divxdec)
396 GstCaps *caps = NULL;
398 gint par_num, par_den;
399 gboolean ret = FALSE;
411 GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'), 16, 16,
412 GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'), 0}
414 GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'), 16, 16,
415 GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'), 0}
417 GST_MAKE_FOURCC ('I', '4', '2', '0'), 12, 12,
418 GST_MAKE_FOURCC ('I', '4', '2', '0'), 0}
420 GST_MAKE_FOURCC ('Y', 'V', '1', '2'), 12, 12,
421 GST_MAKE_FOURCC ('Y', 'V', '1', '2'), 0}
426 GST_DEBUG_OBJECT (divxdec, "fps %d/%d, PAR %d/%d",
427 divxdec->fps_n, divxdec->fps_d, divxdec->par_n, divxdec->par_d);
430 * the info.aspect_* values reflect PAR;
431 * 0:0 is allowed and can be interpreted as 1:1, so correct for it */
432 par_num = divxdec->par_n;
433 par_den = divxdec->par_d;
434 if (par_num == 0 && par_den == 0) {
435 par_num = par_den = 1;
438 for (i = 0; fmt_list[i].fourcc != 0; i++) {
439 divxdec->csp = fmt_list[i].csp;
441 caps = gst_caps_new_simple ("video/x-raw-yuv",
442 "width", G_TYPE_INT, divxdec->width,
443 "height", G_TYPE_INT, divxdec->height,
444 "framerate", GST_TYPE_FRACTION, divxdec->fps_n, divxdec->fps_d,
445 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_num, par_den,
446 "format", GST_TYPE_FOURCC, fmt_list[i].fourcc, NULL);
450 if (gst_divxdec_setup (divxdec) &&
451 gst_pad_set_caps (divxdec->srcpad, caps)) {
452 divxdec->csp = fmt_list[i].csp;
453 divxdec->bpp = fmt_list[i].bpp;
454 divxdec->bitcnt = fmt_list[i].bitcnt;
459 gst_caps_unref (caps);
466 /* if we got here - it's not good */
471 gst_caps_unref (caps);
479 gst_divxdec_connect (GstPad * pad, GstCaps * caps)
484 gboolean ret = FALSE;
486 GstStructure *structure = gst_caps_get_structure (caps, 0);
488 divxdec = GST_DIVXDEC (gst_pad_get_parent (pad));
490 /* if there's something old around, remove it */
491 if (divxdec->handle) {
492 gst_divxdec_unset (divxdec);
495 /* if we get here, we know the input is divx. we
496 * only need to bother with the output colorspace */
497 gst_structure_get_int (structure, "width", &divxdec->width);
498 gst_structure_get_int (structure, "height", &divxdec->height);
499 gst_structure_get_int (structure, "divxversion", &divxdec->version);
501 /* get pixel aspect ratio if it's set */
502 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
504 divxdec->par_n = gst_value_get_fraction_numerator (par),
505 divxdec->par_d = gst_value_get_fraction_denominator (par);
508 fps = gst_structure_get_value (structure, "framerate");
510 divxdec->fps_n = gst_value_get_fraction_numerator (fps);
511 divxdec->fps_d = gst_value_get_fraction_denominator (fps);
516 ret = gst_divxdec_negotiate (divxdec);
517 gst_object_unref (divxdec);
522 static GstStateChangeReturn
523 gst_divxdec_change_state (GstElement * element, GstStateChange transition)
525 GstStateChangeReturn ret;
527 switch (transition) {
528 case GST_STATE_CHANGE_NULL_TO_READY:
530 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
536 ret = parent_class->change_state (element, transition);
538 switch (transition) {
539 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
541 case GST_STATE_CHANGE_PAUSED_TO_READY:
543 case GST_STATE_CHANGE_READY_TO_NULL:
554 plugin_init (GstPlugin * plugin)
558 lib_version = decore (NULL, DEC_OPT_VERSION, 0, 0);
559 if (lib_version != DECORE_VERSION) {
560 g_warning ("Version mismatch! This plugin was compiled for "
561 "DivX version %d, while your library has version %d!",
562 DECORE_VERSION, lib_version);
566 return gst_element_register (plugin, "divxdec",
567 GST_RANK_SECONDARY, GST_TYPE_DIVXDEC);
571 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
576 "5.03", GST_LICENSE_UNKNOWN, "divx4linux", "http://www.divx.com/");