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.

311 lines
10KB

  1. /*
  2. * innoHeim/Rsupport Screen Capture Codec
  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. * @file
  23. * innoHeim/Rsupport Screen Capture Codec decoder
  24. *
  25. * Fourcc: ISCC, RSCC
  26. *
  27. * Lossless codec, data stored in tiles, with optional deflate compression.
  28. *
  29. * Header contains the number of tiles in a frame with the tile coordinates,
  30. * and it can be deflated or not. Similarly, pixel data comes after the header
  31. * and a variable size value, and it can be deflated or just raw.
  32. *
  33. * Supports: BGRA
  34. */
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include <zlib.h>
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/internal.h"
  40. #include "avcodec.h"
  41. #include "bytestream.h"
  42. #include "internal.h"
  43. #define TILE_SIZE 8
  44. typedef struct Tile {
  45. int x, y;
  46. int w, h;
  47. } Tile;
  48. typedef struct RsccContext {
  49. GetByteContext gbc;
  50. AVFrame *reference;
  51. Tile *tiles;
  52. unsigned int tiles_size;
  53. /* zlib interaction */
  54. uint8_t *inflated_buf;
  55. uLongf inflated_size;
  56. } RsccContext;
  57. static av_cold int rscc_init(AVCodecContext *avctx)
  58. {
  59. RsccContext *ctx = avctx->priv_data;
  60. /* These needs to be set to estimate uncompressed buffer */
  61. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  62. if (ret < 0) {
  63. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  64. avctx->width, avctx->height);
  65. return ret;
  66. }
  67. /* Allocate reference frame */
  68. ctx->reference = av_frame_alloc();
  69. if (!ctx->reference)
  70. return AVERROR(ENOMEM);
  71. if (avctx->codec_tag == MKTAG('I','S','C','C')) {
  72. avctx->pix_fmt = AV_PIX_FMT_BGRA;
  73. } else {
  74. avctx->pix_fmt = AV_PIX_FMT_BGR0;
  75. }
  76. /* Store the value to check for keyframes */
  77. ctx->inflated_size = avctx->width * avctx->height * 4;
  78. /* Allocate maximum size possible, a full frame */
  79. ctx->inflated_buf = av_malloc(ctx->inflated_size);
  80. if (!ctx->inflated_buf)
  81. return AVERROR(ENOMEM);
  82. return 0;
  83. }
  84. static av_cold int rscc_close(AVCodecContext *avctx)
  85. {
  86. RsccContext *ctx = avctx->priv_data;
  87. av_freep(&ctx->tiles);
  88. av_freep(&ctx->inflated_buf);
  89. av_frame_free(&ctx->reference);
  90. return 0;
  91. }
  92. static int rscc_decode_frame(AVCodecContext *avctx, void *data,
  93. int *got_frame, AVPacket *avpkt)
  94. {
  95. RsccContext *ctx = avctx->priv_data;
  96. GetByteContext *gbc = &ctx->gbc;
  97. GetByteContext tiles_gbc;
  98. AVFrame *frame = data;
  99. const uint8_t *pixels, *raw;
  100. uint8_t *inflated_tiles = NULL;
  101. int tiles_nb, packed_size, pixel_size = 0;
  102. int i, ret = 0;
  103. bytestream2_init(gbc, avpkt->data, avpkt->size);
  104. /* Size check */
  105. if (bytestream2_get_bytes_left(gbc) < 12) {
  106. av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
  107. return AVERROR_INVALIDDATA;
  108. }
  109. /* Read number of tiles, and allocate the array */
  110. tiles_nb = bytestream2_get_le16(gbc);
  111. av_fast_malloc(&ctx->tiles, &ctx->tiles_size,
  112. tiles_nb * sizeof(*ctx->tiles));
  113. if (!ctx->tiles) {
  114. ret = AVERROR(ENOMEM);
  115. goto end;
  116. }
  117. av_log(avctx, AV_LOG_DEBUG, "Frame with %d tiles.\n", tiles_nb);
  118. /* When there are more than 5 tiles, they are packed together with
  119. * a size header. When that size does not match the number of tiles
  120. * times the tile size, it means it needs to be inflated as well */
  121. if (tiles_nb > 5) {
  122. uLongf packed_tiles_size;
  123. if (tiles_nb < 32)
  124. packed_tiles_size = bytestream2_get_byte(gbc);
  125. else
  126. packed_tiles_size = bytestream2_get_le16(gbc);
  127. ff_dlog(avctx, "packed tiles of size %lu.\n", packed_tiles_size);
  128. /* If necessary, uncompress tiles, and hijack the bytestream reader */
  129. if (packed_tiles_size != tiles_nb * TILE_SIZE) {
  130. uLongf length = tiles_nb * TILE_SIZE;
  131. inflated_tiles = av_malloc(length);
  132. if (!inflated_tiles) {
  133. ret = AVERROR(ENOMEM);
  134. goto end;
  135. }
  136. ret = uncompress(inflated_tiles, &length,
  137. gbc->buffer, packed_tiles_size);
  138. if (ret) {
  139. av_log(avctx, AV_LOG_ERROR, "Tile deflate error %d.\n", ret);
  140. ret = AVERROR_UNKNOWN;
  141. goto end;
  142. }
  143. /* Skip the compressed tile section in the main byte reader,
  144. * and point it to read the newly uncompressed data */
  145. bytestream2_skip(gbc, packed_tiles_size);
  146. bytestream2_init(&tiles_gbc, inflated_tiles, length);
  147. gbc = &tiles_gbc;
  148. }
  149. }
  150. /* Fill in array of tiles, keeping track of how many pixels are updated */
  151. for (i = 0; i < tiles_nb; i++) {
  152. ctx->tiles[i].x = bytestream2_get_le16(gbc);
  153. ctx->tiles[i].w = bytestream2_get_le16(gbc);
  154. ctx->tiles[i].y = bytestream2_get_le16(gbc);
  155. ctx->tiles[i].h = bytestream2_get_le16(gbc);
  156. if (pixel_size + ctx->tiles[i].w * (int64_t)ctx->tiles[i].h * 4 > INT_MAX) {
  157. av_log(avctx, AV_LOG_ERROR, "Invalid tile dimensions\n");
  158. ret = AVERROR_INVALIDDATA;
  159. goto end;
  160. }
  161. pixel_size += ctx->tiles[i].w * ctx->tiles[i].h * 4;
  162. ff_dlog(avctx, "tile %d orig(%d,%d) %dx%d.\n", i,
  163. ctx->tiles[i].x, ctx->tiles[i].y,
  164. ctx->tiles[i].w, ctx->tiles[i].h);
  165. if (ctx->tiles[i].w == 0 || ctx->tiles[i].h == 0) {
  166. av_log(avctx, AV_LOG_ERROR,
  167. "invalid tile %d at (%d.%d) with size %dx%d.\n", i,
  168. ctx->tiles[i].x, ctx->tiles[i].y,
  169. ctx->tiles[i].w, ctx->tiles[i].h);
  170. ret = AVERROR_INVALIDDATA;
  171. goto end;
  172. } else if (ctx->tiles[i].x + ctx->tiles[i].w > avctx->width ||
  173. ctx->tiles[i].y + ctx->tiles[i].h > avctx->height) {
  174. av_log(avctx, AV_LOG_ERROR,
  175. "out of bounds tile %d at (%d.%d) with size %dx%d.\n", i,
  176. ctx->tiles[i].x, ctx->tiles[i].y,
  177. ctx->tiles[i].w, ctx->tiles[i].h);
  178. ret = AVERROR_INVALIDDATA;
  179. goto end;
  180. }
  181. }
  182. /* Reset the reader in case it had been modified before */
  183. gbc = &ctx->gbc;
  184. /* Extract how much pixel data the tiles contain */
  185. if (pixel_size < 0x100)
  186. packed_size = bytestream2_get_byte(gbc);
  187. else if (pixel_size < 0x10000)
  188. packed_size = bytestream2_get_le16(gbc);
  189. else if (pixel_size < 0x1000000)
  190. packed_size = bytestream2_get_le24(gbc);
  191. else
  192. packed_size = bytestream2_get_le32(gbc);
  193. ff_dlog(avctx, "pixel_size %d packed_size %d.\n", pixel_size, packed_size);
  194. if (packed_size < 0) {
  195. av_log(avctx, AV_LOG_ERROR, "Invalid tile size %d\n", packed_size);
  196. ret = AVERROR_INVALIDDATA;
  197. goto end;
  198. }
  199. /* Get pixels buffer, it may be deflated or just raw */
  200. if (pixel_size == packed_size) {
  201. if (bytestream2_get_bytes_left(gbc) < pixel_size) {
  202. av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", pixel_size);
  203. ret = AVERROR_INVALIDDATA;
  204. goto end;
  205. }
  206. pixels = gbc->buffer;
  207. } else {
  208. uLongf len = ctx->inflated_size;
  209. if (bytestream2_get_bytes_left(gbc) < packed_size) {
  210. av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", packed_size);
  211. ret = AVERROR_INVALIDDATA;
  212. goto end;
  213. }
  214. ret = uncompress(ctx->inflated_buf, &len, gbc->buffer, packed_size);
  215. if (ret) {
  216. av_log(avctx, AV_LOG_ERROR, "Pixel deflate error %d.\n", ret);
  217. ret = AVERROR_UNKNOWN;
  218. goto end;
  219. }
  220. pixels = ctx->inflated_buf;
  221. }
  222. /* Allocate when needed */
  223. ret = ff_reget_buffer(avctx, ctx->reference);
  224. if (ret < 0)
  225. goto end;
  226. /* Pointer to actual pixels, will be updated when data is consumed */
  227. raw = pixels;
  228. for (i = 0; i < tiles_nb; i++) {
  229. uint8_t *dst = ctx->reference->data[0] + ctx->reference->linesize[0] *
  230. (avctx->height - ctx->tiles[i].y - 1) +
  231. ctx->tiles[i].x * 4;
  232. av_image_copy_plane(dst, -1 * ctx->reference->linesize[0],
  233. raw, ctx->tiles[i].w * 4,
  234. ctx->tiles[i].w * 4, ctx->tiles[i].h);
  235. raw += ctx->tiles[i].w * 4 * ctx->tiles[i].h;
  236. }
  237. /* Frame is ready to be output */
  238. ret = av_frame_ref(frame, ctx->reference);
  239. if (ret < 0)
  240. goto end;
  241. /* Keyframe when the number of pixels updated matches the whole surface */
  242. if (pixel_size == ctx->inflated_size) {
  243. frame->pict_type = AV_PICTURE_TYPE_I;
  244. frame->key_frame = 1;
  245. } else {
  246. frame->pict_type = AV_PICTURE_TYPE_P;
  247. }
  248. *got_frame = 1;
  249. end:
  250. av_free(inflated_tiles);
  251. return ret;
  252. }
  253. AVCodec ff_rscc_decoder = {
  254. .name = "rscc",
  255. .long_name = NULL_IF_CONFIG_SMALL("innoHeim/Rsupport Screen Capture Codec"),
  256. .type = AVMEDIA_TYPE_VIDEO,
  257. .id = AV_CODEC_ID_RSCC,
  258. .init = rscc_init,
  259. .decode = rscc_decode_frame,
  260. .close = rscc_close,
  261. .priv_data_size = sizeof(RsccContext),
  262. .capabilities = AV_CODEC_CAP_DR1,
  263. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  264. FF_CODEC_CAP_INIT_CLEANUP,
  265. };