indeo: Convert to the new bitstream reader
[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"
856dbbff
MN
35
36typedef struct Ir2Context{
37 AVCodecContext *avctx;
79d501a8 38 AVFrame *picture;
c3defda0 39 BitstreamContext bc;
856dbbff
MN
40 int decode_delta;
41} Ir2Context;
42
43#define CODE_VLC_BITS 14
44static VLC ir2_vlc;
45
46/* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
c3defda0 47static inline int ir2_get_code(BitstreamContext *bc)
856dbbff 48{
c3defda0 49 return bitstream_read_vlc(bc, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
856dbbff 50}
856dbbff 51
c04c64c0 52static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
422e14f7 53 int pitch, const uint8_t *table)
856dbbff
MN
54{
55 int i;
56 int j;
57 int out = 0;
58 int c;
59 int t;
115329f1 60
c04c64c0 61 if (width & 1)
6ea2c9a4 62 return AVERROR_INVALIDDATA;
f707a5eb 63
856dbbff 64 /* first line contain absolute values, other lines contain deltas */
c04c64c0 65 while (out < width) {
c3defda0 66 c = ir2_get_code(&ctx->bc);
c04c64c0 67 if (c >= 0x80) { /* we have a run */
8b39f75b 68 c -= 0x7F;
c04c64c0 69 if (out + c*2 > width)
6ea2c9a4 70 return AVERROR_INVALIDDATA;
856dbbff
MN
71 for (i = 0; i < c * 2; i++)
72 dst[out++] = 0x80;
73 } else { /* copy two values from table */
74 dst[out++] = table[c * 2];
75 dst[out++] = table[(c * 2) + 1];
76 }
77 }
422e14f7 78 dst += pitch;
115329f1 79
c04c64c0 80 for (j = 1; j < height; j++) {
856dbbff 81 out = 0;
c04c64c0 82 while (out < width) {
c3defda0 83 c = ir2_get_code(&ctx->bc);
c04c64c0 84 if (c >= 0x80) { /* we have a skip */
8b39f75b 85 c -= 0x7F;
c04c64c0 86 if (out + c*2 > width)
6ea2c9a4 87 return AVERROR_INVALIDDATA;
856dbbff 88 for (i = 0; i < c * 2; i++) {
422e14f7 89 dst[out] = dst[out - pitch];
856dbbff
MN
90 out++;
91 }
92 } else { /* add two deltas from table */
422e14f7 93 t = dst[out - pitch] + (table[c * 2] - 128);
c04c64c0 94 t = av_clip_uint8(t);
856dbbff
MN
95 dst[out] = t;
96 out++;
422e14f7 97 t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
c04c64c0 98 t = av_clip_uint8(t);
856dbbff
MN
99 dst[out] = t;
100 out++;
101 }
102 }
422e14f7 103 dst += pitch;
856dbbff 104 }
f707a5eb 105 return 0;
856dbbff
MN
106}
107
c04c64c0 108static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
422e14f7 109 int pitch, const uint8_t *table)
856dbbff
MN
110{
111 int j;
112 int out = 0;
113 int c;
114 int t;
f707a5eb 115
c04c64c0 116 if (width & 1)
6ea2c9a4 117 return AVERROR_INVALIDDATA;
f707a5eb 118
c04c64c0 119 for (j = 0; j < height; j++) {
856dbbff 120 out = 0;
c04c64c0 121 while (out < width) {
c3defda0 122 c = ir2_get_code(&ctx->bc);
c04c64c0
AK
123 if (c >= 0x80) { /* we have a skip */
124 c -= 0x7F;
856dbbff
MN
125 out += c * 2;
126 } else { /* add two deltas from table */
c04c64c0
AK
127 t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
128 t = av_clip_uint8(t);
856dbbff
MN
129 dst[out] = t;
130 out++;
c04c64c0
AK
131 t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
132 t = av_clip_uint8(t);
856dbbff
MN
133 dst[out] = t;
134 out++;
135 }
136 }
422e14f7 137 dst += pitch;
856dbbff 138 }
f707a5eb 139 return 0;
856dbbff
MN
140}
141
115329f1 142static int ir2_decode_frame(AVCodecContext *avctx,
df9b9567 143 void *data, int *got_frame,
7a00bbad 144 AVPacket *avpkt)
856dbbff
MN
145{
146 Ir2Context * const s = avctx->priv_data;
c04c64c0
AK
147 const uint8_t *buf = avpkt->data;
148 int buf_size = avpkt->size;
149 AVFrame *picture = data;
79d501a8 150 AVFrame * const p = s->picture;
6ea2c9a4 151 int start, ret;
f8c34f4b 152 int ltab, ctab;
856dbbff 153
759001c5 154 if ((ret = ff_reget_buffer(avctx, p)) < 0) {
856dbbff 155 av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
6ea2c9a4 156 return ret;
856dbbff
MN
157 }
158
b7ce4f1d
AC
159 start = 48; /* hardcoded for now */
160
161 if (start >= buf_size) {
162 av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
163 return AVERROR_INVALIDDATA;
164 }
165
856dbbff 166 s->decode_delta = buf[18];
115329f1 167
856dbbff 168 /* decide whether frame uses deltas or not */
aaf47bcd 169#ifndef BITSTREAM_READER_LE
856dbbff 170 for (i = 0; i < buf_size; i++)
d5c62122 171 buf[i] = ff_reverse[buf[i]];
ef56de32 172#endif
856dbbff 173
c3defda0 174 bitstream_init(&s->bc, buf + start, (buf_size - start) * 8);
856dbbff 175
f8c34f4b
LB
176 ltab = buf[0x22] & 3;
177 ctab = buf[0x22] >> 2;
856dbbff 178 if (s->decode_delta) { /* intraframe */
7b1fbd47 179 if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
79d501a8 180 p->data[0], p->linesize[0],
f8c34f4b 181 ir2_delta_table[ltab])) < 0)
7b1fbd47
AK
182 return ret;
183
856dbbff 184 /* swapped U and V */
7b1fbd47 185 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 186 p->data[2], p->linesize[2],
f8c34f4b 187 ir2_delta_table[ctab])) < 0)
7b1fbd47
AK
188 return ret;
189 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 190 p->data[1], p->linesize[1],
f8c34f4b 191 ir2_delta_table[ctab])) < 0)
7b1fbd47 192 return ret;
856dbbff 193 } else { /* interframe */
7b1fbd47 194 if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
79d501a8 195 p->data[0], p->linesize[0],
f8c34f4b 196 ir2_delta_table[ltab])) < 0)
7b1fbd47 197 return ret;
856dbbff 198 /* swapped U and V */
7b1fbd47 199 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 200 p->data[2], p->linesize[2],
f8c34f4b 201 ir2_delta_table[ctab])) < 0)
7b1fbd47
AK
202 return ret;
203 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
79d501a8 204 p->data[1], p->linesize[1],
f8c34f4b 205 ir2_delta_table[ctab])) < 0)
7b1fbd47 206 return ret;
856dbbff
MN
207 }
208
79d501a8 209 if ((ret = av_frame_ref(picture, p)) < 0)
759001c5
AK
210 return ret;
211
df9b9567 212 *got_frame = 1;
856dbbff
MN
213
214 return buf_size;
215}
216
c04c64c0
AK
217static av_cold int ir2_decode_init(AVCodecContext *avctx)
218{
856dbbff 219 Ir2Context * const ic = avctx->priv_data;
bd4110f9 220 static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
856dbbff
MN
221
222 ic->avctx = avctx;
223
716d413c 224 avctx->pix_fmt= AV_PIX_FMT_YUV410P;
115329f1 225
79d501a8
AK
226 ic->picture = av_frame_alloc();
227 if (!ic->picture)
228 return AVERROR(ENOMEM);
3b199d29 229
bd4110f9
KS
230 ir2_vlc.table = vlc_tables;
231 ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
aaf47bcd 232#ifdef BITSTREAM_READER_LE
856dbbff
MN
233 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
234 &ir2_codes[0][1], 4, 2,
bd4110f9 235 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE);
ef56de32 236#else
c11aac6c
CL
237 init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
238 &ir2_codes[0][1], 4, 2,
bd4110f9 239 &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC);
ef56de32 240#endif
115329f1 241
856dbbff
MN
242 return 0;
243}
244
c04c64c0
AK
245static av_cold int ir2_decode_end(AVCodecContext *avctx)
246{
6d924b5a 247 Ir2Context * const ic = avctx->priv_data;
6d924b5a 248
79d501a8 249 av_frame_free(&ic->picture);
6d924b5a
KS
250
251 return 0;
252}
253
d36beb3f 254AVCodec ff_indeo2_decoder = {
ec6402b7 255 .name = "indeo2",
b2bed932 256 .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
ec6402b7 257 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 258 .id = AV_CODEC_ID_INDEO2,
ec6402b7
AK
259 .priv_data_size = sizeof(Ir2Context),
260 .init = ir2_decode_init,
261 .close = ir2_decode_end,
262 .decode = ir2_decode_frame,
def97856 263 .capabilities = AV_CODEC_CAP_DR1,
856dbbff 264};