Cleanups and buffer alloc.
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include <avcodec.h>
28
29 #include "gstffmpegcodecmap.h"
30
31 GST_DEBUG_CATEGORY (ffmpegcolorspace_debug);
32 #define GST_CAT_DEFAULT ffmpegcolorspace_debug
33
34 #define GST_TYPE_FFMPEGCSP \
35   (gst_ffmpegcsp_get_type())
36 #define GST_FFMPEGCSP(obj) \
37   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGCSP,GstFFMpegCsp))
38 #define GST_FFMPEGCSP_CLASS(klass) \
39   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGCSP,GstFFMpegCsp))
40 #define GST_IS_FFMPEGCSP(obj) \
41   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGCSP))
42 #define GST_IS_FFMPEGCSP_CLASS(obj) \
43   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGCSP))
44
45 typedef struct _GstFFMpegCsp GstFFMpegCsp;
46 typedef struct _GstFFMpegCspClass GstFFMpegCspClass;
47
48 struct _GstFFMpegCsp
49 {
50   GstElement element;
51
52   GstPad *sinkpad, *srcpad;
53
54   gint width, height;
55   gfloat fps;
56   enum PixelFormat from_pixfmt, to_pixfmt;
57   AVPicture from_frame, to_frame;
58   AVPaletteControl *palette;
59
60   GstCaps *src_prefered;
61   GstCaps *sink_prefered;
62 };
63
64 struct _GstFFMpegCspClass
65 {
66   GstElementClass parent_class;
67 };
68
69 /* elementfactory information */
70 static GstElementDetails ffmpegcsp_details = {
71   "FFMPEG Colorspace converter",
72   "Filter/Converter/Video",
73   "Converts video from one colorspace to another",
74   "Ronald Bultje <rbultje@ronald.bitfreak.net>",
75 };
76
77
78 /* Stereo signals and args */
79 enum
80 {
81   /* FILL ME */
82   LAST_SIGNAL
83 };
84
85 enum
86 {
87   ARG_0,
88 };
89
90 static GType gst_ffmpegcsp_get_type (void);
91
92 static void gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass);
93 static void gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass);
94 static void gst_ffmpegcsp_init (GstFFMpegCsp * space);
95
96 static void gst_ffmpegcsp_set_property (GObject * object,
97     guint prop_id, const GValue * value, GParamSpec * pspec);
98 static void gst_ffmpegcsp_get_property (GObject * object,
99     guint prop_id, GValue * value, GParamSpec * pspec);
100
101 static GstFlowReturn gst_ffmpegcsp_bufferalloc (GstPad * pad, guint64 offset,
102     guint size, GstCaps * caps, GstBuffer ** buf);
103 static GstFlowReturn gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer);
104 static GstElementStateReturn gst_ffmpegcsp_change_state (GstElement * element);
105
106 static GstPadTemplate *srctempl, *sinktempl;
107 static GstElementClass *parent_class = NULL;
108
109 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
110
111
112 static GstCaps *
113 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
114 {
115   int i;
116   GstStructure *structure;
117   GstCaps *rgbcaps;
118
119   caps = gst_caps_make_writable (caps);
120
121   for (i = 0; i < gst_caps_get_size (caps); i++) {
122     structure = gst_caps_get_structure (caps, i);
123
124     gst_structure_set_name (structure, "video/x-raw-yuv");
125     gst_structure_remove_field (structure, "format");
126     gst_structure_remove_field (structure, "endianness");
127     gst_structure_remove_field (structure, "depth");
128     gst_structure_remove_field (structure, "bpp");
129     gst_structure_remove_field (structure, "red_mask");
130     gst_structure_remove_field (structure, "green_mask");
131     gst_structure_remove_field (structure, "blue_mask");
132     gst_structure_remove_field (structure, "alpha_mask");
133   }
134
135   gst_caps_do_simplify (caps);
136   rgbcaps = gst_caps_copy (caps);
137
138   for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
139     structure = gst_caps_get_structure (rgbcaps, i);
140
141     gst_structure_set_name (structure, "video/x-raw-rgb");
142   }
143
144   gst_caps_append (caps, rgbcaps);
145
146   return caps;
147 }
148
149 static GstCaps *
150 gst_ffmpegcsp_getcaps (GstPad * pad)
151 {
152   GstFFMpegCsp *space;
153   GstCaps *othercaps;
154   GstCaps *caps;
155   GstPad *otherpad;
156
157   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
158
159   otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
160   /* we can do whatever the peer can */
161   othercaps = gst_pad_peer_get_caps (otherpad);
162   if (othercaps != NULL) {
163     /* without the format info */
164     othercaps = gst_ffmpegcsp_caps_remove_format_info (othercaps);
165     /* and filtered against our padtemplate */
166     caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
167     gst_caps_unref (othercaps);
168   } else {
169     caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
170   }
171
172   return caps;
173 }
174
175 static gboolean
176 gst_ffmpegcsp_configure_context (GstPad * pad, const GstCaps * caps, gint width,
177     gint height)
178 {
179   AVCodecContext *ctx;
180   GstFFMpegCsp *space;
181
182   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
183
184   /* loop over all possibilities and select the first one we can convert and
185    * is accepted by the peer */
186   ctx = avcodec_alloc_context ();
187
188   ctx->width = width;
189   ctx->height = height;
190   ctx->pix_fmt = PIX_FMT_NB;
191   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
192   if (ctx->pix_fmt == PIX_FMT_NB) {
193     av_free (ctx);
194
195     /* we disable ourself here */
196     if (pad == space->srcpad) {
197       space->to_pixfmt = PIX_FMT_NB;
198     } else {
199       space->from_pixfmt = PIX_FMT_NB;
200     }
201
202     return FALSE;
203   } else {
204     if (pad == space->srcpad) {
205       space->to_pixfmt = ctx->pix_fmt;
206     } else {
207       space->from_pixfmt = ctx->pix_fmt;
208
209       /* palette */
210       if (space->palette)
211         av_free (space->palette);
212       space->palette = ctx->palctrl;
213     }
214     av_free (ctx);
215   }
216   return TRUE;
217 }
218
219 /* configureing the caps on a pad means that we should check if we
220  * can get a fic format for that caps. Then we need to figure out
221  * how we can convert that to the peer format */
222 static gboolean
223 gst_ffmpegcsp_setcaps (GstPad * pad, GstCaps * caps)
224 {
225   GstStructure *structure;
226   GstFFMpegCsp *space;
227   GstPad *otherpeer;
228   GstPad *otherpad;
229   int height, width;
230   double framerate;
231   const GValue *par = NULL;
232   GstCaps **other_prefered, **prefered;
233
234   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
235
236   GST_DEBUG_OBJECT (space, "setcaps on %s:%s with caps %" GST_PTR_FORMAT,
237       GST_DEBUG_PAD_NAME (pad), caps);
238
239   otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
240   prefered =
241       (pad == space->srcpad) ? &space->src_prefered : &space->sink_prefered;
242   other_prefered =
243       (pad == space->srcpad) ? &space->sink_prefered : &space->src_prefered;
244
245   structure = gst_caps_get_structure (caps, 0);
246   gst_structure_get_int (structure, "width", &width);
247   gst_structure_get_int (structure, "height", &height);
248   gst_structure_get_double (structure, "framerate", &framerate);
249   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
250
251   if (!gst_ffmpegcsp_configure_context (pad, caps, width, height))
252     goto configure_error_source;
253
254   gst_caps_replace (prefered, caps);
255
256   otherpeer = gst_pad_get_peer (otherpad);
257   if (otherpeer) {
258     /* check passthrough */
259     if (gst_pad_accept_caps (otherpeer, caps)) {
260       if (!gst_ffmpegcsp_configure_context (otherpad, caps, width, height))
261         goto configure_error_target;
262       gst_caps_replace (other_prefered, caps);
263     } else {
264       GstCaps *othercaps;
265
266       /* set the size on the otherpad */
267       othercaps = gst_pad_get_caps (otherpeer);
268       if (othercaps) {
269         GstCaps *targetcaps = gst_caps_copy_nth (othercaps, 0);
270
271         gst_caps_unref (othercaps);
272
273         gst_caps_set_simple (targetcaps,
274             "width", G_TYPE_INT, width,
275             "height", G_TYPE_INT, height,
276             "framerate", G_TYPE_DOUBLE, framerate, NULL);
277
278         if (par) {
279           gst_caps_set_simple (targetcaps,
280               "pixel-aspect-ratio", GST_TYPE_FRACTION,
281               gst_value_get_fraction_numerator (par),
282               gst_value_get_fraction_denominator (par), NULL);
283         }
284         if (!gst_ffmpegcsp_configure_context (otherpad, targetcaps, width,
285                 height)) {
286           gst_caps_unref (targetcaps);
287           goto configure_error_target;
288         }
289         gst_caps_replace (other_prefered, targetcaps);
290         gst_caps_unref (targetcaps);
291       }
292     }
293     gst_object_unref (GST_OBJECT (otherpeer));
294   }
295
296   space->width = width;
297   space->height = height;
298
299   return TRUE;
300
301 configure_error_source:
302   {
303     GST_DEBUG ("could not configure context for source");
304     return FALSE;
305   }
306 configure_error_target:
307   {
308     gst_object_unref (GST_OBJECT (otherpeer));
309     GST_DEBUG ("could not configure context for target");
310     return FALSE;
311   }
312 }
313
314 static GType
315 gst_ffmpegcsp_get_type (void)
316 {
317   static GType ffmpegcsp_type = 0;
318
319   if (!ffmpegcsp_type) {
320     static const GTypeInfo ffmpegcsp_info = {
321       sizeof (GstFFMpegCspClass),
322       (GBaseInitFunc) gst_ffmpegcsp_base_init,
323       NULL,
324       (GClassInitFunc) gst_ffmpegcsp_class_init,
325       NULL,
326       NULL,
327       sizeof (GstFFMpegCsp),
328       0,
329       (GInstanceInitFunc) gst_ffmpegcsp_init,
330     };
331
332     ffmpegcsp_type = g_type_register_static (GST_TYPE_ELEMENT,
333         "GstFFMpegColorspace", &ffmpegcsp_info, 0);
334   }
335
336   return ffmpegcsp_type;
337 }
338
339 static void
340 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
341 {
342   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
343
344   gst_element_class_add_pad_template (element_class, srctempl);
345   gst_element_class_add_pad_template (element_class, sinktempl);
346   gst_element_class_set_details (element_class, &ffmpegcsp_details);
347 }
348
349 static void
350 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
351 {
352   GObjectClass *gobject_class;
353   GstElementClass *gstelement_class;
354
355   gobject_class = (GObjectClass *) klass;
356   gstelement_class = (GstElementClass *) klass;
357
358   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
359
360   gobject_class->set_property = gst_ffmpegcsp_set_property;
361   gobject_class->get_property = gst_ffmpegcsp_get_property;
362
363   gstelement_class->change_state = gst_ffmpegcsp_change_state;
364
365   GST_DEBUG_CATEGORY_INIT (ffmpegcolorspace_debug, "ffmpegcolorspace", 0,
366       "FFMPEG-based colorspace converter");
367 }
368
369 static void
370 gst_ffmpegcsp_init (GstFFMpegCsp * space)
371 {
372   space->sinkpad = gst_pad_new_from_template (sinktempl, "sink");
373   gst_pad_set_getcaps_function (space->sinkpad, gst_ffmpegcsp_getcaps);
374   gst_pad_set_setcaps_function (space->sinkpad, gst_ffmpegcsp_setcaps);
375   gst_pad_set_chain_function (space->sinkpad, gst_ffmpegcsp_chain);
376   gst_pad_set_bufferalloc_function (space->sinkpad, gst_ffmpegcsp_bufferalloc);
377   gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
378
379   space->srcpad = gst_pad_new_from_template (srctempl, "src");
380   gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
381   gst_pad_set_getcaps_function (space->srcpad, gst_ffmpegcsp_getcaps);
382   gst_pad_set_setcaps_function (space->srcpad, gst_ffmpegcsp_setcaps);
383
384   space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
385   space->palette = NULL;
386 }
387
388 static GstFlowReturn
389 gst_ffmpegcsp_bufferalloc (GstPad * pad, guint64 offset, guint size,
390     GstCaps * caps, GstBuffer ** buf)
391 {
392   GstFlowReturn ret;
393   GstFFMpegCsp *space;
394
395   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
396
397   if ((space->from_pixfmt == space->to_pixfmt) &&
398       space->from_pixfmt != PIX_FMT_NB) {
399     ret = gst_pad_alloc_buffer (space->srcpad, offset, size, caps, buf);
400   } else {
401     *buf = NULL;
402     ret = GST_FLOW_OK;
403   }
404   return ret;
405 }
406
407 static GstFlowReturn
408 gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer)
409 {
410   GstFFMpegCsp *space;
411   GstFlowReturn res;
412   GstBuffer *outbuf = NULL;
413
414   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
415
416   GST_DEBUG ("from %d -> to %d", space->from_pixfmt, space->to_pixfmt);
417   if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB)
418     goto unkown_format;
419
420   if (space->from_pixfmt == space->to_pixfmt) {
421     GST_DEBUG ("passthrough conversion");
422     /* use input as output buffer */
423     outbuf = buffer;
424   } else {
425     /* get size of our suggested output format */
426     guint size =
427         avpicture_get_size (space->to_pixfmt, space->width, space->height);
428
429     /* get buffer in prefered format, setcaps will be called when it is different */
430     res = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE, size,
431         space->src_prefered, &outbuf);
432     if (res != GST_FLOW_OK)
433       goto no_buffer;
434
435     /* fill from from with source data */
436     gst_ffmpegcsp_avpicture_fill (&space->from_frame,
437         GST_BUFFER_DATA (buffer),
438         space->from_pixfmt, space->width, space->height);
439
440     /* fill optional palette */
441     if (space->palette)
442       space->from_frame.data[1] = (uint8_t *) space->palette;
443
444     /* fill target frame */
445     gst_ffmpegcsp_avpicture_fill (&space->to_frame,
446         GST_BUFFER_DATA (outbuf),
447         space->to_pixfmt, space->width, space->height);
448
449     /* and convert */
450     img_convert (&space->to_frame, space->to_pixfmt,
451         &space->from_frame, space->from_pixfmt, space->width, space->height);
452
453     /* copy timestamps */
454     GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
455     GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buffer);
456     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
457     GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buffer);
458
459     /* we don't need source anymore */
460     gst_buffer_unref (buffer);
461   }
462
463   res = gst_pad_push (space->srcpad, outbuf);
464
465   return res;
466
467   /* ERRORS */
468 unkown_format:
469   {
470     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
471         ("attempting to convert colorspaces between unknown formats"));
472     gst_buffer_unref (buffer);
473     return GST_FLOW_NOT_NEGOTIATED;
474   }
475 no_buffer:
476   {
477     gst_buffer_unref (buffer);
478     return res;
479   }
480 }
481
482 static GstElementStateReturn
483 gst_ffmpegcsp_change_state (GstElement * element)
484 {
485   GstFFMpegCsp *space;
486   GstElementStateReturn ret;
487   gint transition;
488
489   space = GST_FFMPEGCSP (element);
490   transition = GST_STATE_TRANSITION (element);
491
492   switch (transition) {
493     default:
494       break;
495   }
496
497   ret = parent_class->change_state (element);
498
499   switch (transition) {
500     case GST_STATE_PAUSED_TO_READY:
501       if (space->palette)
502         av_free (space->palette);
503       space->palette = NULL;
504       break;
505     default:
506       break;
507   }
508
509   return ret;
510 }
511
512 static void
513 gst_ffmpegcsp_set_property (GObject * object,
514     guint prop_id, const GValue * value, GParamSpec * pspec)
515 {
516   GstFFMpegCsp *space;
517
518   space = GST_FFMPEGCSP (object);
519
520   switch (prop_id) {
521     default:
522       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523       break;
524   }
525 }
526
527 static void
528 gst_ffmpegcsp_get_property (GObject * object,
529     guint prop_id, GValue * value, GParamSpec * pspec)
530 {
531   GstFFMpegCsp *space;
532
533   space = GST_FFMPEGCSP (object);
534
535   switch (prop_id) {
536     default:
537       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
538       break;
539   }
540 }
541
542 gboolean
543 gst_ffmpegcolorspace_register (GstPlugin * plugin)
544 {
545   GstCaps *caps;
546
547   /* template caps */
548   caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
549
550   /* build templates */
551   srctempl = gst_pad_template_new ("src",
552       GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
553
554   /* the sink template will do palette handling as well... */
555   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
556
557   return gst_element_register (plugin, "ffmpegcolorspace",
558       GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
559 }