2 * Copyright (C) 2013 David Schleef <ds@schleef.org>
3 * Copyright (C) 2013 Rdio Inc <ingestions@rdio.com>
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 Street, Suite 500,
18 * Boston, MA 02110-1335, USA.
21 * SECTION:element-gstivtc
23 * The ivtc element is an inverse telecine filter. It takes interlaced
24 * video that was created from progressive content using a telecine
25 * filter, and reconstructs the original progressive content.
28 * <title>Example launch line</title>
30 * gst-launch -v videotestsrc pattern=ball ! video/x-raw,framerate=24/1 !
31 * interlace field-pattern=3:2 !
32 * ivtc ! video/x-raw,framerate=24/1 ! fakesink
35 * This pipeline creates a progressive video stream at 24 fps, and
36 * converts it to a 60 fields per second interlaced stream. Then the
37 * stream is inversed telecine'd back to 24 fps, yielding approximately
38 * the original videotestsrc content.
47 #include <gst/base/gstbasetransform.h>
48 #include <gst/video/video.h>
53 /* only because element registration is in this file */
54 #include "gstcombdetect.h"
56 GST_DEBUG_CATEGORY_STATIC (gst_ivtc_debug_category);
57 #define GST_CAT_DEFAULT gst_ivtc_debug_category
62 static void gst_ivtc_set_property (GObject * object,
63 guint property_id, const GValue * value, GParamSpec * pspec);
64 static void gst_ivtc_get_property (GObject * object,
65 guint property_id, GValue * value, GParamSpec * pspec);
66 static void gst_ivtc_dispose (GObject * object);
67 static void gst_ivtc_finalize (GObject * object);
69 static GstCaps *gst_ivtc_transform_caps (GstBaseTransform * trans,
70 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
71 static GstCaps *gst_ivtc_fixate_caps (GstBaseTransform * trans,
72 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
73 static gboolean gst_ivtc_set_caps (GstBaseTransform * trans, GstCaps * incaps,
75 static gboolean gst_ivtc_transform_size (GstBaseTransform * trans,
76 GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
78 static gboolean gst_ivtc_start (GstBaseTransform * trans);
79 static gboolean gst_ivtc_stop (GstBaseTransform * trans);
80 static gboolean gst_ivtc_sink_event (GstBaseTransform * trans,
82 static GstFlowReturn gst_ivtc_transform (GstBaseTransform * trans,
83 GstBuffer * inbuf, GstBuffer * outbuf);
84 static void gst_ivtc_flush (GstIvtc * ivtc);
85 static void gst_ivtc_retire_fields (GstIvtc * ivtc, int n_fields);
86 static void gst_ivtc_construct_frame (GstIvtc * itvc, GstBuffer * outbuf);
88 static int get_comb_score (GstVideoFrame * top, GstVideoFrame * bottom);
97 #define MAX_WIDTH 2048
100 "format = (string) { I420, Y444, Y42B }, " \
101 "width = [1, 2048], " \
102 "height = " GST_VIDEO_SIZE_RANGE ", " \
103 "framerate = " GST_VIDEO_FPS_RANGE
105 static GstStaticPadTemplate gst_ivtc_sink_template =
106 GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_STATIC_CAPS (VIDEO_CAPS)
112 static GstStaticPadTemplate gst_ivtc_src_template =
113 GST_STATIC_PAD_TEMPLATE ("src",
116 GST_STATIC_CAPS (VIDEO_CAPS)
120 /* class initialization */
122 G_DEFINE_TYPE_WITH_CODE (GstIvtc, gst_ivtc, GST_TYPE_BASE_TRANSFORM,
123 GST_DEBUG_CATEGORY_INIT (gst_ivtc_debug_category, "ivtc", 0,
124 "debug category for ivtc element"));
127 gst_ivtc_class_init (GstIvtcClass * klass)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
130 GstBaseTransformClass *base_transform_class =
131 GST_BASE_TRANSFORM_CLASS (klass);
133 /* Setting up pads and setting metadata should be moved to
134 base_class_init if you intend to subclass this class. */
135 gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
136 gst_static_pad_template_get (&gst_ivtc_sink_template));
137 gst_element_class_add_pad_template (GST_ELEMENT_CLASS (klass),
138 gst_static_pad_template_get (&gst_ivtc_src_template));
140 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
141 "Inverse Telecine", "Video/Filter", "Inverse Telecine Filter",
142 "David Schleef <ds@schleef.org>");
144 gobject_class->set_property = gst_ivtc_set_property;
145 gobject_class->get_property = gst_ivtc_get_property;
146 gobject_class->dispose = gst_ivtc_dispose;
147 gobject_class->finalize = gst_ivtc_finalize;
148 base_transform_class->transform_caps =
149 GST_DEBUG_FUNCPTR (gst_ivtc_transform_caps);
150 base_transform_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_ivtc_fixate_caps);
151 base_transform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ivtc_set_caps);
152 base_transform_class->transform_size =
153 GST_DEBUG_FUNCPTR (gst_ivtc_transform_size);
154 base_transform_class->start = GST_DEBUG_FUNCPTR (gst_ivtc_start);
155 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_ivtc_stop);
156 base_transform_class->sink_event = GST_DEBUG_FUNCPTR (gst_ivtc_sink_event);
157 base_transform_class->transform = GST_DEBUG_FUNCPTR (gst_ivtc_transform);
161 gst_ivtc_init (GstIvtc * ivtc)
166 gst_ivtc_set_property (GObject * object, guint property_id,
167 const GValue * value, GParamSpec * pspec)
169 GstIvtc *ivtc = GST_IVTC (object);
171 GST_DEBUG_OBJECT (ivtc, "set_property");
173 switch (property_id) {
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
181 gst_ivtc_get_property (GObject * object, guint property_id,
182 GValue * value, GParamSpec * pspec)
184 GstIvtc *ivtc = GST_IVTC (object);
186 GST_DEBUG_OBJECT (ivtc, "get_property");
188 switch (property_id) {
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
196 gst_ivtc_dispose (GObject * object)
198 GstIvtc *ivtc = GST_IVTC (object);
200 GST_DEBUG_OBJECT (ivtc, "dispose");
202 /* clean up as possible. may be called multiple times */
204 G_OBJECT_CLASS (gst_ivtc_parent_class)->dispose (object);
208 gst_ivtc_finalize (GObject * object)
210 GstIvtc *ivtc = GST_IVTC (object);
212 GST_DEBUG_OBJECT (ivtc, "finalize");
214 /* clean up object here */
216 G_OBJECT_CLASS (gst_ivtc_parent_class)->finalize (object);
220 gst_ivtc_transform_caps (GstBaseTransform * trans,
221 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
226 othercaps = gst_caps_copy (caps);
228 if (direction == GST_PAD_SRC) {
229 GValue value = G_VALUE_INIT;
230 GValue v = G_VALUE_INIT;
232 g_value_init (&value, GST_TYPE_LIST);
233 g_value_init (&v, G_TYPE_STRING);
235 g_value_set_string (&v, "interleaved");
236 gst_value_list_append_value (&value, &v);
237 g_value_set_string (&v, "mixed");
238 gst_value_list_append_value (&value, &v);
239 g_value_set_string (&v, "progressive");
240 gst_value_list_append_value (&value, &v);
242 for (i = 0; i < gst_caps_get_size (othercaps); i++) {
243 GstStructure *structure = gst_caps_get_structure (othercaps, i);
244 gst_structure_set_value (structure, "interlace-mode", &value);
245 gst_structure_remove_field (structure, "framerate");
247 g_value_reset (&value);
250 for (i = 0; i < gst_caps_get_size (othercaps); i++) {
251 GstStructure *structure = gst_caps_get_structure (othercaps, i);
252 gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
253 "progressive", NULL);
254 gst_structure_remove_field (structure, "framerate");
261 intersect = gst_caps_intersect (othercaps, filter);
262 gst_caps_unref (othercaps);
263 othercaps = intersect;
270 gst_ivtc_fixate_caps (GstBaseTransform * trans, GstPadDirection direction,
271 GstCaps * caps, GstCaps * othercaps)
275 GST_DEBUG_OBJECT (trans, "fixating caps %" GST_PTR_FORMAT, othercaps);
277 result = gst_caps_make_writable (othercaps);
278 if (direction == GST_PAD_SINK) {
279 gst_caps_set_simple (result, "framerate", GST_TYPE_FRACTION, 24, 1, NULL);
282 result = gst_caps_fixate (result);
288 gst_ivtc_set_caps (GstBaseTransform * trans, GstCaps * incaps,
291 GstIvtc *ivtc = GST_IVTC (trans);
293 gst_video_info_from_caps (&ivtc->sink_video_info, incaps);
294 gst_video_info_from_caps (&ivtc->src_video_info, outcaps);
296 ivtc->field_duration = gst_util_uint64_scale_int (GST_SECOND,
297 ivtc->sink_video_info.fps_d, ivtc->sink_video_info.fps_n * 2);
298 GST_DEBUG_OBJECT (trans, "field duration %" GST_TIME_FORMAT,
299 GST_TIME_ARGS (ivtc->field_duration));
306 gst_ivtc_transform_size (GstBaseTransform * trans, GstPadDirection direction,
307 GstCaps * caps, gsize size, GstCaps * othercaps, gsize * othersize)
309 GstIvtc *ivtc = GST_IVTC (trans);
311 GST_DEBUG_OBJECT (ivtc, "transform_size");
318 gst_ivtc_start (GstBaseTransform * trans)
320 GstIvtc *ivtc = GST_IVTC (trans);
322 GST_DEBUG_OBJECT (ivtc, "start");
328 gst_ivtc_stop (GstBaseTransform * trans)
330 GstIvtc *ivtc = GST_IVTC (trans);
332 GST_DEBUG_OBJECT (ivtc, "stop");
333 gst_ivtc_flush (ivtc);
338 /* sink and src pad event handlers */
340 gst_ivtc_sink_event (GstBaseTransform * trans, GstEvent * event)
342 GstIvtc *ivtc = GST_IVTC (trans);
344 GST_DEBUG_OBJECT (ivtc, "sink_event");
346 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
347 const GstSegment *seg;
349 gst_ivtc_flush (ivtc);
351 /* FIXME this should handle update events */
353 gst_event_parse_segment (event, &seg);
354 gst_segment_copy_into (seg, &ivtc->segment);
355 ivtc->current_ts = ivtc->segment.start;
358 return GST_BASE_TRANSFORM_CLASS (gst_ivtc_parent_class)->sink_event (trans,
363 gst_ivtc_flush (GstIvtc * ivtc)
365 GST_FIXME_OBJECT (ivtc, "not sending flushed fields to srcpad");
367 gst_ivtc_retire_fields (ivtc, ivtc->n_fields);
377 add_field (GstIvtc * ivtc, GstBuffer * buffer, int parity, int index)
379 int i = ivtc->n_fields;
381 GstIvtcField *field = &ivtc->fields[i];
383 g_return_if_fail (i < GST_IVTC_MAX_FIELDS);
385 ts = GST_BUFFER_PTS (buffer) + index * ivtc->field_duration;
386 if (ts + ivtc->field_duration < ivtc->segment.start) {
387 /* drop, it's before our segment */
391 GST_DEBUG ("adding field %d", i);
393 field->buffer = gst_buffer_ref (buffer);
394 field->parity = parity;
397 gst_video_frame_map (&ivtc->fields[i].frame, &ivtc->sink_video_info,
398 buffer, GST_MAP_READ);
404 similarity (GstIvtc * ivtc, int i1, int i2)
406 GstIvtcField *f1, *f2;
409 g_return_val_if_fail (i1 >= 0 && i1 < ivtc->n_fields, 0);
410 g_return_val_if_fail (i2 >= 0 && i2 < ivtc->n_fields, 0);
412 f1 = &ivtc->fields[i1];
413 f2 = &ivtc->fields[i2];
415 if (f1->parity == TOP_FIELD) {
416 score = get_comb_score (&f1->frame, &f2->frame);
418 score = get_comb_score (&f2->frame, &f1->frame);
421 GST_DEBUG ("score %d", score);
426 #define GET_LINE(frame,comp,line) (((unsigned char *)(frame)->data[k]) + \
427 (line) * GST_VIDEO_FRAME_COMP_STRIDE((frame), (comp)))
428 #define GET_LINE_IL(top,bottom,comp,line) \
429 (((unsigned char *)(((line)&1)?(bottom):(top))->data[k]) + \
430 (line) * GST_VIDEO_FRAME_COMP_STRIDE((top), (comp)))
433 reconstruct (GstIvtc * ivtc, GstVideoFrame * dest_frame, int i1, int i2)
435 GstVideoFrame *top, *bottom;
439 g_return_if_fail (i1 >= 0 && i1 < ivtc->n_fields);
440 g_return_if_fail (i2 >= 0 && i2 < ivtc->n_fields);
442 if (ivtc->fields[i1].parity == TOP_FIELD) {
443 top = &ivtc->fields[i1].frame;
444 bottom = &ivtc->fields[i2].frame;
446 bottom = &ivtc->fields[i1].frame;
447 top = &ivtc->fields[i2].frame;
450 for (k = 0; k < 3; k++) {
451 height = GST_VIDEO_FRAME_COMP_HEIGHT (top, k);
452 width = GST_VIDEO_FRAME_COMP_WIDTH (top, k);
453 for (j = 0; j < height; j++) {
454 guint8 *dest = GET_LINE (dest_frame, k, j);
455 guint8 *src = GET_LINE_IL (top, bottom, k, j);
457 memcpy (dest, src, width);
464 reconstruct_single (GstIvtc * ivtc, GstVideoFrame * dest_frame, int i1)
470 GstIvtcField *field = &ivtc->fields[i1];
472 for (k = 0; k < 3; k++) {
473 height = GST_VIDEO_FRAME_COMP_HEIGHT (dest_frame, k);
474 width = GST_VIDEO_FRAME_COMP_WIDTH (dest_frame, k);
475 for (j = 0; j < height; j++) {
476 if ((j & 1) == field->parity) {
477 memcpy (GET_LINE (dest_frame, k, j),
478 GET_LINE (&field->frame, k, j), width);
480 if (j == 0 || j == height - 1) {
481 memcpy (GET_LINE (dest_frame, k, j),
482 GET_LINE (&field->frame, k, (j ^ 1)), width);
484 guint8 *dest = GET_LINE (dest_frame, k, j);
485 guint8 *line1 = GET_LINE (&field->frame, k, j - 1);
486 guint8 *line2 = GET_LINE (&field->frame, k, j + 1);
488 for (i = 0; i < width; i++) {
489 dest[i] = (line1[i] + line2[i] + 1) >> 1;
498 gst_ivtc_retire_fields (GstIvtc * ivtc, int n_fields)
505 for (i = 0; i < n_fields; i++) {
506 gst_video_frame_unmap (&ivtc->fields[i].frame);
507 gst_buffer_unref (ivtc->fields[i].buffer);
510 memmove (ivtc->fields, ivtc->fields + n_fields,
511 sizeof (GstIvtcField) * (ivtc->n_fields - n_fields));
512 ivtc->n_fields -= n_fields;
516 gst_ivtc_transform (GstBaseTransform * trans, GstBuffer * inbuf,
519 GstIvtc *ivtc = GST_IVTC (trans);
522 GST_DEBUG_OBJECT (ivtc, "transform");
524 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_TFF)) {
525 add_field (ivtc, inbuf, TOP_FIELD, 0);
526 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_ONEFIELD)) {
527 add_field (ivtc, inbuf, BOTTOM_FIELD, 1);
528 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_RFF)) {
529 add_field (ivtc, inbuf, TOP_FIELD, 2);
533 add_field (ivtc, inbuf, BOTTOM_FIELD, 0);
534 if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_ONEFIELD)) {
535 add_field (ivtc, inbuf, TOP_FIELD, 1);
536 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_RFF)) {
537 add_field (ivtc, inbuf, BOTTOM_FIELD, 2);
542 while (ivtc->n_fields > 0 &&
543 ivtc->fields[0].ts + GST_MSECOND * 50 < ivtc->current_ts) {
544 GST_DEBUG ("retiring early field");
545 gst_ivtc_retire_fields (ivtc, 1);
548 GST_DEBUG ("n_fields %d", ivtc->n_fields);
549 if (ivtc->n_fields < 4) {
550 return GST_BASE_TRANSFORM_FLOW_DROPPED;
553 gst_ivtc_construct_frame (ivtc, outbuf);
554 while (ivtc->n_fields >= 4) {
556 buf = gst_buffer_copy (outbuf);
557 GST_DEBUG ("pushing extra frame");
558 ret = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (trans), buf);
559 if (ret != GST_FLOW_OK) {
563 gst_ivtc_construct_frame (ivtc, outbuf);
570 gst_ivtc_construct_frame (GstIvtc * ivtc, GstBuffer * outbuf)
573 int prev_score, next_score;
574 GstVideoFrame dest_frame;
579 if (ivtc->fields[anchor_index].ts < ivtc->current_ts) {
585 prev_score = similarity (ivtc, anchor_index - 1, anchor_index);
586 next_score = similarity (ivtc, anchor_index, anchor_index + 1);
588 gst_video_frame_map (&dest_frame, &ivtc->src_video_info, outbuf,
591 #define THRESHOLD 100
592 if (prev_score < THRESHOLD) {
593 if (forward_ok && next_score < prev_score) {
594 reconstruct (ivtc, &dest_frame, anchor_index, anchor_index + 1);
595 n_retire = anchor_index + 2;
597 if (prev_score >= THRESHOLD / 2) {
598 GST_INFO ("borderline prev (%d, %d)", prev_score, next_score);
600 reconstruct (ivtc, &dest_frame, anchor_index, anchor_index - 1);
601 n_retire = anchor_index + 1;
603 } else if (next_score < THRESHOLD) {
604 if (next_score >= THRESHOLD / 2) {
605 GST_INFO ("borderline prev (%d, %d)", prev_score, next_score);
607 reconstruct (ivtc, &dest_frame, anchor_index, anchor_index + 1);
609 n_retire = anchor_index + 2;
611 n_retire = anchor_index + 1;
614 if (prev_score < THRESHOLD * 2 || next_score < THRESHOLD * 2) {
615 GST_INFO ("borderline single (%d, %d)", prev_score, next_score);
617 reconstruct_single (ivtc, &dest_frame, anchor_index);
618 n_retire = anchor_index + 1;
621 GST_DEBUG ("retiring %d", n_retire);
622 gst_ivtc_retire_fields (ivtc, n_retire);
624 gst_video_frame_unmap (&dest_frame);
626 GST_BUFFER_PTS (outbuf) = ivtc->current_ts;
627 GST_BUFFER_DTS (outbuf) = ivtc->current_ts;
628 /* FIXME this is not how to produce durations */
629 GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale (GST_SECOND,
630 ivtc->src_video_info.fps_d, ivtc->src_video_info.fps_n);
631 GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED |
632 GST_VIDEO_BUFFER_FLAG_TFF | GST_VIDEO_BUFFER_FLAG_RFF |
633 GST_VIDEO_BUFFER_FLAG_ONEFIELD);
634 ivtc->current_ts += GST_BUFFER_DURATION (outbuf);
639 get_comb_score (GstVideoFrame * top, GstVideoFrame * bottom)
642 int thisline[MAX_WIDTH];
648 height = GST_VIDEO_FRAME_COMP_HEIGHT (top, 0);
649 width = GST_VIDEO_FRAME_COMP_WIDTH (top, 0);
651 memset (thisline, 0, sizeof (thisline));
654 /* remove a few lines from top and bottom, as they sometimes contain
656 for (j = 2; j < height - 2; j++) {
657 guint8 *src1 = GET_LINE_IL (top, bottom, 0, j - 1);
658 guint8 *src2 = GET_LINE_IL (top, bottom, 0, j);
659 guint8 *src3 = GET_LINE_IL (top, bottom, 0, j + 1);
662 for (i = 0; i < width; i++) {
663 if (src2[i] < MIN (src1[i], src3[i]) - 5 ||
664 src2[i] > MAX (src1[i], src3[i]) + 5) {
666 thisline[i] += thisline[i - 1];
669 if (thisline[i] > 1000)
674 if (thisline[i] > 100) {
680 GST_DEBUG ("score %d", score);
688 plugin_init (GstPlugin * plugin)
690 gst_element_register (plugin, "ivtc", GST_RANK_NONE, GST_TYPE_IVTC);
691 gst_element_register (plugin, "combdetect", GST_RANK_NONE,
692 GST_TYPE_COMB_DETECT);
697 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
701 plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)