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.

312 lines
9.7KB

  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. unsigned char *buf;
  41. int size;
  42. } MsrleContext;
  43. #define FETCH_NEXT_STREAM_BYTE() \
  44. if (stream_ptr >= s->size) \
  45. { \
  46. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
  47. return; \
  48. } \
  49. stream_byte = s->buf[stream_ptr++];
  50. static void msrle_decode_pal4(MsrleContext *s)
  51. {
  52. int stream_ptr = 0;
  53. unsigned char rle_code;
  54. unsigned char extra_byte, odd_pixel;
  55. unsigned char stream_byte;
  56. int pixel_ptr = 0;
  57. int row_dec = s->frame.linesize[0];
  58. int row_ptr = (s->avctx->height - 1) * row_dec;
  59. int frame_size = row_dec * s->avctx->height;
  60. int i;
  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. odd_pixel = stream_byte & 1;
  89. rle_code = (stream_byte + 1) / 2;
  90. extra_byte = rle_code & 0x01;
  91. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  92. (row_ptr < 0)) {
  93. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  94. return;
  95. }
  96. for (i = 0; i < rle_code; i++) {
  97. if (pixel_ptr >= s->avctx->width)
  98. break;
  99. FETCH_NEXT_STREAM_BYTE();
  100. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
  101. pixel_ptr++;
  102. if (i + 1 == rle_code && odd_pixel)
  103. break;
  104. if (pixel_ptr >= s->avctx->width)
  105. break;
  106. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
  107. pixel_ptr++;
  108. }
  109. // if the RLE code is odd, skip a byte in the stream
  110. if (extra_byte)
  111. stream_ptr++;
  112. }
  113. } else {
  114. // decode a run of data
  115. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  116. (row_ptr < 0)) {
  117. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  118. return;
  119. }
  120. FETCH_NEXT_STREAM_BYTE();
  121. for (i = 0; i < rle_code; i++) {
  122. if (pixel_ptr >= s->avctx->width)
  123. break;
  124. if ((i & 1) == 0)
  125. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
  126. else
  127. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
  128. pixel_ptr++;
  129. }
  130. }
  131. }
  132. /* one last sanity check on the way out */
  133. if (stream_ptr < s->size)
  134. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
  135. stream_ptr, s->size);
  136. }
  137. static void msrle_decode_pal8(MsrleContext *s)
  138. {
  139. int stream_ptr = 0;
  140. unsigned char rle_code;
  141. unsigned char extra_byte;
  142. unsigned char stream_byte;
  143. int pixel_ptr = 0;
  144. int row_dec = s->frame.linesize[0];
  145. int row_ptr = (s->avctx->height - 1) * row_dec;
  146. int frame_size = row_dec * s->avctx->height;
  147. /* make the palette available */
  148. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  149. if (s->avctx->palctrl->palette_changed) {
  150. s->frame.palette_has_changed = 1;
  151. s->avctx->palctrl->palette_changed = 0;
  152. }
  153. while (row_ptr >= 0) {
  154. FETCH_NEXT_STREAM_BYTE();
  155. rle_code = stream_byte;
  156. if (rle_code == 0) {
  157. /* fetch the next byte to see how to handle escape code */
  158. FETCH_NEXT_STREAM_BYTE();
  159. if (stream_byte == 0) {
  160. /* line is done, goto the next one */
  161. row_ptr -= row_dec;
  162. pixel_ptr = 0;
  163. } else if (stream_byte == 1) {
  164. /* decode is done */
  165. return;
  166. } else if (stream_byte == 2) {
  167. /* reposition frame decode coordinates */
  168. FETCH_NEXT_STREAM_BYTE();
  169. pixel_ptr += stream_byte;
  170. FETCH_NEXT_STREAM_BYTE();
  171. row_ptr -= stream_byte * row_dec;
  172. } else {
  173. /* copy pixels from encoded stream */
  174. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  175. (row_ptr < 0)) {
  176. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  177. return;
  178. }
  179. rle_code = stream_byte;
  180. extra_byte = stream_byte & 0x01;
  181. if (stream_ptr + rle_code + extra_byte > s->size) {
  182. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (2)\n");
  183. return;
  184. }
  185. while (rle_code--) {
  186. FETCH_NEXT_STREAM_BYTE();
  187. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  188. pixel_ptr++;
  189. }
  190. /* if the RLE code is odd, skip a byte in the stream */
  191. if (extra_byte)
  192. stream_ptr++;
  193. }
  194. } else {
  195. /* decode a run of data */
  196. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  197. (row_ptr < 0)) {
  198. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (2)\n");
  199. return;
  200. }
  201. FETCH_NEXT_STREAM_BYTE();
  202. while(rle_code--) {
  203. s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
  204. pixel_ptr++;
  205. }
  206. }
  207. }
  208. /* one last sanity check on the way out */
  209. if (stream_ptr < s->size)
  210. av_log(s->avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
  211. stream_ptr, s->size);
  212. }
  213. static int msrle_decode_init(AVCodecContext *avctx)
  214. {
  215. MsrleContext *s = (MsrleContext *)avctx->priv_data;
  216. s->avctx = avctx;
  217. avctx->pix_fmt = PIX_FMT_PAL8;
  218. avctx->has_b_frames = 0;
  219. s->frame.data[0] = NULL;
  220. return 0;
  221. }
  222. static int msrle_decode_frame(AVCodecContext *avctx,
  223. void *data, int *data_size,
  224. uint8_t *buf, int buf_size)
  225. {
  226. MsrleContext *s = (MsrleContext *)avctx->priv_data;
  227. /* no supplementary picture */
  228. if (buf_size == 0)
  229. return 0;
  230. s->buf = buf;
  231. s->size = buf_size;
  232. s->frame.reference = 1;
  233. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  234. if (avctx->reget_buffer(avctx, &s->frame)) {
  235. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  236. return -1;
  237. }
  238. switch (avctx->bits_per_sample) {
  239. case 8:
  240. msrle_decode_pal8(s);
  241. break;
  242. case 4:
  243. msrle_decode_pal4(s);
  244. break;
  245. default:
  246. av_log(avctx, AV_LOG_ERROR, "Don't know how to decode depth %u.\n",
  247. avctx->bits_per_sample);
  248. }
  249. *data_size = sizeof(AVFrame);
  250. *(AVFrame*)data = s->frame;
  251. /* report that the buffer was completely consumed */
  252. return buf_size;
  253. }
  254. static int msrle_decode_end(AVCodecContext *avctx)
  255. {
  256. MsrleContext *s = (MsrleContext *)avctx->priv_data;
  257. /* release the last frame */
  258. if (s->frame.data[0])
  259. avctx->release_buffer(avctx, &s->frame);
  260. return 0;
  261. }
  262. AVCodec msrle_decoder = {
  263. "msrle",
  264. CODEC_TYPE_VIDEO,
  265. CODEC_ID_MSRLE,
  266. sizeof(MsrleContext),
  267. msrle_decode_init,
  268. NULL,
  269. msrle_decode_end,
  270. msrle_decode_frame,
  271. CODEC_CAP_DR1,
  272. };