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.

414 lines
15KB

  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/bswap.h"
  25. #include "libavutil/adler32.h"
  26. #include "libavutil/display.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/internal.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/spherical.h"
  32. #include "libavutil/stereo3d.h"
  33. #include "libavutil/timestamp.h"
  34. #include "libavutil/timecode.h"
  35. #include "libavutil/mastering_display_metadata.h"
  36. #include "avfilter.h"
  37. #include "internal.h"
  38. #include "video.h"
  39. typedef struct ShowInfoContext {
  40. const AVClass *class;
  41. int calculate_checksums;
  42. } ShowInfoContext;
  43. #define OFFSET(x) offsetof(ShowInfoContext, x)
  44. #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. static const AVOption showinfo_options[] = {
  46. { "checksum", "calculate checksums", OFFSET(calculate_checksums), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, VF },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(showinfo);
  50. static void dump_spherical(AVFilterContext *ctx, AVFrame *frame, AVFrameSideData *sd)
  51. {
  52. AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
  53. double yaw, pitch, roll;
  54. av_log(ctx, AV_LOG_INFO, "spherical information: ");
  55. if (sd->size < sizeof(*spherical)) {
  56. av_log(ctx, AV_LOG_ERROR, "invalid data");
  57. return;
  58. }
  59. if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR)
  60. av_log(ctx, AV_LOG_INFO, "equirectangular ");
  61. else if (spherical->projection == AV_SPHERICAL_CUBEMAP)
  62. av_log(ctx, AV_LOG_INFO, "cubemap ");
  63. else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE)
  64. av_log(ctx, AV_LOG_INFO, "tiled equirectangular ");
  65. else {
  66. av_log(ctx, AV_LOG_WARNING, "unknown");
  67. return;
  68. }
  69. yaw = ((double)spherical->yaw) / (1 << 16);
  70. pitch = ((double)spherical->pitch) / (1 << 16);
  71. roll = ((double)spherical->roll) / (1 << 16);
  72. av_log(ctx, AV_LOG_INFO, "(%f/%f/%f) ", yaw, pitch, roll);
  73. if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
  74. size_t l, t, r, b;
  75. av_spherical_tile_bounds(spherical, frame->width, frame->height,
  76. &l, &t, &r, &b);
  77. av_log(ctx, AV_LOG_INFO,
  78. "[%"SIZE_SPECIFIER", %"SIZE_SPECIFIER", %"SIZE_SPECIFIER", %"SIZE_SPECIFIER"] ",
  79. l, t, r, b);
  80. } else if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
  81. av_log(ctx, AV_LOG_INFO, "[pad %"PRIu32"] ", spherical->padding);
  82. }
  83. }
  84. static void dump_stereo3d(AVFilterContext *ctx, AVFrameSideData *sd)
  85. {
  86. AVStereo3D *stereo;
  87. av_log(ctx, AV_LOG_INFO, "stereoscopic information: ");
  88. if (sd->size < sizeof(*stereo)) {
  89. av_log(ctx, AV_LOG_ERROR, "invalid data");
  90. return;
  91. }
  92. stereo = (AVStereo3D *)sd->data;
  93. av_log(ctx, AV_LOG_INFO, "type - %s", av_stereo3d_type_name(stereo->type));
  94. if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
  95. av_log(ctx, AV_LOG_INFO, " (inverted)");
  96. }
  97. static void dump_roi(AVFilterContext *ctx, AVFrameSideData *sd)
  98. {
  99. int nb_rois;
  100. const AVRegionOfInterest *roi;
  101. uint32_t roi_size;
  102. roi = (const AVRegionOfInterest *)sd->data;
  103. roi_size = roi->self_size;
  104. if (!roi_size || sd->size % roi_size != 0) {
  105. av_log(ctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.");
  106. return;
  107. }
  108. nb_rois = sd->size / roi_size;
  109. av_log(ctx, AV_LOG_INFO, "Regions Of Interest(RoI) information: ");
  110. for (int i = 0; i < nb_rois; i++) {
  111. roi = (const AVRegionOfInterest *)(sd->data + roi_size * i);
  112. av_log(ctx, AV_LOG_INFO, "index: %d, region: (%d, %d)/(%d, %d), qp offset: %d/%d.\n",
  113. i, roi->left, roi->top, roi->right, roi->bottom, roi->qoffset.num, roi->qoffset.den);
  114. }
  115. }
  116. static void dump_mastering_display(AVFilterContext *ctx, AVFrameSideData *sd)
  117. {
  118. AVMasteringDisplayMetadata *mastering_display;
  119. av_log(ctx, AV_LOG_INFO, "mastering display: ");
  120. if (sd->size < sizeof(*mastering_display)) {
  121. av_log(ctx, AV_LOG_ERROR, "invalid data");
  122. return;
  123. }
  124. mastering_display = (AVMasteringDisplayMetadata *)sd->data;
  125. av_log(ctx, AV_LOG_INFO, "has_primaries:%d has_luminance:%d "
  126. "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f) "
  127. "min_luminance=%f, max_luminance=%f",
  128. mastering_display->has_primaries, mastering_display->has_luminance,
  129. av_q2d(mastering_display->display_primaries[0][0]),
  130. av_q2d(mastering_display->display_primaries[0][1]),
  131. av_q2d(mastering_display->display_primaries[1][0]),
  132. av_q2d(mastering_display->display_primaries[1][1]),
  133. av_q2d(mastering_display->display_primaries[2][0]),
  134. av_q2d(mastering_display->display_primaries[2][1]),
  135. av_q2d(mastering_display->white_point[0]), av_q2d(mastering_display->white_point[1]),
  136. av_q2d(mastering_display->min_luminance), av_q2d(mastering_display->max_luminance));
  137. }
  138. static void dump_content_light_metadata(AVFilterContext *ctx, AVFrameSideData *sd)
  139. {
  140. AVContentLightMetadata* metadata = (AVContentLightMetadata*)sd->data;
  141. av_log(ctx, AV_LOG_INFO, "Content Light Level information: "
  142. "MaxCLL=%d, MaxFALL=%d",
  143. metadata->MaxCLL, metadata->MaxFALL);
  144. }
  145. static void dump_color_property(AVFilterContext *ctx, AVFrame *frame)
  146. {
  147. const char *color_range_str = av_color_range_name(frame->color_range);
  148. const char *colorspace_str = av_color_space_name(frame->colorspace);
  149. const char *color_primaries_str = av_color_primaries_name(frame->color_primaries);
  150. const char *color_trc_str = av_color_transfer_name(frame->color_trc);
  151. if (!color_range_str || frame->color_range == AVCOL_RANGE_UNSPECIFIED) {
  152. av_log(ctx, AV_LOG_INFO, "color_range:unknown");
  153. } else {
  154. av_log(ctx, AV_LOG_INFO, "color_range:%s", color_range_str);
  155. }
  156. if (!colorspace_str || frame->colorspace == AVCOL_SPC_UNSPECIFIED) {
  157. av_log(ctx, AV_LOG_INFO, " color_space:unknown");
  158. } else {
  159. av_log(ctx, AV_LOG_INFO, " color_space:%s", colorspace_str);
  160. }
  161. if (!color_primaries_str || frame->color_primaries == AVCOL_PRI_UNSPECIFIED) {
  162. av_log(ctx, AV_LOG_INFO, " color_primaries:unknown");
  163. } else {
  164. av_log(ctx, AV_LOG_INFO, " color_primaries:%s", color_primaries_str);
  165. }
  166. if (!color_trc_str || frame->color_trc == AVCOL_TRC_UNSPECIFIED) {
  167. av_log(ctx, AV_LOG_INFO, " color_trc:unknown");
  168. } else {
  169. av_log(ctx, AV_LOG_INFO, " color_trc:%s", color_trc_str);
  170. }
  171. av_log(ctx, AV_LOG_INFO, "\n");
  172. }
  173. static void update_sample_stats_8(const uint8_t *src, int len, int64_t *sum, int64_t *sum2)
  174. {
  175. int i;
  176. for (i = 0; i < len; i++) {
  177. *sum += src[i];
  178. *sum2 += src[i] * src[i];
  179. }
  180. }
  181. static void update_sample_stats_16(int be, const uint8_t *src, int len, int64_t *sum, int64_t *sum2)
  182. {
  183. const uint16_t *src1 = (const uint16_t *)src;
  184. int i;
  185. for (i = 0; i < len / 2; i++) {
  186. if ((HAVE_BIGENDIAN && !be) || (!HAVE_BIGENDIAN && be)) {
  187. *sum += av_bswap16(src1[i]);
  188. *sum2 += (uint32_t)av_bswap16(src1[i]) * (uint32_t)av_bswap16(src1[i]);
  189. } else {
  190. *sum += src1[i];
  191. *sum2 += (uint32_t)src1[i] * (uint32_t)src1[i];
  192. }
  193. }
  194. }
  195. static void update_sample_stats(int depth, int be, const uint8_t *src, int len, int64_t *sum, int64_t *sum2)
  196. {
  197. if (depth <= 8)
  198. update_sample_stats_8(src, len, sum, sum2);
  199. else
  200. update_sample_stats_16(be, src, len, sum, sum2);
  201. }
  202. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  203. {
  204. AVFilterContext *ctx = inlink->dst;
  205. ShowInfoContext *s = ctx->priv;
  206. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  207. uint32_t plane_checksum[4] = {0}, checksum = 0;
  208. int64_t sum[4] = {0}, sum2[4] = {0};
  209. int32_t pixelcount[4] = {0};
  210. int bitdepth = desc->comp[0].depth;
  211. int be = desc->flags & AV_PIX_FMT_FLAG_BE;
  212. int i, plane, vsub = desc->log2_chroma_h;
  213. for (plane = 0; plane < 4 && s->calculate_checksums && frame->data[plane] && frame->linesize[plane]; plane++) {
  214. uint8_t *data = frame->data[plane];
  215. int h = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
  216. int linesize = av_image_get_linesize(frame->format, frame->width, plane);
  217. int width = linesize >> (bitdepth > 8);
  218. if (linesize < 0)
  219. return linesize;
  220. for (i = 0; i < h; i++) {
  221. plane_checksum[plane] = av_adler32_update(plane_checksum[plane], data, linesize);
  222. checksum = av_adler32_update(checksum, data, linesize);
  223. update_sample_stats(bitdepth, be, data, linesize, sum+plane, sum2+plane);
  224. pixelcount[plane] += width;
  225. data += frame->linesize[plane];
  226. }
  227. }
  228. av_log(ctx, AV_LOG_INFO,
  229. "n:%4"PRId64" pts:%7s pts_time:%-7s pos:%9"PRId64" "
  230. "fmt:%s sar:%d/%d s:%dx%d i:%c iskey:%d type:%c ",
  231. inlink->frame_count_out,
  232. av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base), frame->pkt_pos,
  233. desc->name,
  234. frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
  235. frame->width, frame->height,
  236. !frame->interlaced_frame ? 'P' : /* Progressive */
  237. frame->top_field_first ? 'T' : 'B', /* Top / Bottom */
  238. frame->key_frame,
  239. av_get_picture_type_char(frame->pict_type));
  240. if (s->calculate_checksums) {
  241. av_log(ctx, AV_LOG_INFO,
  242. "checksum:%08"PRIX32" plane_checksum:[%08"PRIX32,
  243. checksum, plane_checksum[0]);
  244. for (plane = 1; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  245. av_log(ctx, AV_LOG_INFO, " %08"PRIX32, plane_checksum[plane]);
  246. av_log(ctx, AV_LOG_INFO, "] mean:[");
  247. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  248. av_log(ctx, AV_LOG_INFO, "%"PRId64" ", (sum[plane] + pixelcount[plane]/2) / pixelcount[plane]);
  249. av_log(ctx, AV_LOG_INFO, "\b] stdev:[");
  250. for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
  251. av_log(ctx, AV_LOG_INFO, "%3.1f ",
  252. sqrt((sum2[plane] - sum[plane]*(double)sum[plane]/pixelcount[plane])/pixelcount[plane]));
  253. av_log(ctx, AV_LOG_INFO, "\b]");
  254. }
  255. av_log(ctx, AV_LOG_INFO, "\n");
  256. for (i = 0; i < frame->nb_side_data; i++) {
  257. AVFrameSideData *sd = frame->side_data[i];
  258. av_log(ctx, AV_LOG_INFO, " side data - ");
  259. switch (sd->type) {
  260. case AV_FRAME_DATA_PANSCAN:
  261. av_log(ctx, AV_LOG_INFO, "pan/scan");
  262. break;
  263. case AV_FRAME_DATA_A53_CC:
  264. av_log(ctx, AV_LOG_INFO, "A/53 closed captions (%d bytes)", sd->size);
  265. break;
  266. case AV_FRAME_DATA_SPHERICAL:
  267. dump_spherical(ctx, frame, sd);
  268. break;
  269. case AV_FRAME_DATA_STEREO3D:
  270. dump_stereo3d(ctx, sd);
  271. break;
  272. case AV_FRAME_DATA_S12M_TIMECODE: {
  273. uint32_t *tc = (uint32_t*)sd->data;
  274. for (int j = 1; j <= tc[0]; j++) {
  275. char tcbuf[AV_TIMECODE_STR_SIZE];
  276. av_timecode_make_smpte_tc_string(tcbuf, tc[j], 0);
  277. av_log(ctx, AV_LOG_INFO, "timecode - %s%s", tcbuf, j != tc[0] ? ", " : "");
  278. }
  279. break;
  280. }
  281. case AV_FRAME_DATA_DISPLAYMATRIX:
  282. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  283. av_display_rotation_get((int32_t *)sd->data));
  284. break;
  285. case AV_FRAME_DATA_AFD:
  286. av_log(ctx, AV_LOG_INFO, "afd: value of %"PRIu8, sd->data[0]);
  287. break;
  288. case AV_FRAME_DATA_REGIONS_OF_INTEREST:
  289. dump_roi(ctx, sd);
  290. break;
  291. case AV_FRAME_DATA_MASTERING_DISPLAY_METADATA:
  292. dump_mastering_display(ctx, sd);
  293. break;
  294. case AV_FRAME_DATA_CONTENT_LIGHT_LEVEL:
  295. dump_content_light_metadata(ctx, sd);
  296. break;
  297. case AV_FRAME_DATA_GOP_TIMECODE: {
  298. char tcbuf[AV_TIMECODE_STR_SIZE];
  299. av_timecode_make_mpeg_tc_string(tcbuf, *(int64_t *)(sd->data));
  300. av_log(ctx, AV_LOG_INFO, "GOP timecode - %s", tcbuf);
  301. break;
  302. }
  303. default:
  304. av_log(ctx, AV_LOG_WARNING, "unknown side data type %d (%d bytes)",
  305. sd->type, sd->size);
  306. break;
  307. }
  308. av_log(ctx, AV_LOG_INFO, "\n");
  309. }
  310. dump_color_property(ctx, frame);
  311. return ff_filter_frame(inlink->dst->outputs[0], frame);
  312. }
  313. static int config_props(AVFilterContext *ctx, AVFilterLink *link, int is_out)
  314. {
  315. av_log(ctx, AV_LOG_INFO, "config %s time_base: %d/%d, frame_rate: %d/%d\n",
  316. is_out ? "out" : "in",
  317. link->time_base.num, link->time_base.den,
  318. link->frame_rate.num, link->frame_rate.den);
  319. return 0;
  320. }
  321. static int config_props_in(AVFilterLink *link)
  322. {
  323. AVFilterContext *ctx = link->dst;
  324. return config_props(ctx, link, 0);
  325. }
  326. static int config_props_out(AVFilterLink *link)
  327. {
  328. AVFilterContext *ctx = link->src;
  329. return config_props(ctx, link, 1);
  330. }
  331. static const AVFilterPad avfilter_vf_showinfo_inputs[] = {
  332. {
  333. .name = "default",
  334. .type = AVMEDIA_TYPE_VIDEO,
  335. .filter_frame = filter_frame,
  336. .config_props = config_props_in,
  337. },
  338. { NULL }
  339. };
  340. static const AVFilterPad avfilter_vf_showinfo_outputs[] = {
  341. {
  342. .name = "default",
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .config_props = config_props_out,
  345. },
  346. { NULL }
  347. };
  348. AVFilter ff_vf_showinfo = {
  349. .name = "showinfo",
  350. .description = NULL_IF_CONFIG_SMALL("Show textual information for each video frame."),
  351. .inputs = avfilter_vf_showinfo_inputs,
  352. .outputs = avfilter_vf_showinfo_outputs,
  353. .priv_size = sizeof(ShowInfoContext),
  354. .priv_class = &showinfo_class,
  355. };