2015-09-25 00:36:50 +02:00
|
|
|
/*
|
|
|
|
* Screenpresso decoder
|
|
|
|
* Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
|
|
|
|
*
|
2015-10-03 12:22:41 +02:00
|
|
|
* This file is part of FFmpeg.
|
2015-09-25 00:36:50 +02:00
|
|
|
*
|
2015-10-03 12:22:41 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2015-09-25 00:36:50 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2015-10-03 12:22:41 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2015-09-25 00:36:50 +02:00
|
|
|
* 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
|
2015-10-03 12:22:41 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2015-09-25 00:36:50 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Screenpresso decoder
|
|
|
|
*
|
|
|
|
* Fourcc: SPV1
|
|
|
|
*
|
|
|
|
* Screenpresso simply horizontally flips and then deflates frames,
|
|
|
|
* alternating full pictures and deltas. Deltas are related to the currently
|
|
|
|
* rebuilt frame (not the reference), and since there is no coordinate system
|
|
|
|
* they contain exactly as many pixel as the keyframe.
|
|
|
|
*
|
2016-08-13 01:35:10 +02:00
|
|
|
* Supports: BGR0, BGR24, RGB555
|
2015-09-25 00:36:50 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/internal.h"
|
2015-11-09 14:28:13 +02:00
|
|
|
#include "libavutil/mem.h"
|
2015-09-25 00:36:50 +02:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct ScreenpressoContext {
|
|
|
|
AVFrame *current;
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* zlib interaction */
|
2015-09-25 00:36:50 +02:00
|
|
|
uint8_t *inflated_buf;
|
|
|
|
uLongf inflated_size;
|
|
|
|
} ScreenpressoContext;
|
|
|
|
|
|
|
|
static av_cold int screenpresso_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
ScreenpressoContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
av_frame_free(&ctx->current);
|
|
|
|
av_freep(&ctx->inflated_buf);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int screenpresso_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
ScreenpressoContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
/* These needs to be set to estimate uncompressed buffer */
|
|
|
|
int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
|
|
|
|
avctx->width, avctx->height);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate current frame */
|
|
|
|
ctx->current = av_frame_alloc();
|
|
|
|
if (!ctx->current)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-03-29 20:30:47 +02:00
|
|
|
/* Allocate maximum size possible, a full RGBA frame */
|
|
|
|
ctx->inflated_size = avctx->width * avctx->height * 4;
|
2015-10-19 14:34:09 +02:00
|
|
|
ctx->inflated_buf = av_malloc(ctx->inflated_size);
|
|
|
|
if (!ctx->inflated_buf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2015-09-25 00:36:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sum_delta_flipped(uint8_t *dst, int dst_linesize,
|
|
|
|
const uint8_t *src, int src_linesize,
|
|
|
|
int bytewidth, int height)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (; height > 0; height--) {
|
2020-01-29 20:20:29 +02:00
|
|
|
const uint8_t *src1 = &src[(height - 1) * src_linesize];
|
2015-09-25 00:36:50 +02:00
|
|
|
for (i = 0; i < bytewidth; i++)
|
2020-01-29 20:20:29 +02:00
|
|
|
dst[i] += src1[i];
|
2015-09-25 00:36:50 +02:00
|
|
|
dst += dst_linesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int screenpresso_decode_frame(AVCodecContext *avctx, void *data,
|
|
|
|
int *got_frame, AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
ScreenpressoContext *ctx = avctx->priv_data;
|
|
|
|
AVFrame *frame = data;
|
2015-10-19 14:34:09 +02:00
|
|
|
uLongf length = ctx->inflated_size;
|
2016-03-29 20:30:47 +02:00
|
|
|
int keyframe, component_size, src_linesize;
|
2015-09-25 00:36:50 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Size check */
|
|
|
|
if (avpkt->size < 3) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2016-03-29 20:30:48 +02:00
|
|
|
/* Compression level (4 bits) and keyframe information (1 bit) */
|
|
|
|
av_log(avctx, AV_LOG_DEBUG, "Compression level %d\n", avpkt->data[0] >> 4);
|
|
|
|
keyframe = avpkt->data[0] & 1;
|
2015-09-25 00:36:50 +02:00
|
|
|
|
2016-03-29 20:30:47 +02:00
|
|
|
/* Pixel size */
|
|
|
|
component_size = ((avpkt->data[1] >> 2) & 0x03) + 1;
|
|
|
|
switch (component_size) {
|
|
|
|
case 2:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
|
|
|
break;
|
|
|
|
case 4:
|
2016-08-13 01:35:10 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR0;
|
2016-03-29 20:30:47 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid bits per pixel value (%d)\n",
|
|
|
|
component_size);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-09-25 00:36:50 +02:00
|
|
|
/* Inflate the frame after the 2 byte header */
|
2015-10-19 14:34:09 +02:00
|
|
|
ret = uncompress(ctx->inflated_buf, &length,
|
2015-09-25 00:36:50 +02:00
|
|
|
avpkt->data + 2, avpkt->size - 2);
|
|
|
|
if (ret) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
|
|
|
|
return AVERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
ret = ff_reget_buffer(avctx, ctx->current, 0);
|
2015-10-19 14:34:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-03-29 20:30:47 +02:00
|
|
|
/* Codec has aligned strides */
|
|
|
|
src_linesize = FFALIGN(avctx->width * component_size, 4);
|
|
|
|
|
2015-09-25 00:36:50 +02:00
|
|
|
/* When a keyframe is found, copy it (flipped) */
|
|
|
|
if (keyframe)
|
|
|
|
av_image_copy_plane(ctx->current->data[0] +
|
|
|
|
ctx->current->linesize[0] * (avctx->height - 1),
|
|
|
|
-1 * ctx->current->linesize[0],
|
2016-03-29 20:30:47 +02:00
|
|
|
ctx->inflated_buf, src_linesize,
|
|
|
|
avctx->width * component_size, avctx->height);
|
2015-09-25 00:36:50 +02:00
|
|
|
/* Otherwise sum the delta on top of the current frame */
|
|
|
|
else
|
|
|
|
sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
|
2016-03-29 20:30:47 +02:00
|
|
|
ctx->inflated_buf, src_linesize,
|
|
|
|
avctx->width * component_size, avctx->height);
|
2015-09-25 00:36:50 +02:00
|
|
|
|
|
|
|
/* Frame is ready to be output */
|
|
|
|
ret = av_frame_ref(frame, ctx->current);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Usual properties */
|
|
|
|
if (keyframe) {
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
frame->key_frame = 1;
|
|
|
|
} else {
|
|
|
|
frame->pict_type = AV_PICTURE_TYPE_P;
|
|
|
|
}
|
|
|
|
*got_frame = 1;
|
|
|
|
|
2016-12-15 21:09:48 +02:00
|
|
|
return avpkt->size;
|
2015-09-25 00:36:50 +02:00
|
|
|
}
|
|
|
|
|
2021-02-25 11:50:26 +02:00
|
|
|
const AVCodec ff_screenpresso_decoder = {
|
2015-09-25 00:36:50 +02:00
|
|
|
.name = "screenpresso",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Screenpresso"),
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = AV_CODEC_ID_SCREENPRESSO,
|
|
|
|
.init = screenpresso_init,
|
|
|
|
.decode = screenpresso_decode_frame,
|
|
|
|
.close = screenpresso_close,
|
|
|
|
.priv_data_size = sizeof(ScreenpressoContext),
|
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
|
|
|
|
FF_CODEC_CAP_INIT_CLEANUP,
|
|
|
|
};
|