2 * Copyright (c) 2011 Stefano Sabatini
4 * This file is part of Libav.
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.
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.
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
23 * filter for showing textual audio frame information
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"
43 typedef struct AShowInfoContext {
45 * Scratch space for individual plane checksums for planar audio
47 uint32_t *plane_checksums;
55 static int config_input(AVFilterLink *inlink)
57 AShowInfoContext *s = inlink->dst->priv;
58 int channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
59 s->plane_checksums = av_malloc(channels * sizeof(*s->plane_checksums));
60 if (!s->plane_checksums)
61 return AVERROR(ENOMEM);
66 static av_cold void uninit(AVFilterContext *ctx)
68 AShowInfoContext *s = ctx->priv;
69 av_freep(&s->plane_checksums);
72 static void dump_matrixenc(AVFilterContext *ctx, AVFrameSideData *sd)
74 enum AVMatrixEncoding enc;
76 av_log(ctx, AV_LOG_INFO, "matrix encoding: ");
78 if (sd->size < sizeof(enum AVMatrixEncoding)) {
79 av_log(ctx, AV_LOG_INFO, "invalid data");
83 enc = *(enum AVMatrixEncoding *)sd->data;
85 case AV_MATRIX_ENCODING_NONE: av_log(ctx, AV_LOG_INFO, "none"); break;
86 case AV_MATRIX_ENCODING_DOLBY: av_log(ctx, AV_LOG_INFO, "Dolby Surround"); break;
87 case AV_MATRIX_ENCODING_DPLII: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic II"); break;
88 case AV_MATRIX_ENCODING_DPLIIX: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic IIx"); break;
89 case AV_MATRIX_ENCODING_DPLIIZ: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic IIz"); break;
90 case AV_MATRIX_ENCODING_DOLBYEX: av_log(ctx, AV_LOG_INFO, "Dolby EX"); break;
91 case AV_MATRIX_ENCODING_DOLBYHEADPHONE: av_log(ctx, AV_LOG_INFO, "Dolby Headphone"); break;
92 default: av_log(ctx, AV_LOG_WARNING, "unknown"); break;
96 static void dump_downmix(AVFilterContext *ctx, AVFrameSideData *sd)
100 av_log(ctx, AV_LOG_INFO, "downmix: ");
101 if (sd->size < sizeof(*di)) {
102 av_log(ctx, AV_LOG_INFO, "invalid data");
106 di = (AVDownmixInfo *)sd->data;
108 av_log(ctx, AV_LOG_INFO, "preferred downmix type - ");
109 switch (di->preferred_downmix_type) {
110 case AV_DOWNMIX_TYPE_LORO: av_log(ctx, AV_LOG_INFO, "Lo/Ro"); break;
111 case AV_DOWNMIX_TYPE_LTRT: av_log(ctx, AV_LOG_INFO, "Lt/Rt"); break;
112 case AV_DOWNMIX_TYPE_DPLII: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic II"); break;
113 default: av_log(ctx, AV_LOG_WARNING, "unknown"); break;
116 av_log(ctx, AV_LOG_INFO, " Mix levels: center %f (%f ltrt) - "
117 "surround %f (%f ltrt) - lfe %f",
118 di->center_mix_level, di->center_mix_level_ltrt,
119 di->surround_mix_level, di->surround_mix_level_ltrt,
123 static void print_gain(AVFilterContext *ctx, const char *str, int32_t gain)
125 av_log(ctx, AV_LOG_INFO, "%s - ", str);
126 if (gain == INT32_MIN)
127 av_log(ctx, AV_LOG_INFO, "unknown");
129 av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f);
130 av_log(ctx, AV_LOG_INFO, ", ");
133 static void print_peak(AVFilterContext *ctx, const char *str, uint32_t peak)
135 av_log(ctx, AV_LOG_INFO, "%s - ", str);
137 av_log(ctx, AV_LOG_INFO, "unknown");
139 av_log(ctx, AV_LOG_INFO, "%f", (float)peak / UINT32_MAX);
140 av_log(ctx, AV_LOG_INFO, ", ");
143 static void dump_replaygain(AVFilterContext *ctx, AVFrameSideData *sd)
147 av_log(ctx, AV_LOG_INFO, "replaygain: ");
148 if (sd->size < sizeof(*rg)) {
149 av_log(ctx, AV_LOG_INFO, "invalid data");
152 rg = (AVReplayGain*)sd->data;
154 print_gain(ctx, "track gain", rg->track_gain);
155 print_peak(ctx, "track peak", rg->track_peak);
156 print_gain(ctx, "album gain", rg->album_gain);
157 print_peak(ctx, "album peak", rg->album_peak);
160 static void dump_unknown(AVFilterContext *ctx, AVFrameSideData *sd)
162 av_log(ctx, AV_LOG_INFO, "unknown side data type: %d, size %d bytes", sd->type, sd->size);
165 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
167 AVFilterContext *ctx = inlink->dst;
168 AShowInfoContext *s = ctx->priv;
169 char chlayout_str[128];
170 uint32_t checksum = 0;
171 int channels = av_get_channel_layout_nb_channels(buf->channel_layout);
172 int planar = av_sample_fmt_is_planar(buf->format);
173 int block_align = av_get_bytes_per_sample(buf->format) * (planar ? 1 : channels);
174 int data_size = buf->nb_samples * block_align;
175 int planes = planar ? channels : 1;
178 for (i = 0; i < planes; i++) {
179 uint8_t *data = buf->extended_data[i];
181 s->plane_checksums[i] = av_adler32_update(0, data, data_size);
182 checksum = i ? av_adler32_update(checksum, data, data_size) :
183 s->plane_checksums[0];
186 av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), -1,
187 buf->channel_layout);
189 av_log(ctx, AV_LOG_INFO,
190 "n:%"PRIu64" pts:%"PRId64" pts_time:%f "
191 "fmt:%s chlayout:%s rate:%d nb_samples:%d "
192 "checksum:%08"PRIX32" ",
193 s->frame, buf->pts, buf->pts * av_q2d(inlink->time_base),
194 av_get_sample_fmt_name(buf->format), chlayout_str,
195 buf->sample_rate, buf->nb_samples,
198 av_log(ctx, AV_LOG_INFO, "plane_checksums: [ ");
199 for (i = 0; i < planes; i++)
200 av_log(ctx, AV_LOG_INFO, "%08"PRIX32" ", s->plane_checksums[i]);
201 av_log(ctx, AV_LOG_INFO, "]\n");
203 for (i = 0; i < buf->nb_side_data; i++) {
204 AVFrameSideData *sd = buf->side_data[i];
206 av_log(ctx, AV_LOG_INFO, " side data - ");
208 case AV_FRAME_DATA_MATRIXENCODING: dump_matrixenc (ctx, sd); break;
209 case AV_FRAME_DATA_DOWNMIX_INFO: dump_downmix (ctx, sd); break;
210 case AV_FRAME_DATA_REPLAYGAIN: dump_replaygain(ctx, sd); break;
211 default: dump_unknown (ctx, sd); break;
214 av_log(ctx, AV_LOG_INFO, "\n");
218 return ff_filter_frame(inlink->dst->outputs[0], buf);
221 static const AVFilterPad inputs[] = {
224 .type = AVMEDIA_TYPE_AUDIO,
225 .get_audio_buffer = ff_null_get_audio_buffer,
226 .config_props = config_input,
227 .filter_frame = filter_frame,
232 static const AVFilterPad outputs[] = {
235 .type = AVMEDIA_TYPE_AUDIO,
240 AVFilter ff_af_ashowinfo = {
242 .description = NULL_IF_CONFIG_SMALL("Show textual information for each audio frame."),
243 .priv_size = sizeof(AShowInfoContext),