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