• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/msrledec.c

Go to the documentation of this file.
00001 /*
00002  * Micrsoft RLE Decoder
00003  * Copyright (C) 2008 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00029 #include "libavutil/intreadwrite.h"
00030 #include "avcodec.h"
00031 
00032 #define FETCH_NEXT_STREAM_BYTE() \
00033     if (stream_ptr >= data_size) \
00034     { \
00035       av_log(avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
00036       return -1; \
00037     } \
00038     stream_byte = data[stream_ptr++];
00039 
00040 static int msrle_decode_pal4(AVCodecContext *avctx, AVPicture *pic,
00041                               const uint8_t *data, int data_size)
00042 {
00043     int stream_ptr = 0;
00044     unsigned char rle_code;
00045     unsigned char extra_byte, odd_pixel;
00046     unsigned char stream_byte;
00047     int pixel_ptr = 0;
00048     int row_dec = pic->linesize[0];
00049     int row_ptr = (avctx->height - 1) * row_dec;
00050     int frame_size = row_dec * avctx->height;
00051     int i;
00052 
00053     while (row_ptr >= 0) {
00054         FETCH_NEXT_STREAM_BYTE();
00055         rle_code = stream_byte;
00056         if (rle_code == 0) {
00057             /* fetch the next byte to see how to handle escape code */
00058             FETCH_NEXT_STREAM_BYTE();
00059             if (stream_byte == 0) {
00060                 /* line is done, goto the next one */
00061                 row_ptr -= row_dec;
00062                 pixel_ptr = 0;
00063             } else if (stream_byte == 1) {
00064                 /* decode is done */
00065                 return 0;
00066             } else if (stream_byte == 2) {
00067                 /* reposition frame decode coordinates */
00068                 FETCH_NEXT_STREAM_BYTE();
00069                 pixel_ptr += stream_byte;
00070                 FETCH_NEXT_STREAM_BYTE();
00071                 row_ptr -= stream_byte * row_dec;
00072         } else {
00073             // copy pixels from encoded stream
00074             odd_pixel =  stream_byte & 1;
00075             rle_code = (stream_byte + 1) / 2;
00076             extra_byte = rle_code & 0x01;
00077             if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
00078                 (row_ptr < 0)) {
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             // if the RLE code is odd, skip a byte in the stream
00098             if (extra_byte)
00099               stream_ptr++;
00100             }
00101         } else {
00102             // decode a run of data
00103             if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
00104                 (row_ptr < 0)) {
00105                 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
00106                 return -1;
00107             }
00108             FETCH_NEXT_STREAM_BYTE();
00109             for (i = 0; i < rle_code; i++) {
00110                 if (pixel_ptr >= avctx->width)
00111                     break;
00112                 if ((i & 1) == 0)
00113                     pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
00114                 else
00115                     pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
00116                 pixel_ptr++;
00117             }
00118         }
00119     }
00120 
00121     /* one last sanity check on the way out */
00122     if (stream_ptr < data_size) {
00123         av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
00124             stream_ptr, data_size);
00125         return -1;
00126     }
00127 
00128     return 0;
00129 }
00130 
00131 
00132 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
00133                                     const uint8_t *data, int srcsize)
00134 {
00135     uint8_t *output, *output_end;
00136     const uint8_t* src = data;
00137     int p1, p2, line=avctx->height, pos=0, i;
00138     uint16_t av_uninit(pix16);
00139     uint32_t av_uninit(pix32);
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) { //Escape code
00146             p2 = *src++;
00147             if(p2 == 0) { //End-of-line
00148                 output = pic->data[0] + (--line) * pic->linesize[0];
00149                 if (line < 0){
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) { //End-of-picture
00156                 return 0;
00157             } else if(p2 == 2) { //Skip
00158                 p1 = *src++;
00159                 p2 = *src++;
00160                 line -= p2;
00161                 if (line < 0){
00162                     av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
00163                     return -1;
00164                 }
00165                 pos += p1;
00166                 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
00167                 continue;
00168             }
00169             // Copy data
00170             if (output + p2 * (depth >> 3) > output_end) {
00171                 src += p2 * (depth >> 3);
00172                 continue;
00173             }
00174             if ((depth == 8) || (depth == 24)) {
00175                 for(i = 0; i < p2 * (depth >> 3); i++) {
00176                     *output++ = *src++;
00177                 }
00178                 // RLE8 copy is actually padded - and runs are not!
00179                 if(depth == 8 && (p2 & 1)) {
00180                     src++;
00181                 }
00182             } else if (depth == 16) {
00183                 for(i = 0; i < p2; i++) {
00184                     pix16 = AV_RL16(src);
00185                     src += 2;
00186                     *(uint16_t*)output = pix16;
00187                     output += 2;
00188                 }
00189             } else if (depth == 32) {
00190                 for(i = 0; i < p2; i++) {
00191                     pix32 = AV_RL32(src);
00192                     src += 4;
00193                     *(uint32_t*)output = pix32;
00194                     output += 4;
00195                 }
00196             }
00197             pos += p2;
00198         } else { //Run of pixels
00199             uint8_t pix[3]; //original pixel
00200             switch(depth){
00201             case  8: pix[0] = *src++;
00202                      break;
00203             case 16: pix16 = AV_RL16(src);
00204                      src += 2;
00205                      break;
00206             case 24: pix[0] = *src++;
00207                      pix[1] = *src++;
00208                      pix[2] = *src++;
00209                      break;
00210             case 32: pix32 = AV_RL32(src);
00211                      src += 4;
00212                      break;
00213             }
00214             if (output + p1 * (depth >> 3) > output_end)
00215                 continue;
00216             for(i = 0; i < p1; i++) {
00217                 switch(depth){
00218                 case  8: *output++ = pix[0];
00219                          break;
00220                 case 16: *(uint16_t*)output = pix16;
00221                          output += 2;
00222                          break;
00223                 case 24: *output++ = pix[0];
00224                          *output++ = pix[1];
00225                          *output++ = pix[2];
00226                          break;
00227                 case 32: *(uint32_t*)output = pix32;
00228                          output += 4;
00229                          break;
00230                 }
00231             }
00232             pos += p1;
00233         }
00234     }
00235 
00236     av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no End-of-picture code\n");
00237     return 0;
00238 }
00239 
00240 
00241 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
00242                     const uint8_t* data, int data_size)
00243 {
00244     switch(depth){
00245     case  4:
00246         return msrle_decode_pal4(avctx, pic, data, data_size);
00247     case  8:
00248     case 16:
00249     case 24:
00250     case 32:
00251         return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
00252     default:
00253         av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
00254         return -1;
00255     }
00256 }
00257 

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1