FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lscrdec.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 97 118 82.2%
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 "internal.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 125 frame->key_frame = (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
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 123 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
125 (x == 0) && (y == 0);
161
162 124 bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
163 124 csize = bytestream2_get_be32(gb);
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
124 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
165 return AVERROR_INVALIDDATA;
166
167 124 offset += size;
168 124 left = size;
169
170 124 s->y = 0;
171 124 s->row_size = w * 3;
172
173 124 av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (!s->buffer)
175 return AVERROR(ENOMEM);
176
177 124 av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (!s->last_row)
179 return AVERROR(ENOMEM);
180
181 124 s->crow_size = w * 3 + 1;
182 124 s->crow_buf = s->buffer + 15;
183 124 zstream->avail_out = s->crow_size;
184 124 zstream->next_out = s->crow_buf;
185 124 s->image_buf = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
186 124 s->image_linesize =-frame->linesize[0];
187
188
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 124 times.
374 while (left > 16) {
189 250 ret = decode_idat(s, zstream, csize);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 250 times.
250 if (ret < 0)
191 return ret;
192 250 left -= csize + 16;
193
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 124 times.
250 if (left > 16) {
194 126 bytestream2_skip(gb, 4);
195 126 csize = bytestream2_get_be32(gb);
196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
197 return AVERROR_INVALIDDATA;
198 }
199 }
200 }
201
202
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
203
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if ((ret = av_frame_ref(rframe, frame)) < 0)
205 return ret;
206
207 16 *got_frame = 1;
208
209 16 return avpkt->size;
210 }
211
212 2 static int lscr_decode_close(AVCodecContext *avctx)
213 {
214 2 LSCRContext *s = avctx->priv_data;
215
216 2 av_frame_free(&s->last_picture);
217 2 av_freep(&s->buffer);
218 2 av_freep(&s->last_row);
219 2 ff_inflate_end(&s->zstream);
220
221 2 return 0;
222 }
223
224 2 static int lscr_decode_init(AVCodecContext *avctx)
225 {
226 2 LSCRContext *s = avctx->priv_data;
227
228 2 avctx->color_range = AVCOL_RANGE_JPEG;
229 2 avctx->pix_fmt = AV_PIX_FMT_BGR24;
230
231 2 s->avctx = avctx;
232 2 s->last_picture = av_frame_alloc();
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->last_picture)
234 return AVERROR(ENOMEM);
235
236 2 ff_pngdsp_init(&s->dsp);
237
238 2 return ff_inflate_init(&s->zstream, avctx);
239 }
240
241 static void lscr_decode_flush(AVCodecContext *avctx)
242 {
243 LSCRContext *s = avctx->priv_data;
244 av_frame_unref(s->last_picture);
245 }
246
247 const FFCodec ff_lscr_decoder = {
248 .p.name = "lscr",
249 .p.long_name = NULL_IF_CONFIG_SMALL("LEAD Screen Capture"),
250 .p.type = AVMEDIA_TYPE_VIDEO,
251 .p.id = AV_CODEC_ID_LSCR,
252 .p.capabilities = AV_CODEC_CAP_DR1,
253 .priv_data_size = sizeof(LSCRContext),
254 .init = lscr_decode_init,
255 .close = lscr_decode_close,
256 FF_CODEC_DECODE_CB(decode_frame_lscr),
257 .flush = lscr_decode_flush,
258 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
259 };
260