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.

104 lines
3.2KB

  1. /*
  2. * Microsoft Paint (MSP) version 2 decoder
  3. * Copyright (c) 2020 Peter Ross (pross@xvid.org)
  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. * Microsoft Paint (MSP) version 2 decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. static int msp2_decode_frame(AVCodecContext *avctx,
  29. void *data, int *got_frame,
  30. AVPacket *avpkt)
  31. {
  32. const uint8_t *buf = avpkt->data;
  33. int buf_size = avpkt->size;
  34. AVFrame *p = data;
  35. int ret;
  36. unsigned int x, y, width = (avctx->width + 7) / 8;
  37. GetByteContext idx, gb;
  38. if (buf_size <= 2 * avctx->height)
  39. return AVERROR_INVALIDDATA;
  40. avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
  41. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  42. return ret;
  43. p->pict_type = AV_PICTURE_TYPE_I;
  44. p->key_frame = 1;
  45. bytestream2_init(&idx, buf, 2 * avctx->height);
  46. buf += 2 * avctx->height;
  47. buf_size -= 2 * avctx->height;
  48. for (y = 0; y < avctx->height; y++) {
  49. unsigned int pkt_size = bytestream2_get_le16(&idx);
  50. if (!pkt_size) {
  51. memset(p->data[0] + y * p->linesize[0], 0xFF, width);
  52. continue;
  53. }
  54. if (pkt_size > buf_size) {
  55. av_log(avctx, AV_LOG_WARNING, "image probably corrupt\n");
  56. pkt_size = buf_size;
  57. }
  58. bytestream2_init(&gb, buf, pkt_size);
  59. x = 0;
  60. while (bytestream2_get_bytes_left(&gb) && x < width) {
  61. int size = bytestream2_get_byte(&gb);
  62. if (size) {
  63. size = FFMIN(size, bytestream2_get_bytes_left(&gb));
  64. memcpy(p->data[0] + y * p->linesize[0] + x, gb.buffer, FFMIN(size, width - x));
  65. bytestream2_skip(&gb, size);
  66. } else {
  67. int value;
  68. size = bytestream2_get_byte(&gb);
  69. if (!size)
  70. avpriv_request_sample(avctx, "escape value");
  71. value = bytestream2_get_byte(&gb);
  72. memset(p->data[0] + y * p->linesize[0] + x, value, FFMIN(size, width - x));
  73. }
  74. x += size;
  75. }
  76. buf += pkt_size;
  77. buf_size -= pkt_size;
  78. }
  79. *got_frame = 1;
  80. return buf_size;
  81. }
  82. AVCodec ff_msp2_decoder = {
  83. .name = "msp2",
  84. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Paint (MSP) version 2"),
  85. .type = AVMEDIA_TYPE_VIDEO,
  86. .id = AV_CODEC_ID_MSP2,
  87. .decode = msp2_decode_frame,
  88. .capabilities = AV_CODEC_CAP_DR1,
  89. };