4 * Copyright (c) 2019 Swaraj Hota
6 * This file is part of FFmpeg.
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.
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.
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
25 #include "avio_internal.h"
27 typedef struct IFVContext {
28 uint32_t next_video_index;
29 uint32_t next_audio_index;
30 uint32_t total_vframes;
31 uint32_t total_aframes;
37 int video_stream_index;
38 int audio_stream_index;
41 static int ifv_probe(const AVProbeData *p)
43 static const uint8_t ifv_magic[] = {0x11, 0xd2, 0xd3, 0xab, 0xba, 0xa9,
44 0xcf, 0x11, 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65, 0x44};
46 if (!memcmp(p->buf, ifv_magic, sizeof(ifv_magic)))
47 return AVPROBE_SCORE_MAX;
52 static int read_index(AVFormatContext *s,
53 enum AVMediaType frame_type,
56 IFVContext *ifv = s->priv_data;
58 int64_t pos, size, timestamp;
59 uint32_t end_index, i;
62 if (frame_type == AVMEDIA_TYPE_VIDEO) {
63 end_index = ifv->total_vframes;
64 st = s->streams[ifv->video_stream_index];
66 end_index = ifv->total_aframes;
67 st = s->streams[ifv->audio_stream_index];
70 for (i = start_index; i < end_index; i++) {
71 pos = avio_rl32(s->pb);
72 size = avio_rl32(s->pb);
75 timestamp = avio_rl32(s->pb);
77 ret = av_add_index_entry(st, pos, timestamp, size, 0, 0);
81 avio_skip(s->pb, frame_type == AVMEDIA_TYPE_VIDEO ? 8: 4);
87 static int parse_header(AVFormatContext *s)
89 IFVContext *ifv = s->priv_data;
93 avio_skip(s->pb, 0x34);
94 avpriv_dict_set_timestamp(&s->metadata, "creation_time", avio_rl32(s->pb) * 1000000LL);
95 avio_skip(s->pb, 0x24);
97 ifv->width = avio_rl16(s->pb);
98 ifv->height = avio_rl16(s->pb);
100 avio_skip(s->pb, 0x8);
101 vid_magic = avio_rl32(s->pb);
103 if (vid_magic != MKTAG('H','2','6','4'))
104 avpriv_request_sample(s, "Unknown video codec %x", vid_magic);
106 avio_skip(s->pb, 0x2c);
107 ifv->sample_rate = avio_rl32(s->pb);
108 aud_magic = avio_rl32(s->pb);
110 if (aud_magic == MKTAG('G','R','A','W')) {
111 ifv->is_audio_present = 1;
112 } else if (aud_magic == MKTAG('P','C','M','U')) {
113 ifv->is_audio_present = 0;
115 avpriv_request_sample(s, "Unknown audio codec %x", aud_magic);
118 avio_skip(s->pb, 0x44);
119 ifv->total_vframes = avio_rl32(s->pb);
120 ifv->total_aframes = avio_rl32(s->pb);
125 static int ifv_read_header(AVFormatContext *s)
127 IFVContext *ifv = s->priv_data;
131 ret = parse_header(s);
135 st = avformat_new_stream(s, NULL);
137 return AVERROR(ENOMEM);
139 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
140 st->codecpar->codec_id = AV_CODEC_ID_H264;
141 st->codecpar->width = ifv->width;
142 st->codecpar->height = ifv->height;
144 ifv->video_stream_index = st->index;
146 avpriv_set_pts_info(st, 32, 1, 1000);
148 if (ifv->is_audio_present) {
149 st = avformat_new_stream(s, NULL);
151 return AVERROR(ENOMEM);
153 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
154 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
155 st->codecpar->channels = 1;
156 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
157 st->codecpar->sample_rate = ifv->sample_rate;
158 ifv->audio_stream_index = st->index;
160 avpriv_set_pts_info(st, 32, 1, 1000);
164 avio_seek(s->pb, 0xf8, SEEK_SET);
166 ret = read_index(s, AVMEDIA_TYPE_VIDEO, 0);
170 if (ifv->is_audio_present) {
172 avio_seek(s->pb, 0x14918, SEEK_SET);
174 ret = read_index(s, AVMEDIA_TYPE_AUDIO, 0);
179 ifv->next_video_index = 0;
180 ifv->next_audio_index = 0;
185 static int ifv_read_packet(AVFormatContext *s, AVPacket *pkt)
187 IFVContext *ifv = s->priv_data;
189 AVIndexEntry *ev, *ea, *e_next;
192 ev = ea = e_next = NULL;
194 if (ifv->next_video_index < ifv->total_vframes) {
195 st = s->streams[ifv->video_stream_index];
196 if (ifv->next_video_index < st->nb_index_entries)
197 e_next = ev = &st->index_entries[ifv->next_video_index];
200 if (ifv->is_audio_present &&
201 ifv->next_audio_index < ifv->total_aframes) {
202 st = s->streams[ifv->audio_stream_index];
203 if (ifv->next_audio_index < st->nb_index_entries) {
204 ea = &st->index_entries[ifv->next_audio_index];
205 if (!ev || ea->timestamp < ev->timestamp)
211 if (ifv->is_audio_present && !ea) {
212 /*read new video and audio indexes*/
214 ifv->next_video_index = ifv->total_vframes;
215 ifv->next_audio_index = ifv->total_aframes;
217 avio_skip(s->pb, 0x1c);
218 ifv->total_vframes += avio_rl32(s->pb);
219 ifv->total_aframes += avio_rl32(s->pb);
220 avio_skip(s->pb, 0xc);
222 if (avio_feof(s->pb))
225 ret = read_index(s, AVMEDIA_TYPE_VIDEO, ifv->next_video_index);
229 ret = read_index(s, AVMEDIA_TYPE_AUDIO, ifv->next_audio_index);
235 } else if (!ifv->is_audio_present) {
236 /*read new video index*/
238 ifv->next_video_index = ifv->total_vframes;
240 avio_skip(s->pb, 0x1c);
241 ifv->total_vframes += avio_rl32(s->pb);
242 avio_skip(s->pb, 0x10);
244 if (avio_feof(s->pb))
247 ret = read_index(s, AVMEDIA_TYPE_VIDEO, ifv->next_video_index);
255 if (!e_next) return AVERROR_EOF;
257 avio_seek(s->pb, e_next->pos, SEEK_SET);
258 ret = av_get_packet(s->pb, pkt, e_next->size);
263 ifv->next_video_index++;
264 pkt->stream_index = ifv->video_stream_index;
266 ifv->next_audio_index++;
267 pkt->stream_index = ifv->audio_stream_index;
270 pkt->pts = e_next->timestamp;
271 pkt->pos = e_next->pos;
276 static int ifv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
278 IFVContext *ifv = s->priv_data;
280 for (unsigned i = 0; i < s->nb_streams; i++) {
281 int index = av_index_search_timestamp(s->streams[i], ts, AVSEEK_FLAG_ANY);
283 ifv->next_video_index = ifv->total_vframes - 1;
284 ifv->next_audio_index = ifv->total_aframes - 1;
288 if (i == ifv->video_stream_index) {
289 ifv->next_video_index = index;
291 ifv->next_audio_index = index;
298 AVInputFormat ff_ifv_demuxer = {
300 .long_name = NULL_IF_CONFIG_SMALL("IFV CCTV DVR"),
301 .priv_data_size = sizeof(IFVContext),
303 .read_probe = ifv_probe,
304 .read_header = ifv_read_header,
305 .read_packet = ifv_read_packet,
306 .read_seek = ifv_read_seek,