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  * TODO: quantization
31  */
32 
33 #include "libavutil/imgutils.h"
35 #include "libavutil/opt.h"
36 #include "avfilter.h"
37 #include "internal.h"
38 
39 #define MV_P_FOR (1<<0)
40 #define MV_B_FOR (1<<1)
41 #define MV_B_BACK (1<<2)
42 
43 typedef struct {
44  const AVClass *class;
45  unsigned mv;
47 
48 #define OFFSET(x) offsetof(CodecViewContext, x)
49 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
50 static const AVOption codecview_options[] = {
51  { "mv", "set motion vectors to visualize", OFFSET(mv), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv" },
52  {"pf", "forward predicted MVs of P-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_P_FOR }, INT_MIN, INT_MAX, FLAGS, "mv"},
53  {"bf", "forward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_B_FOR }, INT_MIN, INT_MAX, FLAGS, "mv"},
54  {"bb", "backward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_B_BACK }, INT_MIN, INT_MAX, FLAGS, "mv"},
55  { NULL }
56 };
57 
58 AVFILTER_DEFINE_CLASS(codecview);
59 
61 {
62  // TODO: we can probably add way more pixel formats without any other
63  // changes; anything with 8-bit luma in first plane should be working
65  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
66  if (!fmts_list)
67  return AVERROR(ENOMEM);
68  return ff_set_common_formats(ctx, fmts_list);
69 }
70 
71 static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
72 {
73  if(*sx > *ex)
74  return clip_line(ex, ey, sx, sy, maxx);
75 
76  if (*sx < 0) {
77  if (*ex < 0)
78  return 1;
79  *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
80  *sx = 0;
81  }
82 
83  if (*ex > maxx) {
84  if (*sx > maxx)
85  return 1;
86  *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
87  *ex = maxx;
88  }
89  return 0;
90 }
91 
92 /**
93  * Draw a line from (ex, ey) -> (sx, sy).
94  * @param w width of the image
95  * @param h height of the image
96  * @param stride stride/linesize of the image
97  * @param color color of the arrow
98  */
99 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
100  int w, int h, int stride, int color)
101 {
102  int x, y, fr, f;
103 
104  if (clip_line(&sx, &sy, &ex, &ey, w - 1))
105  return;
106  if (clip_line(&sy, &sx, &ey, &ex, h - 1))
107  return;
108 
109  sx = av_clip(sx, 0, w - 1);
110  sy = av_clip(sy, 0, h - 1);
111  ex = av_clip(ex, 0, w - 1);
112  ey = av_clip(ey, 0, h - 1);
113 
114  buf[sy * stride + sx] += color;
115 
116  if (FFABS(ex - sx) > FFABS(ey - sy)) {
117  if (sx > ex) {
118  FFSWAP(int, sx, ex);
119  FFSWAP(int, sy, ey);
120  }
121  buf += sx + sy * stride;
122  ex -= sx;
123  f = ((ey - sy) << 16) / ex;
124  for (x = 0; x <= ex; x++) {
125  y = (x * f) >> 16;
126  fr = (x * f) & 0xFFFF;
127  buf[ y * stride + x] += (color * (0x10000 - fr)) >> 16;
128  if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
129  }
130  } else {
131  if (sy > ey) {
132  FFSWAP(int, sx, ex);
133  FFSWAP(int, sy, ey);
134  }
135  buf += sx + sy * stride;
136  ey -= sy;
137  if (ey)
138  f = ((ex - sx) << 16) / ey;
139  else
140  f = 0;
141  for(y= 0; y <= ey; y++){
142  x = (y*f) >> 16;
143  fr = (y*f) & 0xFFFF;
144  buf[y * stride + x ] += (color * (0x10000 - fr)) >> 16;
145  if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
146  }
147  }
148 }
149 
150 /**
151  * Draw an arrow from (ex, ey) -> (sx, sy).
152  * @param w width of the image
153  * @param h height of the image
154  * @param stride stride/linesize of the image
155  * @param color color of the arrow
156  */
157 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
158  int ey, int w, int h, int stride, int color, int tail, int direction)
159 {
160  int dx,dy;
161 
162  if (direction) {
163  FFSWAP(int, sx, ex);
164  FFSWAP(int, sy, ey);
165  }
166 
167  sx = av_clip(sx, -100, w + 100);
168  sy = av_clip(sy, -100, h + 100);
169  ex = av_clip(ex, -100, w + 100);
170  ey = av_clip(ey, -100, h + 100);
171 
172  dx = ex - sx;
173  dy = ey - sy;
174 
175  if (dx * dx + dy * dy > 3 * 3) {
176  int rx = dx + dy;
177  int ry = -dx + dy;
178  int length = sqrt((rx * rx + ry * ry) << 8);
179 
180  // FIXME subpixel accuracy
181  rx = ROUNDED_DIV(rx * 3 << 4, length);
182  ry = ROUNDED_DIV(ry * 3 << 4, length);
183 
184  if (tail) {
185  rx = -rx;
186  ry = -ry;
187  }
188 
189  draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
190  draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
191  }
192  draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
193 }
194 
195 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
196 {
197  AVFilterContext *ctx = inlink->dst;
198  CodecViewContext *s = ctx->priv;
199  AVFilterLink *outlink = ctx->outputs[0];
200 
202  if (sd) {
203  int i;
204  const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
205  for (i = 0; i < sd->size / sizeof(*mvs); i++) {
206  const AVMotionVector *mv = &mvs[i];
207  const int direction = mv->source > 0;
208  if ((direction == 0 && (s->mv & MV_P_FOR) && frame->pict_type == AV_PICTURE_TYPE_P) ||
209  (direction == 0 && (s->mv & MV_B_FOR) && frame->pict_type == AV_PICTURE_TYPE_B) ||
210  (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
211  draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
212  frame->width, frame->height, frame->linesize[0],
213  100, 0, mv->source > 0);
214  }
215  }
216  return ff_filter_frame(outlink, frame);
217 }
218 
219 static const AVFilterPad codecview_inputs[] = {
220  {
221  .name = "default",
222  .type = AVMEDIA_TYPE_VIDEO,
223  .filter_frame = filter_frame,
224  .needs_writable = 1,
225  },
226  { NULL }
227 };
228 
229 static const AVFilterPad codecview_outputs[] = {
230  {
231  .name = "default",
232  .type = AVMEDIA_TYPE_VIDEO,
233  },
234  { NULL }
235 };
236 
238  .name = "codecview",
239  .description = NULL_IF_CONFIG_SMALL("Visualize information about some codecs"),
240  .priv_size = sizeof(CodecViewContext),
242  .inputs = codecview_inputs,
243  .outputs = codecview_outputs,
244  .priv_class = &codecview_class,
246 };
#define MV_B_BACK
Definition: vf_codecview.c:41
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
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:229
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_codecview.c:195
static const AVOption codecview_options[]
Definition: vf_codecview.c:50
#define FLAGS
Definition: vf_codecview.c:49
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:625
#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:451
const char * name
Pad name.
Definition: internal.h:69
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
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:134
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:39
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:157
#define ROUNDED_DIV(a, b)
Definition: common.h:55
A filter pad used for either input or output.
Definition: internal.h:63
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:99
#define MV_B_FOR
Definition: vf_codecview.c:40
int width
width and height of the video frame
Definition: frame.h:220
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:542
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
AVFilter ff_vf_codecview
Definition: vf_codecview.c:237
GLsizei GLsizei * length
Definition: opengl_enc.c:115
AVFILTER_DEFINE_CLASS(codecview)
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
float y
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:48
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:68
static const int8_t mv[256][2]
Definition: 4xm.c:77
static int query_formats(AVFilterContext *ctx)
Definition: vf_codecview.c:60
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
Definition: vf_codecview.c:71
uint8_t * data
Definition: frame.h:136
void * buf
Definition: avisynth_c.h:553
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static const AVFilterPad codecview_inputs[]
Definition: vf_codecview.c:219
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:633
int height
Definition: frame.h:220
#define FFSWAP(type, a, b)
Definition: common.h:84
#define stride
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:267