2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) 2005 Luca Ognibene <luogni@tin.it>
5 * Copyright (C) 2006 Martin Zlomek <martin.zlomek@itonis.tv>
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.
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.
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.
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>
34 #include <gst/video/video.h>
37 #include "gstavcodecmap.h"
38 #include "gstavutils.h"
43 #define DEFAULT_MODE GST_FFMPEGDEINTERLACE_MODE_AUTO
54 GST_FFMPEGDEINTERLACE_MODE_AUTO,
55 GST_FFMPEGDEINTERLACE_MODE_INTERLACED,
56 GST_FFMPEGDEINTERLACE_MODE_DISABLED
57 } GstFFMpegDeinterlaceMode;
59 #define GST_TYPE_FFMPEGDEINTERLACE_MODES (gst_ffmpegdeinterlace_modes_get_type ())
61 gst_ffmpegdeinterlace_modes_get_type (void)
63 static GType deinterlace_modes_type = 0;
65 static const GEnumValue modes_types[] = {
66 {GST_FFMPEGDEINTERLACE_MODE_AUTO, "Auto detection", "auto"},
67 {GST_FFMPEGDEINTERLACE_MODE_INTERLACED, "Force deinterlacing",
69 {GST_FFMPEGDEINTERLACE_MODE_DISABLED, "Run in passthrough mode",
74 if (!deinterlace_modes_type) {
75 deinterlace_modes_type =
76 g_enum_register_static ("GstLibAVDeinterlaceModes", modes_types);
78 return deinterlace_modes_type;
81 typedef struct _GstFFMpegDeinterlace
85 GstPad *sinkpad, *srcpad;
90 GstFFMpegDeinterlaceMode mode;
92 gboolean interlaced; /* is input interlaced? */
96 GstFFMpegDeinterlaceMode new_mode;
98 enum AVPixelFormat pixfmt;
99 AVFrame from_frame, to_frame;
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;
108 } GstFFMpegDeinterlace;
110 typedef struct _GstFFMpegDeinterlaceClass
112 GstElementClass parent_class;
113 } GstFFMpegDeinterlaceClass;
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))
126 GType gst_ffmpegdeinterlace_get_type (void);
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);
133 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
136 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
139 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
142 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
145 G_DEFINE_TYPE (GstFFMpegDeinterlace, gst_ffmpegdeinterlace, GST_TYPE_ELEMENT);
147 static GstFlowReturn gst_ffmpegdeinterlace_chain (GstPad * pad,
148 GstObject * parent, GstBuffer * inbuf);
150 static void gst_ffmpegdeinterlace_dispose (GObject * obj);
153 gst_ffmpegdeinterlace_class_init (GstFFMpegDeinterlaceClass * klass)
155 GObjectClass *gobject_class = (GObjectClass *) klass;
156 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
158 gobject_class->set_property = gst_ffmpegdeinterlace_set_property;
159 gobject_class->get_property = gst_ffmpegdeinterlace_get_property;
162 * GstFFMpegDeinterlace:mode
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.
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)
176 gst_element_class_add_static_pad_template (element_class, &src_factory);
177 gst_element_class_add_static_pad_template (element_class, &sink_factory);
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>");
183 gobject_class->dispose = gst_ffmpegdeinterlace_dispose;
185 gst_type_mark_as_plugin_api (GST_TYPE_FFMPEGDEINTERLACE_MODES, 0);
189 gst_ffmpegdeinterlace_update_passthrough (GstFFMpegDeinterlace * deinterlace)
191 deinterlace->passthrough =
192 (deinterlace->mode == GST_FFMPEGDEINTERLACE_MODE_DISABLED
193 || (!deinterlace->interlaced
194 && deinterlace->mode != GST_FFMPEGDEINTERLACE_MODE_INTERLACED));
195 GST_DEBUG_OBJECT (deinterlace, "Passthrough: %d", deinterlace->passthrough);
199 gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstObject * parent,
202 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
203 GstStructure *structure = gst_caps_get_structure (caps, 0);
209 /* FIXME: use GstVideoInfo etc. */
210 if (!gst_structure_get_int (structure, "width", &deinterlace->width))
212 if (!gst_structure_get_int (structure, "height", &deinterlace->height))
215 deinterlace->interlaced = FALSE;
216 imode = gst_structure_get_string (structure, "interlace-mode");
217 if (imode && (!strcmp (imode, "interleaved") || !strcmp (imode, "mixed"))) {
218 deinterlace->interlaced = TRUE;
220 gst_ffmpegdeinterlace_update_passthrough (deinterlace);
222 ctx = avcodec_alloc_context3 (NULL);
223 ctx->width = deinterlace->width;
224 ctx->height = deinterlace->height;
225 ctx->pix_fmt = AV_PIX_FMT_NB;
226 gst_ffmpeg_caps_with_codectype (AVMEDIA_TYPE_VIDEO, caps, ctx);
227 if (ctx->pix_fmt == AV_PIX_FMT_NB) {
228 gst_ffmpeg_avcodec_close (ctx);
233 deinterlace->pixfmt = ctx->pix_fmt;
237 deinterlace->to_size =
238 av_image_get_buffer_size (deinterlace->pixfmt, deinterlace->width,
239 deinterlace->height, 1);
241 src_caps = gst_caps_copy (caps);
242 gst_caps_set_simple (src_caps, "interlace-mode", G_TYPE_STRING,
243 deinterlace->interlaced ? "progressive" : imode, NULL);
244 ret = gst_pad_set_caps (deinterlace->srcpad, src_caps);
245 gst_caps_unref (src_caps);
251 gst_ffmpegdeinterlace_sink_event (GstPad * pad, GstObject * parent,
254 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
255 gboolean ret = FALSE;
257 switch (GST_EVENT_TYPE (event)) {
262 gst_event_parse_caps (event, &caps);
263 ret = gst_ffmpegdeinterlace_sink_setcaps (pad, parent, caps);
264 gst_event_unref (event);
268 ret = gst_pad_push_event (deinterlace->srcpad, event);
276 gst_ffmpegdeinterlace_init (GstFFMpegDeinterlace * deinterlace)
278 deinterlace->sinkpad =
279 gst_pad_new_from_static_template (&sink_factory, "sink");
280 gst_pad_set_event_function (deinterlace->sinkpad,
281 gst_ffmpegdeinterlace_sink_event);
282 gst_pad_set_chain_function (deinterlace->sinkpad,
283 gst_ffmpegdeinterlace_chain);
284 gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->sinkpad);
286 deinterlace->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
287 gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->srcpad);
289 deinterlace->pixfmt = AV_PIX_FMT_NB;
291 deinterlace->interlaced = FALSE;
292 deinterlace->passthrough = FALSE;
293 deinterlace->reconfigure = FALSE;
294 deinterlace->mode = DEFAULT_MODE;
295 deinterlace->new_mode = -1;
296 deinterlace->last_width = -1;
297 deinterlace->last_height = -1;
298 deinterlace->last_pixfmt = AV_PIX_FMT_NONE;
302 delete_filter_graph (GstFFMpegDeinterlace * deinterlace)
304 if (deinterlace->filter_graph) {
305 av_frame_free (&deinterlace->filter_frame);
306 avfilter_graph_free (&deinterlace->filter_graph);
311 gst_ffmpegdeinterlace_dispose (GObject * obj)
313 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (obj);
315 delete_filter_graph (deinterlace);
317 G_OBJECT_CLASS (gst_ffmpegdeinterlace_parent_class)->dispose (obj);
321 init_filter_graph (GstFFMpegDeinterlace * deinterlace,
322 enum AVPixelFormat pixfmt, int width, int height)
324 AVFilterInOut *inputs = NULL, *outputs = NULL;
328 delete_filter_graph (deinterlace);
329 deinterlace->filter_graph = avfilter_graph_alloc ();
330 snprintf (args, sizeof (args),
331 "buffer=video_size=%dx%d:pix_fmt=%d:time_base=1/1:pixel_aspect=0/1[in];"
332 "[in]yadif[out];" "[out]buffersink", width, height, pixfmt);
334 avfilter_graph_parse2 (deinterlace->filter_graph, args, &inputs,
338 if (inputs || outputs)
340 res = avfilter_graph_config (deinterlace->filter_graph, NULL);
344 deinterlace->buffersrc_ctx =
345 avfilter_graph_get_filter (deinterlace->filter_graph, "Parsed_buffer_0");
346 deinterlace->buffersink_ctx =
347 avfilter_graph_get_filter (deinterlace->filter_graph,
348 "Parsed_buffersink_2");
349 if (!deinterlace->buffersrc_ctx || !deinterlace->buffersink_ctx)
351 deinterlace->filter_frame = av_frame_alloc ();
352 deinterlace->last_width = width;
353 deinterlace->last_height = height;
354 deinterlace->last_pixfmt = pixfmt;
360 process_filter_graph (GstFFMpegDeinterlace * deinterlace, AVFrame * dst,
361 const AVFrame * src, enum AVPixelFormat pixfmt, int width, int height)
365 if (!deinterlace->filter_graph || width != deinterlace->last_width ||
366 height != deinterlace->last_height
367 || pixfmt != deinterlace->last_pixfmt) {
368 res = init_filter_graph (deinterlace, pixfmt, width, height);
373 memcpy (deinterlace->filter_frame->data, src->data, sizeof (src->data));
374 memcpy (deinterlace->filter_frame->linesize, src->linesize,
375 sizeof (src->linesize));
376 deinterlace->filter_frame->width = width;
377 deinterlace->filter_frame->height = height;
378 deinterlace->filter_frame->format = pixfmt;
380 av_buffersrc_add_frame (deinterlace->buffersrc_ctx,
381 deinterlace->filter_frame);
385 av_buffersink_get_frame (deinterlace->buffersink_ctx,
386 deinterlace->filter_frame);
389 av_image_copy (dst->data, dst->linesize,
390 (const uint8_t **) deinterlace->filter_frame->data,
391 deinterlace->filter_frame->linesize, pixfmt, width, height);
392 av_frame_unref (deinterlace->filter_frame);
398 gst_ffmpegdeinterlace_chain (GstPad * pad, GstObject * parent,
401 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
402 GstBuffer *outbuf = NULL;
403 GstFlowReturn result;
404 GstMapInfo from_map, to_map;
406 GST_OBJECT_LOCK (deinterlace);
407 if (deinterlace->reconfigure) {
410 if ((gint) deinterlace->new_mode != -1)
411 deinterlace->mode = deinterlace->new_mode;
412 deinterlace->new_mode = -1;
414 deinterlace->reconfigure = FALSE;
415 GST_OBJECT_UNLOCK (deinterlace);
416 if ((caps = gst_pad_get_current_caps (deinterlace->srcpad))) {
417 gst_ffmpegdeinterlace_sink_setcaps (deinterlace->sinkpad, parent, caps);
418 gst_caps_unref (caps);
421 GST_OBJECT_UNLOCK (deinterlace);
424 if (deinterlace->passthrough)
425 return gst_pad_push (deinterlace->srcpad, inbuf);
427 outbuf = gst_buffer_new_and_alloc (deinterlace->to_size);
429 gst_buffer_map (inbuf, &from_map, GST_MAP_READ);
430 gst_ffmpeg_avpicture_fill (&deinterlace->from_frame, from_map.data,
431 deinterlace->pixfmt, deinterlace->width, deinterlace->height);
433 gst_buffer_map (outbuf, &to_map, GST_MAP_WRITE);
434 gst_ffmpeg_avpicture_fill (&deinterlace->to_frame, to_map.data,
435 deinterlace->pixfmt, deinterlace->width, deinterlace->height);
437 process_filter_graph (deinterlace, &deinterlace->to_frame,
438 &deinterlace->from_frame, deinterlace->pixfmt, deinterlace->width,
439 deinterlace->height);
440 gst_buffer_unmap (outbuf, &to_map);
441 gst_buffer_unmap (inbuf, &from_map);
443 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
445 result = gst_pad_push (deinterlace->srcpad, outbuf);
447 gst_buffer_unref (inbuf);
453 gst_ffmpegdeinterlace_register (GstPlugin * plugin)
455 return gst_element_register (plugin, "avdeinterlace",
456 GST_RANK_NONE, GST_TYPE_FFMPEGDEINTERLACE);
460 gst_ffmpegdeinterlace_set_property (GObject * object, guint prop_id,
461 const GValue * value, GParamSpec * pspec)
463 GstFFMpegDeinterlace *self;
465 g_return_if_fail (GST_IS_FFMPEGDEINTERLACE (object));
466 self = GST_FFMPEGDEINTERLACE (object);
472 GST_OBJECT_LOCK (self);
473 new_mode = g_value_get_enum (value);
474 if (self->mode != new_mode && gst_pad_has_current_caps (self->srcpad)) {
475 self->reconfigure = TRUE;
476 self->new_mode = new_mode;
478 self->mode = new_mode;
479 gst_ffmpegdeinterlace_update_passthrough (self);
481 GST_OBJECT_UNLOCK (self);
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
491 gst_ffmpegdeinterlace_get_property (GObject * object, guint prop_id,
492 GValue * value, GParamSpec * pspec)
494 GstFFMpegDeinterlace *self;
496 g_return_if_fail (GST_IS_FFMPEGDEINTERLACE (object));
497 self = GST_FFMPEGDEINTERLACE (object);
501 g_value_set_enum (value, self->mode);
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);