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.
40 #include "libavutil/imgutils.h"
41 #include "libavutil/internal.h"
46 typedef struct ScreenpressoContext {
50 uint8_t *inflated_buf;
52 } ScreenpressoContext;
54 static av_cold int screenpresso_close(AVCodecContext *avctx)
56 ScreenpressoContext *ctx = avctx->priv_data;
58 av_frame_free(&ctx->current);
59 av_freep(&ctx->inflated_buf);
64 static av_cold int screenpresso_init(AVCodecContext *avctx)
66 ScreenpressoContext *ctx = avctx->priv_data;
68 /* These needs to be set to estimate uncompressed buffer */
69 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
71 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
72 avctx->width, avctx->height);
76 /* Allocate current frame */
77 ctx->current = av_frame_alloc();
79 return AVERROR(ENOMEM);
81 avctx->pix_fmt = AV_PIX_FMT_BGR24;
86 static void sum_delta_flipped(uint8_t *dst, int dst_linesize,
87 const uint8_t *src, int src_linesize,
88 int bytewidth, int height)
91 for (; height > 0; height--) {
92 for (i = 0; i < bytewidth; i++)
93 dst[i] += src[(height - 1) * src_linesize + i];
98 static int screenpresso_decode_frame(AVCodecContext *avctx, void *data,
99 int *got_frame, AVPacket *avpkt)
101 ScreenpressoContext *ctx = avctx->priv_data;
102 AVFrame *frame = data;
107 if (avpkt->size < 3) {
108 av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
109 return AVERROR_INVALIDDATA;
112 /* Basic sanity check, but not really harmful */
113 if ((avpkt->data[0] != 0x73 && avpkt->data[0] != 0x72) ||
114 avpkt->data[1] != 8) { // bpp probably
115 av_log(avctx, AV_LOG_WARNING, "Unknown header 0x%02X%02X\n",
116 avpkt->data[0], avpkt->data[1]);
118 keyframe = (avpkt->data[0] == 0x73);
120 /* Resize deflate buffer and frame on resolution change */
121 if (ctx->inflated_size != avctx->width * avctx->height * 3) {
122 av_frame_unref(ctx->current);
123 ret = ff_get_buffer(avctx, ctx->current, AV_GET_BUFFER_FLAG_REF);
127 /* If malloc fails, reset len to avoid preserving an invalid value */
128 ctx->inflated_size = avctx->width * avctx->height * 3;
129 ret = av_reallocp(&ctx->inflated_buf, ctx->inflated_size);
131 ctx->inflated_size = 0;
136 /* Inflate the frame after the 2 byte header */
137 ret = uncompress(ctx->inflated_buf, &ctx->inflated_size,
138 avpkt->data + 2, avpkt->size - 2);
140 av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
141 return AVERROR_UNKNOWN;
144 /* When a keyframe is found, copy it (flipped) */
146 av_image_copy_plane(ctx->current->data[0] +
147 ctx->current->linesize[0] * (avctx->height - 1),
148 -1 * ctx->current->linesize[0],
149 ctx->inflated_buf, avctx->width * 3,
150 avctx->width * 3, avctx->height);
151 /* Otherwise sum the delta on top of the current frame */
153 sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
154 ctx->inflated_buf, avctx->width * 3,
155 avctx->width * 3, avctx->height);
157 /* Frame is ready to be output */
158 ret = av_frame_ref(frame, ctx->current);
162 /* Usual properties */
164 frame->pict_type = AV_PICTURE_TYPE_I;
165 frame->key_frame = 1;
167 frame->pict_type = AV_PICTURE_TYPE_P;
174 AVCodec ff_screenpresso_decoder = {
175 .name = "screenpresso",
176 .long_name = NULL_IF_CONFIG_SMALL("Screenpresso"),
177 .type = AVMEDIA_TYPE_VIDEO,
178 .id = AV_CODEC_ID_SCREENPRESSO,
179 .init = screenpresso_init,
180 .decode = screenpresso_decode_frame,
181 .close = screenpresso_close,
182 .priv_data_size = sizeof(ScreenpressoContext),
183 .capabilities = AV_CODEC_CAP_DR1,
184 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
185 FF_CODEC_CAP_INIT_CLEANUP,