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.

138 lines
3.7KB

  1. /*
  2. * XBM image format
  3. *
  4. * Copyright (c) 2012 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 "libavutil/avstring.h"
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include "mathops.h"
  26. static int convert(uint8_t x)
  27. {
  28. if (x >= 'a')
  29. x -= 87;
  30. else if (x >= 'A')
  31. x -= 55;
  32. else
  33. x -= '0';
  34. return x;
  35. }
  36. static int parse_str_int(const uint8_t *p, int len, const uint8_t *key)
  37. {
  38. const uint8_t *end = p + len;
  39. for(; p<end - strlen(key); p++) {
  40. if (!memcmp(p, key, strlen(key)))
  41. break;
  42. }
  43. p += strlen(key);
  44. if (p >= end)
  45. return INT_MIN;
  46. for(; p<end; p++) {
  47. char *eptr;
  48. int64_t ret = strtol(p, &eptr, 10);
  49. if ((const uint8_t *)eptr != p)
  50. return ret;
  51. }
  52. return INT_MIN;
  53. }
  54. static int xbm_decode_frame(AVCodecContext *avctx, void *data,
  55. int *got_frame, AVPacket *avpkt)
  56. {
  57. AVFrame *p = data;
  58. int ret, linesize, i, j;
  59. int width = 0;
  60. int height = 0;
  61. const uint8_t *end, *ptr = avpkt->data;
  62. const uint8_t *next;
  63. uint8_t *dst;
  64. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  65. end = avpkt->data + avpkt->size;
  66. width = parse_str_int(avpkt->data, avpkt->size, "_width");
  67. height = parse_str_int(avpkt->data, avpkt->size, "_height");
  68. if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
  69. return ret;
  70. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  71. return ret;
  72. // goto start of image data
  73. next = memchr(ptr, '{', avpkt->size);
  74. if (!next)
  75. next = memchr(ptr, '(', avpkt->size);
  76. if (!next)
  77. return AVERROR_INVALIDDATA;
  78. ptr = next + 1;
  79. linesize = (avctx->width + 7) / 8;
  80. for (i = 0; i < avctx->height; i++) {
  81. dst = p->data[0] + i * p->linesize[0];
  82. for (j = 0; j < linesize; j++) {
  83. uint8_t val;
  84. while (ptr < end && *ptr != 'x' && *ptr != '$')
  85. ptr++;
  86. ptr ++;
  87. if (ptr < end && av_isxdigit(*ptr)) {
  88. val = convert(*ptr++);
  89. if (av_isxdigit(*ptr))
  90. val = (val << 4) + convert(*ptr++);
  91. *dst++ = ff_reverse[val];
  92. if (av_isxdigit(*ptr) && j+1 < linesize) {
  93. j++;
  94. val = convert(*ptr++);
  95. if (av_isxdigit(*ptr))
  96. val = (val << 4) + convert(*ptr++);
  97. *dst++ = ff_reverse[val];
  98. }
  99. } else {
  100. av_log(avctx, AV_LOG_ERROR,
  101. "Unexpected data at %.8s.\n", ptr);
  102. return AVERROR_INVALIDDATA;
  103. }
  104. }
  105. }
  106. p->key_frame = 1;
  107. p->pict_type = AV_PICTURE_TYPE_I;
  108. *got_frame = 1;
  109. return avpkt->size;
  110. }
  111. AVCodec ff_xbm_decoder = {
  112. .name = "xbm",
  113. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  114. .type = AVMEDIA_TYPE_VIDEO,
  115. .id = AV_CODEC_ID_XBM,
  116. .decode = xbm_decode_frame,
  117. .capabilities = AV_CODEC_CAP_DR1,
  118. };