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_bufferalloc (GstPad * pad, guint64 offset,
102 guint size, GstCaps * caps, GstBuffer ** buf);
103 static GstFlowReturn gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer);
104 static GstElementStateReturn gst_ffmpegcsp_change_state (GstElement * element);
106 static GstPadTemplate *srctempl, *sinktempl;
107 static GstElementClass *parent_class = NULL;
109 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
113 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
116 GstStructure *structure;
119 caps = gst_caps_make_writable (caps);
121 for (i = 0; i < gst_caps_get_size (caps); i++) {
122 structure = gst_caps_get_structure (caps, i);
124 gst_structure_set_name (structure, "video/x-raw-yuv");
125 gst_structure_remove_field (structure, "format");
126 gst_structure_remove_field (structure, "endianness");
127 gst_structure_remove_field (structure, "depth");
128 gst_structure_remove_field (structure, "bpp");
129 gst_structure_remove_field (structure, "red_mask");
130 gst_structure_remove_field (structure, "green_mask");
131 gst_structure_remove_field (structure, "blue_mask");
132 gst_structure_remove_field (structure, "alpha_mask");
135 gst_caps_do_simplify (caps);
136 rgbcaps = gst_caps_copy (caps);
138 for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
139 structure = gst_caps_get_structure (rgbcaps, i);
141 gst_structure_set_name (structure, "video/x-raw-rgb");
144 gst_caps_append (caps, rgbcaps);
150 gst_ffmpegcsp_getcaps (GstPad * pad)
157 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
159 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
160 /* we can do whatever the peer can */
161 othercaps = gst_pad_peer_get_caps (otherpad);
162 if (othercaps != NULL) {
163 /* without the format info */
164 othercaps = gst_ffmpegcsp_caps_remove_format_info (othercaps);
165 /* and filtered against our padtemplate */
166 caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
167 gst_caps_unref (othercaps);
169 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
176 gst_ffmpegcsp_configure_context (GstPad * pad, const GstCaps * caps, gint width,
182 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
184 /* loop over all possibilities and select the first one we can convert and
185 * is accepted by the peer */
186 ctx = avcodec_alloc_context ();
189 ctx->height = height;
190 ctx->pix_fmt = PIX_FMT_NB;
191 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
192 if (ctx->pix_fmt == PIX_FMT_NB) {
195 /* we disable ourself here */
196 if (pad == space->srcpad) {
197 space->to_pixfmt = PIX_FMT_NB;
199 space->from_pixfmt = PIX_FMT_NB;
204 if (pad == space->srcpad) {
205 space->to_pixfmt = ctx->pix_fmt;
207 space->from_pixfmt = ctx->pix_fmt;
211 av_free (space->palette);
212 space->palette = ctx->palctrl;
219 /* configureing the caps on a pad means that we should check if we
220 * can get a fic format for that caps. Then we need to figure out
221 * how we can convert that to the peer format */
223 gst_ffmpegcsp_setcaps (GstPad * pad, GstCaps * caps)
225 GstStructure *structure;
231 const GValue *par = NULL;
232 GstCaps **other_prefered, **prefered;
234 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
236 GST_DEBUG_OBJECT (space, "setcaps on %s:%s with caps %" GST_PTR_FORMAT,
237 GST_DEBUG_PAD_NAME (pad), caps);
239 otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
241 (pad == space->srcpad) ? &space->src_prefered : &space->sink_prefered;
243 (pad == space->srcpad) ? &space->sink_prefered : &space->src_prefered;
245 structure = gst_caps_get_structure (caps, 0);
246 gst_structure_get_int (structure, "width", &width);
247 gst_structure_get_int (structure, "height", &height);
248 gst_structure_get_double (structure, "framerate", &framerate);
249 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
251 if (!gst_ffmpegcsp_configure_context (pad, caps, width, height))
252 goto configure_error_source;
254 gst_caps_replace (prefered, caps);
256 otherpeer = gst_pad_get_peer (otherpad);
258 /* check passthrough */
259 if (gst_pad_accept_caps (otherpeer, caps)) {
260 if (!gst_ffmpegcsp_configure_context (otherpad, caps, width, height))
261 goto configure_error_target;
262 gst_caps_replace (other_prefered, caps);
266 /* set the size on the otherpad */
267 othercaps = gst_pad_get_caps (otherpeer);
269 GstCaps *targetcaps = gst_caps_copy_nth (othercaps, 0);
271 gst_caps_unref (othercaps);
273 gst_caps_set_simple (targetcaps,
274 "width", G_TYPE_INT, width,
275 "height", G_TYPE_INT, height,
276 "framerate", G_TYPE_DOUBLE, framerate, NULL);
279 gst_caps_set_simple (targetcaps,
280 "pixel-aspect-ratio", GST_TYPE_FRACTION,
281 gst_value_get_fraction_numerator (par),
282 gst_value_get_fraction_denominator (par), NULL);
284 if (!gst_ffmpegcsp_configure_context (otherpad, targetcaps, width,
286 gst_caps_unref (targetcaps);
287 goto configure_error_target;
289 gst_caps_replace (other_prefered, targetcaps);
290 gst_caps_unref (targetcaps);
293 gst_object_unref (GST_OBJECT (otherpeer));
296 space->width = width;
297 space->height = height;
301 configure_error_source:
303 GST_DEBUG ("could not configure context for source");
306 configure_error_target:
308 gst_object_unref (GST_OBJECT (otherpeer));
309 GST_DEBUG ("could not configure context for target");
315 gst_ffmpegcsp_get_type (void)
317 static GType ffmpegcsp_type = 0;
319 if (!ffmpegcsp_type) {
320 static const GTypeInfo ffmpegcsp_info = {
321 sizeof (GstFFMpegCspClass),
322 (GBaseInitFunc) gst_ffmpegcsp_base_init,
324 (GClassInitFunc) gst_ffmpegcsp_class_init,
327 sizeof (GstFFMpegCsp),
329 (GInstanceInitFunc) gst_ffmpegcsp_init,
332 ffmpegcsp_type = g_type_register_static (GST_TYPE_ELEMENT,
333 "GstFFMpegColorspace", &ffmpegcsp_info, 0);
336 return ffmpegcsp_type;
340 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
342 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
344 gst_element_class_add_pad_template (element_class, srctempl);
345 gst_element_class_add_pad_template (element_class, sinktempl);
346 gst_element_class_set_details (element_class, &ffmpegcsp_details);
350 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
352 GObjectClass *gobject_class;
353 GstElementClass *gstelement_class;
355 gobject_class = (GObjectClass *) klass;
356 gstelement_class = (GstElementClass *) klass;
358 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
360 gobject_class->set_property = gst_ffmpegcsp_set_property;
361 gobject_class->get_property = gst_ffmpegcsp_get_property;
363 gstelement_class->change_state = gst_ffmpegcsp_change_state;
365 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
366 "FFMPEG-based colorspace converter");
370 gst_ffmpegcsp_init (GstFFMpegCsp * space)
372 space->sinkpad = gst_pad_new_from_template (sinktempl, "sink");
373 gst_pad_set_getcaps_function (space->sinkpad, gst_ffmpegcsp_getcaps);
374 gst_pad_set_setcaps_function (space->sinkpad, gst_ffmpegcsp_setcaps);
375 gst_pad_set_chain_function (space->sinkpad, gst_ffmpegcsp_chain);
376 gst_pad_set_bufferalloc_function (space->sinkpad, gst_ffmpegcsp_bufferalloc);
377 gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
379 space->srcpad = gst_pad_new_from_template (srctempl, "src");
380 gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
381 gst_pad_set_getcaps_function (space->srcpad, gst_ffmpegcsp_getcaps);
382 gst_pad_set_setcaps_function (space->srcpad, gst_ffmpegcsp_setcaps);
384 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
385 space->palette = NULL;
389 gst_ffmpegcsp_bufferalloc (GstPad * pad, guint64 offset, guint size,
390 GstCaps * caps, GstBuffer ** buf)
395 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
397 if ((space->from_pixfmt == space->to_pixfmt) &&
398 space->from_pixfmt != PIX_FMT_NB) {
399 ret = gst_pad_alloc_buffer (space->srcpad, offset, size, caps, buf);
408 gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer)
412 GstBuffer *outbuf = NULL;
414 space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
416 GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
417 if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
420 if (space->from_pixfmt == space->to_pixfmt) {
421 GST_DEBUG ("passthrough conversion");
422 /* use input as output buffer */
425 /* get size of our suggested output format */
427 avpicture_get_size (space->to_pixfmt, space->width, space->height);
429 /* get buffer in prefered format, setcaps will be called when it is different */
430 res = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE, size,
431 space->src_prefered, &outbuf);
432 if (res != GST_FLOW_OK)
435 /* fill from from with source data */
436 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
437 GST_BUFFER_DATA (buffer),
438 space->from_pixfmt, space->width, space->height);
440 /* fill optional palette */
442 space->from_frame.data[1] = (uint8_t *) space->palette;
444 /* fill target frame */
445 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
446 GST_BUFFER_DATA (outbuf),
447 space->to_pixfmt, space->width, space->height);
450 img_convert (&space->to_frame, space->to_pixfmt,
451 &space->from_frame, space->from_pixfmt, space->width, space->height);
453 /* copy timestamps */
454 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
455 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
456 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
457 GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buffer);
459 /* we don't need source anymore */
460 gst_buffer_unref (buffer);
463 res = gst_pad_push (space->srcpad, outbuf);
470 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
471 ("attempting to convert colorspaces between unknown formats"));
472 gst_buffer_unref (buffer);
473 return GST_FLOW_NOT_NEGOTIATED;
477 gst_buffer_unref (buffer);
482 static GstElementStateReturn
483 gst_ffmpegcsp_change_state (GstElement * element)
486 GstElementStateReturn ret;
489 space = GST_FFMPEGCSP (element);
490 transition = GST_STATE_TRANSITION (element);
492 switch (transition) {
497 ret = parent_class->change_state (element);
499 switch (transition) {
500 case GST_STATE_PAUSED_TO_READY:
502 av_free (space->palette);
503 space->palette = NULL;
513 gst_ffmpegcsp_set_property (GObject * object,
514 guint prop_id, const GValue * value, GParamSpec * pspec)
518 space = GST_FFMPEGCSP (object);
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528 gst_ffmpegcsp_get_property (GObject * object,
529 guint prop_id, GValue * value, GParamSpec * pspec)
533 space = GST_FFMPEGCSP (object);
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543 gst_ffmpegcolorspace_register (GstPlugin * plugin)
548 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
550 /* build templates */
551 srctempl = gst_pad_template_new ("src",
552 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
554 /* the sink template will do palette handling as well... */
555 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
557 return gst_element_register (plugin, "ffmpegcolorspace",
558 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);