From patchwork Sat Aug 25 20:03:55 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: 10138 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp4790440jap; Sat, 25 Aug 2018 13:04:16 -0700 (PDT) X-Google-Smtp-Source: ANB0VdajZVf3TT49mO920nNy1PGaDPcNw3EsVOR2UlrMmIavXi7dr7Li9nEm1GXPxFfwdqToj5wU X-Received: by 2002:a1c:1609:: with SMTP id 9-v6mr1756493wmw.12.1535227456511; Sat, 25 Aug 2018 13:04:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535227456; cv=none; d=google.com; s=arc-20160816; b=zDibwMGlznBwPQN+gzB+d0XVGL9UaTCKyBYS1m3DDJVVDTjr6/Di05QhZs7cLI96gJ gFQTfyc2FcOLUO+48phIXNcg6ZsSsUXsMcsBOdPJc1N6h+8Dq0Lhj9tMvBXzBQotT/BV alT9z4BYBnIGqqbEkCzP5IUGkN+9ae94xgTTqfwoqFx4AmqE04kFea9depwO+R7SV5GE GQU7mXVD8XWlOdvsxIFYzUjLg0UAE5e/oU5W75KeeiBdY07RU6V606jCWCh0IL7Bh4fj MSYiSx+7h2WzhlbbTBsdMH930JNdCc32pKy4eStlD+eef3o5GIqUsK8BiNIM7I/94/z7 H5Ig== 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=+XBoZW4qEuDliX/QUI6pp8k51HBFkFFf5kFDH9BFXnE=; b=JTd8gEgiqawcU0dlVRYZQRUK77H/paJUlLAm2taOZnvMEkKNiIIiFbiZP7O4eYkhks CVJUlaaAge6+CY/DTw6PYAKYFEIhYi/dJQxMc7oEr4AV2cH6ovGs/X/dcGTLqjiYE8Xj S0orBxNcZV7qpQV0j7KXbA3TPGSBrnO3uk6F07a10kF7+ugDBxlPgmJGSYEhfjC4k6X3 Xwp0XzJiBTPt6T8u1Mmoj4KwmafYJO7hArmfyAxqzlePKyUECWO5XcdF//j6Zmh6FNEN 8P4o/vJC0nX8cnn3m6QA2+tbePqldUAf+GABors+cbDhtWYKsIaZQSMoZHTcwIeJiEtQ iIWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=YbkWGxjS; 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 l10-v6si3456190wmf.200.2018.08.25.13.04.15; Sat, 25 Aug 2018 13:04:16 -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=YbkWGxjS; 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 EEC78689AB5; Sat, 25 Aug 2018 23:04:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B099E6808E6 for ; Sat, 25 Aug 2018 23:04:05 +0300 (EEST) Received: by mail-wm0-f67.google.com with SMTP id j192-v6so4659952wmj.1 for ; Sat, 25 Aug 2018 13:04:08 -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=nhE5KekPDX+2Ap3NwlqH5qokeBC9a7zZA0+d336Bv1Y=; b=YbkWGxjSNqq3e4JCQV97BmX2/KZwEeNLtozYDqu2qQHPFoNSJT1BI5EXa0kqpwdF+A Zr0MrXPtRFAIW9O61E9NedLobc25vgoqViqQqId5hi6xFsZa4yNBg70GmvGaM6d4TbbR u3otOe1DZf54nSWdaCoWyEpe7FMlEE/8vKydJSK3UhNrcqx3IVEUNS8mvYIH7ZJHVTNC YVoDJW1FlhFuXse7uVSGHesHjBA/OTusd3oZpwTLa1TC/I2oBReBBTr2pIbiIgM2nIR9 Mbptd3QAEUxa6+dWM98HrHeGZOO4eDvpABY08MHeg1FyTyufwcl/k5dx3fZS09Agzbfg 6clQ== 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=nhE5KekPDX+2Ap3NwlqH5qokeBC9a7zZA0+d336Bv1Y=; b=Q0zzXFwYUVSKI4KmdIsWM1272n3oNJvPykt/gAe5jsotKwyHvUD+E83dfDlgXUaJsM hH+mK3ev12KCTJhEHuelUQpA5QJxTCxXWbcOYFemiziqJbG/Z8icOVF1kbIled0SxPIN vEQtgSCwyBxcyoSqgCJuiIoqc/Vngsp+nyv5c0oiOZ+mWiIt8RM4BOR9hf/5l+A6M7Nu x63WH+AUrRVK+ew8LiEj1QW6Y67pqKc3C3dZoPzXAvsSTZ81YzQfCSUMiGNQ1/2YlOhf iJtufCEfcWuOCC4UJbX9OcqTxj5RCtTeJ6kvQGyk4D/22o0MEBLUYsXF7rrxERn5kdga TX8w== X-Gm-Message-State: APzg51AmPUciC5UgoJ2bZmMdISgmpMJfNFLHlGCxeqF3BKV//v/+yz5s Ejew99uFPAN3aiyrM5WG9DwJysDx X-Received: by 2002:a1c:48d:: with SMTP id 135-v6mr1877130wme.128.1535227447517; Sat, 25 Aug 2018 13:04:07 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id z10-v6sm1408927wrv.22.2018.08.25.13.04.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Aug 2018 13:04:07 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Aug 2018 22:03:55 +0200 Message-Id: <20180825200356.28361-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec: add MatchWare Screen Capture Codec 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 --- configure | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/mwsc.c | 192 ++++++++++++++++++++++++++++++++++++++++ libavformat/riff.c | 1 + 7 files changed, 204 insertions(+) create mode 100644 libavcodec/mwsc.c diff --git a/configure b/configure index b9c9d0b307..0c1f6a79a7 100755 --- a/configure +++ b/configure @@ -2679,6 +2679,7 @@ msmpeg4v3_decoder_select="h263_decoder" msmpeg4v3_encoder_select="h263_encoder" mss2_decoder_select="mpegvideo qpeldsp vc1_decoder" mts2_decoder_select="mss34dsp" +mwsc_decoder_deps="zlib" mxpeg_decoder_select="mjpeg_decoder" nellymoser_decoder_select="mdct sinewin" nellymoser_encoder_select="audio_frame_queue mdct sinewin" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 9a309c348e..aee4f5431a 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -482,6 +482,7 @@ OBJS-$(CONFIG_MSZH_DECODER) += lcldec.o OBJS-$(CONFIG_MTS2_DECODER) += mss4.o OBJS-$(CONFIG_MVC1_DECODER) += mvcdec.o OBJS-$(CONFIG_MVC2_DECODER) += mvcdec.o +OBJS-$(CONFIG_MWSC_DECODER) += mwsc.o OBJS-$(CONFIG_MXPEG_DECODER) += mxpegdec.o OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index b1d1ef26c0..d41868c591 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -212,6 +212,7 @@ extern AVCodec ff_mszh_decoder; extern AVCodec ff_mts2_decoder; extern AVCodec ff_mvc1_decoder; extern AVCodec ff_mvc2_decoder; +extern AVCodec ff_mwsc_decoder; extern AVCodec ff_mxpeg_decoder; extern AVCodec ff_nuv_decoder; extern AVCodec ff_paf_video_decoder; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 86a658a233..be41b8cf0e 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -449,6 +449,7 @@ enum AVCodecID { AV_CODEC_ID_FITS, AV_CODEC_ID_IMM4, AV_CODEC_ID_PROSUMER, + AV_CODEC_ID_MWSC, /* 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 e611183599..129d0f1aac 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -1668,6 +1668,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("Brooktree ProSumer Video"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_MWSC, + .type = AVMEDIA_TYPE_VIDEO, + .name = "mwsc", + .long_name = NULL_IF_CONFIG_SMALL("MatchWare Screen Capture Codec"), + .props = AV_CODEC_PROP_LOSSLESS, + }, /* various PCM "codecs" */ { diff --git a/libavcodec/mwsc.c b/libavcodec/mwsc.c new file mode 100644 index 0000000000..c1a5aca3c0 --- /dev/null +++ b/libavcodec/mwsc.c @@ -0,0 +1,192 @@ +/* + * MatchWare Screen Capture Codec 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 "avcodec.h" +#include "bytestream.h" +#include "internal.h" + +#include + +typedef struct MWSCContext { + unsigned int decomp_size; + uint8_t *decomp_buf; + z_stream zstream; + AVFrame *prev_frame; +} MWSCContext; + +static int rle_uncompress(GetByteContext *gb, PutByteContext *pb, GetByteContext *gbp, + int width, int height, int stride, int pb_linesize, int gbp_linesize) +{ + int intra = 1, w = 0; + + bytestream2_seek_p(pb, (height - 1) * pb_linesize, SEEK_SET); + + while (bytestream2_get_bytes_left(gb) > 0) { + uint32_t fill = bytestream2_get_le24(gb); + unsigned run = bytestream2_get_byte(gb); + + if (run == 0) { + run = bytestream2_get_le32(gb); + for (int j = 0; j < run; j++, w++) { + if (w == width) { + w = 0; + bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR); + } + bytestream2_put_le24(pb, fill); + } + } else if (run == 255) { + int pos = bytestream2_tell_p(pb); + + bytestream2_seek(gbp, pos, SEEK_SET); + for (int j = 0; j < fill; j++, w++) { + if (w == width) { + w = 0; + bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR); + bytestream2_seek(gbp, -(gbp_linesize + stride), SEEK_CUR); + } + bytestream2_put_le24(pb, bytestream2_get_le24(gbp)); + } + + intra = 0; + } else { + for (int j = 0; j < run; j++, w++) { + if (w == width) { + w = 0; + bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR); + } + bytestream2_put_le24(pb, fill); + } + } + } + + return intra; +} + +static int decode_frame(AVCodecContext *avctx, + void *data, int *got_frame, + AVPacket *avpkt) +{ + MWSCContext *s = avctx->priv_data; + AVFrame *frame = data; + uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; + GetByteContext gb; + GetByteContext gbp; + PutByteContext pb; + 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; + } + s->zstream.next_in = buf; + s->zstream.avail_in = buf_size; + s->zstream.next_out = s->decomp_buf; + s->zstream.avail_out = s->decomp_size; + ret = inflate(&s->zstream, Z_FINISH); + if (ret != Z_STREAM_END) { + av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret); + return AVERROR_EXTERNAL; + } + + if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) + return ret; + + bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out); + bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * frame->linesize[0]); + bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]); + + frame->key_frame = rle_uncompress(&gb, &pb, &gbp, avctx->width, avctx->height, avctx->width * 3, + frame->linesize[0], s->prev_frame->linesize[0]); + + frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P; + + av_frame_unref(s->prev_frame); + if ((ret = av_frame_ref(s->prev_frame, frame)) < 0) + return ret; + + *got_frame = 1; + + return avpkt->size; +} + +static av_cold int decode_init(AVCodecContext *avctx) +{ + MWSCContext *s = avctx->priv_data; + int64_t size; + int zret; + + avctx->pix_fmt = AV_PIX_FMT_BGR24; + + size = 8LL * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32); + if (size >= INT32_MAX) + return AVERROR_INVALIDDATA; + s->decomp_size = size; + if (!(s->decomp_buf = av_malloc(s->decomp_size))) + return AVERROR(ENOMEM); + + 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; + } + + s->prev_frame = av_frame_alloc(); + if (!s->prev_frame) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold int decode_close(AVCodecContext *avctx) +{ + MWSCContext *s = avctx->priv_data; + + av_frame_free(&s->prev_frame); + av_freep(&s->decomp_buf); + s->decomp_size = 0; + inflateEnd(&s->zstream); + + return 0; +} + +AVCodec ff_mwsc_decoder = { + .name = "mwsc", + .long_name = NULL_IF_CONFIG_SMALL("MatchWare Screen Capture Codec"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_MWSC, + .priv_data_size = sizeof(MWSCContext), + .init = decode_init, + .close = decode_close, + .decode = decode_frame, + .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 cf27d0dfd5..aef3c047ac 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -472,6 +472,7 @@ const AVCodecTag ff_codec_bmp_tags[] = { { AV_CODEC_ID_SRGC, MKTAG('S', 'R', 'G', 'C') }, { AV_CODEC_ID_IMM4, MKTAG('I', 'M', 'M', '4') }, { AV_CODEC_ID_PROSUMER, MKTAG('B', 'T', '2', '0') }, + { AV_CODEC_ID_MWSC, MKTAG('M', 'W', 'S', 'C') }, { AV_CODEC_ID_NONE, 0 } };