FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/indeo2.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 104 127 81.9%
Branches: 44 66 66.7%

Line Branch Exec Source
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 */
49 2449607 static inline int ir2_get_code(GetBitContext *gb)
50 {
51 2449607 return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1);
52 }
53
54 633 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 633 int out = 0;
60
61
2/4
✓ Branch 0 taken 633 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 633 times.
633 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
2/2
✓ Branch 0 taken 25231 times.
✓ Branch 1 taken 633 times.
25864 while (out < width) {
66 25231 int c = ir2_get_code(&ctx->gb);
67
2/2
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 24893 times.
25231 if (c >= 0x80) { /* we have a run */
68 338 c -= 0x7F;
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (out + c*2 > width)
70 return AVERROR_INVALIDDATA;
71
2/2
✓ Branch 0 taken 854 times.
✓ Branch 1 taken 338 times.
1192 for (i = 0; i < c * 2; i++)
72 854 dst[out++] = 0x80;
73 } else { /* copy two values from table */
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24893 times.
24893 if (c <= 0)
75 return AVERROR_INVALIDDATA;
76 24893 dst[out++] = table[c * 2];
77 24893 dst[out++] = table[(c * 2) + 1];
78 }
79 }
80 633 dst += pitch;
81
82
2/2
✓ Branch 0 taken 37347 times.
✓ Branch 1 taken 633 times.
37980 for (j = 1; j < height; j++) {
83 37347 out = 0;
84
2/2
✓ Branch 0 taken 2115081 times.
✓ Branch 1 taken 37347 times.
2152428 while (out < width) {
85 int c;
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2115081 times.
2115081 if (get_bits_left(&ctx->gb) <= 0)
87 return AVERROR_INVALIDDATA;
88 2115081 c = ir2_get_code(&ctx->gb);
89
2/2
✓ Branch 0 taken 228280 times.
✓ Branch 1 taken 1886801 times.
2115081 if (c >= 0x80) { /* we have a skip */
90 228280 c -= 0x7F;
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 228280 times.
228280 if (out + c*2 > width)
92 return AVERROR_INVALIDDATA;
93
2/2
✓ Branch 0 taken 733358 times.
✓ Branch 1 taken 228280 times.
961638 for (i = 0; i < c * 2; i++) {
94 733358 dst[out] = dst[out - pitch];
95 733358 out++;
96 }
97 } else { /* add two deltas from table */
98 int t;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1886801 times.
1886801 if (c <= 0)
100 return AVERROR_INVALIDDATA;
101 1886801 t = dst[out - pitch] + (table[c * 2] - 128);
102 1886801 t = av_clip_uint8(t);
103 1886801 dst[out] = t;
104 1886801 out++;
105 1886801 t = dst[out - pitch] + (table[(c * 2) + 1] - 128);
106 1886801 t = av_clip_uint8(t);
107 1886801 dst[out] = t;
108 1886801 out++;
109 }
110 }
111 37347 dst += pitch;
112 }
113 633 return 0;
114 }
115
116 99 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 99 int out = 0;
121 int c;
122 int t;
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99 times.
99 if (width & 1)
125 return AVERROR_INVALIDDATA;
126
127
2/2
✓ Branch 0 taken 5940 times.
✓ Branch 1 taken 99 times.
6039 for (j = 0; j < height; j++) {
128 5940 out = 0;
129
2/2
✓ Branch 0 taken 309295 times.
✓ Branch 1 taken 5940 times.
315235 while (out < width) {
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 309295 times.
309295 if (get_bits_left(&ctx->gb) <= 0)
131 return AVERROR_INVALIDDATA;
132 309295 c = ir2_get_code(&ctx->gb);
133
2/2
✓ Branch 0 taken 56657 times.
✓ Branch 1 taken 252638 times.
309295 if (c >= 0x80) { /* we have a skip */
134 56657 c -= 0x7F;
135 56657 out += c * 2;
136 } else { /* add two deltas from table */
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252638 times.
252638 if (c <= 0)
138 return AVERROR_INVALIDDATA;
139 252638 t = dst[out] + (((table[c * 2] - 128)*3) >> 2);
140 252638 t = av_clip_uint8(t);
141 252638 dst[out] = t;
142 252638 out++;
143 252638 t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2);
144 252638 t = av_clip_uint8(t);
145 252638 dst[out] = t;
146 252638 out++;
147 }
148 }
149 5940 dst += pitch;
150 }
151 99 return 0;
152 }
153
154 244 static int ir2_decode_frame(AVCodecContext *avctx, AVFrame *picture,
155 int *got_frame, AVPacket *avpkt)
156 {
157 244 Ir2Context * const s = avctx->priv_data;
158 244 const uint8_t *buf = avpkt->data;
159 244 int buf_size = avpkt->size;
160 244 AVFrame * const p = s->picture;
161 int start, ret;
162 int ltab, ctab;
163
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
244 if ((ret = ff_reget_buffer(avctx, p, 0)) < 0)
165 return ret;
166
167 244 start = 48; /* hardcoded for now */
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 244 times.
244 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 244 s->decode_delta = buf[18];
175
176 /* decide whether frame uses deltas or not */
177
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
244 if ((ret = init_get_bits8(&s->gb, buf + start, buf_size - start)) < 0)
179 return ret;
180
181 244 ltab = buf[0x22] & 3;
182 244 ctab = buf[0x22] >> 2;
183
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 244 times.
244 if (ctab > 3) {
185 av_log(avctx, AV_LOG_ERROR, "ctab %d is invalid\n", ctab);
186 return AVERROR_INVALIDDATA;
187 }
188
189
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 33 times.
244 if (s->decode_delta) { /* intraframe */
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
211 if ((ret = ir2_decode_plane(s, avctx->width, avctx->height,
191 p->data[0], p->linesize[0],
192 211 ir2_delta_table[ltab])) < 0)
193 return ret;
194
195 /* swapped U and V */
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
211 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
197 p->data[2], p->linesize[2],
198 211 ir2_delta_table[ctab])) < 0)
199 return ret;
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
211 if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2,
201 p->data[1], p->linesize[1],
202 211 ir2_delta_table[ctab])) < 0)
203 return ret;
204 } else { /* interframe */
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height,
206 p->data[0], p->linesize[0],
207 33 ir2_delta_table[ltab])) < 0)
208 return ret;
209 /* swapped U and V */
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
211 p->data[2], p->linesize[2],
212 33 ir2_delta_table[ctab])) < 0)
213 return ret;
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2,
215 p->data[1], p->linesize[1],
216 33 ir2_delta_table[ctab])) < 0)
217 return ret;
218 }
219
220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
244 if ((ret = av_frame_ref(picture, p)) < 0)
221 return ret;
222
223 244 *got_frame = 1;
224
225 244 return buf_size;
226 }
227
228 2 static av_cold void ir2_init_static(void)
229 {
230 2 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 2 }
234
235 4 static av_cold int ir2_decode_init(AVCodecContext *avctx)
236 {
237 static AVOnce init_static_once = AV_ONCE_INIT;
238 4 Ir2Context * const ic = avctx->priv_data;
239
240 4 ic->avctx = avctx;
241
242 4 avctx->pix_fmt= AV_PIX_FMT_YUV410P;
243
244 4 ic->picture = av_frame_alloc();
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ic->picture)
246 return AVERROR(ENOMEM);
247
248 4 ff_thread_once(&init_static_once, ir2_init_static);
249
250 4 return 0;
251 }
252
253 4 static av_cold int ir2_decode_end(AVCodecContext *avctx)
254 {
255 4 Ir2Context * const ic = avctx->priv_data;
256
257 4 av_frame_free(&ic->picture);
258
259 4 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