]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mp4.c
mp4: Merge sample_range_size() into mp4_set_sample_position().
[paraslash.git] / mp4.c
diff --git a/mp4.c b/mp4.c
index 477b4307244731a0702599c8e49cf2a89862efa3..9f11a66e52aca8b9e561542be7ba6d65b36d9d0b 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -8,6 +8,7 @@
 #include <regex.h>
 
 #include "para.h"
+#include "error.h"
 #include "portable_io.h"
 #include "string.h"
 #include "mp4.h"
@@ -61,11 +62,6 @@ struct mp4 {
        struct mp4_metadata meta;
 };
 
-int32_t mp4_total_tracks(const struct mp4 *f)
-{
-       return f->total_tracks;
-}
-
 /*
  * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or
  * read errors are treated as errors.
@@ -993,9 +989,6 @@ static int32_t chunk_of_sample(const struct mp4 *f, int32_t track,
 
        *chunk_sample = 0;
        *chunk = 1;
-       if (f->track[track] == NULL) {
-               return -1;
-       }
 
        total_entries = f->track[track]->stsc_entry_count;
 
@@ -1047,44 +1040,11 @@ static int32_t chunk_to_offset(const struct mp4 *f, int32_t track,
        return 0;
 }
 
-static int32_t sample_range_size(const struct mp4 *f, int32_t track,
-               int32_t chunk_sample, int32_t sample)
-{
-       int32_t i, total;
-       const struct mp4_track *p_track = f->track[track];
-
-       if (p_track->stsz_sample_size) {
-               return (sample - chunk_sample) * p_track->stsz_sample_size;
-       } else {
-               if (sample >= p_track->stsz_sample_count)
-                       return 0;       //error
-
-               for (i = chunk_sample, total = 0; i < sample; i++) {
-                       total += p_track->stsz_table[i];
-               }
-       }
-
-       return total;
-}
-
-static int32_t sample_to_offset(const struct mp4 *f, int32_t track,
-               int32_t sample)
-{
-       int32_t chunk, chunk_sample, chunk_offset1, chunk_offset2;
-
-       chunk_of_sample(f, track, sample, &chunk_sample, &chunk);
-
-       chunk_offset1 = chunk_to_offset(f, track, chunk);
-       chunk_offset2 = chunk_offset1 + sample_range_size(f,
-               track, chunk_sample, sample);
-       return chunk_offset2;
-}
-
 /**
  * Return the number of milliseconds of the given track.
  *
  * \param f As returned by \ref mp4_open_read(), must not be NULL.
- * \param track Between zero and the value returned by \ref mp4_total_tracks().
+ * \param track Between zero and the value returned by \ref mp4_get_total_tracks().
  *
  * The function returns zero if the audio file is of zero length or contains a
  * corrupt track header.
@@ -1098,6 +1058,11 @@ uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
        return t->duration * 1000 / t->timeScale;
 }
 
+int32_t mp4_get_total_tracks(const struct mp4 *f)
+{
+       return f->total_tracks;
+}
+
 /**
  * Check whether the given track number corresponds to an audio track.
  *
@@ -1112,10 +1077,24 @@ bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
        return f->track[track]->is_audio;
 }
 
-void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
+int mp4_set_sample_position(struct mp4 *f, uint32_t track, int32_t sample)
 {
-       int32_t offset = sample_to_offset(f, track, sample);
+       const struct mp4_track *t = f->track[track];
+       int32_t offset, chunk, chunk_sample;
+       uint32_t n, srs; /* sample range size */
+
+       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);
+       if (t->stsz_sample_size > 0)
+               srs = (sample - chunk_sample) * t->stsz_sample_size;
+       else {
+               for (srs = 0, n = chunk_sample; n < sample; n++)
+                       srs += t->stsz_table[n];
+       }
+       offset = chunk_to_offset(f, track, chunk) + srs;
        set_position(f, offset);
+       return 1;
 }
 
 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample)