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.

128 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 "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. return 0;
  29. }
  30. static int convert(uint8_t x)
  31. {
  32. if (x >= 'a')
  33. x -= 87;
  34. else if (x >= 'A')
  35. x -= 55;
  36. else
  37. x -= '0';
  38. return x;
  39. }
  40. static int xbm_decode_frame(AVCodecContext *avctx, void *data,
  41. int *got_frame, AVPacket *avpkt)
  42. {
  43. AVFrame *p = data;
  44. const uint8_t *end, *ptr = avpkt->data;
  45. uint8_t *dst;
  46. int ret, linesize, i, j;
  47. end = avpkt->data + avpkt->size;
  48. while (!avctx->width || !avctx->height) {
  49. char name[256];
  50. int number, len;
  51. ptr += strcspn(ptr, "#");
  52. if (sscanf(ptr, "#define %256s %u", name, &number) != 2) {
  53. av_log(avctx, AV_LOG_ERROR, "Unexpected preprocessor directive\n");
  54. return AVERROR_INVALIDDATA;
  55. }
  56. len = strlen(name);
  57. if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
  58. avctx->height = number;
  59. } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
  60. avctx->width = number;
  61. } else {
  62. av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
  63. return AVERROR_INVALIDDATA;
  64. }
  65. ptr += strcspn(ptr, "\n\r") + 1;
  66. }
  67. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  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. static av_cold int xbm_decode_close(AVCodecContext *avctx)
  96. {
  97. return 0;
  98. }
  99. AVCodec ff_xbm_decoder = {
  100. .name = "xbm",
  101. .type = AVMEDIA_TYPE_VIDEO,
  102. .id = AV_CODEC_ID_XBM,
  103. .init = xbm_decode_init,
  104. .close = xbm_decode_close,
  105. .decode = xbm_decode_frame,
  106. .capabilities = CODEC_CAP_DR1,
  107. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  108. };