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.

138 lines
3.8KB

  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. static av_cold int xbm_decode_init(AVCodecContext *avctx)
  25. {
  26. avctx->coded_frame = avcodec_alloc_frame();
  27. if (!avctx->coded_frame)
  28. return AVERROR(ENOMEM);
  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 *data_size, AVPacket *avpkt)
  43. {
  44. AVFrame *p = avctx->coded_frame;
  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. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  69. if (p->data[0])
  70. avctx->release_buffer(avctx, p);
  71. p->reference = 0;
  72. if ((ret = avctx->get_buffer(avctx, p)) < 0)
  73. return ret;
  74. // goto start of image data
  75. ptr += strcspn(ptr, "{") + 1;
  76. linesize = (avctx->width + 7) / 8;
  77. for (i = 0; i < avctx->height; i++) {
  78. dst = p->data[0] + i * p->linesize[0];
  79. for (j = 0; j < linesize; j++) {
  80. uint8_t val;
  81. ptr += strcspn(ptr, "x") + 1;
  82. if (ptr < end && isxdigit(*ptr)) {
  83. val = convert(*ptr);
  84. ptr++;
  85. if (isxdigit(*ptr))
  86. val = (val << 4) + convert(*ptr);
  87. *dst++ = av_reverse[val];
  88. } else {
  89. av_log(avctx, AV_LOG_ERROR, "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. *data_size = sizeof(AVFrame);
  97. *(AVFrame *)data = *p;
  98. return avpkt->size;
  99. }
  100. static av_cold int xbm_decode_close(AVCodecContext *avctx)
  101. {
  102. if (avctx->coded_frame->data[0])
  103. avctx->release_buffer(avctx, avctx->coded_frame);
  104. av_freep(&avctx->coded_frame);
  105. return 0;
  106. }
  107. AVCodec ff_xbm_decoder = {
  108. .name = "xbm",
  109. .type = AVMEDIA_TYPE_VIDEO,
  110. .id = AV_CODEC_ID_XBM,
  111. .init = xbm_decode_init,
  112. .close = xbm_decode_close,
  113. .decode = xbm_decode_frame,
  114. .capabilities = CODEC_CAP_DR1,
  115. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  116. };