FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_codecview.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 94 147 63.9%
Functions: 5 6 83.3%
Branches: 57 140 40.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2014 Clément Bœsch <u pkh me>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Codec debug viewer filter.
25 *
26 * All the MV drawing code from Michael Niedermayer is extracted from
27 * libavcodec/mpegvideo.c.
28 *
29 * TODO: segmentation
30 */
31
32 #include "libavutil/imgutils.h"
33 #include "libavutil/motion_vector.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/video_enc_params.h"
36 #include "avfilter.h"
37 #include "qp_table.h"
38 #include "internal.h"
39
40 #define MV_P_FOR (1<<0)
41 #define MV_B_FOR (1<<1)
42 #define MV_B_BACK (1<<2)
43 #define MV_TYPE_FOR (1<<0)
44 #define MV_TYPE_BACK (1<<1)
45 #define FRAME_TYPE_I (1<<0)
46 #define FRAME_TYPE_P (1<<1)
47 #define FRAME_TYPE_B (1<<2)
48
49 typedef struct CodecViewContext {
50 const AVClass *class;
51 unsigned mv;
52 unsigned frame_type;
53 unsigned mv_type;
54 int hsub, vsub;
55 int qp;
56 int block;
57 } CodecViewContext;
58
59 #define OFFSET(x) offsetof(CodecViewContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61 #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
62
63 static const AVOption codecview_options[] = {
64 { "mv", "set motion vectors to visualize", OFFSET(mv), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv" },
65 CONST("pf", "forward predicted MVs of P-frames", MV_P_FOR, "mv"),
66 CONST("bf", "forward predicted MVs of B-frames", MV_B_FOR, "mv"),
67 CONST("bb", "backward predicted MVs of B-frames", MV_B_BACK, "mv"),
68 { "qp", NULL, OFFSET(qp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
69 { "mv_type", "set motion vectors type", OFFSET(mv_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv_type" },
70 { "mvt", "set motion vectors type", OFFSET(mv_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv_type" },
71 CONST("fp", "forward predicted MVs", MV_TYPE_FOR, "mv_type"),
72 CONST("bp", "backward predicted MVs", MV_TYPE_BACK, "mv_type"),
73 { "frame_type", "set frame types to visualize motion vectors of", OFFSET(frame_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "frame_type" },
74 { "ft", "set frame types to visualize motion vectors of", OFFSET(frame_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "frame_type" },
75 CONST("if", "I-frames", FRAME_TYPE_I, "frame_type"),
76 CONST("pf", "P-frames", FRAME_TYPE_P, "frame_type"),
77 CONST("bf", "B-frames", FRAME_TYPE_B, "frame_type"),
78 { "block", "set block partitioning structure to visualize", OFFSET(block), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
79 { NULL }
80 };
81
82 AVFILTER_DEFINE_CLASS(codecview);
83
84 17331 static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
85 {
86
2/2
✓ Branch 0 taken 5175 times.
✓ Branch 1 taken 12156 times.
17331 if(*sx > *ex)
87 5175 return clip_line(ex, ey, sx, sy, maxx);
88
89
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 12147 times.
12156 if (*sx < 0) {
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (*ex < 0)
91 return 1;
92 9 *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
93 9 *sx = 0;
94 }
95
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12156 times.
12156 if (*ex > maxx) {
97 if (*sx > maxx)
98 return 1;
99 *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
100 *ex = maxx;
101 }
102 12156 return 0;
103 }
104
105 /**
106 * Draw a line from (ex, ey) -> (sx, sy).
107 * @param w width of the image
108 * @param h height of the image
109 * @param stride stride/linesize of the image
110 * @param color color of the arrow
111 */
112 6078 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
113 int w, int h, int stride, int color)
114 {
115 int x, y, fr, f;
116
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6078 times.
6078 if (clip_line(&sx, &sy, &ex, &ey, w - 1))
118 return;
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6078 times.
6078 if (clip_line(&sy, &sx, &ey, &ex, h - 1))
120 return;
121
122 6078 sx = av_clip(sx, 0, w - 1);
123 6078 sy = av_clip(sy, 0, h - 1);
124 6078 ex = av_clip(ex, 0, w - 1);
125 6078 ey = av_clip(ey, 0, h - 1);
126
127 6078 buf[sy * stride + sx] += color;
128
129
2/2
✓ Branch 0 taken 1182 times.
✓ Branch 1 taken 4896 times.
6078 if (FFABS(ex - sx) > FFABS(ey - sy)) {
130
2/2
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 295 times.
1182 if (sx > ex) {
131 887 FFSWAP(int, sx, ex);
132 887 FFSWAP(int, sy, ey);
133 }
134 1182 buf += sx + sy * stride;
135 1182 ex -= sx;
136 1182 f = ((ey - sy) * (1 << 16)) / ex;
137
2/2
✓ Branch 0 taken 15761 times.
✓ Branch 1 taken 1182 times.
16943 for (x = 0; x <= ex; x++) {
138 15761 y = (x * f) >> 16;
139 15761 fr = (x * f) & 0xFFFF;
140 15761 buf[ y * stride + x] += (color * (0x10000 - fr)) >> 16;
141
2/2
✓ Branch 0 taken 10820 times.
✓ Branch 1 taken 4941 times.
15761 if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
142 }
143 } else {
144
2/2
✓ Branch 0 taken 2168 times.
✓ Branch 1 taken 2728 times.
4896 if (sy > ey) {
145 2168 FFSWAP(int, sx, ex);
146 2168 FFSWAP(int, sy, ey);
147 }
148 4896 buf += sx + sy * stride;
149 4896 ey -= sy;
150
2/2
✓ Branch 0 taken 2647 times.
✓ Branch 1 taken 2249 times.
4896 if (ey)
151 2647 f = ((ex - sx) * (1 << 16)) / ey;
152 else
153 2249 f = 0;
154
2/2
✓ Branch 0 taken 14680 times.
✓ Branch 1 taken 4896 times.
19576 for(y= 0; y <= ey; y++){
155 14680 x = (y*f) >> 16;
156 14680 fr = (y*f) & 0xFFFF;
157 14680 buf[y * stride + x ] += (color * (0x10000 - fr)) >> 16;
158
2/2
✓ Branch 0 taken 6286 times.
✓ Branch 1 taken 8394 times.
14680 if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
159 }
160 }
161 }
162
163 /**
164 * Draw an arrow from (ex, ey) -> (sx, sy).
165 * @param w width of the image
166 * @param h height of the image
167 * @param stride stride/linesize of the image
168 * @param color color of the arrow
169 */
170 3560 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
171 int ey, int w, int h, int stride, int color, int tail, int direction)
172 {
173 int dx,dy;
174
175
2/2
✓ Branch 0 taken 1164 times.
✓ Branch 1 taken 2396 times.
3560 if (direction) {
176 1164 FFSWAP(int, sx, ex);
177 1164 FFSWAP(int, sy, ey);
178 }
179
180 3560 sx = av_clip(sx, -100, w + 100);
181 3560 sy = av_clip(sy, -100, h + 100);
182 3560 ex = av_clip(ex, -100, w + 100);
183 3560 ey = av_clip(ey, -100, h + 100);
184
185 3560 dx = ex - sx;
186 3560 dy = ey - sy;
187
188
2/2
✓ Branch 0 taken 1259 times.
✓ Branch 1 taken 2301 times.
3560 if (dx * dx + dy * dy > 3 * 3) {
189 1259 int rx = dx + dy;
190 1259 int ry = -dx + dy;
191 1259 int length = sqrt((rx * rx + ry * ry) << 8);
192
193 // FIXME subpixel accuracy
194
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 1016 times.
1259 rx = ROUNDED_DIV(rx * (3 << 4), length);
195
2/2
✓ Branch 0 taken 325 times.
✓ Branch 1 taken 934 times.
1259 ry = ROUNDED_DIV(ry * (3 << 4), length);
196
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1259 times.
1259 if (tail) {
198 rx = -rx;
199 ry = -ry;
200 }
201
202 1259 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
203 1259 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
204 }
205 3560 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
206 3560 }
207
208 static void draw_block_rectangle(uint8_t *buf, int sx, int sy, int w, int h, int stride, int color)
209 {
210 for (int x = sx; x < sx + w; x++)
211 buf[x] = color;
212
213 for (int y = sy; y < sy + h; y++) {
214 buf[sx] = color;
215 buf[sx + w - 1] = color;
216 buf += stride;
217 }
218
219 for (int x = sx; x < sx + w; x++)
220 buf[x] = color;
221 }
222
223 67 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
224 {
225 67 AVFilterContext *ctx = inlink->dst;
226 67 CodecViewContext *s = ctx->priv;
227 67 AVFilterLink *outlink = ctx->outputs[0];
228
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (s->qp) {
230 enum AVVideoEncParamsType qp_type;
231 int qstride, ret;
232 int8_t *qp_table;
233
234 ret = ff_qp_table_extract(frame, &qp_table, &qstride, NULL, &qp_type);
235 if (ret < 0) {
236 av_frame_free(&frame);
237 return ret;
238 }
239
240 if (qp_table) {
241 int x, y;
242 const int w = AV_CEIL_RSHIFT(frame->width, s->hsub);
243 const int h = AV_CEIL_RSHIFT(frame->height, s->vsub);
244 uint8_t *pu = frame->data[1];
245 uint8_t *pv = frame->data[2];
246 const int lzu = frame->linesize[1];
247 const int lzv = frame->linesize[2];
248
249 for (y = 0; y < h; y++) {
250 for (x = 0; x < w; x++) {
251 const int qp = ff_norm_qscale(qp_table[(y >> 3) * qstride + (x >> 3)], qp_type) * 128/31;
252 pu[x] = pv[x] = qp;
253 }
254 pu += lzu;
255 pv += lzv;
256 }
257 }
258 av_freep(&qp_table);
259 }
260
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (s->block) {
262 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
263 if (sd) {
264 AVVideoEncParams *par = (AVVideoEncParams*)sd->data;
265 const int stride = frame->linesize[0];
266
267 if (par->nb_blocks) {
268 for (int block_idx = 0; block_idx < par->nb_blocks; block_idx++) {
269 AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
270 uint8_t *buf = frame->data[0] + b->src_y * stride;
271
272 draw_block_rectangle(buf, b->src_x, b->src_y, b->w, b->h, stride, 100);
273 }
274 }
275 }
276 }
277
278
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
67 if (s->mv || s->mv_type) {
279 67 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
280
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 36 times.
67 if (sd) {
281 int i;
282 31 const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
283
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31 const int is_iframe = (s->frame_type & FRAME_TYPE_I) && frame->pict_type == AV_PICTURE_TYPE_I;
284
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31 const int is_pframe = (s->frame_type & FRAME_TYPE_P) && frame->pict_type == AV_PICTURE_TYPE_P;
285
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
31 const int is_bframe = (s->frame_type & FRAME_TYPE_B) && frame->pict_type == AV_PICTURE_TYPE_B;
286
287
2/2
✓ Branch 0 taken 3560 times.
✓ Branch 1 taken 31 times.
3591 for (i = 0; i < sd->size / sizeof(*mvs); i++) {
288 3560 const AVMotionVector *mv = &mvs[i];
289 3560 const int direction = mv->source > 0;
290
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3560 times.
3560 if (s->mv_type) {
292 const int is_fp = direction == 0 && (s->mv_type & MV_TYPE_FOR);
293 const int is_bp = direction == 1 && (s->mv_type & MV_TYPE_BACK);
294
295 if ((!s->frame_type && (is_fp || is_bp)) ||
296 is_iframe && is_fp || is_iframe && is_bp ||
297 is_pframe && is_fp ||
298 is_bframe && is_fp || is_bframe && is_bp)
299 draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
300 frame->width, frame->height, frame->linesize[0],
301 100, 0, direction);
302
1/2
✓ Branch 0 taken 3560 times.
✗ Branch 1 not taken.
3560 } else if (s->mv)
303
7/8
✓ Branch 0 taken 2396 times.
✓ Branch 1 taken 1164 times.
✓ Branch 2 taken 2396 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 772 times.
✓ Branch 5 taken 1624 times.
✓ Branch 6 taken 772 times.
✓ Branch 7 taken 1164 times.
3560 if ((direction == 0 && (s->mv & MV_P_FOR) && frame->pict_type == AV_PICTURE_TYPE_P) ||
304
3/6
✓ Branch 0 taken 772 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 772 times.
✓ Branch 4 taken 1164 times.
✗ Branch 5 not taken.
1936 (direction == 0 && (s->mv & MV_B_FOR) && frame->pict_type == AV_PICTURE_TYPE_B) ||
305
2/4
✓ Branch 0 taken 1164 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1164 times.
✗ Branch 3 not taken.
1164 (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
306 3560 draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
307 3560 frame->width, frame->height, frame->linesize[0],
308 100, 0, direction);
309 }
310 }
311 }
312
313 67 return ff_filter_frame(outlink, frame);
314 }
315
316 2 static int config_input(AVFilterLink *inlink)
317 {
318 2 AVFilterContext *ctx = inlink->dst;
319 2 CodecViewContext *s = ctx->priv;
320 2 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
321
322 2 s->hsub = desc->log2_chroma_w;
323 2 s->vsub = desc->log2_chroma_h;
324 2 return 0;
325 }
326
327 static const AVFilterPad codecview_inputs[] = {
328 {
329 .name = "default",
330 .type = AVMEDIA_TYPE_VIDEO,
331 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
332 .filter_frame = filter_frame,
333 .config_props = config_input,
334 },
335 };
336
337 static const AVFilterPad codecview_outputs[] = {
338 {
339 .name = "default",
340 .type = AVMEDIA_TYPE_VIDEO,
341 },
342 };
343
344 const AVFilter ff_vf_codecview = {
345 .name = "codecview",
346 .description = NULL_IF_CONFIG_SMALL("Visualize information about some codecs."),
347 .priv_size = sizeof(CodecViewContext),
348 FILTER_INPUTS(codecview_inputs),
349 FILTER_OUTPUTS(codecview_outputs),
350 // TODO: we can probably add way more pixel formats without any other
351 // changes; anything with 8-bit luma in first plane should be working
352 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_YUV420P),
353 .priv_class = &codecview_class,
354 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
355 };
356