vlc: Add header #include when the types are used
[libav.git] / libavcodec / indeo2.c
CommitLineData
856dbbff 1/*
02c1592f 2 * Intel Indeo 2 codec
856dbbff
MN
3 * Copyright (c) 2005 Konstantin Shishkov
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
856dbbff
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
856dbbff 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
856dbbff
MN
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
856dbbff 20 */
115329f1 21
856dbbff 22/**
ba87f080 23 * @file
856dbbff
MN
24 * Intel Indeo 2 decoder.
25 */
d5c62122 26
d5c62122 27#include "libavutil/attributes.h"
b6686629
DB
28
29#define BITSTREAM_READER_LE
856dbbff 30#include "avcodec.h"
c3defda0 31#include "bitstream.h"
856dbbff 32#include "indeo2data.h"
759001c5 33#include "internal.h"
d5c62122 34#include "mathops.h"
73fc82f3 35#include "vlc.h"
856dbbff
MN
36
37typedef struct Ir2Context{
38 AVCodecContext *avctx;
79d501a8 39 AVFrame *picture;
c3defda0 40 BitstreamContext bc;
856dbbff
MN
41 int decode_delta;
42} Ir2Context;
43
44#define CODE_VLC_BITS 14
45static VLC ir2_vlc;
46
47/* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
c3defda0 48static inline int ir2_get_code(BitstreamContext *bc)
856dbbff 49{
c3defda0 50 return bitstream_read_vlc(bc, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
856dbbff 51}
856dbbff 52
c04c64c0 53static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
422e14f7 54 int pitch, const uint8_t *table)
856dbbff
MN
55{
56 int i;
57 int j;
58 int out = 0;
59 int c;
60 int t;
115329f1 61
c04c64c0 62 if (width & 1)
6ea2c9a4 63 return AVERROR_INVALIDDATA;
f707a5eb 64
856dbbff 65 /* first line contain absolute values, other lines contain deltas */
c04c64c0 66 while (out < width) {
c3defda0 67 c = ir2_get_code(&ctx->bc);
c04c64c0 68 if (c >= 0x80) { /* we have a run */
8b39f75b 69 c -= 0x7F;
c04c64c0 70 if (out + c*2 > width)
6ea2c9a4 71 return AVERROR_INVALIDDATA;
856dbbff
MN
72 for (i = 0; i < c * 2; i++)
73 dst[out++] = 0x80;
74 } else { /* copy two values from table */
75 dst[out++] = table[c * 2];
76 dst[out++] = table[(c * 2) + 1];
77 }
78 }
422e14f7 79 dst += pitch;
115329f1 80
c04c64c0 81 for (j = 1; j < height; j++) {
856dbbff 82 out = 0;
c04c64c0 83 while (out < width) {
c3defda0 84 c = ir2_get_code(&ctx->bc);
c04c64c0 85 if (c >= 0x80) { /* we have a skip */
8b39f75b 86 c -= 0x7F;
c04c64c0 87 if (out + c*2 > width)
6ea2c9a4 88 return AVERROR_INVALIDDATA;
856dbbff 89 for (i = 0; i < c * 2; i++) {
422e14f7 90 dst[out] = dst[out - pitch];
856dbbff
MN
91 out++;
92 }
93 } else { /* add two deltas from table */
422e14f7 94 t = dst[out - pitch] + (table[c * 2] - 128);
c04c64c0 95 t = av_clip_uint8(t);
856dbbff
MN
96 dst[out] = t;
97 out++;
422e14f7 98 t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
c04c64c0 99 t = av_clip_uint8(t);
856dbbff
MN
100 dst[out] = t;
101 out++;
102 }
103 }
422e14f7 104 dst += pitch;
856dbbff 105 }
f707a5eb 106 return 0;
856dbbff
MN
107}
108
c04c64c0 109static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
422e14f7 110 int pitch, const uint8_t *table)
856dbbff
MN
111{
112 int j;
113 int out = 0;
114 int c;
115 int t;
f707a5eb 116
c04c64c0 117 if (width & 1)
6ea2c9a4 118 return AVERROR_INVALIDDATA;
f707a5eb 119
c04c64c0 120 for (j = 0; j < height; j++) {
856dbbff 121 out = 0;
c04c64c0 122 while (out < width) {
c3defda0 123 c = ir2_get_code(&ctx->bc);
c04c64c0
AK
124 if (c >= 0x80) { /* we have a skip */
125 c -= 0x7F;
856dbbff
MN
126 out += c * 2;
127 } else { /* add two deltas from table */
c04c64c0
AK
128 t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
129 t = av_clip_uint8(t);
856dbbff
MN
130 dst[out] = t;
131 out++;
c04c64c0
AK
132 t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
133 t = av_clip_uint8(t);
856dbbff
MN
134 dst[out] = t;
135 out++;
136 }
137 }
422e14f7 138 dst += pitch;
856dbbff 139 }
f707a5eb 140 return 0;
856dbbff
MN
141}
142
115329f1 143static int ir2_decode_frame(AVCodecContext *avctx,
df9b9567 144 void *data, int *got_frame,
7a00bbad 145 AVPacket *avpkt)
856dbbff
MN
146{
147 Ir2Context * const s = avctx->priv_data;
c04c64c0
AK
148 const uint8_t *buf = avpkt->data;
149 int buf_size = avpkt->size;
150 AVFrame *picture = data;
79d501a8 151 AVFrame * const p = s->picture;
6ea2c9a4 152 int start, ret;
f8c34f4b 153 int ltab, ctab;
856dbbff 154
759001c5 155 if ((ret = ff_reget_buffer(avctx, p)) < 0) {
856dbbff 156 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
6ea2c9a4 157 return ret;
856dbbff
MN
158 }
159
b7ce4f1d
AC
160 start = 48; /* hardcoded for now */
161
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;
165 }
166
856dbbff 167 s->decode_delta = buf[18];
115329f1 168
856dbbff 169 /* decide whether frame uses deltas or not */
aaf47bcd 170#ifndef BITSTREAM_READER_LE
856dbbff 171 for (i = 0; i < buf_size; i++)
d5c62122 172 buf[i] = ff_reverse[buf[i]];
ef56de32 173#endif
856dbbff 174
a25dac97 175 bitstream_init8(&s->bc, buf + start, buf_size - start);
856dbbff 176
f8c34f4b
LB
177 ltab = buf[0x22] & 3;
178 ctab = buf[0x22] >> 2;
856dbbff 179 if (s->decode_delta) { /* intraframe */
7b1fbd47 180 if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
79d501a8 181 p->data[0], p->linesize[0],
f8c34f4b 182 ir2_delta_table[ltab])) < 0)
7b1fbd47
AK
183 return ret;
184
856dbbff 185 /* swapped U and V */
7b1fbd47 186 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 187 p->data[2], p->linesize[2],
f8c34f4b 188 ir2_delta_table[ctab])) < 0)
7b1fbd47
AK
189 return ret;
190 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 191 p->data[1], p->linesize[1],
f8c34f4b 192 ir2_delta_table[ctab])) < 0)
7b1fbd47 193 return ret;
856dbbff 194 } else { /* interframe */
7b1fbd47 195 if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
79d501a8 196 p->data[0], p->linesize[0],
f8c34f4b 197 ir2_delta_table[ltab])) < 0)
7b1fbd47 198 return ret;
856dbbff 199 /* swapped U and V */
7b1fbd47 200 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 201 p->data[2], p->linesize[2],
f8c34f4b 202 ir2_delta_table[ctab])) < 0)
7b1fbd47
AK
203 return ret;
204 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 205 p->data[1], p->linesize[1],
f8c34f4b 206 ir2_delta_table[ctab])) < 0)
7b1fbd47 207 return ret;
856dbbff
MN
208 }
209
79d501a8 210 if ((ret = av_frame_ref(picture, p)) < 0)
759001c5
AK
211 return ret;
212
df9b9567 213 *got_frame = 1;
856dbbff
MN
214
215 return buf_size;
216}
217
c04c64c0
AK
218static av_cold int ir2_decode_init(AVCodecContext *avctx)
219{
856dbbff 220 Ir2Context * const ic = avctx->priv_data;
bd4110f9 221 static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
856dbbff
MN
222
223 ic->avctx = avctx;
224
716d413c 225 avctx->pix_fmt= AV_PIX_FMT_YUV410P;
115329f1 226
79d501a8
AK
227 ic->picture = av_frame_alloc();
228 if (!ic->picture)
229 return AVERROR(ENOMEM);
3b199d29 230
bd4110f9
KS
231 ir2_vlc.table = vlc_tables;
232 ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
aaf47bcd 233#ifdef BITSTREAM_READER_LE
856dbbff
MN
234 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
235 &ir2_codes[0][1], 4, 2,
bd4110f9 236 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
ef56de32 237#else
c11aac6c
CL
238 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
239 &ir2_codes[0][1], 4, 2,
bd4110f9 240 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
ef56de32 241#endif
115329f1 242
856dbbff
MN
243 return 0;
244}
245
c04c64c0
AK
246static av_cold int ir2_decode_end(AVCodecContext *avctx)
247{
6d924b5a 248 Ir2Context * const ic = avctx->priv_data;
6d924b5a 249
79d501a8 250 av_frame_free(&ic->picture);
6d924b5a
KS
251
252 return 0;
253}
254
d36beb3f 255AVCodec ff_indeo2_decoder = {
ec6402b7 256 .name = "indeo2",
b2bed932 257 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
ec6402b7 258 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 259 .id = AV_CODEC_ID_INDEO2,
ec6402b7
AK
260 .priv_data_size = sizeof(Ir2Context),
261 .init = ir2_decode_init,
262 .close = ir2_decode_end,
263 .decode = ir2_decode_frame,
def97856 264 .capabilities = AV_CODEC_CAP_DR1,
856dbbff 265};