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.

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