FFmpeg  4.4.9
tdsc.c
Go to the documentation of this file.
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/imgutils.h"
41 
42 #include "avcodec.h"
43 #include "bytestream.h"
44 #include "internal.h"
45 
46 #define BITMAPINFOHEADER_SIZE 0x28
47 #define TDSF_HEADER_SIZE 0x56
48 #define TDSB_HEADER_SIZE 0x08
49 
50 typedef struct TDSCContext {
51  AVCodecContext *jpeg_avctx; // wrapper context for MJPEG
52 
53  int width, height;
55 
56  AVFrame *refframe; // full decoded frame (without cursor)
57  AVPacket *jpkt; // encoded JPEG tile
58  AVFrame *jpgframe; // decoded JPEG tile
59  uint8_t *tilebuffer; // buffer containing tile data
60 
61  /* zlib interaction */
63  uLongf deflatelen;
64 
65  /* All that is cursor */
70 } TDSCContext;
71 
72 /* 1 byte bits, 1 byte planes, 2 bytes format (probably) */
74  CUR_FMT_MONO = 0x01010004,
75  CUR_FMT_BGRA = 0x20010004,
76  CUR_FMT_RGBA = 0x20010008,
77 };
78 
79 static av_cold int tdsc_close(AVCodecContext *avctx)
80 {
81  TDSCContext *ctx = avctx->priv_data;
82 
83  av_frame_free(&ctx->refframe);
84  av_frame_free(&ctx->jpgframe);
85  av_packet_free(&ctx->jpkt);
86  av_freep(&ctx->deflatebuffer);
87  av_freep(&ctx->tilebuffer);
88  av_freep(&ctx->cursor);
89  avcodec_free_context(&ctx->jpeg_avctx);
90 
91  return 0;
92 }
93 
94 static av_cold int tdsc_init(AVCodecContext *avctx)
95 {
96  TDSCContext *ctx = avctx->priv_data;
97  const AVCodec *codec;
98  int ret;
99 
100  avctx->pix_fmt = AV_PIX_FMT_BGR24;
101 
102  /* These needs to be set to estimate buffer and frame size */
103  if (!(avctx->width && avctx->height)) {
104  av_log(avctx, AV_LOG_ERROR, "Video size not set.\n");
105  return AVERROR_INVALIDDATA;
106  }
107 
108  /* This value should be large enough for a RAW-only frame plus headers */
109  ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
110  ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
111  if (ret < 0)
112  return ret;
113 
114  /* Allocate reference and JPEG frame */
115  ctx->refframe = av_frame_alloc();
116  ctx->jpgframe = av_frame_alloc();
117  ctx->jpkt = av_packet_alloc();
118  if (!ctx->refframe || !ctx->jpgframe || !ctx->jpkt)
119  return AVERROR(ENOMEM);
120 
121  /* Prepare everything needed for JPEG decoding */
123  if (!codec)
124  return AVERROR_BUG;
125  ctx->jpeg_avctx = avcodec_alloc_context3(codec);
126  if (!ctx->jpeg_avctx)
127  return AVERROR(ENOMEM);
128  ctx->jpeg_avctx->flags = avctx->flags;
129  ctx->jpeg_avctx->flags2 = avctx->flags2;
130  ctx->jpeg_avctx->dct_algo = avctx->dct_algo;
131  ctx->jpeg_avctx->idct_algo = avctx->idct_algo;
132  ret = avcodec_open2(ctx->jpeg_avctx, codec, NULL);
133  if (ret < 0)
134  return ret;
135 
136  /* Set the output pixel format on the reference frame */
137  ctx->refframe->format = avctx->pix_fmt;
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. */
146 static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
147 {
148  TDSCContext *ctx = avctx->priv_data;
149  const uint8_t *cursor = ctx->cursor;
150  int x, y;
151  int w = ctx->cursor_w;
152  int h = ctx->cursor_h;
153  int i, j;
154 
155  if (!ctx->cursor)
156  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  if ((unsigned)ctx->cursor_x >= ctx->width ||
162  (unsigned)ctx->cursor_y >= ctx->height)
163  return;
164 
165  x = ctx->cursor_x - ctx->cursor_hot_x;
166  y = ctx->cursor_y - ctx->cursor_hot_y;
167 
168  if (x + w > ctx->width)
169  w = ctx->width - x;
170  if (y + h > ctx->height)
171  h = ctx->height - y;
172  if (x < 0) {
173  w += x;
174  cursor += -x * 4;
175  } else {
176  dst += x * 3;
177  }
178  if (y < 0) {
179  h += y;
180  cursor += -y * ctx->cursor_stride;
181  } else {
182  dst += y * stride;
183  }
184  if (w < 0 || h < 0)
185  return;
186 
187  for (j = 0; j < h; j++) {
188  for (i = 0; i < w; i++) {
189  uint8_t alpha = cursor[i * 4];
190  APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
191  APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
192  APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
193  }
194  dst += stride;
195  cursor += ctx->cursor_stride;
196  }
197 }
198 
199 /* Load cursor data and store it in ABGR mode. */
201 {
202  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  hot_x = bytestream2_get_le16(&ctx->gbc);
207  hot_y = bytestream2_get_le16(&ctx->gbc);
208  w = bytestream2_get_le16(&ctx->gbc);
209  h = bytestream2_get_le16(&ctx->gbc);
210 
211  cursor_fmt = bytestream2_get_le32(&ctx->gbc);
212 
213  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  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  ctx->cursor_w = w;
223  ctx->cursor_h = h;
224  ctx->cursor_stride = FFALIGN(w, 32) * 4;
225  ctx->cursor_hot_x = hot_x;
226  ctx->cursor_hot_y = hot_y;
227  if (ctx->cursor_hot_x > ctx->cursor_w ||
228  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  ctx->cursor_hot_x = FFMIN(ctx->cursor_hot_x, ctx->cursor_w - 1);
232  ctx->cursor_hot_y = FFMIN(ctx->cursor_hot_y, ctx->cursor_h - 1);
233  }
234 
235  ret = av_reallocp(&ctx->cursor, ctx->cursor_stride * ctx->cursor_h);
236  if (ret < 0) {
237  av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer.\n");
238  return ret;
239  }
240 
241  dst = ctx->cursor;
242  /* here data is packed in BE */
243  switch (cursor_fmt) {
244  case CUR_FMT_MONO:
245  for (j = 0; j < ctx->cursor_h; j++) {
246  for (i = 0; i < ctx->cursor_w; i += 32) {
247  uint32_t bits = bytestream2_get_be32(&ctx->gbc);
248  for (k = 0; k < 32; k++) {
249  dst[0] = !!(bits & 0x80000000);
250  dst += 4;
251  bits <<= 1;
252  }
253  }
254  }
255 
256  dst = ctx->cursor;
257  for (j = 0; j < ctx->cursor_h; j++) {
258  for (i = 0; i < ctx->cursor_w; i += 32) {
259  uint32_t bits = bytestream2_get_be32(&ctx->gbc);
260  for (k = 0; k < 32; k++) {
261  int mask_bit = !!(bits & 0x80000000);
262  switch (dst[0] * 2 + mask_bit) {
263  case 0:
264  dst[0] = 0xFF;
265  dst[1] = 0x00;
266  dst[2] = 0x00;
267  dst[3] = 0x00;
268  break;
269  case 1:
270  dst[0] = 0xFF;
271  dst[1] = 0xFF;
272  dst[2] = 0xFF;
273  dst[3] = 0xFF;
274  break;
275  default:
276  dst[0] = 0x00;
277  dst[1] = 0x00;
278  dst[2] = 0x00;
279  dst[3] = 0x00;
280  }
281  dst += 4;
282  bits <<= 1;
283  }
284  }
285  }
286  break;
287  case CUR_FMT_BGRA:
288  case CUR_FMT_RGBA:
289  /* Skip monochrome version of the cursor */
290  bytestream2_skip(&ctx->gbc,
291  ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 3));
292  if (cursor_fmt & 8) { // RGBA -> 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 >> 24;
297  *dst++ = val >> 16;
298  *dst++ = val >> 8;
299  *dst++ = val >> 0;
300  }
301  dst += ctx->cursor_stride - ctx->cursor_w * 4;
302  }
303  } else { // BGRA -> ABGR
304  for (j = 0; j < ctx->cursor_h; j++) {
305  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  break;
316  }
317 
318  return 0;
319 }
320 
321 /* Convert a single YUV pixel to RGB. */
322 static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
323 {
324  out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
325  out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
326  out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
327 }
328 
329 /* Convert a YUV420 buffer to a RGB buffer. */
330 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  for (line = 0; line < height; line++) {
337  for (col = 0; col < width; col++)
338  tdsc_yuv2rgb(dst + col * 3, srcy[col],
339  srcu[col >> 1] - 128, srcv[col >> 1] - 128);
340 
341  dst += dst_stride;
342  srcy += srcy_stride;
343  srcu += srcuv_stride * (line & 1);
344  srcv += srcuv_stride * (line & 1);
345  }
346 }
347 
348 /* Invoke the MJPEG decoder to decode the tile. */
349 static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size,
350  int x, int y, int w, int h)
351 {
352  TDSCContext *ctx = avctx->priv_data;
353  int ret;
354 
355  /* Prepare a packet and send to the MJPEG decoder */
356  av_packet_unref(ctx->jpkt);
357  ctx->jpkt->data = ctx->tilebuffer;
358  ctx->jpkt->size = tile_size;
359 
360  ret = avcodec_send_packet(ctx->jpeg_avctx, ctx->jpkt);
361  if (ret < 0) {
362  av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
363  return ret;
364  }
365 
366  ret = avcodec_receive_frame(ctx->jpeg_avctx, ctx->jpgframe);
367  if (ret < 0 || ctx->jpgframe->format != AV_PIX_FMT_YUVJ420P ||
368  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  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  tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * y,
381  ctx->refframe->linesize[0],
382  ctx->jpgframe->data[0], ctx->jpgframe->linesize[0],
383  ctx->jpgframe->data[1], ctx->jpgframe->data[2],
384  ctx->jpgframe->linesize[1], w, h);
385 
386  av_frame_unref(ctx->jpgframe);
387 
388  return 0;
389 }
390 
391 /* Parse frame and either copy data or decode JPEG. */
392 static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
393 {
394  TDSCContext *ctx = avctx->priv_data;
395  int i;
396 
397  /* Iterate over the number of tiles */
398  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  if (bytestream2_get_bytes_left(&ctx->gbc) < 4 ||
405  bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') ||
407  av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n");
408  return AVERROR_INVALIDDATA;
409  }
410 
411  tile_size = bytestream2_get_le32(&ctx->gbc);
412  if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size + 24LL)
413  return AVERROR_INVALIDDATA;
414 
415  tile_mode = bytestream2_get_le32(&ctx->gbc);
416  bytestream2_skip(&ctx->gbc, 4); // unknown
417  x = bytestream2_get_le32(&ctx->gbc);
418  y = bytestream2_get_le32(&ctx->gbc);
419  x2 = bytestream2_get_le32(&ctx->gbc);
420  y2 = bytestream2_get_le32(&ctx->gbc);
421 
422  if (x < 0 || y < 0 || x2 <= x || y2 <= y ||
423  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  w = x2 - x;
431  h = y2 - y;
432 
433  ret = av_reallocp(&ctx->tilebuffer, tile_size);
434  if (!ctx->tilebuffer)
435  return ret;
436 
437  bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size);
438 
439  if (tile_mode == MKTAG('G','E','P','J')) {
440  /* Decode JPEG tile and copy it in the reference frame */
441  ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h);
442  if (ret < 0)
443  return ret;
444  } else if (tile_mode == MKTAG(' ','W','A','R')) {
445  if (3LL * w * h > tile_size)
446  return AVERROR_INVALIDDATA;
447 
448  /* Just copy the buffer to output */
449  av_image_copy_plane(ctx->refframe->data[0] + x * 3 +
450  ctx->refframe->linesize[0] * y,
451  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  av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, y);
458  }
459 
460  return 0;
461 }
462 
463 static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
464 {
465  TDSCContext *ctx = avctx->priv_data;
466  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  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  w = bytestream2_get_le32(&ctx->gbc);
475  h = -bytestream2_get_le32(&ctx->gbc);
476 
477  if (bytestream2_get_le16(&ctx->gbc) != 1 || // 1 plane
478  bytestream2_get_le16(&ctx->gbc) != 24) // BGR24
479  return AVERROR_INVALIDDATA;
480 
481  bytestream2_skip(&ctx->gbc, 24); // unused fields
482 
483  /* Update sizes */
484  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  if (ret < 0)
489  return ret;
490  init_refframe = 1;
491  }
492  ctx->width = w;
493  ctx->height = h;
494 
495  /* Allocate the reference frame if not already done or on size change */
496  if (init_refframe) {
497  av_frame_unref(ctx->refframe);
498  ctx->refframe->format = avctx->pix_fmt;
499  ctx->refframe->width = w;
500  ctx->refframe->height = h;
501  ret = av_frame_get_buffer(ctx->refframe, 0);
502  if (ret < 0)
503  return ret;
504  memset(ctx->refframe->data[0], 0,
505  (size_t)ctx->refframe->linesize[0] * ctx->refframe->height);
506  }
507 
508  /* Decode all tiles in a frame */
509  return tdsc_decode_tiles(avctx, number_tiles);
510 }
511 
512 static int tdsc_parse_dtsm(AVCodecContext *avctx)
513 {
514  TDSCContext *ctx = avctx->priv_data;
515  int ret;
516  int action = bytestream2_get_le32(&ctx->gbc);
517 
518  bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe?
519 
520  if (action == 2 || action == 3) {
521  /* Load cursor coordinates */
522  ctx->cursor_x = bytestream2_get_le32(&ctx->gbc);
523  ctx->cursor_y = bytestream2_get_le32(&ctx->gbc);
524 
525  /* Load a full cursor sprite */
526  if (action == 3) {
527  ret = tdsc_load_cursor(avctx);
528  /* Do not consider cursor errors fatal unless in explode mode */
529  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  return 0;
537 }
538 
539 static int tdsc_decode_frame(AVCodecContext *avctx, void *data,
540  int *got_frame, AVPacket *avpkt)
541 {
542  TDSCContext *ctx = avctx->priv_data;
543  AVFrame *frame = data;
544  int ret, tag_header, keyframe = 0;
545  uLongf dlen;
546 
547  /* Resize deflate buffer on resolution change */
548  if (ctx->width != avctx->width || ctx->height != avctx->height) {
549  int deflatelen = avctx->width * avctx->height * (3 + 1);
550  if (deflatelen != ctx->deflatelen) {
551  ctx->deflatelen =deflatelen;
552  ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
553  if (ret < 0) {
554  ctx->deflatelen = 0;
555  return ret;
556  }
557  }
558  }
559  dlen = ctx->deflatelen;
560 
561  /* Frames are deflated, need to inflate them first */
562  ret = uncompress(ctx->deflatebuffer, &dlen, avpkt->data, avpkt->size);
563  if (ret) {
564  av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
565  return AVERROR_UNKNOWN;
566  }
567 
568  bytestream2_init(&ctx->gbc, ctx->deflatebuffer, dlen);
569 
570  /* Check for tag and for size info */
571  if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
572  av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
573  return AVERROR_INVALIDDATA;
574  }
575 
576  /* Read tag */
577  tag_header = bytestream2_get_le32(&ctx->gbc);
578 
579  if (tag_header == MKTAG('T','D','S','F')) {
580  int number_tiles;
582  av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n");
583  return AVERROR_INVALIDDATA;
584  }
585  /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
586  number_tiles = bytestream2_get_le32(&ctx->gbc);
587 
588  bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe?
589  keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30;
590 
591  ret = tdsc_parse_tdsf(avctx, number_tiles);
592  if (ret < 0)
593  return ret;
594 
595  /* Check if there is anything else we are able to parse */
596  if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4)
597  tag_header = bytestream2_get_le32(&ctx->gbc);
598  }
599 
600  /* This tag can be after a TDSF block or on its own frame */
601  if (tag_header == MKTAG('D','T','S','M')) {
602  /* First 4 bytes here are the total size in bytes for this frame */
603  int tag_size = bytestream2_get_le32(&ctx->gbc);
604 
605  if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) {
606  av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n");
607  return AVERROR_INVALIDDATA;
608  }
609 
610  ret = tdsc_parse_dtsm(avctx);
611  if (ret < 0)
612  return ret;
613  }
614 
615  /* Get the output frame and copy the reference frame */
616  ret = ff_get_buffer(avctx, frame, 0);
617  if (ret < 0)
618  return ret;
619 
620  ret = av_frame_copy(frame, ctx->refframe);
621  if (ret < 0)
622  return ret;
623 
624  /* Paint the cursor on the output frame */
625  tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]);
626 
627  /* Frame is ready to be output */
628  if (keyframe) {
630  frame->key_frame = 1;
631  } else {
633  }
634  *got_frame = 1;
635 
636  return avpkt->size;
637 }
638 
640  .name = "tdsc",
641  .long_name = NULL_IF_CONFIG_SMALL("TDSC"),
642  .type = AVMEDIA_TYPE_VIDEO,
643  .id = AV_CODEC_ID_TDSC,
644  .init = tdsc_init,
645  .decode = tdsc_decode_frame,
646  .close = tdsc_close,
647  .priv_data_size = sizeof(TDSCContext),
648  .capabilities = AV_CODEC_CAP_DR1,
649  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
651 };
static double val(void *priv, double ch)
Definition: aeval.c:76
#define U(x)
Definition: vp56_arith.h:37
#define av_always_inline
Definition: attributes.h:45
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1660
#define V
Definition: avdct.c:30
#define Y
Definition: boxblur.h:38
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
#define FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define av_clip_uint8
Definition: common.h:128
#define NULL
Definition: coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1911
static AVFrame * frame
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:145
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:946
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:173
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition: options.c:188
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
@ AV_CODEC_ID_TDSC
Definition: codec_id.h:236
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:652
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:589
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:337
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:799
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:161
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:376
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
static const int16_t alpha[]
Definition: ilbcdata.h:55
misc image utilities
int i
Definition: input.c:407
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
uint8_t w
Definition: llviddspenc.c:39
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: macros.h:48
const char data[16]
Definition: mxf.c:142
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:623
int dct_algo
DCT algorithm, see FF_DCT_* below.
Definition: avcodec.h:1710
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1723
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
void * priv_data
Definition: avcodec.h:563
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1649
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1363
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
uint8_t * cursor
Definition: tdsc.c:66
int cursor_h
Definition: tdsc.c:68
GetByteContext gbc
Definition: tdsc.c:54
AVCodecContext * jpeg_avctx
Definition: tdsc.c:51
uint8_t * tilebuffer
Definition: tdsc.c:59
uint8_t * deflatebuffer
Definition: tdsc.c:62
int width
Definition: tdsc.c:53
AVFrame * refframe
Definition: tdsc.c:56
AVPacket * jpkt
Definition: tdsc.c:57
AVFrame * jpgframe
Definition: tdsc.c:58
int height
Definition: tdsc.c:53
int cursor_hot_x
Definition: tdsc.c:69
int cursor_hot_y
Definition: tdsc.c:69
int cursor_y
Definition: tdsc.c:68
int cursor_stride
Definition: tdsc.c:67
int cursor_x
Definition: tdsc.c:68
uLongf deflatelen
Definition: tdsc.c:63
int cursor_w
Definition: tdsc.c:68
Definition: graph2dot.c:48
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static av_cold int tdsc_close(AVCodecContext *avctx)
Definition: tdsc.c:79
#define TDSF_HEADER_SIZE
Definition: tdsc.c:47
#define BITMAPINFOHEADER_SIZE
Definition: tdsc.c:46
AVCodec ff_tdsc_decoder
Definition: tdsc.c:639
static av_cold int tdsc_init(AVCodecContext *avctx)
Definition: tdsc.c:94
static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
Definition: tdsc.c:146
#define TDSB_HEADER_SIZE
Definition: tdsc.c:48
static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
Definition: tdsc.c:392
static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
Definition: tdsc.c:463
#define APPLY_ALPHA(src, new, alpha)
Definition: tdsc.c:142
static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size, int x, int y, int w, int h)
Definition: tdsc.c:349
TDSCCursorFormat
Definition: tdsc.c:73
@ CUR_FMT_MONO
Definition: tdsc.c:74
@ CUR_FMT_BGRA
Definition: tdsc.c:75
@ CUR_FMT_RGBA
Definition: tdsc.c:76
static int tdsc_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tdsc.c:539
static void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
Definition: tdsc.c:322
static av_always_inline void tdsc_blit(uint8_t *dst, int dst_stride, const uint8_t *srcy, int srcy_stride, const uint8_t *srcu, const uint8_t *srcv, int srcuv_stride, int width, int height)
Definition: tdsc.c:330
static int tdsc_parse_dtsm(AVCodecContext *avctx)
Definition: tdsc.c:512
static int tdsc_load_cursor(AVCodecContext *avctx)
Definition: tdsc.c:200
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
uint8_t bits
Definition: vp3data.h:141