comparison aasc.c @ 2588:b6b618986f80 libavcodec

Autodesk Animator Studio Codec (AASC) video decoder, courtesy of Konstantin Shishkov
author melanson
date Sun, 03 Apr 2005 05:02:08 +0000
parents
children 0b546eab515d
comparison
equal deleted inserted replaced
2587:1e8fbc2b64e0 2588:b6b618986f80
1 /*
2 * Autodesc RLE Decoder
3 * Copyright (C) 2005 the ffmpeg project
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /**
21 * @file aasc.c
22 * Autodesc RLE Video Decoder by Konstantin Shishkov
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "common.h"
30 #include "avcodec.h"
31 #include "dsputil.h"
32
33 typedef struct AascContext {
34 AVCodecContext *avctx;
35 AVFrame frame;
36 } AascContext;
37
38 #define FETCH_NEXT_STREAM_BYTE() \
39 if (stream_ptr >= buf_size) \
40 { \
41 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
42 break; \
43 } \
44 stream_byte = buf[stream_ptr++];
45
46 static int aasc_decode_init(AVCodecContext *avctx)
47 {
48 AascContext *s = (AascContext *)avctx->priv_data;
49
50 s->avctx = avctx;
51
52 avctx->pix_fmt = PIX_FMT_BGR24;
53 avctx->has_b_frames = 0;
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 uint8_t *buf, int buf_size)
62 {
63 AascContext *s = (AascContext *)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 = (AascContext *)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 };