msrle.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Micrsoft RLE Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  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. /**
  22. * @file libavcodec/msrle.c
  23. * MS RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
  24. * For more information about the MS RLE format, visit:
  25. * http://www.pcisys.net/~melanson/codecs/
  26. *
  27. * The MS RLE decoder outputs PAL8 colorspace data.
  28. *
  29. * Note that this decoder expects the palette colors from the end of the
  30. * BITMAPINFO header passed through palctrl.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "msrledec.h"
  39. typedef struct MsrleContext {
  40. AVCodecContext *avctx;
  41. AVFrame frame;
  42. const unsigned char *buf;
  43. int size;
  44. } MsrleContext;
  45. static av_cold int msrle_decode_init(AVCodecContext *avctx)
  46. {
  47. MsrleContext *s = avctx->priv_data;
  48. s->avctx = avctx;
  49. avctx->pix_fmt = PIX_FMT_PAL8;
  50. s->frame.data[0] = NULL;
  51. return 0;
  52. }
  53. static int msrle_decode_frame(AVCodecContext *avctx,
  54. void *data, int *data_size,
  55. const uint8_t *buf, int buf_size)
  56. {
  57. MsrleContext *s = avctx->priv_data;
  58. s->buf = buf;
  59. s->size = buf_size;
  60. s->frame.reference = 1;
  61. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  62. if (avctx->reget_buffer(avctx, &s->frame)) {
  63. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  64. return -1;
  65. }
  66. /* make the palette available */
  67. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  68. if (s->avctx->palctrl->palette_changed) {
  69. s->frame.palette_has_changed = 1;
  70. s->avctx->palctrl->palette_changed = 0;
  71. }
  72. ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, buf, buf_size);
  73. *data_size = sizeof(AVFrame);
  74. *(AVFrame*)data = s->frame;
  75. /* report that the buffer was completely consumed */
  76. return buf_size;
  77. }
  78. static av_cold int msrle_decode_end(AVCodecContext *avctx)
  79. {
  80. MsrleContext *s = avctx->priv_data;
  81. /* release the last frame */
  82. if (s->frame.data[0])
  83. avctx->release_buffer(avctx, &s->frame);
  84. return 0;
  85. }
  86. AVCodec msrle_decoder = {
  87. "msrle",
  88. CODEC_TYPE_VIDEO,
  89. CODEC_ID_MSRLE,
  90. sizeof(MsrleContext),
  91. msrle_decode_init,
  92. NULL,
  93. msrle_decode_end,
  94. msrle_decode_frame,
  95. CODEC_CAP_DR1,
  96. .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"),
  97. };