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