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 gst_type_mark_as_plugin_api (GST_TYPE_FFMPEGDEINTERLACE_MODES, 0);
186 }
187
188 static void
gst_ffmpegdeinterlace_update_passthrough(GstFFMpegDeinterlace * deinterlace)189 gst_ffmpegdeinterlace_update_passthrough (GstFFMpegDeinterlace * deinterlace)
190 {
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);
196 }
197
198 static gboolean
gst_ffmpegdeinterlace_sink_setcaps(GstPad * pad,GstCaps * caps)199 gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstCaps * caps)
200 {
201 GstFFMpegDeinterlace *deinterlace =
202 GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad));
203 GstStructure *structure = gst_caps_get_structure (caps, 0);
204 const gchar *imode;
205 AVCodecContext *ctx;
206 GstCaps *src_caps;
207 gboolean ret;
208
209 /* FIXME: use GstVideoInfo etc. */
210 if (!gst_structure_get_int (structure, "width", &deinterlace->width))
211 return FALSE;
212 if (!gst_structure_get_int (structure, "height", &deinterlace->height))
213 return FALSE;
214
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;
219 }
220 gst_ffmpegdeinterlace_update_passthrough (deinterlace);
221
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);
229 av_free (ctx);
230 return FALSE;
231 }
232
233 deinterlace->pixfmt = ctx->pix_fmt;
234
235 av_free (ctx);
236
237 deinterlace->to_size =
238 av_image_get_buffer_size (deinterlace->pixfmt, deinterlace->width,
239 deinterlace->height, 1);
240
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);
246
247 return ret;
248 }
249
250 static gboolean
gst_ffmpegdeinterlace_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)251 gst_ffmpegdeinterlace_sink_event (GstPad * pad, GstObject * parent,
252 GstEvent * event)
253 {
254 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
255 gboolean ret = FALSE;
256
257 switch (GST_EVENT_TYPE (event)) {
258 case GST_EVENT_CAPS:
259 {
260 GstCaps *caps;
261
262 gst_event_parse_caps (event, &caps);
263 ret = gst_ffmpegdeinterlace_sink_setcaps (pad, caps);
264 gst_event_unref (event);
265 break;
266 }
267 default:
268 ret = gst_pad_push_event (deinterlace->srcpad, event);
269 break;
270 }
271
272 return ret;
273 }
274
275 static void
gst_ffmpegdeinterlace_init(GstFFMpegDeinterlace * deinterlace)276 gst_ffmpegdeinterlace_init (GstFFMpegDeinterlace * deinterlace)
277 {
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);
285
286 deinterlace->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
287 gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->srcpad);
288
289 deinterlace->pixfmt = AV_PIX_FMT_NB;
290
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;
299 }
300
301 static void
delete_filter_graph(GstFFMpegDeinterlace * deinterlace)302 delete_filter_graph (GstFFMpegDeinterlace * deinterlace)
303 {
304 if (deinterlace->filter_graph) {
305 av_frame_free (&deinterlace->filter_frame);
306 avfilter_graph_free (&deinterlace->filter_graph);
307 }
308 }
309
310 static void
gst_ffmpegdeinterlace_dispose(GObject * obj)311 gst_ffmpegdeinterlace_dispose (GObject * obj)
312 {
313 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (obj);
314
315 delete_filter_graph (deinterlace);
316
317 G_OBJECT_CLASS (gst_ffmpegdeinterlace_parent_class)->dispose (obj);
318 }
319
320 static int
init_filter_graph(GstFFMpegDeinterlace * deinterlace,enum AVPixelFormat pixfmt,int width,int height)321 init_filter_graph (GstFFMpegDeinterlace * deinterlace,
322 enum AVPixelFormat pixfmt, int width, int height)
323 {
324 AVFilterInOut *inputs = NULL, *outputs = NULL;
325 char args[512];
326 int res;
327
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);
333 res =
334 avfilter_graph_parse2 (deinterlace->filter_graph, args, &inputs,
335 &outputs);
336 if (res < 0)
337 return res;
338 if (inputs || outputs)
339 return -1;
340 res = avfilter_graph_config (deinterlace->filter_graph, NULL);
341 if (res < 0)
342 return res;
343
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)
350 return -1;
351 deinterlace->filter_frame = av_frame_alloc ();
352 deinterlace->last_width = width;
353 deinterlace->last_height = height;
354 deinterlace->last_pixfmt = pixfmt;
355
356 return 0;
357 }
358
359 static int
process_filter_graph(GstFFMpegDeinterlace * deinterlace,AVFrame * dst,const AVFrame * src,enum AVPixelFormat pixfmt,int width,int height)360 process_filter_graph (GstFFMpegDeinterlace * deinterlace, AVFrame * dst,
361 const AVFrame * src, enum AVPixelFormat pixfmt, int width, int height)
362 {
363 int res;
364
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);
369 if (res < 0)
370 return res;
371 }
372
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;
379 res =
380 av_buffersrc_add_frame (deinterlace->buffersrc_ctx,
381 deinterlace->filter_frame);
382 if (res < 0)
383 return res;
384 res =
385 av_buffersink_get_frame (deinterlace->buffersink_ctx,
386 deinterlace->filter_frame);
387 if (res < 0)
388 return res;
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);
393
394 return 0;
395 }
396
397 static GstFlowReturn
gst_ffmpegdeinterlace_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)398 gst_ffmpegdeinterlace_chain (GstPad * pad, GstObject * parent,
399 GstBuffer * inbuf)
400 {
401 GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (parent);
402 GstBuffer *outbuf = NULL;
403 GstFlowReturn result;
404 GstMapInfo from_map, to_map;
405
406 GST_OBJECT_LOCK (deinterlace);
407 if (deinterlace->reconfigure) {
408 GstCaps *caps;
409
410 if ((gint) deinterlace->new_mode != -1)
411 deinterlace->mode = deinterlace->new_mode;
412 deinterlace->new_mode = -1;
413
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, caps);
418 gst_caps_unref (caps);
419 }
420 } else {
421 GST_OBJECT_UNLOCK (deinterlace);
422 }
423
424 if (deinterlace->passthrough)
425 return gst_pad_push (deinterlace->srcpad, inbuf);
426
427 outbuf = gst_buffer_new_and_alloc (deinterlace->to_size);
428
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);
432
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);
436
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);
442
443 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
444
445 result = gst_pad_push (deinterlace->srcpad, outbuf);
446
447 gst_buffer_unref (inbuf);
448
449 return result;
450 }
451
452 gboolean
gst_ffmpegdeinterlace_register(GstPlugin * plugin)453 gst_ffmpegdeinterlace_register (GstPlugin * plugin)
454 {
455 return gst_element_register (plugin, "avdeinterlace",
456 GST_RANK_NONE, GST_TYPE_FFMPEGDEINTERLACE);
457 }
458
459 static void
gst_ffmpegdeinterlace_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)460 gst_ffmpegdeinterlace_set_property (GObject * object, guint prop_id,
461 const GValue * value, GParamSpec * pspec)
462 {
463 GstFFMpegDeinterlace *self;
464
465 g_return_if_fail (GST_IS_FFMPEGDEINTERLACE (object));
466 self = GST_FFMPEGDEINTERLACE (object);
467
468 switch (prop_id) {
469 case PROP_MODE:{
470 gint new_mode;
471
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;
477 } else {
478 self->mode = new_mode;
479 gst_ffmpegdeinterlace_update_passthrough (self);
480 }
481 GST_OBJECT_UNLOCK (self);
482 break;
483 }
484 default:
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
486 }
487
488 }
489
490 static void
gst_ffmpegdeinterlace_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)491 gst_ffmpegdeinterlace_get_property (GObject * object, guint prop_id,
492 GValue * value, GParamSpec * pspec)
493 {
494 GstFFMpegDeinterlace *self;
495
496 g_return_if_fail (GST_IS_FFMPEGDEINTERLACE (object));
497 self = GST_FFMPEGDEINTERLACE (object);
498
499 switch (prop_id) {
500 case PROP_MODE:
501 g_value_set_enum (value, self->mode);
502 break;
503 default:
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
505 }
506 }
507