FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_codecview.c
Date: 2026-05-05 22:00:13
Exec Total Coverage
Lines: 94 156 60.3%
Functions: 5 6 83.3%
Branches: 57 142 40.1%

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/mem.h"
33 #include "libavutil/motion_vector.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/video_enc_params.h"
37 #include "avfilter.h"
38 #include "filters.h"
39 #include "qp_table.h"
40 #include "video.h"
41
42 #define MV_P_FOR (1<<0)
43 #define MV_B_FOR (1<<1)
44 #define MV_B_BACK (1<<2)
45 #define MV_TYPE_FOR (1<<0)
46 #define MV_TYPE_BACK (1<<1)
47 #define FRAME_TYPE_I (1<<0)
48 #define FRAME_TYPE_P (1<<1)
49 #define FRAME_TYPE_B (1<<2)
50
51 typedef struct CodecViewContext {
52 const AVClass *class;
53 unsigned mv;
54 unsigned frame_type;
55 unsigned mv_type;
56 int hsub, vsub;
57 int qp;
58 int block;
59 } CodecViewContext;
60
61 #define OFFSET(x) offsetof(CodecViewContext, x)
62 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
63 #define CONST(name, help, val, u) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, .unit = u }
64
65 static const AVOption codecview_options[] = {
66 { "mv", "set motion vectors to visualize", OFFSET(mv), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, .unit = "mv" },
67 CONST("pf", "forward predicted MVs of P-frames", MV_P_FOR, "mv"),
68 CONST("bf", "forward predicted MVs of B-frames", MV_B_FOR, "mv"),
69 CONST("bb", "backward predicted MVs of B-frames", MV_B_BACK, "mv"),
70 { "qp", NULL, OFFSET(qp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
71 { "mv_type", "set motion vectors type", OFFSET(mv_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, .unit = "mv_type" },
72 { "mvt", "set motion vectors type", OFFSET(mv_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, .unit = "mv_type" },
73 CONST("fp", "forward predicted MVs", MV_TYPE_FOR, "mv_type"),
74 CONST("bp", "backward predicted MVs", MV_TYPE_BACK, "mv_type"),
75 { "frame_type", "set frame types to visualize motion vectors of", OFFSET(frame_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, .unit = "frame_type" },
76 { "ft", "set frame types to visualize motion vectors of", OFFSET(frame_type), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, .unit = "frame_type" },
77 CONST("if", "I-frames", FRAME_TYPE_I, "frame_type"),
78 CONST("pf", "P-frames", FRAME_TYPE_P, "frame_type"),
79 CONST("bf", "B-frames", FRAME_TYPE_B, "frame_type"),
80 { "block", "set block partitioning structure to visualize", OFFSET(block), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
81 { NULL }
82 };
83
84 AVFILTER_DEFINE_CLASS(codecview);
85
86 19937 static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
87 {
88
2/2
✓ Branch 0 taken 5513 times.
✓ Branch 1 taken 14424 times.
19937 if(*sx > *ex)
89 5513 return clip_line(ex, ey, sx, sy, maxx);
90
91
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 14415 times.
14424 if (*sx < 0) {
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (*ex < 0)
93 return 1;
94 9 *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
95 9 *sx = 0;
96 }
97
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14424 times.
14424 if (*ex > maxx) {
99 if (*sx > maxx)
100 return 1;
101 *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
102 *ex = maxx;
103 }
104 14424 return 0;
105 }
106
107 /**
108 * Draw a line from (ex, ey) -> (sx, sy).
109 * @param w width of the image
110 * @param h height of the image
111 * @param stride stride/linesize of the image
112 * @param color color of the arrow
113 */
114 7212 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
115 int w, int h, ptrdiff_t stride, int color)
116 {
117 int x, y, fr, f;
118
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7212 times.
7212 if (clip_line(&sx, &sy, &ex, &ey, w - 1))
120 return;
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7212 times.
7212 if (clip_line(&sy, &sx, &ey, &ex, h - 1))
122 return;
123
124 7212 sx = av_clip(sx, 0, w - 1);
125 7212 sy = av_clip(sy, 0, h - 1);
126 7212 ex = av_clip(ex, 0, w - 1);
127 7212 ey = av_clip(ey, 0, h - 1);
128
129 7212 buf[sy * stride + sx] += color;
130
131
2/2
✓ Branch 0 taken 1356 times.
✓ Branch 1 taken 5856 times.
7212 if (FFABS(ex - sx) > FFABS(ey - sy)) {
132
2/2
✓ Branch 0 taken 977 times.
✓ Branch 1 taken 379 times.
1356 if (sx > ex) {
133 977 FFSWAP(int, sx, ex);
134 977 FFSWAP(int, sy, ey);
135 }
136 1356 buf += sx + sy * stride;
137 1356 ex -= sx;
138 1356 f = ((ey - sy) * (1 << 16)) / ex;
139
2/2
✓ Branch 0 taken 18611 times.
✓ Branch 1 taken 1356 times.
19967 for (x = 0; x <= ex; x++) {
140 18611 y = (x * f) >> 16;
141 18611 fr = (x * f) & 0xFFFF;
142 18611 buf[ y * stride + x] += (color * (0x10000 - fr)) >> 16;
143
2/2
✓ Branch 0 taken 12688 times.
✓ Branch 1 taken 5923 times.
18611 if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
144 }
145 } else {
146
2/2
✓ Branch 0 taken 2264 times.
✓ Branch 1 taken 3592 times.
5856 if (sy > ey) {
147 2264 FFSWAP(int, sx, ex);
148 2264 FFSWAP(int, sy, ey);
149 }
150 5856 buf += sx + sy * stride;
151 5856 ey -= sy;
152
2/2
✓ Branch 0 taken 2856 times.
✓ Branch 1 taken 3000 times.
5856 if (ey)
153 2856 f = ((ex - sx) * (1 << 16)) / ey;
154 else
155 3000 f = 0;
156
2/2
✓ Branch 0 taken 16136 times.
✓ Branch 1 taken 5856 times.
21992 for(y= 0; y <= ey; y++){
157 16136 x = (y*f) >> 16;
158 16136 fr = (y*f) & 0xFFFF;
159 16136 buf[y * stride + x ] += (color * (0x10000 - fr)) >> 16;
160
2/2
✓ Branch 0 taken 6415 times.
✓ Branch 1 taken 9721 times.
16136 if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
161 }
162 }
163 }
164
165 /**
166 * Draw an arrow from (ex, ey) -> (sx, sy).
167 * @param w width of the image
168 * @param h height of the image
169 * @param stride stride/linesize of the image
170 * @param color color of the arrow
171 */
172 4442 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
173 int ey, int w, int h, ptrdiff_t stride, int color, int tail, int direction)
174 {
175 int dx,dy;
176
177
2/2
✓ Branch 0 taken 1477 times.
✓ Branch 1 taken 2965 times.
4442 if (direction) {
178 1477 FFSWAP(int, sx, ex);
179 1477 FFSWAP(int, sy, ey);
180 }
181
182 4442 sx = av_clip(sx, -100, w + 100);
183 4442 sy = av_clip(sy, -100, h + 100);
184 4442 ex = av_clip(ex, -100, w + 100);
185 4442 ey = av_clip(ey, -100, h + 100);
186
187 4442 dx = ex - sx;
188 4442 dy = ey - sy;
189
190
2/2
✓ Branch 0 taken 1385 times.
✓ Branch 1 taken 3057 times.
4442 if (dx * dx + dy * dy > 3 * 3) {
191 1385 int rx = dx + dy;
192 1385 int ry = -dx + dy;
193 1385 int length = sqrt((rx * rx + ry * ry) << 8);
194
195 // FIXME subpixel accuracy
196
2/2
✓ Branch 0 taken 315 times.
✓ Branch 1 taken 1070 times.
1385 rx = ROUNDED_DIV(rx * (3 << 4), length);
197
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 1001 times.
1385 ry = ROUNDED_DIV(ry * (3 << 4), length);
198
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1385 times.
1385 if (tail) {
200 rx = -rx;
201 ry = -ry;
202 }
203
204 1385 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
205 1385 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
206 }
207 4442 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
208 4442 }
209
210 static void draw_block_rectangle(uint8_t *buf, int sx, int sy, int w, int h, ptrdiff_t stride, int color)
211 {
212 for (int x = sx; x < sx + w; x++)
213 buf[x] = color;
214
215 for (int y = sy; y < sy + h; y++) {
216 buf[sx] = color;
217 buf[sx + w - 1] = color;
218 buf += stride;
219 }
220 }
221
222 67 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
223 {
224 67 AVFilterContext *ctx = inlink->dst;
225 67 CodecViewContext *s = ctx->priv;
226 67 AVFilterLink *outlink = ctx->outputs[0];
227
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (s->qp) {
229 enum AVVideoEncParamsType qp_type;
230 int qstride, ret;
231 int8_t *qp_table;
232
233 ret = ff_qp_table_extract(frame, &qp_table, &qstride, NULL, &qp_type);
234 if (ret < 0) {
235 av_frame_free(&frame);
236 return ret;
237 }
238
239 if (qp_table) {
240 int x, y;
241 const int w = AV_CEIL_RSHIFT(frame->width, s->hsub);
242 const int h = AV_CEIL_RSHIFT(frame->height, s->vsub);
243 uint8_t *pu = frame->data[1];
244 uint8_t *pv = frame->data[2];
245 const ptrdiff_t lzu = frame->linesize[1];
246 const ptrdiff_t lzv = frame->linesize[2];
247
248 for (y = 0; y < h; y++) {
249 for (x = 0; x < w; x++) {
250 const int qp = ff_norm_qscale(qp_table[(y >> 3) * qstride + (x >> 3)], qp_type) * 128/31;
251 pu[x] = pv[x] = qp;
252 }
253 pu += lzu;
254 pv += lzv;
255 }
256 }
257 av_freep(&qp_table);
258 }
259
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (s->block) {
261 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
262 if (sd) {
263 AVVideoEncParams *par = (AVVideoEncParams*)sd->data;
264 const ptrdiff_t stride = frame->linesize[0];
265
266 if (par->nb_blocks) {
267 for (int block_idx = 0; block_idx < par->nb_blocks; block_idx++) {
268 AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
269
270 int64_t x0 = b->src_x;
271 int64_t y0 = b->src_y;
272 int64_t x1 = x0 + b->w;
273 int64_t y1 = y0 + b->h;
274
275 x0 = FFMAX(x0, 0);
276 y0 = FFMAX(y0, 0);
277 x1 = FFMIN(x1, frame->width);
278 y1 = FFMIN(y1, frame->height);
279
280 if (x1 <= x0 || y1 <= y0)
281 continue;
282
283 uint8_t *buf = frame->data[0] + y0 * stride;
284 draw_block_rectangle(buf, x0, y0, x1-x0, y1-y0, stride, 100);
285 }
286 }
287 }
288 }
289
290
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) {
291 67 AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
292
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 34 times.
67 if (sd) {
293 int i;
294 33 const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
295
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 const int is_iframe = (s->frame_type & FRAME_TYPE_I) && frame->pict_type == AV_PICTURE_TYPE_I;
296
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 const int is_pframe = (s->frame_type & FRAME_TYPE_P) && frame->pict_type == AV_PICTURE_TYPE_P;
297
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
33 const int is_bframe = (s->frame_type & FRAME_TYPE_B) && frame->pict_type == AV_PICTURE_TYPE_B;
298
299
2/2
✓ Branch 0 taken 4442 times.
✓ Branch 1 taken 33 times.
4475 for (i = 0; i < sd->size / sizeof(*mvs); i++) {
300 4442 const AVMotionVector *mv = &mvs[i];
301 4442 const int direction = mv->source > 0;
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4442 times.
4442 if (s->mv_type) {
304 const int is_fp = direction == 0 && (s->mv_type & MV_TYPE_FOR);
305 const int is_bp = direction == 1 && (s->mv_type & MV_TYPE_BACK);
306
307 if ((!s->frame_type && (is_fp || is_bp)) ||
308 is_iframe && is_fp || is_iframe && is_bp ||
309 is_pframe && is_fp ||
310 is_bframe && is_fp || is_bframe && is_bp)
311 draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
312 frame->width, frame->height, frame->linesize[0],
313 100, 0, direction);
314
1/2
✓ Branch 0 taken 4442 times.
✗ Branch 1 not taken.
4442 } else if (s->mv)
315
7/8
✓ Branch 0 taken 2965 times.
✓ Branch 1 taken 1477 times.
✓ Branch 2 taken 2965 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1037 times.
✓ Branch 5 taken 1928 times.
✓ Branch 6 taken 1037 times.
✓ Branch 7 taken 1477 times.
4442 if ((direction == 0 && (s->mv & MV_P_FOR) && frame->pict_type == AV_PICTURE_TYPE_P) ||
316
3/6
✓ Branch 0 taken 1037 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1037 times.
✓ Branch 4 taken 1477 times.
✗ Branch 5 not taken.
2514 (direction == 0 && (s->mv & MV_B_FOR) && frame->pict_type == AV_PICTURE_TYPE_B) ||
317
2/4
✓ Branch 0 taken 1477 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1477 times.
✗ Branch 3 not taken.
1477 (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
318 4442 draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
319 4442 frame->width, frame->height, frame->linesize[0],
320 100, 0, direction);
321 }
322 }
323 }
324
325 67 return ff_filter_frame(outlink, frame);
326 }
327
328 2 static int config_input(AVFilterLink *inlink)
329 {
330 2 AVFilterContext *ctx = inlink->dst;
331 2 CodecViewContext *s = ctx->priv;
332 2 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
333
334 2 s->hsub = desc->log2_chroma_w;
335 2 s->vsub = desc->log2_chroma_h;
336 2 return 0;
337 }
338
339 static const AVFilterPad codecview_inputs[] = {
340 {
341 .name = "default",
342 .type = AVMEDIA_TYPE_VIDEO,
343 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
344 .filter_frame = filter_frame,
345 .config_props = config_input,
346 },
347 };
348
349 const FFFilter ff_vf_codecview = {
350 .p.name = "codecview",
351 .p.description = NULL_IF_CONFIG_SMALL("Visualize information about some codecs."),
352 .p.priv_class = &codecview_class,
353 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
354 .priv_size = sizeof(CodecViewContext),
355 FILTER_INPUTS(codecview_inputs),
356 FILTER_OUTPUTS(ff_video_default_filterpad),
357 // TODO: we can probably add way more pixel formats without any other
358 // changes; anything with 8-bit luma in first plane should be working
359 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_YUV420P),
360 };
361