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
40
45
46 #define BITMAPINFOHEADER_SIZE 0x28
47 #define TDSF_HEADER_SIZE 0x56
48 #define TDSB_HEADER_SIZE 0x08
49
52
55
60
61 /* zlib interaction */
64
65 /* All that is cursor */
71
72 /* 1 byte bits, 1 byte planes, 2 bytes format (probably) */
77 };
78
80 {
82
90
91 return 0;
92 }
93
95 {
99
101
102 /* These needs to be set to estimate buffer and frame size */
106 }
107
108 /* This value should be large enough for a RAW-only frame plus headers */
113
114 /* Allocate reference and JPEG frame */
118 if (!
ctx->refframe || !
ctx->jpgframe || !
ctx->jpkt)
120
121 /* Prepare everything needed for JPEG decoding */
123 if (!codec)
126 if (!
ctx->jpeg_avctx)
135
136 /* Set the output pixel format on the reference frame */
138
139 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. */
147 {
149 const uint8_t *cursor =
ctx->cursor;
150 int x =
ctx->cursor_x -
ctx->cursor_hot_x;
151 int y =
ctx->cursor_y -
ctx->cursor_hot_y;
152 int w =
ctx->cursor_w;
153 int h =
ctx->cursor_h;
155
157 return;
158
159 if (x +
w >
ctx->width)
161 if (y +
h >
ctx->height)
163 if (x < 0) {
165 cursor += -x * 4;
166 } else {
167 dst += x * 3;
168 }
169 if (y < 0) {
171 cursor += -y *
ctx->cursor_stride;
172 } else {
174 }
176 return;
177
178 for (j = 0; j <
h; j++) {
179 for (
i = 0;
i <
w;
i++) {
180 uint8_t
alpha = cursor[
i * 4];
184 }
186 cursor +=
ctx->cursor_stride;
187 }
188 }
189
190 /* Load cursor data and store it in ABGR mode. */
192 {
194 int i, j, k,
ret, cursor_fmt;
195 uint8_t *dst;
196
197 ctx->cursor_hot_x = bytestream2_get_le16(&
ctx->gbc);
198 ctx->cursor_hot_y = bytestream2_get_le16(&
ctx->gbc);
199 ctx->cursor_w = bytestream2_get_le16(&
ctx->gbc);
200 ctx->cursor_h = bytestream2_get_le16(&
ctx->gbc);
201
203 cursor_fmt = bytestream2_get_le32(&
ctx->gbc);
204
207 "Invalid cursor position (%d.%d outside %dx%d).\n",
210 }
211 if (
ctx->cursor_w < 1 ||
ctx->cursor_w > 256 ||
212 ctx->cursor_h < 1 ||
ctx->cursor_h > 256) {
214 "Invalid cursor dimensions %dx%d.\n",
215 ctx->cursor_w,
ctx->cursor_h);
217 }
218 if (
ctx->cursor_hot_x >
ctx->cursor_w ||
219 ctx->cursor_hot_y >
ctx->cursor_h) {
221 ctx->cursor_hot_x,
ctx->cursor_hot_y);
224 }
225
230 }
231
233 /* here data is packed in BE */
234 switch (cursor_fmt) {
236 for (j = 0; j <
ctx->cursor_h; j++) {
237 for (
i = 0;
i <
ctx->cursor_w;
i += 32) {
238 uint32_t
bits = bytestream2_get_be32(&
ctx->gbc);
239 for (k = 0; k < 32; k++) {
240 dst[0] = !!(
bits & 0x80000000);
241 dst += 4;
243 }
244 }
245 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
246 }
247
249 for (j = 0; j <
ctx->cursor_h; j++) {
250 for (
i = 0;
i <
ctx->cursor_w;
i += 32) {
251 uint32_t
bits = bytestream2_get_be32(&
ctx->gbc);
252 for (k = 0; k < 32; k++) {
253 int mask_bit = !!(
bits & 0x80000000);
254 switch (dst[0] * 2 + mask_bit) {
255 case 0:
256 dst[0] = 0xFF;
257 dst[1] = 0x00;
258 dst[2] = 0x00;
259 dst[3] = 0x00;
260 break;
261 case 1:
262 dst[0] = 0xFF;
263 dst[1] = 0xFF;
264 dst[2] = 0xFF;
265 dst[3] = 0xFF;
266 break;
267 default:
268 dst[0] = 0x00;
269 dst[1] = 0x00;
270 dst[2] = 0x00;
271 dst[3] = 0x00;
272 }
273 dst += 4;
275 }
276 }
277 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
278 }
279 break;
282 /* Skip monochrome version of the cursor */
285 if (cursor_fmt & 8) { // RGBA -> ABGR
286 for (j = 0; j <
ctx->cursor_h; j++) {
287 for (
i = 0;
i <
ctx->cursor_w;
i++) {
288 int val = bytestream2_get_be32(&
ctx->gbc);
293 }
294 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);
304 }
305 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
306 }
307 }
308 break;
309 default:
312 }
313
314 return 0;
315 }
316
317 /* Convert a single YUV pixel to RGB. */
319 {
323 }
324
325 /* Convert a YUV420 buffer to a RGB buffer. */
327 const uint8_t *srcy, int srcy_stride,
328 const uint8_t *srcu, const uint8_t *srcv,
330 {
333 for (col = 0; col <
width; col++)
335 srcu[col >> 1] - 128, srcv[col >> 1] - 128);
336
337 dst += dst_stride;
338 srcy += srcy_stride;
339 srcu += srcuv_stride * (
line & 1);
340 srcv += srcuv_stride * (
line & 1);
341 }
342 }
343
344 /* Invoke the MJPEG decoder to decode the tile. */
346 int x,
int y,
int w,
int h)
347 {
350
351 /* Prepare a packet and send to the MJPEG decoder */
353 ctx->jpkt->data =
ctx->tilebuffer;
354 ctx->jpkt->size = tile_size;
355
360 }
361
365 "JPEG decoding error (%d).\n",
ret);
366
367 /* Normally skip, error if explode */
370 else
371 return 0;
372 }
373
374 /* Let's paint onto the buffer */
375 tdsc_blit(
ctx->refframe->data[0] + x * 3 +
ctx->refframe->linesize[0] * y,
376 ctx->refframe->linesize[0],
377 ctx->jpgframe->data[0],
ctx->jpgframe->linesize[0],
378 ctx->jpgframe->data[1],
ctx->jpgframe->data[2],
379 ctx->jpgframe->linesize[1],
w,
h);
380
382
383 return 0;
384 }
385
386 /* Parse frame and either copy data or decode JPEG. */
388 {
391
392 /* Iterate over the number of tiles */
393 for (
i = 0;
i < number_tiles;
i++) {
394 int tile_size;
395 int tile_mode;
396 int x, y, x2, y2,
w,
h;
398
400 bytestream2_get_le32(&
ctx->gbc) !=
MKTAG(
'T',
'D',
'S',
'B') ||
404 }
405
406 tile_size = bytestream2_get_le32(&
ctx->gbc);
409
410 tile_mode = bytestream2_get_le32(&
ctx->gbc);
412 x = bytestream2_get_le32(&
ctx->gbc);
413 y = bytestream2_get_le32(&
ctx->gbc);
414 x2 = bytestream2_get_le32(&
ctx->gbc);
415 y2 = bytestream2_get_le32(&
ctx->gbc);
416
417 if (x < 0 || y < 0 || x2 <= x || y2 <= y ||
418 x2 >
ctx->width || y2 >
ctx->height
419 ) {
421 "Invalid tile position (%d.%d %d.%d outside %dx%d).\n",
422 x, y, x2, y2,
ctx->width,
ctx->height);
424 }
427
429 if (!
ctx->tilebuffer)
431
433
434 if (tile_mode ==
MKTAG(
'G',
'E',
'P',
'J')) {
435 /* Decode JPEG tile and copy it in the reference frame */
439 }
else if (tile_mode ==
MKTAG(
' ',
'W',
'A',
'R')) {
440 /* Just copy the buffer to output */
442 ctx->refframe->linesize[0] * y,
443 ctx->refframe->linesize[0],
ctx->tilebuffer,
445 } else {
448 }
450 }
451
452 return 0;
453 }
454
456 {
458 int ret,
w,
h, init_refframe = !
ctx->refframe->data[0];
459
460 /* BITMAPINFOHEADER
461 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */
464
465 /* Store size, but wait for context reinit before updating avctx */
466 w = bytestream2_get_le32(&
ctx->gbc);
467 h = -bytestream2_get_le32(&
ctx->gbc);
468
469 if (bytestream2_get_le16(&
ctx->gbc) != 1 ||
// 1 plane
470 bytestream2_get_le16(&
ctx->gbc) != 24)
// BGR24
472
474
475 /* Update sizes */
482 init_refframe = 1;
483 }
484 ctx->refframe->width =
ctx->width =
w;
485 ctx->refframe->height =
ctx->height =
h;
486
487 /* Allocate the reference frame if not already done or on size change */
488 if (init_refframe) {
492 }
493
494 /* Decode all tiles in a frame */
496 }
497
499 {
502 int action = bytestream2_get_le32(&
ctx->gbc);
503
505
506 if (action == 2 || action == 3) {
507 /* Load cursor coordinates */
508 ctx->cursor_x = bytestream2_get_le32(&
ctx->gbc);
509 ctx->cursor_y = bytestream2_get_le32(&
ctx->gbc);
510
511 /* Load a full cursor sprite */
512 if (action == 3) {
514 /* Do not consider cursor errors fatal unless in explode mode */
517 }
518 } else {
520 }
521
522 return 0;
523 }
524
527 {
529 int ret, tag_header, keyframe = 0;
530 uLongf dlen;
531
532 /* Resize deflate buffer on resolution change */
534 int deflatelen = avctx->
width * avctx->
height * (3 + 1);
535 if (deflatelen !=
ctx->deflatelen) {
536 ctx->deflatelen =deflatelen;
541 }
542 }
543 }
544 dlen =
ctx->deflatelen;
545
546 /* Frames are deflated, need to inflate them first */
547 ret = uncompress(
ctx->deflatebuffer, &dlen, avpkt->
data, avpkt->
size);
551 }
552
554
555 /* Check for tag and for size info */
559 }
560
561 /* Read tag */
562 tag_header = bytestream2_get_le32(&
ctx->gbc);
563
564 if (tag_header ==
MKTAG(
'T',
'D',
'S',
'F')) {
565 int number_tiles;
569 }
570 /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
571 number_tiles = bytestream2_get_le32(&
ctx->gbc);
572
574 keyframe = bytestream2_get_le32(&
ctx->gbc) == 0x30;
575
579
580 /* Check if there is anything else we are able to parse */
582 tag_header = bytestream2_get_le32(&
ctx->gbc);
583 }
584
585 /* This tag can be after a TDSF block or on its own frame */
586 if (tag_header ==
MKTAG(
'D',
'T',
'S',
'M')) {
587 /* First 4 bytes here are the total size in bytes for this frame */
588 int tag_size = bytestream2_get_le32(&
ctx->gbc);
589
593 }
594
598 }
599
600 /* Get the output frame and copy the reference frame */
604
608
609 /* Paint the cursor on the output frame */
611
612 /* Frame is ready to be output */
613 if (keyframe) {
615 frame->key_frame = 1;
616 } else {
618 }
619 *got_frame = 1;
620
622 }
623
635 };