packetizer: h264: use poc to compute missing pic_struct
[vlc.git] / include / vlc_arrays.h
blobdcdfa7a556b5d3120dce845219317c0b90dd5e87
1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef VLC_ARRAYS_H_
26 #define VLC_ARRAYS_H_
28 /**
29 * \file
30 * This file defines functions, structures and macros for handling arrays in vlc
33 /* realloc() that never fails *if* downsizing */
34 static inline void *realloc_down( void *ptr, size_t size )
36 void *ret = realloc( ptr, size );
37 return ret ? ret : ptr;
40 #define TAB_INIT( count, tab ) \
41 do { \
42 (count) = 0; \
43 (tab) = NULL; \
44 } while(0)
46 #define TAB_CLEAN( count, tab ) \
47 do { \
48 free( tab ); \
49 (count)= 0; \
50 (tab)= NULL; \
51 } while(0)
53 #define TAB_APPEND_CAST( cast, count, tab, p ) \
54 do { \
55 if( (count) > 0 ) \
56 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
57 else \
58 (tab) = cast malloc( sizeof( *(tab) ) ); \
59 if( !(tab) ) abort(); \
60 (tab)[count] = (p); \
61 (count)++; \
62 } while(0)
64 #define TAB_APPEND( count, tab, p ) \
65 TAB_APPEND_CAST( , count, tab, p )
67 #define TAB_FIND( count, tab, p, idx ) \
68 do { \
69 for( (idx) = 0; (idx) < (count); (idx)++ ) \
70 if( (tab)[(idx)] == (p) ) \
71 break; \
72 if( (idx) >= (count) ) \
73 (idx) = -1; \
74 } while(0)
77 #define TAB_ERASE( count, tab, index ) \
78 do { \
79 if( (count) > 1 ) \
80 memmove( (tab) + (index), \
81 (tab) + (index) + 1, \
82 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
83 (count)--; \
84 if( (count) == 0 ) \
85 { \
86 free( tab ); \
87 (tab) = NULL; \
88 } \
89 } while(0)
91 #define TAB_REMOVE( count, tab, p ) \
92 do { \
93 int i_index; \
94 TAB_FIND( count, tab, p, i_index ); \
95 if( i_index >= 0 ) \
96 TAB_ERASE( count, tab, i_index ); \
97 } while(0)
99 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
100 if( (count) > 0 ) \
101 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
102 else \
103 (tab) = cast malloc( sizeof( *(tab) ) ); \
104 if( !(tab) ) abort(); \
105 if( (count) - (index) > 0 ) \
106 memmove( (tab) + (index) + 1, \
107 (tab) + (index), \
108 ((count) - (index)) * sizeof( *(tab) ) );\
109 (tab)[(index)] = (p); \
110 (count)++; \
111 } while(0)
113 #define TAB_INSERT( count, tab, p, index ) \
114 TAB_INSERT_CAST( , count, tab, p, index )
116 #define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem ) \
117 TAB_INSERT( i_oldsize, p_ar, elem, i_pos )
119 #define REMOVE_ELEM( p_ar, i_size, i_pos ) \
120 TAB_ERASE( i_size, p_ar, i_pos )
123 * Binary search in a sorted array. The key must be comparable by < and >
124 * \param entries array of entries
125 * \param count number of entries
126 * \param elem key to check within an entry (like .id, or ->i_id)
127 * \param zetype type of the key
128 * \param key value of the key
129 * \param answer index of answer within the array. -1 if not found
131 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
132 do { \
133 int low = 0, high = count - 1; \
134 answer = -1; \
135 while( low <= high ) {\
136 int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
137 zetype mid_val = entries[mid] elem;\
138 if( mid_val < key ) \
139 low = mid + 1; \
140 else if ( mid_val > key ) \
141 high = mid -1; \
142 else \
144 answer = mid; break; \
147 } while(0)
150 /************************************************************************
151 * Dynamic arrays with progressive allocation
152 ************************************************************************/
154 /* Internal functions */
155 #define _ARRAY_ALLOC(array, newsize) { \
156 (array).i_alloc = newsize; \
157 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
158 sizeof(*(array).p_elems) ); \
159 if( !(array).p_elems ) abort(); \
162 #define _ARRAY_GROW1(array) { \
163 if( (array).i_alloc < 10 ) \
164 _ARRAY_ALLOC(array, 10 ) \
165 else if( (array).i_alloc == (array).i_size ) \
166 _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) ) \
169 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
171 /* API */
172 #define DECL_ARRAY(type) struct { \
173 int i_alloc; \
174 int i_size; \
175 type *p_elems; \
178 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
180 #define ARRAY_INIT(array) \
181 do { \
182 (array).i_alloc = 0; \
183 (array).i_size = 0; \
184 (array).p_elems = NULL; \
185 } while(0)
187 #define ARRAY_RESET(array) \
188 do { \
189 (array).i_alloc = 0; \
190 (array).i_size = 0; \
191 free( (array).p_elems ); (array).p_elems = NULL; \
192 } while(0)
194 #define ARRAY_APPEND(array, elem) \
195 do { \
196 _ARRAY_GROW1(array); \
197 (array).p_elems[(array).i_size] = elem; \
198 (array).i_size++; \
199 } while(0)
201 #define ARRAY_INSERT(array,elem,pos) \
202 do { \
203 _ARRAY_GROW1(array); \
204 if( (array).i_size - pos ) { \
205 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
206 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
208 (array).p_elems[pos] = elem; \
209 (array).i_size++; \
210 } while(0)
212 #define _ARRAY_SHRINK(array) { \
213 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
214 _ARRAY_ALLOC(array, (array).i_size + 5); \
218 #define ARRAY_REMOVE(array,pos) \
219 do { \
220 if( (array).i_size - (pos) - 1 ) \
222 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
223 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
225 (array).i_size--; \
226 _ARRAY_SHRINK(array); \
227 } while(0)
229 #define ARRAY_VAL(array, pos) array.p_elems[pos]
231 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
232 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
234 #define FOREACH_ARRAY( item, array ) { \
235 int fe_idx; \
236 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
238 item = (array).p_elems[fe_idx];
240 #define FOREACH_END() } }
243 /************************************************************************
244 * Dynamic arrays with progressive allocation (Preferred API)
245 ************************************************************************/
246 typedef struct vlc_array_t
248 size_t i_count;
249 void ** pp_elems;
250 } vlc_array_t;
252 static inline void vlc_array_init( vlc_array_t * p_array )
254 p_array->i_count = 0;
255 p_array->pp_elems = NULL;
258 static inline void vlc_array_clear( vlc_array_t * p_array )
260 free( p_array->pp_elems );
261 vlc_array_init( p_array );
264 /* Read */
265 static inline size_t vlc_array_count( vlc_array_t * p_array )
267 return p_array->i_count;
270 #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
271 # define vlc_array_item_at_index(ar, idx) \
272 _Generic((ar), \
273 const vlc_array_t *: ((ar)->pp_elems[idx]), \
274 vlc_array_t *: ((ar)->pp_elems[idx]))
275 #elif defined (__cplusplus)
276 static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
278 return ar->pp_elems[idx];
281 static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
282 size_t idx )
284 return ar->pp_elems[idx];
286 #endif
288 static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
289 const void *elem )
291 for( size_t i = 0; i < ar->i_count; i++ )
293 if( ar->pp_elems[i] == elem )
294 return i;
296 return -1;
299 /* Write */
300 static inline void vlc_array_insert( vlc_array_t *ar, void *elem, int idx )
302 void **pp = (void **)realloc( ar->pp_elems,
303 sizeof( void * ) * (ar->i_count + 1) );
304 if( unlikely(pp == NULL) )
305 abort();
307 size_t tail = ar->i_count - idx;
308 if( tail > 0 )
309 memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
311 pp[idx] = elem;
312 ar->i_count++;
313 ar->pp_elems = pp;
316 static inline void vlc_array_append( vlc_array_t *ar, void *elem )
318 void **pp = (void **)realloc( ar->pp_elems,
319 sizeof( void * ) * (ar->i_count + 1) );
320 if( unlikely(pp == NULL) )
321 abort();
323 pp[ar->i_count++] = elem;
324 ar->pp_elems = pp;
327 static inline void vlc_array_remove( vlc_array_t *ar, size_t idx )
329 void **pp = ar->pp_elems;
330 size_t tail = ar->i_count - idx - 1;
332 if( tail > 0 )
333 memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
335 ar->i_count--;
337 if( ar->i_count > 0 )
339 pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
340 if( likely(pp != NULL) )
341 ar->pp_elems = pp;
343 else
345 free( pp );
346 ar->pp_elems = NULL;
351 /************************************************************************
352 * Dictionaries
353 ************************************************************************/
355 /* This function is not intended to be crypto-secure, we only want it to be
356 * fast and not suck too much. This one is pretty fast and did 0 collisions
357 * in wenglish's dictionary.
359 static inline uint64_t DictHash( const char *psz_string, int hashsize )
361 uint64_t i_hash = 0;
362 if( psz_string )
364 while( *psz_string )
366 i_hash += *psz_string++;
367 i_hash += i_hash << 10;
368 i_hash ^= i_hash >> 8;
371 return i_hash % hashsize;
374 typedef struct vlc_dictionary_entry_t
376 char * psz_key;
377 void * p_value;
378 struct vlc_dictionary_entry_t * p_next;
379 } vlc_dictionary_entry_t;
381 typedef struct vlc_dictionary_t
383 int i_size;
384 vlc_dictionary_entry_t ** p_entries;
385 } vlc_dictionary_t;
387 static void * const kVLCDictionaryNotFound = NULL;
389 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
391 p_dict->p_entries = NULL;
393 if( i_size > 0 )
395 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
396 if( !p_dict->p_entries )
397 i_size = 0;
399 p_dict->i_size = i_size;
402 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
403 void ( * pf_free )( void * p_data, void * p_obj ),
404 void * p_obj )
406 if( p_dict->p_entries )
408 for( int i = 0; i < p_dict->i_size; i++ )
410 vlc_dictionary_entry_t * p_current, * p_next;
411 p_current = p_dict->p_entries[i];
412 while( p_current )
414 p_next = p_current->p_next;
415 if( pf_free != NULL )
416 ( * pf_free )( p_current->p_value, p_obj );
417 free( p_current->psz_key );
418 free( p_current );
419 p_current = p_next;
422 free( p_dict->p_entries );
423 p_dict->p_entries = NULL;
425 p_dict->i_size = 0;
428 static inline int
429 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
431 if( !p_dict->p_entries )
432 return 0;
434 int i_pos = DictHash( psz_key, p_dict->i_size );
435 return p_dict->p_entries[i_pos] != NULL;
438 static inline void *
439 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
441 if( !p_dict->p_entries )
442 return kVLCDictionaryNotFound;
444 int i_pos = DictHash( psz_key, p_dict->i_size );
445 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
447 if( !p_entry )
448 return kVLCDictionaryNotFound;
450 /* Make sure we return the right item. (Hash collision) */
451 do {
452 if( !strcmp( psz_key, p_entry->psz_key ) )
453 return p_entry->p_value;
454 p_entry = p_entry->p_next;
455 } while( p_entry );
457 return kVLCDictionaryNotFound;
460 static inline int
461 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
463 vlc_dictionary_entry_t * p_entry;
464 int i, count = 0;
466 if( !p_dict->p_entries )
467 return 0;
469 for( i = 0; i < p_dict->i_size; i++ )
471 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
473 return count;
476 static inline char **
477 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
479 vlc_dictionary_entry_t * p_entry;
480 char ** ppsz_ret;
481 int i, count = vlc_dictionary_keys_count( p_dict );
483 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
484 if( unlikely(!ppsz_ret) )
485 return NULL;
487 count = 0;
488 for( i = 0; i < p_dict->i_size; i++ )
490 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
491 ppsz_ret[count++] = strdup( p_entry->psz_key );
493 ppsz_ret[count] = NULL;
494 return ppsz_ret;
497 static inline void
498 __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
499 void * p_value, bool rebuild )
501 if( !p_dict->p_entries )
502 vlc_dictionary_init( p_dict, 1 );
504 int i_pos = DictHash( psz_key, p_dict->i_size );
505 vlc_dictionary_entry_t * p_entry;
507 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
508 p_entry->psz_key = strdup( psz_key );
509 p_entry->p_value = p_value;
510 p_entry->p_next = p_dict->p_entries[i_pos];
511 p_dict->p_entries[i_pos] = p_entry;
512 if( rebuild )
514 /* Count how many items there was */
515 int count;
516 for( count = 1; p_entry->p_next; count++ )
517 p_entry = p_entry->p_next;
518 if( count > 3 ) /* XXX: this need tuning */
520 /* Here it starts to be not good, rebuild a bigger dictionary */
521 struct vlc_dictionary_t new_dict;
522 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
523 int i;
524 vlc_dictionary_init( &new_dict, i_new_size );
525 for( i = 0; i < p_dict->i_size; i++ )
527 p_entry = p_dict->p_entries[i];
528 while( p_entry )
530 __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
531 p_entry->p_value,
532 false /* To avoid multiple rebuild loop */);
533 p_entry = p_entry->p_next;
537 vlc_dictionary_clear( p_dict, NULL, NULL );
538 p_dict->i_size = new_dict.i_size;
539 p_dict->p_entries = new_dict.p_entries;
544 static inline void
545 vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
547 __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
550 static inline void
551 vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
552 void ( * pf_free )( void * p_data, void * p_obj ),
553 void * p_obj )
555 if( !p_dict->p_entries )
556 return;
558 int i_pos = DictHash( psz_key, p_dict->i_size );
559 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
560 vlc_dictionary_entry_t * p_prev;
562 if( !p_entry )
563 return; /* Not found, nothing to do */
565 /* Hash collision */
566 p_prev = NULL;
567 do {
568 if( !strcmp( psz_key, p_entry->psz_key ) )
570 if( pf_free != NULL )
571 ( * pf_free )( p_entry->p_value, p_obj );
572 if( !p_prev )
573 p_dict->p_entries[i_pos] = p_entry->p_next;
574 else
575 p_prev->p_next = p_entry->p_next;
576 free( p_entry->psz_key );
577 free( p_entry );
578 return;
580 p_prev = p_entry;
581 p_entry = p_entry->p_next;
582 } while( p_entry );
584 /* No key was found */
587 #ifdef __cplusplus
588 // C++ helpers
589 template <typename T>
590 void vlc_delete_all( T &container )
592 typename T::iterator it = container.begin();
593 while ( it != container.end() )
595 delete *it;
596 ++it;
598 container.clear();
601 #endif
603 #endif