Move files from gst-plugins-bad into the "subprojects/gst-plugins-bad/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / 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-ivtc
22  * @title: gstivtc
23  *
24  * The ivtc element is an inverse telecine filter.  It takes interlaced
25  * video that was created from progressive content using a telecine
26  * filter, and reconstructs the original progressive content.
27  *
28  * ## Example launch line
29  * |[
30  * gst-launch-1.0 -v videotestsrc pattern=ball ! video/x-raw,framerate=24/1 !
31  *     interlace !
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  *
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 GstCaps *gst_ivtc_transform_caps (GstBaseTransform * trans,
63     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
64 static GstCaps *gst_ivtc_fixate_caps (GstBaseTransform * trans,
65     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
66 static gboolean gst_ivtc_set_caps (GstBaseTransform * trans, GstCaps * incaps,
67     GstCaps * outcaps);
68 static gboolean gst_ivtc_sink_event (GstBaseTransform * trans,
69     GstEvent * event);
70 static GstFlowReturn gst_ivtc_transform (GstBaseTransform * trans,
71     GstBuffer * inbuf, GstBuffer * outbuf);
72 static void gst_ivtc_flush (GstIvtc * ivtc);
73 static void gst_ivtc_retire_fields (GstIvtc * ivtc, int n_fields);
74 static void gst_ivtc_construct_frame (GstIvtc * itvc, GstBuffer * outbuf);
75
76 static int get_comb_score (GstVideoFrame * top, GstVideoFrame * bottom);
77
78 enum
79 {
80   PROP_0
81 };
82
83 /* pad templates */
84
85 #define MAX_WIDTH 2048
86 #define VIDEO_CAPS \
87   "video/x-raw, " \
88   "format = (string) { I420, Y444, Y42B }, " \
89   "width = [1, 2048], " \
90   "height = " GST_VIDEO_SIZE_RANGE ", " \
91   "framerate = " GST_VIDEO_FPS_RANGE
92
93 static GstStaticPadTemplate gst_ivtc_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
95     GST_PAD_SINK,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS (VIDEO_CAPS)
98     );
99
100 static GstStaticPadTemplate gst_ivtc_src_template =
101 GST_STATIC_PAD_TEMPLATE ("src",
102     GST_PAD_SRC,
103     GST_PAD_ALWAYS,
104     GST_STATIC_CAPS (VIDEO_CAPS)
105     );
106
107
108 /* class initialization */
109
110 G_DEFINE_TYPE_WITH_CODE (GstIvtc, gst_ivtc, GST_TYPE_BASE_TRANSFORM,
111     GST_DEBUG_CATEGORY_INIT (gst_ivtc_debug_category, "ivtc", 0,
112         "debug category for ivtc element"));
113 GST_ELEMENT_REGISTER_DEFINE (ivtc, "ivtc", GST_RANK_NONE, GST_TYPE_IVTC);
114
115 static void
116 gst_ivtc_class_init (GstIvtcClass * klass)
117 {
118   GstBaseTransformClass *base_transform_class =
119       GST_BASE_TRANSFORM_CLASS (klass);
120
121   /* Setting up pads and setting metadata should be moved to
122      base_class_init if you intend to subclass this class. */
123   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
124       &gst_ivtc_sink_template);
125   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
126       &gst_ivtc_src_template);
127
128   gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
129       "Inverse Telecine", "Video/Filter", "Inverse Telecine Filter",
130       "David Schleef <ds@schleef.org>");
131
132   base_transform_class->transform_caps =
133       GST_DEBUG_FUNCPTR (gst_ivtc_transform_caps);
134   base_transform_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_ivtc_fixate_caps);
135   base_transform_class->set_caps = GST_DEBUG_FUNCPTR (gst_ivtc_set_caps);
136   base_transform_class->sink_event = GST_DEBUG_FUNCPTR (gst_ivtc_sink_event);
137   base_transform_class->transform = GST_DEBUG_FUNCPTR (gst_ivtc_transform);
138 }
139
140 static void
141 gst_ivtc_init (GstIvtc * ivtc)
142 {
143 }
144
145 static GstCaps *
146 gst_ivtc_transform_caps (GstBaseTransform * trans,
147     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
148 {
149   GstCaps *othercaps;
150   int i;
151
152   othercaps = gst_caps_copy (caps);
153
154   if (direction == GST_PAD_SRC) {
155     GValue value = G_VALUE_INIT;
156     GValue v = G_VALUE_INIT;
157
158     g_value_init (&value, GST_TYPE_LIST);
159     g_value_init (&v, G_TYPE_STRING);
160
161     g_value_set_string (&v, "interleaved");
162     gst_value_list_append_value (&value, &v);
163     g_value_set_string (&v, "mixed");
164     gst_value_list_append_value (&value, &v);
165     g_value_set_string (&v, "progressive");
166     gst_value_list_append_value (&value, &v);
167
168     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
169       GstStructure *structure = gst_caps_get_structure (othercaps, i);
170       gst_structure_set_value (structure, "interlace-mode", &value);
171       gst_structure_remove_field (structure, "framerate");
172     }
173     g_value_unset (&value);
174     g_value_unset (&v);
175   } else {
176     for (i = 0; i < gst_caps_get_size (othercaps); i++) {
177       GstStructure *structure = gst_caps_get_structure (othercaps, i);
178       gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
179           "progressive", NULL);
180       gst_structure_remove_field (structure, "framerate");
181     }
182   }
183
184   if (filter) {
185     GstCaps *intersect;
186
187     intersect = gst_caps_intersect (othercaps, filter);
188     gst_caps_unref (othercaps);
189     othercaps = intersect;
190   }
191
192   return othercaps;
193 }
194
195 static GstCaps *
196 gst_ivtc_fixate_caps (GstBaseTransform * trans, GstPadDirection direction,
197     GstCaps * caps, GstCaps * othercaps)
198 {
199   GstCaps *result;
200
201   GST_DEBUG_OBJECT (trans, "fixating caps %" GST_PTR_FORMAT, othercaps);
202
203   result = gst_caps_make_writable (othercaps);
204   if (direction == GST_PAD_SINK) {
205     GstVideoInfo info;
206     if (gst_video_info_from_caps (&info, caps)) {
207       /* Smarter decision */
208       GST_DEBUG_OBJECT (trans, "Input framerate is %d/%d", info.fps_n,
209           info.fps_d);
210       if (info.fps_n == 30000 && info.fps_d == 1001)
211         gst_caps_set_simple (result, "framerate", GST_TYPE_FRACTION, 24000,
212             1001, NULL);
213       else
214         gst_caps_set_simple (result, "framerate", GST_TYPE_FRACTION, 24, 1,
215             NULL);
216     } else {
217       gst_caps_set_simple (result, "framerate", GST_TYPE_FRACTION, 24, 1, NULL);
218     }
219   }
220
221   result = gst_caps_fixate (result);
222
223   return result;
224 }
225
226 static gboolean
227 gst_ivtc_set_caps (GstBaseTransform * trans, GstCaps * incaps,
228     GstCaps * outcaps)
229 {
230   GstIvtc *ivtc = GST_IVTC (trans);
231
232   gst_video_info_from_caps (&ivtc->sink_video_info, incaps);
233   gst_video_info_from_caps (&ivtc->src_video_info, outcaps);
234
235   ivtc->field_duration = gst_util_uint64_scale_int (GST_SECOND,
236       ivtc->sink_video_info.fps_d, ivtc->sink_video_info.fps_n * 2);
237   GST_DEBUG_OBJECT (trans, "field duration %" GST_TIME_FORMAT,
238       GST_TIME_ARGS (ivtc->field_duration));
239
240   return TRUE;
241 }
242
243 /* sink and src pad event handlers */
244 static gboolean
245 gst_ivtc_sink_event (GstBaseTransform * trans, GstEvent * event)
246 {
247   GstIvtc *ivtc = GST_IVTC (trans);
248
249   GST_DEBUG_OBJECT (ivtc, "sink_event");
250
251   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
252     const GstSegment *seg;
253
254     gst_ivtc_flush (ivtc);
255
256     /* FIXME this should handle update events */
257
258     gst_event_parse_segment (event, &seg);
259     gst_segment_copy_into (seg, &ivtc->segment);
260     ivtc->current_ts = ivtc->segment.start;
261   }
262
263   return GST_BASE_TRANSFORM_CLASS (gst_ivtc_parent_class)->sink_event (trans,
264       event);
265 }
266
267 static void
268 gst_ivtc_flush (GstIvtc * ivtc)
269 {
270   if (ivtc->n_fields > 0) {
271     GST_FIXME_OBJECT (ivtc, "not sending flushed fields to srcpad");
272   }
273
274   gst_ivtc_retire_fields (ivtc, ivtc->n_fields);
275 }
276
277 enum
278 {
279   TOP_FIELD = 0,
280   BOTTOM_FIELD = 1
281 };
282
283 static void
284 add_field (GstIvtc * ivtc, GstBuffer * buffer, int parity, int index)
285 {
286   int i = ivtc->n_fields;
287   GstClockTime ts;
288   GstIvtcField *field = &ivtc->fields[i];
289
290   g_return_if_fail (i < GST_IVTC_MAX_FIELDS);
291
292   ts = GST_BUFFER_PTS (buffer) + index * ivtc->field_duration;
293   if (ts + ivtc->field_duration < ivtc->segment.start) {
294     /* drop, it's before our segment */
295     return;
296   }
297
298   GST_DEBUG ("adding field %d", i);
299
300   field->buffer = gst_buffer_ref (buffer);
301   field->parity = parity;
302   field->ts = ts;
303
304   gst_video_frame_map (&ivtc->fields[i].frame, &ivtc->sink_video_info,
305       buffer, GST_MAP_READ);
306
307   ivtc->n_fields++;
308 }
309
310 static int
311 similarity (GstIvtc * ivtc, int i1, int i2)
312 {
313   GstIvtcField *f1, *f2;
314   int score;
315
316   g_return_val_if_fail (i1 >= 0 && i1 < ivtc->n_fields, 0);
317   g_return_val_if_fail (i2 >= 0 && i2 < ivtc->n_fields, 0);
318
319   f1 = &ivtc->fields[i1];
320   f2 = &ivtc->fields[i2];
321
322   if (f1->parity == TOP_FIELD) {
323     score = get_comb_score (&f1->frame, &f2->frame);
324   } else {
325     score = get_comb_score (&f2->frame, &f1->frame);
326   }
327
328   GST_DEBUG ("score %d", score);
329
330   return score;
331 }
332
333 #define GET_LINE(frame,comp,line) (((unsigned char *)(frame)->data[k]) + \
334       (line) * GST_VIDEO_FRAME_COMP_STRIDE((frame), (comp)))
335 #define GET_LINE_IL(top,bottom,comp,line) \
336   (((unsigned char *)(((line)&1)?(bottom):(top))->data[k]) + \
337       (line) * GST_VIDEO_FRAME_COMP_STRIDE((top), (comp)))
338
339 static void
340 reconstruct (GstIvtc * ivtc, GstVideoFrame * dest_frame, int i1, int i2)
341 {
342   GstVideoFrame *top, *bottom;
343   int width, height;
344   int j, k;
345
346   g_return_if_fail (i1 >= 0 && i1 < ivtc->n_fields);
347   g_return_if_fail (i2 >= 0 && i2 < ivtc->n_fields);
348
349   if (ivtc->fields[i1].parity == TOP_FIELD) {
350     top = &ivtc->fields[i1].frame;
351     bottom = &ivtc->fields[i2].frame;
352   } else {
353     bottom = &ivtc->fields[i1].frame;
354     top = &ivtc->fields[i2].frame;
355   }
356
357   for (k = 0; k < 3; k++) {
358     height = GST_VIDEO_FRAME_COMP_HEIGHT (top, k);
359     width = GST_VIDEO_FRAME_COMP_WIDTH (top, k);
360     for (j = 0; j < height; j++) {
361       guint8 *dest = GET_LINE (dest_frame, k, j);
362       guint8 *src = GET_LINE_IL (top, bottom, k, j);
363
364       memcpy (dest, src, width);
365     }
366   }
367
368 }
369
370 static int
371 reconstruct_line (guint8 * line1, guint8 * line2, int i, int a, int b, int c,
372     int d)
373 {
374   int x;
375
376   x = line1[i - 3] * a;
377   x += line1[i - 2] * b;
378   x += line1[i - 1] * c;
379   x += line1[i - 0] * d;
380   x += line2[i + 0] * d;
381   x += line2[i + 1] * c;
382   x += line2[i + 2] * b;
383   x += line2[i + 3] * a;
384   return (x + 16) >> 5;
385 }
386
387
388 static void
389 reconstruct_single (GstIvtc * ivtc, GstVideoFrame * dest_frame, int i1)
390 {
391   int j;
392   int k;
393   int height;
394   int width;
395   GstIvtcField *field = &ivtc->fields[i1];
396
397   for (k = 0; k < 1; k++) {
398     height = GST_VIDEO_FRAME_COMP_HEIGHT (dest_frame, k);
399     width = GST_VIDEO_FRAME_COMP_WIDTH (dest_frame, k);
400     for (j = 0; j < height; j++) {
401       if ((j & 1) == field->parity) {
402         memcpy (GET_LINE (dest_frame, k, j),
403             GET_LINE (&field->frame, k, j), width);
404       } else {
405         if (j == 0 || j == height - 1) {
406           memcpy (GET_LINE (dest_frame, k, j),
407               GET_LINE (&field->frame, k, (j ^ 1)), width);
408         } else {
409           guint8 *dest = GET_LINE (dest_frame, k, j);
410           guint8 *line1 = GET_LINE (&field->frame, k, j - 1);
411           guint8 *line2 = GET_LINE (&field->frame, k, j + 1);
412           int i;
413
414 #define MARGIN 3
415           for (i = MARGIN; i < width - MARGIN; i++) {
416             int dx, dy;
417
418             dx = -line1[i - 1] - line2[i - 1] + line1[i + 1] + line2[i + 1];
419             dx *= 2;
420
421             dy = -line1[i - 1] - 2 * line1[i] - line1[i + 1]
422                 + line2[i - 1] + 2 * line2[i] + line2[i + 1];
423             if (dy < 0) {
424               dy = -dy;
425               dx = -dx;
426             }
427
428             if (dx == 0 && dy == 0) {
429               dest[i] = (line1[i] + line2[i] + 1) >> 1;
430             } else if (dx < 0) {
431               if (dx < -2 * dy) {
432                 dest[i] = reconstruct_line (line1, line2, i, 0, 0, 0, 16);
433               } else if (dx < -dy) {
434                 dest[i] = reconstruct_line (line1, line2, i, 0, 0, 8, 8);
435               } else if (2 * dx < -dy) {
436                 dest[i] = reconstruct_line (line1, line2, i, 0, 4, 8, 4);
437               } else if (3 * dx < -dy) {
438                 dest[i] = reconstruct_line (line1, line2, i, 1, 7, 7, 1);
439               } else {
440                 dest[i] = reconstruct_line (line1, line2, i, 4, 8, 4, 0);
441               }
442             } else {
443               if (dx > 2 * dy) {
444                 dest[i] = reconstruct_line (line2, line1, i, 0, 0, 0, 16);
445               } else if (dx > dy) {
446                 dest[i] = reconstruct_line (line2, line1, i, 0, 0, 8, 8);
447               } else if (2 * dx > dy) {
448                 dest[i] = reconstruct_line (line2, line1, i, 0, 4, 8, 4);
449               } else if (3 * dx > dy) {
450                 dest[i] = reconstruct_line (line2, line1, i, 1, 7, 7, 1);
451               } else {
452                 dest[i] = reconstruct_line (line2, line1, i, 4, 8, 4, 0);
453               }
454             }
455           }
456
457           for (i = 0; i < MARGIN; i++) {
458             dest[i] = (line1[i] + line2[i] + 1) >> 1;
459           }
460           for (i = width - MARGIN; i < width; i++) {
461             dest[i] = (line1[i] + line2[i] + 1) >> 1;
462           }
463         }
464       }
465     }
466   }
467   for (k = 1; k < 3; k++) {
468     height = GST_VIDEO_FRAME_COMP_HEIGHT (dest_frame, k);
469     width = GST_VIDEO_FRAME_COMP_WIDTH (dest_frame, k);
470     for (j = 0; j < height; j++) {
471       if ((j & 1) == field->parity) {
472         memcpy (GET_LINE (dest_frame, k, j),
473             GET_LINE (&field->frame, k, j), width);
474       } else {
475         if (j == 0 || j == height - 1) {
476           memcpy (GET_LINE (dest_frame, k, j),
477               GET_LINE (&field->frame, k, (j ^ 1)), width);
478         } else {
479           guint8 *dest = GET_LINE (dest_frame, k, j);
480           guint8 *line1 = GET_LINE (&field->frame, k, j - 1);
481           guint8 *line2 = GET_LINE (&field->frame, k, j + 1);
482           int i;
483           for (i = 0; i < width; i++) {
484             dest[i] = (line1[i] + line2[i] + 1) >> 1;
485           }
486         }
487       }
488     }
489   }
490 }
491
492 static void
493 gst_ivtc_retire_fields (GstIvtc * ivtc, int n_fields)
494 {
495   int i;
496
497   if (n_fields == 0)
498     return;
499
500   for (i = 0; i < n_fields; i++) {
501     gst_video_frame_unmap (&ivtc->fields[i].frame);
502     gst_buffer_unref (ivtc->fields[i].buffer);
503   }
504
505   memmove (ivtc->fields, ivtc->fields + n_fields,
506       sizeof (GstIvtcField) * (ivtc->n_fields - n_fields));
507   ivtc->n_fields -= n_fields;
508 }
509
510 static GstFlowReturn
511 gst_ivtc_transform (GstBaseTransform * trans, GstBuffer * inbuf,
512     GstBuffer * outbuf)
513 {
514   GstIvtc *ivtc = GST_IVTC (trans);
515   GstFlowReturn ret;
516
517   GST_DEBUG_OBJECT (ivtc, "transform");
518
519   if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_TFF)) {
520     add_field (ivtc, inbuf, TOP_FIELD, 0);
521     if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_ONEFIELD)) {
522       add_field (ivtc, inbuf, BOTTOM_FIELD, 1);
523       if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_RFF)) {
524         add_field (ivtc, inbuf, TOP_FIELD, 2);
525       }
526     }
527   } else {
528     add_field (ivtc, inbuf, BOTTOM_FIELD, 0);
529     if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_ONEFIELD)) {
530       add_field (ivtc, inbuf, TOP_FIELD, 1);
531       if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_VIDEO_BUFFER_FLAG_RFF)) {
532         add_field (ivtc, inbuf, BOTTOM_FIELD, 2);
533       }
534     }
535   }
536
537   while (ivtc->n_fields > 0 &&
538       ivtc->fields[0].ts + GST_MSECOND * 50 < ivtc->current_ts) {
539     GST_DEBUG ("retiring early field");
540     gst_ivtc_retire_fields (ivtc, 1);
541   }
542
543   GST_DEBUG ("n_fields %d", ivtc->n_fields);
544   if (ivtc->n_fields < 4) {
545     return GST_BASE_TRANSFORM_FLOW_DROPPED;
546   }
547
548   gst_ivtc_construct_frame (ivtc, outbuf);
549   while (ivtc->n_fields >= 4) {
550     GstBuffer *buf;
551     buf = gst_buffer_copy (outbuf);
552     GST_DEBUG ("pushing extra frame");
553     ret = gst_pad_push (GST_BASE_TRANSFORM_SRC_PAD (trans), buf);
554     if (ret != GST_FLOW_OK) {
555       return ret;
556     }
557
558     gst_ivtc_construct_frame (ivtc, outbuf);
559   }
560
561   return GST_FLOW_OK;
562 }
563
564 static void
565 gst_ivtc_construct_frame (GstIvtc * ivtc, GstBuffer * outbuf)
566 {
567   int anchor_index;
568   int prev_score, next_score;
569   GstVideoFrame dest_frame;
570   int n_retire;
571   gboolean forward_ok;
572
573   anchor_index = 1;
574   if (ivtc->fields[anchor_index].ts < ivtc->current_ts) {
575     forward_ok = TRUE;
576   } else {
577     forward_ok = FALSE;
578   }
579
580   prev_score = similarity (ivtc, anchor_index - 1, anchor_index);
581   next_score = similarity (ivtc, anchor_index, anchor_index + 1);
582
583   gst_video_frame_map (&dest_frame, &ivtc->src_video_info, outbuf,
584       GST_MAP_WRITE);
585
586 #define THRESHOLD 100
587   if (prev_score < THRESHOLD) {
588     if (forward_ok && next_score < prev_score) {
589       reconstruct (ivtc, &dest_frame, anchor_index, anchor_index + 1);
590       n_retire = anchor_index + 2;
591     } else {
592       if (prev_score >= THRESHOLD / 2) {
593         GST_INFO ("borderline prev (%d, %d)", prev_score, next_score);
594       }
595       reconstruct (ivtc, &dest_frame, anchor_index, anchor_index - 1);
596       n_retire = anchor_index + 1;
597     }
598   } else if (next_score < THRESHOLD) {
599     if (next_score >= THRESHOLD / 2) {
600       GST_INFO ("borderline prev (%d, %d)", prev_score, next_score);
601     }
602     reconstruct (ivtc, &dest_frame, anchor_index, anchor_index + 1);
603     if (forward_ok) {
604       n_retire = anchor_index + 2;
605     } else {
606       n_retire = anchor_index + 1;
607     }
608   } else {
609     if (prev_score < THRESHOLD * 2 || next_score < THRESHOLD * 2) {
610       GST_INFO ("borderline single (%d, %d)", prev_score, next_score);
611     }
612     reconstruct_single (ivtc, &dest_frame, anchor_index);
613     n_retire = anchor_index + 1;
614   }
615
616   GST_DEBUG ("retiring %d", n_retire);
617   gst_ivtc_retire_fields (ivtc, n_retire);
618
619   gst_video_frame_unmap (&dest_frame);
620
621   GST_BUFFER_PTS (outbuf) = ivtc->current_ts;
622   GST_BUFFER_DTS (outbuf) = ivtc->current_ts;
623   /* FIXME this is not how to produce durations */
624   GST_BUFFER_DURATION (outbuf) = gst_util_uint64_scale (GST_SECOND,
625       ivtc->src_video_info.fps_d, ivtc->src_video_info.fps_n);
626   GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED |
627       GST_VIDEO_BUFFER_FLAG_TFF | GST_VIDEO_BUFFER_FLAG_RFF |
628       GST_VIDEO_BUFFER_FLAG_ONEFIELD);
629   ivtc->current_ts += GST_BUFFER_DURATION (outbuf);
630
631 }
632
633 static int
634 get_comb_score (GstVideoFrame * top, GstVideoFrame * bottom)
635 {
636   int j;
637   int thisline[MAX_WIDTH];
638   int score = 0;
639   int height;
640   int width;
641   int k;
642
643   height = GST_VIDEO_FRAME_COMP_HEIGHT (top, 0);
644   width = GST_VIDEO_FRAME_COMP_WIDTH (top, 0);
645
646   memset (thisline, 0, sizeof (thisline));
647
648   k = 0;
649   /* remove a few lines from top and bottom, as they sometimes contain
650    * artifacts */
651   for (j = 2; j < height - 2; j++) {
652     guint8 *src1 = GET_LINE_IL (top, bottom, 0, j - 1);
653     guint8 *src2 = GET_LINE_IL (top, bottom, 0, j);
654     guint8 *src3 = GET_LINE_IL (top, bottom, 0, j + 1);
655     int i;
656
657     for (i = 0; i < width; i++) {
658       if (src2[i] < MIN (src1[i], src3[i]) - 5 ||
659           src2[i] > MAX (src1[i], src3[i]) + 5) {
660         if (i > 0) {
661           thisline[i] += thisline[i - 1];
662         }
663         thisline[i]++;
664         if (thisline[i] > 1000)
665           thisline[i] = 1000;
666       } else {
667         thisline[i] = 0;
668       }
669       if (thisline[i] > 100) {
670         score++;
671       }
672     }
673   }
674
675   GST_DEBUG ("score %d", score);
676
677   return score;
678 }
679
680
681
682 static gboolean
683 plugin_init (GstPlugin * plugin)
684 {
685   GST_ELEMENT_REGISTER (ivtc, plugin);
686   GST_ELEMENT_REGISTER (combdetect, plugin);
687
688   return TRUE;
689 }
690
691 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
692     GST_VERSION_MINOR,
693     ivtc,
694     "Inverse Telecine",
695     plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN)