3a8bc8545b2c980ea897ba42ad766ae7eec764f4
[platform/upstream/gstreamer.git] / gst / videoconvert / gstvideoconvert.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * This file:
4  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
5  * Copyright (C) 2010 David Schleef <ds@schleef.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:element-videoconvert
25  *
26  * Convert video frames between a great variety of videoconvert formats.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! videoconvert ! ximagesink
32  * ]|
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include "gstvideoconvert.h"
41 #include <gst/video/video.h>
42
43 #include <string.h>
44
45 GST_DEBUG_CATEGORY (videoconvert_debug);
46 #define GST_CAT_DEFAULT videoconvert_debug
47 GST_DEBUG_CATEGORY (videoconvert_performance);
48
49 enum
50 {
51   PROP_0,
52   PROP_DITHER
53 };
54
55 #define CSP_VIDEO_CAPS                                          \
56   "video/x-raw-yuv, width = "GST_VIDEO_SIZE_RANGE" , "                  \
57   "height="GST_VIDEO_SIZE_RANGE",framerate="GST_VIDEO_FPS_RANGE","      \
58   "format= (fourcc) { I420 , NV12 , NV21 , YV12 , YUY2 , Y42B , Y444 , YUV9 , YVU9 , Y41B , Y800 , Y8 , GREY , Y16 , UYVY , YVYU , IYU1 , v308 , AYUV, v210, v216, A420, AY64 } ;" \
59   GST_VIDEO_CAPS_RGB";"                                                 \
60   GST_VIDEO_CAPS_BGR";"                                                 \
61   GST_VIDEO_CAPS_RGBx";"                                                \
62   GST_VIDEO_CAPS_xRGB";"                                                \
63   GST_VIDEO_CAPS_BGRx";"                                                \
64   GST_VIDEO_CAPS_xBGR";"                                                \
65   GST_VIDEO_CAPS_RGBA";"                                                \
66   GST_VIDEO_CAPS_ARGB";"                                                \
67   GST_VIDEO_CAPS_BGRA";"                                                \
68   GST_VIDEO_CAPS_ABGR";"                                                \
69   GST_VIDEO_CAPS_RGB_16";"                                              \
70   GST_VIDEO_CAPS_BGR_16";"                                              \
71   GST_VIDEO_CAPS_RGB_15";"                                              \
72   GST_VIDEO_CAPS_BGR_15";"                                              \
73   GST_VIDEO_CAPS_RGB8_PALETTED "; "                                     \
74   GST_VIDEO_CAPS_GRAY8";"                                               \
75   GST_VIDEO_CAPS_GRAY16("BIG_ENDIAN")";"                                \
76   GST_VIDEO_CAPS_GRAY16("LITTLE_ENDIAN")";"                             \
77   GST_VIDEO_CAPS_r210";"                                                \
78   GST_VIDEO_CAPS_ARGB_64
79
80 static GstStaticPadTemplate gst_video_convert_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
82     GST_PAD_SRC,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS (CSP_VIDEO_CAPS)
85     );
86
87 static GstStaticPadTemplate gst_video_convert_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (CSP_VIDEO_CAPS)
92     );
93
94 GType gst_video_convert_get_type (void);
95
96 static void gst_video_convert_set_property (GObject * object,
97     guint property_id, const GValue * value, GParamSpec * pspec);
98 static void gst_video_convert_get_property (GObject * object,
99     guint property_id, GValue * value, GParamSpec * pspec);
100
101 static gboolean gst_video_convert_set_caps (GstBaseTransform * btrans,
102     GstCaps * incaps, GstCaps * outcaps);
103 static gboolean gst_video_convert_get_unit_size (GstBaseTransform * btrans,
104     GstCaps * caps, gsize * size);
105 static GstFlowReturn gst_video_convert_transform (GstBaseTransform * btrans,
106     GstBuffer * inbuf, GstBuffer * outbuf);
107
108 static GQuark _QRAWRGB;         /* "video/x-raw-rgb" */
109 static GQuark _QRAWYUV;         /* "video/x-raw-yuv" */
110 static GQuark _QALPHAMASK;      /* "alpha_mask" */
111
112
113 static GType
114 dither_method_get_type (void)
115 {
116   static GType gtype = 0;
117
118   if (gtype == 0) {
119     static const GEnumValue values[] = {
120       {DITHER_NONE, "No dithering (default)", "none"},
121       {DITHER_VERTERR, "Vertical error propogation", "verterr"},
122       {DITHER_HALFTONE, "Half-tone", "halftone"},
123       {0, NULL, NULL}
124     };
125
126     gtype = g_enum_register_static ("GstColorspaceDitherMethod", values);
127   }
128   return gtype;
129 }
130
131 /* copies the given caps */
132 static GstCaps *
133 gst_video_convert_caps_remove_format_info (GstCaps * caps)
134 {
135   GstStructure *yuvst, *rgbst, *grayst;
136   gint i, n;
137   GstCaps *res;
138
139   res = gst_caps_new_empty ();
140
141   n = gst_caps_get_size (caps);
142   for (i = 0; i < n; i++) {
143     yuvst = gst_caps_get_structure (caps, i);
144
145     /* If this is already expressed by the existing caps
146      * skip this structure */
147     if (i > 0 && gst_caps_is_subset_structure (res, yuvst))
148       continue;
149
150     yuvst = gst_structure_copy (yuvst);
151     gst_structure_set_name (yuvst, "video/x-raw-yuv");
152     gst_structure_remove_fields (yuvst, "format", "endianness", "depth",
153         "bpp", "red_mask", "green_mask", "blue_mask", "alpha_mask",
154         "palette_data", NULL);
155
156     rgbst = gst_structure_copy (yuvst);
157     gst_structure_set_name (rgbst, "video/x-raw-rgb");
158     gst_structure_remove_fields (rgbst, "color-matrix", "chroma-site", NULL);
159
160     grayst = gst_structure_copy (rgbst);
161     gst_structure_set_name (grayst, "video/x-raw-gray");
162
163     gst_caps_append_structure (res, yuvst);
164     gst_caps_append_structure (res, rgbst);
165     gst_caps_append_structure (res, grayst);
166   }
167
168   return res;
169 }
170
171 /* The caps can be transformed into any other caps with format info removed.
172  * However, we should prefer passthrough, so if passthrough is possible,
173  * put it first in the list. */
174 static GstCaps *
175 gst_video_convert_transform_caps (GstBaseTransform * btrans,
176     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
177 {
178   GstCaps *template;
179   GstCaps *tmp, *tmp2;
180   GstCaps *result;
181
182   template = gst_static_pad_template_get_caps (&gst_video_convert_src_template);
183   result = gst_caps_copy (caps);
184
185   /* Get all possible caps that we can transform to */
186   tmp = gst_video_convert_caps_remove_format_info (caps);
187
188   if (filter) {
189     tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
190     gst_caps_unref (tmp);
191     tmp = tmp2;
192   }
193
194   result = tmp;
195
196   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
197       GST_PTR_FORMAT, caps, result);
198
199   return result;
200 }
201
202 static gboolean
203 gst_video_convert_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
204     GstCaps * outcaps)
205 {
206   GstVideoConvert *space;
207   GstVideoFormat in_format;
208   GstVideoFormat out_format;
209   gint in_height, in_width;
210   gint out_height, out_width;
211   gint in_fps_n, in_fps_d, in_par_n, in_par_d;
212   gint out_fps_n, out_fps_d, out_par_n, out_par_d;
213   gboolean have_in_par, have_out_par;
214   gboolean have_in_interlaced, have_out_interlaced;
215   gboolean in_interlaced, out_interlaced;
216   gboolean ret;
217   ColorSpaceColorSpec in_spec, out_spec;
218
219   space = GST_VIDEO_CONVERT_CAST (btrans);
220
221   if (space->convert) {
222     videoconvert_convert_free (space->convert);
223   }
224
225   /* input caps */
226
227   ret = gst_video_format_parse_caps (incaps, &in_format, &in_width, &in_height);
228   if (!ret)
229     goto no_width_height;
230
231   ret = gst_video_parse_caps_framerate (incaps, &in_fps_n, &in_fps_d);
232   if (!ret)
233     goto no_framerate;
234
235   have_in_par = gst_video_parse_caps_pixel_aspect_ratio (incaps,
236       &in_par_n, &in_par_d);
237   have_in_interlaced = gst_video_format_parse_caps_interlaced (incaps,
238       &in_interlaced);
239
240   if (gst_video_format_is_rgb (in_format)) {
241     in_spec = COLOR_SPEC_RGB;
242   } else if (gst_video_format_is_yuv (in_format)) {
243     const gchar *matrix = gst_video_parse_caps_color_matrix (incaps);
244
245     if (matrix && g_str_equal (matrix, "hdtv"))
246       in_spec = COLOR_SPEC_YUV_BT709;
247     else
248       in_spec = COLOR_SPEC_YUV_BT470_6;
249   } else {
250     in_spec = COLOR_SPEC_GRAY;
251   }
252
253   /* output caps */
254
255   ret =
256       gst_video_format_parse_caps (outcaps, &out_format, &out_width,
257       &out_height);
258   if (!ret)
259     goto no_width_height;
260
261   ret = gst_video_parse_caps_framerate (outcaps, &out_fps_n, &out_fps_d);
262   if (!ret)
263     goto no_framerate;
264
265   have_out_par = gst_video_parse_caps_pixel_aspect_ratio (outcaps,
266       &out_par_n, &out_par_d);
267   have_out_interlaced = gst_video_format_parse_caps_interlaced (incaps,
268       &out_interlaced);
269
270   if (gst_video_format_is_rgb (out_format)) {
271     out_spec = COLOR_SPEC_RGB;
272   } else if (gst_video_format_is_yuv (out_format)) {
273     const gchar *matrix = gst_video_parse_caps_color_matrix (outcaps);
274
275     if (matrix && g_str_equal (matrix, "hdtv"))
276       out_spec = COLOR_SPEC_YUV_BT709;
277     else
278       out_spec = COLOR_SPEC_YUV_BT470_6;
279   } else {
280     out_spec = COLOR_SPEC_GRAY;
281   }
282
283   /* these must match */
284   if (in_width != out_width || in_height != out_height ||
285       in_fps_n != out_fps_n || in_fps_d != out_fps_d)
286     goto format_mismatch;
287
288   /* if present, these must match too */
289   if (have_in_par && have_out_par &&
290       (in_par_n != out_par_n || in_par_d != out_par_d))
291     goto format_mismatch;
292
293   /* if present, these must match too */
294   if (have_in_interlaced && have_out_interlaced &&
295       in_interlaced != out_interlaced)
296     goto format_mismatch;
297
298   space->from_format = in_format;
299   space->from_spec = in_spec;
300   space->to_format = out_format;
301   space->to_spec = out_spec;
302   space->width = in_width;
303   space->height = in_height;
304   space->interlaced = in_interlaced;
305
306   space->convert = videoconvert_convert_new (out_format, out_spec, in_format,
307       in_spec, in_width, in_height);
308   if (space->convert) {
309     videoconvert_convert_set_interlaced (space->convert, in_interlaced);
310   }
311   /* palette, only for from data */
312   if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED &&
313       space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
314     goto format_mismatch;
315   } else if (space->from_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
316     GstBuffer *palette;
317     guint32 *data;
318
319     palette = gst_video_parse_caps_palette (incaps);
320
321     if (!palette || gst_buffer_get_size (palette) < 256 * 4) {
322       if (palette)
323         gst_buffer_unref (palette);
324       goto invalid_palette;
325     }
326
327     data = gst_buffer_map (palette, NULL, NULL, GST_MAP_READ);
328     videoconvert_convert_set_palette (space->convert, data);
329     gst_buffer_unmap (palette, data, -1);
330
331     gst_buffer_unref (palette);
332   } else if (space->to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
333     const guint32 *palette;
334     GstBuffer *p_buf;
335
336     palette = videoconvert_convert_get_palette (space->convert);
337
338     p_buf = gst_buffer_new_and_alloc (256 * 4);
339     gst_buffer_fill (p_buf, 0, palette, 256 * 4);
340     gst_caps_set_simple (outcaps, "palette_data", GST_TYPE_BUFFER, p_buf, NULL);
341     gst_buffer_unref (p_buf);
342   }
343
344   GST_DEBUG ("reconfigured %d %d", space->from_format, space->to_format);
345
346   return TRUE;
347
348   /* ERRORS */
349 no_width_height:
350   {
351     GST_ERROR_OBJECT (space, "did not specify width or height");
352     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
353     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
354     return FALSE;
355   }
356 no_framerate:
357   {
358     GST_ERROR_OBJECT (space, "did not specify framerate");
359     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
360     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
361     return FALSE;
362   }
363 format_mismatch:
364   {
365     GST_ERROR_OBJECT (space, "input and output formats do not match");
366     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
367     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
368     return FALSE;
369   }
370 invalid_palette:
371   {
372     GST_ERROR_OBJECT (space, "invalid palette");
373     space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
374     space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
375     return FALSE;
376   }
377 }
378
379 #define gst_video_convert_parent_class parent_class
380 G_DEFINE_TYPE (GstVideoConvert, gst_video_convert, GST_TYPE_VIDEO_FILTER);
381
382 static void
383 gst_video_convert_finalize (GObject * obj)
384 {
385   GstVideoConvert *space = GST_VIDEO_CONVERT (obj);
386
387   if (space->convert) {
388     videoconvert_convert_free (space->convert);
389   }
390
391   G_OBJECT_CLASS (parent_class)->finalize (obj);
392 }
393
394 static void
395 gst_video_convert_class_init (GstVideoConvertClass * klass)
396 {
397   GObjectClass *gobject_class = (GObjectClass *) klass;
398   GstElementClass *gstelement_class = (GstElementClass *) klass;
399   GstBaseTransformClass *gstbasetransform_class =
400       (GstBaseTransformClass *) klass;
401
402   gobject_class->set_property = gst_video_convert_set_property;
403   gobject_class->get_property = gst_video_convert_get_property;
404   gobject_class->finalize = gst_video_convert_finalize;
405
406   gst_element_class_add_pad_template (gstelement_class,
407       gst_static_pad_template_get (&gst_video_convert_src_template));
408   gst_element_class_add_pad_template (gstelement_class,
409       gst_static_pad_template_get (&gst_video_convert_sink_template));
410
411   gst_element_class_set_details_simple (gstelement_class,
412       " Colorspace converter", "Filter/Converter/Video",
413       "Converts video from one colorspace to another",
414       "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
415
416   _QRAWRGB = g_quark_from_string ("video/x-raw-rgb");
417   _QRAWYUV = g_quark_from_string ("video/x-raw-yuv");
418   _QALPHAMASK = g_quark_from_string ("alpha_mask");
419
420   gstbasetransform_class->transform_caps =
421       GST_DEBUG_FUNCPTR (gst_video_convert_transform_caps);
422   gstbasetransform_class->set_caps =
423       GST_DEBUG_FUNCPTR (gst_video_convert_set_caps);
424   gstbasetransform_class->get_unit_size =
425       GST_DEBUG_FUNCPTR (gst_video_convert_get_unit_size);
426   gstbasetransform_class->transform =
427       GST_DEBUG_FUNCPTR (gst_video_convert_transform);
428
429   gstbasetransform_class->passthrough_on_same_caps = TRUE;
430
431   g_object_class_install_property (gobject_class, PROP_DITHER,
432       g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
433           dither_method_get_type (), DITHER_NONE,
434           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 }
436
437 static void
438 gst_video_convert_init (GstVideoConvert * space)
439 {
440   space->from_format = GST_VIDEO_FORMAT_UNKNOWN;
441   space->to_format = GST_VIDEO_FORMAT_UNKNOWN;
442 }
443
444 void
445 gst_video_convert_set_property (GObject * object, guint property_id,
446     const GValue * value, GParamSpec * pspec)
447 {
448   GstVideoConvert *csp;
449
450   csp = GST_VIDEO_CONVERT (object);
451
452   switch (property_id) {
453     case PROP_DITHER:
454       csp->dither = g_value_get_enum (value);
455       break;
456     default:
457       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
458       break;
459   }
460 }
461
462 void
463 gst_video_convert_get_property (GObject * object, guint property_id,
464     GValue * value, GParamSpec * pspec)
465 {
466   GstVideoConvert *csp;
467
468   csp = GST_VIDEO_CONVERT (object);
469
470   switch (property_id) {
471     case PROP_DITHER:
472       g_value_set_enum (value, csp->dither);
473       break;
474     default:
475       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
476       break;
477   }
478 }
479
480 static gboolean
481 gst_video_convert_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
482     gsize * size)
483 {
484   gboolean ret = TRUE;
485   GstVideoFormat format;
486   gint width, height;
487
488   g_assert (size);
489
490   ret = gst_video_format_parse_caps (caps, &format, &width, &height);
491   if (ret) {
492     *size = gst_video_format_get_size (format, width, height);
493   }
494
495   return ret;
496 }
497
498 static GstFlowReturn
499 gst_video_convert_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
500     GstBuffer * outbuf)
501 {
502   GstVideoConvert *space;
503   guint8 *indata, *outdata;
504   gsize insize, outsize;
505
506   space = GST_VIDEO_CONVERT_CAST (btrans);
507
508   GST_DEBUG ("from %d -> to %d", space->from_format, space->to_format);
509
510   if (G_UNLIKELY (space->from_format == GST_VIDEO_FORMAT_UNKNOWN ||
511           space->to_format == GST_VIDEO_FORMAT_UNKNOWN))
512     goto unknown_format;
513
514   videoconvert_convert_set_dither (space->convert, space->dither);
515
516   indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
517   outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
518
519   videoconvert_convert_convert (space->convert, outdata, indata);
520
521   gst_buffer_unmap (outbuf, outdata, outsize);
522   gst_buffer_unmap (inbuf, indata, insize);
523
524   /* baseclass copies timestamps */
525   GST_DEBUG ("from %d -> to %d done", space->from_format, space->to_format);
526
527   return GST_FLOW_OK;
528
529   /* ERRORS */
530 unknown_format:
531   {
532     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
533         ("attempting to convert colorspaces between unknown formats"));
534     return GST_FLOW_NOT_NEGOTIATED;
535   }
536 #if 0
537 not_supported:
538   {
539     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
540         ("cannot convert between formats"));
541     return GST_FLOW_NOT_SUPPORTED;
542   }
543 #endif
544 }
545
546 static gboolean
547 plugin_init (GstPlugin * plugin)
548 {
549   GST_DEBUG_CATEGORY_INIT (videoconvert_debug, "videoconvert", 0,
550       "Colorspace Converter");
551   GST_DEBUG_CATEGORY_GET (videoconvert_performance, "GST_PERFORMANCE");
552
553   return gst_element_register (plugin, "videoconvert",
554       GST_RANK_NONE, GST_TYPE_VIDEO_CONVERT);
555 }
556
557 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
558     GST_VERSION_MINOR,
559     "videoconvert", "Colorspace conversion", plugin_init, VERSION, "LGPL", "",
560     "")