4 * Copyright (c) 2018 Paul B Mahol
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
23 #include "libavutil/parseutils.h"
24 #include "avio_internal.h"
28 typedef struct DHAVContext {
32 unsigned frame_subnumber;
33 unsigned frame_number;
43 int video_stream_index;
44 int audio_stream_index;
47 typedef struct DHAVStream {
48 int64_t last_timestamp;
52 static int dhav_probe(const AVProbeData *p)
54 if (!memcmp(p->buf, "DAHUA", 5))
55 return AVPROBE_SCORE_MAX;
57 if (memcmp(p->buf, "DHAV", 4))
60 if (p->buf[4] == 0xf0 ||
64 return AVPROBE_SCORE_MAX;
68 static int dhav_read_header(AVFormatContext *s)
70 DHAVContext *dhav = s->priv_data;
73 ffio_ensure_seekback(s->pb, 5);
74 avio_read(s->pb, signature, sizeof(signature));
75 if (!memcmp(signature, "DAHUA", 5))
76 avio_skip(s->pb, 0x400 - 5);
78 avio_seek(s->pb, -5, SEEK_CUR);
80 s->ctx_flags |= AVFMTCTX_NOHEADER;
81 dhav->video_stream_index = -1;
82 dhav->audio_stream_index = -1;
87 static int64_t get_pts(AVFormatContext *s, DHAVStream *st)
89 DHAVContext *dhav = s->priv_data;
91 int year, month, day, hour, min, sec;
94 sec = dhav->date & 0x3F;
95 min = (dhav->date >> 6) & 0x3F;
96 hour = (dhav->date >> 12) & 0x1F;
97 day = (dhav->date >> 17) & 0x1F;
98 month = (dhav->date >> 22) & 0x0F;
99 year = ((dhav->date >> 26) & 0x3F) + 2000;
101 timeinfo.tm_year = year - 1900;
102 timeinfo.tm_mon = month - 1;
103 timeinfo.tm_mday = day;
104 timeinfo.tm_hour = hour;
105 timeinfo.tm_min = min;
106 timeinfo.tm_sec = sec;*/
108 if (st->last_timestamp == AV_NOPTS_VALUE) {
109 st->last_timestamp = dhav->timestamp;
112 if (st->last_timestamp <= dhav->timestamp) {
113 st->pts += dhav->timestamp - st->last_timestamp;
115 st->pts += 65535 + dhav->timestamp - st->last_timestamp;
118 st->last_timestamp = dhav->timestamp;
123 static const uint32_t sample_rates[] = {
124 8000, 4000, 8000, 11025, 16000,
125 20000, 22050, 32000, 44100, 48000,
126 96000, 192000, 64000,
129 static int parse_ext(AVFormatContext *s, int length)
131 DHAVContext *dhav = s->priv_data;
135 int type = avio_r8(s->pb);
140 dhav->width = 8 * avio_r8(s->pb);
141 dhav->height = 8 * avio_r8(s->pb);
146 dhav->video_codec = avio_r8(s->pb);
147 dhav->frame_rate = avio_r8(s->pb);
152 dhav->width = avio_rl16(s->pb);
153 dhav->height = avio_rl16(s->pb);
157 dhav->audio_channels = avio_r8(s->pb);
158 dhav->audio_codec = avio_r8(s->pb);
159 index = avio_r8(s->pb);
160 if (index < FF_ARRAY_ELEMS(sample_rates)) {
161 dhav->sample_rate = sample_rates[index];
163 dhav->sample_rate = 8000;
173 dhav->audio_channels = avio_r8(s->pb);
174 dhav->audio_codec = avio_r8(s->pb);
175 index = avio_r8(s->pb);
176 if (index < FF_ARRAY_ELEMS(sample_rates)) {
177 dhav->sample_rate = sample_rates[index];
179 dhav->sample_rate = 8000;
189 case 0x9b: // sample aspect ratio
206 av_log(s, AV_LOG_INFO, "Unknown type: %X, skipping rest of header.\n", type);
207 avio_skip(s->pb, length - 1);
215 static int read_chunk(AVFormatContext *s)
217 DHAVContext *dhav = s->priv_data;
218 unsigned frame_length, ext_length;
222 start = avio_tell(s->pb);
224 if (avio_feof(s->pb))
227 if (avio_rl32(s->pb) != MKTAG('D','H','A','V'))
228 return AVERROR_INVALIDDATA;
230 dhav->type = avio_r8(s->pb);
231 dhav->subtype = avio_r8(s->pb);
232 dhav->channel = avio_r8(s->pb);
233 dhav->frame_subnumber = avio_r8(s->pb);
234 dhav->frame_number = avio_rl32(s->pb);
235 frame_length = avio_rl32(s->pb);
237 if (frame_length < 24)
238 return AVERROR_INVALIDDATA;
239 if (dhav->type == 0xf1) {
240 avio_skip(s->pb, frame_length - 16);
244 dhav->date = avio_rl32(s->pb);
245 dhav->timestamp = avio_rl16(s->pb);
246 ext_length = avio_r8(s->pb);
247 avio_skip(s->pb, 1); // checksum
249 ret = parse_ext(s, ext_length);
253 end = avio_tell(s->pb);
255 return frame_length - 8 - (end - start);
258 static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
260 DHAVContext *dhav = s->priv_data;
264 start = avio_tell(s->pb);
266 while ((ret = read_chunk(s)) == 0)
272 if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
273 AVStream *st = avformat_new_stream(s, NULL);
277 return AVERROR(ENOMEM);
279 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
280 switch (dhav->video_codec) {
281 case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
282 case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
285 case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264; break;
286 case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC; break;
287 default: avpriv_request_sample(s, "Unknown video codec %X\n", dhav->video_codec);
289 st->codecpar->width = dhav->width;
290 st->codecpar->height = dhav->height;
291 st->avg_frame_rate.num = dhav->frame_rate;
292 st->avg_frame_rate.den = 1;
293 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
295 return AVERROR(ENOMEM);
296 dst->last_timestamp = AV_NOPTS_VALUE;
297 dhav->video_stream_index = st->index;
299 avpriv_set_pts_info(st, 64, 1, 1000);
300 } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
301 AVStream *st = avformat_new_stream(s, NULL);
305 return AVERROR(ENOMEM);
307 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
308 switch (dhav->audio_codec) {
309 case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8; break;
310 case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
311 case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
312 case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
313 case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
314 case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; break;
315 case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC; break;
316 case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
317 case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
318 case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS; break;
319 default: avpriv_request_sample(s, "Unknown audio codec %X\n", dhav->audio_codec);
321 st->codecpar->channels = dhav->audio_channels;
322 st->codecpar->sample_rate = dhav->sample_rate;
323 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
325 return AVERROR(ENOMEM);
326 dst->last_timestamp = AV_NOPTS_VALUE;
327 dhav->audio_stream_index = st->index;
329 avpriv_set_pts_info(st, 64, 1, 1000);
332 ret = av_get_packet(s->pb, pkt, ret);
335 pkt->stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
336 if (dhav->type != 0xfc)
337 pkt->flags |= AV_PKT_FLAG_KEY;
338 if (pkt->stream_index >= 0)
339 pkt->pts = get_pts(s, s->streams[pkt->stream_index]->priv_data);
342 if (avio_rl32(s->pb) != MKTAG('d','h','a','v'))
343 return AVERROR_INVALIDDATA;
349 static int dhav_read_seek(AVFormatContext *s, int stream_index,
350 int64_t timestamp, int flags)
352 AVStream *st = s->streams[stream_index];
353 int index = av_index_search_timestamp(st, timestamp, flags);
358 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
361 pts = st->index_entries[index].timestamp;
363 for (int n = 0; n < s->nb_streams; n++) {
364 AVStream *st = s->streams[n];
365 DHAVStream *dst = st->priv_data;
368 dst->last_timestamp = AV_NOPTS_VALUE;
374 AVInputFormat ff_dhav_demuxer = {
376 .long_name = NULL_IF_CONFIG_SMALL("Video DAV"),
377 .priv_data_size = sizeof(DHAVContext),
378 .read_probe = dhav_probe,
379 .read_header = dhav_read_header,
380 .read_packet = dhav_read_packet,
381 .read_seek = dhav_read_seek,
383 .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,