FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegutils.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 89 184 48.4%
Functions: 3 6 50.0%
Branches: 54 152 35.5%

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