FFmpeg coverage


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