• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * This file:
4  * Copyright (C) 2005 Luca Ognibene <luogni@tin.it>
5  * Copyright (C) 2006 Martin Zlomek <martin.zlomek@itonis.tv>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26 
27 #include <libavcodec/avcodec.h>
28 #include <libavutil/imgutils.h>
29 #include <libavfilter/avfilter.h>
30 #include <libavfilter/buffersrc.h>
31 #include <libavfilter/buffersink.h>
32 
33 #include <gst/gst.h>
34 #include <gst/video/video.h>
35 
36 #include "gstav.h"
37 #include "gstavcodecmap.h"
38 #include "gstavutils.h"
39 
40 
41 /* Properties */
42 
43 #define DEFAULT_MODE            GST_FFMPEGDEINTERLACE_MODE_AUTO
44 
45 enum
46 {
47   PROP_0,
48   PROP_MODE,
49   PROP_LAST
50 };
51 
52 typedef enum
53 {
54   GST_FFMPEGDEINTERLACE_MODE_AUTO,
55   GST_FFMPEGDEINTERLACE_MODE_INTERLACED,
56   GST_FFMPEGDEINTERLACE_MODE_DISABLED
57 } GstFFMpegDeinterlaceMode;
58 
59 #define GST_TYPE_FFMPEGDEINTERLACE_MODES (gst_ffmpegdeinterlace_modes_get_type ())
60 static GType
gst_ffmpegdeinterlace_modes_get_type(void)61 gst_ffmpegdeinterlace_modes_get_type (void)
62 {
63   static GType deinterlace_modes_type = 0;
64 
65   static const GEnumValue modes_types[] = {
66     {GST_FFMPEGDEINTERLACE_MODE_AUTO, "Auto detection", "auto"},
67     {GST_FFMPEGDEINTERLACE_MODE_INTERLACED, "Force deinterlacing",
68         "interlaced"},
69     {GST_FFMPEGDEINTERLACE_MODE_DISABLED, "Run in passthrough mode",
70         "disabled"},
71     {0, NULL, NULL},
72   };
73 
74   if (!deinterlace_modes_type) {
75     deinterlace_modes_type =
76         g_enum_register_static ("GstLibAVDeinterlaceModes", modes_types);
77   }
78   return deinterlace_modes_type;
79 }
80 
81 typedef struct _GstFFMpegDeinterlace
82 {
83   GstElement element;
84 
85   GstPad *sinkpad, *srcpad;
86 
87   gint width, height;
88   gint to_size;
89 
90   GstFFMpegDeinterlaceMode mode;
91 
92   gboolean interlaced;          /* is input interlaced? */
93   gboolean passthrough;
94 
95   gboolean reconfigure;
96   GstFFMpegDeinterlaceMode new_mode;
97 
98   enum AVPixelFormat pixfmt;
99   AVFrame from_frame, to_frame;
100 
101   AVFilterContext *buffersink_ctx;
102   AVFilterContext *buffersrc_ctx;
103   AVFilterGraph *filter_graph;
104   AVFrame *filter_frame;
105   int last_width, last_height;
106   enum AVPixelFormat last_pixfmt;
107 
108 } GstFFMpegDeinterlace;
109 
110 typedef struct _GstFFMpegDeinterlaceClass
111 {
112   GstElementClass parent_class;
113 } GstFFMpegDeinterlaceClass;
114 
115 #define GST_TYPE_FFMPEGDEINTERLACE \
116   (gst_ffmpegdeinterlace_get_type())
117 #define GST_FFMPEGDEINTERLACE(obj) \
118   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGDEINTERLACE,GstFFMpegDeinterlace))
119 #define GST_FFMPEGDEINTERLACE_CLASS(klass) \
120   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGDEINTERLACE,GstFFMpegDeinterlace))
121 #define GST_IS_FFMPEGDEINTERLACE(obj) \
122   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGDEINTERLACE))
123 #define GST_IS_FFMPEGDEINTERLACE_CLASS(klass) \
124   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGDEINTERLACE))
125 
126 GType gst_ffmpegdeinterlace_get_type (void);
127 
128 static void gst_ffmpegdeinterlace_set_property (GObject * self, guint prop_id,
129     const GValue * value, GParamSpec * pspec);
130 static void gst_ffmpegdeinterlace_get_property (GObject * self, guint prop_id,
131     GValue * value, GParamSpec * pspec);
132 
133 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
134     GST_PAD_SRC,
135     GST_PAD_ALWAYS,
136     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
137     );
138 
139 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
140     GST_PAD_SINK,
141     GST_PAD_ALWAYS,
142     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
143     );
144 
145 G_DEFINE_TYPE (GstFFMpegDeinterlace, gst_ffmpegdeinterlace, GST_TYPE_ELEMENT);
146 
147 static GstFlowReturn gst_ffmpegdeinterlace_chain (GstPad * pad,
148     GstObject * parent, GstBuffer * inbuf);
149 
150 static void gst_ffmpegdeinterlace_dispose (GObject * obj);
151 
152 static void
gst_ffmpegdeinterlace_class_init(GstFFMpegDeinterlaceClass * klass)153 gst_ffmpegdeinterlace_class_init (GstFFMpegDeinterlaceClass * klass)
154 {
155   GObjectClass *gobject_class = (GObjectClass *) klass;
156   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
157 
158   gobject_class->set_property = gst_ffmpegdeinterlace_set_property;
159   gobject_class->get_property = gst_ffmpegdeinterlace_get_property;
160 
161   /**
162    * GstFFMpegDeinterlace:mode
163    *
164    * This selects whether the deinterlacing methods should
165    * always be applied or if they should only be applied
166    * on content that has the "interlaced" flag on the caps.
167    *
168    * Since: 0.10.13
169    */
170   g_object_class_install_property (gobject_class, PROP_MODE,
171       g_param_spec_enum ("mode", "Mode", "Deinterlace Mode",
172           GST_TYPE_FFMPEGDEINTERLACE_MODES,
173           DEFAULT_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
174       );
175 
176   gst_element_class_add_static_pad_template (element_class, &src_factory);
177   gst_element_class_add_static_pad_template (element_class, &sink_factory);
178 
179   gst_element_class_set_static_metadata (element_class,
180       "libav Deinterlace element", "Filter/Effect/Video/Deinterlace",
181       "Deinterlace video", "Luca Ognibene <luogni@tin.it>");
182 
183   gobject_class->dispose = gst_ffmpegdeinterlace_dispose;
184 }
185 
186 static void
gst_ffmpegdeinterlace_update_passthrough(GstFFMpegDeinterlace * deinterlace)187 gst_ffmpegdeinterlace_update_passthrough (GstFFMpegDeinterlace * deinterlace)
188 {
189   deinterlace->passthrough =
190       (deinterlace->mode == GST_FFMPEGDEINTERLACE_MODE_DISABLED
191       || (!deinterlace->interlaced
192           && deinterlace->mode != GST_FFMPEGDEINTERLACE_MODE_INTERLACED));
193   GST_DEBUG_OBJECT (deinterlace, "Passthrough: %d", deinterlace->passthrough);
194 }
195 
196 static gboolean
gst_ffmpegdeinterlace_sink_setcaps(GstPad * pad,GstCaps * caps)197 gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstCaps * caps)
198 {
199   GstFFMpegDeinterlace *deinterlace =
200       GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad));
201   GstStructure *structure = gst_caps_get_structure (caps, 0);
202   const gchar *imode;
203   AVCodecContext *ctx;
204   GstCaps *src_caps;
205   gboolean ret;
206 
207   /* FIXME: use GstVideoInfo etc. */
208   if (!gst_structure_get_int (structure, "width", &deinterlace->width))
209     return FALSE;
210   if (!gst_structure_get_int (structure, "height", &deinterlace->height))
211     return FALSE;
212 
213   deinterlace->interlaced = FALSE;
214   imode = gst_structure_get_string (structure, "interlace-mode");
215   if (imode && (!strcmp (imode, "interleaved") || !strcmp (imode, "mixed"))) {
216     deinterlace->interlaced = TRUE;
217   }
218   gst_ffmpegdeinterlace_update_passthrough (deinterlace);
219 
220   ctx = avcodec_alloc_context3 (NULL);
221   ctx->width = deinterlace->width;
222   ctx->height = deinterlace->height;
223   ctx->pix_fmt = AV_PIX_FMT_NB;
224   gst_ffmpeg_caps_with_codectype (AVMEDIA_TYPE_VIDEO, caps, ctx);
225   if (ctx->pix_fmt == AV_PIX_FMT_NB) {
226     gst_ffmpeg_avcodec_close (ctx);
227     av_free (ctx);
228     return FALSE;
229   }
230 
231   deinterlace->pixfmt = ctx->pix_fmt;
232 
233   av_free (ctx);
234 
235   deinterlace->to_size =
236       av_image_get_buffer_size (deinterlace->pixfmt, deinterlace->width,
237       deinterlace->height, 1);
238 
239   src_caps = gst_caps_copy (caps);
240   gst_caps_set_simple (src_caps, "interlace-mode", G_TYPE_STRING,
241       deinterlace->interlaced ? "progressive" : imode, NULL);
242   ret = gst_pad_set_caps (deinterlace->srcpad, src_caps);
243   gst_caps_unref (src_caps);
244 
245   return ret;
246 }
247 
248 static gboolean
gst_ffmpegdeinterlace_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)249 gst_ffmpegdeinterlace_sink_event (GstPad * pad, GstObject * parent,
250     GstEvent * event)
251 {
252   GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
253   gboolean ret = FALSE;
254 
255   switch (GST_EVENT_TYPE (event)) {
256     case GST_EVENT_CAPS:
257     {
258       GstCaps *caps;
259 
260       gst_event_parse_caps (event, &caps);
261       ret = gst_ffmpegdeinterlace_sink_setcaps (pad, caps);
262       gst_event_unref (event);
263       break;
264     }
265     default:
266       ret = gst_pad_push_event (deinterlace->srcpad, event);
267       break;
268   }
269 
270   return ret;
271 }
272 
273 static void
gst_ffmpegdeinterlace_init(GstFFMpegDeinterlace * deinterlace)274 gst_ffmpegdeinterlace_init (GstFFMpegDeinterlace * deinterlace)
275 {
276   deinterlace->sinkpad =
277       gst_pad_new_from_static_template (&sink_factory, "sink");
278   gst_pad_set_event_function (deinterlace->sinkpad,
279       gst_ffmpegdeinterlace_sink_event);
280   gst_pad_set_chain_function (deinterlace->sinkpad,
281       gst_ffmpegdeinterlace_chain);
282   gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->sinkpad);
283 
284   deinterlace->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
285   gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->srcpad);
286 
287   deinterlace->pixfmt = AV_PIX_FMT_NB;
288 
289   deinterlace->interlaced = FALSE;
290   deinterlace->passthrough = FALSE;
291   deinterlace->reconfigure = FALSE;
292   deinterlace->mode = DEFAULT_MODE;
293   deinterlace->new_mode = -1;
294   deinterlace->last_width = -1;
295   deinterlace->last_height = -1;
296   deinterlace->last_pixfmt = AV_PIX_FMT_NONE;
297 }
298 
299 static void
delete_filter_graph(GstFFMpegDeinterlace * deinterlace)300 delete_filter_graph (GstFFMpegDeinterlace * deinterlace)
301 {
302   if (deinterlace->filter_graph) {
303     av_frame_free (&deinterlace->filter_frame);
304     avfilter_graph_free (&deinterlace->filter_graph);
305   }
306 }
307 
308 static void
gst_ffmpegdeinterlace_dispose(GObject * obj)309 gst_ffmpegdeinterlace_dispose (GObject * obj)
310 {
311   GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (obj);
312 
313   delete_filter_graph (deinterlace);
314 
315   G_OBJECT_CLASS (gst_ffmpegdeinterlace_parent_class)->dispose (obj);
316 }
317 
318 static int
init_filter_graph(GstFFMpegDeinterlace * deinterlace,enum AVPixelFormat pixfmt,int width,int height)319 init_filter_graph (GstFFMpegDeinterlace * deinterlace,
320     enum AVPixelFormat pixfmt, int width, int height)
321 {
322   AVFilterInOut *inputs = NULL, *outputs = NULL;
323   char args[512];
324   int res;
325 
326   delete_filter_graph (deinterlace);
327   deinterlace->filter_graph = avfilter_graph_alloc ();
328   snprintf (args, sizeof (args),
329       "buffer=video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0/1[in];"
330       "[in]yadif[out];" "[out]buffersink", width, height, pixfmt);
331   res =
332       avfilter_graph_parse2 (deinterlace->filter_graph, args, &inputs,
333       &outputs);
334   if (res < 0)
335     return res;
336   if (inputs || outputs)
337     return -1;
338   res = avfilter_graph_config (deinterlace->filter_graph, NULL);
339   if (res < 0)
340     return res;
341 
342   deinterlace->buffersrc_ctx =
343       avfilter_graph_get_filter (deinterlace->filter_graph, "Parsed_buffer_0");
344   deinterlace->buffersink_ctx =
345       avfilter_graph_get_filter (deinterlace->filter_graph,
346       "Parsed_buffersink_2");
347   if (!deinterlace->buffersrc_ctx || !deinterlace->buffersink_ctx)
348     return -1;
349   deinterlace->filter_frame = av_frame_alloc ();
350   deinterlace->last_width = width;
351   deinterlace->last_height = height;
352   deinterlace->last_pixfmt = pixfmt;
353 
354   return 0;
355 }
356 
357 static int
process_filter_graph(GstFFMpegDeinterlace * deinterlace,AVFrame * dst,const AVFrame * src,enum AVPixelFormat pixfmt,int width,int height)358 process_filter_graph (GstFFMpegDeinterlace * deinterlace, AVFrame * dst,
359     const AVFrame * src, enum AVPixelFormat pixfmt, int width, int height)
360 {
361   int res;
362 
363   if (!deinterlace->filter_graph || width != deinterlace->last_width ||
364       height != deinterlace->last_height
365       || pixfmt != deinterlace->last_pixfmt) {
366     res = init_filter_graph (deinterlace, pixfmt, width, height);
367     if (res < 0)
368       return res;
369   }
370 
371   memcpy (deinterlace->filter_frame->data, src->data, sizeof (src->data));
372   memcpy (deinterlace->filter_frame->linesize, src->linesize,
373       sizeof (src->linesize));
374   deinterlace->filter_frame->width = width;
375   deinterlace->filter_frame->height = height;
376   deinterlace->filter_frame->format = pixfmt;
377   res =
378       av_buffersrc_add_frame (deinterlace->buffersrc_ctx,
379       deinterlace->filter_frame);
380   if (res < 0)
381     return res;
382   res =
383       av_buffersink_get_frame (deinterlace->buffersink_ctx,
384       deinterlace->filter_frame);
385   if (res < 0)
386     return res;
387   av_image_copy (dst->data, dst->linesize,
388       (const uint8_t **) deinterlace->filter_frame->data,
389       deinterlace->filter_frame->linesize, pixfmt, width, height);
390   av_frame_unref (deinterlace->filter_frame);
391 
392   return 0;
393 }
394 
395 static GstFlowReturn
gst_ffmpegdeinterlace_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)396 gst_ffmpegdeinterlace_chain (GstPad * pad, GstObject * parent,
397     GstBuffer * inbuf)
398 {
399   GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
400   GstBuffer *outbuf = NULL;
401   GstFlowReturn result;
402   GstMapInfo from_map, to_map;
403 
404   GST_OBJECT_LOCK (deinterlace);
405   if (deinterlace->reconfigure) {
406     GstCaps *caps;
407 
408     if ((gint) deinterlace->new_mode != -1)
409       deinterlace->mode = deinterlace->new_mode;
410     deinterlace->new_mode = -1;
411 
412     deinterlace->reconfigure = FALSE;
413     GST_OBJECT_UNLOCK (deinterlace);
414     if ((caps = gst_pad_get_current_caps (deinterlace->srcpad))) {
415       gst_ffmpegdeinterlace_sink_setcaps (deinterlace->sinkpad, caps);
416       gst_caps_unref (caps);
417     }
418   } else {
419     GST_OBJECT_UNLOCK (deinterlace);
420   }
421 
422   if (deinterlace->passthrough)
423     return gst_pad_push (deinterlace->srcpad, inbuf);
424 
425   outbuf = gst_buffer_new_and_alloc (deinterlace->to_size);
426 
427   gst_buffer_map (inbuf, &from_map, GST_MAP_READ);
428   gst_ffmpeg_avpicture_fill (&deinterlace->from_frame, from_map.data,
429       deinterlace->pixfmt, deinterlace->width, deinterlace->height);
430 
431   gst_buffer_map (outbuf, &to_map, GST_MAP_WRITE);
432   gst_ffmpeg_avpicture_fill (&deinterlace->to_frame, to_map.data,
433       deinterlace->pixfmt, deinterlace->width, deinterlace->height);
434 
435   process_filter_graph (deinterlace, &deinterlace->to_frame,
436       &deinterlace->from_frame, deinterlace->pixfmt, deinterlace->width,
437       deinterlace->height);
438   gst_buffer_unmap (outbuf, &to_map);
439   gst_buffer_unmap (inbuf, &from_map);
440 
441   gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
442 
443   result = gst_pad_push (deinterlace->srcpad, outbuf);
444 
445   gst_buffer_unref (inbuf);
446 
447   return result;
448 }
449 
450 gboolean
gst_ffmpegdeinterlace_register(GstPlugin * plugin)451 gst_ffmpegdeinterlace_register (GstPlugin * plugin)
452 {
453   return gst_element_register (plugin, "avdeinterlace",
454       GST_RANK_NONE, GST_TYPE_FFMPEGDEINTERLACE);
455 }
456 
457 static void
gst_ffmpegdeinterlace_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)458 gst_ffmpegdeinterlace_set_property (GObject * object, guint prop_id,
459     const GValue * value, GParamSpec * pspec)
460 {
461   GstFFMpegDeinterlace *self;
462 
463   g_return_if_fail (GST_IS_FFMPEGDEINTERLACE (object));
464   self = GST_FFMPEGDEINTERLACE (object);
465 
466   switch (prop_id) {
467     case PROP_MODE:{
468       gint new_mode;
469 
470       GST_OBJECT_LOCK (self);
471       new_mode = g_value_get_enum (value);
472       if (self->mode != new_mode && gst_pad_has_current_caps (self->srcpad)) {
473         self->reconfigure = TRUE;
474         self->new_mode = new_mode;
475       } else {
476         self->mode = new_mode;
477         gst_ffmpegdeinterlace_update_passthrough (self);
478       }
479       GST_OBJECT_UNLOCK (self);
480       break;
481     }
482     default:
483       G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
484   }
485 
486 }
487 
488 static void
gst_ffmpegdeinterlace_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)489 gst_ffmpegdeinterlace_get_property (GObject * object, guint prop_id,
490     GValue * value, GParamSpec * pspec)
491 {
492   GstFFMpegDeinterlace *self;
493 
494   g_return_if_fail (GST_IS_FFMPEGDEINTERLACE (object));
495   self = GST_FFMPEGDEINTERLACE (object);
496 
497   switch (prop_id) {
498     case PROP_MODE:
499       g_value_set_enum (value, self->mode);
500       break;
501     default:
502       G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
503   }
504 }
505