DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

FFMPEG中的libswscale是做像素转换的,但是对于一些复杂的操作,比如添加水印等,这个库就不行了,这时候就要说一下另外一个AVFilter。AVFilter完全可以替代libswscale的所有功能,也许有一天,ffmpeg也会这样去做。AVFilter虽然学起来有点复杂,但是为了学好FFMPEG,为了涨工资,我忍了。(*^__^*) 嘻嘻……

 

概念:

做个directshow的同学,对于这一块应该很了解了。

1.AVFilterGraph:和dshow中的filtergraph功能基本上一样,管理filter的,可以看成filter的一个容器。

2.AVFilter:就是一个filter啦。但是为什么叫做filter,其实就是一个过滤器,目标资源通过该过滤器总该发生一点变化是吧。。。。。

3.AVFilterLink:代表两个filter的关联媒介。

4.AVFilterPad:代表一个filter的输入输出口,就跟dshow中的pin差不多。只有输入pad的叫做source,只有输出pad的叫做sink。

 

下面是官方提供的一个例子:

 

[cpp] view plain copy
 
 print?
  1. /* 
  2. *modifier by tongli 
  3. */  
  4. #define _XOPEN_SOURCE 600   
  5. #include <stdio.h>  
  6. #include <windows.h>  
  7. #include "snprintf.h"  
  8. extern "C"  
  9. {  
  10. #include <libavcodec/avcodec.h>  
  11. #include <libavformat/avformat.h>  
  12. #include <libavfilter/avfiltergraph.h>  
  13. #include <libavfilter/avcodec.h>  
  14. #include <libavfilter/buffersink.h>  
  15. #include <libavfilter/buffersrc.h>  
  16. #include <libavutil/opt.h>  
  17. }  
  18.   
  19. const char *filter_descr = "scale=78:24";  
  20.   
  21. static AVFormatContext *fmt_ctx;  
  22. static AVCodecContext *dec_ctx;  
  23. AVFilterContext *buffersink_ctx;  
  24. AVFilterContext *buffersrc_ctx;  
  25. AVFilterGraph *filter_graph;  
  26. static int video_stream_index = -1;  
  27. static int64_t last_pts = AV_NOPTS_VALUE;  
  28.   
  29. static int open_input_file(const char *filename)  
  30. {  
  31.     int ret;  
  32.     AVCodec *dec;  
  33.   
  34.     if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {  
  35.         av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");  
  36.         return ret;  
  37.     }  
  38.   
  39.     if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {  
  40.         av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");  
  41.         return ret;  
  42.     }  
  43.   
  44.     /* select the video stream */  
  45.     ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);  
  46.     if (ret < 0) {  
  47.         av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");  
  48.         return ret;  
  49.     }  
  50.     video_stream_index = ret;  
  51.     dec_ctx = fmt_ctx->streams[video_stream_index]->codec;  
  52.     av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);  
  53.   
  54.     /* init the video decoder */  
  55.     if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {  
  56.         av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");  
  57.         return ret;  
  58.     }  
  59.   
  60.     return 0;  
  61. }  
  62.   
  63. static int init_filters(const char *filters_descr)  
  64. {  
  65.     char args[512];  
  66.     int ret = 0;  
  67.     AVFilter *buffersrc = avfilter_get_by_name("buffer");  
  68.     AVFilter *buffersink = avfilter_get_by_name("buffersink");  
  69.     AVFilterInOut *outputs = avfilter_inout_alloc();  
  70.     AVFilterInOut *inputs = avfilter_inout_alloc();  
  71.     AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;  
  72.     enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };  
  73.   
  74.     filter_graph = avfilter_graph_alloc();  
  75.     if (!outputs || !inputs || !filter_graph) {  
  76.         ret = AVERROR(ENOMEM);  
  77.         goto end;  
  78.     }  
  79.   
  80.     /* buffer video source: the decoded frames from the decoder will be inserted here. */  
  81.     snprintf(args, sizeof(args),  
  82.         "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",  
  83.         dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,  
  84.         time_base.num, time_base.den,  
  85.         dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);  
  86.   
  87.     ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",  
  88.         args, NULL, filter_graph);  
  89.     if (ret < 0) {  
  90.         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");  
  91.         goto end;  
  92.     }  
  93.   
  94.     /* buffer video sink: to terminate the filter chain. */  
  95.     ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",  
  96.         NULL, NULL, filter_graph);  
  97.     if (ret < 0) {  
  98.         av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");  
  99.         goto end;  
  100.     }  
  101.   
  102.     ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,  
  103.         AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);  
  104.     if (ret < 0) {  
  105.         av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");  
  106.         goto end;  
  107.     }  
  108.   
  109.     /* Endpoints for the filter graph. */  
  110.     outputs->name = av_strdup("in");  
  111.     outputs->filter_ctx = buffersrc_ctx;  
  112.     outputs->pad_idx = 0;  
  113.     outputs->next = NULL;  
  114.   
  115.     inputs->name = av_strdup("out");  
  116.     inputs->filter_ctx = buffersink_ctx;  
  117.     inputs->pad_idx = 0;  
  118.     inputs->next = NULL;  
  119.   
  120.     if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,  
  121.         &inputs, &outputs, NULL)) < 0)  
  122.         goto end;  
  123.   
  124.     if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)  
  125.         goto end;  
  126.   
  127. end:  
  128.     avfilter_inout_free(&inputs);  
  129.     avfilter_inout_free(&outputs);  
  130.   
  131.     return ret;  
  132. }  
  133.   
  134. static void display_frame(const AVFrame *frame, AVRational time_base)  
  135. {  
  136.     int x, y;  
  137.     uint8_t *p0, *p;  
  138.     int64_t delay;  
  139.     AVRational ar_base_q = { 1, AV_TIME_BASE };  
  140.     if (frame->pts != AV_NOPTS_VALUE) {  
  141.         if (last_pts != AV_NOPTS_VALUE) {  
  142.             /* sleep roughly the right amount of time; 
  143.             * usleep is in microseconds, just like AV_TIME_BASE. */  
  144.             delay = av_rescale_q(frame->pts - last_pts,  
  145.                 time_base, ar_base_q);  
  146.             if (delay > 0 && delay < 1000000)  
  147.                 //usleep(delay);  
  148.                 Sleep(delay);  
  149.         }  
  150.         last_pts = frame->pts;  
  151.     }  
  152.   
  153.     /* Trivial ASCII grayscale display. */  
  154.     p0 = frame->data[0];  
  155.     puts("\033c");  
  156.     for (y = 0; y < frame->height; y++) {  
  157.         p = p0;  
  158.         for (x = 0; x < frame->width; x++)  
  159.             putchar(" .-+#"[*(p++) / 52]);  
  160.         putchar('\n');  
  161.         p0 += frame->linesize[0];  
  162.     }  
  163.     fflush(stdout);  
  164. }  
  165.   
  166. int main(int argc, char **argv)  
  167. {  
  168.     int ret;  
  169.     AVPacket packet;  
  170.     AVFrame *frame = av_frame_alloc();  
  171.     AVFrame *filt_frame = av_frame_alloc();  
  172.     int got_frame;  
  173.   
  174.     if (!frame || !filt_frame) {  
  175.         perror("Could not allocate frame");  
  176.         exit(1);  
  177.     }  
  178.   
  179.     av_register_all();  
  180.     avfilter_register_all();  
  181.   
  182.     if ((ret = open_input_file("style.ts")) < 0)  
  183.         goto end;  
  184.     if ((ret = init_filters(filter_descr)) < 0)  
  185.         goto end;  
  186.   
  187.     /* read all packets */  
  188.     while (1) {  
  189.         if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)  
  190.             break;  
  191.   
  192.         if (packet.stream_index == video_stream_index) {  
  193.             got_frame = 0;  
  194.             ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);  
  195.             if (ret < 0) {  
  196.                 av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");  
  197.                 break;  
  198.             }  
  199.   
  200.             if (got_frame) {  
  201.                 frame->pts = av_frame_get_best_effort_timestamp(frame);  
  202.   
  203.                 /* push the decoded frame into the filtergraph */  
  204.                 if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {  
  205.                     av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");  
  206.                     break;  
  207.                 }  
  208.   
  209.                 /* pull filtered frames from the filtergraph */  
  210.                 while (1) {  
  211.                     ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);  
  212.                     if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)  
  213.                         break;  
  214.                     if (ret < 0)  
  215.                         goto end;  
  216.                     display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);  
  217.                     av_frame_unref(filt_frame);  
  218.                 }  
  219.                 av_frame_unref(frame);  
  220.             }  
  221.         }  
  222.         av_free_packet(&packet);  
  223.     }  
  224. end:  
  225.     avfilter_graph_free(&filter_graph);  
  226.     avcodec_close(dec_ctx);  
  227.     avformat_close_input(&fmt_ctx);  
  228.     av_frame_free(&frame);  
  229.     av_frame_free(&filt_frame);  
  230.   
  231.     if (ret < 0 && ret != AVERROR_EOF) {  
  232.         fprintf(stderr, "Error occurred: %s\n"); //av_err2str(ret));  
  233.         exit(1);  
  234.     }  
  235.   
  236.     exit(0);  
  237. }  

未完待续。。。。

posted on 2017-08-09 19:01  DoubleLi  阅读(1821)  评论(0编辑  收藏  举报