]> git.sesse.net Git - ffmpeg/blob - libavcodec/screenpresso.c
6c434de82737e55ae1990645c15eb576b345c3c8
[ffmpeg] / libavcodec / screenpresso.c
1 /*
2  * Screenpresso decoder
3  * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Screenpresso decoder
25  *
26  * Fourcc: SPV1
27  *
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.
32  *
33  * Supports: BGR24
34  */
35
36 #include <stdint.h>
37 #include <string.h>
38 #include <zlib.h>
39
40 #include "libavutil/imgutils.h"
41 #include "libavutil/internal.h"
42
43 #include "avcodec.h"
44 #include "internal.h"
45
46 typedef struct ScreenpressoContext {
47     AVFrame *current;
48
49     /* zlib interation */
50     uint8_t *inflated_buf;
51     uLongf inflated_size;
52 } ScreenpressoContext;
53
54 static av_cold int screenpresso_close(AVCodecContext *avctx)
55 {
56     ScreenpressoContext *ctx = avctx->priv_data;
57
58     av_frame_free(&ctx->current);
59     av_freep(&ctx->inflated_buf);
60
61     return 0;
62 }
63
64 static av_cold int screenpresso_init(AVCodecContext *avctx)
65 {
66     ScreenpressoContext *ctx = avctx->priv_data;
67
68     /* These needs to be set to estimate uncompressed buffer */
69     int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
70     if (ret < 0) {
71         av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
72                avctx->width, avctx->height);
73         return ret;
74     }
75
76     /* Allocate current frame */
77     ctx->current = av_frame_alloc();
78     if (!ctx->current)
79         return AVERROR(ENOMEM);
80
81     avctx->pix_fmt = AV_PIX_FMT_BGR24;
82
83     /* Allocate maximum size possible, a full frame */
84     ctx->inflated_size = avctx->width * avctx->height * 3;
85     ctx->inflated_buf  = av_malloc(ctx->inflated_size);
86     if (!ctx->inflated_buf)
87         return AVERROR(ENOMEM);
88
89     return 0;
90 }
91
92 static void sum_delta_flipped(uint8_t       *dst, int dst_linesize,
93                               const uint8_t *src, int src_linesize,
94                               int bytewidth, int height)
95 {
96     int i;
97     for (; height > 0; height--) {
98         for (i = 0; i < bytewidth; i++)
99             dst[i] += src[(height - 1) * src_linesize + i];
100         dst += dst_linesize;
101     }
102 }
103
104 static int screenpresso_decode_frame(AVCodecContext *avctx, void *data,
105                                      int *got_frame, AVPacket *avpkt)
106 {
107     ScreenpressoContext *ctx = avctx->priv_data;
108     AVFrame *frame = data;
109     uLongf length = ctx->inflated_size;
110     int keyframe;
111     int ret;
112
113     /* Size check */
114     if (avpkt->size < 3) {
115         av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
116         return AVERROR_INVALIDDATA;
117     }
118
119     /* Basic sanity check, but not really harmful */
120     if ((avpkt->data[0] != 0x73 && avpkt->data[0] != 0x72) ||
121         avpkt->data[1] != 8) { // bpp probably
122         av_log(avctx, AV_LOG_WARNING, "Unknown header 0x%02X%02X\n",
123                avpkt->data[0], avpkt->data[1]);
124     }
125     keyframe = (avpkt->data[0] == 0x73);
126
127     /* Inflate the frame after the 2 byte header */
128     ret = uncompress(ctx->inflated_buf, &length,
129                      avpkt->data + 2, avpkt->size - 2);
130     if (ret) {
131         av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
132         return AVERROR_UNKNOWN;
133     }
134
135     ret = ff_reget_buffer(avctx, ctx->current);
136     if (ret < 0)
137         return ret;
138
139     /* When a keyframe is found, copy it (flipped) */
140     if (keyframe)
141         av_image_copy_plane(ctx->current->data[0] +
142                             ctx->current->linesize[0] * (avctx->height - 1),
143                             -1 * ctx->current->linesize[0],
144                             ctx->inflated_buf, avctx->width * 3,
145                             avctx->width * 3, avctx->height);
146     /* Otherwise sum the delta on top of the current frame */
147     else
148         sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
149                           ctx->inflated_buf, avctx->width * 3,
150                           avctx->width * 3, avctx->height);
151
152     /* Frame is ready to be output */
153     ret = av_frame_ref(frame, ctx->current);
154     if (ret < 0)
155         return ret;
156
157     /* Usual properties */
158     if (keyframe) {
159         frame->pict_type = AV_PICTURE_TYPE_I;
160         frame->key_frame = 1;
161     } else {
162         frame->pict_type = AV_PICTURE_TYPE_P;
163     }
164     *got_frame = 1;
165
166     return 0;
167 }
168
169 AVCodec ff_screenpresso_decoder = {
170     .name           = "screenpresso",
171     .long_name      = NULL_IF_CONFIG_SMALL("Screenpresso"),
172     .type           = AVMEDIA_TYPE_VIDEO,
173     .id             = AV_CODEC_ID_SCREENPRESSO,
174     .init           = screenpresso_init,
175     .decode         = screenpresso_decode_frame,
176     .close          = screenpresso_close,
177     .priv_data_size = sizeof(ScreenpressoContext),
178     .capabilities   = AV_CODEC_CAP_DR1,
179     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
180                       FF_CODEC_CAP_INIT_CLEANUP,
181 };