]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_codecview.c
lavfi: deprecate avfilter_link_set_closed().
[ffmpeg] / libavfilter / vf_codecview.c
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 "avfilter.h"
36 #include "internal.h"
37
38 #define MV_P_FOR  (1<<0)
39 #define MV_B_FOR  (1<<1)
40 #define MV_B_BACK (1<<2)
41
42 typedef struct {
43     const AVClass *class;
44     unsigned mv;
45     int hsub, vsub;
46     int qp;
47 } CodecViewContext;
48
49 #define OFFSET(x) offsetof(CodecViewContext, x)
50 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
51 static const AVOption codecview_options[] = {
52     { "mv", "set motion vectors to visualize", OFFSET(mv), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv" },
53         {"pf", "forward predicted MVs of P-frames",  0, AV_OPT_TYPE_CONST, {.i64 = MV_P_FOR },  INT_MIN, INT_MAX, FLAGS, "mv"},
54         {"bf", "forward predicted MVs of B-frames",  0, AV_OPT_TYPE_CONST, {.i64 = MV_B_FOR },  INT_MIN, INT_MAX, FLAGS, "mv"},
55         {"bb", "backward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_B_BACK }, INT_MIN, INT_MAX, FLAGS, "mv"},
56     { "qp", NULL, OFFSET(qp), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
57     { NULL }
58 };
59
60 AVFILTER_DEFINE_CLASS(codecview);
61
62 static int query_formats(AVFilterContext *ctx)
63 {
64     // TODO: we can probably add way more pixel formats without any other
65     // changes; anything with 8-bit luma in first plane should be working
66     static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE};
67     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
68     if (!fmts_list)
69         return AVERROR(ENOMEM);
70     return ff_set_common_formats(ctx, fmts_list);
71 }
72
73 static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
74 {
75     if(*sx > *ex)
76         return clip_line(ex, ey, sx, sy, maxx);
77
78     if (*sx < 0) {
79         if (*ex < 0)
80             return 1;
81         *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
82         *sx = 0;
83     }
84
85     if (*ex > maxx) {
86         if (*sx > maxx)
87             return 1;
88         *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
89         *ex = maxx;
90     }
91     return 0;
92 }
93
94 /**
95  * Draw a line from (ex, ey) -> (sx, sy).
96  * @param w width of the image
97  * @param h height of the image
98  * @param stride stride/linesize of the image
99  * @param color color of the arrow
100  */
101 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
102                       int w, int h, int stride, int color)
103 {
104     int x, y, fr, f;
105
106     if (clip_line(&sx, &sy, &ex, &ey, w - 1))
107         return;
108     if (clip_line(&sy, &sx, &ey, &ex, h - 1))
109         return;
110
111     sx = av_clip(sx, 0, w - 1);
112     sy = av_clip(sy, 0, h - 1);
113     ex = av_clip(ex, 0, w - 1);
114     ey = av_clip(ey, 0, h - 1);
115
116     buf[sy * stride + sx] += color;
117
118     if (FFABS(ex - sx) > FFABS(ey - sy)) {
119         if (sx > ex) {
120             FFSWAP(int, sx, ex);
121             FFSWAP(int, sy, ey);
122         }
123         buf += sx + sy * stride;
124         ex  -= sx;
125         f    = ((ey - sy) << 16) / ex;
126         for (x = 0; x <= ex; x++) {
127             y  = (x * f) >> 16;
128             fr = (x * f) & 0xFFFF;
129                    buf[ y      * stride + x] += (color * (0x10000 - fr)) >> 16;
130             if(fr) buf[(y + 1) * stride + x] += (color *            fr ) >> 16;
131         }
132     } else {
133         if (sy > ey) {
134             FFSWAP(int, sx, ex);
135             FFSWAP(int, sy, ey);
136         }
137         buf += sx + sy * stride;
138         ey  -= sy;
139         if (ey)
140             f = ((ex - sx) << 16) / ey;
141         else
142             f = 0;
143         for(y= 0; y <= ey; y++){
144             x  = (y*f) >> 16;
145             fr = (y*f) & 0xFFFF;
146                    buf[y * stride + x    ] += (color * (0x10000 - fr)) >> 16;
147             if(fr) buf[y * stride + x + 1] += (color *            fr ) >> 16;
148         }
149     }
150 }
151
152 /**
153  * Draw an arrow from (ex, ey) -> (sx, sy).
154  * @param w width of the image
155  * @param h height of the image
156  * @param stride stride/linesize of the image
157  * @param color color of the arrow
158  */
159 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
160                        int ey, int w, int h, int stride, int color, int tail, int direction)
161 {
162     int dx,dy;
163
164     if (direction) {
165         FFSWAP(int, sx, ex);
166         FFSWAP(int, sy, ey);
167     }
168
169     sx = av_clip(sx, -100, w + 100);
170     sy = av_clip(sy, -100, h + 100);
171     ex = av_clip(ex, -100, w + 100);
172     ey = av_clip(ey, -100, h + 100);
173
174     dx = ex - sx;
175     dy = ey - sy;
176
177     if (dx * dx + dy * dy > 3 * 3) {
178         int rx =  dx + dy;
179         int ry = -dx + dy;
180         int length = sqrt((rx * rx + ry * ry) << 8);
181
182         // FIXME subpixel accuracy
183         rx = ROUNDED_DIV(rx * 3 << 4, length);
184         ry = ROUNDED_DIV(ry * 3 << 4, length);
185
186         if (tail) {
187             rx = -rx;
188             ry = -ry;
189         }
190
191         draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
192         draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
193     }
194     draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
195 }
196
197 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
198 {
199     AVFilterContext *ctx = inlink->dst;
200     CodecViewContext *s = ctx->priv;
201     AVFilterLink *outlink = ctx->outputs[0];
202
203     if (s->qp) {
204         int qstride, qp_type;
205         int8_t *qp_table = av_frame_get_qp_table(frame, &qstride, &qp_type);
206
207         if (qp_table) {
208             int x, y;
209             const int w = FF_CEIL_RSHIFT(frame->width,  s->hsub);
210             const int h = FF_CEIL_RSHIFT(frame->height, s->vsub);
211             uint8_t *pu = frame->data[1];
212             uint8_t *pv = frame->data[2];
213             const int lzu = frame->linesize[1];
214             const int lzv = frame->linesize[2];
215
216             for (y = 0; y < h; y++) {
217                 for (x = 0; x < w; x++) {
218                     const int qp = ff_norm_qscale(qp_table[(y >> 3) * qstride + (x >> 3)], qp_type) * 128/31;
219                     pu[x] = pv[x] = qp;
220                 }
221                 pu += lzu;
222                 pv += lzv;
223             }
224         }
225     }
226
227     if (s->mv) {
228         AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
229         if (sd) {
230             int i;
231             const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
232             for (i = 0; i < sd->size / sizeof(*mvs); i++) {
233                 const AVMotionVector *mv = &mvs[i];
234                 const int direction = mv->source > 0;
235                 if ((direction == 0 && (s->mv & MV_P_FOR)  && frame->pict_type == AV_PICTURE_TYPE_P) ||
236                     (direction == 0 && (s->mv & MV_B_FOR)  && frame->pict_type == AV_PICTURE_TYPE_B) ||
237                     (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
238                     draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
239                                frame->width, frame->height, frame->linesize[0],
240                                100, 0, mv->source > 0);
241             }
242         }
243     }
244
245     return ff_filter_frame(outlink, frame);
246 }
247
248 static int config_input(AVFilterLink *inlink)
249 {
250     AVFilterContext *ctx = inlink->dst;
251     CodecViewContext *s = ctx->priv;
252     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
253
254     s->hsub = desc->log2_chroma_w;
255     s->vsub = desc->log2_chroma_h;
256     return 0;
257 }
258
259 static const AVFilterPad codecview_inputs[] = {
260     {
261         .name           = "default",
262         .type           = AVMEDIA_TYPE_VIDEO,
263         .filter_frame   = filter_frame,
264         .config_props   = config_input,
265         .needs_writable = 1,
266     },
267     { NULL }
268 };
269
270 static const AVFilterPad codecview_outputs[] = {
271     {
272         .name = "default",
273         .type = AVMEDIA_TYPE_VIDEO,
274     },
275     { NULL }
276 };
277
278 AVFilter ff_vf_codecview = {
279     .name          = "codecview",
280     .description   = NULL_IF_CONFIG_SMALL("Visualize information about some codecs."),
281     .priv_size     = sizeof(CodecViewContext),
282     .query_formats = query_formats,
283     .inputs        = codecview_inputs,
284     .outputs       = codecview_outputs,
285     .priv_class    = &codecview_class,
286     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
287 };