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.

193 lines
5.8KB

  1. /*
  2. * MatchWare Screen Capture Codec decoder
  3. *
  4. * Copyright (c) 2018 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 MWSCContext {
  30. unsigned int decomp_size;
  31. uint8_t *decomp_buf;
  32. z_stream zstream;
  33. AVFrame *prev_frame;
  34. } MWSCContext;
  35. static int rle_uncompress(GetByteContext *gb, PutByteContext *pb, GetByteContext *gbp,
  36. int width, int height, int stride, int pb_linesize, int gbp_linesize)
  37. {
  38. int intra = 1, w = 0;
  39. bytestream2_seek_p(pb, (height - 1) * pb_linesize, SEEK_SET);
  40. while (bytestream2_get_bytes_left(gb) > 0) {
  41. uint32_t fill = bytestream2_get_le24(gb);
  42. unsigned run = bytestream2_get_byte(gb);
  43. if (run == 0) {
  44. run = bytestream2_get_le32(gb);
  45. for (int j = 0; j < run; j++, w++) {
  46. if (w == width) {
  47. w = 0;
  48. bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
  49. }
  50. bytestream2_put_le24(pb, fill);
  51. }
  52. } else if (run == 255) {
  53. int pos = bytestream2_tell_p(pb);
  54. bytestream2_seek(gbp, pos, SEEK_SET);
  55. for (int j = 0; j < fill; j++, w++) {
  56. if (w == width) {
  57. w = 0;
  58. bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
  59. bytestream2_seek(gbp, -(gbp_linesize + stride), SEEK_CUR);
  60. }
  61. bytestream2_put_le24(pb, bytestream2_get_le24(gbp));
  62. }
  63. intra = 0;
  64. } else {
  65. for (int j = 0; j < run; j++, w++) {
  66. if (w == width) {
  67. w = 0;
  68. bytestream2_seek_p(pb, -(pb_linesize + stride), SEEK_CUR);
  69. }
  70. bytestream2_put_le24(pb, fill);
  71. }
  72. }
  73. }
  74. return intra;
  75. }
  76. static int decode_frame(AVCodecContext *avctx,
  77. void *data, int *got_frame,
  78. AVPacket *avpkt)
  79. {
  80. MWSCContext *s = avctx->priv_data;
  81. AVFrame *frame = data;
  82. uint8_t *buf = avpkt->data;
  83. int buf_size = avpkt->size;
  84. GetByteContext gb;
  85. GetByteContext gbp;
  86. PutByteContext pb;
  87. int ret;
  88. ret = inflateReset(&s->zstream);
  89. if (ret != Z_OK) {
  90. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
  91. return AVERROR_EXTERNAL;
  92. }
  93. s->zstream.next_in = buf;
  94. s->zstream.avail_in = buf_size;
  95. s->zstream.next_out = s->decomp_buf;
  96. s->zstream.avail_out = s->decomp_size;
  97. ret = inflate(&s->zstream, Z_FINISH);
  98. if (ret != Z_STREAM_END) {
  99. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
  100. return AVERROR_EXTERNAL;
  101. }
  102. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  103. return ret;
  104. bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
  105. bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
  106. bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
  107. frame->key_frame = rle_uncompress(&gb, &pb, &gbp, avctx->width, avctx->height, avctx->width * 3,
  108. frame->linesize[0], s->prev_frame->linesize[0]);
  109. frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  110. av_frame_unref(s->prev_frame);
  111. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  112. return ret;
  113. *got_frame = 1;
  114. return avpkt->size;
  115. }
  116. static av_cold int decode_init(AVCodecContext *avctx)
  117. {
  118. MWSCContext *s = avctx->priv_data;
  119. int64_t size;
  120. int zret;
  121. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  122. size = 32LL * avctx->height * avctx->width;
  123. if (size >= INT32_MAX)
  124. return AVERROR_INVALIDDATA;
  125. s->decomp_size = size;
  126. if (!(s->decomp_buf = av_malloc(s->decomp_size)))
  127. return AVERROR(ENOMEM);
  128. s->zstream.zalloc = Z_NULL;
  129. s->zstream.zfree = Z_NULL;
  130. s->zstream.opaque = Z_NULL;
  131. zret = inflateInit(&s->zstream);
  132. if (zret != Z_OK) {
  133. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  134. return AVERROR_EXTERNAL;
  135. }
  136. s->prev_frame = av_frame_alloc();
  137. if (!s->prev_frame)
  138. return AVERROR(ENOMEM);
  139. return 0;
  140. }
  141. static av_cold int decode_close(AVCodecContext *avctx)
  142. {
  143. MWSCContext *s = avctx->priv_data;
  144. av_frame_free(&s->prev_frame);
  145. av_freep(&s->decomp_buf);
  146. s->decomp_size = 0;
  147. inflateEnd(&s->zstream);
  148. return 0;
  149. }
  150. AVCodec ff_mwsc_decoder = {
  151. .name = "mwsc",
  152. .long_name = NULL_IF_CONFIG_SMALL("MatchWare Screen Capture Codec"),
  153. .type = AVMEDIA_TYPE_VIDEO,
  154. .id = AV_CODEC_ID_MWSC,
  155. .priv_data_size = sizeof(MWSCContext),
  156. .init = decode_init,
  157. .close = decode_close,
  158. .decode = decode_frame,
  159. .capabilities = AV_CODEC_CAP_DR1,
  160. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  161. FF_CODEC_CAP_INIT_CLEANUP,
  162. };