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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #ifdef HAVE_FFMPEG_UNINSTALLED
30 # include <libavcodec/avcodec.h>
34 #include <gst/video/video.h>
36 #include "gstffmpeg.h"
37 #include "gstffmpegcodecmap.h"
38 #include "gstffmpegutils.h"
40 typedef struct _GstFFMpegDeinterlace
44 GstPad *sinkpad, *srcpad;
49 enum PixelFormat pixfmt;
50 AVPicture from_frame, to_frame;
51 } GstFFMpegDeinterlace;
53 typedef struct _GstFFMpegDeinterlaceClass
55 GstElementClass parent_class;
56 } GstFFMpegDeinterlaceClass;
58 #define GST_TYPE_FFMPEGDEINTERLACE \
59 (gst_ffmpegdeinterlace_get_type())
60 #define GST_FFMPEGDEINTERLACE(obj) \
61 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGDEINTERLACE,GstFFMpegDeinterlace))
62 #define GST_FFMPEGDEINTERLACE_CLASS(klass) \
63 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGDEINTERLACE,GstFFMpegDeinterlace))
64 #define GST_IS_FFMPEGDEINTERLACE(obj) \
65 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGDEINTERLACE))
66 #define GST_IS_FFMPEGDEINTERLACE_CLASS(klass) \
67 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGDEINTERLACE))
69 GType gst_ffmpegdeinterlace_get_type (void);
71 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
74 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
77 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
83 GST_BOILERPLATE (GstFFMpegDeinterlace, gst_ffmpegdeinterlace, GstElement,
86 static GstFlowReturn gst_ffmpegdeinterlace_chain (GstPad * pad,
90 gst_ffmpegdeinterlace_base_init (gpointer g_class)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94 gst_element_class_add_pad_template (element_class,
95 gst_static_pad_template_get (&src_factory));
96 gst_element_class_add_pad_template (element_class,
97 gst_static_pad_template_get (&sink_factory));
98 gst_element_class_set_details_simple (element_class,
99 "FFMPEG Deinterlace element", "Filter/Converter/Video",
100 "Deinterlace video", "Luca Ognibene <luogni@tin.it>");
104 gst_ffmpegdeinterlace_class_init (GstFFMpegDeinterlaceClass * klass)
109 gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstCaps * caps)
111 GstFFMpegDeinterlace *deinterlace =
112 GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad));
113 GstStructure *structure = gst_caps_get_structure (caps, 0);
116 if (!gst_structure_get_int (structure, "width", &deinterlace->width))
118 if (!gst_structure_get_int (structure, "height", &deinterlace->height))
121 ctx = avcodec_alloc_context ();
122 ctx->width = deinterlace->width;
123 ctx->height = deinterlace->height;
124 ctx->pix_fmt = PIX_FMT_NB;
125 gst_ffmpeg_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx);
126 if (ctx->pix_fmt == PIX_FMT_NB) {
131 deinterlace->pixfmt = ctx->pix_fmt;
135 deinterlace->to_size =
136 avpicture_get_size (deinterlace->pixfmt, deinterlace->width,
137 deinterlace->height);
139 return gst_pad_set_caps (deinterlace->srcpad, caps);
143 gst_ffmpegdeinterlace_init (GstFFMpegDeinterlace * deinterlace,
144 GstFFMpegDeinterlaceClass * klass)
146 deinterlace->sinkpad =
147 gst_pad_new_from_static_template (&sink_factory, "sink");
148 gst_pad_set_setcaps_function (deinterlace->sinkpad,
149 gst_ffmpegdeinterlace_sink_setcaps);
150 gst_pad_set_chain_function (deinterlace->sinkpad,
151 gst_ffmpegdeinterlace_chain);
152 gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->sinkpad);
154 deinterlace->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
155 gst_element_add_pad (GST_ELEMENT (deinterlace), deinterlace->srcpad);
157 deinterlace->pixfmt = PIX_FMT_NB;
161 gst_ffmpegdeinterlace_chain (GstPad * pad, GstBuffer * inbuf)
163 GstFFMpegDeinterlace *deinterlace =
164 GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad));
165 GstBuffer *outbuf = NULL;
166 GstFlowReturn result;
169 gst_pad_alloc_buffer (deinterlace->srcpad, GST_BUFFER_OFFSET_NONE,
170 deinterlace->to_size, GST_PAD_CAPS (deinterlace->srcpad), &outbuf);
171 if (result == GST_FLOW_OK) {
172 gst_ffmpeg_avpicture_fill (&deinterlace->from_frame,
173 GST_BUFFER_DATA (inbuf), deinterlace->pixfmt, deinterlace->width,
174 deinterlace->height);
176 gst_ffmpeg_avpicture_fill (&deinterlace->to_frame, GST_BUFFER_DATA (outbuf),
177 deinterlace->pixfmt, deinterlace->width, deinterlace->height);
179 avpicture_deinterlace (&deinterlace->to_frame, &deinterlace->from_frame,
180 deinterlace->pixfmt, deinterlace->width, deinterlace->height);
182 gst_buffer_copy_metadata (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS);
184 result = gst_pad_push (deinterlace->srcpad, outbuf);
187 gst_buffer_unref (inbuf);
193 gst_ffmpegdeinterlace_register (GstPlugin * plugin)
195 return gst_element_register (plugin, "ffdeinterlace",
196 GST_RANK_NONE, GST_TYPE_FFMPEGDEINTERLACE);