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.

147 lines
4.0KB

  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 "libavutil/avstring.h"
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include "mathops.h"
  26. static int get_nibble(uint8_t x)
  27. {
  28. int ret = 255;
  29. if (x <= '9') {
  30. if (x >= '0')
  31. ret = x - '0';
  32. } else if (x >= 'a') {
  33. if (x <= 'f')
  34. ret = x - ('a' - 10);
  35. } else if (x >= 'A' && x <= 'F')
  36. ret = x - ('A' - 10);
  37. return ret;
  38. }
  39. static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
  40. {
  41. int keylen = strlen(key);
  42. const uint8_t *e = end - keylen;
  43. for(; p < e; p++) {
  44. if (!memcmp(p, key, keylen))
  45. break;
  46. }
  47. p += keylen;
  48. if (p >= end)
  49. return INT_MIN;
  50. for(; p<end; p++) {
  51. char *eptr;
  52. int64_t ret = strtol(p, &eptr, 10);
  53. if ((const uint8_t *)eptr != p)
  54. return ret;
  55. }
  56. return INT_MIN;
  57. }
  58. static int xbm_decode_frame(AVCodecContext *avctx, void *data,
  59. int *got_frame, AVPacket *avpkt)
  60. {
  61. AVFrame *p = data;
  62. int ret, linesize, i, j;
  63. int width = 0;
  64. int height = 0;
  65. const uint8_t *end, *ptr = avpkt->data;
  66. const uint8_t *next;
  67. uint8_t *dst;
  68. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  69. end = avpkt->data + avpkt->size;
  70. width = parse_str_int(avpkt->data, end, "_width");
  71. height = parse_str_int(avpkt->data, end, "_height");
  72. if ((ret = ff_set_dimensions(avctx, width, height)) < 0)
  73. return ret;
  74. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  75. return ret;
  76. // goto start of image data
  77. next = memchr(ptr, '{', avpkt->size);
  78. if (!next)
  79. next = memchr(ptr, '(', avpkt->size);
  80. if (!next)
  81. return AVERROR_INVALIDDATA;
  82. ptr = next + 1;
  83. linesize = (avctx->width + 7) / 8;
  84. for (i = 0; i < avctx->height; i++) {
  85. dst = p->data[0] + i * p->linesize[0];
  86. for (j = 0; j < linesize; j++) {
  87. uint8_t nib, val;
  88. while (ptr < end && *ptr != 'x' && *ptr != '$')
  89. ptr++;
  90. ptr ++;
  91. if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
  92. ptr++;
  93. if ((nib = get_nibble(*ptr)) <= 15) {
  94. val = (val << 4) + nib;
  95. ptr++;
  96. }
  97. *dst++ = ff_reverse[val];
  98. if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
  99. j++;
  100. ptr++;
  101. if ((nib = get_nibble(*ptr)) <= 15) {
  102. val = (val << 4) + nib;
  103. ptr++;
  104. }
  105. *dst++ = ff_reverse[val];
  106. }
  107. } else {
  108. av_log(avctx, AV_LOG_ERROR,
  109. "Unexpected data at %.8s.\n", ptr);
  110. return AVERROR_INVALIDDATA;
  111. }
  112. }
  113. }
  114. p->key_frame = 1;
  115. p->pict_type = AV_PICTURE_TYPE_I;
  116. *got_frame = 1;
  117. return avpkt->size;
  118. }
  119. AVCodec ff_xbm_decoder = {
  120. .name = "xbm",
  121. .long_name = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
  122. .type = AVMEDIA_TYPE_VIDEO,
  123. .id = AV_CODEC_ID_XBM,
  124. .decode = xbm_decode_frame,
  125. .capabilities = AV_CODEC_CAP_DR1,
  126. };