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 #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
00038 const char *filter_descr = "scale=78:24";
00039
00040 static AVFormatContext *fmt_ctx;
00041 static AVCodecContext *dec_ctx;
00042 AVFilterContext *buffersink_ctx;
00043 AVFilterContext *buffersrc_ctx;
00044 AVFilterGraph *filter_graph;
00045 static int video_stream_index = -1;
00046 static int64_t last_pts = AV_NOPTS_VALUE;
00047
00048 static int open_input_file(const char *filename)
00049 {
00050 int ret;
00051 AVCodec *dec;
00052
00053 if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
00054 av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
00055 return ret;
00056 }
00057
00058 if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
00059 av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
00060 return ret;
00061 }
00062
00063
00064 ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
00065 if (ret < 0) {
00066 av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
00067 return ret;
00068 }
00069 video_stream_index = ret;
00070 dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
00071
00072
00073 if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
00074 av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
00075 return ret;
00076 }
00077
00078 return 0;
00079 }
00080
00081 static int init_filters(const char *filters_descr)
00082 {
00083 char args[512];
00084 int ret;
00085 AVFilter *buffersrc = avfilter_get_by_name("buffer");
00086 AVFilter *buffersink = avfilter_get_by_name("buffersink");
00087 AVFilterInOut *outputs = avfilter_inout_alloc();
00088 AVFilterInOut *inputs = avfilter_inout_alloc();
00089 enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
00090 filter_graph = avfilter_graph_alloc();
00091
00092
00093 snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
00094 dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
00095 dec_ctx->time_base.num, dec_ctx->time_base.den,
00096 dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
00097 ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
00098 args, NULL, filter_graph);
00099 if (ret < 0) {
00100 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
00101 return ret;
00102 }
00103
00104
00105 ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
00106 NULL, pix_fmts, filter_graph);
00107 if (ret < 0) {
00108 av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
00109 return ret;
00110 }
00111
00112
00113 outputs->name = av_strdup("in");
00114 outputs->filter_ctx = buffersrc_ctx;
00115 outputs->pad_idx = 0;
00116 outputs->next = NULL;
00117
00118 inputs->name = av_strdup("out");
00119 inputs->filter_ctx = buffersink_ctx;
00120 inputs->pad_idx = 0;
00121 inputs->next = NULL;
00122
00123 if ((ret = avfilter_graph_parse(filter_graph, filters_descr,
00124 &inputs, &outputs, NULL)) < 0)
00125 return ret;
00126
00127 if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
00128 return ret;
00129 return 0;
00130 }
00131
00132 static void display_picref(AVFilterBufferRef *picref, AVRational time_base)
00133 {
00134 int x, y;
00135 uint8_t *p0, *p;
00136 int64_t delay;
00137
00138 if (picref->pts != AV_NOPTS_VALUE) {
00139 if (last_pts != AV_NOPTS_VALUE) {
00140
00141
00142 delay = av_rescale_q(picref->pts - last_pts,
00143 time_base, AV_TIME_BASE_Q);
00144 if (delay > 0 && delay < 1000000)
00145 usleep(delay);
00146 }
00147 last_pts = picref->pts;
00148 }
00149
00150
00151 p0 = picref->data[0];
00152 puts("\033c");
00153 for (y = 0; y < picref->video->h; y++) {
00154 p = p0;
00155 for (x = 0; x < picref->video->w; x++)
00156 putchar(" .-+#"[*(p++) / 52]);
00157 putchar('\n');
00158 p0 += picref->linesize[0];
00159 }
00160 fflush(stdout);
00161 }
00162
00163 int main(int argc, char **argv)
00164 {
00165 int ret;
00166 AVPacket packet;
00167 AVFrame frame;
00168 int got_frame;
00169
00170 if (argc != 2) {
00171 fprintf(stderr, "Usage: %s file\n", argv[0]);
00172 exit(1);
00173 }
00174
00175 avcodec_register_all();
00176 av_register_all();
00177 avfilter_register_all();
00178
00179 if ((ret = open_input_file(argv[1])) < 0)
00180 goto end;
00181 if ((ret = init_filters(filter_descr)) < 0)
00182 goto end;
00183
00184
00185 while (1) {
00186 AVFilterBufferRef *picref;
00187 if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
00188 break;
00189
00190 if (packet.stream_index == video_stream_index) {
00191 avcodec_get_frame_defaults(&frame);
00192 got_frame = 0;
00193 ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet);
00194 av_free_packet(&packet);
00195 if (ret < 0) {
00196 av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
00197 break;
00198 }
00199
00200 if (got_frame) {
00201 frame.pts = av_frame_get_best_effort_timestamp(&frame);
00202
00203
00204 av_vsrc_buffer_add_frame(buffersrc_ctx, &frame, 0);
00205
00206
00207 while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
00208 av_buffersink_get_buffer_ref(buffersink_ctx, &picref, 0);
00209 if (picref) {
00210 display_picref(picref, buffersink_ctx->inputs[0]->time_base);
00211 avfilter_unref_buffer(picref);
00212 }
00213 }
00214 }
00215 }
00216 }
00217 end:
00218 avfilter_graph_free(&filter_graph);
00219 if (dec_ctx)
00220 avcodec_close(dec_ctx);
00221 avformat_close_input(&fmt_ctx);
00222
00223 if (ret < 0 && ret != AVERROR_EOF) {
00224 char buf[1024];
00225 av_strerror(ret, buf, sizeof(buf));
00226 fprintf(stderr, "Error occurred: %s\n", buf);
00227 exit(1);
00228 }
00229
00230 exit(0);
00231 }