FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qtrle.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 282 309 91.3%
Branches: 155 221 70.1%

Line Branch Exec Source
1 /*
2 * Quicktime Animation (RLE) Video Decoder
3 * Copyright (C) 2004 The FFmpeg project
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 * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
25 * For more information about the QT RLE format, visit:
26 * http://www.pcisys.net/~melanson/codecs/
27 *
28 * The QT RLE decoder has seven modes of operation:
29 * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
30 * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
31 * data. 24-bit data is RGB24 and 32-bit data is RGB32.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "avcodec.h"
39 #include "decode.h"
40 #include "bytestream.h"
41 #include "codec_internal.h"
42 #include "internal.h"
43
44 typedef struct QtrleContext {
45 AVCodecContext *avctx;
46 AVFrame *frame;
47
48 GetByteContext g;
49 uint32_t pal[256];
50 } QtrleContext;
51
52 #define CHECK_PIXEL_PTR(n) \
53 if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
54 av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
55 pixel_ptr + n, pixel_limit); \
56 return; \
57 } \
58
59 38 static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
60 {
61 int rle_code;
62 int pixel_ptr;
63 38 int row_inc = s->frame->linesize[0];
64 uint8_t pi0, pi1; /* 2 8-pixel values */
65 38 uint8_t *rgb = s->frame->data[0];
66 38 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
67 int skip;
68 /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
69 * as 'go to next line' during the decoding of a frame but is 'go to first
70 * line' at the beginning. Since we always interpret it as 'go to next line'
71 * in the decoding loop (which makes code simpler/faster), the first line
72 * would not be counted, so we count one more.
73 * See: https://trac.ffmpeg.org/ticket/226
74 * In the following decoding loop, row_ptr will be the position of the
75 * current row. */
76
77 38 row_ptr -= row_inc;
78 38 pixel_ptr = row_ptr;
79 38 lines_to_change++;
80
1/2
✓ Branch 0 taken 13034 times.
✗ Branch 1 not taken.
13034 while (lines_to_change) {
81 13034 skip = bytestream2_get_byte(&s->g);
82 13034 rle_code = (int8_t)bytestream2_get_byte(&s->g);
83
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 12996 times.
13034 if (rle_code == 0)
84 38 break;
85
2/2
✓ Branch 0 taken 9120 times.
✓ Branch 1 taken 3876 times.
12996 if(skip & 0x80) {
86 9120 lines_to_change--;
87 9120 row_ptr += row_inc;
88 9120 pixel_ptr = row_ptr + 2 * 8 * (skip & 0x7f);
89 } else
90 3876 pixel_ptr += 2 * 8 * skip;
91
2/4
✓ Branch 0 taken 12996 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12996 times.
12996 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
92
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12996 times.
12996 if(rle_code == -1)
94 continue;
95
96
2/2
✓ Branch 0 taken 1825 times.
✓ Branch 1 taken 11171 times.
12996 if (rle_code < 0) {
97 /* decode the run length code */
98 1825 rle_code = -rle_code;
99 /* get the next 2 bytes from the stream, treat them as groups
100 * of 8 pixels, and output them rle_code times */
101
102 1825 pi0 = bytestream2_get_byte(&s->g);
103 1825 pi1 = bytestream2_get_byte(&s->g);
104
2/4
✓ Branch 0 taken 1825 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1825 times.
1825 CHECK_PIXEL_PTR(rle_code * 2 * 8);
105
106
2/2
✓ Branch 0 taken 5247 times.
✓ Branch 1 taken 1825 times.
7072 while (rle_code--) {
107 5247 rgb[pixel_ptr++] = (pi0 >> 7) & 0x01;
108 5247 rgb[pixel_ptr++] = (pi0 >> 6) & 0x01;
109 5247 rgb[pixel_ptr++] = (pi0 >> 5) & 0x01;
110 5247 rgb[pixel_ptr++] = (pi0 >> 4) & 0x01;
111 5247 rgb[pixel_ptr++] = (pi0 >> 3) & 0x01;
112 5247 rgb[pixel_ptr++] = (pi0 >> 2) & 0x01;
113 5247 rgb[pixel_ptr++] = (pi0 >> 1) & 0x01;
114 5247 rgb[pixel_ptr++] = pi0 & 0x01;
115 5247 rgb[pixel_ptr++] = (pi1 >> 7) & 0x01;
116 5247 rgb[pixel_ptr++] = (pi1 >> 6) & 0x01;
117 5247 rgb[pixel_ptr++] = (pi1 >> 5) & 0x01;
118 5247 rgb[pixel_ptr++] = (pi1 >> 4) & 0x01;
119 5247 rgb[pixel_ptr++] = (pi1 >> 3) & 0x01;
120 5247 rgb[pixel_ptr++] = (pi1 >> 2) & 0x01;
121 5247 rgb[pixel_ptr++] = (pi1 >> 1) & 0x01;
122 5247 rgb[pixel_ptr++] = pi1 & 0x01;
123 }
124 } else {
125 /* copy the same pixel directly to output 2 times */
126 11171 rle_code *= 2;
127
2/4
✓ Branch 0 taken 11171 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11171 times.
11171 CHECK_PIXEL_PTR(rle_code * 8);
128
129
2/2
✓ Branch 0 taken 352790 times.
✓ Branch 1 taken 11171 times.
363961 while (rle_code--) {
130 352790 int x = bytestream2_get_byte(&s->g);
131 352790 rgb[pixel_ptr++] = (x >> 7) & 0x01;
132 352790 rgb[pixel_ptr++] = (x >> 6) & 0x01;
133 352790 rgb[pixel_ptr++] = (x >> 5) & 0x01;
134 352790 rgb[pixel_ptr++] = (x >> 4) & 0x01;
135 352790 rgb[pixel_ptr++] = (x >> 3) & 0x01;
136 352790 rgb[pixel_ptr++] = (x >> 2) & 0x01;
137 352790 rgb[pixel_ptr++] = (x >> 1) & 0x01;
138 352790 rgb[pixel_ptr++] = x & 0x01;
139 }
140 }
141 }
142 }
143
144 114 static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
145 int lines_to_change, int bpp)
146 {
147 int rle_code, i;
148 int pixel_ptr;
149 114 int row_inc = s->frame->linesize[0];
150 uint8_t pi[16]; /* 16 palette indices */
151 114 uint8_t *rgb = s->frame->data[0];
152 114 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
153
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 38 times.
114 int num_pixels = (bpp == 4) ? 8 : 16;
154
155
2/2
✓ Branch 0 taken 27360 times.
✓ Branch 1 taken 114 times.
27474 while (lines_to_change--) {
156 27360 pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
157
2/4
✓ Branch 0 taken 27360 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27360 times.
27360 CHECK_PIXEL_PTR(0);
158
159
2/2
✓ Branch 1 taken 145116 times.
✓ Branch 2 taken 27360 times.
172476 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 145116 times.
145116 if (bytestream2_get_bytes_left(&s->g) < 1)
161 return;
162
2/2
✓ Branch 0 taken 32727 times.
✓ Branch 1 taken 112389 times.
145116 if (rle_code == 0) {
163 /* there's another skip code in the stream */
164 32727 pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
165
2/4
✓ Branch 0 taken 32727 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32727 times.
32727 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
166
2/2
✓ Branch 0 taken 30399 times.
✓ Branch 1 taken 81990 times.
112389 } else if (rle_code < 0) {
167 /* decode the run length code */
168 30399 rle_code = -rle_code;
169 /* get the next 4 bytes from the stream, treat them as palette
170 * indexes, and output them rle_code times */
171
2/2
✓ Branch 0 taken 262512 times.
✓ Branch 1 taken 30399 times.
292911 for (i = num_pixels-1; i >= 0; i--) {
172 262512 pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
173 262512 bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
174 }
175
2/4
✓ Branch 0 taken 30399 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30399 times.
30399 CHECK_PIXEL_PTR(rle_code * num_pixels);
176
2/2
✓ Branch 0 taken 101496 times.
✓ Branch 1 taken 30399 times.
131895 while (rle_code--) {
177 101496 memcpy(&rgb[pixel_ptr], &pi, num_pixels);
178 101496 pixel_ptr += num_pixels;
179 }
180 } else {
181 /* copy the same pixel directly to output 4 times */
182 81990 rle_code *= 4;
183
2/4
✓ Branch 0 taken 81990 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 81990 times.
81990 CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
184
2/2
✓ Branch 0 taken 2884420 times.
✓ Branch 1 taken 81990 times.
2966410 while (rle_code--) {
185
2/2
✓ Branch 0 taken 2213528 times.
✓ Branch 1 taken 670892 times.
2884420 if(bpp == 4) {
186 2213528 int x = bytestream2_get_byte(&s->g);
187 2213528 rgb[pixel_ptr++] = (x >> 4) & 0x0f;
188 2213528 rgb[pixel_ptr++] = x & 0x0f;
189 } else {
190 670892 int x = bytestream2_get_byte(&s->g);
191 670892 rgb[pixel_ptr++] = (x >> 6) & 0x03;
192 670892 rgb[pixel_ptr++] = (x >> 4) & 0x03;
193 670892 rgb[pixel_ptr++] = (x >> 2) & 0x03;
194 670892 rgb[pixel_ptr++] = x & 0x03;
195 }
196 }
197 }
198 }
199 27360 row_ptr += row_inc;
200 }
201 }
202
203 208 static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
204 {
205 int rle_code;
206 int pixel_ptr;
207 208 int row_inc = s->frame->linesize[0];
208 uint8_t pi1, pi2, pi3, pi4; /* 4 palette indexes */
209 208 uint8_t *rgb = s->frame->data[0];
210 208 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
211
212
2/2
✓ Branch 0 taken 48311 times.
✓ Branch 1 taken 208 times.
48519 while (lines_to_change--) {
213 48311 pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
214
2/4
✓ Branch 0 taken 48311 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48311 times.
48311 CHECK_PIXEL_PTR(0);
215
216
2/2
✓ Branch 1 taken 151027 times.
✓ Branch 2 taken 48311 times.
199338 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 151027 times.
151027 if (bytestream2_get_bytes_left(&s->g) < 1)
218 return;
219
2/2
✓ Branch 0 taken 39089 times.
✓ Branch 1 taken 111938 times.
151027 if (rle_code == 0) {
220 /* there's another skip code in the stream */
221 39089 pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
222
2/4
✓ Branch 0 taken 39089 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39089 times.
39089 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
223
2/2
✓ Branch 0 taken 18173 times.
✓ Branch 1 taken 93765 times.
111938 } else if (rle_code < 0) {
224 /* decode the run length code */
225 18173 rle_code = -rle_code;
226 /* get the next 4 bytes from the stream, treat them as palette
227 * indexes, and output them rle_code times */
228 18173 pi1 = bytestream2_get_byte(&s->g);
229 18173 pi2 = bytestream2_get_byte(&s->g);
230 18173 pi3 = bytestream2_get_byte(&s->g);
231 18173 pi4 = bytestream2_get_byte(&s->g);
232
233
2/4
✓ Branch 0 taken 18173 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18173 times.
18173 CHECK_PIXEL_PTR(rle_code * 4);
234
235
2/2
✓ Branch 0 taken 101581 times.
✓ Branch 1 taken 18173 times.
119754 while (rle_code--) {
236 101581 rgb[pixel_ptr++] = pi1;
237 101581 rgb[pixel_ptr++] = pi2;
238 101581 rgb[pixel_ptr++] = pi3;
239 101581 rgb[pixel_ptr++] = pi4;
240 }
241 } else {
242 /* copy the same pixel directly to output 4 times */
243 93765 rle_code *= 4;
244
2/4
✓ Branch 0 taken 93765 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93765 times.
93765 CHECK_PIXEL_PTR(rle_code);
245
246 93765 bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
247 93765 pixel_ptr += rle_code;
248 }
249 }
250 48311 row_ptr += row_inc;
251 }
252 }
253
254 83 static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
255 {
256 int rle_code;
257 int pixel_ptr;
258 83 int row_inc = s->frame->linesize[0];
259 uint16_t rgb16;
260 83 uint8_t *rgb = s->frame->data[0];
261 83 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
262
263
2/2
✓ Branch 0 taken 9960 times.
✓ Branch 1 taken 83 times.
10043 while (lines_to_change--) {
264 9960 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
265
2/4
✓ Branch 0 taken 9960 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9960 times.
9960 CHECK_PIXEL_PTR(0);
266
267
2/2
✓ Branch 1 taken 268702 times.
✓ Branch 2 taken 9960 times.
278662 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 268702 times.
268702 if (bytestream2_get_bytes_left(&s->g) < 1)
269 return;
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 268702 times.
268702 if (rle_code == 0) {
271 /* there's another skip code in the stream */
272 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
273 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
274
2/2
✓ Branch 0 taken 234583 times.
✓ Branch 1 taken 34119 times.
268702 } else if (rle_code < 0) {
275 /* decode the run length code */
276 234583 rle_code = -rle_code;
277 234583 rgb16 = bytestream2_get_be16(&s->g);
278
279
2/4
✓ Branch 0 taken 234583 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 234583 times.
234583 CHECK_PIXEL_PTR(rle_code * 2);
280
281
2/2
✓ Branch 0 taken 1529430 times.
✓ Branch 1 taken 234583 times.
1764013 while (rle_code--) {
282 1529430 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
283 1529430 pixel_ptr += 2;
284 }
285 } else {
286
2/4
✓ Branch 0 taken 34119 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 34119 times.
34119 CHECK_PIXEL_PTR(rle_code * 2);
287
288 /* copy pixels directly to output */
289
2/2
✓ Branch 0 taken 64170 times.
✓ Branch 1 taken 34119 times.
98289 while (rle_code--) {
290 64170 rgb16 = bytestream2_get_be16(&s->g);
291 64170 *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
292 64170 pixel_ptr += 2;
293 }
294 }
295 }
296 9960 row_ptr += row_inc;
297 }
298 }
299
300 250 static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
301 {
302 int rle_code, rle_code_half;
303 int pixel_ptr;
304 250 int row_inc = s->frame->linesize[0];
305 uint8_t b;
306 uint16_t rg;
307 250 uint8_t *rgb = s->frame->data[0];
308 250 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
309
310
2/2
✓ Branch 0 taken 51302 times.
✓ Branch 1 taken 250 times.
51552 while (lines_to_change--) {
311 51302 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
312
2/4
✓ Branch 0 taken 51302 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 51302 times.
51302 CHECK_PIXEL_PTR(0);
313
314
2/2
✓ Branch 1 taken 1555857 times.
✓ Branch 2 taken 51302 times.
1607159 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1555857 times.
1555857 if (bytestream2_get_bytes_left(&s->g) < 1)
316 return;
317
2/2
✓ Branch 0 taken 109372 times.
✓ Branch 1 taken 1446485 times.
1555857 if (rle_code == 0) {
318 /* there's another skip code in the stream */
319 109372 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
320
2/4
✓ Branch 0 taken 109372 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 109372 times.
109372 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
321
2/2
✓ Branch 0 taken 764625 times.
✓ Branch 1 taken 681860 times.
1446485 } else if (rle_code < 0) {
322 /* decode the run length code */
323 764625 rle_code = -rle_code;
324 764625 rg = bytestream2_get_ne16(&s->g);
325 764625 b = bytestream2_get_byte(&s->g);
326
327
2/4
✓ Branch 0 taken 764625 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 764625 times.
764625 CHECK_PIXEL_PTR(rle_code * 3);
328
329
2/2
✓ Branch 0 taken 2373520 times.
✓ Branch 1 taken 764625 times.
3138145 while (rle_code--) {
330 2373520 AV_WN16(rgb + pixel_ptr, rg);
331 2373520 rgb[pixel_ptr + 2] = b;
332 2373520 pixel_ptr += 3;
333 }
334 } else {
335
2/4
✓ Branch 0 taken 681860 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 681860 times.
681860 CHECK_PIXEL_PTR(rle_code * 3);
336
337 681860 rle_code_half = rle_code / 2;
338
339
2/2
✓ Branch 0 taken 6637727 times.
✓ Branch 1 taken 681860 times.
7319587 while (rle_code_half--) { /* copy 2 raw rgb value at the same time */
340 6637727 AV_WN32(rgb + pixel_ptr, bytestream2_get_ne32(&s->g)); /* rgbr */
341 6637727 AV_WN16(rgb + pixel_ptr + 4, bytestream2_get_ne16(&s->g)); /* rgbr */
342 6637727 pixel_ptr += 6;
343 }
344
345
2/2
✓ Branch 0 taken 229085 times.
✓ Branch 1 taken 452775 times.
681860 if (rle_code % 2 != 0){ /* not even raw value */
346 229085 AV_WN16(rgb + pixel_ptr, bytestream2_get_ne16(&s->g));
347 229085 rgb[pixel_ptr + 2] = bytestream2_get_byte(&s->g);
348 229085 pixel_ptr += 3;
349 }
350 }
351 }
352 51302 row_ptr += row_inc;
353 }
354 }
355
356 26 static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
357 {
358 int rle_code, rle_code_half;
359 int pixel_ptr;
360 26 int row_inc = s->frame->linesize[0];
361 unsigned int argb;
362 26 uint8_t *rgb = s->frame->data[0];
363 26 int pixel_limit = s->frame->linesize[0] * s->avctx->height;
364
365
2/2
✓ Branch 0 taken 12480 times.
✓ Branch 1 taken 26 times.
12506 while (lines_to_change--) {
366 12480 pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
367
2/4
✓ Branch 0 taken 12480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12480 times.
12480 CHECK_PIXEL_PTR(0);
368
369
2/2
✓ Branch 1 taken 1193492 times.
✓ Branch 2 taken 12480 times.
1205972 while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
370
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1193492 times.
1193492 if (bytestream2_get_bytes_left(&s->g) < 1)
371 return;
372
2/2
✓ Branch 0 taken 244756 times.
✓ Branch 1 taken 948736 times.
1193492 if (rle_code == 0) {
373 /* there's another skip code in the stream */
374 244756 pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
375
2/4
✓ Branch 0 taken 244756 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 244756 times.
244756 CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
376
2/2
✓ Branch 0 taken 565158 times.
✓ Branch 1 taken 383578 times.
948736 } else if (rle_code < 0) {
377 /* decode the run length code */
378 565158 rle_code = -rle_code;
379 565158 argb = bytestream2_get_ne32(&s->g);
380
381
2/4
✓ Branch 0 taken 565158 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 565158 times.
565158 CHECK_PIXEL_PTR(rle_code * 4);
382
383
2/2
✓ Branch 0 taken 2203040 times.
✓ Branch 1 taken 565158 times.
2768198 while (rle_code--) {
384 2203040 AV_WN32A(rgb + pixel_ptr, argb);
385 2203040 pixel_ptr += 4;
386 }
387 } else {
388
2/4
✓ Branch 0 taken 383578 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 383578 times.
383578 CHECK_PIXEL_PTR(rle_code * 4);
389
390 /* copy pixels directly to output */
391 383578 rle_code_half = rle_code / 2;
392
2/2
✓ Branch 0 taken 487580 times.
✓ Branch 1 taken 383578 times.
871158 while (rle_code_half--) { /* copy 2 argb raw value at the same time */
393 487580 AV_WN64(rgb + pixel_ptr, bytestream2_get_ne64(&s->g));
394 487580 pixel_ptr += 8;
395 }
396
397
2/2
✓ Branch 0 taken 256768 times.
✓ Branch 1 taken 126810 times.
383578 if (rle_code % 2 != 0){ /* not even raw value */
398 256768 AV_WN32A(rgb + pixel_ptr, bytestream2_get_ne32(&s->g));
399 256768 pixel_ptr += 4;
400 }
401 }
402 }
403 12480 row_ptr += row_inc;
404 }
405 }
406
407 40 static av_cold int qtrle_decode_init(AVCodecContext *avctx)
408 {
409 40 QtrleContext *s = avctx->priv_data;
410
411 40 s->avctx = avctx;
412
4/5
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
40 switch (avctx->bits_per_coded_sample) {
413 18 case 1:
414 case 2:
415 case 4:
416 case 8:
417 case 33:
418 case 34:
419 case 36:
420 case 40:
421 18 avctx->pix_fmt = AV_PIX_FMT_PAL8;
422 18 break;
423
424 2 case 16:
425 2 avctx->pix_fmt = AV_PIX_FMT_RGB555;
426 2 break;
427
428 18 case 24:
429 18 avctx->pix_fmt = AV_PIX_FMT_RGB24;
430 18 break;
431
432 2 case 32:
433 2 avctx->pix_fmt = AV_PIX_FMT_ARGB;
434 2 break;
435
436 default:
437 av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
438 avctx->bits_per_coded_sample);
439 return AVERROR_INVALIDDATA;
440 }
441
442 40 s->frame = av_frame_alloc();
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!s->frame)
444 return AVERROR(ENOMEM);
445
446 40 return 0;
447 }
448
449 828 static int qtrle_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
450 int *got_frame, AVPacket *avpkt)
451 {
452 828 QtrleContext *s = avctx->priv_data;
453 int header, start_line;
454 int height, row_ptr;
455 828 int has_palette = 0;
456 828 int duplicate = 0;
457 int ret, size;
458
459 828 bytestream2_init(&s->g, avpkt->data, avpkt->size);
460
461 /* check if this frame is even supposed to change */
462
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 719 times.
828 if (avpkt->size < 8) {
463 109 duplicate = 1;
464 109 goto done;
465 }
466
467 /* start after the chunk size */
468 719 size = bytestream2_get_be32(&s->g) & 0x3FFFFFFF;
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 719 times.
719 if (size - avpkt->size > size * (int64_t)avctx->discard_damaged_percentage / 100)
470 return AVERROR_INVALIDDATA;
471
472
473 /* fetch the header */
474 719 header = bytestream2_get_be16(&s->g);
475
476 /* if a header is present, fetch additional decoding parameters */
477
2/2
✓ Branch 0 taken 369 times.
✓ Branch 1 taken 350 times.
719 if (header & 0x0008) {
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 if (avpkt->size < 14) {
479 duplicate = 1;
480 goto done;
481 }
482 369 start_line = bytestream2_get_be16(&s->g);
483 369 bytestream2_skip(&s->g, 2);
484 369 height = bytestream2_get_be16(&s->g);
485 369 bytestream2_skip(&s->g, 2);
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 369 times.
369 if (height > s->avctx->height - start_line) {
487 duplicate = 1;
488 goto done;
489 }
490 } else {
491 350 start_line = 0;
492 350 height = s->avctx->height;
493 }
494
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 719 times.
719 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
495 return ret;
496
497 719 row_ptr = s->frame->linesize[0] * start_line;
498
499
7/8
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 208 times.
✓ Branch 4 taken 83 times.
✓ Branch 5 taken 250 times.
✓ Branch 6 taken 26 times.
✗ Branch 7 not taken.
719 switch (avctx->bits_per_coded_sample) {
500 38 case 1:
501 case 33:
502 38 qtrle_decode_1bpp(s, row_ptr, height);
503 38 has_palette = 1;
504 38 break;
505
506 38 case 2:
507 case 34:
508 38 qtrle_decode_2n4bpp(s, row_ptr, height, 2);
509 38 has_palette = 1;
510 38 break;
511
512 76 case 4:
513 case 36:
514 76 qtrle_decode_2n4bpp(s, row_ptr, height, 4);
515 76 has_palette = 1;
516 76 break;
517
518 208 case 8:
519 case 40:
520 208 qtrle_decode_8bpp(s, row_ptr, height);
521 208 has_palette = 1;
522 208 break;
523
524 83 case 16:
525 83 qtrle_decode_16bpp(s, row_ptr, height);
526 83 break;
527
528 250 case 24:
529 250 qtrle_decode_24bpp(s, row_ptr, height);
530 250 break;
531
532 26 case 32:
533 26 qtrle_decode_32bpp(s, row_ptr, height);
534 26 break;
535
536 default:
537 av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
538 avctx->bits_per_coded_sample);
539 break;
540 }
541
542
2/2
✓ Branch 0 taken 359 times.
✓ Branch 1 taken 360 times.
719 if(has_palette) {
543 360 s->frame->palette_has_changed = ff_copy_palette(s->pal, avpkt, avctx);
544
545 /* make the palette available on the way out */
546 360 memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
547 }
548
549 359 done:
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 828 times.
828 if (!s->frame->data[0])
551 return AVERROR_INVALIDDATA;
552
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 719 times.
828 if (duplicate) {
553 // ff_reget_buffer() isn't needed when frames don't change, so just update
554 // frame props.
555 109 ret = ff_decode_frame_props(avctx, s->frame);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109 times.
109 if (ret < 0)
557 return ret;
558 }
559
560
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 828 times.
828 if ((ret = av_frame_ref(rframe, s->frame)) < 0)
561 return ret;
562 828 *got_frame = 1;
563
564 /* always report that the buffer was completely consumed */
565 828 return avpkt->size;
566 }
567
568 static void qtrle_decode_flush(AVCodecContext *avctx)
569 {
570 QtrleContext *s = avctx->priv_data;
571
572 av_frame_unref(s->frame);
573 }
574
575 40 static av_cold int qtrle_decode_end(AVCodecContext *avctx)
576 {
577 40 QtrleContext *s = avctx->priv_data;
578
579 40 av_frame_free(&s->frame);
580
581 40 return 0;
582 }
583
584 const FFCodec ff_qtrle_decoder = {
585 .p.name = "qtrle",
586 .p.long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
587 .p.type = AVMEDIA_TYPE_VIDEO,
588 .p.id = AV_CODEC_ID_QTRLE,
589 .priv_data_size = sizeof(QtrleContext),
590 .init = qtrle_decode_init,
591 .close = qtrle_decode_end,
592 FF_CODEC_DECODE_CB(qtrle_decode_frame),
593 .flush = qtrle_decode_flush,
594 .p.capabilities = AV_CODEC_CAP_DR1,
595 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
596 };
597