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.

264 lines
9.6KB

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