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
35 typedef struct AascContext {
36 AVCodecContext *avctx;
40 #define FETCH_NEXT_STREAM_BYTE() \
41 if (stream_ptr >= buf_size) \
43 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
46 stream_byte = buf[stream_ptr++];
48 static int aasc_decode_init(AVCodecContext *avctx)
50 AascContext *s = (AascContext *)avctx->priv_data;
54 avctx->pix_fmt = PIX_FMT_BGR24;
55 s->frame.data[0] = NULL;
60 static int aasc_decode_frame(AVCodecContext *avctx,
61 void *data, int *data_size,
62 uint8_t *buf, int buf_size)
64 AascContext *s = (AascContext *)avctx->priv_data;
66 unsigned char rle_code;
67 unsigned char stream_byte;
73 s->frame.reference = 1;
74 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
75 if (avctx->reget_buffer(avctx, &s->frame)) {
76 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
80 row_dec = s->frame.linesize[0];
81 row_ptr = (s->avctx->height - 1) * row_dec;
82 frame_size = row_dec * s->avctx->height;
84 while (row_ptr >= 0) {
85 FETCH_NEXT_STREAM_BYTE();
86 rle_code = stream_byte;
88 /* fetch the next byte to see how to handle escape code */
89 FETCH_NEXT_STREAM_BYTE();
90 if (stream_byte == 0) {
91 /* line is done, goto the next one */
94 } else if (stream_byte == 1) {
97 } else if (stream_byte == 2) {
98 /* reposition frame decode coordinates */
99 FETCH_NEXT_STREAM_BYTE();
100 pixel_ptr += stream_byte;
101 FETCH_NEXT_STREAM_BYTE();
102 row_ptr -= stream_byte * row_dec;
104 /* copy pixels from encoded stream */
105 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
107 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
111 rle_code = stream_byte;
112 if (stream_ptr + rle_code > buf_size) {
113 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n");
117 for (i = 0; i < rle_code; i++) {
118 FETCH_NEXT_STREAM_BYTE();
119 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
126 /* decode a run of data */
127 if ((pixel_ptr + rle_code > avctx->width * 3) ||
129 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
133 FETCH_NEXT_STREAM_BYTE();
136 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
142 /* one last sanity check on the way out */
143 if (stream_ptr < buf_size)
144 av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n",
145 stream_ptr, buf_size);
147 *data_size = sizeof(AVFrame);
148 *(AVFrame*)data = s->frame;
150 /* report that the buffer was completely consumed */
154 static int aasc_decode_end(AVCodecContext *avctx)
156 AascContext *s = (AascContext *)avctx->priv_data;
158 /* release the last frame */
159 if (s->frame.data[0])
160 avctx->release_buffer(avctx, &s->frame);
165 AVCodec aasc_decoder = {