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/avassert.h"
24 #include "avio_internal.h"
28 typedef struct DHAVContext {
38 int video_stream_index;
39 int audio_stream_index;
42 static int dhav_probe(AVProbeData *p)
44 if (!memcmp(p->buf, "DAHUA", 5))
45 return AVPROBE_SCORE_MAX;
47 if (memcmp(p->buf, "DHAV", 4))
50 if (p->buf[4] == 0xf0 ||
54 return AVPROBE_SCORE_MAX;
58 static int dhav_read_header(AVFormatContext *s)
60 DHAVContext *dhav = s->priv_data;
63 ffio_ensure_seekback(s->pb, 5);
64 avio_read(s->pb, signature, sizeof(signature));
65 if (!memcmp(signature, "DAHUA", 5))
66 avio_skip(s->pb, 0x400 - 5);
68 avio_seek(s->pb, -5, SEEK_CUR);
70 s->ctx_flags |= AVFMTCTX_NOHEADER;
71 dhav->video_stream_index = -1;
72 dhav->audio_stream_index = -1;
77 static const uint32_t sample_rates[] = {
78 8000, 4000, 8000, 11025, 16000,
79 20000, 22050, 32000, 44100, 48000,
83 static int parse_ext(AVFormatContext *s, int length)
85 DHAVContext *dhav = s->priv_data;
89 int type = avio_r8(s->pb);
94 dhav->width = 8 * avio_r8(s->pb);
95 dhav->height = 8 * avio_r8(s->pb);
100 dhav->video_codec = avio_r8(s->pb);
101 dhav->frame_rate = avio_r8(s->pb);
106 dhav->width = avio_rl16(s->pb);
107 dhav->height = avio_rl16(s->pb);
111 dhav->channels = avio_r8(s->pb);
112 dhav->audio_codec = avio_r8(s->pb);
113 index = avio_r8(s->pb);
114 if (index < FF_ARRAY_ELEMS(sample_rates)) {
115 dhav->sample_rate = sample_rates[index];
117 dhav->sample_rate = 8000;
127 dhav->channels = avio_r8(s->pb);
128 dhav->audio_codec = avio_r8(s->pb);
129 index = avio_r8(s->pb);
130 if (index < FF_ARRAY_ELEMS(sample_rates)) {
131 dhav->sample_rate = sample_rates[index];
133 dhav->sample_rate = 8000;
143 case 0x9b: // sample aspect ratio
160 av_log(s, AV_LOG_INFO, "Unknown type: %X, skipping rest of header.\n", type);
161 avio_skip(s->pb, length - 1);
169 static int read_chunk(AVFormatContext *s)
171 DHAVContext *dhav = s->priv_data;
176 start = avio_tell(s->pb);
178 if (avio_feof(s->pb))
181 if (avio_rl32(s->pb) != MKTAG('D','H','A','V'))
182 return AVERROR_INVALIDDATA;
184 dhav->type = avio_r8(s->pb);
186 dhav->pts = avio_rl32(s->pb);
187 size = avio_rl32(s->pb);
189 return AVERROR_INVALIDDATA;
190 if (dhav->type == 0xf1) {
191 avio_skip(s->pb, size - 16);
197 skip = avio_r8(s->pb);
200 ret = parse_ext(s, skip);
204 end = avio_tell(s->pb);
206 return size - 8 - (end - start);
209 static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
211 DHAVContext *dhav = s->priv_data;
215 start = avio_tell(s->pb);
217 while ((ret = read_chunk(s)) == 0)
223 if (dhav->type == 0xfd && dhav->video_stream_index == -1) {
224 AVStream *st = avformat_new_stream(s, NULL);
227 return AVERROR(ENOMEM);
229 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
230 switch (dhav->video_codec) {
231 case 0x1: st->codecpar->codec_id = AV_CODEC_ID_MPEG4; break;
232 case 0x3: st->codecpar->codec_id = AV_CODEC_ID_MJPEG; break;
235 case 0x8: st->codecpar->codec_id = AV_CODEC_ID_H264; break;
236 case 0xc: st->codecpar->codec_id = AV_CODEC_ID_HEVC; break;
237 default: avpriv_request_sample(s, "Unknown video codec %X\n", dhav->video_codec);
239 st->codecpar->width = dhav->width;
240 st->codecpar->height = dhav->height;
241 dhav->video_stream_index = st->index;
243 avpriv_set_pts_info(st, 64, 1, dhav->frame_rate);
244 } else if (dhav->type == 0xf0 && dhav->audio_stream_index == -1) {
245 AVStream *st = avformat_new_stream(s, NULL);
248 return AVERROR(ENOMEM);
250 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
251 switch (dhav->audio_codec) {
252 case 0x07: st->codecpar->codec_id = AV_CODEC_ID_PCM_S8; break;
253 case 0x0c: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
254 case 0x10: st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; break;
255 case 0x0a: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
256 case 0x16: st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; break;
257 case 0x0e: st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; break;
258 case 0x1a: st->codecpar->codec_id = AV_CODEC_ID_AAC; break;
259 case 0x1f: st->codecpar->codec_id = AV_CODEC_ID_MP2; break;
260 case 0x21: st->codecpar->codec_id = AV_CODEC_ID_MP3; break;
261 case 0x0d: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_MS; break;
262 default: avpriv_request_sample(s, "Unknown audio codec %X\n", dhav->audio_codec);
264 st->codecpar->channels = dhav->channels;
265 st->codecpar->sample_rate = dhav->sample_rate;
266 dhav->audio_stream_index = st->index;
268 avpriv_set_pts_info(st, 64, ret, dhav->sample_rate);
271 ret = av_get_packet(s->pb, pkt, ret);
274 pkt->stream_index = dhav->type == 0xf0 ? dhav->audio_stream_index : dhav->video_stream_index;
275 if (dhav->type != 0xfc)
276 pkt->flags |= AV_PKT_FLAG_KEY;
277 pkt->pts = dhav->pts;
280 if (avio_rl32(s->pb) != MKTAG('d','h','a','v'))
281 return AVERROR_INVALIDDATA;
287 AVInputFormat ff_dhav_demuxer = {
289 .long_name = NULL_IF_CONFIG_SMALL("Video DAV"),
290 .priv_data_size = sizeof(DHAVContext),
291 .read_probe = dhav_probe,
292 .read_header = dhav_read_header,
293 .read_packet = dhav_read_packet,
295 .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,