FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegutils.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 92 156 59.0%
Branches: 59 138 42.8%

Line Branch Exec Source
1 /*
2 * Mpeg video formats-related defines and utility functions
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 <stdint.h>
22
23 #include "libavutil/common.h"
24 #include "libavutil/frame.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/motion_vector.h"
27 #include "libavutil/avassert.h"
28
29 #include "avcodec.h"
30 #include "mpegutils.h"
31
32 3560 static int add_mb(AVMotionVector *mb, uint32_t mb_type,
33 int dst_x, int dst_y,
34 int motion_x, int motion_y, int motion_scale,
35 int direction)
36 {
37
4/4
✓ Branch 0 taken 2444 times.
✓ Branch 1 taken 1116 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 2414 times.
3560 mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
38
4/4
✓ Branch 0 taken 2444 times.
✓ Branch 1 taken 1116 times.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 2384 times.
3560 mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
39 3560 mb->motion_x = motion_x;
40 3560 mb->motion_y = motion_y;
41 3560 mb->motion_scale = motion_scale;
42 3560 mb->dst_x = dst_x;
43 3560 mb->dst_y = dst_y;
44 3560 mb->src_x = dst_x + motion_x / motion_scale;
45 3560 mb->src_y = dst_y + motion_y / motion_scale;
46
2/2
✓ Branch 0 taken 1164 times.
✓ Branch 1 taken 2396 times.
3560 mb->source = direction ? 1 : -1;
47 3560 mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
48 3560 return 1;
49 }
50
51 204929 void ff_draw_horiz_band(AVCodecContext *avctx,
52 AVFrame *cur, AVFrame *last,
53 int y, int h, int picture_structure,
54 int first_field, int low_delay)
55 {
56 204929 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
57 204929 int vshift = desc->log2_chroma_h;
58 204929 const int field_pic = picture_structure != PICT_FRAME;
59
2/2
✓ Branch 0 taken 13112 times.
✓ Branch 1 taken 191817 times.
204929 if (field_pic) {
60 13112 h <<= 1;
61 13112 y <<= 1;
62 }
63
64 204929 h = FFMIN(h, avctx->height - y);
65
66
4/4
✓ Branch 0 taken 13112 times.
✓ Branch 1 taken 191817 times.
✓ Branch 2 taken 6570 times.
✓ Branch 3 taken 6542 times.
204929 if (field_pic && first_field &&
67
1/2
✓ Branch 0 taken 6570 times.
✗ Branch 1 not taken.
6570 !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
68 6570 return;
69
70
2/2
✓ Branch 0 taken 3350 times.
✓ Branch 1 taken 195009 times.
198359 if (avctx->draw_horiz_band) {
71 AVFrame *src;
72 int offset[AV_NUM_DATA_POINTERS];
73 int i;
74
75
2/4
✓ Branch 0 taken 3350 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3350 times.
3350 if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay ||
76 (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
77 3350 src = cur;
78 else if (last)
79 src = last;
80 else
81 return;
82
83
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3350 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3350 if (cur->pict_type == AV_PICTURE_TYPE_B &&
84 picture_structure == PICT_FRAME &&
85 avctx->codec_id != AV_CODEC_ID_SVQ3) {
86 for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
87 offset[i] = 0;
88 } else {
89 3350 offset[0]= y * src->linesize[0];
90 3350 offset[1]=
91 3350 offset[2]= (y >> vshift) * src->linesize[1];
92
2/2
✓ Branch 0 taken 16750 times.
✓ Branch 1 taken 3350 times.
20100 for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
93 16750 offset[i] = 0;
94 }
95
96 3350 emms_c();
97
98 3350 avctx->draw_horiz_band(avctx, src, offset,
99 y, picture_structure, h);
100 }
101 }
102
103 32803 void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
104 uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
105 int *low_delay,
106 int mb_width, int mb_height, int mb_stride, int quarter_sample)
107 {
108
4/6
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 32738 times.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 65 times.
✗ Branch 5 not taken.
32803 if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) {
109 65 const int shift = 1 + quarter_sample;
110 65 const int scale = 1 << shift;
111
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 65 times.
65 const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
112 65 const int mv_stride = (mb_width << mv_sample_log2) +
113 65 (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
114 65 int mb_x, mb_y, mbcount = 0;
115
116 /* size is width * height * 2 * 4 where 2 is for directions and 4 is
117 * for the maximum number of MB (4 MB in case of IS_8x8) */
118 65 AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (!mvs)
120 return;
121
122
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 65 times.
1355 for (mb_y = 0; mb_y < mb_height; mb_y++) {
123
2/2
✓ Branch 0 taken 45180 times.
✓ Branch 1 taken 1290 times.
46470 for (mb_x = 0; mb_x < mb_width; mb_x++) {
124 45180 int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
125
2/2
✓ Branch 0 taken 90360 times.
✓ Branch 1 taken 45180 times.
135540 for (direction = 0; direction < 2; direction++) {
126
2/2
✓ Branch 0 taken 87682 times.
✓ Branch 1 taken 2678 times.
90360 if (!USES_LIST(mb_type, direction))
127 87682 continue;
128
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 2399 times.
2678 if (IS_8X8(mb_type)) {
129
2/2
✓ Branch 0 taken 1116 times.
✓ Branch 1 taken 279 times.
1395 for (i = 0; i < 4; i++) {
130 1116 int sx = mb_x * 16 + 4 + 8 * (i & 1);
131 1116 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
132 1116 int xy = (mb_x * 2 + (i & 1) +
133 1116 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
134 1116 int mx = motion_val[direction][xy][0];
135 1116 int my = motion_val[direction][xy][1];
136 1116 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
137 }
138
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 2369 times.
2399 } else if (IS_16X8(mb_type)) {
139
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (i = 0; i < 2; i++) {
140 60 int sx = mb_x * 16 + 8;
141 60 int sy = mb_y * 16 + 4 + 8 * i;
142 60 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
143 60 int mx = motion_val[direction][xy][0];
144 60 int my = motion_val[direction][xy][1];
145
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (IS_INTERLACED(mb_type))
147 my *= 2;
148
149 60 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
150 }
151
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2354 times.
2369 } else if (IS_8X16(mb_type)) {
152
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 15 times.
45 for (i = 0; i < 2; i++) {
153 30 int sx = mb_x * 16 + 4 + 8 * i;
154 30 int sy = mb_y * 16 + 8;
155 30 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
156 30 int mx = motion_val[direction][xy][0];
157 30 int my = motion_val[direction][xy][1];
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (IS_INTERLACED(mb_type))
160 my *= 2;
161
162 30 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
163 }
164 } else {
165 2354 int sx = mb_x * 16 + 8;
166 2354 int sy = mb_y * 16 + 8;
167 2354 int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
168 2354 int mx = motion_val[direction][xy][0];
169 2354 int my = motion_val[direction][xy][1];
170 2354 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
171 }
172 }
173 }
174 }
175
176
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 34 times.
65 if (mbcount) {
177 AVFrameSideData *sd;
178
179 31 av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
180 31 sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!sd) {
182 av_freep(&mvs);
183 return;
184 }
185 31 memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
186 }
187
188 65 av_freep(&mvs);
189 }
190
191 /* TODO: export all the following to make them accessible for users (and filters) */
192
2/4
✓ Branch 0 taken 32803 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32803 times.
32803 if (avctx->hwaccel || !mbtype_table)
193 return;
194
195
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32803 times.
32803 if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
197 int x,y;
198
199 av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
200 av_get_picture_type_char(pict->pict_type));
201 for (y = 0; y < mb_height; y++) {
202 for (x = 0; x < mb_width; x++) {
203 if (avctx->debug & FF_DEBUG_SKIP) {
204 int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
205 if (count > 9)
206 count = 9;
207 av_log(avctx, AV_LOG_DEBUG, "%1d", count);
208 }
209 if (avctx->debug & FF_DEBUG_QP) {
210 av_log(avctx, AV_LOG_DEBUG, "%2d",
211 qscale_table[x + y * mb_stride]);
212 }
213 if (avctx->debug & FF_DEBUG_MB_TYPE) {
214 int mb_type = mbtype_table[x + y * mb_stride];
215 // Type & MV direction
216 if (IS_PCM(mb_type))
217 av_log(avctx, AV_LOG_DEBUG, "P");
218 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
219 av_log(avctx, AV_LOG_DEBUG, "A");
220 else if (IS_INTRA4x4(mb_type))
221 av_log(avctx, AV_LOG_DEBUG, "i");
222 else if (IS_INTRA16x16(mb_type))
223 av_log(avctx, AV_LOG_DEBUG, "I");
224 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
225 av_log(avctx, AV_LOG_DEBUG, "d");
226 else if (IS_DIRECT(mb_type))
227 av_log(avctx, AV_LOG_DEBUG, "D");
228 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
229 av_log(avctx, AV_LOG_DEBUG, "g");
230 else if (IS_GMC(mb_type))
231 av_log(avctx, AV_LOG_DEBUG, "G");
232 else if (IS_SKIP(mb_type))
233 av_log(avctx, AV_LOG_DEBUG, "S");
234 else if (!USES_LIST(mb_type, 1))
235 av_log(avctx, AV_LOG_DEBUG, ">");
236 else if (!USES_LIST(mb_type, 0))
237 av_log(avctx, AV_LOG_DEBUG, "<");
238 else {
239 av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
240 av_log(avctx, AV_LOG_DEBUG, "X");
241 }
242
243 // segmentation
244 if (IS_8X8(mb_type))
245 av_log(avctx, AV_LOG_DEBUG, "+");
246 else if (IS_16X8(mb_type))
247 av_log(avctx, AV_LOG_DEBUG, "-");
248 else if (IS_8X16(mb_type))
249 av_log(avctx, AV_LOG_DEBUG, "|");
250 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
251 av_log(avctx, AV_LOG_DEBUG, " ");
252 else
253 av_log(avctx, AV_LOG_DEBUG, "?");
254
255
256 if (IS_INTERLACED(mb_type))
257 av_log(avctx, AV_LOG_DEBUG, "=");
258 else
259 av_log(avctx, AV_LOG_DEBUG, " ");
260 }
261 }
262 av_log(avctx, AV_LOG_DEBUG, "\n");
263 }
264 }
265 }
266