2017-04-15 10:57:07 +02:00
|
|
|
/*
|
|
|
|
* Mandsoft Screen Capture Codec decoder
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 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"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2017-04-15 10:57:07 +02:00
|
|
|
#include "internal.h"
|
2022-03-15 13:27:32 +02:00
|
|
|
#include "zlib_wrapper.h"
|
2017-04-15 10:57:07 +02:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
typedef struct MSCCContext {
|
|
|
|
unsigned bpp;
|
|
|
|
unsigned int decomp_size;
|
|
|
|
uint8_t *decomp_buf;
|
|
|
|
unsigned int uncomp_size;
|
|
|
|
uint8_t *uncomp_buf;
|
2022-03-15 13:27:32 +02:00
|
|
|
FFZStream zstream;
|
2017-04-22 15:51:52 +02:00
|
|
|
|
|
|
|
uint32_t pal[256];
|
2017-04-15 10:57:07 +02:00
|
|
|
} MSCCContext;
|
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)
|
2017-04-15 10:57:07 +02:00
|
|
|
{
|
2017-04-22 15:51:52 +02:00
|
|
|
MSCCContext *s = avctx->priv_data;
|
|
|
|
unsigned x = 0, y = 0;
|
|
|
|
|
2017-04-15 10:57:07 +02:00
|
|
|
while (bytestream2_get_bytes_left(gb) > 0) {
|
|
|
|
uint32_t fill;
|
|
|
|
int j;
|
|
|
|
unsigned run = bytestream2_get_byte(gb);
|
|
|
|
|
|
|
|
if (run) {
|
|
|
|
switch (avctx->bits_per_coded_sample) {
|
|
|
|
case 8:
|
|
|
|
fill = bytestream2_get_byte(gb);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
fill = bytestream2_get_le16(gb);
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
fill = bytestream2_get_le24(gb);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
fill = bytestream2_get_le32(gb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < run; j++) {
|
|
|
|
switch (avctx->bits_per_coded_sample) {
|
|
|
|
case 8:
|
|
|
|
bytestream2_put_byte(pb, fill);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
bytestream2_put_le16(pb, fill);
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
bytestream2_put_le24(pb, fill);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
bytestream2_put_le32(pb, fill);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 15:51:52 +02:00
|
|
|
x += run;
|
2017-04-15 10:57:07 +02:00
|
|
|
} else {
|
|
|
|
unsigned copy = bytestream2_get_byte(gb);
|
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
if (copy == 0) {
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
bytestream2_seek_p(pb, y * avctx->width * s->bpp, SEEK_SET);
|
|
|
|
} else if (copy == 1) {
|
2017-04-15 10:57:07 +02:00
|
|
|
return 0;
|
|
|
|
} else if (copy == 2) {
|
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
x += bytestream2_get_byte(gb);
|
|
|
|
y += bytestream2_get_byte(gb);
|
2017-04-15 10:57:07 +02:00
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
bytestream2_seek_p(pb, y * avctx->width * s->bpp + x * s->bpp, SEEK_SET);
|
2017-04-15 10:57:07 +02:00
|
|
|
} else {
|
|
|
|
for (j = 0; j < copy; j++) {
|
|
|
|
switch (avctx->bits_per_coded_sample) {
|
|
|
|
case 8:
|
|
|
|
bytestream2_put_byte(pb, bytestream2_get_byte(gb));
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
bytestream2_put_le16(pb, bytestream2_get_le16(gb));
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
bytestream2_put_le24(pb, bytestream2_get_le24(gb));
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
bytestream2_put_le32(pb, bytestream2_get_le32(gb));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 15:51:52 +02:00
|
|
|
|
|
|
|
if (s->bpp == 1 && (copy & 1))
|
|
|
|
bytestream2_skip(gb, 1);
|
|
|
|
x += copy;
|
2017-04-15 10:57:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
2017-04-15 10:57:07 +02:00
|
|
|
{
|
|
|
|
MSCCContext *s = avctx->priv_data;
|
2022-03-15 13:27:32 +02:00
|
|
|
z_stream *const zstream = &s->zstream.zstream;
|
2017-04-16 18:44:57 +02:00
|
|
|
uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2017-04-15 10:57:07 +02:00
|
|
|
GetByteContext gb;
|
|
|
|
PutByteContext pb;
|
|
|
|
int ret, j;
|
|
|
|
|
|
|
|
if (avpkt->size < 3)
|
2017-04-22 15:51:52 +02:00
|
|
|
return buf_size;
|
|
|
|
|
2017-04-15 10:57:07 +02:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-04-16 18:44:57 +02:00
|
|
|
if (avctx->codec_id == AV_CODEC_ID_MSCC) {
|
|
|
|
avpkt->data[2] ^= avpkt->data[0];
|
|
|
|
buf += 2;
|
|
|
|
buf_size -= 2;
|
|
|
|
}
|
2017-04-15 10:57:07 +02:00
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t size;
|
2017-04-22 15:51:52 +02:00
|
|
|
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
|
|
|
|
|
|
|
|
if (pal && size == AVPALETTE_SIZE) {
|
|
|
|
frame->palette_has_changed = 1;
|
|
|
|
for (j = 0; j < 256; j++)
|
|
|
|
s->pal[j] = 0xFF000000 | AV_RL32(pal + j * 4);
|
|
|
|
} else if (pal) {
|
2021-04-14 14:59:32 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Palette size %"SIZE_SPECIFIER" is wrong\n", size);
|
2017-04-22 15:51:52 +02:00
|
|
|
}
|
|
|
|
memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:27:32 +02:00
|
|
|
ret = inflateReset(zstream);
|
2017-04-15 10:57:07 +02:00
|
|
|
if (ret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
2022-03-15 13:27:32 +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);
|
2017-04-15 10:57:07 +02:00
|
|
|
if (ret != Z_STREAM_END) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2022-03-15 13:27:32 +02:00
|
|
|
bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
|
2017-04-15 10:57:07 +02:00
|
|
|
bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
|
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
ret = rle_uncompress(avctx, &gb, &pb);
|
2017-04-15 10:57:07 +02:00
|
|
|
if (ret)
|
|
|
|
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->key_frame = 1;
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
|
|
|
|
*got_frame = 1;
|
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MSCCContext *s = avctx->priv_data;
|
2022-03-15 13:27:32 +02:00
|
|
|
int stride;
|
2017-04-15 10:57:07 +02:00
|
|
|
|
|
|
|
switch (avctx->bits_per_coded_sample) {
|
2017-04-22 15:51:52 +02:00
|
|
|
case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
|
2017-04-15 10:57:07 +02:00
|
|
|
case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
|
|
|
|
case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
|
|
|
|
case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; 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;
|
2017-04-22 15:51:52 +02:00
|
|
|
stride = 4 * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
|
2017-04-15 10:57:07 +02:00
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
s->decomp_size = 2 * avctx->height * stride;
|
2017-04-15 10:57:07 +02:00
|
|
|
if (!(s->decomp_buf = av_malloc(s->decomp_size)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2017-04-22 15:51:52 +02:00
|
|
|
s->uncomp_size = avctx->height * stride;
|
2017-04-15 10:57:07 +02:00
|
|
|
if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2022-03-15 13:27:32 +02:00
|
|
|
return ff_inflate_init(&s->zstream, avctx);
|
2017-04-15 10:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int decode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
MSCCContext *s = avctx->priv_data;
|
|
|
|
|
|
|
|
av_freep(&s->decomp_buf);
|
|
|
|
s->decomp_size = 0;
|
|
|
|
av_freep(&s->uncomp_buf);
|
|
|
|
s->uncomp_size = 0;
|
2022-03-15 13:27:32 +02:00
|
|
|
ff_inflate_end(&s->zstream);
|
2017-04-15 10:57:07 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_mscc_decoder = {
|
|
|
|
.p.name = "mscc",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Mandsoft Screen Capture Codec"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_MSCC,
|
2017-04-15 10:57:07 +02:00
|
|
|
.priv_data_size = sizeof(MSCCContext),
|
|
|
|
.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,
|
2021-05-05 20:50:16 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
|
2017-04-15 10:57:07 +02:00
|
|
|
};
|
2017-04-16 18:44:57 +02:00
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_srgc_decoder = {
|
|
|
|
.p.name = "srgc",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Screen Recorder Gold Codec"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_SRGC,
|
2017-04-16 18:44:57 +02:00
|
|
|
.priv_data_size = sizeof(MSCCContext),
|
|
|
|
.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,
|
2021-05-05 20:50:16 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
|
2017-04-16 18:44:57 +02:00
|
|
|
};
|