3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Screenpresso decoder
28 * Screenpresso simply horizontally flips and then deflates frames,
29 * alternating full pictures and deltas. Deltas are related to the currently
30 * rebuilt frame (not the reference), and since there is no coordinate system
31 * they contain exactly as many pixel as the keyframe.
33 * Supports: BGR0, BGR24, RGB555
40 #include "libavutil/imgutils.h"
41 #include "libavutil/internal.h"
42 #include "libavutil/mem.h"
47 typedef struct ScreenpressoContext {
50 /* zlib interaction */
51 uint8_t *inflated_buf;
53 } ScreenpressoContext;
55 static av_cold int screenpresso_close(AVCodecContext *avctx)
57 ScreenpressoContext *ctx = avctx->priv_data;
59 av_frame_free(&ctx->current);
60 av_freep(&ctx->inflated_buf);
65 static av_cold int screenpresso_init(AVCodecContext *avctx)
67 ScreenpressoContext *ctx = avctx->priv_data;
69 /* These needs to be set to estimate uncompressed buffer */
70 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
72 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
73 avctx->width, avctx->height);
77 /* Allocate current frame */
78 ctx->current = av_frame_alloc();
80 return AVERROR(ENOMEM);
82 /* Allocate maximum size possible, a full RGBA frame */
83 ctx->inflated_size = avctx->width * avctx->height * 4;
84 ctx->inflated_buf = av_malloc(ctx->inflated_size);
85 if (!ctx->inflated_buf)
86 return AVERROR(ENOMEM);
91 static void sum_delta_flipped(uint8_t *dst, int dst_linesize,
92 const uint8_t *src, int src_linesize,
93 int bytewidth, int height)
96 for (; height > 0; height--) {
97 for (i = 0; i < bytewidth; i++)
98 dst[i] += src[(height - 1) * src_linesize + i];
103 static int screenpresso_decode_frame(AVCodecContext *avctx, void *data,
104 int *got_frame, AVPacket *avpkt)
106 ScreenpressoContext *ctx = avctx->priv_data;
107 AVFrame *frame = data;
108 uLongf length = ctx->inflated_size;
109 int keyframe, component_size, src_linesize;
113 if (avpkt->size < 3) {
114 av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
115 return AVERROR_INVALIDDATA;
118 /* Compression level (4 bits) and keyframe information (1 bit) */
119 av_log(avctx, AV_LOG_DEBUG, "Compression level %d\n", avpkt->data[0] >> 4);
120 keyframe = avpkt->data[0] & 1;
123 component_size = ((avpkt->data[1] >> 2) & 0x03) + 1;
124 switch (component_size) {
126 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
129 avctx->pix_fmt = AV_PIX_FMT_BGR24;
132 avctx->pix_fmt = AV_PIX_FMT_BGR0;
135 av_log(avctx, AV_LOG_ERROR, "Invalid bits per pixel value (%d)\n",
137 return AVERROR_INVALIDDATA;
140 /* Inflate the frame after the 2 byte header */
141 ret = uncompress(ctx->inflated_buf, &length,
142 avpkt->data + 2, avpkt->size - 2);
144 av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
145 return AVERROR_UNKNOWN;
148 ret = ff_reget_buffer(avctx, ctx->current);
152 /* Codec has aligned strides */
153 src_linesize = FFALIGN(avctx->width * component_size, 4);
155 /* When a keyframe is found, copy it (flipped) */
157 av_image_copy_plane(ctx->current->data[0] +
158 ctx->current->linesize[0] * (avctx->height - 1),
159 -1 * ctx->current->linesize[0],
160 ctx->inflated_buf, src_linesize,
161 avctx->width * component_size, avctx->height);
162 /* Otherwise sum the delta on top of the current frame */
164 sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
165 ctx->inflated_buf, src_linesize,
166 avctx->width * component_size, avctx->height);
168 /* Frame is ready to be output */
169 ret = av_frame_ref(frame, ctx->current);
173 /* Usual properties */
175 frame->pict_type = AV_PICTURE_TYPE_I;
176 frame->key_frame = 1;
178 frame->pict_type = AV_PICTURE_TYPE_P;
185 AVCodec ff_screenpresso_decoder = {
186 .name = "screenpresso",
187 .long_name = NULL_IF_CONFIG_SMALL("Screenpresso"),
188 .type = AVMEDIA_TYPE_VIDEO,
189 .id = AV_CODEC_ID_SCREENPRESSO,
190 .init = screenpresso_init,
191 .decode = screenpresso_decode_frame,
192 .close = screenpresso_close,
193 .priv_data_size = sizeof(ScreenpressoContext),
194 .capabilities = AV_CODEC_CAP_DR1,
195 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
196 FF_CODEC_CAP_INIT_CLEANUP,