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.
23 * SECTION:element-ffmpegcolorspace
26 * <title>Example launch line</title>
29 * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! ffmpegcolorspace ! ximagesink
39 #include "gstffmpegcolorspace.h"
40 #include "gstffmpegcodecmap.h"
42 GST_DEBUG_CATEGORY (ffmpegcolorspace_debug);
43 #define GST_CAT_DEFAULT ffmpegcolorspace_debug
45 /* elementfactory information */
46 static GstElementDetails ffmpegcsp_details = {
47 "FFMPEG Colorspace converter",
48 "Filter/Converter/Video",
49 "Converts video from one colorspace to another",
50 "Ronald Bultje <rbultje@ronald.bitfreak.net>",
54 /* Stereo signals and args */
66 static GType gst_ffmpegcsp_get_type (void);
68 static void gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass);
69 static void gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass);
70 static void gst_ffmpegcsp_init (GstFFMpegCsp * space);
72 static gboolean gst_ffmpegcsp_set_caps (GstBaseTransform * btrans,
73 GstCaps * incaps, GstCaps * outcaps);
74 static gboolean gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans,
75 GstCaps * caps, guint * size);
76 static GstFlowReturn gst_ffmpegcsp_transform (GstBaseTransform * btrans,
77 GstBuffer * inbuf, GstBuffer * outbuf);
79 static GstFlowReturn gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans,
83 static GstPadTemplate *sinktempl, *srctempl;
84 static GstElementClass *parent_class = NULL;
86 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
88 /* copies the given caps */
90 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
93 GstStructure *structure;
96 caps = gst_caps_copy (caps);
98 for (i = 0; i < gst_caps_get_size (caps); i++) {
99 structure = gst_caps_get_structure (caps, i);
101 gst_structure_set_name (structure, "video/x-raw-yuv");
102 gst_structure_remove_field (structure, "format");
103 gst_structure_remove_field (structure, "endianness");
104 gst_structure_remove_field (structure, "depth");
105 gst_structure_remove_field (structure, "bpp");
106 gst_structure_remove_field (structure, "red_mask");
107 gst_structure_remove_field (structure, "green_mask");
108 gst_structure_remove_field (structure, "blue_mask");
109 gst_structure_remove_field (structure, "alpha_mask");
112 gst_caps_do_simplify (caps);
113 rgbcaps = gst_caps_copy (caps);
115 for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
116 structure = gst_caps_get_structure (rgbcaps, i);
118 gst_structure_set_name (structure, "video/x-raw-rgb");
121 gst_caps_append (caps, rgbcaps);
126 /* The caps can be transformed into any other caps with format info removed.
127 * However, we should prefer passthrough, so if passthrough is possible,
128 * put it first in the list. */
130 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
131 GstPadDirection direction, GstCaps * caps)
137 space = GST_FFMPEGCSP (btrans);
139 template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
140 result = gst_caps_intersect (caps, template);
142 gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));
144 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
145 GST_PTR_FORMAT, caps, result);
150 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
154 GstStructure *structure;
155 gint in_height, in_width;
156 gint out_height, out_width;
157 gdouble in_framerate, out_framerate;
158 const GValue *in_par = NULL;
159 const GValue *out_par = NULL;
163 space = GST_FFMPEGCSP (btrans);
165 /* parse in and output values */
166 structure = gst_caps_get_structure (incaps, 0);
168 /* we have to have width and height */
169 res = gst_structure_get_int (structure, "width", &in_width);
170 res &= gst_structure_get_int (structure, "height", &in_height);
171 res &= gst_structure_get_double (structure, "framerate", &in_framerate);
173 goto no_width_height;
175 /* this is optional */
176 in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
178 structure = gst_caps_get_structure (outcaps, 0);
180 /* we have to have width and height */
181 res = gst_structure_get_int (structure, "width", &out_width);
182 res &= gst_structure_get_int (structure, "height", &out_height);
183 res &= gst_structure_get_double (structure, "framerate", &out_framerate);
185 goto no_width_height;
187 /* this is optional */
188 out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
190 /* these must match */
191 if (in_width != out_width || in_height != out_height ||
192 in_framerate != out_framerate)
193 goto format_mismatch;
195 /* if present, these must match too */
196 if (in_par && out_par
197 && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
198 goto format_mismatch;
200 ctx = avcodec_alloc_context ();
202 space->width = ctx->width = in_width;
203 space->height = ctx->height = in_height;
205 /* get from format */
206 ctx->pix_fmt = PIX_FMT_NB;
207 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
208 if (ctx->pix_fmt == PIX_FMT_NB)
209 goto invalid_in_caps;
210 space->from_pixfmt = ctx->pix_fmt;
212 /* palette, only for from data */
214 av_free (space->palette);
215 space->palette = ctx->palctrl;
218 ctx->pix_fmt = PIX_FMT_NB;
219 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
220 if (ctx->pix_fmt == PIX_FMT_NB)
221 goto invalid_out_caps;
222 space->to_pixfmt = ctx->pix_fmt;
224 GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
233 GST_DEBUG ("did not specify width or height");
234 space->from_pixfmt = PIX_FMT_NB;
235 space->to_pixfmt = PIX_FMT_NB;
240 GST_DEBUG ("input and output formats do not match");
241 space->from_pixfmt = PIX_FMT_NB;
242 space->to_pixfmt = PIX_FMT_NB;
247 GST_DEBUG ("could not configure context for input format");
249 space->from_pixfmt = PIX_FMT_NB;
250 space->to_pixfmt = PIX_FMT_NB;
255 GST_DEBUG ("could not configure context for output format");
257 space->from_pixfmt = PIX_FMT_NB;
258 space->to_pixfmt = PIX_FMT_NB;
264 gst_ffmpegcsp_get_type (void)
266 static GType ffmpegcsp_type = 0;
268 if (!ffmpegcsp_type) {
269 static const GTypeInfo ffmpegcsp_info = {
270 sizeof (GstFFMpegCspClass),
271 (GBaseInitFunc) gst_ffmpegcsp_base_init,
273 (GClassInitFunc) gst_ffmpegcsp_class_init,
276 sizeof (GstFFMpegCsp),
278 (GInstanceInitFunc) gst_ffmpegcsp_init,
281 ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
282 "GstFFMpegColorspace", &ffmpegcsp_info, 0);
285 return ffmpegcsp_type;
289 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
291 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
293 gst_element_class_add_pad_template (element_class, srctempl);
294 gst_element_class_add_pad_template (element_class, sinktempl);
295 gst_element_class_set_details (element_class, &ffmpegcsp_details);
299 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
301 GObjectClass *gobject_class;
302 GstElementClass *gstelement_class;
303 GstBaseTransformClass *gstbasetransform_class;
305 gobject_class = (GObjectClass *) klass;
306 gstelement_class = (GstElementClass *) klass;
307 gstbasetransform_class = (GstBaseTransformClass *) klass;
309 parent_class = g_type_class_ref (GST_TYPE_BASE_TRANSFORM);
311 gstbasetransform_class->transform_caps =
312 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
313 gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
314 gstbasetransform_class->get_unit_size =
315 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
316 gstbasetransform_class->transform =
317 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
319 gstbasetransform_class->transform_ip =
320 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
323 gstbasetransform_class->passthrough_on_same_caps = TRUE;
325 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
326 "FFMPEG-based colorspace converter");
330 gst_ffmpegcsp_init (GstFFMpegCsp * space)
332 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
333 space->palette = NULL;
337 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
340 GstFFMpegCsp *space = NULL;
341 GstStructure *structure = NULL;
342 AVCodecContext *ctx = NULL;
345 g_return_val_if_fail (size, FALSE);
347 space = GST_FFMPEGCSP (btrans);
349 structure = gst_caps_get_structure (caps, 0);
350 gst_structure_get_int (structure, "width", &width);
351 gst_structure_get_int (structure, "height", &height);
353 ctx = avcodec_alloc_context ();
355 g_assert (ctx != NULL);
357 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
359 *size = avpicture_get_size (ctx->pix_fmt, width, height);
370 /* FIXME: Could use transform_ip to implement endianness swap type operations */
372 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
380 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
385 space = GST_FFMPEGCSP (btrans);
387 GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
388 if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
391 /* fill from with source data */
392 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
393 GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);
395 /* fill optional palette */
397 space->from_frame.data[1] = (uint8_t *) space->palette->palette;
399 /* fill target frame */
400 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
401 GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
404 img_convert (&space->to_frame, space->to_pixfmt,
405 &space->from_frame, space->from_pixfmt, space->width, space->height);
407 /* copy timestamps */
408 gst_buffer_stamp (outbuf, inbuf);
409 GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
416 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
417 ("attempting to convert colorspaces between unknown formats"));
418 return GST_FLOW_NOT_NEGOTIATED;
423 gst_ffmpegcolorspace_register (GstPlugin * plugin)
428 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
430 /* build templates */
431 srctempl = gst_pad_template_new ("src",
432 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
434 /* the sink template will do palette handling as well... */
435 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
437 return gst_element_register (plugin, "ffmpegcolorspace",
438 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);