Convert elements to use fractions for their framerate.
[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   GstCaps *graycaps;
96
97   caps = gst_caps_copy (caps);
98
99   for (i = 0; i < gst_caps_get_size (caps); i++) {
100     structure = gst_caps_get_structure (caps, i);
101
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   }
112
113   gst_caps_do_simplify (caps);
114   rgbcaps = gst_caps_copy (caps);
115
116   for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
117     structure = gst_caps_get_structure (rgbcaps, i);
118
119     gst_structure_set_name (structure, "video/x-raw-rgb");
120   }
121   graycaps = gst_caps_copy (caps);
122
123   for (i = 0; i < gst_caps_get_size (graycaps); i++) {
124     structure = gst_caps_get_structure (graycaps, i);
125
126     gst_structure_set_name (structure, "video/x-raw-gray");
127   }
128
129   gst_caps_append (caps, graycaps);
130   gst_caps_append (caps, rgbcaps);
131
132   return caps;
133 }
134
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. */
138 static GstCaps *
139 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
140     GstPadDirection direction, GstCaps * caps)
141 {
142   GstFFMpegCsp *space;
143   GstCaps *template;
144   GstCaps *result;
145
146   space = GST_FFMPEGCSP (btrans);
147
148   template = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
149   result = gst_caps_intersect (caps, template);
150
151   gst_caps_append (result, gst_ffmpegcsp_caps_remove_format_info (caps));
152
153   GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
154       GST_PTR_FORMAT, caps, result);
155   return result;
156 }
157
158 static gboolean
159 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
160     GstCaps * outcaps)
161 {
162   GstFFMpegCsp *space;
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;
170   AVCodecContext *ctx;
171   gboolean res;
172
173   space = GST_FFMPEGCSP (btrans);
174
175   /* parse in and output values */
176   structure = gst_caps_get_structure (incaps, 0);
177
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);
181   if (!res)
182     goto no_width_height;
183
184   /* and framerate */
185   in_framerate = gst_structure_get_value (structure, "framerate");
186   if (in_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (in_framerate))
187     goto no_framerate;
188
189   /* this is optional */
190   in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
191
192   structure = gst_caps_get_structure (outcaps, 0);
193
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);
197   if (!res)
198     goto no_width_height;
199
200   /* and framerate */
201   out_framerate = gst_structure_get_value (structure, "framerate");
202   if (out_framerate == NULL || !GST_VALUE_HOLDS_FRACTION (out_framerate))
203     goto no_framerate;
204
205   /* this is optional */
206   out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
207
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;
212
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;
217
218   ctx = avcodec_alloc_context ();
219
220   space->width = ctx->width = in_width;
221   space->height = ctx->height = in_height;
222
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;
229
230   /* palette, only for from data */
231   if (space->palette)
232     av_free (space->palette);
233   space->palette = ctx->palctrl;
234
235   /* get to format */
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;
241
242   GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
243
244   av_free (ctx);
245
246   return TRUE;
247
248   /* ERRORS */
249 no_width_height:
250   {
251     GST_DEBUG ("did not specify width or height");
252     space->from_pixfmt = PIX_FMT_NB;
253     space->to_pixfmt = PIX_FMT_NB;
254     return FALSE;
255   }
256 no_framerate:
257   {
258     GST_DEBUG ("did not specify framerate");
259     space->from_pixfmt = PIX_FMT_NB;
260     space->to_pixfmt = PIX_FMT_NB;
261     return FALSE;
262   }
263 format_mismatch:
264   {
265     GST_DEBUG ("input and output formats do not match");
266     space->from_pixfmt = PIX_FMT_NB;
267     space->to_pixfmt = PIX_FMT_NB;
268     return FALSE;
269   }
270 invalid_in_caps:
271   {
272     GST_DEBUG ("could not configure context for input format");
273     av_free (ctx);
274     space->from_pixfmt = PIX_FMT_NB;
275     space->to_pixfmt = PIX_FMT_NB;
276     return FALSE;
277   }
278 invalid_out_caps:
279   {
280     GST_DEBUG ("could not configure context for output format");
281     av_free (ctx);
282     space->from_pixfmt = PIX_FMT_NB;
283     space->to_pixfmt = PIX_FMT_NB;
284     return FALSE;
285   }
286 }
287
288 static GType
289 gst_ffmpegcsp_get_type (void)
290 {
291   static GType ffmpegcsp_type = 0;
292
293   if (!ffmpegcsp_type) {
294     static const GTypeInfo ffmpegcsp_info = {
295       sizeof (GstFFMpegCspClass),
296       (GBaseInitFunc) gst_ffmpegcsp_base_init,
297       NULL,
298       (GClassInitFunc) gst_ffmpegcsp_class_init,
299       NULL,
300       NULL,
301       sizeof (GstFFMpegCsp),
302       0,
303       (GInstanceInitFunc) gst_ffmpegcsp_init,
304     };
305
306     ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
307         "GstFFMpegColorspace", &ffmpegcsp_info, 0);
308   }
309
310   return ffmpegcsp_type;
311 }
312
313 static void
314 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
315 {
316   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
317
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);
321 }
322
323 static void
324 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
325 {
326   GObjectClass *gobject_class;
327   GstElementClass *gstelement_class;
328   GstBaseTransformClass *gstbasetransform_class;
329
330   gobject_class = (GObjectClass *) klass;
331   gstelement_class = (GstElementClass *) klass;
332   gstbasetransform_class = (GstBaseTransformClass *) klass;
333
334   parent_class = g_type_class_ref (GST_TYPE_BASE_TRANSFORM);
335
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);
343 #if 0
344   gstbasetransform_class->transform_ip =
345       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
346 #endif
347
348   gstbasetransform_class->passthrough_on_same_caps = TRUE;
349
350   GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
351       "FFMPEG-based colorspace converter");
352 }
353
354 static void
355 gst_ffmpegcsp_init (GstFFMpegCsp * space)
356 {
357   space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
358   space->palette = NULL;
359 }
360
361 static gboolean
362 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
363     guint * size)
364 {
365   GstFFMpegCsp *space = NULL;
366   GstStructure *structure = NULL;
367   AVCodecContext *ctx = NULL;
368   gint width, height;
369
370   g_return_val_if_fail (size, FALSE);
371
372   space = GST_FFMPEGCSP (btrans);
373
374   structure = gst_caps_get_structure (caps, 0);
375   gst_structure_get_int (structure, "width", &width);
376   gst_structure_get_int (structure, "height", &height);
377
378   ctx = avcodec_alloc_context ();
379
380   g_assert (ctx != NULL);
381
382   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
383
384   *size = avpicture_get_size (ctx->pix_fmt, width, height);
385
386   if (space->palette)
387     *size -= 4 * 256;
388
389   av_free (ctx);
390
391   return TRUE;
392 }
393
394 #if 0
395 /* FIXME: Could use transform_ip to implement endianness swap type operations */
396 static GstFlowReturn
397 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
398 {
399   /* do nothing */
400   return GST_FLOW_OK;
401 }
402 #endif
403
404 static GstFlowReturn
405 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
406     GstBuffer * outbuf)
407 {
408   GstFFMpegCsp *space;
409
410   space = GST_FFMPEGCSP (btrans);
411
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)
414     goto unknown_format;
415
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);
419
420   /* fill optional palette */
421   if (space->palette)
422     space->from_frame.data[1] = (uint8_t *) space->palette->palette;
423
424   /* fill target frame */
425   gst_ffmpegcsp_avpicture_fill (&space->to_frame,
426       GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
427
428   /* and convert */
429   img_convert (&space->to_frame, space->to_pixfmt,
430       &space->from_frame, space->from_pixfmt, space->width, space->height);
431
432   /* copy timestamps */
433   gst_buffer_stamp (outbuf, inbuf);
434   GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
435
436   return GST_FLOW_OK;
437
438   /* ERRORS */
439 unknown_format:
440   {
441     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
442         ("attempting to convert colorspaces between unknown formats"));
443     return GST_FLOW_NOT_NEGOTIATED;
444   }
445 }
446
447 gboolean
448 gst_ffmpegcolorspace_register (GstPlugin * plugin)
449 {
450   GstCaps *caps;
451
452   /* template caps */
453   caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
454
455   /* build templates */
456   srctempl = gst_pad_template_new ("src",
457       GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
458
459   /* the sink template will do palette handling as well... */
460   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
461
462   return gst_element_register (plugin, "ffmpegcolorspace",
463       GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
464 }