../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
88 / 172
51.2%
Branches:
56 / 136
41.2%
Functions:
3 / 6
50.0%

libavcodec/mpegutils.c
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/bprint.h"
24 #include "libavutil/common.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/motion_vector.h"
29 #include "libavutil/avassert.h"
30
31 #include "avcodec.h"
32 #include "mpegutils.h"
33
34 5113 static int add_mb(AVMotionVector *mb, uint32_t mb_type,
35 int dst_x, int dst_y,
36 int motion_x, int motion_y, int motion_scale,
37 int direction)
38 {
39
4/4
✓ Branch 2 → 3 taken 3357 times.
✓ Branch 2 → 4 taken 1756 times.
✓ Branch 3 → 4 taken 48 times.
✓ Branch 3 → 5 taken 3309 times.
5113 mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
40
4/4
✓ Branch 6 → 7 taken 3357 times.
✓ Branch 6 → 8 taken 1756 times.
✓ Branch 7 → 8 taken 86 times.
✓ Branch 7 → 9 taken 3271 times.
5113 mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
41 5113 mb->motion_x = motion_x;
42 5113 mb->motion_y = motion_y;
43 5113 mb->motion_scale = motion_scale;
44 5113 mb->dst_x = dst_x;
45 5113 mb->dst_y = dst_y;
46 5113 mb->src_x = dst_x + motion_x / motion_scale;
47 5113 mb->src_y = dst_y + motion_y / motion_scale;
48
2/2
✓ Branch 10 → 11 taken 1473 times.
✓ Branch 10 → 12 taken 3640 times.
5113 mb->source = direction ? 1 : -1;
49 5113 mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
50 5113 return 1;
51 }
52
53 229371 void ff_draw_horiz_band(AVCodecContext *avctx,
54 const AVFrame *cur, const AVFrame *last,
55 int y, int h, int picture_structure,
56 int first_field, int low_delay)
57 {
58 229371 const int field_pic = picture_structure != PICT_FRAME;
59 const AVPixFmtDescriptor *desc;
60 const AVFrame *src;
61 int offset[AV_NUM_DATA_POINTERS];
62
63
2/2
✓ Branch 2 → 3 taken 226021 times.
✓ Branch 2 → 4 taken 3350 times.
229371 if (!avctx->draw_horiz_band)
64 226021 return;
65
66
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 3350 times.
3350 if (field_pic) {
67 ✗ h <<= 1;
68 ✗ y <<= 1;
69 }
70
71 3350 h = FFMIN(h, avctx->height - y);
72
73
1/4
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 3350 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
3350 if (field_pic && first_field &&
74
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
75 ✗ return;
76
77
2/4
✓ Branch 10 → 11 taken 3350 times.
✗ Branch 10 → 13 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 3350 times.
3350 if (cur->pict_type == AV_PICTURE_TYPE_B || low_delay ||
78
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
79 3350 src = cur;
80
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ else if (last)
81 ✗ src = last;
82 else
83 ✗ return;
84
85 3350 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
86
87 3350 offset[0] = y * src->linesize[0];
88 3350 offset[1] =
89 3350 offset[2] = (y >> desc->log2_chroma_h) * src->linesize[1];
90
2/2
✓ Branch 20 → 19 taken 16750 times.
✓ Branch 20 → 21 taken 3350 times.
20100 for (int i = 3; i < AV_NUM_DATA_POINTERS; i++)
91 16750 offset[i] = 0;
92
93 3350 avctx->draw_horiz_band(avctx, src, offset,
94 y, picture_structure, h);
95 }
96
97 #define HAS_MV_EXT(mb_type, flags, dir) ((mb_type) & flags[(dir)])
98
99 ✗ static char get_type_mv_char(int mb_type, const int mb_type_mv_flags[2])
100 {
101 // Type & MV direction
102
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (IS_PCM(mb_type))
103 ✗ return 'P';
104
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ else if (IS_ACPRED(mb_type))
105 ✗ return 'A';
106
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ else if (IS_INTRA4x4(mb_type))
107 ✗ return 'i';
108
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ else if (IS_INTRA16x16(mb_type))
109 ✗ return 'I';
110
0/4
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 13 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
111 ✗ return 'd';
112
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ else if (IS_DIRECT(mb_type))
113 ✗ return 'D';
114
0/4
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
115 ✗ return 'g';
116
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ else if (IS_GMC(mb_type))
117 ✗ return 'G';
118
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ else if (IS_SKIP(mb_type))
119 ✗ return 'S';
120
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ else if (!HAS_MV_EXT(mb_type, 1, mb_type_mv_flags))
121 ✗ return '>';
122
0/2
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ else if (!HAS_MV_EXT(mb_type, 0, mb_type_mv_flags))
123 ✗ return '<';
124 else {
125 av_assert2(HAS_MV_EXT(mb_type, 0, mb_type_mv_flags) && HAS_MV_EXT(mb_type, 1, mb_type_mv_flags));
126 ✗ return 'X';
127 }
128 }
129
130 ✗ static char get_segmentation_char(int mb_type)
131 {
132
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (IS_8X8(mb_type))
133 ✗ return '+';
134
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ else if (IS_16X8(mb_type))
135 ✗ return '-';
136
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ else if (IS_8X16(mb_type))
137 ✗ return '|';
138
0/4
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
139 ✗ return ' ';
140
141 ✗ return '?';
142 }
143
144 ✗ static char get_interlacement_char(int mb_type)
145 {
146
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (IS_INTERLACED(mb_type))
147 ✗ return '=';
148 else
149 ✗ return ' ';
150 }
151
152 38391 void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict,
153 const uint32_t *mbtype_table,
154 const int8_t *qscale_table, int16_t (*const motion_val[2])[2],
155 int mb_width, int mb_height, int mb_stride, int quarter_sample)
156 {
157 38391 const int is_h264 = avctx->codec_id == AV_CODEC_ID_H264;
158
2/2
✓ Branch 2 → 3 taken 25274 times.
✓ Branch 2 → 4 taken 13117 times.
38391 const int mb_type_mv_flags[2] = { is_h264 ? MB_TYPE_L0 : MB_TYPE_FORWARD_MV,
159
2/2
✓ Branch 5 → 6 taken 25274 times.
✓ Branch 5 → 7 taken 13117 times.
38391 is_h264 ? MB_TYPE_L1 : MB_TYPE_BACKWARD_MV };
160
161
4/6
✓ Branch 8 → 9 taken 69 times.
✓ Branch 8 → 59 taken 38322 times.
✓ Branch 9 → 10 taken 69 times.
✗ Branch 9 → 59 not taken.
✓ Branch 10 → 11 taken 69 times.
✗ Branch 10 → 59 not taken.
38391 if ((avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS) && mbtype_table && motion_val[0]) {
162 69 const int shift = 1 + quarter_sample;
163 69 const int scale = 1 << shift;
164
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 69 times.
69 const int mv_sample_log2 = is_h264 ? 2 : 1;
165 69 const int mv_stride = (mb_width << mv_sample_log2) + !is_h264;
166 69 int mb_x, mb_y, mbcount = 0;
167
168 /* size is width * height * 2 * 4 where 2 is for directions and 4 is
169 * for the maximum number of MB (4 MB in case of IS_8x8) */
170 69 AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
171
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 69 times.
69 if (!mvs)
172 ✗ return;
173
174
2/2
✓ Branch 48 → 18 taken 1366 times.
✓ Branch 48 → 49 taken 69 times.
1435 for (mb_y = 0; mb_y < mb_height; mb_y++) {
175
2/2
✓ Branch 46 → 19 taken 47412 times.
✓ Branch 46 → 47 taken 1366 times.
48778 for (mb_x = 0; mb_x < mb_width; mb_x++) {
176 47412 int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
177
2/2
✓ Branch 44 → 20 taken 94824 times.
✓ Branch 44 → 45 taken 47412 times.
142236 for (direction = 0; direction < 2; direction++) {
178
2/2
✓ Branch 20 → 21 taken 91095 times.
✓ Branch 20 → 22 taken 3729 times.
94824 if (!HAS_MV_EXT(mb_type, direction, mb_type_mv_flags))
179 91095 continue;
180
2/2
✓ Branch 22 → 23 taken 439 times.
✓ Branch 22 → 27 taken 3290 times.
3729 if (IS_8X8(mb_type)) {
181
2/2
✓ Branch 26 → 24 taken 1756 times.
✓ Branch 26 → 43 taken 439 times.
2195 for (i = 0; i < 4; i++) {
182 1756 int sx = mb_x * 16 + 4 + 8 * (i & 1);
183 1756 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
184 1756 int xy = (mb_x * 2 + (i & 1) +
185 1756 (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
186 1756 int mx = motion_val[direction][xy][0];
187 1756 int my = motion_val[direction][xy][1];
188 1756 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
189 }
190
2/2
✓ Branch 27 → 28 taken 43 times.
✓ Branch 27 → 34 taken 3247 times.
3290 } else if (IS_16X8(mb_type)) {
191
2/2
✓ Branch 33 → 29 taken 86 times.
✓ Branch 33 → 43 taken 43 times.
129 for (i = 0; i < 2; i++) {
192 86 int sx = mb_x * 16 + 8;
193 86 int sy = mb_y * 16 + 4 + 8 * i;
194 86 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
195 86 int mx = motion_val[direction][xy][0];
196 86 int my = motion_val[direction][xy][1];
197
198
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 86 times.
86 if (IS_INTERLACED(mb_type))
199 ✗ my *= 2;
200
201 86 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
202 }
203
2/2
✓ Branch 34 → 35 taken 24 times.
✓ Branch 34 → 41 taken 3223 times.
3247 } else if (IS_8X16(mb_type)) {
204
2/2
✓ Branch 40 → 36 taken 48 times.
✓ Branch 40 → 43 taken 24 times.
72 for (i = 0; i < 2; i++) {
205 48 int sx = mb_x * 16 + 4 + 8 * i;
206 48 int sy = mb_y * 16 + 8;
207 48 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
208 48 int mx = motion_val[direction][xy][0];
209 48 int my = motion_val[direction][xy][1];
210
211
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 48 times.
48 if (IS_INTERLACED(mb_type))
212 ✗ my *= 2;
213
214 48 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
215 }
216 } else {
217 3223 int sx = mb_x * 16 + 8;
218 3223 int sy = mb_y * 16 + 8;
219 3223 int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
220 3223 int mx = motion_val[direction][xy][0];
221 3223 int my = motion_val[direction][xy][1];
222 3223 mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, scale, direction);
223 }
224 }
225 }
226 }
227
228
2/2
✓ Branch 49 → 50 taken 35 times.
✓ Branch 49 → 56 taken 34 times.
69 if (mbcount) {
229 AVFrameSideData *sd;
230
231 35 av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %"PRId64"\n", mbcount, avctx->frame_num);
232 35 sd = av_frame_new_side_data(pict, AV_FRAME_DATA_MOTION_VECTORS, mbcount * sizeof(AVMotionVector));
233
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 55 taken 35 times.
35 if (!sd) {
234 ✗ av_freep(&mvs);
235 ✗ return;
236 }
237 35 memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
238 }
239
240 69 av_freep(&mvs);
241 }
242
243 /* TODO: export all the following to make them accessible for users (and filters) */
244
2/4
✓ Branch 59 → 60 taken 38391 times.
✗ Branch 59 → 61 not taken.
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 62 taken 38391 times.
38391 if (avctx->hwaccel || !mbtype_table)
245 ✗ return;
246
247
248
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 101 taken 38391 times.
38391 if (avctx->debug & (FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
249 int x,y;
250 AVBPrint buf;
251 int n;
252 int margin_left;
253 int x_step;
254
255 ✗ av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
256 ✗ av_get_picture_type_char(pict->pict_type));
257
258 ✗ margin_left = 2;
259 ✗ n = mb_width << 4;
260
0/2
✗ Branch 67 → 66 not taken.
✗ Branch 67 → 68 not taken.
✗ while ((n /= 10))
261 ✗ margin_left++;
262
263 ✗ av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
264 ✗ av_bprint_chars(&buf, ' ', margin_left);
265
266 ✗ n = 0;
267
0/2
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
✗ if (avctx->debug & FF_DEBUG_QP)
268 ✗ n += 2;
269
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
✗ if (avctx->debug & FF_DEBUG_MB_TYPE)
270 ✗ n += 3;
271
0/2
✗ Branch 74 → 75 not taken.
✗ Branch 74 → 76 not taken.
✗ x_step = (mb_width * 16 > 999) ? 8 : 4;
272
0/2
✗ Branch 80 → 78 not taken.
✗ Branch 80 → 81 not taken.
✗ for (x = 0; x < mb_width; x += x_step)
273 ✗ av_bprintf(&buf, "%-*d", n * x_step, x << 4);
274
275 ✗ av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str);
276
277
0/2
✗ Branch 98 → 83 not taken.
✗ Branch 98 → 99 not taken.
✗ for (y = 0; y < mb_height; y++) {
278 ✗ av_bprint_clear(&buf);
279
0/2
✗ Branch 95 → 85 not taken.
✗ Branch 95 → 96 not taken.
✗ for (x = 0; x < mb_width; x++) {
280
0/2
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 87 not taken.
✗ if (x == 0)
281 ✗ av_bprintf(&buf, "%*d ", margin_left - 1, y << 4);
282
0/2
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 89 not taken.
✗ if (avctx->debug & FF_DEBUG_QP) {
283 ✗ av_bprintf(&buf, "%2d", qscale_table[x + y * mb_stride]);
284 }
285
0/2
✗ Branch 89 → 90 not taken.
✗ Branch 89 → 94 not taken.
✗ if (avctx->debug & FF_DEBUG_MB_TYPE) {
286 ✗ int mb_type = mbtype_table[x + y * mb_stride];
287
288 ✗ av_bprintf(&buf, "%c%c%c",
289 ✗ get_type_mv_char(mb_type, mb_type_mv_flags),
290 ✗ get_segmentation_char(mb_type),
291 ✗ get_interlacement_char(mb_type));
292 }
293 }
294
295 ✗ av_log(avctx, AV_LOG_DEBUG, "%s\n", buf.str);
296 }
297 ✗ av_bprint_finalize(&buf, NULL);
298 }
299 }
300