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 avctx->has_b_frames = 0;
56 s->frame.data[0] = NULL;
61 static int aasc_decode_frame(AVCodecContext *avctx,
62 void *data, int *data_size,
63 uint8_t *buf, int buf_size)
65 AascContext *s = (AascContext *)avctx->priv_data;
67 unsigned char rle_code;
68 unsigned char stream_byte;
74 s->frame.reference = 1;
75 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
76 if (avctx->reget_buffer(avctx, &s->frame)) {
77 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
81 row_dec = s->frame.linesize[0];
82 row_ptr = (s->avctx->height - 1) * row_dec;
83 frame_size = row_dec * s->avctx->height;
85 while (row_ptr >= 0) {
86 FETCH_NEXT_STREAM_BYTE();
87 rle_code = stream_byte;
89 /* fetch the next byte to see how to handle escape code */
90 FETCH_NEXT_STREAM_BYTE();
91 if (stream_byte == 0) {
92 /* line is done, goto the next one */
95 } else if (stream_byte == 1) {
98 } else if (stream_byte == 2) {
99 /* reposition frame decode coordinates */
100 FETCH_NEXT_STREAM_BYTE();
101 pixel_ptr += stream_byte;
102 FETCH_NEXT_STREAM_BYTE();
103 row_ptr -= stream_byte * row_dec;
105 /* copy pixels from encoded stream */
106 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
108 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
112 rle_code = stream_byte;
113 if (stream_ptr + rle_code > buf_size) {
114 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (copy2)\n");
118 for (i = 0; i < rle_code; i++) {
119 FETCH_NEXT_STREAM_BYTE();
120 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
127 /* decode a run of data */
128 if ((pixel_ptr + rle_code > avctx->width * 3) ||
130 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
134 FETCH_NEXT_STREAM_BYTE();
137 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
143 /* one last sanity check on the way out */
144 if (stream_ptr < buf_size)
145 av_log(s->avctx, AV_LOG_ERROR, " AASC: ended frame decode with bytes left over (%d < %d)\n",
146 stream_ptr, buf_size);
148 *data_size = sizeof(AVFrame);
149 *(AVFrame*)data = s->frame;
151 /* report that the buffer was completely consumed */
155 static int aasc_decode_end(AVCodecContext *avctx)
157 AascContext *s = (AascContext *)avctx->priv_data;
159 /* release the last frame */
160 if (s->frame.data[0])
161 avctx->release_buffer(avctx, &s->frame);
166 AVCodec aasc_decoder = {