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.

101 lines
2.9KB

  1. /*
  2. * V.Flash PTX (.ptx) image decoder
  3. * Copyright (c) 2007 Ivo van Poorten
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  27. AVPacket *avpkt) {
  28. const uint8_t *buf = avpkt->data;
  29. const uint8_t *buf_end = avpkt->data + avpkt->size;
  30. AVFrame * const p = data;
  31. unsigned int offset, w, h, y, stride, bytes_per_pixel;
  32. int ret;
  33. uint8_t *ptr;
  34. if (buf_end - buf < 14)
  35. return AVERROR_INVALIDDATA;
  36. offset = AV_RL16(buf);
  37. w = AV_RL16(buf+8);
  38. h = AV_RL16(buf+10);
  39. bytes_per_pixel = AV_RL16(buf+12) >> 3;
  40. if (bytes_per_pixel != 2) {
  41. avpriv_request_sample(avctx, "Image format not RGB15");
  42. return AVERROR_PATCHWELCOME;
  43. }
  44. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  45. if (buf_end - buf < offset)
  46. return AVERROR_INVALIDDATA;
  47. if (offset != 0x2c)
  48. avpriv_request_sample(avctx, "offset != 0x2c");
  49. buf += offset;
  50. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  51. return ret;
  52. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  53. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  54. return ret;
  55. }
  56. p->pict_type = AV_PICTURE_TYPE_I;
  57. ptr = p->data[0];
  58. stride = p->linesize[0];
  59. for (y = 0; y < h && buf_end - buf >= w * bytes_per_pixel; y++) {
  60. #if HAVE_BIGENDIAN
  61. unsigned int x;
  62. for (x=0; x<w*bytes_per_pixel; x+=bytes_per_pixel)
  63. AV_WN16(ptr+x, AV_RL16(buf+x));
  64. #else
  65. memcpy(ptr, buf, w*bytes_per_pixel);
  66. #endif
  67. ptr += stride;
  68. buf += w*bytes_per_pixel;
  69. }
  70. *got_frame = 1;
  71. if (y < h) {
  72. av_log(avctx, AV_LOG_WARNING, "incomplete packet\n");
  73. return avpkt->size;
  74. }
  75. return offset + w*h*bytes_per_pixel;
  76. }
  77. AVCodec ff_ptx_decoder = {
  78. .name = "ptx",
  79. .long_name = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
  80. .type = AVMEDIA_TYPE_VIDEO,
  81. .id = AV_CODEC_ID_PTX,
  82. .decode = ptx_decode_frame,
  83. .capabilities = CODEC_CAP_DR1,
  84. };