]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
mp4: Introduce mp4_is_audio_track().
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 11 Aug 2021 17:12:07 +0000 (19:12 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:35 +0000 (21:37 +0200)
Currently the aac audio format handler iterates over the tracks
in an mp4 file. For each track it tries to get the audio-specific
configuration by calling mp4_get_decoder_config() and calls into faad
to check whether it is a valid configuration for the aac decoder.

We can simplify all this because the mp4 code already knows the type
of each track, albeit it does not expose this information yet. So
provide the new mp4_is_audio_track() helper and let the aac audio
format handler pick the first track for which this helper returns true.

As an additional benefit, we can remove the now unused
mp4_get_decoder_config().

aac_afh.c
mp4.c
mp4.h

index aed2153608fc56616be17a291230f6d8be4b5f4c..fdb0339c7715bf9e319c03bdf6990ecac1814313 100644 (file)
--- a/aac_afh.c
+++ b/aac_afh.c
@@ -51,24 +51,13 @@ static uint32_t aac_afh_seek_cb(void *user_data, uint64_t pos)
 
 static int32_t aac_afh_get_track(struct mp4 *mp4)
 {
-       int32_t i, rc, num_tracks = mp4_total_tracks(mp4);
+       int32_t i, num_tracks = mp4_total_tracks(mp4);
 
        assert(num_tracks >= 0);
-       for (i = 0; i < num_tracks; i++) {
-               unsigned char *buf = NULL;
-               unsigned buf_size = 0;
-
-               mp4_get_decoder_config(mp4, i, &buf, &buf_size);
-               if (buf) {
-                       mp4AudioSpecificConfig masc;
-                       rc = NeAACDecAudioSpecificConfig(buf, buf_size, &masc);
-                       free(buf);
-                       if (rc < 0)
-                               continue;
+       for (i = 0; i < num_tracks; i++)
+               if (mp4_is_audio_track(mp4, i))
                        return i;
-               }
-       }
-       return -1; /* no audio track */
+       return -E_MP4_TRACK; /* no audio track */
 }
 
 static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
@@ -87,10 +76,10 @@ static int aac_afh_open(const void *map, size_t mapsize, void **afh_context)
        c->mp4 = mp4_open_read(&c->cb);
        if (!c->mp4)
                goto free_ctx;
-       c->track = aac_afh_get_track(c->mp4);
-       ret = -E_MP4_TRACK;
-       if (c->track < 0)
+       ret = aac_afh_get_track(c->mp4);
+       if (ret < 0)
                goto close_mp4;
+       c->track = ret;
        *afh_context = c;
        return 0;
 close_mp4:
diff --git a/mp4.c b/mp4.c
index d9f86fc7414685c5448252f8e8048c2979935d88..be974b3c4e5c8186152f3e7a86b17ee1eaee52ed 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -1235,27 +1235,6 @@ static int32_t parse_atoms(struct mp4 *f, int meta_only)
        return 0;
 }
 
-void mp4_get_decoder_config(const struct mp4 *f, int track,
-               unsigned char **ppBuf, unsigned int *pBufSize)
-{
-       if (track >= f->total_tracks) {
-               *ppBuf = NULL;
-               *pBufSize = 0;
-               return;
-       }
-
-       if (f->track[track]->decoderConfig == NULL
-               || f->track[track]->decoderConfigLen == 0) {
-               *ppBuf = NULL;
-               *pBufSize = 0;
-       } else {
-               *ppBuf = para_malloc(f->track[track]->decoderConfigLen);
-               memcpy(*ppBuf, f->track[track]->decoderConfig,
-                       f->track[track]->decoderConfigLen);
-               *pBufSize = f->track[track]->decoderConfigLen;
-       }
-}
-
 struct mp4 *mp4_open_read(struct mp4_callback *f)
 {
        struct mp4 *ff = para_calloc(sizeof(struct mp4));
@@ -1425,6 +1404,20 @@ uint64_t mp4_get_duration(const struct mp4 *f, int32_t track)
        return t->duration * 1000 / t->timeScale;
 }
 
+/**
+ * Check whether the given track number corresponds to an audio track.
+ *
+ * \param f See \ref mp4_get_duration().
+ * \param track See \ref mp4_get_duration().
+ *
+ * Besides audio tracks, an mp4 file may contain video and system tracks. For
+ * those the function returns false.
+ */
+bool mp4_is_audio_track(const struct mp4 *f, int32_t track)
+{
+       return f->track[track]->type == TRACK_AUDIO;
+}
+
 void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample)
 {
        int32_t offset = sample_to_offset(f, track, sample);
diff --git a/mp4.h b/mp4.h
index 225c92c0fa7fdb21f2b8ad9df5be9783fa3e7457..a61ef97bfc4190e63e3bbed2f3fdb5c4b28e99c5 100644 (file)
--- a/mp4.h
+++ b/mp4.h
@@ -22,8 +22,7 @@ struct mp4; /* opaque */
 
 void mp4_set_sample_position(struct mp4 *f, int32_t track, int32_t sample);
 int32_t mp4_total_tracks(const struct mp4 *f);
-void mp4_get_decoder_config(const struct mp4 *f, int track,
-               unsigned char** ppBuf, unsigned int* pBufSize);
+bool mp4_is_audio_track(const struct mp4 *f, int32_t track);
 struct mp4 *mp4_open_read(struct mp4_callback *f);
 void mp4_close(struct mp4 *f);
 int32_t mp4_get_sample_size(const struct mp4 *f, int track, int sample);