| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * SGI image decoder | ||
| 3 | * Todd Kirby <doubleshot@pacbell.net> | ||
| 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 | #include "avcodec.h" | ||
| 23 | #include "bytestream.h" | ||
| 24 | #include "codec_internal.h" | ||
| 25 | #include "decode.h" | ||
| 26 | #include "libavutil/attributes.h" | ||
| 27 | #include "sgi.h" | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Expand an RLE row into a channel. | ||
| 31 | * @param logctx a logcontext | ||
| 32 | * @param out_buf Points to one line after the output buffer. | ||
| 33 | * @param g GetByteContext used to read input from | ||
| 34 | * @param width length of out_buf in nb of elements | ||
| 35 | * @return nb of elements written, else return error code. | ||
| 36 | */ | ||
| 37 | 21152 | static int expand_rle_row8(void *logctx, uint8_t *out_buf, | |
| 38 | GetByteContext *g, unsigned width) | ||
| 39 | { | ||
| 40 | unsigned char pixel, count; | ||
| 41 | 21152 | unsigned char *orig = out_buf; | |
| 42 | 21152 | uint8_t *out_end = out_buf + width; | |
| 43 | |||
| 44 |
2/2✓ Branch 0 taken 525269 times.
✓ Branch 1 taken 21152 times.
|
546421 | while (out_buf < out_end) { |
| 45 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 525269 times.
|
525269 | if (bytestream2_get_bytes_left(g) < 1) |
| 46 | ✗ | return AVERROR_INVALIDDATA; | |
| 47 | 525269 | pixel = bytestream2_get_byteu(g); | |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 525269 times.
|
525269 | if (!(count = (pixel & 0x7f))) { |
| 49 | ✗ | break; | |
| 50 | } | ||
| 51 | |||
| 52 | /* Check for buffer overflow. */ | ||
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 525269 times.
|
525269 | if (out_end - out_buf < count) { |
| 54 | ✗ | av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n"); | |
| 55 | ✗ | return AVERROR_INVALIDDATA; | |
| 56 | } | ||
| 57 | |||
| 58 |
2/2✓ Branch 0 taken 146105 times.
✓ Branch 1 taken 379164 times.
|
525269 | if (pixel & 0x80) { |
| 59 |
2/2✓ Branch 0 taken 4780725 times.
✓ Branch 1 taken 146105 times.
|
4926830 | while (count--) |
| 60 | 4780725 | *out_buf++ = bytestream2_get_byte(g); | |
| 61 | } else { | ||
| 62 | 379164 | pixel = bytestream2_get_byte(g); | |
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 3975499 times.
✓ Branch 1 taken 379164 times.
|
4354663 | while (count--) |
| 65 | 3975499 | *out_buf++ = pixel; | |
| 66 | } | ||
| 67 | } | ||
| 68 | 21152 | return out_buf - orig; | |
| 69 | } | ||
| 70 | |||
| 71 | 4096 | static int expand_rle_row16(void *logctx, uint16_t *out_buf, | |
| 72 | GetByteContext *g, unsigned width) | ||
| 73 | { | ||
| 74 | unsigned short pixel; | ||
| 75 | unsigned char count; | ||
| 76 | 4096 | unsigned short *orig = out_buf; | |
| 77 | 4096 | uint16_t *out_end = out_buf + width; | |
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 144880 times.
✓ Branch 1 taken 4096 times.
|
148976 | while (out_buf < out_end) { |
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 144880 times.
|
144880 | if (bytestream2_get_bytes_left(g) < 2) |
| 81 | ✗ | return AVERROR_INVALIDDATA; | |
| 82 | 144880 | pixel = bytestream2_get_be16u(g); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144880 times.
|
144880 | if (!(count = (pixel & 0x7f))) |
| 84 | ✗ | break; | |
| 85 | |||
| 86 | /* Check for buffer overflow. */ | ||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144880 times.
|
144880 | if (out_end - out_buf < count) { |
| 88 | ✗ | av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n"); | |
| 89 | ✗ | return AVERROR_INVALIDDATA; | |
| 90 | } | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 18988 times.
✓ Branch 1 taken 125892 times.
|
144880 | if (pixel & 0x80) { |
| 93 |
2/2✓ Branch 0 taken 289372 times.
✓ Branch 1 taken 18988 times.
|
308360 | while (count--) { |
| 94 | 289372 | pixel = bytestream2_get_ne16(g); | |
| 95 | 289372 | AV_WN16A(out_buf, pixel); | |
| 96 | 289372 | out_buf++; | |
| 97 | } | ||
| 98 | } else { | ||
| 99 | 125892 | pixel = bytestream2_get_ne16(g); | |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 1807780 times.
✓ Branch 1 taken 125892 times.
|
1933672 | while (count--) { |
| 102 | 1807780 | AV_WN16A(out_buf, pixel); | |
| 103 | 1807780 | out_buf++; | |
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | 4096 | return out_buf - orig; | |
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | /** | ||
| 112 | * Read a run length encoded SGI image. | ||
| 113 | * @param out_buf output buffer | ||
| 114 | * @param s the current image state | ||
| 115 | * @return 0 if no error, else return error code. | ||
| 116 | */ | ||
| 117 | 33 | static int read_rle_sgi(void *logctx, uint8_t *out[4], ptrdiff_t stride[4], | |
| 118 | GetByteContext *g, unsigned width, int height, | ||
| 119 | unsigned nb_components, unsigned bytes_per_channel) | ||
| 120 | { | ||
| 121 | 33 | unsigned int len = height * nb_components * 4; | |
| 122 | 33 | GetByteContext g_table = *g; | |
| 123 | unsigned int start_offset; | ||
| 124 | int ret; | ||
| 125 | |||
| 126 | /* size of RLE offset and length tables */ | ||
| 127 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if (len * 2 > bytestream2_get_bytes_left(g)) { |
| 128 | ✗ | return AVERROR_INVALIDDATA; | |
| 129 | } | ||
| 130 | |||
| 131 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 33 times.
|
126 | for (unsigned z = 0; z < nb_components; z++) { |
| 132 | 93 | uint8_t *dest_row = out[z] + (height - 1) * stride[z]; | |
| 133 | while (1) { | ||
| 134 | 25248 | start_offset = bytestream2_get_be32(&g_table); | |
| 135 | 25248 | bytestream2_seek(g, start_offset, SEEK_SET); | |
| 136 |
2/2✓ Branch 0 taken 21152 times.
✓ Branch 1 taken 4096 times.
|
25248 | if (bytes_per_channel == 1) |
| 137 | 21152 | ret = expand_rle_row8(logctx, dest_row, g, width); | |
| 138 | else | ||
| 139 | 4096 | ret = expand_rle_row16(logctx, (uint16_t *)dest_row, g, width); | |
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25248 times.
|
25248 | if (ret != width) |
| 141 | ✗ | return AVERROR_INVALIDDATA; | |
| 142 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 25155 times.
|
25248 | if (dest_row == out[z]) |
| 143 | 93 | break; | |
| 144 | 25155 | dest_row -= stride[z]; | |
| 145 | } | ||
| 146 | } | ||
| 147 | 33 | return 0; | |
| 148 | } | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Read an uncompressed SGI image. | ||
| 152 | * @param out_buf output buffer | ||
| 153 | * @param s the current image state | ||
| 154 | * @return 0 if read success, else return error code. | ||
| 155 | */ | ||
| 156 | 6 | static int read_uncompressed_sgi(uint8_t *const out[4], const ptrdiff_t stride[4], | |
| 157 | GetByteContext *g, unsigned width, int height, | ||
| 158 | unsigned nb_components, unsigned bytes_per_channel) | ||
| 159 | { | ||
| 160 | 6 | unsigned rowsize = width * bytes_per_channel; | |
| 161 | |||
| 162 | /* Test buffer size. */ | ||
| 163 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (rowsize * (int64_t)height * nb_components > bytestream2_get_bytes_left(g)) |
| 164 | ✗ | return AVERROR_INVALIDDATA; | |
| 165 | |||
| 166 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
|
22 | for (unsigned z = 0; z < nb_components; z++) { |
| 167 | 16 | uint8_t *cur_row = out[z] + (height - 1) * stride[z]; | |
| 168 | while (1) { | ||
| 169 | 4096 | bytestream2_get_bufferu(g, cur_row, rowsize); | |
| 170 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4080 times.
|
4096 | if (cur_row == out[z]) |
| 171 | 16 | break; | |
| 172 | 4080 | cur_row -= stride[z]; | |
| 173 | } | ||
| 174 | } | ||
| 175 | 6 | return 0; | |
| 176 | } | ||
| 177 | |||
| 178 | 39 | static int decode_frame(AVCodecContext *avctx, AVFrame *p, | |
| 179 | int *got_frame, AVPacket *avpkt) | ||
| 180 | { | ||
| 181 | GetByteContext g; | ||
| 182 | unsigned int bytes_per_channel, nb_components, dimension, rle, width; | ||
| 183 | uint8_t *out[4]; | ||
| 184 | ptrdiff_t linesize[4]; | ||
| 185 | int height; | ||
| 186 | 39 | int ret = 0; | |
| 187 | |||
| 188 | 39 | bytestream2_init(&g, avpkt->data, avpkt->size); | |
| 189 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | if (bytestream2_get_bytes_left(&g) < SGI_HEADER_SIZE) { |
| 190 | ✗ | av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size); | |
| 191 | ✗ | return AVERROR_INVALIDDATA; | |
| 192 | } | ||
| 193 | |||
| 194 | /* Test for SGI magic. */ | ||
| 195 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | if (bytestream2_get_be16u(&g) != SGI_MAGIC) { |
| 196 | ✗ | av_log(avctx, AV_LOG_ERROR, "bad magic number\n"); | |
| 197 | ✗ | return AVERROR_INVALIDDATA; | |
| 198 | } | ||
| 199 | |||
| 200 | 39 | rle = bytestream2_get_byteu(&g); | |
| 201 | 39 | bytes_per_channel = bytestream2_get_byteu(&g); | |
| 202 | 39 | dimension = bytestream2_get_be16u(&g); | |
| 203 | 39 | width = bytestream2_get_be16u(&g); | |
| 204 | 39 | height = bytestream2_get_be16u(&g); | |
| 205 | 39 | nb_components = bytestream2_get_be16u(&g); | |
| 206 | |||
| 207 |
3/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
39 | if (bytes_per_channel != 1 && bytes_per_channel != 2) { |
| 208 | ✗ | av_log(avctx, AV_LOG_ERROR, "wrong channel number\n"); | |
| 209 | ✗ | return AVERROR_INVALIDDATA; | |
| 210 | } | ||
| 211 | |||
| 212 | /* Check for supported image dimensions. */ | ||
| 213 |
3/4✓ Branch 0 taken 31 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
|
39 | if (dimension != 2 && dimension != 3) { |
| 214 | ✗ | av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n"); | |
| 215 | ✗ | return AVERROR_INVALIDDATA; | |
| 216 | } | ||
| 217 | |||
| 218 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 31 times.
|
39 | if (nb_components == SGI_GRAYSCALE) { |
| 219 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8; |
| 220 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 8 times.
|
31 | } else if (nb_components == SGI_RGB) { |
| 221 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 19 times.
|
23 | avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRP16BE : AV_PIX_FMT_GBRP; |
| 222 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | } else if (nb_components == SGI_RGBA) { |
| 223 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRAP16BE : AV_PIX_FMT_GBRAP; |
| 224 | } else { | ||
| 225 | ✗ | av_log(avctx, AV_LOG_ERROR, "wrong picture format\n"); | |
| 226 | ✗ | return AVERROR_INVALIDDATA; | |
| 227 | } | ||
| 228 | |||
| 229 | 39 | ret = ff_set_dimensions(avctx, width, height); | |
| 230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (ret < 0) |
| 231 | ✗ | return ret; | |
| 232 | |||
| 233 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | if ((ret = ff_get_buffer(avctx, p, 0)) < 0) |
| 234 | ✗ | return ret; | |
| 235 | |||
| 236 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
|
39 | switch (nb_components) { |
| 237 | #define MAP(in_idx, out_idx) \ | ||
| 238 | out[(in_idx)] = p->data[(out_idx)]; \ | ||
| 239 | linesize[(in_idx)] = p->linesize[(out_idx)] | ||
| 240 | 8 | case SGI_GRAYSCALE: | |
| 241 | 8 | MAP(0, 0); | |
| 242 | 8 | break; | |
| 243 | 8 | case SGI_RGBA: | |
| 244 | 8 | MAP(3, 3); | |
| 245 | av_fallthrough; | ||
| 246 | 31 | case SGI_RGB: | |
| 247 | 31 | MAP(0, 2); | |
| 248 | 31 | MAP(1, 0); | |
| 249 | 31 | MAP(2, 1); | |
| 250 | 31 | break; | |
| 251 | } | ||
| 252 | |||
| 253 | /* Skip header. */ | ||
| 254 | 39 | bytestream2_seek(&g, SGI_HEADER_SIZE, SEEK_SET); | |
| 255 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 6 times.
|
39 | if (rle) { |
| 256 | 33 | ret = read_rle_sgi(avctx, out, linesize, &g, | |
| 257 | width, height, nb_components, bytes_per_channel); | ||
| 258 | } else { | ||
| 259 | 6 | ret = read_uncompressed_sgi(out, linesize, &g, | |
| 260 | width, height, nb_components, bytes_per_channel); | ||
| 261 | } | ||
| 262 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (ret) |
| 263 | ✗ | return ret; | |
| 264 | |||
| 265 | 39 | *got_frame = 1; | |
| 266 | 39 | return avpkt->size; | |
| 267 | } | ||
| 268 | |||
| 269 | const FFCodec ff_sgi_decoder = { | ||
| 270 | .p.name = "sgi", | ||
| 271 | CODEC_LONG_NAME("SGI image"), | ||
| 272 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 273 | .p.id = AV_CODEC_ID_SGI, | ||
| 274 | FF_CODEC_DECODE_CB(decode_frame), | ||
| 275 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 276 | }; | ||
| 277 |