comparison src/ffmpeg/libavcodec/aasc.c @ 808:e8776388b02a trunk

[svn] - add ffmpeg
author nenolod
date Mon, 12 Mar 2007 11:18:54 -0700
parents
children
comparison
equal deleted inserted replaced
807:0f9c8d4d3ac4 808:e8776388b02a
1 /*
2 * Autodesc RLE Decoder
3 * Copyright (C) 2005 the ffmpeg project
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file aasc.c
24 * Autodesc RLE Video Decoder by Konstantin Shishkov
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "common.h"
32 #include "avcodec.h"
33 #include "dsputil.h"
34
35 typedef struct AascContext {
36 AVCodecContext *avctx;
37 AVFrame frame;
38 } AascContext;
39
40 #define FETCH_NEXT_STREAM_BYTE() \
41 if (stream_ptr >= buf_size) \
42 { \
43 av_log(s->avctx, AV_LOG_ERROR, " AASC: stream ptr just went out of bounds (fetch)\n"); \
44 break; \
45 } \
46 stream_byte = buf[stream_ptr++];
47
48 static int aasc_decode_init(AVCodecContext *avctx)
49 {
50 AascContext *s = (AascContext *)avctx->priv_data;
51
52 s->avctx = avctx;
53
54 avctx->pix_fmt = PIX_FMT_BGR24;
55 avctx->has_b_frames = 0;
56 s->frame.data[0] = NULL;
57
58 return 0;
59 }
60
61 static int aasc_decode_frame(AVCodecContext *avctx,
62 void *data, int *data_size,
63 uint8_t *buf, int buf_size)
64 {
65 AascContext *s = (AascContext *)avctx->priv_data;
66 int stream_ptr = 4;
67 unsigned char rle_code;
68 unsigned char stream_byte;
69 int pixel_ptr = 0;
70 int row_dec, row_ptr;
71 int frame_size;
72 int i;
73
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");
78 return -1;
79 }
80
81 row_dec = s->frame.linesize[0];
82 row_ptr = (s->avctx->height - 1) * row_dec;
83 frame_size = row_dec * s->avctx->height;
84
85 while (row_ptr >= 0) {
86 FETCH_NEXT_STREAM_BYTE();
87 rle_code = stream_byte;
88 if (rle_code == 0) {
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 */
93 row_ptr -= row_dec;
94 pixel_ptr = 0;
95 } else if (stream_byte == 1) {
96 /* decode is done */
97 break;
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;
104 } else {
105 /* copy pixels from encoded stream */
106 if ((pixel_ptr + stream_byte > avctx->width * 3) ||
107 (row_ptr < 0)) {
108 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (copy1)\n");
109 break;
110 }
111
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");
115 break;
116 }
117
118 for (i = 0; i < rle_code; i++) {
119 FETCH_NEXT_STREAM_BYTE();
120 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
121 pixel_ptr++;
122 }
123 if (rle_code & 1)
124 stream_ptr++;
125 }
126 } else {
127 /* decode a run of data */
128 if ((pixel_ptr + rle_code > avctx->width * 3) ||
129 (row_ptr < 0)) {
130 av_log(s->avctx, AV_LOG_ERROR, " AASC: frame ptr just went out of bounds (run1)\n");
131 break;
132 }
133
134 FETCH_NEXT_STREAM_BYTE();
135
136 while(rle_code--) {
137 s->frame.data[0][row_ptr + pixel_ptr] = stream_byte;
138 pixel_ptr++;
139 }
140 }
141 }
142
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);
147
148 *data_size = sizeof(AVFrame);
149 *(AVFrame*)data = s->frame;
150
151 /* report that the buffer was completely consumed */
152 return buf_size;
153 }
154
155 static int aasc_decode_end(AVCodecContext *avctx)
156 {
157 AascContext *s = (AascContext *)avctx->priv_data;
158
159 /* release the last frame */
160 if (s->frame.data[0])
161 avctx->release_buffer(avctx, &s->frame);
162
163 return 0;
164 }
165
166 AVCodec aasc_decoder = {
167 "aasc",
168 CODEC_TYPE_VIDEO,
169 CODEC_ID_AASC,
170 sizeof(AascContext),
171 aasc_decode_init,
172 NULL,
173 aasc_decode_end,
174 aasc_decode_frame,
175 CODEC_CAP_DR1,
176 };