3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Intel Indeo 2 decoder.
27 #include "libavutil/attributes.h"
29 #define BITSTREAM_READER_LE
31 #include "bitstream.h"
32 #include "indeo2data.h"
37 typedef struct Ir2Context{
38 AVCodecContext *avctx;
44 #define CODE_VLC_BITS 14
47 /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
48 static inline int ir2_get_code(BitstreamContext *bc)
50 return bitstream_read_vlc(bc, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
53 static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
54 int pitch, const uint8_t *table)
63 return AVERROR_INVALIDDATA;
65 /* first line contain absolute values, other lines contain deltas */
67 c = ir2_get_code(&ctx->bc);
68 if (c >= 0x80) { /* we have a run */
70 if (out + c*2 > width)
71 return AVERROR_INVALIDDATA;
72 for (i = 0; i < c * 2; i++)
74 } else { /* copy two values from table */
75 dst[out++] = table[c * 2];
76 dst[out++] = table[(c * 2) + 1];
81 for (j = 1; j < height; j++) {
84 c = ir2_get_code(&ctx->bc);
85 if (c >= 0x80) { /* we have a skip */
87 if (out + c*2 > width)
88 return AVERROR_INVALIDDATA;
89 for (i = 0; i < c * 2; i++) {
90 dst[out] = dst[out - pitch];
93 } else { /* add two deltas from table */
94 t = dst[out - pitch] + (table[c * 2] - 128);
98 t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
109 static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
110 int pitch, const uint8_t *table)
118 return AVERROR_INVALIDDATA;
120 for (j = 0; j < height; j++) {
122 while (out < width) {
123 c = ir2_get_code(&ctx->bc);
124 if (c >= 0x80) { /* we have a skip */
127 } else { /* add two deltas from table */
128 t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
129 t = av_clip_uint8(t);
132 t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
133 t = av_clip_uint8(t);
143 static int ir2_decode_frame(AVCodecContext *avctx,
144 void *data, int *got_frame,
147 Ir2Context * const s = avctx->priv_data;
148 const uint8_t *buf = avpkt->data;
149 int buf_size = avpkt->size;
150 AVFrame *picture = data;
151 AVFrame * const p = s->picture;
155 if ((ret = ff_reget_buffer(avctx, p)) < 0) {
156 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
160 start = 48; /* hardcoded for now */
162 if (start >= buf_size) {
163 av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
164 return AVERROR_INVALIDDATA;
167 s->decode_delta = buf[18];
169 /* decide whether frame uses deltas or not */
170 #ifndef BITSTREAM_READER_LE
171 for (i = 0; i < buf_size; i++)
172 buf[i] = ff_reverse[buf[i]];
175 bitstream_init8(&s->bc, buf + start, buf_size - start);
177 ltab = buf[0x22] & 3;
178 ctab = buf[0x22] >> 2;
179 if (s->decode_delta) { /* intraframe */
180 if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
181 p->data[0], p->linesize[0],
182 ir2_delta_table[ltab])) < 0)
185 /* swapped U and V */
186 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
187 p->data[2], p->linesize[2],
188 ir2_delta_table[ctab])) < 0)
190 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
191 p->data[1], p->linesize[1],
192 ir2_delta_table[ctab])) < 0)
194 } else { /* interframe */
195 if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
196 p->data[0], p->linesize[0],
197 ir2_delta_table[ltab])) < 0)
199 /* swapped U and V */
200 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
201 p->data[2], p->linesize[2],
202 ir2_delta_table[ctab])) < 0)
204 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
205 p->data[1], p->linesize[1],
206 ir2_delta_table[ctab])) < 0)
210 if ((ret = av_frame_ref(picture, p)) < 0)
218 static av_cold int ir2_decode_init(AVCodecContext *avctx)
220 Ir2Context * const ic = avctx->priv_data;
221 static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
225 avctx->pix_fmt= AV_PIX_FMT_YUV410P;
227 ic->picture = av_frame_alloc();
229 return AVERROR(ENOMEM);
231 ir2_vlc.table = vlc_tables;
232 ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
233 #ifdef BITSTREAM_READER_LE
234 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
235 &ir2_codes[0][1], 4, 2,
236 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
238 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
239 &ir2_codes[0][1], 4, 2,
240 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
246 static av_cold int ir2_decode_end(AVCodecContext *avctx)
248 Ir2Context * const ic = avctx->priv_data;
250 av_frame_free(&ic->picture);
255 AVCodec ff_indeo2_decoder = {
257 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
258 .type = AVMEDIA_TYPE_VIDEO,
259 .id = AV_CODEC_ID_INDEO2,
260 .priv_data_size = sizeof(Ir2Context),
261 .init = ir2_decode_init,
262 .close = ir2_decode_end,
263 .decode = ir2_decode_frame,
264 .capabilities = AV_CODEC_CAP_DR1,