GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/tdsc.c Lines: 252 322 78.3 %
Date: 2020-08-14 10:39:37 Branches: 110 182 60.4 %

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/imgutils.h"
40
41
#include "avcodec.h"
42
#include "bytestream.h"
43
#include "internal.h"
44
45
#define BITMAPINFOHEADER_SIZE 0x28
46
#define TDSF_HEADER_SIZE      0x56
47
#define TDSB_HEADER_SIZE      0x08
48
49
typedef struct TDSCContext {
50
    AVCodecContext *jpeg_avctx;   // wrapper context for MJPEG
51
52
    int width, height;
53
    GetByteContext gbc;
54
55
    AVFrame *refframe;          // full decoded frame (without cursor)
56
    AVFrame *jpgframe;          // decoded JPEG tile
57
    uint8_t *tilebuffer;        // buffer containing tile data
58
59
    /* zlib interaction */
60
    uint8_t *deflatebuffer;
61
    uLongf deflatelen;
62
63
    /* All that is cursor */
64
    uint8_t    *cursor;
65
    int        cursor_stride;
66
    int        cursor_w, cursor_h, cursor_x, cursor_y;
67
    int        cursor_hot_x, cursor_hot_y;
68
} TDSCContext;
69
70
/* 1 byte bits, 1 byte planes, 2 bytes format (probably) */
71
enum TDSCCursorFormat {
72
    CUR_FMT_MONO = 0x01010004,
73
    CUR_FMT_BGRA = 0x20010004,
74
    CUR_FMT_RGBA = 0x20010008,
75
};
76
77
2
static av_cold int tdsc_close(AVCodecContext *avctx)
78
{
79
2
    TDSCContext *ctx = avctx->priv_data;
80
81
2
    av_frame_free(&ctx->refframe);
82
2
    av_frame_free(&ctx->jpgframe);
83
2
    av_freep(&ctx->deflatebuffer);
84
2
    av_freep(&ctx->tilebuffer);
85
2
    av_freep(&ctx->cursor);
86
2
    avcodec_free_context(&ctx->jpeg_avctx);
87
88
2
    return 0;
89
}
90
91
2
static av_cold int tdsc_init(AVCodecContext *avctx)
92
{
93
2
    TDSCContext *ctx = avctx->priv_data;
94
    const AVCodec *codec;
95
    int ret;
96
97
2
    avctx->pix_fmt = AV_PIX_FMT_BGR24;
98
99
    /* These needs to be set to estimate buffer and frame size */
100

2
    if (!(avctx->width && avctx->height)) {
101
        av_log(avctx, AV_LOG_ERROR, "Video size not set.\n");
102
        return AVERROR_INVALIDDATA;
103
    }
104
105
    /* This value should be large enough for a RAW-only frame plus headers */
106
2
    ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
107
2
    ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
108
2
    if (ret < 0)
109
        return ret;
110
111
    /* Allocate reference and JPEG frame */
112
2
    ctx->refframe = av_frame_alloc();
113
2
    ctx->jpgframe = av_frame_alloc();
114

2
    if (!ctx->refframe || !ctx->jpgframe)
115
        return AVERROR(ENOMEM);
116
117
    /* Prepare everything needed for JPEG decoding */
118
2
    codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
119
2
    if (!codec)
120
        return AVERROR_BUG;
121
2
    ctx->jpeg_avctx = avcodec_alloc_context3(codec);
122
2
    if (!ctx->jpeg_avctx)
123
        return AVERROR(ENOMEM);
124
2
    ctx->jpeg_avctx->flags = avctx->flags;
125
2
    ctx->jpeg_avctx->flags2 = avctx->flags2;
126
2
    ctx->jpeg_avctx->dct_algo = avctx->dct_algo;
127
2
    ctx->jpeg_avctx->idct_algo = avctx->idct_algo;
128
2
    ret = ff_codec_open2_recursive(ctx->jpeg_avctx, codec, NULL);
129
2
    if (ret < 0)
130
        return ret;
131
132
    /* Set the output pixel format on the reference frame */
133
2
    ctx->refframe->format = avctx->pix_fmt;
134
135
2
    return 0;
136
}
137
138
#define APPLY_ALPHA(src, new, alpha) \
139
    src = (src * (256 - alpha) + new * alpha) >> 8
140
141
/* Paint a region over a buffer, without drawing out of its bounds. */
142
41
static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
143
{
144
41
    TDSCContext *ctx = avctx->priv_data;
145
41
    const uint8_t *cursor = ctx->cursor;
146
41
    int x = ctx->cursor_x - ctx->cursor_hot_x;
147
41
    int y = ctx->cursor_y - ctx->cursor_hot_y;
148
41
    int w = ctx->cursor_w;
149
41
    int h = ctx->cursor_h;
150
    int i, j;
151
152
41
    if (!ctx->cursor)
153
17
        return;
154
155
24
    if (x + w > ctx->width)
156
        w = ctx->width - x;
157
24
    if (y + h > ctx->height)
158
        h = ctx->height - y;
159
24
    if (x < 0) {
160
        w      +=  x;
161
        cursor += -x * 4;
162
    } else {
163
24
        dst    +=  x * 3;
164
    }
165
24
    if (y < 0) {
166
        h      +=  y;
167
        cursor += -y * ctx->cursor_stride;
168
    } else {
169
24
        dst    +=  y * stride;
170
    }
171

24
    if (w < 0 || h < 0)
172
        return;
173
174
799
    for (j = 0; j < h; j++) {
175
25926
        for (i = 0; i < w; i++) {
176
25151
            uint8_t alpha = cursor[i * 4];
177
25151
            APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
178
25151
            APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
179
25151
            APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
180
        }
181
775
        dst    += stride;
182
775
        cursor += ctx->cursor_stride;
183
    }
184
}
185
186
/* Load cursor data and store it in ABGR mode. */
187
3
static int tdsc_load_cursor(AVCodecContext *avctx)
188
{
189
3
    TDSCContext *ctx  = avctx->priv_data;
190
    int i, j, k, ret, cursor_fmt;
191
    uint8_t *dst;
192
193
3
    ctx->cursor_hot_x = bytestream2_get_le16(&ctx->gbc);
194
3
    ctx->cursor_hot_y = bytestream2_get_le16(&ctx->gbc);
195
3
    ctx->cursor_w     = bytestream2_get_le16(&ctx->gbc);
196
3
    ctx->cursor_h     = bytestream2_get_le16(&ctx->gbc);
197
198
3
    ctx->cursor_stride = FFALIGN(ctx->cursor_w, 32) * 4;
199
3
    cursor_fmt = bytestream2_get_le32(&ctx->gbc);
200
201

3
    if (ctx->cursor_x >= avctx->width || ctx->cursor_y >= avctx->height) {
202
        av_log(avctx, AV_LOG_ERROR,
203
               "Invalid cursor position (%d.%d outside %dx%d).\n",
204
               ctx->cursor_x, ctx->cursor_y, avctx->width, avctx->height);
205
        return AVERROR_INVALIDDATA;
206
    }
207

3
    if (ctx->cursor_w < 1 || ctx->cursor_w > 256 ||
208

3
        ctx->cursor_h < 1 || ctx->cursor_h > 256) {
209
        av_log(avctx, AV_LOG_ERROR,
210
               "Invalid cursor dimensions %dx%d.\n",
211
               ctx->cursor_w, ctx->cursor_h);
212
        return AVERROR_INVALIDDATA;
213
    }
214
3
    if (ctx->cursor_hot_x > ctx->cursor_w ||
215
3
        ctx->cursor_hot_y > ctx->cursor_h) {
216
        av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d.%d.\n",
217
               ctx->cursor_hot_x, ctx->cursor_hot_y);
218
        ctx->cursor_hot_x = FFMIN(ctx->cursor_hot_x, ctx->cursor_w - 1);
219
        ctx->cursor_hot_y = FFMIN(ctx->cursor_hot_y, ctx->cursor_h - 1);
220
    }
221
222
3
    ret = av_reallocp(&ctx->cursor, ctx->cursor_stride * ctx->cursor_h);
223
3
    if (ret < 0) {
224
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer.\n");
225
        return ret;
226
    }
227
228
3
    dst = ctx->cursor;
229
    /* here data is packed in BE */
230
3
    switch (cursor_fmt) {
231
2
    case CUR_FMT_MONO:
232
66
        for (j = 0; j < ctx->cursor_h; j++) {
233
128
            for (i = 0; i < ctx->cursor_w; i += 32) {
234
64
                uint32_t bits = bytestream2_get_be32(&ctx->gbc);
235
2112
                for (k = 0; k < 32; k++) {
236
2048
                    dst[0] = !!(bits & 0x80000000);
237
2048
                    dst   += 4;
238
2048
                    bits <<= 1;
239
                }
240
            }
241
64
            dst += ctx->cursor_stride - ctx->cursor_w * 4;
242
        }
243
244
2
        dst = ctx->cursor;
245
66
        for (j = 0; j < ctx->cursor_h; j++) {
246
128
            for (i = 0; i < ctx->cursor_w; i += 32) {
247
64
                uint32_t bits = bytestream2_get_be32(&ctx->gbc);
248
2112
                for (k = 0; k < 32; k++) {
249
2048
                    int mask_bit = !!(bits & 0x80000000);
250
2048
                    switch (dst[0] * 2 + mask_bit) {
251
104
                    case 0:
252
104
                        dst[0] = 0xFF;
253
104
                        dst[1] = 0x00;
254
104
                        dst[2] = 0x00;
255
104
                        dst[3] = 0x00;
256
104
                        break;
257
178
                    case 1:
258
178
                        dst[0] = 0xFF;
259
178
                        dst[1] = 0xFF;
260
178
                        dst[2] = 0xFF;
261
178
                        dst[3] = 0xFF;
262
178
                        break;
263
1766
                    default:
264
1766
                        dst[0] = 0x00;
265
1766
                        dst[1] = 0x00;
266
1766
                        dst[2] = 0x00;
267
1766
                        dst[3] = 0x00;
268
                    }
269
2048
                    dst   += 4;
270
2048
                    bits <<= 1;
271
                }
272
            }
273
64
            dst += ctx->cursor_stride - ctx->cursor_w * 4;
274
        }
275
2
        break;
276
1
    case CUR_FMT_BGRA:
277
    case CUR_FMT_RGBA:
278
        /* Skip monochrome version of the cursor */
279
1
        bytestream2_skip(&ctx->gbc,
280
1
                         ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 3));
281
1
        if (cursor_fmt & 8) { // RGBA -> ABGR
282
40
            for (j = 0; j < ctx->cursor_h; j++) {
283
1638
                for (i = 0; i < ctx->cursor_w; i++) {
284
1599
                    int val = bytestream2_get_be32(&ctx->gbc);
285
1599
                    *dst++ = val >> 24;
286
1599
                    *dst++ = val >> 16;
287
1599
                    *dst++ = val >>  8;
288
1599
                    *dst++ = val >>  0;
289
                }
290
39
                dst += ctx->cursor_stride - ctx->cursor_w * 4;
291
            }
292
        } else { // BGRA -> ABGR
293
            for (j = 0; j < ctx->cursor_h; j++) {
294
                for (i = 0; i < ctx->cursor_w; i++) {
295
                    int val = bytestream2_get_be32(&ctx->gbc);
296
                    *dst++ = val >>  0;
297
                    *dst++ = val >> 24;
298
                    *dst++ = val >> 16;
299
                    *dst++ = val >>  8;
300
                }
301
                dst += ctx->cursor_stride - ctx->cursor_w * 4;
302
            }
303
        }
304
1
        break;
305
    default:
306
        avpriv_request_sample(avctx, "Cursor format %08x", cursor_fmt);
307
        return AVERROR_PATCHWELCOME;
308
    }
309
310
3
    return 0;
311
}
312
313
/* Convert a single YUV pixel to RGB. */
314
1231360
static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
315
{
316
1231360
    out[0] = av_clip_uint8(Y + (             91881 * V + 32768 >> 16));
317
1231360
    out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
318
1231360
    out[2] = av_clip_uint8(Y + (116130 * U             + 32768 >> 16));
319
1231360
}
320
321
/* Convert a YUV420 buffer to a RGB buffer. */
322
273
static av_always_inline void tdsc_blit(uint8_t *dst, int dst_stride,
323
                                       const uint8_t *srcy, int srcy_stride,
324
                                       const uint8_t *srcu, const uint8_t *srcv,
325
                                       int srcuv_stride, int width, int height)
326
{
327
    int col, line;
328
8305
    for (line = 0; line < height; line++) {
329
1239392
        for (col = 0; col < width; col++)
330
1231360
            tdsc_yuv2rgb(dst + col * 3, srcy[col],
331
1231360
                         srcu[col >> 1] - 128, srcv[col >> 1] - 128);
332
333
8032
        dst  +=   dst_stride;
334
8032
        srcy +=  srcy_stride;
335
8032
        srcu += srcuv_stride * (line & 1);
336
8032
        srcv += srcuv_stride * (line & 1);
337
    }
338
273
}
339
340
/* Invoke the MJPEG decoder to decode the tile. */
341
273
static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size,
342
                                 int x, int y, int w, int h)
343
{
344
273
    TDSCContext *ctx = avctx->priv_data;
345
    AVPacket jpkt;
346
    int ret;
347
348
    /* Prepare a packet and send to the MJPEG decoder */
349
273
    av_init_packet(&jpkt);
350
273
    jpkt.data = ctx->tilebuffer;
351
273
    jpkt.size = tile_size;
352
353
273
    ret = avcodec_send_packet(ctx->jpeg_avctx, &jpkt);
354
273
    if (ret < 0) {
355
        av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
356
        return ret;
357
    }
358
359
273
    ret = avcodec_receive_frame(ctx->jpeg_avctx, ctx->jpgframe);
360

273
    if (ret < 0 || ctx->jpgframe->format != AV_PIX_FMT_YUVJ420P) {
361
        av_log(avctx, AV_LOG_ERROR,
362
               "JPEG decoding error (%d).\n", ret);
363
364
        /* Normally skip, error if explode */
365
        if (avctx->err_recognition & AV_EF_EXPLODE)
366
            return AVERROR_INVALIDDATA;
367
        else
368
            return 0;
369
    }
370
371
    /* Let's paint onto the buffer */
372
273
    tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * y,
373
273
              ctx->refframe->linesize[0],
374
273
              ctx->jpgframe->data[0], ctx->jpgframe->linesize[0],
375
273
              ctx->jpgframe->data[1], ctx->jpgframe->data[2],
376
273
              ctx->jpgframe->linesize[1], w, h);
377
378
273
    av_frame_unref(ctx->jpgframe);
379
380
273
    return 0;
381
}
382
383
/* Parse frame and either copy data or decode JPEG. */
384
36
static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
385
{
386
36
    TDSCContext *ctx = avctx->priv_data;
387
    int i;
388
389
    /* Iterate over the number of tiles */
390
598
    for (i = 0; i < number_tiles; i++) {
391
        int tile_size;
392
        int tile_mode;
393
        int x, y, x2, y2, w, h;
394
        int ret;
395
396

1124
        if (bytestream2_get_bytes_left(&ctx->gbc) < 4 ||
397
1124
            bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') ||
398
562
            bytestream2_get_bytes_left(&ctx->gbc) < TDSB_HEADER_SIZE - 4) {
399
            av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n");
400
            return AVERROR_INVALIDDATA;
401
        }
402
403
562
        tile_size = bytestream2_get_le32(&ctx->gbc);
404
562
        if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size)
405
            return AVERROR_INVALIDDATA;
406
407
562
        tile_mode = bytestream2_get_le32(&ctx->gbc);
408
562
        bytestream2_skip(&ctx->gbc, 4); // unknown
409
562
        x = bytestream2_get_le32(&ctx->gbc);
410
562
        y = bytestream2_get_le32(&ctx->gbc);
411
562
        x2 = bytestream2_get_le32(&ctx->gbc);
412
562
        y2 = bytestream2_get_le32(&ctx->gbc);
413
414


562
        if (x < 0 || y < 0 || x2 <= x || y2 <= y ||
415

562
            x2 > ctx->width || y2 > ctx->height
416
        ) {
417
            av_log(avctx, AV_LOG_ERROR,
418
                   "Invalid tile position (%d.%d %d.%d outside %dx%d).\n",
419
                   x, y, x2, y2, ctx->width, ctx->height);
420
            return AVERROR_INVALIDDATA;
421
        }
422
562
        w = x2 - x;
423
562
        h = y2 - y;
424
425
562
        ret = av_reallocp(&ctx->tilebuffer, tile_size);
426
562
        if (!ctx->tilebuffer)
427
            return ret;
428
429
562
        bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size);
430
431
562
        if (tile_mode == MKTAG('G','E','P','J')) {
432
            /* Decode JPEG tile and copy it in the reference frame */
433
273
            ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h);
434
273
            if (ret < 0)
435
                return ret;
436
289
        } else if (tile_mode == MKTAG(' ','W','A','R')) {
437
            /* Just copy the buffer to output */
438
289
            av_image_copy_plane(ctx->refframe->data[0] + x * 3 +
439
289
                                ctx->refframe->linesize[0] * y,
440
289
                                ctx->refframe->linesize[0], ctx->tilebuffer,
441
                                w * 3, w * 3, h);
442
        } else {
443
            av_log(avctx, AV_LOG_ERROR, "Unknown tile type %08x.\n", tile_mode);
444
            return AVERROR_INVALIDDATA;
445
        }
446
562
        av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, y);
447
    }
448
449
36
    return 0;
450
}
451
452
36
static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
453
{
454
36
    TDSCContext *ctx = avctx->priv_data;
455
36
    int ret, w, h, init_refframe = !ctx->refframe->data[0];
456
457
    /* BITMAPINFOHEADER
458
     * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */
459
36
    if (bytestream2_get_le32(&ctx->gbc) != BITMAPINFOHEADER_SIZE)
460
        return AVERROR_INVALIDDATA;
461
462
    /* Store size, but wait for context reinit before updating avctx */
463
36
    w =  bytestream2_get_le32(&ctx->gbc);
464
36
    h = -bytestream2_get_le32(&ctx->gbc);
465
466

72
    if (bytestream2_get_le16(&ctx->gbc) != 1 ||  // 1 plane
467
36
        bytestream2_get_le16(&ctx->gbc) != 24)   // BGR24
468
        return AVERROR_INVALIDDATA;
469
470
36
    bytestream2_skip(&ctx->gbc, 24); // unused fields
471
472
    /* Update sizes */
473

36
    if (avctx->width != w || avctx->height != h) {
474
        av_log(avctx, AV_LOG_DEBUG, "Size update %dx%d -> %d%d.\n",
475
               avctx->width, avctx->height, ctx->width, ctx->height);
476
        ret = ff_set_dimensions(avctx, w, h);
477
        if (ret < 0)
478
            return ret;
479
        init_refframe = 1;
480
    }
481
36
    ctx->refframe->width  = ctx->width  = w;
482
36
    ctx->refframe->height = ctx->height = h;
483
484
    /* Allocate the reference frame if not already done or on size change */
485
36
    if (init_refframe) {
486
1
        ret = av_frame_get_buffer(ctx->refframe, 0);
487
1
        if (ret < 0)
488
            return ret;
489
    }
490
491
    /* Decode all tiles in a frame */
492
36
    return tdsc_decode_tiles(avctx, number_tiles);
493
}
494
495
34
static int tdsc_parse_dtsm(AVCodecContext *avctx)
496
{
497
34
    TDSCContext *ctx = avctx->priv_data;
498
    int ret;
499
34
    int action = bytestream2_get_le32(&ctx->gbc);
500
501
34
    bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe?
502
503

34
    if (action == 2 || action == 3) {
504
        /* Load cursor coordinates */
505
34
        ctx->cursor_x = bytestream2_get_le32(&ctx->gbc);
506
34
        ctx->cursor_y = bytestream2_get_le32(&ctx->gbc);
507
508
        /* Load a full cursor sprite */
509
34
        if (action == 3) {
510
3
            ret = tdsc_load_cursor(avctx);
511
            /* Do not consider cursor errors fatal unless in explode mode */
512

3
            if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
513
                return ret;
514
        }
515
    } else {
516
        avpriv_request_sample(avctx, "Cursor action %d", action);
517
    }
518
519
34
    return 0;
520
}
521
522
42
static int tdsc_decode_frame(AVCodecContext *avctx, void *data,
523
                             int *got_frame, AVPacket *avpkt)
524
{
525
42
    TDSCContext *ctx = avctx->priv_data;
526
42
    AVFrame *frame = data;
527
42
    int ret, tag_header, keyframe = 0;
528
    uLongf dlen;
529
530
    /* Resize deflate buffer on resolution change */
531

42
    if (ctx->width != avctx->width || ctx->height != avctx->height) {
532
1
        ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
533
1
        ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
534
1
        if (ret < 0)
535
            return ret;
536
    }
537
42
    dlen = ctx->deflatelen;
538
539
    /* Frames are deflated, need to inflate them first */
540
42
    ret = uncompress(ctx->deflatebuffer, &dlen, avpkt->data, avpkt->size);
541
42
    if (ret) {
542
1
        av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
543
1
        return AVERROR_UNKNOWN;
544
    }
545
546
41
    bytestream2_init(&ctx->gbc, ctx->deflatebuffer, dlen);
547
548
    /* Check for tag and for size info */
549
41
    if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
550
        av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
551
        return AVERROR_INVALIDDATA;
552
    }
553
554
    /* Read tag */
555
41
    tag_header = bytestream2_get_le32(&ctx->gbc);
556
557
41
    if (tag_header == MKTAG('T','D','S','F')) {
558
        int number_tiles;
559
36
        if (bytestream2_get_bytes_left(&ctx->gbc) < TDSF_HEADER_SIZE) {
560
            av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n");
561
            return AVERROR_INVALIDDATA;
562
        }
563
        /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
564
36
        number_tiles = bytestream2_get_le32(&ctx->gbc);
565
566
36
        bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe?
567
36
        keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30;
568
569
36
        ret = tdsc_parse_tdsf(avctx, number_tiles);
570
36
        if (ret < 0)
571
            return ret;
572
573
        /* Check if there is anything else we are able to parse */
574
36
        if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4)
575
31
            tag_header = bytestream2_get_le32(&ctx->gbc);
576
    }
577
578
    /* This tag can be after a TDSF block or on its own frame */
579
41
    if (tag_header == MKTAG('D','T','S','M')) {
580
        /* First 4 bytes here are the total size in bytes for this frame */
581
34
        int tag_size = bytestream2_get_le32(&ctx->gbc);
582
583
34
        if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) {
584
            av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n");
585
            return AVERROR_INVALIDDATA;
586
        }
587
588
34
        ret = tdsc_parse_dtsm(avctx);
589
34
        if (ret < 0)
590
            return ret;
591
    }
592
593
    /* Get the output frame and copy the reference frame */
594
41
    ret = ff_get_buffer(avctx, frame, 0);
595
41
    if (ret < 0)
596
        return ret;
597
598
41
    ret = av_frame_copy(frame, ctx->refframe);
599
41
    if (ret < 0)
600
        return ret;
601
602
    /* Paint the cursor on the output frame */
603
41
    tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]);
604
605
    /* Frame is ready to be output */
606
41
    if (keyframe) {
607
2
        frame->pict_type = AV_PICTURE_TYPE_I;
608
2
        frame->key_frame = 1;
609
    } else {
610
39
        frame->pict_type = AV_PICTURE_TYPE_P;
611
    }
612
41
    *got_frame = 1;
613
614
41
    return avpkt->size;
615
}
616
617
AVCodec ff_tdsc_decoder = {
618
    .name           = "tdsc",
619
    .long_name      = NULL_IF_CONFIG_SMALL("TDSC"),
620
    .type           = AVMEDIA_TYPE_VIDEO,
621
    .id             = AV_CODEC_ID_TDSC,
622
    .init           = tdsc_init,
623
    .decode         = tdsc_decode_frame,
624
    .close          = tdsc_close,
625
    .priv_data_size = sizeof(TDSCContext),
626
    .capabilities   = AV_CODEC_CAP_DR1,
627
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
628
                      FF_CODEC_CAP_INIT_CLEANUP,
629
};