[FFmpeg-devel] [PATCH] avformat: add DHAV demuxer

Paul B Mahol onemda at gmail.com
Thu Nov 22 22:24:23 EET 2018


Missing pts handling and correct codec detection.
More samples wanted.

Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/dhav.c       | 136 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+)
 create mode 100644 libavformat/dhav.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index e4d997c4a0..a6c5ea9aa3 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -149,6 +149,7 @@ OBJS-$(CONFIG_DAUD_DEMUXER)              += dauddec.o
 OBJS-$(CONFIG_DAUD_MUXER)                += daudenc.o
 OBJS-$(CONFIG_DCSTR_DEMUXER)             += dcstr.o
 OBJS-$(CONFIG_DFA_DEMUXER)               += dfa.o
+OBJS-$(CONFIG_DHAV_DEMUXER)              += dhav.o
 OBJS-$(CONFIG_DIRAC_DEMUXER)             += diracdec.o rawdec.o
 OBJS-$(CONFIG_DIRAC_MUXER)               += rawenc.o
 OBJS-$(CONFIG_DNXHD_DEMUXER)             += dnxhddec.o rawdec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 498077e1de..5fb5bf17c6 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -110,6 +110,7 @@ extern AVInputFormat  ff_daud_demuxer;
 extern AVOutputFormat ff_daud_muxer;
 extern AVInputFormat  ff_dcstr_demuxer;
 extern AVInputFormat  ff_dfa_demuxer;
+extern AVInputFormat  ff_dhav_demuxer;
 extern AVInputFormat  ff_dirac_demuxer;
 extern AVOutputFormat ff_dirac_muxer;
 extern AVInputFormat  ff_dnxhd_demuxer;
diff --git a/libavformat/dhav.c b/libavformat/dhav.c
new file mode 100644
index 0000000000..ad27949d0f
--- /dev/null
+++ b/libavformat/dhav.c
@@ -0,0 +1,136 @@
+/*
+ * DHAV demuxer
+ *
+ * Copyright (c) 2017 Paul B Mahol
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct DHAVContext {
+    unsigned type;
+    int64_t apts;
+    int64_t vpts;
+} DHAVContext;
+
+static int dhav_probe(AVProbeData *p)
+{
+    if (memcmp(p->buf, "DHAV", 4))
+        return 0;
+
+    return AVPROBE_SCORE_MAX;
+}
+
+static int dhav_read_header(AVFormatContext *s)
+{
+    AVStream *st, *ast;
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    st->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
+    st->codecpar->codec_id    = AV_CODEC_ID_H264;
+    st->need_parsing          = AVSTREAM_PARSE_FULL_RAW;
+    avpriv_set_pts_info(st, 64, 1, 25);
+
+    ast = avformat_new_stream(s, NULL);
+    if (!ast)
+        return AVERROR(ENOMEM);
+
+    ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
+    ast->codecpar->codec_id    = AV_CODEC_ID_PCM_ALAW;
+    ast->codecpar->channels    = 1;
+    ast->codecpar->sample_rate = 8000;
+    avpriv_set_pts_info(ast, 64, 1, 8000);
+
+    s->ctx_flags |= AVFMTCTX_NOHEADER;
+
+    return 0;
+}
+
+static int read_chunk(AVFormatContext *s)
+{
+    DHAVContext *dhav = s->priv_data;
+    unsigned size, skip;
+    int64_t start, end;
+
+    start = avio_tell(s->pb);
+
+    if (avio_feof(s->pb))
+        return AVERROR_EOF;
+
+    if (avio_rl32(s->pb) != MKTAG('D','H','A','V'))
+        return AVERROR_INVALIDDATA;
+
+    dhav->type = avio_r8(s->pb);
+    avio_skip(s->pb, 3);
+    avio_rl32(s->pb);
+    size = avio_rl32(s->pb);
+    if (dhav->type == 0xf1) {
+        avio_skip(s->pb, size - 16);
+        return 0;
+    }
+
+    avio_rl32(s->pb);
+    avio_skip(s->pb, 2);
+    skip = avio_r8(s->pb);
+    avio_skip(s->pb, 1);
+    avio_skip(s->pb, skip);
+
+    end = avio_tell(s->pb);
+
+    return size - 8 - (end - start);
+}
+
+static int dhav_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    DHAVContext *dhav = s->priv_data;
+    int64_t start;
+    int ret;
+
+    start = avio_tell(s->pb);
+
+    while ((ret = read_chunk(s)) == 0)
+        ;
+
+    if (ret < 0)
+        return ret;
+    ret = av_get_packet(s->pb, pkt, ret);
+    if (ret < 0)
+        return ret;
+    pkt->stream_index = dhav->type == 0xF0 ? 1 : 0;
+    pkt->pos = start;
+    if (avio_rl32(s->pb) != MKTAG('d','h','a','v'))
+        return AVERROR_INVALIDDATA;
+    avio_skip(s->pb, 4);
+
+    return ret;
+}
+
+AVInputFormat ff_dhav_demuxer = {
+    .name           = "dhav",
+    .long_name      = NULL_IF_CONFIG_SMALL("DHAV"),
+    .priv_data_size = sizeof(DHAVContext),
+    .read_probe     = dhav_probe,
+    .read_header    = dhav_read_header,
+    .read_packet    = dhav_read_packet,
+    .extensions     = "dav",
+    .flags          = AVFMT_GENERIC_INDEX,
+};
-- 
2.17.1



More information about the ffmpeg-devel mailing list