1 /*
2  * Intel Indeo 2 codec
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "libavutil/thread.h"
29 
30 #define BITSTREAM_READER_LE
31 #include "avcodec.h"
32 #include "codec_internal.h"
33 #include "get_bits.h"
34 #include "indeo2data.h"
35 #include "internal.h"
36 #include "mathops.h"
37 
38 typedef struct Ir2Context{
39     AVCodecContext *avctx;
40     AVFrame *picture;
41     GetBitContext gb;
42     int decode_delta;
43 } Ir2Context;
44 
45 #define CODE_VLC_BITS 14
46 static VLC ir2_vlc;
47 
48 /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */
ir2_get_code(GetBitContext *gb)49 static inline int ir2_get_code(GetBitContext *gb)
50 {
51     return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1);
52 }
53 
ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, int pitch, const uint8_t *table)54 static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst,
55                             int pitch, const uint8_t *table)
56 {
57     int i;
58     int j;
59     int out = 0;
60 
61     if ((width & 1) || width * height / (2*(IR2_CODES - 0x7F)) > get_bits_left(&ctx->gb))
62         return AVERROR_INVALIDDATA;
63 
64     /* first line contain absolute values, other lines contain deltas */
65     while (out < width) {
66         int c = ir2_get_code(&ctx->gb);
67         if (c >= 0x80) { /* we have a run */
68             c -= 0x7F;
69             if (out + c*2 > width)
70                 return AVERROR_INVALIDDATA;
71             for (i = 0; i < c * 2; i++)
72                 dst[out++] = 0x80;
73         } else { /* copy two values from table */
74             if (c <= 0)
75                 return AVERROR_INVALIDDATA;
76             dst[out++] = table[c * 2];
77             dst[out++] = table[(c * 2) + 1];
78         }
79     }
80     dst += pitch;
81 
82     for (j = 1; j < height; j++) {
83         out = 0;
84         while (out < width) {
85             int c;
86             if (get_bits_left(&ctx->gb) <= 0)
87                 return AVERROR_INVALIDDATA;
88             c = ir2_get_code(&ctx->gb);
89             if (c >= 0x80) { /* we have a skip */
90                 c -= 0x7F;
91                 if (out + c*2 > width)
92                     return AVERROR_INVALIDDATA;
93                 for (i = 0; i < c * 2; i++) {
94                     dst[out] = dst[out - pitch];
95                     out++;
96                 }
97             } else { /* add two deltas from table */
98                 int t;
99                 if (c <= 0)
100                     return AVERROR_INVALIDDATA;
101                 t        = dst[out - pitch] + (table[c * 2] - 128);
102                 t        = av_clip_uint8(t);
103                 dst[out] = t;
104                 out++;
105                 t        = dst[out - pitch] + (table[(c * 2) + 1] - 128);
106                 t        = av_clip_uint8(t);
107                 dst[out] = t;
108                 out++;
109             }
110         }
111         dst += pitch;
112     }
113     return 0;
114 }
115 
ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, int pitch, const uint8_t *table)116 static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst,
117                                   int pitch, const uint8_t *table)
118 {
119     int j;
120     int out = 0;
121     int c;
122     int t;
123 
124     if (width & 1)
125         return AVERROR_INVALIDDATA;
126 
127     for (j = 0; j < height; j++) {
128         out = 0;
129         while (out < width) {
130             if (get_bits_left(&ctx->gb) <= 0)
131                 return AVERROR_INVALIDDATA;
132             c = ir2_get_code(&ctx->gb);
133             if (c >= 0x80) { /* we have a skip */
134                 c   -= 0x7F;
135                 out += c * 2;
136             } else { /* add two deltas from table */
137                 if (c <= 0)
138                     return AVERROR_INVALIDDATA;
139                 t        = dst[out] + (((table[c * 2] - 128)*3) >> 2);
140                 t        = av_clip_uint8(t);
141                 dst[out] = t;
142                 out++;
143                 t        = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
144                 t        = av_clip_uint8(t);
145                 dst[out] = t;
146                 out++;
147             }
148         }
149         dst += pitch;
150     }
151     return 0;
152 }
153 
ir2_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)154 static int ir2_decode_frame(AVCodecContext *avctx, AVFrame *picture,
155                             int *got_frame, AVPacket *avpkt)
156 {
157     Ir2Context * const s = avctx->priv_data;
158     const uint8_t *buf   = avpkt->data;
159     int buf_size         = avpkt->size;
160     AVFrame * const p    = s->picture;
161     int start, ret;
162     int ltab, ctab;
163 
164     if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
165         return ret;
166 
167     start = 48; /* hardcoded for now */
168 
169     if (start >= buf_size) {
170         av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size);
171         return AVERROR_INVALIDDATA;
172     }
173 
174     s->decode_delta = buf[18];
175 
176     /* decide whether frame uses deltas or not */
177 
178     if ((ret = init_get_bits8(&s->gb, buf + start, buf_size - start)) < 0)
179         return ret;
180 
181     ltab = buf[0x22] & 3;
182     ctab = buf[0x22] >> 2;
183 
184     if (ctab > 3) {
185         av_log(avctx, AV_LOG_ERROR, "ctab %d is invalid\n", ctab);
186         return AVERROR_INVALIDDATA;
187     }
188 
189     if (s->decode_delta) { /* intraframe */
190         if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
191                                     p->data[0], p->linesize[0],
192                                     ir2_delta_table[ltab])) < 0)
193             return ret;
194 
195         /* swapped U and V */
196         if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
197                                     p->data[2], p->linesize[2],
198                                     ir2_delta_table[ctab])) < 0)
199             return ret;
200         if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
201                                     p->data[1], p->linesize[1],
202                                     ir2_delta_table[ctab])) < 0)
203             return ret;
204     } else { /* interframe */
205         if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
206                                           p->data[0], p->linesize[0],
207                                           ir2_delta_table[ltab])) < 0)
208             return ret;
209         /* swapped U and V */
210         if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
211                                           p->data[2], p->linesize[2],
212                                           ir2_delta_table[ctab])) < 0)
213             return ret;
214         if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
215                                           p->data[1], p->linesize[1],
216                                           ir2_delta_table[ctab])) < 0)
217             return ret;
218     }
219 
220     if ((ret = av_frame_ref(picture, p)) < 0)
221         return ret;
222 
223     *got_frame = 1;
224 
225     return buf_size;
226 }
227 
ir2_init_static(void)228 static av_cold void ir2_init_static(void)
229 {
230     INIT_VLC_STATIC_FROM_LENGTHS(&ir2_vlc, CODE_VLC_BITS, IR2_CODES,
231                                  &ir2_tab[0][1], 2, &ir2_tab[0][0], 2, 1,
232                                  0, INIT_VLC_OUTPUT_LE, 1 << CODE_VLC_BITS);
233 }
234 
ir2_decode_init(AVCodecContext *avctx)235 static av_cold int ir2_decode_init(AVCodecContext *avctx)
236 {
237     static AVOnce init_static_once = AV_ONCE_INIT;
238     Ir2Context * const ic = avctx->priv_data;
239 
240     ic->avctx = avctx;
241 
242     avctx->pix_fmt= AV_PIX_FMT_YUV410P;
243 
244     ic->picture = av_frame_alloc();
245     if (!ic->picture)
246         return AVERROR(ENOMEM);
247 
248     ff_thread_once(&init_static_once, ir2_init_static);
249 
250     return 0;
251 }
252 
ir2_decode_end(AVCodecContext *avctx)253 static av_cold int ir2_decode_end(AVCodecContext *avctx)
254 {
255     Ir2Context * const ic = avctx->priv_data;
256 
257     av_frame_free(&ic->picture);
258 
259     return 0;
260 }
261 
262 const FFCodec ff_indeo2_decoder = {
263     .p.name         = "indeo2",
264     .p.long_name    = NULL_IF_CONFIG_SMALL("Intel Indeo 2"),
265     .p.type         = AVMEDIA_TYPE_VIDEO,
266     .p.id           = AV_CODEC_ID_INDEO2,
267     .priv_data_size = sizeof(Ir2Context),
268     .init           = ir2_decode_init,
269     .close          = ir2_decode_end,
270     FF_CODEC_DECODE_CB(ir2_decode_frame),
271     .p.capabilities = AV_CODEC_CAP_DR1,
272     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
273 };
274