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.

362 lines
12KB

  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: PAL8, BGRA, BGR24, RGB555
  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_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. if (tiles_nb == 0) {
  138. av_log(avctx, AV_LOG_DEBUG, "no tiles\n");
  139. return avpkt->size;
  140. }
  141. av_fast_malloc(&ctx->tiles, &ctx->tiles_size,
  142. tiles_nb * sizeof(*ctx->tiles));
  143. if (!ctx->tiles) {
  144. ret = AVERROR(ENOMEM);
  145. goto end;
  146. }
  147. av_log(avctx, AV_LOG_DEBUG, "Frame with %d tiles.\n", tiles_nb);
  148. /* When there are more than 5 tiles, they are packed together with
  149. * a size header. When that size does not match the number of tiles
  150. * times the tile size, it means it needs to be inflated as well */
  151. if (tiles_nb > 5) {
  152. uLongf packed_tiles_size;
  153. if (tiles_nb < 32)
  154. packed_tiles_size = bytestream2_get_byte(gbc);
  155. else
  156. packed_tiles_size = bytestream2_get_le16(gbc);
  157. ff_dlog(avctx, "packed tiles of size %lu.\n", packed_tiles_size);
  158. /* If necessary, uncompress tiles, and hijack the bytestream reader */
  159. if (packed_tiles_size != tiles_nb * TILE_SIZE) {
  160. uLongf length = tiles_nb * TILE_SIZE;
  161. inflated_tiles = av_malloc(length);
  162. if (!inflated_tiles) {
  163. ret = AVERROR(ENOMEM);
  164. goto end;
  165. }
  166. ret = uncompress(inflated_tiles, &length,
  167. gbc->buffer, packed_tiles_size);
  168. if (ret) {
  169. av_log(avctx, AV_LOG_ERROR, "Tile deflate error %d.\n", ret);
  170. ret = AVERROR_UNKNOWN;
  171. goto end;
  172. }
  173. /* Skip the compressed tile section in the main byte reader,
  174. * and point it to read the newly uncompressed data */
  175. bytestream2_skip(gbc, packed_tiles_size);
  176. bytestream2_init(&tiles_gbc, inflated_tiles, length);
  177. gbc = &tiles_gbc;
  178. }
  179. }
  180. /* Fill in array of tiles, keeping track of how many pixels are updated */
  181. for (i = 0; i < tiles_nb; i++) {
  182. ctx->tiles[i].x = bytestream2_get_le16(gbc);
  183. ctx->tiles[i].w = bytestream2_get_le16(gbc);
  184. ctx->tiles[i].y = bytestream2_get_le16(gbc);
  185. ctx->tiles[i].h = bytestream2_get_le16(gbc);
  186. if (pixel_size + ctx->tiles[i].w * (int64_t)ctx->tiles[i].h * ctx->component_size > INT_MAX) {
  187. av_log(avctx, AV_LOG_ERROR, "Invalid tile dimensions\n");
  188. ret = AVERROR_INVALIDDATA;
  189. goto end;
  190. }
  191. pixel_size += ctx->tiles[i].w * ctx->tiles[i].h * ctx->component_size;
  192. ff_dlog(avctx, "tile %d orig(%d,%d) %dx%d.\n", i,
  193. ctx->tiles[i].x, ctx->tiles[i].y,
  194. ctx->tiles[i].w, ctx->tiles[i].h);
  195. if (ctx->tiles[i].w == 0 || ctx->tiles[i].h == 0) {
  196. av_log(avctx, AV_LOG_ERROR,
  197. "invalid tile %d at (%d.%d) with size %dx%d.\n", i,
  198. ctx->tiles[i].x, ctx->tiles[i].y,
  199. ctx->tiles[i].w, ctx->tiles[i].h);
  200. ret = AVERROR_INVALIDDATA;
  201. goto end;
  202. } else if (ctx->tiles[i].x + ctx->tiles[i].w > avctx->width ||
  203. ctx->tiles[i].y + ctx->tiles[i].h > avctx->height) {
  204. av_log(avctx, AV_LOG_ERROR,
  205. "out of bounds tile %d at (%d.%d) with size %dx%d.\n", i,
  206. ctx->tiles[i].x, ctx->tiles[i].y,
  207. ctx->tiles[i].w, ctx->tiles[i].h);
  208. ret = AVERROR_INVALIDDATA;
  209. goto end;
  210. }
  211. }
  212. /* Reset the reader in case it had been modified before */
  213. gbc = &ctx->gbc;
  214. /* Extract how much pixel data the tiles contain */
  215. if (pixel_size < 0x100)
  216. packed_size = bytestream2_get_byte(gbc);
  217. else if (pixel_size < 0x10000)
  218. packed_size = bytestream2_get_le16(gbc);
  219. else if (pixel_size < 0x1000000)
  220. packed_size = bytestream2_get_le24(gbc);
  221. else
  222. packed_size = bytestream2_get_le32(gbc);
  223. ff_dlog(avctx, "pixel_size %d packed_size %d.\n", pixel_size, packed_size);
  224. if (packed_size < 0) {
  225. av_log(avctx, AV_LOG_ERROR, "Invalid tile size %d\n", packed_size);
  226. ret = AVERROR_INVALIDDATA;
  227. goto end;
  228. }
  229. /* Get pixels buffer, it may be deflated or just raw */
  230. if (pixel_size == packed_size) {
  231. if (bytestream2_get_bytes_left(gbc) < pixel_size) {
  232. av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", pixel_size);
  233. ret = AVERROR_INVALIDDATA;
  234. goto end;
  235. }
  236. pixels = gbc->buffer;
  237. } else {
  238. uLongf len = ctx->inflated_size;
  239. if (bytestream2_get_bytes_left(gbc) < packed_size) {
  240. av_log(avctx, AV_LOG_ERROR, "Insufficient input for %d\n", packed_size);
  241. ret = AVERROR_INVALIDDATA;
  242. goto end;
  243. }
  244. ret = uncompress(ctx->inflated_buf, &len, gbc->buffer, packed_size);
  245. if (ret) {
  246. av_log(avctx, AV_LOG_ERROR, "Pixel deflate error %d.\n", ret);
  247. ret = AVERROR_UNKNOWN;
  248. goto end;
  249. }
  250. pixels = ctx->inflated_buf;
  251. }
  252. /* Allocate when needed */
  253. ret = ff_reget_buffer(avctx, ctx->reference);
  254. if (ret < 0)
  255. goto end;
  256. /* Pointer to actual pixels, will be updated when data is consumed */
  257. raw = pixels;
  258. for (i = 0; i < tiles_nb; i++) {
  259. uint8_t *dst = ctx->reference->data[0] + ctx->reference->linesize[0] *
  260. (avctx->height - ctx->tiles[i].y - 1) +
  261. ctx->tiles[i].x * ctx->component_size;
  262. av_image_copy_plane(dst, -1 * ctx->reference->linesize[0],
  263. raw, ctx->tiles[i].w * ctx->component_size,
  264. ctx->tiles[i].w * ctx->component_size,
  265. ctx->tiles[i].h);
  266. raw += ctx->tiles[i].w * ctx->component_size * ctx->tiles[i].h;
  267. }
  268. /* Frame is ready to be output */
  269. ret = av_frame_ref(frame, ctx->reference);
  270. if (ret < 0)
  271. goto end;
  272. /* Keyframe when the number of pixels updated matches the whole surface */
  273. if (pixel_size == ctx->inflated_size) {
  274. frame->pict_type = AV_PICTURE_TYPE_I;
  275. frame->key_frame = 1;
  276. } else {
  277. frame->pict_type = AV_PICTURE_TYPE_P;
  278. }
  279. /* Palette handling */
  280. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  281. int size;
  282. const uint8_t *palette = av_packet_get_side_data(avpkt,
  283. AV_PKT_DATA_PALETTE,
  284. &size);
  285. if (palette && size == AVPALETTE_SIZE) {
  286. frame->palette_has_changed = 1;
  287. memcpy(ctx->palette, palette, AVPALETTE_SIZE);
  288. } else if (palette) {
  289. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
  290. }
  291. memcpy (frame->data[1], ctx->palette, AVPALETTE_SIZE);
  292. }
  293. *got_frame = 1;
  294. ret = avpkt->size;
  295. end:
  296. av_free(inflated_tiles);
  297. return ret;
  298. }
  299. AVCodec ff_rscc_decoder = {
  300. .name = "rscc",
  301. .long_name = NULL_IF_CONFIG_SMALL("innoHeim/Rsupport Screen Capture Codec"),
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. .id = AV_CODEC_ID_RSCC,
  304. .init = rscc_init,
  305. .decode = rscc_decode_frame,
  306. .close = rscc_close,
  307. .priv_data_size = sizeof(RsccContext),
  308. .capabilities = AV_CODEC_CAP_DR1,
  309. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  310. FF_CODEC_CAP_INIT_CLEANUP,
  311. };