From ddf5fb71ee9c8b2d9a23c0f661a84896cd7050fc Mon Sep 17 00:00:00 2001 From: Thomas Volkert Date: Sun, 21 Sep 2014 12:10:42 +0200 Subject: rtpenc: HEVC/H.265 support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Storsjö --- Changelog | 2 +- libavformat/Makefile | 1 + libavformat/rtpenc.c | 13 +++++ libavformat/rtpenc.h | 1 + libavformat/rtpenc_hevc.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++ libavformat/sdp.c | 6 +++ libavformat/version.h | 2 +- 7 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 libavformat/rtpenc_hevc.c diff --git a/Changelog b/Changelog index bbbe9dc248..27770e5e24 100644 --- a/Changelog +++ b/Changelog @@ -3,7 +3,7 @@ releases are sorted from youngest to oldest. version : - aliases and defaults for Ogg subtypes (opus, spx) -- HEVC/H.265 RTP payload format (draft v6) depacketizer +- HEVC/H.265 RTP payload format (draft v6) packetizer and depacketizer - avplay now exits by default at the end of playback diff --git a/libavformat/Makefile b/libavformat/Makefile index dbb24277d3..ff887f07e2 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -291,6 +291,7 @@ OBJS-$(CONFIG_RTP_MUXER) += rtp.o \ rtpenc_amr.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 7bc73735c2..647a8073b7 100644 --- a/libavformat/rtpenc.c +++ b/libavformat/rtpenc.c @@ -52,6 +52,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: @@ -196,6 +197,15 @@ static int rtp_write_header(AVFormatContext *s1) s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1; } break; + case AV_CODEC_ID_HEVC: + /* Only check for the standardized hvcC version of extradata, keeping + * things simple and similar to the avcC/H264 case above, instead + * of trying to handle the pre-standardization versions (as in + * libavcodec/hevc.c). */ + if (st->codec->extradata_size > 21 && st->codec->extradata[0] == 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; @@ -566,6 +576,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 35ee1513fd..4a72a49bde 100644 --- a/libavformat/rtpenc.h +++ b/libavformat/rtpenc.h @@ -84,6 +84,7 @@ void ff_rtp_send_h264(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 0000000000..c6474dcdaa --- /dev/null +++ b/libavformat/rtpenc_hevc.c @@ -0,0 +1,130 @@ +/* + * RTP packetizer for HEVC/H.265 payload format (draft version 6) + * Copyright (c) 2014 Thomas Volkert + * + * This file is part of Libav. + * + * Libav 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. + * + * Libav 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 Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avc.h" +#include "avformat.h" +#include "rtpenc.h" + +#define RTP_HEVC_HEADERS_SIZE 3 + +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_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 = ff_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 = ff_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/sdp.c b/libavformat/sdp.c index a690219006..3c39ac7988 100644 --- a/libavformat/sdp.c +++ b/libavformat/sdp.c @@ -428,6 +428,12 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, payload_type, payload_type, c->width, c->height); break; + case AV_CODEC_ID_HEVC: + if (c->extradata_size) + av_log(NULL, AV_LOG_WARNING, "HEVC extradata not currently " + "passed properly through SDP\n"); + av_strlcatf(buff, size, "a=rtpmap:%d H265/90000\r\n", payload_type); + break; case AV_CODEC_ID_MPEG4: if (c->extradata_size) { config = extradata2config(c); diff --git a/libavformat/version.h b/libavformat/version.h index 4fef2b4ff0..688c84a50e 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,7 +30,7 @@ #include "libavutil/version.h" #define LIBAVFORMAT_VERSION_MAJOR 56 -#define LIBAVFORMAT_VERSION_MINOR 4 +#define LIBAVFORMAT_VERSION_MINOR 5 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ -- cgit v1.2.3