| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Quicktime Graphics (SMC) Video Decoder | ||
| 3 | * Copyright (C) 2003 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 SMC Video Decoder by Mike Melanson (melanson@pcisys.net) | ||
| 25 | * For more information about the SMC format, visit: | ||
| 26 | * http://www.pcisys.net/~melanson/codecs/ | ||
| 27 | * | ||
| 28 | * The SMC decoder outputs PAL8 colorspace data. | ||
| 29 | */ | ||
| 30 | |||
| 31 | #include <string.h> | ||
| 32 | |||
| 33 | #include "avcodec.h" | ||
| 34 | #include "bytestream.h" | ||
| 35 | #include "codec_internal.h" | ||
| 36 | #include "decode.h" | ||
| 37 | |||
| 38 | #define CPAIR 2 | ||
| 39 | #define CQUAD 4 | ||
| 40 | #define COCTET 8 | ||
| 41 | |||
| 42 | #define COLORS_PER_TABLE 256 | ||
| 43 | |||
| 44 | typedef struct SmcContext { | ||
| 45 | |||
| 46 | AVCodecContext *avctx; | ||
| 47 | AVFrame *frame; | ||
| 48 | |||
| 49 | /* SMC color tables */ | ||
| 50 | uint8_t color_pairs[COLORS_PER_TABLE * CPAIR]; | ||
| 51 | uint8_t color_quads[COLORS_PER_TABLE * CQUAD]; | ||
| 52 | uint8_t color_octets[COLORS_PER_TABLE * COCTET]; | ||
| 53 | |||
| 54 | uint32_t pal[256]; | ||
| 55 | } SmcContext; | ||
| 56 | |||
| 57 | #define GET_BLOCK_COUNT() \ | ||
| 58 | (opcode & 0x10) ? (1 + bytestream2_get_byte(gb)) : 1 + (opcode & 0x0F); | ||
| 59 | |||
| 60 | #define ADVANCE_BLOCK() \ | ||
| 61 | { \ | ||
| 62 | pixel_ptr += 4; \ | ||
| 63 | if (pixel_ptr >= width) \ | ||
| 64 | { \ | ||
| 65 | pixel_ptr = 0; \ | ||
| 66 | row_ptr += stride * 4; \ | ||
| 67 | } \ | ||
| 68 | total_blocks--; \ | ||
| 69 | if (total_blocks < !!n_blocks) \ | ||
| 70 | { \ | ||
| 71 | av_log(s->avctx, AV_LOG_ERROR, "block counter just went negative (this should not happen)\n"); \ | ||
| 72 | return AVERROR_INVALIDDATA; \ | ||
| 73 | } \ | ||
| 74 | } | ||
| 75 | |||
| 76 | 320 | static int smc_decode_stream(SmcContext *s, GetByteContext *gb) | |
| 77 | { | ||
| 78 | 320 | int width = s->avctx->width; | |
| 79 | 320 | int height = s->avctx->height; | |
| 80 | 320 | int stride = s->frame->linesize[0]; | |
| 81 | int i; | ||
| 82 | int chunk_size; | ||
| 83 | 320 | int buf_size = bytestream2_size(gb); | |
| 84 | uint8_t opcode; | ||
| 85 | int n_blocks; | ||
| 86 | unsigned int color_flags; | ||
| 87 | unsigned int color_flags_a; | ||
| 88 | unsigned int color_flags_b; | ||
| 89 | unsigned int flag_mask; | ||
| 90 | |||
| 91 | 320 | uint8_t * const pixels = s->frame->data[0]; | |
| 92 | |||
| 93 | 320 | int image_size = height * s->frame->linesize[0]; | |
| 94 | 320 | int row_ptr = 0; | |
| 95 | 320 | int pixel_ptr = 0; | |
| 96 | int pixel_x, pixel_y; | ||
| 97 | 320 | int row_inc = stride - 4; | |
| 98 | int block_ptr; | ||
| 99 | int prev_block_ptr; | ||
| 100 | int prev_block_ptr1, prev_block_ptr2; | ||
| 101 | int prev_block_flag; | ||
| 102 | int total_blocks; | ||
| 103 | int color_table_index; /* indexes to color pair, quad, or octet tables */ | ||
| 104 | int pixel; | ||
| 105 | |||
| 106 | 320 | int color_pair_index = 0; | |
| 107 | 320 | int color_quad_index = 0; | |
| 108 | 320 | int color_octet_index = 0; | |
| 109 | |||
| 110 | /* make the palette available */ | ||
| 111 | 320 | memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE); | |
| 112 | |||
| 113 | 320 | bytestream2_skip(gb, 1); | |
| 114 | 320 | chunk_size = bytestream2_get_be24(gb); | |
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 320 times.
|
320 | if (chunk_size != buf_size) |
| 116 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n", | |
| 117 | chunk_size, buf_size); | ||
| 118 | |||
| 119 | 320 | chunk_size = buf_size; | |
| 120 | 320 | total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4); | |
| 121 | |||
| 122 | /* traverse through the blocks */ | ||
| 123 |
2/2✓ Branch 0 taken 1019761 times.
✓ Branch 1 taken 320 times.
|
1020401 | while (total_blocks) { |
| 124 | /* sanity checks */ | ||
| 125 | /* make sure the row pointer hasn't gone wild */ | ||
| 126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1019761 times.
|
1019761 | if (row_ptr >= image_size) { |
| 127 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "just went out of bounds (row ptr = %d, height = %d)\n", | |
| 128 | row_ptr, image_size); | ||
| 129 | ✗ | return AVERROR_INVALIDDATA; | |
| 130 | } | ||
| 131 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1019761 times.
|
1019761 | if (bytestream2_get_bytes_left(gb) < 1) { |
| 132 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "input too small\n"); | |
| 133 | ✗ | return AVERROR_INVALIDDATA; | |
| 134 | } | ||
| 135 | |||
| 136 | 1019761 | opcode = bytestream2_get_byteu(gb); | |
| 137 |
7/9✗ Branch 0 not taken.
✓ Branch 1 taken 37007 times.
✓ Branch 2 taken 1110 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12199 times.
✓ Branch 5 taken 27872 times.
✓ Branch 6 taken 185929 times.
✓ Branch 7 taken 546628 times.
✓ Branch 8 taken 209016 times.
|
1019761 | switch (opcode & 0xF0) { |
| 138 | /* skip n blocks */ | ||
| 139 | 37007 | case 0x00: | |
| 140 | case 0x10: | ||
| 141 |
2/2✓ Branch 0 taken 11961 times.
✓ Branch 1 taken 25046 times.
|
37007 | n_blocks = GET_BLOCK_COUNT(); |
| 142 |
2/2✓ Branch 0 taken 429603 times.
✓ Branch 1 taken 37007 times.
|
466610 | while (n_blocks--) { |
| 143 |
3/4✓ Branch 0 taken 6402 times.
✓ Branch 1 taken 423201 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 429603 times.
|
429603 | ADVANCE_BLOCK(); |
| 144 | } | ||
| 145 | 37007 | break; | |
| 146 | |||
| 147 | /* repeat last block n times */ | ||
| 148 | 1110 | case 0x20: | |
| 149 | case 0x30: | ||
| 150 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1099 times.
|
1110 | n_blocks = GET_BLOCK_COUNT(); |
| 151 | |||
| 152 | /* sanity check */ | ||
| 153 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1110 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1110 | if ((row_ptr == 0) && (pixel_ptr == 0)) { |
| 154 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "encountered repeat block opcode (%02X) but no blocks rendered yet\n", | |
| 155 | opcode & 0xF0); | ||
| 156 | ✗ | return AVERROR_INVALIDDATA; | |
| 157 | } | ||
| 158 | |||
| 159 | /* figure out where the previous block started */ | ||
| 160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1110 times.
|
1110 | if (pixel_ptr == 0) |
| 161 | ✗ | prev_block_ptr1 = | |
| 162 | ✗ | (row_ptr - s->avctx->width * 4) + s->avctx->width - 4; | |
| 163 | else | ||
| 164 | 1110 | prev_block_ptr1 = row_ptr + pixel_ptr - 4; | |
| 165 | |||
| 166 |
2/2✓ Branch 0 taken 2374 times.
✓ Branch 1 taken 1110 times.
|
3484 | while (n_blocks--) { |
| 167 | 2374 | block_ptr = row_ptr + pixel_ptr; | |
| 168 | 2374 | prev_block_ptr = prev_block_ptr1; | |
| 169 |
2/2✓ Branch 0 taken 9496 times.
✓ Branch 1 taken 2374 times.
|
11870 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 170 |
2/2✓ Branch 0 taken 37984 times.
✓ Branch 1 taken 9496 times.
|
47480 | for (pixel_x = 0; pixel_x < 4; pixel_x++) { |
| 171 | 37984 | pixels[block_ptr++] = pixels[prev_block_ptr++]; | |
| 172 | } | ||
| 173 | 9496 | block_ptr += row_inc; | |
| 174 | 9496 | prev_block_ptr += row_inc; | |
| 175 | } | ||
| 176 |
3/4✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2342 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2374 times.
|
2374 | ADVANCE_BLOCK(); |
| 177 | } | ||
| 178 | 1110 | break; | |
| 179 | |||
| 180 | /* repeat previous pair of blocks n times */ | ||
| 181 | ✗ | case 0x40: | |
| 182 | case 0x50: | ||
| 183 | ✗ | n_blocks = GET_BLOCK_COUNT(); | |
| 184 | ✗ | n_blocks *= 2; | |
| 185 | |||
| 186 | /* sanity check */ | ||
| 187 | ✗ | if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) { | |
| 188 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "encountered repeat block opcode (%02X) but not enough blocks rendered yet\n", | |
| 189 | opcode & 0xF0); | ||
| 190 | ✗ | return AVERROR_INVALIDDATA; | |
| 191 | } | ||
| 192 | |||
| 193 | /* figure out where the previous 2 blocks started */ | ||
| 194 | ✗ | if (pixel_ptr == 0) | |
| 195 | ✗ | prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + | |
| 196 | ✗ | s->avctx->width - 4 * 2; | |
| 197 | ✗ | else if (pixel_ptr == 4) | |
| 198 | ✗ | prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc; | |
| 199 | else | ||
| 200 | ✗ | prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2; | |
| 201 | |||
| 202 | ✗ | if (pixel_ptr == 0) | |
| 203 | ✗ | prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc; | |
| 204 | else | ||
| 205 | ✗ | prev_block_ptr2 = row_ptr + pixel_ptr - 4; | |
| 206 | |||
| 207 | ✗ | prev_block_flag = 0; | |
| 208 | ✗ | while (n_blocks--) { | |
| 209 | ✗ | block_ptr = row_ptr + pixel_ptr; | |
| 210 | ✗ | if (prev_block_flag) | |
| 211 | ✗ | prev_block_ptr = prev_block_ptr2; | |
| 212 | else | ||
| 213 | ✗ | prev_block_ptr = prev_block_ptr1; | |
| 214 | ✗ | prev_block_flag = !prev_block_flag; | |
| 215 | |||
| 216 | ✗ | for (pixel_y = 0; pixel_y < 4; pixel_y++) { | |
| 217 | ✗ | for (pixel_x = 0; pixel_x < 4; pixel_x++) { | |
| 218 | ✗ | pixels[block_ptr++] = pixels[prev_block_ptr++]; | |
| 219 | } | ||
| 220 | ✗ | block_ptr += row_inc; | |
| 221 | ✗ | prev_block_ptr += row_inc; | |
| 222 | } | ||
| 223 | ✗ | ADVANCE_BLOCK(); | |
| 224 | } | ||
| 225 | ✗ | break; | |
| 226 | |||
| 227 | /* 1-color block encoding */ | ||
| 228 | 12199 | case 0x60: | |
| 229 | case 0x70: | ||
| 230 |
2/2✓ Branch 0 taken 1093 times.
✓ Branch 1 taken 11106 times.
|
12199 | n_blocks = GET_BLOCK_COUNT(); |
| 231 | 12199 | pixel = bytestream2_get_byte(gb); | |
| 232 | |||
| 233 |
2/2✓ Branch 0 taken 51781 times.
✓ Branch 1 taken 12199 times.
|
63980 | while (n_blocks--) { |
| 234 | 51781 | block_ptr = row_ptr + pixel_ptr; | |
| 235 |
2/2✓ Branch 0 taken 207124 times.
✓ Branch 1 taken 51781 times.
|
258905 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 236 |
2/2✓ Branch 0 taken 828496 times.
✓ Branch 1 taken 207124 times.
|
1035620 | for (pixel_x = 0; pixel_x < 4; pixel_x++) { |
| 237 | 828496 | pixels[block_ptr++] = pixel; | |
| 238 | } | ||
| 239 | 207124 | block_ptr += row_inc; | |
| 240 | } | ||
| 241 |
3/4✓ Branch 0 taken 632 times.
✓ Branch 1 taken 51149 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 51781 times.
|
51781 | ADVANCE_BLOCK(); |
| 242 | } | ||
| 243 | 12199 | break; | |
| 244 | |||
| 245 | /* 2-color block encoding */ | ||
| 246 | 27872 | case 0x80: | |
| 247 | case 0x90: | ||
| 248 | 27872 | n_blocks = (opcode & 0x0F) + 1; | |
| 249 | |||
| 250 | /* figure out which color pair to use to paint the 2-color block */ | ||
| 251 |
2/2✓ Branch 0 taken 6110 times.
✓ Branch 1 taken 21762 times.
|
27872 | if ((opcode & 0xF0) == 0x80) { |
| 252 | /* fetch the next 2 colors from bytestream and store in next | ||
| 253 | * available entry in the color pair table */ | ||
| 254 |
2/2✓ Branch 0 taken 12220 times.
✓ Branch 1 taken 6110 times.
|
18330 | for (i = 0; i < CPAIR; i++) { |
| 255 | 12220 | pixel = bytestream2_get_byte(gb); | |
| 256 | 12220 | color_table_index = CPAIR * color_pair_index + i; | |
| 257 | 12220 | s->color_pairs[color_table_index] = pixel; | |
| 258 | } | ||
| 259 | /* this is the base index to use for this block */ | ||
| 260 | 6110 | color_table_index = CPAIR * color_pair_index; | |
| 261 | 6110 | color_pair_index++; | |
| 262 | /* wraparound */ | ||
| 263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6110 times.
|
6110 | if (color_pair_index == COLORS_PER_TABLE) |
| 264 | ✗ | color_pair_index = 0; | |
| 265 | } else | ||
| 266 | 21762 | color_table_index = CPAIR * bytestream2_get_byte(gb); | |
| 267 | |||
| 268 |
2/2✓ Branch 0 taken 35826 times.
✓ Branch 1 taken 27872 times.
|
63698 | while (n_blocks--) { |
| 269 | 35826 | color_flags = bytestream2_get_be16(gb); | |
| 270 | 35826 | flag_mask = 0x8000; | |
| 271 | 35826 | block_ptr = row_ptr + pixel_ptr; | |
| 272 |
2/2✓ Branch 0 taken 143304 times.
✓ Branch 1 taken 35826 times.
|
179130 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 273 |
2/2✓ Branch 0 taken 573216 times.
✓ Branch 1 taken 143304 times.
|
716520 | for (pixel_x = 0; pixel_x < 4; pixel_x++) { |
| 274 |
2/2✓ Branch 0 taken 306808 times.
✓ Branch 1 taken 266408 times.
|
573216 | if (color_flags & flag_mask) |
| 275 | 306808 | pixel = color_table_index + 1; | |
| 276 | else | ||
| 277 | 266408 | pixel = color_table_index; | |
| 278 | 573216 | flag_mask >>= 1; | |
| 279 | 573216 | pixels[block_ptr++] = s->color_pairs[pixel]; | |
| 280 | } | ||
| 281 | 143304 | block_ptr += row_inc; | |
| 282 | } | ||
| 283 |
3/4✓ Branch 0 taken 176 times.
✓ Branch 1 taken 35650 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35826 times.
|
35826 | ADVANCE_BLOCK(); |
| 284 | } | ||
| 285 | 27872 | break; | |
| 286 | |||
| 287 | /* 4-color block encoding */ | ||
| 288 | 185929 | case 0xA0: | |
| 289 | case 0xB0: | ||
| 290 | 185929 | n_blocks = (opcode & 0x0F) + 1; | |
| 291 | |||
| 292 | /* figure out which color quad to use to paint the 4-color block */ | ||
| 293 |
2/2✓ Branch 0 taken 41737 times.
✓ Branch 1 taken 144192 times.
|
185929 | if ((opcode & 0xF0) == 0xA0) { |
| 294 | /* fetch the next 4 colors from bytestream and store in next | ||
| 295 | * available entry in the color quad table */ | ||
| 296 |
2/2✓ Branch 0 taken 166948 times.
✓ Branch 1 taken 41737 times.
|
208685 | for (i = 0; i < CQUAD; i++) { |
| 297 | 166948 | pixel = bytestream2_get_byte(gb); | |
| 298 | 166948 | color_table_index = CQUAD * color_quad_index + i; | |
| 299 | 166948 | s->color_quads[color_table_index] = pixel; | |
| 300 | } | ||
| 301 | /* this is the base index to use for this block */ | ||
| 302 | 41737 | color_table_index = CQUAD * color_quad_index; | |
| 303 | 41737 | color_quad_index++; | |
| 304 | /* wraparound */ | ||
| 305 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 41688 times.
|
41737 | if (color_quad_index == COLORS_PER_TABLE) |
| 306 | 49 | color_quad_index = 0; | |
| 307 | } else | ||
| 308 | 144192 | color_table_index = CQUAD * bytestream2_get_byte(gb); | |
| 309 | |||
| 310 |
2/2✓ Branch 0 taken 232539 times.
✓ Branch 1 taken 185929 times.
|
418468 | while (n_blocks--) { |
| 311 | 232539 | color_flags = bytestream2_get_be32(gb); | |
| 312 | /* flag mask actually acts as a bit shift count here */ | ||
| 313 | 232539 | flag_mask = 30; | |
| 314 | 232539 | block_ptr = row_ptr + pixel_ptr; | |
| 315 |
2/2✓ Branch 0 taken 930156 times.
✓ Branch 1 taken 232539 times.
|
1162695 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 316 |
2/2✓ Branch 0 taken 3720624 times.
✓ Branch 1 taken 930156 times.
|
4650780 | for (pixel_x = 0; pixel_x < 4; pixel_x++) { |
| 317 | 3720624 | pixel = color_table_index + | |
| 318 | 3720624 | ((color_flags >> flag_mask) & 0x03); | |
| 319 | 3720624 | flag_mask -= 2; | |
| 320 | 3720624 | pixels[block_ptr++] = s->color_quads[pixel]; | |
| 321 | } | ||
| 322 | 930156 | block_ptr += row_inc; | |
| 323 | } | ||
| 324 |
3/4✓ Branch 0 taken 2355 times.
✓ Branch 1 taken 230184 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 232539 times.
|
232539 | ADVANCE_BLOCK(); |
| 325 | } | ||
| 326 | 185929 | break; | |
| 327 | |||
| 328 | /* 8-color block encoding */ | ||
| 329 | 546628 | case 0xC0: | |
| 330 | case 0xD0: | ||
| 331 | 546628 | n_blocks = (opcode & 0x0F) + 1; | |
| 332 | |||
| 333 | /* figure out which color octet to use to paint the 8-color block */ | ||
| 334 |
2/2✓ Branch 0 taken 400744 times.
✓ Branch 1 taken 145884 times.
|
546628 | if ((opcode & 0xF0) == 0xC0) { |
| 335 | /* fetch the next 8 colors from bytestream and store in next | ||
| 336 | * available entry in the color octet table */ | ||
| 337 |
2/2✓ Branch 0 taken 3205952 times.
✓ Branch 1 taken 400744 times.
|
3606696 | for (i = 0; i < COCTET; i++) { |
| 338 | 3205952 | pixel = bytestream2_get_byte(gb); | |
| 339 | 3205952 | color_table_index = COCTET * color_octet_index + i; | |
| 340 | 3205952 | s->color_octets[color_table_index] = pixel; | |
| 341 | } | ||
| 342 | /* this is the base index to use for this block */ | ||
| 343 | 400744 | color_table_index = COCTET * color_octet_index; | |
| 344 | 400744 | color_octet_index++; | |
| 345 | /* wraparound */ | ||
| 346 |
2/2✓ Branch 0 taken 1434 times.
✓ Branch 1 taken 399310 times.
|
400744 | if (color_octet_index == COLORS_PER_TABLE) |
| 347 | 1434 | color_octet_index = 0; | |
| 348 | } else | ||
| 349 | 145884 | color_table_index = COCTET * bytestream2_get_byte(gb); | |
| 350 | |||
| 351 |
2/2✓ Branch 0 taken 569144 times.
✓ Branch 1 taken 546628 times.
|
1115772 | while (n_blocks--) { |
| 352 | /* | ||
| 353 | For this input of 6 hex bytes: | ||
| 354 | 01 23 45 67 89 AB | ||
| 355 | Mangle it to this output: | ||
| 356 | flags_a = xx012456, flags_b = xx89A37B | ||
| 357 | */ | ||
| 358 | /* build the color flags */ | ||
| 359 | 569144 | int val1 = bytestream2_get_be16(gb); | |
| 360 | 569144 | int val2 = bytestream2_get_be16(gb); | |
| 361 | 569144 | int val3 = bytestream2_get_be16(gb); | |
| 362 | 569144 | color_flags_a = ((val1 & 0xFFF0) << 8) | (val2 >> 4); | |
| 363 | 569144 | color_flags_b = ((val3 & 0xFFF0) << 8) | | |
| 364 | 569144 | ((val1 & 0x0F) << 8) | ((val2 & 0x0F) << 4) | (val3 & 0x0F); | |
| 365 | |||
| 366 | 569144 | color_flags = color_flags_a; | |
| 367 | /* flag mask actually acts as a bit shift count here */ | ||
| 368 | 569144 | flag_mask = 21; | |
| 369 | 569144 | block_ptr = row_ptr + pixel_ptr; | |
| 370 |
2/2✓ Branch 0 taken 2276576 times.
✓ Branch 1 taken 569144 times.
|
2845720 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 371 | /* reload flags at third row (iteration pixel_y == 2) */ | ||
| 372 |
2/2✓ Branch 0 taken 569144 times.
✓ Branch 1 taken 1707432 times.
|
2276576 | if (pixel_y == 2) { |
| 373 | 569144 | color_flags = color_flags_b; | |
| 374 | 569144 | flag_mask = 21; | |
| 375 | } | ||
| 376 |
2/2✓ Branch 0 taken 9106304 times.
✓ Branch 1 taken 2276576 times.
|
11382880 | for (pixel_x = 0; pixel_x < 4; pixel_x++) { |
| 377 | 9106304 | pixel = color_table_index + | |
| 378 | 9106304 | ((color_flags >> flag_mask) & 0x07); | |
| 379 | 9106304 | flag_mask -= 3; | |
| 380 | 9106304 | pixels[block_ptr++] = s->color_octets[pixel]; | |
| 381 | } | ||
| 382 | 2276576 | block_ptr += row_inc; | |
| 383 | } | ||
| 384 |
3/4✓ Branch 0 taken 6478 times.
✓ Branch 1 taken 562666 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 569144 times.
|
569144 | ADVANCE_BLOCK(); |
| 385 | } | ||
| 386 | 546628 | break; | |
| 387 | |||
| 388 | /* 16-color block encoding (every pixel is a different color) */ | ||
| 389 | 209016 | case 0xE0: | |
| 390 | case 0xF0: | ||
| 391 | 209016 | n_blocks = (opcode & 0x0F) + 1; | |
| 392 | |||
| 393 |
2/2✓ Branch 0 taken 209183 times.
✓ Branch 1 taken 209016 times.
|
418199 | while (n_blocks--) { |
| 394 | 209183 | block_ptr = row_ptr + pixel_ptr; | |
| 395 |
2/2✓ Branch 0 taken 836732 times.
✓ Branch 1 taken 209183 times.
|
1045915 | for (pixel_y = 0; pixel_y < 4; pixel_y++) { |
| 396 |
2/2✓ Branch 0 taken 3346928 times.
✓ Branch 1 taken 836732 times.
|
4183660 | for (pixel_x = 0; pixel_x < 4; pixel_x++) { |
| 397 | 3346928 | pixels[block_ptr++] = bytestream2_get_byte(gb); | |
| 398 | } | ||
| 399 | 836732 | block_ptr += row_inc; | |
| 400 | } | ||
| 401 |
3/4✓ Branch 0 taken 2375 times.
✓ Branch 1 taken 206808 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 209183 times.
|
209183 | ADVANCE_BLOCK(); |
| 402 | } | ||
| 403 | 209016 | break; | |
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | 320 | return 0; | |
| 408 | } | ||
| 409 | |||
| 410 | 10 | static av_cold int smc_decode_init(AVCodecContext *avctx) | |
| 411 | { | ||
| 412 | 10 | SmcContext *s = avctx->priv_data; | |
| 413 | |||
| 414 | 10 | s->avctx = avctx; | |
| 415 | 10 | avctx->pix_fmt = AV_PIX_FMT_PAL8; | |
| 416 | |||
| 417 | 10 | s->frame = av_frame_alloc(); | |
| 418 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!s->frame) |
| 419 | ✗ | return AVERROR(ENOMEM); | |
| 420 | |||
| 421 | 10 | return 0; | |
| 422 | } | ||
| 423 | |||
| 424 | 320 | static int smc_decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
| 425 | int *got_frame, AVPacket *avpkt) | ||
| 426 | { | ||
| 427 | 320 | const uint8_t *buf = avpkt->data; | |
| 428 | 320 | int buf_size = avpkt->size; | |
| 429 | 320 | SmcContext *s = avctx->priv_data; | |
| 430 | GetByteContext gb; | ||
| 431 | int ret; | ||
| 432 | 320 | int total_blocks = ((s->avctx->width + 3) / 4) * ((s->avctx->height + 3) / 4); | |
| 433 | |||
| 434 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 320 times.
|
320 | if (total_blocks / 1024 > avpkt->size) |
| 435 | ✗ | return AVERROR_INVALIDDATA; | |
| 436 | |||
| 437 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 320 times.
|
320 | if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0) |
| 438 | ✗ | return ret; | |
| 439 | |||
| 440 | 320 | ff_copy_palette(s->pal, avpkt, avctx); | |
| 441 | |||
| 442 | 320 | bytestream2_init(&gb, buf, buf_size); | |
| 443 | 320 | ret = smc_decode_stream(s, &gb); | |
| 444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 320 times.
|
320 | if (ret < 0) |
| 445 | ✗ | return ret; | |
| 446 | |||
| 447 | 320 | *got_frame = 1; | |
| 448 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 320 times.
|
320 | if ((ret = av_frame_ref(rframe, s->frame)) < 0) |
| 449 | ✗ | return ret; | |
| 450 | |||
| 451 | /* always report that the buffer was completely consumed */ | ||
| 452 | 320 | return buf_size; | |
| 453 | } | ||
| 454 | |||
| 455 | 10 | static av_cold int smc_decode_end(AVCodecContext *avctx) | |
| 456 | { | ||
| 457 | 10 | SmcContext *s = avctx->priv_data; | |
| 458 | |||
| 459 | 10 | av_frame_free(&s->frame); | |
| 460 | |||
| 461 | 10 | return 0; | |
| 462 | } | ||
| 463 | |||
| 464 | const FFCodec ff_smc_decoder = { | ||
| 465 | .p.name = "smc", | ||
| 466 | CODEC_LONG_NAME("QuickTime Graphics (SMC)"), | ||
| 467 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 468 | .p.id = AV_CODEC_ID_SMC, | ||
| 469 | .priv_data_size = sizeof(SmcContext), | ||
| 470 | .init = smc_decode_init, | ||
| 471 | .close = smc_decode_end, | ||
| 472 | FF_CODEC_DECODE_CB(smc_decode_frame), | ||
| 473 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 474 | }; | ||
| 475 |