]> git.sesse.net Git - ffmpeg/blob - libavcodec/indeo2.c
vaapi_h265: Convert to use coded bitstream infrastructure
[ffmpeg] / libavcodec / indeo2.c
1 /*
2  * Intel Indeo 2 codec
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Intel Indeo 2 decoder.
25  */
26
27 #include "libavutil/attributes.h"
28
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "bitstream.h"
32 #include "indeo2data.h"
33 #include "internal.h"
34 #include "mathops.h"
35 #include "vlc.h"
36
37 typedef struct Ir2Context{
38     AVCodecContext *avctx;
39     AVFrame *picture;
40     BitstreamContext bc;
41     int decode_delta;
42 } Ir2Context;
43
44 #define CODE_VLC_BITS 14
45 static VLC ir2_vlc;
46
47 /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
48 static inline int ir2_get_code(BitstreamContext *bc)
49 {
50     return bitstream_read_vlc(bc, ir2_vlc.table, CODE_VLC_BITS, 1) + 1;
51 }
52
53 static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
54                             int pitch, const uint8_t *table)
55 {
56     int i;
57     int j;
58     int out = 0;
59     int c;
60     int t;
61
62     if (width & 1)
63         return AVERROR_INVALIDDATA;
64
65     /* first line contain absolute values, other lines contain deltas */
66     while (out < width) {
67         c = ir2_get_code(&ctx->bc);
68         if (c >= 0x80) { /* we have a run */
69             c -= 0x7F;
70             if (out + c*2 > width)
71                 return AVERROR_INVALIDDATA;
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     }
79     dst += pitch;
80
81     for (j = 1; j < height; j++) {
82         out = 0;
83         while (out < width) {
84             c = ir2_get_code(&ctx->bc);
85             if (c >= 0x80) { /* we have a skip */
86                 c -= 0x7F;
87                 if (out + c*2 > width)
88                     return AVERROR_INVALIDDATA;
89                 for (i = 0; i < c * 2; i++) {
90                     dst[out] = dst[out - pitch];
91                     out++;
92                 }
93             } else { /* add two deltas from table */
94                 t        = dst[out - pitch] + (table[c * 2] - 128);
95                 t        = av_clip_uint8(t);
96                 dst[out] = t;
97                 out++;
98                 t        = dst[out - pitch] + (table[(c * 2) + 1] - 128);
99                 t        = av_clip_uint8(t);
100                 dst[out] = t;
101                 out++;
102             }
103         }
104         dst += pitch;
105     }
106     return 0;
107 }
108
109 static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
110                                   int pitch, const uint8_t *table)
111 {
112     int j;
113     int out = 0;
114     int c;
115     int t;
116
117     if (width & 1)
118         return AVERROR_INVALIDDATA;
119
120     for (j = 0; j < height; j++) {
121         out = 0;
122         while (out < width) {
123             c = ir2_get_code(&ctx->bc);
124             if (c >= 0x80) { /* we have a skip */
125                 c   -= 0x7F;
126                 out += c * 2;
127             } else { /* add two deltas from table */
128                 t        = dst[out] + (((table[c * 2] - 128)*3) >> 2);
129                 t        = av_clip_uint8(t);
130                 dst[out] = t;
131                 out++;
132                 t        = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
133                 t        = av_clip_uint8(t);
134                 dst[out] = t;
135                 out++;
136             }
137         }
138         dst += pitch;
139     }
140     return 0;
141 }
142
143 static int ir2_decode_frame(AVCodecContext *avctx,
144                         void *data, int *got_frame,
145                         AVPacket *avpkt)
146 {
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;
152     int start, ret;
153     int ltab, ctab;
154
155     if ((ret = ff_reget_buffer(avctx, p)) < 0) {
156         av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
157         return ret;
158     }
159
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
167     s->decode_delta = buf[18];
168
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]];
173 #endif
174
175     bitstream_init8(&s->bc, buf + start, buf_size - start);
176
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)
183             return ret;
184
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)
189             return ret;
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)
193             return ret;
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)
198             return ret;
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)
203             return ret;
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)
207             return ret;
208     }
209
210     if ((ret = av_frame_ref(picture, p)) < 0)
211         return ret;
212
213     *got_frame = 1;
214
215     return buf_size;
216 }
217
218 static av_cold int ir2_decode_init(AVCodecContext *avctx)
219 {
220     Ir2Context * const ic = avctx->priv_data;
221     static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2];
222
223     ic->avctx = avctx;
224
225     avctx->pix_fmt= AV_PIX_FMT_YUV410P;
226
227     ic->picture = av_frame_alloc();
228     if (!ic->picture)
229         return AVERROR(ENOMEM);
230
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);
237 #else
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);
241 #endif
242
243     return 0;
244 }
245
246 static av_cold int ir2_decode_end(AVCodecContext *avctx)
247 {
248     Ir2Context * const ic = avctx->priv_data;
249
250     av_frame_free(&ic->picture);
251
252     return 0;
253 }
254
255 AVCodec ff_indeo2_decoder = {
256     .name           = "indeo2",
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,
265 };