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.

310 lines
9.7KB

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