[FFmpeg-devel] [PATCH] rtpdec: support for VC-2 HQ RTP payload format (draft v1)

silvo at gmx.net silvo at gmx.net
Wed Feb 3 10:21:19 CET 2016


From: Thomas Volkert <silvo at gmx.net>

---
 Changelog                    |   1 +
 libavformat/Makefile         |   1 +
 libavformat/rtpdec.c         |   1 +
 libavformat/rtpdec_formats.h |   1 +
 libavformat/rtpdec_vc2hq.c   | 225 +++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h        |   4 +-
 6 files changed, 231 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/rtpdec_vc2hq.c

diff --git a/Changelog b/Changelog
index 2f2ca3e..0b484d8 100644
--- a/Changelog
+++ b/Changelog
@@ -64,6 +64,7 @@ version <next>:
 - new DCA decoder with full support for DTS-HD extensions
 - significant performance improvements in Windows Television (WTV) demuxer
 - nnedi deinterlacer
+- VC-2 HQ RTP payload format (draft v1) depacketizer
 
 
 version 2.8:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 35a383d..db86742 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -51,6 +51,7 @@ OBJS-$(CONFIG_RTPDEC)                    += rdt.o                       \
                                             rtpdec_qdm2.o               \
                                             rtpdec_qt.o                 \
                                             rtpdec_svq3.o               \
+                                            rtpdec_vc2hq.o              \
                                             rtpdec_vp8.o                \
                                             rtpdec_vp9.o                \
                                             rtpdec_xiph.o               \
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index c3e50d4..18a0803 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -105,6 +105,7 @@ void ff_register_rtp_dynamic_payload_handlers(void)
     ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler);
     ff_register_dynamic_payload_handler(&ff_svq3_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_theora_dynamic_handler);
+    ff_register_dynamic_payload_handler(&ff_vc2hq_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_vorbis_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_vp8_dynamic_handler);
     ff_register_dynamic_payload_handler(&ff_vp9_dynamic_handler);
diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h
index 50a2f4c..4f58dee 100644
--- a/libavformat/rtpdec_formats.h
+++ b/libavformat/rtpdec_formats.h
@@ -80,6 +80,7 @@ extern RTPDynamicProtocolHandler ff_quicktime_rtp_aud_handler;
 extern RTPDynamicProtocolHandler ff_quicktime_rtp_vid_handler;
 extern RTPDynamicProtocolHandler ff_svq3_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_theora_dynamic_handler;
+extern RTPDynamicProtocolHandler ff_vc2hq_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_vorbis_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_vp8_dynamic_handler;
 extern RTPDynamicProtocolHandler ff_vp9_dynamic_handler;
diff --git a/libavformat/rtpdec_vc2hq.c b/libavformat/rtpdec_vc2hq.c
new file mode 100644
index 0000000..5c6f4e5
--- /dev/null
+++ b/libavformat/rtpdec_vc2hq.c
@@ -0,0 +1,225 @@
+/*
+ * RTP parser for VC-2 HQ payload format (draft version 1) - experimental
+ * Copyright (c) 2016 Thomas Volkert
+ *
+ * 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 "libavutil/intreadwrite.h"
+#include "libavcodec/dirac.h"
+
+#include "avio_internal.h"
+#include "rtpdec_formats.h"
+
+#define RTP_VC2HQ_PL_HEADER_SIZE             4
+
+#define DIRAC_DATA_UNIT_HEADER_SIZE          13
+#define DIRAC_PIC_NR_SIZE                    4
+#define DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT      0xEC
+
+struct PayloadContext {
+    AVIOContext *buf;
+    uint32_t    frame_size;
+    uint32_t    frame_nr;
+    uint32_t    timestamp;
+    uint32_t    last_unit_size;
+    int         seen_sequence_header;
+};
+
+static const uint8_t start_sequence[] = { 'B', 'B', 'C', 'D' };
+
+static void fill_parse_info_header(PayloadContext *pl_ctx, uint8_t *buf,
+                                   uint8_t parse_code, uint32_t data_unit_size)
+{
+    memcpy(buf, start_sequence, sizeof(start_sequence));
+    buf[4]  = parse_code;
+    AV_WB32(&buf[5], data_unit_size);
+    AV_WB32(&buf[9], pl_ctx->last_unit_size);
+
+    pl_ctx->last_unit_size = data_unit_size;
+}
+
+static int vc2hq_handle_sequence_header(PayloadContext *pl_ctx, AVStream *st, AVPacket *pkt,
+                                        const uint8_t *buf, int len)
+{
+    int res;
+    uint32_t size = DIRAC_DATA_UNIT_HEADER_SIZE + len;
+
+    if ((res = av_new_packet(pkt, DIRAC_DATA_UNIT_HEADER_SIZE + len)) < 0)
+        return res;
+
+    fill_parse_info_header(pl_ctx, pkt->data, 0x00, size);
+    /* payload of seq. header */
+    memcpy(pkt->data + DIRAC_DATA_UNIT_HEADER_SIZE, buf, len);
+    pkt->stream_index = st->index;
+
+    pl_ctx->seen_sequence_header = 1;
+
+    return 0;
+}
+
+static int vc2hq_mark_end_of_sequence(PayloadContext *pl_ctx, AVStream *st, AVPacket *pkt)
+{
+    int res;
+    uint32_t size = 0;
+
+    /* create A/V packet */
+    if ((res = av_new_packet(pkt, DIRAC_DATA_UNIT_HEADER_SIZE)) < 0)
+        return res;
+
+    fill_parse_info_header(pl_ctx, pkt->data, 0x10, size);
+    pkt->stream_index = st->index;
+
+    pl_ctx->seen_sequence_header = 0;
+
+    return 0;
+}
+
+static int vc2hq_handle_frame_fragment(AVFormatContext *ctx, PayloadContext *pl_ctx, AVStream *st,
+                                       AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len,
+                                       int flags)
+{
+    int res;
+    uint32_t pic_nr;
+    uint16_t frag_len;
+    uint16_t no_slices;
+
+    /* sanity check for size of input packet: 16 bytes header in any case as minimum */
+    if (len < 16) {
+        av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
+        return AVERROR_INVALIDDATA;
+    }
+
+    pic_nr = AV_RB32(&buf[4]);
+    frag_len = AV_RB16(&buf[12]);
+    no_slices = AV_RB16(&buf[14]);
+
+    if (pl_ctx->buf && pl_ctx->frame_nr != pic_nr) {
+        av_log(ctx, AV_LOG_WARNING, "Dropping buffered RTP/VC2hq packet fragments - non-continuous picture numbers\n");
+        ffio_free_dyn_buf(&pl_ctx->buf);
+    }
+
+    /* transform parameters? */
+    if (no_slices == 0) {
+        if (len < frag_len + 16) {
+            av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
+            return AVERROR_INVALIDDATA;
+        }
+
+        /* start frame buffering with new dynamic buffer */
+        if (!pl_ctx->buf) {
+
+            res = avio_open_dyn_buf(&pl_ctx->buf);
+            if (res < 0)
+                return res;
+
+            /* reserve memory for frame header */
+            res = avio_seek(pl_ctx->buf, DIRAC_DATA_UNIT_HEADER_SIZE + DIRAC_PIC_NR_SIZE, SEEK_SET);
+            if (res < 0)
+                return res;
+
+            pl_ctx->frame_nr = pic_nr;
+            pl_ctx->timestamp = *timestamp;
+            pl_ctx->frame_size = DIRAC_DATA_UNIT_HEADER_SIZE + DIRAC_PIC_NR_SIZE;
+        }
+
+        avio_write(pl_ctx->buf, buf + 16 /* skip pl header */, frag_len);
+        pl_ctx->frame_size += frag_len;
+
+        return AVERROR(EAGAIN);
+    } else {
+        if (len < frag_len + 20) {
+            av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
+            return AVERROR_INVALIDDATA;
+        }
+
+        /* transform parameters were missed, no buffer available */
+        if (!pl_ctx->buf)
+            return AVERROR_INVALIDDATA;
+
+        avio_write(pl_ctx->buf, buf + 20 /* skip pl header */, frag_len);
+        pl_ctx->frame_size += frag_len;
+
+        /* RTP marker bit means: last fragment of current frame was received;
+           otherwise, an additional fragment is needed for the current frame */
+        if (!(flags & RTP_FLAG_MARKER))
+            return AVERROR(EAGAIN);
+    }
+
+    /* close frame buffering and create A/V packet */
+    res = ff_rtp_finalize_packet(pkt, &pl_ctx->buf, st->index);
+    if (res < 0)
+        return res;
+
+    fill_parse_info_header(pl_ctx, pkt->data, 0xE8, pl_ctx->frame_size);
+    AV_WB32(&pkt->data[13], pl_ctx->frame_nr);
+
+    pl_ctx->frame_size = 0;
+
+    return 0;
+}
+
+static int vc2hq_handle_packet(AVFormatContext *ctx, PayloadContext *pl_ctx,
+                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
+                               const uint8_t *buf, int len, uint16_t seq,
+                               int flags)
+{
+    uint8_t parse_code = 0;
+    int res = 0;
+
+    if (pl_ctx->buf && pl_ctx->timestamp != *timestamp) {
+        av_log(ctx, AV_LOG_WARNING, "Dropping buffered RTP/VC2hq packet fragments - non-continuous timestamps\n");
+        ffio_free_dyn_buf(&pl_ctx->buf);
+        pl_ctx->frame_size = 0;
+    }
+
+    /* sanity check for size of input packet: needed header data as minimum */
+    if (len < RTP_VC2HQ_PL_HEADER_SIZE) {
+        av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
+        return AVERROR_INVALIDDATA;
+    }
+
+    parse_code = buf[3];
+
+    /* wait for next sequence header? */
+    if (pl_ctx->seen_sequence_header || parse_code == DIRAC_PCODE_SEQ_HEADER) {
+        switch(parse_code) {
+        /* sequence header */
+        case DIRAC_PCODE_SEQ_HEADER:
+            res = vc2hq_handle_sequence_header(pl_ctx, st, pkt, buf + RTP_VC2HQ_PL_HEADER_SIZE, len - RTP_VC2HQ_PL_HEADER_SIZE);
+            break;
+        /* end of sequence */
+        case DIRAC_PCODE_END_SEQ:
+            res = vc2hq_mark_end_of_sequence(pl_ctx, st, pkt);
+            break;
+        /* HQ picture fragment */
+        case DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT:
+            res = vc2hq_handle_frame_fragment(ctx, pl_ctx, st, pkt, timestamp, buf, len, flags);
+            break;
+        }
+    }
+
+    return res;
+}
+
+RTPDynamicProtocolHandler ff_vc2hq_dynamic_handler = {
+    .enc_name         = "VC2",
+    .codec_type       = AVMEDIA_TYPE_VIDEO,
+    .codec_id         = AV_CODEC_ID_DIRAC,
+    .priv_data_size   = sizeof(PayloadContext),
+    .parse_packet     = vc2hq_handle_packet
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 423317d..16df2b8 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/version.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR  57
-#define LIBAVFORMAT_VERSION_MINOR  23
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MINOR  24
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
1.9.1



More information about the ffmpeg-devel mailing list