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.

309 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 "avcodec.h"
  37. #include "dsputil.h"
  38. typedef struct MsrleContext {
  39. AVCodecContext *avctx;
  40. AVFrame frame;
  41. const 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 av_cold int msrle_decode_init(AVCodecContext *avctx)
  215. {
  216. MsrleContext *s = avctx->priv_data;
  217. s->avctx = avctx;
  218. avctx->pix_fmt = PIX_FMT_PAL8;
  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. const uint8_t *buf, int buf_size)
  225. {
  226. MsrleContext *s = avctx->priv_data;
  227. s->buf = buf;
  228. s->size = buf_size;
  229. s->frame.reference = 1;
  230. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  231. if (avctx->reget_buffer(avctx, &s->frame)) {
  232. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  233. return -1;
  234. }
  235. switch (avctx->bits_per_sample) {
  236. case 8:
  237. msrle_decode_pal8(s);
  238. break;
  239. case 4:
  240. msrle_decode_pal4(s);
  241. break;
  242. default:
  243. av_log(avctx, AV_LOG_ERROR, "Don't know how to decode depth %u.\n",
  244. avctx->bits_per_sample);
  245. }
  246. *data_size = sizeof(AVFrame);
  247. *(AVFrame*)data = s->frame;
  248. /* report that the buffer was completely consumed */
  249. return buf_size;
  250. }
  251. static av_cold int msrle_decode_end(AVCodecContext *avctx)
  252. {
  253. MsrleContext *s = avctx->priv_data;
  254. /* release the last frame */
  255. if (s->frame.data[0])
  256. avctx->release_buffer(avctx, &s->frame);
  257. return 0;
  258. }
  259. AVCodec msrle_decoder = {
  260. "msrle",
  261. CODEC_TYPE_VIDEO,
  262. CODEC_ID_MSRLE,
  263. sizeof(MsrleContext),
  264. msrle_decode_init,
  265. NULL,
  266. msrle_decode_end,
  267. msrle_decode_frame,
  268. CODEC_CAP_DR1,
  269. .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"),
  270. };