]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/msrledec.c
Started attempt to fix seek handling - still not completed
[frescor/ffmpeg.git] / libavcodec / msrledec.c
1 /*
2  * Micrsoft 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 libavcodec/msrledec.c
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
32 #define FETCH_NEXT_STREAM_BYTE() \
33     if (stream_ptr >= data_size) \
34     { \
35       av_log(avctx, AV_LOG_ERROR, " MS RLE: stream ptr just went out of bounds (1)\n"); \
36       return -1; \
37     } \
38     stream_byte = data[stream_ptr++];
39
40 static int msrle_decode_pal4(AVCodecContext *avctx, AVPicture *pic,
41                               const uint8_t *data, int data_size)
42 {
43     int stream_ptr = 0;
44     unsigned char rle_code;
45     unsigned char extra_byte, odd_pixel;
46     unsigned char stream_byte;
47     int pixel_ptr = 0;
48     int row_dec = pic->linesize[0];
49     int row_ptr = (avctx->height - 1) * row_dec;
50     int frame_size = row_dec * avctx->height;
51     int i;
52
53     while (row_ptr >= 0) {
54         FETCH_NEXT_STREAM_BYTE();
55         rle_code = stream_byte;
56         if (rle_code == 0) {
57             /* fetch the next byte to see how to handle escape code */
58             FETCH_NEXT_STREAM_BYTE();
59             if (stream_byte == 0) {
60                 /* line is done, goto the next one */
61                 row_ptr -= row_dec;
62                 pixel_ptr = 0;
63             } else if (stream_byte == 1) {
64                 /* decode is done */
65                 return 0;
66             } else if (stream_byte == 2) {
67                 /* reposition frame decode coordinates */
68                 FETCH_NEXT_STREAM_BYTE();
69                 pixel_ptr += stream_byte;
70                 FETCH_NEXT_STREAM_BYTE();
71                 row_ptr -= stream_byte * row_dec;
72         } else {
73             // copy pixels from encoded stream
74             odd_pixel =  stream_byte & 1;
75             rle_code = (stream_byte + 1) / 2;
76             extra_byte = rle_code & 0x01;
77             if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
78                 (row_ptr < 0)) {
79                 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
80                 return -1;
81             }
82
83             for (i = 0; i < rle_code; i++) {
84                 if (pixel_ptr >= avctx->width)
85                     break;
86                 FETCH_NEXT_STREAM_BYTE();
87                 pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
88                 pixel_ptr++;
89                 if (i + 1 == rle_code && odd_pixel)
90                     break;
91                 if (pixel_ptr >= avctx->width)
92                     break;
93                 pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
94                 pixel_ptr++;
95             }
96
97             // if the RLE code is odd, skip a byte in the stream
98             if (extra_byte)
99               stream_ptr++;
100             }
101         } else {
102             // decode a run of data
103             if ((row_ptr + pixel_ptr + stream_byte > frame_size) ||
104                 (row_ptr < 0)) {
105                 av_log(avctx, AV_LOG_ERROR, " MS RLE: frame ptr just went out of bounds (1)\n");
106                 return -1;
107             }
108             FETCH_NEXT_STREAM_BYTE();
109             for (i = 0; i < rle_code; i++) {
110                 if (pixel_ptr >= avctx->width)
111                     break;
112                 if ((i & 1) == 0)
113                     pic->data[0][row_ptr + pixel_ptr] = stream_byte >> 4;
114                 else
115                     pic->data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F;
116                 pixel_ptr++;
117             }
118         }
119     }
120
121     /* one last sanity check on the way out */
122     if (stream_ptr < data_size) {
123         av_log(avctx, AV_LOG_ERROR, " MS RLE: ended frame decode with bytes left over (%d < %d)\n",
124             stream_ptr, data_size);
125         return -1;
126     }
127
128     return 0;
129 }
130
131
132 static int msrle_decode_8_16_24_32(AVCodecContext *avctx, AVPicture *pic, int depth,
133                                     const uint8_t *data, int srcsize)
134 {
135     uint8_t *output, *output_end;
136     const uint8_t* src = data;
137     int p1, p2, line=avctx->height, pos=0, i;
138     uint16_t av_uninit(pix16);
139     uint32_t av_uninit(pix32);
140
141     output = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
142     output_end = pic->data[0] + (avctx->height) * pic->linesize[0];
143     while(src < data + srcsize) {
144         p1 = *src++;
145         if(p1 == 0) { //Escape code
146             p2 = *src++;
147             if(p2 == 0) { //End-of-line
148                 output = pic->data[0] + (--line) * pic->linesize[0];
149                 if (line < 0){
150                     av_log(avctx, AV_LOG_ERROR, "Next line is beyond picture bounds\n");
151                     return -1;
152                 }
153                 pos = 0;
154                 continue;
155             } else if(p2 == 1) { //End-of-picture
156                 return 0;
157             } else if(p2 == 2) { //Skip
158                 p1 = *src++;
159                 p2 = *src++;
160                 line -= p2;
161                 if (line < 0){
162                     av_log(avctx, AV_LOG_ERROR, "Skip beyond picture bounds\n");
163                     return -1;
164                 }
165                 pos += p1;
166                 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
167                 continue;
168             }
169             // Copy data
170             if (output + p2 * (depth >> 3) > output_end) {
171                 src += p2 * (depth >> 3);
172                 continue;
173             }
174             if ((depth == 8) || (depth == 24)) {
175                 for(i = 0; i < p2 * (depth >> 3); i++) {
176                     *output++ = *src++;
177                 }
178                 // RLE8 copy is actually padded - and runs are not!
179                 if(depth == 8 && (p2 & 1)) {
180                     src++;
181                 }
182             } else if (depth == 16) {
183                 for(i = 0; i < p2; i++) {
184                     pix16 = AV_RL16(src);
185                     src += 2;
186                     *(uint16_t*)output = pix16;
187                     output += 2;
188                 }
189             } else if (depth == 32) {
190                 for(i = 0; i < p2; i++) {
191                     pix32 = AV_RL32(src);
192                     src += 4;
193                     *(uint32_t*)output = pix32;
194                     output += 4;
195                 }
196             }
197             pos += p2;
198         } else { //Run of pixels
199             uint8_t pix[3]; //original pixel
200             switch(depth){
201             case  8: pix[0] = *src++;
202                      break;
203             case 16: pix16 = AV_RL16(src);
204                      src += 2;
205                      break;
206             case 24: pix[0] = *src++;
207                      pix[1] = *src++;
208                      pix[2] = *src++;
209                      break;
210             case 32: pix32 = AV_RL32(src);
211                      src += 4;
212                      break;
213             }
214             if (output + p1 * (depth >> 3) > output_end)
215                 continue;
216             for(i = 0; i < p1; i++) {
217                 switch(depth){
218                 case  8: *output++ = pix[0];
219                          break;
220                 case 16: *(uint16_t*)output = pix16;
221                          output += 2;
222                          break;
223                 case 24: *output++ = pix[0];
224                          *output++ = pix[1];
225                          *output++ = pix[2];
226                          break;
227                 case 32: *(uint32_t*)output = pix32;
228                          output += 4;
229                          break;
230                 }
231             }
232             pos += p1;
233         }
234     }
235
236     av_log(avctx, AV_LOG_WARNING, "MS RLE warning: no End-of-picture code\n");
237     return 0;
238 }
239
240
241 int ff_msrle_decode(AVCodecContext *avctx, AVPicture *pic, int depth,
242                     const uint8_t* data, int data_size)
243 {
244     switch(depth){
245     case  4:
246         return msrle_decode_pal4(avctx, pic, data, data_size);
247     case  8:
248     case 16:
249     case 24:
250     case 32:
251         return msrle_decode_8_16_24_32(avctx, pic, depth, data, data_size);
252     default:
253         av_log(avctx, AV_LOG_ERROR, "Unknown depth %d\n", depth);
254         return -1;
255     }
256 }
257