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 < 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 ((depth == 8) || (depth == 24)) {
00176 for(i = 0; i < p2 * (depth >> 3); i++) {
00177 *output++ = *src++;
00178 }
00179
00180 if(depth == 8 && (p2 & 1)) {
00181 src++;
00182 }
00183 } else if (depth == 16) {
00184 for(i = 0; i < p2; i++) {
00185 pix16 = AV_RL16(src);
00186 src += 2;
00187 *(uint16_t*)output = pix16;
00188 output += 2;
00189 }
00190 } else if (depth == 32) {
00191 for(i = 0; i < p2; i++) {
00192 pix32 = AV_RL32(src);
00193 src += 4;
00194 *(uint32_t*)output = pix32;
00195 output += 4;
00196 }
00197 }
00198 pos += p2;
00199 } else {
00200 uint8_t pix[3];
00201 switch(depth){
00202 case 8: pix[0] = *src++;
00203 break;
00204 case 16: pix16 = AV_RL16(src);
00205 src += 2;
00206 break;
00207 case 24: pix[0] = *src++;
00208 pix[1] = *src++;
00209 pix[2] = *src++;
00210 break;
00211 case 32: pix32 = AV_RL32(src);
00212 src += 4;
00213 break;
00214 }
00215 if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
00216 ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
00217 continue;
00218 for(i = 0; i < p1; i++) {
00219 switch(depth){
00220 case 8: *output++ = pix[0];
00221 break;
00222 case 16: *(uint16_t*)output = pix16;
00223 output += 2;
00224 break;
00225 case 24: *output++ = pix[0];
00226 *output++ = pix[1];
00227 *output++ = pix[2];
00228 break;
00229 case 32: *(uint32_t*)output = pix32;
00230 output += 4;
00231 break;
00232 }
00233 }
00234 pos += p1;
00235 }
00236 }
00237
00238 av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
00239 return 0;
00240 }
00241
00242
00243 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
00244 const uint8_t* data, int data_size)
00245 {
00246 switch(depth){
00247 case 4:
00248 return msrle_decode_pal4(avctx, pic, data, data_size);
00249 case 8:
00250 case 16:
00251 case 24:
00252 case 32:
00253 return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
00254 default:
00255 av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
00256 return -1;
00257 }
00258 }
00259