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.

313 lines
11KB

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