00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00029 #define _XOPEN_SOURCE 600 
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     
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     
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     
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     
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     
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             
00138 
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     
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     
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                 
00202                 av_vsrc_buffer_add_frame(buffersrc_ctx, &frame);
00203 
00204                 
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 }