vf_codecview.c 8.9 KB

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