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.

345 lines
11KB

  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, BGR24, RGB555, PAL8
  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. int component_size;
  54. uint32_t pal[AVPALETTE_COUNT];
  55. /* zlib interaction */
  56. uint8_t *inflated_buf;
  57. uLongf inflated_size;
  58. } RsccContext;
  59. static av_cold int rscc_init(AVCodecContext *avctx)
  60. {
  61. RsccContext *ctx = avctx->priv_data;
  62. /* These needs to be set to estimate uncompressed buffer */
  63. int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  64. if (ret < 0) {
  65. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
  66. avctx->width, avctx->height);
  67. return ret;
  68. }
  69. /* Allocate reference frame */
  70. ctx->reference = av_frame_alloc();
  71. if (!ctx->reference)
  72. return AVERROR(ENOMEM);
  73. /* Get pixel format and the size of the pixel */
  74. if (avctx->codec_tag == MKTAG('I', 'S', 'C', 'C')) {
  75. avctx->pix_fmt = AV_PIX_FMT_BGRA;
  76. ctx->component_size = 4;
  77. } else if (avctx->codec_tag == MKTAG('R', 'S', 'C', 'C')) {
  78. ctx->component_size = avctx->bits_per_coded_sample / 8;
  79. switch (avctx->bits_per_coded_sample) {
  80. case 8:
  81. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  82. break;
  83. case 16:
  84. avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
  85. break;
  86. case 24:
  87. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  88. break;
  89. case 32:
  90. avctx->pix_fmt = AV_PIX_FMT_BGR0;
  91. break;
  92. default:
  93. av_log(avctx, AV_LOG_ERROR, "Invalid bits per pixel value (%d)\n",
  94. avctx->bits_per_coded_sample);
  95. return AVERROR_INVALIDDATA;
  96. }
  97. } else {
  98. avctx->pix_fmt = AV_PIX_FMT_BGR0;
  99. ctx->component_size = 4;
  100. av_log(avctx, AV_LOG_WARNING, "Invalid codec tag\n");
  101. }
  102. /* Store the value to check for keyframes */
  103. ctx->inflated_size = avctx->width * avctx->height * ctx->component_size;
  104. /* Allocate maximum size possible, a full frame */
  105. ctx->inflated_buf = av_malloc(ctx->inflated_size);
  106. if (!ctx->inflated_buf)
  107. return AVERROR(ENOMEM);
  108. return 0;
  109. }
  110. static av_cold int rscc_close(AVCodecContext *avctx)
  111. {
  112. RsccContext *ctx = avctx->priv_data;
  113. av_freep(&ctx->tiles);
  114. av_freep(&ctx->inflated_buf);
  115. av_frame_free(&ctx->reference);
  116. return 0;
  117. }
  118. static int rscc_decode_frame(AVCodecContext *avctx, void *data,
  119. int *got_frame, AVPacket *avpkt)
  120. {
  121. RsccContext *ctx = avctx->priv_data;
  122. GetByteContext *gbc = &ctx->gbc;
  123. GetByteContext tiles_gbc;
  124. AVFrame *frame = data;
  125. const uint8_t *pixels, *raw;
  126. uint8_t *inflated_tiles = NULL;
  127. int tiles_nb, packed_size, pixel_size = 0;
  128. int i, ret = 0;
  129. bytestream2_init(gbc, avpkt->data, avpkt->size);
  130. /* Size check */
  131. if (bytestream2_get_bytes_left(gbc) < 12) {
  132. av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
  133. return AVERROR_INVALIDDATA;
  134. }
  135. /* Read number of tiles, and allocate the array */
  136. tiles_nb = bytestream2_get_le16(gbc);
  137. av_fast_malloc(&ctx->tiles, &ctx->tiles_size,
  138. tiles_nb * sizeof(*ctx->tiles));
  139. if (!ctx->tiles) {
  140. ret = AVERROR(ENOMEM);
  141. goto end;
  142. }
  143. av_log(avctx, AV_LOG_DEBUG, "Frame with %d tiles.\n", tiles_nb);
  144. /* When there are more than 5 tiles, they are packed together with
  145. * a size header. When that size does not match the number of tiles
  146. * times the tile size, it means it needs to be inflated as well */
  147. if (tiles_nb > 5) {
  148. uLongf packed_tiles_size;
  149. if (tiles_nb < 32)
  150. packed_tiles_size = bytestream2_get_byte(gbc);
  151. else
  152. packed_tiles_size = bytestream2_get_le16(gbc);
  153. ff_dlog(avctx, "packed tiles of size %lu.\n", packed_tiles_size);
  154. /* If necessary, uncompress tiles, and hijack the bytestream reader */
  155. if (packed_tiles_size != tiles_nb * TILE_SIZE) {
  156. uLongf length = tiles_nb * TILE_SIZE;
  157. inflated_tiles = av_malloc(length);
  158. if (!inflated_tiles) {
  159. ret = AVERROR(ENOMEM);
  160. goto end;
  161. }
  162. ret = uncompress(inflated_tiles, &length,
  163. gbc->buffer, packed_tiles_size);
  164. if (ret) {
  165. av_log(avctx, AV_LOG_ERROR, "Tile deflate error %d.\n", ret);
  166. ret = AVERROR_UNKNOWN;
  167. goto end;
  168. }
  169. /* Skip the compressed tile section in the main byte reader,
  170. * and point it to read the newly uncompressed data */
  171. bytestream2_skip(gbc, packed_tiles_size);
  172. bytestream2_init(&tiles_gbc, inflated_tiles, length);
  173. gbc = &tiles_gbc;
  174. }
  175. }
  176. /* Fill in array of tiles, keeping track of how many pixels are updated */
  177. for (i = 0; i < tiles_nb; i++) {
  178. ctx->tiles[i].x = bytestream2_get_le16(gbc);
  179. ctx->tiles[i].w = bytestream2_get_le16(gbc);
  180. ctx->tiles[i].y = bytestream2_get_le16(gbc);
  181. ctx->tiles[i].h = bytestream2_get_le16(gbc);
  182. pixel_size += ctx->tiles[i].w * ctx->tiles[i].h * ctx->component_size;
  183. ff_dlog(avctx, "tile %d orig(%d,%d) %dx%d.\n", i,
  184. ctx->tiles[i].x, ctx->tiles[i].y,
  185. ctx->tiles[i].w, ctx->tiles[i].h);
  186. if (ctx->tiles[i].w == 0 || ctx->tiles[i].h == 0) {
  187. av_log(avctx, AV_LOG_ERROR,
  188. "invalid tile %d at (%d.%d) with size %dx%d.\n", i,
  189. ctx->tiles[i].x, ctx->tiles[i].y,
  190. ctx->tiles[i].w, ctx->tiles[i].h);
  191. ret = AVERROR_INVALIDDATA;
  192. goto end;
  193. } else if (ctx->tiles[i].x + ctx->tiles[i].w > avctx->width ||
  194. ctx->tiles[i].y + ctx->tiles[i].h > avctx->height) {
  195. av_log(avctx, AV_LOG_ERROR,
  196. "out of bounds tile %d at (%d.%d) with size %dx%d.\n", i,
  197. ctx->tiles[i].x, ctx->tiles[i].y,
  198. ctx->tiles[i].w, ctx->tiles[i].h);
  199. ret = AVERROR_INVALIDDATA;
  200. goto end;
  201. }
  202. }
  203. /* Reset the reader in case it had been modified before */
  204. gbc = &ctx->gbc;
  205. /* Extract how much pixel data the tiles contain */
  206. if (pixel_size < 0x100)
  207. packed_size = bytestream2_get_byte(gbc);
  208. else if (pixel_size < 0x10000)
  209. packed_size = bytestream2_get_le16(gbc);
  210. else if (pixel_size < 0x1000000)
  211. packed_size = bytestream2_get_le24(gbc);
  212. else
  213. packed_size = bytestream2_get_le32(gbc);
  214. ff_dlog(avctx, "pixel_size %d packed_size %d.\n", pixel_size, packed_size);
  215. if (packed_size < 0) {
  216. av_log(avctx, AV_LOG_ERROR, "Invalid tile size %d\n", packed_size);
  217. ret = AVERROR_INVALIDDATA;
  218. goto end;
  219. }
  220. /* Get pixels buffer, it may be deflated or just raw */
  221. if (pixel_size == packed_size) {
  222. if (bytestream2_get_bytes_left(gbc) < pixel_size) {
  223. av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", pixel_size);
  224. ret = AVERROR_INVALIDDATA;
  225. goto end;
  226. }
  227. pixels = gbc->buffer;
  228. } else {
  229. uLongf len = ctx->inflated_size;
  230. if (bytestream2_get_bytes_left(gbc) < packed_size) {
  231. av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", packed_size);
  232. ret = AVERROR_INVALIDDATA;
  233. goto end;
  234. }
  235. ret = uncompress(ctx->inflated_buf, &len, gbc->buffer, packed_size);
  236. if (ret) {
  237. av_log(avctx, AV_LOG_ERROR, "Pixel deflate error %d.\n", ret);
  238. ret = AVERROR_UNKNOWN;
  239. goto end;
  240. }
  241. pixels = ctx->inflated_buf;
  242. }
  243. /* Allocate when needed */
  244. ret = ff_reget_buffer(avctx, ctx->reference);
  245. if (ret < 0)
  246. goto end;
  247. /* Pointer to actual pixels, will be updated when data is consumed */
  248. raw = pixels;
  249. for (i = 0; i < tiles_nb; i++) {
  250. uint8_t *dst = ctx->reference->data[0] + ctx->reference->linesize[0] *
  251. (avctx->height - ctx->tiles[i].y - 1) +
  252. ctx->tiles[i].x * ctx->component_size;
  253. av_image_copy_plane(dst, -1 * ctx->reference->linesize[0],
  254. raw, ctx->tiles[i].w * ctx->component_size,
  255. ctx->tiles[i].w * ctx->component_size,
  256. ctx->tiles[i].h);
  257. raw += ctx->tiles[i].w * ctx->component_size * ctx->tiles[i].h;
  258. }
  259. /* Frame is ready to be output */
  260. ret = av_frame_ref(frame, ctx->reference);
  261. if (ret < 0)
  262. goto end;
  263. /* Keyframe when the number of pixels updated matches the whole surface */
  264. if (pixel_size == ctx->inflated_size) {
  265. frame->pict_type = AV_PICTURE_TYPE_I;
  266. frame->key_frame = 1;
  267. } else {
  268. frame->pict_type = AV_PICTURE_TYPE_P;
  269. }
  270. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  271. int size;
  272. const uint8_t *pal = av_packet_get_side_data(avpkt,
  273. AV_PKT_DATA_PALETTE,
  274. &size);
  275. if (pal && size == AV_PKT_DATA_PALETTE) {
  276. frame->palette_has_changed = 1;
  277. memcpy(ctx->pal, pal, AVPALETTE_SIZE);
  278. } else if (pal) {
  279. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
  280. }
  281. memcpy (frame->data[1], ctx->pal, AVPALETTE_SIZE);
  282. }
  283. *got_frame = 1;
  284. end:
  285. av_free(inflated_tiles);
  286. return ret;
  287. }
  288. AVCodec ff_rscc_decoder = {
  289. .name = "rscc",
  290. .long_name = NULL_IF_CONFIG_SMALL("innoHeim/Rsupport Screen Capture Codec"),
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .id = AV_CODEC_ID_RSCC,
  293. .init = rscc_init,
  294. .decode = rscc_decode_frame,
  295. .close = rscc_close,
  296. .priv_data_size = sizeof(RsccContext),
  297. .capabilities = AV_CODEC_CAP_DR1,
  298. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  299. FF_CODEC_CAP_INIT_CLEANUP,
  300. };