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.

123 lines
3.5KB

  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 xbm_decode_frame(AVCodecContext *avctx, void *data,
  37. int *got_frame, AVPacket *avpkt)
  38. {
  39. AVFrame *p = data;
  40. int ret, linesize, i, j;
  41. int width = 0;
  42. int height = 0;
  43. const uint8_t *end, *ptr = avpkt->data;
  44. uint8_t *dst;
  45. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  46. end = avpkt->data + avpkt->size;
  47. while (!width || !height) {
  48. char name[256];
  49. int number, len;
  50. ptr += strcspn(ptr, "#");
  51. if (ptr >= avpkt->data + avpkt->size) {
  52. av_log(avctx, AV_LOG_ERROR, "End of file reached.\n");
  53. return AVERROR_INVALIDDATA;
  54. }
  55. if (sscanf(ptr, "#define %255s %u", name, &number) != 2) {
  56. av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
  57. return AVERROR_INVALIDDATA;
  58. }
  59. len = strlen(name);
  60. if ((len > 6) && !height && !memcmp(name + len - 7, "_height", 7)) {
  61. height = number;
  62. } else if ((len > 5) && !width && !memcmp(name + len - 6, "_width", 6)) {
  63. width = number;
  64. } else {
  65. av_log(avctx, AV_LOG_WARNING, "Unknown define '%s'\n", name);
  66. }
  67. ptr += strcspn(ptr, "\n\r") + 1;
  68. }
  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. ptr += strcspn(ptr, "{") + 1;
  75. linesize = (avctx->width + 7) / 8;
  76. for (i = 0; i < avctx->height; i++) {
  77. dst = p->data[0] + i * p->linesize[0];
  78. for (j = 0; j < linesize; j++) {
  79. uint8_t val;
  80. ptr += strcspn(ptr, "x") + 1;
  81. if (ptr < end && av_isxdigit(*ptr)) {
  82. val = convert(*ptr);
  83. ptr++;
  84. if (av_isxdigit(*ptr))
  85. val = (val << 4) + convert(*ptr);
  86. *dst++ = ff_reverse[val];
  87. } else {
  88. av_log(avctx, AV_LOG_ERROR,
  89. "Unexpected data at %.8s.\n", ptr);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. }
  93. }
  94. p->key_frame = 1;
  95. p->pict_type = AV_PICTURE_TYPE_I;
  96. *got_frame = 1;
  97. return avpkt->size;
  98. }
  99. AVCodec ff_xbm_decoder = {
  100. .name = "xbm",
  101. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  102. .type = AVMEDIA_TYPE_VIDEO,
  103. .id = AV_CODEC_ID_XBM,
  104. .decode = xbm_decode_frame,
  105. .capabilities = CODEC_CAP_DR1,
  106. };