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.

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