af_ashowinfo.c 9.3 KB

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