| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * SGI image encoder | ||
| 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 "libavutil/mem.h" | ||
| 23 | #include "libavutil/opt.h" | ||
| 24 | |||
| 25 | #include "avcodec.h" | ||
| 26 | #include "bytestream.h" | ||
| 27 | #include "codec_internal.h" | ||
| 28 | #include "encode.h" | ||
| 29 | #include "sgi.h" | ||
| 30 | #include "rle.h" | ||
| 31 | |||
| 32 | #define SGI_SINGLE_CHAN 2 | ||
| 33 | #define SGI_MULTI_CHAN 3 | ||
| 34 | |||
| 35 | typedef struct SgiContext { | ||
| 36 | AVClass *class; | ||
| 37 | |||
| 38 | int rle; | ||
| 39 | } SgiContext; | ||
| 40 | |||
| 41 | 1 | static av_cold int encode_init(AVCodecContext *avctx) | |
| 42 | { | ||
| 43 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (avctx->width > 65535 || avctx->height > 65535) { |
| 44 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unsupported resolution %dx%d. " | |
| 45 | "SGI does not support resolutions above 65535x65535\n", | ||
| 46 | avctx->width, avctx->height); | ||
| 47 | ✗ | return AVERROR_INVALIDDATA; | |
| 48 | } | ||
| 49 | |||
| 50 | 1 | return 0; | |
| 51 | } | ||
| 52 | |||
| 53 | 11232 | static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src, | |
| 54 | int w, int bpp) | ||
| 55 | { | ||
| 56 | 11232 | int val, count, x, start = bytestream2_tell_p(pbc); | |
| 57 | void (*bytestream2_put)(PutByteContext *, unsigned int); | ||
| 58 | |||
| 59 |
1/2✓ Branch 0 taken 11232 times.
✗ Branch 1 not taken.
|
11232 | if (bpp == 1) |
| 60 | 11232 | bytestream2_put = bytestream2_put_byte; | |
| 61 | else | ||
| 62 | ✗ | bytestream2_put = bytestream2_put_be16; | |
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 86081 times.
✓ Branch 1 taken 11232 times.
|
97313 | for (x = 0; x < w; x += count) { |
| 65 | /* see if we can encode the next set of pixels with RLE */ | ||
| 66 | 86081 | count = ff_rle_count_pixels(src, w - x, bpp, 1); | |
| 67 |
2/2✓ Branch 0 taken 34170 times.
✓ Branch 1 taken 51911 times.
|
86081 | if (count > 1) { |
| 68 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34170 times.
|
34170 | if (bytestream2_get_bytes_left_p(pbc) < bpp * 2) |
| 69 | ✗ | return AVERROR_INVALIDDATA; | |
| 70 | |||
| 71 |
1/2✓ Branch 0 taken 34170 times.
✗ Branch 1 not taken.
|
34170 | val = bpp == 1 ? *src : AV_RB16(src); |
| 72 | 34170 | bytestream2_put(pbc, count); | |
| 73 | 34170 | bytestream2_put(pbc, val); | |
| 74 | } else { | ||
| 75 | int i; | ||
| 76 | /* fall back on uncompressed */ | ||
| 77 | 51911 | count = ff_rle_count_pixels(src, w - x, bpp, 0); | |
| 78 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51911 times.
|
51911 | if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1)) |
| 79 | ✗ | return AVERROR_INVALIDDATA; | |
| 80 | |||
| 81 | 51911 | bytestream2_put(pbc, count + 0x80); | |
| 82 |
2/2✓ Branch 0 taken 3782869 times.
✓ Branch 1 taken 51911 times.
|
3834780 | for (i = 0; i < count; i++) { |
| 83 |
1/2✓ Branch 0 taken 3782869 times.
✗ Branch 1 not taken.
|
3782869 | val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp); |
| 84 | 3782869 | bytestream2_put(pbc, val); | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | 86081 | src += count * bpp; | |
| 89 | } | ||
| 90 | |||
| 91 | 11232 | return bytestream2_tell_p(pbc) - start; | |
| 92 | } | ||
| 93 | |||
| 94 | 13 | static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
| 95 | const AVFrame *frame, int *got_packet) | ||
| 96 | { | ||
| 97 | 13 | SgiContext *s = avctx->priv_data; | |
| 98 | 13 | const AVFrame * const p = frame; | |
| 99 | PutByteContext pbc; | ||
| 100 | uint8_t *encode_buf; | ||
| 101 | int x, y, z, length, tablesize, ret, i; | ||
| 102 | unsigned int width, height, depth, dimension; | ||
| 103 | unsigned int bytes_per_channel, pixmax, put_be; | ||
| 104 | |||
| 105 | 13 | width = avctx->width; | |
| 106 | 13 | height = avctx->height; | |
| 107 | 13 | bytes_per_channel = 1; | |
| 108 | 13 | pixmax = 0xFF; | |
| 109 | 13 | put_be = HAVE_BIGENDIAN; | |
| 110 | |||
| 111 |
1/10✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
13 | switch (avctx->pix_fmt) { |
| 112 | ✗ | case AV_PIX_FMT_GRAY8: | |
| 113 | ✗ | dimension = SGI_SINGLE_CHAN; | |
| 114 | ✗ | depth = SGI_GRAYSCALE; | |
| 115 | ✗ | break; | |
| 116 | 13 | case AV_PIX_FMT_RGB24: | |
| 117 | 13 | dimension = SGI_MULTI_CHAN; | |
| 118 | 13 | depth = SGI_RGB; | |
| 119 | 13 | break; | |
| 120 | ✗ | case AV_PIX_FMT_RGBA: | |
| 121 | ✗ | dimension = SGI_MULTI_CHAN; | |
| 122 | ✗ | depth = SGI_RGBA; | |
| 123 | ✗ | break; | |
| 124 | ✗ | case AV_PIX_FMT_GRAY16LE: | |
| 125 | ✗ | put_be = !HAVE_BIGENDIAN; | |
| 126 | ✗ | case AV_PIX_FMT_GRAY16BE: | |
| 127 | ✗ | bytes_per_channel = 2; | |
| 128 | ✗ | pixmax = 0xFFFF; | |
| 129 | ✗ | dimension = SGI_SINGLE_CHAN; | |
| 130 | ✗ | depth = SGI_GRAYSCALE; | |
| 131 | ✗ | break; | |
| 132 | ✗ | case AV_PIX_FMT_RGB48LE: | |
| 133 | ✗ | put_be = !HAVE_BIGENDIAN; | |
| 134 | ✗ | case AV_PIX_FMT_RGB48BE: | |
| 135 | ✗ | bytes_per_channel = 2; | |
| 136 | ✗ | pixmax = 0xFFFF; | |
| 137 | ✗ | dimension = SGI_MULTI_CHAN; | |
| 138 | ✗ | depth = SGI_RGB; | |
| 139 | ✗ | break; | |
| 140 | ✗ | case AV_PIX_FMT_RGBA64LE: | |
| 141 | ✗ | put_be = !HAVE_BIGENDIAN; | |
| 142 | ✗ | case AV_PIX_FMT_RGBA64BE: | |
| 143 | ✗ | bytes_per_channel = 2; | |
| 144 | ✗ | pixmax = 0xFFFF; | |
| 145 | ✗ | dimension = SGI_MULTI_CHAN; | |
| 146 | ✗ | depth = SGI_RGBA; | |
| 147 | ✗ | break; | |
| 148 | ✗ | default: | |
| 149 | ✗ | return AVERROR_INVALIDDATA; | |
| 150 | } | ||
| 151 | |||
| 152 | 13 | tablesize = depth * height * 4; | |
| 153 | 13 | length = SGI_HEADER_SIZE; | |
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (!s->rle) |
| 155 | ✗ | length += depth * height * width; | |
| 156 | else // assume sgi_rle_encode() produces at most 2x size of input | ||
| 157 | 13 | length += tablesize * 2 + depth * height * (2 * width + 1); | |
| 158 | |||
| 159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if ((ret = ff_alloc_packet(avctx, pkt, bytes_per_channel * length)) < 0) |
| 160 | ✗ | return ret; | |
| 161 | |||
| 162 | 13 | bytestream2_init_writer(&pbc, pkt->data, pkt->size); | |
| 163 | |||
| 164 | /* Encode header. */ | ||
| 165 | 13 | bytestream2_put_be16(&pbc, SGI_MAGIC); | |
| 166 | 13 | bytestream2_put_byte(&pbc, s->rle); /* RLE 1 - VERBATIM 0 */ | |
| 167 | 13 | bytestream2_put_byte(&pbc, bytes_per_channel); | |
| 168 | 13 | bytestream2_put_be16(&pbc, dimension); | |
| 169 | 13 | bytestream2_put_be16(&pbc, width); | |
| 170 | 13 | bytestream2_put_be16(&pbc, height); | |
| 171 | 13 | bytestream2_put_be16(&pbc, depth); | |
| 172 | |||
| 173 | 13 | bytestream2_put_be32(&pbc, 0L); /* pixmin */ | |
| 174 | 13 | bytestream2_put_be32(&pbc, pixmax); | |
| 175 | 13 | bytestream2_put_be32(&pbc, 0L); /* dummy */ | |
| 176 | |||
| 177 | /* name */ | ||
| 178 |
2/2✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 13 times.
|
1053 | for (i = 0; i < 80; i++) |
| 179 | 1040 | bytestream2_put_byte(&pbc, 0L); | |
| 180 | |||
| 181 | /* colormap */ | ||
| 182 | 13 | bytestream2_put_be32(&pbc, 0L); | |
| 183 | |||
| 184 | /* The rest of the 512 byte header is unused. */ | ||
| 185 |
2/2✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 13 times.
|
5265 | for (i = 0; i < 404; i++) |
| 186 | 5252 | bytestream2_put_byte(&pbc, 0L); | |
| 187 | |||
| 188 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if (s->rle) { |
| 189 | PutByteContext taboff_pcb, tablen_pcb; | ||
| 190 | |||
| 191 | /* Skip RLE offset table. */ | ||
| 192 | 13 | bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize); | |
| 193 | 13 | bytestream2_skip_p(&pbc, tablesize); | |
| 194 | |||
| 195 | /* Skip RLE length table. */ | ||
| 196 | 13 | bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize); | |
| 197 | 13 | bytestream2_skip_p(&pbc, tablesize); | |
| 198 | |||
| 199 | /* Make an intermediate consecutive buffer. */ | ||
| 200 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if (!(encode_buf = av_malloc(width * bytes_per_channel))) |
| 201 | ✗ | return AVERROR(ENOMEM); | |
| 202 | |||
| 203 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
|
52 | for (z = 0; z < depth; z++) { |
| 204 | 39 | const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel; | |
| 205 | |||
| 206 |
2/2✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 39 times.
|
11271 | for (y = 0; y < height; y++) { |
| 207 | 11232 | bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc)); | |
| 208 | |||
| 209 |
2/2✓ Branch 0 taken 3953664 times.
✓ Branch 1 taken 11232 times.
|
3964896 | for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel) |
| 210 |
1/2✓ Branch 0 taken 3953664 times.
✗ Branch 1 not taken.
|
3953664 | if (bytes_per_channel == 1) { |
| 211 | 3953664 | encode_buf[x] = in_buf[depth * x]; | |
| 212 | ✗ | } else if (HAVE_BIGENDIAN ^ put_be) { | |
| 213 | ✗ | encode_buf[x + 1] = in_buf[depth * x]; | |
| 214 | ✗ | encode_buf[x] = in_buf[depth * x + 1]; | |
| 215 | } else { | ||
| 216 | ✗ | encode_buf[x] = in_buf[depth * x]; | |
| 217 | ✗ | encode_buf[x + 1] = in_buf[depth * x + 1]; | |
| 218 | } | ||
| 219 | |||
| 220 | 11232 | length = sgi_rle_encode(&pbc, encode_buf, width, | |
| 221 | bytes_per_channel); | ||
| 222 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11232 times.
|
11232 | if (length < 1) { |
| 223 | ✗ | av_free(encode_buf); | |
| 224 | ✗ | return AVERROR_INVALIDDATA; | |
| 225 | } | ||
| 226 | |||
| 227 | 11232 | bytestream2_put_be32(&tablen_pcb, length); | |
| 228 | 11232 | in_buf -= p->linesize[0]; | |
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | 13 | av_free(encode_buf); | |
| 233 | } else { | ||
| 234 | ✗ | for (z = 0; z < depth; z++) { | |
| 235 | ✗ | const uint8_t *in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel; | |
| 236 | |||
| 237 | ✗ | for (y = 0; y < height; y++) { | |
| 238 | ✗ | for (x = 0; x < width * depth; x += depth) | |
| 239 | ✗ | if (bytes_per_channel == 1) | |
| 240 | ✗ | bytestream2_put_byte(&pbc, in_buf[x]); | |
| 241 | else | ||
| 242 | ✗ | if (put_be) | |
| 243 | ✗ | bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]); | |
| 244 | else | ||
| 245 | ✗ | bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]); | |
| 246 | |||
| 247 | ✗ | in_buf -= p->linesize[0]; | |
| 248 | } | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | /* total length */ | ||
| 253 | 13 | pkt->size = bytestream2_tell_p(&pbc); | |
| 254 | 13 | *got_packet = 1; | |
| 255 | |||
| 256 | 13 | return 0; | |
| 257 | } | ||
| 258 | |||
| 259 | #define OFFSET(x) offsetof(SgiContext, x) | ||
| 260 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
| 261 | static const AVOption options[] = { | ||
| 262 | { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE }, | ||
| 263 | |||
| 264 | { NULL }, | ||
| 265 | }; | ||
| 266 | |||
| 267 | static const AVClass sgi_class = { | ||
| 268 | .class_name = "sgi", | ||
| 269 | .item_name = av_default_item_name, | ||
| 270 | .option = options, | ||
| 271 | .version = LIBAVUTIL_VERSION_INT, | ||
| 272 | }; | ||
| 273 | |||
| 274 | const FFCodec ff_sgi_encoder = { | ||
| 275 | .p.name = "sgi", | ||
| 276 | CODEC_LONG_NAME("SGI image"), | ||
| 277 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 278 | .p.id = AV_CODEC_ID_SGI, | ||
| 279 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
| 280 | .priv_data_size = sizeof(SgiContext), | ||
| 281 | .p.priv_class = &sgi_class, | ||
| 282 | .init = encode_init, | ||
| 283 | FF_CODEC_ENCODE_CB(encode_frame), | ||
| 284 | CODEC_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, | ||
| 285 | AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE, | ||
| 286 | AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE, | ||
| 287 | AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8), | ||
| 288 | }; | ||
| 289 |