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;
97 caps = gst_caps_copy (caps);
99 for (i = 0; i < gst_caps_get_size (caps); i++) {
100 structure = gst_caps_get_structure (caps, i);
102 gst_structure_set_name (structure, "video/x-raw-yuv");
103 gst_structure_remove_field (structure, "format");
104 gst_structure_remove_field (structure, "endianness");
105 gst_structure_remove_field (structure, "depth");
106 gst_structure_remove_field (structure, "bpp");
107 gst_structure_remove_field (structure, "red_mask");
108 gst_structure_remove_field (structure, "green_mask");
109 gst_structure_remove_field (structure, "blue_mask");
110 gst_structure_remove_field (structure, "alpha_mask");
113 gst_caps_do_simplify (caps);
114 rgbcaps = gst_caps_copy (caps);
116 for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
117 structure = gst_caps_get_structure (rgbcaps, i);
119 gst_structure_set_name (structure, "video/x-raw-rgb");
121 graycaps = gst_caps_copy (caps);
123 for (i = 0; i < gst_caps_get_size (graycaps); i++) {
124 structure = gst_caps_get_structure (graycaps, i);
126 gst_structure_set_name (structure, "video/x-raw-gray");
129 gst_caps_append (caps, graycaps);
130 gst_caps_append (caps, rgbcaps);
135 /* The caps can be transformed into any other caps with format info removed.
136 * However, we should prefer passthrough, so if passthrough is possible,
137 * put it first in the list. */
139 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
140 GstPadDirection direction, GstCaps * caps)
146 space = GST_FFMPEGCSP (btrans);
148 template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
149 result = gst_caps_intersect (caps, template);
151 gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));
153 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
154 GST_PTR_FORMAT, caps, result);
159 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
163 GstStructure *structure;
164 gint in_height, in_width;
165 gint out_height, out_width;
166 const GValue *in_framerate = NULL;
167 const GValue *out_framerate = NULL;
168 const GValue *in_par = NULL;
169 const GValue *out_par = NULL;
173 space = GST_FFMPEGCSP (btrans);
175 /* parse in and output values */
176 structure = gst_caps_get_structure (incaps, 0);
178 /* we have to have width and height */
179 res = gst_structure_get_int (structure, "width", &in_width);
180 res &= gst_structure_get_int (structure, "height", &in_height);
182 goto no_width_height;
185 in_framerate = gst_structure_get_value (structure, "framerate");
186 if (in_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (in_framerate))
189 /* this is optional */
190 in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
192 structure = gst_caps_get_structure (outcaps, 0);
194 /* we have to have width and height */
195 res = gst_structure_get_int (structure, "width", &out_width);
196 res &= gst_structure_get_int (structure, "height", &out_height);
198 goto no_width_height;
201 out_framerate = gst_structure_get_value (structure, "framerate");
202 if (out_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (out_framerate))
205 /* this is optional */
206 out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
208 /* these must match */
209 if (in_width != out_width || in_height != out_height ||
210 gst_value_compare (in_framerate, out_framerate) != GST_VALUE_EQUAL)
211 goto format_mismatch;
213 /* if present, these must match too */
214 if (in_par && out_par
215 && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
216 goto format_mismatch;
218 ctx = avcodec_alloc_context ();
220 space->width = ctx->width = in_width;
221 space->height = ctx->height = in_height;
223 /* get from format */
224 ctx->pix_fmt = PIX_FMT_NB;
225 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
226 if (ctx->pix_fmt == PIX_FMT_NB)
227 goto invalid_in_caps;
228 space->from_pixfmt = ctx->pix_fmt;
230 /* palette, only for from data */
232 av_free (space->palette);
233 space->palette = ctx->palctrl;
236 ctx->pix_fmt = PIX_FMT_NB;
237 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
238 if (ctx->pix_fmt == PIX_FMT_NB)
239 goto invalid_out_caps;
240 space->to_pixfmt = ctx->pix_fmt;
242 GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
251 GST_DEBUG ("did not specify width or height");
252 space->from_pixfmt = PIX_FMT_NB;
253 space->to_pixfmt = PIX_FMT_NB;
258 GST_DEBUG ("did not specify framerate");
259 space->from_pixfmt = PIX_FMT_NB;
260 space->to_pixfmt = PIX_FMT_NB;
265 GST_DEBUG ("input and output formats do not match");
266 space->from_pixfmt = PIX_FMT_NB;
267 space->to_pixfmt = PIX_FMT_NB;
272 GST_DEBUG ("could not configure context for input format");
274 space->from_pixfmt = PIX_FMT_NB;
275 space->to_pixfmt = PIX_FMT_NB;
280 GST_DEBUG ("could not configure context for output format");
282 space->from_pixfmt = PIX_FMT_NB;
283 space->to_pixfmt = PIX_FMT_NB;
289 gst_ffmpegcsp_get_type (void)
291 static GType ffmpegcsp_type = 0;
293 if (!ffmpegcsp_type) {
294 static const GTypeInfo ffmpegcsp_info = {
295 sizeof (GstFFMpegCspClass),
296 (GBaseInitFunc) gst_ffmpegcsp_base_init,
298 (GClassInitFunc) gst_ffmpegcsp_class_init,
301 sizeof (GstFFMpegCsp),
303 (GInstanceInitFunc) gst_ffmpegcsp_init,
306 ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
307 "GstFFMpegColorspace", &ffmpegcsp_info, 0);
310 return ffmpegcsp_type;
314 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
316 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
318 gst_element_class_add_pad_template (element_class, srctempl);
319 gst_element_class_add_pad_template (element_class, sinktempl);
320 gst_element_class_set_details (element_class, &ffmpegcsp_details);
324 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
326 GObjectClass *gobject_class;
327 GstElementClass *gstelement_class;
328 GstBaseTransformClass *gstbasetransform_class;
330 gobject_class = (GObjectClass *) klass;
331 gstelement_class = (GstElementClass *) klass;
332 gstbasetransform_class = (GstBaseTransformClass *) klass;
334 parent_class = g_type_class_ref (GST_TYPE_BASE_TRANSFORM);
336 gstbasetransform_class->transform_caps =
337 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
338 gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
339 gstbasetransform_class->get_unit_size =
340 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
341 gstbasetransform_class->transform =
342 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
344 gstbasetransform_class->transform_ip =
345 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
348 gstbasetransform_class->passthrough_on_same_caps = TRUE;
350 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
351 "FFMPEG-based colorspace converter");
355 gst_ffmpegcsp_init (GstFFMpegCsp * space)
357 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
358 space->palette = NULL;
362 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
365 GstFFMpegCsp *space = NULL;
366 GstStructure *structure = NULL;
367 AVCodecContext *ctx = NULL;
370 g_return_val_if_fail (size, FALSE);
372 space = GST_FFMPEGCSP (btrans);
374 structure = gst_caps_get_structure (caps, 0);
375 gst_structure_get_int (structure, "width", &width);
376 gst_structure_get_int (structure, "height", &height);
378 ctx = avcodec_alloc_context ();
380 g_assert (ctx != NULL);
382 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
384 *size = avpicture_get_size (ctx->pix_fmt, width, height);
395 /* FIXME: Could use transform_ip to implement endianness swap type operations */
397 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
405 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
410 space = GST_FFMPEGCSP (btrans);
412 GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
413 if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
416 /* fill from with source data */
417 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
418 GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);
420 /* fill optional palette */
422 space->from_frame.data[1] = (uint8_t *) space->palette->palette;
424 /* fill target frame */
425 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
426 GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
429 img_convert (&space->to_frame, space->to_pixfmt,
430 &space->from_frame, space->from_pixfmt, space->width, space->height);
432 /* copy timestamps */
433 gst_buffer_stamp (outbuf, inbuf);
434 GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
441 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
442 ("attempting to convert colorspaces between unknown formats"));
443 return GST_FLOW_NOT_NEGOTIATED;
448 gst_ffmpegcolorspace_register (GstPlugin * plugin)
453 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
455 /* build templates */
456 srctempl = gst_pad_template_new ("src",
457 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
459 /* the sink template will do palette handling as well... */
460 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
462 return gst_element_register (plugin, "ffmpegcolorspace",
463 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);