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.

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