vf_showinfo.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * filter for showing textual video frame information
  22. */
  23. #include <inttypes.h>
  24. #include "libavutil/adler32.h"
  25. #include "libavutil/display.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/stereo3d.h"
  30. #include "libavutil/timestamp.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. static void dump_stereo3d(AVFilterContext *ctx, AVFrameSideData *sd)
  35. {
  36. AVStereo3D *stereo;
  37. av_log(ctx, AV_LOG_INFO, "stereoscopic information: ");
  38. if (sd->size < sizeof(*stereo)) {
  39. av_log(ctx, AV_LOG_INFO, "invalid data");
  40. return;
  41. }
  42. stereo = (AVStereo3D *)sd->data;
  43. av_log(ctx, AV_LOG_INFO, "type - ");
  44. switch (stereo->type) {
  45. case AV_STEREO3D_2D: av_log(ctx, AV_LOG_INFO, "2D"); break;
  46. case AV_STEREO3D_SIDEBYSIDE: av_log(ctx, AV_LOG_INFO, "side by side"); break;
  47. case AV_STEREO3D_TOPBOTTOM: av_log(ctx, AV_LOG_INFO, "top and bottom"); break;
  48. case AV_STEREO3D_FRAMESEQUENCE: av_log(ctx, AV_LOG_INFO, "frame alternate"); break;
  49. case AV_STEREO3D_CHECKERBOARD: av_log(ctx, AV_LOG_INFO, "checkerboard"); break;
  50. case AV_STEREO3D_LINES: av_log(ctx, AV_LOG_INFO, "interleaved lines"); break;
  51. case AV_STEREO3D_COLUMNS: av_log(ctx, AV_LOG_INFO, "interleaved columns"); break;
  52. case AV_STEREO3D_SIDEBYSIDE_QUINCUNX: av_log(ctx, AV_LOG_INFO, "side by side "
  53. "(quincunx subsampling)"); break;
  54. default: av_log(ctx, AV_LOG_WARNING, "unknown"); break;
  55. }
  56. if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
  57. av_log(ctx, AV_LOG_INFO, " (inverted)");
  58. }
  59. static void update_sample_stats(const uint8_t *src, int len, int64_t *sum, int64_t *sum2)
  60. {
  61. int i;
  62. for (i = 0; i < len; i++) {
  63. *sum += src[i];
  64. *sum2 += src[i] * src[i];
  65. }
  66. }
  67. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  68. {
  69. AVFilterContext *ctx = inlink->dst;
  70. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  71. uint32_t plane_checksum[4] = {0}, checksum = 0;
  72. int64_t sum[4] = {0}, sum2[4] = {0};
  73. int32_t pixelcount[4] = {0};
  74. int i, plane, vsub = desc->log2_chroma_h;
  75. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  76. uint8_t *data = frame->data[plane];
  77. int h = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  78. int linesize = av_image_get_linesize(frame->format, frame->width, plane);
  79. if (linesize < 0)
  80. return linesize;
  81. for (i = 0; i < h; i++) {
  82. plane_checksum[plane] = av_adler32_update(plane_checksum[plane], data, linesize);
  83. checksum = av_adler32_update(checksum, data, linesize);
  84. update_sample_stats(data, linesize, sum+plane, sum2+plane);
  85. pixelcount[plane] += linesize;
  86. data += frame->linesize[plane];
  87. }
  88. }
  89. av_log(ctx, AV_LOG_INFO,
  90. "n:%4"PRId64" pts:%7s pts_time:%-7s pos:%9"PRId64" "
  91. "fmt:%s sar:%d/%d s:%dx%d i:%c iskey:%d type:%c "
  92. "checksum:%08"PRIX32" plane_checksum:[%08"PRIX32,
  93. inlink->frame_count,
  94. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base), av_frame_get_pkt_pos(frame),
  95. desc->name,
  96. frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
  97. frame->width, frame->height,
  98. !frame->interlaced_frame ? 'P' : /* Progressive */
  99. frame->top_field_first ? 'T' : 'B', /* Top / Bottom */
  100. frame->key_frame,
  101. av_get_picture_type_char(frame->pict_type),
  102. checksum, plane_checksum[0]);
  103. for (plane = 1; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  104. av_log(ctx, AV_LOG_INFO, " %08"PRIX32, plane_checksum[plane]);
  105. av_log(ctx, AV_LOG_INFO, "] mean:[");
  106. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  107. av_log(ctx, AV_LOG_INFO, "%"PRId64" ", (sum[plane] + pixelcount[plane]/2) / pixelcount[plane]);
  108. av_log(ctx, AV_LOG_INFO, "\b] stdev:[");
  109. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  110. av_log(ctx, AV_LOG_INFO, "%3.1f ",
  111. sqrt((sum2[plane] - sum[plane]*(double)sum[plane]/pixelcount[plane])/pixelcount[plane]));
  112. av_log(ctx, AV_LOG_INFO, "\b]\n");
  113. for (i = 0; i < frame->nb_side_data; i++) {
  114. AVFrameSideData *sd = frame->side_data[i];
  115. av_log(ctx, AV_LOG_INFO, " side data - ");
  116. switch (sd->type) {
  117. case AV_FRAME_DATA_PANSCAN:
  118. av_log(ctx, AV_LOG_INFO, "pan/scan");
  119. break;
  120. case AV_FRAME_DATA_A53_CC:
  121. av_log(ctx, AV_LOG_INFO, "A/53 closed captions (%d bytes)", sd->size);
  122. break;
  123. case AV_FRAME_DATA_STEREO3D:
  124. dump_stereo3d(ctx, sd);
  125. break;
  126. case AV_FRAME_DATA_DISPLAYMATRIX:
  127. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  128. av_display_rotation_get((int32_t *)sd->data));
  129. break;
  130. case AV_FRAME_DATA_AFD:
  131. av_log(ctx, AV_LOG_INFO, "afd: value of %"PRIu8, sd->data[0]);
  132. break;
  133. default:
  134. av_log(ctx, AV_LOG_WARNING, "unknown side data type %d (%d bytes)",
  135. sd->type, sd->size);
  136. break;
  137. }
  138. av_log(ctx, AV_LOG_INFO, "\n");
  139. }
  140. return ff_filter_frame(inlink->dst->outputs[0], frame);
  141. }
  142. static int config_props(AVFilterContext *ctx, AVFilterLink *link, int is_out)
  143. {
  144. av_log(ctx, AV_LOG_INFO, "config %s time_base: %d/%d, frame_rate: %d/%d\n",
  145. is_out ? "out" : "in",
  146. link->time_base.num, link->time_base.den,
  147. link->frame_rate.num, link->frame_rate.den);
  148. return 0;
  149. }
  150. static int config_props_in(AVFilterLink *link)
  151. {
  152. AVFilterContext *ctx = link->dst;
  153. return config_props(ctx, link, 0);
  154. }
  155. static int config_props_out(AVFilterLink *link)
  156. {
  157. AVFilterContext *ctx = link->src;
  158. return config_props(ctx, link, 1);
  159. }
  160. static const AVFilterPad avfilter_vf_showinfo_inputs[] = {
  161. {
  162. .name = "default",
  163. .type = AVMEDIA_TYPE_VIDEO,
  164. .filter_frame = filter_frame,
  165. .config_props = config_props_in,
  166. },
  167. { NULL }
  168. };
  169. static const AVFilterPad avfilter_vf_showinfo_outputs[] = {
  170. {
  171. .name = "default",
  172. .type = AVMEDIA_TYPE_VIDEO,
  173. .config_props = config_props_out,
  174. },
  175. { NULL }
  176. };
  177. AVFilter ff_vf_showinfo = {
  178. .name = "showinfo",
  179. .description = NULL_IF_CONFIG_SMALL("Show textual information for each video frame."),
  180. .inputs = avfilter_vf_showinfo_inputs,
  181. .outputs = avfilter_vf_showinfo_outputs,
  182. };