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.

257 lines
8.6KB

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