msrle.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Micrsoft RLE Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  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. /**
  22. * @file
  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. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "msrledec.h"
  35. typedef struct MsrleContext {
  36. AVCodecContext *avctx;
  37. AVFrame frame;
  38. const unsigned char *buf;
  39. int size;
  40. uint32_t pal[256];
  41. } MsrleContext;
  42. static av_cold int msrle_decode_init(AVCodecContext *avctx)
  43. {
  44. MsrleContext *s = avctx->priv_data;
  45. s->avctx = avctx;
  46. switch (avctx->bits_per_coded_sample) {
  47. case 4:
  48. case 8:
  49. avctx->pix_fmt = PIX_FMT_PAL8;
  50. break;
  51. case 24:
  52. avctx->pix_fmt = PIX_FMT_BGR24;
  53. break;
  54. default:
  55. av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample\n");
  56. return -1;
  57. }
  58. s->frame.data[0] = NULL;
  59. return 0;
  60. }
  61. static int msrle_decode_frame(AVCodecContext *avctx,
  62. void *data, int *data_size,
  63. AVPacket *avpkt)
  64. {
  65. const uint8_t *buf = avpkt->data;
  66. int buf_size = avpkt->size;
  67. MsrleContext *s = avctx->priv_data;
  68. int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8;
  69. s->buf = buf;
  70. s->size = buf_size;
  71. s->frame.reference = 1;
  72. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  73. if (avctx->reget_buffer(avctx, &s->frame)) {
  74. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  75. return -1;
  76. }
  77. if (avctx->bits_per_coded_sample <= 8) {
  78. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  79. if (pal) {
  80. s->frame.palette_has_changed = 1;
  81. memcpy(s->pal, pal, AVPALETTE_SIZE);
  82. }
  83. /* make the palette available */
  84. memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
  85. }
  86. /* FIXME how to correctly detect RLE ??? */
  87. if (avctx->height * istride == avpkt->size) { /* assume uncompressed */
  88. int linesize = avctx->width * avctx->bits_per_coded_sample / 8;
  89. uint8_t *ptr = s->frame.data[0];
  90. uint8_t *buf = avpkt->data + (avctx->height-1)*istride;
  91. int i, j;
  92. for (i = 0; i < avctx->height; i++) {
  93. if (avctx->bits_per_coded_sample == 4) {
  94. for (j = 0; j < avctx->width - 1; j += 2) {
  95. ptr[j+0] = buf[j>>1] >> 4;
  96. ptr[j+1] = buf[j>>1] & 0xF;
  97. }
  98. if (avctx->width & 1)
  99. ptr[j+0] = buf[j>>1] >> 4;
  100. } else {
  101. memcpy(ptr, buf, linesize);
  102. }
  103. buf -= istride;
  104. ptr += s->frame.linesize[0];
  105. }
  106. } else {
  107. ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, buf, buf_size);
  108. }
  109. *data_size = sizeof(AVFrame);
  110. *(AVFrame*)data = s->frame;
  111. /* report that the buffer was completely consumed */
  112. return buf_size;
  113. }
  114. static av_cold int msrle_decode_end(AVCodecContext *avctx)
  115. {
  116. MsrleContext *s = avctx->priv_data;
  117. /* release the last frame */
  118. if (s->frame.data[0])
  119. avctx->release_buffer(avctx, &s->frame);
  120. return 0;
  121. }
  122. AVCodec ff_msrle_decoder = {
  123. "msrle",
  124. AVMEDIA_TYPE_VIDEO,
  125. CODEC_ID_MSRLE,
  126. sizeof(MsrleContext),
  127. msrle_decode_init,
  128. NULL,
  129. msrle_decode_end,
  130. msrle_decode_frame,
  131. CODEC_CAP_DR1,
  132. .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"),
  133. };