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;
44 int video_stream_index;
45 int audio_stream_index;
48 typedef struct DHAVStream {
49 int64_t last_timestamp;
53 static int dhav_probe(const AVProbeData *p)
55 if (!memcmp(p->buf, "DAHUA", 5))
56 return AVPROBE_SCORE_MAX;
58 if (memcmp(p->buf, "DHAV", 4))
61 if (p->buf[4] == 0xf0 ||
65 return AVPROBE_SCORE_MAX;
69 static int dhav_read_header(AVFormatContext *s)
71 DHAVContext *dhav = s->priv_data;
74 ffio_ensure_seekback(s->pb, 5);
75 avio_read(s->pb, signature, sizeof(signature));
76 if (!memcmp(signature, "DAHUA", 5)) {
77 avio_skip(s->pb, 0x400 - 5);
78 dhav->last_good_pos = avio_tell(s->pb);
80 if (!memcmp(signature, "DHAV", 4)) {
81 avio_seek(s->pb, -5, SEEK_CUR);
82 dhav->last_good_pos = avio_tell(s->pb);
83 } else if (s->pb->seekable) {
84 avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
85 while (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
88 seek_back = avio_rl32(s->pb) + 8;
89 dhav->last_good_pos = avio_tell(s->pb);
90 if (dhav->last_good_pos < seek_back)
92 avio_seek(s->pb, -seek_back, SEEK_CUR);
97 s->ctx_flags |= AVFMTCTX_NOHEADER;
98 dhav->video_stream_index = -1;
99 dhav->audio_stream_index = -1;
104 static int64_t get_pts(AVFormatContext *s, DHAVStream *st)
106 DHAVContext *dhav = s->priv_data;
108 int year, month, day, hour, min, sec;
111 sec = dhav->date & 0x3F;
112 min = (dhav->date >> 6) & 0x3F;
113 hour = (dhav->date >> 12) & 0x1F;
114 day = (dhav->date >> 17) & 0x1F;
115 month = (dhav->date >> 22) & 0x0F;
116 year = ((dhav->date >> 26) & 0x3F) + 2000;
118 timeinfo.tm_year = year - 1900;
119 timeinfo.tm_mon = month - 1;
120 timeinfo.tm_mday = day;
121 timeinfo.tm_hour = hour;
122 timeinfo.tm_min = min;
123 timeinfo.tm_sec = sec;*/
125 if (st->last_timestamp == AV_NOPTS_VALUE) {
126 st->last_timestamp = dhav->timestamp;
129 if (st->last_timestamp <= dhav->timestamp) {
130 st->pts += dhav->timestamp - st->last_timestamp;
132 st->pts += 65535 + dhav->timestamp - st->last_timestamp;
135 st->last_timestamp = dhav->timestamp;
140 static const uint32_t sample_rates[] = {
141 8000, 4000, 8000, 11025, 16000,
142 20000, 22050, 32000, 44100, 48000,
143 96000, 192000, 64000,
146 static int parse_ext(AVFormatContext *s, int length)
148 DHAVContext *dhav = s->priv_data;
152 int type = avio_r8(s->pb);
156 ret = avio_skip(s->pb, 1);
157 dhav->width = 8 * avio_r8(s->pb);
158 dhav->height = 8 * avio_r8(s->pb);
162 ret = avio_skip(s->pb, 1);
163 dhav->video_codec = avio_r8(s->pb);
164 dhav->frame_rate = avio_r8(s->pb);
168 ret = avio_skip(s->pb, 3);
169 dhav->width = avio_rl16(s->pb);
170 dhav->height = avio_rl16(s->pb);
174 dhav->audio_channels = avio_r8(s->pb);
175 dhav->audio_codec = avio_r8(s->pb);
176 index = avio_r8(s->pb);
177 if (index < FF_ARRAY_ELEMS(sample_rates)) {
178 dhav->sample_rate = sample_rates[index];
180 dhav->sample_rate = 8000;
185 ret = avio_skip(s->pb, 7);
189 ret = avio_skip(s->pb, 1);
190 dhav->audio_channels = avio_r8(s->pb);
191 dhav->audio_codec = avio_r8(s->pb);
192 index = avio_r8(s->pb);
193 if (index < FF_ARRAY_ELEMS(sample_rates)) {
194 dhav->sample_rate = sample_rates[index];
196 dhav->sample_rate = 8000;
198 ret = avio_skip(s->pb, 3);
206 case 0x9b: // sample aspect ratio
208 ret = avio_skip(s->pb, 7);
219 ret = avio_skip(s->pb, 3);
223 av_log(s, AV_LOG_INFO, "Unknown type: %X, skipping rest of header.\n", type);
224 ret = avio_skip(s->pb, length - 1);
235 static int read_chunk(AVFormatContext *s)
237 DHAVContext *dhav = s->priv_data;
238 int frame_length, ext_length;
242 if (avio_feof(s->pb))
245 if (avio_rl32(s->pb) != MKTAG('D','H','A','V')) {
246 dhav->last_good_pos += 0x8000;
247 avio_seek(s->pb, dhav->last_good_pos, SEEK_SET);
249 while (avio_rl32(s->pb) != MKTAG('D','H','A','V')) {
250 if (avio_feof(s->pb))
252 dhav->last_good_pos += 0x8000;
253 ret = avio_skip(s->pb, 0x8000 - 4);
259 start = avio_tell(s->pb) - 4;
260 dhav->last_good_pos = start;
261 dhav->type = avio_r8(s->pb);
262 dhav->subtype = avio_r8(s->pb);
263 dhav->channel = avio_r8(s->pb);
264 dhav->frame_subnumber = avio_r8(s->pb);
265 dhav->frame_number = avio_rl32(s->pb);
266 frame_length = avio_rl32(s->pb);
268 if (frame_length < 24)
269 return AVERROR_INVALIDDATA;
270 if (dhav->type == 0xf1) {
271 ret = avio_skip(s->pb, frame_length - 16);
272 return ret < 0 ? ret : 0;
275 dhav->date = avio_rl32(s->pb);
276 dhav->timestamp = avio_rl16(s->pb);
277 ext_length = avio_r8(s->pb);
278 avio_skip(s->pb, 1); // checksum
280 ret = parse_ext(s, ext_length);
284 end = avio_tell(s->pb);
286 return frame_length - 8 - (end - start);
289 static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
291 DHAVContext *dhav = s->priv_data;
292 int ret, stream_index;
295 while ((ret = read_chunk(s)) == 0)
301 if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
302 AVStream *st = avformat_new_stream(s, NULL);
306 return AVERROR(ENOMEM);
308 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
309 switch (dhav->video_codec) {
310 case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
311 case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
314 case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264; break;
315 case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC; break;
316 default: avpriv_request_sample(s, "Unknown video codec %X\n", dhav->video_codec);
318 st->codecpar->width = dhav->width;
319 st->codecpar->height = dhav->height;
320 st->avg_frame_rate.num = dhav->frame_rate;
321 st->avg_frame_rate.den = 1;
322 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
324 return AVERROR(ENOMEM);
325 dst->last_timestamp = AV_NOPTS_VALUE;
326 dhav->video_stream_index = st->index;
328 avpriv_set_pts_info(st, 64, 1, 1000);
329 } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
330 AVStream *st = avformat_new_stream(s, NULL);
334 return AVERROR(ENOMEM);
336 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
337 switch (dhav->audio_codec) {
338 case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8; break;
339 case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
340 case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
341 case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
342 case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
343 case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; break;
344 case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC; break;
345 case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
346 case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
347 case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS; break;
348 default: avpriv_request_sample(s, "Unknown audio codec %X\n", dhav->audio_codec);
350 st->codecpar->channels = dhav->audio_channels;
351 st->codecpar->sample_rate = dhav->sample_rate;
352 st->priv_data = dst = av_mallocz(sizeof(DHAVStream));
354 return AVERROR(ENOMEM);
355 dst->last_timestamp = AV_NOPTS_VALUE;
356 dhav->audio_stream_index = st->index;
358 avpriv_set_pts_info(st, 64, 1, 1000);
361 stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
362 if (stream_index < 0) {
363 avio_skip(s->pb, ret);
364 if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
369 ret = av_get_packet(s->pb, pkt, ret);
372 pkt->stream_index = stream_index;
373 if (dhav->type != 0xfc)
374 pkt->flags |= AV_PKT_FLAG_KEY;
375 if (pkt->stream_index >= 0)
376 pkt->pts = get_pts(s, s->streams[pkt->stream_index]->priv_data);
378 pkt->pos = dhav->last_good_pos;
379 if (avio_rl32(s->pb) == MKTAG('d','h','a','v'))
385 static int dhav_read_seek(AVFormatContext *s, int stream_index,
386 int64_t timestamp, int flags)
388 DHAVContext *dhav = s->priv_data;
389 AVStream *st = s->streams[stream_index];
390 int index = av_index_search_timestamp(st, timestamp, flags);
395 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
398 pts = st->index_entries[index].timestamp;
400 for (int n = 0; n < s->nb_streams; n++) {
401 AVStream *st = s->streams[n];
402 DHAVStream *dst = st->priv_data;
405 dst->last_timestamp = AV_NOPTS_VALUE;
407 dhav->last_good_pos = avio_tell(s->pb);
412 AVInputFormat ff_dhav_demuxer = {
414 .long_name = NULL_IF_CONFIG_SMALL("Video DAV"),
415 .priv_data_size = sizeof(DHAVContext),
416 .read_probe = dhav_probe,
417 .read_header = dhav_read_header,
418 .read_packet = dhav_read_packet,
419 .read_seek = dhav_read_seek,
421 .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,