winks: port to 1.0
[platform/upstream/gstreamer.git] / sys / winks / ksvideohelpers.c
1 /*
2  * Copyright (C) 2007 Haakon Sporsheim <hakon.sporsheim@tandberg.com>
3  *               2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
4  *               2009 Knut Inge Hvidsten <knut.inge.hvidsten@tandberg.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "ksvideohelpers.h"
23
24 #include <math.h>
25 #include <uuids.h>
26 #include "kshelpers.h"
27
28 GST_DEBUG_CATEGORY_EXTERN (gst_ks_debug);
29 #define GST_CAT_DEFAULT gst_ks_debug
30
31 static const GUID MEDIASUBTYPE_FOURCC =
32     { 0x0 /* FourCC here */ , 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00,
33     0x38, 0x9B, 0x71}
34 };
35
36 typedef struct _KsVideoDeviceEntry KsVideoDeviceEntry;
37
38 struct _KsVideoDeviceEntry
39 {
40   KsDeviceEntry *device;
41   gint priority;
42 };
43
44 static void
45 ks_video_device_entry_decide_priority (KsVideoDeviceEntry * videodevice)
46 {
47   HANDLE filter_handle;
48
49   videodevice->priority = 0;
50
51   filter_handle = CreateFile (videodevice->device->path,
52       GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
53       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
54   if (ks_is_valid_handle (filter_handle)) {
55     GUID *propsets = NULL;
56     gulong propsets_len;
57
58     if (ks_object_get_supported_property_sets (filter_handle, &propsets,
59             &propsets_len)) {
60       gulong i;
61
62       for (i = 0; i < propsets_len; i++) {
63         if (memcmp (&propsets[i], &PROPSETID_VIDCAP_CAMERACONTROL,
64                 sizeof (GUID)) == 0) {
65           videodevice->priority++;
66           break;
67         }
68       }
69
70       g_free (propsets);
71     }
72   }
73
74   CloseHandle (filter_handle);
75 }
76
77 static gint
78 ks_video_device_entry_compare (gconstpointer a, gconstpointer b)
79 {
80   const KsVideoDeviceEntry *videodevice_a = a;
81   const KsVideoDeviceEntry *videodevice_b = b;
82
83   if (videodevice_a->priority > videodevice_b->priority)
84     return -1;
85   else if (videodevice_a->priority == videodevice_b->priority)
86     return 0;
87   else
88     return 1;
89 }
90
91 GList *
92 ks_video_device_list_sort_cameras_first (GList * devices)
93 {
94   GList *videodevices = NULL, *walk;
95   guint i;
96
97   for (walk = devices; walk != NULL; walk = walk->next) {
98     KsDeviceEntry *device = walk->data;
99     KsVideoDeviceEntry *videodevice;
100
101     videodevice = g_new (KsVideoDeviceEntry, 1);
102     videodevice->device = device;
103     ks_video_device_entry_decide_priority (videodevice);
104
105     videodevices = g_list_append (videodevices, videodevice);
106   }
107
108   videodevices = g_list_sort (videodevices, ks_video_device_entry_compare);
109
110   g_list_free (devices);
111   devices = NULL;
112
113   for (walk = videodevices, i = 0; walk != NULL; walk = walk->next, i++) {
114     KsVideoDeviceEntry *videodevice = walk->data;
115
116     videodevice->device->index = i;
117     devices = g_list_append (devices, videodevice->device);
118
119     g_free (videodevice);
120   }
121
122   g_list_free (videodevices);
123
124   return devices;
125 }
126
127 static GstStructure *
128 ks_video_format_to_structure (GUID subtype_guid, GUID format_guid)
129 {
130   GstStructure *structure = NULL;
131   const gchar *media_type = NULL, *format = NULL;
132
133   if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_MJPG) || IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_TVMJ) ||     /* FIXME: NOT tested */
134       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_WAKE) ||        /* FIXME: NOT tested */
135       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_CFCC) ||        /* FIXME: NOT tested */
136       IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_IJPG)) {        /* FIXME: NOT tested */
137     media_type = "image/jpeg";
138   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB555)) {
139     media_type = "video/x-raw";
140     format = "RGB15";
141   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB565)) {
142     media_type = "video/x-raw";
143     format = "RGB16";
144   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB24)) {
145     media_type = "video/x-raw";
146     format = "RGBx";
147   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB32)) {
148     media_type = "video/x-raw";
149     format = "RGB";
150   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB32)) {
151     media_type = "video/x-raw";
152     format = "ARGB";
153   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB1555)) {
154     GST_WARNING ("Unsupported video format ARGB15555");
155   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB4444)) {
156     GST_WARNING ("Unsupported video format ARGB4444");
157   } else if (memcmp (&subtype_guid.Data2, &MEDIASUBTYPE_FOURCC.Data2,
158           sizeof (subtype_guid) - sizeof (subtype_guid.Data1)) == 0) {
159     guint8 *p = (guint8 *) & subtype_guid.Data1;
160     gchar *format = g_strdup_printf ("%c%c%c%c", p[0], p[1], p[2], p[3]);
161     structure = gst_structure_new ("video/x-raw", "format",
162         G_TYPE_STRING, format, NULL);
163     g_free (format);
164   } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_dvsd)) {
165     if (IsEqualGUID (&format_guid, &FORMAT_DvInfo)) {
166       structure = gst_structure_new ("video/x-dv",
167           "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
168     } else if (IsEqualGUID (&format_guid, &FORMAT_VideoInfo)) {
169       structure = gst_structure_new ("video/x-dv",
170           "systemstream", G_TYPE_BOOLEAN, FALSE,
171           "format", G_TYPE_STRING, "dvsd", NULL);
172     }
173   }
174
175   if (media_type) {
176     structure = gst_structure_new_empty (media_type);
177     if (format) {
178       gst_structure_set (structure, "format", G_TYPE_STRING, format, NULL);
179     }
180   }
181
182   if (!structure) {
183     GST_DEBUG ("Unknown DirectShow Video GUID %08x-%04x-%04x-%04x-%08x%04x",
184         (guint) subtype_guid.Data1, (guint) subtype_guid.Data2,
185         (guint) subtype_guid.Data3,
186         (guint) subtype_guid.Data4, (guint) & subtype_guid.Data4[2],
187         (guint) & subtype_guid.Data4[6]);
188   }
189
190   return structure;
191 }
192
193 static void
194 guess_aspect (gint width, gint height, gint * par_width, gint * par_height)
195 {
196   /*
197    * As we dont have access to the actual pixel aspect, we will try to do a
198    * best-effort guess. The guess is based on most sensors being either 4/3
199    * or 16/9, and most pixel aspects being close to 1/1.
200    */
201   if ((width == 768) && (height == 448)) {      /* special case for w448p */
202     *par_width = 28;
203     *par_height = 27;
204   } else {
205     if (((float) width / (float) height) < 1.2778) {
206       *par_width = 12;
207       *par_height = 11;
208     } else {
209       *par_width = 1;
210       *par_height = 1;
211     }
212   }
213 }
214
215 /* NOTE: would probably be better to use a continued fractions approach here */
216 static void
217 compress_fraction (gint64 in_num, gint64 in_den, gint64 * out_num,
218     gint64 * out_den)
219 {
220   gdouble on, od, orig;
221   guint denominators[] = { 1, 2, 3, 5, 7 }, i;
222   const gdouble max_loss = 0.1;
223
224   on = in_num;
225   od = in_den;
226   orig = on / od;
227
228   for (i = 0; i < G_N_ELEMENTS (denominators); i++) {
229     gint64 cur_n, cur_d;
230     gdouble cur, loss;
231
232     cur_n = floor ((on / (od / (gdouble) denominators[i])) + 0.5);
233     cur_d = denominators[i];
234     cur = (gdouble) cur_n / (gdouble) cur_d;
235     loss = fabs (cur - orig);
236
237     if (loss <= max_loss) {
238       *out_num = cur_n;
239       *out_den = cur_d;
240
241       return;
242     }
243   }
244
245   *out_num = in_num;
246   *out_den = in_den;
247 }
248
249 static gboolean
250 ks_video_append_video_stream_cfg_fields (GstStructure * structure,
251     const KS_VIDEO_STREAM_CONFIG_CAPS * vscc)
252 {
253   GValue val = { 0, };
254   gint64 min_n, min_d;
255   gint64 max_n, max_d;
256
257   g_return_val_if_fail (structure, FALSE);
258   g_return_val_if_fail (vscc, FALSE);
259
260   /* width */
261   if (vscc->MinOutputSize.cx == vscc->MaxOutputSize.cx) {
262     gst_structure_set (structure,
263         "width", G_TYPE_INT, vscc->MaxOutputSize.cx, NULL);
264   } else {
265     gst_structure_set (structure,
266         "width", GST_TYPE_INT_RANGE,
267         vscc->MinOutputSize.cx, vscc->MaxOutputSize.cx, NULL);
268   }
269
270   /* height */
271   if (vscc->MinOutputSize.cy == vscc->MaxOutputSize.cy) {
272     gst_structure_set (structure,
273         "height", G_TYPE_INT, vscc->MaxOutputSize.cy, NULL);
274   } else {
275     gst_structure_set (structure,
276         "height", GST_TYPE_INT_RANGE,
277         vscc->MinOutputSize.cy, vscc->MaxOutputSize.cy, NULL);
278   }
279
280   /* framerate */
281   compress_fraction (NANOSECONDS, vscc->MinFrameInterval, &min_n, &min_d);
282   compress_fraction (NANOSECONDS, vscc->MaxFrameInterval, &max_n, &max_d);
283
284   if (min_n == max_n && min_d == max_d) {
285     g_value_init (&val, GST_TYPE_FRACTION);
286     gst_value_set_fraction (&val, max_n, max_d);
287   } else {
288     g_value_init (&val, GST_TYPE_FRACTION_RANGE);
289     gst_value_set_fraction_range_full (&val, max_n, max_d, min_n, min_d);
290   }
291
292   gst_structure_set_value (structure, "framerate", &val);
293   g_value_unset (&val);
294
295   {
296     gint par_width, par_height;
297
298     guess_aspect (vscc->MaxOutputSize.cx, vscc->MaxOutputSize.cy,
299         &par_width, &par_height);
300
301     gst_structure_set (structure,
302         "pixel-aspect-ratio", GST_TYPE_FRACTION, par_width, par_height, NULL);
303   }
304
305   return TRUE;
306 }
307
308 KsVideoMediaType *
309 ks_video_media_type_dup (KsVideoMediaType * media_type)
310 {
311   KsVideoMediaType *result = g_new (KsVideoMediaType, 1);
312
313   memcpy (result, media_type, sizeof (KsVideoMediaType));
314
315   result->range = g_malloc (media_type->range->FormatSize);
316   memcpy ((gpointer) result->range, media_type->range,
317       media_type->range->FormatSize);
318
319   result->format = g_malloc (media_type->format_size);
320   memcpy (result->format, media_type->format, media_type->format_size);
321
322   result->translated_caps = gst_caps_ref (media_type->translated_caps);
323
324   return result;
325 }
326
327 void
328 ks_video_media_type_free (KsVideoMediaType * media_type)
329 {
330   if (media_type == NULL)
331     return;
332
333   g_free ((gpointer) media_type->range);
334
335   g_free (media_type->format);
336
337   if (media_type->translated_caps != NULL)
338     gst_caps_unref (media_type->translated_caps);
339
340   g_free (media_type);
341 }
342
343 static GList *
344 ks_video_media_type_list_remove_duplicates (GList * media_types)
345 {
346   GList *master, *duplicates;
347
348   do {
349     GList *entry;
350
351     master = duplicates = NULL;
352
353     /* Find the first set of duplicates and their master */
354     for (entry = media_types; entry != NULL && duplicates == NULL;
355         entry = entry->next) {
356       KsVideoMediaType *mt = entry->data;
357       GList *other_entry;
358
359       for (other_entry = media_types; other_entry != NULL;
360           other_entry = other_entry->next) {
361         KsVideoMediaType *other_mt = other_entry->data;
362
363         if (other_mt == mt)
364           continue;
365
366         if (gst_caps_is_equal (mt->translated_caps, other_mt->translated_caps))
367           duplicates = g_list_prepend (duplicates, other_mt);
368       }
369
370       if (duplicates != NULL)
371         master = entry;
372     }
373
374     if (duplicates != NULL) {
375       KsVideoMediaType *selected_mt = master->data;
376
377       /*
378        * Pick a FORMAT_VideoInfo2 if present, if not we just stay with the
379        * first entry
380        */
381       for (entry = duplicates; entry != NULL; entry = entry->next) {
382         KsVideoMediaType *mt = entry->data;
383
384         if (IsEqualGUID (&mt->range->Specifier, &FORMAT_VideoInfo2)) {
385           ks_video_media_type_free (selected_mt);
386           selected_mt = mt;
387         } else {
388           ks_video_media_type_free (mt);
389         }
390
391         /* Remove the dupe from the main list */
392         media_types = g_list_remove (media_types, mt);
393       }
394
395       /* Update master node with the selected MediaType */
396       master->data = selected_mt;
397
398       g_list_free (duplicates);
399     }
400   }
401   while (master != NULL);
402
403   return media_types;
404 }
405
406 GList *
407 ks_video_probe_filter_for_caps (HANDLE filter_handle)
408 {
409   GList *ret = NULL;
410   gulong pin_count;
411   guint pin_id;
412
413   if (!ks_filter_get_pin_property (filter_handle, 0, KSPROPSETID_Pin,
414           KSPROPERTY_PIN_CTYPES, &pin_count, sizeof (pin_count), NULL))
415     goto beach;
416
417   GST_DEBUG ("pin_count = %lu", pin_count);
418
419   for (pin_id = 0; pin_id < pin_count; pin_id++) {
420     KSPIN_COMMUNICATION pin_comm;
421     KSPIN_DATAFLOW pin_flow;
422     GUID pin_cat;
423
424     if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
425             KSPROPERTY_PIN_COMMUNICATION, &pin_comm, sizeof (pin_comm), NULL))
426       continue;
427
428     if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
429             KSPROPERTY_PIN_DATAFLOW, &pin_flow, sizeof (pin_flow), NULL))
430       continue;
431
432     if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
433             KSPROPERTY_PIN_CATEGORY, &pin_cat, sizeof (pin_cat), NULL))
434       continue;
435
436     GST_DEBUG ("pin[%u]: pin_comm=%d, pin_flow=%d", pin_id, pin_comm, pin_flow);
437
438     if (pin_flow == KSPIN_DATAFLOW_OUT &&
439         memcmp (&pin_cat, &PINNAME_CAPTURE, sizeof (GUID)) == 0) {
440       KSMULTIPLE_ITEM *items;
441
442       if (ks_filter_get_pin_property_multi (filter_handle, pin_id,
443               KSPROPSETID_Pin, KSPROPERTY_PIN_DATARANGES, &items, NULL)) {
444         KSDATARANGE *range = (KSDATARANGE *) (items + 1);
445         guint i;
446
447         for (i = 0; i < items->Count; i++) {
448           if (IsEqualGUID (&range->MajorFormat, &KSDATAFORMAT_TYPE_VIDEO)) {
449             KsVideoMediaType *entry;
450             gpointer src_vscc, src_format;
451             GstStructure *media_structure;
452
453             entry = g_new0 (KsVideoMediaType, 1);
454             entry->pin_id = pin_id;
455
456             entry->range = g_malloc (range->FormatSize);
457             memcpy ((gpointer) entry->range, range, range->FormatSize);
458
459             if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo)) {
460               KS_DATARANGE_VIDEO *vr = (KS_DATARANGE_VIDEO *) entry->range;
461
462               src_vscc = &vr->ConfigCaps;
463               src_format = &vr->VideoInfoHeader;
464
465               entry->format_size = sizeof (vr->VideoInfoHeader);
466               entry->sample_size = vr->VideoInfoHeader.bmiHeader.biSizeImage;
467             } else if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo2)) {
468               KS_DATARANGE_VIDEO2 *vr = (KS_DATARANGE_VIDEO2 *) entry->range;
469
470               src_vscc = &vr->ConfigCaps;
471               src_format = &vr->VideoInfoHeader;
472
473               entry->format_size = sizeof (vr->VideoInfoHeader);
474               entry->sample_size = vr->VideoInfoHeader.bmiHeader.biSizeImage;
475             } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEGVideo)) {
476               /* Untested and probably wrong... */
477               KS_DATARANGE_MPEG1_VIDEO *vr =
478                   (KS_DATARANGE_MPEG1_VIDEO *) entry->range;
479
480               src_vscc = &vr->ConfigCaps;
481               src_format = &vr->VideoInfoHeader;
482
483               entry->format_size = sizeof (vr->VideoInfoHeader);
484               entry->sample_size =
485                   vr->VideoInfoHeader.hdr.bmiHeader.biSizeImage;
486             } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEG2Video)) {
487               /* Untested and probably wrong... */
488               KS_DATARANGE_MPEG2_VIDEO *vr =
489                   (KS_DATARANGE_MPEG2_VIDEO *) entry->range;
490
491               src_vscc = &vr->ConfigCaps;
492               src_format = &vr->VideoInfoHeader;
493
494               entry->format_size = sizeof (vr->VideoInfoHeader);
495               entry->sample_size =
496                   vr->VideoInfoHeader.hdr.bmiHeader.biSizeImage;
497             } else {
498               gchar *guid_str;
499
500               guid_str = ks_guid_to_string (&range->Specifier);
501               GST_DEBUG ("pin[%u]: ignoring unknown specifier GUID %s",
502                   pin_id, guid_str);
503               g_free (guid_str);
504
505               ks_video_media_type_free (entry);
506               entry = NULL;
507             }
508
509             if (entry != NULL) {
510               g_assert (entry->sample_size != 0);
511
512               memcpy ((gpointer) & entry->vscc, src_vscc, sizeof (entry->vscc));
513
514               entry->format = g_malloc (entry->format_size);
515               memcpy (entry->format, src_format, entry->format_size);
516
517               media_structure =
518                   ks_video_format_to_structure (range->SubFormat,
519                   range->MajorFormat);
520
521               if (media_structure == NULL) {
522                 g_warning ("ks_video_format_to_structure returned NULL");
523                 ks_video_media_type_free (entry);
524                 entry = NULL;
525               } else if (ks_video_append_video_stream_cfg_fields
526                   (media_structure, &entry->vscc)) {
527                 entry->translated_caps = gst_caps_new_empty ();
528                 gst_caps_append_structure (entry->translated_caps,
529                     media_structure);
530               } else {
531                 gst_structure_free (media_structure);
532                 ks_video_media_type_free (entry);
533                 entry = NULL;
534               }
535
536               if (entry != NULL)
537                 ret = g_list_prepend (ret, entry);
538             }
539           }
540
541           /* REVISIT: Each KSDATARANGE should start on a 64-bit boundary */
542           range = (KSDATARANGE *) (((guchar *) range) + range->FormatSize);
543         }
544
545         g_free (items);
546       }
547     }
548   }
549
550   if (ret != NULL) {
551     ret = g_list_reverse (ret);
552     ret = ks_video_media_type_list_remove_duplicates (ret);
553   }
554
555 beach:
556   return ret;
557 }
558
559 KSPIN_CONNECT *
560 ks_video_create_pin_conn_from_media_type (KsVideoMediaType * media_type)
561 {
562   KSPIN_CONNECT *conn = NULL;
563   KSDATAFORMAT *format = NULL;
564   guint8 *vih;
565
566   conn = g_malloc0 (sizeof (KSPIN_CONNECT) + sizeof (KSDATAFORMAT) +
567       media_type->format_size);
568
569   conn->Interface.Set = KSINTERFACESETID_Standard;
570   conn->Interface.Id = KSINTERFACE_STANDARD_STREAMING;
571   conn->Interface.Flags = 0;
572
573   conn->Medium.Set = KSMEDIUMSETID_Standard;
574   conn->Medium.Id = KSMEDIUM_TYPE_ANYINSTANCE;
575   conn->Medium.Flags = 0;
576
577   conn->PinId = media_type->pin_id;
578   conn->PinToHandle = NULL;
579   conn->Priority.PriorityClass = KSPRIORITY_NORMAL;
580   conn->Priority.PrioritySubClass = KSPRIORITY_NORMAL;
581
582   format = (KSDATAFORMAT *) (conn + 1);
583   memcpy (format, media_type->range, sizeof (KSDATAFORMAT));
584   format->FormatSize = sizeof (KSDATAFORMAT) + media_type->format_size;
585
586   vih = (guint8 *) (format + 1);
587   memcpy (vih, media_type->format, media_type->format_size);
588
589   return conn;
590 }
591
592 gboolean
593 ks_video_fixate_media_type (const KSDATARANGE * range,
594     guint8 * format, gint width, gint height, gint fps_n, gint fps_d)
595 {
596   KS_DATARANGE_VIDEO *vr;
597   KS_VIDEOINFOHEADER *vih;
598   KS_BITMAPINFOHEADER *bih;
599   DWORD dwRate;
600
601   g_return_val_if_fail (format != NULL, FALSE);
602
603   if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo)) {
604     bih = &((KS_VIDEOINFOHEADER *) format)->bmiHeader;
605   } else if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo2)) {
606     bih = &((KS_VIDEOINFOHEADER2 *) format)->bmiHeader;
607   } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEGVideo)) {
608     bih = &((KS_MPEG1VIDEOINFO *) format)->hdr.bmiHeader;
609   } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEG2Video)) {
610     bih = &((KS_MPEGVIDEOINFO2 *) format)->hdr.bmiHeader;
611   } else {
612     return FALSE;
613   }
614
615   /* These formats' structures share the most basic stuff */
616   vr = (KS_DATARANGE_VIDEO *) range;
617   vih = (KS_VIDEOINFOHEADER *) format;
618
619   /* FIXME: Need to figure out how to properly handle ranges */
620   if (bih->biWidth != width || bih->biHeight != height)
621     return FALSE;
622
623   /* Framerate, clamped because of fraction conversion rounding errors */
624   vih->AvgTimePerFrame =
625       gst_util_uint64_scale_int_round (NANOSECONDS, fps_d, fps_n);
626   vih->AvgTimePerFrame =
627       MAX (vih->AvgTimePerFrame, vr->ConfigCaps.MinFrameInterval);
628   vih->AvgTimePerFrame =
629       MIN (vih->AvgTimePerFrame, vr->ConfigCaps.MaxFrameInterval);
630
631   /* Bitrate, clamped for the same reason as framerate */
632   dwRate = (width * height * fps_n) / fps_d;
633   vih->dwBitRate = dwRate * bih->biBitCount;
634   vih->dwBitRate = MAX (vih->dwBitRate, vr->ConfigCaps.MinBitsPerSecond);
635   vih->dwBitRate = MIN (vih->dwBitRate, vr->ConfigCaps.MaxBitsPerSecond);
636
637   return TRUE;
638 }
639
640 static GstStructure *
641 ks_video_append_var_video_fields (GstStructure * structure)
642 {
643   if (structure) {
644     gst_structure_set (structure,
645         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
646         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
647         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
648   }
649
650   return structure;
651 }
652
653 GstCaps *
654 ks_video_get_all_caps (void)
655 {
656   static GstCaps *caps = NULL;
657
658   if (caps == NULL) {
659     GstStructure *structure;
660     caps = gst_caps_new_empty ();
661
662     /* from Windows SDK 6.0 uuids.h */
663     /* RGB formats */
664     structure =
665         ks_video_append_var_video_fields (ks_video_format_to_structure
666         (MEDIASUBTYPE_RGB555, FORMAT_VideoInfo));
667     gst_caps_append_structure (caps, structure);
668
669     structure =
670         ks_video_append_var_video_fields (ks_video_format_to_structure
671         (MEDIASUBTYPE_RGB565, FORMAT_VideoInfo));
672     gst_caps_append_structure (caps, structure);
673
674     structure =
675         ks_video_append_var_video_fields (ks_video_format_to_structure
676         (MEDIASUBTYPE_RGB24, FORMAT_VideoInfo));
677     gst_caps_append_structure (caps, structure);
678
679     structure =
680         ks_video_append_var_video_fields (ks_video_format_to_structure
681         (MEDIASUBTYPE_RGB32, FORMAT_VideoInfo));
682     gst_caps_append_structure (caps, structure);
683
684     /* YUV formats */
685     structure =
686         ks_video_append_var_video_fields (gst_structure_new_empty
687         ("video/x-raw"));
688     gst_caps_append_structure (caps, structure);
689
690     /* Other formats */
691     structure =
692         ks_video_append_var_video_fields (ks_video_format_to_structure
693         (MEDIASUBTYPE_MJPG, FORMAT_VideoInfo));
694     gst_caps_append_structure (caps, structure);
695
696     structure =
697         ks_video_append_var_video_fields (ks_video_format_to_structure
698         (MEDIASUBTYPE_dvsd, FORMAT_VideoInfo));
699     gst_caps_append_structure (caps, structure);
700
701     structure =                 /* no variable video fields (width, height, framerate) */
702         ks_video_format_to_structure (MEDIASUBTYPE_dvsd, FORMAT_DvInfo);
703     gst_caps_append_structure (caps, structure);
704   }
705
706   return caps;
707 }