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)
134
135 /* Set the output pixel format on the reference frame */
137
138 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. */
146 {
148 const uint8_t *cursor =
ctx->cursor;
149 int x =
ctx->cursor_x -
ctx->cursor_hot_x;
150 int y =
ctx->cursor_y -
ctx->cursor_hot_y;
151 int w =
ctx->cursor_w;
152 int h =
ctx->cursor_h;
154
156 return;
157
158 if (x +
w >
ctx->width)
160 if (y +
h >
ctx->height)
162 if (x < 0) {
164 cursor += -x * 4;
165 } else {
166 dst += x * 3;
167 }
168 if (y < 0) {
170 cursor += -y *
ctx->cursor_stride;
171 } else {
173 }
175 return;
176
177 for (j = 0; j <
h; j++) {
178 for (
i = 0;
i <
w;
i++) {
179 uint8_t
alpha = cursor[
i * 4];
183 }
185 cursor +=
ctx->cursor_stride;
186 }
187 }
188
189 /* Load cursor data and store it in ABGR mode. */
191 {
193 int i, j, k,
ret, cursor_fmt;
194 uint8_t *dst;
195
196 ctx->cursor_hot_x = bytestream2_get_le16(&
ctx->gbc);
197 ctx->cursor_hot_y = bytestream2_get_le16(&
ctx->gbc);
198 ctx->cursor_w = bytestream2_get_le16(&
ctx->gbc);
199 ctx->cursor_h = bytestream2_get_le16(&
ctx->gbc);
200
202 cursor_fmt = bytestream2_get_le32(&
ctx->gbc);
203
206 "Invalid cursor position (%d.%d outside %dx%d).\n",
209 }
210 if (
ctx->cursor_w < 1 ||
ctx->cursor_w > 256 ||
211 ctx->cursor_h < 1 ||
ctx->cursor_h > 256) {
213 "Invalid cursor dimensions %dx%d.\n",
214 ctx->cursor_w,
ctx->cursor_h);
216 }
217 if (
ctx->cursor_hot_x >
ctx->cursor_w ||
218 ctx->cursor_hot_y >
ctx->cursor_h) {
220 ctx->cursor_hot_x,
ctx->cursor_hot_y);
223 }
224
229 }
230
232 /* here data is packed in BE */
233 switch (cursor_fmt) {
235 for (j = 0; j <
ctx->cursor_h; j++) {
236 for (
i = 0;
i <
ctx->cursor_w;
i += 32) {
237 uint32_t
bits = bytestream2_get_be32(&
ctx->gbc);
238 for (k = 0; k < 32; k++) {
239 dst[0] = !!(
bits & 0x80000000);
240 dst += 4;
242 }
243 }
244 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
245 }
246
248 for (j = 0; j <
ctx->cursor_h; j++) {
249 for (
i = 0;
i <
ctx->cursor_w;
i += 32) {
250 uint32_t
bits = bytestream2_get_be32(&
ctx->gbc);
251 for (k = 0; k < 32; k++) {
252 int mask_bit = !!(
bits & 0x80000000);
253 switch (dst[0] * 2 + mask_bit) {
254 case 0:
255 dst[0] = 0xFF;
256 dst[1] = 0x00;
257 dst[2] = 0x00;
258 dst[3] = 0x00;
259 break;
260 case 1:
261 dst[0] = 0xFF;
262 dst[1] = 0xFF;
263 dst[2] = 0xFF;
264 dst[3] = 0xFF;
265 break;
266 default:
267 dst[0] = 0x00;
268 dst[1] = 0x00;
269 dst[2] = 0x00;
270 dst[3] = 0x00;
271 }
272 dst += 4;
274 }
275 }
276 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
277 }
278 break;
281 /* Skip monochrome version of the cursor */
284 if (cursor_fmt & 8) { // RGBA -> ABGR
285 for (j = 0; j <
ctx->cursor_h; j++) {
286 for (
i = 0;
i <
ctx->cursor_w;
i++) {
287 int val = bytestream2_get_be32(&
ctx->gbc);
292 }
293 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
294 }
295 } else { // BGRA -> ABGR
296 for (j = 0; j <
ctx->cursor_h; j++) {
297 for (
i = 0;
i <
ctx->cursor_w;
i++) {
298 int val = bytestream2_get_be32(&
ctx->gbc);
303 }
304 dst +=
ctx->cursor_stride -
ctx->cursor_w * 4;
305 }
306 }
307 break;
308 default:
311 }
312
313 return 0;
314 }
315
316 /* Convert a single YUV pixel to RGB. */
318 {
322 }
323
324 /* Convert a YUV420 buffer to a RGB buffer. */
326 const uint8_t *srcy, int srcy_stride,
327 const uint8_t *srcu, const uint8_t *srcv,
329 {
332 for (col = 0; col <
width; col++)
334 srcu[col >> 1] - 128, srcv[col >> 1] - 128);
335
336 dst += dst_stride;
337 srcy += srcy_stride;
338 srcu += srcuv_stride * (
line & 1);
339 srcv += srcuv_stride * (
line & 1);
340 }
341 }
342
343 /* Invoke the MJPEG decoder to decode the tile. */
345 int x,
int y,
int w,
int h)
346 {
349
350 /* Prepare a packet and send to the MJPEG decoder */
352 ctx->jpkt->data =
ctx->tilebuffer;
353 ctx->jpkt->size = tile_size;
354
359 }
360
364 "JPEG decoding error (%d).\n",
ret);
365
366 /* Normally skip, error if explode */
369 else
370 return 0;
371 }
372
373 /* Let's paint onto the buffer */
374 tdsc_blit(
ctx->refframe->data[0] + x * 3 +
ctx->refframe->linesize[0] * y,
375 ctx->refframe->linesize[0],
376 ctx->jpgframe->data[0],
ctx->jpgframe->linesize[0],
377 ctx->jpgframe->data[1],
ctx->jpgframe->data[2],
378 ctx->jpgframe->linesize[1],
w,
h);
379
381
382 return 0;
383 }
384
385 /* Parse frame and either copy data or decode JPEG. */
387 {
390
391 /* Iterate over the number of tiles */
392 for (
i = 0;
i < number_tiles;
i++) {
393 int tile_size;
394 int tile_mode;
395 int x, y, x2, y2,
w,
h;
397
399 bytestream2_get_le32(&
ctx->gbc) !=
MKTAG(
'T',
'D',
'S',
'B') ||
403 }
404
405 tile_size = bytestream2_get_le32(&
ctx->gbc);
408
409 tile_mode = bytestream2_get_le32(&
ctx->gbc);
411 x = bytestream2_get_le32(&
ctx->gbc);
412 y = bytestream2_get_le32(&
ctx->gbc);
413 x2 = bytestream2_get_le32(&
ctx->gbc);
414 y2 = bytestream2_get_le32(&
ctx->gbc);
415
416 if (x < 0 || y < 0 || x2 <= x || y2 <= y ||
417 x2 >
ctx->width || y2 >
ctx->height
418 ) {
420 "Invalid tile position (%d.%d %d.%d outside %dx%d).\n",
421 x, y, x2, y2,
ctx->width,
ctx->height);
423 }
426
428 if (!
ctx->tilebuffer)
430
432
433 if (tile_mode ==
MKTAG(
'G',
'E',
'P',
'J')) {
434 /* Decode JPEG tile and copy it in the reference frame */
438 }
else if (tile_mode ==
MKTAG(
' ',
'W',
'A',
'R')) {
439 /* Just copy the buffer to output */
441 ctx->refframe->linesize[0] * y,
442 ctx->refframe->linesize[0],
ctx->tilebuffer,
444 } else {
447 }
449 }
450
451 return 0;
452 }
453
455 {
457 int ret,
w,
h, init_refframe = !
ctx->refframe->data[0];
458
459 /* BITMAPINFOHEADER
460 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */
463
464 /* Store size, but wait for context reinit before updating avctx */
465 w = bytestream2_get_le32(&
ctx->gbc);
466 h = -bytestream2_get_le32(&
ctx->gbc);
467
468 if (bytestream2_get_le16(&
ctx->gbc) != 1 ||
// 1 plane
469 bytestream2_get_le16(&
ctx->gbc) != 24)
// BGR24
471
473
474 /* Update sizes */
481 init_refframe = 1;
482 }
483 ctx->refframe->width =
ctx->width =
w;
484 ctx->refframe->height =
ctx->height =
h;
485
486 /* Allocate the reference frame if not already done or on size change */
487 if (init_refframe) {
491 }
492
493 /* Decode all tiles in a frame */
495 }
496
498 {
501 int action = bytestream2_get_le32(&
ctx->gbc);
502
504
505 if (action == 2 || action == 3) {
506 /* Load cursor coordinates */
507 ctx->cursor_x = bytestream2_get_le32(&
ctx->gbc);
508 ctx->cursor_y = bytestream2_get_le32(&
ctx->gbc);
509
510 /* Load a full cursor sprite */
511 if (action == 3) {
513 /* Do not consider cursor errors fatal unless in explode mode */
516 }
517 } else {
519 }
520
521 return 0;
522 }
523
526 {
528 int ret, tag_header, keyframe = 0;
529 uLongf dlen;
530
531 /* Resize deflate buffer on resolution change */
533 int deflatelen = avctx->
width * avctx->
height * (3 + 1);
534 if (deflatelen !=
ctx->deflatelen) {
535 ctx->deflatelen =deflatelen;
540 }
541 }
542 }
543 dlen =
ctx->deflatelen;
544
545 /* Frames are deflated, need to inflate them first */
546 ret = uncompress(
ctx->deflatebuffer, &dlen, avpkt->
data, avpkt->
size);
550 }
551
553
554 /* Check for tag and for size info */
558 }
559
560 /* Read tag */
561 tag_header = bytestream2_get_le32(&
ctx->gbc);
562
563 if (tag_header ==
MKTAG(
'T',
'D',
'S',
'F')) {
564 int number_tiles;
568 }
569 /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
570 number_tiles = bytestream2_get_le32(&
ctx->gbc);
571
573 keyframe = bytestream2_get_le32(&
ctx->gbc) == 0x30;
574
578
579 /* Check if there is anything else we are able to parse */
581 tag_header = bytestream2_get_le32(&
ctx->gbc);
582 }
583
584 /* This tag can be after a TDSF block or on its own frame */
585 if (tag_header ==
MKTAG(
'D',
'T',
'S',
'M')) {
586 /* First 4 bytes here are the total size in bytes for this frame */
587 int tag_size = bytestream2_get_le32(&
ctx->gbc);
588
592 }
593
597 }
598
599 /* Get the output frame and copy the reference frame */
603
607
608 /* Paint the cursor on the output frame */
610
611 /* Frame is ready to be output */
612 if (keyframe) {
615 } else {
617 }
618 *got_frame = 1;
619
621 }
622
634 };