| 1 | /* |
| 2 | * Copyright (c) 2011 Stefano Sabatini |
| 3 | * |
| 4 | * This file is part of FFmpeg. |
| 5 | * |
| 6 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; 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/timestamp.h" |
| 38 | #include "libavutil/samplefmt.h" |
| 39 | |
| 40 | #include "audio.h" |
| 41 | #include "avfilter.h" |
| 42 | #include "internal.h" |
| 43 | |
| 44 | typedef struct AShowInfoContext { |
| 45 | /** |
| 46 | * Scratch space for individual plane checksums for planar audio |
| 47 | */ |
| 48 | uint32_t *plane_checksums; |
| 49 | } AShowInfoContext; |
| 50 | |
| 51 | static av_cold void uninit(AVFilterContext *ctx) |
| 52 | { |
| 53 | AShowInfoContext *s = ctx->priv; |
| 54 | av_freep(&s->plane_checksums); |
| 55 | } |
| 56 | |
| 57 | static void dump_matrixenc(AVFilterContext *ctx, AVFrameSideData *sd) |
| 58 | { |
| 59 | enum AVMatrixEncoding enc; |
| 60 | |
| 61 | av_log(ctx, AV_LOG_INFO, "matrix encoding: "); |
| 62 | |
| 63 | if (sd->size < sizeof(enum AVMatrixEncoding)) { |
| 64 | av_log(ctx, AV_LOG_INFO, "invalid data"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | enc = *(enum AVMatrixEncoding *)sd->data; |
| 69 | switch (enc) { |
| 70 | case AV_MATRIX_ENCODING_NONE: av_log(ctx, AV_LOG_INFO, "none"); break; |
| 71 | case AV_MATRIX_ENCODING_DOLBY: av_log(ctx, AV_LOG_INFO, "Dolby Surround"); break; |
| 72 | case AV_MATRIX_ENCODING_DPLII: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic II"); break; |
| 73 | case AV_MATRIX_ENCODING_DPLIIX: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic IIx"); break; |
| 74 | case AV_MATRIX_ENCODING_DPLIIZ: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic IIz"); break; |
| 75 | case AV_MATRIX_ENCODING_DOLBYEX: av_log(ctx, AV_LOG_INFO, "Dolby EX"); break; |
| 76 | case AV_MATRIX_ENCODING_DOLBYHEADPHONE: av_log(ctx, AV_LOG_INFO, "Dolby Headphone"); break; |
| 77 | default: av_log(ctx, AV_LOG_WARNING, "unknown"); break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void dump_downmix(AVFilterContext *ctx, AVFrameSideData *sd) |
| 82 | { |
| 83 | AVDownmixInfo *di; |
| 84 | |
| 85 | av_log(ctx, AV_LOG_INFO, "downmix: "); |
| 86 | if (sd->size < sizeof(*di)) { |
| 87 | av_log(ctx, AV_LOG_INFO, "invalid data"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | di = (AVDownmixInfo *)sd->data; |
| 92 | |
| 93 | av_log(ctx, AV_LOG_INFO, "preferred downmix type - "); |
| 94 | switch (di->preferred_downmix_type) { |
| 95 | case AV_DOWNMIX_TYPE_LORO: av_log(ctx, AV_LOG_INFO, "Lo/Ro"); break; |
| 96 | case AV_DOWNMIX_TYPE_LTRT: av_log(ctx, AV_LOG_INFO, "Lt/Rt"); break; |
| 97 | case AV_DOWNMIX_TYPE_DPLII: av_log(ctx, AV_LOG_INFO, "Dolby Pro Logic II"); break; |
| 98 | default: av_log(ctx, AV_LOG_WARNING, "unknown"); break; |
| 99 | } |
| 100 | |
| 101 | av_log(ctx, AV_LOG_INFO, " Mix levels: center %f (%f ltrt) - " |
| 102 | "surround %f (%f ltrt) - lfe %f", |
| 103 | di->center_mix_level, di->center_mix_level_ltrt, |
| 104 | di->surround_mix_level, di->surround_mix_level_ltrt, |
| 105 | di->lfe_mix_level); |
| 106 | } |
| 107 | |
| 108 | static void print_gain(AVFilterContext *ctx, const char *str, int32_t gain) |
| 109 | { |
| 110 | av_log(ctx, AV_LOG_INFO, "%s - ", str); |
| 111 | if (gain == INT32_MIN) |
| 112 | av_log(ctx, AV_LOG_INFO, "unknown"); |
| 113 | else |
| 114 | av_log(ctx, AV_LOG_INFO, "%f", gain / 100000.0f); |
| 115 | av_log(ctx, AV_LOG_INFO, ", "); |
| 116 | } |
| 117 | |
| 118 | static void print_peak(AVFilterContext *ctx, const char *str, uint32_t peak) |
| 119 | { |
| 120 | av_log(ctx, AV_LOG_INFO, "%s - ", str); |
| 121 | if (!peak) |
| 122 | av_log(ctx, AV_LOG_INFO, "unknown"); |
| 123 | else |
| 124 | av_log(ctx, AV_LOG_INFO, "%f", (float)peak / UINT32_MAX); |
| 125 | av_log(ctx, AV_LOG_INFO, ", "); |
| 126 | } |
| 127 | |
| 128 | static void dump_replaygain(AVFilterContext *ctx, AVFrameSideData *sd) |
| 129 | { |
| 130 | AVReplayGain *rg; |
| 131 | |
| 132 | av_log(ctx, AV_LOG_INFO, "replaygain: "); |
| 133 | if (sd->size < sizeof(*rg)) { |
| 134 | av_log(ctx, AV_LOG_INFO, "invalid data"); |
| 135 | return; |
| 136 | } |
| 137 | rg = (AVReplayGain*)sd->data; |
| 138 | |
| 139 | print_gain(ctx, "track gain", rg->track_gain); |
| 140 | print_peak(ctx, "track peak", rg->track_peak); |
| 141 | print_gain(ctx, "album gain", rg->album_gain); |
| 142 | print_peak(ctx, "album peak", rg->album_peak); |
| 143 | } |
| 144 | |
| 145 | static void dump_unknown(AVFilterContext *ctx, AVFrameSideData *sd) |
| 146 | { |
| 147 | av_log(ctx, AV_LOG_INFO, "unknown side data type: %d, size %d bytes", sd->type, sd->size); |
| 148 | } |
| 149 | |
| 150 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
| 151 | { |
| 152 | AVFilterContext *ctx = inlink->dst; |
| 153 | AShowInfoContext *s = ctx->priv; |
| 154 | char chlayout_str[128]; |
| 155 | uint32_t checksum = 0; |
| 156 | int channels = inlink->channels; |
| 157 | int planar = av_sample_fmt_is_planar(buf->format); |
| 158 | int block_align = av_get_bytes_per_sample(buf->format) * (planar ? 1 : channels); |
| 159 | int data_size = buf->nb_samples * block_align; |
| 160 | int planes = planar ? channels : 1; |
| 161 | int i; |
| 162 | void *tmp_ptr = av_realloc(s->plane_checksums, channels * sizeof(*s->plane_checksums)); |
| 163 | |
| 164 | if (!tmp_ptr) |
| 165 | return AVERROR(ENOMEM); |
| 166 | s->plane_checksums = tmp_ptr; |
| 167 | |
| 168 | for (i = 0; i < planes; i++) { |
| 169 | uint8_t *data = buf->extended_data[i]; |
| 170 | |
| 171 | s->plane_checksums[i] = av_adler32_update(0, data, data_size); |
| 172 | checksum = i ? av_adler32_update(checksum, data, data_size) : |
| 173 | s->plane_checksums[0]; |
| 174 | } |
| 175 | |
| 176 | av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), -1, |
| 177 | buf->channel_layout); |
| 178 | |
| 179 | av_log(ctx, AV_LOG_INFO, |
| 180 | "n:%"PRId64" pts:%s pts_time:%s pos:%"PRId64" " |
| 181 | "fmt:%s channels:%d chlayout:%s rate:%d nb_samples:%d " |
| 182 | "checksum:%08"PRIX32" ", |
| 183 | inlink->frame_count, |
| 184 | av_ts2str(buf->pts), av_ts2timestr(buf->pts, &inlink->time_base), |
| 185 | av_frame_get_pkt_pos(buf), |
| 186 | av_get_sample_fmt_name(buf->format), av_frame_get_channels(buf), chlayout_str, |
| 187 | buf->sample_rate, buf->nb_samples, |
| 188 | checksum); |
| 189 | |
| 190 | av_log(ctx, AV_LOG_INFO, "plane_checksums: [ "); |
| 191 | for (i = 0; i < planes; i++) |
| 192 | av_log(ctx, AV_LOG_INFO, "%08"PRIX32" ", s->plane_checksums[i]); |
| 193 | av_log(ctx, AV_LOG_INFO, "]\n"); |
| 194 | |
| 195 | for (i = 0; i < buf->nb_side_data; i++) { |
| 196 | AVFrameSideData *sd = buf->side_data[i]; |
| 197 | |
| 198 | av_log(ctx, AV_LOG_INFO, " side data - "); |
| 199 | switch (sd->type) { |
| 200 | case AV_FRAME_DATA_MATRIXENCODING: dump_matrixenc (ctx, sd); break; |
| 201 | case AV_FRAME_DATA_DOWNMIX_INFO: dump_downmix (ctx, sd); break; |
| 202 | case AV_FRAME_DATA_REPLAYGAIN: dump_replaygain(ctx, sd); break; |
| 203 | default: dump_unknown (ctx, sd); break; |
| 204 | } |
| 205 | |
| 206 | av_log(ctx, AV_LOG_INFO, "\n"); |
| 207 | } |
| 208 | |
| 209 | return ff_filter_frame(inlink->dst->outputs[0], buf); |
| 210 | } |
| 211 | |
| 212 | static const AVFilterPad inputs[] = { |
| 213 | { |
| 214 | .name = "default", |
| 215 | .type = AVMEDIA_TYPE_AUDIO, |
| 216 | .filter_frame = filter_frame, |
| 217 | }, |
| 218 | { NULL } |
| 219 | }; |
| 220 | |
| 221 | static const AVFilterPad outputs[] = { |
| 222 | { |
| 223 | .name = "default", |
| 224 | .type = AVMEDIA_TYPE_AUDIO, |
| 225 | }, |
| 226 | { NULL } |
| 227 | }; |
| 228 | |
| 229 | AVFilter ff_af_ashowinfo = { |
| 230 | .name = "ashowinfo", |
| 231 | .description = NULL_IF_CONFIG_SMALL("Show textual information for each audio frame."), |
| 232 | .priv_size = sizeof(AShowInfoContext), |
| 233 | .uninit = uninit, |
| 234 | .inputs = inputs, |
| 235 | .outputs = outputs, |
| 236 | }; |