| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * TDSC decoder | ||
| 3 | * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com> | ||
| 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 | * TDSC decoder | ||
| 25 | * | ||
| 26 | * Fourcc: TSDC | ||
| 27 | * | ||
| 28 | * TDSC is very simple. It codes picture by tiles, storing them in raw BGR24 | ||
| 29 | * format or compressing them in JPEG. Frames can be full pictures or just | ||
| 30 | * updates to the previous frame. Cursor is found in its own frame or at the | ||
| 31 | * bottom of the picture. Every frame is then packed with zlib. | ||
| 32 | * | ||
| 33 | * Supports: BGR24 | ||
| 34 | */ | ||
| 35 | |||
| 36 | #include <stdint.h> | ||
| 37 | #include <zlib.h> | ||
| 38 | |||
| 39 | #include "libavutil/attributes_internal.h" | ||
| 40 | #include "libavutil/imgutils.h" | ||
| 41 | #include "libavutil/mem.h" | ||
| 42 | |||
| 43 | #include "avcodec.h" | ||
| 44 | #include "bytestream.h" | ||
| 45 | #include "codec_internal.h" | ||
| 46 | #include "decode.h" | ||
| 47 | |||
| 48 | #define BITMAPINFOHEADER_SIZE 0x28 | ||
| 49 | #define TDSF_HEADER_SIZE 0x56 | ||
| 50 | #define TDSB_HEADER_SIZE 0x08 | ||
| 51 | |||
| 52 | typedef struct TDSCContext { | ||
| 53 | AVCodecContext *jpeg_avctx; // wrapper context for MJPEG | ||
| 54 | |||
| 55 | int width, height; | ||
| 56 | GetByteContext gbc; | ||
| 57 | |||
| 58 | AVFrame *refframe; // full decoded frame (without cursor) | ||
| 59 | AVPacket *jpkt; // encoded JPEG tile | ||
| 60 | AVFrame *jpgframe; // decoded JPEG tile | ||
| 61 | uint8_t *tilebuffer; // buffer containing tile data | ||
| 62 | |||
| 63 | /* zlib interaction */ | ||
| 64 | uint8_t *deflatebuffer; | ||
| 65 | uLongf deflatelen; | ||
| 66 | |||
| 67 | /* All that is cursor */ | ||
| 68 | uint8_t *cursor; | ||
| 69 | int cursor_stride; | ||
| 70 | int cursor_w, cursor_h, cursor_x, cursor_y; | ||
| 71 | int cursor_hot_x, cursor_hot_y; | ||
| 72 | } TDSCContext; | ||
| 73 | |||
| 74 | /* 1 byte bits, 1 byte planes, 2 bytes format (probably) */ | ||
| 75 | enum TDSCCursorFormat { | ||
| 76 | CUR_FMT_MONO = 0x01010004, | ||
| 77 | CUR_FMT_BGRA = 0x20010004, | ||
| 78 | CUR_FMT_RGBA = 0x20010008, | ||
| 79 | }; | ||
| 80 | |||
| 81 | 2 | static av_cold int tdsc_close(AVCodecContext *avctx) | |
| 82 | { | ||
| 83 | 2 | TDSCContext *ctx = avctx->priv_data; | |
| 84 | |||
| 85 | 2 | av_frame_free(&ctx->refframe); | |
| 86 | 2 | av_frame_free(&ctx->jpgframe); | |
| 87 | 2 | av_packet_free(&ctx->jpkt); | |
| 88 | 2 | av_freep(&ctx->deflatebuffer); | |
| 89 | 2 | av_freep(&ctx->tilebuffer); | |
| 90 | 2 | av_freep(&ctx->cursor); | |
| 91 | 2 | avcodec_free_context(&ctx->jpeg_avctx); | |
| 92 | |||
| 93 | 2 | return 0; | |
| 94 | } | ||
| 95 | |||
| 96 | 2 | static av_cold int tdsc_init(AVCodecContext *avctx) | |
| 97 | { | ||
| 98 | 2 | TDSCContext *ctx = avctx->priv_data; | |
| 99 | int ret; | ||
| 100 | |||
| 101 | 2 | avctx->pix_fmt = AV_PIX_FMT_BGR24; | |
| 102 | |||
| 103 | /* These needs to be set to estimate buffer and frame size */ | ||
| 104 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!(avctx->width && avctx->height)) { |
| 105 | ✗ | av_log(avctx, AV_LOG_ERROR, "Video size not set.\n"); | |
| 106 | ✗ | return AVERROR_INVALIDDATA; | |
| 107 | } | ||
| 108 | |||
| 109 | /* This value should be large enough for a RAW-only frame plus headers */ | ||
| 110 | 2 | ctx->deflatelen = avctx->width * avctx->height * (3 + 1); | |
| 111 | 2 | ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen); | |
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 113 | ✗ | return ret; | |
| 114 | |||
| 115 | /* Allocate reference and JPEG frame */ | ||
| 116 | 2 | ctx->refframe = av_frame_alloc(); | |
| 117 | 2 | ctx->jpgframe = av_frame_alloc(); | |
| 118 | 2 | ctx->jpkt = av_packet_alloc(); | |
| 119 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (!ctx->refframe || !ctx->jpgframe || !ctx->jpkt) |
| 120 | ✗ | return AVERROR(ENOMEM); | |
| 121 | |||
| 122 | /* Prepare everything needed for JPEG decoding */ | ||
| 123 | EXTERN const FFCodec ff_mjpeg_decoder; | ||
| 124 | 2 | ctx->jpeg_avctx = avcodec_alloc_context3(&ff_mjpeg_decoder.p); | |
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!ctx->jpeg_avctx) |
| 126 | ✗ | return AVERROR(ENOMEM); | |
| 127 | 2 | ctx->jpeg_avctx->flags = avctx->flags; | |
| 128 | 2 | ctx->jpeg_avctx->flags2 = avctx->flags2; | |
| 129 | 2 | ctx->jpeg_avctx->idct_algo = avctx->idct_algo; | |
| 130 | 2 | ctx->jpeg_avctx->max_pixels = avctx->max_pixels; | |
| 131 | 2 | ret = avcodec_open2(ctx->jpeg_avctx, NULL, NULL); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 133 | ✗ | return ret; | |
| 134 | |||
| 135 | /* Set the output pixel format on the reference frame */ | ||
| 136 | 2 | ctx->refframe->format = avctx->pix_fmt; | |
| 137 | |||
| 138 | 2 | return 0; | |
| 139 | } | ||
| 140 | |||
| 141 | #define APPLY_ALPHA(src, new, alpha) \ | ||
| 142 | src = (src * (256 - alpha) + new * alpha) >> 8 | ||
| 143 | |||
| 144 | /* Paint a region over a buffer, without drawing out of its bounds. */ | ||
| 145 | 41 | static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride) | |
| 146 | { | ||
| 147 | 41 | TDSCContext *ctx = avctx->priv_data; | |
| 148 | 41 | const uint8_t *cursor = ctx->cursor; | |
| 149 | int x, y; | ||
| 150 | 41 | int w = ctx->cursor_w; | |
| 151 | 41 | int h = ctx->cursor_h; | |
| 152 | int i, j; | ||
| 153 | |||
| 154 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 24 times.
|
41 | if (!ctx->cursor) |
| 155 | 17 | return; | |
| 156 | |||
| 157 | /* A cursor position outside the frame is invalid; skip drawing it. | ||
| 158 | * cursor_x/y come straight from the bitstream, so bound them before | ||
| 159 | * the (16 bit) hot spot shift to avoid overflowing the clip math. */ | ||
| 160 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if ((unsigned)ctx->cursor_x >= ctx->width || |
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | (unsigned)ctx->cursor_y >= ctx->height) |
| 162 | ✗ | return; | |
| 163 | |||
| 164 | 24 | x = ctx->cursor_x - ctx->cursor_hot_x; | |
| 165 | 24 | y = ctx->cursor_y - ctx->cursor_hot_y; | |
| 166 | |||
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (x + w > ctx->width) |
| 168 | ✗ | w = ctx->width - x; | |
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (y + h > ctx->height) |
| 170 | ✗ | h = ctx->height - y; | |
| 171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (x < 0) { |
| 172 | ✗ | w += x; | |
| 173 | ✗ | cursor += -x * 4; | |
| 174 | } else { | ||
| 175 | 24 | dst += x * 3; | |
| 176 | } | ||
| 177 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (y < 0) { |
| 178 | ✗ | h += y; | |
| 179 | ✗ | cursor += -y * ctx->cursor_stride; | |
| 180 | } else { | ||
| 181 | 24 | dst += y * stride; | |
| 182 | } | ||
| 183 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | if (w < 0 || h < 0) |
| 184 | ✗ | return; | |
| 185 | |||
| 186 |
2/2✓ Branch 0 taken 775 times.
✓ Branch 1 taken 24 times.
|
799 | for (j = 0; j < h; j++) { |
| 187 |
2/2✓ Branch 0 taken 25151 times.
✓ Branch 1 taken 775 times.
|
25926 | for (i = 0; i < w; i++) { |
| 188 | 25151 | uint8_t alpha = cursor[i * 4]; | |
| 189 | 25151 | APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha); | |
| 190 | 25151 | APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha); | |
| 191 | 25151 | APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha); | |
| 192 | } | ||
| 193 | 775 | dst += stride; | |
| 194 | 775 | cursor += ctx->cursor_stride; | |
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | /* Load cursor data and store it in ABGR mode. */ | ||
| 199 | 3 | static int tdsc_load_cursor(AVCodecContext *avctx) | |
| 200 | { | ||
| 201 | 3 | TDSCContext *ctx = avctx->priv_data; | |
| 202 | int i, j, k, ret, cursor_fmt; | ||
| 203 | uint8_t *dst; | ||
| 204 | |||
| 205 | 3 | ctx->cursor_hot_x = bytestream2_get_le16(&ctx->gbc); | |
| 206 | 3 | ctx->cursor_hot_y = bytestream2_get_le16(&ctx->gbc); | |
| 207 | 3 | ctx->cursor_w = bytestream2_get_le16(&ctx->gbc); | |
| 208 | 3 | ctx->cursor_h = bytestream2_get_le16(&ctx->gbc); | |
| 209 | |||
| 210 | 3 | ctx->cursor_stride = FFALIGN(ctx->cursor_w, 32) * 4; | |
| 211 | 3 | cursor_fmt = bytestream2_get_le32(&ctx->gbc); | |
| 212 | |||
| 213 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (ctx->cursor_w < 1 || ctx->cursor_w > 256 || |
| 214 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | ctx->cursor_h < 1 || ctx->cursor_h > 256) { |
| 215 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 216 | "Invalid cursor dimensions %dx%d.\n", | ||
| 217 | ctx->cursor_w, ctx->cursor_h); | ||
| 218 | ✗ | return AVERROR_INVALIDDATA; | |
| 219 | } | ||
| 220 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (ctx->cursor_hot_x > ctx->cursor_w || |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | ctx->cursor_hot_y > ctx->cursor_h) { |
| 222 | ✗ | av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d.%d.\n", | |
| 223 | ctx->cursor_hot_x, ctx->cursor_hot_y); | ||
| 224 | ✗ | ctx->cursor_hot_x = FFMIN(ctx->cursor_hot_x, ctx->cursor_w - 1); | |
| 225 | ✗ | ctx->cursor_hot_y = FFMIN(ctx->cursor_hot_y, ctx->cursor_h - 1); | |
| 226 | } | ||
| 227 | |||
| 228 | 3 | ret = av_reallocp(&ctx->cursor, ctx->cursor_stride * ctx->cursor_h); | |
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) { |
| 230 | ✗ | av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer.\n"); | |
| 231 | ✗ | return ret; | |
| 232 | } | ||
| 233 | |||
| 234 | 3 | dst = ctx->cursor; | |
| 235 | /* here data is packed in BE */ | ||
| 236 |
2/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
3 | switch (cursor_fmt) { |
| 237 | 2 | case CUR_FMT_MONO: | |
| 238 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2 times.
|
66 | for (j = 0; j < ctx->cursor_h; j++) { |
| 239 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
|
128 | for (i = 0; i < ctx->cursor_w; i += 32) { |
| 240 | 64 | uint32_t bits = bytestream2_get_be32(&ctx->gbc); | |
| 241 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 64 times.
|
2112 | for (k = 0; k < 32; k++) { |
| 242 | 2048 | dst[0] = !!(bits & 0x80000000); | |
| 243 | 2048 | dst += 4; | |
| 244 | 2048 | bits <<= 1; | |
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | 2 | dst = ctx->cursor; | |
| 250 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2 times.
|
66 | for (j = 0; j < ctx->cursor_h; j++) { |
| 251 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
|
128 | for (i = 0; i < ctx->cursor_w; i += 32) { |
| 252 | 64 | uint32_t bits = bytestream2_get_be32(&ctx->gbc); | |
| 253 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 64 times.
|
2112 | for (k = 0; k < 32; k++) { |
| 254 | 2048 | int mask_bit = !!(bits & 0x80000000); | |
| 255 |
3/3✓ Branch 0 taken 104 times.
✓ Branch 1 taken 178 times.
✓ Branch 2 taken 1766 times.
|
2048 | switch (dst[0] * 2 + mask_bit) { |
| 256 | 104 | case 0: | |
| 257 | 104 | dst[0] = 0xFF; | |
| 258 | 104 | dst[1] = 0x00; | |
| 259 | 104 | dst[2] = 0x00; | |
| 260 | 104 | dst[3] = 0x00; | |
| 261 | 104 | break; | |
| 262 | 178 | case 1: | |
| 263 | 178 | dst[0] = 0xFF; | |
| 264 | 178 | dst[1] = 0xFF; | |
| 265 | 178 | dst[2] = 0xFF; | |
| 266 | 178 | dst[3] = 0xFF; | |
| 267 | 178 | break; | |
| 268 | 1766 | default: | |
| 269 | 1766 | dst[0] = 0x00; | |
| 270 | 1766 | dst[1] = 0x00; | |
| 271 | 1766 | dst[2] = 0x00; | |
| 272 | 1766 | dst[3] = 0x00; | |
| 273 | } | ||
| 274 | 2048 | dst += 4; | |
| 275 | 2048 | bits <<= 1; | |
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | 2 | break; | |
| 280 | 1 | case CUR_FMT_BGRA: | |
| 281 | case CUR_FMT_RGBA: | ||
| 282 | /* Skip monochrome version of the cursor */ | ||
| 283 | 1 | bytestream2_skip(&ctx->gbc, | |
| 284 | 1 | ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 3)); | |
| 285 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (cursor_fmt & 8) { // RGBA -> ABGR |
| 286 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1 times.
|
40 | for (j = 0; j < ctx->cursor_h; j++) { |
| 287 |
2/2✓ Branch 0 taken 1599 times.
✓ Branch 1 taken 39 times.
|
1638 | for (i = 0; i < ctx->cursor_w; i++) { |
| 288 | 1599 | int val = bytestream2_get_be32(&ctx->gbc); | |
| 289 | 1599 | *dst++ = val >> 24; | |
| 290 | 1599 | *dst++ = val >> 16; | |
| 291 | 1599 | *dst++ = val >> 8; | |
| 292 | 1599 | *dst++ = val >> 0; | |
| 293 | } | ||
| 294 | 39 | dst += ctx->cursor_stride - ctx->cursor_w * 4; | |
| 295 | } | ||
| 296 | } else { // BGRA -> ABGR | ||
| 297 | ✗ | for (j = 0; j < ctx->cursor_h; j++) { | |
| 298 | ✗ | for (i = 0; i < ctx->cursor_w; i++) { | |
| 299 | ✗ | int val = bytestream2_get_be32(&ctx->gbc); | |
| 300 | ✗ | *dst++ = val >> 0; | |
| 301 | ✗ | *dst++ = val >> 24; | |
| 302 | ✗ | *dst++ = val >> 16; | |
| 303 | ✗ | *dst++ = val >> 8; | |
| 304 | } | ||
| 305 | ✗ | dst += ctx->cursor_stride - ctx->cursor_w * 4; | |
| 306 | } | ||
| 307 | } | ||
| 308 | 1 | break; | |
| 309 | ✗ | default: | |
| 310 | ✗ | avpriv_request_sample(avctx, "Cursor format %08x", cursor_fmt); | |
| 311 | ✗ | return AVERROR_PATCHWELCOME; | |
| 312 | } | ||
| 313 | |||
| 314 | 3 | return 0; | |
| 315 | } | ||
| 316 | |||
| 317 | /* Convert a single YUV pixel to RGB. */ | ||
| 318 | 1231360 | static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V) | |
| 319 | { | ||
| 320 | 1231360 | out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16)); | |
| 321 | 1231360 | out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16)); | |
| 322 | 1231360 | out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16)); | |
| 323 | 1231360 | } | |
| 324 | |||
| 325 | /* Convert a YUV420 buffer to a RGB buffer. */ | ||
| 326 | 273 | static av_always_inline void tdsc_blit(uint8_t *dst, int dst_stride, | |
| 327 | const uint8_t *srcy, int srcy_stride, | ||
| 328 | const uint8_t *srcu, const uint8_t *srcv, | ||
| 329 | int srcuv_stride, int width, int height) | ||
| 330 | { | ||
| 331 | int col, line; | ||
| 332 |
2/2✓ Branch 0 taken 8032 times.
✓ Branch 1 taken 273 times.
|
8305 | for (line = 0; line < height; line++) { |
| 333 |
2/2✓ Branch 0 taken 1231360 times.
✓ Branch 1 taken 8032 times.
|
1239392 | for (col = 0; col < width; col++) |
| 334 | 1231360 | tdsc_yuv2rgb(dst + col * 3, srcy[col], | |
| 335 | 1231360 | srcu[col >> 1] - 128, srcv[col >> 1] - 128); | |
| 336 | |||
| 337 | 8032 | dst += dst_stride; | |
| 338 | 8032 | srcy += srcy_stride; | |
| 339 | 8032 | srcu += srcuv_stride * (line & 1); | |
| 340 | 8032 | srcv += srcuv_stride * (line & 1); | |
| 341 | } | ||
| 342 | 273 | } | |
| 343 | |||
| 344 | /* Invoke the MJPEG decoder to decode the tile. */ | ||
| 345 | 273 | static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size, | |
| 346 | int x, int y, int w, int h) | ||
| 347 | { | ||
| 348 | 273 | TDSCContext *ctx = avctx->priv_data; | |
| 349 | int ret; | ||
| 350 | |||
| 351 | /* Prepare a packet and send to the MJPEG decoder */ | ||
| 352 | 273 | av_packet_unref(ctx->jpkt); | |
| 353 | 273 | ctx->jpkt->data = ctx->tilebuffer; | |
| 354 | 273 | ctx->jpkt->size = tile_size; | |
| 355 | |||
| 356 | 273 | ret = avcodec_send_packet(ctx->jpeg_avctx, ctx->jpkt); | |
| 357 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
|
273 | if (ret < 0) { |
| 358 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); | |
| 359 | ✗ | return ret; | |
| 360 | } | ||
| 361 | |||
| 362 | 273 | ret = avcodec_receive_frame(ctx->jpeg_avctx, ctx->jpgframe); | |
| 363 |
2/4✓ Branch 0 taken 273 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 273 times.
✗ Branch 3 not taken.
|
273 | if (ret < 0 || ctx->jpgframe->format != AV_PIX_FMT_YUVJ420P || |
| 364 |
2/4✓ Branch 0 taken 273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 273 times.
|
273 | w > ctx->jpgframe->width || h > ctx->jpgframe->height) { |
| 365 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 366 | "JPEG decoding error (%d).\n", ret); | ||
| 367 | |||
| 368 | /* Normally skip, error if explode */ | ||
| 369 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
| 370 | ✗ | return AVERROR_INVALIDDATA; | |
| 371 | else | ||
| 372 | ✗ | return 0; | |
| 373 | } | ||
| 374 | |||
| 375 | /* Let's paint onto the buffer */ | ||
| 376 | 273 | tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * y, | |
| 377 | 273 | ctx->refframe->linesize[0], | |
| 378 | 273 | ctx->jpgframe->data[0], ctx->jpgframe->linesize[0], | |
| 379 | 273 | ctx->jpgframe->data[1], ctx->jpgframe->data[2], | |
| 380 | 273 | ctx->jpgframe->linesize[1], w, h); | |
| 381 | |||
| 382 | 273 | av_frame_unref(ctx->jpgframe); | |
| 383 | |||
| 384 | 273 | return 0; | |
| 385 | } | ||
| 386 | |||
| 387 | /* Parse frame and either copy data or decode JPEG. */ | ||
| 388 | 36 | static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles) | |
| 389 | { | ||
| 390 | 36 | TDSCContext *ctx = avctx->priv_data; | |
| 391 | int i; | ||
| 392 | |||
| 393 | /* Iterate over the number of tiles */ | ||
| 394 |
2/2✓ Branch 0 taken 562 times.
✓ Branch 1 taken 36 times.
|
598 | for (i = 0; i < number_tiles; i++) { |
| 395 | int tile_size; | ||
| 396 | int tile_mode; | ||
| 397 | int x, y, x2, y2, w, h; | ||
| 398 | int ret; | ||
| 399 | |||
| 400 |
2/4✓ Branch 1 taken 562 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 562 times.
✗ Branch 4 not taken.
|
1124 | if (bytestream2_get_bytes_left(&ctx->gbc) < 4 || |
| 401 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 562 times.
|
1124 | bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') || |
| 402 | 562 | bytestream2_get_bytes_left(&ctx->gbc) < TDSB_HEADER_SIZE - 4) { | |
| 403 | ✗ | av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n"); | |
| 404 | ✗ | return AVERROR_INVALIDDATA; | |
| 405 | } | ||
| 406 | |||
| 407 | 562 | tile_size = bytestream2_get_le32(&ctx->gbc); | |
| 408 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 562 times.
|
562 | if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size + 24LL) |
| 409 | ✗ | return AVERROR_INVALIDDATA; | |
| 410 | |||
| 411 | 562 | tile_mode = bytestream2_get_le32(&ctx->gbc); | |
| 412 | 562 | bytestream2_skip(&ctx->gbc, 4); // unknown | |
| 413 | 562 | x = bytestream2_get_le32(&ctx->gbc); | |
| 414 | 562 | y = bytestream2_get_le32(&ctx->gbc); | |
| 415 | 562 | x2 = bytestream2_get_le32(&ctx->gbc); | |
| 416 | 562 | y2 = bytestream2_get_le32(&ctx->gbc); | |
| 417 | |||
| 418 |
4/8✓ Branch 0 taken 562 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 562 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 562 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 562 times.
✗ Branch 7 not taken.
|
562 | if (x < 0 || y < 0 || x2 <= x || y2 <= y || |
| 419 |
2/4✓ Branch 0 taken 562 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 562 times.
|
562 | x2 > ctx->width || y2 > ctx->height |
| 420 | ) { | ||
| 421 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
| 422 | "Invalid tile position (%d.%d %d.%d outside %dx%d).\n", | ||
| 423 | x, y, x2, y2, ctx->width, ctx->height); | ||
| 424 | ✗ | return AVERROR_INVALIDDATA; | |
| 425 | } | ||
| 426 | 562 | w = x2 - x; | |
| 427 | 562 | h = y2 - y; | |
| 428 | |||
| 429 | 562 | ret = av_reallocp(&ctx->tilebuffer, tile_size); | |
| 430 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 562 times.
|
562 | if (!ctx->tilebuffer) |
| 431 | ✗ | return ret; | |
| 432 | |||
| 433 | 562 | bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size); | |
| 434 | |||
| 435 |
2/2✓ Branch 0 taken 273 times.
✓ Branch 1 taken 289 times.
|
562 | if (tile_mode == MKTAG('G','E','P','J')) { |
| 436 | /* Decode JPEG tile and copy it in the reference frame */ | ||
| 437 | 273 | ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h); | |
| 438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
|
273 | if (ret < 0) |
| 439 | ✗ | return ret; | |
| 440 |
1/2✓ Branch 0 taken 289 times.
✗ Branch 1 not taken.
|
289 | } else if (tile_mode == MKTAG(' ','W','A','R')) { |
| 441 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 289 times.
|
289 | if (3LL * w * h > tile_size) |
| 442 | ✗ | return AVERROR_INVALIDDATA; | |
| 443 | |||
| 444 | /* Just copy the buffer to output */ | ||
| 445 | 289 | av_image_copy_plane(ctx->refframe->data[0] + x * 3 + | |
| 446 | 289 | ctx->refframe->linesize[0] * y, | |
| 447 | 289 | ctx->refframe->linesize[0], ctx->tilebuffer, | |
| 448 | w * 3, w * 3, h); | ||
| 449 | } else { | ||
| 450 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown tile type %08x.\n", tile_mode); | |
| 451 | ✗ | return AVERROR_INVALIDDATA; | |
| 452 | } | ||
| 453 | 562 | av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, y); | |
| 454 | } | ||
| 455 | |||
| 456 | 36 | return 0; | |
| 457 | } | ||
| 458 | |||
| 459 | 36 | static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles) | |
| 460 | { | ||
| 461 | 36 | TDSCContext *ctx = avctx->priv_data; | |
| 462 | 36 | int ret, w, h, init_refframe = !ctx->refframe->data[0]; | |
| 463 | |||
| 464 | /* BITMAPINFOHEADER | ||
| 465 | * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */ | ||
| 466 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | if (bytestream2_get_le32(&ctx->gbc) != BITMAPINFOHEADER_SIZE) |
| 467 | ✗ | return AVERROR_INVALIDDATA; | |
| 468 | |||
| 469 | /* Store size, but wait for context reinit before updating avctx */ | ||
| 470 | 36 | w = bytestream2_get_le32(&ctx->gbc); | |
| 471 | 36 | h = -bytestream2_get_le32(&ctx->gbc); | |
| 472 | |||
| 473 |
2/4✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 36 times.
|
72 | if (bytestream2_get_le16(&ctx->gbc) != 1 || // 1 plane |
| 474 | 36 | bytestream2_get_le16(&ctx->gbc) != 24) // BGR24 | |
| 475 | ✗ | return AVERROR_INVALIDDATA; | |
| 476 | |||
| 477 | 36 | bytestream2_skip(&ctx->gbc, 24); // unused fields | |
| 478 | |||
| 479 | /* Update sizes */ | ||
| 480 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
|
36 | if (avctx->width != w || avctx->height != h) { |
| 481 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Size update %dx%d -> %d%d.\n", | |
| 482 | avctx->width, avctx->height, ctx->width, ctx->height); | ||
| 483 | ✗ | ret = ff_set_dimensions(avctx, w, h); | |
| 484 | ✗ | if (ret < 0) | |
| 485 | ✗ | return ret; | |
| 486 | ✗ | init_refframe = 1; | |
| 487 | } | ||
| 488 | 36 | ctx->width = w; | |
| 489 | 36 | ctx->height = h; | |
| 490 | |||
| 491 | /* Allocate the reference frame if not already done or on size change */ | ||
| 492 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 35 times.
|
36 | if (init_refframe) { |
| 493 | 1 | av_frame_unref(ctx->refframe); | |
| 494 | 1 | ctx->refframe->format = avctx->pix_fmt; | |
| 495 | 1 | ctx->refframe->width = w; | |
| 496 | 1 | ctx->refframe->height = h; | |
| 497 | 1 | ret = av_frame_get_buffer(ctx->refframe, 0); | |
| 498 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 499 | ✗ | return ret; | |
| 500 | } | ||
| 501 | |||
| 502 | /* Decode all tiles in a frame */ | ||
| 503 | 36 | return tdsc_decode_tiles(avctx, number_tiles); | |
| 504 | } | ||
| 505 | |||
| 506 | 34 | static int tdsc_parse_dtsm(AVCodecContext *avctx) | |
| 507 | { | ||
| 508 | 34 | TDSCContext *ctx = avctx->priv_data; | |
| 509 | int ret; | ||
| 510 | 34 | int action = bytestream2_get_le32(&ctx->gbc); | |
| 511 | |||
| 512 | 34 | bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe? | |
| 513 | |||
| 514 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
34 | if (action == 2 || action == 3) { |
| 515 | /* Load cursor coordinates */ | ||
| 516 | 34 | ctx->cursor_x = bytestream2_get_le32(&ctx->gbc); | |
| 517 | 34 | ctx->cursor_y = bytestream2_get_le32(&ctx->gbc); | |
| 518 | |||
| 519 | /* Load a full cursor sprite */ | ||
| 520 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
|
34 | if (action == 3) { |
| 521 | 3 | ret = tdsc_load_cursor(avctx); | |
| 522 | /* Do not consider cursor errors fatal unless in explode mode */ | ||
| 523 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3 | if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) |
| 524 | ✗ | return ret; | |
| 525 | } | ||
| 526 | } else { | ||
| 527 | ✗ | avpriv_request_sample(avctx, "Cursor action %d", action); | |
| 528 | } | ||
| 529 | |||
| 530 | 34 | return 0; | |
| 531 | } | ||
| 532 | |||
| 533 | 42 | static int tdsc_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 534 | int *got_frame, AVPacket *avpkt) | ||
| 535 | { | ||
| 536 | 42 | TDSCContext *ctx = avctx->priv_data; | |
| 537 | 42 | int ret, tag_header, keyframe = 0; | |
| 538 | uLongf dlen; | ||
| 539 | |||
| 540 | /* Resize deflate buffer on resolution change */ | ||
| 541 |
3/4✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 41 times.
|
42 | if (ctx->width != avctx->width || ctx->height != avctx->height) { |
| 542 | 1 | int deflatelen = avctx->width * avctx->height * (3 + 1); | |
| 543 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (deflatelen != ctx->deflatelen) { |
| 544 | ✗ | ctx->deflatelen =deflatelen; | |
| 545 | ✗ | ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen); | |
| 546 | ✗ | if (ret < 0) { | |
| 547 | ✗ | ctx->deflatelen = 0; | |
| 548 | ✗ | return ret; | |
| 549 | } | ||
| 550 | } | ||
| 551 | } | ||
| 552 | 42 | dlen = ctx->deflatelen; | |
| 553 | |||
| 554 | /* Frames are deflated, need to inflate them first */ | ||
| 555 | 42 | ret = uncompress(ctx->deflatebuffer, &dlen, avpkt->data, avpkt->size); | |
| 556 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 41 times.
|
42 | if (ret != Z_OK) { |
| 557 | 1 | av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret); | |
| 558 | 1 | return AVERROR_UNKNOWN; | |
| 559 | } | ||
| 560 | |||
| 561 | 41 | bytestream2_init(&ctx->gbc, ctx->deflatebuffer, dlen); | |
| 562 | |||
| 563 | /* Check for tag and for size info */ | ||
| 564 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
|
41 | if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) { |
| 565 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n"); | |
| 566 | ✗ | return AVERROR_INVALIDDATA; | |
| 567 | } | ||
| 568 | |||
| 569 | /* Read tag */ | ||
| 570 | 41 | tag_header = bytestream2_get_le32(&ctx->gbc); | |
| 571 | |||
| 572 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 5 times.
|
41 | if (tag_header == MKTAG('T','D','S','F')) { |
| 573 | int number_tiles; | ||
| 574 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
|
36 | if (bytestream2_get_bytes_left(&ctx->gbc) < TDSF_HEADER_SIZE) { |
| 575 | ✗ | av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n"); | |
| 576 | ✗ | return AVERROR_INVALIDDATA; | |
| 577 | } | ||
| 578 | /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */ | ||
| 579 | 36 | number_tiles = bytestream2_get_le32(&ctx->gbc); | |
| 580 | |||
| 581 | 36 | bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe? | |
| 582 | 36 | keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30; | |
| 583 | |||
| 584 | 36 | ret = tdsc_parse_tdsf(avctx, number_tiles); | |
| 585 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (ret < 0) |
| 586 | ✗ | return ret; | |
| 587 | |||
| 588 | /* Check if there is anything else we are able to parse */ | ||
| 589 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 5 times.
|
36 | if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4) |
| 590 | 31 | tag_header = bytestream2_get_le32(&ctx->gbc); | |
| 591 | } | ||
| 592 | |||
| 593 | /* This tag can be after a TDSF block or on its own frame */ | ||
| 594 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 7 times.
|
41 | if (tag_header == MKTAG('D','T','S','M')) { |
| 595 | /* First 4 bytes here are the total size in bytes for this frame */ | ||
| 596 | 34 | int tag_size = bytestream2_get_le32(&ctx->gbc); | |
| 597 | |||
| 598 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
|
34 | if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) { |
| 599 | ✗ | av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n"); | |
| 600 | ✗ | return AVERROR_INVALIDDATA; | |
| 601 | } | ||
| 602 | |||
| 603 | 34 | ret = tdsc_parse_dtsm(avctx); | |
| 604 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | if (ret < 0) |
| 605 | ✗ | return ret; | |
| 606 | } | ||
| 607 | |||
| 608 | /* Get the output frame and copy the reference frame */ | ||
| 609 | 41 | ret = ff_get_buffer(avctx, frame, 0); | |
| 610 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (ret < 0) |
| 611 | ✗ | return ret; | |
| 612 | |||
| 613 | 41 | ret = av_frame_copy(frame, ctx->refframe); | |
| 614 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
41 | if (ret < 0) |
| 615 | ✗ | return ret; | |
| 616 | |||
| 617 | /* Paint the cursor on the output frame */ | ||
| 618 | 41 | tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]); | |
| 619 | |||
| 620 | /* Frame is ready to be output */ | ||
| 621 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39 times.
|
41 | if (keyframe) { |
| 622 | 2 | frame->pict_type = AV_PICTURE_TYPE_I; | |
| 623 | 2 | frame->flags |= AV_FRAME_FLAG_KEY; | |
| 624 | } else { | ||
| 625 | 39 | frame->pict_type = AV_PICTURE_TYPE_P; | |
| 626 | } | ||
| 627 | 41 | *got_frame = 1; | |
| 628 | |||
| 629 | 41 | return avpkt->size; | |
| 630 | } | ||
| 631 | |||
| 632 | const FFCodec ff_tdsc_decoder = { | ||
| 633 | .p.name = "tdsc", | ||
| 634 | CODEC_LONG_NAME("TDSC"), | ||
| 635 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 636 | .p.id = AV_CODEC_ID_TDSC, | ||
| 637 | .init = tdsc_init, | ||
| 638 | FF_CODEC_DECODE_CB(tdsc_decode_frame), | ||
| 639 | .close = tdsc_close, | ||
| 640 | .priv_data_size = sizeof(TDSCContext), | ||
| 641 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 642 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 643 | }; | ||
| 644 |