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.Str;
28 private import gobject.ObjectG;
29 private import gobject.Value;
30 private import gobject.ValueArray;
31 private import gstreamer.Plugin;
32 private import gstreamer.c.functions;
33 public  import gstreamer.c.types;
34 public  import gstreamerc.gstreamertypes;
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 	 *     size = the size of the memory block to dump
87 	 */
88 	public static void dumpMem(char* mem, uint size)
89 	{
90 		gst_util_dump_mem(mem, size);
91 	}
92 
93 	/**
94 	 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
95 	 * the result in @res_n and @res_d.
96 	 *
97 	 * Params:
98 	 *     aN = Numerator of first value
99 	 *     aD = Denominator of first value
100 	 *     bN = Numerator of second value
101 	 *     bD = Denominator of second value
102 	 *     resN = Pointer to #gint to hold the result numerator
103 	 *     resD = Pointer to #gint to hold the result denominator
104 	 *
105 	 * Returns: %FALSE on overflow, %TRUE otherwise.
106 	 */
107 	public static bool fractionAdd(int aN, int aD, int bN, int bD, out int resN, out int resD)
108 	{
109 		return gst_util_fraction_add(aN, aD, bN, bD, &resN, &resD) != 0;
110 	}
111 
112 	/**
113 	 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
114 	 * -1 if a < b, 0 if a = b and 1 if a > b.
115 	 *
116 	 * Params:
117 	 *     aN = Numerator of first value
118 	 *     aD = Denominator of first value
119 	 *     bN = Numerator of second value
120 	 *     bD = Denominator of second value
121 	 *
122 	 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
123 	 */
124 	public static int fractionCompare(int aN, int aD, int bN, int bD)
125 	{
126 		return gst_util_fraction_compare(aN, aD, bN, bD);
127 	}
128 
129 	/**
130 	 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
131 	 * the result in @res_n and @res_d.
132 	 *
133 	 * Params:
134 	 *     aN = Numerator of first value
135 	 *     aD = Denominator of first value
136 	 *     bN = Numerator of second value
137 	 *     bD = Denominator of second value
138 	 *     resN = Pointer to #gint to hold the result numerator
139 	 *     resD = Pointer to #gint to hold the result denominator
140 	 *
141 	 * Returns: %FALSE on overflow, %TRUE otherwise.
142 	 */
143 	public static bool fractionMultiply(int aN, int aD, int bN, int bD, out int resN, out int resD)
144 	{
145 		return gst_util_fraction_multiply(aN, aD, bN, bD, &resN, &resD) != 0;
146 	}
147 
148 	/**
149 	 * Transforms a fraction to a #gdouble.
150 	 *
151 	 * Params:
152 	 *     srcN = Fraction numerator as #gint
153 	 *     srcD = Fraction denominator #gint
154 	 *     dest = pointer to a #gdouble for the result
155 	 */
156 	public static void fractionToDouble(int srcN, int srcD, out double dest)
157 	{
158 		gst_util_fraction_to_double(srcN, srcD, &dest);
159 	}
160 
161 	/** */
162 	public static ulong gdoubleToGuint64(double value)
163 	{
164 		return gst_util_gdouble_to_guint64(value);
165 	}
166 
167 	/**
168 	 * Get a timestamp as GstClockTime to be used for interval measurements.
169 	 * The timestamp should not be interpreted in any other way.
170 	 *
171 	 * Returns: the timestamp
172 	 */
173 	public static GstClockTime getTimestamp()
174 	{
175 		return gst_util_get_timestamp();
176 	}
177 
178 	/**
179 	 * Calculates the greatest common divisor of @a
180 	 * and @b.
181 	 *
182 	 * Params:
183 	 *     a = First value as #gint
184 	 *     b = Second value as #gint
185 	 *
186 	 * Returns: Greatest common divisor of @a and @b
187 	 */
188 	public static int greatestCommonDivisor(int a, int b)
189 	{
190 		return gst_util_greatest_common_divisor(a, b);
191 	}
192 
193 	/**
194 	 * Calculates the greatest common divisor of @a
195 	 * and @b.
196 	 *
197 	 * Params:
198 	 *     a = First value as #gint64
199 	 *     b = Second value as #gint64
200 	 *
201 	 * Returns: Greatest common divisor of @a and @b
202 	 */
203 	public static long greatestCommonDivisorInt64(long a, long b)
204 	{
205 		return gst_util_greatest_common_divisor_int64(a, b);
206 	}
207 
208 	/**
209 	 * Return a constantly incrementing group id.
210 	 *
211 	 * This function is used to generate a new group-id for the
212 	 * stream-start event.
213 	 *
214 	 * Returns: A constantly incrementing unsigned integer, which might
215 	 *     overflow back to 0 at some point.
216 	 */
217 	public static uint groupIdNext()
218 	{
219 		return gst_util_group_id_next();
220 	}
221 
222 	/** */
223 	public static double guint64ToGdouble(ulong value)
224 	{
225 		return gst_util_guint64_to_gdouble(value);
226 	}
227 
228 	/**
229 	 * Compare two sequence numbers, handling wraparound.
230 	 *
231 	 * The current implementation just returns (gint32)(@s1 - @s2).
232 	 *
233 	 * Params:
234 	 *     s1 = A sequence number.
235 	 *     s2 = Another sequence number.
236 	 *
237 	 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
238 	 *     positive number if @s1 is after @s2.
239 	 */
240 	public static int seqnumCompare(uint s1, uint s2)
241 	{
242 		return gst_util_seqnum_compare(s1, s2);
243 	}
244 
245 	/**
246 	 * Return a constantly incrementing sequence number.
247 	 *
248 	 * This function is used internally to GStreamer to be able to determine which
249 	 * events and messages are "the same". For example, elements may set the seqnum
250 	 * on a segment-done message to be the same as that of the last seek event, to
251 	 * indicate that event and the message correspond to the same segment.
252 	 *
253 	 * Returns: A constantly incrementing 32-bit unsigned integer, which might
254 	 *     overflow back to 0 at some point. Use gst_util_seqnum_compare() to make sure
255 	 *     you handle wraparound correctly.
256 	 */
257 	public static uint seqnumNext()
258 	{
259 		return gst_util_seqnum_next();
260 	}
261 
262 	/**
263 	 * Converts the string value to the type of the objects argument and
264 	 * sets the argument with it.
265 	 *
266 	 * Note that this function silently returns if @object has no property named
267 	 * @name or when @value cannot be converted to the type of the property.
268 	 *
269 	 * Params:
270 	 *     object = the object to set the argument of
271 	 *     name = the name of the argument to set
272 	 *     value = the string value to set
273 	 */
274 	public static void setObjectArg(ObjectG object, string name, string value)
275 	{
276 		gst_util_set_object_arg((object is null) ? null : object.getObjectGStruct(), Str.toStringz(name), Str.toStringz(value));
277 	}
278 
279 	/**
280 	 * Converts the string to the type of the value and
281 	 * sets the value with it.
282 	 *
283 	 * Note that this function is dangerous as it does not return any indication
284 	 * if the conversion worked or not.
285 	 *
286 	 * Params:
287 	 *     value = the value to set
288 	 *     valueStr = the string to get the value from
289 	 */
290 	public static void setValueFromString(out Value value, string valueStr)
291 	{
292 		GValue* outvalue = gMalloc!GValue();
293 
294 		gst_util_set_value_from_string(outvalue, Str.toStringz(valueStr));
295 
296 		value = ObjectG.getDObject!(Value)(outvalue, true);
297 	}
298 
299 	/**
300 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
301 	 * underflows and without loss of precision.
302 	 *
303 	 * This function can potentially be very slow if val and num are both
304 	 * greater than G_MAXUINT32.
305 	 *
306 	 * Params:
307 	 *     val = the number to scale
308 	 *     num = the numerator of the scale ratio
309 	 *     denom = the denominator of the scale ratio
310 	 *
311 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
312 	 *     function returns G_MAXUINT64.  If the result is not exactly
313 	 *     representable as an integer it is truncated.  See also
314 	 *     gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil(),
315 	 *     gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
316 	 *     gst_util_uint64_scale_int_ceil().
317 	 */
318 	public static ulong uint64Scale(ulong val, ulong num, ulong denom)
319 	{
320 		return gst_util_uint64_scale(val, num, denom);
321 	}
322 
323 	/**
324 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
325 	 * underflows and without loss of precision.
326 	 *
327 	 * This function can potentially be very slow if val and num are both
328 	 * greater than G_MAXUINT32.
329 	 *
330 	 * Params:
331 	 *     val = the number to scale
332 	 *     num = the numerator of the scale ratio
333 	 *     denom = the denominator of the scale ratio
334 	 *
335 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
336 	 *     function returns G_MAXUINT64.  If the result is not exactly
337 	 *     representable as an integer, it is rounded up.  See also
338 	 *     gst_util_uint64_scale(), gst_util_uint64_scale_round(),
339 	 *     gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
340 	 *     gst_util_uint64_scale_int_ceil().
341 	 */
342 	public static ulong uint64ScaleCeil(ulong val, ulong num, ulong denom)
343 	{
344 		return gst_util_uint64_scale_ceil(val, num, denom);
345 	}
346 
347 	/**
348 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
349 	 * underflows and without loss of precision.  @num must be non-negative and
350 	 * @denom must be positive.
351 	 *
352 	 * Params:
353 	 *     val = guint64 (such as a #GstClockTime) to scale.
354 	 *     num = numerator of the scale factor.
355 	 *     denom = denominator of the scale factor.
356 	 *
357 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
358 	 *     function returns G_MAXUINT64.  If the result is not exactly
359 	 *     representable as an integer, it is truncated.  See also
360 	 *     gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil(),
361 	 *     gst_util_uint64_scale(), gst_util_uint64_scale_round(),
362 	 *     gst_util_uint64_scale_ceil().
363 	 */
364 	public static ulong uint64ScaleInt(ulong val, int num, int denom)
365 	{
366 		return gst_util_uint64_scale_int(val, num, denom);
367 	}
368 
369 	/**
370 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
371 	 * underflows and without loss of precision.  @num must be non-negative and
372 	 * @denom must be positive.
373 	 *
374 	 * Params:
375 	 *     val = guint64 (such as a #GstClockTime) to scale.
376 	 *     num = numerator of the scale factor.
377 	 *     denom = denominator of the scale factor.
378 	 *
379 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
380 	 *     function returns G_MAXUINT64.  If the result is not exactly
381 	 *     representable as an integer, it is rounded up.  See also
382 	 *     gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
383 	 *     gst_util_uint64_scale(), gst_util_uint64_scale_round(),
384 	 *     gst_util_uint64_scale_ceil().
385 	 */
386 	public static ulong uint64ScaleIntCeil(ulong val, int num, int denom)
387 	{
388 		return gst_util_uint64_scale_int_ceil(val, num, denom);
389 	}
390 
391 	/**
392 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
393 	 * underflows and without loss of precision.  @num must be non-negative and
394 	 * @denom must be positive.
395 	 *
396 	 * Params:
397 	 *     val = guint64 (such as a #GstClockTime) to scale.
398 	 *     num = numerator of the scale factor.
399 	 *     denom = denominator of the scale factor.
400 	 *
401 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
402 	 *     function returns G_MAXUINT64.  If the result is not exactly
403 	 *     representable as an integer, it is rounded to the nearest integer
404 	 *     (half-way cases are rounded up).  See also gst_util_uint64_scale_int(),
405 	 *     gst_util_uint64_scale_int_ceil(), gst_util_uint64_scale(),
406 	 *     gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil().
407 	 */
408 	public static ulong uint64ScaleIntRound(ulong val, int num, int denom)
409 	{
410 		return gst_util_uint64_scale_int_round(val, num, denom);
411 	}
412 
413 	/**
414 	 * Scale @val by the rational number @num / @denom, avoiding overflows and
415 	 * underflows and without loss of precision.
416 	 *
417 	 * This function can potentially be very slow if val and num are both
418 	 * greater than G_MAXUINT32.
419 	 *
420 	 * Params:
421 	 *     val = the number to scale
422 	 *     num = the numerator of the scale ratio
423 	 *     denom = the denominator of the scale ratio
424 	 *
425 	 * Returns: @val * @num / @denom.  In the case of an overflow, this
426 	 *     function returns G_MAXUINT64.  If the result is not exactly
427 	 *     representable as an integer, it is rounded to the nearest integer
428 	 *     (half-way cases are rounded up).  See also gst_util_uint64_scale(),
429 	 *     gst_util_uint64_scale_ceil(), gst_util_uint64_scale_int(),
430 	 *     gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil().
431 	 */
432 	public static ulong uint64ScaleRound(ulong val, ulong num, ulong denom)
433 	{
434 		return gst_util_uint64_scale_round(val, num, denom);
435 	}
436 
437 	/**
438 	 * Calculates the linear regression of the values @xy and places the
439 	 * result in @m_num, @m_denom, @b and @xbase, representing the function
440 	 * y(x) = m_num/m_denom * (x - xbase) + b
441 	 * that has the least-square distance from all points @x and @y.
442 	 *
443 	 * @r_squared will contain the remaining error.
444 	 *
445 	 * If @temp is not %NULL, it will be used as temporary space for the function,
446 	 * in which case the function works without any allocation at all. If @temp is
447 	 * %NULL, an allocation will take place. @temp should have at least the same
448 	 * amount of memory allocated as @xy, i.e. 2*n*sizeof(GstClockTime).
449 	 *
450 	 * > This function assumes (x,y) values with reasonable large differences
451 	 * > between them. It will not calculate the exact results if the differences
452 	 * > between neighbouring values are too small due to not being able to
453 	 * > represent sub-integer values during the calculations.
454 	 *
455 	 * Params:
456 	 *     xy = Pairs of (x,y) values
457 	 *     temp = Temporary scratch space used by the function
458 	 *     n = number of (x,y) pairs
459 	 *     mNum = numerator of calculated slope
460 	 *     mDenom = denominator of calculated slope
461 	 *     b = Offset at Y-axis
462 	 *     xbase = Offset at X-axis
463 	 *     rSquared = R-squared
464 	 *
465 	 * Returns: %TRUE if the linear regression was successfully calculated
466 	 *
467 	 * Since: 1.12
468 	 */
469 	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)
470 	{
471 		return gst_calculate_linear_regression(xy, temp, n, &mNum, &mDenom, &b, &xbase, &rSquared) != 0;
472 	}
473 
474 	/** */
475 	public static bool dynamicTypeRegister(Plugin plugin, GType type)
476 	{
477 		return gst_dynamic_type_register((plugin is null) ? null : plugin.getPluginStruct(), type) != 0;
478 	}
479 
480 	/**
481 	 * Get a property of type %GST_TYPE_ARRAY and transform it into a
482 	 * #GValueArray. This allow language bindings to get GST_TYPE_ARRAY
483 	 * properties which are otherwise not an accessible type.
484 	 *
485 	 * Params:
486 	 *     object = the object to set the array to
487 	 *     name = the name of the property to set
488 	 *     array = a return #GValueArray
489 	 *
490 	 * Since: 1.12
491 	 */
492 	public static bool getObjectArray(ObjectG object, string name, out ValueArray array)
493 	{
494 		GValueArray* outarray = null;
495 
496 		auto p = gst_util_get_object_array((object is null) ? null : object.getObjectGStruct(), Str.toStringz(name), &outarray) != 0;
497 
498 		array = ObjectG.getDObject!(ValueArray)(outarray);
499 
500 		return p;
501 	}
502 
503 	/**
504 	 * Transfer a #GValueArray to %GST_TYPE_ARRAY and set this value on the
505 	 * specified property name. This allow language bindings to set GST_TYPE_ARRAY
506 	 * properties which are otherwise not an accessible type.
507 	 *
508 	 * Params:
509 	 *     object = the object to set the array to
510 	 *     name = the name of the property to set
511 	 *     array = a #GValueArray containing the values
512 	 *
513 	 * Since: 1.12
514 	 */
515 	public static bool setObjectArray(ObjectG object, string name, ValueArray array)
516 	{
517 		return gst_util_set_object_array((object is null) ? null : object.getObjectGStruct(), Str.toStringz(name), (array is null) ? null : array.getValueArrayStruct()) != 0;
518 	}
519 }