FFmpeg
ifv.c
Go to the documentation of this file.
1 /*
2  * IFV demuxer
3  *
4  * Copyright (c) 2019 Swaraj Hota
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
24 #include "avformat.h"
25 #include "internal.h"
26 #include "avio_internal.h"
27 
28 typedef struct IFVContext {
29  uint32_t next_video_index;
30  uint32_t next_audio_index;
31  uint32_t total_vframes;
32  uint32_t total_aframes;
33 
34  int width, height;
37 
40 } IFVContext;
41 
42 static int ifv_probe(const AVProbeData *p)
43 {
44  static const uint8_t ifv_magic[] = {0x11, 0xd2, 0xd3, 0xab, 0xba, 0xa9,
45  0xcf, 0x11, 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65, 0x44};
46 
47  if (!memcmp(p->buf, ifv_magic, sizeof(ifv_magic)))
48  return AVPROBE_SCORE_MAX;
49 
50  return 0;
51 }
52 
55  uint32_t start_index)
56 {
57  IFVContext *ifv = s->priv_data;
58  AVStream *st;
59  int64_t pos, size, timestamp;
60  uint32_t end_index, i;
61  int ret;
62 
64  end_index = ifv->total_vframes;
65  st = s->streams[ifv->video_stream_index];
66  } else {
67  end_index = ifv->total_aframes;
68  st = s->streams[ifv->audio_stream_index];
69  }
70 
71  for (i = start_index; i < end_index; i++) {
72  if (avio_feof(s->pb))
73  return AVERROR_EOF;
74  pos = avio_rl32(s->pb);
75  size = avio_rl32(s->pb);
76 
77  avio_skip(s->pb, 8);
78  timestamp = avio_rl32(s->pb);
79 
80  ret = av_add_index_entry(st, pos, timestamp, size, 0, 0);
81  if (ret < 0)
82  return ret;
83 
84  avio_skip(s->pb, frame_type == AVMEDIA_TYPE_VIDEO ? 8: 4);
85  }
86 
87  return 0;
88 }
89 
91 {
92  IFVContext *ifv = s->priv_data;
93  uint32_t aud_magic;
94  uint32_t vid_magic;
95 
96  avio_skip(s->pb, 0x34);
97  avpriv_dict_set_timestamp(&s->metadata, "creation_time", avio_rl32(s->pb) * 1000000LL);
98  avio_skip(s->pb, 0x24);
99 
100  ifv->width = avio_rl16(s->pb);
101  ifv->height = avio_rl16(s->pb);
102 
103  avio_skip(s->pb, 0x8);
104  vid_magic = avio_rl32(s->pb);
105 
106  if (vid_magic != MKTAG('H','2','6','4'))
107  avpriv_request_sample(s, "Unknown video codec %x", vid_magic);
108 
109  avio_skip(s->pb, 0x2c);
110  ifv->sample_rate = avio_rl32(s->pb);
111  aud_magic = avio_rl32(s->pb);
112 
113  if (aud_magic == MKTAG('G','R','A','W')) {
114  ifv->is_audio_present = 1;
115  } else if (aud_magic == MKTAG('P','C','M','U')) {
116  ifv->is_audio_present = 0;
117  } else {
118  avpriv_request_sample(s, "Unknown audio codec %x", aud_magic);
119  }
120 
121  avio_skip(s->pb, 0x44);
122  ifv->total_vframes = avio_rl32(s->pb);
123  ifv->total_aframes = avio_rl32(s->pb);
124 
125  return 0;
126 }
127 
129 {
130  IFVContext *ifv = s->priv_data;
131  AVStream *st;
132  int ret;
133 
134  ret = parse_header(s);
135  if (ret < 0)
136  return ret;
137 
138  st = avformat_new_stream(s, NULL);
139  if (!st)
140  return AVERROR(ENOMEM);
141 
144  st->codecpar->width = ifv->width;
145  st->codecpar->height = ifv->height;
146  st->start_time = 0;
147  ifv->video_stream_index = st->index;
148 
149  avpriv_set_pts_info(st, 32, 1, 1000);
150 
151  if (ifv->is_audio_present) {
152  st = avformat_new_stream(s, NULL);
153  if (!st)
154  return AVERROR(ENOMEM);
155 
158  st->codecpar->channels = 1;
160  st->codecpar->sample_rate = ifv->sample_rate;
161  ifv->audio_stream_index = st->index;
162 
163  avpriv_set_pts_info(st, 32, 1, 1000);
164  }
165 
166  /*read video index*/
167  avio_seek(s->pb, 0xf8, SEEK_SET);
168 
170  if (ret < 0)
171  return ret;
172 
173  if (ifv->is_audio_present) {
174  /*read audio index*/
175  avio_seek(s->pb, 0x14918, SEEK_SET);
176 
178  if (ret < 0)
179  return ret;
180  }
181 
182  ifv->next_video_index = 0;
183  ifv->next_audio_index = 0;
184 
185  return 0;
186 }
187 
189 {
190  IFVContext *ifv = s->priv_data;
191  AVIndexEntry *ev, *ea, *e_next;
192  int ret;
193 
194  ev = ea = e_next = NULL;
195 
196  if (ifv->next_video_index < ifv->total_vframes) {
197  AVStream *const st = s->streams[ifv->video_stream_index];
198  FFStream *const sti = ffstream(st);
199 
200  if (ifv->next_video_index < sti->nb_index_entries)
201  e_next = ev = &sti->index_entries[ifv->next_video_index];
202  }
203 
204  if (ifv->is_audio_present &&
205  ifv->next_audio_index < ifv->total_aframes) {
206  AVStream *const st = s->streams[ifv->audio_stream_index];
207  FFStream *const sti = ffstream(st);
208 
209  if (ifv->next_audio_index < sti->nb_index_entries) {
210  ea = &sti->index_entries[ifv->next_audio_index];
211  if (!ev || ea->timestamp < ev->timestamp)
212  e_next = ea;
213  }
214  }
215 
216  if (!ev) {
217  uint64_t vframes, aframes;
218  if (ifv->is_audio_present && !ea) {
219  /*read new video and audio indexes*/
220 
221  ifv->next_video_index = ifv->total_vframes;
222  ifv->next_audio_index = ifv->total_aframes;
223 
224  avio_skip(s->pb, 0x1c);
225  vframes = ifv->total_vframes + (uint64_t)avio_rl32(s->pb);
226  aframes = ifv->total_aframes + (uint64_t)avio_rl32(s->pb);
227  if (vframes > INT_MAX || aframes > INT_MAX)
228  return AVERROR_INVALIDDATA;
229  ifv->total_vframes = vframes;
230  ifv->total_aframes = aframes;
231  avio_skip(s->pb, 0xc);
232 
233  if (avio_feof(s->pb))
234  return AVERROR_EOF;
235 
237  if (ret < 0)
238  return ret;
239 
241  if (ret < 0)
242  return ret;
243 
244  return 0;
245 
246  } else if (!ifv->is_audio_present) {
247  /*read new video index*/
248 
249  ifv->next_video_index = ifv->total_vframes;
250 
251  avio_skip(s->pb, 0x1c);
252  vframes = ifv->total_vframes + (uint64_t)avio_rl32(s->pb);
253  if (vframes > INT_MAX)
254  return AVERROR_INVALIDDATA;
255  ifv->total_vframes = vframes;
256  avio_skip(s->pb, 0x10);
257 
258  if (avio_feof(s->pb))
259  return AVERROR_EOF;
260 
262  if (ret < 0)
263  return ret;
264 
265  return 0;
266  }
267  }
268 
269  if (!e_next) return AVERROR_EOF;
270 
271  avio_seek(s->pb, e_next->pos, SEEK_SET);
272  ret = av_get_packet(s->pb, pkt, e_next->size);
273  if (ret < 0)
274  return ret;
275 
276  if (e_next == ev) {
277  ifv->next_video_index++;
279  } else {
280  ifv->next_audio_index++;
282  }
283 
284  pkt->pts = e_next->timestamp;
285  pkt->pos = e_next->pos;
286 
287  return 0;
288 }
289 
290 static int ifv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
291 {
292  IFVContext *ifv = s->priv_data;
293 
294  for (unsigned i = 0; i < s->nb_streams; i++) {
295  int index = av_index_search_timestamp(s->streams[i], ts, AVSEEK_FLAG_ANY);
296  if (index < 0) {
297  ifv->next_video_index = ifv->total_vframes - 1;
298  ifv->next_audio_index = ifv->total_aframes - 1;
299  return 0;
300  }
301 
302  if (i == ifv->video_stream_index) {
303  ifv->next_video_index = index;
304  } else {
305  ifv->next_audio_index = index;
306  }
307  }
308 
309  return 0;
310 }
311 
313  .name = "ifv",
314  .long_name = NULL_IF_CONFIG_SMALL("IFV CCTV DVR"),
315  .priv_data_size = sizeof(IFVContext),
316  .extensions = "ifv",
321 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:724
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
read_index
static int read_index(AVFormatContext *s, enum AVMediaType frame_type, uint32_t start_index)
Definition: ifv.c:53
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
IFVContext::video_stream_index
int video_stream_index
Definition: ifv.c:38
IFVContext::audio_stream_index
int audio_stream_index
Definition: ifv.c:39
index
fg index
Definition: ffmpeg_filter.c:168
IFVContext::next_audio_index
uint32_t next_audio_index
Definition: ifv.c:30
IFVContext::total_vframes
uint32_t total_vframes
Definition: ifv.c:31
AVIndexEntry
Definition: avformat.h:777
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:426
IFVContext::next_video_index
uint32_t next_video_index
Definition: ifv.c:29
AVSEEK_FLAG_ANY
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2153
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:149
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:117
IFVContext::is_audio_present
int is_audio_present
Definition: ifv.c:35
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:726
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:626
s
#define s(width, name)
Definition: cbs_vp9.c:257
parse_header
static int parse_header(AVFormatContext *s)
Definition: ifv.c:90
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:631
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AVIndexEntry::size
int size
Definition: avformat.h:788
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:779
ifv_read_seek
static int ifv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: ifv.c:290
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:77
ifv_read_packet
static int ifv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: ifv.c:188
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:992
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
ifv_probe
static int ifv_probe(const AVProbeData *p)
Definition: ifv.c:42
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:273
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:742
AVMediaType
AVMediaType
Definition: avutil.h:199
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:117
FFStream
Definition: internal.h:188
size
int size
Definition: twinvq_data.h:10344
ifv_read_header
static int ifv_read_header(AVFormatContext *s)
Definition: ifv.c:128
i
int i
Definition: input.c:406
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
frame_type
frame_type
Definition: jpeg2000_parser.c:31
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
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:196
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:845
IFVContext
Definition: ifv.c:28
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
IFVContext::width
int width
Definition: ifv.c:34
ff_ifv_demuxer
const AVInputFormat ff_ifv_demuxer
Definition: ifv.c:312
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:846
channel_layout.h
IFVContext::total_aframes
uint32_t total_aframes
Definition: ifv.c:32
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:778
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: utils.c:1146
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:333
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:271
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
IFVContext::sample_rate
int sample_rate
Definition: ifv.c:36
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:393
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:878
avpriv_dict_set_timestamp
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:258
IFVContext::height
int height
Definition: ifv.c:34
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:237
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:360