Merge avfiltergraphdesc.c in avfiltergraph.c
[ffmpeg-lucabe.git] / libavcodec / aasc.c
blobeec26468bc1c7a8ef555a535acb729ef0ef95cc9
1 /*
2 * Autodesc RLE Decoder
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
22 /**
23 * @file aasc.c
24 * Autodesc RLE Video Decoder by Konstantin Shishkov
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "avcodec.h"
32 #include "dsputil.h"
34 typedef struct AascContext {
35 AVCodecContext *avctx;
36 AVFrame frame;
37 } AascContext;
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++];
47 static av_cold int aasc_decode_init(AVCodecContext *avctx)
49 AascContext *s = avctx->priv_data;
51 s->avctx = avctx;
53 avctx->pix_fmt = PIX_FMT_BGR24;
54 s->frame.data[0] = NULL;
56 return 0;
59 static int aasc_decode_frame(AVCodecContext *avctx,
60 void *data, int *data_size,
61 const uint8_t *buf, int buf_size)
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;
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;
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;
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;
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;
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++;
121 if (rle_code & 1)
122 stream_ptr++;
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;
132 FETCH_NEXT_STREAM_BYTE();
134 while(rle_code--) {
135 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
136 pixel_ptr++;
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 */
150 return buf_size;
153 static av_cold 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);
161 return 0;
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,