3 * Copyright (C) 2005 the ffmpeg project
5 * This file is part of FFmpeg.
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.
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.
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
24 * Autodesc RLE Video Decoder by Konstantin Shishkov
34 typedef struct AascContext {
35 AVCodecContext *avctx;
39 #define FETCH_NEXT_STREAM_BYTE() \
40 if (stream_ptr >= buf_size) \
42 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
45 stream_byte = buf[stream_ptr++];
47 static int aasc_decode_init(AVCodecContext *avctx)
49 AascContext *s = avctx->priv_data;
53 avctx->pix_fmt = PIX_FMT_BGR24;
54 s->frame.data[0] = NULL;
59 static int aasc_decode_frame(AVCodecContext *avctx,
60 void *data, int *data_size,
61 uint8_t *buf, int buf_size)
63 AascContext *s = avctx->priv_data;
65 unsigned char rle_code;
66 unsigned char stream_byte;
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");
79 row_dec = s->frame.linesize[0];
80 row_ptr = (s->avctx->height - 1) * row_dec;
81 frame_size = row_dec * s->avctx->height;
83 while (row_ptr >= 0) {
84 FETCH_NEXT_STREAM_BYTE();
85 rle_code = stream_byte;
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 */
93 } else if (stream_byte == 1) {
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;
103 /* copy pixels from encoded stream */
104 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
106 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
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");
116 for (i = 0; i < rle_code; i++) {
117 FETCH_NEXT_STREAM_BYTE();
118 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
125 /* decode a run of data */
126 if ((pixel_ptr + rle_code > avctx->width * 3) ||
128 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
132 FETCH_NEXT_STREAM_BYTE();
135 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
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);
146 *data_size = sizeof(AVFrame);
147 *(AVFrame*)data = s->frame;
149 /* report that the buffer was completely consumed */
153 static int aasc_decode_end(AVCodecContext *avctx)
155 AascContext *s = avctx->priv_data;
157 /* release the last frame */
158 if (s->frame.data[0])
159 avctx->release_buffer(avctx, &s->frame);
164 AVCodec aasc_decoder = {