FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lscrdec.c
Date: 2023-09-24 13:02: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
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "codec.h"
31 #include "codec_internal.h"
32 #include "decode.h"
33 #include "packet.h"
34 #include "png.h"
35 #include "pngdsp.h"
36 #include "zlib_wrapper.h"
37
38 typedef struct LSCRContext {
39 PNGDSPContext dsp;
40 AVCodecContext *avctx;
41
42 AVFrame *last_picture;
43 uint8_t *buffer;
44 int buffer_size;
45 uint8_t *crow_buf;
46 int crow_size;
47 uint8_t *last_row;
48 unsigned int last_row_size;
49
50 GetByteContext gb;
51 uint8_t *image_buf;
52 int image_linesize;
53 int row_size;
54 int cur_h;
55 int y;
56
57 FFZStream zstream;
58 } LSCRContext;
59
60 4241 static void handle_row(LSCRContext *s)
61 {
62 uint8_t *ptr, *last_row;
63
64 4241 ptr = s->image_buf + s->image_linesize * s->y;
65
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 4117 times.
4241 if (s->y == 0)
66 124 last_row = s->last_row;
67 else
68 4117 last_row = ptr - s->image_linesize;
69
70 4241 ff_png_filter_row(&s->dsp, ptr, s->crow_buf[0], s->crow_buf + 1,
71 last_row, s->row_size, 3);
72
73 4241 s->y++;
74 4241 }
75
76 250 static int decode_idat(LSCRContext *s, z_stream *zstream, int length)
77 {
78 int ret;
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 zstream->avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
80 250 zstream->next_in = s->gb.buffer;
81
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (length <= 0)
83 return AVERROR_INVALIDDATA;
84
85 250 bytestream2_skip(&s->gb, length);
86
87 /* decode one line if possible */
88
2/2
✓ Branch 0 taken 4366 times.
✓ Branch 1 taken 250 times.
4616 while (zstream->avail_in > 0) {
89 4366 ret = inflate(zstream, Z_PARTIAL_FLUSH);
90
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) {
91 av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
92 return AVERROR_EXTERNAL;
93 }
94
2/2
✓ Branch 0 taken 4241 times.
✓ Branch 1 taken 125 times.
4366 if (zstream->avail_out == 0) {
95
1/2
✓ Branch 0 taken 4241 times.
✗ Branch 1 not taken.
4241 if (s->y < s->cur_h) {
96 4241 handle_row(s);
97 }
98 4241 zstream->avail_out = s->crow_size;
99 4241 zstream->next_out = s->crow_buf;
100 }
101
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) {
102 av_log(s->avctx, AV_LOG_WARNING,
103 "%d undecompressed bytes left in buffer\n", zstream->avail_in);
104 return 0;
105 }
106 }
107 250 return 0;
108 }
109
110 16 static int decode_frame_lscr(AVCodecContext *avctx, AVFrame *rframe,
111 int *got_frame, AVPacket *avpkt)
112 {
113 16 LSCRContext *const s = avctx->priv_data;
114 16 GetByteContext *gb = &s->gb;
115 16 AVFrame *frame = s->last_picture;
116 16 int ret, nb_blocks, offset = 0;
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (avpkt->size < 2)
119 return AVERROR_INVALIDDATA;
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (avpkt->size == 2)
121 return 0;
122
123 16 bytestream2_init(gb, avpkt->data, avpkt->size);
124
125 16 nb_blocks = bytestream2_get_le16(gb);
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (bytestream2_get_bytes_left(gb) < 2 + nb_blocks * (12 + 8))
127 return AVERROR_INVALIDDATA;
128
129 16 ret = ff_reget_buffer(avctx, frame,
130 nb_blocks ? 0 : FF_REGET_BUFFER_FLAG_READONLY);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
132 return ret;
133
134
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 16 times.
140 for (int b = 0; b < nb_blocks; b++) {
135 124 z_stream *const zstream = &s->zstream.zstream;
136 int x, y, x2, y2, w, h, left;
137 uint32_t csize, size;
138
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
124 if (inflateReset(zstream) != Z_OK)
140 return AVERROR_EXTERNAL;
141
142 124 bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
143
144 124 x = bytestream2_get_le16(gb);
145 124 y = bytestream2_get_le16(gb);
146 124 x2 = bytestream2_get_le16(gb);
147 124 y2 = bytestream2_get_le16(gb);
148 124 w = x2-x;
149 124 s->cur_h = h = y2-y;
150
151
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 ||
152
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)
153 return AVERROR_INVALIDDATA;
154
155 124 size = bytestream2_get_le32(gb);
156
157
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 123 times.
124 if ((nb_blocks == 1) &&
158
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (w == avctx->width) &&
159
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 (h == avctx->height) &&
160
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (x == 0) && (y == 0))
161 1 frame->flags |= AV_FRAME_FLAG_KEY;
162 else
163 123 frame->flags &= ~AV_FRAME_FLAG_KEY;
164
165 124 bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
166 124 csize = bytestream2_get_be32(gb);
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
124 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
168 return AVERROR_INVALIDDATA;
169
170 124 offset += size;
171 124 left = size;
172
173 124 s->y = 0;
174 124 s->row_size = w * 3;
175
176 124 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (!s->buffer)
178 return AVERROR(ENOMEM);
179
180 124 av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (!s->last_row)
182 return AVERROR(ENOMEM);
183
184 124 s->crow_size = w * 3 + 1;
185 124 s->crow_buf = s->buffer + 15;
186 124 zstream->avail_out = s->crow_size;
187 124 zstream->next_out = s->crow_buf;
188 124 s->image_buf = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
189 124 s->image_linesize =-frame->linesize[0];
190
191
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 124 times.
374 while (left > 16) {
192 250 ret = decode_idat(s, zstream, csize);
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (ret < 0)
194 return ret;
195 250 left -= csize + 16;
196
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 124 times.
250 if (left > 16) {
197 126 bytestream2_skip(gb, 4);
198 126 csize = bytestream2_get_be32(gb);
199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
200 return AVERROR_INVALIDDATA;
201 }
202 }
203 }
204
205
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;
206
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = av_frame_ref(rframe, frame)) < 0)
208 return ret;
209
210 16 *got_frame = 1;
211
212 16 return avpkt->size;
213 }
214
215 2 static int lscr_decode_close(AVCodecContext *avctx)
216 {
217 2 LSCRContext *s = avctx->priv_data;
218
219 2 av_frame_free(&s->last_picture);
220 2 av_freep(&s->buffer);
221 2 av_freep(&s->last_row);
222 2 ff_inflate_end(&s->zstream);
223
224 2 return 0;
225 }
226
227 2 static int lscr_decode_init(AVCodecContext *avctx)
228 {
229 2 LSCRContext *s = avctx->priv_data;
230
231 2 avctx->color_range = AVCOL_RANGE_JPEG;
232 2 avctx->pix_fmt = AV_PIX_FMT_BGR24;
233
234 2 s->avctx = avctx;
235 2 s->last_picture = av_frame_alloc();
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->last_picture)
237 return AVERROR(ENOMEM);
238
239 2 ff_pngdsp_init(&s->dsp);
240
241 2 return ff_inflate_init(&s->zstream, avctx);
242 }
243
244 static void lscr_decode_flush(AVCodecContext *avctx)
245 {
246 LSCRContext *s = avctx->priv_data;
247 av_frame_unref(s->last_picture);
248 }
249
250 const FFCodec ff_lscr_decoder = {
251 .p.name = "lscr",
252 CODEC_LONG_NAME("LEAD Screen Capture"),
253 .p.type = AVMEDIA_TYPE_VIDEO,
254 .p.id = AV_CODEC_ID_LSCR,
255 .p.capabilities = AV_CODEC_CAP_DR1,
256 .priv_data_size = sizeof(LSCRContext),
257 .init = lscr_decode_init,
258 .close = lscr_decode_close,
259 FF_CODEC_DECODE_CB(decode_frame_lscr),
260 .flush = lscr_decode_flush,
261 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
262 };
263