You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

631 lines
21KB

  1. /*
  2. * TDSC decoder
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * TDSC decoder
  24. *
  25. * Fourcc: TSDC
  26. *
  27. * TDSC is very simple. It codes picture by tiles, storing them in raw BGR24
  28. * format or compressing them in JPEG. Frames can be full pictures or just
  29. * updates to the previous frame. Cursor is found in its own frame or at the
  30. * bottom of the picture. Every frame is then packed with zlib.
  31. *
  32. * Supports: BGR24
  33. */
  34. #include <stdint.h>
  35. #include <zlib.h>
  36. #include "libavutil/imgutils.h"
  37. #include "avcodec.h"
  38. #include "bytestream.h"
  39. #include "internal.h"
  40. #define BITMAPINFOHEADER_SIZE 0x28
  41. #define TDSF_HEADER_SIZE 0x56
  42. #define TDSB_HEADER_SIZE 0x08
  43. typedef struct TDSCContext {
  44. AVCodecContext *jpeg_avctx; // wrapper context for MJPEG
  45. int width, height;
  46. GetByteContext gbc;
  47. AVFrame *refframe; // full decoded frame (without cursor)
  48. AVFrame *jpgframe; // decoded JPEG tile
  49. uint8_t *tilebuffer; // buffer containing tile data
  50. /* zlib interaction */
  51. uint8_t *deflatebuffer;
  52. uLongf deflatelen;
  53. /* All that is cursor */
  54. uint8_t *cursor;
  55. int cursor_stride;
  56. int cursor_w, cursor_h, cursor_x, cursor_y;
  57. int cursor_hot_x, cursor_hot_y;
  58. } TDSCContext;
  59. /* 1 byte bits, 1 byte planes, 2 bytes format (probably) */
  60. enum TDSCCursorFormat {
  61. CUR_FMT_MONO = 0x01010004,
  62. CUR_FMT_BGRA = 0x20010004,
  63. CUR_FMT_RGBA = 0x20010008,
  64. };
  65. static av_cold int tdsc_close(AVCodecContext *avctx)
  66. {
  67. TDSCContext *ctx = avctx->priv_data;
  68. av_frame_free(&ctx->refframe);
  69. av_frame_free(&ctx->jpgframe);
  70. av_freep(&ctx->deflatebuffer);
  71. av_freep(&ctx->tilebuffer);
  72. av_freep(&ctx->cursor);
  73. avcodec_free_context(&ctx->jpeg_avctx);
  74. return 0;
  75. }
  76. static av_cold int tdsc_init(AVCodecContext *avctx)
  77. {
  78. TDSCContext *ctx = avctx->priv_data;
  79. const AVCodec *codec;
  80. int ret;
  81. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  82. /* These needs to be set to estimate buffer and frame size */
  83. if (!(avctx->width && avctx->height)) {
  84. av_log(avctx, AV_LOG_ERROR, "Video size not set.\n");
  85. return AVERROR_INVALIDDATA;
  86. }
  87. /* This value should be large enough for a RAW-only frame plus headers */
  88. ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
  89. ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
  90. if (ret < 0)
  91. return ret;
  92. /* Allocate reference and JPEG frame */
  93. ctx->refframe = av_frame_alloc();
  94. ctx->jpgframe = av_frame_alloc();
  95. if (!ctx->refframe || !ctx->jpgframe)
  96. return AVERROR(ENOMEM);
  97. /* Prepare everything needed for JPEG decoding */
  98. codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  99. if (!codec)
  100. return AVERROR_BUG;
  101. ctx->jpeg_avctx = avcodec_alloc_context3(codec);
  102. if (!ctx->jpeg_avctx)
  103. return AVERROR(ENOMEM);
  104. ctx->jpeg_avctx->flags = avctx->flags;
  105. ctx->jpeg_avctx->flags2 = avctx->flags2;
  106. ctx->jpeg_avctx->dct_algo = avctx->dct_algo;
  107. ctx->jpeg_avctx->idct_algo = avctx->idct_algo;;
  108. ret = avcodec_open2(ctx->jpeg_avctx, codec, NULL);
  109. if (ret < 0)
  110. return ret;
  111. /* Set the output pixel format on the reference frame */
  112. ctx->refframe->format = avctx->pix_fmt;
  113. return 0;
  114. }
  115. #define APPLY_ALPHA(src, new, alpha) \
  116. src = (src * (256 - alpha) + new * alpha) >> 8
  117. /* Paint a region over a buffer, without drawing out of its bounds. */
  118. static void tdsc_paint_cursor(AVCodecContext *avctx, uint8_t *dst, int stride)
  119. {
  120. TDSCContext *ctx = avctx->priv_data;
  121. const uint8_t *cursor = ctx->cursor;
  122. int x = ctx->cursor_x - ctx->cursor_hot_x;
  123. int y = ctx->cursor_y - ctx->cursor_hot_y;
  124. int w = ctx->cursor_w;
  125. int h = ctx->cursor_h;
  126. int i, j;
  127. if (!ctx->cursor)
  128. return;
  129. if (x + w > ctx->width)
  130. w = ctx->width - x;
  131. if (y + h > ctx->height)
  132. h = ctx->height - y;
  133. if (x < 0) {
  134. w += x;
  135. cursor += -x * 4;
  136. } else {
  137. dst += x * 3;
  138. }
  139. if (y < 0) {
  140. h += y;
  141. cursor += -y * ctx->cursor_stride;
  142. } else {
  143. dst += y * stride;
  144. }
  145. if (w < 0 || h < 0)
  146. return;
  147. for (j = 0; j < h; j++) {
  148. for (i = 0; i < w; i++) {
  149. uint8_t alpha = cursor[i * 4];
  150. APPLY_ALPHA(dst[i * 3 + 0], cursor[i * 4 + 1], alpha);
  151. APPLY_ALPHA(dst[i * 3 + 1], cursor[i * 4 + 2], alpha);
  152. APPLY_ALPHA(dst[i * 3 + 2], cursor[i * 4 + 3], alpha);
  153. }
  154. dst += stride;
  155. cursor += ctx->cursor_stride;
  156. }
  157. }
  158. /* Load cursor data and store it in ABGR mode. */
  159. static int tdsc_load_cursor(AVCodecContext *avctx)
  160. {
  161. TDSCContext *ctx = avctx->priv_data;
  162. int i, j, k, ret, bits, cursor_fmt;
  163. uint8_t *dst;
  164. ctx->cursor_hot_x = bytestream2_get_le16(&ctx->gbc);
  165. ctx->cursor_hot_y = bytestream2_get_le16(&ctx->gbc);
  166. ctx->cursor_w = bytestream2_get_le16(&ctx->gbc);
  167. ctx->cursor_h = bytestream2_get_le16(&ctx->gbc);
  168. ctx->cursor_stride = FFALIGN(ctx->cursor_w, 32) * 4;
  169. cursor_fmt = bytestream2_get_le32(&ctx->gbc);
  170. if (ctx->cursor_x >= avctx->width || ctx->cursor_y >= avctx->height) {
  171. av_log(avctx, AV_LOG_ERROR,
  172. "Invalid cursor position (%d.%d outside %dx%d).\n",
  173. ctx->cursor_x, ctx->cursor_y, avctx->width, avctx->height);
  174. return AVERROR_INVALIDDATA;
  175. }
  176. if (ctx->cursor_w < 1 || ctx->cursor_w > 256 ||
  177. ctx->cursor_h < 1 || ctx->cursor_h > 256) {
  178. av_log(avctx, AV_LOG_ERROR,
  179. "Invalid cursor dimensions %dx%d.\n",
  180. ctx->cursor_w, ctx->cursor_h);
  181. return AVERROR_INVALIDDATA;
  182. }
  183. if (ctx->cursor_hot_x > ctx->cursor_w ||
  184. ctx->cursor_hot_y > ctx->cursor_h) {
  185. av_log(avctx, AV_LOG_WARNING, "Invalid hotspot position %d.%d.\n",
  186. ctx->cursor_hot_x, ctx->cursor_hot_y);
  187. ctx->cursor_hot_x = FFMIN(ctx->cursor_hot_x, ctx->cursor_w - 1);
  188. ctx->cursor_hot_y = FFMIN(ctx->cursor_hot_y, ctx->cursor_h - 1);
  189. }
  190. ret = av_reallocp(&ctx->cursor, ctx->cursor_stride * ctx->cursor_h);
  191. if (ret < 0) {
  192. av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer.\n");
  193. return ret;
  194. }
  195. dst = ctx->cursor;
  196. /* here data is packed in BE */
  197. switch (cursor_fmt) {
  198. case CUR_FMT_MONO:
  199. for (j = 0; j < ctx->cursor_h; j++) {
  200. for (i = 0; i < ctx->cursor_w; i += 32) {
  201. bits = bytestream2_get_be32(&ctx->gbc);
  202. for (k = 0; k < 32; k++) {
  203. dst[0] = !!(bits & 0x80000000);
  204. dst += 4;
  205. bits <<= 1;
  206. }
  207. }
  208. dst += ctx->cursor_stride - ctx->cursor_w * 4;
  209. }
  210. dst = ctx->cursor;
  211. for (j = 0; j < ctx->cursor_h; j++) {
  212. for (i = 0; i < ctx->cursor_w; i += 32) {
  213. bits = bytestream2_get_be32(&ctx->gbc);
  214. for (k = 0; k < 32; k++) {
  215. int mask_bit = !!(bits & 0x80000000);
  216. switch (dst[0] * 2 + mask_bit) {
  217. case 0:
  218. dst[0] = 0xFF;
  219. dst[1] = 0x00;
  220. dst[2] = 0x00;
  221. dst[3] = 0x00;
  222. break;
  223. case 1:
  224. dst[0] = 0xFF;
  225. dst[1] = 0xFF;
  226. dst[2] = 0xFF;
  227. dst[3] = 0xFF;
  228. break;
  229. default:
  230. dst[0] = 0x00;
  231. dst[1] = 0x00;
  232. dst[2] = 0x00;
  233. dst[3] = 0x00;
  234. }
  235. dst += 4;
  236. bits <<= 1;
  237. }
  238. }
  239. dst += ctx->cursor_stride - ctx->cursor_w * 4;
  240. }
  241. break;
  242. case CUR_FMT_BGRA:
  243. case CUR_FMT_RGBA:
  244. /* Skip monochrome version of the cursor */
  245. bytestream2_skip(&ctx->gbc,
  246. ctx->cursor_h * (FFALIGN(ctx->cursor_w, 32) >> 3));
  247. if (cursor_fmt & 8) { // RGBA -> ABGR
  248. for (j = 0; j < ctx->cursor_h; j++) {
  249. for (i = 0; i < ctx->cursor_w; i++) {
  250. int val = bytestream2_get_be32(&ctx->gbc);
  251. *dst++ = val >> 24;
  252. *dst++ = val >> 16;
  253. *dst++ = val >> 8;
  254. *dst++ = val >> 0;
  255. }
  256. dst += ctx->cursor_stride - ctx->cursor_w * 4;
  257. }
  258. } else { // BGRA -> ABGR
  259. for (j = 0; j < ctx->cursor_h; j++) {
  260. for (i = 0; i < ctx->cursor_w; i++) {
  261. int val = bytestream2_get_be32(&ctx->gbc);
  262. *dst++ = val >> 0;
  263. *dst++ = val >> 24;
  264. *dst++ = val >> 16;
  265. *dst++ = val >> 8;
  266. }
  267. dst += ctx->cursor_stride - ctx->cursor_w * 4;
  268. }
  269. }
  270. break;
  271. default:
  272. avpriv_request_sample(avctx, "Cursor format %08x", cursor_fmt);
  273. return AVERROR_PATCHWELCOME;
  274. }
  275. return 0;
  276. }
  277. /* Convert a single YUV pixel to RGB. */
  278. static inline void tdsc_yuv2rgb(uint8_t *out, int Y, int U, int V)
  279. {
  280. out[0] = av_clip_uint8(Y + ( 91881 * V + 32768 >> 16));
  281. out[1] = av_clip_uint8(Y + (-22554 * U - 46802 * V + 32768 >> 16));
  282. out[2] = av_clip_uint8(Y + (116130 * U + 32768 >> 16));
  283. }
  284. /* Convert a YUV420 buffer to a RGB buffer. */
  285. static av_always_inline void tdsc_blit(uint8_t *dst, int dst_stride,
  286. const uint8_t *srcy, int srcy_stride,
  287. const uint8_t *srcu, const uint8_t *srcv,
  288. int srcuv_stride, int width, int height)
  289. {
  290. int col, line;
  291. for (line = 0; line < height; line++) {
  292. for (col = 0; col < width; col++)
  293. tdsc_yuv2rgb(dst + col * 3, srcy[col],
  294. srcu[col >> 1] - 128, srcv[col >> 1] - 128);
  295. dst += dst_stride;
  296. srcy += srcy_stride;
  297. srcu += srcuv_stride * (line & 1);
  298. srcv += srcuv_stride * (line & 1);
  299. }
  300. }
  301. /* Invoke the MJPEG decoder to decode the tile. */
  302. static int tdsc_decode_jpeg_tile(AVCodecContext *avctx, int tile_size,
  303. int x, int y, int w, int h)
  304. {
  305. TDSCContext *ctx = avctx->priv_data;
  306. AVPacket jpkt;
  307. int ret;
  308. /* Prepare a packet and send to the MJPEG decoder */
  309. av_init_packet(&jpkt);
  310. jpkt.data = ctx->tilebuffer;
  311. jpkt.size = tile_size;
  312. ret = avcodec_send_packet(ctx->jpeg_avctx, &jpkt);
  313. if (ret < 0) {
  314. av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
  315. return ret;
  316. }
  317. ret = avcodec_receive_frame(ctx->jpeg_avctx, ctx->jpgframe);
  318. if (ret < 0 || ctx->jpgframe->format != AV_PIX_FMT_YUVJ420P) {
  319. av_log(avctx, AV_LOG_ERROR,
  320. "JPEG decoding error (%d).\n", ret);
  321. /* Normally skip, error if explode */
  322. if (avctx->err_recognition & AV_EF_EXPLODE)
  323. return AVERROR_INVALIDDATA;
  324. else
  325. return 0;
  326. }
  327. /* Let's paint onto the buffer */
  328. tdsc_blit(ctx->refframe->data[0] + x * 3 + ctx->refframe->linesize[0] * y,
  329. ctx->refframe->linesize[0],
  330. ctx->jpgframe->data[0], ctx->jpgframe->linesize[0],
  331. ctx->jpgframe->data[1], ctx->jpgframe->data[2],
  332. ctx->jpgframe->linesize[1], w, h);
  333. av_frame_unref(ctx->jpgframe);
  334. return 0;
  335. }
  336. /* Parse frame and either copy data or decode JPEG. */
  337. static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles)
  338. {
  339. TDSCContext *ctx = avctx->priv_data;
  340. int i;
  341. /* Iterate over the number of tiles */
  342. for (i = 0; i < number_tiles; i++) {
  343. int tile_size;
  344. int tile_mode;
  345. int x, y, w, h;
  346. int ret;
  347. if (bytestream2_get_bytes_left(&ctx->gbc) < 4 ||
  348. bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') ||
  349. bytestream2_get_bytes_left(&ctx->gbc) < TDSB_HEADER_SIZE - 4) {
  350. av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n");
  351. return AVERROR_INVALIDDATA;
  352. }
  353. tile_size = bytestream2_get_le32(&ctx->gbc);
  354. if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size)
  355. return AVERROR_INVALIDDATA;
  356. tile_mode = bytestream2_get_le32(&ctx->gbc);
  357. bytestream2_skip(&ctx->gbc, 4); // unknown
  358. x = bytestream2_get_le32(&ctx->gbc);
  359. y = bytestream2_get_le32(&ctx->gbc);
  360. w = bytestream2_get_le32(&ctx->gbc) - x;
  361. h = bytestream2_get_le32(&ctx->gbc) - y;
  362. if (x >= ctx->width || y >= ctx->height) {
  363. av_log(avctx, AV_LOG_ERROR,
  364. "Invalid tile position (%d.%d outside %dx%d).\n",
  365. x, y, ctx->width, ctx->height);
  366. return AVERROR_INVALIDDATA;
  367. }
  368. if (x + w > ctx->width || y + h > ctx->height) {
  369. av_log(avctx, AV_LOG_ERROR,
  370. "Invalid tile size %dx%d\n", w, h);
  371. return AVERROR_INVALIDDATA;
  372. }
  373. ret = av_reallocp(&ctx->tilebuffer, tile_size);
  374. if (!ctx->tilebuffer)
  375. return ret;
  376. bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size);
  377. if (tile_mode == MKTAG('G','E','P','J')) {
  378. /* Decode JPEG tile and copy it in the reference frame */
  379. ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h);
  380. if (ret < 0)
  381. return ret;
  382. } else if (tile_mode == MKTAG(' ','W','A','R')) {
  383. /* Just copy the buffer to output */
  384. av_image_copy_plane(ctx->refframe->data[0] + x * 3 +
  385. ctx->refframe->linesize[0] * y,
  386. ctx->refframe->linesize[0], ctx->tilebuffer,
  387. w * 3, w * 3, h);
  388. } else {
  389. av_log(avctx, AV_LOG_ERROR, "Unknown tile type %08x.\n", tile_mode);
  390. return AVERROR_INVALIDDATA;
  391. }
  392. av_log(avctx, AV_LOG_DEBUG, "Tile %d, %dx%d (%d.%d)\n", i, w, h, x, y);
  393. }
  394. return 0;
  395. }
  396. static int tdsc_parse_tdsf(AVCodecContext *avctx, int number_tiles)
  397. {
  398. TDSCContext *ctx = avctx->priv_data;
  399. int ret, w, h, init_refframe = !ctx->refframe->data[0];
  400. /* BITMAPINFOHEADER
  401. * http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376.aspx */
  402. if (bytestream2_get_le32(&ctx->gbc) != BITMAPINFOHEADER_SIZE)
  403. return AVERROR_INVALIDDATA;
  404. /* Store size, but wait for context reinit before updating avctx */
  405. w = bytestream2_get_le32(&ctx->gbc);
  406. h = -bytestream2_get_le32(&ctx->gbc);
  407. if (bytestream2_get_le16(&ctx->gbc) != 1 || // 1 plane
  408. bytestream2_get_le16(&ctx->gbc) != 24) // BGR24
  409. return AVERROR_INVALIDDATA;
  410. bytestream2_skip(&ctx->gbc, 24); // unused fields
  411. /* Update sizes */
  412. if (avctx->width != w || avctx->height != h) {
  413. av_log(avctx, AV_LOG_DEBUG, "Size update %dx%d -> %d%d.\n",
  414. avctx->width, avctx->height, ctx->width, ctx->height);
  415. ret = ff_set_dimensions(avctx, w, h);
  416. if (ret < 0)
  417. return ret;
  418. init_refframe = 1;
  419. }
  420. ctx->refframe->width = ctx->width = w;
  421. ctx->refframe->height = ctx->height = h;
  422. /* Allocate the reference frame if not already done or on size change */
  423. if (init_refframe) {
  424. ret = av_frame_get_buffer(ctx->refframe, 32);
  425. if (ret < 0)
  426. return ret;
  427. }
  428. /* Decode all tiles in a frame */
  429. return tdsc_decode_tiles(avctx, number_tiles);
  430. }
  431. static int tdsc_parse_dtsm(AVCodecContext *avctx)
  432. {
  433. TDSCContext *ctx = avctx->priv_data;
  434. int ret;
  435. int action = bytestream2_get_le32(&ctx->gbc);
  436. bytestream2_skip(&ctx->gbc, 4); // some kind of ID or version maybe?
  437. if (action == 2 || action == 3) {
  438. /* Load cursor coordinates */
  439. ctx->cursor_x = bytestream2_get_le32(&ctx->gbc);
  440. ctx->cursor_y = bytestream2_get_le32(&ctx->gbc);
  441. /* Load a full cursor sprite */
  442. if (action == 3) {
  443. ret = tdsc_load_cursor(avctx);
  444. /* Do not consider cursor errors fatal unless in explode mode */
  445. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  446. return ret;
  447. }
  448. } else {
  449. avpriv_request_sample(avctx, "Cursor action %d", action);
  450. }
  451. return 0;
  452. }
  453. static int tdsc_decode_frame(AVCodecContext *avctx, void *data,
  454. int *got_frame, AVPacket *avpkt)
  455. {
  456. TDSCContext *ctx = avctx->priv_data;
  457. AVFrame *frame = data;
  458. int ret, tag_header, keyframe = 0;
  459. uLongf dlen;
  460. /* Resize deflate buffer on resolution change */
  461. if (ctx->width != avctx->width || ctx->height != avctx->height) {
  462. ctx->deflatelen = avctx->width * avctx->height * (3 + 1);
  463. ret = av_reallocp(&ctx->deflatebuffer, ctx->deflatelen);
  464. if (ret < 0)
  465. return ret;
  466. }
  467. dlen = ctx->deflatelen;
  468. /* Frames are deflated, need to inflate them first */
  469. ret = uncompress(ctx->deflatebuffer, &dlen, avpkt->data, avpkt->size);
  470. if (ret) {
  471. av_log(avctx, AV_LOG_ERROR, "Deflate error %d.\n", ret);
  472. return AVERROR_UNKNOWN;
  473. }
  474. bytestream2_init(&ctx->gbc, ctx->deflatebuffer, dlen);
  475. /* Check for tag and for size info */
  476. if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
  477. av_log(avctx, AV_LOG_ERROR, "Frame is too small.\n");
  478. return AVERROR_INVALIDDATA;
  479. }
  480. /* Read tag */
  481. tag_header = bytestream2_get_le32(&ctx->gbc);
  482. if (tag_header == MKTAG('T','D','S','F')) {
  483. int number_tiles;
  484. if (bytestream2_get_bytes_left(&ctx->gbc) < TDSF_HEADER_SIZE) {
  485. av_log(avctx, AV_LOG_ERROR, "TDSF tag is too small.\n");
  486. return AVERROR_INVALIDDATA;
  487. }
  488. /* First 4 bytes here are the number of GEPJ/WAR tiles in this frame */
  489. number_tiles = bytestream2_get_le32(&ctx->gbc);
  490. bytestream2_skip(&ctx->gbc, 4); // internal timestamp maybe?
  491. keyframe = bytestream2_get_le32(&ctx->gbc) == 0x30;
  492. ret = tdsc_parse_tdsf(avctx, number_tiles);
  493. if (ret < 0)
  494. return ret;
  495. /* Check if there is anything else we are able to parse */
  496. if (bytestream2_get_bytes_left(&ctx->gbc) >= 4 + 4)
  497. tag_header = bytestream2_get_le32(&ctx->gbc);
  498. }
  499. /* This tag can be after a TDSF block or on its own frame */
  500. if (tag_header == MKTAG('D','T','S','M')) {
  501. /* First 4 bytes here are the total size in bytes for this frame */
  502. int tag_size = bytestream2_get_le32(&ctx->gbc);
  503. if (bytestream2_get_bytes_left(&ctx->gbc) < tag_size) {
  504. av_log(avctx, AV_LOG_ERROR, "DTSM tag is too small.\n");
  505. return AVERROR_INVALIDDATA;
  506. }
  507. ret = tdsc_parse_dtsm(avctx);
  508. if (ret < 0)
  509. return ret;
  510. }
  511. /* Get the output frame and copy the reference frame */
  512. ret = ff_get_buffer(avctx, frame, 0);
  513. if (ret < 0)
  514. return ret;
  515. ret = av_frame_copy(frame, ctx->refframe);
  516. if (ret < 0)
  517. return ret;
  518. /* Paint the cursor on the output frame */
  519. tdsc_paint_cursor(avctx, frame->data[0], frame->linesize[0]);
  520. /* Frame is ready to be output */
  521. if (keyframe) {
  522. frame->pict_type = AV_PICTURE_TYPE_I;
  523. frame->key_frame = 1;
  524. } else {
  525. frame->pict_type = AV_PICTURE_TYPE_P;
  526. }
  527. *got_frame = 1;
  528. return 0;
  529. }
  530. AVCodec ff_tdsc_decoder = {
  531. .name = "tdsc",
  532. .long_name = NULL_IF_CONFIG_SMALL("TDSC"),
  533. .type = AVMEDIA_TYPE_VIDEO,
  534. .id = AV_CODEC_ID_TDSC,
  535. .init = tdsc_init,
  536. .decode = tdsc_decode_frame,
  537. .close = tdsc_close,
  538. .priv_data_size = sizeof(TDSCContext),
  539. .capabilities = AV_CODEC_CAP_DR1,
  540. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  541. FF_CODEC_CAP_INIT_CLEANUP,
  542. };