gst/ffmpegcolorspace/gstffmpegcolorspace.c: Well, unreffing a buffer right before...
[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_chain (GstPad * pad, GstBuffer * buffer);
102 static GstElementStateReturn gst_ffmpegcsp_change_state (GstElement * element);
103
104 static GstPadTemplate *srctempl, *sinktempl;
105 static GstElementClass *parent_class = NULL;
106
107 /*static guint gst_ffmpegcsp_signals[LAST_SIGNAL] = { 0 }; */
108
109
110 static GstCaps *
111 gst_ffmpegcsp_caps_remove_format_info (GstCaps * caps)
112 {
113   int i;
114   GstStructure *structure;
115   GstCaps *rgbcaps;
116
117   caps = gst_caps_make_writable (caps);
118
119   for (i = 0; i < gst_caps_get_size (caps); i++) {
120     structure = gst_caps_get_structure (caps, i);
121
122     gst_structure_set_name (structure, "video/x-raw-yuv");
123     gst_structure_remove_field (structure, "format");
124     gst_structure_remove_field (structure, "endianness");
125     gst_structure_remove_field (structure, "depth");
126     gst_structure_remove_field (structure, "bpp");
127     gst_structure_remove_field (structure, "red_mask");
128     gst_structure_remove_field (structure, "green_mask");
129     gst_structure_remove_field (structure, "blue_mask");
130     gst_structure_remove_field (structure, "alpha_mask");
131   }
132
133   gst_caps_do_simplify (caps);
134   rgbcaps = gst_caps_copy (caps);
135
136   for (i = 0; i < gst_caps_get_size (rgbcaps); i++) {
137     structure = gst_caps_get_structure (rgbcaps, i);
138
139     gst_structure_set_name (structure, "video/x-raw-rgb");
140   }
141
142   gst_caps_append (caps, rgbcaps);
143
144   return caps;
145 }
146
147 static GstCaps *
148 gst_ffmpegcsp_getcaps (GstPad * pad)
149 {
150   GstFFMpegCsp *space;
151   GstCaps *othercaps;
152   GstCaps *caps;
153   GstPad *otherpad;
154
155   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
156
157   otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
158   /* we can do whatever the peer can */
159   othercaps = gst_pad_peer_get_caps (otherpad);
160   if (othercaps != NULL) {
161     /* without the format info */
162     othercaps = gst_ffmpegcsp_caps_remove_format_info (othercaps);
163     /* and filtered against our padtemplate */
164     caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
165     gst_caps_unref (othercaps);
166   } else {
167     caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
168   }
169
170   return caps;
171 }
172
173 static gboolean
174 gst_ffmpegcsp_configure_context (GstPad * pad, const GstCaps * caps, gint width,
175     gint height)
176 {
177   AVCodecContext *ctx;
178   GstFFMpegCsp *space;
179
180   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
181
182   /* loop over all possibilities and select the first one we can convert and
183    * is accepted by the peer */
184   ctx = avcodec_alloc_context ();
185
186   ctx->width = width;
187   ctx->height = height;
188   ctx->pix_fmt = PIX_FMT_NB;
189   gst_ffmpegcsp_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
190   if (ctx->pix_fmt == PIX_FMT_NB) {
191     av_free (ctx);
192
193     /* we disable ourself here */
194     if (pad == space->srcpad) {
195       space->to_pixfmt = PIX_FMT_NB;
196     } else {
197       space->from_pixfmt = PIX_FMT_NB;
198     }
199
200     return FALSE;
201   } else {
202     if (pad == space->srcpad) {
203       space->to_pixfmt = ctx->pix_fmt;
204     } else {
205       space->from_pixfmt = ctx->pix_fmt;
206
207       /* palette */
208       if (space->palette)
209         av_free (space->palette);
210       space->palette = ctx->palctrl;
211     }
212     av_free (ctx);
213   }
214   return TRUE;
215 }
216
217 /* configureing the caps on a pad means that we should check if we
218  * can get a fic format for that caps. Then we need to figure out
219  * how we can convert that to the peer format */
220 static gboolean
221 gst_ffmpegcsp_setcaps (GstPad * pad, GstCaps * caps)
222 {
223   GstStructure *structure;
224   GstFFMpegCsp *space;
225   GstPad *otherpeer;
226   GstPad *otherpad;
227   int height, width;
228   double framerate;
229   const GValue *par = NULL;
230   GstCaps **other_prefered, **prefered;
231
232   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
233
234   GST_DEBUG_OBJECT (space, "setcaps on %s:%s with caps %" GST_PTR_FORMAT,
235       GST_DEBUG_PAD_NAME (pad), caps);
236
237   otherpad = (pad == space->srcpad) ? space->sinkpad : space->srcpad;
238   prefered =
239       (pad == space->srcpad) ? &space->src_prefered : &space->sink_prefered;
240   other_prefered =
241       (pad == space->srcpad) ? &space->sink_prefered : &space->src_prefered;
242
243   structure = gst_caps_get_structure (caps, 0);
244   gst_structure_get_int (structure, "width", &width);
245   gst_structure_get_int (structure, "height", &height);
246   gst_structure_get_double (structure, "framerate", &framerate);
247   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
248
249   if (!gst_ffmpegcsp_configure_context (pad, caps, width, height))
250     goto configure_error;
251
252   *prefered = caps;
253
254   otherpeer = gst_pad_get_peer (otherpad);
255   if (otherpeer) {
256     /* check passthrough */
257     //if (gst_pad_accept_caps (otherpeer, caps)) {
258     if (FALSE) {
259       *other_prefered = gst_caps_ref (caps);
260     } else {
261       GstCaps *othercaps;
262
263       /* set the size on the otherpad */
264       othercaps = gst_pad_get_caps (otherpeer);
265       if (othercaps) {
266         GstCaps *targetcaps = gst_caps_copy_nth (othercaps, 0);
267
268         gst_caps_unref (othercaps);
269
270         gst_caps_set_simple (targetcaps,
271             "width", G_TYPE_INT, width,
272             "height", G_TYPE_INT, height,
273             "framerate", G_TYPE_DOUBLE, framerate, NULL);
274
275         if (par) {
276           gst_caps_set_simple (targetcaps,
277               "pixel-aspect-ratio", GST_TYPE_FRACTION,
278               gst_value_get_fraction_numerator (par),
279               gst_value_get_fraction_denominator (par), NULL);
280         }
281         *other_prefered = targetcaps;
282       }
283     }
284     gst_object_unref (GST_OBJECT (otherpeer));
285   }
286
287   space->width = width;
288   space->height = height;
289
290   return TRUE;
291
292 configure_error:
293   {
294     GST_DEBUG ("could not configure context");
295     return FALSE;
296   }
297 }
298
299 static GType
300 gst_ffmpegcsp_get_type (void)
301 {
302   static GType ffmpegcsp_type = 0;
303
304   if (!ffmpegcsp_type) {
305     static const GTypeInfo ffmpegcsp_info = {
306       sizeof (GstFFMpegCspClass),
307       (GBaseInitFunc) gst_ffmpegcsp_base_init,
308       NULL,
309       (GClassInitFunc) gst_ffmpegcsp_class_init,
310       NULL,
311       NULL,
312       sizeof (GstFFMpegCsp),
313       0,
314       (GInstanceInitFunc) gst_ffmpegcsp_init,
315     };
316
317     ffmpegcsp_type = g_type_register_static (GST_TYPE_ELEMENT,
318         "GstFFMpegColorspace", &ffmpegcsp_info, 0);
319   }
320
321   return ffmpegcsp_type;
322 }
323
324 static void
325 gst_ffmpegcsp_base_init (GstFFMpegCspClass * klass)
326 {
327   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
328
329   gst_element_class_add_pad_template (element_class, srctempl);
330   gst_element_class_add_pad_template (element_class, sinktempl);
331   gst_element_class_set_details (element_class, &ffmpegcsp_details);
332 }
333
334 static void
335 gst_ffmpegcsp_class_init (GstFFMpegCspClass * klass)
336 {
337   GObjectClass *gobject_class;
338   GstElementClass *gstelement_class;
339
340   gobject_class = (GObjectClass *) klass;
341   gstelement_class = (GstElementClass *) klass;
342
343   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
344
345   gobject_class->set_property = gst_ffmpegcsp_set_property;
346   gobject_class->get_property = gst_ffmpegcsp_get_property;
347
348   gstelement_class->change_state = gst_ffmpegcsp_change_state;
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->sinkpad = gst_pad_new_from_template (sinktempl, "sink");
358   gst_pad_set_getcaps_function (space->sinkpad, gst_ffmpegcsp_getcaps);
359   gst_pad_set_setcaps_function (space->sinkpad, gst_ffmpegcsp_setcaps);
360   gst_pad_set_chain_function (space->sinkpad, gst_ffmpegcsp_chain);
361   gst_element_add_pad (GST_ELEMENT (space), space->sinkpad);
362
363   space->srcpad = gst_pad_new_from_template (srctempl, "src");
364   gst_element_add_pad (GST_ELEMENT (space), space->srcpad);
365   gst_pad_set_getcaps_function (space->srcpad, gst_ffmpegcsp_getcaps);
366   gst_pad_set_setcaps_function (space->srcpad, gst_ffmpegcsp_setcaps);
367
368   space->from_pixfmt = space->to_pixfmt = PIX_FMT_NB;
369   space->palette = NULL;
370 }
371
372 static GstFlowReturn
373 gst_ffmpegcsp_chain (GstPad * pad, GstBuffer * buffer)
374 {
375   GstBuffer *inbuf = GST_BUFFER (buffer);
376   GstFFMpegCsp *space;
377   GstBuffer *outbuf = NULL;
378
379   //GstCaps *outcaps;
380
381   space = GST_FFMPEGCSP (GST_PAD_PARENT (pad));
382
383   if (!GST_PAD_IS_USABLE (space->srcpad)) {
384     gst_buffer_unref (inbuf);
385     return GST_FLOW_ERROR;
386   }
387
388   /* asume passthrough */
389   guint size =
390       avpicture_get_size (space->from_pixfmt, space->width, space->height);
391
392   outbuf = gst_pad_alloc_buffer (space->srcpad, GST_BUFFER_OFFSET_NONE, size,
393       space->src_prefered);
394   if (outbuf == NULL) {
395     return GST_FLOW_ERROR;
396   }
397
398   if (space->from_pixfmt == PIX_FMT_NB || space->to_pixfmt == PIX_FMT_NB) {
399     GST_ELEMENT_ERROR (space, CORE, NOT_IMPLEMENTED, (NULL),
400         ("attempting to convert colorspaces between unknown formats"));
401     gst_buffer_unref (inbuf);
402     return GST_FLOW_NOT_NEGOTIATED;
403   }
404
405   if (space->from_pixfmt == space->to_pixfmt) {
406     GST_DEBUG ("passthrough conversion %" GST_PTR_FORMAT,
407         GST_PAD_CAPS (space->srcpad));
408     outbuf = inbuf;
409   } else {
410     /* convert */
411     gst_ffmpegcsp_avpicture_fill (&space->from_frame,
412         GST_BUFFER_DATA (inbuf),
413         space->from_pixfmt, space->width, space->height);
414     if (space->palette)
415       space->from_frame.data[1] = (uint8_t *) space->palette;
416     gst_ffmpegcsp_avpicture_fill (&space->to_frame,
417         GST_BUFFER_DATA (outbuf),
418         space->to_pixfmt, space->width, space->height);
419     img_convert (&space->to_frame, space->to_pixfmt,
420         &space->from_frame, space->from_pixfmt, space->width, space->height);
421
422     GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (inbuf);
423     GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (inbuf);
424     gst_buffer_unref (inbuf);
425   }
426
427   return gst_pad_push (space->srcpad, outbuf);
428 }
429
430 static GstElementStateReturn
431 gst_ffmpegcsp_change_state (GstElement * element)
432 {
433   GstFFMpegCsp *space;
434
435   space = GST_FFMPEGCSP (element);
436
437   switch (GST_STATE_TRANSITION (element)) {
438     case GST_STATE_PAUSED_TO_READY:
439       if (space->palette)
440         av_free (space->palette);
441       space->palette = NULL;
442       break;
443   }
444
445   if (parent_class->change_state)
446     return parent_class->change_state (element);
447
448   return GST_STATE_SUCCESS;
449 }
450
451 static void
452 gst_ffmpegcsp_set_property (GObject * object,
453     guint prop_id, const GValue * value, GParamSpec * pspec)
454 {
455   GstFFMpegCsp *space;
456
457   /* it's not null if we got it, but it might not be ours */
458   g_return_if_fail (GST_IS_FFMPEGCSP (object));
459   space = GST_FFMPEGCSP (object);
460
461   switch (prop_id) {
462     default:
463       break;
464   }
465 }
466
467 static void
468 gst_ffmpegcsp_get_property (GObject * object,
469     guint prop_id, GValue * value, GParamSpec * pspec)
470 {
471   GstFFMpegCsp *space;
472
473   /* it's not null if we got it, but it might not be ours */
474   g_return_if_fail (GST_IS_FFMPEGCSP (object));
475   space = GST_FFMPEGCSP (object);
476
477   switch (prop_id) {
478     default:
479       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480       break;
481   }
482 }
483
484 gboolean
485 gst_ffmpegcolorspace_register (GstPlugin * plugin)
486 {
487   GstCaps *caps;
488
489   /* template caps */
490   caps = gst_ffmpegcsp_codectype_to_caps (CODEC_TYPE_VIDEO, NULL);
491
492   /* build templates */
493   srctempl = gst_pad_template_new ("src",
494       GST_PAD_SRC, GST_PAD_ALWAYS, gst_caps_copy (caps));
495
496   /* the sink template will do palette handling as well... */
497   sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps);
498
499   return gst_element_register (plugin, "ffmpegcolorspace",
500       GST_RANK_NONE, GST_TYPE_FFMPEGCSP);
501 }