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.

277 lines
8.3KB

  1. /*
  2. * Mandsoft Screen Capture Codec decoder
  3. *
  4. * Copyright (c) 2017 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. #include <zlib.h>
  29. typedef struct MSCCContext {
  30. unsigned bpp;
  31. unsigned int decomp_size;
  32. uint8_t *decomp_buf;
  33. unsigned int uncomp_size;
  34. uint8_t *uncomp_buf;
  35. z_stream zstream;
  36. uint32_t pal[256];
  37. } MSCCContext;
  38. static int rle_uncompress(AVCodecContext *avctx, GetByteContext *gb, PutByteContext *pb)
  39. {
  40. MSCCContext *s = avctx->priv_data;
  41. unsigned x = 0, y = 0;
  42. while (bytestream2_get_bytes_left(gb) > 0) {
  43. uint32_t fill;
  44. int j;
  45. unsigned run = bytestream2_get_byte(gb);
  46. if (run) {
  47. switch (avctx->bits_per_coded_sample) {
  48. case 8:
  49. fill = bytestream2_get_byte(gb);
  50. break;
  51. case 16:
  52. fill = bytestream2_get_le16(gb);
  53. break;
  54. case 24:
  55. fill = bytestream2_get_le24(gb);
  56. break;
  57. case 32:
  58. fill = bytestream2_get_le32(gb);
  59. break;
  60. }
  61. for (j = 0; j < run; j++) {
  62. switch (avctx->bits_per_coded_sample) {
  63. case 8:
  64. bytestream2_put_byte(pb, fill);
  65. break;
  66. case 16:
  67. bytestream2_put_le16(pb, fill);
  68. break;
  69. case 24:
  70. bytestream2_put_le24(pb, fill);
  71. break;
  72. case 32:
  73. bytestream2_put_le32(pb, fill);
  74. break;
  75. }
  76. }
  77. x += run;
  78. } else {
  79. unsigned copy = bytestream2_get_byte(gb);
  80. if (copy == 0) {
  81. x = 0;
  82. y++;
  83. bytestream2_seek_p(pb, y * avctx->width * s->bpp, SEEK_SET);
  84. } else if (copy == 1) {
  85. return 0;
  86. } else if (copy == 2) {
  87. x += bytestream2_get_byte(gb);
  88. y += bytestream2_get_byte(gb);
  89. bytestream2_seek_p(pb, y * avctx->width * s->bpp + x * s->bpp, SEEK_SET);
  90. } else {
  91. for (j = 0; j < copy; j++) {
  92. switch (avctx->bits_per_coded_sample) {
  93. case 8:
  94. bytestream2_put_byte(pb, bytestream2_get_byte(gb));
  95. break;
  96. case 16:
  97. bytestream2_put_le16(pb, bytestream2_get_le16(gb));
  98. break;
  99. case 24:
  100. bytestream2_put_le24(pb, bytestream2_get_le24(gb));
  101. break;
  102. case 32:
  103. bytestream2_put_le32(pb, bytestream2_get_le32(gb));
  104. break;
  105. }
  106. }
  107. if (s->bpp == 1 && (copy & 1))
  108. bytestream2_skip(gb, 1);
  109. x += copy;
  110. }
  111. }
  112. }
  113. return AVERROR_INVALIDDATA;
  114. }
  115. static int decode_frame(AVCodecContext *avctx,
  116. void *data, int *got_frame,
  117. AVPacket *avpkt)
  118. {
  119. MSCCContext *s = avctx->priv_data;
  120. AVFrame *frame = data;
  121. uint8_t *buf = avpkt->data;
  122. int buf_size = avpkt->size;
  123. GetByteContext gb;
  124. PutByteContext pb;
  125. int ret, j;
  126. if (avpkt->size < 3)
  127. return buf_size;
  128. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  129. return ret;
  130. if (avctx->codec_id == AV_CODEC_ID_MSCC) {
  131. avpkt->data[2] ^= avpkt->data[0];
  132. buf += 2;
  133. buf_size -= 2;
  134. }
  135. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  136. int size;
  137. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
  138. if (pal && size == AVPALETTE_SIZE) {
  139. frame->palette_has_changed = 1;
  140. for (j = 0; j < 256; j++)
  141. s->pal[j] = 0xFF000000 | AV_RL32(pal + j * 4);
  142. } else if (pal) {
  143. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
  144. }
  145. memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
  146. }
  147. ret = inflateReset(&s->zstream);
  148. if (ret != Z_OK) {
  149. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  150. return AVERROR_UNKNOWN;
  151. }
  152. s->zstream.next_in = buf;
  153. s->zstream.avail_in = buf_size;
  154. s->zstream.next_out = s->decomp_buf;
  155. s->zstream.avail_out = s->decomp_size;
  156. ret = inflate(&s->zstream, Z_FINISH);
  157. if (ret != Z_STREAM_END) {
  158. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
  159. return AVERROR_UNKNOWN;
  160. }
  161. bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
  162. bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
  163. ret = rle_uncompress(avctx, &gb, &pb);
  164. if (ret)
  165. return ret;
  166. for (j = 0; j < avctx->height; j++) {
  167. memcpy(frame->data[0] + (avctx->height - j - 1) * frame->linesize[0],
  168. s->uncomp_buf + s->bpp * j * avctx->width, s->bpp * avctx->width);
  169. }
  170. frame->key_frame = 1;
  171. frame->pict_type = AV_PICTURE_TYPE_I;
  172. *got_frame = 1;
  173. return avpkt->size;
  174. }
  175. static av_cold int decode_init(AVCodecContext *avctx)
  176. {
  177. MSCCContext *s = avctx->priv_data;
  178. int stride, zret;
  179. switch (avctx->bits_per_coded_sample) {
  180. case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  181. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  182. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  183. case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
  184. default:
  185. av_log(avctx, AV_LOG_ERROR, "Unsupported bitdepth %i\n", avctx->bits_per_coded_sample);
  186. return AVERROR_INVALIDDATA;
  187. }
  188. s->bpp = avctx->bits_per_coded_sample >> 3;
  189. stride = 4 * ((avctx->width * avctx->bits_per_coded_sample + 31) / 32);
  190. s->decomp_size = 2 * avctx->height * stride;
  191. if (!(s->decomp_buf = av_malloc(s->decomp_size)))
  192. return AVERROR(ENOMEM);
  193. s->uncomp_size = avctx->height * stride;
  194. if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
  195. return AVERROR(ENOMEM);
  196. s->zstream.zalloc = Z_NULL;
  197. s->zstream.zfree = Z_NULL;
  198. s->zstream.opaque = Z_NULL;
  199. zret = inflateInit(&s->zstream);
  200. if (zret != Z_OK) {
  201. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  202. return AVERROR_UNKNOWN;
  203. }
  204. return 0;
  205. }
  206. static av_cold int decode_close(AVCodecContext *avctx)
  207. {
  208. MSCCContext *s = avctx->priv_data;
  209. av_freep(&s->decomp_buf);
  210. s->decomp_size = 0;
  211. av_freep(&s->uncomp_buf);
  212. s->uncomp_size = 0;
  213. inflateEnd(&s->zstream);
  214. return 0;
  215. }
  216. AVCodec ff_mscc_decoder = {
  217. .name = "mscc",
  218. .long_name = NULL_IF_CONFIG_SMALL("Mandsoft Screen Capture Codec"),
  219. .type = AVMEDIA_TYPE_VIDEO,
  220. .id = AV_CODEC_ID_MSCC,
  221. .priv_data_size = sizeof(MSCCContext),
  222. .init = decode_init,
  223. .close = decode_close,
  224. .decode = decode_frame,
  225. .capabilities = AV_CODEC_CAP_DR1,
  226. };
  227. AVCodec ff_srgc_decoder = {
  228. .name = "srgc",
  229. .long_name = NULL_IF_CONFIG_SMALL("Screen Recorder Gold Codec"),
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .id = AV_CODEC_ID_SRGC,
  232. .priv_data_size = sizeof(MSCCContext),
  233. .init = decode_init,
  234. .close = decode_close,
  235. .decode = decode_frame,
  236. .capabilities = AV_CODEC_CAP_DR1,
  237. };