Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/faac/frontend/mp4read.c
Line
Count
Source
1
/*
2
 * FAAD - Freeware Advanced Audio Decoder
3
 * Copyright (C) 2026 Nils Schimmelmann
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 */
15
16
/*
17
 * MP4 reader for the faad frontend: a thin adapter over the libfaam demuxer
18
 * that finds the first AAC track and exposes its AudioSpecificConfig, sample
19
 * locations, gapless trim and tags through either file or memory callbacks.
20
 */
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <stdint.h>
26
#include <stdbool.h>
27
28
#include "faam.h"
29
#include "mp4read.h"
30
#include "cli_io.h"
31
#include "charset.h"
32
#include "encoder_quirks.h"
33
#include "faac_endian.h"
34
35
typedef struct {
36
    const uint8_t *buf;
37
    uint64_t size;
38
    uint64_t pos;
39
} mem_stream;
40
41
static int32_t mem_read(void *user, void *dst, uint32_t bytes)
42
51.0k
{
43
51.0k
    mem_stream *m = (mem_stream *)user;
44
51.0k
    uint64_t avail = m->pos < m->size ? m->size - m->pos : 0;
45
51.0k
    uint32_t n = bytes < avail ? bytes : (uint32_t)avail;
46
51.0k
    memcpy(dst, m->buf + m->pos, n);
47
51.0k
    m->pos += n;
48
51.0k
    return (int32_t)n;
49
51.0k
}
50
51
static bool mem_seek(void *user, uint64_t offset)
52
68.3k
{
53
68.3k
    mem_stream *m = (mem_stream *)user;
54
68.3k
    if (offset > m->size) return false;
55
50.8k
    m->pos = offset;
56
50.8k
    return true;
57
68.3k
}
58
59
static uint64_t mem_tell(void *user)
60
0
{
61
0
    return ((mem_stream *)user)->pos;
62
0
}
63
64
static void add_tag(MP4Track *out, const char *name, const char *value)
65
659
{
66
659
    MP4Tag *t = (MP4Tag *)realloc(out->tags, ((size_t)out->num_tags + 1) * sizeof(*t));
67
659
    if (!t) return;
68
659
    out->tags = t;
69
659
    t += out->num_tags;
70
659
    t->name = strdup(name);
71
659
    t->value = strdup(value);
72
659
    if (!t->name || !t->value) { free(t->name); free(t->value); return; }
73
659
    out->num_tags++;
74
659
}
75
76
static void add_index(MP4Track *out, const char *label, uint16_t num, uint16_t total)
77
30
{
78
30
    char buf[32];
79
30
    if (!num && !total) return;
80
24
    snprintf(buf, sizeof(buf), "%u/%u", num, total);
81
24
    add_tag(out, label, buf);
82
24
}
83
84
/* Display names belong to the frontend; libfaam exposes typed atom values. */
85
static void add_tags(MP4Track *out, faam_demuxer *d, uint32_t count)
86
4.10k
{
87
4.10k
    static const struct { uint32_t key; const char *label; } labels[] = {
88
4.10k
        { FAAM_METADATA_KEY_ENCODER, "Encoder" }, { FAAM_METADATA_KEY_ARTIST, "Artist" },
89
4.10k
        { FAAM_METADATA_KEY_ARTIST_SORT, "Artist Sort" }, { FAAM_METADATA_KEY_COMPOSER, "Composer" },
90
4.10k
        { FAAM_METADATA_KEY_COMPOSER_SORT, "Composer Sort" }, { FAAM_METADATA_KEY_TITLE, "Title" },
91
4.10k
        { FAAM_METADATA_KEY_ALBUM, "Album" }, { FAAM_METADATA_KEY_ALBUM_ARTIST, "Album Artist" },
92
4.10k
        { FAAM_METADATA_KEY_ALBUM_ARTIST_SORT, "Album Artist Sort" },
93
4.10k
        { FAAM_METADATA_KEY_ALBUM_SORT, "Album Sort" }, { FAAM_METADATA_KEY_GENRE, "Genre" },
94
4.10k
        { FAAM_METADATA_KEY_YEAR, "Year" }, { FAAM_METADATA_KEY_COMMENT, "Comment" },
95
4.10k
        { FAAM_METADATA_KEY_TITLE_SORT, "Title Sort" }, { FAAM_METADATA_KEY_LYRICS, "Lyrics" },
96
4.10k
        { FAAM_METADATA_KEY_GROUPING, "Grouping" }, { FAAM_METADATA_KEY_COPYRIGHT, "Copyright" },
97
4.10k
        { FAAM_METADATA_KEY_DESCRIPTION, "Description" },
98
4.10k
        { FAAM_METADATA_KEY_LONG_DESCRIPTION, "Long Description" },
99
4.10k
        { FAAM_METADATA_KEY_WORK, "Work" }, { FAAM_METADATA_KEY_MOVEMENT, "Movement" },
100
4.10k
        { FAAM_METADATA_KEY_BPM, "BPM" }, { FAAM_METADATA_KEY_MOVEMENT_NUMBER, "Movement Number" },
101
4.10k
        { FAAM_METADATA_KEY_MOVEMENT_TOTAL, "Movement Total" },
102
4.10k
        { FAAM_METADATA_KEY_SHOW_WORK_MOVEMENT, "Show Work/Movement" }
103
4.10k
    };
104
5.15k
    for (uint32_t i = 0; i < count; i++) {
105
1.04k
        faam_metadata_entry e = { .struct_size = sizeof(e) };
106
1.04k
        if (faam_demuxer_get_metadata_entry(d, i, &e) != FAAM_OK) continue;
107
1.04k
        const uint8_t *v = e.data;
108
1.04k
        if (e.key == FAAM_METADATA_KEY_COVER_ART) { out->cover_bytes = e.data_bytes; continue; }
109
1.04k
        if (e.key == FAAM_METADATA_KEY_TRACK && e.data_bytes >= 6)
110
21
            add_index(out, "Track", read_u16_be(v + 2), read_u16_be(v + 4));
111
1.02k
        else if (e.key == FAAM_METADATA_KEY_DISC && e.data_bytes >= 6)
112
9
            add_index(out, "Disc", read_u16_be(v + 2), read_u16_be(v + 4));
113
1.01k
        else if (e.key == FAAM_METADATA_KEY_COMPILATION && e.data_bytes && v[0]) add_tag(out, "Compilation", "Yes");
114
1.01k
        else if (e.key == FAAM_METADATA_KEY_GENRE_CODE && e.data_bytes == 2 && read_u16_be(v)) {
115
0
            const char *name = id3_genre_name(read_u16_be(v) - 1u);
116
0
            char value[32];
117
0
            if (!name) {
118
0
                snprintf(value, sizeof(value), "#%u", read_u16_be(v) - 1u);
119
0
                name = value;
120
0
            }
121
0
            add_tag(out, "Genre", name);
122
1.01k
        } else if (e.data_type == FAAM_METADATA_UTF8 && !memchr(v, 0, e.data_bytes)) {
123
759
            const char *label = e.name;
124
759
            if (label && !strcmp(label, "iTunSMPB") && (!e.mean || !strcmp(e.mean, "com.apple.iTunes"))) continue;
125
5.71k
            for (size_t j = 0; !label && j < sizeof(labels) / sizeof(labels[0]); j++)
126
4.95k
                if (e.key == labels[j].key) label = labels[j].label;
127
754
            if (label) add_tag(out, label, e.data);
128
754
        }
129
1.04k
    }
130
4.10k
}
131
132
/* Collect every sample of the chosen track, or report an incomplete index. */
133
static void collect_samples(faam_demuxer *d, uint32_t track_id, uint64_t file_size, MP4Track *out)
134
4.10k
{
135
4.10k
    uint32_t cap = 0;
136
4.10k
    faam_frame_loc loc = { .struct_size = sizeof(loc) };
137
4.10k
    faam_status status;
138
4.10k
    out->sample_error = true;
139
840k
    while ((status = faam_demuxer_next_frame_loc(d, &loc)) == FAAM_OK) {
140
839k
        uint32_t skipped;
141
839k
        if (faam_demuxer_read_frame(d, NULL, 0, &skipped) != FAAM_OK) return;
142
839k
        if (loc.track_id != track_id) continue;
143
831k
        if (loc.file_offset > file_size || loc.frame_bytes > file_size - loc.file_offset) return;
144
829k
        if (out->num_samples == cap) {
145
3.42k
            if (cap > UINT32_MAX / 2) return;
146
3.42k
            uint32_t ncap = cap ? cap * 2 : 1024;
147
3.42k
            if ((uint64_t)ncap * sizeof(MP4Sample) > SIZE_MAX) return;
148
3.42k
            MP4Sample *p = (MP4Sample *)realloc(out->samples, (size_t)ncap * sizeof(*p));
149
3.42k
            if (!p) return;
150
3.42k
            out->samples = p;
151
3.42k
            cap = ncap;
152
3.42k
        }
153
829k
        out->samples[out->num_samples].offset = loc.file_offset;
154
829k
        out->samples[out->num_samples].size = loc.frame_bytes;
155
829k
        out->num_samples++;
156
829k
    }
157
1.62k
    out->sample_error = status != FAAM_END_OF_STREAM;
158
1.62k
}
159
160
static bool read_track_io(faam_io *io, const uint8_t *prefix, uint64_t file_size,
161
                          uint32_t want_track_id, MP4Track *track)
162
6.50k
{
163
6.50k
    memset(track, 0, sizeof(*track));
164
6.50k
    faam_demuxer *d = NULL;
165
6.50k
    if (faam_demuxer_open(NULL, io, &d) != FAAM_OK)
166
85
        return !memcmp(prefix + 4, "ftyp", 4) || !memcmp(prefix + 4, "moov", 4);
167
168
6.41k
    uint32_t num_tracks = 0, total_tracks = 0;
169
6.41k
    faam_demuxer_get_num_tracks(d, &num_tracks, &total_tracks);
170
6.41k
    track->num_tracks = total_tracks;
171
6.41k
    track->num_listed = num_tracks < MP4_MAX_TRACK_SUMMARIES ? num_tracks : MP4_MAX_TRACK_SUMMARIES;
172
173
6.41k
    uint32_t track_id = 0;
174
6.41k
    uint8_t asc[FAAM_CODEC_DATA_MAX];
175
12.6k
    for (uint32_t i = 0; i < num_tracks; i++) {
176
6.20k
        faam_track_info info = { 0 };
177
6.20k
        uint32_t asc_len = 0;
178
6.20k
        info.struct_size = sizeof(info);
179
6.20k
        if (faam_demuxer_get_track_info(d, i, &info) != FAAM_OK) continue;
180
6.20k
        bool usable = info.track_type == FAAM_TRACK_AUDIO && info.codec_id == FAAM_CODEC_AAC &&
181
4.43k
                      faam_demuxer_get_codec_data(d, info.track_id, asc, sizeof(asc), &asc_len) == FAAM_OK && asc_len;
182
6.20k
        if (i < MP4_MAX_TRACK_SUMMARIES) {
183
6.20k
            MP4TrackSummary *sum = &track->summaries[i];
184
6.20k
            sum->id = info.track_id;
185
6.20k
            sum->fourcc = info.fourcc;
186
6.20k
            sum->kind = info.track_type == FAAM_TRACK_AUDIO ? 'a' : info.track_type == FAAM_TRACK_VIDEO ? 'v' : 'o';
187
6.20k
            sum->decodable = usable;
188
6.20k
        }
189
6.20k
        if (track_id || !usable || (want_track_id && info.track_id != want_track_id)) continue;
190
4.10k
        track->asc_buf = (uint8_t *)malloc(asc_len);
191
4.10k
        if (!track->asc_buf) break;
192
4.10k
        memcpy(track->asc_buf, asc, asc_len);
193
4.10k
        track->asc_len = asc_len;
194
4.10k
        track->timescale = info.timescale;
195
4.10k
        track_id = info.track_id;
196
4.10k
        track->track_id = track_id;
197
4.10k
    }
198
199
6.41k
    if (track_id) {
200
4.10k
        char brand[5];
201
4.10k
        faam_gapless_info gapless = { .struct_size = sizeof(gapless) };
202
4.10k
        uint32_t meta_count = 0;
203
4.10k
        if (faam_demuxer_get_major_brand(d, brand) == FAAM_OK) {
204
4.10k
            size_t n = strlen(brand);
205
6.57k
            while (n && brand[n - 1] == ' ') brand[--n] = '\0';
206
4.10k
            memcpy(track->major_brand, brand, n + 1);
207
4.10k
        }
208
4.10k
        if (faam_demuxer_get_track_gapless(d, track_id, &gapless) == FAAM_OK) {
209
4.10k
            track->delay = gapless.encoder_delay;
210
4.10k
            track->padding = gapless.end_padding;
211
4.10k
        }
212
4.10k
        if (faam_demuxer_get_num_metadata(d, &meta_count) == FAAM_OK) add_tags(track, d, meta_count);
213
        /* Same resolution as faam info. */
214
4.10k
        track->delay = encoder_quirk_resolve_delay(d, track_id, track->delay, NULL);
215
4.10k
        track->delay_includes_decoder = encoder_quirk_priming_includes_decoder_delay(d, track_id);
216
4.10k
        collect_samples(d, track_id, file_size, track);
217
4.10k
    }
218
219
6.41k
    faam_demuxer_close(&d);
220
    /* A valid MP4 without AAC audio is still an MP4: the caller reports that.
221
     * Without any track the input is not a container at all (e.g. ADTS), so
222
     * hand it back to be treated as a raw stream. */
223
6.41k
    return total_tracks > 0;
224
6.50k
}
225
226
bool mp4_read_track_buf(const uint8_t *buf, uint64_t file_size, uint32_t want_track_id, MP4Track *track)
227
6.51k
{
228
6.51k
    memset(track, 0, sizeof(*track));
229
6.51k
    if (!buf || file_size < 32) return false;
230
6.50k
    mem_stream ms = { buf, file_size, 0 };
231
6.50k
    faam_io io = { .struct_size = sizeof(io), .user_data = &ms, .read = mem_read, .write = NULL, .seek = mem_seek, .tell = mem_tell, .flush = NULL };
232
6.50k
    return read_track_io(&io, buf, file_size, want_track_id, track);
233
6.51k
}
234
235
bool mp4_read_track_file(FILE *f, uint64_t file_size, uint32_t want_track_id, MP4Track *track)
236
0
{
237
0
    uint8_t prefix[32];
238
0
    memset(track, 0, sizeof(*track));
239
0
    if (!cli_fseek(f, 0) || fread(prefix, 1, sizeof(prefix), f) != sizeof(prefix) ||
240
0
        !cli_fseek(f, 0)) return false;
241
0
    faam_io io = cli_faam_io(f);
242
0
    return read_track_io(&io, prefix, file_size, want_track_id, track);
243
0
}
244
245
void mp4_free_track(MP4Track *track)
246
6.51k
{
247
7.17k
    for (uint32_t i = 0; i < track->num_tags; i++) {
248
659
        free(track->tags[i].name);
249
659
        free(track->tags[i].value);
250
659
    }
251
6.51k
    free(track->tags);
252
6.51k
    free(track->asc_buf);
253
6.51k
    free(track->samples);
254
6.51k
    memset(track, 0, sizeof(*track));
255
6.51k
}