3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
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.
39 #include "libavutil/imgutils.h"
42 #include "bytestream.h"
45 #define BITMAPINFOHEADER_SIZE 0x28
46 #define TDSF_HEADER_SIZE 0x56
47 #define TDSB_HEADER_SIZE 0x08
49 typedef struct TDSCContext {
50 AVCodecContext *jpeg_avctx; // wrapper context for MJPEG
55 AVFrame *refframe; // full decoded frame (without cursor)
56 AVFrame *jpgframe; // decoded JPEG tile
57 uint8_t *tilebuffer; // buffer containing tile data
60 uint8_t *deflatebuffer;
63 /* All that is cursor */
66 int cursor_w, cursor_h, cursor_x, cursor_y;
67 int cursor_hot_x, cursor_hot_y;
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,
77 static av_cold int tdsc_close(AVCodecContext *avctx)
79 TDSCContext *ctx = avctx->priv_data;
81 av_frame_free(&ctx->refframe);
82 av_frame_free(&ctx->jpgframe);
83 av_freep(&ctx->deflatebuffer);
84 av_freep(&ctx->tilebuffer);
85 av_freep(&ctx->cursor);
86 avcodec_free_context(&ctx->jpeg_avctx);
91 static av_cold int tdsc_init(AVCodecContext *avctx)
93 TDSCContext *ctx = avctx->priv_data;
97 avctx->pix_fmt = AV_PIX_FMT_BGR24;
99 /* These needs to be set to estimate buffer and frame size */
100 if (!(avctx->width && avctx->height)) {
101 av_log(avctx, AV_LOG_ERROR, "Video size not set.\n");
102 return AVERROR_INVALIDDATA;
105 /* This value should be large enough for a RAW-only frame plus headers */
106 ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
107 ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
111 /* Allocate reference and JPEG frame */
112 ctx->refframe = av_frame_alloc();
113 ctx->jpgframe = av_frame_alloc();
114 if (!ctx->refframe || !ctx->jpgframe)
115 return AVERROR(ENOMEM);
117 /* Prepare everything needed for JPEG decoding */
118 codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
121 ctx->jpeg_avctx = avcodec_alloc_context3(codec);
122 if (!ctx->jpeg_avctx)
123 return AVERROR(ENOMEM);
124 ctx->jpeg_avctx->flags = avctx->flags;
125 ctx->jpeg_avctx->flags2 = avctx->flags2;
126 ctx->jpeg_avctx->dct_algo = avctx->dct_algo;
127 ctx->jpeg_avctx->idct_algo = avctx->idct_algo;
128 ret = ff_codec_open2_recursive(ctx->jpeg_avctx, codec, NULL);
132 /* Set the output pixel format on the reference frame */
133 ctx->refframe->format = avctx->pix_fmt;
138 #define APPLY_ALPHA(src, new, alpha) \
139 src = (src * (256 - alpha) + new * alpha) >> 8
141 /* Paint a region over a buffer, without drawing out of its bounds. */
142 static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
144 TDSCContext *ctx = avctx->priv_data;
145 const uint8_t *cursor = ctx->cursor;
146 int x = ctx->cursor_x - ctx->cursor_hot_x;
147 int y = ctx->cursor_y - ctx->cursor_hot_y;
148 int w = ctx->cursor_w;
149 int h = ctx->cursor_h;
155 if (x + w > ctx->width)
157 if (y + h > ctx->height)
167 cursor += -y * ctx->cursor_stride;
174 for (j = 0; j < h; j++) {
175 for (i = 0; i < w; i++) {
176 uint8_t alpha = cursor[i * 4];
177 APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
178 APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
179 APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
182 cursor += ctx->cursor_stride;
186 /* Load cursor data and store it in ABGR mode. */
187 static int tdsc_load_cursor(AVCodecContext *avctx)
189 TDSCContext *ctx = avctx->priv_data;
190 int i, j, k, ret, bits, cursor_fmt;
193 ctx->cursor_hot_x = bytestream2_get_le16(&ctx->gbc);
194 ctx->cursor_hot_y = bytestream2_get_le16(&ctx->gbc);
195 ctx->cursor_w = bytestream2_get_le16(&ctx->gbc);
196 ctx->cursor_h = bytestream2_get_le16(&ctx->gbc);
198 ctx->cursor_stride = FFALIGN(ctx->cursor_w, 32) * 4;
199 cursor_fmt = bytestream2_get_le32(&ctx->gbc);
201 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;
207 if (ctx->cursor_w < 1 || ctx->cursor_w > 256 ||
208 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;
214 if (ctx->cursor_hot_x > ctx->cursor_w ||
215 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);
222 ret = av_reallocp(&ctx->cursor, ctx->cursor_stride * ctx->cursor_h);
224 av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer.\n");
229 /* here data is packed in BE */
230 switch (cursor_fmt) {
232 for (j = 0; j < ctx->cursor_h; j++) {
233 for (i = 0; i < ctx->cursor_w; i += 32) {
234 bits = bytestream2_get_be32(&ctx->gbc);
235 for (k = 0; k < 32; k++) {
236 dst[0] = !!(bits & 0x80000000);
241 dst += ctx->cursor_stride - ctx->cursor_w * 4;
245 for (j = 0; j < ctx->cursor_h; j++) {
246 for (i = 0; i < ctx->cursor_w; i += 32) {
247 bits = bytestream2_get_be32(&ctx->gbc);
248 for (k = 0; k < 32; k++) {
249 int mask_bit = !!(bits & 0x80000000);
250 switch (dst[0] * 2 + mask_bit) {
273 dst += ctx->cursor_stride - ctx->cursor_w * 4;
278 /* Skip monochrome version of the cursor */
279 bytestream2_skip(&ctx->gbc,
280 ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 3));
281 if (cursor_fmt & 8) { // RGBA -> ABGR
282 for (j = 0; j < ctx->cursor_h; j++) {
283 for (i = 0; i < ctx->cursor_w; i++) {
284 int val = bytestream2_get_be32(&ctx->gbc);
290 dst += ctx->cursor_stride - ctx->cursor_w * 4;
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);
301 dst += ctx->cursor_stride - ctx->cursor_w * 4;
306 avpriv_request_sample(avctx, "Cursor format %08x", cursor_fmt);
307 return AVERROR_PATCHWELCOME;
313 /* Convert a single YUV pixel to RGB. */
314 static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
316 out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
317 out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
318 out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
321 /* Convert a YUV420 buffer to a RGB buffer. */
322 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)
328 for (line = 0; line < height; line++) {
329 for (col = 0; col < width; col++)
330 tdsc_yuv2rgb(dst + col * 3, srcy[col],
331 srcu[col >> 1] - 128, srcv[col >> 1] - 128);
335 srcu += srcuv_stride * (line & 1);
336 srcv += srcuv_stride * (line & 1);
340 /* Invoke the MJPEG decoder to decode the tile. */
341 static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size,
342 int x, int y, int w, int h)
344 TDSCContext *ctx = avctx->priv_data;
349 /* Prepare a packet and send to the MJPEG decoder */
350 av_init_packet(&jpkt);
351 jpkt.data = ctx->tilebuffer;
352 jpkt.size = tile_size;
354 ret = avcodec_decode_video2(ctx->jpeg_avctx, ctx->jpgframe,
356 if (ret < 0 || !got_frame || ctx->jpgframe->format != AV_PIX_FMT_YUVJ420P) {
357 av_log(avctx, AV_LOG_ERROR,
358 "JPEG decoding error (%d) for (%d) frame.\n",
361 /* Normally skip, error if explode */
362 if (avctx->err_recognition & AV_EF_EXPLODE)
363 return AVERROR_INVALIDDATA;
368 /* Let's paint ont the buffer */
369 tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * y,
370 ctx->refframe->linesize[0],
371 ctx->jpgframe->data[0], ctx->jpgframe->linesize[0],
372 ctx->jpgframe->data[1], ctx->jpgframe->data[2],
373 ctx->jpgframe->linesize[1], w, h);
375 av_frame_unref(ctx->jpgframe);
380 /* Parse frame and either copy data or decode JPEG. */
381 static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
383 TDSCContext *ctx = avctx->priv_data;
386 /* Iterate over the number of tiles */
387 for (i = 0; i < number_tiles; i++) {
393 if (bytestream2_get_bytes_left(&ctx->gbc) < 4 ||
394 bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') ||
395 bytestream2_get_bytes_left(&ctx->gbc) < TDSB_HEADER_SIZE - 4) {
396 av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n");
397 return AVERROR_INVALIDDATA;
400 tile_size = bytestream2_get_le32(&ctx->gbc);
401 if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size)
402 return AVERROR_INVALIDDATA;
404 tile_mode = bytestream2_get_le32(&ctx->gbc);
405 bytestream2_skip(&ctx->gbc, 4); // unknown
406 x = bytestream2_get_le32(&ctx->gbc);
407 y = bytestream2_get_le32(&ctx->gbc);
408 w = bytestream2_get_le32(&ctx->gbc) - x;
409 h = bytestream2_get_le32(&ctx->gbc) - y;
411 if (x >= ctx->width || y >= ctx->height) {
412 av_log(avctx, AV_LOG_ERROR,
413 "Invalid tile position (%d.%d outside %dx%d).\n",
414 x, y, ctx->width, ctx->height);
415 return AVERROR_INVALIDDATA;
417 if (x + w > ctx->width || y + h > ctx->height) {
418 av_log(avctx, AV_LOG_ERROR,
419 "Invalid tile size %dx%d\n", w, h);
420 return AVERROR_INVALIDDATA;
423 ret = av_reallocp(&ctx->tilebuffer, tile_size);
424 if (!ctx->tilebuffer)
427 bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size);
429 if (tile_mode == MKTAG('G','E','P','J')) {
430 /* Decode JPEG tile and copy it in the reference frame */
431 ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h);
434 } else if (tile_mode == MKTAG(' ','W','A','R')) {
435 /* Just copy the buffer to output */
436 av_image_copy_plane(ctx->refframe->data[0] + x * 3 +
437 ctx->refframe->linesize[0] * y,
438 ctx->refframe->linesize[0], ctx->tilebuffer,
441 av_log(avctx, AV_LOG_ERROR, "Unknown tile type %08x.\n", tile_mode);
442 return AVERROR_INVALIDDATA;
444 av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, y);
450 static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
452 TDSCContext *ctx = avctx->priv_data;
453 int ret, w, h, init_refframe = !ctx->refframe->data[0];
456 * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */
457 if (bytestream2_get_le32(&ctx->gbc) != BITMAPINFOHEADER_SIZE)
458 return AVERROR_INVALIDDATA;
460 /* Store size, but wait for context reinit before updating avctx */
461 w = bytestream2_get_le32(&ctx->gbc);
462 h = -bytestream2_get_le32(&ctx->gbc);
464 if (bytestream2_get_le16(&ctx->gbc) != 1 || // 1 plane
465 bytestream2_get_le16(&ctx->gbc) != 24) // BGR24
466 return AVERROR_INVALIDDATA;
468 bytestream2_skip(&ctx->gbc, 24); // unused fields
471 if (avctx->width != w || avctx->height != h) {
472 av_log(avctx, AV_LOG_DEBUG, "Size update %dx%d -> %d%d.\n",
473 avctx->width, avctx->height, ctx->width, ctx->height);
474 ret = ff_set_dimensions(avctx, w, h);
479 ctx->refframe->width = ctx->width = w;
480 ctx->refframe->height = ctx->height = h;
482 /* Allocate the reference frame if not already done or on size change */
484 ret = av_frame_get_buffer(ctx->refframe, 32);
489 /* Decode all tiles in a frame */
490 return tdsc_decode_tiles(avctx, number_tiles);
493 static int tdsc_parse_dtsm(AVCodecContext *avctx)
495 TDSCContext *ctx = avctx->priv_data;
497 int action = bytestream2_get_le32(&ctx->gbc);
499 bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe?
501 if (action == 2 || action == 3) {
502 /* Load cursor coordinates */
503 ctx->cursor_x = bytestream2_get_le32(&ctx->gbc);
504 ctx->cursor_y = bytestream2_get_le32(&ctx->gbc);
506 /* Load a full cursor sprite */
508 ret = tdsc_load_cursor(avctx);
509 /* Do not consider cursor errors fatal unless in explode mode */
510 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
514 avpriv_request_sample(avctx, "Cursor action %d", action);
520 static int tdsc_decode_frame(AVCodecContext *avctx, void *data,
521 int *got_frame, AVPacket *avpkt)
523 TDSCContext *ctx = avctx->priv_data;
524 AVFrame *frame = data;
525 int ret, tag_header, keyframe = 0;
528 /* Resize deflate buffer on resolution change */
529 if (ctx->width != avctx->width || ctx->height != avctx->height) {
530 ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
531 ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
535 dlen = ctx->deflatelen;
537 /* Frames are deflated, need to inflate them first */
538 ret = uncompress(ctx->deflatebuffer, &dlen, avpkt->data, avpkt->size);
540 av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
541 return AVERROR_UNKNOWN;
544 bytestream2_init(&ctx->gbc, ctx->deflatebuffer, dlen);
546 /* Check for tag and for size info */
547 if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
548 av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
549 return AVERROR_INVALIDDATA;
553 tag_header = bytestream2_get_le32(&ctx->gbc);
555 if (tag_header == MKTAG('T','D','S','F')) {
557 if (bytestream2_get_bytes_left(&ctx->gbc) < TDSF_HEADER_SIZE) {
558 av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n");
559 return AVERROR_INVALIDDATA;
561 /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
562 number_tiles = bytestream2_get_le32(&ctx->gbc);
564 bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe?
565 keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30;
567 ret = tdsc_parse_tdsf(avctx, number_tiles);
571 /* Check if there is anything else we are able to parse */
572 if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4)
573 tag_header = bytestream2_get_le32(&ctx->gbc);
576 /* This tag can be after a TDSF block or on its own frame */
577 if (tag_header == MKTAG('D','T','S','M')) {
578 /* First 4 bytes here are the total size in bytes for this frame */
579 int tag_size = bytestream2_get_le32(&ctx->gbc);
581 if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) {
582 av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n");
583 return AVERROR_INVALIDDATA;
586 ret = tdsc_parse_dtsm(avctx);
591 /* Get the output frame and copy the reference frame */
592 ret = ff_get_buffer(avctx, frame, 0);
596 ret = av_frame_copy(frame, ctx->refframe);
600 /* Paint the cursor on the output frame */
601 tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]);
603 /* Frame is ready to be output */
605 frame->pict_type = AV_PICTURE_TYPE_I;
606 frame->key_frame = 1;
608 frame->pict_type = AV_PICTURE_TYPE_P;
615 AVCodec ff_tdsc_decoder = {
617 .long_name = NULL_IF_CONFIG_SMALL("TDSC"),
618 .type = AVMEDIA_TYPE_VIDEO,
619 .id = AV_CODEC_ID_TDSC,
621 .decode = tdsc_decode_frame,
623 .priv_data_size = sizeof(TDSCContext),
624 .capabilities = AV_CODEC_CAP_DR1,
625 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
626 FF_CODEC_CAP_INIT_CLEANUP,