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.

263 lines
9.2KB

  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. unsigned 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. 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. 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 - 1, pos=0, i;
  126. uint16_t pix16;
  127. uint32_t pix32;
  128. unsigned int width= FFABS(pic->linesize[0]) / (depth >> 3);
  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 + 1 < 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. pos += p1;
  150. if (line < 0 || pos >= width){
  151. av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
  152. return -1;
  153. }
  154. output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
  155. continue;
  156. }
  157. // Copy data
  158. if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end)
  159. ||(pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
  160. src += p2 * (depth >> 3);
  161. continue;
  162. }
  163. if(data + srcsize - src < p2 * (depth >> 3)){
  164. av_log(avctx, AV_LOG_ERROR, "Copy beyond input buffer\n");
  165. return -1;
  166. }
  167. if ((depth == 8) || (depth == 24)) {
  168. for(i = 0; i < p2 * (depth >> 3); i++) {
  169. *output++ = *src++;
  170. }
  171. // RLE8 copy is actually padded - and runs are not!
  172. if(depth == 8 && (p2 & 1)) {
  173. src++;
  174. }
  175. } else if (depth == 16) {
  176. for(i = 0; i < p2; i++) {
  177. pix16 = AV_RL16(src);
  178. src += 2;
  179. *(uint16_t*)output = pix16;
  180. output += 2;
  181. }
  182. } else if (depth == 32) {
  183. for(i = 0; i < p2; i++) {
  184. pix32 = AV_RL32(src);
  185. src += 4;
  186. *(uint32_t*)output = pix32;
  187. output += 4;
  188. }
  189. }
  190. pos += p2;
  191. } else { //run of pixels
  192. uint8_t pix[3]; //original pixel
  193. switch(depth){
  194. case 8: pix[0] = *src++;
  195. break;
  196. case 16: pix16 = AV_RL16(src);
  197. src += 2;
  198. break;
  199. case 24: pix[0] = *src++;
  200. pix[1] = *src++;
  201. pix[2] = *src++;
  202. break;
  203. case 32: pix32 = AV_RL32(src);
  204. src += 4;
  205. break;
  206. }
  207. if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
  208. ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
  209. continue;
  210. for(i = 0; i < p1; i++) {
  211. switch(depth){
  212. case 8: *output++ = pix[0];
  213. break;
  214. case 16: *(uint16_t*)output = pix16;
  215. output += 2;
  216. break;
  217. case 24: *output++ = pix[0];
  218. *output++ = pix[1];
  219. *output++ = pix[2];
  220. break;
  221. case 32: *(uint32_t*)output = pix32;
  222. output += 4;
  223. break;
  224. }
  225. }
  226. pos += p1;
  227. }
  228. }
  229. av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
  230. return 0;
  231. }
  232. int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
  233. const uint8_t* data, int data_size)
  234. {
  235. switch(depth){
  236. case 4:
  237. return msrle_decode_pal4(avctx, pic, data, data_size);
  238. case 8:
  239. case 16:
  240. case 24:
  241. case 32:
  242. return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
  243. default:
  244. av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
  245. return -1;
  246. }
  247. }