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