00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <unistd.h>
00031
00032 #include <libavcodec/avcodec.h>
00033 #include <libavformat/avformat.h>
00034 #include <libavfilter/avfiltergraph.h>
00035 #include <libavfilter/avcodec.h>
00036 #include <libavfilter/buffersink.h>
00037 #include <libavfilter/buffersrc.h>
00038
00039 const char *filter_descr = "aresample=8000,aconvert=s16:mono";
00040 const char *player = "ffplay -f s16le -ar 8000 -ac 1 -";
00041
00042 static AVFormatContext *fmt_ctx;
00043 static AVCodecContext *dec_ctx;
00044 AVFilterContext *buffersink_ctx;
00045 AVFilterContext *buffersrc_ctx;
00046 AVFilterGraph *filter_graph;
00047 static int audio_stream_index = -1;
00048
00049 static int open_input_file(const char *filename)
00050 {
00051 int ret;
00052 AVCodec *dec;
00053
00054 if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
00055 av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
00056 return ret;
00057 }
00058
00059 if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
00060 av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
00061 return ret;
00062 }
00063
00064
00065 ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
00066 if (ret < 0) {
00067 av_log(NULL, AV_LOG_ERROR, "Cannot find a audio stream in the input file\n");
00068 return ret;
00069 }
00070 audio_stream_index = ret;
00071 dec_ctx = fmt_ctx->streams[audio_stream_index]->codec;
00072
00073
00074 if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
00075 av_log(NULL, AV_LOG_ERROR, "Cannot open audio decoder\n");
00076 return ret;
00077 }
00078
00079 return 0;
00080 }
00081
00082 static int init_filters(const char *filters_descr)
00083 {
00084 char args[512];
00085 int ret;
00086 AVFilter *abuffersrc = avfilter_get_by_name("abuffer");
00087 AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
00088 AVFilterInOut *outputs = avfilter_inout_alloc();
00089 AVFilterInOut *inputs = avfilter_inout_alloc();
00090 const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
00091 const int64_t *chlayouts = avfilter_all_channel_layouts;
00092 AVABufferSinkParams *abuffersink_params;
00093 const AVFilterLink *outlink;
00094
00095 filter_graph = avfilter_graph_alloc();
00096
00097
00098 if (!dec_ctx->channel_layout)
00099 dec_ctx->channel_layout = av_get_default_channel_layout(dec_ctx->channels);
00100 snprintf(args, sizeof(args), "%d:%d:0x%"PRIx64,
00101 dec_ctx->sample_rate, dec_ctx->sample_fmt, dec_ctx->channel_layout);
00102 ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
00103 args, NULL, filter_graph);
00104 if (ret < 0) {
00105 av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
00106 return ret;
00107 }
00108
00109
00110 abuffersink_params = av_abuffersink_params_alloc();
00111 abuffersink_params->sample_fmts = sample_fmts;
00112 abuffersink_params->channel_layouts = chlayouts;
00113 ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
00114 NULL, abuffersink_params, filter_graph);
00115 av_free(abuffersink_params);
00116 if (ret < 0) {
00117 av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
00118 return ret;
00119 }
00120
00121
00122 outputs->name = av_strdup("in");
00123 outputs->filter_ctx = buffersrc_ctx;
00124 outputs->pad_idx = 0;
00125 outputs->next = NULL;
00126
00127 inputs->name = av_strdup("out");
00128 inputs->filter_ctx = buffersink_ctx;
00129 inputs->pad_idx = 0;
00130 inputs->next = NULL;
00131
00132 if ((ret = avfilter_graph_parse(filter_graph, filters_descr,
00133 &inputs, &outputs, NULL)) < 0)
00134 return ret;
00135
00136 if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
00137 return ret;
00138
00139
00140
00141 outlink = buffersink_ctx->inputs[0];
00142 av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout);
00143 av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n",
00144 (int)outlink->sample_rate,
00145 (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"),
00146 args);
00147
00148 return 0;
00149 }
00150
00151 static void print_samplesref(AVFilterBufferRef *samplesref)
00152 {
00153 const AVFilterBufferRefAudioProps *props = samplesref->audio;
00154 const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout);
00155 const uint16_t *p = (uint16_t*)samplesref->data[0];
00156 const uint16_t *p_end = p + n;
00157
00158 while (p < p_end) {
00159 fputc(*p & 0xff, stdout);
00160 fputc(*p>>8 & 0xff, stdout);
00161 p++;
00162 }
00163 fflush(stdout);
00164 }
00165
00166 int main(int argc, char **argv)
00167 {
00168 int ret;
00169 AVPacket packet;
00170 AVFrame frame;
00171 int got_frame;
00172
00173 if (argc != 2) {
00174 fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
00175 exit(1);
00176 }
00177
00178 avcodec_register_all();
00179 av_register_all();
00180 avfilter_register_all();
00181
00182 if ((ret = open_input_file(argv[1])) < 0)
00183 goto end;
00184 if ((ret = init_filters(filter_descr)) < 0)
00185 goto end;
00186
00187
00188 while (1) {
00189 AVFilterBufferRef *samplesref;
00190 if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
00191 break;
00192
00193 if (packet.stream_index == audio_stream_index) {
00194 avcodec_get_frame_defaults(&frame);
00195 got_frame = 0;
00196 ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &packet);
00197 av_free_packet(&packet);
00198 if (ret < 0) {
00199 av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n");
00200 continue;
00201 }
00202
00203 if (got_frame) {
00204
00205 if (av_buffersrc_add_frame(buffersrc_ctx, &frame, 0) < 0) {
00206 av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
00207 break;
00208 }
00209
00210
00211 while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
00212 av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0);
00213 if (samplesref) {
00214 print_samplesref(samplesref);
00215 avfilter_unref_buffer(samplesref);
00216 }
00217 }
00218 }
00219 }
00220 }
00221 end:
00222 avfilter_graph_free(&filter_graph);
00223 if (dec_ctx)
00224 avcodec_close(dec_ctx);
00225 avformat_close_input(&fmt_ctx);
00226
00227 if (ret < 0 && ret != AVERROR_EOF) {
00228 char buf[1024];
00229 av_strerror(ret, buf, sizeof(buf));
00230 fprintf(stderr, "Error occurred: %s\n", buf);
00231 exit(1);
00232 }
00233
00234 exit(0);
00235 }