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.

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