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.

260 lines
8.3KB

  1. /*
  2. * Copyright (c) 2012 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. * audio to video transmedia filter
  23. */
  24. #include "libavutil/audioconvert.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/parseutils.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "audio.h"
  30. #include "video.h"
  31. typedef struct {
  32. const AVClass *class;
  33. int w, h;
  34. char *rate_str;
  35. AVRational rate;
  36. int buf_idx;
  37. AVFilterBufferRef *outpicref;
  38. int req_fullfilled;
  39. int n;
  40. int sample_count_mod;
  41. } ShowWavesContext;
  42. #define OFFSET(x) offsetof(ShowWavesContext, x)
  43. static const AVOption showwaves_options[] = {
  44. { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  45. { "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  46. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0 },
  47. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0 },
  48. { "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX},
  49. { NULL },
  50. };
  51. static const AVClass showwaves_class = {
  52. .class_name = "showwaves",
  53. .item_name = av_default_item_name,
  54. .option = showwaves_options,
  55. .version = LIBAVUTIL_VERSION_INT,
  56. .category = AV_CLASS_CATEGORY_FILTER,
  57. };
  58. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  59. {
  60. ShowWavesContext *showwaves = ctx->priv;
  61. int err;
  62. showwaves->class = &showwaves_class;
  63. av_opt_set_defaults(showwaves);
  64. showwaves->buf_idx = 0;
  65. if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0) {
  66. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  67. return err;
  68. }
  69. return 0;
  70. }
  71. static av_cold void uninit(AVFilterContext *ctx)
  72. {
  73. ShowWavesContext *showwaves = ctx->priv;
  74. av_freep(&showwaves->rate_str);
  75. avfilter_unref_bufferp(&showwaves->outpicref);
  76. }
  77. static int query_formats(AVFilterContext *ctx)
  78. {
  79. AVFilterFormats *formats = NULL;
  80. AVFilterChannelLayouts *layouts = NULL;
  81. AVFilterLink *inlink = ctx->inputs[0];
  82. AVFilterLink *outlink = ctx->outputs[0];
  83. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
  84. static const enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, -1 };
  85. /* set input audio formats */
  86. formats = ff_make_format_list(sample_fmts);
  87. if (!formats)
  88. return AVERROR(ENOMEM);
  89. ff_formats_ref(formats, &inlink->out_formats);
  90. layouts = ff_all_channel_layouts();
  91. if (!layouts)
  92. return AVERROR(ENOMEM);
  93. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  94. formats = ff_all_samplerates();
  95. if (!formats)
  96. return AVERROR(ENOMEM);
  97. ff_formats_ref(formats, &inlink->out_samplerates);
  98. /* set output video format */
  99. formats = ff_make_format_list(pix_fmts);
  100. if (!formats)
  101. return AVERROR(ENOMEM);
  102. ff_formats_ref(formats, &outlink->in_formats);
  103. return 0;
  104. }
  105. static int config_output(AVFilterLink *outlink)
  106. {
  107. AVFilterContext *ctx = outlink->src;
  108. AVFilterLink *inlink = ctx->inputs[0];
  109. ShowWavesContext *showwaves = ctx->priv;
  110. int err;
  111. if (showwaves->n && showwaves->rate_str) {
  112. av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
  113. return AVERROR(EINVAL);
  114. }
  115. if (!showwaves->n) {
  116. if (!showwaves->rate_str)
  117. showwaves->rate = (AVRational){25,1}; /* set default value */
  118. else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
  119. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
  120. return err;
  121. }
  122. showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
  123. }
  124. outlink->w = showwaves->w;
  125. outlink->h = showwaves->h;
  126. outlink->sample_aspect_ratio = (AVRational){1,1};
  127. outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
  128. (AVRational){showwaves->w,1});
  129. av_log(ctx, AV_LOG_INFO, "s:%dx%d r:%f n:%d\n",
  130. showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
  131. return 0;
  132. }
  133. inline static void push_frame(AVFilterLink *outlink)
  134. {
  135. ShowWavesContext *showwaves = outlink->src->priv;
  136. ff_start_frame(outlink, showwaves->outpicref);
  137. ff_draw_slice(outlink, 0, outlink->h, 1);
  138. ff_end_frame(outlink);
  139. showwaves->req_fullfilled = 1;
  140. showwaves->outpicref = NULL;
  141. showwaves->buf_idx = 0;
  142. }
  143. static int request_frame(AVFilterLink *outlink)
  144. {
  145. ShowWavesContext *showwaves = outlink->src->priv;
  146. AVFilterLink *inlink = outlink->src->inputs[0];
  147. int ret;
  148. showwaves->req_fullfilled = 0;
  149. do {
  150. ret = avfilter_request_frame(inlink);
  151. } while (!showwaves->req_fullfilled && ret >= 0);
  152. if (ret == AVERROR_EOF && showwaves->outpicref)
  153. push_frame(outlink);
  154. return ret;
  155. }
  156. #define MAX_INT16 ((1<<15) -1)
  157. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  158. {
  159. AVFilterContext *ctx = inlink->dst;
  160. AVFilterLink *outlink = ctx->outputs[0];
  161. ShowWavesContext *showwaves = ctx->priv;
  162. const int nb_samples = insamples->audio->nb_samples;
  163. AVFilterBufferRef *outpicref = showwaves->outpicref;
  164. int linesize = outpicref ? outpicref->linesize[0] : 0;
  165. int16_t *p = (int16_t *)insamples->data[0];
  166. int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
  167. int i, j, h;
  168. const int n = showwaves->n;
  169. const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
  170. /* draw data in the buffer */
  171. for (i = 0; i < nb_samples; i++) {
  172. if (showwaves->buf_idx == 0) {
  173. showwaves->outpicref = outpicref =
  174. ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
  175. outlink->w, outlink->h);
  176. outpicref->video->w = outlink->w;
  177. outpicref->video->h = outlink->h;
  178. outpicref->pts = insamples->pts;
  179. outlink->out_buf = outpicref;
  180. linesize = outpicref->linesize[0];
  181. memset(outpicref->data[0], 0, showwaves->h*linesize);
  182. }
  183. for (j = 0; j < nb_channels; j++) {
  184. h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16);
  185. if (h >= 0 && h < outlink->h)
  186. *(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
  187. }
  188. showwaves->sample_count_mod++;
  189. if (showwaves->sample_count_mod == n) {
  190. showwaves->sample_count_mod = 0;
  191. showwaves->buf_idx++;
  192. }
  193. if (showwaves->buf_idx == showwaves->w)
  194. push_frame(outlink);
  195. }
  196. avfilter_unref_buffer(insamples);
  197. }
  198. AVFilter avfilter_avf_showwaves = {
  199. .name = "showwaves",
  200. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
  201. .init = init,
  202. .uninit = uninit,
  203. .query_formats = query_formats,
  204. .priv_size = sizeof(ShowWavesContext),
  205. .inputs = (const AVFilterPad[]) {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_AUDIO,
  209. .filter_samples = filter_samples,
  210. .min_perms = AV_PERM_READ,
  211. },
  212. { .name = NULL }
  213. },
  214. .outputs = (const AVFilterPad[]) {
  215. {
  216. .name = "default",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. .config_props = config_output,
  219. .request_frame = request_frame,
  220. },
  221. { .name = NULL }
  222. },
  223. };