From patchwork Tue Feb 28 18:39:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Riegel X-Patchwork-Id: 2701 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.149 with SMTP id x21csp1579197vsf; Tue, 28 Feb 2017 10:39:53 -0800 (PST) X-Received: by 10.223.156.2 with SMTP id f2mr4044954wrc.4.1488307193053; Tue, 28 Feb 2017 10:39:53 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 1si3489714wrs.86.2017.02.28.10.39.52; Tue, 28 Feb 2017 10:39:53 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C4C5F6891FA; Tue, 28 Feb 2017 20:39:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.savoirfairelinux.com (mail.savoirfairelinux.com [208.88.110.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DCD35688372 for ; Tue, 28 Feb 2017 20:39:31 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id 0E1C99C1C01 for ; Tue, 28 Feb 2017 13:39:40 -0500 (EST) Received: from mail.savoirfairelinux.com ([127.0.0.1]) by localhost (mail.savoirfairelinux.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id Ab3yv2eIJGEN; Tue, 28 Feb 2017 13:39:39 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mail.savoirfairelinux.com (Postfix) with ESMTP id 3DE659C1B82; Tue, 28 Feb 2017 13:39:39 -0500 (EST) X-Virus-Scanned: amavisd-new at mail.savoirfairelinux.com Received: from mail.savoirfairelinux.com ([127.0.0.1]) by localhost (mail.savoirfairelinux.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id vIsb6-x9i9Va; Tue, 28 Feb 2017 13:39:39 -0500 (EST) Received: from workotop.mtl.sfl (unknown [192.168.49.100]) by mail.savoirfairelinux.com (Postfix) with ESMTPSA id 0FA289C1BF9; Tue, 28 Feb 2017 13:39:39 -0500 (EST) From: Damien Riegel To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Feb 2017 13:39:08 -0500 Message-Id: <20170228183908.26339-2-damien.riegel@savoirfairelinux.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170228183908.26339-1-damien.riegel@savoirfairelinux.com> References: <20170228183908.26339-1-damien.riegel@savoirfairelinux.com> Subject: [FFmpeg-devel] [PATCH v3 2/2] rtp: rfc4175: add handler for YCbCr-4:2:2 X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Damien Riegel , stepan.salenikovich@savoirfairelinux.com, eloi.bail@savoirfairelinux.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This adds partial support for the RFC 4175 (raw video over RTP). The only supported formats are the YCbCr-4:2:2 8 bit because it's natively supported by FFmpeg with pixel format UYVY, and 10 bit which requires the vrawdepay codec to convert the payload in a format handled by FFmpeg. Signed-off-by: Damien Riegel --- Changes in v3: - rename rawvideo to rfc4175 - set pixel format in codec parameters - add additional check to prevent buffer overflow libavformat/Makefile | 1 + libavformat/rtpdec.c | 1 + libavformat/rtpdec_formats.h | 1 + libavformat/rtpdec_rfc4175.c | 236 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 libavformat/rtpdec_rfc4175.c diff --git a/libavformat/Makefile b/libavformat/Makefile index fc2d76067b..5a1795ccd7 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -55,6 +55,7 @@ OBJS-$(CONFIG_RTPDEC) += rdt.o \ rtpdec_qcelp.o \ rtpdec_qdm2.o \ rtpdec_qt.o \ + rtpdec_rfc4175.o \ rtpdec_svq3.o \ rtpdec_vc2hq.o \ rtpdec_vp8.o \ diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c index 53cdad7396..4acb1ca629 100644 --- a/libavformat/rtpdec.c +++ b/libavformat/rtpdec.c @@ -114,6 +114,7 @@ void ff_register_rtp_dynamic_payload_handlers(void) ff_register_dynamic_payload_handler(&ff_qt_rtp_vid_handler); ff_register_dynamic_payload_handler(&ff_quicktime_rtp_aud_handler); ff_register_dynamic_payload_handler(&ff_quicktime_rtp_vid_handler); + ff_register_dynamic_payload_handler(&ff_rfc4175_rtp_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); diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h index 3292a3d265..a436c9d62c 100644 --- a/libavformat/rtpdec_formats.h +++ b/libavformat/rtpdec_formats.h @@ -82,6 +82,7 @@ extern RTPDynamicProtocolHandler ff_qt_rtp_aud_handler; extern RTPDynamicProtocolHandler ff_qt_rtp_vid_handler; extern RTPDynamicProtocolHandler ff_quicktime_rtp_aud_handler; extern RTPDynamicProtocolHandler ff_quicktime_rtp_vid_handler; +extern RTPDynamicProtocolHandler ff_rfc4175_rtp_handler; extern RTPDynamicProtocolHandler ff_svq3_dynamic_handler; extern RTPDynamicProtocolHandler ff_theora_dynamic_handler; extern RTPDynamicProtocolHandler ff_vc2hq_dynamic_handler; diff --git a/libavformat/rtpdec_rfc4175.c b/libavformat/rtpdec_rfc4175.c new file mode 100644 index 0000000000..9e3d520ee3 --- /dev/null +++ b/libavformat/rtpdec_rfc4175.c @@ -0,0 +1,236 @@ +/* + * RTP Depacketization of RAW video (TR-03) + * Copyright (c) 2016 Savoir-faire Linux, Inc + * + * 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 + */ + +/* Development sponsored by CBC/Radio-Canada */ + +#include "avio_internal.h" +#include "rtpdec_formats.h" +#include "libavutil/avstring.h" +#include "libavutil/pixdesc.h" + +struct PayloadContext { + char *sampling; + int depth; + int width; + int height; + + uint8_t *frame; + unsigned int frame_size; + unsigned int pgroup; /* size of the pixel group in bytes */ + unsigned int xinc; + + uint32_t timestamp; +}; + +static int rfc4175_parse_format(AVStream *stream, PayloadContext *data) +{ + enum AVPixelFormat pixfmt = AV_PIX_FMT_NONE; + int bits_per_sample = 0; + int tag = 0; + + if (!strcmp(data->sampling, "YCbCr-4:2:2")) { + tag = MKTAG('U', 'Y', 'V', 'Y'); + data->xinc = 2; + + if (data->depth == 8) { + data->pgroup = 4; + bits_per_sample = 16; + pixfmt = AV_PIX_FMT_UYVY422; + } else if (data->depth == 10) { + data->pgroup = 5; + bits_per_sample = 20; + pixfmt = AV_PIX_FMT_YUV422P10; + } else { + return -1; + } + } else { + return -1; + } + + stream->codecpar->format = pixfmt; + stream->codecpar->codec_tag = tag; + stream->codecpar->bits_per_coded_sample = bits_per_sample; + data->frame_size = data->width * data->height * data->pgroup / data->xinc; + + return 0; +} + +static int rfc4175_parse_fmtp(AVFormatContext *s, AVStream *stream, + PayloadContext *data, const char *attr, + const char *value) +{ + if (!strcmp(attr, "width")) + data->width = atoi(value); + else if (!strcmp(attr, "height")) + data->height = atoi(value); + else if (!strcmp(attr, "sampling")) + data->sampling = av_strdup(value); + else if (!strcmp(attr, "depth")) + data->depth = atoi(value); + + return 0; +} + +static int rfc4175_parse_sdp_line(AVFormatContext *s, int st_index, + PayloadContext *data, const char *line) +{ + const char *p; + + if (st_index < 0) + return 0; + + if (av_strstart(line, "fmtp:", &p)) { + AVStream *stream = s->streams[st_index]; + int ret = ff_parse_fmtp(s, stream, data, p, rfc4175_parse_fmtp); + + if (ret < 0) + return ret; + + + if (!data->sampling || !data->depth || !data->width || !data->height) + return -1; + + stream->codecpar->width = data->width; + stream->codecpar->height = data->height; + + ret = rfc4175_parse_format(stream, data); + av_freep(&data->sampling); + + return ret; + } + + return 0; +} + +static int rfc4175_finalize_packet(PayloadContext *data, AVPacket *pkt, + int stream_index) +{ + int ret; + + pkt->stream_index = stream_index; + ret = av_packet_from_data(pkt, data->frame, data->frame_size); + if (ret < 0) { + av_freep(&data->frame); + } + + data->frame = NULL; + + return ret; +} + +static int rfc4175_handle_packet(AVFormatContext *ctx, PayloadContext *data, + AVStream *st, AVPacket *pkt, uint32_t *timestamp, + const uint8_t * buf, int len, + uint16_t seq, int flags) +{ + int length, line, offset, cont; + const uint8_t *headers = buf + 2; /* skip extended seqnum */ + const uint8_t *payload = buf + 2; + int payload_len = len - 2; + int missed_last_packet = 0; + + uint8_t *dest; + + if (*timestamp != data->timestamp) { + if (data->frame) { + /* + * if we're here, it means that two RTP packets didn't have the + * same timestamp, which is a sign that they were packets from two + * different frames, but we didn't get the flag RTP_FLAG_MARKER on + * the first one of these frames (last packet of a frame). + * Finalize the previous frame anyway by filling the AVPacket. + */ + av_log(ctx, AV_LOG_ERROR, "Missed previous RTP Marker\n"); + missed_last_packet = 1; + rfc4175_finalize_packet(data, pkt, st->index); + } + + data->frame = av_malloc(data->frame_size); + + data->timestamp = *timestamp; + + if (!data->frame) { + av_log(ctx, AV_LOG_ERROR, "Out of memory.\n"); + return AVERROR(ENOMEM); + } + } + + /* + * looks for the 'Continuation bit' in scan lines' headers + * to find where data start + */ + do { + if (payload_len < 6) + return AVERROR_INVALIDDATA; + + cont = payload[4] & 0x80; + payload += 6; + payload_len -= 6; + } while (cont); + + /* and now iterate over every scan lines */ + do { + int copy_offset; + + if (payload_len < data->pgroup) + return AVERROR_INVALIDDATA; + + length = (headers[0] << 8) | headers[1]; + line = ((headers[2] & 0x7f) << 8) | headers[3]; + offset = ((headers[4] & 0x7f) << 8) | headers[5]; + cont = headers[4] & 0x80; + headers += 6; + + if (length % data->pgroup) + return AVERROR_INVALIDDATA; + + if (length > payload_len) + length = payload_len; + + /* prevent ill-formed packets to write after buffer's end */ + copy_offset = (line * data->width + offset) * data->pgroup / data->xinc; + if (copy_offset + length > data->frame_size) + return AVERROR_INVALIDDATA; + + dest = data->frame + copy_offset; + memcpy(dest, payload, length); + + payload += length; + payload_len -= length; + } while (cont); + + if ((flags & RTP_FLAG_MARKER)) { + return rfc4175_finalize_packet(data, pkt, st->index); + } else if (missed_last_packet) { + return 0; + } + + return AVERROR(EAGAIN); +} + +RTPDynamicProtocolHandler ff_rfc4175_rtp_handler = { + .enc_name = "raw", + .codec_type = AVMEDIA_TYPE_VIDEO, + .codec_id = AV_CODEC_ID_BITPACKED, + .priv_data_size = sizeof(PayloadContext), + .parse_sdp_a_line = rfc4175_parse_sdp_line, + .parse_packet = rfc4175_handle_packet, +};