Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/smush.c
Line
Count
Source
1
/*
2
 * LucasArts Smush demuxer
3
 * Copyright (c) 2006 Cyril Zorin
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "libavutil/intreadwrite.h"
23
24
#include "avformat.h"
25
#include "avio.h"
26
#include "demux.h"
27
#include "internal.h"
28
29
typedef struct SMUSHContext {
30
    int version;
31
    int audio_stream_index;
32
    int video_stream_index;
33
} SMUSHContext;
34
35
static int smush_read_probe(const AVProbeData *p)
36
958k
{
37
958k
    if (((AV_RL32(p->buf)     == MKTAG('S', 'A', 'N', 'M') &&
38
383
          AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
39
958k
         (AV_RL32(p->buf)     == MKTAG('A', 'N', 'I', 'M') &&
40
289
          AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
41
289
        return AVPROBE_SCORE_MAX;
42
289
    }
43
44
958k
    return 0;
45
958k
}
46
47
static int smush_read_header(AVFormatContext *ctx)
48
3.90k
{
49
3.90k
    SMUSHContext *smush = ctx->priv_data;
50
3.90k
    AVIOContext *pb = ctx->pb;
51
3.90k
    AVStream *vst, *ast;
52
3.90k
    uint32_t magic, nframes, size, subversion, i;
53
3.90k
    uint32_t width = 0, height = 0, got_audio = 0, read = 0;
54
3.90k
    uint32_t sample_rate, channels, palette[256], frame_rate = 15;
55
3.90k
    int ret;
56
57
3.90k
    magic = avio_rb32(pb);
58
3.90k
    avio_skip(pb, 4); // skip movie size
59
60
3.90k
    if (magic == MKBETAG('A', 'N', 'I', 'M')) {
61
605
        if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
62
52
            return AVERROR_INVALIDDATA;
63
64
553
        size = avio_rb32(pb);
65
553
        if (size < 3 * 256 + 6)
66
13
            return AVERROR_INVALIDDATA;
67
540
        size -= 3 * 256 + 6;
68
69
540
        smush->version = 0;
70
540
        subversion     = avio_rl16(pb);
71
540
        nframes        = avio_rl16(pb);
72
540
        if (!nframes)
73
38
            return AVERROR_INVALIDDATA;
74
75
502
        avio_skip(pb, 2); // skip pad
76
77
129k
        for (i = 0; i < 256; i++)
78
128k
            palette[i] = avio_rb24(pb);
79
80
502
        if (subversion > 1) {
81
481
            if (size < 12)
82
13
                return AVERROR_INVALIDDATA;
83
468
            size -= 12;
84
468
            frame_rate = avio_rl32(pb);
85
468
            avio_skip(pb, 4);            // max size of FRME chunk in file
86
468
            sample_rate = avio_rl32(pb);
87
468
            if (frame_rate < 1 || frame_rate > 70)
88
261
                frame_rate = 12;
89
468
        }
90
489
        avio_skip(pb, size);
91
3.30k
    } else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
92
3.11k
        if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
93
56
            return AVERROR_INVALIDDATA;
94
95
3.05k
        size = avio_rb32(pb);
96
3.05k
        if (size < 14)
97
8
            return AVERROR_INVALIDDATA;
98
99
3.04k
        smush->version = 1;
100
3.04k
        subversion = avio_rl16(pb);
101
3.04k
        nframes = avio_rl32(pb);
102
3.04k
        if (!nframes)
103
41
            return AVERROR_INVALIDDATA;
104
105
3.00k
        avio_skip(pb, 2); // skip pad
106
3.00k
        width  = avio_rl16(pb);
107
3.00k
        height = avio_rl16(pb);
108
3.00k
        avio_skip(pb, 2); // skip pad
109
3.00k
        avio_skip(pb, size - 14);
110
111
3.00k
        if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
112
158
            return AVERROR_INVALIDDATA;
113
114
2.85k
        size = avio_rb32(pb);
115
5.39k
        while (!got_audio && ((read + 8) < size)) {
116
2.91k
            uint32_t sig, chunk_size;
117
118
2.91k
            if (avio_feof(pb))
119
85
                return AVERROR_EOF;
120
121
2.83k
            sig        = avio_rb32(pb);
122
2.83k
            chunk_size = avio_rb32(pb);
123
2.83k
            read      += 8;
124
2.83k
            switch (sig) {
125
845
            case MKBETAG('W', 'a', 'v', 'e'):
126
845
                got_audio = 1;
127
845
                sample_rate = avio_rl32(pb);
128
845
                if (!sample_rate)
129
3
                    return AVERROR_INVALIDDATA;
130
131
842
                channels = avio_rl32(pb);
132
842
                if (!channels)
133
28
                    return AVERROR_INVALIDDATA;
134
135
814
                avio_skip(pb, chunk_size - 8);
136
814
                read += chunk_size;
137
814
                break;
138
1.40k
            case MKBETAG('B', 'l', '1', '6'):
139
1.73k
            case MKBETAG('A', 'N', 'N', 'O'):
140
1.73k
                avio_skip(pb, chunk_size);
141
1.73k
                read += chunk_size;
142
1.73k
                break;
143
252
            default:
144
252
                return AVERROR_INVALIDDATA;
145
2.83k
            }
146
2.83k
        }
147
148
2.48k
        avio_skip(pb, size - read);
149
2.48k
    } else {
150
189
        av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
151
189
        return AVERROR_INVALIDDATA;
152
189
    }
153
154
2.97k
    vst = avformat_new_stream(ctx, 0);
155
2.97k
    if (!vst)
156
0
        return AVERROR(ENOMEM);
157
158
2.97k
    smush->video_stream_index = vst->index;
159
160
2.97k
    avpriv_set_pts_info(vst, 64, 1, frame_rate);
161
162
2.97k
    vst->start_time        = 0;
163
2.97k
    vst->duration          =
164
2.97k
    vst->nb_frames         = nframes;
165
2.97k
    vst->avg_frame_rate    = av_inv_q(vst->time_base);
166
2.97k
    vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
167
2.97k
    vst->codecpar->codec_id   = AV_CODEC_ID_SANM;
168
2.97k
    vst->codecpar->codec_tag  = 0;
169
2.97k
    vst->codecpar->width      = width;
170
2.97k
    vst->codecpar->height     = height;
171
172
2.97k
    if (!smush->version) {
173
489
        if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
174
0
            return ret;
175
176
489
        AV_WL16(vst->codecpar->extradata, subversion);
177
125k
        for (i = 0; i < 256; i++)
178
125k
            AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
179
489
    }
180
181
2.97k
    if (got_audio) {
182
814
        ast = avformat_new_stream(ctx, 0);
183
814
        if (!ast)
184
0
            return AVERROR(ENOMEM);
185
186
814
        smush->audio_stream_index = ast->index;
187
188
814
        ast->start_time         = 0;
189
814
        ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
190
814
        ast->codecpar->codec_id    = AV_CODEC_ID_ADPCM_VIMA;
191
814
        ast->codecpar->codec_tag   = 0;
192
814
        ast->codecpar->sample_rate = sample_rate;
193
814
        ast->codecpar->ch_layout.nb_channels = channels;
194
814
        avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
195
814
    }
196
197
2.97k
    return 0;
198
2.97k
}
199
200
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
201
97.0k
{
202
97.0k
    SMUSHContext *smush = ctx->priv_data;
203
97.0k
    AVIOContext *pb = ctx->pb;
204
97.0k
    int done = 0;
205
97.0k
    int ret;
206
207
683k
    while (!done) {
208
591k
        uint32_t sig, size;
209
210
591k
        if (avio_feof(pb))
211
4.74k
            return AVERROR_EOF;
212
213
587k
        sig  = avio_rb32(pb);
214
587k
        size = avio_rb32(pb);
215
216
587k
        switch (sig) {
217
1.04k
        case MKBETAG('F', 'R', 'M', 'E'):
218
1.04k
            if (smush->version)
219
691
                break;
220
355
            if ((ret = av_get_packet(pb, pkt, size)) < 0)
221
9
                return ret;
222
223
346
            pkt->stream_index = smush->video_stream_index;
224
346
            done = 1;
225
346
            break;
226
30.2k
        case MKBETAG('B', 'l', '1', '6'):
227
30.2k
            if ((ret = av_get_packet(pb, pkt, size)) < 0)
228
347
                return ret;
229
230
29.9k
            pkt->stream_index = smush->video_stream_index;
231
29.9k
            pkt->duration = 1;
232
29.9k
            done = 1;
233
29.9k
            break;
234
61.6k
        case MKBETAG('W', 'a', 'v', 'e'):
235
61.6k
            if (size < 13)
236
34
                return AVERROR_INVALIDDATA;
237
61.6k
            if (av_get_packet(pb, pkt, size) < 13)
238
330
                return AVERROR_INVALIDDATA;
239
240
61.3k
            pkt->stream_index = smush->audio_stream_index;
241
61.3k
            pkt->flags       |= AV_PKT_FLAG_KEY;
242
61.3k
            pkt->duration     = AV_RB32(pkt->data);
243
61.3k
            if (pkt->duration == 0xFFFFFFFFu)
244
452
                pkt->duration = AV_RB32(pkt->data + 8);
245
61.3k
            done = 1;
246
61.3k
            break;
247
494k
        default:
248
494k
            avio_skip(pb, size);
249
494k
            break;
250
587k
        }
251
587k
    }
252
253
91.5k
    return 0;
254
97.0k
}
255
256
const FFInputFormat ff_smush_demuxer = {
257
    .p.name         = "smush",
258
    .p.long_name    = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
259
    .priv_data_size = sizeof(SMUSHContext),
260
    .read_probe     = smush_read_probe,
261
    .read_header    = smush_read_header,
262
    .read_packet    = smush_read_packet,
263
};