You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.4KB

  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. formats = ff_make_format_list(sample_fmts);
  67. if (!formats)
  68. return AVERROR(ENOMEM);
  69. ff_formats_ref(formats, &inlink->out_formats);
  70. ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
  71. ff_channel_layouts_ref(layout, &inlink->out_channel_layouts);
  72. formats = ff_all_samplerates();
  73. if (!formats)
  74. return AVERROR(ENOMEM);
  75. ff_formats_ref(formats, &inlink->out_samplerates);
  76. formats = ff_make_format_list(pix_fmts);
  77. if (!formats)
  78. return AVERROR(ENOMEM);
  79. ff_formats_ref(formats, &outlink->in_formats);
  80. return 0;
  81. }
  82. static int config_input(AVFilterLink *inlink)
  83. {
  84. AVFilterContext *ctx = inlink->dst;
  85. AudioPhaseMeterContext *s = ctx->priv;
  86. int nb_samples;
  87. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  88. inlink->partial_buf_size =
  89. inlink->min_samples =
  90. inlink->max_samples = nb_samples;
  91. return 0;
  92. }
  93. static int config_output(AVFilterLink *outlink)
  94. {
  95. AVFilterContext *ctx = outlink->src;
  96. AudioPhaseMeterContext *s = ctx->priv;
  97. outlink->w = s->w;
  98. outlink->h = s->h;
  99. outlink->sample_aspect_ratio = (AVRational){1,1};
  100. outlink->frame_rate = s->frame_rate;
  101. if (!strcmp(s->mpc_str, "none"))
  102. s->draw_median_phase = 0;
  103. else if (av_parse_color(s->mpc, s->mpc_str, -1, ctx) >= 0)
  104. s->draw_median_phase = 1;
  105. else
  106. return AVERROR(EINVAL);
  107. return 0;
  108. }
  109. static inline int get_x(float phase, int w)
  110. {
  111. return (phase + 1.) / 2. * (w - 1);
  112. }
  113. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  114. {
  115. AVFilterContext *ctx = inlink->dst;
  116. AVFilterLink *outlink = ctx->outputs[0];
  117. AudioPhaseMeterContext *s = ctx->priv;
  118. AVDictionary **metadata;
  119. const int rc = s->contrast[0];
  120. const int gc = s->contrast[1];
  121. const int bc = s->contrast[2];
  122. float fphase = 0;
  123. AVFrame *out;
  124. uint8_t *dst;
  125. int i;
  126. if (!s->out || s->out->width != outlink->w ||
  127. s->out->height != outlink->h) {
  128. av_frame_free(&s->out);
  129. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  130. if (!s->out) {
  131. av_frame_free(&in);
  132. return AVERROR(ENOMEM);
  133. }
  134. out = s->out;
  135. for (i = 0; i < outlink->h; i++)
  136. memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
  137. } else {
  138. out = s->out;
  139. for (i = outlink->h - 1; i >= 10; i--)
  140. memmove(out->data[0] + (i ) * out->linesize[0],
  141. out->data[0] + (i-1) * out->linesize[0],
  142. outlink->w * 4);
  143. for (i = 0; i < outlink->w; i++)
  144. AV_WL32(out->data[0] + i * 4, 0);
  145. }
  146. s->out->pts = in->pts;
  147. for (i = 0; i < in->nb_samples; i++) {
  148. const float *src = (float *)in->data[0] + i * 2;
  149. const float f = src[0] * src[1] / (src[0]*src[0] + src[1] * src[1]) * 2;
  150. const float phase = isnan(f) ? 1 : f;
  151. const int x = get_x(phase, s->w);
  152. dst = out->data[0] + x * 4;
  153. dst[0] = FFMIN(255, dst[0] + rc);
  154. dst[1] = FFMIN(255, dst[1] + gc);
  155. dst[2] = FFMIN(255, dst[2] + bc);
  156. dst[3] = 255;
  157. fphase += phase;
  158. }
  159. fphase /= in->nb_samples;
  160. if (s->draw_median_phase) {
  161. dst = out->data[0] + get_x(fphase, s->w) * 4;
  162. AV_WL32(dst, AV_RL32(s->mpc));
  163. }
  164. for (i = 1; i < 10 && i < outlink->h; i++)
  165. memcpy(out->data[0] + i * out->linesize[0], out->data[0], outlink->w * 4);
  166. metadata = avpriv_frame_get_metadatap(out);
  167. if (metadata) {
  168. uint8_t value[128];
  169. snprintf(value, sizeof(value), "%f", fphase);
  170. av_dict_set(metadata, "lavfi.aphasemeter.phase", value, 0);
  171. }
  172. av_frame_free(&in);
  173. return ff_filter_frame(outlink, av_frame_clone(s->out));
  174. }
  175. static av_cold void uninit(AVFilterContext *ctx)
  176. {
  177. AudioPhaseMeterContext *s = ctx->priv;
  178. av_frame_free(&s->out);
  179. }
  180. static const AVFilterPad inputs[] = {
  181. {
  182. .name = "default",
  183. .type = AVMEDIA_TYPE_AUDIO,
  184. .config_props = config_input,
  185. .filter_frame = filter_frame,
  186. },
  187. { NULL }
  188. };
  189. static const AVFilterPad outputs[] = {
  190. {
  191. .name = "default",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .config_props = config_output,
  194. },
  195. { NULL }
  196. };
  197. AVFilter ff_avf_aphasemeter = {
  198. .name = "aphasemeter",
  199. .description = NULL_IF_CONFIG_SMALL("Convert input audio to phase meter video output."),
  200. .uninit = uninit,
  201. .query_formats = query_formats,
  202. .priv_size = sizeof(AudioPhaseMeterContext),
  203. .inputs = inputs,
  204. .outputs = outputs,
  205. .priv_class = &aphasemeter_class,
  206. };