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>
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.
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.
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.
22 #include "ksvideohelpers.h"
26 #include "kshelpers.h"
28 GST_DEBUG_CATEGORY_EXTERN (gst_ks_debug);
29 #define GST_CAT_DEFAULT gst_ks_debug
31 static const GUID MEDIASUBTYPE_FOURCC =
32 { 0x0 /* FourCC here */ , 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00,
36 typedef struct _KsVideoDeviceEntry KsVideoDeviceEntry;
38 struct _KsVideoDeviceEntry
40 KsDeviceEntry *device;
45 ks_video_device_entry_decide_priority (KsVideoDeviceEntry * videodevice)
49 videodevice->priority = 0;
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;
58 if (ks_object_get_supported_property_sets (filter_handle, &propsets,
62 for (i = 0; i < propsets_len; i++) {
63 if (memcmp (&propsets[i], &PROPSETID_VIDCAP_CAMERACONTROL,
64 sizeof (GUID)) == 0) {
65 videodevice->priority++;
74 CloseHandle (filter_handle);
78 ks_video_device_entry_compare (gconstpointer a, gconstpointer b)
80 const KsVideoDeviceEntry *videodevice_a = a;
81 const KsVideoDeviceEntry *videodevice_b = b;
83 if (videodevice_a->priority > videodevice_b->priority)
85 else if (videodevice_a->priority == videodevice_b->priority)
92 ks_video_device_list_sort_cameras_first (GList * devices)
94 GList *videodevices = NULL, *walk;
97 for (walk = devices; walk != NULL; walk = walk->next) {
98 KsDeviceEntry *device = walk->data;
99 KsVideoDeviceEntry *videodevice;
101 videodevice = g_new (KsVideoDeviceEntry, 1);
102 videodevice->device = device;
103 ks_video_device_entry_decide_priority (videodevice);
105 videodevices = g_list_append (videodevices, videodevice);
108 videodevices = g_list_sort (videodevices, ks_video_device_entry_compare);
110 g_list_free (devices);
113 for (walk = videodevices, i = 0; walk != NULL; walk = walk->next, i++) {
114 KsVideoDeviceEntry *videodevice = walk->data;
116 videodevice->device->index = i;
117 devices = g_list_append (devices, videodevice->device);
119 g_free (videodevice);
122 g_list_free (videodevices);
127 static GstStructure *
128 ks_video_format_to_structure (GUID subtype_guid, GUID format_guid,
131 GstStructure *structure = NULL;
132 const gchar *media_type = NULL, *format = NULL;
133 /* RGB formats can be bottom-up (upside down) DIB */
134 gboolean is_rgb = FALSE;
136 if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_MJPG) || IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_TVMJ) || /* FIXME: NOT tested */
137 IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_WAKE) || /* FIXME: NOT tested */
138 IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_CFCC) || /* FIXME: NOT tested */
139 IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_IJPG)) { /* FIXME: NOT tested */
140 media_type = "image/jpeg";
141 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB555)) {
142 media_type = "video/x-raw";
145 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB565)) {
146 media_type = "video/x-raw";
149 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB24)) {
150 media_type = "video/x-raw";
153 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_RGB32)) {
154 media_type = "video/x-raw";
157 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB32)) {
158 media_type = "video/x-raw";
161 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB1555)) {
162 GST_WARNING ("Unsupported video format ARGB15555");
163 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_ARGB4444)) {
164 GST_WARNING ("Unsupported video format ARGB4444");
165 } else if (memcmp (&subtype_guid.Data2, &MEDIASUBTYPE_FOURCC.Data2,
166 sizeof (subtype_guid) - sizeof (subtype_guid.Data1)) == 0) {
167 guint8 *p = (guint8 *) & subtype_guid.Data1;
168 gchar *format = g_strdup_printf ("%c%c%c%c", p[0], p[1], p[2], p[3]);
169 structure = gst_structure_new ("video/x-raw", "format",
170 G_TYPE_STRING, format, NULL);
172 } else if (IsEqualGUID (&subtype_guid, &MEDIASUBTYPE_dvsd)) {
173 if (IsEqualGUID (&format_guid, &FORMAT_DvInfo)) {
174 structure = gst_structure_new ("video/x-dv",
175 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
176 } else if (IsEqualGUID (&format_guid, &FORMAT_VideoInfo)) {
177 structure = gst_structure_new ("video/x-dv",
178 "systemstream", G_TYPE_BOOLEAN, FALSE,
179 "format", G_TYPE_STRING, "dvsd", NULL);
184 structure = gst_structure_new_empty (media_type);
186 gst_structure_set (structure, "format", G_TYPE_STRING, format, NULL);
194 GST_DEBUG ("Unknown DirectShow Video GUID "
195 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
196 (guint) subtype_guid.Data1, subtype_guid.Data2, subtype_guid.Data3,
197 subtype_guid.Data4[0], subtype_guid.Data4[1], subtype_guid.Data4[2],
198 subtype_guid.Data4[3], subtype_guid.Data4[4], subtype_guid.Data4[5],
199 subtype_guid.Data4[6], subtype_guid.Data4[7]);
206 guess_aspect (gint width, gint height, gint * par_width, gint * par_height)
209 * As we dont have access to the actual pixel aspect, we will try to do a
210 * best-effort guess. The guess is based on most sensors being either 4/3
211 * or 16/9, and most pixel aspects being close to 1/1.
213 if ((width == 768) && (height == 448)) { /* special case for w448p */
217 if (((float) width / (float) height) < 1.2778) {
227 /* NOTE: would probably be better to use a continued fractions approach here */
229 compress_fraction (gint64 in_num, gint64 in_den, gint64 * out_num,
232 gdouble on, od, orig;
233 guint denominators[] = { 1, 2, 3, 5, 7 }, i;
234 const gdouble max_loss = 0.1;
240 for (i = 0; i < G_N_ELEMENTS (denominators); i++) {
244 cur_n = floor ((on / (od / (gdouble) denominators[i])) + 0.5);
245 cur_d = denominators[i];
246 cur = (gdouble) cur_n / (gdouble) cur_d;
247 loss = fabs (cur - orig);
249 if (loss <= max_loss) {
262 ks_video_append_video_stream_cfg_fields (GstStructure * structure,
263 const KS_VIDEO_STREAM_CONFIG_CAPS * vscc)
269 g_return_val_if_fail (structure, FALSE);
270 g_return_val_if_fail (vscc, FALSE);
273 if (vscc->MinOutputSize.cx == vscc->MaxOutputSize.cx) {
274 gst_structure_set (structure,
275 "width", G_TYPE_INT, vscc->MaxOutputSize.cx, NULL);
277 gst_structure_set (structure,
278 "width", GST_TYPE_INT_RANGE,
279 vscc->MinOutputSize.cx, vscc->MaxOutputSize.cx, NULL);
283 if (vscc->MinOutputSize.cy == vscc->MaxOutputSize.cy) {
284 gst_structure_set (structure,
285 "height", G_TYPE_INT, vscc->MaxOutputSize.cy, NULL);
287 gst_structure_set (structure,
288 "height", GST_TYPE_INT_RANGE,
289 vscc->MinOutputSize.cy, vscc->MaxOutputSize.cy, NULL);
293 compress_fraction (NANOSECONDS, vscc->MinFrameInterval, &min_n, &min_d);
294 compress_fraction (NANOSECONDS, vscc->MaxFrameInterval, &max_n, &max_d);
296 if (min_n == max_n && min_d == max_d) {
297 g_value_init (&val, GST_TYPE_FRACTION);
298 gst_value_set_fraction (&val, max_n, max_d);
300 g_value_init (&val, GST_TYPE_FRACTION_RANGE);
301 gst_value_set_fraction_range_full (&val, max_n, max_d, min_n, min_d);
304 gst_structure_set_value (structure, "framerate", &val);
305 g_value_unset (&val);
308 gint par_width, par_height;
310 guess_aspect (vscc->MaxOutputSize.cx, vscc->MaxOutputSize.cy,
311 &par_width, &par_height);
313 gst_structure_set (structure,
314 "pixel-aspect-ratio", GST_TYPE_FRACTION, par_width, par_height, NULL);
321 ks_video_media_type_dup (KsVideoMediaType * media_type)
323 KsVideoMediaType *result = g_new (KsVideoMediaType, 1);
325 memcpy (result, media_type, sizeof (KsVideoMediaType));
327 result->range = g_malloc (media_type->range->FormatSize);
328 memcpy ((gpointer) result->range, media_type->range,
329 media_type->range->FormatSize);
331 result->format = g_malloc (media_type->format_size);
332 memcpy (result->format, media_type->format, media_type->format_size);
334 result->translated_caps = gst_caps_ref (media_type->translated_caps);
340 ks_video_media_type_free (KsVideoMediaType * media_type)
342 if (media_type == NULL)
345 g_free ((gpointer) media_type->range);
347 g_free (media_type->format);
349 if (media_type->translated_caps != NULL)
350 gst_caps_unref (media_type->translated_caps);
356 ks_video_media_type_list_remove_duplicates (GList * media_types)
358 GList *master, *duplicates;
363 master = duplicates = NULL;
365 /* Find the first set of duplicates and their master */
366 for (entry = media_types; entry != NULL && duplicates == NULL;
367 entry = entry->next) {
368 KsVideoMediaType *mt = entry->data;
371 for (other_entry = media_types; other_entry != NULL;
372 other_entry = other_entry->next) {
373 KsVideoMediaType *other_mt = other_entry->data;
378 if (gst_caps_is_equal (mt->translated_caps, other_mt->translated_caps))
379 duplicates = g_list_prepend (duplicates, other_mt);
382 if (duplicates != NULL)
386 if (duplicates != NULL) {
387 KsVideoMediaType *selected_mt = master->data;
390 * Pick a FORMAT_VideoInfo2 if present, if not we just stay with the
393 for (entry = duplicates; entry != NULL; entry = entry->next) {
394 KsVideoMediaType *mt = entry->data;
396 if (IsEqualGUID (&mt->range->Specifier, &FORMAT_VideoInfo2)) {
397 ks_video_media_type_free (selected_mt);
400 ks_video_media_type_free (mt);
403 /* Remove the dupe from the main list */
404 media_types = g_list_remove (media_types, mt);
407 /* Update master node with the selected MediaType */
408 master->data = selected_mt;
410 g_list_free (duplicates);
413 while (master != NULL);
419 ks_video_probe_filter_for_caps (HANDLE filter_handle)
425 if (!ks_filter_get_pin_property (filter_handle, 0, KSPROPSETID_Pin,
426 KSPROPERTY_PIN_CTYPES, &pin_count, sizeof (pin_count), NULL))
429 GST_DEBUG ("pin_count = %lu", pin_count);
431 for (pin_id = 0; pin_id < pin_count; pin_id++) {
432 KSPIN_COMMUNICATION pin_comm;
433 KSPIN_DATAFLOW pin_flow;
436 if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
437 KSPROPERTY_PIN_COMMUNICATION, &pin_comm, sizeof (pin_comm), NULL))
440 if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
441 KSPROPERTY_PIN_DATAFLOW, &pin_flow, sizeof (pin_flow), NULL))
444 if (!ks_filter_get_pin_property (filter_handle, pin_id, KSPROPSETID_Pin,
445 KSPROPERTY_PIN_CATEGORY, &pin_cat, sizeof (pin_cat), NULL))
448 GST_DEBUG ("pin[%u]: pin_comm=%d, pin_flow=%d", pin_id, pin_comm, pin_flow);
450 if (pin_flow == KSPIN_DATAFLOW_OUT &&
451 memcmp (&pin_cat, &PINNAME_CAPTURE, sizeof (GUID)) == 0) {
452 KSMULTIPLE_ITEM *items;
454 if (ks_filter_get_pin_property_multi (filter_handle, pin_id,
455 KSPROPSETID_Pin, KSPROPERTY_PIN_DATARANGES, &items, NULL)) {
456 KSDATARANGE *range = (KSDATARANGE *) (items + 1);
459 for (i = 0; i < items->Count; i++) {
460 if (IsEqualGUID (&range->MajorFormat, &MEDIATYPE_Video)
461 || IsEqualGUID (&range->MajorFormat, &MEDIATYPE_Interleaved)) {
462 KsVideoMediaType *entry;
463 gpointer src_vscc, src_format;
464 GstStructure *media_structure;
466 entry = g_new0 (KsVideoMediaType, 1);
467 entry->pin_id = pin_id;
469 entry->range = g_malloc (range->FormatSize);
470 memcpy ((gpointer) entry->range, range, range->FormatSize);
472 if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo)) {
473 KS_DATARANGE_VIDEO *vr = (KS_DATARANGE_VIDEO *) entry->range;
475 src_vscc = &vr->ConfigCaps;
476 src_format = &vr->VideoInfoHeader;
478 entry->format_size = sizeof (vr->VideoInfoHeader);
479 entry->sample_size = vr->VideoInfoHeader.bmiHeader.biSizeImage;
480 } else if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo2)) {
481 KS_DATARANGE_VIDEO2 *vr = (KS_DATARANGE_VIDEO2 *) entry->range;
483 src_vscc = &vr->ConfigCaps;
484 src_format = &vr->VideoInfoHeader;
486 entry->format_size = sizeof (vr->VideoInfoHeader);
487 entry->sample_size = vr->VideoInfoHeader.bmiHeader.biSizeImage;
488 } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEGVideo)) {
489 /* Untested and probably wrong... */
490 KS_DATARANGE_MPEG1_VIDEO *vr =
491 (KS_DATARANGE_MPEG1_VIDEO *) entry->range;
493 src_vscc = &vr->ConfigCaps;
494 src_format = &vr->VideoInfoHeader;
496 entry->format_size = sizeof (vr->VideoInfoHeader);
498 vr->VideoInfoHeader.hdr.bmiHeader.biSizeImage;
499 } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEG2Video)) {
500 KS_DATARANGE_MPEG2_VIDEO *vr =
501 (KS_DATARANGE_MPEG2_VIDEO *) entry->range;
502 src_vscc = &vr->ConfigCaps;
503 src_format = &vr->VideoInfoHeader;
505 entry->format_size = sizeof (vr->VideoInfoHeader);
507 vr->VideoInfoHeader.hdr.bmiHeader.biSizeImage;
508 } else if (IsEqualGUID (&range->Specifier, &FORMAT_DvInfo)) {
509 KS_DATARANGE_DVVIDEO *vr = (KS_DATARANGE_DVVIDEO *) entry->range;
512 src_format = &vr->DVVideoInfo;
514 entry->format_size = sizeof (vr->DVVideoInfo);
515 entry->sample_size = vr->DataRange.SampleSize;
519 guid_str = ks_guid_to_string (&range->Specifier);
520 GST_DEBUG ("pin[%u]: ignoring unknown specifier GUID %s",
524 ks_video_media_type_free (entry);
529 g_assert (entry->sample_size != 0);
531 if (src_vscc != NULL) {
532 memcpy ((gpointer) & entry->vscc, src_vscc,
533 sizeof (entry->vscc));
536 entry->format = g_malloc (entry->format_size);
537 memcpy (entry->format, src_format, entry->format_size);
540 ks_video_format_to_structure (range->SubFormat,
541 range->Specifier, &entry->is_rgb);
543 if (media_structure == NULL) {
544 g_warning ("ks_video_format_to_structure returned NULL");
545 ks_video_media_type_free (entry);
547 } else if (src_vscc == NULL) {
548 entry->translated_caps = gst_caps_new_empty ();
549 gst_caps_append_structure (entry->translated_caps,
551 } else if (ks_video_append_video_stream_cfg_fields
552 (media_structure, &entry->vscc)) {
553 entry->translated_caps = gst_caps_new_empty ();
554 gst_caps_append_structure (entry->translated_caps,
557 gst_structure_free (media_structure);
558 ks_video_media_type_free (entry);
563 ret = g_list_prepend (ret, entry);
567 /* REVISIT: Each KSDATARANGE should start on a 64-bit boundary */
568 range = (KSDATARANGE *) (((guchar *) range) + range->FormatSize);
577 ret = g_list_reverse (ret);
578 ret = ks_video_media_type_list_remove_duplicates (ret);
586 ks_video_create_pin_conn_from_media_type (KsVideoMediaType * media_type)
588 KSPIN_CONNECT *conn = NULL;
589 KSDATAFORMAT *format = NULL;
592 conn = g_malloc0 (sizeof (KSPIN_CONNECT) + sizeof (KSDATAFORMAT) +
593 media_type->format_size);
595 conn->Interface.Set = KSINTERFACESETID_Standard;
596 conn->Interface.Id = KSINTERFACE_STANDARD_STREAMING;
597 conn->Interface.Flags = 0;
599 conn->Medium.Set = KSMEDIUMSETID_Standard;
600 conn->Medium.Id = KSMEDIUM_TYPE_ANYINSTANCE;
601 conn->Medium.Flags = 0;
603 conn->PinId = media_type->pin_id;
604 conn->PinToHandle = NULL;
605 conn->Priority.PriorityClass = KSPRIORITY_NORMAL;
606 conn->Priority.PrioritySubClass = KSPRIORITY_NORMAL;
608 format = (KSDATAFORMAT *) (conn + 1);
609 memcpy (format, media_type->range, sizeof (KSDATAFORMAT));
610 format->FormatSize = sizeof (KSDATAFORMAT) + media_type->format_size;
612 vih = (guint8 *) (format + 1);
613 memcpy (vih, media_type->format, media_type->format_size);
619 ks_video_fixate_media_type (const KSDATARANGE * range,
620 guint8 * format, gint width, gint height, gint fps_n, gint fps_d)
622 KS_DATARANGE_VIDEO *vr;
623 KS_VIDEOINFOHEADER *vih;
624 KS_BITMAPINFOHEADER *bih;
627 g_return_val_if_fail (format != NULL, FALSE);
629 if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo)) {
630 bih = &((KS_VIDEOINFOHEADER *) format)->bmiHeader;
631 } else if (IsEqualGUID (&range->Specifier, &FORMAT_VideoInfo2)) {
632 bih = &((KS_VIDEOINFOHEADER2 *) format)->bmiHeader;
633 } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEGVideo)) {
634 bih = &((KS_MPEG1VIDEOINFO *) format)->hdr.bmiHeader;
635 } else if (IsEqualGUID (&range->Specifier, &FORMAT_MPEG2Video)) {
636 bih = &((KS_MPEGVIDEOINFO2 *) format)->hdr.bmiHeader;
641 /* These formats' structures share the most basic stuff */
642 vr = (KS_DATARANGE_VIDEO *) range;
643 vih = (KS_VIDEOINFOHEADER *) format;
645 /* FIXME: Need to figure out how to properly handle ranges */
646 if (bih->biWidth != width || bih->biHeight != height)
649 /* Framerate, clamped because of fraction conversion rounding errors */
650 vih->AvgTimePerFrame =
651 gst_util_uint64_scale_int_round (NANOSECONDS, fps_d, fps_n);
652 vih->AvgTimePerFrame =
653 MAX (vih->AvgTimePerFrame, vr->ConfigCaps.MinFrameInterval);
654 vih->AvgTimePerFrame =
655 MIN (vih->AvgTimePerFrame, vr->ConfigCaps.MaxFrameInterval);
657 /* Bitrate, clamped for the same reason as framerate */
658 dwRate = (width * height * fps_n) / fps_d;
659 vih->dwBitRate = dwRate * bih->biBitCount;
660 vih->dwBitRate = MAX (vih->dwBitRate, vr->ConfigCaps.MinBitsPerSecond);
661 vih->dwBitRate = MIN (vih->dwBitRate, vr->ConfigCaps.MaxBitsPerSecond);
666 static GstStructure *
667 ks_video_append_var_video_fields (GstStructure * structure)
670 gst_structure_set (structure,
671 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
672 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
673 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
680 ks_video_get_all_caps (void)
682 static GstCaps *caps = NULL;
685 GstStructure *structure;
686 caps = gst_caps_new_empty ();
688 /* from Windows SDK 6.0 uuids.h */
691 ks_video_append_var_video_fields (ks_video_format_to_structure
692 (MEDIASUBTYPE_RGB555, FORMAT_VideoInfo, NULL));
693 gst_caps_append_structure (caps, structure);
696 ks_video_append_var_video_fields (ks_video_format_to_structure
697 (MEDIASUBTYPE_RGB565, FORMAT_VideoInfo, NULL));
698 gst_caps_append_structure (caps, structure);
701 ks_video_append_var_video_fields (ks_video_format_to_structure
702 (MEDIASUBTYPE_RGB24, FORMAT_VideoInfo, NULL));
703 gst_caps_append_structure (caps, structure);
706 ks_video_append_var_video_fields (ks_video_format_to_structure
707 (MEDIASUBTYPE_RGB32, FORMAT_VideoInfo, NULL));
708 gst_caps_append_structure (caps, structure);
712 ks_video_append_var_video_fields (gst_structure_new_empty
714 gst_caps_append_structure (caps, structure);
718 ks_video_append_var_video_fields (ks_video_format_to_structure
719 (MEDIASUBTYPE_MJPG, FORMAT_VideoInfo, NULL));
720 gst_caps_append_structure (caps, structure);
723 ks_video_append_var_video_fields (ks_video_format_to_structure
724 (MEDIASUBTYPE_dvsd, FORMAT_VideoInfo, NULL));
725 gst_caps_append_structure (caps, structure);
727 structure = /* no variable video fields (width, height, framerate) */
728 ks_video_format_to_structure (MEDIASUBTYPE_dvsd, FORMAT_DvInfo, NULL);
729 gst_caps_append_structure (caps, structure);