You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.6KB

  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. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  59. if (!fmts_list)
  60. return AVERROR(ENOMEM);
  61. return ff_set_common_formats(ctx, fmts_list);
  62. }
  63. static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
  64. {
  65. if(*sx > *ex)
  66. return clip_line(ex, ey, sx, sy, maxx);
  67. if (*sx < 0) {
  68. if (*ex < 0)
  69. return 1;
  70. *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
  71. *sx = 0;
  72. }
  73. if (*ex > maxx) {
  74. if (*sx > maxx)
  75. return 1;
  76. *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
  77. *ex = maxx;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * Draw a line from (ex, ey) -> (sx, sy).
  83. * @param w width of the image
  84. * @param h height of the image
  85. * @param stride stride/linesize of the image
  86. * @param color color of the arrow
  87. */
  88. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
  89. int w, int h, int stride, int color)
  90. {
  91. int x, y, fr, f;
  92. if (clip_line(&sx, &sy, &ex, &ey, w - 1))
  93. return;
  94. if (clip_line(&sy, &sx, &ey, &ex, h - 1))
  95. return;
  96. sx = av_clip(sx, 0, w - 1);
  97. sy = av_clip(sy, 0, h - 1);
  98. ex = av_clip(ex, 0, w - 1);
  99. ey = av_clip(ey, 0, h - 1);
  100. buf[sy * stride + sx] += color;
  101. if (FFABS(ex - sx) > FFABS(ey - sy)) {
  102. if (sx > ex) {
  103. FFSWAP(int, sx, ex);
  104. FFSWAP(int, sy, ey);
  105. }
  106. buf += sx + sy * stride;
  107. ex -= sx;
  108. f = ((ey - sy) << 16) / ex;
  109. for (x = 0; x <= ex; x++) {
  110. y = (x * f) >> 16;
  111. fr = (x * f) & 0xFFFF;
  112. buf[ y * stride + x] += (color * (0x10000 - fr)) >> 16;
  113. if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
  114. }
  115. } else {
  116. if (sy > ey) {
  117. FFSWAP(int, sx, ex);
  118. FFSWAP(int, sy, ey);
  119. }
  120. buf += sx + sy * stride;
  121. ey -= sy;
  122. if (ey)
  123. f = ((ex - sx) << 16) / ey;
  124. else
  125. f = 0;
  126. for(y= 0; y <= ey; y++){
  127. x = (y*f) >> 16;
  128. fr = (y*f) & 0xFFFF;
  129. buf[y * stride + x ] += (color * (0x10000 - fr)) >> 16;
  130. if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
  131. }
  132. }
  133. }
  134. /**
  135. * Draw an arrow from (ex, ey) -> (sx, sy).
  136. * @param w width of the image
  137. * @param h height of the image
  138. * @param stride stride/linesize of the image
  139. * @param color color of the arrow
  140. */
  141. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
  142. int ey, int w, int h, int stride, int color, int tail, int direction)
  143. {
  144. int dx,dy;
  145. if (direction) {
  146. FFSWAP(int, sx, ex);
  147. FFSWAP(int, sy, ey);
  148. }
  149. sx = av_clip(sx, -100, w + 100);
  150. sy = av_clip(sy, -100, h + 100);
  151. ex = av_clip(ex, -100, w + 100);
  152. ey = av_clip(ey, -100, h + 100);
  153. dx = ex - sx;
  154. dy = ey - sy;
  155. if (dx * dx + dy * dy > 3 * 3) {
  156. int rx = dx + dy;
  157. int ry = -dx + dy;
  158. int length = sqrt((rx * rx + ry * ry) << 8);
  159. // FIXME subpixel accuracy
  160. rx = ROUNDED_DIV(rx * 3 << 4, length);
  161. ry = ROUNDED_DIV(ry * 3 << 4, length);
  162. if (tail) {
  163. rx = -rx;
  164. ry = -ry;
  165. }
  166. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  167. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  168. }
  169. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  170. }
  171. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  172. {
  173. AVFilterContext *ctx = inlink->dst;
  174. CodecViewContext *s = ctx->priv;
  175. AVFilterLink *outlink = ctx->outputs[0];
  176. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
  177. if (sd) {
  178. int i;
  179. const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
  180. for (i = 0; i < sd->size / sizeof(*mvs); i++) {
  181. const AVMotionVector *mv = &mvs[i];
  182. const int direction = mv->source > 0;
  183. if ((direction == 0 && (s->mv & MV_P_FOR) && frame->pict_type == AV_PICTURE_TYPE_P) ||
  184. (direction == 0 && (s->mv & MV_B_FOR) && frame->pict_type == AV_PICTURE_TYPE_B) ||
  185. (direction == 1 && (s->mv & MV_B_BACK) && frame->pict_type == AV_PICTURE_TYPE_B))
  186. draw_arrow(frame->data[0], mv->dst_x, mv->dst_y, mv->src_x, mv->src_y,
  187. frame->width, frame->height, frame->linesize[0],
  188. 100, 0, mv->source > 0);
  189. }
  190. }
  191. return ff_filter_frame(outlink, frame);
  192. }
  193. static const AVFilterPad codecview_inputs[] = {
  194. {
  195. .name = "default",
  196. .type = AVMEDIA_TYPE_VIDEO,
  197. .filter_frame = filter_frame,
  198. .needs_writable = 1,
  199. },
  200. { NULL }
  201. };
  202. static const AVFilterPad codecview_outputs[] = {
  203. {
  204. .name = "default",
  205. .type = AVMEDIA_TYPE_VIDEO,
  206. },
  207. { NULL }
  208. };
  209. AVFilter ff_vf_codecview = {
  210. .name = "codecview",
  211. .description = NULL_IF_CONFIG_SMALL("Visualize information about some codecs"),
  212. .priv_size = sizeof(CodecViewContext),
  213. .query_formats = query_formats,
  214. .inputs = codecview_inputs,
  215. .outputs = codecview_outputs,
  216. .priv_class = &codecview_class,
  217. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  218. };