X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=afh_common.c;h=0e9b13ca24cd0138a195c27633e37f16ca944ec2;hp=5be43550c202b516aaedf332dbbfba341da20dcb;hb=a6cae51e8c60fa9d92b206d0a51483f75b47735a;hpb=79d6515d49cdb0a91ff7c4a599f2d63cb5678032 diff --git a/afh_common.c b/afh_common.c index 5be43550..0e9b13ca 100644 --- a/afh_common.c +++ b/afh_common.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2013 Andre Noll + * Copyright (C) 1997-2014 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -230,11 +230,12 @@ success: } /** - * Deallocate contents of a filled-in ahi structure + * Deallocate the contents of an afh_info structure. * * \param afhi The structure to clear. * - * The given pointer is kept, everything else is freed. + * This only frees the memory the various pointer fields of \a afhi point to. + * It does *not* free \a afhi itself. */ void clear_afhi(struct afh_info *afhi) { @@ -264,6 +265,12 @@ const char *audio_format_name(int i) return afl[i].name; } +static inline size_t get_chunk_len(long unsigned chunk_num, + const struct afh_info *afhi) +{ + return afhi->chunk_table[chunk_num + 1] - afhi->chunk_table[chunk_num]; +} + /** * Get one chunk of audio data. * @@ -281,7 +288,28 @@ void afh_get_chunk(long unsigned chunk_num, struct afh_info *afhi, { size_t pos = afhi->chunk_table[chunk_num]; *buf = map + pos; - *len = afhi->chunk_table[chunk_num + 1] - pos; + *len = get_chunk_len(chunk_num, afhi); +} + +/** + * Find a suitable start chunk. + * + * \param approx_chunk_num Upper bound for the chunk number to return. + * \param afhi Needed for the chunk table. + * + * \return The first non-empty chunk <= \a approx_chunk_num. + * + * \sa \ref afh_get_chunk(). + */ +int32_t afh_get_start_chunk(int32_t approx_chunk_num, + const struct afh_info *afhi) +{ + int32_t k; + + for (k = PARA_MAX(0, approx_chunk_num); k >= 0; k--) + if (get_chunk_len(k, afhi) > 0) + break; + return k; } /**