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.

140 lines
3.9KB

  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->coded_frame = avcodec_alloc_frame();
  29. if (!avctx->coded_frame)
  30. return AVERROR(ENOMEM);
  31. return 0;
  32. }
  33. static int convert(uint8_t x)
  34. {
  35. if (x >= 'a')
  36. x -= 87;
  37. else if (x >= 'A')
  38. x -= 55;
  39. else
  40. x -= '0';
  41. return x;
  42. }
  43. static int xbm_decode_frame(AVCodecContext *avctx, void *data,
  44. int *got_frame, AVPacket *avpkt)
  45. {
  46. AVFrame *p = avctx->coded_frame;
  47. const uint8_t *end, *ptr = avpkt->data;
  48. uint8_t *dst;
  49. int ret, linesize, i, j;
  50. end = avpkt->data + avpkt->size;
  51. while (!avctx->width || !avctx->height) {
  52. char name[256];
  53. int number, len;
  54. ptr += strcspn(ptr, "#");
  55. if (sscanf(ptr, "#define %256s %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) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
  61. avctx->height = number;
  62. } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
  63. avctx->width = number;
  64. } else {
  65. av_log(avctx, AV_LOG_ERROR, "Unknown define '%s'\n", name);
  66. return AVERROR_INVALIDDATA;
  67. }
  68. ptr += strcspn(ptr, "\n\r") + 1;
  69. }
  70. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  71. if (p->data[0])
  72. avctx->release_buffer(avctx, p);
  73. p->reference = 0;
  74. if ((ret = ff_get_buffer(avctx, p)) < 0)
  75. return ret;
  76. // goto start of image data
  77. ptr += strcspn(ptr, "{") + 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. ptr += strcspn(ptr, "x") + 1;
  84. if (ptr < end && av_isxdigit(*ptr)) {
  85. val = convert(*ptr);
  86. ptr++;
  87. if (av_isxdigit(*ptr))
  88. val = (val << 4) + convert(*ptr);
  89. *dst++ = ff_reverse[val];
  90. } else {
  91. av_log(avctx, AV_LOG_ERROR, "Unexpected data at '%.8s'\n", ptr);
  92. return AVERROR_INVALIDDATA;
  93. }
  94. }
  95. }
  96. p->key_frame = 1;
  97. p->pict_type = AV_PICTURE_TYPE_I;
  98. *got_frame = 1;
  99. *(AVFrame *)data = *p;
  100. return avpkt->size;
  101. }
  102. static av_cold int xbm_decode_close(AVCodecContext *avctx)
  103. {
  104. if (avctx->coded_frame->data[0])
  105. avctx->release_buffer(avctx, avctx->coded_frame);
  106. av_freep(&avctx->coded_frame);
  107. return 0;
  108. }
  109. AVCodec ff_xbm_decoder = {
  110. .name = "xbm",
  111. .type = AVMEDIA_TYPE_VIDEO,
  112. .id = AV_CODEC_ID_XBM,
  113. .init = xbm_decode_init,
  114. .close = xbm_decode_close,
  115. .decode = xbm_decode_frame,
  116. .capabilities = CODEC_CAP_DR1,
  117. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  118. };