ptx.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * V.Flash PTX (.ptx) image decoder
  3. * Copyright (c) 2007 Ivo van Poorten
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "avcodec.h"
  23. typedef struct PTXContext {
  24. AVFrame picture;
  25. } PTXContext;
  26. static av_cold int ptx_init(AVCodecContext *avctx) {
  27. PTXContext *s = avctx->priv_data;
  28. avcodec_get_frame_defaults(&s->picture);
  29. avctx->coded_frame= &s->picture;
  30. return 0;
  31. }
  32. static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  33. const uint8_t *buf, int buf_size) {
  34. PTXContext * const s = avctx->priv_data;
  35. AVFrame *picture = data;
  36. AVFrame * const p = &s->picture;
  37. unsigned int offset, w, h, y, stride, bytes_per_pixel;
  38. uint8_t *ptr;
  39. offset = AV_RL16(buf);
  40. w = AV_RL16(buf+8);
  41. h = AV_RL16(buf+10);
  42. bytes_per_pixel = AV_RL16(buf+12) >> 3;
  43. if (bytes_per_pixel != 2) {
  44. av_log(avctx, AV_LOG_ERROR, "image format is not rgb15, please report on ffmpeg-users mailing list\n");
  45. return -1;
  46. }
  47. avctx->pix_fmt = PIX_FMT_RGB555;
  48. if (offset != 0x2c)
  49. av_log(avctx, AV_LOG_WARNING, "offset != 0x2c, untested due to lack of sample files\n");
  50. buf += offset;
  51. if (p->data[0])
  52. avctx->release_buffer(avctx, p);
  53. if (avcodec_check_dimensions(avctx, w, h))
  54. return -1;
  55. if (w != avctx->width || h != avctx->height)
  56. avcodec_set_dimensions(avctx, w, h);
  57. if (avctx->get_buffer(avctx, p) < 0) {
  58. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  59. return -1;
  60. }
  61. p->pict_type = FF_I_TYPE;
  62. ptr = p->data[0];
  63. stride = p->linesize[0];
  64. for (y=0; y<h; y++) {
  65. #ifdef WORDS_BIGENDIAN
  66. unsigned int x;
  67. for (x=0; x<w*bytes_per_pixel; x+=bytes_per_pixel)
  68. AV_WN16(ptr+x, AV_RL16(buf+x));
  69. #else
  70. memcpy(ptr, buf, w*bytes_per_pixel);
  71. #endif
  72. ptr += stride;
  73. buf += w*bytes_per_pixel;
  74. }
  75. *picture = s->picture;
  76. *data_size = sizeof(AVPicture);
  77. return offset + w*h*bytes_per_pixel;
  78. }
  79. static av_cold int ptx_end(AVCodecContext *avctx) {
  80. PTXContext *s = avctx->priv_data;
  81. if(s->picture.data[0])
  82. avctx->release_buffer(avctx, &s->picture);
  83. return 0;
  84. }
  85. AVCodec ptx_decoder = {
  86. "ptx",
  87. CODEC_TYPE_VIDEO,
  88. CODEC_ID_PTX,
  89. sizeof(PTXContext),
  90. ptx_init,
  91. NULL,
  92. ptx_end,
  93. ptx_decode_frame,
  94. 0,
  95. NULL,
  96. .long_name = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
  97. };