yading@10
|
1 /*
|
yading@10
|
2 * Intel Indeo 2 codec
|
yading@10
|
3 * Copyright (c) 2005 Konstantin Shishkov
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * Intel Indeo 2 decoder.
|
yading@10
|
25 */
|
yading@10
|
26
|
yading@10
|
27 #define BITSTREAM_READER_LE
|
yading@10
|
28 #include "libavutil/attributes.h"
|
yading@10
|
29 #include "avcodec.h"
|
yading@10
|
30 #include "get_bits.h"
|
yading@10
|
31 #include "indeo2data.h"
|
yading@10
|
32 #include "internal.h"
|
yading@10
|
33 #include "mathops.h"
|
yading@10
|
34
|
yading@10
|
35 typedef struct Ir2Context{
|
yading@10
|
36 AVCodecContext *avctx;
|
yading@10
|
37 AVFrame picture;
|
yading@10
|
38 GetBitContext gb;
|
yading@10
|
39 int decode_delta;
|
yading@10
|
40 } Ir2Context;
|
yading@10
|
41
|
yading@10
|
42 #define CODE_VLC_BITS 14
|
yading@10
|
43 static VLC ir2_vlc;
|
yading@10
|
44
|
yading@10
|
45 /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
|
yading@10
|
46 static inline int ir2_get_code(GetBitContext *gb)
|
yading@10
|
47 {
|
yading@10
|
48 return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
|
yading@10
|
49 }
|
yading@10
|
50
|
yading@10
|
51 static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
|
yading@10
|
52 int stride, const uint8_t *table)
|
yading@10
|
53 {
|
yading@10
|
54 int i;
|
yading@10
|
55 int j;
|
yading@10
|
56 int out = 0;
|
yading@10
|
57 int c;
|
yading@10
|
58 int t;
|
yading@10
|
59
|
yading@10
|
60 if (width & 1)
|
yading@10
|
61 return AVERROR_INVALIDDATA;
|
yading@10
|
62
|
yading@10
|
63 /* first line contain absolute values, other lines contain deltas */
|
yading@10
|
64 while (out < width) {
|
yading@10
|
65 c = ir2_get_code(&ctx->gb);
|
yading@10
|
66 if (c >= 0x80) { /* we have a run */
|
yading@10
|
67 c -= 0x7F;
|
yading@10
|
68 if (out + c*2 > width)
|
yading@10
|
69 return AVERROR_INVALIDDATA;
|
yading@10
|
70 for (i = 0; i < c * 2; i++)
|
yading@10
|
71 dst[out++] = 0x80;
|
yading@10
|
72 } else { /* copy two values from table */
|
yading@10
|
73 dst[out++] = table[c * 2];
|
yading@10
|
74 dst[out++] = table[(c * 2) + 1];
|
yading@10
|
75 }
|
yading@10
|
76 }
|
yading@10
|
77 dst += stride;
|
yading@10
|
78
|
yading@10
|
79 for (j = 1; j < height; j++) {
|
yading@10
|
80 out = 0;
|
yading@10
|
81 while (out < width) {
|
yading@10
|
82 c = ir2_get_code(&ctx->gb);
|
yading@10
|
83 if (c >= 0x80) { /* we have a skip */
|
yading@10
|
84 c -= 0x7F;
|
yading@10
|
85 if (out + c*2 > width)
|
yading@10
|
86 return AVERROR_INVALIDDATA;
|
yading@10
|
87 for (i = 0; i < c * 2; i++) {
|
yading@10
|
88 dst[out] = dst[out - stride];
|
yading@10
|
89 out++;
|
yading@10
|
90 }
|
yading@10
|
91 } else { /* add two deltas from table */
|
yading@10
|
92 t = dst[out - stride] + (table[c * 2] - 128);
|
yading@10
|
93 t = av_clip_uint8(t);
|
yading@10
|
94 dst[out] = t;
|
yading@10
|
95 out++;
|
yading@10
|
96 t = dst[out - stride] + (table[(c * 2) + 1] - 128);
|
yading@10
|
97 t = av_clip_uint8(t);
|
yading@10
|
98 dst[out] = t;
|
yading@10
|
99 out++;
|
yading@10
|
100 }
|
yading@10
|
101 }
|
yading@10
|
102 dst += stride;
|
yading@10
|
103 }
|
yading@10
|
104 return 0;
|
yading@10
|
105 }
|
yading@10
|
106
|
yading@10
|
107 static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
|
yading@10
|
108 int stride, const uint8_t *table)
|
yading@10
|
109 {
|
yading@10
|
110 int j;
|
yading@10
|
111 int out = 0;
|
yading@10
|
112 int c;
|
yading@10
|
113 int t;
|
yading@10
|
114
|
yading@10
|
115 if (width & 1)
|
yading@10
|
116 return AVERROR_INVALIDDATA;
|
yading@10
|
117
|
yading@10
|
118 for (j = 0; j < height; j++) {
|
yading@10
|
119 out = 0;
|
yading@10
|
120 while (out < width) {
|
yading@10
|
121 c = ir2_get_code(&ctx->gb);
|
yading@10
|
122 if (c >= 0x80) { /* we have a skip */
|
yading@10
|
123 c -= 0x7F;
|
yading@10
|
124 out += c * 2;
|
yading@10
|
125 } else { /* add two deltas from table */
|
yading@10
|
126 t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
|
yading@10
|
127 t = av_clip_uint8(t);
|
yading@10
|
128 dst[out] = t;
|
yading@10
|
129 out++;
|
yading@10
|
130 t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
|
yading@10
|
131 t = av_clip_uint8(t);
|
yading@10
|
132 dst[out] = t;
|
yading@10
|
133 out++;
|
yading@10
|
134 }
|
yading@10
|
135 }
|
yading@10
|
136 dst += stride;
|
yading@10
|
137 }
|
yading@10
|
138 return 0;
|
yading@10
|
139 }
|
yading@10
|
140
|
yading@10
|
141 static int ir2_decode_frame(AVCodecContext *avctx,
|
yading@10
|
142 void *data, int *got_frame,
|
yading@10
|
143 AVPacket *avpkt)
|
yading@10
|
144 {
|
yading@10
|
145 Ir2Context * const s = avctx->priv_data;
|
yading@10
|
146 const uint8_t *buf = avpkt->data;
|
yading@10
|
147 int buf_size = avpkt->size;
|
yading@10
|
148 AVFrame *picture = data;
|
yading@10
|
149 AVFrame * const p = &s->picture;
|
yading@10
|
150 int start, ret;
|
yading@10
|
151
|
yading@10
|
152 if ((ret = ff_reget_buffer(avctx, p)) < 0)
|
yading@10
|
153 return ret;
|
yading@10
|
154
|
yading@10
|
155 start = 48; /* hardcoded for now */
|
yading@10
|
156
|
yading@10
|
157 if (start >= buf_size) {
|
yading@10
|
158 av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
|
yading@10
|
159 return AVERROR_INVALIDDATA;
|
yading@10
|
160 }
|
yading@10
|
161
|
yading@10
|
162 s->decode_delta = buf[18];
|
yading@10
|
163
|
yading@10
|
164 /* decide whether frame uses deltas or not */
|
yading@10
|
165 #ifndef BITSTREAM_READER_LE
|
yading@10
|
166 for (i = 0; i < buf_size; i++)
|
yading@10
|
167 buf[i] = ff_reverse[buf[i]];
|
yading@10
|
168 #endif
|
yading@10
|
169
|
yading@10
|
170 init_get_bits(&s->gb, buf + start, (buf_size - start) * 8);
|
yading@10
|
171
|
yading@10
|
172 if (s->decode_delta) { /* intraframe */
|
yading@10
|
173 if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
|
yading@10
|
174 s->picture.data[0], s->picture.linesize[0],
|
yading@10
|
175 ir2_luma_table)) < 0)
|
yading@10
|
176 return ret;
|
yading@10
|
177
|
yading@10
|
178 /* swapped U and V */
|
yading@10
|
179 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
|
yading@10
|
180 s->picture.data[2], s->picture.linesize[2],
|
yading@10
|
181 ir2_luma_table)) < 0)
|
yading@10
|
182 return ret;
|
yading@10
|
183 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
|
yading@10
|
184 s->picture.data[1], s->picture.linesize[1],
|
yading@10
|
185 ir2_luma_table)) < 0)
|
yading@10
|
186 return ret;
|
yading@10
|
187 } else { /* interframe */
|
yading@10
|
188 if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
|
yading@10
|
189 s->picture.data[0], s->picture.linesize[0],
|
yading@10
|
190 ir2_luma_table)) < 0)
|
yading@10
|
191 return ret;
|
yading@10
|
192 /* swapped U and V */
|
yading@10
|
193 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
|
yading@10
|
194 s->picture.data[2], s->picture.linesize[2],
|
yading@10
|
195 ir2_luma_table)) < 0)
|
yading@10
|
196 return ret;
|
yading@10
|
197 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
|
yading@10
|
198 s->picture.data[1], s->picture.linesize[1],
|
yading@10
|
199 ir2_luma_table)) < 0)
|
yading@10
|
200 return ret;
|
yading@10
|
201 }
|
yading@10
|
202
|
yading@10
|
203 if ((ret = av_frame_ref(picture, &s->picture)) < 0)
|
yading@10
|
204 return ret;
|
yading@10
|
205
|
yading@10
|
206 *got_frame = 1;
|
yading@10
|
207
|
yading@10
|
208 return buf_size;
|
yading@10
|
209 }
|
yading@10
|
210
|
yading@10
|
211 static av_cold int ir2_decode_init(AVCodecContext *avctx)
|
yading@10
|
212 {
|
yading@10
|
213 Ir2Context * const ic = avctx->priv_data;
|
yading@10
|
214 static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
|
yading@10
|
215
|
yading@10
|
216 avcodec_get_frame_defaults(&ic->picture);
|
yading@10
|
217 ic->avctx = avctx;
|
yading@10
|
218
|
yading@10
|
219 avctx->pix_fmt= AV_PIX_FMT_YUV410P;
|
yading@10
|
220
|
yading@10
|
221 avcodec_get_frame_defaults(&ic->picture);
|
yading@10
|
222
|
yading@10
|
223 ir2_vlc.table = vlc_tables;
|
yading@10
|
224 ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
|
yading@10
|
225 #ifdef BITSTREAM_READER_LE
|
yading@10
|
226 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
|
yading@10
|
227 &ir2_codes[0][1], 4, 2,
|
yading@10
|
228 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
|
yading@10
|
229 #else
|
yading@10
|
230 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
|
yading@10
|
231 &ir2_codes[0][1], 4, 2,
|
yading@10
|
232 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
|
yading@10
|
233 #endif
|
yading@10
|
234
|
yading@10
|
235 return 0;
|
yading@10
|
236 }
|
yading@10
|
237
|
yading@10
|
238 static av_cold int ir2_decode_end(AVCodecContext *avctx)
|
yading@10
|
239 {
|
yading@10
|
240 Ir2Context * const ic = avctx->priv_data;
|
yading@10
|
241 AVFrame *pic = &ic->picture;
|
yading@10
|
242
|
yading@10
|
243 av_frame_unref(pic);
|
yading@10
|
244
|
yading@10
|
245 return 0;
|
yading@10
|
246 }
|
yading@10
|
247
|
yading@10
|
248 AVCodec ff_indeo2_decoder = {
|
yading@10
|
249 .name = "indeo2",
|
yading@10
|
250 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
251 .id = AV_CODEC_ID_INDEO2,
|
yading@10
|
252 .priv_data_size = sizeof(Ir2Context),
|
yading@10
|
253 .init = ir2_decode_init,
|
yading@10
|
254 .close = ir2_decode_end,
|
yading@10
|
255 .decode = ir2_decode_frame,
|
yading@10
|
256 .capabilities = CODEC_CAP_DR1,
|
yading@10
|
257 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
|
yading@10
|
258 };
|