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. while (row_ptr >= 0) {
  62. FETCH_NEXT_STREAM_BYTE();
  63. rle_code = stream_byte;
  64. if (rle_code == 0) {
  65. /* fetch the next byte to see how to handle escape code */
  66. FETCH_NEXT_STREAM_BYTE();
  67. if (stream_byte == 0) {
  68. /* line is done, goto the next one */
  69. row_ptr -= row_dec;
  70. pixel_ptr = 0;
  71. } else if (stream_byte == 1) {
  72. /* decode is done */
  73. return;
  74. } else if (stream_byte == 2) {
  75. /* reposition frame decode coordinates */
  76. FETCH_NEXT_STREAM_BYTE();
  77. pixel_ptr += stream_byte;
  78. FETCH_NEXT_STREAM_BYTE();
  79. row_ptr -= stream_byte * row_dec;
  80. } else {
  81. /* copy pixels from encoded stream */
  82. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  83. (row_ptr < 0)) {
  84. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  85. return;
  86. }
  87. rle_code = stream_byte;
  88. extra_byte = stream_byte & 0x01;
  89. if (stream_ptr + rle_code + extra_byte > s->size) {
  90. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (2)\n");
  91. return;
  92. }
  93. while (rle_code--) {
  94. FETCH_NEXT_STREAM_BYTE();
  95. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  96. pixel_ptr++;
  97. }
  98. /* if the RLE code is odd, skip a byte in the stream */
  99. if (extra_byte)
  100. stream_ptr++;
  101. }
  102. } else {
  103. /* decode a run of data */
  104. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  105. (row_ptr < 0)) {
  106. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (2)\n");
  107. return;
  108. }
  109. FETCH_NEXT_STREAM_BYTE();
  110. while(rle_code--) {
  111. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  112. pixel_ptr++;
  113. }
  114. }
  115. }
  116. /* make the palette available */
  117. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  118. if (s->avctx->palctrl->palette_changed) {
  119. s->frame.palette_has_changed = 1;
  120. s->avctx->palctrl->palette_changed = 0;
  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. };