]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_ashowinfo.c
crypto: consistently use size_t as type for length parameters
[ffmpeg] / libavfilter / af_ashowinfo.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * filter for showing textual audio frame information
24  */
25
26 #include <inttypes.h>
27 #include <stddef.h>
28
29 #include "libavutil/adler32.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/common.h"
33 #include "libavutil/downmix_info.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/replaygain.h"
37 #include "libavutil/samplefmt.h"
38
39 #include "libavcodec/avcodec.h"
40
41 #include "audio.h"
42 #include "avfilter.h"
43 #include "internal.h"
44
45 typedef struct AShowInfoContext {
46     /**
47      * Scratch space for individual plane checksums for planar audio
48      */
49     uint32_t *plane_checksums;
50
51     /**
52      * Frame counter
53      */
54     uint64_t frame;
55 } AShowInfoContext;
56
57 static int config_input(AVFilterLink *inlink)
58 {
59     AShowInfoContext *s = inlink->dst->priv;
60     int channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
61     s->plane_checksums = av_malloc(channels * sizeof(*s->plane_checksums));
62     if (!s->plane_checksums)
63         return AVERROR(ENOMEM);
64
65     return 0;
66 }
67
68 static av_cold void uninit(AVFilterContext *ctx)
69 {
70     AShowInfoContext *s = ctx->priv;
71     av_freep(&s->plane_checksums);
72 }
73
74 static void dump_matrixenc(AVFilterContext *ctx, AVFrameSideData *sd)
75 {
76     enum AVMatrixEncoding enc;
77
78     av_log(ctx, AV_LOG_INFO, "matrix encoding: ");
79
80     if (sd->size < sizeof(enum AVMatrixEncoding)) {
81         av_log(ctx, AV_LOG_INFO, "invalid data");
82         return;
83     }
84
85     enc = *(enum AVMatrixEncoding *)sd->data;
86     switch (enc) {
87     case AV_MATRIX_ENCODING_NONE:           av_log(ctx, AV_LOG_INFO, "none");                break;
88     case AV_MATRIX_ENCODING_DOLBY:          av_log(ctx, AV_LOG_INFO, "Dolby Surround");      break;
89     case AV_MATRIX_ENCODING_DPLII:          av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic II");  break;
90     case AV_MATRIX_ENCODING_DPLIIX:         av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic IIx"); break;
91     case AV_MATRIX_ENCODING_DPLIIZ:         av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic IIz"); break;
92     case AV_MATRIX_ENCODING_DOLBYEX:        av_log(ctx, AV_LOG_INFO, "Dolby EX");            break;
93     case AV_MATRIX_ENCODING_DOLBYHEADPHONE: av_log(ctx, AV_LOG_INFO, "Dolby Headphone");     break;
94     default:                                av_log(ctx, AV_LOG_WARNING, "unknown");          break;
95     }
96 }
97
98 static void dump_downmix(AVFilterContext *ctx, AVFrameSideData *sd)
99 {
100     AVDownmixInfo *di;
101
102     av_log(ctx, AV_LOG_INFO, "downmix: ");
103     if (sd->size < sizeof(*di)) {
104         av_log(ctx, AV_LOG_INFO, "invalid data");
105         return;
106     }
107
108     di = (AVDownmixInfo *)sd->data;
109
110     av_log(ctx, AV_LOG_INFO, "preferred downmix type - ");
111     switch (di->preferred_downmix_type) {
112     case AV_DOWNMIX_TYPE_LORO:    av_log(ctx, AV_LOG_INFO, "Lo/Ro");              break;
113     case AV_DOWNMIX_TYPE_LTRT:    av_log(ctx, AV_LOG_INFO, "Lt/Rt");              break;
114     case AV_DOWNMIX_TYPE_DPLII:   av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic II"); break;
115     default:                      av_log(ctx, AV_LOG_WARNING, "unknown");         break;
116     }
117
118     av_log(ctx, AV_LOG_INFO, " Mix levels: center %f (%f ltrt) - "
119            "surround %f (%f ltrt) - lfe %f",
120            di->center_mix_level, di->center_mix_level_ltrt,
121            di->surround_mix_level, di->surround_mix_level_ltrt,
122            di->lfe_mix_level);
123 }
124
125 static void print_gain(AVFilterContext *ctx, const char *str, int32_t gain)
126 {
127     av_log(ctx, AV_LOG_INFO, "%s - ", str);
128     if (gain == INT32_MIN)
129         av_log(ctx, AV_LOG_INFO, "unknown");
130     else
131         av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
132     av_log(ctx, AV_LOG_INFO, ", ");
133 }
134
135 static void print_peak(AVFilterContext *ctx, const char *str, uint32_t peak)
136 {
137     av_log(ctx, AV_LOG_INFO, "%s - ", str);
138     if (!peak)
139         av_log(ctx, AV_LOG_INFO, "unknown");
140     else
141         av_log(ctx, AV_LOG_INFO, "%f", (float)peak / UINT32_MAX);
142     av_log(ctx, AV_LOG_INFO, ", ");
143 }
144
145 static void dump_replaygain(AVFilterContext *ctx, AVFrameSideData *sd)
146 {
147     AVReplayGain *rg;
148
149     av_log(ctx, AV_LOG_INFO, "replaygain: ");
150     if (sd->size < sizeof(*rg)) {
151         av_log(ctx, AV_LOG_INFO, "invalid data");
152         return;
153     }
154     rg = (AVReplayGain*)sd->data;
155
156     print_gain(ctx, "track gain", rg->track_gain);
157     print_peak(ctx, "track peak", rg->track_peak);
158     print_gain(ctx, "album gain", rg->album_gain);
159     print_peak(ctx, "album peak", rg->album_peak);
160 }
161
162 static void dump_audio_service_type(AVFilterContext *ctx, AVFrameSideData *sd)
163 {
164     enum AVAudioServiceType *ast;
165
166     av_log(ctx, AV_LOG_INFO, "audio service type: ");
167     if (sd->size < sizeof(*ast)) {
168         av_log(ctx, AV_LOG_INFO, "invalid data");
169         return;
170     }
171     ast = (enum AVAudioServiceType*)sd->data;
172     switch (*ast) {
173     case AV_AUDIO_SERVICE_TYPE_MAIN:              av_log(ctx, AV_LOG_INFO, "Main Audio Service"); break;
174     case AV_AUDIO_SERVICE_TYPE_EFFECTS:           av_log(ctx, AV_LOG_INFO, "Effects");            break;
175     case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED: av_log(ctx, AV_LOG_INFO, "Visually Impaired");  break;
176     case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:  av_log(ctx, AV_LOG_INFO, "Hearing Impaired");   break;
177     case AV_AUDIO_SERVICE_TYPE_DIALOGUE:          av_log(ctx, AV_LOG_INFO, "Dialogue");           break;
178     case AV_AUDIO_SERVICE_TYPE_COMMENTARY:        av_log(ctx, AV_LOG_INFO, "Commentary");         break;
179     case AV_AUDIO_SERVICE_TYPE_EMERGENCY:         av_log(ctx, AV_LOG_INFO, "Emergency");          break;
180     case AV_AUDIO_SERVICE_TYPE_VOICE_OVER:        av_log(ctx, AV_LOG_INFO, "Voice Over");         break;
181     case AV_AUDIO_SERVICE_TYPE_KARAOKE:           av_log(ctx, AV_LOG_INFO, "Karaoke");            break;
182     default:                                      av_log(ctx, AV_LOG_INFO, "unknown");            break;
183     }
184 }
185
186 static void dump_unknown(AVFilterContext *ctx, AVFrameSideData *sd)
187 {
188     av_log(ctx, AV_LOG_INFO, "unknown side data type: %d, size %d bytes", sd->type, sd->size);
189 }
190
191 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
192 {
193     AVFilterContext *ctx = inlink->dst;
194     AShowInfoContext *s  = ctx->priv;
195     char chlayout_str[128];
196     uint32_t checksum = 0;
197     int channels    = av_get_channel_layout_nb_channels(buf->channel_layout);
198     int planar      = av_sample_fmt_is_planar(buf->format);
199     int block_align = av_get_bytes_per_sample(buf->format) * (planar ? 1 : channels);
200     int data_size   = buf->nb_samples * block_align;
201     int planes      = planar ? channels : 1;
202     int i;
203
204     for (i = 0; i < planes; i++) {
205         uint8_t *data = buf->extended_data[i];
206
207         s->plane_checksums[i] = av_adler32_update(0, data, data_size);
208         checksum = i ? av_adler32_update(checksum, data, data_size) :
209                        s->plane_checksums[0];
210     }
211
212     av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), -1,
213                                  buf->channel_layout);
214
215     av_log(ctx, AV_LOG_INFO,
216            "n:%"PRIu64" pts:%"PRId64" pts_time:%f "
217            "fmt:%s chlayout:%s rate:%d nb_samples:%d "
218            "checksum:%08"PRIX32" ",
219            s->frame, buf->pts, buf->pts * av_q2d(inlink->time_base),
220            av_get_sample_fmt_name(buf->format), chlayout_str,
221            buf->sample_rate, buf->nb_samples,
222            checksum);
223
224     av_log(ctx, AV_LOG_INFO, "plane_checksums: [ ");
225     for (i = 0; i < planes; i++)
226         av_log(ctx, AV_LOG_INFO, "%08"PRIX32" ", s->plane_checksums[i]);
227     av_log(ctx, AV_LOG_INFO, "]\n");
228
229     for (i = 0; i < buf->nb_side_data; i++) {
230         AVFrameSideData *sd = buf->side_data[i];
231
232         av_log(ctx, AV_LOG_INFO, "  side data - ");
233         switch (sd->type) {
234         case AV_FRAME_DATA_MATRIXENCODING: dump_matrixenc (ctx, sd); break;
235         case AV_FRAME_DATA_DOWNMIX_INFO:   dump_downmix   (ctx, sd); break;
236         case AV_FRAME_DATA_REPLAYGAIN:     dump_replaygain(ctx, sd); break;
237         case AV_FRAME_DATA_AUDIO_SERVICE_TYPE: dump_audio_service_type(ctx, sd); break;
238         default:                           dump_unknown   (ctx, sd); break;
239         }
240
241         av_log(ctx, AV_LOG_INFO, "\n");
242     }
243
244     s->frame++;
245     return ff_filter_frame(inlink->dst->outputs[0], buf);
246 }
247
248 static const AVFilterPad inputs[] = {
249     {
250         .name       = "default",
251         .type             = AVMEDIA_TYPE_AUDIO,
252         .get_audio_buffer = ff_null_get_audio_buffer,
253         .config_props     = config_input,
254         .filter_frame     = filter_frame,
255     },
256     { NULL },
257 };
258
259 static const AVFilterPad outputs[] = {
260     {
261         .name = "default",
262         .type = AVMEDIA_TYPE_AUDIO,
263     },
264     { NULL },
265 };
266
267 AVFilter ff_af_ashowinfo = {
268     .name        = "ashowinfo",
269     .description = NULL_IF_CONFIG_SMALL("Show textual information for each audio frame."),
270     .priv_size   = sizeof(AShowInfoContext),
271     .uninit      = uninit,
272     .inputs      = inputs,
273     .outputs     = outputs,
274 };