FFmpeg
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 <zlib.h>
38 
40 #include "libavutil/imgutils.h"
41 #include "libavutil/mem.h"
42 
43 #include "avcodec.h"
44 #include "bytestream.h"
45 #include "codec_internal.h"
46 #include "decode.h"
47 
48 #define BITMAPINFOHEADER_SIZE 0x28
49 #define TDSF_HEADER_SIZE 0x56
50 #define TDSB_HEADER_SIZE 0x08
51 
52 typedef struct TDSCContext {
53  AVCodecContext *jpeg_avctx; // wrapper context for MJPEG
54 
55  int width, height;
57 
58  AVFrame *refframe; // full decoded frame (without cursor)
59  AVPacket *jpkt; // encoded JPEG tile
60  AVFrame *jpgframe; // decoded JPEG tile
61  uint8_t *tilebuffer; // buffer containing tile data
62 
63  /* zlib interaction */
64  uint8_t *deflatebuffer;
65  uLongf deflatelen;
66 
67  /* All that is cursor */
68  uint8_t *cursor;
72 } TDSCContext;
73 
74 /* 1 byte bits, 1 byte planes, 2 bytes format (probably) */
76  CUR_FMT_MONO = 0x01010004,
77  CUR_FMT_BGRA = 0x20010004,
78  CUR_FMT_RGBA = 0x20010008,
79 };
80 
81 static av_cold int tdsc_close(AVCodecContext *avctx)
82 {
83  TDSCContext *ctx = avctx->priv_data;
84 
85  av_frame_free(&ctx->refframe);
86  av_frame_free(&ctx->jpgframe);
87  av_packet_free(&ctx->jpkt);
88  av_freep(&ctx->deflatebuffer);
89  av_freep(&ctx->tilebuffer);
90  av_freep(&ctx->cursor);
91  avcodec_free_context(&ctx->jpeg_avctx);
92 
93  return 0;
94 }
95 
96 static av_cold int tdsc_init(AVCodecContext *avctx)
97 {
98  TDSCContext *ctx = avctx->priv_data;
99  int ret;
100 
101  avctx->pix_fmt = AV_PIX_FMT_BGR24;
102 
103  /* These needs to be set to estimate buffer and frame size */
104  if (!(avctx->width && avctx->height)) {
105  av_log(avctx, AV_LOG_ERROR, "Video size not set.\n");
106  return AVERROR_INVALIDDATA;
107  }
108 
109  /* This value should be large enough for a RAW-only frame plus headers */
110  ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
111  ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
112  if (ret < 0)
113  return ret;
114 
115  /* Allocate reference and JPEG frame */
116  ctx->refframe = av_frame_alloc();
117  ctx->jpgframe = av_frame_alloc();
118  ctx->jpkt = av_packet_alloc();
119  if (!ctx->refframe || !ctx->jpgframe || !ctx->jpkt)
120  return AVERROR(ENOMEM);
121 
122  /* Prepare everything needed for JPEG decoding */
125  if (!ctx->jpeg_avctx)
126  return AVERROR(ENOMEM);
127  ctx->jpeg_avctx->flags = avctx->flags;
128  ctx->jpeg_avctx->flags2 = avctx->flags2;
129  ctx->jpeg_avctx->idct_algo = avctx->idct_algo;
130  ret = avcodec_open2(ctx->jpeg_avctx, NULL, NULL);
131  if (ret < 0)
132  return ret;
133 
134  /* Set the output pixel format on the reference frame */
135  ctx->refframe->format = avctx->pix_fmt;
136 
137  return 0;
138 }
139 
140 #define APPLY_ALPHA(src, new, alpha) \
141  src = (src * (256 - alpha) + new * alpha) >> 8
142 
143 /* Paint a region over a buffer, without drawing out of its bounds. */
144 static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
145 {
146  TDSCContext *ctx = avctx->priv_data;
147  const uint8_t *cursor = ctx->cursor;
148  int x = ctx->cursor_x - ctx->cursor_hot_x;
149  int y = ctx->cursor_y - ctx->cursor_hot_y;
150  int w = ctx->cursor_w;
151  int h = ctx->cursor_h;
152  int i, j;
153 
154  if (!ctx->cursor)
155  return;
156 
157  if (x + w > ctx->width)
158  w = ctx->width - x;
159  if (y + h > ctx->height)
160  h = ctx->height - y;
161  if (x < 0) {
162  w += x;
163  cursor += -x * 4;
164  } else {
165  dst += x * 3;
166  }
167  if (y < 0) {
168  h += y;
169  cursor += -y * ctx->cursor_stride;
170  } else {
171  dst += y * stride;
172  }
173  if (w < 0 || h < 0)
174  return;
175 
176  for (j = 0; j < h; j++) {
177  for (i = 0; i < w; i++) {
178  uint8_t alpha = cursor[i * 4];
179  APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
180  APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
181  APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
182  }
183  dst += stride;
184  cursor += ctx->cursor_stride;
185  }
186 }
187 
188 /* Load cursor data and store it in ABGR mode. */
190 {
191  TDSCContext *ctx = avctx->priv_data;
192  int i, j, k, ret, cursor_fmt;
193  uint8_t *dst;
194 
195  ctx->cursor_hot_x = bytestream2_get_le16(&ctx->gbc);
196  ctx->cursor_hot_y = bytestream2_get_le16(&ctx->gbc);
197  ctx->cursor_w = bytestream2_get_le16(&ctx->gbc);
198  ctx->cursor_h = bytestream2_get_le16(&ctx->gbc);
199 
200  ctx->cursor_stride = FFALIGN(ctx->cursor_w, 32) * 4;
201  cursor_fmt = bytestream2_get_le32(&ctx->gbc);
202 
203  if (ctx->cursor_x >= avctx->width || ctx->cursor_y >= avctx->height) {
204  av_log(avctx, AV_LOG_ERROR,
205  "Invalid cursor position (%d.%d outside %dx%d).\n",
206  ctx->cursor_x, ctx->cursor_y, avctx->width, avctx->height);
207  return AVERROR_INVALIDDATA;
208  }
209  if (ctx->cursor_w < 1 || ctx->cursor_w > 256 ||
210  ctx->cursor_h < 1 || ctx->cursor_h > 256) {
211  av_log(avctx, AV_LOG_ERROR,
212  "Invalid cursor dimensions %dx%d.\n",
213  ctx->cursor_w, ctx->cursor_h);
214  return AVERROR_INVALIDDATA;
215  }
216  if (ctx->cursor_hot_x > ctx->cursor_w ||
217  ctx->cursor_hot_y > ctx->cursor_h) {
218  av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d.%d.\n",
219  ctx->cursor_hot_x, ctx->cursor_hot_y);
220  ctx->cursor_hot_x = FFMIN(ctx->cursor_hot_x, ctx->cursor_w - 1);
221  ctx->cursor_hot_y = FFMIN(ctx->cursor_hot_y, ctx->cursor_h - 1);
222  }
223 
224  ret = av_reallocp(&ctx->cursor, ctx->cursor_stride * ctx->cursor_h);
225  if (ret < 0) {
226  av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer.\n");
227  return ret;
228  }
229 
230  dst = ctx->cursor;
231  /* here data is packed in BE */
232  switch (cursor_fmt) {
233  case CUR_FMT_MONO:
234  for (j = 0; j < ctx->cursor_h; j++) {
235  for (i = 0; i < ctx->cursor_w; i += 32) {
236  uint32_t bits = bytestream2_get_be32(&ctx->gbc);
237  for (k = 0; k < 32; k++) {
238  dst[0] = !!(bits & 0x80000000);
239  dst += 4;
240  bits <<= 1;
241  }
242  }
243  dst += ctx->cursor_stride - ctx->cursor_w * 4;
244  }
245 
246  dst = ctx->cursor;
247  for (j = 0; j < ctx->cursor_h; j++) {
248  for (i = 0; i < ctx->cursor_w; i += 32) {
249  uint32_t bits = bytestream2_get_be32(&ctx->gbc);
250  for (k = 0; k < 32; k++) {
251  int mask_bit = !!(bits & 0x80000000);
252  switch (dst[0] * 2 + mask_bit) {
253  case 0:
254  dst[0] = 0xFF;
255  dst[1] = 0x00;
256  dst[2] = 0x00;
257  dst[3] = 0x00;
258  break;
259  case 1:
260  dst[0] = 0xFF;
261  dst[1] = 0xFF;
262  dst[2] = 0xFF;
263  dst[3] = 0xFF;
264  break;
265  default:
266  dst[0] = 0x00;
267  dst[1] = 0x00;
268  dst[2] = 0x00;
269  dst[3] = 0x00;
270  }
271  dst += 4;
272  bits <<= 1;
273  }
274  }
275  dst += ctx->cursor_stride - ctx->cursor_w * 4;
276  }
277  break;
278  case CUR_FMT_BGRA:
279  case CUR_FMT_RGBA:
280  /* Skip monochrome version of the cursor */
281  bytestream2_skip(&ctx->gbc,
282  ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 3));
283  if (cursor_fmt & 8) { // RGBA -> ABGR
284  for (j = 0; j < ctx->cursor_h; j++) {
285  for (i = 0; i < ctx->cursor_w; i++) {
286  int val = bytestream2_get_be32(&ctx->gbc);
287  *dst++ = val >> 24;
288  *dst++ = val >> 16;
289  *dst++ = val >> 8;
290  *dst++ = val >> 0;
291  }
292  dst += ctx->cursor_stride - ctx->cursor_w * 4;
293  }
294  } else { // BGRA -> ABGR
295  for (j = 0; j < ctx->cursor_h; j++) {
296  for (i = 0; i < ctx->cursor_w; i++) {
297  int val = bytestream2_get_be32(&ctx->gbc);
298  *dst++ = val >> 0;
299  *dst++ = val >> 24;
300  *dst++ = val >> 16;
301  *dst++ = val >> 8;
302  }
303  dst += ctx->cursor_stride - ctx->cursor_w * 4;
304  }
305  }
306  break;
307  default:
308  avpriv_request_sample(avctx, "Cursor format %08x", cursor_fmt);
309  return AVERROR_PATCHWELCOME;
310  }
311 
312  return 0;
313 }
314 
315 /* Convert a single YUV pixel to RGB. */
316 static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
317 {
318  out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
319  out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
320  out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
321 }
322 
323 /* Convert a YUV420 buffer to a RGB buffer. */
324 static av_always_inline void tdsc_blit(uint8_t *dst, int dst_stride,
325  const uint8_t *srcy, int srcy_stride,
326  const uint8_t *srcu, const uint8_t *srcv,
327  int srcuv_stride, int width, int height)
328 {
329  int col, line;
330  for (line = 0; line < height; line++) {
331  for (col = 0; col < width; col++)
332  tdsc_yuv2rgb(dst + col * 3, srcy[col],
333  srcu[col >> 1] - 128, srcv[col >> 1] - 128);
334 
335  dst += dst_stride;
336  srcy += srcy_stride;
337  srcu += srcuv_stride * (line & 1);
338  srcv += srcuv_stride * (line & 1);
339  }
340 }
341 
342 /* Invoke the MJPEG decoder to decode the tile. */
343 static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size,
344  int x, int y, int w, int h)
345 {
346  TDSCContext *ctx = avctx->priv_data;
347  int ret;
348 
349  /* Prepare a packet and send to the MJPEG decoder */
350  av_packet_unref(ctx->jpkt);
351  ctx->jpkt->data = ctx->tilebuffer;
352  ctx->jpkt->size = tile_size;
353 
354  ret = avcodec_send_packet(ctx->jpeg_avctx, ctx->jpkt);
355  if (ret < 0) {
356  av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
357  return ret;
358  }
359 
360  ret = avcodec_receive_frame(ctx->jpeg_avctx, ctx->jpgframe);
361  if (ret < 0 || ctx->jpgframe->format != AV_PIX_FMT_YUVJ420P) {
362  av_log(avctx, AV_LOG_ERROR,
363  "JPEG decoding error (%d).\n", ret);
364 
365  /* Normally skip, error if explode */
366  if (avctx->err_recognition & AV_EF_EXPLODE)
367  return AVERROR_INVALIDDATA;
368  else
369  return 0;
370  }
371 
372  /* Let's paint onto the buffer */
373  tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * y,
374  ctx->refframe->linesize[0],
375  ctx->jpgframe->data[0], ctx->jpgframe->linesize[0],
376  ctx->jpgframe->data[1], ctx->jpgframe->data[2],
377  ctx->jpgframe->linesize[1], w, h);
378 
379  av_frame_unref(ctx->jpgframe);
380 
381  return 0;
382 }
383 
384 /* Parse frame and either copy data or decode JPEG. */
385 static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
386 {
387  TDSCContext *ctx = avctx->priv_data;
388  int i;
389 
390  /* Iterate over the number of tiles */
391  for (i = 0; i < number_tiles; i++) {
392  int tile_size;
393  int tile_mode;
394  int x, y, x2, y2, w, h;
395  int ret;
396 
397  if (bytestream2_get_bytes_left(&ctx->gbc) < 4 ||
398  bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') ||
400  av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n");
401  return AVERROR_INVALIDDATA;
402  }
403 
404  tile_size = bytestream2_get_le32(&ctx->gbc);
405  if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size)
406  return AVERROR_INVALIDDATA;
407 
408  tile_mode = bytestream2_get_le32(&ctx->gbc);
409  bytestream2_skip(&ctx->gbc, 4); // unknown
410  x = bytestream2_get_le32(&ctx->gbc);
411  y = bytestream2_get_le32(&ctx->gbc);
412  x2 = bytestream2_get_le32(&ctx->gbc);
413  y2 = bytestream2_get_le32(&ctx->gbc);
414 
415  if (x < 0 || y < 0 || x2 <= x || y2 <= y ||
416  x2 > ctx->width || y2 > ctx->height
417  ) {
418  av_log(avctx, AV_LOG_ERROR,
419  "Invalid tile position (%d.%d %d.%d outside %dx%d).\n",
420  x, y, x2, y2, ctx->width, ctx->height);
421  return AVERROR_INVALIDDATA;
422  }
423  w = x2 - x;
424  h = y2 - y;
425 
426  ret = av_reallocp(&ctx->tilebuffer, tile_size);
427  if (!ctx->tilebuffer)
428  return ret;
429 
430  bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size);
431 
432  if (tile_mode == MKTAG('G','E','P','J')) {
433  /* Decode JPEG tile and copy it in the reference frame */
434  ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h);
435  if (ret < 0)
436  return ret;
437  } else if (tile_mode == MKTAG(' ','W','A','R')) {
438  /* Just copy the buffer to output */
439  av_image_copy_plane(ctx->refframe->data[0] + x * 3 +
440  ctx->refframe->linesize[0] * y,
441  ctx->refframe->linesize[0], ctx->tilebuffer,
442  w * 3, w * 3, h);
443  } else {
444  av_log(avctx, AV_LOG_ERROR, "Unknown tile type %08x.\n", tile_mode);
445  return AVERROR_INVALIDDATA;
446  }
447  av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, y);
448  }
449 
450  return 0;
451 }
452 
453 static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
454 {
455  TDSCContext *ctx = avctx->priv_data;
456  int ret, w, h, init_refframe = !ctx->refframe->data[0];
457 
458  /* BITMAPINFOHEADER
459  * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */
460  if (bytestream2_get_le32(&ctx->gbc) != BITMAPINFOHEADER_SIZE)
461  return AVERROR_INVALIDDATA;
462 
463  /* Store size, but wait for context reinit before updating avctx */
464  w = bytestream2_get_le32(&ctx->gbc);
465  h = -bytestream2_get_le32(&ctx->gbc);
466 
467  if (bytestream2_get_le16(&ctx->gbc) != 1 || // 1 plane
468  bytestream2_get_le16(&ctx->gbc) != 24) // BGR24
469  return AVERROR_INVALIDDATA;
470 
471  bytestream2_skip(&ctx->gbc, 24); // unused fields
472 
473  /* Update sizes */
474  if (avctx->width != w || avctx->height != h) {
475  av_log(avctx, AV_LOG_DEBUG, "Size update %dx%d -> %d%d.\n",
476  avctx->width, avctx->height, ctx->width, ctx->height);
477  ret = ff_set_dimensions(avctx, w, h);
478  if (ret < 0)
479  return ret;
480  init_refframe = 1;
481  }
482  ctx->refframe->width = ctx->width = w;
483  ctx->refframe->height = ctx->height = h;
484 
485  /* Allocate the reference frame if not already done or on size change */
486  if (init_refframe) {
487  ret = av_frame_get_buffer(ctx->refframe, 0);
488  if (ret < 0)
489  return ret;
490  }
491 
492  /* Decode all tiles in a frame */
493  return tdsc_decode_tiles(avctx, number_tiles);
494 }
495 
496 static int tdsc_parse_dtsm(AVCodecContext *avctx)
497 {
498  TDSCContext *ctx = avctx->priv_data;
499  int ret;
500  int action = bytestream2_get_le32(&ctx->gbc);
501 
502  bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe?
503 
504  if (action == 2 || action == 3) {
505  /* Load cursor coordinates */
506  ctx->cursor_x = bytestream2_get_le32(&ctx->gbc);
507  ctx->cursor_y = bytestream2_get_le32(&ctx->gbc);
508 
509  /* Load a full cursor sprite */
510  if (action == 3) {
511  ret = tdsc_load_cursor(avctx);
512  /* Do not consider cursor errors fatal unless in explode mode */
513  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
514  return ret;
515  }
516  } else {
517  avpriv_request_sample(avctx, "Cursor action %d", action);
518  }
519 
520  return 0;
521 }
522 
524  int *got_frame, AVPacket *avpkt)
525 {
526  TDSCContext *ctx = avctx->priv_data;
527  int ret, tag_header, keyframe = 0;
528  uLongf dlen;
529 
530  /* Resize deflate buffer on resolution change */
531  if (ctx->width != avctx->width || ctx->height != avctx->height) {
532  int deflatelen = avctx->width * avctx->height * (3 + 1);
533  if (deflatelen != ctx->deflatelen) {
534  ctx->deflatelen =deflatelen;
535  ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
536  if (ret < 0) {
537  ctx->deflatelen = 0;
538  return ret;
539  }
540  }
541  }
542  dlen = ctx->deflatelen;
543 
544  /* Frames are deflated, need to inflate them first */
545  ret = uncompress(ctx->deflatebuffer, &dlen, avpkt->data, avpkt->size);
546  if (ret) {
547  av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
548  return AVERROR_UNKNOWN;
549  }
550 
551  bytestream2_init(&ctx->gbc, ctx->deflatebuffer, dlen);
552 
553  /* Check for tag and for size info */
554  if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
555  av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
556  return AVERROR_INVALIDDATA;
557  }
558 
559  /* Read tag */
560  tag_header = bytestream2_get_le32(&ctx->gbc);
561 
562  if (tag_header == MKTAG('T','D','S','F')) {
563  int number_tiles;
565  av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n");
566  return AVERROR_INVALIDDATA;
567  }
568  /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
569  number_tiles = bytestream2_get_le32(&ctx->gbc);
570 
571  bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe?
572  keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30;
573 
574  ret = tdsc_parse_tdsf(avctx, number_tiles);
575  if (ret < 0)
576  return ret;
577 
578  /* Check if there is anything else we are able to parse */
579  if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4)
580  tag_header = bytestream2_get_le32(&ctx->gbc);
581  }
582 
583  /* This tag can be after a TDSF block or on its own frame */
584  if (tag_header == MKTAG('D','T','S','M')) {
585  /* First 4 bytes here are the total size in bytes for this frame */
586  int tag_size = bytestream2_get_le32(&ctx->gbc);
587 
588  if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) {
589  av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n");
590  return AVERROR_INVALIDDATA;
591  }
592 
593  ret = tdsc_parse_dtsm(avctx);
594  if (ret < 0)
595  return ret;
596  }
597 
598  /* Get the output frame and copy the reference frame */
599  ret = ff_get_buffer(avctx, frame, 0);
600  if (ret < 0)
601  return ret;
602 
603  ret = av_frame_copy(frame, ctx->refframe);
604  if (ret < 0)
605  return ret;
606 
607  /* Paint the cursor on the output frame */
608  tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]);
609 
610  /* Frame is ready to be output */
611  if (keyframe) {
612  frame->pict_type = AV_PICTURE_TYPE_I;
613  frame->flags |= AV_FRAME_FLAG_KEY;
614  } else {
615  frame->pict_type = AV_PICTURE_TYPE_P;
616  }
617  *got_frame = 1;
618 
619  return avpkt->size;
620 }
621 
623  .p.name = "tdsc",
624  CODEC_LONG_NAME("TDSC"),
625  .p.type = AVMEDIA_TYPE_VIDEO,
626  .p.id = AV_CODEC_ID_TDSC,
627  .init = tdsc_init,
629  .close = tdsc_close,
630  .priv_data_size = sizeof(TDSCContext),
631  .p.capabilities = AV_CODEC_CAP_DR1,
632  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
633 };
TDSCContext::deflatelen
uLongf deflatelen
Definition: tdsc.c:65
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:430
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
APPLY_ALPHA
#define APPLY_ALPHA(src, new, alpha)
Definition: tdsc.c:140
tdsc_yuv2rgb
static void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
Definition: tdsc.c:316
out
FILE * out
Definition: movenc.c:55
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:205
GetByteContext
Definition: bytestream.h:33
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1398
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:63
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
w
uint8_t w
Definition: llviddspenc.c:38
TDSCContext::cursor_stride
int cursor_stride
Definition: tdsc.c:69
ff_mjpeg_decoder
const FFCodec ff_mjpeg_decoder
AVPacket::data
uint8_t * data
Definition: packet.h:535
TDSCContext::width
int width
Definition: tdsc.c:55
tdsc_decode_frame
static int tdsc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: tdsc.c:523
FFCodec
Definition: codec_internal.h:127
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
TDSCContext::tilebuffer
uint8_t * tilebuffer
Definition: tdsc.c:61
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
ff_set_dimensions
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:91
TDSCContext::gbc
GetByteContext gbc
Definition: tdsc.c:56
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:75
av_image_copy_plane
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:374
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
TDSCContext::cursor_h
int cursor_h
Definition: tdsc.c:70
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:488
val
static double val(void *priv, double ch)
Definition: aeval.c:77
tdsc_parse_tdsf
static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
Definition: tdsc.c:453
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:51
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
tdsc_init
static av_cold int tdsc_init(AVCodecContext *avctx)
Definition: tdsc.c:96
tdsc_paint_cursor
static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
Definition: tdsc.c:144
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
attributes_internal.h
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:341
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1416
TDSCContext::jpkt
AVPacket * jpkt
Definition: tdsc.c:59
bits
uint8_t bits
Definition: vp3data.h:128
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:702
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
TDSCContext::cursor_hot_x
int cursor_hot_x
Definition: tdsc.c:71
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
EXTERN
#define EXTERN
Definition: attributes_internal.h:34
tdsc_parse_dtsm
static int tdsc_parse_dtsm(AVCodecContext *avctx)
Definition: tdsc.c:496
AV_CODEC_ID_TDSC
@ AV_CODEC_ID_TDSC
Definition: codec_id.h:241
TDSCContext::cursor_x
int cursor_x
Definition: tdsc.c:70
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
TDSB_HEADER_SIZE
#define TDSB_HEADER_SIZE
Definition: tdsc.c:50
avcodec_free_context
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:164
AV_PIX_FMT_YUVJ420P
@ 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:85
V
#define V
Definition: avdct.c:31
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
TDSCContext::jpeg_avctx
AVCodecContext * jpeg_avctx
Definition: tdsc.c:53
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:143
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
TDSCCursorFormat
TDSCCursorFormat
Definition: tdsc.c:75
TDSCContext
Definition: tdsc.c:52
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:495
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1611
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:536
height
#define height
Definition: dsp.h:85
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:713
av_reallocp
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:188
TDSCContext::cursor_w
int cursor_w
Definition: tdsc.c:70
line
Definition: graph2dot.c:48
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:64
tdsc_decode_tiles
static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
Definition: tdsc.c:385
Y
#define Y
Definition: boxblur.h:37
TDSCContext::deflatebuffer
uint8_t * deflatebuffer
Definition: tdsc.c:64
ff_tdsc_decoder
const FFCodec ff_tdsc_decoder
Definition: tdsc.c:622
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:704
TDSCContext::cursor_y
int cursor_y
Definition: tdsc.c:70
TDSCContext::cursor
uint8_t * cursor
Definition: tdsc.c:68
TDSCContext::cursor_hot_y
int cursor_hot_y
Definition: tdsc.c:71
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_always_inline
#define av_always_inline
Definition: attributes.h:49
CUR_FMT_RGBA
@ CUR_FMT_RGBA
Definition: tdsc.c:78
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:498
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1526
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::height
int height
Definition: avcodec.h:592
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:631
TDSF_HEADER_SIZE
#define TDSF_HEADER_SIZE
Definition: tdsc.c:49
avcodec.h
stride
#define stride
Definition: h264pred_template.c:536
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
tdsc_load_cursor
static int tdsc_load_cursor(AVCodecContext *avctx)
Definition: tdsc.c:189
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:431
CUR_FMT_MONO
@ CUR_FMT_MONO
Definition: tdsc.c:76
tdsc_decode_jpeg_tile
static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size, int x, int y, int w, int h)
Definition: tdsc.c:343
av_clip_uint8
#define av_clip_uint8
Definition: common.h:106
TDSCContext::jpgframe
AVFrame * jpgframe
Definition: tdsc.c:60
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
TDSCContext::height
int height
Definition: tdsc.c:55
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
TDSCContext::refframe
AVFrame * refframe
Definition: tdsc.c:58
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
AVPacket
This structure stores compressed data.
Definition: packet.h:512
tdsc_blit
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:324
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
bytestream.h
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
tdsc_close
static av_cold int tdsc_close(AVCodecContext *avctx)
Definition: tdsc.c:81
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2070
width
#define width
Definition: dsp.h:85
CUR_FMT_BGRA
@ CUR_FMT_BGRA
Definition: tdsc.c:77
BITMAPINFOHEADER_SIZE
#define BITMAPINFOHEADER_SIZE
Definition: tdsc.c:48
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40