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.

261 lines
8.9KB

  1. /*
  2. * Microsoft RLE decoder
  3. * Copyright (C) 2008 Konstantin Shishkov
  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
  23. * MS RLE decoder based on decoder by Mike Melanson and my own for TSCC
  24. * For more information about the MS RLE format, visit:
  25. * http://www.multimedia.cx/msrle.txt
  26. */
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include "msrledec.h"
  30. #define FETCH_NEXT_STREAM_BYTE() \
  31. if (stream_ptr >= data_size) \
  32. { \
  33. av_log(avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
  34. return -1; \
  35. } \
  36. stream_byte = data[stream_ptr++];
  37. static int msrle_decode_pal4(AVCodecContext *avctx, AVPicture *pic,
  38. const uint8_t *data, int data_size)
  39. {
  40. int stream_ptr = 0;
  41. unsigned char rle_code;
  42. unsigned char extra_byte, odd_pixel;
  43. unsigned char stream_byte;
  44. int pixel_ptr = 0;
  45. int row_dec = pic->linesize[0];
  46. int row_ptr = (avctx->height - 1) * row_dec;
  47. int frame_size = row_dec * avctx->height;
  48. int i;
  49. while (row_ptr >= 0) {
  50. FETCH_NEXT_STREAM_BYTE();
  51. rle_code = stream_byte;
  52. if (rle_code == 0) {
  53. /* fetch the next byte to see how to handle escape code */
  54. FETCH_NEXT_STREAM_BYTE();
  55. if (stream_byte == 0) {
  56. /* line is done, goto the next one */
  57. row_ptr -= row_dec;
  58. pixel_ptr = 0;
  59. } else if (stream_byte == 1) {
  60. /* decode is done */
  61. return 0;
  62. } else if (stream_byte == 2) {
  63. /* reposition frame decode coordinates */
  64. FETCH_NEXT_STREAM_BYTE();
  65. pixel_ptr += stream_byte;
  66. FETCH_NEXT_STREAM_BYTE();
  67. row_ptr -= stream_byte * row_dec;
  68. } else {
  69. // copy pixels from encoded stream
  70. odd_pixel = stream_byte & 1;
  71. rle_code = (stream_byte + 1) / 2;
  72. extra_byte = rle_code & 0x01;
  73. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  74. (row_ptr < 0)) {
  75. av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  76. return -1;
  77. }
  78. for (i = 0; i < rle_code; i++) {
  79. if (pixel_ptr >= avctx->width)
  80. break;
  81. FETCH_NEXT_STREAM_BYTE();
  82. pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
  83. pixel_ptr++;
  84. if (i + 1 == rle_code && odd_pixel)
  85. break;
  86. if (pixel_ptr >= avctx->width)
  87. break;
  88. pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
  89. pixel_ptr++;
  90. }
  91. // if the RLE code is odd, skip a byte in the stream
  92. if (extra_byte)
  93. stream_ptr++;
  94. }
  95. } else {
  96. // decode a run of data
  97. if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
  98. (row_ptr < 0)) {
  99. av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
  100. return -1;
  101. }
  102. FETCH_NEXT_STREAM_BYTE();
  103. for (i = 0; i < rle_code; i++) {
  104. if (pixel_ptr >= avctx->width)
  105. break;
  106. if ((i & 1) == 0)
  107. pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
  108. else
  109. pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
  110. pixel_ptr++;
  111. }
  112. }
  113. }
  114. /* one last sanity check on the way out */
  115. if (stream_ptr < data_size) {
  116. av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
  117. stream_ptr, data_size);
  118. return -1;
  119. }
  120. return 0;
  121. }
  122. static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
  123. const uint8_t *data, int srcsize)
  124. {
  125. uint8_t *output, *output_end;
  126. const uint8_t* src = data;
  127. int p1, p2, line=avctx->height - 1, pos=0, i;
  128. uint16_t av_uninit(pix16);
  129. uint32_t av_uninit(pix32);
  130. output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
  131. output_end = pic->data[0] + (avctx->height) * pic->linesize[0];
  132. while(src < data + srcsize) {
  133. p1 = *src++;
  134. if(p1 == 0) { //Escape code
  135. p2 = *src++;
  136. if(p2 == 0) { //End-of-line
  137. output = pic->data[0] + (--line) * pic->linesize[0];
  138. if (line < 0 && !(src+1 < data + srcsize && AV_RB16(src) == 1)) {
  139. av_log(avctx, AV_LOG_ERROR, "Next line is beyond picture bounds\n");
  140. return -1;
  141. }
  142. pos = 0;
  143. continue;
  144. } else if(p2 == 1) { //End-of-picture
  145. return 0;
  146. } else if(p2 == 2) { //Skip
  147. p1 = *src++;
  148. p2 = *src++;
  149. line -= p2;
  150. if (line < 0){
  151. av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
  152. return -1;
  153. }
  154. pos += p1;
  155. output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
  156. continue;
  157. }
  158. // Copy data
  159. if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end)
  160. ||(pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
  161. src += p2 * (depth >> 3);
  162. continue;
  163. }
  164. if ((depth == 8) || (depth == 24)) {
  165. for(i = 0; i < p2 * (depth >> 3); i++) {
  166. *output++ = *src++;
  167. }
  168. // RLE8 copy is actually padded - and runs are not!
  169. if(depth == 8 && (p2 & 1)) {
  170. src++;
  171. }
  172. } else if (depth == 16) {
  173. for(i = 0; i < p2; i++) {
  174. pix16 = AV_RL16(src);
  175. src += 2;
  176. *(uint16_t*)output = pix16;
  177. output += 2;
  178. }
  179. } else if (depth == 32) {
  180. for(i = 0; i < p2; i++) {
  181. pix32 = AV_RL32(src);
  182. src += 4;
  183. *(uint32_t*)output = pix32;
  184. output += 4;
  185. }
  186. }
  187. pos += p2;
  188. } else { //run of pixels
  189. uint8_t pix[3]; //original pixel
  190. switch(depth){
  191. case 8: pix[0] = *src++;
  192. break;
  193. case 16: pix16 = AV_RL16(src);
  194. src += 2;
  195. break;
  196. case 24: pix[0] = *src++;
  197. pix[1] = *src++;
  198. pix[2] = *src++;
  199. break;
  200. case 32: pix32 = AV_RL32(src);
  201. src += 4;
  202. break;
  203. }
  204. if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
  205. ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
  206. continue;
  207. for(i = 0; i < p1; i++) {
  208. switch(depth){
  209. case 8: *output++ = pix[0];
  210. break;
  211. case 16: *(uint16_t*)output = pix16;
  212. output += 2;
  213. break;
  214. case 24: *output++ = pix[0];
  215. *output++ = pix[1];
  216. *output++ = pix[2];
  217. break;
  218. case 32: *(uint32_t*)output = pix32;
  219. output += 4;
  220. break;
  221. }
  222. }
  223. pos += p1;
  224. }
  225. }
  226. av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
  227. return 0;
  228. }
  229. int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
  230. const uint8_t* data, int data_size)
  231. {
  232. switch(depth){
  233. case 4:
  234. return msrle_decode_pal4(avctx, pic, data, data_size);
  235. case 8:
  236. case 16:
  237. case 24:
  238. case 32:
  239. return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
  240. default:
  241. av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
  242. return -1;
  243. }
  244. }