FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_codecview.c
Go to the documentation of this file.
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"
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;
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 
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
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 = AV_CEIL_RSHIFT(frame->width, s->hsub);
210  const int h = AV_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) {
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 
279  .name = "codecview",
280  .description = NULL_IF_CONFIG_SMALL("Visualize information about some codecs."),
281  .priv_size = sizeof(CodecViewContext),
283  .inputs = codecview_inputs,
284  .outputs = codecview_outputs,
285  .priv_class = &codecview_class,
287 };
#define MV_B_BACK
Definition: vf_codecview.c:40
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2157
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
AVOption.
Definition: opt.h:245
AVFormatContext * ctx
Definition: movenc-test.c:48
misc image utilities
int16_t src_x
Absolute source position.
Definition: motion_vector.h:38
Main libavfilter public API header.
static const AVFilterPad codecview_outputs[]
Definition: vf_codecview.c:270
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_codecview.c:197
static const AVOption codecview_options[]
Definition: vf_codecview.c:51
#define FLAGS
Definition: vf_codecview.c:50
int8_t * av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
Definition: frame.c:65
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:637
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:122
const char * name
Pad name.
Definition: internal.h:59
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1163
uint8_t
AVOptions.
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
int16_t dst_x
Absolute destination position.
Definition: motion_vector.h:42
static AVFrame * frame
Structure to hold side data for an AVFrame.
Definition: frame.h:144
int32_t source
Where the current macroblock comes from; negative value when it comes from the past, positive value when it comes from the future.
Definition: motion_vector.h:30
#define MV_P_FOR
Definition: vf_codecview.c:38
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color, int tail, int direction)
Draw an arrow from (ex, ey) -> (sx, sy).
Definition: vf_codecview.c:159
#define ROUNDED_DIV(a, b)
Definition: common.h:56
A filter pad used for either input or output.
Definition: internal.h:53
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color)
Draw a line from (ex, ey) -> (sx, sy).
Definition: vf_codecview.c:101
#define MV_B_FOR
Definition: vf_codecview.c:39
int width
width and height of the video frame
Definition: frame.h:230
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
#define pv
Definition: regdef.h:60
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:319
AVFilter ff_vf_codecview
Definition: vf_codecview.c:278
GLsizei GLsizei * length
Definition: opengl_enc.c:115
AVFILTER_DEFINE_CLASS(codecview)
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:252
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
Definition: frame.h:96
#define OFFSET(x)
Definition: vf_codecview.c:49
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
static const int8_t mv[256][2]
Definition: 4xm.c:77
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
static int query_formats(AVFilterContext *ctx)
Definition: vf_codecview.c:62
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
Definition: vf_codecview.c:73
uint8_t * data
Definition: frame.h:146
void * buf
Definition: avisynth_c.h:553
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:141
const char * name
Filter name.
Definition: avfilter.h:145
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
static int ff_norm_qscale(int qscale, int type)
Normalize the qscale factor FIXME the H264 qscale is a log based scale, mpeg1/2 is not...
Definition: internal.h:394
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
static int config_input(AVFilterLink *inlink)
Definition: vf_codecview.c:248
static const AVFilterPad codecview_inputs[]
Definition: vf_codecview.c:259
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
Bi-dir predicted.
Definition: avutil.h:268
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:304
int height
Definition: frame.h:230
#define FFSWAP(type, a, b)
Definition: common.h:99
#define stride
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:267
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58