FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mvrdec.c
Date: 2026-09-16 13:08:21
Exec Total Coverage
Lines: 5 94 5.3%
Functions: 1 3 33.3%
Branches: 3 60 5.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2026 Zhao Zhili
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <limits.h>
22
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 #include "avformat.h"
26 #include "demux.h"
27 #include "internal.h"
28
29 /*
30 * Layout: [512 B header][index: N x 8 B (start_ms:u32, offset:u32)]
31 * [chunks: 16 B local header + H.264 Annex-B NALs]
32 * Each chunk is a self-contained GOP (SPS+PPS+IDR + P-slices).
33 */
34 #define MVR_HEADER_SIZE 512
35 #define MVR_ENTRY_SIZE 8
36 #define MVR_CHUNK_HEADER 16
37
38 typedef struct MVRContext {
39 /* index of the chunk to read next */
40 unsigned next_chunk;
41 } MVRContext;
42
43 8043 static int mvr_probe(const AVProbeData *p)
44 {
45 unsigned width, height;
46 unsigned index_table_size, index_capacity, index_count;
47 size_t index_size;
48
49
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8038 times.
8043 if (p->buf_size < 0x40)
50 5 return 0;
51
52
1/2
✓ Branch 0 taken 8038 times.
✗ Branch 1 not taken.
8038 if (AV_RL32(p->buf) != 4)
53 8038 return 0;
54
55 width = AV_RL32(p->buf + 0x04);
56 height = AV_RL32(p->buf + 0x08);
57 if (width == 0 || width > 8192 || height == 0 || height > 8192)
58 return 0;
59
60 index_table_size = AV_RL32(p->buf + 0x34);
61 index_capacity = AV_RL32(p->buf + 0x38);
62 index_count = AV_RL32(p->buf + 0x3c);
63
64 if (index_capacity == 0 ||
65 av_size_mult(index_capacity, MVR_ENTRY_SIZE, &index_size) < 0 ||
66 index_table_size != index_size ||
67 index_count > index_capacity)
68 return 0;
69
70 /* Must outrank h264 */
71 return AVPROBE_SCORE_EXTENSION + 2;
72 }
73
74 static int mvr_read_header(AVFormatContext *s)
75 {
76 AVIOContext *pb = s->pb;
77 AVStream *st;
78 int64_t filesize;
79 uint64_t time_meta_ms;
80 uint32_t index_count, start_ms, offset;
81 int64_t index_table_size, data_start;
82 int ret;
83
84 filesize = avio_size(pb);
85 if (filesize < MVR_HEADER_SIZE + MVR_ENTRY_SIZE)
86 return AVERROR_INVALIDDATA;
87
88 st = avformat_new_stream(s, NULL);
89 if (!st)
90 return AVERROR(ENOMEM);
91
92 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
93 st->codecpar->codec_id = AV_CODEC_ID_H264;
94 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
95 avpriv_set_pts_info(st, 64, 1, 1000);
96
97 avio_skip(pb, 4);
98 st->codecpar->width = avio_rl32(pb);
99 st->codecpar->height = avio_rl32(pb);
100
101 avio_skip(pb, 0x28 - 0x0c);
102 time_meta_ms = avio_rl64(pb);
103 ff_dict_set_timestamp(&s->metadata, "creation_time",
104 time_meta_ms * 1000);
105 st->duration = avio_rl32(pb);
106
107 /* Skip index_capacity; data starts after the full index table. */
108 index_table_size = avio_rl32(pb);
109 avio_skip(pb, 4);
110 index_count = avio_rl32(pb);
111
112 if (index_table_size == 0 ||
113 index_table_size > UINT32_MAX - MVR_HEADER_SIZE ||
114 index_table_size % MVR_ENTRY_SIZE != 0 ||
115 index_table_size / MVR_ENTRY_SIZE < index_count)
116 return AVERROR_INVALIDDATA;
117
118 if (avio_seek(pb, MVR_HEADER_SIZE, SEEK_SET) < 0)
119 return AVERROR_INVALIDDATA;
120
121 data_start = MVR_HEADER_SIZE + index_table_size;
122 if (filesize < data_start)
123 return AVERROR_INVALIDDATA;
124
125 start_ms = avio_rl32(pb);
126 offset = avio_rl32(pb);
127 for (unsigned i = 0; i < index_count; i++) {
128 uint32_t next_start_ms;
129 int64_t next_offset, chunk_size;
130
131 if (i + 1 < index_count) {
132 next_start_ms = avio_rl32(pb);
133 next_offset = avio_rl32(pb);
134 } else {
135 next_start_ms = 0;
136 next_offset = filesize;
137 }
138
139 if (offset < data_start || next_offset > filesize)
140 return AVERROR_INVALIDDATA;
141
142 chunk_size = next_offset - offset;
143 if (chunk_size <= MVR_CHUNK_HEADER || chunk_size > INT_MAX)
144 return AVERROR_INVALIDDATA;
145
146 ret = av_add_index_entry(st, offset, start_ms,
147 chunk_size, 0, AVINDEX_KEYFRAME);
148 if (ret < 0)
149 return ret;
150
151 start_ms = next_start_ms;
152 offset = next_offset;
153 }
154
155 return 0;
156 }
157
158 static int mvr_read_packet(AVFormatContext *s, AVPacket *pkt)
159 {
160 MVRContext *mvr = s->priv_data;
161 AVStream *st = s->streams[0];
162 FFStream *const sti = ffstream(st);
163 AVIndexEntry *e;
164 int64_t h264_pos;
165 int h264_size, ret;
166
167 /* Resync after a generic seek. */
168 if (s->io_repositioned) {
169 int idx = av_index_search_timestamp(st, sti->cur_dts,
170 AVSEEK_FLAG_BACKWARD);
171 if (idx < 0)
172 return AVERROR(EINVAL);
173 mvr->next_chunk = idx;
174 s->io_repositioned = 0;
175 }
176
177 if (mvr->next_chunk >= sti->nb_index_entries)
178 return AVERROR_EOF;
179
180 e = &sti->index_entries[mvr->next_chunk];
181 h264_pos = e->pos + MVR_CHUNK_HEADER;
182 h264_size = e->size - MVR_CHUNK_HEADER;
183
184 if (avio_seek(s->pb, h264_pos, SEEK_SET) < 0)
185 return AVERROR_EOF;
186
187 ret = av_get_packet(s->pb, pkt, h264_size);
188 if (ret < 0)
189 return ret;
190
191 pkt->pts = e->timestamp;
192 pkt->dts = AV_NOPTS_VALUE;
193 pkt->pos = e->pos;
194 mvr->next_chunk++;
195
196 return 0;
197 }
198
199 const FFInputFormat ff_mvr_demuxer = {
200 .p.name = "mvr",
201 .p.long_name = NULL_IF_CONFIG_SMALL("MVR CCTV"),
202 .p.extensions = "mvr",
203 .p.flags = AVFMT_GENERIC_INDEX,
204 .priv_data_size = sizeof(MVRContext),
205 .read_probe = mvr_probe,
206 .read_header = mvr_read_header,
207 .read_packet = mvr_read_packet,
208 };
209