FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lscrdec.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 99 121 81.8%
Functions: 5 6 83.3%
Branches: 49 78 62.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2019 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22 #include <zlib.h>
23
24 #include "libavutil/frame.h"
25 #include "libavutil/error.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mem.h"
28
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "codec.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "packet.h"
35 #include "png.h"
36 #include "pngdsp.h"
37 #include "zlib_wrapper.h"
38
39 typedef struct LSCRContext {
40 PNGDSPContext dsp;
41 AVCodecContext *avctx;
42
43 AVFrame *last_picture;
44 uint8_t *buffer;
45 int buffer_size;
46 uint8_t *crow_buf;
47 int crow_size;
48 uint8_t *last_row;
49 unsigned int last_row_size;
50
51 GetByteContext gb;
52 uint8_t *image_buf;
53 int image_linesize;
54 int row_size;
55 int cur_h;
56 int y;
57
58 FFZStream zstream;
59 } LSCRContext;
60
61 4241 static void handle_row(LSCRContext *s)
62 {
63 uint8_t *ptr, *last_row;
64
65 4241 ptr = s->image_buf + s->image_linesize * s->y;
66
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 4117 times.
4241 if (s->y == 0)
67 124 last_row = s->last_row;
68 else
69 4117 last_row = ptr - s->image_linesize;
70
71 4241 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
72 last_row, s->row_size, 3);
73
74 4241 s->y++;
75 4241 }
76
77 250 static int decode_idat(LSCRContext *s, z_stream *zstream, int length)
78 {
79 int ret;
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 zstream->avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
81 250 zstream->next_in = s->gb.buffer;
82
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (length <= 0)
84 return AVERROR_INVALIDDATA;
85
86 250 bytestream2_skip(&s->gb, length);
87
88 /* decode one line if possible */
89
2/2
✓ Branch 0 taken 4366 times.
✓ Branch 1 taken 250 times.
4616 while (zstream->avail_in > 0) {
90 4366 ret = inflate(zstream, Z_PARTIAL_FLUSH);
91
3/4
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 4242 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 124 times.
4366 if (ret != Z_OK && ret != Z_STREAM_END) {
92 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
93 return AVERROR_EXTERNAL;
94 }
95
2/2
✓ Branch 0 taken 4241 times.
✓ Branch 1 taken 125 times.
4366 if (zstream->avail_out == 0) {
96
1/2
✓ Branch 0 taken 4241 times.
✗ Branch 1 not taken.
4241 if (s->y < s->cur_h) {
97 4241 handle_row(s);
98 }
99 4241 zstream->avail_out = s->crow_size;
100 4241 zstream->next_out = s->crow_buf;
101 }
102
3/4
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 4242 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 124 times.
4366 if (ret == Z_STREAM_END && zstream->avail_in > 0) {
103 av_log(s->avctx, AV_LOG_WARNING,
104 "%d undecompressed bytes left in buffer\n", zstream->avail_in);
105 return 0;
106 }
107 }
108 250 return 0;
109 }
110
111 16 static int decode_frame_lscr(AVCodecContext *avctx, AVFrame *rframe,
112 int *got_frame, AVPacket *avpkt)
113 {
114 16 LSCRContext *const s = avctx->priv_data;
115 16 GetByteContext *gb = &s->gb;
116 16 AVFrame *frame = s->last_picture;
117 16 int ret, nb_blocks, offset = 0;
118
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (avpkt->size < 2)
120 return AVERROR_INVALIDDATA;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (avpkt->size == 2)
122 return 0;
123
124 16 bytestream2_init(gb, avpkt->data, avpkt->size);
125
126 16 nb_blocks = bytestream2_get_le16(gb);
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
128 return AVERROR_INVALIDDATA;
129
130 16 ret = ff_reget_buffer(avctx, frame,
131 nb_blocks ? 0 : FF_REGET_BUFFER_FLAG_READONLY);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
133 return ret;
134
135
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 16 times.
140 for (int b = 0; b < nb_blocks; b++) {
136 124 z_stream *const zstream = &s->zstream.zstream;
137 int x, y, x2, y2, w, h, left;
138 uint32_t csize, size;
139
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
124 if (inflateReset(zstream) != Z_OK)
141 return AVERROR_EXTERNAL;
142
143 124 bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
144
145 124 x = bytestream2_get_le16(gb);
146 124 y = bytestream2_get_le16(gb);
147 124 x2 = bytestream2_get_le16(gb);
148 124 y2 = bytestream2_get_le16(gb);
149 124 w = x2-x;
150 124 s->cur_h = h = y2-y;
151
152
5/10
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 124 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 124 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 124 times.
✗ Branch 9 not taken.
124 if (w <= 0 || x < 0 || x >= avctx->width || w + x > avctx->width ||
153
3/6
✓ Branch 0 taken 124 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 124 times.
124 h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height)
154 return AVERROR_INVALIDDATA;
155
156 124 size = bytestream2_get_le32(gb);
157
158
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 123 times.
124 if ((nb_blocks == 1) &&
159
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (w == avctx->width) &&
160
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 (h == avctx->height) &&
161
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (x == 0) && (y == 0))
162 1 frame->flags |= AV_FRAME_FLAG_KEY;
163 else
164 123 frame->flags &= ~AV_FRAME_FLAG_KEY;
165
166 124 bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
167 124 csize = bytestream2_get_be32(gb);
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
124 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
169 return AVERROR_INVALIDDATA;
170
171 124 offset += size;
172 124 left = size;
173
174 124 s->y = 0;
175 124 s->row_size = w * 3;
176
177 124 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (!s->buffer)
179 return AVERROR(ENOMEM);
180
181 124 av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (!s->last_row)
183 return AVERROR(ENOMEM);
184
185 124 s->crow_size = w * 3 + 1;
186 124 s->crow_buf = s->buffer + 15;
187 124 zstream->avail_out = s->crow_size;
188 124 zstream->next_out = s->crow_buf;
189 124 s->image_buf = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
190 124 s->image_linesize =-frame->linesize[0];
191
192
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 124 times.
374 while (left > 16) {
193 250 ret = decode_idat(s, zstream, csize);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (ret < 0)
195 return ret;
196 250 left -= csize + 16;
197
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 124 times.
250 if (left > 16) {
198 126 bytestream2_skip(gb, 4);
199 126 csize = bytestream2_get_be32(gb);
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
201 return AVERROR_INVALIDDATA;
202 }
203 }
204 }
205
206
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 frame->pict_type = (frame->flags & AV_FRAME_FLAG_KEY) ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
207
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = av_frame_ref(rframe, frame)) < 0)
209 return ret;
210
211 16 *got_frame = 1;
212
213 16 return avpkt->size;
214 }
215
216 2 static int lscr_decode_close(AVCodecContext *avctx)
217 {
218 2 LSCRContext *s = avctx->priv_data;
219
220 2 av_frame_free(&s->last_picture);
221 2 av_freep(&s->buffer);
222 2 av_freep(&s->last_row);
223 2 ff_inflate_end(&s->zstream);
224
225 2 return 0;
226 }
227
228 2 static int lscr_decode_init(AVCodecContext *avctx)
229 {
230 2 LSCRContext *s = avctx->priv_data;
231
232 2 avctx->color_range = AVCOL_RANGE_JPEG;
233 2 avctx->pix_fmt = AV_PIX_FMT_BGR24;
234
235 2 s->avctx = avctx;
236 2 s->last_picture = av_frame_alloc();
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->last_picture)
238 return AVERROR(ENOMEM);
239
240 2 ff_pngdsp_init(&s->dsp);
241
242 2 return ff_inflate_init(&s->zstream, avctx);
243 }
244
245 static void lscr_decode_flush(AVCodecContext *avctx)
246 {
247 LSCRContext *s = avctx->priv_data;
248 av_frame_unref(s->last_picture);
249 }
250
251 const FFCodec ff_lscr_decoder = {
252 .p.name = "lscr",
253 CODEC_LONG_NAME("LEAD Screen Capture"),
254 .p.type = AVMEDIA_TYPE_VIDEO,
255 .p.id = AV_CODEC_ID_LSCR,
256 .p.capabilities = AV_CODEC_CAP_DR1,
257 .priv_data_size = sizeof(LSCRContext),
258 .init = lscr_decode_init,
259 .close = lscr_decode_close,
260 FF_CODEC_DECODE_CB(decode_frame_lscr),
261 .flush = lscr_decode_flush,
262 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
263 };
264