From patchwork Tue Sep 4 13:57:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 10230 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp3915162jap; Tue, 4 Sep 2018 07:05:01 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaNfrOu6LKHdK0whz66C1UEydm+mZm9KcIsk24Ce5kyvjJ7QtsOarjPTSoG+aZ1SDLi/4Ko X-Received: by 2002:adf:e486:: with SMTP id i6-v6mr23622715wrm.145.1536069901518; Tue, 04 Sep 2018 07:05:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536069901; cv=none; d=google.com; s=arc-20160816; b=p+o0iaJIqDGqzcxywQu4+px2OPgAUsAowSU/5s/Gb6vhFWo+6ws3bPUh2ivG/BL3GL aFOWPuZmUILb3bodJdnRpjvJmIWLaRSiRBRR/pwgUjxF2mgBjFn4/COoRwqd4/69fAvd 3lr2UHVoNBmSiZ85hrwT9XQJQhqlt3o0T1KcH1HWySr9QL4iNrR+3KCeHa9CH/S15+1p 4Mw+M9Zt2fftkrZMdLDVK+rxy+R7/MiLZ/ECiNIiSmm8ckA1qmhYFKZTJc7QZppdOwJ9 Py5CDJa/fmDtY58pGllFtQsi/b1+yjnqXq4bmVhJQWiuyL71zhK8XUHub2uMp8nYVErP YppA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=plqYzoamhJJnisMikgrzrf9t+59BW9g0ts8LvUon0eg=; b=NLH4Z5VFWqni+lJyl6ZzKqiK5OQsB7J7g6nrTCCp1y/HybkCzaKsQKWA3H5WY4T8cD ejxb0EAvHxQNONHVUWawwy5FqOXeqt3UWZagm3CeZRavd2wFlQAfZofBBYd2DNjtjqUu HiW1X1uuZ5Vz2QpWs+ISRnC+OTowGInIr5Q7ut2NZANRJHONRTZTXPVWLKSV7ukdevvA 0eSuZEoM3uHZe6I/0mJl8PvLGbAFO2TWLyN8XCzcKFmhXumST6yFoP8P/Bkc00IoOHZ/ cGHS+DHIp6iO5MdTobaWa5gtX+Yp0BZB9gn+ITyCluEJX/XNvMpEucOtG9EIU5M79JeQ xyRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=J2pspAZH; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x8-v6si17930723wrt.345.2018.09.04.07.04.58; Tue, 04 Sep 2018 07:05:01 -0700 (PDT) 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; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=J2pspAZH; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D752E689EAA; Tue, 4 Sep 2018 17:04:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 140A8680964 for ; Tue, 4 Sep 2018 17:04:43 +0300 (EEST) Received: by mail-ed1-f67.google.com with SMTP id d8-v6so3377083edv.0 for ; Tue, 04 Sep 2018 07:04:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=QkG9u7vUclbyswq17LaFOozdUVrdgf1pfEnormU2WYI=; b=J2pspAZHN7+btviWQnO27RrBCOSIZGET7zJL/12Qd0MG3VVl9AV1IQ9c3hMdk+6iwj aWaFnyxAK6KOFoNpKRGU28VbXmGAC9QJIwMGX5Cmt2TH1cKHX15hm9suJQVXdN3AHlNR RGYsNVxYBDeu3TBOLRoh6EQfMdCX7Bu/MZ03k1maNvTN8SP36Ra7Veey/gVw5MPJGVTG LfKrQD4J6Tex/7dyNnykHvywxQJY+hwN7XR534FKZKBMOCXdsYPSeQqFQ5U3iLTHr6Tl ka8oK/wL361Iuob/QzSYLa6MkMILaupgpi6tM/RNJYn5N0ds7Rv39qoh3T/aRl7ew1N4 eXDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=QkG9u7vUclbyswq17LaFOozdUVrdgf1pfEnormU2WYI=; b=XswBeHF7xf+aVqExVwQjKm387pDs1duI4W0R2CZiTQr8po9V4zub3rGdNA+5GG0SUc NqEjfae4nvpJw72hgiTY9NYDU6Z6fXJGRvdKYLS13MJjQ8zlMZebkUDy2v5uP459x2OI sd26KWG6v4fU7iSogOD/95hChMW63X9ZfEoOiQdAa2/Ige5qi3wU3Gz+4htzJNdkNsvb OgmwG7Pyz64+Kefqw3IK0U9aDZPMqZfKdpXNrAgG2JejBsJwTyxB81/dMC2DpikyFyty dFHsPzniTCpLSiY0t1ZNsK3ibEPetMqEcxszzt0jyBebvJkjLit4HasKtlcf0YxUOO+N 9+bw== X-Gm-Message-State: APzg51B2RPEy4tgBlG678qJDDZK4tqvefQ0n0nUViBQDMwr/2hLj2jn3 cZP/UtjzJn+IXOtTnw1+lPzqzxD6 X-Received: by 2002:a50:9a02:: with SMTP id o2-v6mr37645625edb.236.1536069438710; Tue, 04 Sep 2018 06:57:18 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id u53-v6sm15147209edm.51.2018.09.04.06.57.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Sep 2018 06:57:18 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 4 Sep 2018 15:57:05 +0200 Message-Id: <20180904135705.856-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avcodec: add RemotelyAnywhere Screen Capture decoder 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 + libavcodec/rasc.c | 590 ++++++++++++++++++++++++++++++++++++++++ libavformat/riff.c | 1 + 6 files changed, 601 insertions(+) create mode 100644 libavcodec/rasc.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index f8673f0121..ceec1df972 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -536,6 +536,7 @@ OBJS-$(CONFIG_RA_144_DECODER) += ra144dec.o ra144.o celp_filters.o OBJS-$(CONFIG_RA_144_ENCODER) += ra144enc.o ra144.o celp_filters.o OBJS-$(CONFIG_RA_288_DECODER) += ra288.o celp_filters.o OBJS-$(CONFIG_RALF_DECODER) += ralf.o +OBJS-$(CONFIG_RASC_DECODER) += rasc.o OBJS-$(CONFIG_RAWVIDEO_DECODER) += rawdec.o OBJS-$(CONFIG_RAWVIDEO_ENCODER) += rawenc.o OBJS-$(CONFIG_REALTEXT_DECODER) += realtextdec.o ass.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index a461131c78..8fc1aeab79 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -248,6 +248,7 @@ extern AVCodec ff_r10k_encoder; extern AVCodec ff_r10k_decoder; extern AVCodec ff_r210_encoder; extern AVCodec ff_r210_decoder; +extern AVCodec ff_rasc_decoder; extern AVCodec ff_rawvideo_encoder; extern AVCodec ff_rawvideo_decoder; extern AVCodec ff_rl2_decoder; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index b6688b7af3..719c181a08 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -451,6 +451,7 @@ enum AVCodecID { AV_CODEC_ID_PROSUMER, AV_CODEC_ID_MWSC, AV_CODEC_ID_WCMV, + AV_CODEC_ID_RASC, /* various PCM "codecs" */ AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 46dfe3f5e5..67a30542d1 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1682,6 +1682,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("WinCAM Motion Video"), .props = AV_CODEC_PROP_LOSSLESS, }, + { + .id = AV_CODEC_ID_RASC, + .type = AVMEDIA_TYPE_VIDEO, + .name = "rasc", + .long_name = NULL_IF_CONFIG_SMALL("RemotelyAnywhere Screen Capture"), + .props = AV_CODEC_PROP_LOSSY, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/rasc.c b/libavcodec/rasc.c new file mode 100644 index 0000000000..288fe53a21 --- /dev/null +++ b/libavcodec/rasc.c @@ -0,0 +1,590 @@ +/* + * RemotelyAnywhere Screen Capture decoder + * + * Copyright (c) 2018 Paul B Mahol + * + * 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 +#include +#include + +#include "libavutil/avassert.h" +#include "libavutil/imgutils.h" + +#include "avcodec.h" +#include "bytestream.h" +#include "internal.h" + +#include + +#define KBND MKTAG('K', 'B', 'N', 'D') +#define FINT MKTAG('F', 'I', 'N', 'T') +#define BNDL MKTAG('B', 'N', 'D', 'L') +#define KFRM MKTAG('K', 'F', 'R', 'M') +#define DLTA MKTAG('D', 'L', 'T', 'A') +#define MOUS MKTAG('M', 'O', 'U', 'S') /* TODO */ +#define MPOS MKTAG('M', 'P', 'O', 'S') /* TODO */ +#define MOVE MKTAG('M', 'O', 'V', 'E') /* TODO */ +#define EMPT MKTAG('E', 'M', 'P', 'T') + +typedef struct RASCContext { + GetByteContext gb; + uint8_t *delta; + int delta_size; + int bndl; + z_stream zstream; + AVFrame *frame; + AVFrame *frame1; + AVFrame *frame2; +} RASCContext; + +static void clear_plane(AVCodecContext *avctx, AVFrame *frame) +{ + uint8_t *dst = frame->data[0]; + + for (int y = 0; y < avctx->height; y++) { + memset(dst, 0, avctx->width); + dst += frame->linesize[0]; + } +} + +static int decode_fint(AVCodecContext *avctx, AVFrame *frame, + AVPacket *avpkt, unsigned size) +{ + RASCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + unsigned w, h; + uint32_t *pal; + int ret; + + clear_plane(avctx, frame); + bytestream2_skip(gb, 8); + w = bytestream2_get_le32(gb); + h = bytestream2_get_le32(gb); + bytestream2_skip(gb, 56); + + ret = ff_set_dimensions(avctx, w, h); + if (ret < 0) + return ret; + avctx->width = w; + avctx->height = h; + + pal = (uint32_t *)frame->data[1]; + for (int i = 0; i < 256; i++) { + pal[i] = bytestream2_get_le32(gb) | 0xFF000000u; + } + + return 0; +} + +static int decode_move(AVCodecContext *avctx, AVFrame *frame, + AVPacket *avpkt, unsigned size) +{ + RASCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + unsigned compression, nb_moves; + + bytestream2_skip(gb, 8); + nb_moves = bytestream2_get_le32(gb); + bytestream2_skip(gb, 8); + compression = bytestream2_get_le32(gb); + + if (nb_moves > INT32_MAX / 16) + return AVERROR_INVALIDDATA; + + if (bytestream2_get_bytes_left(gb) < 16 * nb_moves) + return AVERROR_INVALIDDATA; + + for (int i = 0; i < nb_moves; i++) { + int type, start_x, start_y, end_x, end_y; + + type = bytestream2_get_le16(gb); + start_x = bytestream2_get_le16(gb); + start_y = bytestream2_get_le16(gb); + end_x = bytestream2_get_le16(gb); + end_y = bytestream2_get_le16(gb); + bytestream2_skip(gb, 6); + } + + return 0; +} + +static int decode_dlta(AVCodecContext *avctx, AVFrame *frame, + AVPacket *avpkt, unsigned size) +{ + RASCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + GetByteContext dc; + unsigned uncompressed_size, pos; + unsigned x, y, w, h; + int cx, compression, zret; + uint8_t *dst, *b1, *b2; + + pos = bytestream2_tell(gb); + bytestream2_skip(gb, 12); + uncompressed_size = bytestream2_get_le32(gb); + x = bytestream2_get_le32(gb); + y = bytestream2_get_le32(gb); + w = bytestream2_get_le32(gb); + h = bytestream2_get_le32(gb); + + if (x >= avctx->width || y >= avctx->height || w > avctx->width || h > avctx->height) + return AVERROR_INVALIDDATA; + + if (x + w > avctx->width || y + h > avctx->height) + return AVERROR_INVALIDDATA; + + bytestream2_skip(gb, 4); + compression = bytestream2_get_le32(gb); + + if (compression == 1) { + zret = inflateReset(&s->zstream); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret); + return AVERROR_EXTERNAL; + } + + av_fast_padded_malloc(&s->delta, &s->delta_size, uncompressed_size); + + s->zstream.next_in = avpkt->data + bytestream2_tell(gb); + s->zstream.avail_in = FFMIN(size, bytestream2_get_bytes_left(gb)); + + s->zstream.next_out = s->delta; + s->zstream.avail_out = s->delta_size; + + zret = inflate(&s->zstream, Z_FINISH); + if (zret != Z_STREAM_END) { + av_log(avctx, AV_LOG_ERROR, + "Inflate failed with return code: %d.\n", zret); + return AVERROR_INVALIDDATA; + } + + bytestream2_init(&dc, s->delta, uncompressed_size); + } else if (compression == 0) { + if (bytestream2_get_bytes_left(gb) < uncompressed_size) + return AVERROR_INVALIDDATA; + bytestream2_init(&dc, avpkt->data + bytestream2_tell(gb), uncompressed_size); + } else if (compression == 2) { + av_assert0(0); + } else { + return AVERROR_INVALIDDATA; + } + + + dst = frame->data[0] + frame->linesize[0] * (y + h - 1) + x; + b1 = s->frame1->data[0] + s->frame1->linesize[0] * (y + h - 1) + x; + b2 = s->frame2->data[0] + s->frame2->linesize[0] * (y + h - 1) + x; + cx = 0; + while (bytestream2_get_bytes_left(&dc) > 0) { + int type = bytestream2_get_byte(&dc); + int len = bytestream2_get_byte(&dc); + unsigned fill; + + switch (type) { + case 1: + while (len > 0) { + dst[cx] = b2[cx]; + cx++; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 2: + while (len > 0) { + int v0 = b1[cx]; + int v1 = b2[cx]; + + b2[cx] = v0; + dst[cx] = v0; + b1[cx] = v1; + cx++; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 3: + while (len > 0) { + fill = bytestream2_get_byte(&dc); + AV_WL32(b1 + cx, AV_RL32(b2 + cx)); + AV_WL32(b2 + cx, fill); + dst[cx] = fill; + cx++; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 4: + fill = bytestream2_get_byte(&dc); + while (len > 0) { + AV_WL32(b1 + cx, AV_RL32(b2 + cx)); + AV_WL32(dst + cx, fill); + AV_WL32(b2 + cx, fill); + cx++; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 5: + fill = bytestream2_get_le16(&dc); + while (len > 0) { + AV_WL16(dst + cx, fill); + cx += 2; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 6: + fill = bytestream2_get_le24(&dc); + while (len > 0) { + AV_WL24(dst + cx, fill); + cx += 3; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 7: + fill = bytestream2_get_le32(&dc); + while (len > 0) { + AV_WL32(dst + cx, fill); + AV_WL32(b1 + cx, AV_RL32(b2 + cx)); + AV_WL32(b2 + cx, fill); + cx += 4; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 10: + while (len > 0) { + AV_WL32(dst + cx, AV_RL32(b2 + cx)); + cx += 4; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 12: + while (len > 0) { + unsigned v0, v1; + + v0 = AV_RL32(b2 + cx); + v1 = AV_RL32(b1 + cx); + AV_WL32(b2 + cx, v1); + AV_WL32(dst + cx, v1); + AV_WL32(b1 + cx, v0); + cx += 4; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + case 13: + while (len > 0) { + fill = bytestream2_get_le32(&dc); + AV_WL32(dst + cx, fill); + AV_WL32(b1 + cx, AV_RL32(b2 + cx)); + AV_WL32(b2 + cx, fill); + cx += 4; + if (cx >= w) { + cx = 0; + dst -= frame->linesize[0]; + b1 -= s->frame1->linesize[0]; + b2 -= s->frame2->linesize[0]; + } + len--; + } + break; + default: + return AVERROR_INVALIDDATA; + } + } + + bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos)); + + return 0; +} + +static int decode_kfrm(AVCodecContext *avctx, AVFrame *frame, + AVPacket *avpkt, unsigned size) +{ + RASCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + uint32_t *pal; + uint8_t *dst, *cptr; + unsigned pos; + int zret; + + pos = bytestream2_tell(gb); + bytestream2_skip(gb, 72); + + pal = (uint32_t *)frame->data[1]; + for (int i = 0; i < 256; i++) { + pal[i] = bytestream2_get_le32(gb) | 0xFF000000u; + } + + zret = inflateReset(&s->zstream); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret); + return AVERROR_EXTERNAL; + } + + s->zstream.next_in = avpkt->data + bytestream2_tell(gb); + s->zstream.avail_in = bytestream2_get_bytes_left(gb); + + dst = frame->data[0] + (avctx->height - 1) * frame->linesize[0]; + cptr = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0]; + for (int i = 0; i < avctx->height; i++) { + s->zstream.next_out = dst; + s->zstream.avail_out = FFALIGN(avctx->width, 4); + + zret = inflate(&s->zstream, Z_SYNC_FLUSH); + if (zret != Z_OK && zret != Z_STREAM_END) { + av_log(avctx, AV_LOG_ERROR, + "Inflate failed with return code: %d.\n", zret); + return AVERROR_INVALIDDATA; + } + + memcpy(cptr, dst, FFALIGN(avctx->width, 4)); + dst -= frame->linesize[0]; + cptr -= s->frame2->linesize[0]; + } + + dst = s->frame1->data[0] + (avctx->height - 1) * frame->linesize[0]; + for (int i = 0; i < avctx->height; i++) { + s->zstream.next_out = dst; + s->zstream.avail_out = FFALIGN(avctx->width, 4); + + zret = inflate(&s->zstream, Z_SYNC_FLUSH); + if (zret != Z_OK && zret != Z_STREAM_END) { + av_log(avctx, AV_LOG_ERROR, + "Inflate failed with return code: %d.\n", zret); + return AVERROR_INVALIDDATA; + } + + dst -= s->frame1->linesize[0]; + } + + bytestream2_skip(gb, size - (bytestream2_tell(gb) - pos)); + + return 0; +} + +static int init_frames(AVCodecContext *avctx) +{ + RASCContext *s = avctx->priv_data; + int ret; + + if (!s->frame1 || !s->frame1->data[0]) { + av_frame_free(&s->frame1); + + s->frame1 = av_frame_alloc(); + if (!s->frame1) + return AVERROR(ENOMEM); + + if ((ret = ff_get_buffer(avctx, s->frame1, 0)) < 0) + return ret; + clear_plane(avctx, s->frame1); + } + + if (!s->frame2 || !s->frame2->data[0]) { + av_frame_free(&s->frame2); + + s->frame2 = av_frame_alloc(); + if (!s->frame2) + return AVERROR(ENOMEM); + + if ((ret = ff_get_buffer(avctx, s->frame2, 0)) < 0) + return ret; + clear_plane(avctx, s->frame2); + } + + if (!s->frame) { + s->frame = av_frame_alloc(); + if (!s->frame) + return AVERROR(ENOMEM); + } + + if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) + return ret; + + return 0; +} + +static int decode_frame(AVCodecContext *avctx, + void *data, int *got_frame, + AVPacket *avpkt) +{ + RASCContext *s = avctx->priv_data; + GetByteContext *gb = &s->gb; + int ret; + + ret = inflateReset(&s->zstream); + if (ret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret); + return AVERROR_EXTERNAL; + } + + ret = init_frames(avctx); + if (ret < 0) + return ret; + + bytestream2_init(gb, avpkt->data, avpkt->size); + + while (bytestream2_get_bytes_left(gb) > 0) { + unsigned type, size = 0; + + type = bytestream2_get_le32(gb); + s->bndl = 0; + if (type == KBND || type == BNDL) { + s->bndl = type == BNDL; + type = bytestream2_get_le32(gb); + } + if (type != EMPT) + size = bytestream2_get_le32(gb); + if (bytestream2_get_bytes_left(gb) < size) + return AVERROR_INVALIDDATA; + + switch (type) { + case FINT: + ret = decode_fint(avctx, s->frame, avpkt, size); + break; + case KFRM: + ret = decode_kfrm(avctx, s->frame, avpkt, size); + break; + case DLTA: + ret = decode_dlta(avctx, s->frame, avpkt, size); + break; + case MOVE: + ret = decode_move(avctx, s->frame, avpkt, size); + break; + case EMPT: + return avpkt->size; + default: + bytestream2_skip(gb, size); + } + + if (ret < 0) + return ret; + } + + if ((ret = av_frame_ref(data, s->frame)) < 0) + return ret; + + *got_frame = 1; + + return avpkt->size; +} + +static av_cold int decode_init(AVCodecContext *avctx) +{ + RASCContext *s = avctx->priv_data; + int zret; + + avctx->pix_fmt = AV_PIX_FMT_PAL8; + + s->zstream.zalloc = Z_NULL; + s->zstream.zfree = Z_NULL; + s->zstream.opaque = Z_NULL; + zret = inflateInit(&s->zstream); + if (zret != Z_OK) { + av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); + return AVERROR_EXTERNAL; + } + + return 0; +} + +static av_cold int decode_close(AVCodecContext *avctx) +{ + RASCContext *s = avctx->priv_data; + + av_freep(&s->delta); + s->delta_size = 0; + av_frame_free(&s->frame); + av_frame_free(&s->frame1); + av_frame_free(&s->frame2); + inflateEnd(&s->zstream); + + return 0; +} + +static void decode_flush(AVCodecContext *avctx) +{ + RASCContext *s = avctx->priv_data; + + av_frame_free(&s->frame1); + av_frame_free(&s->frame2); +} + +AVCodec ff_rasc_decoder = { + .name = "rasc", + .long_name = NULL_IF_CONFIG_SMALL("RemotelyAnywhere Screen Capture"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_RASC, + .priv_data_size = sizeof(RASCContext), + .init = decode_init, + .close = decode_close, + .decode = decode_frame, + .flush = decode_flush, + .capabilities = AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | + FF_CODEC_CAP_INIT_CLEANUP, +}; diff --git a/libavformat/riff.c b/libavformat/riff.c index 5f1aafe514..3907e1a9f3 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -474,6 +474,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { AV_CODEC_ID_PROSUMER, MKTAG('B', 'T', '2', '0') }, { AV_CODEC_ID_MWSC, MKTAG('M', 'W', 'S', 'C') }, { AV_CODEC_ID_WCMV, MKTAG('W', 'C', 'M', 'V') }, + { AV_CODEC_ID_RASC, MKTAG('R', 'A', 'S', 'C') }, { AV_CODEC_ID_NONE, 0 } };