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.

289 lines
10KB

  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/spherical.h"
  30. #include "libavutil/stereo3d.h"
  31. #include "libavutil/timestamp.h"
  32. #include "libavutil/timecode.h"
  33. #include "avfilter.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. static void dump_spherical(AVFilterContext *ctx, AVFrame *frame, AVFrameSideData *sd)
  37. {
  38. AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
  39. double yaw, pitch, roll;
  40. av_log(ctx, AV_LOG_INFO, "spherical information: ");
  41. if (sd->size < sizeof(*spherical)) {
  42. av_log(ctx, AV_LOG_INFO, "invalid data");
  43. return;
  44. }
  45. if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR)
  46. av_log(ctx, AV_LOG_INFO, "equirectangular ");
  47. else if (spherical->projection == AV_SPHERICAL_CUBEMAP)
  48. av_log(ctx, AV_LOG_INFO, "cubemap ");
  49. else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE)
  50. av_log(ctx, AV_LOG_INFO, "tiled equirectangular ");
  51. else {
  52. av_log(ctx, AV_LOG_WARNING, "unknown");
  53. return;
  54. }
  55. yaw = ((double)spherical->yaw) / (1 << 16);
  56. pitch = ((double)spherical->pitch) / (1 << 16);
  57. roll = ((double)spherical->roll) / (1 << 16);
  58. av_log(ctx, AV_LOG_INFO, "(%f/%f/%f) ", yaw, pitch, roll);
  59. if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
  60. size_t l, t, r, b;
  61. av_spherical_tile_bounds(spherical, frame->width, frame->height,
  62. &l, &t, &r, &b);
  63. av_log(ctx, AV_LOG_INFO,
  64. "[%"SIZE_SPECIFIER", %"SIZE_SPECIFIER", %"SIZE_SPECIFIER", %"SIZE_SPECIFIER"] ",
  65. l, t, r, b);
  66. } else if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
  67. av_log(ctx, AV_LOG_INFO, "[pad %"PRIu32"] ", spherical->padding);
  68. }
  69. }
  70. static void dump_stereo3d(AVFilterContext *ctx, AVFrameSideData *sd)
  71. {
  72. AVStereo3D *stereo;
  73. av_log(ctx, AV_LOG_INFO, "stereoscopic information: ");
  74. if (sd->size < sizeof(*stereo)) {
  75. av_log(ctx, AV_LOG_INFO, "invalid data");
  76. return;
  77. }
  78. stereo = (AVStereo3D *)sd->data;
  79. av_log(ctx, AV_LOG_INFO, "type - %s", av_stereo3d_type_name(stereo->type));
  80. if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
  81. av_log(ctx, AV_LOG_INFO, " (inverted)");
  82. }
  83. static void dump_color_property(AVFilterContext *ctx, AVFrame *frame)
  84. {
  85. const char *color_range_str = av_color_range_name(frame->color_range);
  86. const char *colorspace_str = av_color_space_name(frame->colorspace);
  87. const char *color_primaries_str = av_color_primaries_name(frame->color_primaries);
  88. const char *color_trc_str = av_color_transfer_name(frame->color_trc);
  89. if (!color_range_str || frame->color_range == AVCOL_RANGE_UNSPECIFIED) {
  90. av_log(ctx, AV_LOG_INFO, "color_range:unknown");
  91. } else {
  92. av_log(ctx, AV_LOG_INFO, "color_range:%s", color_range_str);
  93. }
  94. if (!colorspace_str || frame->colorspace == AVCOL_SPC_UNSPECIFIED) {
  95. av_log(ctx, AV_LOG_INFO, " color_space:unknown");
  96. } else {
  97. av_log(ctx, AV_LOG_INFO, " color_space:%s", colorspace_str);
  98. }
  99. if (!color_primaries_str || frame->color_primaries == AVCOL_PRI_UNSPECIFIED) {
  100. av_log(ctx, AV_LOG_INFO, " color_primaries:unknown");
  101. } else {
  102. av_log(ctx, AV_LOG_INFO, " color_primaries:%s", color_primaries_str);
  103. }
  104. if (!color_trc_str || frame->color_trc == AVCOL_TRC_UNSPECIFIED) {
  105. av_log(ctx, AV_LOG_INFO, " color_trc:unknown");
  106. } else {
  107. av_log(ctx, AV_LOG_INFO, " color_trc:%s", color_trc_str);
  108. }
  109. av_log(ctx, AV_LOG_INFO, "\n");
  110. }
  111. static void update_sample_stats(const uint8_t *src, int len, int64_t *sum, int64_t *sum2)
  112. {
  113. int i;
  114. for (i = 0; i < len; i++) {
  115. *sum += src[i];
  116. *sum2 += src[i] * src[i];
  117. }
  118. }
  119. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  123. uint32_t plane_checksum[4] = {0}, checksum = 0;
  124. int64_t sum[4] = {0}, sum2[4] = {0};
  125. int32_t pixelcount[4] = {0};
  126. int i, plane, vsub = desc->log2_chroma_h;
  127. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
  128. uint8_t *data = frame->data[plane];
  129. int h = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  130. int linesize = av_image_get_linesize(frame->format, frame->width, plane);
  131. if (linesize < 0)
  132. return linesize;
  133. for (i = 0; i < h; i++) {
  134. plane_checksum[plane] = av_adler32_update(plane_checksum[plane], data, linesize);
  135. checksum = av_adler32_update(checksum, data, linesize);
  136. update_sample_stats(data, linesize, sum+plane, sum2+plane);
  137. pixelcount[plane] += linesize;
  138. data += frame->linesize[plane];
  139. }
  140. }
  141. av_log(ctx, AV_LOG_INFO,
  142. "n:%4"PRId64" pts:%7s pts_time:%-7s pos:%9"PRId64" "
  143. "fmt:%s sar:%d/%d s:%dx%d i:%c iskey:%d type:%c "
  144. "checksum:%08"PRIX32" plane_checksum:[%08"PRIX32,
  145. inlink->frame_count_out,
  146. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base), frame->pkt_pos,
  147. desc->name,
  148. frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
  149. frame->width, frame->height,
  150. !frame->interlaced_frame ? 'P' : /* Progressive */
  151. frame->top_field_first ? 'T' : 'B', /* Top / Bottom */
  152. frame->key_frame,
  153. av_get_picture_type_char(frame->pict_type),
  154. checksum, plane_checksum[0]);
  155. for (plane = 1; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  156. av_log(ctx, AV_LOG_INFO, " %08"PRIX32, plane_checksum[plane]);
  157. av_log(ctx, AV_LOG_INFO, "] mean:[");
  158. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  159. av_log(ctx, AV_LOG_INFO, "%"PRId64" ", (sum[plane] + pixelcount[plane]/2) / pixelcount[plane]);
  160. av_log(ctx, AV_LOG_INFO, "\b] stdev:[");
  161. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  162. av_log(ctx, AV_LOG_INFO, "%3.1f ",
  163. sqrt((sum2[plane] - sum[plane]*(double)sum[plane]/pixelcount[plane])/pixelcount[plane]));
  164. av_log(ctx, AV_LOG_INFO, "\b]\n");
  165. for (i = 0; i < frame->nb_side_data; i++) {
  166. AVFrameSideData *sd = frame->side_data[i];
  167. av_log(ctx, AV_LOG_INFO, " side data - ");
  168. switch (sd->type) {
  169. case AV_FRAME_DATA_PANSCAN:
  170. av_log(ctx, AV_LOG_INFO, "pan/scan");
  171. break;
  172. case AV_FRAME_DATA_A53_CC:
  173. av_log(ctx, AV_LOG_INFO, "A/53 closed captions (%d bytes)", sd->size);
  174. break;
  175. case AV_FRAME_DATA_SPHERICAL:
  176. dump_spherical(ctx, frame, sd);
  177. break;
  178. case AV_FRAME_DATA_STEREO3D:
  179. dump_stereo3d(ctx, sd);
  180. break;
  181. case AV_FRAME_DATA_S12M_TIMECODE: {
  182. uint32_t *tc = (uint32_t*)sd->data;
  183. for (int j = 1; j < tc[0]; j++) {
  184. char tcbuf[AV_TIMECODE_STR_SIZE];
  185. av_timecode_make_smpte_tc_string(tcbuf, tc[j], 0);
  186. av_log(ctx, AV_LOG_INFO, "timecode - %s%s", tcbuf, j != tc[0] - 1 ? ", " : "");
  187. }
  188. break;
  189. }
  190. case AV_FRAME_DATA_DISPLAYMATRIX:
  191. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  192. av_display_rotation_get((int32_t *)sd->data));
  193. break;
  194. case AV_FRAME_DATA_AFD:
  195. av_log(ctx, AV_LOG_INFO, "afd: value of %"PRIu8, sd->data[0]);
  196. break;
  197. default:
  198. av_log(ctx, AV_LOG_WARNING, "unknown side data type %d (%d bytes)",
  199. sd->type, sd->size);
  200. break;
  201. }
  202. av_log(ctx, AV_LOG_INFO, "\n");
  203. }
  204. dump_color_property(ctx, frame);
  205. return ff_filter_frame(inlink->dst->outputs[0], frame);
  206. }
  207. static int config_props(AVFilterContext *ctx, AVFilterLink *link, int is_out)
  208. {
  209. av_log(ctx, AV_LOG_INFO, "config %s time_base: %d/%d, frame_rate: %d/%d\n",
  210. is_out ? "out" : "in",
  211. link->time_base.num, link->time_base.den,
  212. link->frame_rate.num, link->frame_rate.den);
  213. return 0;
  214. }
  215. static int config_props_in(AVFilterLink *link)
  216. {
  217. AVFilterContext *ctx = link->dst;
  218. return config_props(ctx, link, 0);
  219. }
  220. static int config_props_out(AVFilterLink *link)
  221. {
  222. AVFilterContext *ctx = link->src;
  223. return config_props(ctx, link, 1);
  224. }
  225. static const AVFilterPad avfilter_vf_showinfo_inputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .filter_frame = filter_frame,
  230. .config_props = config_props_in,
  231. },
  232. { NULL }
  233. };
  234. static const AVFilterPad avfilter_vf_showinfo_outputs[] = {
  235. {
  236. .name = "default",
  237. .type = AVMEDIA_TYPE_VIDEO,
  238. .config_props = config_props_out,
  239. },
  240. { NULL }
  241. };
  242. AVFilter ff_vf_showinfo = {
  243. .name = "showinfo",
  244. .description = NULL_IF_CONFIG_SMALL("Show textual information for each video frame."),
  245. .inputs = avfilter_vf_showinfo_inputs,
  246. .outputs = avfilter_vf_showinfo_outputs,
  247. };