../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
261 / 333
78.4%
Branches:
117 / 196
59.7%
Functions:
11 / 11
100.0%

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