00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include "libavutil/intreadwrite.h"
00030 #include "avcodec.h"
00031 #include "msrledec.h"
00032
00033 #define FETCH_NEXT_STREAM_BYTE() \
00034 if (stream_ptr >= data_size) \
00035 { \
00036 av_log(avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
00037 return -1; \
00038 } \
00039 stream_byte = data[stream_ptr++];
00040
00041 static int msrle_decode_pal4(AVCodecContext *avctx, AVPicture *pic,
00042 const uint8_t *data, int data_size)
00043 {
00044 int stream_ptr = 0;
00045 unsigned char rle_code;
00046 unsigned char extra_byte, odd_pixel;
00047 unsigned char stream_byte;
00048 unsigned int pixel_ptr = 0;
00049 int row_dec = pic->linesize[0];
00050 int row_ptr = (avctx->height - 1) * row_dec;
00051 int frame_size = row_dec * avctx->height;
00052 int i;
00053
00054 while (row_ptr >= 0) {
00055 FETCH_NEXT_STREAM_BYTE();
00056 rle_code = stream_byte;
00057 if (rle_code == 0) {
00058
00059 FETCH_NEXT_STREAM_BYTE();
00060 if (stream_byte == 0) {
00061
00062 row_ptr -= row_dec;
00063 pixel_ptr = 0;
00064 } else if (stream_byte == 1) {
00065
00066 return 0;
00067 } else if (stream_byte == 2) {
00068
00069 FETCH_NEXT_STREAM_BYTE();
00070 pixel_ptr += stream_byte;
00071 FETCH_NEXT_STREAM_BYTE();
00072 row_ptr -= stream_byte * row_dec;
00073 } else {
00074
00075 odd_pixel = stream_byte & 1;
00076 rle_code = (stream_byte + 1) / 2;
00077 extra_byte = rle_code & 0x01;
00078 if (row_ptr + pixel_ptr + stream_byte > frame_size) {
00079 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
00080 return -1;
00081 }
00082
00083 for (i = 0; i < rle_code; i++) {
00084 if (pixel_ptr >= avctx->width)
00085 break;
00086 FETCH_NEXT_STREAM_BYTE();
00087 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
00088 pixel_ptr++;
00089 if (i + 1 == rle_code && odd_pixel)
00090 break;
00091 if (pixel_ptr >= avctx->width)
00092 break;
00093 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
00094 pixel_ptr++;
00095 }
00096
00097
00098 if (extra_byte)
00099 stream_ptr++;
00100 }
00101 } else {
00102
00103 if (row_ptr + pixel_ptr + stream_byte > frame_size) {
00104 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
00105 return -1;
00106 }
00107 FETCH_NEXT_STREAM_BYTE();
00108 for (i = 0; i < rle_code; i++) {
00109 if (pixel_ptr >= avctx->width)
00110 break;
00111 if ((i & 1) == 0)
00112 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
00113 else
00114 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
00115 pixel_ptr++;
00116 }
00117 }
00118 }
00119
00120
00121 if (stream_ptr < data_size) {
00122 av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
00123 stream_ptr, data_size);
00124 return -1;
00125 }
00126
00127 return 0;
00128 }
00129
00130
00131 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
00132 const uint8_t *data, int srcsize)
00133 {
00134 uint8_t *output, *output_end;
00135 const uint8_t* src = data;
00136 int p1, p2, line=avctx->height - 1, pos=0, i;
00137 uint16_t av_uninit(pix16);
00138 uint32_t av_uninit(pix32);
00139 unsigned int width= FFABS(pic->linesize[0]) / (depth >> 3);
00140
00141 output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
00142 output_end = pic->data[0] + avctx->height * pic->linesize[0];
00143 while(src + 1 < data + srcsize) {
00144 p1 = *src++;
00145 if(p1 == 0) {
00146 p2 = *src++;
00147 if(p2 == 0) {
00148 output = pic->data[0] + (--line) * pic->linesize[0];
00149 if (line < 0 && !(src+1 < data + srcsize && AV_RB16(src) == 1)) {
00150 av_log(avctx, AV_LOG_ERROR, "Next line is beyond picture bounds\n");
00151 return -1;
00152 }
00153 pos = 0;
00154 continue;
00155 } else if(p2 == 1) {
00156 return 0;
00157 } else if(p2 == 2) {
00158 p1 = *src++;
00159 p2 = *src++;
00160 line -= p2;
00161 pos += p1;
00162 if (line < 0 || pos >= width){
00163 av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
00164 return -1;
00165 }
00166 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
00167 continue;
00168 }
00169
00170 if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end)
00171 ||(pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
00172 src += p2 * (depth >> 3);
00173 continue;
00174 }
00175 if(data + srcsize - src < p2 * (depth >> 3)){
00176 av_log(avctx, AV_LOG_ERROR, "Copy beyond input buffer\n");
00177 return -1;
00178 }
00179 if ((depth == 8) || (depth == 24)) {
00180 for(i = 0; i < p2 * (depth >> 3); i++) {
00181 *output++ = *src++;
00182 }
00183
00184 if(depth == 8 && (p2 & 1)) {
00185 src++;
00186 }
00187 } else if (depth == 16) {
00188 for(i = 0; i < p2; i++) {
00189 pix16 = AV_RL16(src);
00190 src += 2;
00191 *(uint16_t*)output = pix16;
00192 output += 2;
00193 }
00194 } else if (depth == 32) {
00195 for(i = 0; i < p2; i++) {
00196 pix32 = AV_RL32(src);
00197 src += 4;
00198 *(uint32_t*)output = pix32;
00199 output += 4;
00200 }
00201 }
00202 pos += p2;
00203 } else {
00204 uint8_t pix[3];
00205 switch(depth){
00206 case 8: pix[0] = *src++;
00207 break;
00208 case 16: pix16 = AV_RL16(src);
00209 src += 2;
00210 break;
00211 case 24: pix[0] = *src++;
00212 pix[1] = *src++;
00213 pix[2] = *src++;
00214 break;
00215 case 32: pix32 = AV_RL32(src);
00216 src += 4;
00217 break;
00218 }
00219 if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
00220 ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
00221 continue;
00222 for(i = 0; i < p1; i++) {
00223 switch(depth){
00224 case 8: *output++ = pix[0];
00225 break;
00226 case 16: *(uint16_t*)output = pix16;
00227 output += 2;
00228 break;
00229 case 24: *output++ = pix[0];
00230 *output++ = pix[1];
00231 *output++ = pix[2];
00232 break;
00233 case 32: *(uint32_t*)output = pix32;
00234 output += 4;
00235 break;
00236 }
00237 }
00238 pos += p1;
00239 }
00240 }
00241
00242 av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
00243 return 0;
00244 }
00245
00246
00247 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
00248 const uint8_t* data, int data_size)
00249 {
00250 switch(depth){
00251 case 4:
00252 return msrle_decode_pal4(avctx, pic, data, data_size);
00253 case 8:
00254 case 16:
00255 case 24:
00256 case 32:
00257 return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
00258 default:
00259 av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
00260 return -1;
00261 }
00262 }
00263