• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   int src_fps_n;
98   int src_fps_d;
99 
100   GstBuffer *stored_frame;
101   gint stored_fields;
102   gint phase_index;
103   int field_index;              /* index of the next field to push, 0=top 1=bottom */
104   GstClockTime timebase;
105   int fields_since_timebase;
106   guint pattern_offset;         /* initial offset into the pattern */
107   gboolean passthrough;
108 };
109 
110 struct _GstInterlaceClass
111 {
112   GstElementClass element_class;
113 };
114 
115 enum
116 {
117   PROP_0,
118   PROP_TOP_FIELD_FIRST,
119   PROP_PATTERN,
120   PROP_PATTERN_OFFSET,
121   PROP_ALLOW_RFF
122 };
123 
124 typedef enum
125 {
126   GST_INTERLACE_PATTERN_1_1,
127   GST_INTERLACE_PATTERN_2_2,
128   GST_INTERLACE_PATTERN_2_3,
129   GST_INTERLACE_PATTERN_2_3_3_2,
130   GST_INTERLACE_PATTERN_EURO,
131   GST_INTERLACE_PATTERN_3_4R3,
132   GST_INTERLACE_PATTERN_3R7_4,
133   GST_INTERLACE_PATTERN_3_3_4,
134   GST_INTERLACE_PATTERN_3_3,
135   GST_INTERLACE_PATTERN_3_2R4,
136   GST_INTERLACE_PATTERN_1_2R4,
137 } GstInterlacePattern;
138 
139 #define GST_INTERLACE_PATTERN (gst_interlace_pattern_get_type ())
140 static GType
gst_interlace_pattern_get_type(void)141 gst_interlace_pattern_get_type (void)
142 {
143   static GType interlace_pattern_type = 0;
144   static const GEnumValue pattern_types[] = {
145     {GST_INTERLACE_PATTERN_1_1, "1:1 (e.g. 60p -> 60i)", "1:1"},
146     {GST_INTERLACE_PATTERN_2_2, "2:2 (e.g. 30p -> 60i)", "2:2"},
147     {GST_INTERLACE_PATTERN_2_3, "2:3 (e.g. 24p -> 60i telecine)", "2:3"},
148     {GST_INTERLACE_PATTERN_2_3_3_2, "2:3:3:2 (e.g. 24p -> 60i telecine)",
149         "2:3:3:2"},
150     {GST_INTERLACE_PATTERN_EURO, "Euro 2-11:3 (e.g. 24p -> 50i telecine)",
151         "2-11:3"},
152     {GST_INTERLACE_PATTERN_3_4R3, "3:4-3 (e.g. 16p -> 60i telecine)", "3:4-3"},
153     {GST_INTERLACE_PATTERN_3R7_4, "3-7:4 (e.g. 16p -> 50i telecine)", "3-7:4"},
154     {GST_INTERLACE_PATTERN_3_3_4, "3:3:4 (e.g. 18p -> 60i telecine)", "3:3:4"},
155     {GST_INTERLACE_PATTERN_3_3, "3:3 (e.g. 20p -> 60i telecine)", "3:3"},
156     {GST_INTERLACE_PATTERN_3_2R4, "3:2-4 (e.g. 27.5p -> 60i telecine)",
157         "3:2-4"},
158     {GST_INTERLACE_PATTERN_1_2R4, "1:2-4 (e.g. 27.5p -> 50i telecine)",
159         "1:2-4"},
160     {0, NULL, NULL}
161   };
162 
163   if (!interlace_pattern_type) {
164     interlace_pattern_type =
165         g_enum_register_static ("GstInterlacePattern", pattern_types);
166   }
167 
168   return interlace_pattern_type;
169 }
170 
171 static GstStaticPadTemplate gst_interlace_src_template =
172 GST_STATIC_PAD_TEMPLATE ("src",
173     GST_PAD_SRC,
174     GST_PAD_ALWAYS,
175     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
176         ("{AYUV,YUY2,UYVY,I420,YV12,Y42B,Y444,NV12,NV21}")
177         ",interlace-mode={interleaved,mixed}")
178     );
179 
180 static GstStaticPadTemplate gst_interlace_sink_template =
181 GST_STATIC_PAD_TEMPLATE ("sink",
182     GST_PAD_SINK,
183     GST_PAD_ALWAYS,
184     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
185         ("{AYUV,YUY2,UYVY,I420,YV12,Y42B,Y444,NV12,NV21}")
186     )
187     );
188 
189 GType gst_interlace_get_type (void);
190 static void gst_interlace_finalize (GObject * obj);
191 
192 static void gst_interlace_set_property (GObject * object,
193     guint prop_id, const GValue * value, GParamSpec * pspec);
194 static void gst_interlace_get_property (GObject * object,
195     guint prop_id, GValue * value, GParamSpec * pspec);
196 
197 static gboolean gst_interlace_sink_event (GstPad * pad, GstObject * parent,
198     GstEvent * event);
199 static gboolean gst_interlace_sink_query (GstPad * pad, GstObject * parent,
200     GstQuery * query);
201 static GstFlowReturn gst_interlace_chain (GstPad * pad, GstObject * parent,
202     GstBuffer * buffer);
203 
204 static gboolean gst_interlace_src_query (GstPad * pad, GstObject * parent,
205     GstQuery * query);
206 
207 static GstStateChangeReturn gst_interlace_change_state (GstElement * element,
208     GstStateChange transition);
209 
210 #define gst_interlace_parent_class parent_class
211 G_DEFINE_TYPE (GstInterlace, gst_interlace, GST_TYPE_ELEMENT);
212 
213 static void
gst_interlace_class_init(GstInterlaceClass * klass)214 gst_interlace_class_init (GstInterlaceClass * klass)
215 {
216   GObjectClass *object_class = G_OBJECT_CLASS (klass);
217   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
218 
219   parent_class = g_type_class_peek_parent (klass);
220 
221   object_class->set_property = gst_interlace_set_property;
222   object_class->get_property = gst_interlace_get_property;
223   object_class->finalize = gst_interlace_finalize;
224 
225   g_object_class_install_property (object_class, PROP_TOP_FIELD_FIRST,
226       g_param_spec_boolean ("top-field-first", "top field first",
227           "Interlaced stream should be top field first", FALSE,
228           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 
230   g_object_class_install_property (object_class, PROP_PATTERN,
231       g_param_spec_enum ("field-pattern", "Field pattern",
232           "The output field pattern", GST_INTERLACE_PATTERN,
233           GST_INTERLACE_PATTERN_2_3,
234           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 
236   g_object_class_install_property (object_class, PROP_PATTERN_OFFSET,
237       g_param_spec_uint ("pattern-offset", "Pattern offset",
238           "The initial field pattern offset. Counts from 0.",
239           0, 12, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 
241   g_object_class_install_property (object_class, PROP_ALLOW_RFF,
242       g_param_spec_boolean ("allow-rff", "Allow Repeat-First-Field flags",
243           "Allow generation of buffers with RFF flag set, i.e., duration of 3 fields",
244           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 
246   gst_element_class_set_static_metadata (element_class,
247       "Interlace filter", "Filter/Video",
248       "Creates an interlaced video from progressive frames",
249       "David Schleef <ds@schleef.org>");
250 
251   gst_element_class_add_static_pad_template (element_class,
252       &gst_interlace_sink_template);
253   gst_element_class_add_static_pad_template (element_class,
254       &gst_interlace_src_template);
255 
256   element_class->change_state = gst_interlace_change_state;
257 }
258 
259 static void
gst_interlace_finalize(GObject * obj)260 gst_interlace_finalize (GObject * obj)
261 {
262   G_OBJECT_CLASS (parent_class)->finalize (obj);
263 }
264 
265 static void
gst_interlace_reset(GstInterlace * interlace)266 gst_interlace_reset (GstInterlace * interlace)
267 {
268   interlace->phase_index = interlace->pattern_offset;
269   interlace->timebase = GST_CLOCK_TIME_NONE;
270   interlace->field_index = 0;
271   interlace->passthrough = FALSE;
272   if (interlace->stored_frame) {
273     gst_buffer_unref (interlace->stored_frame);
274     interlace->stored_frame = NULL;
275   }
276 }
277 
278 static void
gst_interlace_init(GstInterlace * interlace)279 gst_interlace_init (GstInterlace * interlace)
280 {
281   GST_DEBUG ("gst_interlace_init");
282   interlace->sinkpad =
283       gst_pad_new_from_static_template (&gst_interlace_sink_template, "sink");
284   gst_pad_set_chain_function (interlace->sinkpad, gst_interlace_chain);
285   gst_pad_set_event_function (interlace->sinkpad, gst_interlace_sink_event);
286   gst_pad_set_query_function (interlace->sinkpad, gst_interlace_sink_query);
287   gst_element_add_pad (GST_ELEMENT (interlace), interlace->sinkpad);
288 
289   interlace->srcpad =
290       gst_pad_new_from_static_template (&gst_interlace_src_template, "src");
291   gst_pad_set_query_function (interlace->srcpad, gst_interlace_src_query);
292   gst_element_add_pad (GST_ELEMENT (interlace), interlace->srcpad);
293 
294   interlace->top_field_first = FALSE;
295   interlace->allow_rff = FALSE;
296   interlace->pattern = GST_INTERLACE_PATTERN_2_3;
297   interlace->pattern_offset = 0;
298   gst_interlace_reset (interlace);
299 }
300 
301 typedef struct _PulldownFormat PulldownFormat;
302 struct _PulldownFormat
303 {
304   const gchar *name;
305   /* ratio between outgoing field rate / 2 and incoming frame rate.
306    * I.e., 24p -> 60i is 1.25  */
307   int ratio_n, ratio_d;
308   int n_fields[13];
309 };
310 
311 static const PulldownFormat formats[] = {
312   /* 60p -> 60i or 50p -> 50i */
313   {"1:1", 1, 2, {1}},
314   /* 30p -> 60i or 25p -> 50i */
315   {"2:2", 1, 1, {2}},
316   /* 24p -> 60i telecine */
317   {"2:3", 5, 4, {2, 3,}},
318   {"2:3:3:2", 5, 4, {2, 3, 3, 2,}},
319   /* 24p -> 50i Euro pulldown */
320   {"2-11:3", 25, 24, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,}},
321   /* 16p (16000/1001) -> 60i (NTSC 30000/1001) */
322   {"3:4-3", 15, 8, {3, 4, 4, 4,}},
323   /* 16p -> 50i (PAL) */
324   {"3-7:4", 25, 16, {3, 3, 3, 3, 3, 3, 3, 4,}},
325   /* 18p to NTSC 60i */
326   {"3:3:4", 5, 3, {3, 3, 4,}},
327   /* 20p to NTSC 60i */
328   {"3:3", 3, 2, {3, 3,}},
329   /* 27.5 to NTSC 60i */
330   {"3:2-4", 11, 10, {3, 2, 2, 2, 2,}},
331   /* 27.5 to PAL 50i */
332   {"1:2-4", 9, 10, {1, 2, 2, 2, 2,}},
333 };
334 
335 static void
gst_interlace_decorate_buffer(GstInterlace * interlace,GstBuffer * buf,int n_fields,gboolean interlaced)336 gst_interlace_decorate_buffer (GstInterlace * interlace, GstBuffer * buf,
337     int n_fields, gboolean interlaced)
338 {
339   /* field duration = src_fps_d / (2 * src_fps_n) */
340   if (interlace->src_fps_n == 0) {
341     /* If we don't know the fps, we can't generate timestamps/durations */
342     GST_BUFFER_DTS (buf) = GST_CLOCK_TIME_NONE;
343     GST_BUFFER_PTS (buf) = GST_CLOCK_TIME_NONE;
344     GST_BUFFER_DURATION (buf) = GST_CLOCK_TIME_NONE;
345   } else {
346     GST_BUFFER_DTS (buf) = interlace->timebase +
347         gst_util_uint64_scale (GST_SECOND,
348         interlace->src_fps_d * interlace->fields_since_timebase,
349         interlace->src_fps_n * 2);
350     GST_BUFFER_PTS (buf) = GST_BUFFER_DTS (buf);
351     GST_BUFFER_DURATION (buf) =
352         gst_util_uint64_scale (GST_SECOND, interlace->src_fps_d * n_fields,
353         interlace->src_fps_n * 2);
354   }
355 
356   if (interlace->field_index == 0) {
357     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF);
358   }
359   if (n_fields == 3) {
360     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_RFF);
361   }
362   if (n_fields == 1) {
363     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_ONEFIELD);
364   }
365   if (interlace->pattern > GST_INTERLACE_PATTERN_2_2 && n_fields == 2
366       && interlaced) {
367     GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
368   }
369 }
370 
371 static gboolean
gst_interlace_setcaps(GstInterlace * interlace,GstCaps * caps)372 gst_interlace_setcaps (GstInterlace * interlace, GstCaps * caps)
373 {
374   gboolean ret;
375   GstVideoInfo info;
376   GstCaps *othercaps;
377   const PulldownFormat *pdformat;
378 
379   if (!gst_video_info_from_caps (&info, caps))
380     goto caps_error;
381 
382   othercaps = gst_caps_copy (caps);
383   pdformat = &formats[interlace->pattern];
384 
385   interlace->phase_index = interlace->pattern_offset;
386 
387   interlace->src_fps_n = info.fps_n * pdformat->ratio_n;
388   interlace->src_fps_d = info.fps_d * pdformat->ratio_d;
389 
390   if (interlace->pattern > GST_INTERLACE_PATTERN_2_2) {
391     gst_caps_set_simple (othercaps, "interlace-mode", G_TYPE_STRING, "mixed",
392         NULL);
393   } else {
394     gst_caps_set_simple (othercaps, "interlace-mode", G_TYPE_STRING,
395         "interleaved", NULL);
396   }
397 
398   if (gst_caps_can_intersect (caps, othercaps)) {
399     interlace->passthrough = TRUE;
400   } else {
401     if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
402       GST_ERROR_OBJECT (interlace,
403           "Caps %" GST_PTR_FORMAT " not compatible with %" GST_PTR_FORMAT, caps,
404           othercaps);
405       gst_caps_unref (othercaps);
406       goto caps_error;
407     }
408     interlace->passthrough = FALSE;
409     gst_caps_set_simple (othercaps, "framerate", GST_TYPE_FRACTION,
410         interlace->src_fps_n, interlace->src_fps_d, NULL);
411     if (interlace->pattern <= GST_INTERLACE_PATTERN_2_2) {
412       gst_caps_set_simple (othercaps, "field-order", G_TYPE_STRING,
413           interlace->top_field_first ? "top-field-first" : "bottom-field-first",
414           NULL);
415     }
416   }
417 
418   ret = gst_pad_set_caps (interlace->srcpad, othercaps);
419   gst_caps_unref (othercaps);
420 
421   interlace->info = info;
422 
423   return ret;
424 
425 caps_error:
426   {
427     GST_DEBUG_OBJECT (interlace, "error parsing caps");
428     return FALSE;
429   }
430 }
431 
432 static gboolean
gst_interlace_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)433 gst_interlace_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
434 {
435   gboolean ret;
436   GstInterlace *interlace;
437 
438   interlace = GST_INTERLACE (parent);
439 
440   switch (GST_EVENT_TYPE (event)) {
441     case GST_EVENT_FLUSH_START:
442       GST_DEBUG_OBJECT (interlace, "handling FLUSH_START");
443       ret = gst_pad_push_event (interlace->srcpad, event);
444       break;
445     case GST_EVENT_FLUSH_STOP:
446       GST_DEBUG_OBJECT (interlace, "handling FLUSH_STOP");
447       gst_interlace_reset (interlace);
448       ret = gst_pad_push_event (interlace->srcpad, event);
449       break;
450     case GST_EVENT_EOS:
451 #if 0
452       /* FIXME revive this when we output ONEFIELD and RFF buffers */
453     {
454       gint num_fields;
455       const PulldownFormat *format = &formats[interlace->pattern];
456 
457       num_fields =
458           format->n_fields[interlace->phase_index] -
459           interlace->stored_fields_pushed;
460       interlace->stored_fields_pushed = 0;
461 
462       /* on EOS we want to push as many sane frames as are left */
463       while (num_fields > 1) {
464         GstBuffer *output_buffer;
465 
466         /* make metadata writable before editing it */
467         interlace->stored_frame =
468             gst_buffer_make_metadata_writable (interlace->stored_frame);
469         num_fields -= 2;
470 
471         gst_interlace_decorate_buffer (interlace, interlace->stored_frame,
472             n_fields, FALSE);
473 
474         /* ref output_buffer/stored frame because we want to keep it for now
475          * and pushing gives away a ref */
476         output_buffer = gst_buffer_ref (interlace->stored_frame);
477         if (gst_pad_push (interlace->srcpad, output_buffer)) {
478           GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p",
479               output_buffer);
480           return FALSE;
481         }
482         output_buffer = NULL;
483 
484         if (num_fields <= 1) {
485           gst_buffer_unref (interlace->stored_frame);
486           interlace->stored_frame = NULL;
487           break;
488         }
489       }
490 
491       /* increment the phase index */
492       interlace->phase_index++;
493       if (!format->n_fields[interlace->phase_index]) {
494         interlace->phase_index = 0;
495       }
496     }
497 #endif
498 
499       ret = gst_pad_push_event (interlace->srcpad, event);
500       break;
501     case GST_EVENT_CAPS:
502     {
503       GstCaps *caps;
504 
505       gst_event_parse_caps (event, &caps);
506       ret = gst_interlace_setcaps (interlace, caps);
507       gst_event_unref (event);
508       break;
509     }
510     default:
511       ret = gst_pad_push_event (interlace->srcpad, event);
512       break;
513   }
514 
515   return ret;
516 }
517 
518 static gboolean
gst_interlace_fraction_double(gint * n_out,gint * d_out,gboolean half)519 gst_interlace_fraction_double (gint * n_out, gint * d_out, gboolean half)
520 {
521   gint n, d, gcd;
522 
523   n = *n_out;
524   d = *d_out;
525 
526   if (d == 0)
527     return FALSE;
528 
529   if (n == 0)
530     return TRUE;
531 
532   gcd = gst_util_greatest_common_divisor (n, d);
533   n /= gcd;
534   d /= gcd;
535 
536   if (half) {
537     if (G_MAXINT / 2 >= ABS (d)) {
538       d *= 2;
539     } else if (n >= 2 && n != G_MAXINT) {
540       n /= 2;
541     } else {
542       d = G_MAXINT;
543     }
544   } else {
545     if (G_MAXINT / 2 >= ABS (n)) {
546       n *= 2;
547     } else if (d >= 2 && d != G_MAXINT) {
548       d /= 2;
549     } else {
550       n = G_MAXINT;
551     }
552   }
553 
554   *n_out = n;
555   *d_out = d;
556 
557   return TRUE;
558 }
559 
560 static GstCaps *
gst_interlace_caps_double_framerate(GstCaps * caps,gboolean half)561 gst_interlace_caps_double_framerate (GstCaps * caps, gboolean half)
562 {
563   guint len;
564 
565   for (len = gst_caps_get_size (caps); len > 0; len--) {
566     GstStructure *s = gst_caps_get_structure (caps, len - 1);
567     const GValue *val;
568 
569     val = gst_structure_get_value (s, "framerate");
570     if (!val)
571       continue;
572 
573     if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION) {
574       gint n, d;
575 
576       n = gst_value_get_fraction_numerator (val);
577       d = gst_value_get_fraction_denominator (val);
578 
579       if (!gst_interlace_fraction_double (&n, &d, half)) {
580         gst_caps_remove_structure (caps, len - 1);
581         continue;
582       }
583 
584       gst_structure_set (s, "framerate", GST_TYPE_FRACTION, n, d, NULL);
585     } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
586       const GValue *min, *max;
587       GValue nrange = { 0, }, nmin = {
588       0,}, nmax = {
589       0,};
590       gint n, d;
591 
592       g_value_init (&nrange, GST_TYPE_FRACTION_RANGE);
593       g_value_init (&nmin, GST_TYPE_FRACTION);
594       g_value_init (&nmax, GST_TYPE_FRACTION);
595 
596       min = gst_value_get_fraction_range_min (val);
597       max = gst_value_get_fraction_range_max (val);
598 
599       n = gst_value_get_fraction_numerator (min);
600       d = gst_value_get_fraction_denominator (min);
601 
602       if (!gst_interlace_fraction_double (&n, &d, half)) {
603         g_value_unset (&nrange);
604         g_value_unset (&nmax);
605         g_value_unset (&nmin);
606         gst_caps_remove_structure (caps, len - 1);
607         continue;
608       }
609 
610       gst_value_set_fraction (&nmin, n, d);
611 
612       n = gst_value_get_fraction_numerator (max);
613       d = gst_value_get_fraction_denominator (max);
614 
615       if (!gst_interlace_fraction_double (&n, &d, half)) {
616         g_value_unset (&nrange);
617         g_value_unset (&nmax);
618         g_value_unset (&nmin);
619         gst_caps_remove_structure (caps, len - 1);
620         continue;
621       }
622 
623       gst_value_set_fraction (&nmax, n, d);
624       gst_value_set_fraction_range (&nrange, &nmin, &nmax);
625 
626       gst_structure_take_value (s, "framerate", &nrange);
627 
628       g_value_unset (&nmin);
629       g_value_unset (&nmax);
630     } else if (G_VALUE_TYPE (val) == GST_TYPE_LIST) {
631       const GValue *lval;
632       GValue nlist = { 0, };
633       GValue nval = { 0, };
634       gint i;
635 
636       g_value_init (&nlist, GST_TYPE_LIST);
637       for (i = gst_value_list_get_size (val); i > 0; i--) {
638         gint n, d;
639 
640         lval = gst_value_list_get_value (val, i - 1);
641 
642         if (G_VALUE_TYPE (lval) != GST_TYPE_FRACTION)
643           continue;
644 
645         n = gst_value_get_fraction_numerator (lval);
646         d = gst_value_get_fraction_denominator (lval);
647 
648         /* Double/Half the framerate but if this fails simply
649          * skip this value from the list */
650         if (!gst_interlace_fraction_double (&n, &d, half)) {
651           continue;
652         }
653 
654         g_value_init (&nval, GST_TYPE_FRACTION);
655 
656         gst_value_set_fraction (&nval, n, d);
657         gst_value_list_append_and_take_value (&nlist, &nval);
658       }
659       gst_structure_take_value (s, "framerate", &nlist);
660     }
661   }
662 
663   return caps;
664 }
665 
666 static GstCaps *
gst_interlace_getcaps(GstPad * pad,GstInterlace * interlace,GstCaps * filter)667 gst_interlace_getcaps (GstPad * pad, GstInterlace * interlace, GstCaps * filter)
668 {
669   GstPad *otherpad;
670   GstCaps *othercaps, *tcaps;
671   GstCaps *icaps;
672   GstCaps *clean_filter = NULL;
673   const char *mode;
674   guint i;
675 
676   otherpad =
677       (pad == interlace->srcpad) ? interlace->sinkpad : interlace->srcpad;
678 
679   if (filter != NULL) {
680     clean_filter = gst_caps_copy (filter);
681     clean_filter =
682         gst_interlace_caps_double_framerate (clean_filter,
683         (pad == interlace->sinkpad));
684     for (i = 0; i < gst_caps_get_size (clean_filter); ++i) {
685       GstStructure *s;
686 
687       s = gst_caps_get_structure (clean_filter, i);
688       gst_structure_remove_field (s, "interlace-mode");
689     }
690   }
691 
692   tcaps = gst_pad_get_pad_template_caps (otherpad);
693   othercaps = gst_pad_peer_query_caps (otherpad, clean_filter);
694   if (othercaps) {
695     icaps = gst_caps_intersect (othercaps, tcaps);
696     gst_caps_unref (othercaps);
697     gst_caps_unref (tcaps);
698   } else {
699     icaps = tcaps;
700   }
701 
702   if (clean_filter) {
703     othercaps = gst_caps_intersect (icaps, clean_filter);
704     gst_caps_unref (icaps);
705     icaps = othercaps;
706   }
707 
708   icaps = gst_caps_make_writable (icaps);
709   tcaps = gst_caps_copy (icaps);
710   if (interlace->pattern > GST_INTERLACE_PATTERN_2_2) {
711     mode = "mixed";
712   } else {
713     mode = "interleaved";
714   }
715   gst_caps_set_simple (icaps, "interlace-mode", G_TYPE_STRING,
716       pad == interlace->srcpad ? mode : "progressive", NULL);
717   if (pad == interlace->sinkpad) {
718     gst_caps_set_simple (tcaps, "interlace-mode", G_TYPE_STRING, mode, NULL);
719     icaps = gst_caps_merge (icaps, tcaps);
720     tcaps = NULL;
721   } else {
722     gst_caps_unref (tcaps);
723   }
724 
725   icaps =
726       gst_interlace_caps_double_framerate (icaps, (pad == interlace->srcpad));
727 
728   if (clean_filter)
729     gst_caps_unref (clean_filter);
730 
731   return icaps;
732 }
733 
734 static gboolean
gst_interlace_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)735 gst_interlace_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
736 {
737   gboolean ret;
738   GstInterlace *interlace;
739 
740   interlace = GST_INTERLACE (parent);
741 
742   switch (GST_QUERY_TYPE (query)) {
743     case GST_QUERY_CAPS:
744     {
745       GstCaps *filter, *caps;
746 
747       gst_query_parse_caps (query, &filter);
748       caps = gst_interlace_getcaps (pad, interlace, filter);
749       gst_query_set_caps_result (query, caps);
750       gst_caps_unref (caps);
751       ret = TRUE;
752       break;
753     }
754     default:
755       ret = gst_pad_query_default (pad, parent, query);
756       break;
757   }
758   return ret;
759 }
760 
761 static gboolean
gst_interlace_src_query(GstPad * pad,GstObject * parent,GstQuery * query)762 gst_interlace_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
763 {
764   gboolean ret;
765   GstInterlace *interlace;
766 
767   interlace = GST_INTERLACE (parent);
768 
769   switch (GST_QUERY_TYPE (query)) {
770     case GST_QUERY_CAPS:
771     {
772       GstCaps *filter, *caps;
773 
774       gst_query_parse_caps (query, &filter);
775       caps = gst_interlace_getcaps (pad, interlace, filter);
776       gst_query_set_caps_result (query, caps);
777       gst_caps_unref (caps);
778       ret = TRUE;
779       break;
780     }
781     default:
782       ret = gst_pad_query_default (pad, parent, query);
783       break;
784   }
785   return ret;
786 }
787 
788 static void
copy_field(GstInterlace * interlace,GstBuffer * dest,GstBuffer * src,int field_index)789 copy_field (GstInterlace * interlace, GstBuffer * dest, GstBuffer * src,
790     int field_index)
791 {
792   GstVideoInfo *info = &interlace->info;
793   gint i, j, n_planes;
794   guint8 *d, *s;
795   GstVideoFrame dframe, sframe;
796 
797   if (!gst_video_frame_map (&dframe, info, dest, GST_MAP_WRITE))
798     goto dest_map_failed;
799 
800   if (!gst_video_frame_map (&sframe, info, src, GST_MAP_READ))
801     goto src_map_failed;
802 
803   n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
804 
805   for (i = 0; i < n_planes; i++) {
806     gint cheight, cwidth;
807     gint ss, ds;
808 
809     d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
810     s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
811 
812     ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
813     ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
814 
815     d += field_index * ds;
816     s += field_index * ss;
817 
818     cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&dframe, i);
819     cwidth = MIN (ABS (ss), ABS (ds));
820 
821     for (j = field_index; j < cheight; j += 2) {
822       memcpy (d, s, cwidth);
823       d += ds * 2;
824       s += ss * 2;
825     }
826   }
827 
828   gst_video_frame_unmap (&dframe);
829   gst_video_frame_unmap (&sframe);
830   return;
831 
832 dest_map_failed:
833   {
834     GST_ERROR_OBJECT (interlace, "failed to map dest");
835     return;
836   }
837 src_map_failed:
838   {
839     GST_ERROR_OBJECT (interlace, "failed to map src");
840     gst_video_frame_unmap (&dframe);
841     return;
842   }
843 }
844 
845 
846 static GstFlowReturn
gst_interlace_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)847 gst_interlace_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
848 {
849   GstInterlace *interlace = GST_INTERLACE (parent);
850   GstFlowReturn ret = GST_FLOW_OK;
851   gint num_fields = 0;
852   int current_fields;
853   const PulldownFormat *format;
854   GstClockTime timestamp;
855 
856   timestamp = GST_BUFFER_TIMESTAMP (buffer);
857 
858   GST_DEBUG ("Received buffer at %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
859 
860   GST_DEBUG ("duration %" GST_TIME_FORMAT " flags %04x %s %s %s",
861       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
862       GST_BUFFER_FLAGS (buffer),
863       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" : "",
864       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" : "",
865       (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ? "onefield"
866       : "");
867 
868   if (interlace->passthrough) {
869     return gst_pad_push (interlace->srcpad, buffer);
870   }
871 
872   if (GST_BUFFER_FLAGS (buffer) & GST_BUFFER_FLAG_DISCONT) {
873     GST_DEBUG ("discont");
874 
875     if (interlace->stored_frame) {
876       gst_buffer_unref (interlace->stored_frame);
877     }
878     interlace->stored_frame = NULL;
879     interlace->stored_fields = 0;
880 
881     if (interlace->top_field_first) {
882       interlace->field_index = 0;
883     } else {
884       interlace->field_index = 1;
885     }
886   }
887 
888   if (interlace->timebase == GST_CLOCK_TIME_NONE) {
889     /* get the initial ts */
890     interlace->timebase = timestamp;
891   }
892 
893   format = &formats[interlace->pattern];
894 
895   if (interlace->stored_fields == 0
896       && interlace->phase_index == interlace->pattern_offset
897       && GST_CLOCK_TIME_IS_VALID (timestamp)) {
898     interlace->timebase = timestamp;
899     interlace->fields_since_timebase = 0;
900   }
901 
902   if (!format->n_fields[interlace->phase_index]) {
903     interlace->phase_index = 0;
904   }
905 
906   current_fields = format->n_fields[interlace->phase_index];
907   /* increment the phase index */
908   interlace->phase_index++;
909   GST_DEBUG ("incoming buffer assigned %d fields", current_fields);
910 
911   num_fields = interlace->stored_fields + current_fields;
912   while (num_fields >= 2) {
913     GstBuffer *output_buffer;
914     int n_output_fields;
915     gboolean interlaced = FALSE;
916 
917     GST_DEBUG ("have %d fields, %d current, %d stored",
918         num_fields, current_fields, interlace->stored_fields);
919 
920     if (interlace->stored_fields > 0) {
921       GST_DEBUG ("1 field from stored, 1 from current");
922 
923       output_buffer = gst_buffer_new_and_alloc (gst_buffer_get_size (buffer));
924       /* take the first field from the stored frame */
925       copy_field (interlace, output_buffer, interlace->stored_frame,
926           interlace->field_index);
927       interlace->stored_fields--;
928       /* take the second field from the incoming buffer */
929       copy_field (interlace, output_buffer, buffer, interlace->field_index ^ 1);
930       current_fields--;
931       n_output_fields = 2;
932       interlaced = TRUE;
933     } else {
934       output_buffer = gst_buffer_make_writable (gst_buffer_ref (buffer));
935       if (num_fields >= 3 && interlace->allow_rff) {
936         GST_DEBUG ("3 fields from current");
937         /* take both fields from incoming buffer */
938         current_fields -= 3;
939         n_output_fields = 3;
940       } else {
941         GST_DEBUG ("2 fields from current");
942         /* take both buffers from incoming buffer */
943         current_fields -= 2;
944         n_output_fields = 2;
945       }
946     }
947     num_fields -= n_output_fields;
948 
949     gst_interlace_decorate_buffer (interlace, output_buffer, n_output_fields,
950         interlaced);
951     interlace->fields_since_timebase += n_output_fields;
952     interlace->field_index ^= (n_output_fields & 1);
953 
954     GST_DEBUG_OBJECT (interlace, "output timestamp %" GST_TIME_FORMAT
955         " duration %" GST_TIME_FORMAT " flags %04x %s %s %s",
956         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (output_buffer)),
957         GST_TIME_ARGS (GST_BUFFER_DURATION (output_buffer)),
958         GST_BUFFER_FLAGS (output_buffer),
959         (GST_BUFFER_FLAGS (output_buffer) & GST_VIDEO_BUFFER_FLAG_TFF) ? "tff" :
960         "",
961         (GST_BUFFER_FLAGS (output_buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" :
962         "",
963         (GST_BUFFER_FLAGS (output_buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ?
964         "onefield" : "");
965 
966     ret = gst_pad_push (interlace->srcpad, output_buffer);
967     if (ret != GST_FLOW_OK) {
968       GST_DEBUG_OBJECT (interlace, "Failed to push buffer %p", output_buffer);
969       break;
970     }
971   }
972 
973   GST_DEBUG ("done.  %d fields remaining", current_fields);
974 
975   if (interlace->stored_frame) {
976     gst_buffer_unref (interlace->stored_frame);
977     interlace->stored_frame = NULL;
978     interlace->stored_fields = 0;
979   }
980 
981   if (current_fields > 0) {
982     interlace->stored_frame = buffer;
983     interlace->stored_fields = current_fields;
984   } else {
985     gst_buffer_unref (buffer);
986   }
987   return ret;
988 }
989 
990 static void
gst_interlace_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)991 gst_interlace_set_property (GObject * object,
992     guint prop_id, const GValue * value, GParamSpec * pspec)
993 {
994   GstInterlace *interlace = GST_INTERLACE (object);
995 
996   switch (prop_id) {
997     case PROP_TOP_FIELD_FIRST:
998       interlace->top_field_first = g_value_get_boolean (value);
999       break;
1000     case PROP_PATTERN:
1001       interlace->pattern = g_value_get_enum (value);
1002       break;
1003     case PROP_PATTERN_OFFSET:
1004       interlace->pattern_offset = g_value_get_uint (value);
1005       break;
1006     case PROP_ALLOW_RFF:
1007       interlace->allow_rff = g_value_get_boolean (value);
1008       break;
1009     default:
1010       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1011       break;
1012   }
1013 }
1014 
1015 static void
gst_interlace_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1016 gst_interlace_get_property (GObject * object,
1017     guint prop_id, GValue * value, GParamSpec * pspec)
1018 {
1019   GstInterlace *interlace = GST_INTERLACE (object);
1020 
1021   switch (prop_id) {
1022     case PROP_TOP_FIELD_FIRST:
1023       g_value_set_boolean (value, interlace->top_field_first);
1024       break;
1025     case PROP_PATTERN:
1026       g_value_set_enum (value, interlace->pattern);
1027       break;
1028     case PROP_PATTERN_OFFSET:
1029       g_value_set_uint (value, interlace->pattern_offset);
1030       break;
1031     case PROP_ALLOW_RFF:
1032       g_value_set_boolean (value, interlace->allow_rff);
1033       break;
1034     default:
1035       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1036       break;
1037   }
1038 }
1039 
1040 static GstStateChangeReturn
gst_interlace_change_state(GstElement * element,GstStateChange transition)1041 gst_interlace_change_state (GstElement * element, GstStateChange transition)
1042 {
1043   //GstInterlace *interlace = GST_INTERLACE (element);
1044 
1045   switch (transition) {
1046     case GST_STATE_CHANGE_PAUSED_TO_READY:
1047       //gst_interlace_reset (interlace);
1048       break;
1049     default:
1050       break;
1051   }
1052 
1053   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1054 }
1055 
1056 static gboolean
plugin_init(GstPlugin * plugin)1057 plugin_init (GstPlugin * plugin)
1058 {
1059   GST_DEBUG_CATEGORY_INIT (gst_interlace_debug, "interlace", 0,
1060       "interlace element");
1061 
1062   return gst_element_register (plugin, "interlace", GST_RANK_NONE,
1063       GST_TYPE_INTERLACE);
1064 }
1065 
1066 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1067     GST_VERSION_MINOR,
1068     interlace,
1069     "Create an interlaced video stream",
1070     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
1071