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.

225 lines
7.3KB

  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * This file is part of Libav.
  4. *
  5. * Libav 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. * Libav 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 Libav; 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 "avfilter.h"
  32. #include "internal.h"
  33. #include "video.h"
  34. typedef struct ShowInfoContext {
  35. unsigned int frame;
  36. } ShowInfoContext;
  37. static void dump_spherical(AVFilterContext *ctx, AVFrame *frame, AVFrameSideData *sd)
  38. {
  39. AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
  40. double yaw, pitch, roll;
  41. av_log(ctx, AV_LOG_INFO, "spherical information: ");
  42. if (sd->size < sizeof(*spherical)) {
  43. av_log(ctx, AV_LOG_INFO, "invalid data");
  44. return;
  45. }
  46. if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR)
  47. av_log(ctx, AV_LOG_INFO, "equirectangular ");
  48. else if (spherical->projection == AV_SPHERICAL_CUBEMAP)
  49. av_log(ctx, AV_LOG_INFO, "cubemap ");
  50. else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE)
  51. av_log(ctx, AV_LOG_INFO, "tiled equirectangular ");
  52. else {
  53. av_log(ctx, AV_LOG_WARNING, "unknown");
  54. return;
  55. }
  56. yaw = ((double)spherical->yaw) / (1 << 16);
  57. pitch = ((double)spherical->pitch) / (1 << 16);
  58. roll = ((double)spherical->roll) / (1 << 16);
  59. av_log(ctx, AV_LOG_INFO, "(%f/%f/%f) ", yaw, pitch, roll);
  60. if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
  61. size_t l, t, r, b;
  62. av_spherical_tile_bounds(spherical, frame->width, frame->height,
  63. &l, &t, &r, &b);
  64. av_log(ctx, AV_LOG_INFO, "[%zu, %zu, %zu, %zu] ", l, t, r, b);
  65. } else if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
  66. av_log(ctx, AV_LOG_INFO, "[pad %"PRIu32"] ", spherical->padding);
  67. }
  68. }
  69. static void dump_stereo3d(AVFilterContext *ctx, AVFrameSideData *sd)
  70. {
  71. AVStereo3D *stereo;
  72. av_log(ctx, AV_LOG_INFO, "stereoscopic information: ");
  73. if (sd->size < sizeof(*stereo)) {
  74. av_log(ctx, AV_LOG_INFO, "invalid data");
  75. return;
  76. }
  77. stereo = (AVStereo3D *)sd->data;
  78. av_log(ctx, AV_LOG_INFO, "type - %s", av_stereo3d_type_name(stereo->type));
  79. if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
  80. av_log(ctx, AV_LOG_INFO, " (inverted)");
  81. }
  82. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  83. {
  84. AVFilterContext *ctx = inlink->dst;
  85. ShowInfoContext *showinfo = ctx->priv;
  86. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  87. uint32_t plane_checksum[4] = {0}, checksum = 0;
  88. int i, plane, vsub = desc->log2_chroma_h;
  89. for (plane = 0; frame->data[plane] && plane < 4; plane++) {
  90. uint8_t *data = frame->data[plane];
  91. int h = plane == 1 || plane == 2 ? inlink->h >> vsub : inlink->h;
  92. int linesize = av_image_get_linesize(frame->format, frame->width, plane);
  93. if (linesize < 0)
  94. return linesize;
  95. for (i = 0; i < h; i++) {
  96. plane_checksum[plane] = av_adler32_update(plane_checksum[plane], data, linesize);
  97. checksum = av_adler32_update(checksum, data, linesize);
  98. data += frame->linesize[plane];
  99. }
  100. }
  101. av_log(ctx, AV_LOG_INFO,
  102. "n:%d pts:%"PRId64" pts_time:%f "
  103. "fmt:%s sar:%d/%d s:%dx%d i:%c iskey:%d type:%c "
  104. "checksum:%"PRIu32" plane_checksum:[%"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32"]\n",
  105. showinfo->frame,
  106. frame->pts, frame->pts * av_q2d(inlink->time_base),
  107. desc->name,
  108. frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
  109. frame->width, frame->height,
  110. !frame->interlaced_frame ? 'P' : /* Progressive */
  111. frame->top_field_first ? 'T' : 'B', /* Top / Bottom */
  112. frame->key_frame,
  113. av_get_picture_type_char(frame->pict_type),
  114. checksum, plane_checksum[0], plane_checksum[1], plane_checksum[2], plane_checksum[3]);
  115. for (i = 0; i < frame->nb_side_data; i++) {
  116. AVFrameSideData *sd = frame->side_data[i];
  117. av_log(ctx, AV_LOG_INFO, " side data - ");
  118. switch (sd->type) {
  119. case AV_FRAME_DATA_PANSCAN:
  120. av_log(ctx, AV_LOG_INFO, "pan/scan");
  121. break;
  122. case AV_FRAME_DATA_A53_CC:
  123. av_log(ctx, AV_LOG_INFO, "A/53 closed captions (%d bytes)", sd->size);
  124. break;
  125. case AV_FRAME_DATA_SPHERICAL:
  126. dump_spherical(ctx, frame, sd);
  127. break;
  128. case AV_FRAME_DATA_STEREO3D:
  129. dump_stereo3d(ctx, sd);
  130. break;
  131. case AV_FRAME_DATA_DISPLAYMATRIX:
  132. av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
  133. av_display_rotation_get((int32_t *)sd->data));
  134. break;
  135. case AV_FRAME_DATA_AFD:
  136. av_log(ctx, AV_LOG_INFO, "afd: value of %"PRIu8, sd->data[0]);
  137. break;
  138. default:
  139. av_log(ctx, AV_LOG_WARNING, "unknown side data type %d (%d bytes)",
  140. sd->type, sd->size);
  141. break;
  142. }
  143. av_log(ctx, AV_LOG_INFO, "\n");
  144. }
  145. showinfo->frame++;
  146. return ff_filter_frame(inlink->dst->outputs[0], frame);
  147. }
  148. static int config_props(AVFilterContext *ctx, AVFilterLink *link, int is_out)
  149. {
  150. av_log(ctx, AV_LOG_INFO, "config %s time_base: %d/%d, frame_rate: %d/%d\n",
  151. is_out ? "out" : "in",
  152. link->time_base.num, link->time_base.den,
  153. link->frame_rate.num, link->frame_rate.den);
  154. return 0;
  155. }
  156. static int config_props_in(AVFilterLink *link)
  157. {
  158. AVFilterContext *ctx = link->dst;
  159. return config_props(ctx, link, 0);
  160. }
  161. static int config_props_out(AVFilterLink *link)
  162. {
  163. AVFilterContext *ctx = link->src;
  164. return config_props(ctx, link, 1);
  165. }
  166. static const AVFilterPad avfilter_vf_showinfo_inputs[] = {
  167. {
  168. .name = "default",
  169. .type = AVMEDIA_TYPE_VIDEO,
  170. .get_video_buffer = ff_null_get_video_buffer,
  171. .filter_frame = filter_frame,
  172. .config_props = config_props_in,
  173. },
  174. { NULL }
  175. };
  176. static const AVFilterPad avfilter_vf_showinfo_outputs[] = {
  177. {
  178. .name = "default",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .config_props = config_props_out,
  181. },
  182. { NULL }
  183. };
  184. AVFilter ff_vf_showinfo = {
  185. .name = "showinfo",
  186. .description = NULL_IF_CONFIG_SMALL("Show textual information for each video frame."),
  187. .priv_size = sizeof(ShowInfoContext),
  188. .inputs = avfilter_vf_showinfo_inputs,
  189. .outputs = avfilter_vf_showinfo_outputs,
  190. };