FFmpeg
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 
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 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  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);
86  return AVERROR_INVALIDDATA;
87 
88  st = avformat_new_stream(s, NULL);
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 
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 
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;
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 };
av_size_mult
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
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:53
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
mvr_probe
static int mvr_probe(const AVProbeData *p)
Definition: mvrdec.c:43
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:326
AVIndexEntry
Definition: avformat.h:601
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:609
avpriv_set_pts_info
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:829
MVR_HEADER_SIZE
#define MVR_HEADER_SIZE
Definition: mvrdec.c:34
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
av_add_index_entry
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
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:806
MVRContext::next_chunk
unsigned next_chunk
Definition: mvrdec.c:40
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
mvr_read_header
static int mvr_read_header(AVFormatContext *s)
Definition: mvrdec.c:74
intreadwrite.h
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:551
AVCodecParameters::width
int width
The width of the video frame in pixels.
Definition: codec_par.h:143
AVIndexEntry::size
int size
Definition: avformat.h:612
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:603
limits.h
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:314
AVFormatContext
Format I/O context.
Definition: avformat.h:1314
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:770
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2573
NULL
#define NULL
Definition: coverity.c:32
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:186
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:452
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:462
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:733
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
MVR_CHUNK_HEADER
#define MVR_CHUNK_HEADER
Definition: mvrdec.c:36
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:88
height
#define height
Definition: dsp.h:89
FFStream
Definition: internal.h:128
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
ff_dict_set_timestamp
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
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:247
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:70
MVRContext
Definition: mvrdec.c:38
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:602
ff_mvr_demuxer
const FFInputFormat ff_mvr_demuxer
Definition: mvrdec.c:199
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
read_header
static int read_header(FFV1Context *f, RangeCoder *c)
Definition: ffv1dec.c:574
filesize
static int64_t filesize(AVIOContext *pb)
Definition: ffmpeg_mux.c:51
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:596
AVCodecParameters::height
int height
The height of the video frame in pixels.
Definition: codec_par.h:150
s
uint8_t s
Definition: llvidencdsp.c:39
mvr_read_packet
static int mvr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mvrdec.c:158
demux.h
av_get_packet
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
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:747
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:602
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:321
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:184
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
mem.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:57
AVPacket
This structure stores compressed data.
Definition: packet.h:580
FFStream::cur_dts
int64_t cur_dts
Definition: internal.h:353
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:623
FFInputFormat
Definition: demux.h:66
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:741
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:592
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
pkt
static AVPacket * pkt
Definition: demux_decode.c:55
width
#define width
Definition: dsp.h:89
MVR_ENTRY_SIZE
#define MVR_ENTRY_SIZE
Definition: mvrdec.c:35
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: seek.c:245