From 14c8bf96ad08f02eb1427a7196859fa9a96ec46c Mon Sep 17 00:00:00 2001 From: David Schleef Date: Mon, 24 May 2010 12:55:16 -0700 Subject: [PATCH 1/7] basevideodecoder: (in vp8) Mark discont buffers --- gst-libs/gst/video/gstbasevideodecoder.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gst-libs/gst/video/gstbasevideodecoder.c b/gst-libs/gst/video/gstbasevideodecoder.c index b34820eb38..f00a2621e0 100644 --- a/gst-libs/gst/video/gstbasevideodecoder.c +++ b/gst-libs/gst/video/gstbasevideodecoder.c @@ -1034,6 +1034,10 @@ gst_base_video_decoder_finish_frame (GstBaseVideoDecoder * base_video_decoder, GST_BUFFER_FLAG_UNSET (frame->src_buffer, GST_VIDEO_BUFFER_ONEFIELD); } } + if (base_video_decoder->discont) { + GST_BUFFER_FLAG_UNSET (frame->src_buffer, GST_BUFFER_FLAG_DISCONT); + base_video_decoder->discont = FALSE; + } GST_BUFFER_TIMESTAMP (frame->src_buffer) = frame->presentation_timestamp; GST_BUFFER_DURATION (frame->src_buffer) = frame->presentation_duration; From 00174c8fa888f521f3739f2490f7ccc43ae98da4 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Fri, 21 May 2010 23:39:06 -0700 Subject: [PATCH 2/7] invtelecine: handle different frame sizes --- gst/invtelecine/Makefile.am | 2 +- gst/invtelecine/gstinvtelecine.c | 108 ++++++++++++++++++++++++------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/gst/invtelecine/Makefile.am b/gst/invtelecine/Makefile.am index 37838f6b7d..8f596291a3 100644 --- a/gst/invtelecine/Makefile.am +++ b/gst/invtelecine/Makefile.am @@ -9,7 +9,7 @@ libgstinvtelecine_la_CFLAGS = \ libgstinvtelecine_la_LIBADD = \ $(GST_LIBS) \ - $(GST_PLUGINS_BASE_LIBS) + $(GST_PLUGINS_BASE_LIBS) -lgstvideo-@GST_MAJORMINOR@ libgstinvtelecine_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstinvtelecine_la_LIBTOOLFLAGS = --tag=disable-static diff --git a/gst/invtelecine/gstinvtelecine.c b/gst/invtelecine/gstinvtelecine.c index bdb9e5fd3f..c4b398fb39 100644 --- a/gst/invtelecine/gstinvtelecine.c +++ b/gst/invtelecine/gstinvtelecine.c @@ -68,6 +68,11 @@ struct _GstInvtelecine int phase; Field fifo[FIFO_SIZE]; + + int width; + int height; + GstVideoFormat format; + gboolean interlaced; }; struct _GstInvtelecineClass @@ -107,6 +112,7 @@ static void gst_invtelecine_set_property (GObject * object, static void gst_invtelecine_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); +static gboolean gst_invtelecine_setcaps (GstPad * pad, GstCaps * caps); static GstStateChangeReturn gst_invtelecine_change_state (GstElement * element, GstStateChange transition); @@ -180,6 +186,7 @@ gst_invtelecine_init (GstInvtelecine * invtelecine) gst_pad_new_from_static_template (&gst_invtelecine_sink_template, "sink"); gst_element_add_pad (GST_ELEMENT (invtelecine), invtelecine->sinkpad); gst_pad_set_chain_function (invtelecine->sinkpad, gst_invtelecine_chain); + gst_pad_set_setcaps_function (invtelecine->sinkpad, gst_invtelecine_setcaps); invtelecine->srcpad = gst_pad_new_from_static_template (&gst_invtelecine_src_template, "src"); @@ -187,6 +194,43 @@ gst_invtelecine_init (GstInvtelecine * invtelecine) } +static gboolean +gst_invtelecine_setcaps (GstPad * pad, GstCaps * caps) +{ + GstInvtelecine *invtelecine; + gboolean ret; + int width, height; + GstVideoFormat format; + gboolean interlaced = TRUE; + int fps_n, fps_d; + + invtelecine = GST_INVTELECINE (gst_pad_get_parent (pad)); + + ret = gst_video_format_parse_caps (caps, &format, &width, &height); + gst_video_format_parse_caps_interlaced (caps, &interlaced); + ret &= gst_video_parse_caps_framerate (caps, &fps_n, &fps_d); + + if (ret) { + GstCaps *srccaps = gst_caps_copy (caps); + + ret = gst_pad_set_caps (invtelecine->srcpad, srccaps); + + } + + if (ret) { + invtelecine->format = format; + invtelecine->width = width; + invtelecine->height = height; + invtelecine->interlaced = interlaced; + } + + g_object_unref (invtelecine); + + return ret; +} + + + #define MAX_FIELD_SCORE 100 static double @@ -220,18 +264,21 @@ gst_invtelecine_compare_fields (GstInvtelecine * invtelecine, int field1, sum = 0; field_index = invtelecine->fifo[field1].field_index; - for (j = field_index; j < 480; j += 2) { - if (j == 0 || j == 479) + for (j = field_index; j < invtelecine->height; j += 2) { + if (j == 0 || j == invtelecine->height - 1) continue; - data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + 720 * j; + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * j; data2_1 = - GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + 720 * (j - 1); + GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j - 1); data2_2 = - GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + 720 * (j + 1); + GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j + 1); linesum = 0; - for (i = 1; i < 719; i++) { + for (i = 1; i < invtelecine->width - 1; i++) { have = data1[i - 1] + data1[i + 1]; hdiff = abs (data1[i - 1] - data1[i + 1]); vave = data2_1[i] + data2_2[i]; @@ -242,7 +289,7 @@ gst_invtelecine_compare_fields (GstInvtelecine * invtelecine, int field1, sum += linesum; } - sum /= 720 * 240; + sum /= (invtelecine->width * invtelecine->height / 2); return MIN (sum, MAX_FIELD_SCORE); } @@ -342,12 +389,12 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) a[p] = get_score (invtelecine, p); } if (a[0] >= 8 && a[1] < 4) { - GST_WARNING ("locked field=%d (phase = %d, score = %d)", + GST_WARNING ("locked 3:2 field=%d (phase = %d, score = %d)", invtelecine->field, 0, a[0]); invtelecine->locked = TRUE; invtelecine->phase = 0; } else if (a[1] >= 8 && a[0] < 4) { - GST_WARNING ("locked field=%d (phase = %d, score = %d)", + GST_WARNING ("locked 3:2 field=%d (phase = %d, score = %d)", invtelecine->field, 1, a[1]); invtelecine->locked = TRUE; invtelecine->phase = 1; @@ -358,11 +405,13 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) if (invtelecine->locked) { num_fields = pulldown_2_3[invtelecine->phase]; +#if 0 g_print ("frame %d %g %g %g\n", invtelecine->field, invtelecine->fifo[0].prev, invtelecine->fifo[1].prev, (num_fields == 3) ? invtelecine->fifo[2].prev : 0); +#endif } else { num_fields = 2; @@ -389,26 +438,31 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) } static void -copy_field (GstBuffer * d, GstBuffer * s, int field_index) +copy_field (GstBuffer * d, GstBuffer * s, int field_index, int width, + int height) { int j; guint8 *dest; guint8 *src; - for (j = field_index; j < 480; j += 2) { - dest = GST_BUFFER_DATA (d) + j * 720; - src = GST_BUFFER_DATA (s) + j * 720; - memcpy (dest, src, 720); + for (j = field_index; j < height; j += 2) { + dest = GST_BUFFER_DATA (d) + j * width; + src = GST_BUFFER_DATA (s) + j * width; + memcpy (dest, src, width); } - for (j = field_index; j < 240; j += 2) { - dest = GST_BUFFER_DATA (d) + 720 * 480 + j * 360; - src = GST_BUFFER_DATA (s) + 720 * 480 + j * 360; - memcpy (dest, src, 360); + for (j = field_index; j < height / 2; j += 2) { + dest = GST_BUFFER_DATA (d) + width * height + j * width / 2; + src = GST_BUFFER_DATA (s) + width * height + j * width / 2; + memcpy (dest, src, width / 2); } - for (j = field_index; j < 240; j += 2) { - dest = GST_BUFFER_DATA (d) + 720 * 480 + 360 * 240 + j * 360; - src = GST_BUFFER_DATA (s) + 720 * 480 + 360 * 240 + j * 360; - memcpy (dest, src, 360); + for (j = field_index; j < height / 2; j += 2) { + dest = + GST_BUFFER_DATA (d) + width * height + width / 2 * height / 2 + + j * width / 2; + src = + GST_BUFFER_DATA (s) + width * height + width / 2 * height / 2 + + j * width / 2; + memcpy (dest, src, width / 2); } } @@ -420,10 +474,14 @@ gst_invtelecine_output_fields (GstInvtelecine * invtelecine, int num_fields) field_index = invtelecine->fifo[0].field_index; - buffer = gst_buffer_new_and_alloc (720 * 480 + 360 * 240 + 360 * 240); + buffer = + gst_buffer_new_and_alloc (invtelecine->width * invtelecine->height * 3 / + 2); - copy_field (buffer, invtelecine->fifo[0].buffer, field_index); - copy_field (buffer, invtelecine->fifo[1].buffer, field_index ^ 1); + copy_field (buffer, invtelecine->fifo[0].buffer, field_index, + invtelecine->width, invtelecine->height); + copy_field (buffer, invtelecine->fifo[1].buffer, field_index ^ 1, + invtelecine->width, invtelecine->height); gst_buffer_set_caps (buffer, GST_BUFFER_CAPS (invtelecine->fifo[0].buffer)); From b213b038048abab477f6464ffb3d0043a09d5be4 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Tue, 9 Mar 2010 12:32:42 -0800 Subject: [PATCH 3/7] invtelecine: Add multiple pulldown formats --- gst/invtelecine/gstinvtelecine.c | 154 ++++++++++++++++++++++++++++++- 1 file changed, 153 insertions(+), 1 deletion(-) diff --git a/gst/invtelecine/gstinvtelecine.c b/gst/invtelecine/gstinvtelecine.c index c4b398fb39..a0340e7174 100644 --- a/gst/invtelecine/gstinvtelecine.c +++ b/gst/invtelecine/gstinvtelecine.c @@ -51,6 +51,7 @@ struct _Field GstBuffer *buffer; int field_index; double prev; + double prev2; }; struct _GstInvtelecine @@ -294,6 +295,49 @@ gst_invtelecine_compare_fields (GstInvtelecine * invtelecine, int field1, return MIN (sum, MAX_FIELD_SCORE); } +static double +gst_invtelecine_compare_same_fields (GstInvtelecine * invtelecine, int field1, + int field2) +{ + int i; + int j; + guint8 *data1; + guint8 *data2; + int field_index; + int diff; + double sum; + double linesum; + + if (field1 < 0 || field2 < 0) + return MAX_FIELD_SCORE; + if (invtelecine->fifo[field1].buffer == NULL || + invtelecine->fifo[field2].buffer == NULL) + return MAX_FIELD_SCORE; + if (invtelecine->fifo[field1].buffer == invtelecine->fifo[field2].buffer && + invtelecine->fifo[field1].field_index == + invtelecine->fifo[field2].field_index) { + return 0; + } + + sum = 0; + field_index = invtelecine->fifo[field1].field_index; + for (j = field_index; j < 480; j += 2) { + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + 720 * j; + data2 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + 720 * j; + + linesum = 0; + for (i = 0; i < 720; i++) { + diff = (data1[i] - data2[i]); + linesum += diff * diff; + } + sum += linesum; + } + + sum /= 720 * 240; + + return MIN (sum, MAX_FIELD_SCORE); +} + static void gst_invtelecine_push_field (GstInvtelecine * invtelecine, GstBuffer * buffer, int field_index) @@ -309,12 +353,106 @@ gst_invtelecine_push_field (GstInvtelecine * invtelecine, GstBuffer * buffer, invtelecine->fifo[i].field_index = field_index; invtelecine->fifo[i].prev = gst_invtelecine_compare_fields (invtelecine, i, i - 1); - //g_print("compare %f\n", invtelecine->fifo[i].prev); + invtelecine->fifo[i].prev2 = + gst_invtelecine_compare_same_fields (invtelecine, i, i - 2); + g_print ("compare %g %g\n", invtelecine->fifo[i].prev, + invtelecine->fifo[i].prev2); } int pulldown_2_3[] = { 2, 3 }; +typedef struct _PulldownFormat PulldownFormat; +struct _PulldownFormat +{ + int cycle_length; + int n_fields[10]; +}; + +static const PulldownFormat formats[] = { + /* interlaced */ + {1, {1}}, + /* 30p */ + {2, {2}}, + /* 24p */ + {5, {2, 3,}}, +}; + +static int +get_score_2 (GstInvtelecine * invtelecine, int format_index, int phase) +{ + const PulldownFormat *format = formats + format_index; + int field_index; + int k; + int i; + int score; + + GST_DEBUG ("score2 format_index %d phase %d", format_index, phase); + + phase = (invtelecine->field + phase) % format->cycle_length; + + field_index = 0; + k = 0; + while (phase > 0) { + field_index++; + if (field_index >= format->n_fields[k]) { + field_index = 0; + k++; + if (format->n_fields[k] == 0) { + k = 0; + } + } + phase--; + } + + /* k is the frame index in the format */ + /* field_index is the field index in the frame */ + + score = 0; + for (i = 0; i < 15; i++) { + if (field_index == 0) { + if (invtelecine->fifo[i].prev > 50) { + /* Strong picture change signal */ + score++; + } + } else { + if (invtelecine->fifo[i].prev > 50) { + /* A secondary field with visible combing */ + score -= 5; + } else if (field_index == 1) { + if (invtelecine->fifo[i].prev > 5) { + score--; + } else if (invtelecine->fifo[i].prev < 3) { + /* In the noise */ + score++; + } + } else { + if (invtelecine->fifo[i].prev2 < 1) { + score += 2; + } + if (invtelecine->fifo[i].prev2 > 10) { + /* A tertiary field that doesn't match */ + score -= 5; + } + } + } + + GST_DEBUG ("i=%d phase=%d fi=%d prev=%g score=%d", i, phase, field_index, + invtelecine->fifo[i].prev, score); + + field_index++; + if (field_index >= format->n_fields[k]) { + field_index = 0; + k++; + if (format->n_fields[k] == 0) { + k = 0; + } + } + } + + return score; +} + static int get_score (GstInvtelecine * invtelecine, int phase) { @@ -370,9 +508,23 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) { int score; int num_fields; + int scores[8]; GST_DEBUG ("process %d", invtelecine->num_fields); while (invtelecine->num_fields > 15) { + scores[0] = get_score_2 (invtelecine, 0, 0); + scores[1] = get_score_2 (invtelecine, 1, 0); + scores[2] = get_score_2 (invtelecine, 1, 1); + scores[3] = get_score_2 (invtelecine, 2, 0); + scores[4] = get_score_2 (invtelecine, 2, 1); + scores[5] = get_score_2 (invtelecine, 2, 2); + scores[6] = get_score_2 (invtelecine, 2, 3); + scores[7] = get_score_2 (invtelecine, 2, 4); + + g_print ("scores %d %d %d %d %d %d %d %d %d\n", invtelecine->field, + scores[0], scores[1], scores[2], scores[3], + scores[4], scores[5], scores[6], scores[7]); + if (invtelecine->locked) { score = get_score (invtelecine, invtelecine->phase); if (score < 4) { From 750cdc912b6cabadc534bba267973a7aab6430a5 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Sat, 22 May 2010 00:44:40 -0700 Subject: [PATCH 4/7] invtelecine: Change format table Add format name, phase table. --- gst/invtelecine/gstinvtelecine.c | 103 +++++++++++++++++++++++++++---- 1 file changed, 91 insertions(+), 12 deletions(-) diff --git a/gst/invtelecine/gstinvtelecine.c b/gst/invtelecine/gstinvtelecine.c index a0340e7174..4707a318fd 100644 --- a/gst/invtelecine/gstinvtelecine.c +++ b/gst/invtelecine/gstinvtelecine.c @@ -66,6 +66,7 @@ struct _GstInvtelecine int field; gboolean locked; + int last_lock; int phase; Field fifo[FIFO_SIZE]; @@ -355,8 +356,10 @@ gst_invtelecine_push_field (GstInvtelecine * invtelecine, GstBuffer * buffer, gst_invtelecine_compare_fields (invtelecine, i, i - 1); invtelecine->fifo[i].prev2 = gst_invtelecine_compare_same_fields (invtelecine, i, i - 2); +#if 0 g_print ("compare %g %g\n", invtelecine->fifo[i].prev, invtelecine->fifo[i].prev2); +#endif } @@ -365,17 +368,18 @@ int pulldown_2_3[] = { 2, 3 }; typedef struct _PulldownFormat PulldownFormat; struct _PulldownFormat { + const char *name; int cycle_length; int n_fields[10]; }; static const PulldownFormat formats[] = { /* interlaced */ - {1, {1}}, + {"interlaced", 1, {1}}, /* 30p */ - {2, {2}}, + {"2:1", 2, {2}}, /* 24p */ - {5, {2, 3,}}, + {"3:2", 5, {2, 3,}}, }; static int @@ -453,6 +457,7 @@ get_score_2 (GstInvtelecine * invtelecine, int format_index, int phase) return score; } +#if 0 static int get_score (GstInvtelecine * invtelecine, int phase) { @@ -502,29 +507,102 @@ get_score (GstInvtelecine * invtelecine, int phase) return score; } +#endif + +int format_table[] = { 0, 1, 1, 2, 2, 2, 2, 2 }; +int phase_table[] = { 0, 0, 1, 0, 1, 2, 3, 4 }; static void gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) { - int score; + //int score; int num_fields; int scores[8]; + int i; + int max_i; + //int format; + int phase; GST_DEBUG ("process %d", invtelecine->num_fields); while (invtelecine->num_fields > 15) { - scores[0] = get_score_2 (invtelecine, 0, 0); - scores[1] = get_score_2 (invtelecine, 1, 0); - scores[2] = get_score_2 (invtelecine, 1, 1); - scores[3] = get_score_2 (invtelecine, 2, 0); - scores[4] = get_score_2 (invtelecine, 2, 1); - scores[5] = get_score_2 (invtelecine, 2, 2); - scores[6] = get_score_2 (invtelecine, 2, 3); - scores[7] = get_score_2 (invtelecine, 2, 4); + num_fields = 0; + for (i = 0; i < 8; i++) { + scores[i] = get_score_2 (invtelecine, format_table[i], phase_table[i]); + } + +#if 0 g_print ("scores %d %d %d %d %d %d %d %d %d\n", invtelecine->field, scores[0], scores[1], scores[2], scores[3], scores[4], scores[5], scores[6], scores[7]); +#endif + max_i = invtelecine->last_lock; + for (i = 0; i < 8; i++) { + int field_index; + int k; + + phase = (invtelecine->field + phase_table[i]) % + formats[format_table[i]].cycle_length; + + field_index = 0; + k = 0; + while (phase > 0) { + field_index++; + if (field_index >= formats[format_table[i]].n_fields[k]) { + field_index = 0; + k++; + if (formats[format_table[i]].n_fields[k] == 0) { + k = 0; + } + } + phase--; + } + + if (field_index == 0) { + if (scores[i] > scores[max_i]) { + max_i = i; + } + } + } + + if (max_i != invtelecine->last_lock) { + + GST_WARNING ("new structure %s, phase %d", + formats[format_table[max_i]].name, phase_table[max_i]); + + invtelecine->last_lock = max_i; + } + + { + int field_index; + int k; + + phase = (invtelecine->field + phase_table[max_i]) % + formats[format_table[max_i]].cycle_length; + + field_index = 0; + k = 0; + while (phase > 0) { + field_index++; + if (field_index >= formats[format_table[max_i]].n_fields[k]) { + field_index = 0; + k++; + if (formats[format_table[max_i]].n_fields[k] == 0) { + k = 0; + } + } + phase--; + } + + num_fields = formats[format_table[max_i]].n_fields[k]; + } + + if (num_fields == 0) { + GST_WARNING ("unlocked"); + num_fields = 1; + } +#if 0 if (invtelecine->locked) { score = get_score (invtelecine, invtelecine->phase); if (score < 4) { @@ -568,6 +646,7 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) } else { num_fields = 2; } +#endif gst_invtelecine_output_fields (invtelecine, num_fields); From c439a6bc364241400eb35571ceb3458e6aa4f001 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Sat, 22 May 2010 11:30:54 -0700 Subject: [PATCH 5/7] invtelecine: Add 4:2:2 formats --- gst/invtelecine/gstinvtelecine.c | 137 +++++++++++++++++++++---------- 1 file changed, 92 insertions(+), 45 deletions(-) diff --git a/gst/invtelecine/gstinvtelecine.c b/gst/invtelecine/gstinvtelecine.c index 4707a318fd..3bd513cdec 100644 --- a/gst/invtelecine/gstinvtelecine.c +++ b/gst/invtelecine/gstinvtelecine.c @@ -92,7 +92,7 @@ static GstStaticPadTemplate gst_invtelecine_src_template = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420") + GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{YUY2,UYVY,I420,YV12}") ) ); @@ -100,7 +100,7 @@ static GstStaticPadTemplate gst_invtelecine_sink_template = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420") + GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{YUY2,UYVY,I420,YV12}") ) ); @@ -270,23 +270,52 @@ gst_invtelecine_compare_fields (GstInvtelecine * invtelecine, int field1, if (j == 0 || j == invtelecine->height - 1) continue; - data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + - invtelecine->width * j; - data2_1 = - GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + - invtelecine->width * (j - 1); - data2_2 = - GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + - invtelecine->width * (j + 1); + if (invtelecine->format == GST_VIDEO_FORMAT_I420 || + invtelecine->format == GST_VIDEO_FORMAT_YV12) { + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * j; + data2_1 = + GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j - 1); + data2_2 = + GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j + 1); - linesum = 0; - for (i = 1; i < invtelecine->width - 1; i++) { - have = data1[i - 1] + data1[i + 1]; - hdiff = abs (data1[i - 1] - data1[i + 1]); - vave = data2_1[i] + data2_2[i]; - vdiff = abs (data2_1[i] - data2_2[i]); - den = MAX (1, MAX (hdiff, vdiff)); - linesum += (have - vave) * (have - vave) / (den * den); + /* planar 4:2:0 */ + linesum = 0; + for (i = 1; i < invtelecine->width - 1; i++) { + have = data1[i - 1] + data1[i + 1]; + hdiff = abs (data1[i - 1] - data1[i + 1]); + vave = data2_1[i] + data2_2[i]; + vdiff = abs (data2_1[i] - data2_2[i]); + den = MAX (1, MAX (hdiff, vdiff)); + linesum += (have - vave) * (have - vave) / (den * den); + } + } else { + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * 2 * j; + data2_1 = + GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * 2 * (j - 1); + data2_2 = + GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * 2 * (j + 1); + if (invtelecine->format == GST_VIDEO_FORMAT_UYVY) { + data1++; + data2_1++; + data2_2++; + } + + /* packed 4:2:2 */ + linesum = 0; + for (i = 1; i < invtelecine->width - 1; i++) { + have = data1[(i - 1) * 2] + data1[(i + 1) * 2]; + hdiff = abs (data1[(i - 1) * 2] - data1[(i + 1) * 2]); + vave = data2_1[i * 2] + data2_2[i * 2]; + vdiff = abs (data2_1[i * 2] - data2_2[i * 2]); + den = MAX (1, MAX (hdiff, vdiff)); + linesum += (have - vave) * (have - vave) / (den * den); + } } sum += linesum; } @@ -669,31 +698,44 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) } static void -copy_field (GstBuffer * d, GstBuffer * s, int field_index, int width, - int height) +copy_field (GstInvtelecine * invtelecine, GstBuffer * d, GstBuffer * s, + int field_index) { int j; guint8 *dest; guint8 *src; + int width = invtelecine->width; + int height = invtelecine->height; - for (j = field_index; j < height; j += 2) { - dest = GST_BUFFER_DATA (d) + j * width; - src = GST_BUFFER_DATA (s) + j * width; - memcpy (dest, src, width); - } - for (j = field_index; j < height / 2; j += 2) { - dest = GST_BUFFER_DATA (d) + width * height + j * width / 2; - src = GST_BUFFER_DATA (s) + width * height + j * width / 2; - memcpy (dest, src, width / 2); - } - for (j = field_index; j < height / 2; j += 2) { - dest = - GST_BUFFER_DATA (d) + width * height + width / 2 * height / 2 + - j * width / 2; - src = - GST_BUFFER_DATA (s) + width * height + width / 2 * height / 2 + - j * width / 2; - memcpy (dest, src, width / 2); + if (invtelecine->format == GST_VIDEO_FORMAT_I420 || + invtelecine->format == GST_VIDEO_FORMAT_YV12) { + /* planar 4:2:0 */ + for (j = field_index; j < height; j += 2) { + dest = GST_BUFFER_DATA (d) + j * width; + src = GST_BUFFER_DATA (s) + j * width; + memcpy (dest, src, width); + } + for (j = field_index; j < height / 2; j += 2) { + dest = GST_BUFFER_DATA (d) + width * height + j * width / 2; + src = GST_BUFFER_DATA (s) + width * height + j * width / 2; + memcpy (dest, src, width / 2); + } + for (j = field_index; j < height / 2; j += 2) { + dest = + GST_BUFFER_DATA (d) + width * height + width / 2 * height / 2 + + j * width / 2; + src = + GST_BUFFER_DATA (s) + width * height + width / 2 * height / 2 + + j * width / 2; + memcpy (dest, src, width / 2); + } + } else { + /* packed 4:2:2 */ + for (j = field_index; j < height; j += 2) { + dest = GST_BUFFER_DATA (d) + j * width * 2; + src = GST_BUFFER_DATA (s) + j * width * 2; + memcpy (dest, src, width * 2); + } } } @@ -705,14 +747,19 @@ gst_invtelecine_output_fields (GstInvtelecine * invtelecine, int num_fields) field_index = invtelecine->fifo[0].field_index; - buffer = - gst_buffer_new_and_alloc (invtelecine->width * invtelecine->height * 3 / - 2); + if (invtelecine->format == GST_VIDEO_FORMAT_I420 || + invtelecine->format == GST_VIDEO_FORMAT_YV12) { + buffer = + gst_buffer_new_and_alloc (invtelecine->width * invtelecine->height * 3 / + 2); + } else { + buffer = + gst_buffer_new_and_alloc (invtelecine->width * invtelecine->height * 2); + } - copy_field (buffer, invtelecine->fifo[0].buffer, field_index, - invtelecine->width, invtelecine->height); - copy_field (buffer, invtelecine->fifo[1].buffer, field_index ^ 1, - invtelecine->width, invtelecine->height); + copy_field (invtelecine, buffer, invtelecine->fifo[0].buffer, field_index); + copy_field (invtelecine, buffer, invtelecine->fifo[1].buffer, + field_index ^ 1); gst_buffer_set_caps (buffer, GST_BUFFER_CAPS (invtelecine->fifo[0].buffer)); From 88f4a6a0ed922a1482b17e7be6b371b9a874544f Mon Sep 17 00:00:00 2001 From: David Schleef Date: Sat, 22 May 2010 11:36:37 -0700 Subject: [PATCH 6/7] invtelecine: remove old code --- gst/invtelecine/gstinvtelecine.c | 97 -------------------------------- 1 file changed, 97 deletions(-) diff --git a/gst/invtelecine/gstinvtelecine.c b/gst/invtelecine/gstinvtelecine.c index 3bd513cdec..7df9f92e44 100644 --- a/gst/invtelecine/gstinvtelecine.c +++ b/gst/invtelecine/gstinvtelecine.c @@ -486,58 +486,6 @@ get_score_2 (GstInvtelecine * invtelecine, int format_index, int phase) return score; } -#if 0 -static int -get_score (GstInvtelecine * invtelecine, int phase) -{ - int i; - int score = 0; - int field_index = 0; - - GST_DEBUG ("scoring for phase %d", phase); - for (i = 0; i < 15; i++) { - if (field_index == 0) { - if (invtelecine->fifo[i].prev > 50) { - /* Strong picture change signal */ - score++; - } else if (i < 14 && - pulldown_2_3[phase] >= 2 && - (invtelecine->fifo[i].prev < invtelecine->fifo[i + 1].prev * 0.5)) { - score--; - } else if (i < 13 && - pulldown_2_3[phase] >= 3 && - (invtelecine->fifo[i].prev < invtelecine->fifo[i + 2].prev * 0.5)) { - score--; - } else { - - } - } else { - if (invtelecine->fifo[i].prev > 50) { - /* A secondary field with visible combing */ - return -10; - } else if (invtelecine->fifo[i].prev > 5) { - score--; - } else if (invtelecine->fifo[i].prev < 3) { - /* In the noise */ - score++; - } else { - } - } - GST_DEBUG ("i=%d phase=%d fi=%d prev=%g score=%d", i, phase, field_index, - invtelecine->fifo[i].prev, score); - field_index++; - if (field_index == pulldown_2_3[phase]) { - field_index = 0; - phase++; - if (phase == 2) - phase = 0; - } - } - - return score; -} -#endif - int format_table[] = { 0, 1, 1, 2, 2, 2, 2, 2 }; int phase_table[] = { 0, 0, 1, 0, 1, 2, 3, 4 }; @@ -631,51 +579,6 @@ gst_invtelecine_process (GstInvtelecine * invtelecine, gboolean flush) GST_WARNING ("unlocked"); num_fields = 1; } -#if 0 - if (invtelecine->locked) { - score = get_score (invtelecine, invtelecine->phase); - if (score < 4) { - GST_WARNING ("unlocked field=%d (phase = %d, score = %d)", - invtelecine->field, invtelecine->phase, score); - invtelecine->locked = FALSE; - } - } - if (!invtelecine->locked) { - int p; - int a[2]; - - for (p = 0; p < 2; p++) { - a[p] = get_score (invtelecine, p); - } - if (a[0] >= 8 && a[1] < 4) { - GST_WARNING ("locked 3:2 field=%d (phase = %d, score = %d)", - invtelecine->field, 0, a[0]); - invtelecine->locked = TRUE; - invtelecine->phase = 0; - } else if (a[1] >= 8 && a[0] < 4) { - GST_WARNING ("locked 3:2 field=%d (phase = %d, score = %d)", - invtelecine->field, 1, a[1]); - invtelecine->locked = TRUE; - invtelecine->phase = 1; - } - } - //g_print ("score %d %d\n", a[0], a[1]); - - if (invtelecine->locked) { - num_fields = pulldown_2_3[invtelecine->phase]; - -#if 0 - g_print ("frame %d %g %g %g\n", - invtelecine->field, - invtelecine->fifo[0].prev, - invtelecine->fifo[1].prev, - (num_fields == 3) ? invtelecine->fifo[2].prev : 0); -#endif - - } else { - num_fields = 2; - } -#endif gst_invtelecine_output_fields (invtelecine, num_fields); From 6ceb310dc6ab14d02c82b2e2b104746525dcbe34 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Sat, 22 May 2010 18:14:09 -0700 Subject: [PATCH 7/7] invtelecine: Add verify-field-flags property When enabled, check that the video matches the field flags on the incoming buffers. --- gst/invtelecine/gstinvtelecine.c | 206 +++++++++++++++++++++++++++---- 1 file changed, 185 insertions(+), 21 deletions(-) diff --git a/gst/invtelecine/gstinvtelecine.c b/gst/invtelecine/gstinvtelecine.c index 7df9f92e44..2d22128d54 100644 --- a/gst/invtelecine/gstinvtelecine.c +++ b/gst/invtelecine/gstinvtelecine.c @@ -25,6 +25,7 @@ #include #include #include +#include GST_DEBUG_CATEGORY (gst_invtelecine_debug); #define GST_CAT_DEFAULT gst_invtelecine_debug @@ -51,7 +52,10 @@ struct _Field GstBuffer *buffer; int field_index; double prev; + double prev1; double prev2; + double prev3; + }; struct _GstInvtelecine @@ -61,6 +65,10 @@ struct _GstInvtelecine GstPad *srcpad; GstPad *sinkpad; + /* properties */ + gboolean verify_field_flags; + + /* state */ int next_field; int num_fields; int field; @@ -75,6 +83,8 @@ struct _GstInvtelecine int height; GstVideoFormat format; gboolean interlaced; + + double bad_flag_metric; }; struct _GstInvtelecineClass @@ -85,7 +95,8 @@ struct _GstInvtelecineClass enum { - ARG_0 + ARG_0, + PROP_VERIFY_FIELD_FLAGS }; static GstStaticPadTemplate gst_invtelecine_src_template = @@ -178,6 +189,13 @@ gst_invtelecine_class_init (GstInvtelecineClass * klass) object_class->get_property = gst_invtelecine_get_property; element_class->change_state = gst_invtelecine_change_state; + + g_object_class_install_property (object_class, PROP_VERIFY_FIELD_FLAGS, + g_param_spec_boolean ("verify-field-flags", "verify field flags", + "Verify that field dominance (top/bottom field first) buffer " + "flags are correct", FALSE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + } static void @@ -194,6 +212,8 @@ gst_invtelecine_init (GstInvtelecine * invtelecine) gst_pad_new_from_static_template (&gst_invtelecine_src_template, "src"); gst_element_add_pad (GST_ELEMENT (invtelecine), invtelecine->srcpad); + invtelecine->bad_flag_metric = 1.0; + invtelecine->verify_field_flags = FALSE; } static gboolean @@ -326,14 +346,15 @@ gst_invtelecine_compare_fields (GstInvtelecine * invtelecine, int field1, } static double -gst_invtelecine_compare_same_fields (GstInvtelecine * invtelecine, int field1, +gst_invtelecine_compare_fields_mse (GstInvtelecine * invtelecine, int field1, int field2) { int i; int j; guint8 *data1; guint8 *data2; - int field_index; + int field_index1; + int field_index2; int diff; double sum; double linesum; @@ -350,22 +371,135 @@ gst_invtelecine_compare_same_fields (GstInvtelecine * invtelecine, int field1, } sum = 0; - field_index = invtelecine->fifo[field1].field_index; - for (j = field_index; j < 480; j += 2) { - data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + 720 * j; - data2 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + 720 * j; + field_index1 = invtelecine->fifo[field1].field_index; + field_index2 = invtelecine->fifo[field2].field_index; + if (invtelecine->format == GST_VIDEO_FORMAT_I420 || + invtelecine->format == GST_VIDEO_FORMAT_YV12) { + for (j = 0; j < invtelecine->height; j += 2) { + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * (j + field_index1); + data2 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j + field_index2); - linesum = 0; - for (i = 0; i < 720; i++) { - diff = (data1[i] - data2[i]); - linesum += diff * diff; + linesum = 0; + for (i = 0; i < invtelecine->width; i++) { + diff = (data1[i] - data2[i]); + linesum += diff * diff; + } + sum += linesum; + } + } else { + for (j = 0; j < invtelecine->height; j += 2) { + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * 2 * (j + field_index1); + data2 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * 2 * (j + field_index2); + + if (invtelecine->format == GST_VIDEO_FORMAT_UYVY) { + data1++; + data2++; + } + + linesum = 0; + for (i = 0; i < invtelecine->width; i++) { + diff = (data1[i * 2] - data2[i * 2]); + linesum += diff * diff; + } + sum += linesum; } - sum += linesum; } - sum /= 720 * 240; + sum /= invtelecine->width * invtelecine->height / 2; - return MIN (sum, MAX_FIELD_SCORE); + //return MIN (sum, MAX_FIELD_SCORE); + return sum; +} + +static double +gst_invtelecine_compare_fields_mse_ave (GstInvtelecine * invtelecine, + int field1, int field2) +{ + int i; + int j; + guint8 *data1; + guint8 *data2_1; + guint8 *data2_2; + int field_index1; + int field_index2; + double diff; + double sum; + double linesum; + +#define MAX_FIELD_SCORE_2 1e9 + if (field1 < 0 || field2 < 0) + return MAX_FIELD_SCORE_2; + if (invtelecine->fifo[field1].buffer == NULL || + invtelecine->fifo[field2].buffer == NULL) + return MAX_FIELD_SCORE_2; + if (invtelecine->fifo[field1].buffer == invtelecine->fifo[field2].buffer && + invtelecine->fifo[field1].field_index == + invtelecine->fifo[field2].field_index) { + return 0; + } + + sum = 0; + field_index1 = invtelecine->fifo[field1].field_index; + field_index2 = invtelecine->fifo[field2].field_index; + if (invtelecine->format == GST_VIDEO_FORMAT_I420 || + invtelecine->format == GST_VIDEO_FORMAT_YV12) { + for (j = 0; j < invtelecine->height; j += 2) { + if (j + field_index1 == 0 || j + field_index1 == invtelecine->height - 1) + continue; + + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * (j + field_index1); + data2_1 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j + field_index1 - 1); + data2_2 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * (j + field_index1 + 1); + + linesum = 0; + for (i = 0; i < invtelecine->width; i++) { + diff = (data1[i] - (data2_1[i] + data2_2[i]) / 2); + diff *= diff; + linesum += diff * diff; + } + sum += linesum; + } + } else { + for (j = 0; j < invtelecine->height; j += 2) { + if (j + field_index1 == 0 || j + field_index1 == invtelecine->height - 1) + continue; + + data1 = GST_BUFFER_DATA (invtelecine->fifo[field1].buffer) + + invtelecine->width * 2 * (j + field_index1); + data2_1 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * 2 * (j + field_index1 - 1); + data2_2 = GST_BUFFER_DATA (invtelecine->fifo[field2].buffer) + + invtelecine->width * 2 * (j + field_index1 + 1); + + if (invtelecine->format == GST_VIDEO_FORMAT_UYVY) { + data1++; + data2_1++; + data2_2++; + } + + linesum = 0; + for (i = 0; i < invtelecine->width; i++) { + diff = (data1[i] - (data2_1[i] + data2_2[i]) / 2); + diff *= diff; + linesum += diff * diff; + } + sum += linesum; + } + } + + sum /= invtelecine->width * (invtelecine->height / 2 - 1); + + g_assert (sum > 0); + + //return MIN (sum, MAX_FIELD_SCORE); + return sqrt (sum); } static void @@ -384,12 +518,31 @@ gst_invtelecine_push_field (GstInvtelecine * invtelecine, GstBuffer * buffer, invtelecine->fifo[i].prev = gst_invtelecine_compare_fields (invtelecine, i, i - 1); invtelecine->fifo[i].prev2 = - gst_invtelecine_compare_same_fields (invtelecine, i, i - 2); + gst_invtelecine_compare_fields_mse (invtelecine, i, i - 2); + + if (invtelecine->verify_field_flags) { + invtelecine->fifo[i].prev3 = + gst_invtelecine_compare_fields_mse_ave (invtelecine, i, i - 3); + invtelecine->fifo[i].prev1 = + gst_invtelecine_compare_fields_mse_ave (invtelecine, i, i - 1); + +#define ALPHA 0.2 + if (invtelecine->fifo[i].prev3 != 0) { + invtelecine->bad_flag_metric *= (1 - ALPHA); + invtelecine->bad_flag_metric += + ALPHA * (invtelecine->fifo[i].prev1 / invtelecine->fifo[i].prev3); + } #if 0 - g_print ("compare %g %g\n", invtelecine->fifo[i].prev, - invtelecine->fifo[i].prev2); + g_print ("42 %g %g %g\n", invtelecine->bad_flag_metric, + invtelecine->fifo[i].prev1, invtelecine->fifo[i].prev3); #endif + if (invtelecine->bad_flag_metric > 1.2) { + GST_WARNING ("bad field flags? metric %g > 1.2", + invtelecine->bad_flag_metric); + } + } + } int pulldown_2_3[] = { 2, 3 }; @@ -696,6 +849,10 @@ gst_invtelecine_chain (GstPad * pad, GstBuffer * buffer) (guint) (GST_BUFFER_TIMESTAMP (buffer) % GST_SECOND)); field_index = (GST_BUFFER_FLAGS (buffer) & GST_VIDEO_BUFFER_TFF) ? 0 : 1; +//#define BAD +#ifdef BAD + field_index ^= 1; +#endif GST_DEBUG ("duration %" GST_TIME_FORMAT " flags %04x %s %s %s", GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)), @@ -706,13 +863,14 @@ gst_invtelecine_chain (GstPad * pad, GstBuffer * buffer) ""); if (GST_BUFFER_FLAGS (buffer) & GST_BUFFER_FLAG_DISCONT) { - GST_DEBUG ("discont"); + GST_ERROR ("discont"); invtelecine->next_field = field_index; + invtelecine->bad_flag_metric = 1.0; } if (invtelecine->next_field != field_index) { - GST_DEBUG ("wrong field first, expecting %d got %d", + GST_WARNING ("wrong field first, expecting %d got %d", invtelecine->next_field, field_index); invtelecine->next_field = field_index; } @@ -743,9 +901,12 @@ static void gst_invtelecine_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { - //GstInvtelecine *invtelecine = GST_INVTELECINE (object); + GstInvtelecine *invtelecine = GST_INVTELECINE (object); switch (prop_id) { + case PROP_VERIFY_FIELD_FLAGS: + invtelecine->verify_field_flags = g_value_get_boolean (value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -756,9 +917,12 @@ static void gst_invtelecine_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { - //GstInvtelecine *invtelecine = GST_INVTELECINE (object); + GstInvtelecine *invtelecine = GST_INVTELECINE (object); switch (prop_id) { + case PROP_VERIFY_FIELD_FLAGS: + g_value_set_boolean (value, invtelecine->verify_field_flags); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break;