libavfilter/vf_showinfo.c
31d8467d
 /*
  * Copyright (c) 2011 Stefano Sabatini
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
ee42716b
  * filter for showing textual video frame information
31d8467d
  */
 
7513234b
 #include <inttypes.h>
 
31d8467d
 #include "libavutil/adler32.h"
5a69ded1
 #include "libavutil/display.h"
31d8467d
 #include "libavutil/imgutils.h"
1d9c2dc8
 #include "libavutil/internal.h"
31d8467d
 #include "libavutil/pixdesc.h"
883ce264
 #include "libavutil/spherical.h"
b4679ecd
 #include "libavutil/stereo3d.h"
175ab5f9
 #include "libavutil/timestamp.h"
b4679ecd
 
31d8467d
 #include "avfilter.h"
6b55aab0
 #include "internal.h"
c04c533f
 #include "video.h"
31d8467d
 
883ce264
 static void dump_spherical(AVFilterContext *ctx, AVFrame *frame, AVFrameSideData *sd)
 {
     AVSphericalMapping *spherical = (AVSphericalMapping *)sd->data;
     double yaw, pitch, roll;
 
     av_log(ctx, AV_LOG_INFO, "spherical information: ");
     if (sd->size < sizeof(*spherical)) {
         av_log(ctx, AV_LOG_INFO, "invalid data");
         return;
     }
 
     if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR)
         av_log(ctx, AV_LOG_INFO, "equirectangular ");
     else if (spherical->projection == AV_SPHERICAL_CUBEMAP)
         av_log(ctx, AV_LOG_INFO, "cubemap ");
     else if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE)
         av_log(ctx, AV_LOG_INFO, "tiled equirectangular ");
     else {
         av_log(ctx, AV_LOG_WARNING, "unknown");
         return;
     }
 
     yaw = ((double)spherical->yaw) / (1 << 16);
     pitch = ((double)spherical->pitch) / (1 << 16);
     roll = ((double)spherical->roll) / (1 << 16);
     av_log(ctx, AV_LOG_INFO, "(%f/%f/%f) ", yaw, pitch, roll);
 
     if (spherical->projection == AV_SPHERICAL_EQUIRECTANGULAR_TILE) {
         size_t l, t, r, b;
         av_spherical_tile_bounds(spherical, frame->width, frame->height,
                                  &l, &t, &r, &b);
9b79c65e
         av_log(ctx, AV_LOG_INFO,
                "[%"SIZE_SPECIFIER", %"SIZE_SPECIFIER", %"SIZE_SPECIFIER", %"SIZE_SPECIFIER"] ",
                l, t, r, b);
883ce264
     } else if (spherical->projection == AV_SPHERICAL_CUBEMAP) {
         av_log(ctx, AV_LOG_INFO, "[pad %"PRIu32"] ", spherical->padding);
     }
 }
 
b4679ecd
 static void dump_stereo3d(AVFilterContext *ctx, AVFrameSideData *sd)
 {
     AVStereo3D *stereo;
 
     av_log(ctx, AV_LOG_INFO, "stereoscopic information: ");
     if (sd->size < sizeof(*stereo)) {
         av_log(ctx, AV_LOG_INFO, "invalid data");
         return;
     }
 
     stereo = (AVStereo3D *)sd->data;
 
3f128fc4
     av_log(ctx, AV_LOG_INFO, "type - %s", av_stereo3d_type_name(stereo->type));
b4679ecd
 
     if (stereo->flags & AV_STEREO3D_FLAG_INVERT)
         av_log(ctx, AV_LOG_INFO, " (inverted)");
 }
 
09725c1d
 static void update_sample_stats(const uint8_t *src, int len, int64_t *sum, int64_t *sum2)
 {
     int i;
 
     for (i = 0; i < len; i++) {
         *sum += src[i];
         *sum2 += src[i] * src[i];
     }
 }
 
7e350379
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
31d8467d
 {
     AVFilterContext *ctx = inlink->dst;
59ee9f78
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
a05d0207
     uint32_t plane_checksum[4] = {0}, checksum = 0;
09725c1d
     int64_t sum[4] = {0}, sum2[4] = {0};
     int32_t pixelcount[4] = {0};
59ee9f78
     int i, plane, vsub = desc->log2_chroma_h;
31d8467d
 
e43a0a23
     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
0663a9ea
         uint8_t *data = frame->data[plane];
21f94684
         int h = plane == 1 || plane == 2 ? AV_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
6d96c7ea
         int linesize = av_image_get_linesize(frame->format, frame->width, plane);
bb82ea79
 
2aecfd4f
         if (linesize < 0)
             return linesize;
 
bb82ea79
         for (i = 0; i < h; i++) {
a05d0207
             plane_checksum[plane] = av_adler32_update(plane_checksum[plane], data, linesize);
             checksum = av_adler32_update(checksum, data, linesize);
09725c1d
 
             update_sample_stats(data, linesize, sum+plane, sum2+plane);
             pixelcount[plane] += linesize;
0663a9ea
             data += frame->linesize[plane];
bb82ea79
         }
31d8467d
     }
 
     av_log(ctx, AV_LOG_INFO,
44d34772
            "n:%4"PRId64" pts:%7s pts_time:%-7s pos:%9"PRId64" "
31d8467d
            "fmt:%s sar:%d/%d s:%dx%d i:%c iskey:%d type:%c "
7ef8d97b
            "checksum:%08"PRIX32" plane_checksum:[%08"PRIX32,
183ce55b
            inlink->frame_count_out,
6af050d7
            av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base), frame->pkt_pos,
59ee9f78
            desc->name,
7e350379
            frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
            frame->width, frame->height,
            !frame->interlaced_frame ? 'P' :         /* Progressive  */
            frame->top_field_first   ? 'T' : 'B',    /* Top / Bottom */
            frame->key_frame,
            av_get_picture_type_char(frame->pict_type),
1e6408a1
            checksum, plane_checksum[0]);
 
e43a0a23
     for (plane = 1; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
7ef8d97b
         av_log(ctx, AV_LOG_INFO, " %08"PRIX32, plane_checksum[plane]);
09725c1d
     av_log(ctx, AV_LOG_INFO, "] mean:[");
     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
         av_log(ctx, AV_LOG_INFO, "%"PRId64" ", (sum[plane] + pixelcount[plane]/2) / pixelcount[plane]);
     av_log(ctx, AV_LOG_INFO, "\b] stdev:[");
     for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++)
         av_log(ctx, AV_LOG_INFO, "%3.1f ",
                sqrt((sum2[plane] - sum[plane]*(double)sum[plane]/pixelcount[plane])/pixelcount[plane]));
     av_log(ctx, AV_LOG_INFO, "\b]\n");
31d8467d
 
b4679ecd
     for (i = 0; i < frame->nb_side_data; i++) {
         AVFrameSideData *sd = frame->side_data[i];
 
         av_log(ctx, AV_LOG_INFO, "  side data - ");
         switch (sd->type) {
         case AV_FRAME_DATA_PANSCAN:
             av_log(ctx, AV_LOG_INFO, "pan/scan");
             break;
         case AV_FRAME_DATA_A53_CC:
             av_log(ctx, AV_LOG_INFO, "A/53 closed captions (%d bytes)", sd->size);
             break;
883ce264
         case AV_FRAME_DATA_SPHERICAL:
             dump_spherical(ctx, frame, sd);
             break;
b4679ecd
         case AV_FRAME_DATA_STEREO3D:
             dump_stereo3d(ctx, sd);
             break;
5a69ded1
         case AV_FRAME_DATA_DISPLAYMATRIX:
             av_log(ctx, AV_LOG_INFO, "displaymatrix: rotation of %.2f degrees",
                    av_display_rotation_get((int32_t *)sd->data));
             break;
2a3c36e9
         case AV_FRAME_DATA_AFD:
1ef9e837
             av_log(ctx, AV_LOG_INFO, "afd: value of %"PRIu8, sd->data[0]);
2a3c36e9
             break;
b4679ecd
         default:
             av_log(ctx, AV_LOG_WARNING, "unknown side data type %d (%d bytes)",
                    sd->type, sd->size);
             break;
         }
 
         av_log(ctx, AV_LOG_INFO, "\n");
     }
 
0663a9ea
     return ff_filter_frame(inlink->dst->outputs[0], frame);
31d8467d
 }
 
fdb93996
 static int config_props(AVFilterContext *ctx, AVFilterLink *link, int is_out)
 {
 
     av_log(ctx, AV_LOG_INFO, "config %s time_base: %d/%d, frame_rate: %d/%d\n",
1339009c
            is_out ? "out" : "in",
fdb93996
            link->time_base.num, link->time_base.den,
1339009c
            link->frame_rate.num, link->frame_rate.den);
fdb93996
 
     return 0;
 }
 
 static int config_props_in(AVFilterLink *link)
 {
     AVFilterContext *ctx = link->dst;
     return config_props(ctx, link, 0);
 }
 
 static int config_props_out(AVFilterLink *link)
 {
     AVFilterContext *ctx = link->src;
     return config_props(ctx, link, 1);
 }
 
568c70e7
 static const AVFilterPad avfilter_vf_showinfo_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
565e4993
         .filter_frame     = filter_frame,
1339009c
         .config_props     = config_props_in,
568c70e7
     },
     { NULL }
 };
 
 static const AVFilterPad avfilter_vf_showinfo_outputs[] = {
     {
         .name = "default",
fdb93996
         .type = AVMEDIA_TYPE_VIDEO,
         .config_props  = config_props_out,
568c70e7
     },
     { NULL }
 };
 
cd43ca04
 AVFilter ff_vf_showinfo = {
31d8467d
     .name        = "showinfo",
     .description = NULL_IF_CONFIG_SMALL("Show textual information for each video frame."),
b211607b
     .inputs      = avfilter_vf_showinfo_inputs,
     .outputs     = avfilter_vf_showinfo_outputs,
31d8467d
 };