[FFmpeg-cvslog] rtpenc: HEVC/H.265 support

Thomas Volkert git at videolan.org
Sun Sep 21 13:04:34 CEST 2014


ffmpeg | branch: master | Thomas Volkert <thomas at homer-conferencing.com> | Sun Sep 21 12:10:42 2014 +0200| [6821a5a4adcb40c458356e8bb90416dd8f5dd6b2] | committer: Michael Niedermayer

rtpenc: HEVC/H.265 support

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=6821a5a4adcb40c458356e8bb90416dd8f5dd6b2
---

 Changelog                 |    1 +
 libavformat/Makefile      |    1 +
 libavformat/rtpenc.c      |   11 ++++
 libavformat/rtpenc.h      |    1 +
 libavformat/rtpenc_hevc.c |  146 +++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h     |    2 +-
 6 files changed, 161 insertions(+), 1 deletion(-)

diff --git a/Changelog b/Changelog
index 68ae1d7..65f8345 100644
--- a/Changelog
+++ b/Changelog
@@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
 releases are sorted from youngest to oldest.
 
 version <next>:
+- HEVC/H.265 RTP payload format (draft v6) packetizer
 
 version 2.4:
 - Icecast protocol
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 3d124fb..d3bf48f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -367,6 +367,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
                                             rtpenc_h261.o    \
                                             rtpenc_h263.o    \
                                             rtpenc_h263_rfc2190.o \
+                                            rtpenc_hevc.o    \
                                             rtpenc_jpeg.o \
                                             rtpenc_mpv.o     \
                                             rtpenc.o      \
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index 9c587d2..dfaccef 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -53,6 +53,7 @@ static int is_supported(enum AVCodecID id)
     case AV_CODEC_ID_H263:
     case AV_CODEC_ID_H263P:
     case AV_CODEC_ID_H264:
+    case AV_CODEC_ID_HEVC:
     case AV_CODEC_ID_MPEG1VIDEO:
     case AV_CODEC_ID_MPEG2VIDEO:
     case AV_CODEC_ID_MPEG4:
@@ -200,6 +201,13 @@ static int rtp_write_header(AVFormatContext *s1)
             s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
         }
         break;
+    case AV_CODEC_ID_HEVC:
+        if (st->codec->extradata_size > 21 &&
+            (st->codec->extradata[0] || st->codec->extradata[1] ||
+             st->codec->extradata[2] > 1)) {
+            s->nal_length_size = (st->codec->extradata[21] & 0x03) + 1;
+        }
+        break;
     case AV_CODEC_ID_VORBIS:
     case AV_CODEC_ID_THEORA:
         if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
@@ -577,6 +585,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
     case AV_CODEC_ID_H263P:
         ff_rtp_send_h263(s1, pkt->data, size);
         break;
+    case AV_CODEC_ID_HEVC:
+        ff_rtp_send_hevc(s1, pkt->data, size);
+        break;
     case AV_CODEC_ID_VORBIS:
     case AV_CODEC_ID_THEORA:
         ff_rtp_send_xiph(s1, pkt->data, size);
diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h
index 48b6b02..4eb98d8 100644
--- a/libavformat/rtpenc.h
+++ b/libavformat/rtpenc.h
@@ -85,6 +85,7 @@ void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *buf1, int size);
 void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
 void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size,
                               const uint8_t *mb_info, int mb_info_size);
+void ff_rtp_send_hevc(AVFormatContext *s1, const uint8_t *buf1, int size);
 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
 void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size);
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
diff --git a/libavformat/rtpenc_hevc.c b/libavformat/rtpenc_hevc.c
new file mode 100644
index 0000000..a2516de
--- /dev/null
+++ b/libavformat/rtpenc_hevc.c
@@ -0,0 +1,146 @@
+/*
+ * RTP packetizer for HEVC/H.265 payload format (draft version 6)
+ * Copyright (c) 2014 Thomas Volkert <thomas at homer-conferencing.com>
+ *
+ * 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 "avc.h"
+#include "rtpenc.h"
+
+#define RTP_HEVC_HEADERS_SIZE 3
+
+static const uint8_t *avc_mp4_find_startcode(const uint8_t *start, const uint8_t *end, int nal_length_size)
+{
+    unsigned int res = 0;
+
+    /* is the given data big enough for 1 NAL unit? */
+    if (end - start < nal_length_size)
+        return NULL;
+
+    while (nal_length_size--)
+        res = (res << 8) | *start++;
+
+    if (res > end - start)
+        return NULL;
+
+    return start + res;
+}
+
+static void nal_send(AVFormatContext *ctx, const uint8_t *buf, int len, int last_packet_of_frame)
+{
+    RTPMuxContext *rtp_ctx = ctx->priv_data;
+    int rtp_payload_size   = rtp_ctx->max_payload_size - RTP_HEVC_HEADERS_SIZE;
+    int nal_type           = (buf[0] >> 1) & 0x3F;
+
+    /* send it as one single NAL unit? */
+    if (len <= rtp_ctx->max_payload_size) {
+        /* use the original NAL unit buffer and transmit it as RTP payload */
+        ff_rtp_send_data(ctx, buf, len, last_packet_of_frame);
+    } else {
+        /*
+          create the HEVC payload header and transmit the buffer as fragmentation units (FU)
+
+             0                   1
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |F|   Type    |  LayerId  | TID |
+            +-------------+-----------------+
+
+               F       = 0
+               Type    = 49 (fragmentation unit (FU))
+               LayerId = 0
+               TID     = 1
+         */
+        rtp_ctx->buf[0] = 49 << 1;
+        rtp_ctx->buf[1] = 1;
+
+        /*
+              create the FU header
+
+              0 1 2 3 4 5 6 7
+             +-+-+-+-+-+-+-+-+
+             |S|E|  FuType   |
+             +---------------+
+
+                S       = variable
+                E       = variable
+                FuType  = NAL unit type
+         */
+        rtp_ctx->buf[2]  = nal_type;
+        /* set the S bit: mark as start fragment */
+        rtp_ctx->buf[2] |= 1 << 7;
+
+        /* pass the original NAL header */
+        buf += 2;
+        len -= 2;
+
+        while (len + RTP_HEVC_HEADERS_SIZE > rtp_ctx->max_payload_size) {
+            /* complete and send current RTP packet */
+            memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, rtp_payload_size);
+            ff_rtp_send_data(ctx, rtp_ctx->buf, rtp_ctx->max_payload_size, 0);
+
+            buf += rtp_payload_size;
+            len -= rtp_payload_size;
+
+            /* reset the S bit */
+            rtp_ctx->buf[2] &= ~(1 << 7);
+        }
+
+        /* set the E bit: mark as last fragment */
+        rtp_ctx->buf[2] |= 1 << 6;
+
+        /* complete and send last RTP packet */
+        memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, len);
+        ff_rtp_send_data(ctx, rtp_ctx->buf, len + 2, last_packet_of_frame);
+    }
+}
+
+void ff_rtp_send_hevc(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size)
+{
+    const uint8_t *next_NAL_unit;
+    const uint8_t *buf_ptr, *buf_end = frame_buf + frame_size;
+    RTPMuxContext *rtp_ctx = ctx->priv_data;
+
+    /* use the default 90 KHz time stamp */
+    rtp_ctx->timestamp = rtp_ctx->cur_timestamp;
+
+    if (rtp_ctx->nal_length_size)
+        buf_ptr = avc_mp4_find_startcode(frame_buf, buf_end, rtp_ctx->nal_length_size) ? frame_buf : buf_end;
+    else
+        buf_ptr = ff_avc_find_startcode(frame_buf, buf_end);
+
+    /* find all NAL units and send them as separate packets */
+    while (buf_ptr < buf_end) {
+        if (rtp_ctx->nal_length_size) {
+            next_NAL_unit = avc_mp4_find_startcode(buf_ptr, buf_end, rtp_ctx->nal_length_size);
+            if (!next_NAL_unit)
+                next_NAL_unit = buf_end;
+
+            buf_ptr += rtp_ctx->nal_length_size;
+        } else {
+            while (!*(buf_ptr++)) ;
+            next_NAL_unit = ff_avc_find_startcode(buf_ptr, buf_end);
+        }
+        /* send the next NAL unit */
+        nal_send(ctx, buf_ptr, next_NAL_unit - buf_ptr, next_NAL_unit == buf_end);
+
+        /* jump to the next NAL unit */
+        buf_ptr = next_NAL_unit;
+    }
+}
diff --git a/libavformat/version.h b/libavformat/version.h
index a869602..61decb9 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #define LIBAVFORMAT_VERSION_MAJOR 56
 #define LIBAVFORMAT_VERSION_MINOR  4
-#define LIBAVFORMAT_VERSION_MICRO 102
+#define LIBAVFORMAT_VERSION_MICRO 103
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \



More information about the ffmpeg-cvslog mailing list