• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

doc/examples/filtering.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Nicolas George
00003  * Copyright (c) 2011 Stefano Sabatini
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00029 #define _XOPEN_SOURCE 600 /* for usleep */
00030 
00031 #include <libavcodec/avcodec.h>
00032 #include <libavformat/avformat.h>
00033 #include <libavfilter/avfiltergraph.h>
00034 #include <libavfilter/vsrc_buffer.h>
00035 
00036 const char *filter_descr = "scale=78:24";
00037 
00038 static AVFormatContext *fmt_ctx;
00039 static AVCodecContext *dec_ctx;
00040 AVFilterContext *buffersink_ctx;
00041 AVFilterContext *buffersrc_ctx;
00042 AVFilterGraph *filter_graph;
00043 static int video_stream_index = -1;
00044 static int64_t last_pts = AV_NOPTS_VALUE;
00045 
00046 static int open_input_file(const char *filename)
00047 {
00048     int ret, i;
00049     AVCodec *dec;
00050 
00051     if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
00052         av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
00053         return ret;
00054     }
00055 
00056     if ((ret = av_find_stream_info(fmt_ctx)) < 0) {
00057         av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
00058         return ret;
00059     }
00060 
00061     /* select the video stream */
00062     ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
00063     if (ret < 0) {
00064         av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
00065         return ret;
00066     }
00067     video_stream_index = ret;
00068     dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
00069 
00070     /* init the video decoder */
00071     if ((ret = avcodec_open(dec_ctx, dec)) < 0) {
00072         av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
00073         return ret;
00074     }
00075 
00076     return 0;
00077 }
00078 
00079 static int init_filters(const char *filters_descr)
00080 {
00081     char args[512];
00082     int ret;
00083     AVFilter *buffersrc  = avfilter_get_by_name("buffer");
00084     AVFilter *buffersink = avfilter_get_by_name("buffersink");
00085     AVFilterInOut *outputs = avfilter_inout_alloc();
00086     AVFilterInOut *inputs  = avfilter_inout_alloc();
00087     enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
00088     filter_graph = avfilter_graph_alloc();
00089 
00090     /* buffer video source: the decoded frames from the decoder will be inserted here. */
00091     snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
00092              dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
00093              dec_ctx->time_base.num, dec_ctx->time_base.den,
00094              dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
00095     ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
00096                                        args, NULL, filter_graph);
00097     if (ret < 0) {
00098         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
00099         return ret;
00100     }
00101 
00102     /* buffer video sink: to terminate the filter chain. */
00103     ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
00104                                        NULL, pix_fmts, filter_graph);
00105     if (ret < 0) {
00106         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
00107         return ret;
00108     }
00109 
00110     /* Endpoints for the filter graph. */
00111     outputs->name       = av_strdup("in");
00112     outputs->filter_ctx = buffersrc_ctx;
00113     outputs->pad_idx    = 0;
00114     outputs->next       = NULL;
00115 
00116     inputs->name       = av_strdup("out");
00117     inputs->filter_ctx = buffersink_ctx;
00118     inputs->pad_idx    = 0;
00119     inputs->next       = NULL;
00120 
00121     if ((ret = avfilter_graph_parse(filter_graph, filter_descr,
00122                                     &inputs, &outputs, NULL)) < 0)
00123         return ret;
00124 
00125     if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
00126         return ret;
00127 }
00128 
00129 static void display_picref(AVFilterBufferRef *picref, AVRational time_base)
00130 {
00131     int x, y;
00132     uint8_t *p0, *p;
00133     int64_t delay;
00134 
00135     if (picref->pts != AV_NOPTS_VALUE) {
00136         if (last_pts != AV_NOPTS_VALUE) {
00137             /* sleep roughly the right amount of time;
00138              * usleep is in microseconds, just like AV_TIME_BASE. */
00139             delay = av_rescale_q(picref->pts - last_pts,
00140                                  time_base, AV_TIME_BASE_Q);
00141             if (delay > 0 && delay < 1000000)
00142                 usleep(delay);
00143         }
00144         last_pts = picref->pts;
00145     }
00146 
00147     /* Trivial ASCII grayscale display. */
00148     p0 = picref->data[0];
00149     puts("\033c");
00150     for (y = 0; y < picref->video->h; y++) {
00151         p = p0;
00152         for (x = 0; x < picref->video->w; x++)
00153             putchar(" .-+#"[*(p++) / 52]);
00154         putchar('\n');
00155         p0 += picref->linesize[0];
00156     }
00157     fflush(stdout);
00158 }
00159 
00160 int main(int argc, char **argv)
00161 {
00162     int ret;
00163     AVPacket packet;
00164     AVFrame frame;
00165     int got_frame;
00166 
00167     if (argc != 2) {
00168         fprintf(stderr, "Usage: %s file\n", argv[0]);
00169         exit(1);
00170     }
00171 
00172     avcodec_register_all();
00173     av_register_all();
00174     avfilter_register_all();
00175 
00176     if ((ret = open_input_file(argv[1])) < 0)
00177         goto end;
00178     if ((ret = init_filters(filter_descr)) < 0)
00179         goto end;
00180 
00181     /* read all packets */
00182     while (1) {
00183         AVFilterBufferRef *picref;
00184         if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
00185             break;
00186 
00187         if (packet.stream_index == video_stream_index) {
00188             avcodec_get_frame_defaults(&frame);
00189             got_frame = 0;
00190             ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet);
00191             av_free_packet(&packet);
00192             if (ret < 0) {
00193                 av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
00194                 break;
00195             }
00196 
00197             if (got_frame) {
00198                 if (frame.pts == AV_NOPTS_VALUE)
00199                     frame.pts = frame.pkt_dts == AV_NOPTS_VALUE ?
00200                         frame.pkt_dts : frame.pkt_pts;
00201                 /* push the decoded frame into the filtergraph */
00202                 av_vsrc_buffer_add_frame(buffersrc_ctx, &frame);
00203 
00204                 /* pull filtered pictures from the filtergraph */
00205                 while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
00206                     av_vsink_buffer_get_video_buffer_ref(buffersink_ctx, &picref, 0);
00207                     if (picref) {
00208                         display_picref(picref, buffersink_ctx->inputs[0]->time_base);
00209                         avfilter_unref_buffer(picref);
00210                     }
00211                 }
00212             }
00213         }
00214     }
00215 end:
00216     avfilter_graph_free(&filter_graph);
00217     if (dec_ctx)
00218         avcodec_close(dec_ctx);
00219     av_close_input_file(fmt_ctx);
00220 
00221     if (ret < 0 && ret != AVERROR_EOF) {
00222         char buf[1024];
00223         av_strerror(ret, buf, sizeof(buf));
00224         fprintf(stderr, "Error occurred: %s\n", buf);
00225         exit(1);
00226     }
00227 
00228     exit(0);
00229 }
Generated on Fri Feb 1 2013 14:34:27 for FFmpeg by doxygen 1.7.1