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.

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