From f591b7b5265fcf29dad12c4995ee43b0e3d594e7 Mon Sep 17 00:00:00 2001 From: Thomas Volkert Date: Tue, 3 May 2016 19:07:37 +0200 Subject: [PATCH] rtpenc: packetizer for VC-2 HQ RTP payload format (draft v1) --- Changelog | 2 +- MAINTAINERS | 1 + libavformat/Makefile | 1 + libavformat/rtpenc.c | 15 +++++ libavformat/rtpenc.h | 1 + libavformat/rtpenc_vc2hq.c | 134 +++++++++++++++++++++++++++++++++++++ libavformat/sdp.c | 3 + libavformat/version.h | 2 +- 8 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 libavformat/rtpenc_vc2hq.c diff --git a/Changelog b/Changelog index 86b72852a7..bc4512dedb 100644 --- a/Changelog +++ b/Changelog @@ -12,7 +12,7 @@ version : - ciescope filter - protocol blacklisting API - MediaCodec H264 decoding -- VC-2 HQ RTP payload format (draft v1) depacketizer +- VC-2 HQ RTP payload format (draft v1) depacketizer and packetizer - AudioToolbox audio decoders - AudioToolbox audio encoders - coreimage filter (GPU based image filtering on OSX) diff --git a/MAINTAINERS b/MAINTAINERS index d2593e52c1..14bf377d1f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -502,6 +502,7 @@ Muxers/Demuxers: rtpdec_hevc.*, rtpenc_hevc.* Thomas Volkert rtpdec_mpa_robust.* Gilles Chanteperdrix rtpdec_asf.* Ronald S. Bultje + rtpdec_vc2hq.*, rtpenc_vc2hq.* Thomas Volkert rtpdec_vp9.c Thomas Volkert rtpenc_mpv.*, rtpenc_aac.* Martin Storsjo rtsp.c Luca Barbato diff --git a/libavformat/Makefile b/libavformat/Makefile index 0f401766bd..d0ae27917e 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -408,6 +408,7 @@ OBJS-$(CONFIG_RTP_MUXER) += rtp.o \ rtpenc_jpeg.o \ rtpenc_mpv.o \ rtpenc.o \ + rtpenc_vc2hq.o \ rtpenc_vp8.o \ rtpenc_xiph.o \ avc.o hevc.o diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c index ef51236ab3..f477650a87 100644 --- a/libavformat/rtpenc.c +++ b/libavformat/rtpenc.c @@ -49,6 +49,7 @@ static const AVClass rtp_muxer_class = { static int is_supported(enum AVCodecID id) { switch(id) { + case AV_CODEC_ID_DIRAC: case AV_CODEC_ID_H261: case AV_CODEC_ID_H263: case AV_CODEC_ID_H263P: @@ -173,6 +174,17 @@ static int rtp_write_header(AVFormatContext *s1) n = 1; s->max_payload_size = n * TS_PACKET_SIZE; break; + case AV_CODEC_ID_DIRAC: + if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { + av_log(s, AV_LOG_ERROR, + "Packetizing VC-2 is experimental and does not use all values " + "of the specification " + "(even though most receivers may handle it just fine). " + "Please set -strict experimental in order to enable it.\n"); + ret = AVERROR_EXPERIMENTAL; + goto fail; + } + break; case AV_CODEC_ID_H261: if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { av_log(s, AV_LOG_ERROR, @@ -550,6 +562,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt) case AV_CODEC_ID_MPEG2TS: rtp_send_mpegts_raw(s1, pkt->data, size); break; + case AV_CODEC_ID_DIRAC: + ff_rtp_send_vc2hq(s1, pkt->data, size, st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0); + break; case AV_CODEC_ID_H264: ff_rtp_send_h264_hevc(s1, pkt->data, size); break; diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h index d34153fa67..dc30a6da48 100644 --- a/libavformat/rtpenc.h +++ b/libavformat/rtpenc.h @@ -91,6 +91,7 @@ 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); void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size); void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size); +void ff_rtp_send_vc2hq(AVFormatContext *s1, const uint8_t *buf, int size, int interlaced); void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size); void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buff, int size); diff --git a/libavformat/rtpenc_vc2hq.c b/libavformat/rtpenc_vc2hq.c new file mode 100644 index 0000000000..deda62183a --- /dev/null +++ b/libavformat/rtpenc_vc2hq.c @@ -0,0 +1,134 @@ +/* + * RTP packetizer 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 "libavcodec/get_bits.h" +#include "libavcodec/golomb.h" + +#include "avformat.h" +#include "rtpenc.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 + +static void send_packet(AVFormatContext *ctx, uint8_t parse_code, int info_hdr_size, const uint8_t *buf, int size, int i, int f, int rtp_m) +{ + RTPMuxContext *rtp_ctx = ctx->priv_data; + + AV_WB16(&rtp_ctx->buf[0], 0); /* extended sequence number */ + AV_WB8 (&rtp_ctx->buf[2], i ? (f ? (0x03) : (0x02)) : 0x00); /* flags: interlaced, second field */ + AV_WB8 (&rtp_ctx->buf[3], parse_code); + if (size > 0) + memcpy(&rtp_ctx->buf[4 + info_hdr_size], buf, size); + ff_rtp_send_data(ctx, rtp_ctx->buf, RTP_VC2HQ_PL_HEADER_SIZE + info_hdr_size + size, rtp_m); +} + +static void send_picture(AVFormatContext *ctx, const uint8_t *buf, int size, int interlaced) +{ + RTPMuxContext *rtp_ctx = ctx->priv_data; + GetBitContext gc; + int lvl, second_field; + uint32_t pic_nr, wavelet_depth, prefix_bytes, size_scaler; + uint16_t frag_len; + char *info_hdr = &rtp_ctx->buf[4]; + + pic_nr = AV_RB32(&buf[0]); + buf += DIRAC_PIC_NR_SIZE; + size -= DIRAC_PIC_NR_SIZE; + second_field = interlaced && (pic_nr & 0x01); + + init_get_bits(&gc, buf, 8 * size); + svq3_get_ue_golomb(&gc); /* wavelet_idx */ + wavelet_depth = svq3_get_ue_golomb(&gc); + svq3_get_ue_golomb(&gc); /* num_x */ + svq3_get_ue_golomb(&gc); /* num_y */ + prefix_bytes = svq3_get_ue_golomb(&gc); + size_scaler = svq3_get_ue_golomb(&gc); + /* pass the quantization matrices */ + svq3_get_ue_golomb(&gc); + for(lvl = 0; lvl < wavelet_depth; lvl++) + { + svq3_get_ue_golomb(&gc); + svq3_get_ue_golomb(&gc); + svq3_get_ue_golomb(&gc); + } + + frag_len = (get_bits_count(&gc) + 7) / 8; /* length of transform parameters */ + + AV_WB32(&info_hdr[ 0], pic_nr); + AV_WB16(&info_hdr[ 4], prefix_bytes); + AV_WB16(&info_hdr[ 6], size_scaler); + AV_WB16(&info_hdr[ 8], frag_len); + AV_WB16(&info_hdr[10], 0 /* nr. of slices */); + send_packet(ctx, DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT, 12, buf, frag_len, interlaced, second_field, 0); + buf += frag_len; + size -= frag_len; + + while (size > 0) { + frag_len = FFMIN(rtp_ctx->max_payload_size - 20 /* pl header */, size); + AV_WB16(&info_hdr[ 8], frag_len); + AV_WB16(&info_hdr[10], 1 /* nr. of slices */); + AV_WB16(&info_hdr[12], 0 /* slice x */); + AV_WB16(&info_hdr[14], 0 /* slice y */); + + size -= frag_len; + send_packet(ctx, DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT, 16, buf, frag_len, interlaced, second_field, size > 0 ? 0 : 1); + buf += frag_len; + } +} + +void ff_rtp_send_vc2hq(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size, int interlaced) +{ + const uint8_t *end = frame_buf + frame_size; + const uint8_t *unit = frame_buf; + uint8_t parse_code; + uint32_t unit_size; + + while (unit < end) { + parse_code = unit[4]; + unit_size = AV_RB32(&unit[5]); + + switch (parse_code) { + /* sequence header */ + /* end of sequence */ + case DIRAC_PCODE_SEQ_HEADER: + case DIRAC_PCODE_END_SEQ: + send_packet(ctx, parse_code, 0, unit + DIRAC_DATA_UNIT_HEADER_SIZE, unit_size - DIRAC_DATA_UNIT_HEADER_SIZE, 0, 0, 0); + break; + /* HQ picture */ + case DIRAC_PCODE_PICTURE_HQ: + send_picture(ctx, unit + DIRAC_DATA_UNIT_HEADER_SIZE, unit_size - DIRAC_DATA_UNIT_HEADER_SIZE, interlaced); + break; + /* parse codes without specification */ + case DIRAC_PCODE_AUX: + case DIRAC_PCODE_PAD: + break; + default: + avpriv_report_missing_feature(ctx, "VC-2 parse code %d", parse_code); + break; + } + unit += unit_size; + } +} diff --git a/libavformat/sdp.c b/libavformat/sdp.c index 7ba4a9a3a0..8279d595c4 100644 --- a/libavformat/sdp.c +++ b/libavformat/sdp.c @@ -485,6 +485,9 @@ static char *sdp_write_media_attributes(char *buff, int size, AVStream *st, int AVCodecParameters *p = st->codecpar; switch (p->codec_id) { + case AV_CODEC_ID_DIRAC: + av_strlcatf(buff, size, "a=rtpmap:%d VC2/90000\r\n", payload_type); + break; case AV_CODEC_ID_H264: { int mode = 1; if (fmt && fmt->oformat && fmt->oformat->priv_class && diff --git a/libavformat/version.h b/libavformat/version.h index 6a3a17fd21..7f721da52f 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,7 +30,7 @@ #include "libavutil/version.h" #define LIBAVFORMAT_VERSION_MAJOR 57 -#define LIBAVFORMAT_VERSION_MINOR 35 +#define LIBAVFORMAT_VERSION_MINOR 36 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \