}
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;
}
/**
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 {