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.

334 lines
11KB

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