FFmpeg
Loading...
Searching...
No Matches
mvrdec.c
Go to the documentation of this file.
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
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
38typedef struct MVRContext {
39 /* index of the chunk to read next */
40 unsigned next_chunk;
42
43static 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 if (p->buf_size < 0x40)
50 return 0;
51
52 if (AV_RL32(p->buf) != 4)
53 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
75{
76 AVIOContext *pb = s->pb;
77 AVStream *st;
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);
87
89 if (!st)
90 return AVERROR(ENOMEM);
91
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
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
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,
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
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),
208};
const FFInputFormat ff_mvr_demuxer
Definition mvrdec.c:199
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2616
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:611
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
uint64_t avio_rl64(AVIOContext *s)
Definition aviobuf.c:741
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:51
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_H264
Definition codec_id.h:77
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
int av_size_mult(size_t a, size_t b, size_t *r)
Multiply two size_t values checking for overflow.
Definition mem.c:565
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition utils.c:616
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
Memory handling functions.
#define MVR_HEADER_SIZE
Definition mvrdec.c:34
#define MVR_ENTRY_SIZE
Definition mvrdec.c:35
static int mvr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mvrdec.c:158
static int mvr_read_header(AVFormatContext *s)
Definition mvrdec.c:74
static int mvr_probe(const AVProbeData *p)
Definition mvrdec.c:43
#define MVR_CHUNK_HEADER
Definition mvrdec.c:36
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int64_t pos
Definition avformat.h:621
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:622
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t cur_dts
Definition internal.h:360
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
enum AVStreamParseType need_parsing
Definition internal.h:321
unsigned next_chunk
Definition mvrdec.c:40
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89