]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/aasc.c
const
[frescor/ffmpeg.git] / libavcodec / aasc.c
1 /*
2  * Autodesc RLE Decoder
3  * Copyright (C) 2005 the ffmpeg project
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 aasc.c
24  * Autodesc RLE Video Decoder by Konstantin Shishkov
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "avcodec.h"
32 #include "dsputil.h"
33
34 typedef struct AascContext {
35     AVCodecContext *avctx;
36     AVFrame frame;
37 } AascContext;
38
39 #define FETCH_NEXT_STREAM_BYTE() \
40     if (stream_ptr >= buf_size) \
41     { \
42       av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
43       break; \
44     } \
45     stream_byte = buf[stream_ptr++];
46
47 static int aasc_decode_init(AVCodecContext *avctx)
48 {
49     AascContext *s = avctx->priv_data;
50
51     s->avctx = avctx;
52
53     avctx->pix_fmt = PIX_FMT_BGR24;
54     s->frame.data[0] = NULL;
55
56     return 0;
57 }
58
59 static int aasc_decode_frame(AVCodecContext *avctx,
60                               void *data, int *data_size,
61                               const uint8_t *buf, int buf_size)
62 {
63     AascContext *s = avctx->priv_data;
64     int stream_ptr = 4;
65     unsigned char rle_code;
66     unsigned char stream_byte;
67     int pixel_ptr = 0;
68     int row_dec, row_ptr;
69     int frame_size;
70     int i;
71
72     s->frame.reference = 1;
73     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
74     if (avctx->reget_buffer(avctx, &s->frame)) {
75         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
76         return -1;
77     }
78
79     row_dec = s->frame.linesize[0];
80     row_ptr = (s->avctx->height - 1) * row_dec;
81     frame_size = row_dec * s->avctx->height;
82
83     while (row_ptr >= 0) {
84         FETCH_NEXT_STREAM_BYTE();
85         rle_code = stream_byte;
86         if (rle_code == 0) {
87             /* fetch the next byte to see how to handle escape code */
88             FETCH_NEXT_STREAM_BYTE();
89             if (stream_byte == 0) {
90                 /* line is done, goto the next one */
91                 row_ptr -= row_dec;
92                 pixel_ptr = 0;
93             } else if (stream_byte == 1) {
94                 /* decode is done */
95                 break;
96             } else if (stream_byte == 2) {
97                 /* reposition frame decode coordinates */
98                 FETCH_NEXT_STREAM_BYTE();
99                 pixel_ptr += stream_byte;
100                 FETCH_NEXT_STREAM_BYTE();
101                 row_ptr -= stream_byte * row_dec;
102             } else {
103                 /* copy pixels from encoded stream */
104                 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
105                     (row_ptr < 0)) {
106                     av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
107                     break;
108                 }
109
110                 rle_code = stream_byte;
111                 if (stream_ptr + rle_code > buf_size) {
112                     av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n");
113                     break;
114                 }
115
116                 for (i = 0; i < rle_code; i++) {
117                     FETCH_NEXT_STREAM_BYTE();
118                     s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
119                     pixel_ptr++;
120                 }
121                 if (rle_code & 1)
122                     stream_ptr++;
123             }
124         } else {
125             /* decode a run of data */
126             if ((pixel_ptr + rle_code > avctx->width * 3) ||
127                 (row_ptr < 0)) {
128                 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
129                 break;
130             }
131
132             FETCH_NEXT_STREAM_BYTE();
133
134             while(rle_code--) {
135                 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
136                 pixel_ptr++;
137             }
138         }
139     }
140
141     /* one last sanity check on the way out */
142     if (stream_ptr < buf_size)
143         av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n",
144             stream_ptr, buf_size);
145
146     *data_size = sizeof(AVFrame);
147     *(AVFrame*)data = s->frame;
148
149     /* report that the buffer was completely consumed */
150     return buf_size;
151 }
152
153 static int aasc_decode_end(AVCodecContext *avctx)
154 {
155     AascContext *s = avctx->priv_data;
156
157     /* release the last frame */
158     if (s->frame.data[0])
159         avctx->release_buffer(avctx, &s->frame);
160
161     return 0;
162 }
163
164 AVCodec aasc_decoder = {
165     "aasc",
166     CODEC_TYPE_VIDEO,
167     CODEC_ID_AASC,
168     sizeof(AascContext),
169     aasc_decode_init,
170     NULL,
171     aasc_decode_end,
172     aasc_decode_frame,
173     CODEC_CAP_DR1,
174 };