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.

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