vf_codecview.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * @file
  23. * Codec debug viewer filter.
  24. *
  25. * All the MV drawing code from Michael Niedermayer is extracted from
  26. * libavcodec/mpegvideo.c.
  27. *
  28. * TODO: segmentation
  29. * TODO: quantization
  30. */
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/motion_vector.h"
  33. #include "libavutil/opt.h"
  34. #include "avfilter.h"
  35. #include "internal.h"
  36. #define MV_P_FOR (1<<0)
  37. #define MV_B_FOR (1<<1)
  38. #define MV_B_BACK (1<<2)
  39. typedef struct {
  40. const AVClass *class;
  41. unsigned mv;
  42. } CodecViewContext;
  43. #define OFFSET(x) offsetof(CodecViewContext, x)
  44. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  45. static const AVOption codecview_options[] = {
  46. { "mv", "set motion vectors to visualize", OFFSET(mv), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "mv" },
  47. {"pf", "forward predicted MVs of P-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_P_FOR }, INT_MIN, INT_MAX, FLAGS, "mv"},
  48. {"bf", "forward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_B_FOR }, INT_MIN, INT_MAX, FLAGS, "mv"},
  49. {"bb", "backward predicted MVs of B-frames", 0, AV_OPT_TYPE_CONST, {.i64 = MV_B_BACK }, INT_MIN, INT_MAX, FLAGS, "mv"},
  50. { NULL }
  51. };
  52. AVFILTER_DEFINE_CLASS(codecview);
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. // TODO: we can probably add way more pixel formats without any other
  56. // changes; anything with 8-bit luma in first plane should be working
  57. static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE};
  58. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  59. return 0;
  60. }
  61. static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
  62. {
  63. if(*sx > *ex)
  64. return clip_line(ex, ey, sx, sy, maxx);
  65. if (*sx < 0) {
  66. if (*ex < 0)
  67. return 1;
  68. *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
  69. *sx = 0;
  70. }
  71. if (*ex > maxx) {
  72. if (*sx > maxx)
  73. return 1;
  74. *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
  75. *ex = maxx;
  76. }
  77. return 0;
  78. }
  79. /**
  80. * Draw a line from (ex, ey) -> (sx, sy).
  81. * @param w width of the image
  82. * @param h height of the image
  83. * @param stride stride/linesize of the image
  84. * @param color color of the arrow
  85. */
  86. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  87. int w, int h, int stride, int color)
  88. {
  89. int x, y, fr, f;
  90. if (clip_line(&sx, &sy, &ex, &ey, w - 1))
  91. return;
  92. if (clip_line(&sy, &sx, &ey, &ex, h - 1))
  93. return;
  94. sx = av_clip(sx, 0, w - 1);
  95. sy = av_clip(sy, 0, h - 1);
  96. ex = av_clip(ex, 0, w - 1);
  97. ey = av_clip(ey, 0, h - 1);
  98. buf[sy * stride + sx] += color;
  99. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  100. if (sx > ex) {
  101. FFSWAP(int, sx, ex);
  102. FFSWAP(int, sy, ey);
  103. }
  104. buf += sx + sy * stride;
  105. ex -= sx;
  106. f = ((ey - sy) << 16) / ex;
  107. for (x = 0; x <= ex; x++) {
  108. y = (x * f) >> 16;
  109. fr = (x * f) & 0xFFFF;
  110. buf[ y * stride + x] += (color * (0x10000 - fr)) >> 16;
  111. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  112. }
  113. } else {
  114. if (sy > ey) {
  115. FFSWAP(int, sx, ex);
  116. FFSWAP(int, sy, ey);
  117. }
  118. buf += sx + sy * stride;
  119. ey -= sy;
  120. if (ey)
  121. f = ((ex - sx) << 16) / ey;
  122. else
  123. f = 0;
  124. for(y= 0; y <= ey; y++){
  125. x = (y*f) >> 16;
  126. fr = (y*f) & 0xFFFF;
  127. buf[y * stride + x ] += (color * (0x10000 - fr)) >> 16;
  128. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  129. }
  130. }
  131. }
  132. /**
  133. * Draw an arrow from (ex, ey) -> (sx, sy).
  134. * @param w width of the image
  135. * @param h height of the image
  136. * @param stride stride/linesize of the image
  137. * @param color color of the arrow
  138. */
  139. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  140. int ey, int w, int h, int stride, int color, int tail, int direction)
  141. {
  142. int dx,dy;
  143. if (direction) {
  144. FFSWAP(int, sx, ex);
  145. FFSWAP(int, sy, ey);
  146. }
  147. sx = av_clip(sx, -100, w + 100);
  148. sy = av_clip(sy, -100, h + 100);
  149. ex = av_clip(ex, -100, w + 100);
  150. ey = av_clip(ey, -100, h + 100);
  151. dx = ex - sx;
  152. dy = ey - sy;
  153. if (dx * dx + dy * dy > 3 * 3) {
  154. int rx = dx + dy;
  155. int ry = -dx + dy;
  156. int length = sqrt((rx * rx + ry * ry) << 8);
  157. // FIXME subpixel accuracy
  158. rx = ROUNDED_DIV(rx * 3 << 4, length);
  159. ry = ROUNDED_DIV(ry * 3 << 4, length);
  160. if (tail) {
  161. rx = -rx;
  162. ry = -ry;
  163. }
  164. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  165. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  166. }
  167. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  168. }
  169. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  170. {
  171. AVFilterContext *ctx = inlink->dst;
  172. CodecViewContext *s = ctx->priv;
  173. AVFilterLink *outlink = ctx->outputs[0];
  174. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
  175. if (sd) {
  176. int i;
  177. const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
  178. for (i = 0; i < sd->size / sizeof(*mvs); i++) {
  179. const AVMotionVector *mv = &mvs[i];
  180. const int direction = mv->source > 0;
  181. if ((direction == 0 && (s->mv & MV_P_FOR) && frame->pict_type == AV_PICTURE_TYPE_P) ||
  182. (direction == 0 && (s->mv & MV_B_FOR) && frame->pict_type == AV_PICTURE_TYPE_B) ||
  183. (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
  184. draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
  185. frame->width, frame->height, frame->linesize[0],
  186. 100, 0, mv->source > 0);
  187. }
  188. }
  189. return ff_filter_frame(outlink, frame);
  190. }
  191. static const AVFilterPad codecview_inputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. .filter_frame = filter_frame,
  196. .needs_writable = 1,
  197. },
  198. { NULL }
  199. };
  200. static const AVFilterPad codecview_outputs[] = {
  201. {
  202. .name = "default",
  203. .type = AVMEDIA_TYPE_VIDEO,
  204. },
  205. { NULL }
  206. };
  207. AVFilter ff_vf_codecview = {
  208. .name = "codecview",
  209. .description = NULL_IF_CONFIG_SMALL("Visualize information about some codecs"),
  210. .priv_size = sizeof(CodecViewContext),
  211. .query_formats = query_formats,
  212. .inputs = codecview_inputs,
  213. .outputs = codecview_outputs,
  214. .priv_class = &codecview_class,
  215. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  216. };