| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Silicon Graphics Motion Video Compressor 1 & 2 decoder | ||
| 3 | * Copyright (c) 2012 Peter Ross | ||
| 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 | * Silicon Graphics Motion Video Compressor 1 & 2 decoder | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "config_components.h" | ||
| 28 | |||
| 29 | #include "libavutil/intreadwrite.h" | ||
| 30 | |||
| 31 | #include "avcodec.h" | ||
| 32 | #include "bytestream.h" | ||
| 33 | #include "codec_internal.h" | ||
| 34 | #include "decode.h" | ||
| 35 | |||
| 36 | typedef struct MvcContext { | ||
| 37 | int vflip; | ||
| 38 | } MvcContext; | ||
| 39 | |||
| 40 | 4 | static av_cold int mvc_decode_init(AVCodecContext *avctx) | |
| 41 | { | ||
| 42 | 4 | MvcContext *s = avctx->priv_data; | |
| 43 | 4 | int width = avctx->width; | |
| 44 | 4 | int height = avctx->height; | |
| 45 | int ret; | ||
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (avctx->codec_id == AV_CODEC_ID_MVC1) { |
| 48 | 2 | width += 3; | |
| 49 | 2 | height += 3; | |
| 50 | } | ||
| 51 | 4 | width &= ~3; | |
| 52 | 4 | height &= ~3; | |
| 53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_set_dimensions(avctx, width, height)) < 0) |
| 54 | ✗ | return ret; | |
| 55 | |||
| 56 | 8 | avctx->pix_fmt = (avctx->codec_id == AV_CODEC_ID_MVC1) ? AV_PIX_FMT_RGB555 | |
| 57 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | : AV_PIX_FMT_RGB32; |
| 58 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
8 | s->vflip = avctx->extradata_size >= 9 && |
| 59 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9); |
| 60 | 4 | return 0; | |
| 61 | } | ||
| 62 | |||
| 63 | 28 | static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb, | |
| 64 | uint8_t *dst_start, int width, int height, int linesize) | ||
| 65 | { | ||
| 66 | uint8_t *dst; | ||
| 67 | uint16_t v[8]; | ||
| 68 | int mask, x, y, i; | ||
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 2016 times.
✓ Branch 1 taken 28 times.
|
2044 | for (y = 0; y < height; y += 4) { |
| 71 |
2/2✓ Branch 0 taken 193536 times.
✓ Branch 1 taken 2016 times.
|
195552 | for (x = 0; x < width; x += 4) { |
| 72 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 193536 times.
|
193536 | if (bytestream2_get_bytes_left(gb) < 6) |
| 73 | ✗ | return 0; | |
| 74 | |||
| 75 | 193536 | mask = bytestream2_get_be16u(gb); | |
| 76 | 193536 | v[0] = bytestream2_get_be16u(gb); | |
| 77 | 193536 | v[1] = bytestream2_get_be16u(gb); | |
| 78 |
2/2✓ Branch 0 taken 35749 times.
✓ Branch 1 taken 157787 times.
|
193536 | if ((v[0] & 0x8000)) { |
| 79 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 35749 times.
|
35749 | if (bytestream2_get_bytes_left(gb) < 12) { |
| 80 | ✗ | av_log(avctx, AV_LOG_WARNING, "buffer overflow\n"); | |
| 81 | ✗ | return AVERROR_INVALIDDATA; | |
| 82 | } | ||
| 83 |
2/2✓ Branch 0 taken 214494 times.
✓ Branch 1 taken 35749 times.
|
250243 | for (i = 2; i < 8; i++) |
| 84 | 214494 | v[i] = bytestream2_get_be16u(gb); | |
| 85 | } else { | ||
| 86 | 157787 | v[2] = v[4] = v[6] = v[0]; | |
| 87 | 157787 | v[3] = v[5] = v[7] = v[1]; | |
| 88 | } | ||
| 89 | |||
| 90 | #define PIX16(target, true, false) \ | ||
| 91 | i = (mask & target) ? true : false; \ | ||
| 92 | AV_WN16A(dst, v[i] & 0x7FFF); \ | ||
| 93 | dst += 2; | ||
| 94 | |||
| 95 | #define ROW16(row, a1, a0, b1, b0) \ | ||
| 96 | dst = dst_start + (y + row) * linesize + x * 2; \ | ||
| 97 | PIX16(1 << (row * 4), a1, a0) \ | ||
| 98 | PIX16(1 << (row * 4 + 1), a1, a0) \ | ||
| 99 | PIX16(1 << (row * 4 + 2), b1, b0) \ | ||
| 100 | PIX16(1 << (row * 4 + 3), b1, b0) | ||
| 101 | |||
| 102 |
4/4✓ Branch 0 taken 104958 times.
✓ Branch 1 taken 88578 times.
✓ Branch 2 taken 104746 times.
✓ Branch 3 taken 88790 times.
|
193536 | ROW16(0, 0, 1, 2, 3); |
| 103 |
4/4✓ Branch 0 taken 101604 times.
✓ Branch 1 taken 91932 times.
✓ Branch 2 taken 100965 times.
✓ Branch 3 taken 92571 times.
|
193536 | ROW16(1, 0, 1, 2, 3); |
| 104 |
8/8✓ Branch 0 taken 103317 times.
✓ Branch 1 taken 90219 times.
✓ Branch 2 taken 105128 times.
✓ Branch 3 taken 88408 times.
✓ Branch 4 taken 105316 times.
✓ Branch 5 taken 88220 times.
✓ Branch 6 taken 105104 times.
✓ Branch 7 taken 88432 times.
|
193536 | ROW16(2, 4, 5, 6, 7); |
| 105 |
8/8✓ Branch 0 taken 96671 times.
✓ Branch 1 taken 96865 times.
✓ Branch 2 taken 97605 times.
✓ Branch 3 taken 95931 times.
✓ Branch 4 taken 97659 times.
✓ Branch 5 taken 95877 times.
✓ Branch 6 taken 97511 times.
✓ Branch 7 taken 96025 times.
|
193536 | ROW16(3, 4, 5, 6, 7); |
| 106 | } | ||
| 107 | } | ||
| 108 | 28 | return 0; | |
| 109 | } | ||
| 110 | |||
| 111 | 47031 | static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel) | |
| 112 | { | ||
| 113 | int i, j; | ||
| 114 |
2/2✓ Branch 0 taken 188124 times.
✓ Branch 1 taken 47031 times.
|
235155 | for (j = 0; j < 4; j++) |
| 115 |
2/2✓ Branch 0 taken 752496 times.
✓ Branch 1 taken 188124 times.
|
940620 | for (i = 0; i < 4; i++) |
| 116 | 752496 | AV_WN32A(dst + j * linesize + i * 4, pixel); | |
| 117 | 47031 | } | |
| 118 | |||
| 119 | #define PIX32(target, true, false) \ | ||
| 120 | AV_WN32A(dst, (mask & target) ? v[true] : v[false]); \ | ||
| 121 | dst += 4; | ||
| 122 | |||
| 123 | #define ROW32(row, a1, a0, b1, b0) \ | ||
| 124 | dst = dst_start + (y + row) * linesize + x * 4; \ | ||
| 125 | PIX32(1 << (row * 4), a1, a0) \ | ||
| 126 | PIX32(1 << (row * 4 + 1), a1, a0) \ | ||
| 127 | PIX32(1 << (row * 4 + 2), b1, b0) \ | ||
| 128 | PIX32(1 << (row * 4 + 3), b1, b0) | ||
| 129 | |||
| 130 | #define MVC2_BLOCK \ | ||
| 131 | ROW32(0, 1, 0, 3, 2); \ | ||
| 132 | ROW32(1, 1, 0, 3, 2); \ | ||
| 133 | ROW32(2, 5, 4, 7, 6); \ | ||
| 134 | ROW32(3, 5, 4, 7, 6); | ||
| 135 | |||
| 136 | 33 | static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb, | |
| 137 | uint8_t *dst_start, int width, int height, | ||
| 138 | int linesize, int vflip) | ||
| 139 | { | ||
| 140 | uint8_t *dst; | ||
| 141 | uint32_t color[128], v[8]; | ||
| 142 | int w, h, nb_colors, i, x, y, p0, p1, mask; | ||
| 143 | |||
| 144 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if (bytestream2_get_bytes_left(gb) < 6) |
| 145 | ✗ | return AVERROR_INVALIDDATA; | |
| 146 | |||
| 147 | 33 | w = bytestream2_get_be16u(gb); | |
| 148 | 33 | h = bytestream2_get_be16u(gb); | |
| 149 |
2/4✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
|
33 | if ((w & ~3) != width || (h & ~3) != height) |
| 150 | ✗ | av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n"); | |
| 151 | |||
| 152 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if (bytestream2_get_byteu(gb)) { |
| 153 | ✗ | avpriv_request_sample(avctx, "bitmap feature"); | |
| 154 | ✗ | return AVERROR_PATCHWELCOME; | |
| 155 | } | ||
| 156 | |||
| 157 | 33 | nb_colors = bytestream2_get_byteu(gb); | |
| 158 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
|
33 | if (bytestream2_get_bytes_left(gb) < nb_colors * 3) |
| 159 | ✗ | return AVERROR_INVALIDDATA; | |
| 160 |
2/2✓ Branch 0 taken 4221 times.
✓ Branch 1 taken 33 times.
|
4254 | for (i = 0; i < FFMIN(nb_colors, 128); i++) |
| 161 | 4221 | color[i] = 0xFF000000 | bytestream2_get_be24u(gb); | |
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
|
33 | if (nb_colors > 128) |
| 163 | ✗ | bytestream2_skip(gb, (nb_colors - 128) * 3); | |
| 164 | |||
| 165 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | if (vflip) { |
| 166 | 33 | dst_start += (height - 1) * linesize; | |
| 167 | 33 | linesize = -linesize; | |
| 168 | } | ||
| 169 | 33 | x = y = 0; | |
| 170 |
1/2✓ Branch 1 taken 65142 times.
✗ Branch 2 not taken.
|
65142 | while (bytestream2_get_bytes_left(gb) >= 1) { |
| 171 | 65142 | p0 = bytestream2_get_byteu(gb); | |
| 172 |
2/2✓ Branch 0 taken 47031 times.
✓ Branch 1 taken 18111 times.
|
65142 | if ((p0 & 0x80)) { |
| 173 |
2/2✓ Branch 0 taken 1327 times.
✓ Branch 1 taken 45704 times.
|
47031 | if ((p0 & 0x40)) { |
| 174 | 1327 | p0 &= 0x3F; | |
| 175 | 1327 | p0 = (p0 << 2) | (p0 >> 4); | |
| 176 | 1327 | set_4x4_block(dst_start + y * linesize + x * 4, linesize, | |
| 177 | 1327 | 0xFF000000 | (p0 << 16) | (p0 << 8) | p0); | |
| 178 | } else { | ||
| 179 | int g, r; | ||
| 180 | 45704 | p0 &= 0x3F; | |
| 181 | 45704 | p0 = (p0 << 2) | (p0 >> 4); | |
| 182 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45704 times.
|
45704 | if (bytestream2_get_bytes_left(gb) < 2) |
| 183 | ✗ | return AVERROR_INVALIDDATA; | |
| 184 | 45704 | g = bytestream2_get_byteu(gb); | |
| 185 | 45704 | r = bytestream2_get_byteu(gb); | |
| 186 | 45704 | set_4x4_block(dst_start + y * linesize + x * 4, linesize, | |
| 187 | 45704 | 0xFF000000 | (r << 16) | (g << 8) | p0); | |
| 188 | } | ||
| 189 | } else { | ||
| 190 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 18111 times.
|
18111 | if (bytestream2_get_bytes_left(gb) < 1) |
| 191 | ✗ | return AVERROR_INVALIDDATA; | |
| 192 | 18111 | p1 = bytestream2_get_byteu(gb); | |
| 193 |
2/2✓ Branch 0 taken 3192 times.
✓ Branch 1 taken 14919 times.
|
18111 | if ((p1 & 0x80)) { |
| 194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3192 times.
|
3192 | if ((p0 & 0x7F) == (p1 & 0x7F)) { |
| 195 | ✗ | set_4x4_block(dst_start + y * linesize + x * 4, linesize, | |
| 196 | ✗ | color[p0 & 0x7F]); | |
| 197 | } else { | ||
| 198 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3192 times.
|
3192 | if (bytestream2_get_bytes_left(gb) < 2) |
| 199 | ✗ | return AVERROR_INVALIDDATA; | |
| 200 | 3192 | v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F]; | |
| 201 | 3192 | v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F]; | |
| 202 | 3192 | mask = bytestream2_get_le16u(gb); | |
| 203 |
32/32✓ Branch 0 taken 1602 times.
✓ Branch 1 taken 1590 times.
✓ Branch 2 taken 1625 times.
✓ Branch 3 taken 1567 times.
✓ Branch 4 taken 1660 times.
✓ Branch 5 taken 1532 times.
✓ Branch 6 taken 1654 times.
✓ Branch 7 taken 1538 times.
✓ Branch 8 taken 1660 times.
✓ Branch 9 taken 1532 times.
✓ Branch 10 taken 1632 times.
✓ Branch 11 taken 1560 times.
✓ Branch 12 taken 1660 times.
✓ Branch 13 taken 1532 times.
✓ Branch 14 taken 1797 times.
✓ Branch 15 taken 1395 times.
✓ Branch 16 taken 1591 times.
✓ Branch 17 taken 1601 times.
✓ Branch 18 taken 1622 times.
✓ Branch 19 taken 1570 times.
✓ Branch 20 taken 1611 times.
✓ Branch 21 taken 1581 times.
✓ Branch 22 taken 1687 times.
✓ Branch 23 taken 1505 times.
✓ Branch 24 taken 1661 times.
✓ Branch 25 taken 1531 times.
✓ Branch 26 taken 1705 times.
✓ Branch 27 taken 1487 times.
✓ Branch 28 taken 1680 times.
✓ Branch 29 taken 1512 times.
✓ Branch 30 taken 1699 times.
✓ Branch 31 taken 1493 times.
|
3192 | MVC2_BLOCK |
| 204 | } | ||
| 205 | } else { | ||
| 206 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 14919 times.
|
14919 | if (bytestream2_get_bytes_left(gb) < 8) |
| 207 | ✗ | return AVERROR_INVALIDDATA; | |
| 208 | 14919 | v[0] = color[p0 & 0x7F]; | |
| 209 | 14919 | v[1] = color[p1 & 0x7F]; | |
| 210 |
2/2✓ Branch 0 taken 89514 times.
✓ Branch 1 taken 14919 times.
|
104433 | for (i = 2; i < 8; i++) |
| 211 | 89514 | v[i] = color[bytestream2_get_byteu(gb) & 0x7F]; | |
| 212 | 14919 | mask = bytestream2_get_le16u(gb); | |
| 213 |
32/32✓ Branch 0 taken 6362 times.
✓ Branch 1 taken 8557 times.
✓ Branch 2 taken 5832 times.
✓ Branch 3 taken 9087 times.
✓ Branch 4 taken 5658 times.
✓ Branch 5 taken 9261 times.
✓ Branch 6 taken 6055 times.
✓ Branch 7 taken 8864 times.
✓ Branch 8 taken 6534 times.
✓ Branch 9 taken 8385 times.
✓ Branch 10 taken 5839 times.
✓ Branch 11 taken 9080 times.
✓ Branch 12 taken 5851 times.
✓ Branch 13 taken 9068 times.
✓ Branch 14 taken 6169 times.
✓ Branch 15 taken 8750 times.
✓ Branch 16 taken 5822 times.
✓ Branch 17 taken 9097 times.
✓ Branch 18 taken 5347 times.
✓ Branch 19 taken 9572 times.
✓ Branch 20 taken 5344 times.
✓ Branch 21 taken 9575 times.
✓ Branch 22 taken 5883 times.
✓ Branch 23 taken 9036 times.
✓ Branch 24 taken 6715 times.
✓ Branch 25 taken 8204 times.
✓ Branch 26 taken 6032 times.
✓ Branch 27 taken 8887 times.
✓ Branch 28 taken 6065 times.
✓ Branch 29 taken 8854 times.
✓ Branch 30 taken 6310 times.
✓ Branch 31 taken 8609 times.
|
14919 | MVC2_BLOCK |
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | 65142 | x += 4; | |
| 218 |
2/2✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 63591 times.
|
65142 | if (x >= width) { |
| 219 | 1551 | y += 4; | |
| 220 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1518 times.
|
1551 | if (y >= height) |
| 221 | 33 | break; | |
| 222 | 1518 | x = 0; | |
| 223 | } | ||
| 224 | } | ||
| 225 | 33 | return 0; | |
| 226 | } | ||
| 227 | |||
| 228 | 61 | static int mvc_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 229 | int *got_frame, AVPacket *avpkt) | ||
| 230 | { | ||
| 231 | 61 | MvcContext *s = avctx->priv_data; | |
| 232 | GetByteContext gb; | ||
| 233 | int ret; | ||
| 234 | |||
| 235 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 61 times.
|
61 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 236 | ✗ | return ret; | |
| 237 | |||
| 238 | 61 | bytestream2_init(&gb, avpkt->data, avpkt->size); | |
| 239 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 33 times.
|
61 | if (avctx->codec_id == AV_CODEC_ID_MVC1) |
| 240 | 28 | ret = decode_mvc1(avctx, &gb, frame->data[0], | |
| 241 | avctx->width, avctx->height, frame->linesize[0]); | ||
| 242 | else | ||
| 243 | 33 | ret = decode_mvc2(avctx, &gb, frame->data[0], | |
| 244 | avctx->width, avctx->height, frame->linesize[0], | ||
| 245 | s->vflip); | ||
| 246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
|
61 | if (ret < 0) |
| 247 | ✗ | return ret; | |
| 248 | |||
| 249 | 61 | *got_frame = 1; | |
| 250 | |||
| 251 | 61 | return avpkt->size; | |
| 252 | } | ||
| 253 | |||
| 254 | #if CONFIG_MVC1_DECODER | ||
| 255 | const FFCodec ff_mvc1_decoder = { | ||
| 256 | .p.name = "mvc1", | ||
| 257 | CODEC_LONG_NAME("Silicon Graphics Motion Video Compressor 1"), | ||
| 258 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 259 | .p.id = AV_CODEC_ID_MVC1, | ||
| 260 | .priv_data_size = sizeof(MvcContext), | ||
| 261 | .init = mvc_decode_init, | ||
| 262 | FF_CODEC_DECODE_CB(mvc_decode_frame), | ||
| 263 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 264 | }; | ||
| 265 | #endif | ||
| 266 | |||
| 267 | #if CONFIG_MVC2_DECODER | ||
| 268 | const FFCodec ff_mvc2_decoder = { | ||
| 269 | .p.name = "mvc2", | ||
| 270 | CODEC_LONG_NAME("Silicon Graphics Motion Video Compressor 2"), | ||
| 271 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 272 | .p.id = AV_CODEC_ID_MVC2, | ||
| 273 | .priv_data_size = sizeof(MvcContext), | ||
| 274 | .init = mvc_decode_init, | ||
| 275 | FF_CODEC_DECODE_CB(mvc_decode_frame), | ||
| 276 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 277 | }; | ||
| 278 | #endif | ||
| 279 |