27c8b333bd4f00747a29b73baf08985dbddb7840
[platform/upstream/gstreamer.git] / gst / ivtc / gstivtc.c
1 /* GStreamer
2  * Copyright (C) 2013 David Schleef <ds@schleef.org>
3  * Copyright (C) 2013 Rdio Inc <ingestions@rdio.com>
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 Street, Suite 500,
18  * Boston, MA 02110-1335, USA.
19  */
20 /**
21  * SECTION:element-gstivtc
22  *
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.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
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
33  * ]|
34  *
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.
39  * </refsect2>
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <gst/gst.h>
47 #include <gst/base/gstbasetransform.h>
48 #include <gst/video/video.h>
49 #include "gstivtc.h"
50 #include <string.h>
51 #include <math.h>
52
53 /* only because element registration is in this file */
54 #include "gstcombdetect.h"
55
56 GST_DEBUG_CATEGORY_STATIC (gst_ivtc_debug_category);
57 #define GST_CAT_DEFAULT gst_ivtc_debug_category
58
59 /* prototypes */
60
61
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);
68
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,
74     GstCaps * outcaps);
75 static gboolean gst_ivtc_transform_size (GstBaseTransform * trans,
76     GstPadDirection direction, GstCaps * caps, gsize size, GstCaps * othercaps,
77     gsize * othersize);
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,
81     GstEvent * event);
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);
87
88 static int get_comb_score (GstVideoFrame * top, GstVideoFrame * bottom);
89
90 enum
91 {
92   PROP_0
93 };
94
95 /* pad templates */
96
97 #define MAX_WIDTH 2048
98 #define VIDEO_CAPS \
99   "video/x-raw, " \
100   "format = (string) { I420, Y444, Y42B }, " \
101   "width = [1, 2048], " \
102   "height = " GST_VIDEO_SIZE_RANGE ", " \
103   "framerate = " GST_VIDEO_FPS_RANGE
104
105 static GstStaticPadTemplate gst_ivtc_sink_template =
106 GST_STATIC_PAD_TEMPLATE ("sink",
107     GST_PAD_SINK,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS (VIDEO_CAPS)
110     );
111
112 static GstStaticPadTemplate gst_ivtc_src_template =
113 GST_STATIC_PAD_TEMPLATE ("src",
114     GST_PAD_SRC,
115     GST_PAD_ALWAYS,
116     GST_STATIC_CAPS (VIDEO_CAPS)
117     );
118
119
120 /* class initialization */
121
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"));
125
126 static void
127 gst_ivtc_class_init (GstIvtcClass * klass)
128 {
129   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
130   GstBaseTransformClass *base_transform_class =
131       GST_BASE_TRANSFORM_CLASS (klass);
132
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));
139
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>");
143
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);
158 }
159
160 static void
161 gst_ivtc_init (GstIvtc * ivtc)
162 {
163 }
164
165 void
166 gst_ivtc_set_property (GObject * object, guint property_id,
167     const GValue * value, GParamSpec * pspec)
168 {
169   GstIvtc *ivtc = GST_IVTC (object);
170
171   GST_DEBUG_OBJECT (ivtc, "set_property");
172
173   switch (property_id) {
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
176       break;
177   }
178 }
179
180 void
181 gst_ivtc_get_property (GObject * object, guint property_id,
182     GValue * value, GParamSpec * pspec)
183 {
184   GstIvtc *ivtc = GST_IVTC (object);
185
186   GST_DEBUG_OBJECT (ivtc, "get_property");
187
188   switch (property_id) {
189     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
191       break;
192   }
193 }
194
195 void
196 gst_ivtc_dispose (GObject * object)
197 {
198   GstIvtc *ivtc = GST_IVTC (object);
199
200   GST_DEBUG_OBJECT (ivtc, "dispose");
201
202   /* clean up as possible.  may be called multiple times */
203
204   G_OBJECT_CLASS (gst_ivtc_parent_class)->dispose (object);
205 }
206
207 void
208 gst_ivtc_finalize (GObject * object)
209 {
210   GstIvtc *ivtc = GST_IVTC (object);
211
212   GST_DEBUG_OBJECT (ivtc, "finalize");
213
214   /* clean up object here */
215
216   G_OBJECT_CLASS (gst_ivtc_parent_class)->finalize (object);
217 }
218
219 static GstCaps *
220 gst_ivtc_transform_caps (GstBaseTransform * trans,
221     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
222 {
223   GstCaps *othercaps;
224   int i;
225
226   othercaps = gst_caps_copy (caps);
227
228   if (direction == GST_PAD_SRC) {
229     GValue value = G_VALUE_INIT;
230     GValue v = G_VALUE_INIT;
231
232     g_value_init (&value, GST_TYPE_LIST);
233     g_value_init (&v, G_TYPE_STRING);
234
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);
241
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");
246     }
247     g_value_reset (&value);
248     g_value_reset (&v);
249   } else {
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");
255     }
256   }
257
258   if (filter) {
259     GstCaps *intersect;
260
261     intersect = gst_caps_intersect (othercaps, filter);
262     gst_caps_unref (othercaps);
263     othercaps = intersect;
264   }
265
266   return othercaps;
267 }
268
269 static GstCaps *
270 gst_ivtc_fixate_caps (GstBaseTransform * trans, GstPadDirection direction,
271     GstCaps * caps, GstCaps * othercaps)
272 {
273   GstCaps *result;
274
275   GST_DEBUG_OBJECT (trans, "fixating caps %" GST_PTR_FORMAT, othercaps);
276
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);
280   }
281
282   result = gst_caps_fixate (result);
283
284   return result;
285 }
286
287 static gboolean
288 gst_ivtc_set_caps (GstBaseTransform * trans, GstCaps * incaps,
289     GstCaps * outcaps)
290 {
291   GstIvtc *ivtc = GST_IVTC (trans);
292
293   gst_video_info_from_caps (&ivtc->sink_video_info, incaps);
294   gst_video_info_from_caps (&ivtc->src_video_info, outcaps);
295
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));
300
301   return TRUE;
302 }
303
304 /* transform size */
305 static gboolean
306 gst_ivtc_transform_size (GstBaseTransform * trans, GstPadDirection direction,
307     GstCaps * caps, gsize size, GstCaps * othercaps, gsize * othersize)
308 {
309   GstIvtc *ivtc = GST_IVTC (trans);
310
311   GST_DEBUG_OBJECT (ivtc, "transform_size");
312
313   return TRUE;
314 }
315
316 /* states */
317 static gboolean
318 gst_ivtc_start (GstBaseTransform * trans)
319 {
320   GstIvtc *ivtc = GST_IVTC (trans);
321
322   GST_DEBUG_OBJECT (ivtc, "start");
323
324   return TRUE;
325 }
326
327 static gboolean
328 gst_ivtc_stop (GstBaseTransform * trans)
329 {
330   GstIvtc *ivtc = GST_IVTC (trans);
331
332   GST_DEBUG_OBJECT (ivtc, "stop");
333   gst_ivtc_flush (ivtc);
334
335   return TRUE;
336 }
337
338 /* sink and src pad event handlers */
339 static gboolean
340 gst_ivtc_sink_event (GstBaseTransform * trans, GstEvent * event)
341 {
342   GstIvtc *ivtc = GST_IVTC (trans);
343
344   GST_DEBUG_OBJECT (ivtc, "sink_event");
345
346   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
347     const GstSegment *seg;
348
349     gst_ivtc_flush (ivtc);
350
351     /* FIXME this should handle update events */
352
353     gst_event_parse_segment (event, &seg);
354     gst_segment_copy_into (seg, &ivtc->segment);
355     ivtc->current_ts = ivtc->segment.start;
356   }
357
358   return GST_BASE_TRANSFORM_CLASS (gst_ivtc_parent_class)->sink_event (trans,
359       event);
360 }
361
362 static void
363 gst_ivtc_flush (GstIvtc * ivtc)
364 {
365   GST_FIXME_OBJECT (ivtc, "not sending flushed fields to srcpad");
366
367   gst_ivtc_retire_fields (ivtc, ivtc->n_fields);
368 }
369
370 enum
371 {
372   TOP_FIELD = 0,
373   BOTTOM_FIELD = 1
374 };
375
376 static void
377 add_field (GstIvtc * ivtc, GstBuffer * buffer, int parity, int index)
378 {
379   int i = ivtc->n_fields;
380   GstClockTime ts;
381   GstIvtcField *field = &ivtc->fields[i];
382
383   g_return_if_fail (i < GST_IVTC_MAX_FIELDS);
384
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 */
388     return;
389   }
390
391   GST_DEBUG ("adding field %d", i);
392
393   field->buffer = gst_buffer_ref (buffer);
394   field->parity = parity;
395   field->ts = ts;
396
397   gst_video_frame_map (&ivtc->fields[i].frame, &ivtc->sink_video_info,
398       buffer, GST_MAP_READ);
399
400   ivtc->n_fields++;
401 }
402
403 static int
404 similarity (GstIvtc * ivtc, int i1, int i2)
405 {
406   GstIvtcField *f1, *f2;
407   int score;
408
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);
411
412   f1 = &ivtc->fields[i1];
413   f2 = &ivtc->fields[i2];
414
415   if (f1->parity == TOP_FIELD) {
416     score = get_comb_score (&f1->frame, &f2->frame);
417   } else {
418     score = get_comb_score (&f2->frame, &f1->frame);
419   }
420
421   GST_DEBUG ("score %d", score);
422
423   return score;
424 }
425
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)))
431
432 static void
433 reconstruct (GstIvtc * ivtc, GstVideoFrame * dest_frame, int i1, int i2)
434 {
435   GstVideoFrame *top, *bottom;
436   int width, height;
437   int j, k;
438
439   g_return_if_fail (i1 >= 0 && i1 < ivtc->n_fields);
440   g_return_if_fail (i2 >= 0 && i2 < ivtc->n_fields);
441
442   if (ivtc->fields[i1].parity == TOP_FIELD) {
443     top = &ivtc->fields[i1].frame;
444     bottom = &ivtc->fields[i2].frame;
445   } else {
446     bottom = &ivtc->fields[i1].frame;
447     top = &ivtc->fields[i2].frame;
448   }
449
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);
456
457       memcpy (dest, src, width);
458     }
459   }
460
461 }
462
463 static void
464 reconstruct_single (GstIvtc * ivtc, GstVideoFrame * dest_frame, int i1)
465 {
466   int j;
467   int k;
468   int height;
469   int width;
470   GstIvtcField *field = &ivtc->fields[i1];
471
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);
479       } else {
480         if (j == 0 || j == height - 1) {
481           memcpy (GET_LINE (dest_frame, k, j),
482               GET_LINE (&field->frame, k, (j ^ 1)), width);
483         } else {
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);
487           int i;
488           for (i = 0; i < width; i++) {
489             dest[i] = (line1[i] + line2[i] + 1) >> 1;
490           }
491         }
492       }
493     }
494   }
495 }
496
497 static void
498 gst_ivtc_retire_fields (GstIvtc * ivtc, int n_fields)
499 {
500   int i;
501
502   if (n_fields == 0)
503     return;
504
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);
508   }
509
510   memmove (ivtc->fields, ivtc->fields + n_fields,
511       sizeof (GstIvtcField) * (ivtc->n_fields - n_fields));
512   ivtc->n_fields -= n_fields;
513 }
514
515 static GstFlowReturn
516 gst_ivtc_transform (GstBaseTransform * trans, GstBuffer * inbuf,
517     GstBuffer * outbuf)
518 {
519   GstIvtc *ivtc = GST_IVTC (trans);
520   GstFlowReturn ret;
521
522   GST_DEBUG_OBJECT (ivtc, "transform");
523
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);
530       }
531     }
532   } else {
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);
538       }
539     }
540   }
541
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);
546   }
547
548   GST_DEBUG ("n_fields %d", ivtc->n_fields);
549   if (ivtc->n_fields < 4) {
550     return GST_BASE_TRANSFORM_FLOW_DROPPED;
551   }
552
553   gst_ivtc_construct_frame (ivtc, outbuf);
554   while (ivtc->n_fields >= 4) {
555     GstBuffer *buf;
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) {
560       return ret;
561     }
562
563     gst_ivtc_construct_frame (ivtc, outbuf);
564   }
565
566   return GST_FLOW_OK;
567 }
568
569 static void
570 gst_ivtc_construct_frame (GstIvtc * ivtc, GstBuffer * outbuf)
571 {
572   int anchor_index;
573   int prev_score, next_score;
574   GstVideoFrame dest_frame;
575   int n_retire;
576   gboolean forward_ok;
577
578   anchor_index = 1;
579   if (ivtc->fields[anchor_index].ts < ivtc->current_ts) {
580     forward_ok = TRUE;
581   } else {
582     forward_ok = FALSE;
583   }
584
585   prev_score = similarity (ivtc, anchor_index - 1, anchor_index);
586   next_score = similarity (ivtc, anchor_index, anchor_index + 1);
587
588   gst_video_frame_map (&dest_frame, &ivtc->src_video_info, outbuf,
589       GST_MAP_WRITE);
590
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;
596     } else {
597       if (prev_score >= THRESHOLD / 2) {
598         GST_INFO ("borderline prev (%d, %d)", prev_score, next_score);
599       }
600       reconstruct (ivtc, &dest_frame, anchor_index, anchor_index - 1);
601       n_retire = anchor_index + 1;
602     }
603   } else if (next_score < THRESHOLD) {
604     if (next_score >= THRESHOLD / 2) {
605       GST_INFO ("borderline prev (%d, %d)", prev_score, next_score);
606     }
607     reconstruct (ivtc, &dest_frame, anchor_index, anchor_index + 1);
608     if (forward_ok) {
609       n_retire = anchor_index + 2;
610     } else {
611       n_retire = anchor_index + 1;
612     }
613   } else {
614     if (prev_score < THRESHOLD * 2 || next_score < THRESHOLD * 2) {
615       GST_INFO ("borderline single (%d, %d)", prev_score, next_score);
616     }
617     reconstruct_single (ivtc, &dest_frame, anchor_index);
618     n_retire = anchor_index + 1;
619   }
620
621   GST_DEBUG ("retiring %d", n_retire);
622   gst_ivtc_retire_fields (ivtc, n_retire);
623
624   gst_video_frame_unmap (&dest_frame);
625
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);
635
636 }
637
638 static int
639 get_comb_score (GstVideoFrame * top, GstVideoFrame * bottom)
640 {
641   int j;
642   int thisline[MAX_WIDTH];
643   int score = 0;
644   int height;
645   int width;
646   int k;
647
648   height = GST_VIDEO_FRAME_COMP_HEIGHT (top, 0);
649   width = GST_VIDEO_FRAME_COMP_WIDTH (top, 0);
650
651   memset (thisline, 0, sizeof (thisline));
652
653   k = 0;
654   /* remove a few lines from top and bottom, as they sometimes contain
655    * artifacts */
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);
660     int i;
661
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) {
665         if (i > 0) {
666           thisline[i] += thisline[i - 1];
667         }
668         thisline[i]++;
669         if (thisline[i] > 1000)
670           thisline[i] = 1000;
671       } else {
672         thisline[i] = 0;
673       }
674       if (thisline[i] > 100) {
675         score++;
676       }
677     }
678   }
679
680   GST_DEBUG ("score %d", score);
681
682   return score;
683 }
684
685
686
687 static gboolean
688 plugin_init (GstPlugin * plugin)
689 {
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);
693
694   return TRUE;
695 }
696
697 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
698     GST_VERSION_MINOR,
699     ivtc,
700     "Inverse Telecine",
701     plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)