diff --git a/ChangeLog b/ChangeLog index 5bca4bb08f..5159572534 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2007-09-04 Wim Taymans + + * gst/gststructure.c: + (gst_structure_fixate_field_nearest_fraction): + Fix fraction list fixation code. Take the fraction with the smallest + difference with the target instead of the first one in the list. + + * tests/check/gst/gststructure.c: (GST_START_TEST), + (gst_structure_suite): + Added test to verify correct fraction list fixation behaviour. + 2007-09-02 Tim-Philipp Müller * win32/common/libgstreamer.def: diff --git a/gst/gststructure.c b/gst/gststructure.c index 66a570d523..cc7324769d 100644 --- a/gst/gststructure.c +++ b/gst/gststructure.c @@ -2167,23 +2167,23 @@ gst_structure_fixate_field_nearest_fraction (GstStructure * structure, gst_value_set_fraction (&target, target_numerator, target_denominator); + best = NULL; + n = gst_value_list_get_size (value); for (i = 0; i < n; i++) { list_value = gst_value_list_get_value (value, i); if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) { - if (best == NULL) { - best = list_value; - gst_value_set_fraction (&best_diff, 0, 1); - } else { - if (gst_value_compare (list_value, &target) == GST_VALUE_LESS_THAN) - gst_value_fraction_subtract (&cur_diff, &target, list_value); - else - gst_value_fraction_subtract (&cur_diff, list_value, &target); - if (gst_value_compare (&cur_diff, &best_diff) == GST_VALUE_LESS_THAN) { - best = list_value; - g_value_copy (&cur_diff, &best_diff); - } + if (gst_value_compare (list_value, &target) == GST_VALUE_LESS_THAN) + gst_value_fraction_subtract (&cur_diff, &target, list_value); + else + gst_value_fraction_subtract (&cur_diff, list_value, &target); + + if (!best + || gst_value_compare (&cur_diff, + &best_diff) == GST_VALUE_LESS_THAN) { + best = list_value; + g_value_copy (&cur_diff, &best_diff); } } } @@ -2196,6 +2196,5 @@ gst_structure_fixate_field_nearest_fraction (GstStructure * structure, g_value_unset (&target); return res; } - return FALSE; } diff --git a/tests/check/gst/gststructure.c b/tests/check/gst/gststructure.c index e9620a2e3a..029eab0095 100644 --- a/tests/check/gst/gststructure.c +++ b/tests/check/gst/gststructure.c @@ -204,6 +204,44 @@ GST_START_TEST (test_fixate) GST_END_TEST; +GST_START_TEST (test_fixate_frac_list) +{ + GstStructure *s; + GValue list = { 0 }; + GValue frac = { 0 }; + gchar *str; + gint num, denom; + + g_value_init (&list, GST_TYPE_LIST); + g_value_init (&frac, GST_TYPE_FRACTION); + + gst_value_set_fraction (&frac, 30, 1); + gst_value_list_append_value (&list, &frac); + gst_value_set_fraction (&frac, 15, 1); + gst_value_list_append_value (&list, &frac); + gst_value_set_fraction (&frac, 10, 1); + gst_value_list_append_value (&list, &frac); + + s = gst_structure_new ("name", NULL); + gst_structure_set_value (s, "frac", &list); + g_value_unset (&frac); + g_value_unset (&list); + + str = gst_structure_to_string (s); + GST_DEBUG ("list %s", str); + g_free (str); + + /* fixate to the nearest fraction, this should give 15/1 */ + fail_unless (gst_structure_fixate_field_nearest_fraction (s, "frac", 14, 1)); + + fail_unless (gst_structure_get_fraction (s, "frac", &num, &denom)); + fail_unless (num == 15); + fail_unless (denom == 1); + + gst_structure_free (s); +} + +GST_END_TEST; Suite * gst_structure_suite (void) @@ -217,6 +255,7 @@ gst_structure_suite (void) tcase_add_test (tc_chain, test_complete_structure); tcase_add_test (tc_chain, test_structure_new); tcase_add_test (tc_chain, test_fixate); + tcase_add_test (tc_chain, test_fixate_frac_list); return s; }