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