gst/ffmpegcolorspace/gstffmpegcolorspace.c: We are asked to compute a buffer size...
[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 static GstCaps *
89 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
90 {
91   int i;
92   GstStructure *structure;
93   GstCaps *rgbcaps;
94
95   caps = gst_caps_copy (caps);
96
97   for (i = 0; i < gst_caps_get_size (caps); i++) {
98     structure = gst_caps_get_structure (caps, i);
99
100     gst_structure_set_name (structure, "video/x-raw-yuv");
101     gst_structure_remove_field (structure, "format");
102     gst_structure_remove_field (structure, "endianness");
103     gst_structure_remove_field (structure, "depth");
104     gst_structure_remove_field (structure, "bpp");
105     gst_structure_remove_field (structure, "red_mask");
106     gst_structure_remove_field (structure, "green_mask");
107     gst_structure_remove_field (structure, "blue_mask");
108     gst_structure_remove_field (structure, "alpha_mask");
109   }
110
111   gst_caps_do_simplify (caps);
112   rgbcaps = gst_caps_copy (caps);
113
114   for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
115     structure = gst_caps_get_structure (rgbcaps, i);
116
117     gst_structure_set_name (structure, "video/x-raw-rgb");
118   }
119
120   gst_caps_append (caps, rgbcaps);
121
122   return caps;
123 }
124
125 static GstCaps *
126 gst_ffmpegcsp_transform_caps (GstBaseTransform * btrans,
127     GstPadDirection direction, GstCaps * caps)
128 {
129   GstFFMpegCsp *space;
130   GstCaps *result;
131
132   space = GST_FFMPEGCSP (btrans);
133
134   result = gst_ffmpegcsp_caps_remove_format_info (caps);
135
136   return result;
137 }
138
139 static gboolean
140 gst_ffmpegcsp_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
141     GstCaps * outcaps)
142 {
143   GstFFMpegCsp *space;
144   GstStructure *structure;
145   gint in_height, in_width;
146   gint out_height, out_width;
147   gdouble in_framerate, out_framerate;
148   const GValue *in_par = NULL;
149   const GValue *out_par = NULL;
150   AVCodecContext *ctx;
151
152   space = GST_FFMPEGCSP (btrans);
153
154   /* parse in and output values */
155   structure = gst_caps_get_structure (incaps, 0);
156   gst_structure_get_int (structure, "width", &in_width);
157   gst_structure_get_int (structure, "height", &in_height);
158   gst_structure_get_double (structure, "framerate", &in_framerate);
159   in_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
160
161   structure = gst_caps_get_structure (outcaps, 0);
162   gst_structure_get_int (structure, "width", &out_width);
163   gst_structure_get_int (structure, "height", &out_height);
164   gst_structure_get_double (structure, "framerate", &out_framerate);
165   out_par = gst_structure_get_value (structure, "pixel-aspect-ratio");
166
167   if (in_width != out_width || in_height != out_height ||
168       in_framerate != out_framerate)
169     goto format_mismatch;
170
171   if (in_par && out_par
172       && gst_value_compare (in_par, out_par) != GST_VALUE_EQUAL)
173     goto format_mismatch;
174
175   ctx = avcodec_alloc_context ();
176
177   space->width = ctx->width = in_width;
178   space->height = ctx->height = in_height;
179
180   /* get from format */
181   ctx->pix_fmt = PIX_FMT_NB;
182   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx);
183   if (ctx->pix_fmt == PIX_FMT_NB)
184     goto invalid_in_caps;
185   space->from_pixfmt = ctx->pix_fmt;
186
187   /* palette, only for from data */
188   if (space->palette)
189     av_free (space->palette);
190   space->palette = ctx->palctrl;
191
192   /* get to format */
193   ctx->pix_fmt = PIX_FMT_NB;
194   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, outcaps, ctx);
195   if (ctx->pix_fmt == PIX_FMT_NB)
196     goto invalid_out_caps;
197   space->to_pixfmt = ctx->pix_fmt;
198
199   GST_DEBUG ("reconfigured %d %d", space->from_pixfmt, space->to_pixfmt);
200
201   av_free (ctx);
202
203   return TRUE;
204
205   /* ERRORS */
206 format_mismatch:
207   {
208     GST_DEBUG ("input and output formats do not match");
209     space->from_pixfmt = PIX_FMT_NB;
210     space->to_pixfmt = PIX_FMT_NB;
211     return FALSE;
212   }
213 invalid_in_caps:
214   {
215     GST_DEBUG ("could not configure context for input format");
216     av_free (ctx);
217     space->from_pixfmt = PIX_FMT_NB;
218     space->to_pixfmt = PIX_FMT_NB;
219     return FALSE;
220   }
221 invalid_out_caps:
222   {
223     GST_DEBUG ("could not configure context for output format");
224     av_free (ctx);
225     space->from_pixfmt = PIX_FMT_NB;
226     space->to_pixfmt = PIX_FMT_NB;
227     return FALSE;
228   }
229 }
230
231 static GType
232 gst_ffmpegcsp_get_type (void)
233 {
234   static GType ffmpegcsp_type = 0;
235
236   if (!ffmpegcsp_type) {
237     static const GTypeInfo ffmpegcsp_info = {
238       sizeof (GstFFMpegCspClass),
239       (GBaseInitFunc) gst_ffmpegcsp_base_init,
240       NULL,
241       (GClassInitFunc) gst_ffmpegcsp_class_init,
242       NULL,
243       NULL,
244       sizeof (GstFFMpegCsp),
245       0,
246       (GInstanceInitFunc) gst_ffmpegcsp_init,
247     };
248
249     ffmpegcsp_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
250         "GstFFMpegColorspace", &ffmpegcsp_info, 0);
251   }
252
253   return ffmpegcsp_type;
254 }
255
256 static void
257 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
258 {
259   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
260
261   gst_element_class_add_pad_template (element_class, srctempl);
262   gst_element_class_add_pad_template (element_class, sinktempl);
263   gst_element_class_set_details (element_class, &ffmpegcsp_details);
264 }
265
266 static void
267 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
268 {
269   GObjectClass *gobject_class;
270   GstElementClass *gstelement_class;
271   GstBaseTransformClass *gstbasetransform_class;
272
273   gobject_class = (GObjectClass *) klass;
274   gstelement_class = (GstElementClass *) klass;
275   gstbasetransform_class = (GstBaseTransformClass *) klass;
276
277   parent_class = g_type_class_ref (GST_TYPE_BASE_TRANSFORM);
278
279   gstbasetransform_class->transform_caps =
280       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_caps);
281   gstbasetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ffmpegcsp_set_caps);
282   gstbasetransform_class->get_unit_size =
283       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_get_unit_size);
284   gstbasetransform_class->transform =
285       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform);
286 #if 0
287   gstbasetransform_class->transform_ip =
288       GST_DEBUG_FUNCPTR (gst_ffmpegcsp_transform_ip);
289 #endif
290
291   gstbasetransform_class->passthrough_on_same_caps = TRUE;
292
293   GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
294       "FFMPEG-based colorspace converter");
295 }
296
297 static void
298 gst_ffmpegcsp_init (GstFFMpegCsp * space)
299 {
300   space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
301   space->palette = NULL;
302 }
303
304 static gboolean
305 gst_ffmpegcsp_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
306     guint * size)
307 {
308   GstFFMpegCsp *space = NULL;
309   GstStructure *structure = NULL;
310   AVCodecContext *ctx = NULL;
311   gint width, height;
312
313   g_return_val_if_fail (size, FALSE);
314
315   space = GST_FFMPEGCSP (btrans);
316
317   structure = gst_caps_get_structure (caps, 0);
318   gst_structure_get_int (structure, "width", &width);
319   gst_structure_get_int (structure, "height", &height);
320
321   ctx = avcodec_alloc_context ();
322
323   g_assert (ctx != NULL);
324
325   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
326
327   *size = avpicture_get_size (ctx->pix_fmt, width, height);
328
329   av_free (ctx);
330
331   return TRUE;
332 }
333
334 #if 0
335 /* FIXME: Could use transform_ip to implement endianness swap type operations */
336 static GstFlowReturn
337 gst_ffmpegcsp_transform_ip (GstBaseTransform * btrans, GstBuffer * inbuf)
338 {
339   /* do nothing */
340   return GST_FLOW_OK;
341 }
342 #endif
343
344 static GstFlowReturn
345 gst_ffmpegcsp_transform (GstBaseTransform * btrans, GstBuffer * inbuf,
346     GstBuffer * outbuf)
347 {
348   GstFFMpegCsp *space;
349
350   space = GST_FFMPEGCSP (btrans);
351
352   GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
353   if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
354     goto unknown_format;
355
356   /* fill from with source data */
357   gst_ffmpegcsp_avpicture_fill (&space->from_frame,
358       GST_BUFFER_DATA (inbuf), space->from_pixfmt, space->width, space->height);
359
360   /* fill optional palette */
361   if (space->palette)
362     space->from_frame.data[1] = (uint8_t *) space->palette;
363
364   /* fill target frame */
365   gst_ffmpegcsp_avpicture_fill (&space->to_frame,
366       GST_BUFFER_DATA (outbuf), space->to_pixfmt, space->width, space->height);
367
368   /* and convert */
369   img_convert (&space->to_frame, space->to_pixfmt,
370       &space->from_frame, space->from_pixfmt, space->width, space->height);
371
372   /* copy timestamps */
373   gst_buffer_stamp (outbuf, inbuf);
374   GST_DEBUG ("from %d -> to %d done", space->from_pixfmt, space->to_pixfmt);
375
376   return GST_FLOW_OK;
377
378   /* ERRORS */
379 unknown_format:
380   {
381     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
382         ("attempting to convert colorspaces between unknown formats"));
383     return GST_FLOW_NOT_NEGOTIATED;
384   }
385 }
386
387 gboolean
388 gst_ffmpegcolorspace_register (GstPlugin * plugin)
389 {
390   GstCaps *caps;
391
392   /* template caps */
393   caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
394
395   /* build templates */
396   srctempl = gst_pad_template_new ("src",
397       GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
398
399   /* the sink template will do palette handling as well... */
400   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
401
402   return gst_element_register (plugin, "ffmpegcolorspace",
403       GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
404 }