FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wmv2dec.c
Date: 2026-01-23 14:02:51
Exec Total Coverage
Lines: 412 451 91.4%
Functions: 22 23 95.7%
Branches: 184 223 82.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002 The FFmpeg Project
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/mem_internal.h"
23
24 #include "avcodec.h"
25 #include "codec_internal.h"
26 #include "h263dec.h"
27 #include "intrax8.h"
28 #include "mathops.h"
29 #include "mpegutils.h"
30 #include "mpegvideo.h"
31 #include "mpegvideodec.h"
32 #include "msmpeg4.h"
33 #include "msmpeg4_vc1_data.h"
34 #include "msmpeg4dec.h"
35 #include "qpeldsp.h"
36 #include "simple_idct.h"
37 #include "wmv2.h"
38 #include "wmv2data.h"
39 #include "wmv2dec.h"
40
41 typedef struct WMV2DecContext {
42 MSMP4DecContext ms;
43 IntraX8Context x8;
44
45 qpel_mc_func put_mspel_pixels_tab[8];
46
47 int j_type_bit;
48 int j_type;
49 int abt_flag;
50 int abt_type;
51 int abt_type_table[6];
52 int per_mb_abt;
53 int per_block_abt;
54 int mspel_bit;
55 int cbp_table_index;
56 int top_left_mv_flag;
57 int per_mb_rl_bit;
58 int hshift;
59
60 DECLARE_ALIGNED(32, int16_t, abt_block2)[6][64];
61 } WMV2DecContext;
62
63 71392 static void wmv2_mspel8_h_lowpass(uint8_t *dst, const uint8_t *src,
64 int dstStride, int srcStride, int h)
65 {
66 71392 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
67
68
2/2
✓ Branch 0 taken 660824 times.
✓ Branch 1 taken 71392 times.
732216 for (int i = 0; i < h; i++) {
69 660824 dst[0] = cm[(9 * (src[0] + src[1]) - (src[-1] + src[2]) + 8) >> 4];
70 660824 dst[1] = cm[(9 * (src[1] + src[2]) - (src[0] + src[3]) + 8) >> 4];
71 660824 dst[2] = cm[(9 * (src[2] + src[3]) - (src[1] + src[4]) + 8) >> 4];
72 660824 dst[3] = cm[(9 * (src[3] + src[4]) - (src[2] + src[5]) + 8) >> 4];
73 660824 dst[4] = cm[(9 * (src[4] + src[5]) - (src[3] + src[6]) + 8) >> 4];
74 660824 dst[5] = cm[(9 * (src[5] + src[6]) - (src[4] + src[7]) + 8) >> 4];
75 660824 dst[6] = cm[(9 * (src[6] + src[7]) - (src[5] + src[8]) + 8) >> 4];
76 660824 dst[7] = cm[(9 * (src[7] + src[8]) - (src[6] + src[9]) + 8) >> 4];
77 660824 dst += dstStride;
78 660824 src += srcStride;
79 }
80 71392 }
81
82 55828 static void wmv2_mspel8_v_lowpass(uint8_t *dst, const uint8_t *src,
83 int dstStride, int srcStride, int w)
84 {
85 55828 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
86
87
2/2
✓ Branch 0 taken 446624 times.
✓ Branch 1 taken 55828 times.
502452 for (int i = 0; i < w; i++) {
88 446624 const int src_1 = src[-srcStride];
89 446624 const int src0 = src[0];
90 446624 const int src1 = src[srcStride];
91 446624 const int src2 = src[2 * srcStride];
92 446624 const int src3 = src[3 * srcStride];
93 446624 const int src4 = src[4 * srcStride];
94 446624 const int src5 = src[5 * srcStride];
95 446624 const int src6 = src[6 * srcStride];
96 446624 const int src7 = src[7 * srcStride];
97 446624 const int src8 = src[8 * srcStride];
98 446624 const int src9 = src[9 * srcStride];
99 446624 dst[0 * dstStride] = cm[(9 * (src0 + src1) - (src_1 + src2) + 8) >> 4];
100 446624 dst[1 * dstStride] = cm[(9 * (src1 + src2) - (src0 + src3) + 8) >> 4];
101 446624 dst[2 * dstStride] = cm[(9 * (src2 + src3) - (src1 + src4) + 8) >> 4];
102 446624 dst[3 * dstStride] = cm[(9 * (src3 + src4) - (src2 + src5) + 8) >> 4];
103 446624 dst[4 * dstStride] = cm[(9 * (src4 + src5) - (src3 + src6) + 8) >> 4];
104 446624 dst[5 * dstStride] = cm[(9 * (src5 + src6) - (src4 + src7) + 8) >> 4];
105 446624 dst[6 * dstStride] = cm[(9 * (src6 + src7) - (src5 + src8) + 8) >> 4];
106 446624 dst[7 * dstStride] = cm[(9 * (src7 + src8) - (src6 + src9) + 8) >> 4];
107 446624 src++;
108 446624 dst++;
109 }
110 55828 }
111
112 static void put_mspel8_mc10_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
113 {
114 uint8_t half[64];
115
116 wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
117 ff_put_pixels8_l2_8(dst, src, half, stride, stride, 8, 8);
118 }
119
120 27436 static void put_mspel8_mc20_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
121 {
122 27436 wmv2_mspel8_h_lowpass(dst, src, stride, stride, 8);
123 27436 }
124
125 14060 static void put_mspel8_mc30_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
126 {
127 uint8_t half[64];
128
129 14060 wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
130 14060 ff_put_pixels8_l2_8(dst, src + 1, half, stride, stride, 8, 8);
131 14060 }
132
133 6720 static void put_mspel8_mc02_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
134 {
135 6720 wmv2_mspel8_v_lowpass(dst, src, stride, stride, 8);
136 6720 }
137
138 9800 static void put_mspel8_mc12_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
139 {
140 uint8_t halfH[88];
141 uint8_t halfV[64];
142 uint8_t halfHV[64];
143
144 9800 wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
145 9800 wmv2_mspel8_v_lowpass(halfV, src, 8, stride, 8);
146 9800 wmv2_mspel8_v_lowpass(halfHV, halfH + 8, 8, 8, 8);
147 9800 ff_put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
148 9800 }
149
150 9412 static void put_mspel8_mc32_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
151 {
152 uint8_t halfH[88];
153 uint8_t halfV[64];
154 uint8_t halfHV[64];
155
156 9412 wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
157 9412 wmv2_mspel8_v_lowpass(halfV, src + 1, 8, stride, 8);
158 9412 wmv2_mspel8_v_lowpass(halfHV, halfH + 8, 8, 8, 8);
159 9412 ff_put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
160 9412 }
161
162 10684 static void put_mspel8_mc22_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
163 {
164 uint8_t halfH[88];
165
166 10684 wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
167 10684 wmv2_mspel8_v_lowpass(dst, halfH + 8, stride, 8, 8);
168 10684 }
169
170 12 static av_cold void wmv2_mspel_init(WMV2DecContext *w)
171 {
172 12 w->put_mspel_pixels_tab[0] = ff_put_pixels8x8_c;
173 12 w->put_mspel_pixels_tab[1] = put_mspel8_mc10_c;
174 12 w->put_mspel_pixels_tab[2] = put_mspel8_mc20_c;
175 12 w->put_mspel_pixels_tab[3] = put_mspel8_mc30_c;
176 12 w->put_mspel_pixels_tab[4] = put_mspel8_mc02_c;
177 12 w->put_mspel_pixels_tab[5] = put_mspel8_mc12_c;
178 12 w->put_mspel_pixels_tab[6] = put_mspel8_mc22_c;
179 12 w->put_mspel_pixels_tab[7] = put_mspel8_mc32_c;
180 12 }
181
182 80493 void ff_mspel_motion(MPVContext *const s, uint8_t *dest_y,
183 uint8_t *dest_cb, uint8_t *dest_cr,
184 uint8_t *const *ref_picture,
185 const op_pixels_func (*pix_op)[4],
186 int motion_x, int motion_y, int h)
187 {
188 80493 WMV2DecContext *const w = (WMV2DecContext *) s;
189 const uint8_t *ptr;
190 int dxy, mx, my, src_x, src_y, v_edge_pos;
191 ptrdiff_t offset, linesize, uvlinesize;
192 80493 int emu = 0;
193
194 80493 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
195 80493 dxy = 2 * dxy + w->hshift;
196 80493 src_x = s->mb_x * 16 + (motion_x >> 1);
197 80493 src_y = s->mb_y * 16 + (motion_y >> 1);
198
199 /* WARNING: do no forget half pels */
200 80493 v_edge_pos = s->v_edge_pos;
201 80493 src_x = av_clip(src_x, -16, s->width);
202 80493 src_y = av_clip(src_y, -16, s->height);
203
204
3/4
✓ Branch 0 taken 80492 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80492 times.
80493 if (src_x <= -16 || src_x >= s->width)
205 1 dxy &= ~3;
206
3/4
✓ Branch 0 taken 80493 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 80492 times.
80493 if (src_y <= -16 || src_y >= s->height)
207 1 dxy &= ~4;
208
209 80493 linesize = s->linesize;
210 80493 uvlinesize = s->uvlinesize;
211 80493 ptr = ref_picture[0] + (src_y * linesize) + src_x;
212
213
6/6
✓ Branch 0 taken 76543 times.
✓ Branch 1 taken 3950 times.
✓ Branch 2 taken 71431 times.
✓ Branch 3 taken 5112 times.
✓ Branch 4 taken 67720 times.
✓ Branch 5 taken 3711 times.
80493 if (src_x < 1 || src_y < 1 || src_x + 17 >= s->h_edge_pos ||
214
2/2
✓ Branch 0 taken 4837 times.
✓ Branch 1 taken 62883 times.
67720 src_y + h + 1 >= v_edge_pos) {
215 17610 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr - 1 - s->linesize,
216 s->linesize, s->linesize, 19, 19,
217 src_x - 1, src_y - 1,
218 s->h_edge_pos, s->v_edge_pos);
219 17610 ptr = s->sc.edge_emu_buffer + 1 + s->linesize;
220 17610 emu = 1;
221 }
222
223 80493 w->put_mspel_pixels_tab[dxy](dest_y, ptr, linesize);
224 80493 w->put_mspel_pixels_tab[dxy](dest_y + 8, ptr + 8, linesize);
225 80493 w->put_mspel_pixels_tab[dxy](dest_y + 8 * linesize, ptr + 8 * linesize, linesize);
226 80493 w->put_mspel_pixels_tab[dxy](dest_y + 8 + 8 * linesize, ptr + 8 + 8 * linesize, linesize);
227
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80493 times.
80493 if (s->avctx->flags & AV_CODEC_FLAG_GRAY)
229 return;
230
231 80493 dxy = 0;
232
2/2
✓ Branch 0 taken 22071 times.
✓ Branch 1 taken 58422 times.
80493 if ((motion_x & 3) != 0)
233 22071 dxy |= 1;
234
2/2
✓ Branch 0 taken 11804 times.
✓ Branch 1 taken 68689 times.
80493 if ((motion_y & 3) != 0)
235 11804 dxy |= 2;
236 80493 mx = motion_x >> 2;
237 80493 my = motion_y >> 2;
238
239 80493 src_x = s->mb_x * 8 + mx;
240 80493 src_y = s->mb_y * 8 + my;
241 80493 src_x = av_clip(src_x, -8, s->width >> 1);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80493 times.
80493 if (src_x == (s->width >> 1))
243 dxy &= ~1;
244 80493 src_y = av_clip(src_y, -8, s->height >> 1);
245
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 80492 times.
80493 if (src_y == (s->height >> 1))
246 1 dxy &= ~2;
247 80493 offset = (src_y * uvlinesize) + src_x;
248 80493 ptr = ref_picture[1] + offset;
249
2/2
✓ Branch 0 taken 17610 times.
✓ Branch 1 taken 62883 times.
80493 if (emu) {
250 17610 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
251 s->uvlinesize, s->uvlinesize,
252 9, 9,
253 src_x, src_y,
254 17610 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
255 17610 ptr = s->sc.edge_emu_buffer;
256 }
257 80493 pix_op[1][dxy](dest_cb, ptr, uvlinesize, h >> 1);
258
259 80493 ptr = ref_picture[2] + offset;
260
2/2
✓ Branch 0 taken 17610 times.
✓ Branch 1 taken 62883 times.
80493 if (emu) {
261 17610 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
262 s->uvlinesize, s->uvlinesize,
263 9, 9,
264 src_x, src_y,
265 17610 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
266 17610 ptr = s->sc.edge_emu_buffer;
267 }
268 80493 pix_op[1][dxy](dest_cr, ptr, uvlinesize, h >> 1);
269 }
270
271 1140894 static void wmv2_add_block(WMV2DecContext *w, int16_t blocks1[][64],
272 uint8_t *dst, int stride, int n)
273 {
274 1140894 H263DecContext *const h = &w->ms.h;
275
276
2/2
✓ Branch 0 taken 180589 times.
✓ Branch 1 taken 960305 times.
1140894 if (h->c.block_last_index[n] >= 0) {
277 180589 int16_t *block1 = blocks1[n];
278
3/4
✓ Branch 0 taken 149142 times.
✓ Branch 1 taken 16469 times.
✓ Branch 2 taken 14978 times.
✗ Branch 3 not taken.
180589 switch (w->abt_type_table[n]) {
279 149142 case 0:
280 149142 h->c.idsp.idct_add(dst, stride, block1);
281 149142 break;
282 16469 case 1:
283 16469 ff_simple_idct84_add(dst, stride, block1);
284 16469 ff_simple_idct84_add(dst + 4 * stride, stride, w->abt_block2[n]);
285 16469 h->c.bdsp.clear_block(w->abt_block2[n]);
286 16469 break;
287 14978 case 2:
288 14978 ff_simple_idct48_add(dst, stride, block1);
289 14978 ff_simple_idct48_add(dst + 4, stride, w->abt_block2[n]);
290 14978 h->c.bdsp.clear_block(w->abt_block2[n]);
291 14978 break;
292 default:
293 av_unreachable("abt_type_table is read via decode012");
294 }
295 }
296 1140894 }
297
298 190149 void ff_wmv2_add_mb(MpegEncContext *s, int16_t block1[6][64],
299 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr)
300 {
301 190149 WMV2DecContext *const w = (WMV2DecContext *) s;
302
303 190149 wmv2_add_block(w, block1, dest_y, s->linesize, 0);
304 190149 wmv2_add_block(w, block1, dest_y + 8, s->linesize, 1);
305 190149 wmv2_add_block(w, block1, dest_y + 8 * s->linesize, s->linesize, 2);
306 190149 wmv2_add_block(w, block1, dest_y + 8 + 8 * s->linesize, s->linesize, 3);
307
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 190149 times.
190149 if (s->avctx->flags & AV_CODEC_FLAG_GRAY)
309 return;
310
311 190149 wmv2_add_block(w, block1, dest_cb, s->uvlinesize, 4);
312 190149 wmv2_add_block(w, block1, dest_cr, s->uvlinesize, 5);
313 }
314
315 645 static int parse_mb_skip(WMV2DecContext *w)
316 {
317 645 H263DecContext *const h = &w->ms.h;
318 645 int coded_mb_count = 0;
319 645 uint32_t *const mb_type = h->c.cur_pic.mb_type;
320
321 645 int skip_type = get_bits(&h->gb, 2);
322
4/5
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 354 times.
✓ Branch 3 taken 33 times.
✗ Branch 4 not taken.
645 switch (skip_type) {
323 230 case SKIP_TYPE_NONE:
324
2/2
✓ Branch 0 taken 3315 times.
✓ Branch 1 taken 230 times.
3545 for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++)
325
2/2
✓ Branch 0 taken 68865 times.
✓ Branch 1 taken 3315 times.
72180 for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++)
326 68865 mb_type[mb_y * h->c.mb_stride + mb_x] =
327 MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
328 230 break;
329 28 case SKIP_TYPE_MPEG:
330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (get_bits_left(&h->gb) < h->c.mb_height * h->c.mb_width)
331 return AVERROR_INVALIDDATA;
332
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 28 times.
448 for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++)
333
2/2
✓ Branch 0 taken 8400 times.
✓ Branch 1 taken 420 times.
8820 for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++)
334 8400 mb_type[mb_y * h->c.mb_stride + mb_x] =
335
2/2
✓ Branch 1 taken 5149 times.
✓ Branch 2 taken 3251 times.
8400 (get_bits1(&h->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
336 28 break;
337 354 case SKIP_TYPE_ROW:
338
2/2
✓ Branch 0 taken 5310 times.
✓ Branch 1 taken 354 times.
5664 for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++) {
339
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5310 times.
5310 if (get_bits_left(&h->gb) < 1)
340 return AVERROR_INVALIDDATA;
341
2/2
✓ Branch 1 taken 820 times.
✓ Branch 2 taken 4490 times.
5310 if (get_bits1(&h->gb)) {
342
2/2
✓ Branch 0 taken 16400 times.
✓ Branch 1 taken 820 times.
17220 for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++)
343 16400 mb_type[mb_y * h->c.mb_stride + mb_x] =
344 MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
345 } else {
346
2/2
✓ Branch 0 taken 89800 times.
✓ Branch 1 taken 4490 times.
94290 for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++)
347 89800 mb_type[mb_y * h->c.mb_stride + mb_x] =
348
2/2
✓ Branch 1 taken 42505 times.
✓ Branch 2 taken 47295 times.
89800 (get_bits1(&h->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
349 }
350 }
351 354 break;
352 33 case SKIP_TYPE_COL:
353
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 33 times.
693 for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++) {
354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 660 times.
660 if (get_bits_left(&h->gb) < 1)
355 return AVERROR_INVALIDDATA;
356
2/2
✓ Branch 1 taken 171 times.
✓ Branch 2 taken 489 times.
660 if (get_bits1(&h->gb)) {
357
2/2
✓ Branch 0 taken 2565 times.
✓ Branch 1 taken 171 times.
2736 for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++)
358 2565 mb_type[mb_y * h->c.mb_stride + mb_x] =
359 MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
360 } else {
361
2/2
✓ Branch 0 taken 7335 times.
✓ Branch 1 taken 489 times.
7824 for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++)
362 7335 mb_type[mb_y * h->c.mb_stride + mb_x] =
363
2/2
✓ Branch 1 taken 5106 times.
✓ Branch 2 taken 2229 times.
7335 (get_bits1(&h->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
364 }
365 }
366 33 break;
367 }
368
369
2/2
✓ Branch 0 taken 9540 times.
✓ Branch 1 taken 645 times.
10185 for (int mb_y = 0; mb_y < h->c.mb_height; mb_y++)
370
2/2
✓ Branch 0 taken 193365 times.
✓ Branch 1 taken 9540 times.
202905 for (int mb_x = 0; mb_x < h->c.mb_width; mb_x++)
371 193365 coded_mb_count += !IS_SKIP(mb_type[mb_y * h->c.mb_stride + mb_x]);
372
373
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 645 times.
645 if (coded_mb_count > get_bits_left(&h->gb))
374 return AVERROR_INVALIDDATA;
375
376 645 return 0;
377 }
378
379 12 static av_cold int decode_ext_header(AVCodecContext *avctx, WMV2DecContext *w)
380 {
381 12 H263DecContext *const h = &w->ms.h;
382 GetBitContext gb;
383 int fps;
384 int code;
385
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (avctx->extradata_size < 4)
387 return AVERROR_INVALIDDATA;
388
389 12 init_get_bits(&gb, avctx->extradata, 32);
390
391 12 fps = get_bits(&gb, 5);
392 12 w->ms.bit_rate = get_bits(&gb, 11) * 1024;
393 12 w->mspel_bit = get_bits1(&gb);
394 12 h->loop_filter = get_bits1(&gb);
395 12 w->abt_flag = get_bits1(&gb);
396 12 w->j_type_bit = get_bits1(&gb);
397 12 w->top_left_mv_flag = get_bits1(&gb);
398 12 w->per_mb_rl_bit = get_bits1(&gb);
399 12 code = get_bits(&gb, 3);
400
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (code == 0)
402 return AVERROR_INVALIDDATA;
403
404 12 h->slice_height = h->c.mb_height / code;
405
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (avctx->debug & FF_DEBUG_PICT_INFO)
407 av_log(avctx, AV_LOG_DEBUG,
408 "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, "
409 "tl_mv_flag:%d, mbrl_bit:%d, code:%d, loop_filter:%d, "
410 "slices:%d\n",
411 fps, w->ms.bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit,
412 w->top_left_mv_flag, w->per_mb_rl_bit, code, h->loop_filter,
413 code);
414 12 return 0;
415 }
416
417 674 static int wmv2_decode_picture_header(H263DecContext *const h)
418 {
419 int code;
420
421 674 h->c.pict_type = get_bits1(&h->gb) + 1;
422
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 645 times.
674 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
423 29 code = get_bits(&h->gb, 7);
424 29 av_log(h->c.avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
425 }
426 674 h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 674 times.
674 if (h->c.qscale <= 0)
428 return AVERROR_INVALIDDATA;
429
430
4/4
✓ Branch 0 taken 645 times.
✓ Branch 1 taken 29 times.
✓ Branch 3 taken 387 times.
✓ Branch 4 taken 258 times.
674 if (h->c.pict_type != AV_PICTURE_TYPE_I && show_bits(&h->gb, 1)) {
431 387 GetBitContext gb = h->gb;
432 387 int skip_type = get_bits(&gb, 2);
433
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 354 times.
387 int run = skip_type == SKIP_TYPE_COL ? h->c.mb_width : h->c.mb_height;
434
435
1/2
✓ Branch 0 taken 387 times.
✗ Branch 1 not taken.
387 while (run > 0) {
436 387 int block = FFMIN(run, 25);
437
1/2
✓ Branch 1 taken 387 times.
✗ Branch 2 not taken.
387 if (get_bits(&gb, block) + 1 != 1<<block)
438 387 break;
439 run -= block;
440 }
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 387 times.
387 if (!run)
442 return FRAME_SKIPPED;
443 }
444
445 674 return 0;
446 }
447
448 674 int ff_wmv2_decode_secondary_picture_header(H263DecContext *const h)
449 {
450 674 WMV2DecContext *const w = (WMV2DecContext *)h;
451
452
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 645 times.
674 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
453 /* Is filling with zeroes really the right thing to do? */
454 29 memset(h->c.cur_pic.mb_type, 0,
455 29 sizeof(*h->c.cur_pic.mb_type) * h->c.mb_height * h->c.mb_stride);
456
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (w->j_type_bit)
457 29 w->j_type = get_bits1(&h->gb);
458 else
459 w->j_type = 0; // FIXME check
460
461
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 3 times.
29 if (!w->j_type) {
462
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (w->per_mb_rl_bit)
463 26 w->ms.per_mb_rl_table = get_bits1(&h->gb);
464 else
465 w->ms.per_mb_rl_table = 0;
466
467
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (!w->ms.per_mb_rl_table) {
468 26 w->ms.rl_chroma_table_index = decode012(&h->gb);
469 26 w->ms.rl_table_index = decode012(&h->gb);
470 }
471
472 26 w->ms.dc_table_index = get_bits1(&h->gb);
473
474 // at minimum one bit per macroblock is required at least in a valid frame,
475 // we discard frames much smaller than this. Frames smaller than 1/8 of the
476 // smallest "black/skip" frame generally contain not much recoverable content
477 // while at the same time they have the highest computational requirements
478 // per byte
479
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
26 if (get_bits_left(&h->gb) * 8LL < (h->c.width+15)/16 * ((h->c.height+15)/16))
480 return AVERROR_INVALIDDATA;
481 }
482 29 h->c.inter_intra_pred = 0;
483 29 h->c.no_rounding = 1;
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
485 av_log(h->c.avctx, AV_LOG_DEBUG,
486 "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
487 h->c.qscale, w->ms.rl_chroma_table_index, w->ms.rl_table_index,
488 w->ms.dc_table_index, w->ms.per_mb_rl_table, w->j_type);
489 }
490 } else {
491 int cbp_index;
492 int ret;
493 645 w->j_type = 0;
494
495 645 ret = parse_mb_skip(w);
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 if (ret < 0)
497 return ret;
498 645 cbp_index = decode012(&h->gb);
499 645 w->cbp_table_index = wmv2_get_cbp_table_index(h->c.qscale, cbp_index);
500
501
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 if (w->mspel_bit)
502 645 h->c.mspel = get_bits1(&h->gb);
503 else
504 h->c.mspel = 0; // FIXME check
505
506
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 if (w->abt_flag) {
507 645 w->per_mb_abt = get_bits1(&h->gb) ^ 1;
508
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 458 times.
645 if (!w->per_mb_abt)
509 187 w->abt_type = decode012(&h->gb);
510 }
511
512
1/2
✓ Branch 0 taken 645 times.
✗ Branch 1 not taken.
645 if (w->per_mb_rl_bit)
513 645 w->ms.per_mb_rl_table = get_bits1(&h->gb);
514 else
515 w->ms.per_mb_rl_table = 0;
516
517
2/2
✓ Branch 0 taken 617 times.
✓ Branch 1 taken 28 times.
645 if (!w->ms.per_mb_rl_table) {
518 617 w->ms.rl_table_index = decode012(&h->gb);
519 617 w->ms.rl_chroma_table_index = w->ms.rl_table_index;
520 }
521
522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 645 times.
645 if (get_bits_left(&h->gb) < 2)
523 return AVERROR_INVALIDDATA;
524
525 645 w->ms.dc_table_index = get_bits1(&h->gb);
526 645 w->ms.mv_table_index = get_bits1(&h->gb);
527
528 645 h->c.inter_intra_pred = 0; // (h->c.width * h->c.height < 320 * 240 && w->ms.bit_rate <= II_BITRATE);
529 645 h->c.no_rounding ^= 1;
530
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
532 av_log(h->c.avctx, AV_LOG_DEBUG,
533 "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d "
534 "per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
535 w->ms.rl_table_index, w->ms.rl_chroma_table_index,
536 w->ms.dc_table_index, w->ms.mv_table_index,
537 w->ms.per_mb_rl_table, h->c.qscale, h->c.mspel,
538 w->per_mb_abt, w->abt_type, w->cbp_table_index,
539 h->c.inter_intra_pred);
540 }
541 }
542 674 w->ms.esc3_level_length = 0;
543 674 w->ms.esc3_run_length = 0;
544
545
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 671 times.
674 if (w->j_type) {
546 3 ff_intrax8_decode_picture(&w->x8, h->c.cur_pic.ptr,
547 &h->gb, &h->c.mb_x, &h->c.mb_y,
548 3 2 * h->c.qscale, (h->c.qscale - 1) | 1,
549 h->loop_filter, h->c.low_delay);
550
551 3 ff_er_add_slice(&h->c.er, 0, 0,
552 3 (h->c.mb_x >> 1) - 1, (h->c.mb_y >> 1) - 1,
553 ER_MB_END);
554 3 return 1;
555 }
556
557 671 return 0;
558 }
559
560 118424 static inline void wmv2_decode_motion(WMV2DecContext *w, int *mx_ptr, int *my_ptr)
561 {
562 118424 H263DecContext *const h = &w->ms.h;
563
564 118424 ff_msmpeg4_decode_motion(&w->ms, mx_ptr, my_ptr);
565
566
4/4
✓ Branch 0 taken 50949 times.
✓ Branch 1 taken 67475 times.
✓ Branch 2 taken 19528 times.
✓ Branch 3 taken 31421 times.
118424 if ((((*mx_ptr) | (*my_ptr)) & 1) && h->c.mspel)
567 19528 w->hshift = get_bits1(&h->gb);
568 else
569 98896 w->hshift = 0;
570 118424 }
571
572 118424 static int16_t *wmv2_pred_motion(WMV2DecContext *w, int *px, int *py)
573 {
574 118424 H263DecContext *const h = &w->ms.h;
575 int diff, type;
576
577 118424 int wrap = h->c.b8_stride;
578 118424 int xy = h->c.block_index[0];
579
580 118424 int16_t *mot_val = h->c.cur_pic.motion_val[0][xy];
581
582 118424 const int16_t *A = h->c.cur_pic.motion_val[0][xy - 1];
583 118424 const int16_t *B = h->c.cur_pic.motion_val[0][xy - wrap];
584 118424 const int16_t *C = h->c.cur_pic.motion_val[0][xy + 2 - wrap];
585
586
8/8
✓ Branch 0 taken 113312 times.
✓ Branch 1 taken 5112 times.
✓ Branch 2 taken 109268 times.
✓ Branch 3 taken 4044 times.
✓ Branch 4 taken 68720 times.
✓ Branch 5 taken 40548 times.
✓ Branch 6 taken 22431 times.
✓ Branch 7 taken 46289 times.
118424 if (h->c.mb_x && !h->c.first_slice_line && !h->c.mspel && w->top_left_mv_flag)
587 22431 diff = FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
588 else
589 95993 diff = 0;
590
591
2/2
✓ Branch 0 taken 3203 times.
✓ Branch 1 taken 115221 times.
118424 if (diff >= 8)
592 3203 type = get_bits1(&h->gb);
593 else
594 115221 type = 2;
595
596
2/2
✓ Branch 0 taken 1698 times.
✓ Branch 1 taken 116726 times.
118424 if (type == 0) {
597 1698 *px = A[0];
598 1698 *py = A[1];
599
2/2
✓ Branch 0 taken 1505 times.
✓ Branch 1 taken 115221 times.
116726 } else if (type == 1) {
600 1505 *px = B[0];
601 1505 *py = B[1];
602 } else {
603 /* special case for first (slice) line */
604
2/2
✓ Branch 0 taken 4281 times.
✓ Branch 1 taken 110940 times.
115221 if (h->c.first_slice_line) {
605 4281 *px = A[0];
606 4281 *py = A[1];
607 } else {
608 110940 *px = mid_pred(A[0], B[0], C[0]);
609 110940 *py = mid_pred(A[1], B[1], C[1]);
610 }
611 }
612
613 118424 return mot_val;
614 }
615
616 710544 static inline int wmv2_decode_inter_block(WMV2DecContext *w, int16_t *block,
617 int n, int cbp)
618 {
619 710544 H263DecContext *const h = &w->ms.h;
620 static const int sub_cbp_table[3] = { 2, 3, 1 };
621 int sub_cbp, ret;
622
623
2/2
✓ Branch 0 taken 529955 times.
✓ Branch 1 taken 180589 times.
710544 if (!cbp) {
624 529955 h->c.block_last_index[n] = -1;
625 529955 return 0;
626 }
627
628
2/2
✓ Branch 0 taken 20828 times.
✓ Branch 1 taken 159761 times.
180589 if (w->per_block_abt)
629 20828 w->abt_type = decode012(&h->gb);
630 180589 w->abt_type_table[n] = w->abt_type;
631
632
2/2
✓ Branch 0 taken 31447 times.
✓ Branch 1 taken 149142 times.
180589 if (w->abt_type) {
633
2/2
✓ Branch 0 taken 16469 times.
✓ Branch 1 taken 14978 times.
31447 const uint8_t *scantable = w->abt_type == 1 ? ff_wmv2_scantableA : ff_wmv2_scantableB;
634
635 31447 sub_cbp = sub_cbp_table[decode012(&h->gb)];
636
637
2/2
✓ Branch 0 taken 18950 times.
✓ Branch 1 taken 12497 times.
31447 if (sub_cbp & 1) {
638 18950 ret = ff_msmpeg4_decode_block(&w->ms, block, n, 1, scantable);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18950 times.
18950 if (ret < 0)
640 return ret;
641 }
642
643
2/2
✓ Branch 0 taken 19090 times.
✓ Branch 1 taken 12357 times.
31447 if (sub_cbp & 2) {
644 19090 ret = ff_msmpeg4_decode_block(&w->ms, w->abt_block2[n], n, 1, scantable);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19090 times.
19090 if (ret < 0)
646 return ret;
647 }
648
649 31447 h->c.block_last_index[n] = 63;
650
651 31447 return 0;
652 } else {
653 149142 return ff_msmpeg4_decode_block(&w->ms, block, n, 1,
654 149142 h->c.inter_scantable.permutated);
655 }
656 }
657
658 201150 static int wmv2_decode_mb(H263DecContext *const h)
659 {
660 /* The following is only allowed because this decoder
661 * does not use slice threading. */
662 201150 WMV2DecContext *const w = (WMV2DecContext *) h;
663 201150 MSMP4DecContext *const ms = &w->ms;
664 int cbp, code, i, ret;
665 uint8_t *coded_val;
666
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201150 times.
201150 if (w->j_type)
668 return 0;
669
670
2/2
✓ Branch 0 taken 193365 times.
✓ Branch 1 taken 7785 times.
201150 if (h->c.pict_type == AV_PICTURE_TYPE_P) {
671
2/2
✓ Branch 0 taken 71725 times.
✓ Branch 1 taken 121640 times.
193365 if (IS_SKIP(h->c.cur_pic.mb_type[h->c.mb_y * h->c.mb_stride + h->c.mb_x])) {
672 /* skip mb */
673 71725 h->c.mb_intra = 0;
674
2/2
✓ Branch 0 taken 430350 times.
✓ Branch 1 taken 71725 times.
502075 for (i = 0; i < 6; i++)
675 430350 h->c.block_last_index[i] = -1;
676 71725 h->c.mv_dir = MV_DIR_FORWARD;
677 71725 h->c.mv_type = MV_TYPE_16X16;
678 71725 h->c.mv[0][0][0] = 0;
679 71725 h->c.mv[0][0][1] = 0;
680 71725 h->c.mb_skipped = 1;
681 71725 w->hshift = 0;
682 71725 return 0;
683 }
684
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 121640 times.
121640 if (get_bits_left(&h->gb) <= 0)
685 return AVERROR_INVALIDDATA;
686
687 121640 code = get_vlc2(&h->gb, ff_mb_non_intra_vlc[w->cbp_table_index],
688 MB_NON_INTRA_VLC_BITS, 3);
689 121640 h->c.mb_intra = (~code & 0x40) >> 6;
690
691 121640 cbp = code & 0x3f;
692 } else {
693 7785 h->c.mb_intra = 1;
694
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7785 times.
7785 if (get_bits_left(&h->gb) <= 0)
695 return AVERROR_INVALIDDATA;
696 7785 code = get_vlc2(&h->gb, ff_msmp4_mb_i_vlc,
697 MSMP4_MB_INTRA_VLC_BITS, 2);
698 /* predict coded block pattern */
699 7785 cbp = 0;
700
2/2
✓ Branch 0 taken 46710 times.
✓ Branch 1 taken 7785 times.
54495 for (i = 0; i < 6; i++) {
701 46710 int val = ((code >> (5 - i)) & 1);
702
2/2
✓ Branch 0 taken 31140 times.
✓ Branch 1 taken 15570 times.
46710 if (i < 4) {
703 31140 int pred = ff_msmpeg4_coded_block_pred(&h->c, i, &coded_val);
704 31140 val = val ^ pred;
705 31140 *coded_val = val;
706 }
707 46710 cbp |= val << (5 - i);
708 }
709 }
710
711
2/2
✓ Branch 0 taken 118424 times.
✓ Branch 1 taken 11001 times.
129425 if (!h->c.mb_intra) {
712 int mx, my;
713 118424 wmv2_pred_motion(w, &mx, &my);
714
715
2/2
✓ Branch 0 taken 76773 times.
✓ Branch 1 taken 41651 times.
118424 if (cbp) {
716 76773 h->c.bdsp.clear_blocks(h->block[0]);
717
2/2
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 75117 times.
76773 if (ms->per_mb_rl_table) {
718 1656 ms->rl_table_index = decode012(&h->gb);
719 1656 ms->rl_chroma_table_index = ms->rl_table_index;
720 }
721
722
3/4
✓ Branch 0 taken 76773 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37493 times.
✓ Branch 3 taken 39280 times.
76773 if (w->abt_flag && w->per_mb_abt) {
723 37493 w->per_block_abt = get_bits1(&h->gb);
724
2/2
✓ Branch 0 taken 30618 times.
✓ Branch 1 taken 6875 times.
37493 if (!w->per_block_abt)
725 30618 w->abt_type = decode012(&h->gb);
726 } else
727 39280 w->per_block_abt = 0;
728 }
729
730 118424 wmv2_decode_motion(w, &mx, &my);
731
732 118424 h->c.mv_dir = MV_DIR_FORWARD;
733 118424 h->c.mv_type = MV_TYPE_16X16;
734 118424 h->c.mv[0][0][0] = mx;
735 118424 h->c.mv[0][0][1] = my;
736
737
2/2
✓ Branch 0 taken 710544 times.
✓ Branch 1 taken 118424 times.
828968 for (i = 0; i < 6; i++) {
738
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 710544 times.
710544 if ((ret = wmv2_decode_inter_block(w, h->block[i], i, (cbp >> (5 - i)) & 1)) < 0) {
739 av_log(h->c.avctx, AV_LOG_ERROR,
740 "\nerror while decoding inter block: %d x %d (%d)\n",
741 h->c.mb_x, h->c.mb_y, i);
742 return ret;
743 }
744 }
745 } else {
746 11001 if (h->c.pict_type == AV_PICTURE_TYPE_P)
747 ff_dlog(h->c.avctx, "%d%d ", h->c.inter_intra_pred, cbp);
748 ff_dlog(h->c.avctx, "I at %d %d %d %06X\n", h->c.mb_x, h->c.mb_y,
749 ((cbp & 3) ? 1 : 0) + ((cbp & 0x3C) ? 2 : 0),
750 show_bits(&h->gb, 24));
751 11001 h->c.ac_pred = get_bits1(&h->gb);
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11001 times.
11001 if (h->c.inter_intra_pred) {
753 h->c.h263_aic_dir = get_vlc2(&h->gb, ff_inter_intra_vlc,
754 INTER_INTRA_VLC_BITS, 1);
755 ff_dlog(h->c.avctx, "%d%d %d %d/",
756 h->c.ac_pred, h->c.h263_aic_dir, h->c.mb_x, h->c.mb_y);
757 }
758
4/4
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 10740 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 133 times.
11001 if (ms->per_mb_rl_table && cbp) {
759 128 ms->rl_table_index = decode012(&h->gb);
760 128 ms->rl_chroma_table_index = ms->rl_table_index;
761 }
762
763 11001 h->c.bdsp.clear_blocks(h->block[0]);
764
2/2
✓ Branch 0 taken 66006 times.
✓ Branch 1 taken 11001 times.
77007 for (i = 0; i < 6; i++) {
765 66006 ret = ff_msmpeg4_decode_block(ms, h->block[i], i, (cbp >> (5 - i)) & 1, NULL);
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66006 times.
66006 if (ret < 0) {
767 av_log(h->c.avctx, AV_LOG_ERROR,
768 "\nerror while decoding intra block: %d x %d (%d)\n",
769 h->c.mb_x, h->c.mb_y, i);
770 return ret;
771 }
772 }
773 }
774
775 129425 return 0;
776 }
777
778 12 static av_cold int wmv2_decode_init(AVCodecContext *avctx)
779 {
780 12 WMV2DecContext *const w = avctx->priv_data;
781 12 H263DecContext *const h = &w->ms.h;
782 12 MpegEncContext *const s = &h->c;
783 int ret;
784
785 12 wmv2_mspel_init(w);
786
787
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
788 return ret;
789
790 12 h->decode_header = wmv2_decode_picture_header;
791 12 h->decode_mb = wmv2_decode_mb;
792
793 12 decode_ext_header(avctx, w);
794
795 12 return ff_intrax8_common_init(avctx, &w->x8, h->block[0],
796 s->mb_width, s->mb_height);
797 }
798
799 12 static av_cold int wmv2_decode_end(AVCodecContext *avctx)
800 {
801 12 WMV2DecContext *const w = avctx->priv_data;
802
803 12 ff_intrax8_common_end(&w->x8);
804 12 return ff_mpv_decode_close(avctx);
805 }
806
807 const FFCodec ff_wmv2_decoder = {
808 .p.name = "wmv2",
809 CODEC_LONG_NAME("Windows Media Video 8"),
810 .p.type = AVMEDIA_TYPE_VIDEO,
811 .p.id = AV_CODEC_ID_WMV2,
812 .priv_data_size = sizeof(WMV2DecContext),
813 .init = wmv2_decode_init,
814 .close = wmv2_decode_end,
815 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
816 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
817 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
818 };
819