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("ffabuffersink");
00088     AVFilterInOut *outputs = avfilter_inout_alloc();
00089     AVFilterInOut *inputs  = avfilter_inout_alloc();
00090     const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
00091     AVABufferSinkParams *abuffersink_params;
00092     const AVFilterLink *outlink;
00093     AVRational time_base = fmt_ctx->streams[audio_stream_index]->time_base;
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),
00101             "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
00102              time_base.num, time_base.den, dec_ctx->sample_rate,
00103              av_get_sample_fmt_name(dec_ctx->sample_fmt), dec_ctx->channel_layout);
00104     ret = avfilter_graph_create_filter(&buffersrc_ctx, abuffersrc, "in",
00105                                        args, NULL, filter_graph);
00106     if (ret < 0) {
00107         av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
00108         return ret;
00109     }
00110 
00111     
00112     abuffersink_params = av_abuffersink_params_alloc();
00113     abuffersink_params->sample_fmts     = sample_fmts;
00114     ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
00115                                        NULL, abuffersink_params, filter_graph);
00116     av_free(abuffersink_params);
00117     if (ret < 0) {
00118         av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
00119         return ret;
00120     }
00121 
00122     
00123     outputs->name       = av_strdup("in");
00124     outputs->filter_ctx = buffersrc_ctx;
00125     outputs->pad_idx    = 0;
00126     outputs->next       = NULL;
00127 
00128     inputs->name       = av_strdup("out");
00129     inputs->filter_ctx = buffersink_ctx;
00130     inputs->pad_idx    = 0;
00131     inputs->next       = NULL;
00132 
00133     if ((ret = avfilter_graph_parse(filter_graph, filters_descr,
00134                                     &inputs, &outputs, NULL)) < 0)
00135         return ret;
00136 
00137     if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
00138         return ret;
00139 
00140     
00141 
00142     outlink = buffersink_ctx->inputs[0];
00143     av_get_channel_layout_string(args, sizeof(args), -1, outlink->channel_layout);
00144     av_log(NULL, AV_LOG_INFO, "Output: srate:%dHz fmt:%s chlayout:%s\n",
00145            (int)outlink->sample_rate,
00146            (char *)av_x_if_null(av_get_sample_fmt_name(outlink->format), "?"),
00147            args);
00148 
00149     return 0;
00150 }
00151 
00152 static void print_samplesref(AVFilterBufferRef *samplesref)
00153 {
00154     const AVFilterBufferRefAudioProps *props = samplesref->audio;
00155     const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout);
00156     const uint16_t *p     = (uint16_t*)samplesref->data[0];
00157     const uint16_t *p_end = p + n;
00158 
00159     while (p < p_end) {
00160         fputc(*p    & 0xff, stdout);
00161         fputc(*p>>8 & 0xff, stdout);
00162         p++;
00163     }
00164     fflush(stdout);
00165 }
00166 
00167 int main(int argc, char **argv)
00168 {
00169     int ret;
00170     AVPacket packet;
00171     AVFrame frame;
00172     int got_frame;
00173 
00174     if (argc != 2) {
00175         fprintf(stderr, "Usage: %s file | %s\n", argv[0], player);
00176         exit(1);
00177     }
00178 
00179     avcodec_register_all();
00180     av_register_all();
00181     avfilter_register_all();
00182 
00183     if ((ret = open_input_file(argv[1])) < 0)
00184         goto end;
00185     if ((ret = init_filters(filter_descr)) < 0)
00186         goto end;
00187 
00188     
00189     while (1) {
00190         AVFilterBufferRef *samplesref;
00191         if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
00192             break;
00193 
00194         if (packet.stream_index == audio_stream_index) {
00195             avcodec_get_frame_defaults(&frame);
00196             got_frame = 0;
00197             ret = avcodec_decode_audio4(dec_ctx, &frame, &got_frame, &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 (1) {
00212                     ret = av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0);
00213                     if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
00214                         break;
00215                     if(ret < 0)
00216                         goto end;
00217                     if (samplesref) {
00218                         print_samplesref(samplesref);
00219                         avfilter_unref_bufferp(&samplesref);
00220                     }
00221                 }
00222             }
00223         }
00224         av_free_packet(&packet);
00225     }
00226 end:
00227     avfilter_graph_free(&filter_graph);
00228     if (dec_ctx)
00229         avcodec_close(dec_ctx);
00230     avformat_close_input(&fmt_ctx);
00231 
00232     if (ret < 0 && ret != AVERROR_EOF) {
00233         char buf[1024];
00234         av_strerror(ret, buf, sizeof(buf));
00235         fprintf(stderr, "Error occurred: %s\n", buf);
00236         exit(1);
00237     }
00238 
00239     exit(0);
00240 }