FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/indeo5.c
Date: 2026-05-03 23:58:45
Exec Total Coverage
Lines: 245 353 69.4%
Functions: 7 8 87.5%
Branches: 120 214 56.1%

Line Branch Exec Source
1 /*
2 * Indeo Video Interactive v5 compatible decoder
3 * Copyright (c) 2009 Maxim Poliakovski
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 * Indeo Video Interactive version 5 decoder
25 *
26 * Indeo5 data is usually transported within .avi or .mov files.
27 * Known FOURCCs: 'IV50'
28 */
29
30 #include "libavutil/attributes.h"
31 #define BITSTREAM_READER_LE
32 #include "avcodec.h"
33 #include "codec_internal.h"
34 #include "get_bits.h"
35 #include "ivi.h"
36 #include "ivi_dsp.h"
37 #include "indeo5data.h"
38
39 /**
40 * Indeo5 frame types.
41 */
42 enum {
43 FRAMETYPE_INTRA = 0,
44 FRAMETYPE_INTER = 1, ///< non-droppable P-frame
45 FRAMETYPE_INTER_SCAL = 2, ///< droppable P-frame used in the scalability mode
46 FRAMETYPE_INTER_NOREF = 3, ///< droppable P-frame
47 FRAMETYPE_NULL = 4 ///< empty frame with no data
48 };
49
50 #define IVI5_PIC_SIZE_ESC 15
51
52 /**
53 * Decode Indeo5 GOP (Group of pictures) header.
54 * This header is present in key frames only.
55 * It defines parameters for all frames in a GOP.
56 *
57 * @param[in,out] ctx ptr to the decoder context
58 * @param[in] avctx ptr to the AVCodecContext
59 * @return result code: 0 = OK, -1 = error
60 */
61 9 static int decode_gop_header(IVI45DecContext *ctx, AVCodecContext *avctx)
62 {
63 int result, i, p, tile_size, pic_size_indx, mb_size, blk_size, is_scalable;
64 9 int quant_mat, blk_size_changed = 0;
65 IVIBandDesc *band, *band1, *band2;
66 IVIPicConfig pic_conf;
67
68 9 ctx->gop_flags = get_bits(&ctx->gb, 8);
69
70
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 ctx->gop_hdr_size = (ctx->gop_flags & 1) ? get_bits(&ctx->gb, 16) : 0;
71
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ctx->gop_flags & IVI5_IS_PROTECTED)
73 ctx->lock_word = get_bits_long(&ctx->gb, 32);
74
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 tile_size = (ctx->gop_flags & 0x40) ? 64 << get_bits(&ctx->gb, 2) : 0;
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (tile_size > 256) {
77 av_log(avctx, AV_LOG_ERROR, "Invalid tile size: %d\n", tile_size);
78 return AVERROR_INVALIDDATA;
79 }
80
81 /* decode number of wavelet bands */
82 /* num_levels * 3 + 1 */
83 9 pic_conf.luma_bands = get_bits(&ctx->gb, 2) * 3 + 1;
84 9 pic_conf.chroma_bands = get_bits1(&ctx->gb) * 3 + 1;
85
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
86
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
9 if (is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
87 av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
88 pic_conf.luma_bands, pic_conf.chroma_bands);
89 return AVERROR_INVALIDDATA;
90 }
91
92 9 pic_size_indx = get_bits(&ctx->gb, 4);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (pic_size_indx == IVI5_PIC_SIZE_ESC) {
94 pic_conf.pic_height = get_bits(&ctx->gb, 13);
95 pic_conf.pic_width = get_bits(&ctx->gb, 13);
96 } else {
97 9 pic_conf.pic_height = ivi5_common_pic_sizes[pic_size_indx * 2 + 1] << 2;
98 9 pic_conf.pic_width = ivi5_common_pic_sizes[pic_size_indx * 2 ] << 2;
99 }
100
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ctx->gop_flags & 2) {
102 avpriv_report_missing_feature(avctx, "YV12 picture format");
103 return AVERROR_PATCHWELCOME;
104 }
105
106 9 pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
107 9 pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
108
109
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!tile_size) {
110 9 pic_conf.tile_height = pic_conf.pic_height;
111 9 pic_conf.tile_width = pic_conf.pic_width;
112 } else {
113 pic_conf.tile_height = pic_conf.tile_width = tile_size;
114 }
115
116 /* check if picture layout was changed and reallocate buffers */
117
3/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 8 times.
9 if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf) || ctx->gop_invalid) {
118 1 result = ff_ivi_init_planes(avctx, ctx->planes, &pic_conf, 0);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (result < 0) {
120 av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
121 return result;
122 }
123 1 ctx->pic_conf = pic_conf;
124 1 ctx->is_scalable = is_scalable;
125 1 blk_size_changed = 1; /* force reallocation of the internal structures */
126 }
127
128
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (p = 0; p <= 1; p++) {
129
4/4
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 18 times.
36 for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
130 18 band = &ctx->planes[p].bands[i];
131
132 18 band->is_halfpel = get_bits1(&ctx->gb);
133
134 18 mb_size = get_bits1(&ctx->gb);
135 18 blk_size = 8 >> get_bits1(&ctx->gb);
136 18 mb_size = blk_size << !mb_size;
137
138
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
18 if (p==0 && blk_size==4) {
139 av_log(avctx, AV_LOG_ERROR, "4x4 luma blocks are unsupported!\n");
140 return AVERROR_PATCHWELCOME;
141 }
142
143
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
18 blk_size_changed = mb_size != band->mb_size || blk_size != band->blk_size;
144
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (blk_size_changed) {
145 2 band->mb_size = mb_size;
146 2 band->blk_size = blk_size;
147 }
148
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (get_bits1(&ctx->gb)) {
150 avpriv_report_missing_feature(avctx, "Extended transform info");
151 return AVERROR_PATCHWELCOME;
152 }
153
154 /* select transform function and scan pattern according to plane and band number */
155
2/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
18 switch ((p << 2) + i) {
156 9 case 0:
157 9 band->inv_transform = ff_ivi_inverse_slant_8x8;
158 9 band->dc_transform = ff_ivi_dc_slant_2d;
159 9 band->scan = ff_zigzag_direct;
160 9 band->transform_size = 8;
161 9 break;
162
163 case 1:
164 band->inv_transform = ff_ivi_row_slant8;
165 band->dc_transform = ff_ivi_dc_row_slant;
166 band->scan = ff_ivi_vertical_scan_8x8;
167 band->transform_size = 8;
168 break;
169
170 case 2:
171 band->inv_transform = ff_ivi_col_slant8;
172 band->dc_transform = ff_ivi_dc_col_slant;
173 band->scan = ff_ivi_horizontal_scan_8x8;
174 band->transform_size = 8;
175 break;
176
177 case 3:
178 band->inv_transform = ff_ivi_put_pixels_8x8;
179 band->dc_transform = ff_ivi_put_dc_pixel_8x8;
180 band->scan = ff_ivi_horizontal_scan_8x8;
181 band->transform_size = 8;
182 break;
183
184 9 case 4:
185 9 band->inv_transform = ff_ivi_inverse_slant_4x4;
186 9 band->dc_transform = ff_ivi_dc_slant_2d;
187 9 band->scan = ff_ivi_direct_scan_4x4;
188 9 band->transform_size = 4;
189 9 break;
190 }
191
192
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
27 band->is_2d_trans = band->inv_transform == ff_ivi_inverse_slant_8x8 ||
193
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 band->inv_transform == ff_ivi_inverse_slant_4x4;
194
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (band->transform_size != band->blk_size) {
196 av_log(avctx, AV_LOG_ERROR, "transform and block size mismatch (%d != %d)\n", band->transform_size, band->blk_size);
197 return AVERROR_INVALIDDATA;
198 }
199
200 /* select dequant matrix according to plane and band number */
201
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (!p) {
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 quant_mat = (pic_conf.luma_bands > 1) ? i+1 : 0;
203 } else {
204 9 quant_mat = 5;
205 }
206
207
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (band->blk_size == 8) {
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if(quant_mat >= 5){
209 av_log(avctx, AV_LOG_ERROR, "quant_mat %d too large!\n", quant_mat);
210 return -1;
211 }
212 9 band->intra_base = &ivi5_base_quant_8x8_intra[quant_mat][0];
213 9 band->inter_base = &ivi5_base_quant_8x8_inter[quant_mat][0];
214 9 band->intra_scale = &ivi5_scale_quant_8x8_intra[quant_mat][0];
215 9 band->inter_scale = &ivi5_scale_quant_8x8_inter[quant_mat][0];
216 } else {
217 9 band->intra_base = ivi5_base_quant_4x4_intra;
218 9 band->inter_base = ivi5_base_quant_4x4_inter;
219 9 band->intra_scale = ivi5_scale_quant_4x4_intra;
220 9 band->inter_scale = ivi5_scale_quant_4x4_inter;
221 }
222
223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (get_bits(&ctx->gb, 2)) {
224 av_log(avctx, AV_LOG_ERROR, "End marker missing!\n");
225 return AVERROR_INVALIDDATA;
226 }
227 }
228 }
229
230 /* copy chroma parameters into the 2nd chroma plane */
231
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (i = 0; i < pic_conf.chroma_bands; i++) {
232 9 band1 = &ctx->planes[1].bands[i];
233 9 band2 = &ctx->planes[2].bands[i];
234
235 9 band2->width = band1->width;
236 9 band2->height = band1->height;
237 9 band2->mb_size = band1->mb_size;
238 9 band2->blk_size = band1->blk_size;
239 9 band2->is_halfpel = band1->is_halfpel;
240 9 band2->intra_base = band1->intra_base;
241 9 band2->inter_base = band1->inter_base;
242 9 band2->intra_scale = band1->intra_scale;
243 9 band2->inter_scale = band1->inter_scale;
244 9 band2->scan = band1->scan;
245 9 band2->inv_transform = band1->inv_transform;
246 9 band2->dc_transform = band1->dc_transform;
247 9 band2->is_2d_trans = band1->is_2d_trans;
248 9 band2->transform_size= band1->transform_size;
249 }
250
251 /* reallocate internal structures if needed */
252
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (blk_size_changed) {
253 1 result = ff_ivi_init_tiles(ctx->planes, pic_conf.tile_width,
254 1 pic_conf.tile_height);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (result < 0) {
256 av_log(avctx, AV_LOG_ERROR,
257 "Couldn't reallocate internal structures!\n");
258 return result;
259 }
260 }
261
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ctx->gop_flags & 8) {
263 if (get_bits(&ctx->gb, 3)) {
264 av_log(avctx, AV_LOG_ERROR, "Alignment bits are not zero!\n");
265 return AVERROR_INVALIDDATA;
266 }
267
268 if (get_bits1(&ctx->gb))
269 skip_bits(&ctx->gb, 24); /* skip transparency fill color */
270 }
271
272 9 align_get_bits(&ctx->gb);
273
274 9 skip_bits(&ctx->gb, 23); /* FIXME: unknown meaning */
275
276 /* skip GOP extension if any */
277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_bits1(&ctx->gb)) {
278 do {
279 i = get_bits(&ctx->gb, 16);
280 } while (i & 0x8000);
281 }
282
283 9 align_get_bits(&ctx->gb);
284
285 9 return 0;
286 }
287
288
289 /**
290 * Skip a header extension.
291 *
292 * @param[in,out] gb the GetBit context
293 */
294 static inline int skip_hdr_extension(GetBitContext *gb)
295 {
296 int i, len;
297
298 do {
299 len = get_bits(gb, 8);
300 if (8*len > get_bits_left(gb))
301 return AVERROR_INVALIDDATA;
302 for (i = 0; i < len; i++) skip_bits(gb, 8);
303 } while(len);
304
305 return 0;
306 }
307
308
309 /**
310 * Decode Indeo5 picture header.
311 *
312 * @param[in,out] ctx ptr to the decoder context
313 * @param[in] avctx ptr to the AVCodecContext
314 * @return result code: 0 = OK, -1 = error
315 */
316 134 static int decode_pic_hdr(IVI45DecContext *ctx, AVCodecContext *avctx)
317 {
318 int ret;
319
320
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 134 times.
134 if (get_bits(&ctx->gb, 5) != 0x1F) {
321 av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
322 return AVERROR_INVALIDDATA;
323 }
324
325 134 ctx->prev_frame_type = ctx->frame_type;
326 134 ctx->frame_type = get_bits(&ctx->gb, 3);
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
134 if (ctx->frame_type >= 5) {
328 av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d \n", ctx->frame_type);
329 ctx->frame_type = FRAMETYPE_INTRA;
330 return AVERROR_INVALIDDATA;
331 }
332
333 134 ctx->frame_num = get_bits(&ctx->gb, 8);
334
335
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 125 times.
134 if (ctx->frame_type == FRAMETYPE_INTRA) {
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = decode_gop_header(ctx, avctx)) < 0) {
337 av_log(avctx, AV_LOG_ERROR, "Invalid GOP header, skipping frames.\n");
338 ctx->gop_invalid = 1;
339 return ret;
340 }
341 9 ctx->gop_invalid = 0;
342 }
343
344
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
134 if (ctx->frame_type == FRAMETYPE_INTER_SCAL && !ctx->is_scalable) {
345 av_log(avctx, AV_LOG_ERROR, "Scalable inter frame in non scalable stream\n");
346 ctx->frame_type = FRAMETYPE_INTER;
347 return AVERROR_INVALIDDATA;
348 }
349
350
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 1 times.
134 if (ctx->frame_type != FRAMETYPE_NULL) {
351 133 ctx->frame_flags = get_bits(&ctx->gb, 8);
352
353
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 ctx->pic_hdr_size = (ctx->frame_flags & 1) ? get_bits(&ctx->gb, 24) : 0;
354
355
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 ctx->checksum = (ctx->frame_flags & 0x10) ? get_bits(&ctx->gb, 16) : 0;
356
357 /* skip unknown extension if any */
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (ctx->frame_flags & 0x20)
359 skip_hdr_extension(&ctx->gb); /* XXX: untested */
360
361 /* decode macroblock huffman codebook */
362 133 ret = ff_ivi_dec_huff_desc(&ctx->gb, ctx->frame_flags & 0x40,
363 IVI_MB_HUFF, &ctx->mb_vlc, avctx);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 if (ret < 0)
365 return ret;
366
367 133 skip_bits(&ctx->gb, 3); /* FIXME: unknown meaning! */
368 }
369
370 134 align_get_bits(&ctx->gb);
371
372 134 return 0;
373 }
374
375
376 /**
377 * Decode Indeo5 band header.
378 *
379 * @param[in,out] ctx ptr to the decoder context
380 * @param[in,out] band ptr to the band descriptor
381 * @param[in] avctx ptr to the AVCodecContext
382 * @return result code: 0 = OK, -1 = error
383 */
384 399 static int decode_band_hdr(IVI45DecContext *ctx, IVIBandDesc *band,
385 AVCodecContext *avctx)
386 {
387 int i, ret;
388 uint8_t band_flags;
389
390 399 band_flags = get_bits(&ctx->gb, 8);
391
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 399 times.
399 if (band_flags & 1) {
393 band->is_empty = 1;
394 return 0;
395 }
396
397
1/2
✓ Branch 0 taken 399 times.
✗ Branch 1 not taken.
399 band->data_size = (ctx->frame_flags & 0x80) ? get_bits(&ctx->gb, 24) : 0;
398
399 399 band->inherit_mv = band_flags & 2;
400 399 band->inherit_qdelta = band_flags & 8;
401 399 band->qdelta_present = band_flags & 4;
402
1/2
✓ Branch 0 taken 399 times.
✗ Branch 1 not taken.
399 if (!band->qdelta_present) band->inherit_qdelta = 1;
403
404 /* decode rvmap probability corrections if any */
405 399 band->num_corr = 0; /* there are no corrections */
406
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 147 times.
399 if (band_flags & 0x10) {
407 252 band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 252 times.
252 if (band->num_corr > 61) {
409 av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",
410 band->num_corr);
411 return AVERROR_INVALIDDATA;
412 }
413
414 /* read correction pairs */
415
2/2
✓ Branch 0 taken 2064 times.
✓ Branch 1 taken 252 times.
2316 for (i = 0; i < band->num_corr * 2; i++)
416 2064 band->corr[i] = get_bits(&ctx->gb, 8);
417 }
418
419 /* select appropriate rvmap table for this band */
420
2/2
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 2 times.
399 band->rvmap_sel = (band_flags & 0x40) ? get_bits(&ctx->gb, 3) : 8;
421
422 /* decode block huffman codebook */
423 399 ret = ff_ivi_dec_huff_desc(&ctx->gb, band_flags & 0x80, IVI_BLK_HUFF,
424 &band->blk_vlc, avctx);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 399 times.
399 if (ret < 0)
426 return ret;
427
428 399 band->checksum_present = get_bits1(&ctx->gb);
429
1/2
✓ Branch 0 taken 399 times.
✗ Branch 1 not taken.
399 if (band->checksum_present)
430 399 band->checksum = get_bits(&ctx->gb, 16);
431
432 399 band->glob_quant = get_bits(&ctx->gb, 5);
433
434 /* skip unknown extension if any */
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 399 times.
399 if (band_flags & 0x20) { /* XXX: untested */
436 align_get_bits(&ctx->gb);
437 skip_hdr_extension(&ctx->gb);
438 }
439
440 399 align_get_bits(&ctx->gb);
441
442 399 return 0;
443 }
444
445
446 /**
447 * Decode info (block type, cbp, quant delta, motion vector)
448 * for all macroblocks in the current tile.
449 *
450 * @param[in,out] ctx ptr to the decoder context
451 * @param[in,out] band ptr to the band descriptor
452 * @param[in,out] tile ptr to the tile descriptor
453 * @param[in] avctx ptr to the AVCodecContext
454 * @return result code: 0 = OK, -1 = error
455 */
456 399 static int decode_mb_info(IVI45DecContext *ctx, IVIBandDesc *band,
457 IVITile *tile, AVCodecContext *avctx)
458 {
459 int x, y, mv_x, mv_y, mv_delta, offs, mb_offset,
460 mv_scale, blks_per_mb, s;
461 IVIMbInfo *mb, *ref_mb;
462 399 int row_offset = band->mb_size * band->pitch;
463
464 399 mb = tile->mbs;
465 399 ref_mb = tile->ref_mbs;
466 399 offs = tile->ypos * band->pitch + tile->xpos;
467
468
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 266 times.
399 if (!ref_mb &&
469
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 133 times.
133 ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))
470 return AVERROR_INVALIDDATA;
471
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 399 times.
399 if (tile->num_MBs != IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size)) {
473 av_log(avctx, AV_LOG_ERROR, "Allocated tile size %d mismatches parameters %d\n",
474 tile->num_MBs, IVI_MBs_PER_TILE(tile->width, tile->height, band->mb_size));
475 return AVERROR_INVALIDDATA;
476 }
477
478 /* scale factor for motion vectors */
479 399 mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);
480 399 mv_x = mv_y = 0;
481
482
2/2
✓ Branch 0 taken 4788 times.
✓ Branch 1 taken 399 times.
5187 for (y = tile->ypos; y < (tile->ypos + tile->height); y += band->mb_size) {
483 4788 mb_offset = offs;
484
485
2/2
✓ Branch 0 taken 71820 times.
✓ Branch 1 taken 4788 times.
76608 for (x = tile->xpos; x < (tile->xpos + tile->width); x += band->mb_size) {
486 71820 mb->xpos = x;
487 71820 mb->ypos = y;
488 71820 mb->buf_offs = mb_offset;
489
490
2/2
✓ Branch 1 taken 27025 times.
✓ Branch 2 taken 44795 times.
71820 if (get_bits1(&ctx->gb)) {
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27025 times.
27025 if (ctx->frame_type == FRAMETYPE_INTRA) {
492 av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");
493 return AVERROR_INVALIDDATA;
494 }
495 27025 mb->type = 1; /* empty macroblocks are always INTER */
496 27025 mb->cbp = 0; /* all blocks are empty */
497
498 27025 mb->q_delta = 0;
499
4/6
✓ Branch 0 taken 7730 times.
✓ Branch 1 taken 19295 times.
✓ Branch 2 taken 7730 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7730 times.
27025 if (!band->plane && !band->band_num && (ctx->frame_flags & 8)) {
500 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
501 IVI_VLC_BITS, 1);
502 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
503 }
504
505 27025 mb->mv_x = mb->mv_y = 0; /* no motion vector coded */
506
3/4
✓ Branch 0 taken 19295 times.
✓ Branch 1 taken 7730 times.
✓ Branch 2 taken 19295 times.
✗ Branch 3 not taken.
27025 if (band->inherit_mv && ref_mb){
507 /* motion vector inheritance */
508
1/2
✓ Branch 0 taken 19295 times.
✗ Branch 1 not taken.
19295 if (mv_scale) {
509 19295 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
510 19295 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
511 } else {
512 mb->mv_x = ref_mb->mv_x;
513 mb->mv_y = ref_mb->mv_y;
514 }
515 }
516 } else {
517
3/4
✓ Branch 0 taken 28585 times.
✓ Branch 1 taken 16210 times.
✓ Branch 2 taken 28585 times.
✗ Branch 3 not taken.
44795 if (band->inherit_mv && ref_mb) {
518 28585 mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */
519
2/2
✓ Branch 0 taken 1620 times.
✓ Branch 1 taken 14590 times.
16210 } else if (ctx->frame_type == FRAMETYPE_INTRA) {
520 1620 mb->type = 0; /* mb_type is always INTRA for intra-frames */
521 } else {
522 14590 mb->type = get_bits1(&ctx->gb);
523 }
524
525
2/2
✓ Branch 0 taken 16210 times.
✓ Branch 1 taken 28585 times.
44795 blks_per_mb = band->mb_size != band->blk_size ? 4 : 1;
526 44795 mb->cbp = get_bits(&ctx->gb, blks_per_mb);
527
528 44795 mb->q_delta = 0;
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44795 times.
44795 if (band->qdelta_present) {
530 if (band->inherit_qdelta) {
531 if (ref_mb) mb->q_delta = ref_mb->q_delta;
532 } else if (mb->cbp || (!band->plane && !band->band_num &&
533 (ctx->frame_flags & 8))) {
534 mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
535 IVI_VLC_BITS, 1);
536 mb->q_delta = IVI_TOSIGNED(mb->q_delta);
537 }
538 }
539
540
2/2
✓ Branch 0 taken 13239 times.
✓ Branch 1 taken 31556 times.
44795 if (!mb->type) {
541 13239 mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */
542 } else {
543
3/4
✓ Branch 0 taken 19759 times.
✓ Branch 1 taken 11797 times.
✓ Branch 2 taken 19759 times.
✗ Branch 3 not taken.
31556 if (band->inherit_mv && ref_mb){
544 /* motion vector inheritance */
545
1/2
✓ Branch 0 taken 19759 times.
✗ Branch 1 not taken.
19759 if (mv_scale) {
546 19759 mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);
547 19759 mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);
548 } else {
549 mb->mv_x = ref_mb->mv_x;
550 mb->mv_y = ref_mb->mv_y;
551 }
552 } else {
553 /* decode motion vector deltas */
554 11797 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
555 IVI_VLC_BITS, 1);
556 11797 mv_y += IVI_TOSIGNED(mv_delta);
557 11797 mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,
558 IVI_VLC_BITS, 1);
559 11797 mv_x += IVI_TOSIGNED(mv_delta);
560 11797 mb->mv_x = mv_x;
561 11797 mb->mv_y = mv_y;
562 }
563 }
564 }
565
566 71820 s= band->is_halfpel;
567
2/2
✓ Branch 0 taken 58581 times.
✓ Branch 1 taken 13239 times.
71820 if (mb->type)
568
1/2
✓ Branch 0 taken 58581 times.
✗ Branch 1 not taken.
58581 if ( x + (mb->mv_x >>s) + (y+ (mb->mv_y >>s))*band->pitch < 0 ||
569 58581 x + ((mb->mv_x+s)>>s) + band->mb_size - 1
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58581 times.
58581 + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize - 1) {
571 av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);
572 return AVERROR_INVALIDDATA;
573 }
574
575 71820 mb++;
576
2/2
✓ Branch 0 taken 47880 times.
✓ Branch 1 taken 23940 times.
71820 if (ref_mb)
577 47880 ref_mb++;
578 71820 mb_offset += band->mb_size;
579 }
580
581 4788 offs += row_offset;
582 }
583
584 399 align_get_bits(&ctx->gb);
585
586 399 return 0;
587 }
588
589
590 /**
591 * Switch buffers.
592 *
593 * @param[in,out] ctx ptr to the decoder context
594 */
595 134 static void switch_buffers(IVI45DecContext *ctx)
596 {
597
2/4
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
134 switch (ctx->prev_frame_type) {
598 133 case FRAMETYPE_INTRA:
599 case FRAMETYPE_INTER:
600 133 ctx->buf_switch ^= 1;
601 133 ctx->dst_buf = ctx->buf_switch;
602 133 ctx->ref_buf = ctx->buf_switch ^ 1;
603 133 break;
604 case FRAMETYPE_INTER_SCAL:
605 if (!ctx->inter_scal) {
606 ctx->ref2_buf = 2;
607 ctx->inter_scal = 1;
608 }
609 FFSWAP(int, ctx->dst_buf, ctx->ref2_buf);
610 ctx->ref_buf = ctx->ref2_buf;
611 break;
612 case FRAMETYPE_INTER_NOREF:
613 break;
614 }
615
616
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
134 switch (ctx->frame_type) {
617 9 case FRAMETYPE_INTRA:
618 9 ctx->buf_switch = 0;
619 av_fallthrough;
620 133 case FRAMETYPE_INTER:
621 133 ctx->inter_scal = 0;
622 133 ctx->dst_buf = ctx->buf_switch;
623 133 ctx->ref_buf = ctx->buf_switch ^ 1;
624 133 break;
625 1 case FRAMETYPE_INTER_SCAL:
626 case FRAMETYPE_INTER_NOREF:
627 case FRAMETYPE_NULL:
628 1 break;
629 }
630 134 }
631
632
633 268 static int is_nonnull_frame(IVI45DecContext *ctx)
634 {
635 268 return ctx->frame_type != FRAMETYPE_NULL;
636 }
637
638
639 /**
640 * Initialize Indeo5 decoder.
641 */
642 2 static av_cold int decode_init(AVCodecContext *avctx)
643 {
644 2 IVI45DecContext *ctx = avctx->priv_data;
645 int result;
646
647 2 ctx->gop_invalid = 1;
648
649 2 ff_ivi_init_static_vlc();
650
651 /* copy rvmap tables in our context so we can apply changes to them */
652 2 memcpy(ctx->rvmap_tabs, ff_ivi_rvmap_tabs, sizeof(ff_ivi_rvmap_tabs));
653
654 /* set the initial picture layout according to the basic profile:
655 there is only one band per plane (no scalability), only one tile (no local decoding)
656 and picture format = YVU9 */
657 2 ctx->pic_conf.pic_width = avctx->width;
658 2 ctx->pic_conf.pic_height = avctx->height;
659 2 ctx->pic_conf.chroma_width = (avctx->width + 3) >> 2;
660 2 ctx->pic_conf.chroma_height = (avctx->height + 3) >> 2;
661 2 ctx->pic_conf.tile_width = avctx->width;
662 2 ctx->pic_conf.tile_height = avctx->height;
663 2 ctx->pic_conf.luma_bands = ctx->pic_conf.chroma_bands = 1;
664
665 2 result = ff_ivi_init_planes(avctx, ctx->planes, &ctx->pic_conf, 0);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result) {
667 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
668 return AVERROR_INVALIDDATA;
669 }
670
671 2 ctx->buf_switch = 0;
672 2 ctx->inter_scal = 0;
673
674 2 ctx->decode_pic_hdr = decode_pic_hdr;
675 2 ctx->decode_band_hdr = decode_band_hdr;
676 2 ctx->decode_mb_info = decode_mb_info;
677 2 ctx->switch_buffers = switch_buffers;
678 2 ctx->is_nonnull_frame = is_nonnull_frame;
679
680 2 ctx->is_indeo4 = 0;
681
682 2 avctx->pix_fmt = AV_PIX_FMT_YUV410P;
683
684 2 return 0;
685 }
686
687 const FFCodec ff_indeo5_decoder = {
688 .p.name = "indeo5",
689 CODEC_LONG_NAME("Intel Indeo Video Interactive 5"),
690 .p.type = AVMEDIA_TYPE_VIDEO,
691 .p.id = AV_CODEC_ID_INDEO5,
692 .priv_data_size = sizeof(IVI45DecContext),
693 .init = decode_init,
694 .close = ff_ivi_decode_close,
695 FF_CODEC_DECODE_CB(ff_ivi_decode_frame),
696 .p.capabilities = AV_CODEC_CAP_DR1,
697 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
698 };
699