diff options
-rw-r--r-- | libhb/common.c | 34 | ||||
-rw-r--r-- | libhb/common.h | 1 | ||||
-rw-r--r-- | libhb/stream.c | 11 |
3 files changed, 45 insertions, 1 deletions
diff --git a/libhb/common.c b/libhb/common.c index 90bd23cb6..9c06d783b 100644 --- a/libhb/common.c +++ b/libhb/common.c @@ -748,6 +748,40 @@ void hb_list_add( hb_list_t * l, void * p ) } /********************************************************************** + * hb_list_insert + ********************************************************************** + * Adds an item at the specifiec position in the list, making it bigger + * if necessary. + * Can safely be called with a NULL pointer to add, it will be ignored. + *********************************************************************/ +void hb_list_insert( hb_list_t * l, int pos, void * p ) +{ + if( !p ) + { + return; + } + + if( l->items_count == l->items_alloc ) + { + /* We need a bigger boat */ + l->items_alloc += HB_LIST_DEFAULT_SIZE; + l->items = realloc( l->items, + l->items_alloc * sizeof( void * ) ); + } + + if ( l->items_count != pos ) + { + /* Shift all items after it sizeof( void * ) bytes earlier */ + memmove( &l->items[pos+1], &l->items[pos], + ( l->items_count - pos ) * sizeof( void * ) ); + } + + + l->items[pos] = p; + (l->items_count)++; +} + +/********************************************************************** * hb_list_rem ********************************************************************** * Remove an item from the list. Bad things will happen if called diff --git a/libhb/common.h b/libhb/common.h index 4844bd689..295ef9241 100644 --- a/libhb/common.h +++ b/libhb/common.h @@ -86,6 +86,7 @@ typedef struct hb_lock_s hb_lock_t; hb_list_t * hb_list_init(); int hb_list_count( hb_list_t * ); void hb_list_add( hb_list_t *, void * ); +void hb_list_insert( hb_list_t * l, int pos, void * p ); void hb_list_rem( hb_list_t *, void * ); void * hb_list_item( hb_list_t *, int ); void hb_list_close( hb_list_t ** ); diff --git a/libhb/stream.c b/libhb/stream.c index fd213ecc3..77bdc3c8b 100644 --- a/libhb/stream.c +++ b/libhb/stream.c @@ -2033,7 +2033,16 @@ static void add_audio_to_title(hb_title_t *title, int id) } set_audio_description( audio, lang_for_code( 0 ) ); - hb_list_add( title->list_audio, audio ); + + // Sort by id when adding to the list + int i; + for ( i = 0; i < hb_list_count( title->list_audio ); i++ ) + { + hb_audio_t *tmp = hb_list_item( title->list_audio, i ); + if ( audio->id < tmp->id ) + break; + } + hb_list_insert( title->list_audio, i, audio ); } static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title) |