filtering_audio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2010 Nicolas George
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2012 Clément Bœsch
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /**
  25. * @file
  26. * API example for audio decoding and filtering
  27. */
  28. #include <unistd.h>
  29. #include <libavcodec/avcodec.h>
  30. #include <libavformat/avformat.h>
  31. #include <libavfilter/avfiltergraph.h>
  32. #include <libavfilter/avcodec.h>
  33. #include <libavfilter/buffersink.h>
  34. #include <libavfilter/buffersrc.h>
  35. const char *filter_descr = "aresample=8000,aconvert=s16:mono";
  36. const char *player = "ffplay -f s16le -ar 8000 -ac 1 -";
  37. static AVFormatContext *fmt_ctx;
  38. static AVCodecContext *dec_ctx;
  39. AVFilterContext *buffersink_ctx;
  40. AVFilterContext *buffersrc_ctx;
  41. AVFilterGraph *filter_graph;
  42. static int audio_stream_index = -1;
  43. static int open_input_file(const char *filename)
  44. {
  45. int ret;
  46. AVCodec *dec;
  47. if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
  48. av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
  49. return ret;
  50. }
  51. if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
  52. av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
  53. return ret;
  54. }
  55. /* select the audio stream */
  56. ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
  57. if (ret < 0) {
  58. av_log(NULL, AV_LOG_ERROR, "Cannot find a audio stream in the input file\n");
  59. return ret;
  60. }
  61. audio_stream_index = ret;
  62. dec_ctx = fmt_ctx->streams[audio_stream_index]->codec;
  63. /* init the audio decoder */
  64. if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
  65. av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n");
  66. return ret;
  67. }
  68. return 0;
  69. }
  70. static int init_filters(const char *filters_descr)
  71. {
  72. char args[512];
  73. int ret;
  74. AVFilter *abuffersrc = avfilter_get_by_name("abuffer");
  75. AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
  76. AVFilterInOut *outputs = avfilter_inout_alloc();
  77. AVFilterInOut *inputs = avfilter_inout_alloc();
  78. const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
  79. const int64_t *chlayouts = avfilter_all_channel_layouts;
  80. AVABufferSinkParams *abuffersink_params;
  81. const AVFilterLink *outlink;
  82. filter_graph = avfilter_graph_alloc();
  83. /* buffer audio source: the decoded frames from the decoder will be inserted here. */
  84. if (!dec_ctx->channel_layout)
  85. dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
  86. snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64,
  87. dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout);
  88. ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
  89. args, NULL, filter_graph);
  90. if (ret < 0) {
  91. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
  92. return ret;
  93. }
  94. /* buffer audio sink: to terminate the filter chain. */
  95. abuffersink_params = av_abuffersink_params_alloc();
  96. abuffersink_params->sample_fmts = sample_fmts;
  97. abuffersink_params->channel_layouts = chlayouts;
  98. ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
  99. NULL, abuffersink_params, filter_graph);
  100. av_free(abuffersink_params);
  101. if (ret < 0) {
  102. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
  103. return ret;
  104. }
  105. /* Endpoints for the filter graph. */
  106. outputs->name = av_strdup("in");
  107. outputs->filter_ctx = buffersrc_ctx;
  108. outputs->pad_idx = 0;
  109. outputs->next = NULL;
  110. inputs->name = av_strdup("out");
  111. inputs->filter_ctx = buffersink_ctx;
  112. inputs->pad_idx = 0;
  113. inputs->next = NULL;
  114. if ((ret = avfilter_graph_parse(filter_graph, filters_descr,
  115. &inputs, &outputs, NULL)) < 0)
  116. return ret;
  117. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  118. return ret;
  119. /* Print summary of the sink buffer
  120. * Note: args buffer is reused to store channel layout string */
  121. outlink = buffersink_ctx->inputs[0];
  122. av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout);
  123. av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n",
  124. (int)outlink->sample_rate,
  125. (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"),
  126. args);
  127. return 0;
  128. }
  129. static void print_samplesref(AVFilterBufferRef *samplesref)
  130. {
  131. const AVFilterBufferRefAudioProps *props = samplesref->audio;
  132. const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout);
  133. const uint16_t *p = (uint16_t*)samplesref->data[0];
  134. const uint16_t *p_end = p + n;
  135. while (p < p_end) {
  136. fputc(*p & 0xff, stdout);
  137. fputc(*p>>8 & 0xff, stdout);
  138. p++;
  139. }
  140. fflush(stdout);
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. int ret;
  145. AVPacket packet;
  146. AVFrame frame;
  147. int got_frame;
  148. if (argc != 2) {
  149. fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
  150. exit(1);
  151. }
  152. avcodec_register_all();
  153. av_register_all();
  154. avfilter_register_all();
  155. if ((ret = open_input_file(argv[1])) < 0)
  156. goto end;
  157. if ((ret = init_filters(filter_descr)) < 0)
  158. goto end;
  159. /* read all packets */
  160. while (1) {
  161. AVFilterBufferRef *samplesref;
  162. if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
  163. break;
  164. if (packet.stream_index == audio_stream_index) {
  165. avcodec_get_frame_defaults(&frame);
  166. got_frame = 0;
  167. ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &packet);
  168. av_free_packet(&packet);
  169. if (ret < 0) {
  170. av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n");
  171. continue;
  172. }
  173. if (got_frame) {
  174. /* push the audio data from decoded frame into the filtergraph */
  175. if (av_buffersrc_add_frame(buffersrc_ctx, &frame, 0) < 0) {
  176. av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
  177. break;
  178. }
  179. /* pull filtered audio from the filtergraph */
  180. while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
  181. av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0);
  182. if (samplesref) {
  183. print_samplesref(samplesref);
  184. avfilter_unref_buffer(samplesref);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. end:
  191. avfilter_graph_free(&filter_graph);
  192. if (dec_ctx)
  193. avcodec_close(dec_ctx);
  194. avformat_close_input(&fmt_ctx);
  195. if (ret < 0 && ret != AVERROR_EOF) {
  196. char buf[1024];
  197. av_strerror(ret, buf, sizeof(buf));
  198. fprintf(stderr, "Error occurred: %s\n", buf);
  199. exit(1);
  200. }
  201. exit(0);
  202. }