filter_audio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * copyright (c) 2013 Andrew Kelley
  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. * libavfilter API usage example.
  23. *
  24. * @example filter_audio.c
  25. * This example will generate a sine wave audio,
  26. * pass it through a simple filter chain, and then compute the MD5 checksum of
  27. * the output data.
  28. *
  29. * The filter chain it uses is:
  30. * (input) -> abuffer -> volume -> aformat -> abuffersink -> (output)
  31. *
  32. * abuffer: This provides the endpoint where you can feed the decoded samples.
  33. * volume: In this example we hardcode it to 0.90.
  34. * aformat: This converts the samples to the samplefreq, channel layout,
  35. * and sample format required by the audio device.
  36. * abuffersink: This provides the endpoint where you can read the samples after
  37. * they have passed through the filter chain.
  38. */
  39. #include <inttypes.h>
  40. #include <math.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include "libavutil/channel_layout.h"
  44. #include "libavutil/md5.h"
  45. #include "libavutil/mem.h"
  46. #include "libavutil/opt.h"
  47. #include "libavutil/samplefmt.h"
  48. #include "libavfilter/avfilter.h"
  49. #include "libavfilter/buffersink.h"
  50. #include "libavfilter/buffersrc.h"
  51. #define INPUT_SAMPLERATE 48000
  52. #define INPUT_FORMAT AV_SAMPLE_FMT_FLTP
  53. #define INPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_5POINT0
  54. #define VOLUME_VAL 0.90
  55. static int init_filter_graph(AVFilterGraph **graph, AVFilterContext **src,
  56. AVFilterContext **sink)
  57. {
  58. AVFilterGraph *filter_graph;
  59. AVFilterContext *abuffer_ctx;
  60. AVFilter *abuffer;
  61. AVFilterContext *volume_ctx;
  62. AVFilter *volume;
  63. AVFilterContext *aformat_ctx;
  64. AVFilter *aformat;
  65. AVFilterContext *abuffersink_ctx;
  66. AVFilter *abuffersink;
  67. AVDictionary *options_dict = NULL;
  68. uint8_t options_str[1024];
  69. uint8_t ch_layout[64];
  70. int err;
  71. /* Create a new filtergraph, which will contain all the filters. */
  72. filter_graph = avfilter_graph_alloc();
  73. if (!filter_graph) {
  74. fprintf(stderr, "Unable to create filter graph.\n");
  75. return AVERROR(ENOMEM);
  76. }
  77. /* Create the abuffer filter;
  78. * it will be used for feeding the data into the graph. */
  79. abuffer = avfilter_get_by_name("abuffer");
  80. if (!abuffer) {
  81. fprintf(stderr, "Could not find the abuffer filter.\n");
  82. return AVERROR_FILTER_NOT_FOUND;
  83. }
  84. abuffer_ctx = avfilter_graph_alloc_filter(filter_graph, abuffer, "src");
  85. if (!abuffer_ctx) {
  86. fprintf(stderr, "Could not allocate the abuffer instance.\n");
  87. return AVERROR(ENOMEM);
  88. }
  89. /* Set the filter options through the AVOptions API. */
  90. av_get_channel_layout_string(ch_layout, sizeof(ch_layout), 0, INPUT_CHANNEL_LAYOUT);
  91. av_opt_set (abuffer_ctx, "channel_layout", ch_layout, AV_OPT_SEARCH_CHILDREN);
  92. av_opt_set (abuffer_ctx, "sample_fmt", av_get_sample_fmt_name(INPUT_FORMAT), AV_OPT_SEARCH_CHILDREN);
  93. av_opt_set_q (abuffer_ctx, "time_base", (AVRational){ 1, INPUT_SAMPLERATE }, AV_OPT_SEARCH_CHILDREN);
  94. av_opt_set_int(abuffer_ctx, "sample_rate", INPUT_SAMPLERATE, AV_OPT_SEARCH_CHILDREN);
  95. /* Now initialize the filter; we pass NULL options, since we have already
  96. * set all the options above. */
  97. err = avfilter_init_str(abuffer_ctx, NULL);
  98. if (err < 0) {
  99. fprintf(stderr, "Could not initialize the abuffer filter.\n");
  100. return err;
  101. }
  102. /* Create volume filter. */
  103. volume = avfilter_get_by_name("volume");
  104. if (!volume) {
  105. fprintf(stderr, "Could not find the volume filter.\n");
  106. return AVERROR_FILTER_NOT_FOUND;
  107. }
  108. volume_ctx = avfilter_graph_alloc_filter(filter_graph, volume, "volume");
  109. if (!volume_ctx) {
  110. fprintf(stderr, "Could not allocate the volume instance.\n");
  111. return AVERROR(ENOMEM);
  112. }
  113. /* A different way of passing the options is as key/value pairs in a
  114. * dictionary. */
  115. av_dict_set(&options_dict, "volume", AV_STRINGIFY(VOLUME_VAL), 0);
  116. err = avfilter_init_dict(volume_ctx, &options_dict);
  117. av_dict_free(&options_dict);
  118. if (err < 0) {
  119. fprintf(stderr, "Could not initialize the volume filter.\n");
  120. return err;
  121. }
  122. /* Create the aformat filter;
  123. * it ensures that the output is of the format we want. */
  124. aformat = avfilter_get_by_name("aformat");
  125. if (!aformat) {
  126. fprintf(stderr, "Could not find the aformat filter.\n");
  127. return AVERROR_FILTER_NOT_FOUND;
  128. }
  129. aformat_ctx = avfilter_graph_alloc_filter(filter_graph, aformat, "aformat");
  130. if (!aformat_ctx) {
  131. fprintf(stderr, "Could not allocate the aformat instance.\n");
  132. return AVERROR(ENOMEM);
  133. }
  134. /* A third way of passing the options is in a string of the form
  135. * key1=value1:key2=value2.... */
  136. snprintf(options_str, sizeof(options_str),
  137. "sample_fmts=%s:sample_rates=%d:channel_layouts=0x%"PRIx64,
  138. av_get_sample_fmt_name(AV_SAMPLE_FMT_S16), 44100,
  139. (uint64_t)AV_CH_LAYOUT_STEREO);
  140. err = avfilter_init_str(aformat_ctx, options_str);
  141. if (err < 0) {
  142. av_log(NULL, AV_LOG_ERROR, "Could not initialize the aformat filter.\n");
  143. return err;
  144. }
  145. /* Finally create the abuffersink filter;
  146. * it will be used to get the filtered data out of the graph. */
  147. abuffersink = avfilter_get_by_name("abuffersink");
  148. if (!abuffersink) {
  149. fprintf(stderr, "Could not find the abuffersink filter.\n");
  150. return AVERROR_FILTER_NOT_FOUND;
  151. }
  152. abuffersink_ctx = avfilter_graph_alloc_filter(filter_graph, abuffersink, "sink");
  153. if (!abuffersink_ctx) {
  154. fprintf(stderr, "Could not allocate the abuffersink instance.\n");
  155. return AVERROR(ENOMEM);
  156. }
  157. /* This filter takes no options. */
  158. err = avfilter_init_str(abuffersink_ctx, NULL);
  159. if (err < 0) {
  160. fprintf(stderr, "Could not initialize the abuffersink instance.\n");
  161. return err;
  162. }
  163. /* Connect the filters;
  164. * in this simple case the filters just form a linear chain. */
  165. err = avfilter_link(abuffer_ctx, 0, volume_ctx, 0);
  166. if (err >= 0)
  167. err = avfilter_link(volume_ctx, 0, aformat_ctx, 0);
  168. if (err >= 0)
  169. err = avfilter_link(aformat_ctx, 0, abuffersink_ctx, 0);
  170. if (err < 0) {
  171. fprintf(stderr, "Error connecting filters\n");
  172. return err;
  173. }
  174. /* Configure the graph. */
  175. err = avfilter_graph_config(filter_graph, NULL);
  176. if (err < 0) {
  177. av_log(NULL, AV_LOG_ERROR, "Error configuring the filter graph\n");
  178. return err;
  179. }
  180. *graph = filter_graph;
  181. *src = abuffer_ctx;
  182. *sink = abuffersink_ctx;
  183. return 0;
  184. }
  185. /* Do something useful with the filtered data: this simple
  186. * example just prints the MD5 checksum of each plane to stdout. */
  187. static int process_output(struct AVMD5 *md5, AVFrame *frame)
  188. {
  189. int planar = av_sample_fmt_is_planar(frame->format);
  190. int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  191. int planes = planar ? channels : 1;
  192. int bps = av_get_bytes_per_sample(frame->format);
  193. int plane_size = bps * frame->nb_samples * (planar ? 1 : channels);
  194. int i, j;
  195. for (i = 0; i < planes; i++) {
  196. uint8_t checksum[16];
  197. av_md5_init(md5);
  198. av_md5_sum(checksum, frame->extended_data[i], plane_size);
  199. fprintf(stdout, "plane %d: 0x", i);
  200. for (j = 0; j < sizeof(checksum); j++)
  201. fprintf(stdout, "%02X", checksum[j]);
  202. fprintf(stdout, "\n");
  203. }
  204. fprintf(stdout, "\n");
  205. return 0;
  206. }
  207. /* Construct a frame of audio data to be filtered;
  208. * this simple example just synthesizes a sine wave. */
  209. static int get_input(AVFrame *frame, int frame_num)
  210. {
  211. int err, i, j;
  212. #define FRAME_SIZE 1024
  213. /* Set up the frame properties and allocate the buffer for the data. */
  214. frame->sample_rate = INPUT_SAMPLERATE;
  215. frame->format = INPUT_FORMAT;
  216. frame->channel_layout = INPUT_CHANNEL_LAYOUT;
  217. frame->nb_samples = FRAME_SIZE;
  218. frame->pts = frame_num * FRAME_SIZE;
  219. err = av_frame_get_buffer(frame, 0);
  220. if (err < 0)
  221. return err;
  222. /* Fill the data for each channel. */
  223. for (i = 0; i < 5; i++) {
  224. float *data = (float*)frame->extended_data[i];
  225. for (j = 0; j < frame->nb_samples; j++)
  226. data[j] = sin(2 * M_PI * (frame_num + j) * (i + 1) / FRAME_SIZE);
  227. }
  228. return 0;
  229. }
  230. int main(int argc, char *argv[])
  231. {
  232. struct AVMD5 *md5;
  233. AVFilterGraph *graph;
  234. AVFilterContext *src, *sink;
  235. AVFrame *frame;
  236. uint8_t errstr[1024];
  237. float duration;
  238. int err, nb_frames, i;
  239. if (argc < 2) {
  240. fprintf(stderr, "Usage: %s <duration>\n", argv[0]);
  241. return 1;
  242. }
  243. duration = atof(argv[1]);
  244. nb_frames = duration * INPUT_SAMPLERATE / FRAME_SIZE;
  245. if (nb_frames <= 0) {
  246. fprintf(stderr, "Invalid duration: %s\n", argv[1]);
  247. return 1;
  248. }
  249. avfilter_register_all();
  250. /* Allocate the frame we will be using to store the data. */
  251. frame = av_frame_alloc();
  252. if (!frame) {
  253. fprintf(stderr, "Error allocating the frame\n");
  254. return 1;
  255. }
  256. md5 = av_md5_alloc();
  257. if (!md5) {
  258. fprintf(stderr, "Error allocating the MD5 context\n");
  259. return 1;
  260. }
  261. /* Set up the filtergraph. */
  262. err = init_filter_graph(&graph, &src, &sink);
  263. if (err < 0) {
  264. fprintf(stderr, "Unable to init filter graph:");
  265. goto fail;
  266. }
  267. /* the main filtering loop */
  268. for (i = 0; i < nb_frames; i++) {
  269. /* get an input frame to be filtered */
  270. err = get_input(frame, i);
  271. if (err < 0) {
  272. fprintf(stderr, "Error generating input frame:");
  273. goto fail;
  274. }
  275. /* Send the frame to the input of the filtergraph. */
  276. err = av_buffersrc_add_frame(src, frame);
  277. if (err < 0) {
  278. av_frame_unref(frame);
  279. fprintf(stderr, "Error submitting the frame to the filtergraph:");
  280. goto fail;
  281. }
  282. /* Get all the filtered output that is available. */
  283. while ((err = av_buffersink_get_frame(sink, frame)) >= 0) {
  284. /* now do something with our filtered frame */
  285. err = process_output(md5, frame);
  286. if (err < 0) {
  287. fprintf(stderr, "Error processing the filtered frame:");
  288. goto fail;
  289. }
  290. av_frame_unref(frame);
  291. }
  292. if (err == AVERROR(EAGAIN)) {
  293. /* Need to feed more frames in. */
  294. continue;
  295. } else if (err == AVERROR_EOF) {
  296. /* Nothing more to do, finish. */
  297. break;
  298. } else if (err < 0) {
  299. /* An error occurred. */
  300. fprintf(stderr, "Error filtering the data:");
  301. goto fail;
  302. }
  303. }
  304. avfilter_graph_free(&graph);
  305. av_frame_free(&frame);
  306. av_freep(&md5);
  307. return 0;
  308. fail:
  309. av_strerror(err, errstr, sizeof(errstr));
  310. fprintf(stderr, "%s\n", errstr);
  311. return 1;
  312. }