FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/screenpresso.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 64 75 85.3%
Functions: 4 4 100.0%
Branches: 19 26 73.1%

Line Branch Exec Source
1 /*
2 * Screenpresso decoder
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
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: BGR0, BGR24, RGB555
34 */
35
36 #include <stdint.h>
37 #include <zlib.h>
38
39 #include "libavutil/imgutils.h"
40 #include "libavutil/internal.h"
41 #include "libavutil/mem.h"
42
43 #include "avcodec.h"
44 #include "codec_internal.h"
45 #include "decode.h"
46
47 typedef struct ScreenpressoContext {
48 AVFrame *current;
49
50 /* zlib interaction */
51 uint8_t *inflated_buf;
52 uLongf inflated_size;
53 } ScreenpressoContext;
54
55 6 static av_cold int screenpresso_close(AVCodecContext *avctx)
56 {
57 6 ScreenpressoContext *ctx = avctx->priv_data;
58
59 6 av_frame_free(&ctx->current);
60 6 av_freep(&ctx->inflated_buf);
61
62 6 return 0;
63 }
64
65 6 static av_cold int screenpresso_init(AVCodecContext *avctx)
66 {
67 6 ScreenpressoContext *ctx = avctx->priv_data;
68
69 /* These needs to be set to estimate uncompressed buffer */
70 6 int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
72 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
73 avctx->width, avctx->height);
74 return ret;
75 }
76
77 /* Allocate current frame */
78 6 ctx->current = av_frame_alloc();
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ctx->current)
80 return AVERROR(ENOMEM);
81
82 /* Allocate maximum size possible, a full RGBA frame */
83 6 ctx->inflated_size = avctx->width * avctx->height * 4;
84 6 ctx->inflated_buf = av_malloc(ctx->inflated_size);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ctx->inflated_buf)
86 return AVERROR(ENOMEM);
87
88 6 return 0;
89 }
90
91 13 static void sum_delta_flipped(uint8_t *dst, int dst_linesize,
92 const uint8_t *src, int src_linesize,
93 int bytewidth, int height)
94 {
95 int i;
96
2/2
✓ Branch 0 taken 3480 times.
✓ Branch 1 taken 13 times.
3493 for (; height > 0; height--) {
97 3480 const uint8_t *src1 = &src[(height - 1) * src_linesize];
98
2/2
✓ Branch 0 taken 4070400 times.
✓ Branch 1 taken 3480 times.
4073880 for (i = 0; i < bytewidth; i++)
99 4070400 dst[i] += src1[i];
100 3480 dst += dst_linesize;
101 }
102 13 }
103
104 21 static int screenpresso_decode_frame(AVCodecContext *avctx, AVFrame *frame,
105 int *got_frame, AVPacket *avpkt)
106 {
107 21 ScreenpressoContext *ctx = avctx->priv_data;
108 21 uLongf length = ctx->inflated_size;
109 int keyframe, component_size, src_linesize;
110 int ret;
111
112 /* Size check */
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (avpkt->size < 3) {
114 av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
115 return AVERROR_INVALIDDATA;
116 }
117
118 /* Compression level (4 bits) and keyframe information (1 bit) */
119 21 av_log(avctx, AV_LOG_DEBUG, "Compression level %d\n", avpkt->data[0] >> 4);
120 21 keyframe = avpkt->data[0] & 1;
121
122 /* Pixel size */
123 21 component_size = ((avpkt->data[1] >> 2) & 0x03) + 1;
124
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
21 switch (component_size) {
125 10 case 2:
126 10 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
127 10 break;
128 5 case 3:
129 5 avctx->pix_fmt = AV_PIX_FMT_BGR24;
130 5 break;
131 6 case 4:
132 6 avctx->pix_fmt = AV_PIX_FMT_BGR0;
133 6 break;
134 default:
135 av_log(avctx, AV_LOG_ERROR, "Invalid bits per pixel value (%d)\n",
136 component_size);
137 return AVERROR_INVALIDDATA;
138 }
139
140 /* Inflate the frame after the 2 byte header */
141 21 ret = uncompress(ctx->inflated_buf, &length,
142 21 avpkt->data + 2, avpkt->size - 2);
143
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if (ret) {
144 2 av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
145 2 return AVERROR_UNKNOWN;
146 }
147
148 19 ret = ff_reget_buffer(avctx, ctx->current, 0);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
150 return ret;
151
152 /* Codec has aligned strides */
153 19 src_linesize = FFALIGN(avctx->width * component_size, 4);
154
155 /* When a keyframe is found, copy it (flipped) */
156
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13 times.
19 if (keyframe)
157 6 av_image_copy_plane(ctx->current->data[0] +
158 6 ctx->current->linesize[0] * (avctx->height - 1),
159 6 -1 * ctx->current->linesize[0],
160 6 ctx->inflated_buf, src_linesize,
161 6 avctx->width * component_size, avctx->height);
162 /* Otherwise sum the delta on top of the current frame */
163 else
164 13 sum_delta_flipped(ctx->current->data[0], ctx->current->linesize[0],
165 13 ctx->inflated_buf, src_linesize,
166 13 avctx->width * component_size, avctx->height);
167
168 /* Frame is ready to be output */
169 19 ret = av_frame_ref(frame, ctx->current);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
171 return ret;
172
173 /* Usual properties */
174
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 13 times.
19 if (keyframe) {
175 6 frame->pict_type = AV_PICTURE_TYPE_I;
176 6 frame->flags |= AV_FRAME_FLAG_KEY;
177 } else {
178 13 frame->pict_type = AV_PICTURE_TYPE_P;
179 }
180 19 *got_frame = 1;
181
182 19 return avpkt->size;
183 }
184
185 const FFCodec ff_screenpresso_decoder = {
186 .p.name = "screenpresso",
187 CODEC_LONG_NAME("Screenpresso"),
188 .p.type = AVMEDIA_TYPE_VIDEO,
189 .p.id = AV_CODEC_ID_SCREENPRESSO,
190 .init = screenpresso_init,
191 FF_CODEC_DECODE_CB(screenpresso_decode_frame),
192 .close = screenpresso_close,
193 .priv_data_size = sizeof(ScreenpressoContext),
194 .p.capabilities = AV_CODEC_CAP_DR1,
195 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
196 };
197