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.

120 lines
3.4KB

  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 "avcodec.h"
  23. #include "internal.h"
  24. #include "mathops.h"
  25. #include "libavutil/avstring.h"
  26. static av_cold int xbm_decode_init(AVCodecContext *avctx)
  27. {
  28. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  29. return 0;
  30. }
  31. static int convert(uint8_t x)
  32. {
  33. if (x >= 'a')
  34. x -= 87;
  35. else if (x >= 'A')
  36. x -= 55;
  37. else
  38. x -= '0';
  39. return x;
  40. }
  41. static int xbm_decode_frame(AVCodecContext *avctx, void *data,
  42. int *got_frame, AVPacket *avpkt)
  43. {
  44. AVFrame *p = data;
  45. const uint8_t *end, *ptr = avpkt->data;
  46. uint8_t *dst;
  47. int ret, linesize, i, j;
  48. end = avpkt->data + avpkt->size;
  49. while (!avctx->width || !avctx->height) {
  50. char name[256];
  51. int number, len;
  52. ptr += strcspn(ptr, "#");
  53. if (sscanf(ptr, "#define %256s %u", name, &number) != 2) {
  54. av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
  55. return AVERROR_INVALIDDATA;
  56. }
  57. len = strlen(name);
  58. if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
  59. avctx->height = number;
  60. } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
  61. avctx->width = number;
  62. } else {
  63. av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
  64. return AVERROR_INVALIDDATA;
  65. }
  66. ptr += strcspn(ptr, "\n\r") + 1;
  67. }
  68. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  69. return ret;
  70. // goto start of image data
  71. ptr += strcspn(ptr, "{") + 1;
  72. linesize = (avctx->width + 7) / 8;
  73. for (i = 0; i < avctx->height; i++) {
  74. dst = p->data[0] + i * p->linesize[0];
  75. for (j = 0; j < linesize; j++) {
  76. uint8_t val;
  77. ptr += strcspn(ptr, "x") + 1;
  78. if (ptr < end && av_isxdigit(*ptr)) {
  79. val = convert(*ptr);
  80. ptr++;
  81. if (av_isxdigit(*ptr))
  82. val = (val << 4) + convert(*ptr);
  83. *dst++ = ff_reverse[val];
  84. } else {
  85. av_log(avctx, AV_LOG_ERROR, "Unexpected data at '%.8s'\n", ptr);
  86. return AVERROR_INVALIDDATA;
  87. }
  88. }
  89. }
  90. p->key_frame = 1;
  91. p->pict_type = AV_PICTURE_TYPE_I;
  92. *got_frame = 1;
  93. return avpkt->size;
  94. }
  95. AVCodec ff_xbm_decoder = {
  96. .name = "xbm",
  97. .type = AVMEDIA_TYPE_VIDEO,
  98. .id = AV_CODEC_ID_XBM,
  99. .init = xbm_decode_init,
  100. .decode = xbm_decode_frame,
  101. .capabilities = CODEC_CAP_DR1,
  102. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  103. };