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 }; */
89 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
92 GstStructure *structure;
95 caps = gst_caps_copy (caps);
97 for (i = 0; i < gst_caps_get_size (caps); i++) {
98 structure = gst_caps_get_structure (caps, i);
100 gst_structure_set_name (structure, "video/x-raw-yuv");
101 gst_structure_remove_field (structure, "format");
102 gst_structure_remove_field (structure, "endianness");
103 gst_structure_remove_field (structure, "depth");
104 gst_structure_remove_field (structure, "bpp");
105 gst_structure_remove_field (structure, "red_mask");
106 gst_structure_remove_field (structure, "green_mask");
107 gst_structure_remove_field (structure, "blue_mask");
108 gst_structure_remove_field (structure, "alpha_mask");
111 gst_caps_do_simplify (caps);
112 rgbcaps = gst_caps_copy (caps);
114 for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
115 structure = gst_caps_get_structure (rgbcaps, i);
117 gst_structure_set_name (structure, "video/x-raw-rgb");
120 gst_caps_append (caps, rgbcaps);
126 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
127 GstPadDirection direction, GstCaps * caps)
132 space = GST_FFMPEGCSP (btrans);
134 result = gst_ffmpegcsp_caps_remove_format_info (caps);
140 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
144 GstStructure *structure;
145 gint in_height, in_width;
146 gint out_height, out_width;
147 gdouble in_framerate, out_framerate;
148 const GValue *in_par = NULL;
149 const GValue *out_par = NULL;
152 space = GST_FFMPEGCSP (btrans);
154 /* parse in and output values */
155 structure = gst_caps_get_structure (incaps, 0);
156 gst_structure_get_int (structure, "width", &in_width);
157 gst_structure_get_int (structure, "height", &in_height);
158 gst_structure_get_double (structure, "framerate", &in_framerate);
159 in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
161 structure = gst_caps_get_structure (outcaps, 0);
162 gst_structure_get_int (structure, "width", &out_width);
163 gst_structure_get_int (structure, "height", &out_height);
164 gst_structure_get_double (structure, "framerate", &out_framerate);
165 out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
167 if (in_width != out_width || in_height != out_height ||
168 in_framerate != out_framerate)
169 goto format_mismatch;
171 if (in_par && out_par
172 && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
173 goto format_mismatch;
175 ctx = avcodec_alloc_context ();
177 space->width = ctx->width = in_width;
178 space->height = ctx->height = in_height;
180 /* get from format */
181 ctx->pix_fmt = PIX_FMT_NB;
182 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
183 if (ctx->pix_fmt == PIX_FMT_NB)
184 goto invalid_in_caps;
185 space->from_pixfmt = ctx->pix_fmt;
187 /* palette, only for from data */
189 av_free (space->palette);
190 space->palette = ctx->palctrl;
193 ctx->pix_fmt = PIX_FMT_NB;
194 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
195 if (ctx->pix_fmt == PIX_FMT_NB)
196 goto invalid_out_caps;
197 space->to_pixfmt = ctx->pix_fmt;
199 GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
208 GST_DEBUG ("input and output formats do not match");
209 space->from_pixfmt = PIX_FMT_NB;
210 space->to_pixfmt = PIX_FMT_NB;
215 GST_DEBUG ("could not configure context for input format");
217 space->from_pixfmt = PIX_FMT_NB;
218 space->to_pixfmt = PIX_FMT_NB;
223 GST_DEBUG ("could not configure context for output format");
225 space->from_pixfmt = PIX_FMT_NB;
226 space->to_pixfmt = PIX_FMT_NB;
232 gst_ffmpegcsp_get_type (void)
234 static GType ffmpegcsp_type = 0;
236 if (!ffmpegcsp_type) {
237 static const GTypeInfo ffmpegcsp_info = {
238 sizeof (GstFFMpegCspClass),
239 (GBaseInitFunc) gst_ffmpegcsp_base_init,
241 (GClassInitFunc) gst_ffmpegcsp_class_init,
244 sizeof (GstFFMpegCsp),
246 (GInstanceInitFunc) gst_ffmpegcsp_init,
249 ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
250 "GstFFMpegColorspace", &ffmpegcsp_info, 0);
253 return ffmpegcsp_type;
257 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
259 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
261 gst_element_class_add_pad_template (element_class, srctempl);
262 gst_element_class_add_pad_template (element_class, sinktempl);
263 gst_element_class_set_details (element_class, &ffmpegcsp_details);
267 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
269 GObjectClass *gobject_class;
270 GstElementClass *gstelement_class;
271 GstBaseTransformClass *gstbasetransform_class;
273 gobject_class = (GObjectClass *) klass;
274 gstelement_class = (GstElementClass *) klass;
275 gstbasetransform_class = (GstBaseTransformClass *) klass;
277 parent_class = g_type_class_ref (GST_TYPE_BASE_TRANSFORM);
279 gstbasetransform_class->transform_caps =
280 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
281 gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
282 gstbasetransform_class->get_unit_size =
283 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
284 gstbasetransform_class->transform =
285 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
287 gstbasetransform_class->transform_ip =
288 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
291 gstbasetransform_class->passthrough_on_same_caps = TRUE;
293 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
294 "FFMPEG-based colorspace converter");
298 gst_ffmpegcsp_init (GstFFMpegCsp * space)
300 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
301 space->palette = NULL;
305 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
308 GstFFMpegCsp *space = NULL;
309 GstStructure *structure = NULL;
310 AVCodecContext *ctx = NULL;
313 g_return_val_if_fail (size, FALSE);
315 space = GST_FFMPEGCSP (btrans);
317 structure = gst_caps_get_structure (caps, 0);
318 gst_structure_get_int (structure, "width", &width);
319 gst_structure_get_int (structure, "height", &height);
321 ctx = avcodec_alloc_context ();
323 g_assert (ctx != NULL);
325 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
327 *size = avpicture_get_size (ctx->pix_fmt, width, height);
335 /* FIXME: Could use transform_ip to implement endianness swap type operations */
337 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
345 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
350 space = GST_FFMPEGCSP (btrans);
352 GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
353 if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
356 /* fill from with source data */
357 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
358 GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);
360 /* fill optional palette */
362 space->from_frame.data[1] = (uint8_t *) space->palette;
364 /* fill target frame */
365 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
366 GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
369 img_convert (&space->to_frame, space->to_pixfmt,
370 &space->from_frame, space->from_pixfmt, space->width, space->height);
372 /* copy timestamps */
373 gst_buffer_stamp (outbuf, inbuf);
374 GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
381 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
382 ("attempting to convert colorspaces between unknown formats"));
383 return GST_FLOW_NOT_NEGOTIATED;
388 gst_ffmpegcolorspace_register (GstPlugin * plugin)
393 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
395 /* build templates */
396 srctempl = gst_pad_template_new ("src",
397 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
399 /* the sink template will do palette handling as well... */
400 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
402 return gst_element_register (plugin, "ffmpegcolorspace",
403 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);