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.

258 lines
8.7KB

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