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