3ef03c25145bf992f6dd52f2183e4ff9c6a48670
[platform/upstream/gstreamer.git] / gst / ffmpegcolorspace / gstffmpegcolorspace.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  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-ffmpegcolorspace
24  *
25  * <refsect2>
26  * <title>Example launch line</title>
27  * <para>
28  * <programlisting>
29  * gst-launch -v videotestsrc ! video/x-raw-yuv,format=\(fourcc\)YUY2 ! ffmpegcolorspace ! ximagesink
30  * </programlisting>
31  * </para>
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #  include "config.h"
37 #endif
38
39 #include "gstffmpegcolorspace.h"
40 #include "gstffmpegcodecmap.h"
41
42 GST_DEBUG_CATEGORY (ffmpegcolorspace_debug);
43 #define GST_CAT_DEFAULT ffmpegcolorspace_debug
44
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>",
51 };
52
53
54 /* Stereo signals and args */
55 enum
56 {
57   /* FILL ME */
58   LAST_SIGNAL
59 };
60
61 enum
62 {
63   ARG_0,
64 };
65
66 static GType gst_ffmpegcsp_get_type (void);
67
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);
71
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);
78 #if 0
79 static GstFlowReturn gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans,
80     GstBuffer * inbuf);
81 #endif
82
83 static GstPadTemplate *sinktempl, *srctempl;
84 static GstElementClass *parent_class = NULL;
85
86 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
87
88 /* copies the given caps */
89 static GstCaps *
90 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
91 {
92   int i;
93   GstStructure *structure;
94   GstCaps *rgbcaps;
95
96   caps = gst_caps_copy (caps);
97
98   for (i = 0; i < gst_caps_get_size (caps); i++) {
99     structure = gst_caps_get_structure (caps, i);
100
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   }
111
112   gst_caps_do_simplify (caps);
113   rgbcaps = gst_caps_copy (caps);
114
115   for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
116     structure = gst_caps_get_structure (rgbcaps, i);
117
118     gst_structure_set_name (structure, "video/x-raw-rgb");
119   }
120
121   gst_caps_append (caps, rgbcaps);
122
123   return caps;
124 }
125
126 /* The caps can be transformed into any other caps with format info removed.
127  * However, we should prefer passthrough, so if passthrough is possible,
128  * put it first in the list. */
129 static GstCaps *
130 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
131     GstPadDirection direction, GstCaps * caps)
132 {
133   GstFFMpegCsp *space;
134   GstCaps *template;
135   GstCaps *result;
136
137   space = GST_FFMPEGCSP (btrans);
138
139   template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
140   result = gst_caps_intersect (caps, template);
141
142   gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));
143
144   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
145       GST_PTR_FORMAT, caps, result);
146   return result;
147 }
148
149 static gboolean
150 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
151     GstCaps * outcaps)
152 {
153   GstFFMpegCsp *space;
154   GstStructure *structure;
155   gint in_height, in_width;
156   gint out_height, out_width;
157   gdouble in_framerate, out_framerate;
158   const GValue *in_par = NULL;
159   const GValue *out_par = NULL;
160   AVCodecContext *ctx;
161   gboolean res;
162
163   space = GST_FFMPEGCSP (btrans);
164
165   /* parse in and output values */
166   structure = gst_caps_get_structure (incaps, 0);
167
168   /* we have to have width and height */
169   res = gst_structure_get_int (structure, "width", &in_width);
170   res &= gst_structure_get_int (structure, "height", &in_height);
171   res &= gst_structure_get_double (structure, "framerate", &in_framerate);
172   if (!res)
173     goto no_width_height;
174
175   /* this is optional */
176   in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
177
178   structure = gst_caps_get_structure (outcaps, 0);
179
180   /* we have to have width and height */
181   res = gst_structure_get_int (structure, "width", &out_width);
182   res &= gst_structure_get_int (structure, "height", &out_height);
183   res &= gst_structure_get_double (structure, "framerate", &out_framerate);
184   if (!res)
185     goto no_width_height;
186
187   /* this is optional */
188   out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
189
190   /* these must match */
191   if (in_width != out_width || in_height != out_height ||
192       in_framerate != out_framerate)
193     goto format_mismatch;
194
195   /* if present, these must match too */
196   if (in_par && out_par
197       && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
198     goto format_mismatch;
199
200   ctx = avcodec_alloc_context ();
201
202   space->width = ctx->width = in_width;
203   space->height = ctx->height = in_height;
204
205   /* get from format */
206   ctx->pix_fmt = PIX_FMT_NB;
207   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
208   if (ctx->pix_fmt == PIX_FMT_NB)
209     goto invalid_in_caps;
210   space->from_pixfmt = ctx->pix_fmt;
211
212   /* palette, only for from data */
213   if (space->palette)
214     av_free (space->palette);
215   space->palette = ctx->palctrl;
216
217   /* get to format */
218   ctx->pix_fmt = PIX_FMT_NB;
219   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
220   if (ctx->pix_fmt == PIX_FMT_NB)
221     goto invalid_out_caps;
222   space->to_pixfmt = ctx->pix_fmt;
223
224   GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
225
226   av_free (ctx);
227
228   return TRUE;
229
230   /* ERRORS */
231 no_width_height:
232   {
233     GST_DEBUG ("did not specify width or height");
234     space->from_pixfmt = PIX_FMT_NB;
235     space->to_pixfmt = PIX_FMT_NB;
236     return FALSE;
237   }
238 format_mismatch:
239   {
240     GST_DEBUG ("input and output formats do not match");
241     space->from_pixfmt = PIX_FMT_NB;
242     space->to_pixfmt = PIX_FMT_NB;
243     return FALSE;
244   }
245 invalid_in_caps:
246   {
247     GST_DEBUG ("could not configure context for input format");
248     av_free (ctx);
249     space->from_pixfmt = PIX_FMT_NB;
250     space->to_pixfmt = PIX_FMT_NB;
251     return FALSE;
252   }
253 invalid_out_caps:
254   {
255     GST_DEBUG ("could not configure context for output format");
256     av_free (ctx);
257     space->from_pixfmt = PIX_FMT_NB;
258     space->to_pixfmt = PIX_FMT_NB;
259     return FALSE;
260   }
261 }
262
263 static GType
264 gst_ffmpegcsp_get_type (void)
265 {
266   static GType ffmpegcsp_type = 0;
267
268   if (!ffmpegcsp_type) {
269     static const GTypeInfo ffmpegcsp_info = {
270       sizeof (GstFFMpegCspClass),
271       (GBaseInitFunc) gst_ffmpegcsp_base_init,
272       NULL,
273       (GClassInitFunc) gst_ffmpegcsp_class_init,
274       NULL,
275       NULL,
276       sizeof (GstFFMpegCsp),
277       0,
278       (GInstanceInitFunc) gst_ffmpegcsp_init,
279     };
280
281     ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
282         "GstFFMpegColorspace", &ffmpegcsp_info, 0);
283   }
284
285   return ffmpegcsp_type;
286 }
287
288 static void
289 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
290 {
291   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
292
293   gst_element_class_add_pad_template (element_class, srctempl);
294   gst_element_class_add_pad_template (element_class, sinktempl);
295   gst_element_class_set_details (element_class, &ffmpegcsp_details);
296 }
297
298 static void
299 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
300 {
301   GObjectClass *gobject_class;
302   GstElementClass *gstelement_class;
303   GstBaseTransformClass *gstbasetransform_class;
304
305   gobject_class = (GObjectClass *) klass;
306   gstelement_class = (GstElementClass *) klass;
307   gstbasetransform_class = (GstBaseTransformClass *) klass;
308
309   parent_class = g_type_class_ref (GST_TYPE_BASE_TRANSFORM);
310
311   gstbasetransform_class->transform_caps =
312       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
313   gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
314   gstbasetransform_class->get_unit_size =
315       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
316   gstbasetransform_class->transform =
317       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
318 #if 0
319   gstbasetransform_class->transform_ip =
320       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
321 #endif
322
323   gstbasetransform_class->passthrough_on_same_caps = TRUE;
324
325   GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
326       "FFMPEG-based colorspace converter");
327 }
328
329 static void
330 gst_ffmpegcsp_init (GstFFMpegCsp * space)
331 {
332   space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
333   space->palette = NULL;
334 }
335
336 static gboolean
337 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
338     guint * size)
339 {
340   GstFFMpegCsp *space = NULL;
341   GstStructure *structure = NULL;
342   AVCodecContext *ctx = NULL;
343   gint width, height;
344
345   g_return_val_if_fail (size, FALSE);
346
347   space = GST_FFMPEGCSP (btrans);
348
349   structure = gst_caps_get_structure (caps, 0);
350   gst_structure_get_int (structure, "width", &width);
351   gst_structure_get_int (structure, "height", &height);
352
353   ctx = avcodec_alloc_context ();
354
355   g_assert (ctx != NULL);
356
357   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
358
359   *size = avpicture_get_size (ctx->pix_fmt, width, height);
360
361   if (space->palette)
362     *size -= 4 * 256;
363
364   av_free (ctx);
365
366   return TRUE;
367 }
368
369 #if 0
370 /* FIXME: Could use transform_ip to implement endianness swap type operations */
371 static GstFlowReturn
372 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
373 {
374   /* do nothing */
375   return GST_FLOW_OK;
376 }
377 #endif
378
379 static GstFlowReturn
380 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
381     GstBuffer * outbuf)
382 {
383   GstFFMpegCsp *space;
384
385   space = GST_FFMPEGCSP (btrans);
386
387   GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
388   if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
389     goto unknown_format;
390
391   /* fill from with source data */
392   gst_ffmpegcsp_avpicture_fill (&space->from_frame,
393       GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);
394
395   /* fill optional palette */
396   if (space->palette)
397     space->from_frame.data[1] = (uint8_t *) space->palette->palette;
398
399   /* fill target frame */
400   gst_ffmpegcsp_avpicture_fill (&space->to_frame,
401       GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
402
403   /* and convert */
404   img_convert (&space->to_frame, space->to_pixfmt,
405       &space->from_frame, space->from_pixfmt, space->width, space->height);
406
407   /* copy timestamps */
408   gst_buffer_stamp (outbuf, inbuf);
409   GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
410
411   return GST_FLOW_OK;
412
413   /* ERRORS */
414 unknown_format:
415   {
416     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
417         ("attempting to convert colorspaces between unknown formats"));
418     return GST_FLOW_NOT_NEGOTIATED;
419   }
420 }
421
422 gboolean
423 gst_ffmpegcolorspace_register (GstPlugin * plugin)
424 {
425   GstCaps *caps;
426
427   /* template caps */
428   caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
429
430   /* build templates */
431   srctempl = gst_pad_template_new ("src",
432       GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
433
434   /* the sink template will do palette handling as well... */
435   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
436
437   return gst_element_register (plugin, "ffmpegcolorspace",
438       GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
439 }