msrledec.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Microsoft RLE decoder
  3. * Copyright (C) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 av_uninit(pix16);
  127. uint32_t av_uninit(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 < 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 ((depth == 8) || (depth == 24)) {
  164. for(i = 0; i < p2 * (depth >> 3); i++) {
  165. *output++ = *src++;
  166. }
  167. // RLE8 copy is actually padded - and runs are not!
  168. if(depth == 8 && (p2 & 1)) {
  169. src++;
  170. }
  171. } else if (depth == 16) {
  172. for(i = 0; i < p2; i++) {
  173. pix16 = AV_RL16(src);
  174. src += 2;
  175. *(uint16_t*)output = pix16;
  176. output += 2;
  177. }
  178. } else if (depth == 32) {
  179. for(i = 0; i < p2; i++) {
  180. pix32 = AV_RL32(src);
  181. src += 4;
  182. *(uint32_t*)output = pix32;
  183. output += 4;
  184. }
  185. }
  186. pos += p2;
  187. } else { //run of pixels
  188. uint8_t pix[3]; //original pixel
  189. switch(depth){
  190. case 8: pix[0] = *src++;
  191. break;
  192. case 16: pix16 = AV_RL16(src);
  193. src += 2;
  194. break;
  195. case 24: pix[0] = *src++;
  196. pix[1] = *src++;
  197. pix[2] = *src++;
  198. break;
  199. case 32: pix32 = AV_RL32(src);
  200. src += 4;
  201. break;
  202. }
  203. if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
  204. ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
  205. continue;
  206. for(i = 0; i < p1; i++) {
  207. switch(depth){
  208. case 8: *output++ = pix[0];
  209. break;
  210. case 16: *(uint16_t*)output = pix16;
  211. output += 2;
  212. break;
  213. case 24: *output++ = pix[0];
  214. *output++ = pix[1];
  215. *output++ = pix[2];
  216. break;
  217. case 32: *(uint32_t*)output = pix32;
  218. output += 4;
  219. break;
  220. }
  221. }
  222. pos += p1;
  223. }
  224. }
  225. av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
  226. return 0;
  227. }
  228. int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
  229. const uint8_t* data, int data_size)
  230. {
  231. switch(depth){
  232. case 4:
  233. return msrle_decode_pal4(avctx, pic, data, data_size);
  234. case 8:
  235. case 16:
  236. case 24:
  237. case 32:
  238. return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
  239. default:
  240. av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
  241. return -1;
  242. }
  243. }