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.

219 lines
6.6KB

  1. /*
  2. * Micrsoft RLE Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file msrle.c
  21. * MS RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
  22. * For more information about the MS RLE format, visit:
  23. * http://www.pcisys.net/~melanson/codecs/
  24. *
  25. * The MS RLE decoder outputs PAL8 colorspace data.
  26. *
  27. * Note that this decoder expects the palette colors from the end of the
  28. * BITMAPINFO header passed through palctrl.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "common.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. typedef struct MsrleContext {
  38. AVCodecContext *avctx;
  39. AVFrame frame;
  40. AVFrame prev_frame;
  41. unsigned char *buf;
  42. int size;
  43. } MsrleContext;
  44. #define FETCH_NEXT_STREAM_BYTE() \
  45. if (stream_ptr >= s->size) \
  46. { \
  47. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
  48. return; \
  49. } \
  50. stream_byte = s->buf[stream_ptr++];
  51. static void msrle_decode_pal8(MsrleContext *s)
  52. {
  53. int stream_ptr = 0;
  54. unsigned char rle_code;
  55. unsigned char extra_byte;
  56. unsigned char stream_byte;
  57. int pixel_ptr = 0;
  58. int row_dec = s->frame.linesize[0];
  59. int row_ptr = (s->avctx->height - 1) * row_dec;
  60. int frame_size = row_dec * s->avctx->height;
  61. /* make the palette available */
  62. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  63. if (s->avctx->palctrl->palette_changed) {
  64. s->frame.palette_has_changed = 1;
  65. s->avctx->palctrl->palette_changed = 0;
  66. }
  67. while (row_ptr >= 0) {
  68. FETCH_NEXT_STREAM_BYTE();
  69. rle_code = stream_byte;
  70. if (rle_code == 0) {
  71. /* fetch the next byte to see how to handle escape code */
  72. FETCH_NEXT_STREAM_BYTE();
  73. if (stream_byte == 0) {
  74. /* line is done, goto the next one */
  75. row_ptr -= row_dec;
  76. pixel_ptr = 0;
  77. } else if (stream_byte == 1) {
  78. /* decode is done */
  79. return;
  80. } else if (stream_byte == 2) {
  81. /* reposition frame decode coordinates */
  82. FETCH_NEXT_STREAM_BYTE();
  83. pixel_ptr += stream_byte;
  84. FETCH_NEXT_STREAM_BYTE();
  85. row_ptr -= stream_byte * row_dec;
  86. } else {
  87. /* copy pixels from encoded stream */
  88. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  89. (row_ptr < 0)) {
  90. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  91. return;
  92. }
  93. rle_code = stream_byte;
  94. extra_byte = stream_byte & 0x01;
  95. if (stream_ptr + rle_code + extra_byte > s->size) {
  96. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (2)\n");
  97. return;
  98. }
  99. while (rle_code--) {
  100. FETCH_NEXT_STREAM_BYTE();
  101. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  102. pixel_ptr++;
  103. }
  104. /* if the RLE code is odd, skip a byte in the stream */
  105. if (extra_byte)
  106. stream_ptr++;
  107. }
  108. } else {
  109. /* decode a run of data */
  110. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  111. (row_ptr < 0)) {
  112. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (2)\n");
  113. return;
  114. }
  115. FETCH_NEXT_STREAM_BYTE();
  116. while(rle_code--) {
  117. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  118. pixel_ptr++;
  119. }
  120. }
  121. }
  122. /* one last sanity check on the way out */
  123. if (stream_ptr < s->size)
  124. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
  125. stream_ptr, s->size);
  126. }
  127. static int msrle_decode_init(AVCodecContext *avctx)
  128. {
  129. MsrleContext *s = (MsrleContext *)avctx->priv_data;
  130. s->avctx = avctx;
  131. avctx->pix_fmt = PIX_FMT_PAL8;
  132. avctx->has_b_frames = 0;
  133. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  134. return 0;
  135. }
  136. static int msrle_decode_frame(AVCodecContext *avctx,
  137. void *data, int *data_size,
  138. uint8_t *buf, int buf_size)
  139. {
  140. MsrleContext *s = (MsrleContext *)avctx->priv_data;
  141. s->buf = buf;
  142. s->size = buf_size;
  143. s->frame.reference = 1;
  144. if (avctx->get_buffer(avctx, &s->frame)) {
  145. av_log(avctx, AV_LOG_ERROR, " MS RLE: get_buffer() failed\n");
  146. return -1;
  147. }
  148. if (s->prev_frame.data[0] && (s->frame.linesize[0] != s->prev_frame.linesize[0]))
  149. av_log(avctx, AV_LOG_ERROR, " MS RLE: Buffer linesize changed: current %u, previous %u.\n"
  150. " Expect wrong image and/or crash!\n",
  151. s->frame.linesize[0], s->prev_frame.linesize[0]);
  152. /* grossly inefficient, but...oh well */
  153. if (s->prev_frame.data[0] != NULL)
  154. memcpy(s->frame.data[0], s->prev_frame.data[0],
  155. s->frame.linesize[0] * s->avctx->height);
  156. msrle_decode_pal8(s);
  157. if (s->prev_frame.data[0])
  158. avctx->release_buffer(avctx, &s->prev_frame);
  159. /* shuffle frames */
  160. s->prev_frame = s->frame;
  161. *data_size = sizeof(AVFrame);
  162. *(AVFrame*)data = s->frame;
  163. /* report that the buffer was completely consumed */
  164. return buf_size;
  165. }
  166. static int msrle_decode_end(AVCodecContext *avctx)
  167. {
  168. MsrleContext *s = (MsrleContext *)avctx->priv_data;
  169. /* release the last frame */
  170. if (s->prev_frame.data[0])
  171. avctx->release_buffer(avctx, &s->prev_frame);
  172. return 0;
  173. }
  174. AVCodec msrle_decoder = {
  175. "msrle",
  176. CODEC_TYPE_VIDEO,
  177. CODEC_ID_MSRLE,
  178. sizeof(MsrleContext),
  179. msrle_decode_init,
  180. NULL,
  181. msrle_decode_end,
  182. msrle_decode_frame,
  183. CODEC_CAP_DR1,
  184. };