2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include "gstffmpegcodecmap.h"
31 GST_DEBUG_CATEGORY (ffmpegcolorspace_debug);
32 #define GST_CAT_DEFAULT ffmpegcolorspace_debug
34 #define GST_TYPE_FFMPEGCSP \
35 (gst_ffmpegcsp_get_type())
36 #define GST_FFMPEGCSP(obj) \
37 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGCSP,GstFFMpegCsp))
38 #define GST_FFMPEGCSP_CLASS(klass) \
39 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGCSP,GstFFMpegCsp))
40 #define GST_IS_FFMPEGCSP(obj) \
41 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGCSP))
42 #define GST_IS_FFMPEGCSP_CLASS(obj) \
43 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGCSP))
45 typedef struct _GstFFMpegCsp GstFFMpegCsp;
46 typedef struct _GstFFMpegCspClass GstFFMpegCspClass;
52 GstPad *sinkpad, *srcpad;
56 enum PixelFormat from_pixfmt, to_pixfmt;
57 AVPicture from_frame, to_frame;
58 AVPaletteControl *palette;
60 GstCaps *src_prefered;
61 GstCaps *sink_prefered;
64 struct _GstFFMpegCspClass
66 GstElementClass parent_class;
69 /* elementfactory information */
70 static GstElementDetails ffmpegcsp_details = {
71 "FFMPEG Colorspace converter",
72 "Filter/Converter/Video",
73 "Converts video from one colorspace to another",
74 "Ronald Bultje <rbultje@ronald.bitfreak.net>",
78 /* Stereo signals and args */
90 static GType gst_ffmpegcsp_get_type (void);
92 static void gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass);
93 static void gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass);
94 static void gst_ffmpegcsp_init (GstFFMpegCsp * space);
96 static void gst_ffmpegcsp_set_property (GObject * object,
97 guint prop_id, const GValue * value, GParamSpec * pspec);
98 static void gst_ffmpegcsp_get_property (GObject * object,
99 guint prop_id, GValue * value, GParamSpec * pspec);
101 static GstFlowReturn gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer);
102 static GstElementStateReturn gst_ffmpegcsp_change_state (GstElement * element);
104 static GstPadTemplate *srctempl, *sinktempl;
105 static GstElementClass *parent_class = NULL;
107 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
111 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
114 GstStructure *structure;
117 caps = gst_caps_make_writable (caps);
119 for (i = 0; i < gst_caps_get_size (caps); i++) {
120 structure = gst_caps_get_structure (caps, i);
122 gst_structure_set_name (structure, "video/x-raw-yuv");
123 gst_structure_remove_field (structure, "format");
124 gst_structure_remove_field (structure, "endianness");
125 gst_structure_remove_field (structure, "depth");
126 gst_structure_remove_field (structure, "bpp");
127 gst_structure_remove_field (structure, "red_mask");
128 gst_structure_remove_field (structure, "green_mask");
129 gst_structure_remove_field (structure, "blue_mask");
130 gst_structure_remove_field (structure, "alpha_mask");
133 gst_caps_do_simplify (caps);
134 rgbcaps = gst_caps_copy (caps);
136 for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
137 structure = gst_caps_get_structure (rgbcaps, i);
139 gst_structure_set_name (structure, "video/x-raw-rgb");
142 gst_caps_append (caps, rgbcaps);
148 gst_ffmpegcsp_getcaps (GstPad * pad)
155 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
157 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
158 /* we can do whatever the peer can */
159 othercaps = gst_pad_peer_get_caps (otherpad);
160 if (othercaps != NULL) {
161 /* without the format info */
162 othercaps = gst_ffmpegcsp_caps_remove_format_info (othercaps);
163 /* and filtered against our padtemplate */
164 caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
165 gst_caps_unref (othercaps);
167 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
174 gst_ffmpegcsp_configure_context (GstPad * pad, const GstCaps * caps, gint width,
180 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
182 /* loop over all possibilities and select the first one we can convert and
183 * is accepted by the peer */
184 ctx = avcodec_alloc_context ();
187 ctx->height = height;
188 ctx->pix_fmt = PIX_FMT_NB;
189 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
190 if (ctx->pix_fmt == PIX_FMT_NB) {
193 /* we disable ourself here */
194 if (pad == space->srcpad) {
195 space->to_pixfmt = PIX_FMT_NB;
197 space->from_pixfmt = PIX_FMT_NB;
202 if (pad == space->srcpad) {
203 space->to_pixfmt = ctx->pix_fmt;
205 space->from_pixfmt = ctx->pix_fmt;
209 av_free (space->palette);
210 space->palette = ctx->palctrl;
217 /* configureing the caps on a pad means that we should check if we
218 * can get a fic format for that caps. Then we need to figure out
219 * how we can convert that to the peer format */
221 gst_ffmpegcsp_setcaps (GstPad * pad, GstCaps * caps)
223 GstStructure *structure;
229 const GValue *par = NULL;
230 GstCaps **other_prefered, **prefered;
232 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
234 GST_DEBUG_OBJECT (space, "setcaps on %s:%s with caps %" GST_PTR_FORMAT,
235 GST_DEBUG_PAD_NAME (pad), caps);
237 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
239 (pad == space->srcpad) ? &space->src_prefered : &space->sink_prefered;
241 (pad == space->srcpad) ? &space->sink_prefered : &space->src_prefered;
243 structure = gst_caps_get_structure (caps, 0);
244 gst_structure_get_int (structure, "width", &width);
245 gst_structure_get_int (structure, "height", &height);
246 gst_structure_get_double (structure, "framerate", &framerate);
247 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
249 if (!gst_ffmpegcsp_configure_context (pad, caps, width, height))
250 goto configure_error;
254 otherpeer = gst_pad_get_peer (otherpad);
256 /* check passthrough */
257 //if (gst_pad_accept_caps (otherpeer, caps)) {
259 *other_prefered = gst_caps_ref (caps);
263 /* set the size on the otherpad */
264 othercaps = gst_pad_get_caps (otherpeer);
266 GstCaps *targetcaps = gst_caps_copy_nth (othercaps, 0);
268 gst_caps_unref (othercaps);
270 gst_caps_set_simple (targetcaps,
271 "width", G_TYPE_INT, width,
272 "height", G_TYPE_INT, height,
273 "framerate", G_TYPE_DOUBLE, framerate, NULL);
276 gst_caps_set_simple (targetcaps,
277 "pixel-aspect-ratio", GST_TYPE_FRACTION,
278 gst_value_get_fraction_numerator (par),
279 gst_value_get_fraction_denominator (par), NULL);
281 *other_prefered = targetcaps;
284 gst_object_unref (GST_OBJECT (otherpeer));
287 space->width = width;
288 space->height = height;
294 GST_DEBUG ("could not configure context");
300 gst_ffmpegcsp_get_type (void)
302 static GType ffmpegcsp_type = 0;
304 if (!ffmpegcsp_type) {
305 static const GTypeInfo ffmpegcsp_info = {
306 sizeof (GstFFMpegCspClass),
307 (GBaseInitFunc) gst_ffmpegcsp_base_init,
309 (GClassInitFunc) gst_ffmpegcsp_class_init,
312 sizeof (GstFFMpegCsp),
314 (GInstanceInitFunc) gst_ffmpegcsp_init,
317 ffmpegcsp_type = g_type_register_static (GST_TYPE_ELEMENT,
318 "GstFFMpegColorspace", &ffmpegcsp_info, 0);
321 return ffmpegcsp_type;
325 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
327 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
329 gst_element_class_add_pad_template (element_class, srctempl);
330 gst_element_class_add_pad_template (element_class, sinktempl);
331 gst_element_class_set_details (element_class, &ffmpegcsp_details);
335 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
337 GObjectClass *gobject_class;
338 GstElementClass *gstelement_class;
340 gobject_class = (GObjectClass *) klass;
341 gstelement_class = (GstElementClass *) klass;
343 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
345 gobject_class->set_property = gst_ffmpegcsp_set_property;
346 gobject_class->get_property = gst_ffmpegcsp_get_property;
348 gstelement_class->change_state = gst_ffmpegcsp_change_state;
350 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
351 "FFMPEG-based colorspace converter");
355 gst_ffmpegcsp_init (GstFFMpegCsp * space)
357 space->sinkpad = gst_pad_new_from_template (sinktempl, "sink");
358 gst_pad_set_getcaps_function (space->sinkpad, gst_ffmpegcsp_getcaps);
359 gst_pad_set_setcaps_function (space->sinkpad, gst_ffmpegcsp_setcaps);
360 gst_pad_set_chain_function (space->sinkpad, gst_ffmpegcsp_chain);
361 gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
363 space->srcpad = gst_pad_new_from_template (srctempl, "src");
364 gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
365 gst_pad_set_getcaps_function (space->srcpad, gst_ffmpegcsp_getcaps);
366 gst_pad_set_setcaps_function (space->srcpad, gst_ffmpegcsp_setcaps);
368 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
369 space->palette = NULL;
373 gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer)
375 GstBuffer *inbuf = GST_BUFFER (buffer);
377 GstBuffer *outbuf = NULL;
381 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
383 if (!GST_PAD_IS_USABLE (space->srcpad)) {
384 gst_buffer_unref (inbuf);
385 return GST_FLOW_ERROR;
388 /* asume passthrough */
390 avpicture_get_size (space->from_pixfmt, space->width, space->height);
392 outbuf = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE, size,
393 space->src_prefered);
394 if (outbuf == NULL) {
395 return GST_FLOW_ERROR;
398 if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB) {
399 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
400 ("attempting to convert colorspaces between unknown formats"));
401 gst_buffer_unref (inbuf);
402 return GST_FLOW_NOT_NEGOTIATED;
405 if (space->from_pixfmt == space->to_pixfmt) {
406 GST_DEBUG ("passthrough conversion %" GST_PTR_FORMAT,
407 GST_PAD_CAPS (space->srcpad));
411 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
412 GST_BUFFER_DATA (inbuf),
413 space->from_pixfmt, space->width, space->height);
415 space->from_frame.data[1] = (uint8_t *) space->palette;
416 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
417 GST_BUFFER_DATA (outbuf),
418 space->to_pixfmt, space->width, space->height);
419 img_convert (&space->to_frame, space->to_pixfmt,
420 &space->from_frame, space->from_pixfmt, space->width, space->height);
422 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (inbuf);
423 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (inbuf);
424 gst_buffer_unref (inbuf);
427 return gst_pad_push (space->srcpad, outbuf);
430 static GstElementStateReturn
431 gst_ffmpegcsp_change_state (GstElement * element)
435 space = GST_FFMPEGCSP (element);
437 switch (GST_STATE_TRANSITION (element)) {
438 case GST_STATE_PAUSED_TO_READY:
440 av_free (space->palette);
441 space->palette = NULL;
445 if (parent_class->change_state)
446 return parent_class->change_state (element);
448 return GST_STATE_SUCCESS;
452 gst_ffmpegcsp_set_property (GObject * object,
453 guint prop_id, const GValue * value, GParamSpec * pspec)
457 /* it's not null if we got it, but it might not be ours */
458 g_return_if_fail (GST_IS_FFMPEGCSP (object));
459 space = GST_FFMPEGCSP (object);
468 gst_ffmpegcsp_get_property (GObject * object,
469 guint prop_id, GValue * value, GParamSpec * pspec)
473 /* it's not null if we got it, but it might not be ours */
474 g_return_if_fail (GST_IS_FFMPEGCSP (object));
475 space = GST_FFMPEGCSP (object);
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 gst_ffmpegcolorspace_register (GstPlugin * plugin)
490 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
492 /* build templates */
493 srctempl = gst_pad_template_new ("src",
494 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
496 /* the sink template will do palette handling as well... */
497 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
499 return gst_element_register (plugin, "ffmpegcolorspace",
500 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);