avf_aphasemeter.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2015 Paul B Mahol
  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. * audio to video multimedia aphasemeter filter
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "audio.h"
  32. #include "video.h"
  33. #include "internal.h"
  34. typedef struct AudioPhaseMeterContext {
  35. const AVClass *class;
  36. AVFrame *out;
  37. int w, h;
  38. AVRational frame_rate;
  39. int contrast[4];
  40. uint8_t *mpc_str;
  41. uint8_t mpc[4];
  42. int draw_median_phase;
  43. } AudioPhaseMeterContext;
  44. #define OFFSET(x) offsetof(AudioPhaseMeterContext, x)
  45. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  46. static const AVOption aphasemeter_options[] = {
  47. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
  48. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
  49. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="800x400"}, 0, 0, FLAGS },
  50. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="800x400"}, 0, 0, FLAGS },
  51. { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=2}, 0, 255, FLAGS },
  52. { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=7}, 0, 255, FLAGS },
  53. { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
  54. { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(aphasemeter);
  58. static int query_formats(AVFilterContext *ctx)
  59. {
  60. AVFilterFormats *formats = NULL;
  61. AVFilterChannelLayouts *layout = NULL;
  62. AVFilterLink *inlink = ctx->inputs[0];
  63. AVFilterLink *outlink = ctx->outputs[0];
  64. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  65. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  66. int ret;
  67. formats = ff_make_format_list(sample_fmts);
  68. if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
  69. (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
  70. (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
  71. return ret;
  72. formats = ff_all_samplerates();
  73. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  74. return ret;
  75. formats = ff_make_format_list(pix_fmts);
  76. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  77. return ret;
  78. return 0;
  79. }
  80. static int config_input(AVFilterLink *inlink)
  81. {
  82. AVFilterContext *ctx = inlink->dst;
  83. AudioPhaseMeterContext *s = ctx->priv;
  84. int nb_samples;
  85. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  86. inlink->partial_buf_size =
  87. inlink->min_samples =
  88. inlink->max_samples = nb_samples;
  89. return 0;
  90. }
  91. static int config_output(AVFilterLink *outlink)
  92. {
  93. AVFilterContext *ctx = outlink->src;
  94. AudioPhaseMeterContext *s = ctx->priv;
  95. outlink->w = s->w;
  96. outlink->h = s->h;
  97. outlink->sample_aspect_ratio = (AVRational){1,1};
  98. outlink->frame_rate = s->frame_rate;
  99. if (!strcmp(s->mpc_str, "none"))
  100. s->draw_median_phase = 0;
  101. else if (av_parse_color(s->mpc, s->mpc_str, -1, ctx) >= 0)
  102. s->draw_median_phase = 1;
  103. else
  104. return AVERROR(EINVAL);
  105. return 0;
  106. }
  107. static inline int get_x(float phase, int w)
  108. {
  109. return (phase + 1.) / 2. * (w - 1);
  110. }
  111. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  112. {
  113. AVFilterContext *ctx = inlink->dst;
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. AudioPhaseMeterContext *s = ctx->priv;
  116. AVDictionary **metadata;
  117. const int rc = s->contrast[0];
  118. const int gc = s->contrast[1];
  119. const int bc = s->contrast[2];
  120. float fphase = 0;
  121. AVFrame *out;
  122. uint8_t *dst;
  123. int i;
  124. if (!s->out || s->out->width != outlink->w ||
  125. s->out->height != outlink->h) {
  126. av_frame_free(&s->out);
  127. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  128. if (!s->out) {
  129. av_frame_free(&in);
  130. return AVERROR(ENOMEM);
  131. }
  132. out = s->out;
  133. for (i = 0; i < outlink->h; i++)
  134. memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
  135. } else {
  136. out = s->out;
  137. for (i = outlink->h - 1; i >= 10; i--)
  138. memmove(out->data[0] + (i ) * out->linesize[0],
  139. out->data[0] + (i-1) * out->linesize[0],
  140. outlink->w * 4);
  141. for (i = 0; i < outlink->w; i++)
  142. AV_WL32(out->data[0] + i * 4, 0);
  143. }
  144. s->out->pts = in->pts;
  145. for (i = 0; i < in->nb_samples; i++) {
  146. const float *src = (float *)in->data[0] + i * 2;
  147. const float f = src[0] * src[1] / (src[0]*src[0] + src[1] * src[1]) * 2;
  148. const float phase = isnan(f) ? 1 : f;
  149. const int x = get_x(phase, s->w);
  150. dst = out->data[0] + x * 4;
  151. dst[0] = FFMIN(255, dst[0] + rc);
  152. dst[1] = FFMIN(255, dst[1] + gc);
  153. dst[2] = FFMIN(255, dst[2] + bc);
  154. dst[3] = 255;
  155. fphase += phase;
  156. }
  157. fphase /= in->nb_samples;
  158. if (s->draw_median_phase) {
  159. dst = out->data[0] + get_x(fphase, s->w) * 4;
  160. AV_WL32(dst, AV_RL32(s->mpc));
  161. }
  162. for (i = 1; i < 10 && i < outlink->h; i++)
  163. memcpy(out->data[0] + i * out->linesize[0], out->data[0], outlink->w * 4);
  164. metadata = avpriv_frame_get_metadatap(out);
  165. if (metadata) {
  166. uint8_t value[128];
  167. snprintf(value, sizeof(value), "%f", fphase);
  168. av_dict_set(metadata, "lavfi.aphasemeter.phase", value, 0);
  169. }
  170. av_frame_free(&in);
  171. return ff_filter_frame(outlink, av_frame_clone(s->out));
  172. }
  173. static av_cold void uninit(AVFilterContext *ctx)
  174. {
  175. AudioPhaseMeterContext *s = ctx->priv;
  176. av_frame_free(&s->out);
  177. }
  178. static const AVFilterPad inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_AUDIO,
  182. .config_props = config_input,
  183. .filter_frame = filter_frame,
  184. },
  185. { NULL }
  186. };
  187. static const AVFilterPad outputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .config_props = config_output,
  192. },
  193. { NULL }
  194. };
  195. AVFilter ff_avf_aphasemeter = {
  196. .name = "aphasemeter",
  197. .description = NULL_IF_CONFIG_SMALL("Convert input audio to phase meter video output."),
  198. .uninit = uninit,
  199. .query_formats = query_formats,
  200. .priv_size = sizeof(AudioPhaseMeterContext),
  201. .inputs = inputs,
  202. .outputs = outputs,
  203. .priv_class = &aphasemeter_class,
  204. };