2 * Copyright (C) 2010 David A. Schleef <ds@schleef.org>
3 * Copyright (C) 2010 Robert Swain <robert.swain@collabora.co.uk>
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.
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.
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.
21 * SECTION:element-interlace
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.
29 * ## Example launch line
31 * gst-launch-1.0 -v videotestsrc pattern=ball ! interlace ! xvimagesink
33 * This pipeline illustrates the combing effects caused by displaying
34 * two interlaced fields as one progressive frame.
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
41 * This pipeline converts a progressive video stream into an interlaced
42 * stream suitable for standard definition NTSC.
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 !
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
61 #include <gst/video/video.h>
66 GST_DEBUG_CATEGORY (gst_interlace_debug);
67 #define GST_CAT_DEFAULT gst_interlace_debug
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))
80 typedef struct _GstInterlace GstInterlace;
81 typedef struct _GstInterlaceClass GstInterlaceClass;
91 gboolean top_field_first;
97 GstVideoInfo out_info;
101 GstBuffer *stored_frame;
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;
111 struct _GstInterlaceClass
113 GstElementClass element_class;
119 PROP_TOP_FIELD_FIRST,
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;
140 #define GST_INTERLACE_PATTERN (gst_interlace_pattern_get_type ())
142 gst_interlace_pattern_get_type (void)
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)",
151 {GST_INTERLACE_PATTERN_EURO, "Euro 2-11:3 (e.g. 24p -> 50i telecine)",
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)",
159 {GST_INTERLACE_PATTERN_1_2R4, "1:2-4 (e.g. 27.5p -> 50i telecine)",
164 if (!interlace_pattern_type) {
165 interlace_pattern_type =
166 g_enum_register_static ("GstInterlacePattern", pattern_types);
169 return interlace_pattern_type;
172 #define VIDEO_FORMATS "{AYUV,YUY2,UYVY,I420,YV12,Y42B,Y444,NV12,NV21}"
174 static GstStaticPadTemplate gst_interlace_src_template =
175 GST_STATIC_PAD_TEMPLATE ("src",
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,
182 ",interlace-mode=alternate")
185 static GstStaticPadTemplate gst_interlace_sink_template =
186 GST_STATIC_PAD_TEMPLATE ("sink",
189 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
190 ("{AYUV,YUY2,UYVY,I420,YV12,Y42B,Y444,NV12,NV21}")
194 GType gst_interlace_get_type (void);
195 static void gst_interlace_finalize (GObject * obj);
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);
202 static gboolean gst_interlace_sink_event (GstPad * pad, GstObject * parent,
204 static gboolean gst_interlace_sink_query (GstPad * pad, GstObject * parent,
206 static GstFlowReturn gst_interlace_chain (GstPad * pad, GstObject * parent,
209 static gboolean gst_interlace_src_query (GstPad * pad, GstObject * parent,
212 static GstStateChangeReturn gst_interlace_change_state (GstElement * element,
213 GstStateChange transition);
215 #define gst_interlace_parent_class parent_class
216 G_DEFINE_TYPE (GstInterlace, gst_interlace, GST_TYPE_ELEMENT);
219 gst_interlace_class_init (GstInterlaceClass * klass)
221 GObjectClass *object_class = G_OBJECT_CLASS (klass);
222 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
224 parent_class = g_type_class_peek_parent (klass);
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;
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));
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));
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));
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));
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>");
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);
261 element_class->change_state = gst_interlace_change_state;
263 gst_type_mark_as_plugin_api (GST_INTERLACE_PATTERN, 0);
267 gst_interlace_finalize (GObject * obj)
269 G_OBJECT_CLASS (parent_class)->finalize (obj);
273 gst_interlace_reset (GstInterlace * interlace)
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;
286 gst_interlace_init (GstInterlace * interlace)
288 GST_DEBUG ("gst_interlace_init");
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);
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);
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);
308 typedef struct _PulldownFormat PulldownFormat;
309 struct _PulldownFormat
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;
318 static const PulldownFormat formats[] = {
319 /* 60p -> 60i or 50p -> 50i */
321 /* 30p -> 60i or 25p -> 50i */
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,}},
343 gst_interlace_decorate_buffer_ts (GstInterlace * interlace, GstBuffer * buf,
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;
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);
365 gst_interlace_decorate_buffer (GstInterlace * interlace, GstBuffer * buf,
366 int n_fields, gboolean interlaced)
368 gst_interlace_decorate_buffer_ts (interlace, buf, n_fields);
370 if (interlace->field_index == 0) {
371 GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF);
374 GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_RFF);
377 GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_ONEFIELD);
379 if (interlace->pattern > GST_INTERLACE_PATTERN_2_2 && n_fields == 2
381 GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_INTERLACED);
386 interlace_mode_from_pattern (GstInterlace * interlace)
388 if (interlace->pattern > GST_INTERLACE_PATTERN_2_2)
391 return "interleaved";
395 dup_caps_with_alternate (GstCaps * caps)
397 GstCaps *with_alternate;
398 GstCapsFeatures *features;
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);
404 gst_caps_set_simple (with_alternate, "interlace-mode", G_TYPE_STRING,
407 return with_alternate;
411 gst_interlace_setcaps (GstInterlace * interlace, GstCaps * caps)
414 GstVideoInfo info, out_info;
415 GstCaps *othercaps, *src_peer_caps;
416 const PulldownFormat *pdformat;
419 if (!gst_video_info_from_caps (&info, caps))
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);
434 if (!gst_video_info_from_caps (&out_info, othercaps)) {
435 gst_caps_unref (othercaps);
440 GST_VIDEO_INFO_INTERLACE_MODE (&out_info) ==
441 GST_VIDEO_INTERLACE_MODE_ALTERNATE;
443 pdformat = &formats[interlace->pattern];
445 interlace->phase_index = interlace->pattern_offset;
447 interlace->src_fps_n = info.fps_n * pdformat->ratio_n;
448 interlace->src_fps_d = info.fps_d * pdformat->ratio_d;
451 GST_DEBUG_OBJECT (interlace,
452 "producing alternate stream as requested downstream");
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;
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,
469 gst_caps_unref (othercaps);
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",
482 GST_DEBUG_OBJECT (interlace->sinkpad, "set caps %" GST_PTR_FORMAT, caps);
483 GST_DEBUG_OBJECT (interlace->srcpad, "set caps %" GST_PTR_FORMAT, othercaps);
485 ret = gst_pad_set_caps (interlace->srcpad, othercaps);
486 gst_caps_unref (othercaps);
488 interlace->info = info;
489 interlace->out_info = out_info;
495 GST_DEBUG_OBJECT (interlace, "error parsing caps");
501 gst_interlace_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
504 GstInterlace *interlace;
506 interlace = GST_INTERLACE (parent);
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);
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);
520 /* FIXME revive this when we output ONEFIELD and RFF buffers */
523 const PulldownFormat *format = &formats[interlace->pattern];
526 format->n_fields[interlace->phase_index] -
527 interlace->stored_fields_pushed;
528 interlace->stored_fields_pushed = 0;
530 /* on EOS we want to push as many sane frames as are left */
531 while (num_fields > 1) {
532 GstBuffer *output_buffer;
534 /* make metadata writable before editing it */
535 interlace->stored_frame =
536 gst_buffer_make_metadata_writable (interlace->stored_frame);
539 gst_interlace_decorate_buffer (interlace, interlace->stored_frame,
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",
550 output_buffer = NULL;
552 if (num_fields <= 1) {
553 gst_buffer_unref (interlace->stored_frame);
554 interlace->stored_frame = NULL;
559 /* increment the phase index */
560 interlace->phase_index++;
561 if (!format->n_fields[interlace->phase_index]) {
562 interlace->phase_index = 0;
567 ret = gst_pad_push_event (interlace->srcpad, event);
573 gst_event_parse_caps (event, &caps);
574 ret = gst_interlace_setcaps (interlace, caps);
575 gst_event_unref (event);
579 ret = gst_pad_push_event (interlace->srcpad, event);
587 gst_interlace_fraction_double (gint * n_out, gint * d_out, gboolean half)
600 gcd = gst_util_greatest_common_divisor (n, d);
605 if (G_MAXINT / 2 >= ABS (d)) {
607 } else if (n >= 2 && n != G_MAXINT) {
613 if (G_MAXINT / 2 >= ABS (n)) {
615 } else if (d >= 2 && d != G_MAXINT) {
629 gst_interlace_caps_double_framerate (GstCaps * caps, gboolean half)
633 for (len = gst_caps_get_size (caps); len > 0; len--) {
634 GstStructure *s = gst_caps_get_structure (caps, len - 1);
637 val = gst_structure_get_value (s, "framerate");
641 if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION) {
644 n = gst_value_get_fraction_numerator (val);
645 d = gst_value_get_fraction_denominator (val);
647 if (!gst_interlace_fraction_double (&n, &d, half)) {
648 gst_caps_remove_structure (caps, len - 1);
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 = {
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);
664 min = gst_value_get_fraction_range_min (val);
665 max = gst_value_get_fraction_range_max (val);
667 n = gst_value_get_fraction_numerator (min);
668 d = gst_value_get_fraction_denominator (min);
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);
678 gst_value_set_fraction (&nmin, n, d);
680 n = gst_value_get_fraction_numerator (max);
681 d = gst_value_get_fraction_denominator (max);
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);
691 gst_value_set_fraction (&nmax, n, d);
692 gst_value_set_fraction_range (&nrange, &nmin, &nmax);
694 gst_structure_take_value (s, "framerate", &nrange);
696 g_value_unset (&nmin);
697 g_value_unset (&nmax);
698 } else if (G_VALUE_TYPE (val) == GST_TYPE_LIST) {
700 GValue nlist = { 0, };
701 GValue nval = { 0, };
704 g_value_init (&nlist, GST_TYPE_LIST);
705 for (i = gst_value_list_get_size (val); i > 0; i--) {
708 lval = gst_value_list_get_value (val, i - 1);
710 if (G_VALUE_TYPE (lval) != GST_TYPE_FRACTION)
713 n = gst_value_get_fraction_numerator (lval);
714 d = gst_value_get_fraction_denominator (lval);
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)) {
722 g_value_init (&nval, GST_TYPE_FRACTION);
724 gst_value_set_fraction (&nval, n, d);
725 gst_value_list_append_and_take_value (&nlist, &nval);
727 gst_structure_take_value (s, "framerate", &nlist);
735 gst_interlace_getcaps (GstPad * pad, GstInterlace * interlace, GstCaps * filter)
738 GstCaps *othercaps, *tcaps;
740 GstCaps *clean_filter = NULL;
745 (pad == interlace->srcpad) ? interlace->sinkpad : interlace->srcpad;
747 if (filter != NULL) {
748 clean_filter = gst_caps_copy (filter);
750 gst_interlace_caps_double_framerate (clean_filter,
751 (pad == interlace->sinkpad));
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.
760 gst_caps_append (clean_filter, dup_caps_with_alternate (clean_filter));
763 for (i = 0; i < gst_caps_get_size (clean_filter); ++i) {
766 s = gst_caps_get_structure (clean_filter, i);
767 gst_structure_remove_field (s, "interlace-mode");
771 tcaps = gst_pad_get_pad_template_caps (otherpad);
772 othercaps = gst_pad_peer_query_caps (otherpad, clean_filter);
774 icaps = gst_caps_intersect (othercaps, tcaps);
775 gst_caps_unref (othercaps);
776 gst_caps_unref (tcaps);
782 othercaps = gst_caps_intersect (icaps, clean_filter);
783 gst_caps_unref (icaps);
787 icaps = gst_caps_make_writable (icaps);
788 mode = interlace_mode_from_pattern (interlace);
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));
796 GstCaps *interlaced, *alternate;
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;
803 features = gst_caps_get_features (icaps, i);
804 gst_caps_features_remove (features, GST_CAPS_FEATURE_FORMAT_INTERLACED);
807 gst_caps_set_simple (icaps, "interlace-mode", G_TYPE_STRING, "progressive",
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,
815 alternate = dup_caps_with_alternate (icaps);
817 icaps = gst_caps_merge (icaps, interlaced);
818 icaps = gst_caps_merge (icaps, alternate);
822 gst_interlace_caps_double_framerate (icaps, (pad == interlace->srcpad));
825 gst_caps_unref (clean_filter);
827 GST_DEBUG_OBJECT (pad, "caps: %" GST_PTR_FORMAT, icaps);
832 gst_interlace_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
835 GstInterlace *interlace;
837 interlace = GST_INTERLACE (parent);
839 switch (GST_QUERY_TYPE (query)) {
842 GstCaps *filter, *caps;
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);
852 ret = gst_pad_query_default (pad, parent, query);
859 gst_interlace_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
862 GstInterlace *interlace;
864 interlace = GST_INTERLACE (parent);
866 switch (GST_QUERY_TYPE (query)) {
869 GstCaps *filter, *caps;
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);
879 ret = gst_pad_query_default (pad, parent, query);
886 copy_fields (GstInterlace * interlace, GstBuffer * dest, GstBuffer * src,
889 GstVideoInfo *info = &interlace->info;
892 GstVideoFrame dframe, sframe;
894 if (!gst_video_frame_map (&dframe, info, dest, GST_MAP_WRITE))
895 goto dest_map_failed;
897 if (!gst_video_frame_map (&sframe, info, src, GST_MAP_READ))
900 n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
902 for (i = 0; i < n_planes; i++) {
903 gint cheight, cwidth;
906 d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
907 s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
909 ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
910 ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
912 d += field_index * ds;
913 s += field_index * ss;
915 cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&dframe, i);
916 cwidth = MIN (ABS (ss), ABS (ds));
918 for (j = field_index; j < cheight; j += 2) {
919 memcpy (d, s, cwidth);
925 gst_video_frame_unmap (&dframe);
926 gst_video_frame_unmap (&sframe);
931 GST_ERROR_OBJECT (interlace, "failed to map dest");
936 GST_ERROR_OBJECT (interlace, "failed to map src");
937 gst_video_frame_unmap (&dframe);
943 copy_field (GstInterlace * interlace, GstBuffer * src, int field_index)
946 GstVideoFrame dframe, sframe;
950 gst_buffer_new_allocate (NULL, GST_VIDEO_INFO_SIZE (&interlace->out_info),
953 if (!gst_video_frame_map (&dframe, &interlace->out_info, dest, GST_MAP_WRITE))
954 goto dest_map_failed;
956 if (!gst_video_frame_map (&sframe, &interlace->info, src, GST_MAP_READ))
959 n_planes = GST_VIDEO_FRAME_N_PLANES (&dframe);
961 for (i = 0; i < n_planes; i++) {
963 gint cheight, cwidth;
966 d = GST_VIDEO_FRAME_PLANE_DATA (&dframe, i);
967 s = GST_VIDEO_FRAME_PLANE_DATA (&sframe, i);
969 ds = GST_VIDEO_FRAME_PLANE_STRIDE (&dframe, i);
970 ss = GST_VIDEO_FRAME_PLANE_STRIDE (&sframe, i);
972 cheight = GST_VIDEO_FRAME_COMP_HEIGHT (&sframe, i);
973 cwidth = MIN (ABS (ss), ABS (ds));
975 for (j = field_index; j < cheight; j += 2) {
976 memcpy (d, s, cwidth);
982 gst_video_frame_unmap (&dframe);
983 gst_video_frame_unmap (&sframe);
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);
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);
1002 static GstFlowReturn
1003 gst_interlace_push_buffer (GstInterlace * interlace, GstBuffer * buffer)
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" :
1012 (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_RFF) ? "rff" :
1014 (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_FLAG_ONEFIELD) ?
1017 return gst_pad_push (interlace->srcpad, buffer);
1020 static GstFlowReturn
1021 gst_interlace_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
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;
1031 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1033 GST_DEBUG ("Received buffer at %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
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"
1043 if (interlace->passthrough) {
1044 return gst_pad_push (interlace->srcpad, buffer);
1047 if (GST_BUFFER_FLAGS (buffer) & GST_BUFFER_FLAG_DISCONT) {
1048 GST_DEBUG ("discont");
1050 if (interlace->stored_frame) {
1051 gst_buffer_unref (interlace->stored_frame);
1053 interlace->stored_frame = NULL;
1054 interlace->stored_fields = 0;
1056 if (interlace->top_field_first) {
1057 interlace->field_index = 0;
1059 interlace->field_index = 1;
1063 if (interlace->timebase == GST_CLOCK_TIME_NONE) {
1064 /* get the initial ts */
1065 interlace->timebase = timestamp;
1068 format = &formats[interlace->pattern];
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;
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;
1085 GST_DEBUG ("incoming buffer assigned %d fields", current_fields);
1088 GST_VIDEO_INFO_INTERLACE_MODE (&interlace->out_info) ==
1089 GST_VIDEO_INTERLACE_MODE_ALTERNATE;
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;
1097 GST_DEBUG ("have %d fields, %d current, %d stored",
1098 num_fields, current_fields, interlace->stored_fields);
1100 if (interlace->stored_fields > 0) {
1101 GST_DEBUG ("1 field from stored, 1 from current");
1104 /* take the first field from the stored frame */
1105 output_buffer = copy_field (interlace, interlace->stored_frame,
1106 interlace->field_index);
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;
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);
1124 interlace->stored_fields--;
1126 n_output_fields = 2;
1130 output_buffer = copy_field (interlace, buffer, interlace->field_index);
1132 return GST_FLOW_ERROR;
1134 copy_field (interlace, buffer, interlace->field_index ^ 1);
1135 if (!output_buffer2)
1136 return GST_FLOW_ERROR;
1138 output_buffer = gst_buffer_copy (buffer);
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;
1147 GST_DEBUG ("2 fields from current");
1148 /* take both buffers from incoming buffer */
1149 current_fields -= 2;
1150 n_output_fields = 2;
1153 num_fields -= n_output_fields;
1156 g_assert (!output_buffer2);
1157 gst_interlace_decorate_buffer (interlace, output_buffer, n_output_fields,
1160 g_assert (output_buffer2);
1161 gst_interlace_decorate_buffer_ts (interlace, output_buffer,
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);
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);
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);
1179 GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1180 GST_BUFFER_FLAG_SET (output_buffer2, GST_VIDEO_BUFFER_FLAG_INTERLACED);
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);
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);
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",
1205 GST_DEBUG ("done. %d fields remaining", current_fields);
1207 if (interlace->stored_frame) {
1208 gst_buffer_unref (interlace->stored_frame);
1209 interlace->stored_frame = NULL;
1210 interlace->stored_fields = 0;
1213 if (current_fields > 0) {
1214 interlace->stored_frame = buffer;
1215 interlace->stored_fields = current_fields;
1217 gst_buffer_unref (buffer);
1223 gst_interlace_set_property (GObject * object,
1224 guint prop_id, const GValue * value, GParamSpec * pspec)
1226 GstInterlace *interlace = GST_INTERLACE (object);
1229 case PROP_TOP_FIELD_FIRST:
1230 interlace->top_field_first = g_value_get_boolean (value);
1233 interlace->pattern = g_value_get_enum (value);
1235 case PROP_PATTERN_OFFSET:
1236 interlace->pattern_offset = g_value_get_uint (value);
1238 case PROP_ALLOW_RFF:
1239 interlace->allow_rff = g_value_get_boolean (value);
1242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1248 gst_interlace_get_property (GObject * object,
1249 guint prop_id, GValue * value, GParamSpec * pspec)
1251 GstInterlace *interlace = GST_INTERLACE (object);
1254 case PROP_TOP_FIELD_FIRST:
1255 g_value_set_boolean (value, interlace->top_field_first);
1258 g_value_set_enum (value, interlace->pattern);
1260 case PROP_PATTERN_OFFSET:
1261 g_value_set_uint (value, interlace->pattern_offset);
1263 case PROP_ALLOW_RFF:
1264 g_value_set_boolean (value, interlace->allow_rff);
1267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1272 static GstStateChangeReturn
1273 gst_interlace_change_state (GstElement * element, GstStateChange transition)
1275 //GstInterlace *interlace = GST_INTERLACE (element);
1277 switch (transition) {
1278 case GST_STATE_CHANGE_PAUSED_TO_READY:
1279 //gst_interlace_reset (interlace);
1285 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1289 plugin_init (GstPlugin * plugin)
1291 GST_DEBUG_CATEGORY_INIT (gst_interlace_debug, "interlace", 0,
1292 "interlace element");
1294 return gst_element_register (plugin, "interlace", GST_RANK_NONE,
1295 GST_TYPE_INTERLACE);
1298 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1301 "Create an interlaced video stream",
1302 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)