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 (ffmpegcolorspace_debug);
43 #define GST_CAT_DEFAULT ffmpegcolorspace_debug
44 GST_DEBUG_CATEGORY (ffmpegcolorspace_performance);
46 /* elementfactory information */
47 static const GstElementDetails ffmpegcsp_details =
48 GST_ELEMENT_DETAILS ("FFMPEG Colorspace converter",
49 "Filter/Converter/Video",
50 "Converts video from one colorspace to another",
51 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.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");
111 gst_structure_remove_field (structure, "palette_data");
114 gst_caps_do_simplify (caps);
115 rgbcaps = gst_caps_copy (caps);
117 for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
118 structure = gst_caps_get_structure (rgbcaps, i);
120 gst_structure_set_name (structure, "video/x-raw-rgb");
122 graycaps = gst_caps_copy (caps);
124 for (i = 0; i < gst_caps_get_size (graycaps); i++) {
125 structure = gst_caps_get_structure (graycaps, i);
127 gst_structure_set_name (structure, "video/x-raw-gray");
130 gst_caps_append (caps, graycaps);
131 gst_caps_append (caps, rgbcaps);
138 gst_ffmpegcsp_structure_is_alpha (GstStructure * s)
142 name = gst_structure_get_name (s);
144 if (g_str_equal (name, "video/x-raw-rgb")) {
145 return gst_structure_has_field (s, "alpha_mask");
146 } else if (g_str_equal (name, "video/x-raw-yuv")) {
149 if (!gst_structure_get_fourcc (s, "format", &fourcc))
152 return (fourcc == GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
158 /* The caps can be transformed into any other caps with format info removed.
159 * However, we should prefer passthrough, so if passthrough is possible,
160 * put it first in the list. */
162 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
163 GstPadDirection direction, GstCaps * caps)
171 GstCaps *alpha, *non_alpha;
173 s = gst_caps_get_structure (caps, 0);
174 is_alpha = gst_ffmpegcsp_structure_is_alpha (s);
176 template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
177 result = gst_caps_intersect (caps, template);
179 /* Get all possible caps that we can transform to */
180 tmp = gst_ffmpegcsp_caps_remove_format_info (caps);
181 tmp2 = gst_caps_intersect (tmp, template);
182 gst_caps_unref (template);
183 gst_caps_unref (tmp);
186 /* Now move alpha formats to the beginning if caps is an alpha format
187 * or at the end if caps is no alpha format */
188 n = gst_caps_get_size (tmp);
190 alpha = gst_caps_new_empty ();
191 non_alpha = gst_caps_new_empty ();
193 for (i = 0; i < n; i++) {
194 s = gst_caps_get_structure (tmp, i);
196 if (gst_ffmpegcsp_structure_is_alpha (s))
197 gst_caps_append_structure (alpha, gst_structure_copy (s));
199 gst_caps_append_structure (non_alpha, gst_structure_copy (s));
202 s = gst_caps_get_structure (caps, 0);
203 gst_caps_unref (tmp);
205 if (gst_ffmpegcsp_structure_is_alpha (s)) {
206 gst_caps_append (alpha, non_alpha);
209 gst_caps_append (non_alpha, alpha);
213 gst_caps_append (result, tmp);
215 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
216 GST_PTR_FORMAT, caps, result);
222 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
226 GstStructure *structure;
227 gint in_height, in_width;
228 gint out_height, out_width;
229 const GValue *in_framerate = NULL;
230 const GValue *out_framerate = NULL;
231 const GValue *in_par = NULL;
232 const GValue *out_par = NULL;
236 space = GST_FFMPEGCSP (btrans);
238 /* parse in and output values */
239 structure = gst_caps_get_structure (incaps, 0);
241 /* we have to have width and height */
242 res = gst_structure_get_int (structure, "width", &in_width);
243 res &= gst_structure_get_int (structure, "height", &in_height);
245 goto no_width_height;
248 in_framerate = gst_structure_get_value (structure, "framerate");
249 if (in_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (in_framerate))
252 /* this is optional */
253 in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
255 structure = gst_caps_get_structure (outcaps, 0);
257 /* we have to have width and height */
258 res = gst_structure_get_int (structure, "width", &out_width);
259 res &= gst_structure_get_int (structure, "height", &out_height);
261 goto no_width_height;
264 out_framerate = gst_structure_get_value (structure, "framerate");
265 if (out_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (out_framerate))
268 /* this is optional */
269 out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
271 /* these must match */
272 if (in_width != out_width || in_height != out_height ||
273 gst_value_compare (in_framerate, out_framerate) != GST_VALUE_EQUAL)
274 goto format_mismatch;
276 /* if present, these must match too */
277 if (in_par && out_par
278 && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
279 goto format_mismatch;
281 ctx = avcodec_alloc_context ();
283 space->width = ctx->width = in_width;
284 space->height = ctx->height = in_height;
286 space->interlaced = FALSE;
287 gst_structure_get_boolean (structure, "interlaced", &space->interlaced);
289 /* get from format */
290 ctx->pix_fmt = PIX_FMT_NB;
291 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
292 if (ctx->pix_fmt == PIX_FMT_NB)
293 goto invalid_in_caps;
294 space->from_pixfmt = ctx->pix_fmt;
296 /* palette, only for from data */
298 av_free (space->palette);
299 space->palette = ctx->palctrl;
303 ctx->pix_fmt = PIX_FMT_NB;
304 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
305 if (ctx->pix_fmt == PIX_FMT_NB)
306 goto invalid_out_caps;
307 space->to_pixfmt = ctx->pix_fmt;
309 GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
318 GST_DEBUG_OBJECT (space, "did not specify width or height");
319 space->from_pixfmt = PIX_FMT_NB;
320 space->to_pixfmt = PIX_FMT_NB;
325 GST_DEBUG_OBJECT (space, "did not specify framerate");
326 space->from_pixfmt = PIX_FMT_NB;
327 space->to_pixfmt = PIX_FMT_NB;
332 GST_DEBUG_OBJECT (space, "input and output formats do not match");
333 space->from_pixfmt = PIX_FMT_NB;
334 space->to_pixfmt = PIX_FMT_NB;
339 GST_DEBUG_OBJECT (space, "could not configure context for input format");
341 space->from_pixfmt = PIX_FMT_NB;
342 space->to_pixfmt = PIX_FMT_NB;
347 GST_DEBUG_OBJECT (space, "could not configure context for output format");
349 space->from_pixfmt = PIX_FMT_NB;
350 space->to_pixfmt = PIX_FMT_NB;
356 gst_ffmpegcsp_get_type (void)
358 static GType ffmpegcsp_type = 0;
360 if (!ffmpegcsp_type) {
361 static const GTypeInfo ffmpegcsp_info = {
362 sizeof (GstFFMpegCspClass),
363 (GBaseInitFunc) gst_ffmpegcsp_base_init,
365 (GClassInitFunc) gst_ffmpegcsp_class_init,
368 sizeof (GstFFMpegCsp),
370 (GInstanceInitFunc) gst_ffmpegcsp_init,
373 ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
374 "GstFFMpegCsp", &ffmpegcsp_info, 0);
377 return ffmpegcsp_type;
381 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
383 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
385 gst_element_class_add_pad_template (element_class, srctempl);
386 gst_element_class_add_pad_template (element_class, sinktempl);
387 gst_element_class_set_details (element_class, &ffmpegcsp_details);
391 gst_ffmpegcsp_finalize (GObject * obj)
393 GstFFMpegCsp *space = GST_FFMPEGCSP (obj);
396 av_free (space->palette);
398 G_OBJECT_CLASS (parent_class)->finalize (obj);
402 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
404 GObjectClass *gobject_class;
405 GstBaseTransformClass *gstbasetransform_class;
407 gobject_class = (GObjectClass *) klass;
408 gstbasetransform_class = (GstBaseTransformClass *) klass;
410 parent_class = g_type_class_peek_parent (klass);
412 gobject_class->finalize = gst_ffmpegcsp_finalize;
414 gstbasetransform_class->transform_caps =
415 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
416 gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
417 gstbasetransform_class->get_unit_size =
418 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
419 gstbasetransform_class->transform =
420 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
422 gstbasetransform_class->transform_ip =
423 GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
426 gstbasetransform_class->passthrough_on_same_caps = TRUE;
430 gst_ffmpegcsp_init (GstFFMpegCsp * space)
432 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (space), TRUE);
433 space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
434 space->palette = NULL;
438 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
441 GstStructure *structure = NULL;
442 AVCodecContext *ctx = NULL;
448 structure = gst_caps_get_structure (caps, 0);
449 gst_structure_get_int (structure, "width", &width);
450 gst_structure_get_int (structure, "height", &height);
452 ctx = avcodec_alloc_context ();
454 g_assert (ctx != NULL);
456 ctx->pix_fmt = PIX_FMT_NB;
458 gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
460 if (G_UNLIKELY (ctx->pix_fmt == PIX_FMT_NB)) {
465 *size = avpicture_get_size (ctx->pix_fmt, width, height);
467 /* ffmpeg frames have the palette after the frame data, whereas
468 * GStreamer currently puts it into the caps as 'palette_data' field,
469 * so for paletted data the frame size avpicture_get_size() returns is
470 * 1024 bytes larger than what GStreamer expects. */
471 if (gst_structure_has_field (structure, "palette_data") &&
472 ctx->pix_fmt == PIX_FMT_PAL8) {
473 *size -= 4 * 256; /* = AVPALETTE_SIZE */
479 av_free (ctx->palctrl);
486 /* FIXME: Could use transform_ip to implement endianness swap type operations */
488 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
496 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
502 space = GST_FFMPEGCSP (btrans);
504 GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
506 if (G_UNLIKELY (space->from_pixfmt == PIX_FMT_NB ||
507 space->to_pixfmt == PIX_FMT_NB))
510 /* fill from with source data */
511 gst_ffmpegcsp_avpicture_fill (&space->from_frame,
512 GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height,
515 /* fill optional palette */
517 space->from_frame.data[1] = (uint8_t *) space->palette->palette;
519 /* fill target frame */
520 gst_ffmpegcsp_avpicture_fill (&space->to_frame,
521 GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height,
525 result = img_convert (&space->to_frame, space->to_pixfmt,
526 &space->from_frame, space->from_pixfmt, space->width, space->height);
530 /* baseclass copies timestamps */
531 GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
538 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
539 ("attempting to convert colorspaces between unknown formats"));
540 return GST_FLOW_NOT_NEGOTIATED;
544 GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
545 ("cannot convert between formats"));
546 return GST_FLOW_NOT_SUPPORTED;
551 gst_ffmpegcolorspace_register (GstPlugin * plugin)
555 GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
556 "FFMPEG-based colorspace converter");
557 GST_DEBUG_CATEGORY_GET (ffmpegcolorspace_performance, "GST_PERFORMANCE");
560 caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
562 /* build templates */
563 srctempl = gst_pad_template_new ("src",
564 GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
566 /* the sink template will do palette handling as well... */
567 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
569 return gst_element_register (plugin, "ffmpegcolorspace",
570 GST_RANK_NONE, GST_TYPE_FFMPEGCSP);