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;
42 int64_t last_good_pos;
45 int video_stream_index;
46 int audio_stream_index;
49 typedef struct DHAVStream {
50 int64_t last_timestamp;
55 static int dhav_probe(const AVProbeData *p)
57 if (!memcmp(p->buf, "DAHUA", 5))
58 return AVPROBE_SCORE_MAX;
60 if (memcmp(p->buf, "DHAV", 4))
63 if (p->buf[4] == 0xf0 ||
67 return AVPROBE_SCORE_MAX;
71 static const uint32_t sample_rates[] = {
72 8000, 4000, 8000, 11025, 16000,
73 20000, 22050, 32000, 44100, 48000,
77 static int parse_ext(AVFormatContext *s, int length)
79 DHAVContext *dhav = s->priv_data;
83 int type = avio_r8(s->pb);
87 ret = avio_skip(s->pb, 1);
88 dhav->width = 8 * avio_r8(s->pb);
89 dhav->height = 8 * avio_r8(s->pb);
93 ret = avio_skip(s->pb, 1);
94 dhav->video_codec = avio_r8(s->pb);
95 dhav->frame_rate = avio_r8(s->pb);
99 ret = avio_skip(s->pb, 3);
100 dhav->width = avio_rl16(s->pb);
101 dhav->height = avio_rl16(s->pb);
105 dhav->audio_channels = avio_r8(s->pb);
106 dhav->audio_codec = avio_r8(s->pb);
107 index = avio_r8(s->pb);
108 if (index < FF_ARRAY_ELEMS(sample_rates)) {
109 dhav->sample_rate = sample_rates[index];
111 dhav->sample_rate = 8000;
116 ret = avio_skip(s->pb, 7);
120 ret = avio_skip(s->pb, 1);
121 dhav->audio_channels = avio_r8(s->pb);
122 dhav->audio_codec = avio_r8(s->pb);
123 index = avio_r8(s->pb);
124 if (index < FF_ARRAY_ELEMS(sample_rates)) {
125 dhav->sample_rate = sample_rates[index];
127 dhav->sample_rate = 8000;
129 ret = avio_skip(s->pb, 3);
137 case 0x9b: // sample aspect ratio
139 ret = avio_skip(s->pb, 7);
150 ret = avio_skip(s->pb, 3);
154 av_log(s, AV_LOG_INFO, "Unknown type: %X, skipping rest of header.\n", type);
155 ret = avio_skip(s->pb, length - 1);
166 static int read_chunk(AVFormatContext *s)
168 DHAVContext *dhav = s->priv_data;
169 int frame_length, ext_length;
173 if (avio_feof(s->pb))
176 while (avio_r8(s->pb) != 'D' || avio_r8(s->pb) != 'H' || avio_r8(s->pb) != 'A' || avio_r8(s->pb) != 'V') {
177 if (avio_feof(s->pb))
181 start = avio_tell(s->pb) - 4;
182 dhav->last_good_pos = start;
183 dhav->type = avio_r8(s->pb);
184 dhav->subtype = avio_r8(s->pb);
185 dhav->channel = avio_r8(s->pb);
186 dhav->frame_subnumber = avio_r8(s->pb);
187 dhav->frame_number = avio_rl32(s->pb);
188 frame_length = avio_rl32(s->pb);
189 dhav->date = avio_rl32(s->pb);
191 if (frame_length < 24)
192 return AVERROR_INVALIDDATA;
193 if (dhav->type == 0xf1) {
194 ret = avio_skip(s->pb, frame_length - 20);
195 return ret < 0 ? ret : 0;
198 dhav->timestamp = avio_rl16(s->pb);
199 ext_length = avio_r8(s->pb);
200 avio_skip(s->pb, 1); // checksum
202 ret = parse_ext(s, ext_length);
206 end = avio_tell(s->pb);
208 return frame_length - 8 - (end - start);
211 static void get_timeinfo(unsigned date, struct tm *timeinfo)
213 int year, month, day, hour, min, sec;
216 min = (date >> 6) & 0x3F;
217 hour = (date >> 12) & 0x1F;
218 day = (date >> 17) & 0x1F;
219 month = (date >> 22) & 0x0F;
220 year = ((date >> 26) & 0x3F) + 2000;
222 timeinfo->tm_year = year - 1900;
223 timeinfo->tm_mon = month - 1;
224 timeinfo->tm_mday = day;
225 timeinfo->tm_hour = hour;
226 timeinfo->tm_min = min;
227 timeinfo->tm_sec = sec;
230 static int64_t get_duration(AVFormatContext *s)
232 DHAVContext *dhav = s->priv_data;
233 int64_t start_pos = avio_tell(s->pb);
234 int64_t start = 0, end = 0;
237 if (!s->pb->seekable)
240 avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
241 if (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
242 int seek_back = avio_rl32(s->pb);
244 avio_seek(s->pb, -seek_back, SEEK_CUR);
246 get_timeinfo(dhav->date, &timeinfo);
247 end = av_timegm(&timeinfo) * 1000LL;
249 avio_seek(s->pb, start_pos, SEEK_SET);
253 avio_seek(s->pb, start_pos, SEEK_SET);
256 get_timeinfo(dhav->date, &timeinfo);
257 start = av_timegm(&timeinfo) * 1000LL;
259 avio_seek(s->pb, start_pos, SEEK_SET);
264 static int dhav_read_header(AVFormatContext *s)
266 DHAVContext *dhav = s->priv_data;
267 uint8_t signature[5];
269 ffio_ensure_seekback(s->pb, 5);
270 avio_read(s->pb, signature, sizeof(signature));
271 if (!memcmp(signature, "DAHUA", 5)) {
272 avio_skip(s->pb, 0x400 - 5);
273 dhav->last_good_pos = avio_tell(s->pb);
275 if (!memcmp(signature, "DHAV", 4)) {
276 avio_seek(s->pb, -5, SEEK_CUR);
277 dhav->last_good_pos = avio_tell(s->pb);
278 } else if (s->pb->seekable) {
279 avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
280 while (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
283 seek_back = avio_rl32(s->pb) + 8;
286 dhav->last_good_pos = avio_tell(s->pb);
287 avio_seek(s->pb, -seek_back, SEEK_CUR);
289 avio_seek(s->pb, dhav->last_good_pos, SEEK_SET);
293 dhav->duration = get_duration(s);
294 dhav->last_good_pos = avio_tell(s->pb);
295 s->ctx_flags |= AVFMTCTX_NOHEADER;
296 dhav->video_stream_index = -1;
297 dhav->audio_stream_index = -1;
302 static int64_t get_pts(AVFormatContext *s, int stream_index)
304 DHAVStream *dst = s->streams[stream_index]->priv_data;
305 DHAVContext *dhav = s->priv_data;
309 get_timeinfo(dhav->date, &timeinfo);
311 t = av_timegm(&timeinfo);
312 if (dst->last_time == t) {
313 int64_t diff = dhav->timestamp - dst->last_timestamp;
319 dst->pts = t * 1000LL;
323 dst->last_timestamp = dhav->timestamp;
328 static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
330 DHAVContext *dhav = s->priv_data;
331 int size, ret, stream_index;
334 while ((ret = read_chunk(s)) == 0)
340 if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
341 AVStream *st = avformat_new_stream(s, NULL);
345 return AVERROR(ENOMEM);
347 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
348 switch (dhav->video_codec) {
349 case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
350 case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
353 case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264; break;
354 case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC; break;
355 default: avpriv_request_sample(s, "Unknown video codec %X", dhav->video_codec);
357 st->duration = dhav->duration;
358 st->codecpar->width = dhav->width;
359 st->codecpar->height = dhav->height;
360 st->avg_frame_rate.num = dhav->frame_rate;
361 st->avg_frame_rate.den = 1;
362 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
364 return AVERROR(ENOMEM);
365 dst->last_time = AV_NOPTS_VALUE;
366 dhav->video_stream_index = st->index;
368 avpriv_set_pts_info(st, 64, 1, 1000);
369 } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
370 AVStream *st = avformat_new_stream(s, NULL);
374 return AVERROR(ENOMEM);
376 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
377 switch (dhav->audio_codec) {
378 case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8; break;
379 case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
380 case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
381 case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
382 case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
383 case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; break;
384 case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC; break;
385 case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
386 case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
387 case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS; break;
388 default: avpriv_request_sample(s, "Unknown audio codec %X", dhav->audio_codec);
390 st->duration = dhav->duration;
391 st->codecpar->channels = dhav->audio_channels;
392 st->codecpar->sample_rate = dhav->sample_rate;
393 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
395 return AVERROR(ENOMEM);
396 dst->last_time = AV_NOPTS_VALUE;
397 dhav->audio_stream_index = st->index;
399 avpriv_set_pts_info(st, 64, 1, 1000);
402 stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
403 if (stream_index < 0) {
404 avio_skip(s->pb, ret);
405 if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
411 ret = av_get_packet(s->pb, pkt, size);
414 pkt->stream_index = stream_index;
415 if (dhav->type != 0xfc)
416 pkt->flags |= AV_PKT_FLAG_KEY;
418 if (pkt->stream_index >= 0)
419 pkt->pts = get_pts(s, pkt->stream_index);
420 pkt->pos = dhav->last_good_pos;
421 if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
427 static int dhav_read_seek(AVFormatContext *s, int stream_index,
428 int64_t timestamp, int flags)
430 DHAVContext *dhav = s->priv_data;
431 AVStream *st = s->streams[stream_index];
432 int index = av_index_search_timestamp(st, timestamp, flags);
437 if (avio_seek(s->pb, st->internal->index_entries[index].pos, SEEK_SET) < 0)
440 pts = st->internal->index_entries[index].timestamp;
442 for (int n = 0; n < s->nb_streams; n++) {
443 AVStream *st = s->streams[n];
444 DHAVStream *dst = st->priv_data;
447 dst->last_time = AV_NOPTS_VALUE;
449 dhav->last_good_pos = avio_tell(s->pb);
454 const AVInputFormat ff_dhav_demuxer = {
456 .long_name = NULL_IF_CONFIG_SMALL("Video DAV"),
457 .priv_data_size = sizeof(DHAVContext),
458 .read_probe = dhav_probe,
459 .read_header = dhav_read_header,
460 .read_packet = dhav_read_packet,
461 .read_seek = dhav_read_seek,
463 .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_TS_DISCONT | AVFMT_TS_NONSTRICT,