FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4video.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 81 106 76.4%
Branches: 25 36 69.4%

Line Branch Exec Source
1 /*
2 * MPEG-4 decoder / encoder common code
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "libavutil/thread.h"
24
25 #include "mpegutils.h"
26 #include "mpegvideo.h"
27 #include "mpeg4video.h"
28 #include "mpeg4data.h"
29
30 152 static av_cold void mpeg4_init_rl_intra(void)
31 {
32 static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
33 152 ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table);
34 152 }
35
36 152 av_cold void ff_mpeg4_init_rl_intra(void)
37 {
38 static AVOnce init_static_once = AV_ONCE_INIT;
39 152 ff_thread_once(&init_static_once, mpeg4_init_rl_intra);
40 152 }
41
42 11288 int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s)
43 {
44
3/4
✓ Branch 0 taken 1437 times.
✓ Branch 1 taken 3746 times.
✓ Branch 2 taken 6105 times.
✗ Branch 3 not taken.
11288 switch (s->pict_type) {
45 1437 case AV_PICTURE_TYPE_I:
46 1437 return 16;
47 3746 case AV_PICTURE_TYPE_P:
48 case AV_PICTURE_TYPE_S:
49 3746 return s->f_code + 15;
50 6105 case AV_PICTURE_TYPE_B:
51 6105 return FFMAX3(s->f_code, s->b_code, 2) + 15;
52 default:
53 return -1;
54 }
55 }
56
57 7615 void ff_mpeg4_clean_buffers(MpegEncContext *s)
58 {
59 int c_wrap, c_xy, l_wrap, l_xy;
60
61 7615 l_wrap = s->b8_stride;
62 7615 l_xy = (2 * s->mb_y - 1) * l_wrap + s->mb_x * 2 - 1;
63 7615 c_wrap = s->mb_stride;
64 7615 c_xy = (s->mb_y - 1) * c_wrap + s->mb_x - 1;
65
66 /* clean AC */
67 7615 memset(s->ac_val[0] + l_xy, 0, (l_wrap * 2 + 1) * 16 * sizeof(int16_t));
68 7615 memset(s->ac_val[1] + c_xy, 0, (c_wrap + 1) * 16 * sizeof(int16_t));
69 7615 memset(s->ac_val[2] + c_xy, 0, (c_wrap + 1) * 16 * sizeof(int16_t));
70
71 /* clean MV */
72 // we can't clear the MVs as they might be needed by a B-frame
73 7615 s->last_mv[0][0][0] =
74 7615 s->last_mv[0][0][1] =
75 7615 s->last_mv[1][0][0] =
76 7615 s->last_mv[1][0][1] = 0;
77 7615 }
78
79 #define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
80 #define tab_bias (tab_size / 2)
81
82 // used by MPEG-4 and rv10 decoder
83 2186 void ff_mpeg4_init_direct_mv(MpegEncContext *s)
84 {
85 int i;
86
2/2
✓ Branch 0 taken 139904 times.
✓ Branch 1 taken 2186 times.
142090 for (i = 0; i < tab_size; i++) {
87 139904 s->direct_scale_mv[0][i] = (i - tab_bias) * s->pb_time / s->pp_time;
88 139904 s->direct_scale_mv[1][i] = (i - tab_bias) * (s->pb_time - s->pp_time) /
89 139904 s->pp_time;
90 }
91 2186 }
92
93 587165 static inline void ff_mpeg4_set_one_direct_mv(MpegEncContext *s, int mx,
94 int my, int i)
95 {
96 587165 int xy = s->block_index[i];
97 587165 uint16_t time_pp = s->pp_time;
98 587165 uint16_t time_pb = s->pb_time;
99 int p_mx, p_my;
100
101 587165 p_mx = s->next_picture.motion_val[0][xy][0];
102
2/2
✓ Branch 0 taken 566474 times.
✓ Branch 1 taken 20691 times.
587165 if ((unsigned)(p_mx + tab_bias) < tab_size) {
103 566474 s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias] + mx;
104 566474 s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
105
2/2
✓ Branch 0 taken 151782 times.
✓ Branch 1 taken 414692 times.
566474 : s->direct_scale_mv[1][p_mx + tab_bias];
106 } else {
107 20691 s->mv[0][i][0] = p_mx * time_pb / time_pp + mx;
108 20691 s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
109
2/2
✓ Branch 0 taken 13044 times.
✓ Branch 1 taken 7647 times.
20691 : p_mx * (time_pb - time_pp) / time_pp;
110 }
111 587165 p_my = s->next_picture.motion_val[0][xy][1];
112
2/2
✓ Branch 0 taken 566864 times.
✓ Branch 1 taken 20301 times.
587165 if ((unsigned)(p_my + tab_bias) < tab_size) {
113 566864 s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias] + my;
114 566864 s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
115
2/2
✓ Branch 0 taken 147533 times.
✓ Branch 1 taken 419331 times.
566864 : s->direct_scale_mv[1][p_my + tab_bias];
116 } else {
117 20301 s->mv[0][i][1] = p_my * time_pb / time_pp + my;
118 20301 s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
119
2/2
✓ Branch 0 taken 10025 times.
✓ Branch 1 taken 10276 times.
20301 : p_my * (time_pb - time_pp) / time_pp;
120 }
121 587165 }
122
123 #undef tab_size
124 #undef tab_bias
125
126 /**
127 * @return the mb_type
128 */
129 303413 int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
130 {
131 303413 const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
132 303413 const int colocated_mb_type = s->next_picture.mb_type[mb_index];
133 uint16_t time_pp;
134 uint16_t time_pb;
135 int i;
136
137 // FIXME avoid divides
138 // try special case with shifts for 1 and 3 B-frames?
139
140
2/2
✓ Branch 0 taken 94584 times.
✓ Branch 1 taken 208829 times.
303413 if (IS_8X8(colocated_mb_type)) {
141 94584 s->mv_type = MV_TYPE_8X8;
142
2/2
✓ Branch 0 taken 378336 times.
✓ Branch 1 taken 94584 times.
472920 for (i = 0; i < 4; i++)
143 378336 ff_mpeg4_set_one_direct_mv(s, mx, my, i);
144 94584 return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208829 times.
208829 } else if (IS_INTERLACED(colocated_mb_type)) {
146 s->mv_type = MV_TYPE_FIELD;
147 for (i = 0; i < 2; i++) {
148 int field_select = s->next_picture.ref_index[0][4 * mb_index + 2 * i];
149 s->field_select[0][i] = field_select;
150 s->field_select[1][i] = i;
151 if (s->top_field_first) {
152 time_pp = s->pp_field_time - field_select + i;
153 time_pb = s->pb_field_time - field_select + i;
154 } else {
155 time_pp = s->pp_field_time + field_select - i;
156 time_pb = s->pb_field_time + field_select - i;
157 }
158 s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0] *
159 time_pb / time_pp + mx;
160 s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1] *
161 time_pb / time_pp + my;
162 s->mv[1][i][0] = mx ? s->mv[0][i][0] -
163 s->p_field_mv_table[i][0][mb_index][0]
164 : s->p_field_mv_table[i][0][mb_index][0] *
165 (time_pb - time_pp) / time_pp;
166 s->mv[1][i][1] = my ? s->mv[0][i][1] -
167 s->p_field_mv_table[i][0][mb_index][1]
168 : s->p_field_mv_table[i][0][mb_index][1] *
169 (time_pb - time_pp) / time_pp;
170 }
171 return MB_TYPE_DIRECT2 | MB_TYPE_16x8 |
172 MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
173 } else {
174 208829 ff_mpeg4_set_one_direct_mv(s, mx, my, 0);
175 208829 s->mv[0][1][0] =
176 208829 s->mv[0][2][0] =
177 208829 s->mv[0][3][0] = s->mv[0][0][0];
178 208829 s->mv[0][1][1] =
179 208829 s->mv[0][2][1] =
180 208829 s->mv[0][3][1] = s->mv[0][0][1];
181 208829 s->mv[1][1][0] =
182 208829 s->mv[1][2][0] =
183 208829 s->mv[1][3][0] = s->mv[1][0][0];
184 208829 s->mv[1][1][1] =
185 208829 s->mv[1][2][1] =
186 208829 s->mv[1][3][1] = s->mv[1][0][1];
187
1/2
✓ Branch 0 taken 208829 times.
✗ Branch 1 not taken.
208829 if ((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) ||
188
2/2
✓ Branch 0 taken 179671 times.
✓ Branch 1 taken 29158 times.
208829 !s->quarter_sample)
189 179671 s->mv_type = MV_TYPE_16X16;
190 else
191 29158 s->mv_type = MV_TYPE_8X8;
192 // Note see prev line
193 208829 return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1;
194 }
195 }
196