Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/smjpegdec.c
Line
Count
Source
1
/*
2
 * SMJPEG demuxer
3
 * Copyright (c) 2011 Paul B Mahol
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
/**
23
 * @file
24
 * This is a demuxer for Loki SDL Motion JPEG files
25
 */
26
27
#include <inttypes.h>
28
29
#include "libavutil/mem.h"
30
#include "avformat.h"
31
#include "demux.h"
32
#include "internal.h"
33
#include "smjpeg.h"
34
35
typedef struct SMJPEGContext {
36
    int audio_stream_index;
37
    int video_stream_index;
38
} SMJPEGContext;
39
40
static int smjpeg_probe(const AVProbeData *p)
41
964k
{
42
964k
    if (!memcmp(p->buf, SMJPEG_MAGIC, 8))
43
684
        return AVPROBE_SCORE_MAX;
44
964k
    return 0;
45
964k
}
46
47
static int smjpeg_read_header(AVFormatContext *s)
48
3.86k
{
49
3.86k
    SMJPEGContext *sc = s->priv_data;
50
3.86k
    AVStream *ast = NULL, *vst = NULL;
51
3.86k
    AVIOContext *pb = s->pb;
52
3.86k
    uint32_t version, htype, hlength, duration;
53
3.86k
    char *comment;
54
55
3.86k
    sc->audio_stream_index =
56
3.86k
    sc->video_stream_index = -1;
57
58
3.86k
    avio_skip(pb, 8); // magic
59
3.86k
    version = avio_rb32(pb);
60
3.86k
    if (version)
61
3.59k
        avpriv_request_sample(s, "Unknown version %"PRIu32, version);
62
63
3.86k
    duration = avio_rb32(pb); // in msec
64
65
9.14k
    while (!avio_feof(pb)) {
66
8.69k
        htype = avio_rl32(pb);
67
8.69k
        switch (htype) {
68
1.42k
        case SMJPEG_TXT:
69
1.42k
            hlength = avio_rb32(pb);
70
1.42k
            if (!hlength || hlength > 512)
71
40
                return AVERROR_INVALIDDATA;
72
1.38k
            comment = av_malloc(hlength + 1);
73
1.38k
            if (!comment)
74
0
                return AVERROR(ENOMEM);
75
1.38k
            if (avio_read(pb, comment, hlength) != hlength) {
76
37
                av_freep(&comment);
77
37
                av_log(s, AV_LOG_ERROR, "error when reading comment\n");
78
37
                return AVERROR_INVALIDDATA;
79
37
            }
80
1.34k
            comment[hlength] = 0;
81
1.34k
            av_dict_set(&s->metadata, "comment", comment,
82
1.34k
                        AV_DICT_DONT_STRDUP_VAL);
83
1.34k
            break;
84
2.35k
        case SMJPEG_SND:
85
2.35k
            if (ast) {
86
1
                avpriv_request_sample(s, "Multiple audio streams");
87
1
                return AVERROR_PATCHWELCOME;
88
1
            }
89
2.34k
            hlength = avio_rb32(pb);
90
2.34k
            if (hlength < 8)
91
8
                return AVERROR_INVALIDDATA;
92
2.34k
            ast = avformat_new_stream(s, 0);
93
2.34k
            if (!ast)
94
0
                return AVERROR(ENOMEM);
95
2.34k
            ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
96
2.34k
            ast->codecpar->sample_rate = avio_rb16(pb);
97
2.34k
            ast->codecpar->bits_per_coded_sample = avio_r8(pb);
98
2.34k
            ast->codecpar->ch_layout.nb_channels = avio_r8(pb);
99
2.34k
            ast->codecpar->codec_tag   = avio_rl32(pb);
100
2.34k
            ast->codecpar->codec_id    = ff_codec_get_id(ff_codec_smjpeg_audio_tags,
101
2.34k
                                                         ast->codecpar->codec_tag);
102
2.34k
            ast->duration           = duration;
103
2.34k
            sc->audio_stream_index  = ast->index;
104
2.34k
            avpriv_set_pts_info(ast, 32, 1, 1000);
105
2.34k
            avio_skip(pb, hlength - 8);
106
2.34k
            break;
107
1.59k
        case SMJPEG_VID:
108
1.59k
            if (vst) {
109
1
                avpriv_request_sample(s, "Multiple video streams");
110
1
                return AVERROR_INVALIDDATA;
111
1
            }
112
1.59k
            hlength = avio_rb32(pb);
113
1.59k
            if (hlength < 12)
114
3
                return AVERROR_INVALIDDATA;
115
1.59k
            vst = avformat_new_stream(s, 0);
116
1.59k
            if (!vst)
117
0
                return AVERROR(ENOMEM);
118
1.59k
            vst->nb_frames            = avio_rb32(pb);
119
1.59k
            vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
120
1.59k
            vst->codecpar->width      = avio_rb16(pb);
121
1.59k
            vst->codecpar->height     = avio_rb16(pb);
122
1.59k
            vst->codecpar->codec_tag  = avio_rl32(pb);
123
1.59k
            vst->codecpar->codec_id   = ff_codec_get_id(ff_codec_smjpeg_video_tags,
124
1.59k
                                                        vst->codecpar->codec_tag);
125
1.59k
            vst->duration          = duration;
126
1.59k
            sc->video_stream_index = vst->index;
127
1.59k
            avpriv_set_pts_info(vst, 32, 1, 1000);
128
1.59k
            avio_skip(pb, hlength - 12);
129
1.59k
            break;
130
2.95k
        case SMJPEG_HEND:
131
2.95k
            return 0;
132
367
        default:
133
367
            av_log(s, AV_LOG_ERROR, "unknown header %"PRIx32"\n", htype);
134
367
            return AVERROR_INVALIDDATA;
135
8.69k
        }
136
8.69k
    }
137
138
456
    return AVERROR_EOF;
139
3.86k
}
140
141
static int smjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)
142
763k
{
143
763k
    SMJPEGContext *sc = s->priv_data;
144
763k
    uint32_t dtype, size, timestamp;
145
763k
    int64_t pos;
146
763k
    int ret;
147
148
763k
    if (avio_feof(s->pb))
149
3.07k
        return AVERROR_EOF;
150
760k
    pos   = avio_tell(s->pb);
151
760k
    dtype = avio_rl32(s->pb);
152
760k
    switch (dtype) {
153
361k
    case SMJPEG_SNDD:
154
361k
        if (sc->audio_stream_index < 0)
155
10
            return AVERROR_INVALIDDATA;
156
361k
        timestamp = avio_rb32(s->pb);
157
361k
        size = avio_rb32(s->pb);
158
361k
        ret = av_get_packet(s->pb, pkt, size);
159
361k
        pkt->stream_index = sc->audio_stream_index;
160
361k
        pkt->pts = timestamp;
161
361k
        pkt->pos = pos;
162
361k
        break;
163
398k
    case SMJPEG_VIDD:
164
398k
        if (sc->video_stream_index < 0)
165
11
            return AVERROR_INVALIDDATA;
166
398k
        timestamp = avio_rb32(s->pb);
167
398k
        size = avio_rb32(s->pb);
168
398k
        ret = av_get_packet(s->pb, pkt, size);
169
398k
        pkt->stream_index = sc->video_stream_index;
170
398k
        pkt->pts = timestamp;
171
398k
        pkt->pos = pos;
172
398k
        break;
173
5
    case SMJPEG_DONE:
174
5
        ret = AVERROR_EOF;
175
5
        break;
176
1.49k
    default:
177
1.49k
        av_log(s, AV_LOG_ERROR, "unknown chunk %"PRIx32"\n", dtype);
178
1.49k
        ret = AVERROR_INVALIDDATA;
179
1.49k
        break;
180
760k
    }
181
760k
    return ret;
182
760k
}
183
184
const FFInputFormat ff_smjpeg_demuxer = {
185
    .p.name         = "smjpeg",
186
    .p.long_name    = NULL_IF_CONFIG_SMALL("Loki SDL MJPEG"),
187
    .p.extensions   = "mjpg",
188
    .p.flags        = AVFMT_GENERIC_INDEX,
189
    .priv_data_size = sizeof(SMJPEGContext),
190
    .read_probe     = smjpeg_probe,
191
    .read_header    = smjpeg_read_header,
192
    .read_packet    = smjpeg_read_packet,
193
};