+struct PreferedCapsInfo
+{
+ gint width;
+ gint height;
+ gint fps_n;
+ gint fps_d;
+};
+
+static gboolean
+gst_vl42_src_fixate_fields (GQuark field_id, GValue * value, gpointer user_data)
+{
+ GstStructure *s = user_data;
+
+ if (field_id == g_quark_from_string ("interlace-mode"))
+ return TRUE;
+
+ if (field_id == g_quark_from_string ("colorimetry"))
+ return TRUE;
+
+ gst_structure_fixate_field (s, g_quark_to_string (field_id));
+
+ return TRUE;
+}
+
+static void
+gst_v4l2_src_fixate_struct_with_preference (GstStructure * s,
+ struct PreferedCapsInfo *pref)
+{
+ if (gst_structure_has_field (s, "width"))
+ gst_structure_fixate_field_nearest_int (s, "width", pref->width);
+
+ if (gst_structure_has_field (s, "height"))
+ gst_structure_fixate_field_nearest_int (s, "height", pref->height);
+
+ if (gst_structure_has_field (s, "framerate"))
+ gst_structure_fixate_field_nearest_fraction (s, "framerate", pref->fps_n,
+ pref->fps_d);
+
+ /* Finally, fixate everything else except the interlace-mode and colorimetry
+ * which still need further negotiation as it wasn't probed */
+ gst_structure_map_in_place (s, gst_vl42_src_fixate_fields, s);
+}
+
+static void
+gst_v4l2_src_parse_fixed_struct (GstStructure * s,
+ gint * width, gint * height, gint * fps_n, gint * fps_d)
+{
+ if (gst_structure_has_field (s, "width") && width)
+ gst_structure_get_int (s, "width", width);
+
+ if (gst_structure_has_field (s, "height") && height)
+ gst_structure_get_int (s, "height", height);
+
+ if (gst_structure_has_field (s, "framerate") && fps_n && fps_d)
+ gst_structure_get_fraction (s, "framerate", fps_n, fps_d);
+}
+
+/* TODO Consider framerate */
+static gint
+gst_v4l2src_fixed_caps_compare (GstCaps * caps_a, GstCaps * caps_b,
+ struct PreferedCapsInfo *pref)
+{
+ GstStructure *a, *b;
+ gint aw = G_MAXINT, ah = G_MAXINT, ad = G_MAXINT;
+ gint bw = G_MAXINT, bh = G_MAXINT, bd = G_MAXINT;
+ gint ret;
+
+ a = gst_caps_get_structure (caps_a, 0);
+ b = gst_caps_get_structure (caps_b, 0);
+
+ gst_v4l2_src_parse_fixed_struct (a, &aw, &ah, NULL, NULL);
+ gst_v4l2_src_parse_fixed_struct (b, &bw, &bh, NULL, NULL);
+
+ /* When both are smaller then pref, just append to the end */
+ if ((bw < pref->width || bh < pref->height)
+ && (aw < pref->width || ah < pref->height)) {
+ ret = 1;
+ goto done;
+ }
+
+ /* If a is smaller then pref and not b, then a goes after b */
+ if (aw < pref->width || ah < pref->height) {
+ ret = 1;
+ goto done;
+ }
+
+ /* If b is smaller then pref and not a, then a goes before b */
+ if (bw < pref->width || bh < pref->height) {
+ ret = -1;
+ goto done;
+ }
+
+ /* Both are larger or equal to the preference, prefer the smallest */
+ ad = MAX (1, aw - pref->width) * MAX (1, ah - pref->height);
+ bd = MAX (1, bw - pref->width) * MAX (1, bh - pref->height);
+
+ /* Adjust slightly in case width/height matched the preference */
+ if (aw == pref->width)
+ ad -= 1;
+
+ if (ah == pref->height)
+ ad -= 1;
+
+ if (bw == pref->width)
+ bd -= 1;
+
+ if (bh == pref->height)
+ bd -= 1;
+
+ /* If the choices are equivalent, maintain the order */
+ if (ad == bd)
+ ret = 1;
+ else
+ ret = ad - bd;
+
+done:
+ GST_TRACE ("Placing %ix%i (%s) %s %ix%i (%s)", aw, ah,
+ gst_structure_get_string (a, "format"), ret > 0 ? "after" : "before", bw,
+ bh, gst_structure_get_string (b, "format"));
+ return ret;
+}
+
+static gboolean
+gst_v4l2src_set_format (GstV4l2Src * v4l2src, GstCaps * caps,
+ GstV4l2Error * error)
+{
+ GstV4l2Object *obj;
+
+ obj = v4l2src->v4l2object;
+
+ /* make sure we stop capturing and dealloc buffers */
+ if (!gst_v4l2_object_stop (obj))
+ return FALSE;
+
+ g_signal_emit (v4l2src, gst_v4l2_signals[SIGNAL_PRE_SET_FORMAT], 0,
+ v4l2src->v4l2object->video_fd, caps);
+
+ return gst_v4l2_object_set_format (obj, caps, error);
+}
+