interlace: Only half the framerate for 1:1 field pattern
[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     if (interlace->pattern == GST_INTERLACE_PATTERN_1_1) {
750       clean_filter =
751           gst_interlace_caps_double_framerate (clean_filter,
752           (pad == interlace->sinkpad));
753     } else if (interlace->pattern != GST_INTERLACE_PATTERN_2_2) {
754       GST_FIXME_OBJECT (interlace,
755           "Add calculations for telecine framerate conversions");
756       for (i = 0; i < gst_caps_get_size (clean_filter); ++i) {
757         GstStructure *s = gst_caps_get_structure (clean_filter, i);
758
759         gst_structure_remove_field (s, "framerate");
760       }
761     }
762
763     if (pad == interlace->sinkpad) {
764       /* @filter may contain the different formats supported upstream.
765        * Those will be used to filter the src pad caps as this element
766        * is not supposed to do any video format conversion.
767        * Add a variant of the filter with the Interlaced feature as we want
768        * to be able to negotiate it if needed.
769        */
770       gst_caps_append (clean_filter, dup_caps_with_alternate (clean_filter));
771     }
772
773     for (i = 0; i < gst_caps_get_size (clean_filter); ++i) {
774       GstStructure *s;
775
776       s = gst_caps_get_structure (clean_filter, i);
777       gst_structure_remove_field (s, "interlace-mode");
778     }
779   }
780
781   tcaps = gst_pad_get_pad_template_caps (otherpad);
782   othercaps = gst_pad_peer_query_caps (otherpad, clean_filter);
783   if (othercaps) {
784     icaps = gst_caps_intersect (othercaps, tcaps);
785     gst_caps_unref (othercaps);
786     gst_caps_unref (tcaps);
787   } else {
788     icaps = tcaps;
789   }
790
791   if (clean_filter) {
792     othercaps = gst_caps_intersect (icaps, clean_filter);
793     gst_caps_unref (icaps);
794     icaps = othercaps;
795   }
796
797   icaps = gst_caps_make_writable (icaps);
798   mode = interlace_mode_from_pattern (interlace);
799
800   if (pad == interlace->srcpad) {
801     /* Set interlace-mode to what the element will produce, so either
802      * mixed/interleaved or alternate if the caps feature is present. */
803     gst_caps_set_simple (icaps, "interlace-mode", G_TYPE_STRING, mode, NULL);
804     icaps = gst_caps_merge (icaps, dup_caps_with_alternate (icaps));
805   } else {
806     GstCaps *interlaced, *alternate;
807
808     /* Sink pad is supposed to receive a progressive stream so remove the
809      * Interlaced feature and set interlace-mode=progressive */
810     for (i = 0; i < gst_caps_get_size (icaps); ++i) {
811       GstCapsFeatures *features;
812
813       features = gst_caps_get_features (icaps, i);
814       gst_caps_features_remove (features, GST_CAPS_FEATURE_FORMAT_INTERLACED);
815     }
816
817     gst_caps_set_simple (icaps, "interlace-mode", G_TYPE_STRING, "progressive",
818         NULL);
819
820     /* Now add variants of the same caps with the interlace-mode and Interlaced
821      * caps so we can operate in passthrough if needed. */
822     interlaced = gst_caps_copy (icaps);
823     gst_caps_set_simple (interlaced, "interlace-mode", G_TYPE_STRING, mode,
824         NULL);
825     alternate = dup_caps_with_alternate (icaps);
826
827     icaps = gst_caps_merge (icaps, interlaced);
828     icaps = gst_caps_merge (icaps, alternate);
829   }
830
831   if (interlace->pattern == GST_INTERLACE_PATTERN_1_1) {
832     icaps =
833         gst_interlace_caps_double_framerate (icaps, (pad == interlace->srcpad));
834   } else if (interlace->pattern != GST_INTERLACE_PATTERN_2_2) {
835     GST_FIXME_OBJECT (interlace,
836         "Add calculations for telecine framerate conversions");
837     for (i = 0; i < gst_caps_get_size (icaps); ++i) {
838       GstStructure *s = gst_caps_get_structure (icaps, i);
839
840       gst_structure_remove_field (s, "framerate");
841     }
842   }
843
844   if (clean_filter)
845     gst_caps_unref (clean_filter);
846
847   GST_DEBUG_OBJECT (pad, "caps: %" GST_PTR_FORMAT, icaps);
848   return icaps;
849 }
850
851 static gboolean
852 gst_interlace_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
853 {
854   gboolean ret;
855   GstInterlace *interlace;
856
857   interlace = GST_INTERLACE (parent);
858
859   switch (GST_QUERY_TYPE (query)) {
860     case GST_QUERY_CAPS:
861     {
862       GstCaps *filter, *caps;
863
864       gst_query_parse_caps (query, &filter);
865       caps = gst_interlace_getcaps (pad, interlace, filter);
866       gst_query_set_caps_result (query, caps);
867       gst_caps_unref (caps);
868       ret = TRUE;
869       break;
870     }
871     default:
872       ret = gst_pad_query_default (pad, parent, query);
873       break;
874   }
875   return ret;
876 }
877
878 static gboolean
879 gst_interlace_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
880 {
881   gboolean ret;
882   GstInterlace *interlace;
883
884   interlace = GST_INTERLACE (parent);
885
886   switch (GST_QUERY_TYPE (query)) {
887     case GST_QUERY_CAPS:
888     {
889       GstCaps *filter, *caps;
890
891       gst_query_parse_caps (query, &filter);
892       caps = gst_interlace_getcaps (pad, interlace, filter);
893       gst_query_set_caps_result (query, caps);
894       gst_caps_unref (caps);
895       ret = TRUE;
896       break;
897     }
898     default:
899       ret = gst_pad_query_default (pad, parent, query);
900       break;
901   }
902   return ret;
903 }
904
905 static void
906 copy_fields (GstInterlace * interlace, GstBuffer * dest, GstBuffer * src,
907     int field_index)
908 {
909   GstVideoInfo *info = &interlace->info;
910   gint i, j, n_planes;
911   guint8 *d, *s;
912   GstVideoFrame dframe, sframe;
913
914   if (!gst_video_frame_map (&dframe, info, dest, GST_MAP_WRITE))
915     goto dest_map_failed;
916
917   if (!gst_video_frame_map (&sframe, info, src, GST_MAP_READ))
918     goto src_map_failed;
919
920   n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
921
922   for (i = 0; i < n_planes; i++) {
923     gint cheight, cwidth;
924     gint ss, ds;
925
926     d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
927     s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
928
929     ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
930     ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
931
932     d += field_index * ds;
933     s += field_index * ss;
934
935     cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&dframe, i);
936     cwidth = MIN (ABS (ss), ABS (ds));
937
938     for (j = field_index; j < cheight; j += 2) {
939       memcpy (d, s, cwidth);
940       d += ds * 2;
941       s += ss * 2;
942     }
943   }
944
945   gst_video_frame_unmap (&dframe);
946   gst_video_frame_unmap (&sframe);
947   return;
948
949 dest_map_failed:
950   {
951     GST_ERROR_OBJECT (interlace, "failed to map dest");
952     return;
953   }
954 src_map_failed:
955   {
956     GST_ERROR_OBJECT (interlace, "failed to map src");
957     gst_video_frame_unmap (&dframe);
958     return;
959   }
960 }
961
962 static GstBuffer *
963 copy_field (GstInterlace * interlace, GstBuffer * src, int field_index)
964 {
965   gint i, j, n_planes;
966   GstVideoFrame dframe, sframe;
967   GstBuffer *dest;
968
969   dest =
970       gst_buffer_new_allocate (NULL, GST_VIDEO_INFO_SIZE (&interlace->out_info),
971       NULL);
972
973   if (!gst_video_frame_map (&dframe, &interlace->out_info, dest, GST_MAP_WRITE))
974     goto dest_map_failed;
975
976   if (!gst_video_frame_map (&sframe, &interlace->info, src, GST_MAP_READ))
977     goto src_map_failed;
978
979   n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
980
981   for (i = 0; i < n_planes; i++) {
982     guint8 *d, *s;
983     gint cheight, cwidth;
984     gint ss, ds;
985
986     d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
987     s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
988
989     ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
990     ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
991
992     cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&sframe, i);
993     cwidth = MIN (ABS (ss), ABS (ds));
994
995     for (j = field_index; j < cheight; j += 2) {
996       memcpy (d, s, cwidth);
997       d += ds;
998       s += ss * 2;
999     }
1000   }
1001
1002   gst_video_frame_unmap (&dframe);
1003   gst_video_frame_unmap (&sframe);
1004   return dest;
1005 dest_map_failed:
1006   {
1007     GST_ELEMENT_ERROR (interlace, CORE, FAILED, ("Failed to write map buffer"),
1008         ("Failed to map dest buffer for field %d", field_index));
1009     gst_buffer_unref (dest);
1010     return NULL;
1011   }
1012 src_map_failed:
1013   {
1014     GST_ELEMENT_ERROR (interlace, CORE, FAILED, ("Failed to read map buffer"),
1015         ("Failed to map source buffer for field %d", field_index));
1016     gst_buffer_unref (dest);
1017     gst_video_frame_unmap (&dframe);
1018     return NULL;
1019   }
1020 }
1021
1022 static GstFlowReturn
1023 gst_interlace_push_buffer (GstInterlace * interlace, GstBuffer * buffer)
1024 {
1025   GST_DEBUG_OBJECT (interlace, "output timestamp %" GST_TIME_FORMAT
1026       " duration %" GST_TIME_FORMAT " flags %04x %s %s %s",
1027       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1028       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1029       GST_BUFFER_FLAGS (buffer),
1030       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" :
1031       "",
1032       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" :
1033       "",
1034       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ?
1035       "onefield" : "");
1036
1037   return gst_pad_push (interlace->srcpad, buffer);
1038 }
1039
1040 static GstFlowReturn
1041 gst_interlace_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1042 {
1043   GstInterlace *interlace = GST_INTERLACE (parent);
1044   GstFlowReturn ret = GST_FLOW_OK;
1045   gint num_fields = 0;
1046   guint current_fields;
1047   const PulldownFormat *format;
1048   GstClockTime timestamp;
1049   gboolean alternate;
1050
1051   timestamp = GST_BUFFER_TIMESTAMP (buffer);
1052
1053   GST_DEBUG ("Received buffer at %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
1054
1055   GST_DEBUG ("duration %" GST_TIME_FORMAT " flags %04x %s %s %s",
1056       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
1057       GST_BUFFER_FLAGS (buffer),
1058       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" : "",
1059       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" : "",
1060       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ? "onefield"
1061       : "");
1062
1063   if (interlace->passthrough) {
1064     return gst_pad_push (interlace->srcpad, buffer);
1065   }
1066
1067   if (GST_BUFFER_FLAGS (buffer) & GST_BUFFER_FLAG_DISCONT) {
1068     GST_DEBUG ("discont");
1069
1070     if (interlace->stored_frame) {
1071       gst_buffer_unref (interlace->stored_frame);
1072     }
1073     interlace->stored_frame = NULL;
1074     interlace->stored_fields = 0;
1075
1076     if (interlace->top_field_first) {
1077       interlace->field_index = 0;
1078     } else {
1079       interlace->field_index = 1;
1080     }
1081   }
1082
1083   if (interlace->timebase == GST_CLOCK_TIME_NONE) {
1084     /* get the initial ts */
1085     interlace->timebase = timestamp;
1086   }
1087
1088   format = &formats[interlace->pattern];
1089
1090   if (interlace->stored_fields == 0
1091       && interlace->phase_index == interlace->pattern_offset
1092       && GST_CLOCK_TIME_IS_VALID (timestamp)) {
1093     interlace->timebase = timestamp;
1094     interlace->fields_since_timebase = 0;
1095   }
1096
1097   current_fields = format->n_fields[interlace->phase_index];
1098   /* increment the phase index */
1099   interlace->phase_index++;
1100   g_assert (interlace->phase_index < G_N_ELEMENTS (format->n_fields));
1101   if (!format->n_fields[interlace->phase_index]) {
1102     interlace->phase_index = 0;
1103   }
1104
1105   GST_DEBUG ("incoming buffer assigned %d fields", current_fields);
1106
1107   alternate =
1108       GST_VIDEO_INFO_INTERLACE_MODE (&interlace->out_info) ==
1109       GST_VIDEO_INTERLACE_MODE_ALTERNATE;
1110
1111   num_fields = interlace->stored_fields + current_fields;
1112   while (num_fields >= 2) {
1113     GstBuffer *output_buffer, *output_buffer2 = NULL;
1114     guint n_output_fields;
1115     gboolean interlaced = FALSE;
1116
1117     GST_DEBUG ("have %d fields, %d current, %d stored",
1118         num_fields, current_fields, interlace->stored_fields);
1119
1120     if (interlace->stored_fields > 0) {
1121       GST_DEBUG ("1 field from stored, 1 from current");
1122
1123       if (alternate) {
1124         /* take the first field from the stored frame */
1125         output_buffer = copy_field (interlace, interlace->stored_frame,
1126             interlace->field_index);
1127         if (!output_buffer)
1128           return GST_FLOW_ERROR;
1129         /* take the second field from the incoming buffer */
1130         output_buffer2 = copy_field (interlace, buffer,
1131             interlace->field_index ^ 1);
1132         if (!output_buffer2)
1133           return GST_FLOW_ERROR;
1134       } else {
1135         output_buffer = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer));
1136         /* take the first field from the stored frame */
1137         copy_fields (interlace, output_buffer, interlace->stored_frame,
1138             interlace->field_index);
1139         /* take the second field from the incoming buffer */
1140         copy_fields (interlace, output_buffer, buffer,
1141             interlace->field_index ^ 1);
1142       }
1143
1144       interlace->stored_fields--;
1145       current_fields--;
1146       n_output_fields = 2;
1147       interlaced = TRUE;
1148     } else {
1149       if (alternate) {
1150         output_buffer = copy_field (interlace, buffer, interlace->field_index);
1151         if (!output_buffer)
1152           return GST_FLOW_ERROR;
1153         output_buffer2 =
1154             copy_field (interlace, buffer, interlace->field_index ^ 1);
1155         if (!output_buffer2)
1156           return GST_FLOW_ERROR;
1157       } else {
1158         output_buffer = gst_buffer_copy (buffer);
1159       }
1160
1161       if (num_fields >= 3 && interlace->allow_rff) {
1162         GST_DEBUG ("3 fields from current");
1163         /* take both fields from incoming buffer */
1164         current_fields -= 3;
1165         n_output_fields = 3;
1166       } else {
1167         GST_DEBUG ("2 fields from current");
1168         /* take both buffers from incoming buffer */
1169         current_fields -= 2;
1170         n_output_fields = 2;
1171       }
1172     }
1173     num_fields -= n_output_fields;
1174
1175     if (!alternate) {
1176       g_assert (!output_buffer2);
1177       gst_interlace_decorate_buffer (interlace, output_buffer, n_output_fields,
1178           interlaced);
1179     } else {
1180       g_assert (output_buffer2);
1181       gst_interlace_decorate_buffer_ts (interlace, output_buffer,
1182           n_output_fields);
1183
1184       /* Both fields share the same ts */
1185       GST_BUFFER_PTS (output_buffer2) = GST_BUFFER_PTS (output_buffer);
1186       GST_BUFFER_DTS (output_buffer2) = GST_BUFFER_DTS (output_buffer);
1187       GST_BUFFER_DURATION (output_buffer2) =
1188           GST_BUFFER_DURATION (output_buffer);
1189
1190       if (interlace->field_index == 0) {
1191         GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1192         GST_BUFFER_FLAG_SET (output_buffer2,
1193             GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1194       } else {
1195         GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
1196         GST_BUFFER_FLAG_SET (output_buffer2, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
1197       }
1198
1199       GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1200       GST_BUFFER_FLAG_SET (output_buffer2, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1201     }
1202
1203     /* Guard against overflows here. If this ever happens, resetting the phase
1204      * above would never happen because of some bugs */
1205     g_assert (interlace->fields_since_timebase <= G_MAXUINT - n_output_fields);
1206     interlace->fields_since_timebase += n_output_fields;
1207     interlace->field_index ^= (n_output_fields & 1);
1208
1209     ret = gst_interlace_push_buffer (interlace, output_buffer);
1210     if (ret != GST_FLOW_OK) {
1211       GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p", output_buffer);
1212       break;
1213     }
1214
1215     if (output_buffer2) {
1216       ret = gst_interlace_push_buffer (interlace, output_buffer2);
1217       if (ret != GST_FLOW_OK) {
1218         GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p",
1219             output_buffer2);
1220         break;
1221       }
1222     }
1223   }
1224
1225   GST_DEBUG ("done.  %d fields remaining", current_fields);
1226
1227   if (interlace->stored_frame) {
1228     gst_buffer_unref (interlace->stored_frame);
1229     interlace->stored_frame = NULL;
1230     interlace->stored_fields = 0;
1231   }
1232
1233   if (current_fields > 0) {
1234     interlace->stored_frame = buffer;
1235     interlace->stored_fields = current_fields;
1236   } else {
1237     gst_buffer_unref (buffer);
1238   }
1239   return ret;
1240 }
1241
1242 static void
1243 gst_interlace_set_property (GObject * object,
1244     guint prop_id, const GValue * value, GParamSpec * pspec)
1245 {
1246   GstInterlace *interlace = GST_INTERLACE (object);
1247
1248   switch (prop_id) {
1249     case PROP_TOP_FIELD_FIRST:
1250       interlace->top_field_first = g_value_get_boolean (value);
1251       break;
1252     case PROP_PATTERN:
1253       interlace->pattern = g_value_get_enum (value);
1254       break;
1255     case PROP_PATTERN_OFFSET:
1256       interlace->pattern_offset = g_value_get_uint (value);
1257       break;
1258     case PROP_ALLOW_RFF:
1259       interlace->allow_rff = g_value_get_boolean (value);
1260       break;
1261     default:
1262       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1263       break;
1264   }
1265 }
1266
1267 static void
1268 gst_interlace_get_property (GObject * object,
1269     guint prop_id, GValue * value, GParamSpec * pspec)
1270 {
1271   GstInterlace *interlace = GST_INTERLACE (object);
1272
1273   switch (prop_id) {
1274     case PROP_TOP_FIELD_FIRST:
1275       g_value_set_boolean (value, interlace->top_field_first);
1276       break;
1277     case PROP_PATTERN:
1278       g_value_set_enum (value, interlace->pattern);
1279       break;
1280     case PROP_PATTERN_OFFSET:
1281       g_value_set_uint (value, interlace->pattern_offset);
1282       break;
1283     case PROP_ALLOW_RFF:
1284       g_value_set_boolean (value, interlace->allow_rff);
1285       break;
1286     default:
1287       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1288       break;
1289   }
1290 }
1291
1292 static GstStateChangeReturn
1293 gst_interlace_change_state (GstElement * element, GstStateChange transition)
1294 {
1295   //GstInterlace *interlace = GST_INTERLACE (element);
1296
1297   switch (transition) {
1298     case GST_STATE_CHANGE_PAUSED_TO_READY:
1299       //gst_interlace_reset (interlace);
1300       break;
1301     default:
1302       break;
1303   }
1304
1305   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1306 }
1307
1308 static gboolean
1309 plugin_init (GstPlugin * plugin)
1310 {
1311   GST_DEBUG_CATEGORY_INIT (gst_interlace_debug, "interlace", 0,
1312       "interlace element");
1313
1314   return gst_element_register (plugin, "interlace", GST_RANK_NONE,
1315       GST_TYPE_INTERLACE);
1316 }
1317
1318 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1319     GST_VERSION_MINOR,
1320     interlace,
1321     "Create an interlaced video stream",
1322     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)