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.

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