31760ac12b780537c301f77966e6fff4775c637d
[platform/upstream/gstreamer.git] / gst / interlace / gstinterlace.c
1 /* GStreamer
2  * Copyright (C) 2010 David A. Schleef <ds@schleef.org>
3  * Copyright (C) 2010 Robert Swain <robert.swain@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 /**
21  * SECTION:element-interlace
22  * @title: interlace
23  *
24  * The interlace element takes a non-interlaced raw video stream as input,
25  * creates fields out of each frame, then combines fields into interlaced
26  * frames to output as an interlaced video stream. It can also produce
27  * telecined streams from progressive input.
28  *
29  * ## Example launch line
30  * |[
31  * gst-launch-1.0 -v videotestsrc pattern=ball ! interlace ! xvimagesink
32  * ]|
33  * This pipeline illustrates the combing effects caused by displaying
34  * two interlaced fields as one progressive frame.
35  * |[
36  * gst-launch-1.0 -v filesrc location=/path/to/file ! decodebin ! videorate !
37  *   videoscale ! video/x-raw,format=\(string\)I420,width=720,height=480,
38  *   framerate=60000/1001,pixel-aspect-ratio=11/10 !
39  *   interlace top-field-first=false ! autovideosink
40  * ]|
41  * This pipeline converts a progressive video stream into an interlaced
42  * stream suitable for standard definition NTSC.
43  * |[
44  * gst-launch-1.0 -v videotestsrc pattern=ball ! video/x-raw,
45  *   format=\(string\)I420,width=720,height=480,framerate=24000/1001,
46  *   pixel-aspect-ratio=11/10 ! interlace !
47  *   autovideosink
48  * ]|
49  * This pipeline converts a 24 frames per second progressive film stream into a
50  * 30000/1001 2:3:2:3... pattern telecined stream suitable for displaying film
51  * content on NTSC.
52  *
53  */
54
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59
60 #include <gst/gst.h>
61 #include <gst/video/video.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <math.h>
65
66 GST_DEBUG_CATEGORY (gst_interlace_debug);
67 #define GST_CAT_DEFAULT gst_interlace_debug
68
69 #define GST_TYPE_INTERLACE \
70   (gst_interlace_get_type())
71 #define GST_INTERLACE(obj) \
72   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_INTERLACE,GstInterlace))
73 #define GST_INTERLACE_DEC_CLASS(klass) \
74   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_INTERLACE,GstInterlaceClass))
75 #define GST_IS_GST_INTERLACE(obj) \
76   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_INTERLACE))
77 #define GST_IS_GST_INTERLACE_CLASS(obj) \
78   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_INTERLACE))
79
80 typedef struct _GstInterlace GstInterlace;
81 typedef struct _GstInterlaceClass GstInterlaceClass;
82
83 struct _GstInterlace
84 {
85   GstElement element;
86
87   GstPad *srcpad;
88   GstPad *sinkpad;
89
90   /* properties */
91   gboolean top_field_first;
92   gint pattern;
93   gboolean allow_rff;
94
95   /* state */
96   GstVideoInfo info;
97   GstVideoInfo out_info;
98   int src_fps_n;
99   int src_fps_d;
100
101   GstBuffer *stored_frame;
102   guint stored_fields;
103   guint phase_index;
104   guint field_index;            /* index of the next field to push, 0=top 1=bottom */
105   GstClockTime timebase;
106   guint fields_since_timebase;
107   guint pattern_offset;         /* initial offset into the pattern */
108   gboolean passthrough;
109 };
110
111 struct _GstInterlaceClass
112 {
113   GstElementClass element_class;
114 };
115
116 enum
117 {
118   PROP_0,
119   PROP_TOP_FIELD_FIRST,
120   PROP_PATTERN,
121   PROP_PATTERN_OFFSET,
122   PROP_ALLOW_RFF
123 };
124
125 typedef enum
126 {
127   GST_INTERLACE_PATTERN_1_1,
128   GST_INTERLACE_PATTERN_2_2,
129   GST_INTERLACE_PATTERN_2_3,
130   GST_INTERLACE_PATTERN_2_3_3_2,
131   GST_INTERLACE_PATTERN_EURO,
132   GST_INTERLACE_PATTERN_3_4R3,
133   GST_INTERLACE_PATTERN_3R7_4,
134   GST_INTERLACE_PATTERN_3_3_4,
135   GST_INTERLACE_PATTERN_3_3,
136   GST_INTERLACE_PATTERN_3_2R4,
137   GST_INTERLACE_PATTERN_1_2R4,
138 } GstInterlacePattern;
139
140 #define GST_INTERLACE_PATTERN (gst_interlace_pattern_get_type ())
141 static GType
142 gst_interlace_pattern_get_type (void)
143 {
144   static GType interlace_pattern_type = 0;
145   static const GEnumValue pattern_types[] = {
146     {GST_INTERLACE_PATTERN_1_1, "1:1 (e.g. 60p -> 60i)", "1:1"},
147     {GST_INTERLACE_PATTERN_2_2, "2:2 (e.g. 30p -> 60i)", "2:2"},
148     {GST_INTERLACE_PATTERN_2_3, "2:3 (e.g. 24p -> 60i telecine)", "2:3"},
149     {GST_INTERLACE_PATTERN_2_3_3_2, "2:3:3:2 (e.g. 24p -> 60i telecine)",
150         "2:3:3:2"},
151     {GST_INTERLACE_PATTERN_EURO, "Euro 2-11:3 (e.g. 24p -> 50i telecine)",
152         "2-11:3"},
153     {GST_INTERLACE_PATTERN_3_4R3, "3:4-3 (e.g. 16p -> 60i telecine)", "3:4-3"},
154     {GST_INTERLACE_PATTERN_3R7_4, "3-7:4 (e.g. 16p -> 50i telecine)", "3-7:4"},
155     {GST_INTERLACE_PATTERN_3_3_4, "3:3:4 (e.g. 18p -> 60i telecine)", "3:3:4"},
156     {GST_INTERLACE_PATTERN_3_3, "3:3 (e.g. 20p -> 60i telecine)", "3:3"},
157     {GST_INTERLACE_PATTERN_3_2R4, "3:2-4 (e.g. 27.5p -> 60i telecine)",
158         "3:2-4"},
159     {GST_INTERLACE_PATTERN_1_2R4, "1:2-4 (e.g. 27.5p -> 50i telecine)",
160         "1:2-4"},
161     {0, NULL, NULL}
162   };
163
164   if (!interlace_pattern_type) {
165     interlace_pattern_type =
166         g_enum_register_static ("GstInterlacePattern", pattern_types);
167   }
168
169   return interlace_pattern_type;
170 }
171
172 #define VIDEO_FORMATS "{AYUV,YUY2,UYVY,I420,YV12,Y42B,Y444,NV12,NV21}"
173
174 static GstStaticPadTemplate gst_interlace_src_template =
175     GST_STATIC_PAD_TEMPLATE ("src",
176     GST_PAD_SRC,
177     GST_PAD_ALWAYS,
178     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_FORMATS)
179         ",interlace-mode={interleaved,mixed} ;"
180         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_FORMAT_INTERLACED,
181             VIDEO_FORMATS)
182         ",interlace-mode=alternate")
183     );
184
185 static GstStaticPadTemplate gst_interlace_sink_template =
186 GST_STATIC_PAD_TEMPLATE ("sink",
187     GST_PAD_SINK,
188     GST_PAD_ALWAYS,
189     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
190         ("{AYUV,YUY2,UYVY,I420,YV12,Y42B,Y444,NV12,NV21}")
191     )
192     );
193
194 GType gst_interlace_get_type (void);
195 static void gst_interlace_finalize (GObject * obj);
196
197 static void gst_interlace_set_property (GObject * object,
198     guint prop_id, const GValue * value, GParamSpec * pspec);
199 static void gst_interlace_get_property (GObject * object,
200     guint prop_id, GValue * value, GParamSpec * pspec);
201
202 static gboolean gst_interlace_sink_event (GstPad * pad, GstObject * parent,
203     GstEvent * event);
204 static gboolean gst_interlace_sink_query (GstPad * pad, GstObject * parent,
205     GstQuery * query);
206 static GstFlowReturn gst_interlace_chain (GstPad * pad, GstObject * parent,
207     GstBuffer * buffer);
208
209 static gboolean gst_interlace_src_query (GstPad * pad, GstObject * parent,
210     GstQuery * query);
211
212 static GstStateChangeReturn gst_interlace_change_state (GstElement * element,
213     GstStateChange transition);
214
215 #define gst_interlace_parent_class parent_class
216 G_DEFINE_TYPE (GstInterlace, gst_interlace, GST_TYPE_ELEMENT);
217
218 static void
219 gst_interlace_class_init (GstInterlaceClass * klass)
220 {
221   GObjectClass *object_class = G_OBJECT_CLASS (klass);
222   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
223
224   parent_class = g_type_class_peek_parent (klass);
225
226   object_class->set_property = gst_interlace_set_property;
227   object_class->get_property = gst_interlace_get_property;
228   object_class->finalize = gst_interlace_finalize;
229
230   g_object_class_install_property (object_class, PROP_TOP_FIELD_FIRST,
231       g_param_spec_boolean ("top-field-first", "top field first",
232           "Interlaced stream should be top field first", FALSE,
233           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234
235   g_object_class_install_property (object_class, PROP_PATTERN,
236       g_param_spec_enum ("field-pattern", "Field pattern",
237           "The output field pattern", GST_INTERLACE_PATTERN,
238           GST_INTERLACE_PATTERN_2_3,
239           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240
241   g_object_class_install_property (object_class, PROP_PATTERN_OFFSET,
242       g_param_spec_uint ("pattern-offset", "Pattern offset",
243           "The initial field pattern offset. Counts from 0.",
244           0, 12, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245
246   g_object_class_install_property (object_class, PROP_ALLOW_RFF,
247       g_param_spec_boolean ("allow-rff", "Allow Repeat-First-Field flags",
248           "Allow generation of buffers with RFF flag set, i.e., duration of 3 fields",
249           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250
251   gst_element_class_set_static_metadata (element_class,
252       "Interlace filter", "Filter/Video",
253       "Creates an interlaced video from progressive frames",
254       "David Schleef <ds@schleef.org>");
255
256   gst_element_class_add_static_pad_template (element_class,
257       &gst_interlace_sink_template);
258   gst_element_class_add_static_pad_template (element_class,
259       &gst_interlace_src_template);
260
261   element_class->change_state = gst_interlace_change_state;
262
263   gst_type_mark_as_plugin_api (GST_INTERLACE_PATTERN, 0);
264 }
265
266 static void
267 gst_interlace_finalize (GObject * obj)
268 {
269   G_OBJECT_CLASS (parent_class)->finalize (obj);
270 }
271
272 static void
273 gst_interlace_reset (GstInterlace * interlace)
274 {
275   interlace->phase_index = interlace->pattern_offset;
276   interlace->timebase = GST_CLOCK_TIME_NONE;
277   interlace->field_index = 0;
278   interlace->passthrough = FALSE;
279   if (interlace->stored_frame) {
280     gst_buffer_unref (interlace->stored_frame);
281     interlace->stored_frame = NULL;
282   }
283 }
284
285 static void
286 gst_interlace_init (GstInterlace * interlace)
287 {
288   GST_DEBUG ("gst_interlace_init");
289   interlace->sinkpad =
290       gst_pad_new_from_static_template (&gst_interlace_sink_template, "sink");
291   gst_pad_set_chain_function (interlace->sinkpad, gst_interlace_chain);
292   gst_pad_set_event_function (interlace->sinkpad, gst_interlace_sink_event);
293   gst_pad_set_query_function (interlace->sinkpad, gst_interlace_sink_query);
294   gst_element_add_pad (GST_ELEMENT (interlace), interlace->sinkpad);
295
296   interlace->srcpad =
297       gst_pad_new_from_static_template (&gst_interlace_src_template, "src");
298   gst_pad_set_query_function (interlace->srcpad, gst_interlace_src_query);
299   gst_element_add_pad (GST_ELEMENT (interlace), interlace->srcpad);
300
301   interlace->top_field_first = FALSE;
302   interlace->allow_rff = FALSE;
303   interlace->pattern = GST_INTERLACE_PATTERN_2_3;
304   interlace->pattern_offset = 0;
305   gst_interlace_reset (interlace);
306 }
307
308 typedef struct _PulldownFormat PulldownFormat;
309 struct _PulldownFormat
310 {
311   const gchar *name;
312   /* ratio between outgoing field rate / 2 and incoming frame rate.
313    * I.e., 24p -> 60i is 1.25  */
314   int ratio_n, ratio_d;
315   int n_fields[13];
316 };
317
318 static const PulldownFormat formats[] = {
319   /* 60p -> 60i or 50p -> 50i */
320   {"1:1", 1, 2, {1}},
321   /* 30p -> 60i or 25p -> 50i */
322   {"2:2", 1, 1, {2}},
323   /* 24p -> 60i telecine */
324   {"2:3", 5, 4, {2, 3,}},
325   {"2:3:3:2", 5, 4, {2, 3, 3, 2,}},
326   /* 24p -> 50i Euro pulldown */
327   {"2-11:3", 25, 24, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,}},
328   /* 16p (16000/1001) -> 60i (NTSC 30000/1001) */
329   {"3:4-3", 15, 8, {3, 4, 4, 4,}},
330   /* 16p -> 50i (PAL) */
331   {"3-7:4", 25, 16, {3, 3, 3, 3, 3, 3, 3, 4,}},
332   /* 18p to NTSC 60i */
333   {"3:3:4", 5, 3, {3, 3, 4,}},
334   /* 20p to NTSC 60i */
335   {"3:3", 3, 2, {3, 3,}},
336   /* 27.5 to NTSC 60i */
337   {"3:2-4", 11, 10, {3, 2, 2, 2, 2,}},
338   /* 27.5 to PAL 50i */
339   {"1:2-4", 9, 10, {1, 2, 2, 2, 2,}},
340 };
341
342 static void
343 gst_interlace_decorate_buffer_ts (GstInterlace * interlace, GstBuffer * buf,
344     int n_fields)
345 {
346   /* field duration = src_fps_d / (2 * src_fps_n) */
347   if (interlace->src_fps_n == 0) {
348     /* If we don't know the fps, we can't generate timestamps/durations */
349     GST_BUFFER_DTS (buf) = GST_CLOCK_TIME_NONE;
350     GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
351     GST_BUFFER_DURATION (buf) = GST_CLOCK_TIME_NONE;
352   } else {
353     GST_BUFFER_DTS (buf) = interlace->timebase +
354         gst_util_uint64_scale (GST_SECOND,
355         interlace->src_fps_d * interlace->fields_since_timebase,
356         interlace->src_fps_n * 2);
357     GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf);
358     GST_BUFFER_DURATION (buf) =
359         gst_util_uint64_scale (GST_SECOND, interlace->src_fps_d * n_fields,
360         interlace->src_fps_n * 2);
361   }
362 }
363
364 static void
365 gst_interlace_decorate_buffer (GstInterlace * interlace, GstBuffer * buf,
366     int n_fields, gboolean interlaced)
367 {
368   gst_interlace_decorate_buffer_ts (interlace, buf, n_fields);
369
370   if (interlace->field_index == 0) {
371     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF);
372   }
373   if (n_fields == 3) {
374     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_RFF);
375   }
376   if (n_fields == 1) {
377     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_ONEFIELD);
378   }
379   if (interlace->pattern > GST_INTERLACE_PATTERN_2_2 && n_fields == 2
380       && interlaced) {
381     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
382   }
383 }
384
385 static const gchar *
386 interlace_mode_from_pattern (GstInterlace * interlace)
387 {
388   if (interlace->pattern > GST_INTERLACE_PATTERN_2_2)
389     return "mixed";
390   else
391     return "interleaved";
392 }
393
394 static GstCaps *
395 dup_caps_with_alternate (GstCaps * caps)
396 {
397   GstCaps *with_alternate;
398   GstCapsFeatures *features;
399
400   with_alternate = gst_caps_copy (caps);
401   features = gst_caps_features_new (GST_CAPS_FEATURE_FORMAT_INTERLACED, NULL);
402   gst_caps_set_features_simple (with_alternate, features);
403
404   gst_caps_set_simple (with_alternate, "interlace-mode", G_TYPE_STRING,
405       "alternate", NULL);
406
407   return with_alternate;
408 }
409
410 static gboolean
411 gst_interlace_setcaps (GstInterlace * interlace, GstCaps * caps)
412 {
413   gboolean ret;
414   GstVideoInfo info, out_info;
415   GstCaps *othercaps, *src_peer_caps;
416   const PulldownFormat *pdformat;
417   gboolean alternate;
418
419   if (!gst_video_info_from_caps (&info, caps))
420     goto caps_error;
421
422   /* Check if downstream prefers alternate mode */
423   othercaps = gst_caps_copy (caps);
424   gst_caps_set_simple (othercaps, "interlace-mode", G_TYPE_STRING,
425       interlace_mode_from_pattern (interlace), NULL);
426   gst_caps_append (othercaps, dup_caps_with_alternate (othercaps));
427   src_peer_caps = gst_pad_peer_query_caps (interlace->srcpad, othercaps);
428   gst_caps_unref (othercaps);
429   othercaps = gst_caps_fixate (src_peer_caps);
430   if (gst_caps_is_empty (othercaps)) {
431     gst_caps_unref (othercaps);
432     goto caps_error;
433   }
434   if (!gst_video_info_from_caps (&out_info, othercaps)) {
435     gst_caps_unref (othercaps);
436     goto caps_error;
437   }
438
439   alternate =
440       GST_VIDEO_INFO_INTERLACE_MODE (&out_info) ==
441       GST_VIDEO_INTERLACE_MODE_ALTERNATE;
442
443   pdformat = &formats[interlace->pattern];
444
445   interlace->phase_index = interlace->pattern_offset;
446
447   interlace->src_fps_n = info.fps_n * pdformat->ratio_n;
448   interlace->src_fps_d = info.fps_d * pdformat->ratio_d;
449
450   if (alternate) {
451     GST_DEBUG_OBJECT (interlace,
452         "producing alternate stream as requested downstream");
453   }
454
455   if (gst_caps_can_intersect (caps, othercaps)) {
456     /* FIXME: field-order is optional in the caps. This means that, if we're
457      * in a non-telecine mode and we have TFF upstream and
458      * top-field-first=FALSE in interlace (or the other way around), AND
459      * field-order isn't mentioned in the caps, we will do passthrough here
460      * and end up outptuting wrong data. Must detect missing field-order info
461      * and not do passthrough in that case, but instead check the
462      * GstVideoBufferFlags at the switch_fields check */
463     interlace->passthrough = TRUE;
464   } else {
465     if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
466       GST_ERROR_OBJECT (interlace,
467           "Caps %" GST_PTR_FORMAT " not compatible with %" GST_PTR_FORMAT, caps,
468           othercaps);
469       gst_caps_unref (othercaps);
470       goto caps_error;
471     }
472     interlace->passthrough = FALSE;
473     gst_caps_set_simple (othercaps, "framerate", GST_TYPE_FRACTION,
474         interlace->src_fps_n, interlace->src_fps_d, NULL);
475     if (interlace->pattern <= GST_INTERLACE_PATTERN_2_2 || alternate) {
476       gst_caps_set_simple (othercaps, "field-order", G_TYPE_STRING,
477           interlace->top_field_first ? "top-field-first" : "bottom-field-first",
478           NULL);
479     }
480   }
481
482   GST_DEBUG_OBJECT (interlace->sinkpad, "set caps %" GST_PTR_FORMAT, caps);
483   GST_DEBUG_OBJECT (interlace->srcpad, "set caps %" GST_PTR_FORMAT, othercaps);
484
485   ret = gst_pad_set_caps (interlace->srcpad, othercaps);
486   gst_caps_unref (othercaps);
487
488   interlace->info = info;
489   interlace->out_info = out_info;
490
491   return ret;
492
493 caps_error:
494   {
495     GST_DEBUG_OBJECT (interlace, "error parsing caps");
496     return FALSE;
497   }
498 }
499
500 static gboolean
501 gst_interlace_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
502 {
503   gboolean ret;
504   GstInterlace *interlace;
505
506   interlace = GST_INTERLACE (parent);
507
508   switch (GST_EVENT_TYPE (event)) {
509     case GST_EVENT_FLUSH_START:
510       GST_DEBUG_OBJECT (interlace, "handling FLUSH_START");
511       ret = gst_pad_push_event (interlace->srcpad, event);
512       break;
513     case GST_EVENT_FLUSH_STOP:
514       GST_DEBUG_OBJECT (interlace, "handling FLUSH_STOP");
515       gst_interlace_reset (interlace);
516       ret = gst_pad_push_event (interlace->srcpad, event);
517       break;
518     case GST_EVENT_EOS:
519 #if 0
520       /* FIXME revive this when we output ONEFIELD and RFF buffers */
521     {
522       gint num_fields;
523       const PulldownFormat *format = &formats[interlace->pattern];
524
525       num_fields =
526           format->n_fields[interlace->phase_index] -
527           interlace->stored_fields_pushed;
528       interlace->stored_fields_pushed = 0;
529
530       /* on EOS we want to push as many sane frames as are left */
531       while (num_fields > 1) {
532         GstBuffer *output_buffer;
533
534         /* make metadata writable before editing it */
535         interlace->stored_frame =
536             gst_buffer_make_metadata_writable (interlace->stored_frame);
537         num_fields -= 2;
538
539         gst_interlace_decorate_buffer (interlace, interlace->stored_frame,
540             n_fields, FALSE);
541
542         /* ref output_buffer/stored frame because we want to keep it for now
543          * and pushing gives away a ref */
544         output_buffer = gst_buffer_ref (interlace->stored_frame);
545         if (gst_pad_push (interlace->srcpad, output_buffer)) {
546           GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p",
547               output_buffer);
548           return FALSE;
549         }
550         output_buffer = NULL;
551
552         if (num_fields <= 1) {
553           gst_buffer_unref (interlace->stored_frame);
554           interlace->stored_frame = NULL;
555           break;
556         }
557       }
558
559       /* increment the phase index */
560       interlace->phase_index++;
561       if (!format->n_fields[interlace->phase_index]) {
562         interlace->phase_index = 0;
563       }
564     }
565 #endif
566
567       ret = gst_pad_push_event (interlace->srcpad, event);
568       break;
569     case GST_EVENT_CAPS:
570     {
571       GstCaps *caps;
572
573       gst_event_parse_caps (event, &caps);
574       ret = gst_interlace_setcaps (interlace, caps);
575       gst_event_unref (event);
576       break;
577     }
578     default:
579       ret = gst_pad_push_event (interlace->srcpad, event);
580       break;
581   }
582
583   return ret;
584 }
585
586 static gboolean
587 gst_interlace_fraction_double (gint * n_out, gint * d_out, gboolean half)
588 {
589   gint n, d, gcd;
590
591   n = *n_out;
592   d = *d_out;
593
594   if (d == 0)
595     return FALSE;
596
597   if (n == 0)
598     return TRUE;
599
600   gcd = gst_util_greatest_common_divisor (n, d);
601   n /= gcd;
602   d /= gcd;
603
604   if (half) {
605     if (G_MAXINT / 2 >= ABS (d)) {
606       d *= 2;
607     } else if (n >= 2 && n != G_MAXINT) {
608       n /= 2;
609     } else {
610       d = G_MAXINT;
611     }
612   } else {
613     if (G_MAXINT / 2 >= ABS (n)) {
614       n *= 2;
615     } else if (d >= 2 && d != G_MAXINT) {
616       d /= 2;
617     } else {
618       n = G_MAXINT;
619     }
620   }
621
622   *n_out = n;
623   *d_out = d;
624
625   return TRUE;
626 }
627
628 static GstCaps *
629 gst_interlace_caps_double_framerate (GstCaps * caps, gboolean half)
630 {
631   guint len;
632
633   for (len = gst_caps_get_size (caps); len > 0; len--) {
634     GstStructure *s = gst_caps_get_structure (caps, len - 1);
635     const GValue *val;
636
637     val = gst_structure_get_value (s, "framerate");
638     if (!val)
639       continue;
640
641     if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION) {
642       gint n, d;
643
644       n = gst_value_get_fraction_numerator (val);
645       d = gst_value_get_fraction_denominator (val);
646
647       if (!gst_interlace_fraction_double (&n, &d, half)) {
648         gst_caps_remove_structure (caps, len - 1);
649         continue;
650       }
651
652       gst_structure_set (s, "framerate", GST_TYPE_FRACTION, n, d, NULL);
653     } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
654       const GValue *min, *max;
655       GValue nrange = { 0, }, nmin = {
656       0,}, nmax = {
657       0,};
658       gint n, d;
659
660       g_value_init (&nrange, GST_TYPE_FRACTION_RANGE);
661       g_value_init (&nmin, GST_TYPE_FRACTION);
662       g_value_init (&nmax, GST_TYPE_FRACTION);
663
664       min = gst_value_get_fraction_range_min (val);
665       max = gst_value_get_fraction_range_max (val);
666
667       n = gst_value_get_fraction_numerator (min);
668       d = gst_value_get_fraction_denominator (min);
669
670       if (!gst_interlace_fraction_double (&n, &d, half)) {
671         g_value_unset (&nrange);
672         g_value_unset (&nmax);
673         g_value_unset (&nmin);
674         gst_caps_remove_structure (caps, len - 1);
675         continue;
676       }
677
678       gst_value_set_fraction (&nmin, n, d);
679
680       n = gst_value_get_fraction_numerator (max);
681       d = gst_value_get_fraction_denominator (max);
682
683       if (!gst_interlace_fraction_double (&n, &d, half)) {
684         g_value_unset (&nrange);
685         g_value_unset (&nmax);
686         g_value_unset (&nmin);
687         gst_caps_remove_structure (caps, len - 1);
688         continue;
689       }
690
691       gst_value_set_fraction (&nmax, n, d);
692       gst_value_set_fraction_range (&nrange, &nmin, &nmax);
693
694       gst_structure_take_value (s, "framerate", &nrange);
695
696       g_value_unset (&nmin);
697       g_value_unset (&nmax);
698     } else if (G_VALUE_TYPE (val) == GST_TYPE_LIST) {
699       const GValue *lval;
700       GValue nlist = { 0, };
701       GValue nval = { 0, };
702       gint i;
703
704       g_value_init (&nlist, GST_TYPE_LIST);
705       for (i = gst_value_list_get_size (val); i > 0; i--) {
706         gint n, d;
707
708         lval = gst_value_list_get_value (val, i - 1);
709
710         if (G_VALUE_TYPE (lval) != GST_TYPE_FRACTION)
711           continue;
712
713         n = gst_value_get_fraction_numerator (lval);
714         d = gst_value_get_fraction_denominator (lval);
715
716         /* Double/Half the framerate but if this fails simply
717          * skip this value from the list */
718         if (!gst_interlace_fraction_double (&n, &d, half)) {
719           continue;
720         }
721
722         g_value_init (&nval, GST_TYPE_FRACTION);
723
724         gst_value_set_fraction (&nval, n, d);
725         gst_value_list_append_and_take_value (&nlist, &nval);
726       }
727       gst_structure_take_value (s, "framerate", &nlist);
728     }
729   }
730
731   return caps;
732 }
733
734 static GstCaps *
735 gst_interlace_getcaps (GstPad * pad, GstInterlace * interlace, GstCaps * filter)
736 {
737   GstPad *otherpad;
738   GstCaps *othercaps, *tcaps;
739   GstCaps *icaps;
740   GstCaps *clean_filter = NULL;
741   const char *mode;
742   guint i;
743
744   otherpad =
745       (pad == interlace->srcpad) ? interlace->sinkpad : interlace->srcpad;
746
747   if (filter != NULL) {
748     clean_filter = gst_caps_copy (filter);
749     clean_filter =
750         gst_interlace_caps_double_framerate (clean_filter,
751         (pad == interlace->sinkpad));
752
753     if (pad == interlace->sinkpad) {
754       /* @filter may contain the different formats supported upstream.
755        * Those will be used to filter the src pad caps as this element
756        * is not supposed to do any video format conversion.
757        * Add a variant of the filter with the Interlaced feature as we want
758        * to be able to negotiate it if needed.
759        */
760       gst_caps_append (clean_filter, dup_caps_with_alternate (clean_filter));
761     }
762
763     for (i = 0; i < gst_caps_get_size (clean_filter); ++i) {
764       GstStructure *s;
765
766       s = gst_caps_get_structure (clean_filter, i);
767       gst_structure_remove_field (s, "interlace-mode");
768     }
769   }
770
771   tcaps = gst_pad_get_pad_template_caps (otherpad);
772   othercaps = gst_pad_peer_query_caps (otherpad, clean_filter);
773   if (othercaps) {
774     icaps = gst_caps_intersect (othercaps, tcaps);
775     gst_caps_unref (othercaps);
776     gst_caps_unref (tcaps);
777   } else {
778     icaps = tcaps;
779   }
780
781   if (clean_filter) {
782     othercaps = gst_caps_intersect (icaps, clean_filter);
783     gst_caps_unref (icaps);
784     icaps = othercaps;
785   }
786
787   icaps = gst_caps_make_writable (icaps);
788   mode = interlace_mode_from_pattern (interlace);
789
790   if (pad == interlace->srcpad) {
791     /* Set interlace-mode to what the element will produce, so either
792      * mixed/interleaved or alternate if the caps feature is present. */
793     gst_caps_set_simple (icaps, "interlace-mode", G_TYPE_STRING, mode, NULL);
794     icaps = gst_caps_merge (icaps, dup_caps_with_alternate (icaps));
795   } else {
796     GstCaps *interlaced, *alternate;
797
798     /* Sink pad is supposed to receive a progressive stream so remove the
799      * Interlaced feature and set interlace-mode=progressive */
800     for (i = 0; i < gst_caps_get_size (icaps); ++i) {
801       GstCapsFeatures *features;
802
803       features = gst_caps_get_features (icaps, i);
804       gst_caps_features_remove (features, GST_CAPS_FEATURE_FORMAT_INTERLACED);
805     }
806
807     gst_caps_set_simple (icaps, "interlace-mode", G_TYPE_STRING, "progressive",
808         NULL);
809
810     /* Now add variants of the same caps with the interlace-mode and Interlaced
811      * caps so we can operate in passthrough if needed. */
812     interlaced = gst_caps_copy (icaps);
813     gst_caps_set_simple (interlaced, "interlace-mode", G_TYPE_STRING, mode,
814         NULL);
815     alternate = dup_caps_with_alternate (icaps);
816
817     icaps = gst_caps_merge (icaps, interlaced);
818     icaps = gst_caps_merge (icaps, alternate);
819   }
820
821   icaps =
822       gst_interlace_caps_double_framerate (icaps, (pad == interlace->srcpad));
823
824   if (clean_filter)
825     gst_caps_unref (clean_filter);
826
827   GST_DEBUG_OBJECT (pad, "caps: %" GST_PTR_FORMAT, icaps);
828   return icaps;
829 }
830
831 static gboolean
832 gst_interlace_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
833 {
834   gboolean ret;
835   GstInterlace *interlace;
836
837   interlace = GST_INTERLACE (parent);
838
839   switch (GST_QUERY_TYPE (query)) {
840     case GST_QUERY_CAPS:
841     {
842       GstCaps *filter, *caps;
843
844       gst_query_parse_caps (query, &filter);
845       caps = gst_interlace_getcaps (pad, interlace, filter);
846       gst_query_set_caps_result (query, caps);
847       gst_caps_unref (caps);
848       ret = TRUE;
849       break;
850     }
851     default:
852       ret = gst_pad_query_default (pad, parent, query);
853       break;
854   }
855   return ret;
856 }
857
858 static gboolean
859 gst_interlace_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
860 {
861   gboolean ret;
862   GstInterlace *interlace;
863
864   interlace = GST_INTERLACE (parent);
865
866   switch (GST_QUERY_TYPE (query)) {
867     case GST_QUERY_CAPS:
868     {
869       GstCaps *filter, *caps;
870
871       gst_query_parse_caps (query, &filter);
872       caps = gst_interlace_getcaps (pad, interlace, filter);
873       gst_query_set_caps_result (query, caps);
874       gst_caps_unref (caps);
875       ret = TRUE;
876       break;
877     }
878     default:
879       ret = gst_pad_query_default (pad, parent, query);
880       break;
881   }
882   return ret;
883 }
884
885 static void
886 copy_fields (GstInterlace * interlace, GstBuffer * dest, GstBuffer * src,
887     int field_index)
888 {
889   GstVideoInfo *info = &interlace->info;
890   gint i, j, n_planes;
891   guint8 *d, *s;
892   GstVideoFrame dframe, sframe;
893
894   if (!gst_video_frame_map (&dframe, info, dest, GST_MAP_WRITE))
895     goto dest_map_failed;
896
897   if (!gst_video_frame_map (&sframe, info, src, GST_MAP_READ))
898     goto src_map_failed;
899
900   n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
901
902   for (i = 0; i < n_planes; i++) {
903     gint cheight, cwidth;
904     gint ss, ds;
905
906     d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
907     s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
908
909     ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
910     ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
911
912     d += field_index * ds;
913     s += field_index * ss;
914
915     cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&dframe, i);
916     cwidth = MIN (ABS (ss), ABS (ds));
917
918     for (j = field_index; j < cheight; j += 2) {
919       memcpy (d, s, cwidth);
920       d += ds * 2;
921       s += ss * 2;
922     }
923   }
924
925   gst_video_frame_unmap (&dframe);
926   gst_video_frame_unmap (&sframe);
927   return;
928
929 dest_map_failed:
930   {
931     GST_ERROR_OBJECT (interlace, "failed to map dest");
932     return;
933   }
934 src_map_failed:
935   {
936     GST_ERROR_OBJECT (interlace, "failed to map src");
937     gst_video_frame_unmap (&dframe);
938     return;
939   }
940 }
941
942 static GstBuffer *
943 copy_field (GstInterlace * interlace, GstBuffer * src, int field_index)
944 {
945   gint i, j, n_planes;
946   GstVideoFrame dframe, sframe;
947   GstBuffer *dest;
948
949   dest =
950       gst_buffer_new_allocate (NULL, GST_VIDEO_INFO_SIZE (&interlace->out_info),
951       NULL);
952
953   if (!gst_video_frame_map (&dframe, &interlace->out_info, dest, GST_MAP_WRITE))
954     goto dest_map_failed;
955
956   if (!gst_video_frame_map (&sframe, &interlace->info, src, GST_MAP_READ))
957     goto src_map_failed;
958
959   n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
960
961   for (i = 0; i < n_planes; i++) {
962     guint8 *d, *s;
963     gint cheight, cwidth;
964     gint ss, ds;
965
966     d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
967     s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
968
969     ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
970     ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
971
972     cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&sframe, i);
973     cwidth = MIN (ABS (ss), ABS (ds));
974
975     for (j = field_index; j < cheight; j += 2) {
976       memcpy (d, s, cwidth);
977       d += ds;
978       s += ss * 2;
979     }
980   }
981
982   gst_video_frame_unmap (&dframe);
983   gst_video_frame_unmap (&sframe);
984   return dest;
985 dest_map_failed:
986   {
987     GST_ELEMENT_ERROR (interlace, CORE, FAILED, ("Failed to write map buffer"),
988         ("Failed to map dest buffer for field %d", field_index));
989     gst_buffer_unref (dest);
990     return NULL;
991   }
992 src_map_failed:
993   {
994     GST_ELEMENT_ERROR (interlace, CORE, FAILED, ("Failed to read map buffer"),
995         ("Failed to map source buffer for field %d", field_index));
996     gst_buffer_unref (dest);
997     gst_video_frame_unmap (&dframe);
998     return NULL;
999   }
1000 }
1001
1002 static GstFlowReturn
1003 gst_interlace_push_buffer (GstInterlace * interlace, GstBuffer * buffer)
1004 {
1005   GST_DEBUG_OBJECT (interlace, "output timestamp %" GST_TIME_FORMAT
1006       " duration %" GST_TIME_FORMAT " flags %04x %s %s %s",
1007       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1008       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1009       GST_BUFFER_FLAGS (buffer),
1010       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" :
1011       "",
1012       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" :
1013       "",
1014       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ?
1015       "onefield" : "");
1016
1017   return gst_pad_push (interlace->srcpad, buffer);
1018 }
1019
1020 static GstFlowReturn
1021 gst_interlace_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1022 {
1023   GstInterlace *interlace = GST_INTERLACE (parent);
1024   GstFlowReturn ret = GST_FLOW_OK;
1025   gint num_fields = 0;
1026   guint current_fields;
1027   const PulldownFormat *format;
1028   GstClockTime timestamp;
1029   gboolean alternate;
1030
1031   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1032
1033   GST_DEBUG ("Received buffer at %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
1034
1035   GST_DEBUG ("duration %" GST_TIME_FORMAT " flags %04x %s %s %s",
1036       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1037       GST_BUFFER_FLAGS (buffer),
1038       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" : "",
1039       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" : "",
1040       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ? "onefield"
1041       : "");
1042
1043   if (interlace->passthrough) {
1044     return gst_pad_push (interlace->srcpad, buffer);
1045   }
1046
1047   if (GST_BUFFER_FLAGS (buffer) & GST_BUFFER_FLAG_DISCONT) {
1048     GST_DEBUG ("discont");
1049
1050     if (interlace->stored_frame) {
1051       gst_buffer_unref (interlace->stored_frame);
1052     }
1053     interlace->stored_frame = NULL;
1054     interlace->stored_fields = 0;
1055
1056     if (interlace->top_field_first) {
1057       interlace->field_index = 0;
1058     } else {
1059       interlace->field_index = 1;
1060     }
1061   }
1062
1063   if (interlace->timebase == GST_CLOCK_TIME_NONE) {
1064     /* get the initial ts */
1065     interlace->timebase = timestamp;
1066   }
1067
1068   format = &formats[interlace->pattern];
1069
1070   if (interlace->stored_fields == 0
1071       && interlace->phase_index == interlace->pattern_offset
1072       && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1073     interlace->timebase = timestamp;
1074     interlace->fields_since_timebase = 0;
1075   }
1076
1077   current_fields = format->n_fields[interlace->phase_index];
1078   /* increment the phase index */
1079   interlace->phase_index++;
1080   g_assert (interlace->phase_index < G_N_ELEMENTS (format->n_fields));
1081   if (!format->n_fields[interlace->phase_index]) {
1082     interlace->phase_index = 0;
1083   }
1084
1085   GST_DEBUG ("incoming buffer assigned %d fields", current_fields);
1086
1087   alternate =
1088       GST_VIDEO_INFO_INTERLACE_MODE (&interlace->out_info) ==
1089       GST_VIDEO_INTERLACE_MODE_ALTERNATE;
1090
1091   num_fields = interlace->stored_fields + current_fields;
1092   while (num_fields >= 2) {
1093     GstBuffer *output_buffer, *output_buffer2 = NULL;
1094     guint n_output_fields;
1095     gboolean interlaced = FALSE;
1096
1097     GST_DEBUG ("have %d fields, %d current, %d stored",
1098         num_fields, current_fields, interlace->stored_fields);
1099
1100     if (interlace->stored_fields > 0) {
1101       GST_DEBUG ("1 field from stored, 1 from current");
1102
1103       if (alternate) {
1104         /* take the first field from the stored frame */
1105         output_buffer = copy_field (interlace, interlace->stored_frame,
1106             interlace->field_index);
1107         if (!output_buffer)
1108           return GST_FLOW_ERROR;
1109         /* take the second field from the incoming buffer */
1110         output_buffer2 = copy_field (interlace, buffer,
1111             interlace->field_index ^ 1);
1112         if (!output_buffer2)
1113           return GST_FLOW_ERROR;
1114       } else {
1115         output_buffer = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer));
1116         /* take the first field from the stored frame */
1117         copy_fields (interlace, output_buffer, interlace->stored_frame,
1118             interlace->field_index);
1119         /* take the second field from the incoming buffer */
1120         copy_fields (interlace, output_buffer, buffer,
1121             interlace->field_index ^ 1);
1122       }
1123
1124       interlace->stored_fields--;
1125       current_fields--;
1126       n_output_fields = 2;
1127       interlaced = TRUE;
1128     } else {
1129       if (alternate) {
1130         output_buffer = copy_field (interlace, buffer, interlace->field_index);
1131         if (!output_buffer)
1132           return GST_FLOW_ERROR;
1133         output_buffer2 =
1134             copy_field (interlace, buffer, interlace->field_index ^ 1);
1135         if (!output_buffer2)
1136           return GST_FLOW_ERROR;
1137       } else {
1138         output_buffer = gst_buffer_copy (buffer);
1139       }
1140
1141       if (num_fields >= 3 && interlace->allow_rff) {
1142         GST_DEBUG ("3 fields from current");
1143         /* take both fields from incoming buffer */
1144         current_fields -= 3;
1145         n_output_fields = 3;
1146       } else {
1147         GST_DEBUG ("2 fields from current");
1148         /* take both buffers from incoming buffer */
1149         current_fields -= 2;
1150         n_output_fields = 2;
1151       }
1152     }
1153     num_fields -= n_output_fields;
1154
1155     if (!alternate) {
1156       g_assert (!output_buffer2);
1157       gst_interlace_decorate_buffer (interlace, output_buffer, n_output_fields,
1158           interlaced);
1159     } else {
1160       g_assert (output_buffer2);
1161       gst_interlace_decorate_buffer_ts (interlace, output_buffer,
1162           n_output_fields);
1163
1164       /* Both fields share the same ts */
1165       GST_BUFFER_PTS (output_buffer2) = GST_BUFFER_PTS (output_buffer);
1166       GST_BUFFER_DTS (output_buffer2) = GST_BUFFER_DTS (output_buffer);
1167       GST_BUFFER_DURATION (output_buffer2) =
1168           GST_BUFFER_DURATION (output_buffer);
1169
1170       if (interlace->field_index == 0) {
1171         GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1172         GST_BUFFER_FLAG_SET (output_buffer2,
1173             GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1174       } else {
1175         GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1176         GST_BUFFER_FLAG_SET (output_buffer2, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1177       }
1178
1179       GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1180       GST_BUFFER_FLAG_SET (output_buffer2, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1181     }
1182
1183     /* Guard against overflows here. If this ever happens, resetting the phase
1184      * above would never happen because of some bugs */
1185     g_assert (interlace->fields_since_timebase <= G_MAXUINT - n_output_fields);
1186     interlace->fields_since_timebase += n_output_fields;
1187     interlace->field_index ^= (n_output_fields & 1);
1188
1189     ret = gst_interlace_push_buffer (interlace, output_buffer);
1190     if (ret != GST_FLOW_OK) {
1191       GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p", output_buffer);
1192       break;
1193     }
1194
1195     if (output_buffer2) {
1196       ret = gst_interlace_push_buffer (interlace, output_buffer2);
1197       if (ret != GST_FLOW_OK) {
1198         GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p",
1199             output_buffer2);
1200         break;
1201       }
1202     }
1203   }
1204
1205   GST_DEBUG ("done.  %d fields remaining", current_fields);
1206
1207   if (interlace->stored_frame) {
1208     gst_buffer_unref (interlace->stored_frame);
1209     interlace->stored_frame = NULL;
1210     interlace->stored_fields = 0;
1211   }
1212
1213   if (current_fields > 0) {
1214     interlace->stored_frame = buffer;
1215     interlace->stored_fields = current_fields;
1216   } else {
1217     gst_buffer_unref (buffer);
1218   }
1219   return ret;
1220 }
1221
1222 static void
1223 gst_interlace_set_property (GObject * object,
1224     guint prop_id, const GValue * value, GParamSpec * pspec)
1225 {
1226   GstInterlace *interlace = GST_INTERLACE (object);
1227
1228   switch (prop_id) {
1229     case PROP_TOP_FIELD_FIRST:
1230       interlace->top_field_first = g_value_get_boolean (value);
1231       break;
1232     case PROP_PATTERN:
1233       interlace->pattern = g_value_get_enum (value);
1234       break;
1235     case PROP_PATTERN_OFFSET:
1236       interlace->pattern_offset = g_value_get_uint (value);
1237       break;
1238     case PROP_ALLOW_RFF:
1239       interlace->allow_rff = g_value_get_boolean (value);
1240       break;
1241     default:
1242       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1243       break;
1244   }
1245 }
1246
1247 static void
1248 gst_interlace_get_property (GObject * object,
1249     guint prop_id, GValue * value, GParamSpec * pspec)
1250 {
1251   GstInterlace *interlace = GST_INTERLACE (object);
1252
1253   switch (prop_id) {
1254     case PROP_TOP_FIELD_FIRST:
1255       g_value_set_boolean (value, interlace->top_field_first);
1256       break;
1257     case PROP_PATTERN:
1258       g_value_set_enum (value, interlace->pattern);
1259       break;
1260     case PROP_PATTERN_OFFSET:
1261       g_value_set_uint (value, interlace->pattern_offset);
1262       break;
1263     case PROP_ALLOW_RFF:
1264       g_value_set_boolean (value, interlace->allow_rff);
1265       break;
1266     default:
1267       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1268       break;
1269   }
1270 }
1271
1272 static GstStateChangeReturn
1273 gst_interlace_change_state (GstElement * element, GstStateChange transition)
1274 {
1275   //GstInterlace *interlace = GST_INTERLACE (element);
1276
1277   switch (transition) {
1278     case GST_STATE_CHANGE_PAUSED_TO_READY:
1279       //gst_interlace_reset (interlace);
1280       break;
1281     default:
1282       break;
1283   }
1284
1285   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1286 }
1287
1288 static gboolean
1289 plugin_init (GstPlugin * plugin)
1290 {
1291   GST_DEBUG_CATEGORY_INIT (gst_interlace_debug, "interlace", 0,
1292       "interlace element");
1293
1294   return gst_element_register (plugin, "interlace", GST_RANK_NONE,
1295       GST_TYPE_INTERLACE);
1296 }
1297
1298 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1299     GST_VERSION_MINOR,
1300     interlace,
1301     "Create an interlaced video stream",
1302     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)