summaryrefslogtreecommitdiff
path: root/ext/closedcaption/misc.h
blob: 687b454c0a4c81defd56b6b3e6d53b1201c8819a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 *  libzvbi -- Miscellaneous cows and chickens
 *
 *  Copyright (C) 2000-2003 Iñaki García Etxebarria
 *  Copyright (C) 2002-2007 Michael H. Schimek
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301  USA.
 */

/* $Id: misc.h,v 1.24 2013-07-02 02:32:31 mschimek Exp $ */

#ifndef MISC_H
#define MISC_H

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>		/* (u)intXX_t */
#include <sys/types.h>		/* (s)size_t */
#include <float.h>		/* DBL_MAX */
#include <limits.h>		/* (S)SIZE_MAX */
#include <assert.h>
#include <glib.h>
#include <gst/gst.h>

#include "macros.h"

#define N_ELEMENTS(array) (sizeof (array) / sizeof (*(array)))

#ifdef __GNUC__

#if __GNUC__ < 3
/* Expect expression usually true/false, schedule accordingly. */
#  define likely(expr) (expr)
#  define unlikely(expr) (expr)
#else
#  define likely(expr) __builtin_expect(expr, 1)
#  define unlikely(expr) __builtin_expect(expr, 0)
#endif

#undef __i386__
#undef __i686__
/* FIXME #cpu is deprecated
#if #cpu (i386)
#  define __i386__ 1
#endif
#if #cpu (i686)
#  define __i686__ 1
#endif
*/

/* &x == PARENT (&x.tm_min, struct tm, tm_min),
   safer than &x == (struct tm *) &x.tm_min. A NULL _ptr is safe and
   will return NULL, not -offsetof(_member). */
#undef PARENT
#define PARENT(_ptr, _type, _member) ({					\
	__typeof__ (&((_type *) 0)->_member) _p = (_ptr);		\
	(_p != 0) ? (_type *)(((char *) _p) - offsetof (_type,		\
	  _member)) : (_type *) 0;					\
})

/* Like PARENT(), to be used with const _ptr. */
#define CONST_PARENT(_ptr, _type, _member) ({				\
	__typeof__ (&((const _type *) 0)->_member) _p = (_ptr);		\
	(_p != 0) ? (const _type *)(((const char *) _p) - offsetof	\
	 (const _type, _member)) : (const _type *) 0;			\
})

/* Note the following macros have no side effects only when you
   compile with GCC, so don't expect this. */

/* Absolute value of int, long or long long without a branch.
   Note ABS (INT_MIN) -> INT_MAX + 1. */
#undef ABS
#define ABS(n) ({							\
	register __typeof__ (n) _n = (n), _t = _n;			\
	if (-1 == (-1 >> 1)) { /* do we have signed shifts? */		\
		_t >>= sizeof (_t) * 8 - 1;				\
		_n ^= _t;						\
		_n -= _t;						\
	} else if (_n < 0) { /* also warns if n is unsigned type */	\
		_n = -_n;						\
	}								\
	/* return */ _n;						\
})

#undef MIN
#define MIN(x, y) ({							\
	__typeof__ (x) _x = (x);					\
	__typeof__ (y) _y = (y);					\
	(void)(&_x == &_y); /* warn if types do not match */		\
	/* return */ (_x < _y) ? _x : _y;				\
})

#undef MAX
#define MAX(x, y) ({							\
	__typeof__ (x) _x = (x);					\
	__typeof__ (y) _y = (y);					\
	(void)(&_x == &_y); /* warn if types do not match */		\
	/* return */ (_x > _y) ? _x : _y;				\
})

/* Note other compilers may swap only int, long or pointer. */
#undef SWAP
#define SWAP(x, y)							\
do {									\
	__typeof__ (x) _x = x;						\
	x = y;								\
	y = _x;								\
} while (0)

#undef SATURATE
#ifdef __i686__ /* has conditional move */
#define SATURATE(n, min, max) ({					\
	__typeof__ (n) _n = (n);					\
	__typeof__ (n) _min = (min);					\
	__typeof__ (n) _max = (max);					\
	(void)(&_n == &_min); /* warn if types do not match */		\
	(void)(&_n == &_max);						\
	if (_n < _min)							\
		_n = _min;						\
	if (_n > _max)							\
		_n = _max;						\
	/* return */ _n;						\
})
#else
#define SATURATE(n, min, max) ({					\
	__typeof__ (n) _n = (n);					\
	__typeof__ (n) _min = (min);					\
	__typeof__ (n) _max = (max);					\
	(void)(&_n == &_min); /* warn if types do not match */		\
	(void)(&_n == &_max);						\
	if (_n < _min)							\
		_n = _min;						\
	else if (_n > _max)						\
		_n = _max;						\
	/* return */ _n;						\
})
#endif

#else /* !__GNUC__ */

#define likely(expr) (expr)
#define unlikely(expr) (expr)
#undef __i386__
#undef __i686__

static char *
PARENT_HELPER (char *p, unsigned int offset)
{ return (0 == p) ? ((char *) 0) : p - offset; }

static const char *
CONST_PARENT_HELPER (const char *p, unsigned int offset)
{ return (0 == p) ? ((char *) 0) : p - offset; }

#define PARENT(_ptr, _type, _member)					\
	((0 == offsetof (_type, _member)) ? (_type *)(_ptr)		\
	 : (_type *) PARENT_HELPER ((char *)(_ptr), offsetof (_type, _member)))
#define CONST_PARENT(_ptr, _type, _member)				\
	((0 == offsetof (const _type, _member)) ? (const _type *)(_ptr)	\
	 : (const _type *) CONST_PARENT_HELPER ((const char *)(_ptr),	\
	  offsetof (const _type, _member)))

#undef ABS
#define ABS(n) (((n) < 0) ? -(n) : (n))

#undef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))

#undef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

#undef SWAP
#define SWAP(x, y)							\
do {									\
	long _x = x;							\
	x = y;								\
	y = _x;								\
} while (0)

#undef SATURATE
#define SATURATE(n, min, max) MIN (MAX (min, n), max)

#endif /* !__GNUC__ */

/* 32 bit constant byte reverse, e.g. 0xAABBCCDD -> 0xDDCCBBAA. */
#define SWAB32(m)							\
	(+ (((m) & 0xFF000000) >> 24)					\
	 + (((m) & 0xFF0000) >> 8)					\
	 + (((m) & 0xFF00) << 8)					\
	 + (((m) & 0xFF) << 24))

#ifdef HAVE_BUILTIN_POPCOUNT
#  define popcnt(x) __builtin_popcount ((uint32_t)(x))
#else
#  define popcnt(x) _vbi_popcnt (x)
#endif

extern unsigned int
_vbi_popcnt			(uint32_t		x);

/* NB GCC inlines and optimizes these functions when size is const. */
#define SET(var) memset (&(var), ~0, sizeof (var))

#define CLEAR(var) memset (&(var), 0, sizeof (var))

/* Useful to copy arrays, otherwise use assignment. */
#define COPY(d, s)							\
	(assert (sizeof (d) == sizeof (s)), memcpy (d, s, sizeof (d)))

/* Copy string const into char array. */
#define STRACPY(array, s)						\
do {									\
	/* Complain if s is no string const or won't fit. */		\
	const char t_[sizeof (array) - 1] _vbi_unused = s;		\
									\
	memcpy (array, s, sizeof (s));					\
} while (0)

/* Copy bits through mask. */
#define COPY_SET_MASK(dest, from, mask)					\
	(dest ^= (from) ^ (dest & (mask)))

/* Set bits if cond is TRUE, clear if FALSE. */
#define COPY_SET_COND(dest, bits, cond)					\
	 ((cond) ? (dest |= (bits)) : (dest &= ~(bits)))

/* Set and clear bits. */
#define COPY_SET_CLEAR(dest, set, clear)				\
	(dest = (dest & ~(clear)) | (set))

/* For applications, debugging and fault injection during unit tests. */

#define vbi_malloc malloc
#define vbi_realloc realloc
#define vbi_strdup strdup
#define vbi_free free

#define vbi_cache_malloc vbi_malloc
#define vbi_cache_free vbi_free

/* Helper functions. */

_vbi_inline int
_vbi_to_ascii			(int			c)
{
	if (c < 0)
		return '?';

	c &= 0x7F;

	if (c < 0x20 || c >= 0x7F)
		return '.';

	return c;
}

typedef struct {
	const char *		key;
	int			value;
} _vbi_key_value_pair;

extern vbi_bool
_vbi_keyword_lookup		(int *			value,
				 const char **		inout_s,
				 const _vbi_key_value_pair * table,
				 unsigned int		n_pairs)
  _vbi_nonnull ((1, 2, 3));

extern void
_vbi_shrink_vector_capacity	(void **		vector,
				 size_t *		capacity,
				 size_t			min_capacity,
				 size_t			element_size)
  _vbi_nonnull ((1, 2));
extern vbi_bool
_vbi_grow_vector_capacity	(void **		vector,
				 size_t *		capacity,
				 size_t			min_capacity,
				 size_t			element_size)
  _vbi_nonnull ((1, 2));

GST_DEBUG_CATEGORY_EXTERN (libzvbi_debug);

#ifndef GST_DISABLE_GST_DEBUG
/* Logging stuff. */
#ifdef G_HAVE_ISO_VARARGS
#define VBI_CAT_LEVEL_LOG(level,object,...) G_STMT_START{		\
  if (G_UNLIKELY ((level) <= GST_LEVEL_MAX && (level) <= _gst_debug_min)) {						\
	  gst_debug_log (libzvbi_debug, (level), __FILE__, GST_FUNCTION, __LINE__, \
        (GObject *) (object), __VA_ARGS__);				\
  }									\
}G_STMT_END
#else /* G_HAVE_GNUC_VARARGS */
#ifdef G_HAVE_GNUC_VARARGS
#define VBI_CAT_LEVEL_LOG(level,object,args...) G_STMT_START{	\
  if (G_UNLIKELY ((level) <= GST_LEVEL_MAX && (level) <= _gst_debug_min)) {						\
	  gst_debug_log (libzvbi_debug, (level), __FILE__, GST_FUNCTION, __LINE__, \
        (GObject *) (object), ##args );					\
  }									\
}G_STMT_END
#else /* no variadic macros, use inline */
static inline void
VBI_CAT_LEVEL_LOG_valist (GstDebugCategory * cat,
    GstDebugLevel level, gpointer object, const char *format, va_list varargs)
{
  if (G_UNLIKELY ((level) <= GST_LEVEL_MAX && (level) <= _gst_debug_min)) {
    gst_debug_log_valist (cat, level, "", "", 0, (GObject *) object, format,
        varargs);
  }
}

static inline void
VBI_CAT_LEVEL_LOG (GstDebugLevel level,
    gpointer object, const char *format, ...)
{
  va_list varargs;

  va_start (varargs, format);
  GST_CAT_LEVEL_LOG_valist (libzvbi_debug, level, object, format, varargs);
  va_end (varargs);
}
#endif
#endif /* G_HAVE_ISO_VARARGS */
#else
static inline void
VBI_CAT_LEVEL_LOG (GstDebugLevel level,
    gpointer object, const char *format, ...)
{
}
#endif	/* GST_DISABLE_GST_DEBUG */

#ifdef G_HAVE_GNUC_VARARGS
#define error(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_ERROR, NULL, templ , ##args)
#define warning(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_WARNING, NULL, templ , ##args)
#define notice(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_INFO, NULL, templ , ##args)
#define info(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_INFO, NULL, templ , ##args)
#define debug1(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_DEBUG, NULL, templ , ##args)
#define debug2(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_LOG, NULL, templ , ##args)
#define debug3(hook, templ, args...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_TRACE, NULL, templ , ##args)
#elif defined(G_HAVE_ISO_VARARGS)
#define error(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_ERROR, NULL, templ, __VA_ARGS__)
#define warning(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_WARNING, NULL, templ, __VA_ARGS__)
#define notice(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_INFO, NULL, templ, __VA_ARGS__)
#define info(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_INFO, NULL, templ, __VA_ARGS__)
#define debug1(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_DEBUG, NULL, templ, __VA_ARGS__)
#define debug2(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_LOG, NULL, templ, __VA_ARGS__)
#define debug3(hook, templ, ...)					\
	VBI_CAT_LEVEL_LOG (GST_LEVEL_TRACE, NULL, templ, __VA_ARGS__)
#else
/* if someone needs this, they can implement the inline functions for it */
#error "variadic macros are required"
#endif


#if 0				/* Replaced logging with GStreamer logging system */
extern _vbi_log_hook		_vbi_global_log;

extern void
_vbi_log_vprintf		(vbi_log_fn *		log_fn,
				 void *			user_data,
				 vbi_log_mask		level,
				 const char *		source_file,
				 const char *		context,
				 const char *		templ,
				 va_list		ap)
  _vbi_nonnull ((1, 4, 5, 6));
extern void
_vbi_log_printf		(vbi_log_fn *		log_fn,
				 void *			user_data,
				 vbi_log_mask		level,
				 const char *		source_file,
				 const char *		context,
				 const char *		templ,
				 ...)
  _vbi_nonnull ((1, 4, 5, 6)) _vbi_format ((printf, 6, 7));

#define _vbi_log(hook, level, templ, args...)				\
do {									\
	_vbi_log_hook *_h = hook;					\
									\
	if ((NULL != _h && 0 != (_h->mask & level))			\
	    || (_h = &_vbi_global_log, 0 != (_h->mask & level)))	\
		_vbi_log_printf (_h->fn, _h->user_data,		\
				  level, __FILE__, __FUNCTION__,	\
				  templ , ##args);			\
} while (0)

#define _vbi_vlog(hook, level, templ, ap)				\
do {									\
	_vbi_log_hook *_h = hook;					\
									\
	if ((NULL != _h && 0 != (_h->mask & level))			\
	    || (_h = &_vbi_global_log, 0 != (_h->mask & level)))	\
		_vbi_log_vprintf (_h->fn, _h->user_data,		\
				  level, __FILE__, __FUNCTION__,	\
				  templ, ap);				\
} while (0)
#define error(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_ERROR, templ , ##args)
#define warning(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_ERROR, templ , ##args)
#define notice(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_NOTICE, templ , ##args)
#define info(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_INFO, templ , ##args)
#define debug1(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_DEBUG, templ , ##args)
#define debug2(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_DEBUG2, templ , ##args)
#define debug3(hook, templ, args...)					\
	_vbi_log (hook, VBI_LOG_DEBUG3, templ , ##args)
#endif

/* Portability stuff. */

/* These should be defined in inttypes.h. */
#ifndef PRId64
#  define PRId64 "lld"
#endif
#ifndef PRIu64
#  define PRIu64 "llu"
#endif
#ifndef PRIx64
#  define PRIx64 "llx"
#endif

/* Should be defined in C99 limits.h? */
#ifndef SIZE_MAX
#  define SIZE_MAX ((size_t) -1)
#endif

#ifndef TIME_MIN
#  define TIME_MIN (_vbi_time_min ())
_vbi_inline time_t
_vbi_time_min			(void)
{
	const time_t t = (time_t) -1.25;

	if (t < -1) {
		return (time_t)((sizeof (time_t) > 4) ? DBL_MIN : FLT_MIN);
	} else if (t < 0) {
		return ((uint64_t) 1) << (sizeof (time_t) * 8 - 1);
	} else {
		return 0;
	}
}
#endif

#ifndef TIME_MAX
#  define TIME_MAX (_vbi_time_max ())
_vbi_inline time_t
_vbi_time_max			(void)
{
	const time_t t = (time_t) -1.25;

	if (t < -1) {
		return (time_t)((sizeof (time_t) > 4) ? DBL_MAX : FLT_MAX);
	} else if (t < 0) {
		/* Most likely signed 32 or 64 bit. */
		return (((uint64_t) 1) << (sizeof (time_t) * 8 - 1)) - 1;
	} else {
		return -1;
	}
}
#endif

/* __va_copy is a GNU extension. */
#ifndef __va_copy
#  define __va_copy(ap1, ap2) do { ap1 = ap2; } while (0)
#endif

#if 0
/* Use this instead of strncpy(). strlcpy() is a BSD extension. */
#ifndef HAVE_STRLCPY
#  define strlcpy _vbi_strlcpy
#endif
#undef strncpy
#define strncpy use_strlcpy_instead

extern size_t
_vbi_strlcpy			(char *			dst,
				 const char *		src,
				 size_t			size)
  _vbi_nonnull ((1, 2));
#endif

/* /\* strndup() is a BSD/GNU extension. *\/ */
/* #ifndef HAVE_STRNDUP */
/* #  define strndup _vbi_strndup */
/* #endif */

/* extern char * */
/* _vbi_strndup			(const char *		s, */
/* 				 size_t			len) */
/*   _vbi_nonnull ((1)); */

/* vasprintf() is a GNU extension. */
#ifndef HAVE_VASPRINTF
#  define vasprintf _vbi_vasprintf
#endif

extern int
_vbi_vasprintf			(char **		dstp,
				 const char *		templ,
				 va_list		ap)
  _vbi_nonnull ((1, 2));

/* asprintf() is a GNU extension. */
#ifndef HAVE_ASPRINTF
#  define asprintf _vbi_asprintf
#endif

extern int
_vbi_asprintf			(char **		dstp,
				 const char *		templ,
				 ...)
  _vbi_nonnull ((1, 2)) _vbi_format ((printf, 2, 3));

#undef sprintf
#define sprintf use_snprintf_or_asprintf_instead

#endif /* MISC_H */

/*
Local variables:
c-set-style: K&R
c-basic-offset: 8
End:
*/