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
25 * Convert video frames between a great variety of colorspace formats.
28 * <title>Example launch line</title>
30 * 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_STATIC (ffmpegcolorspace_debug);
43 #define GST_CAT_DEFAULT ffmpegcolorspace_debug
45 /* elementfactory information */
46 static const GstElementDetails ffmpegcsp_details =
47 GST_ELEMENT_DETAILS ("FFMPEG Colorspace converter",
48 "Filter/Converter/Video",
49 "Converts video from one colorspace to another",
50 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
53 /* Stereo signals and args */
65 static GType gst_ffmpegcsp_get_type (void);
67 static void gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass);
68 static void gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass);
69 static void gst_ffmpegcsp_init (GstFFMpegCsp * space);
71 static gboolean gst_ffmpegcsp_set_caps (GstBaseTransform * btrans,
72 GstCaps * incaps, GstCaps * outcaps);
73 static gboolean gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans,
74 GstCaps * caps, guint * size);
75 static GstFlowReturn gst_ffmpegcsp_transform (GstBaseTransform * btrans,
76 GstBuffer * inbuf, GstBuffer * outbuf);
78 static GstFlowReturn gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans,
82 static GstPadTemplate *sinktempl, *srctempl;
83 static GstElementClass *parent_class = NULL;
85 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
87 /* copies the given caps */
89 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
92 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");
110 gst_structure_remove_field (structure, "palette_data");
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)
145 template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
146 result = gst_caps_intersect (caps, template);
147 gst_caps_unref (template);
149 gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));
151 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
152 GST_PTR_FORMAT, caps, result);
158 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
162 GstStructure *structure;
163 gint in_height, in_width;
164 gint out_height, out_width;
165 const GValue *in_framerate = NULL;
166 const GValue *out_framerate = NULL;
167 const GValue *in_par = NULL;
168 const GValue *out_par = NULL;
172 space = GST_FFMPEGCSP (btrans);
174 /* parse in and output values */
175 structure = gst_caps_get_structure (incaps, 0);
177 /* we have to have width and height */
178 res = gst_structure_get_int (structure, "width", &in_width);
179 res &= gst_structure_get_int (structure, "height", &in_height);
181 goto no_width_height;
184 in_framerate = gst_structure_get_value (structure, "framerate");
185 if (in_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (in_framerate))
188 /* this is optional */
189 in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
191 structure = gst_caps_get_structure (outcaps, 0);
193 /* we have to have width and height */
194 res = gst_structure_get_int (structure, "width", &out_width);
195 res &= gst_structure_get_int (structure, "height", &out_height);
197 goto no_width_height;
200 out_framerate = gst_structure_get_value (structure, "framerate");
201 if (out_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (out_framerate))
204 /* this is optional */
205 out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
207 /* these must match */
208 if (in_width != out_width || in_height != out_height ||
209 gst_value_compare (in_framerate, out_framerate) != GST_VALUE_EQUAL)
210 goto format_mismatch;
212 /* if present, these must match too */
213 if (in_par && out_par
214 && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
215 goto format_mismatch;
217 ctx = avcodec_alloc_context ();
219 space->width = ctx->width = in_width;
220 space->height = ctx->height = in_height;
222 space->interlaced = FALSE;
223 gst_structure_get_boolean (structure, "interlaced", &space->interlaced);
225 /* get from format */
226 ctx->pix_fmt = PIX_FMT_NB;
227 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
228 if (ctx->pix_fmt == PIX_FMT_NB)
229 goto invalid_in_caps;
230 space->from_pixfmt = ctx->pix_fmt;
232 /* palette, only for from data */
234 av_free (space->palette);
235 space->palette = ctx->palctrl;
239 ctx->pix_fmt = PIX_FMT_NB;
240 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
241 if (ctx->pix_fmt == PIX_FMT_NB)
242 goto invalid_out_caps;
243 space->to_pixfmt = ctx->pix_fmt;
245 GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
254 GST_DEBUG_OBJECT (space, "did not specify width or height");
255 space->from_pixfmt = PIX_FMT_NB;
256 space->to_pixfmt = PIX_FMT_NB;
261 GST_DEBUG_OBJECT (space, "did not specify framerate");
262 space->from_pixfmt = PIX_FMT_NB;
263 space->to_pixfmt = PIX_FMT_NB;
268 GST_DEBUG_OBJECT (space, "input and output formats do not match");
269 space->from_pixfmt = PIX_FMT_NB;
270 space->to_pixfmt = PIX_FMT_NB;
275 GST_DEBUG_OBJECT (space, "could not configure context for input format");
277 space->from_pixfmt = PIX_FMT_NB;
278 space->to_pixfmt = PIX_FMT_NB;
283 GST_DEBUG_OBJECT (space, "could not configure context for output format");
285 space->from_pixfmt = PIX_FMT_NB;
286 space->to_pixfmt = PIX_FMT_NB;
292 gst_ffmpegcsp_get_type (void)
294 static GType ffmpegcsp_type = 0;
296 if (!ffmpegcsp_type) {
297 static const GTypeInfo ffmpegcsp_info = {
298 sizeof (GstFFMpegCspClass),
299 (GBaseInitFunc) gst_ffmpegcsp_base_init,
301 (GClassInitFunc) gst_ffmpegcsp_class_init,
304 sizeof (GstFFMpegCsp),
306 (GInstanceInitFunc) gst_ffmpegcsp_init,
309 ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
310 "GstFFMpegCsp", &ffmpegcsp_info, 0);
313 return ffmpegcsp_type;
317 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
319 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
321 gst_element_class_add_pad_template (element_class, srctempl);
322 gst_element_class_add_pad_template (element_class, sinktempl);
323 gst_element_class_set_details (element_class, &ffmpegcsp_details);
327 gst_ffmpegcsp_finalize (GObject * obj)
329 GstFFMpegCsp *space = GST_FFMPEGCSP (obj);
332 av_free (space->palette);
334 G_OBJECT_CLASS (parent_class)->finalize (obj);
338 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
340 GObjectClass *gobject_class;
341 GstElementClass *gstelement_class;
342 GstBaseTransformClass *gstbasetransform_class;
344 gobject_class = (GObjectClass *) klass;
345 gstelement_class = (GstElementClass *) klass;
346 gstbasetransform_class = (GstBaseTransformClass *) klass;
348 parent_class = g_type_class_peek_parent (klass);
350 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_finalize);
352 gstbasetransform_class->transform_caps =
353 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
354 gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
355 gstbasetransform_class->get_unit_size =
356 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
357 gstbasetransform_class->transform =
358 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
360 gstbasetransform_class->transform_ip =
361 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
364 gstbasetransform_class->passthrough_on_same_caps = TRUE;
366 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
367 "FFMPEG-based colorspace converter");
371 gst_ffmpegcsp_init (GstFFMpegCsp * space)
373 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (space), TRUE);
374 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
375 space->palette = NULL;
379 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
382 GstStructure *structure = NULL;
383 AVCodecContext *ctx = NULL;
389 structure = gst_caps_get_structure (caps, 0);
390 gst_structure_get_int (structure, "width", &width);
391 gst_structure_get_int (structure, "height", &height);
393 ctx = avcodec_alloc_context ();
395 g_assert (ctx != NULL);
397 ctx->pix_fmt = PIX_FMT_NB;
399 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
401 if (G_UNLIKELY (ctx->pix_fmt == PIX_FMT_NB)) {
406 *size = avpicture_get_size (ctx->pix_fmt, width, height);
408 /* ffmpeg frames have the palette after the frame data, whereas
409 * GStreamer currently puts it into the caps as 'palette_data' field,
410 * so for paletted data the frame size avpicture_get_size() returns is
411 * 1024 bytes larger than what GStreamer expects. */
412 if (gst_structure_has_field (structure, "palette_data") &&
413 ctx->pix_fmt == PIX_FMT_PAL8) {
414 *size -= 4 * 256; /* = AVPALETTE_SIZE */
420 av_free (ctx->palctrl);
427 /* FIXME: Could use transform_ip to implement endianness swap type operations */
429 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
437 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
443 space = GST_FFMPEGCSP (btrans);
445 GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
446 if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
449 /* fill from with source data */
450 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
451 GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height,
454 /* fill optional palette */
456 space->from_frame.data[1] = (uint8_t *) space->palette->palette;
458 /* fill target frame */
459 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
460 GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height,
464 result = img_convert (&space->to_frame, space->to_pixfmt,
465 &space->from_frame, space->from_pixfmt, space->width, space->height);
469 /* baseclass copies timestamps */
470 GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
477 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
478 ("attempting to convert colorspaces between unknown formats"));
479 return GST_FLOW_NOT_NEGOTIATED;
483 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
484 ("cannot convert between formats"));
485 return GST_FLOW_NOT_SUPPORTED;
490 gst_ffmpegcolorspace_register (GstPlugin * plugin)
495 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
497 /* build templates */
498 srctempl = gst_pad_template_new ("src",
499 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
501 /* the sink template will do palette handling as well... */
502 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
504 return gst_element_register (plugin, "ffmpegcolorspace",
505 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);