[FFmpeg-devel] avcodec: add MatchWare Screen Capture Codec

Submitted by Paul B Mahol on Aug. 24, 2018, 6:48 p.m.

Details

Message ID 20180824184815.22395-1-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol Aug. 24, 2018, 6:48 p.m.
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/Makefile     |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/avcodec.h    |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/mwsc.c       | 174 ++++++++++++++++++++++++++++++++++++++++
 libavformat/riff.c      |   1 +
 6 files changed, 185 insertions(+)
 create mode 100644 libavcodec/mwsc.c

Comments

James Almer Aug. 24, 2018, 7:42 p.m.
On 8/24/2018 3:48 PM, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavcodec/Makefile     |   1 +
>  libavcodec/allcodecs.c  |   1 +
>  libavcodec/avcodec.h    |   1 +
>  libavcodec/codec_desc.c |   7 ++
>  libavcodec/mwsc.c       | 174 ++++++++++++++++++++++++++++++++++++++++
>  libavformat/riff.c      |   1 +
>  6 files changed, 185 insertions(+)
>  create mode 100644 libavcodec/mwsc.c
> 
> 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 2a4be2ca4f..b327284449 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..5ae8330500
> --- /dev/null
> +++ b/libavcodec/mwsc.c
> @@ -0,0 +1,174 @@
> +/*
> + * 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 <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +
> +#include "avcodec.h"
> +#include "bytestream.h"
> +#include "internal.h"
> +
> +#include <zlib.h>

Needs a zlib dependency in configure, then.

> +
> +typedef struct MWSCContext {
> +    unsigned          bpp;
> +    unsigned int      decomp_size;
> +    uint8_t          *decomp_buf;
> +    unsigned int      uncomp_size;
> +    uint8_t          *uncomp_buf;
> +    z_stream          zstream;
> +} MWSCContext;
> +
> +static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb, int bpp)
> +{
> +    int intra = 1;
> +
> +    while (bytestream2_get_bytes_left(gb) > 0) {
> +        uint32_t fill = bytestream2_get_le24(gb);
> +        unsigned run = bytestream2_get_byte(gb);
> +        int j;
> +
> +        if (run == 0) {
> +            run = bytestream2_get_le32(gb);
> +            for (j = 0; j < run; j++) {
> +                bytestream2_put_le24(pb, fill);
> +            }
> +        } else if (run == 255) {
> +            bytestream2_skip_p(pb, fill * 3);
> +            intra = 0;
> +        } else {
> +            for (j = 0; j < run; j++) {
> +                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;
> +    PutByteContext pb;
> +    int ret, j;
> +
> +    ret = inflateReset(&s->zstream);
> +    if (ret != Z_OK) {
> +        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
> +        return AVERROR_UNKNOWN;

AVERROR_EXTERNAL. Same for all the cases below.

> +    }
> +    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_UNKNOWN;
> +    }
> +
> +    bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
> +    bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
> +
> +    frame->key_frame = rle_uncompress(avctx, &gb, &pb, s->bpp);
> +
> +    if ((ret = ff_reget_buffer(avctx, frame)) < 0)
> +        return ret;
> +
> +    for (j = 0; j < avctx->height; j++) {
> +        memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
> +               s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
> +    }
> +
> +    frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
> +
> +    *got_frame = 1;
> +
> +    return avpkt->size;
> +}
> +
> +static av_cold int decode_init(AVCodecContext *avctx)
> +{
> +    MWSCContext *s = avctx->priv_data;
> +    int zret;
> +
> +    switch (avctx->bits_per_coded_sample) {
> +    case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24;  break;
> +    default:
> +        av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);

Why %i?

> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    s->bpp = avctx->bits_per_coded_sample >> 3;
> +    memset(&s->zstream, 0, sizeof(z_stream));

It's already zeroed.

> +
> +    s->decomp_size = 8 * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);

avctx->height and avctx->width are not checked for sane values. And even
then, couldn't this overflow?

> +    if (!(s->decomp_buf = av_malloc(s->decomp_size)))
> +        return AVERROR(ENOMEM);
> +
> +    s->uncomp_size = 4 * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);

Same.

> +    if (!(s->uncomp_buf = av_malloc(s->uncomp_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_UNKNOWN;
> +    }
> +
> +    return 0;
> +}
> +
> +static av_cold int decode_close(AVCodecContext *avctx)
> +{
> +    MWSCContext *s = avctx->priv_data;
> +
> +    av_freep(&s->decomp_buf);
> +    s->decomp_size = 0;
> +    av_freep(&s->uncomp_buf);
> +    s->uncomp_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,

FF_CODEC_CAP_INIT_THREADSAFE and FF_CODEC_CAP_INIT_CLEANUP in
AVCodec.caps_internal?

> +};
> 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 }
>  };
>  
>
Paul B Mahol Aug. 24, 2018, 7:48 p.m.
On 8/24/18, James Almer <jamrial@gmail.com> wrote:
> On 8/24/2018 3:48 PM, Paul B Mahol wrote:
>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>> ---
>>  libavcodec/Makefile     |   1 +
>>  libavcodec/allcodecs.c  |   1 +
>>  libavcodec/avcodec.h    |   1 +
>>  libavcodec/codec_desc.c |   7 ++
>>  libavcodec/mwsc.c       | 174 ++++++++++++++++++++++++++++++++++++++++
>>  libavformat/riff.c      |   1 +
>>  6 files changed, 185 insertions(+)
>>  create mode 100644 libavcodec/mwsc.c
>>

[...]

>> +
>> +    s->decomp_size = 8 * avctx->height * ((avctx->width *
>> avctx->bits_per_coded_sample + 31) / 32);
>
> avctx->height and avctx->width are not checked for sane values. And even
> then, couldn't this overflow?
>

What you propose as solution? IIRC width/height are already checked at
higher level,
no need to check them again because we do not change them here.
James Almer Aug. 24, 2018, 7:52 p.m.
On 8/24/2018 4:48 PM, Paul B Mahol wrote:
> On 8/24/18, James Almer <jamrial@gmail.com> wrote:
>> On 8/24/2018 3:48 PM, Paul B Mahol wrote:
>>> Signed-off-by: Paul B Mahol <onemda@gmail.com>
>>> ---
>>>  libavcodec/Makefile     |   1 +
>>>  libavcodec/allcodecs.c  |   1 +
>>>  libavcodec/avcodec.h    |   1 +
>>>  libavcodec/codec_desc.c |   7 ++
>>>  libavcodec/mwsc.c       | 174 ++++++++++++++++++++++++++++++++++++++++
>>>  libavformat/riff.c      |   1 +
>>>  6 files changed, 185 insertions(+)
>>>  create mode 100644 libavcodec/mwsc.c
>>>
> 
> [...]
> 
>>> +
>>> +    s->decomp_size = 8 * avctx->height * ((avctx->width *
>>> avctx->bits_per_coded_sample + 31) / 32);
>>
>> avctx->height and avctx->width are not checked for sane values. And even
>> then, couldn't this overflow?
>>
> 
> What you propose as solution? IIRC width/height are already checked at
> higher level,
> no need to check them again because we do not change them here.

At least check for overflow in this calculation. Use av_malloc_array(),
or check before setting s->decomp_size.

Patch hide | download patch | download mbox

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 2a4be2ca4f..b327284449 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..5ae8330500
--- /dev/null
+++ b/libavcodec/mwsc.c
@@ -0,0 +1,174 @@ 
+/*
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "avcodec.h"
+#include "bytestream.h"
+#include "internal.h"
+
+#include <zlib.h>
+
+typedef struct MWSCContext {
+    unsigned          bpp;
+    unsigned int      decomp_size;
+    uint8_t          *decomp_buf;
+    unsigned int      uncomp_size;
+    uint8_t          *uncomp_buf;
+    z_stream          zstream;
+} MWSCContext;
+
+static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb, int bpp)
+{
+    int intra = 1;
+
+    while (bytestream2_get_bytes_left(gb) > 0) {
+        uint32_t fill = bytestream2_get_le24(gb);
+        unsigned run = bytestream2_get_byte(gb);
+        int j;
+
+        if (run == 0) {
+            run = bytestream2_get_le32(gb);
+            for (j = 0; j < run; j++) {
+                bytestream2_put_le24(pb, fill);
+            }
+        } else if (run == 255) {
+            bytestream2_skip_p(pb, fill * 3);
+            intra = 0;
+        } else {
+            for (j = 0; j < run; j++) {
+                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;
+    PutByteContext pb;
+    int ret, j;
+
+    ret = inflateReset(&s->zstream);
+    if (ret != Z_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
+        return AVERROR_UNKNOWN;
+    }
+    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_UNKNOWN;
+    }
+
+    bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
+    bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
+
+    frame->key_frame = rle_uncompress(avctx, &gb, &pb, s->bpp);
+
+    if ((ret = ff_reget_buffer(avctx, frame)) < 0)
+        return ret;
+
+    for (j = 0; j < avctx->height; j++) {
+        memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
+               s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
+    }
+
+    frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+
+    *got_frame = 1;
+
+    return avpkt->size;
+}
+
+static av_cold int decode_init(AVCodecContext *avctx)
+{
+    MWSCContext *s = avctx->priv_data;
+    int zret;
+
+    switch (avctx->bits_per_coded_sample) {
+    case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24;  break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
+        return AVERROR_INVALIDDATA;
+    }
+
+    s->bpp = avctx->bits_per_coded_sample >> 3;
+    memset(&s->zstream, 0, sizeof(z_stream));
+
+    s->decomp_size = 8 * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
+    if (!(s->decomp_buf = av_malloc(s->decomp_size)))
+        return AVERROR(ENOMEM);
+
+    s->uncomp_size = 4 * avctx->height * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
+    if (!(s->uncomp_buf = av_malloc(s->uncomp_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_UNKNOWN;
+    }
+
+    return 0;
+}
+
+static av_cold int decode_close(AVCodecContext *avctx)
+{
+    MWSCContext *s = avctx->priv_data;
+
+    av_freep(&s->decomp_buf);
+    s->decomp_size = 0;
+    av_freep(&s->uncomp_buf);
+    s->uncomp_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,
+};
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 }
 };