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.

253 lines
7.9KB

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