From: Andre Noll Date: Wed, 18 Aug 2021 21:15:07 +0000 (+0200) Subject: mp4: Simplify chunk_of_sample(). X-Git-Tag: v0.7.1~7^2~50 X-Git-Url: http://git.tue.mpg.de/?a=commitdiff_plain;h=623699153a7718e29d6571a9fea1b96244813ef9;p=paraslash.git mp4: Simplify chunk_of_sample(). This function was unnecessarily complex. The equivalent replacement code is much shorter and easier to read. Besides reducing the number of local variables, we drop the chunk_sample parameter and return this number via the return value of the function. --- diff --git a/mp4.c b/mp4.c index 1c20d89f..71858305 100644 --- a/mp4.c +++ b/mp4.c @@ -981,46 +981,24 @@ void mp4_close(struct mp4 *f) } static int32_t chunk_of_sample(const struct mp4 *f, int32_t track, - int32_t sample, int32_t *chunk_sample, int32_t *chunk) + int32_t sample, int32_t *chunk) { - int32_t total_entries = 0; - int32_t chunk2entry; - int32_t chunk1, chunk2, chunk1samples, range_samples, total = 0; - - *chunk_sample = 0; - *chunk = 1; - - total_entries = f->track[track]->stsc_entry_count; - - chunk1 = 1; - chunk1samples = 0; - chunk2entry = 0; - - do { - chunk2 = f->track[track]->stsc_first_chunk[chunk2entry]; - *chunk = chunk2 - chunk1; - range_samples = *chunk * chunk1samples; + const struct mp4_track *t = f->track[track]; + uint32_t *fc = t->stsc_first_chunk, *spc = t->stsc_samples_per_chunk; + int32_t chunk1, chunk1samples, n, total, i; - if (sample < total + range_samples) + for (i = 1, total = 0; i < t->stsc_entry_count; i++, total += n) { + n = (fc[i] - fc[i - 1]) * spc[i - 1]; /* number of samples */ + if (sample < total + n) break; - - chunk1samples = f->track[track]->stsc_samples_per_chunk[chunk2entry]; - chunk1 = chunk2; - - if (chunk2entry < total_entries) { - chunk2entry++; - total += range_samples; - } - } while (chunk2entry < total_entries); - - if (chunk1samples) + } + chunk1 = fc[i - 1]; + chunk1samples = spc[i - 1]; + if (chunk1samples != 0) *chunk = (sample - total) / chunk1samples + chunk1; else *chunk = 1; - - *chunk_sample = total + (*chunk - chunk1) * chunk1samples; - - return 0; + return total + (*chunk - chunk1) * chunk1samples; } /** @@ -1068,7 +1046,7 @@ int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample) if (sample >= t->stsz_sample_count || track >= f->total_tracks) return -ERRNO_TO_PARA_ERROR(EINVAL); - chunk_of_sample(f, track, sample, &chunk_sample, &chunk); + chunk_sample = chunk_of_sample(f, track, sample, &chunk); if (t->stsz_sample_size > 0) srs = (sample - chunk_sample) * t->stsz_sample_size; else {