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.

131 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. 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. return AVERROR_INVALIDDATA;
  55. len = strlen(name);
  56. if ((len > 6) && !avctx->height && !memcmp(name + len - 7, "_height", 7)) {
  57. avctx->height = number;
  58. } else if ((len > 5) && !avctx->width && !memcmp(name + len - 6, "_width", 6)) {
  59. avctx->width = number;
  60. } else {
  61. return AVERROR_INVALIDDATA;
  62. }
  63. ptr += strcspn(ptr, "\n\r") + 1;
  64. }
  65. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  66. if (p->data[0])
  67. avctx->release_buffer(avctx, p);
  68. p->reference = 0;
  69. if ((ret = avctx->get_buffer(avctx, p)) < 0)
  70. return ret;
  71. linesize = (avctx->width + 7) / 8;
  72. for (i = 0; i < avctx->height; i++) {
  73. dst = p->data[0] + i * p->linesize[0];
  74. for (j = 0; j < linesize; j++) {
  75. uint8_t val;
  76. ptr += strcspn(ptr, "x") + 1;
  77. if (ptr < end && isxdigit(*ptr)) {
  78. val = convert(*ptr);
  79. ptr++;
  80. if (isxdigit(*ptr))
  81. val = (val << 4) + convert(*ptr);
  82. *dst++ = av_reverse[val];
  83. } else {
  84. return AVERROR_INVALIDDATA;
  85. }
  86. }
  87. }
  88. p->key_frame = 1;
  89. p->pict_type = AV_PICTURE_TYPE_I;
  90. *data_size = sizeof(AVFrame);
  91. *(AVFrame *)data = *p;
  92. return avpkt->size;
  93. }
  94. static av_cold int xbm_decode_close(AVCodecContext *avctx)
  95. {
  96. if (avctx->coded_frame->data[0])
  97. avctx->release_buffer(avctx, avctx->coded_frame);
  98. av_freep(&avctx->coded_frame);
  99. return 0;
  100. }
  101. AVCodec ff_xbm_decoder = {
  102. .name = "xbm",
  103. .type = AVMEDIA_TYPE_VIDEO,
  104. .id = CODEC_ID_XBM,
  105. .init = xbm_decode_init,
  106. .close = xbm_decode_close,
  107. .decode = xbm_decode_frame,
  108. .capabilities = CODEC_CAP_DR1,
  109. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  110. };