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.

274 lines
8.4KB

  1. /*
  2. * Copyright (c) 2016 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. #include "libavutil/avassert.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/parseutils.h"
  25. #include "avfilter.h"
  26. #include "filters.h"
  27. #include "formats.h"
  28. #include "audio.h"
  29. #include "video.h"
  30. #include "internal.h"
  31. typedef struct AudioBitScopeContext {
  32. const AVClass *class;
  33. int w, h;
  34. AVRational frame_rate;
  35. char *colors;
  36. int nb_channels;
  37. int nb_samples;
  38. int depth;
  39. uint8_t *fg;
  40. uint64_t counter[64];
  41. } AudioBitScopeContext;
  42. #define OFFSET(x) offsetof(AudioBitScopeContext, x)
  43. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  44. static const AVOption abitscope_options[] = {
  45. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  46. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  47. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
  48. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="1024x256"}, 0, 0, FLAGS },
  49. { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
  50. { NULL }
  51. };
  52. AVFILTER_DEFINE_CLASS(abitscope);
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. AVFilterFormats *formats = NULL;
  56. AVFilterChannelLayouts *layouts;
  57. AVFilterLink *inlink = ctx->inputs[0];
  58. AVFilterLink *outlink = ctx->outputs[0];
  59. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE };
  60. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  61. int ret;
  62. formats = ff_make_format_list(sample_fmts);
  63. if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
  64. return ret;
  65. layouts = ff_all_channel_counts();
  66. if (!layouts)
  67. return AVERROR(ENOMEM);
  68. if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
  69. return ret;
  70. formats = ff_all_samplerates();
  71. if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
  72. return ret;
  73. formats = ff_make_format_list(pix_fmts);
  74. if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
  75. return ret;
  76. return 0;
  77. }
  78. static int config_input(AVFilterLink *inlink)
  79. {
  80. AVFilterContext *ctx = inlink->dst;
  81. AudioBitScopeContext *s = ctx->priv;
  82. int ch;
  83. char *colors, *saveptr = NULL;
  84. s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
  85. s->nb_channels = inlink->channels;
  86. s->depth = inlink->format == AV_SAMPLE_FMT_S16P ? 16 : 32;
  87. s->fg = av_malloc_array(s->nb_channels, 4 * sizeof(*s->fg));
  88. if (!s->fg)
  89. return AVERROR(ENOMEM);
  90. colors = av_strdup(s->colors);
  91. if (!colors)
  92. return AVERROR(ENOMEM);
  93. for (ch = 0; ch < s->nb_channels; ch++) {
  94. uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
  95. char *color;
  96. color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
  97. if (color)
  98. av_parse_color(fg, color, -1, ctx);
  99. s->fg[4 * ch + 0] = fg[0];
  100. s->fg[4 * ch + 1] = fg[1];
  101. s->fg[4 * ch + 2] = fg[2];
  102. s->fg[4 * ch + 3] = fg[3];
  103. }
  104. av_free(colors);
  105. return 0;
  106. }
  107. static int config_output(AVFilterLink *outlink)
  108. {
  109. AudioBitScopeContext *s = outlink->src->priv;
  110. outlink->w = s->w;
  111. outlink->h = s->h;
  112. outlink->sample_aspect_ratio = (AVRational){1,1};
  113. outlink->frame_rate = s->frame_rate;
  114. return 0;
  115. }
  116. static void count_bits(AudioBitScopeContext *s, uint32_t sample, int max)
  117. {
  118. int i;
  119. for (i = 0; i < max; i++) {
  120. if (sample & (1U << i))
  121. s->counter[i]++;
  122. }
  123. }
  124. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. AVFilterLink *outlink = ctx->outputs[0];
  128. AudioBitScopeContext *s = ctx->priv;
  129. AVFrame *outpicref;
  130. int ch, i, j, b;
  131. outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  132. if (!outpicref) {
  133. av_frame_free(&insamples);
  134. return AVERROR(ENOMEM);
  135. }
  136. for (i = 0; i < outlink->h; i++)
  137. memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w * 4);
  138. outpicref->pts = insamples->pts;
  139. outpicref->sample_aspect_ratio = (AVRational){1,1};
  140. switch (insamples->format) {
  141. case AV_SAMPLE_FMT_S16P:
  142. for (ch = 0; ch < inlink->channels; ch++) {
  143. uint16_t *in = (uint16_t *)insamples->extended_data[ch];
  144. int w = outpicref->width / inlink->channels;
  145. int h = outpicref->height / 16;
  146. uint32_t color = AV_RN32(&s->fg[4 * ch]);
  147. memset(s->counter, 0, sizeof(s->counter));
  148. for (i = 0; i < insamples->nb_samples; i++)
  149. count_bits(s, in[i], 16);
  150. for (b = 0; b < 16; b++) {
  151. for (j = 1; j < h - 1; j++) {
  152. uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
  153. int ww = (s->counter[16 - b - 1] / (float)insamples->nb_samples) * (w - 1);
  154. for (i = 0; i < ww; i++) {
  155. AV_WN32(&dst[i * 4], color);
  156. }
  157. }
  158. }
  159. }
  160. break;
  161. case AV_SAMPLE_FMT_S32P:
  162. for (ch = 0; ch < inlink->channels; ch++) {
  163. uint32_t *in = (uint32_t *)insamples->extended_data[ch];
  164. int w = outpicref->width / inlink->channels;
  165. int h = outpicref->height / 32;
  166. uint32_t color = AV_RN32(&s->fg[4 * ch]);
  167. memset(s->counter, 0, sizeof(s->counter));
  168. for (i = 0; i < insamples->nb_samples; i++)
  169. count_bits(s, in[i], 32);
  170. for (b = 0; b < 32; b++) {
  171. for (j = 1; j < h - 1; j++) {
  172. uint8_t *dst = outpicref->data[0] + (b * h + j) * outpicref->linesize[0] + w * ch * 4;
  173. int ww = (s->counter[32 - b - 1] / (float)insamples->nb_samples) * (w - 1);
  174. for (i = 0; i < ww; i++) {
  175. AV_WN32(&dst[i * 4], color);
  176. }
  177. }
  178. }
  179. }
  180. break;
  181. }
  182. av_frame_free(&insamples);
  183. return ff_filter_frame(outlink, outpicref);
  184. }
  185. static int activate(AVFilterContext *ctx)
  186. {
  187. AVFilterLink *inlink = ctx->inputs[0];
  188. AVFilterLink *outlink = ctx->outputs[0];
  189. AudioBitScopeContext *s = ctx->priv;
  190. AVFrame *in;
  191. int ret;
  192. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  193. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  194. if (ret < 0)
  195. return ret;
  196. if (ret > 0)
  197. return filter_frame(inlink, in);
  198. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  199. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  200. return FFERROR_NOT_READY;
  201. }
  202. static const AVFilterPad inputs[] = {
  203. {
  204. .name = "default",
  205. .type = AVMEDIA_TYPE_AUDIO,
  206. .config_props = config_input,
  207. },
  208. { NULL }
  209. };
  210. static const AVFilterPad outputs[] = {
  211. {
  212. .name = "default",
  213. .type = AVMEDIA_TYPE_VIDEO,
  214. .config_props = config_output,
  215. },
  216. { NULL }
  217. };
  218. AVFilter ff_avf_abitscope = {
  219. .name = "abitscope",
  220. .description = NULL_IF_CONFIG_SMALL("Convert input audio to audio bit scope video output."),
  221. .query_formats = query_formats,
  222. .priv_size = sizeof(AudioBitScopeContext),
  223. .inputs = inputs,
  224. .outputs = outputs,
  225. .activate = activate,
  226. .priv_class = &abitscope_class,
  227. };