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 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 (row_ptr < 0)) {
00080 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
00081 return -1;
00082 }
00083
00084 for (i = 0; i < rle_code; i++) {
00085 if (pixel_ptr >= avctx->width)
00086 break;
00087 FETCH_NEXT_STREAM_BYTE();
00088 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
00089 pixel_ptr++;
00090 if (i + 1 == rle_code && odd_pixel)
00091 break;
00092 if (pixel_ptr >= avctx->width)
00093 break;
00094 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
00095 pixel_ptr++;
00096 }
00097
00098
00099 if (extra_byte)
00100 stream_ptr++;
00101 }
00102 } else {
00103
00104 if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
00105 (row_ptr < 0)) {
00106 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
00107 return -1;
00108 }
00109 FETCH_NEXT_STREAM_BYTE();
00110 for (i = 0; i < rle_code; i++) {
00111 if (pixel_ptr >= avctx->width)
00112 break;
00113 if ((i & 1) == 0)
00114 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
00115 else
00116 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
00117 pixel_ptr++;
00118 }
00119 }
00120 }
00121
00122
00123 if (stream_ptr < data_size) {
00124 av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
00125 stream_ptr, data_size);
00126 return -1;
00127 }
00128
00129 return 0;
00130 }
00131
00132
00133 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
00134 const uint8_t *data, int srcsize)
00135 {
00136 uint8_t *output, *output_end;
00137 const uint8_t* src = data;
00138 int p1, p2, line=avctx->height - 1, pos=0, i;
00139 uint16_t av_uninit(pix16);
00140 uint32_t av_uninit(pix32);
00141
00142 output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
00143 output_end = pic->data[0] + (avctx->height) * pic->linesize[0];
00144 while(src < data + srcsize) {
00145 p1 = *src++;
00146 if(p1 == 0) {
00147 p2 = *src++;
00148 if(p2 == 0) {
00149 output = pic->data[0] + (--line) * pic->linesize[0];
00150 if (line < 0 && !(src+1 < data + srcsize && AV_RB16(src) == 1)) {
00151 av_log(avctx, AV_LOG_ERROR, "Next line is beyond picture bounds\n");
00152 return -1;
00153 }
00154 pos = 0;
00155 continue;
00156 } else if(p2 == 1) {
00157 return 0;
00158 } else if(p2 == 2) {
00159 p1 = *src++;
00160 p2 = *src++;
00161 line -= p2;
00162 if (line < 0){
00163 av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
00164 return -1;
00165 }
00166 pos += p1;
00167 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
00168 continue;
00169 }
00170
00171 if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end)
00172 ||(pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
00173 src += p2 * (depth >> 3);
00174 continue;
00175 }
00176 if ((depth == 8) || (depth == 24)) {
00177 for(i = 0; i < p2 * (depth >> 3); i++) {
00178 *output++ = *src++;
00179 }
00180
00181 if(depth == 8 && (p2 & 1)) {
00182 src++;
00183 }
00184 } else if (depth == 16) {
00185 for(i = 0; i < p2; i++) {
00186 pix16 = AV_RL16(src);
00187 src += 2;
00188 *(uint16_t*)output = pix16;
00189 output += 2;
00190 }
00191 } else if (depth == 32) {
00192 for(i = 0; i < p2; i++) {
00193 pix32 = AV_RL32(src);
00194 src += 4;
00195 *(uint32_t*)output = pix32;
00196 output += 4;
00197 }
00198 }
00199 pos += p2;
00200 } else {
00201 uint8_t pix[3];
00202 switch(depth){
00203 case 8: pix[0] = *src++;
00204 break;
00205 case 16: pix16 = AV_RL16(src);
00206 src += 2;
00207 break;
00208 case 24: pix[0] = *src++;
00209 pix[1] = *src++;
00210 pix[2] = *src++;
00211 break;
00212 case 32: pix32 = AV_RL32(src);
00213 src += 4;
00214 break;
00215 }
00216 if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end)
00217 ||(pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
00218 continue;
00219 for(i = 0; i < p1; i++) {
00220 switch(depth){
00221 case 8: *output++ = pix[0];
00222 break;
00223 case 16: *(uint16_t*)output = pix16;
00224 output += 2;
00225 break;
00226 case 24: *output++ = pix[0];
00227 *output++ = pix[1];
00228 *output++ = pix[2];
00229 break;
00230 case 32: *(uint32_t*)output = pix32;
00231 output += 4;
00232 break;
00233 }
00234 }
00235 pos += p1;
00236 }
00237 }
00238
00239 av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no end-of-picture code\n");
00240 return 0;
00241 }
00242
00243
00244 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
00245 const uint8_t* data, int data_size)
00246 {
00247 switch(depth){
00248 case 4:
00249 return msrle_decode_pal4(avctx, pic, data, data_size);
00250 case 8:
00251 case 16:
00252 case 24:
00253 case 32:
00254 return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
00255 default:
00256 av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
00257 return -1;
00258 }
00259 }
00260