FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mods.c
Date: 2026-07-16 17:05:34
Exec Total Coverage
Lines: 3 63 4.8%
Functions: 1 3 33.3%
Branches: 1 26 3.8%

Line Branch Exec Source
1 /*
2 * MODS demuxer
3 * Copyright (c) 2015-2016 Florian Nouwt
4 * Copyright (c) 2017 Adib Surani
5 * Copyright (c) 2020 Paul B Mahol
6 * Copyright (c) 2026 Link Mauve
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "libavutil/intreadwrite.h"
26
27 #include "avformat.h"
28 #include "demux.h"
29 #include "internal.h"
30
31 typedef struct MODSDemuxContext {
32 uint32_t index_pos;
33 } MODSDemuxContext;
34
35 7768 static int mods_probe(const AVProbeData *p)
36 {
37
1/2
✓ Branch 0 taken 7768 times.
✗ Branch 1 not taken.
7768 if (memcmp(p->buf, "MODSN3\x0a\x00", 8))
38 7768 return 0;
39 if (AV_RB32(p->buf + 8) == 0)
40 return 0;
41 if (AV_RB32(p->buf + 12) == 0)
42 return 0;
43 if (AV_RB32(p->buf + 16) == 0)
44 return 0;
45 return AVPROBE_SCORE_MAX;
46 }
47
48 static int mods_read_header(AVFormatContext *s)
49 {
50 AVIOContext *pb = s->pb;
51 MODSDemuxContext *ctx = s->priv_data;
52 AVRational fps;
53 int64_t pos;
54 int64_t timestamp;
55 int num_keyframes;
56 const AVIndexEntry *e;
57
58 AVStream *st = avformat_new_stream(s, NULL);
59 if (!st)
60 return AVERROR(ENOMEM);
61
62 avio_skip(pb, 8);
63
64 st->nb_frames = avio_rl32(pb);
65 st->duration = st->nb_frames;
66 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
67 st->codecpar->codec_id = AV_CODEC_ID_MOBICLIP;
68 st->codecpar->width = avio_rl32(pb);
69 st->codecpar->height = avio_rl32(pb);
70
71 fps.num = avio_rl32(pb);
72 fps.den = 0x1000000;
73 avpriv_set_pts_info(st, 64, fps.den, fps.num);
74
75 avio_skip(pb, 16);
76
77 pos = avio_rl32(pb);
78 num_keyframes = avio_rl32(pb);
79 avio_seek(pb, pos, SEEK_SET);
80 ctx->index_pos = pos;
81
82 for (int i = 0; i < num_keyframes; ++i) {
83 timestamp = avio_rl32(pb);
84 pos = avio_rl32(pb);
85 if (avio_feof(pb))
86 return AVERROR_INVALIDDATA;
87
88 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
89 }
90
91 e = avformat_index_get_entry(st, 0);
92 if (!e)
93 return AVERROR_INVALIDDATA;
94
95 avpriv_update_cur_dts(s, st, e->timestamp);
96 avio_seek(pb, e->pos, SEEK_SET);
97
98 return 0;
99 }
100
101 static int mods_read_packet(AVFormatContext *s, AVPacket *pkt)
102 {
103 AVIOContext *pb = s->pb;
104 MODSDemuxContext *ctx = s->priv_data;
105 AVStream *st = s->streams[0];
106 unsigned size;
107 int64_t pos;
108 int ret;
109 const AVIndexEntry *e;
110
111 if (avio_feof(pb))
112 return AVERROR_EOF;
113
114 /* This assumes the keyframes index directly follows the last packet. */
115 pos = avio_tell(pb);
116 if (pos == ctx->index_pos)
117 return AVERROR_EOF;
118
119 size = avio_rl32(pb) >> 14;
120 ret = av_get_packet(pb, pkt, size);
121 if (ret < 0)
122 return ret;
123 pkt->pos = pos;
124 pkt->stream_index = 0;
125
126 e = avformat_index_get_entry_from_timestamp(st, ffstream(st)->cur_dts, 0);
127 if (e && e->pos == pos) {
128 pkt->flags |= AV_PKT_FLAG_KEY;
129 pkt->pts = pkt->dts = e->timestamp;
130 }
131
132 return ret;
133 }
134
135 const FFInputFormat ff_mods_demuxer = {
136 .p.name = "mods",
137 .p.long_name = NULL_IF_CONFIG_SMALL("MobiClip MODS"),
138 .p.extensions = "mods",
139 .p.flags = AVFMT_GENERIC_INDEX,
140 .priv_data_size = sizeof(MODSDemuxContext),
141 .read_probe = mods_probe,
142 .read_header = mods_read_header,
143 .read_packet = mods_read_packet,
144 };
145