2018-08-24 17:01:00 +02:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2018-08-24 17:01:00 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:28:16 +02:00
|
|
|
#include "decode.h"
|
2022-03-15 13:13:19 +02:00
|
|
|
#include "zlib_wrapper.h"
|
2018-08-24 17:01:00 +02:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
typedef struct MWSCContext {
|
|
|
|
unsigned int decomp_size;
|
|
|
|
uint8_t *decomp_buf;
|
|
|
|
AVFrame *prev_frame;
|
2022-03-15 13:13:19 +02:00
|
|
|
FFZStream zstream;
|
2018-08-24 17:01:00 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2018-08-24 17:01:00 +02:00
|
|
|
{
|
|
|
|
MWSCContext *s = avctx->priv_data;
|
2022-03-15 13:13:19 +02:00
|
|
|
z_stream *const zstream = &s->zstream.zstream;
|
2022-07-04 12:03:01 +02:00
|
|
|
const uint8_t *buf = avpkt->data;
|
2018-08-24 17:01:00 +02:00
|
|
|
int buf_size = avpkt->size;
|
|
|
|
GetByteContext gb;
|
|
|
|
GetByteContext gbp;
|
|
|
|
PutByteContext pb;
|
|
|
|
int ret;
|
|
|
|
|
2022-03-15 13:13:19 +02:00
|
|
|
ret = inflateReset(zstream);
|
2018-08-24 17:01:00 +02:00
|
|
|
if (ret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
|
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
2022-03-15 13:13:19 +02:00
|
|
|
zstream->next_in = buf;
|
|
|
|
zstream->avail_in = buf_size;
|
|
|
|
zstream->next_out = s->decomp_buf;
|
|
|
|
zstream->avail_out = s->decomp_size;
|
|
|
|
ret = inflate(zstream, Z_FINISH);
|
2018-08-24 17:01:00 +02:00
|
|
|
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;
|
|
|
|
|
2022-03-15 13:13:19 +02:00
|
|
|
bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
|
2018-08-24 17:01:00 +02:00
|
|
|
bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
|
|
|
|
bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
|
|
|
|
|
2023-04-12 18:58:54 +02:00
|
|
|
if (rle_uncompress(&gb, &pb, &gbp, avctx->width, avctx->height, avctx->width * 3,
|
|
|
|
frame->linesize[0], s->prev_frame->linesize[0]))
|
|
|
|
frame->flags |= AV_FRAME_FLAG_KEY;
|
|
|
|
else
|
|
|
|
frame->flags &= ~AV_FRAME_FLAG_KEY;
|
2018-08-24 17:01:00 +02:00
|
|
|
|
2023-04-12 18:58:54 +02:00
|
|
|
frame->pict_type = (frame->flags & AV_FRAME_FLAG_KEY) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
|
2018-08-24 17:01:00 +02:00
|
|
|
|
2023-09-07 12:02:26 +02:00
|
|
|
if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
|
2018-08-24 17:01:00 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
*got_frame = 1;
|
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MWSCContext *s = avctx->priv_data;
|
|
|
|
int64_t size;
|
|
|
|
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
|
|
|
|
|
|
|
size = 32LL * avctx->height * avctx->width;
|
|
|
|
if (size >= INT32_MAX)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
s->decomp_size = size;
|
|
|
|
if (!(s->decomp_buf = av_malloc(s->decomp_size)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->prev_frame = av_frame_alloc();
|
|
|
|
if (!s->prev_frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2022-03-15 13:13:19 +02:00
|
|
|
return ff_inflate_init(&s->zstream, avctx);
|
2018-08-24 17:01:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-03-15 13:13:19 +02:00
|
|
|
ff_inflate_end(&s->zstream);
|
2018-08-24 17:01:00 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_mwsc_decoder = {
|
|
|
|
.p.name = "mwsc",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("MatchWare Screen Capture Codec"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_MWSC,
|
2018-08-24 17:01:00 +02:00
|
|
|
.priv_data_size = sizeof(MWSCContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_close,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2018-08-24 17:01:00 +02:00
|
|
|
};
|