1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.Utils;
26 
27 private import glib.MemorySlice;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Value;
31 private import gobject.ValueArray;
32 private import gstreamer.Plugin;
33 private import gstreamer.c.functions;
34 public  import gstreamer.c.types;
35 
36 
37 /** */
38 public struct Utils
39 {
40 
41 	/**
42 	 * Searches inside @array for @search_data by using the comparison function
43 	 * @search_func. @array must be sorted ascending.
44 	 *
45 	 * As @search_data is always passed as second argument to @search_func it's
46 	 * not required that @search_data has the same type as the array elements.
47 	 *
48 	 * The complexity of this search function is O(log (num_elements)).
49 	 *
50 	 * Params:
51 	 *     array = the sorted input array
52 	 *     numElements = number of elements in the array
53 	 *     elementSize = size of every element in bytes
54 	 *     searchFunc = function to compare two elements, @search_data will always be passed as second argument
55 	 *     mode = search mode that should be used
56 	 *     searchData = element that should be found
57 	 *     userData = data to pass to @search_func
58 	 *
59 	 * Returns: The address of the found
60 	 *     element or %NULL if nothing was found
61 	 */
62 	public static void* arrayBinarySearch(void* array, uint numElements, size_t elementSize, GCompareDataFunc searchFunc, GstSearchMode mode, void* searchData, void* userData)
63 	{
64 		return gst_util_array_binary_search(array, numElements, elementSize, searchFunc, mode, searchData, userData);
65 	}
66 
67 	/**
68 	 * Transforms a #gdouble to a fraction and simplifies
69 	 * the result.
70 	 *
71 	 * Params:
72 	 *     src = #gdouble to transform
73 	 *     destN = pointer to a #gint to hold the result numerator
74 	 *     destD = pointer to a #gint to hold the result denominator
75 	 */
76 	public static void doubleToFraction(double src, out int destN, out int destD)
77 	{
78 		gst_util_double_to_fraction(src, &destN, &destD);
79 	}
80 
81 	/**
82 	 * Dumps the memory block into a hex representation. Useful for debugging.
83 	 *
84 	 * Params:
85 	 *     mem = a pointer to the memory to dump
86 	 */
87 	public static void dumpMem(char[] mem)
88 	{
89 		gst_util_dump_mem(mem.ptr, cast(uint)mem.length);
90 	}
91 
92 	/**
93 	 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
94 	 * the result in @res_n and @res_d.
95 	 *
96 	 * Params:
97 	 *     aN = Numerator of first value
98 	 *     aD = Denominator of first value
99 	 *     bN = Numerator of second value
100 	 *     bD = Denominator of second value
101 	 *     resN = Pointer to #gint to hold the result numerator
102 	 *     resD = Pointer to #gint to hold the result denominator
103 	 *
104 	 * Returns: %FALSE on overflow, %TRUE otherwise.
105 	 */
106 	public static bool fractionAdd(int aN, int aD, int bN, int bD, out int resN, out int resD)
107 	{
108 		return gst_util_fraction_add(aN, aD, bN, bD, &resN, &resD) != 0;
109 	}
110 
111 	/**
112 	 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
113 	 * -1 if a < b, 0 if a = b and 1 if a > b.
114 	 *
115 	 * Params:
116 	 *     aN = Numerator of first value
117 	 *     aD = Denominator of first value
118 	 *     bN = Numerator of second value
119 	 *     bD = Denominator of second value
120 	 *
121 	 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
122 	 */
123 	public static int fractionCompare(int aN, int aD, int bN, int bD)
124 	{
125 		return gst_util_fraction_compare(aN, aD, bN, bD);
126 	}
127 
128 	/**
129 	 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
130 	 * the result in @res_n and @res_d.
131 	 *
132 	 * Params:
133 	 *     aN = Numerator of first value
134 	 *     aD = Denominator of first value
135 	 *     bN = Numerator of second value
136 	 *     bD = Denominator of second value
137 	 *     resN = Pointer to #gint to hold the result numerator
138 	 *     resD = Pointer to #gint to hold the result denominator
139 	 *
140 	 * Returns: %FALSE on overflow, %TRUE otherwise.
141 	 */
142 	public static bool fractionMultiply(int aN, int aD, int bN, int bD, out int resN, out int resD)
143 	{
144 		return gst_util_fraction_multiply(aN, aD, bN, bD, &resN, &resD) != 0;
145 	}
146 
147 	/**
148 	 * Transforms a fraction to a #gdouble.
149 	 *
150 	 * Params:
151 	 *     srcN = Fraction numerator as #gint
152 	 *     srcD = Fraction denominator #gint
153 	 *     dest = pointer to a #gdouble for the result
154 	 */
155 	public static void fractionToDouble(int srcN, int srcD, out double dest)
156 	{
157 		gst_util_fraction_to_double(srcN, srcD, &dest);
158 	}
159 
160 	/**
161 	 *
162 	 * Params:
163 	 *     value = The #gdouble value to convert guint64 double
164 	 * Returns: @value casted to #guint64
165 	 */
166 	public static ulong gdoubleToGuint64(double value)
167 	{
168 		return gst_util_gdouble_to_guint64(value);
169 	}
170 
171 	/**
172 	 * Get a timestamp as GstClockTime to be used for interval measurements.
173 	 * The timestamp should not be interpreted in any other way.
174 	 *
175 	 * Returns: the timestamp
176 	 */
177 	public static GstClockTime getTimestamp()
178 	{
179 		return gst_util_get_timestamp();
180 	}
181 
182 	/**
183 	 * Calculates the greatest common divisor of @a
184 	 * and @b.
185 	 *
186 	 * Params:
187 	 *     a = First value as #gint
188 	 *     b = Second value as #gint
189 	 *
190 	 * Returns: Greatest common divisor of @a and @b
191 	 */
192 	public static int greatestCommonDivisor(int a, int b)
193 	{
194 		return gst_util_greatest_common_divisor(a, b);
195 	}
196 
197 	/**
198 	 * Calculates the greatest common divisor of @a
199 	 * and @b.
200 	 *
201 	 * Params:
202 	 *     a = First value as #gint64
203 	 *     b = Second value as #gint64
204 	 *
205 	 * Returns: Greatest common divisor of @a and @b
206 	 */
207 	public static long greatestCommonDivisorInt64(long a, long b)
208 	{
209 		return gst_util_greatest_common_divisor_int64(a, b);
210 	}
211 
212 	/**
213 	 * Return a constantly incrementing group id.
214 	 *
215 	 * This function is used to generate a new group-id for the
216 	 * stream-start event.
217 	 *
218 	 * This function never returns %GST_GROUP_ID_INVALID (which is 0)
219 	 *
220 	 * Returns: A constantly incrementing unsigned integer, which might
221 	 *     overflow back to 0 at some point.
222 	 */
223 	public static uint groupIdNext()
224 	{
225 		return gst_util_group_id_next();
226 	}
227 
228 	/**
229 	 *
230 	 * Params:
231 	 *     value = The #guint64 value to convert to double
232 	 * Returns: @value casted to #gdouble
233 	 */
234 	public static double guint64ToGdouble(ulong value)
235 	{
236 		return gst_util_guint64_to_gdouble(value);
237 	}
238 
239 	/**
240 	 * Compare two sequence numbers, handling wraparound.
241 	 *
242 	 * The current implementation just returns (gint32)(@s1 - @s2).
243 	 *
244 	 * Params:
245 	 *     s1 = A sequence number.
246 	 *     s2 = Another sequence number.
247 	 *
248 	 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
249 	 *     positive number if @s1 is after @s2.
250 	 */
251 	public static int seqnumCompare(uint s1, uint s2)
252 	{
253 		return gst_util_seqnum_compare(s1, s2);
254 	}
255 
256 	/**
257 	 * Return a constantly incrementing sequence number.
258 	 *
259 	 * This function is used internally to GStreamer to be able to determine which
260 	 * events and messages are "the same". For example, elements may set the seqnum
261 	 * on a segment-done message to be the same as that of the last seek event, to
262 	 * indicate that event and the message correspond to the same segment.
263 	 *
264 	 * This function never returns %GST_SEQNUM_INVALID (which is 0).
265 	 *
266 	 * Returns: A constantly incrementing 32-bit unsigned integer, which might
267 	 *     overflow at some point. Use gst_util_seqnum_compare() to make sure
268 	 *     you handle wraparound correctly.
269 	 */
270 	public static uint seqnumNext()
271 	{
272 		return gst_util_seqnum_next();
273 	}
274 
275 	/**
276 	 * Converts the string value to the type of the objects argument and
277 	 * sets the argument with it.
278 	 *
279 	 * Note that this function silently returns if @object has no property named
280 	 * @name or when @value cannot be converted to the type of the property.
281 	 *
282 	 * Params:
283 	 *     object = the object to set the argument of
284 	 *     name = the name of the argument to set
285 	 *     value = the string value to set
286 	 */
287 	public static void setObjectArg(ObjectG object, string name, string value)
288 	{
289 		gst_util_set_object_arg((object is null) ? null : object.getObjectGStruct(), Str.toStringz(name), Str.toStringz(value));
290 	}
291 
292 	/**
293 	 * Converts the string to the type of the value and
294 	 * sets the value with it.
295 	 *
296 	 * Note that this function is dangerous as it does not return any indication
297 	 * if the conversion worked or not.
298 	 *
299 	 * Params:
300 	 *     value = the value to set
301 	 *     valueStr = the string to get the value from
302 	 */
303 	public static void setValueFromString(out Value value, string valueStr)
304 	{
305 		GValue* outvalue = sliceNew!GValue();
306 
307 		gst_util_set_value_from_string(outvalue, Str.toStringz(valueStr));
308 
309 		value = ObjectG.getDObject!(Value)(outvalue, true);
310 	}
311 
312 	/**
313 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
314 	 * underflows and without loss of precision.
315 	 *
316 	 * This function can potentially be very slow if val and num are both
317 	 * greater than G_MAXUINT32.
318 	 *
319 	 * Params:
320 	 *     val = the number to scale
321 	 *     num = the numerator of the scale ratio
322 	 *     denom = the denominator of the scale ratio
323 	 *
324 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
325 	 *     function returns G_MAXUINT64.  If the result is not exactly
326 	 *     representable as an integer it is truncated.  See also
327 	 *     gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil(),
328 	 *     gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
329 	 *     gst_util_uint64_scale_int_ceil().
330 	 */
331 	public static ulong uint64Scale(ulong val, ulong num, ulong denom)
332 	{
333 		return gst_util_uint64_scale(val, num, denom);
334 	}
335 
336 	/**
337 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
338 	 * underflows and without loss of precision.
339 	 *
340 	 * This function can potentially be very slow if val and num are both
341 	 * greater than G_MAXUINT32.
342 	 *
343 	 * Params:
344 	 *     val = the number to scale
345 	 *     num = the numerator of the scale ratio
346 	 *     denom = the denominator of the scale ratio
347 	 *
348 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
349 	 *     function returns G_MAXUINT64.  If the result is not exactly
350 	 *     representable as an integer, it is rounded up.  See also
351 	 *     gst_util_uint64_scale(), gst_util_uint64_scale_round(),
352 	 *     gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
353 	 *     gst_util_uint64_scale_int_ceil().
354 	 */
355 	public static ulong uint64ScaleCeil(ulong val, ulong num, ulong denom)
356 	{
357 		return gst_util_uint64_scale_ceil(val, num, denom);
358 	}
359 
360 	/**
361 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
362 	 * underflows and without loss of precision.  @num must be non-negative and
363 	 * @denom must be positive.
364 	 *
365 	 * Params:
366 	 *     val = guint64 (such as a #GstClockTime) to scale.
367 	 *     num = numerator of the scale factor.
368 	 *     denom = denominator of the scale factor.
369 	 *
370 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
371 	 *     function returns G_MAXUINT64.  If the result is not exactly
372 	 *     representable as an integer, it is truncated.  See also
373 	 *     gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil(),
374 	 *     gst_util_uint64_scale(), gst_util_uint64_scale_round(),
375 	 *     gst_util_uint64_scale_ceil().
376 	 */
377 	public static ulong uint64ScaleInt(ulong val, int num, int denom)
378 	{
379 		return gst_util_uint64_scale_int(val, num, denom);
380 	}
381 
382 	/**
383 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
384 	 * underflows and without loss of precision.  @num must be non-negative and
385 	 * @denom must be positive.
386 	 *
387 	 * Params:
388 	 *     val = guint64 (such as a #GstClockTime) to scale.
389 	 *     num = numerator of the scale factor.
390 	 *     denom = denominator of the scale factor.
391 	 *
392 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
393 	 *     function returns G_MAXUINT64.  If the result is not exactly
394 	 *     representable as an integer, it is rounded up.  See also
395 	 *     gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
396 	 *     gst_util_uint64_scale(), gst_util_uint64_scale_round(),
397 	 *     gst_util_uint64_scale_ceil().
398 	 */
399 	public static ulong uint64ScaleIntCeil(ulong val, int num, int denom)
400 	{
401 		return gst_util_uint64_scale_int_ceil(val, num, denom);
402 	}
403 
404 	/**
405 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
406 	 * underflows and without loss of precision.  @num must be non-negative and
407 	 * @denom must be positive.
408 	 *
409 	 * Params:
410 	 *     val = guint64 (such as a #GstClockTime) to scale.
411 	 *     num = numerator of the scale factor.
412 	 *     denom = denominator of the scale factor.
413 	 *
414 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
415 	 *     function returns G_MAXUINT64.  If the result is not exactly
416 	 *     representable as an integer, it is rounded to the nearest integer
417 	 *     (half-way cases are rounded up).  See also gst_util_uint64_scale_int(),
418 	 *     gst_util_uint64_scale_int_ceil(), gst_util_uint64_scale(),
419 	 *     gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil().
420 	 */
421 	public static ulong uint64ScaleIntRound(ulong val, int num, int denom)
422 	{
423 		return gst_util_uint64_scale_int_round(val, num, denom);
424 	}
425 
426 	/**
427 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
428 	 * underflows and without loss of precision.
429 	 *
430 	 * This function can potentially be very slow if val and num are both
431 	 * greater than G_MAXUINT32.
432 	 *
433 	 * Params:
434 	 *     val = the number to scale
435 	 *     num = the numerator of the scale ratio
436 	 *     denom = the denominator of the scale ratio
437 	 *
438 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
439 	 *     function returns G_MAXUINT64.  If the result is not exactly
440 	 *     representable as an integer, it is rounded to the nearest integer
441 	 *     (half-way cases are rounded up).  See also gst_util_uint64_scale(),
442 	 *     gst_util_uint64_scale_ceil(), gst_util_uint64_scale_int(),
443 	 *     gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil().
444 	 */
445 	public static ulong uint64ScaleRound(ulong val, ulong num, ulong denom)
446 	{
447 		return gst_util_uint64_scale_round(val, num, denom);
448 	}
449 
450 	/**
451 	 * Calculates the linear regression of the values @xy and places the
452 	 * result in @m_num, @m_denom, @b and @xbase, representing the function
453 	 * y(x) = m_num/m_denom * (x - xbase) + b
454 	 * that has the least-square distance from all points @x and @y.
455 	 *
456 	 * @r_squared will contain the remaining error.
457 	 *
458 	 * If @temp is not %NULL, it will be used as temporary space for the function,
459 	 * in which case the function works without any allocation at all. If @temp is
460 	 * %NULL, an allocation will take place. @temp should have at least the same
461 	 * amount of memory allocated as @xy, i.e. 2*n*sizeof(GstClockTime).
462 	 *
463 	 * > This function assumes (x,y) values with reasonable large differences
464 	 * > between them. It will not calculate the exact results if the differences
465 	 * > between neighbouring values are too small due to not being able to
466 	 * > represent sub-integer values during the calculations.
467 	 *
468 	 * Params:
469 	 *     xy = Pairs of (x,y) values
470 	 *     temp = Temporary scratch space used by the function
471 	 *     n = number of (x,y) pairs
472 	 *     mNum = numerator of calculated slope
473 	 *     mDenom = denominator of calculated slope
474 	 *     b = Offset at Y-axis
475 	 *     xbase = Offset at X-axis
476 	 *     rSquared = R-squared
477 	 *
478 	 * Returns: %TRUE if the linear regression was successfully calculated
479 	 *
480 	 * Since: 1.12
481 	 */
482 	public static bool calculateLinearRegression(GstClockTime* xy, GstClockTime* temp, uint n, out GstClockTime mNum, out GstClockTime mDenom, out GstClockTime b, out GstClockTime xbase, out double rSquared)
483 	{
484 		return gst_calculate_linear_regression(xy, temp, n, &mNum, &mDenom, &b, &xbase, &rSquared) != 0;
485 	}
486 
487 	/**
488 	 * Registers a new #GstDynamicTypeFactory in the registry
489 	 *
490 	 * Params:
491 	 *     plugin = The #GstPlugin to register @dyn_type for
492 	 *     type = The #GType to register dynamically
493 	 *
494 	 * Since: 1.12
495 	 */
496 	public static bool dynamicTypeRegister(Plugin plugin, GType type)
497 	{
498 		return gst_dynamic_type_register((plugin is null) ? null : plugin.getPluginStruct(), type) != 0;
499 	}
500 
501 	/**
502 	 * Get a property of type %GST_TYPE_ARRAY and transform it into a
503 	 * #GValueArray. This allow language bindings to get GST_TYPE_ARRAY
504 	 * properties which are otherwise not an accessible type.
505 	 *
506 	 * Params:
507 	 *     object = the object to set the array to
508 	 *     name = the name of the property to set
509 	 *     array = a return #GValueArray
510 	 *
511 	 * Since: 1.12
512 	 */
513 	public static bool getObjectArray(ObjectG object, string name, out ValueArray array)
514 	{
515 		GValueArray* outarray = null;
516 
517 		auto __p = gst_util_get_object_array((object is null) ? null : object.getObjectGStruct(), Str.toStringz(name), &outarray) != 0;
518 
519 		array = ObjectG.getDObject!(ValueArray)(outarray);
520 
521 		return __p;
522 	}
523 
524 	/**
525 	 * Transfer a #GValueArray to %GST_TYPE_ARRAY and set this value on the
526 	 * specified property name. This allow language bindings to set GST_TYPE_ARRAY
527 	 * properties which are otherwise not an accessible type.
528 	 *
529 	 * Params:
530 	 *     object = the object to set the array to
531 	 *     name = the name of the property to set
532 	 *     array = a #GValueArray containing the values
533 	 *
534 	 * Since: 1.12
535 	 */
536 	public static bool setObjectArray(ObjectG object, string name, ValueArray array)
537 	{
538 		return gst_util_set_object_array((object is null) ? null : object.getObjectGStruct(), Str.toStringz(name), (array is null) ? null : array.getValueArrayStruct()) != 0;
539 	}
540 }