Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6680742
  • 博文数量: 702
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13243
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
文章分类

全部博文(702)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(179)

2014年(129)

2013年(123)

2012年(61)

分类: 高性能计算

2018-12-28 11:17:59

五、Filter实现的源码分析

5.1 滤镜的回调函数的调用流程,以单滤镜设置(如 -vf "delogo")为例

init()
query_format(); // 输入输出的格式查询: 列出滤镜支持的格式列表
config_input();
config_output();
for (;;) {
request_frame();
filter_frame();
}
uninit();

5.2 函数的调用堆栈


  1. STEP1:  init(); // 滤镜初始化 : 用于初始化滤镜
  2. #0 init (ctx=0x24dbe40) at libavfilter/vf_delogo.c:196
  3. #1 0x00000000004e9ad5 in avfilter_init_str (filter=0x24dbe40, args=0x24dbdc0 "x=10:y=10:w=80:h=20") at libavfilter/avfilter.c:1018
  4. #2 0x00000000004fb54a in create_filter (filt_ctx=0x7fffffffce20, ctx=0x24da800, index=<value optimized out>, name=<value optimized out>, args=0x24dbdc0 "x=10:y=10:w=80:h=20",
  5.     log_ctx=0x24da800) at libavfilter/graphparser.c:149
  6. #3 0x00000000004fbee2 in parse_filter (graph=0x24da800, filters=0x23a5488 "", inputs=0x7fffffffd0b8, outputs=0x7fffffffd0b0) at libavfilter/graphparser.c:192
  7. #4 avfilter_graph_parse2 (graph=0x24da800, filters=0x23a5488 "", inputs=0x7fffffffd0b8, outputs=0x7fffffffd0b0) at libavfilter/graphparser.c:427
  8. #5 0x00000000004bc528 in configure_filtergraph (fg=0x23a5500) at fftools/ffmpeg_filter.c:1047
  9. #6 0x00000000004cb4ba in ifilter_send_frame (ifilter=0x23aabc0, frame=0x24b2480) at fftools/ffmpeg.c:2173
  10. #7 0x00000000004cb9b6 in send_frame_to_filters (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570,
  11.     eof=<value optimized out>, decode_failed=0x7fffffffd578) at fftools/ffmpeg.c:2254
  12. #8 decode_video (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570, eof=<value optimized out>, decode_failed=0x7fffffffd578)
  13.     at fftools/ffmpeg.c:2455
  14. #9 0x00000000004cc1ad in process_input_packet (ist=0x23adfc0, pkt=<value optimized out>, no_eof=0) at fftools/ffmpeg.c:2609
  15. #10 0x00000000004ce659 in process_input () at fftools/ffmpeg.c:4441
  16. #11 transcode_step () at fftools/ffmpeg.c:4561
  17. #12 0x00000000004d0e65 in transcode (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4615
  18. #13 main (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4847
  19. preinit(); // 滤镜预初始化: 用于分配空间和初始化子对象


  20. STEP2:  query_format(); // 输入输出的格式查询: 列出滤镜支持的格式列表
  21. #0 query_formats (ctx=0x24dbe40) at libavfilter/vf_delogo.c:188
  22. #1 0x00000000004ec8e6 in filter_query_formats (ctx=0x24dbe40) at libavfilter/avfiltergraph.c:326
  23. #2 0x00000000004ed014 in query_formats (graph=0x24da800, log_ctx=0x0) at libavfilter/avfiltergraph.c:454
  24. #3 0x00000000004edc51 in graph_config_formats (graphctx=0x24da800, log_ctx=0x0) at libavfilter/avfiltergraph.c:1163
  25. #4 avfilter_graph_config (graphctx=0x24da800, log_ctx=0x0) at libavfilter/avfiltergraph.c:1274
  26. #5 0x00000000004bcabb in configure_filtergraph (fg=0x23a5500) at fftools/ffmpeg_filter.c:1100
  27. #6 0x00000000004cb4ba in ifilter_send_frame (ifilter=0x23aabc0, frame=0x24b2480) at fftools/ffmpeg.c:2173
  28. #7 0x00000000004cb9b6 in send_frame_to_filters (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570,
  29.     eof=<value optimized out>, decode_failed=0x7fffffffd578) at fftools/ffmpeg.c:2254
  30. #8 decode_video (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570, eof=<value optimized out>, decode_failed=0x7fffffffd578)
  31.     at fftools/ffmpeg.c:2455
  32. #9 0x00000000004cc1ad in process_input_packet (ist=0x23adfc0, pkt=<value optimized out>, no_eof=0) at fftools/ffmpeg.c:2609
  33. #10 0x00000000004ce659 in process_input () at fftools/ffmpeg.c:4441
  34. #11 transcode_step () at fftools/ffmpeg.c:4561
  35. #12 0x00000000004d0e65 in transcode (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4615
  36. #13 main (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4847


  37. STEP3: 配置滤镜的输入、输出格式和参数
  38.                 config_input();
  39.       config_output();
  40. #0 config_input (inlink=0x24dd980) at libavfilter/vf_delogo.c:231
  41. #1 0x00000000004ea930 in avfilter_config_links (filter=0x24dbe40) at libavfilter/avfilter.c:369
  42. #2 0x00000000004ea8b0 in avfilter_config_links (filter=0x24da900) at libavfilter/avfilter.c:307
  43. #3 0x00000000004ea8b0 in avfilter_config_links (filter=0x24dec40) at libavfilter/avfilter.c:307
  44. #4 0x00000000004ee922 in graph_config_links (graphctx=0x24da800, log_ctx=0x0) at libavfilter/avfiltergraph.c:261
  45. #5 avfilter_graph_config (graphctx=0x24da800, log_ctx=0x0) at libavfilter/avfiltergraph.c:1276
  46. #6 0x00000000004bcabb in configure_filtergraph (fg=0x23a5500) at fftools/ffmpeg_filter.c:1100
  47. #7 0x00000000004cb4ba in ifilter_send_frame (ifilter=0x23aabc0, frame=0x24b2480) at fftools/ffmpeg.c:2173
  48. #8 0x00000000004cb9b6 in send_frame_to_filters (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570, eof=<value optimized out>, decode_failed=0x7fffffffd578) at fftools/ffmpeg.c:2254
  49. #9 decode_video (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570, eof=<value optimized out>, decode_failed=0x7fffffffd578) at fftools/ffmpeg.c:2455
  50. #10 0x00000000004cc1ad in process_input_packet (ist=0x23adfc0, pkt=<value optimized out>, no_eof=0) at fftools/ffmpeg.c:2609
  51. #11 0x00000000004ce659 in process_input () at fftools/ffmpeg.c:4441
  52. #12 transcode_step () at fftools/ffmpeg.c:4561
  53. #13 0x00000000004d0e65 in transcode (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4615
  54. #14 main (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4847


  55. STEP4: 滤镜体对每一帧数据进行处理
  56.            filter_frame();
  57. #0 filter_frame (inlink=0x24dd980, in=0x24de480) at libavfilter/vf_delogo.c:245
  58. #1 0x00000000004eb92e in ff_filter_frame_framed (filter=<value optimized out>) at libavfilter/avfilter.c:1071
  59. #2 ff_filter_frame_to_filter (filter=<value optimized out>) at libavfilter/avfilter.c:1219
  60. #3 ff_filter_activate_default (filter=<value optimized out>) at libavfilter/avfilter.c:1268
  61. #4 ff_filter_activate (filter=<value optimized out>) at libavfilter/avfilter.c:1430
  62. #5 0x00000000004efad0 in push_frame (ctx=<value optimized out>, frame=0x24b2480, flags=<value optimized out>) at libavfilter/buffersrc.c:181
  63. #6 av_buffersrc_add_frame_internal (ctx=<value optimized out>, frame=0x24b2480, flags=<value optimized out>) at libavfilter/buffersrc.c:255
  64. #7 0x00000000004efc2f in av_buffersrc_add_frame_flags (ctx=0x24ddcc0, frame=<value optimized out>, flags=<value optimized out>) at libavfilter/buffersrc.c:164
  65. #8 0x00000000004cb39d in ifilter_send_frame (ifilter=0x23aabc0, frame=0x24b2480) at fftools/ffmpeg.c:2180
  66. #9 0x00000000004cb9b6 in send_frame_to_filters (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570, eof=<value optimized out>, decode_failed=0x7fffffffd578) at fftools/ffmpeg.c:2254
  67. #10 decode_video (ist=0x23adfc0, pkt=<value optimized out>, got_output=<value optimized out>, duration_pts=0x7fffffffd570, eof=<value optimized out>, decode_failed=0x7fffffffd578) at fftools/ffmpeg.c:2455
  68. #11 0x00000000004cc1ad in process_input_packet (ist=0x23adfc0, pkt=<value optimized out>, no_eof=0) at fftools/ffmpeg.c:2609
  69. #12 0x00000000004ce659 in process_input () at fftools/ffmpeg.c:4441
  70. #13 transcode_step () at fftools/ffmpeg.c:4561
  71. #14 0x00000000004d0e65 in transcode (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4615
  72. #15 main (argc=<value optimized out>, argv=<value optimized out>) at fftools/ffmpeg.c:4847

  73. STEP5:  uninit()

5.3 filter_frame()调用流程


  1. 5.3.1. decode_video //ffmpeg.c
  2. 最初的源头,是ffmpeg.c的decode_video函数。
  3. 核心代码如下所示:
  4. static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
  5. {
  6.     AVFrame* decoded_frame, f;
  7.     //解码
  8.     ret = avcodec_decode_video2(ist->dec_ctx,
  9.                                 decoded_frame, got_output, pkt);
  10.     //......
  11.     //将解码后的帧数据 送给滤镜
  12.     for (i = 0; i < ist->nb_filters; i++) {
  13.         f = decodec_frame;
  14.         ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f, AV_BUFFERSRC_FLAG_PUSH);
  15.     }
  16. }

  17. 可见,最重要做2件事,一个解码,一个送给滤镜。
  18. 送给哪个滤镜呢?
  19. InputStream *ist的nb_filters为1,其实就是指向名字为“buffer”的filter(源文件:buffersrc.c)
  20. 这个filter与其他filter不同的是,它是所有filter的第一个入口。
  21. 解码完,都先给它,它再传递给下一个。为啥先给他呢?
  22. 很简单,它是一个FIFO,缓存数据用的。

  23. 5.3.2. av_buffersrc_add_frame_flags//buffersrc.c
  24. 该函数直接走到av_buffersrc_add_frame_internal //buffersrc.c

  25. 5.3.3. av_buffersrc_add_frame_internal //buffersrc.c
  26. static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
  27.                                            AVFrame *frame, int flags)
  28. {
  29.     AVFrame *copy = NULL;
  30.     av_frame_ref(copy, frame);

  31.     // 将解码后的帧数据 frame 写FIFO
  32.     av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL);

  33.     //
  34.     if ((flags & AV_BUFFERSRC_FLAG_PUSH))
  35.         if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
  36.             return ret;

  37.     return 0;
  38. }

  39. 可见, 该函数把frame写到FIFO,然后调了自己的output_pads[0]的request_frame。

  40. 5.3.4. request_frame //buffersrc.c
  41. static int request_frame(AVFilterLink *link)
  42. {
  43.     BufferSourceContext *c = link->src->priv;
  44.     AVFrame *frame;
  45.     int ret;

  46.     //省略......

  47.     // 从FIFO中读取一帧数据
  48.     av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
  49.     av_log(NULL, AV_LOG_WARNING, "request_frame, frame-pts %lld \n", frame->pts);

  50.     //这个link,是第一个link,链接当前的AVFilterContext和下一个AVFilterContext,也就用户设置的真正要用的滤镜代码
  51.     ret = ff_filter_frame(link, frame);

  52.     return ret;
  53. }

  54. 可见它从FIFO读取一帧数据。然后调用ff_filter_frame。此时输入的link是第一个AVFilterLink。

  55. 5.3.5. ff_filter_frame // avfilter.c
  56. 该函数做了一些基本检查,走到ff_filter_frame_framed

  57. 5.3.6. ff_filter_frame_framed //avfilter.c
  58. {
  59.     //定义一个函数指针filter_frame。所指向的函数,参数为AVFilterLink *, AVFrame *,返回值为int
  60.     int (*filter_frame)(AVFilterLink *, AVFrame *);

  61.     //下一个AVFilterContext,对本例来说,就是我们自己写的transform 滤镜,源码在vf_transform.c
  62.     AVFilterContext *dstctx = link->dst;
  63.     AVFilterPad *dst = link->dstpad;
  64.     AVFrame *out = NULL;
  65.     int ret;

  66.     //函数指针filter_frame,link->dstpad其实就是dstctx->input_pads,也就是transform滤镜定义的
  67.     if (!(filter_frame = dst->filter_frame))
  68.         filter_frame = default_filter_frame;
  69.     //省略300字

  70.     ret = filter_frame(link, out);
  71.     link->frame_count++;
  72.     ff_update_link_current_pts(link, pts);
  73.     return ret;
  74. }

  75. 定义一个函数指针filter_frame。
  76. 所指向的函数,必须是参数为(AVFilterLink , AVFrame ) ,返回值为int
  77. filter_frame = dst->filter_frame
  78. dst = link->dstpad,而link->dstpad就是transform过滤器定义的input_pads

  79. static const AVFilterPad avfilter_vf_transform_inputs[] = {
  80.     {
  81.         .name = "default",
  82.         .type = AVMEDIA_TYPE_VIDEO,
  83.         .filter_frame = filter_frame,
  84.     },
  85.     { NULL }
  86. };

  87. 所以,filter_frame函数指针,指向的就是vf_transform.c实现的filter_frame函数。

  88. 5.3.7. filter_frame //vf_transform.c,
  89. 当然啦,ffmpeg定义的各种filter,比如vf_colorbalance.c,vf_scale.c等,
  90. 也有这个函数,流程一样的
  91. static int filter_frame(AVFilterLink *link, AVFrame *in)
  92. {
  93.     AVFilterContext *avctx = link->dst; //第一个link的dst AVFilterContext,其实就是当前的filter的AVFilterContext
  94.     AVFilterLink *outlink = avctx->outputs[0]; //当前的AVFilterContext,outputs[0]指向第二个AVFilterLink
  95.     AVFrame *out;

  96.     //分配一个空的AVFrame。
  97.     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  98.     if (!out) {
  99.         av_frame_free(&in);
  100.         return AVERROR(ENOMEM);
  101.     }

  102.     //分配的空buffer的参数和上一个基本一致,但修改宽高。当然啦,如果你愿意,不修改宽高,那就不需要下面2句。
  103.     av_frame_copy_props(out, in);
  104.     out->width = outlink->w;
  105.     out->height = outlink->h;
  106.     out->format = outlink->format;

  107.     ThreadData td;
  108.     td.in = in;
  109.     td.out = out;
  110.     int res;
  111.     if(res = avctx->internal->execute(avctx, do_conversion, &td, NULL, FFMIN(outlink->h, avctx->graph->nb_threads))) {
  112.         return res;
  113.     }//启用一个子线程,执行比较耗时的变换。do_conversion是我们要做的变换。

  114.     av_frame_free(&in);
  115.     
  116.     // 此时的ff_filter_frame,输入参数和前面buffersrc.c调用的已经不一样。outlink是第二个AVFilterLink,buffer也是做了变换的新的buffer
  117.     return ff_filter_frame(outlink, out);
  118. }

  119. 通过ff_get_video_buffer,分配一个空buffer,该buffer用于存储变换的结果,
  120. 并会通过ff_filter_frame传递到下一个filter。
  121. do_conversion是一个真正做变换的函数,但其实如果要做的处理并不耗时,也不一定要用另一个线程来处理。
  122. 直接在该filter_frame做也行。
  123. 处理好的新的数据,放在out,调用ff_filter_frame,传递给下一个filter。
  124. 注意,ff_filter_frame的oulink,对应上图的第二个AVFilterLink。

  125. 5.3.8. 再次走进ff_filter_frame // avfilter.c
  126. 如上已知,ff_filter_frame只做了一些基本检查,走到ff_filter_frame_framed。
  127. 故而我们直接看ff_filter_frame_framed


  128. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  129. {
  130.     //定义一个函数指针filter_frame。所指向的函数,参数为AVFilterLink *, AVFrame *,返回值为int
  131.     int (*filter_frame)(AVFilterLink *, AVFrame *);

  132.      //下一个AVFilterContext,对本例来说,就是系统默认的第三个滤镜,名字叫"format",源码在vf_format.c
  133.     AVFilterContext *dstctx = link->dst;
  134.     AVFilterPad *dst = link->dstpad;
  135.     AVFrame *out = NULL;
  136.     int ret;

  137.     if (!(filter_frame = dst->filter_frame)) //vf_format.c没有实现filter函数,因为返回为空
  138.         filter_frame = default_filter_frame; //所以函数会走到这,函数指针filter_frame 将指向default_filter_frame
  139.     //省略300字

  140.     ret = filter_frame(link, out);
  141.     link->frame_count++;
  142.     ff_update_link_current_pts(link, pts);
  143.     return ret;
  144. }

  145. 如注释所说,由于vf_format.c没有实现filter函数,所以此时的filter_frame指针,指向的是defalut_filter_frame。

  146. 5.3.9. default_filter_frame //avfilter.c
  147. static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
  148. {
  149.     //该函数没干啥,又调用ff_filter_frame了,第一个参数,换成第三个AVFilterLink了,第二个参数不变,frame默默的传递出去
  150.     return ff_filter_frame(link->dst->outputs[0], frame);
  151. }

  152. 此时link->dst->outputs[0]对应上图第三个AVFilterLink。

  153. 5.3.10. 第三次走进ff_filter_frame // avfilter.c
  154. static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
  155. {
  156.     //定义一个函数指针filter_frame。所指向的函数,参数为AVFilterLink *, AVFrame *,返回值为int
  157.     int (*filter_frame)(AVFilterLink *, AVFrame *);

  158.     //下一个AVFilterContext,对本例来说,就是系统默认的最后一个滤镜,名字叫"buffersink",源码在bufffersink.c
  159.     AVFilterContext *dstctx = link->dst;
  160.     AVFilterPad *dst = link->dstpad;
  161.     AVFrame *out = NULL;
  162.     int ret;

  163.     if (!(filter_frame = dst->filter_frame)) //指向buffersink.c实现的filte_frame函数
  164.         filter_frame = default_filter_frame;
  165.     //省略300字

  166.     ret = filter_frame(link, out);
  167.     link->frame_count++;
  168.     ff_update_link_current_pts(link, pts);
  169.     return ret;
  170. }

  171. 此时的filter_frame指针,指向buffersink.c实现的filter_frame函数

  172. 5.3.11. filter_frame //buffersink.c

  173. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  174. {
  175.     AVFilterContext *ctx = link->dst;
  176.     BufferSinkContext *buf = link->dst->priv;
  177.     int ret;

  178.     if ((ret = add_buffer_ref(ctx, frame)) < 0)
  179.         return ret;
  180.     //省略300字
  181.     return 0;
  182. }

  183. static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
  184. {
  185.     BufferSinkContext *buf = ctx->priv;

  186.     /* cache frame */
  187.     //把buffer存到FIFO
  188.     av_fifo_generic_write(buf->fifo, &ref, FIFO_INIT_ELEMENT_SIZE, NULL);
  189.     return 0;
  190. }

  191. 很清晰的看到,其实就是把buffer存到FIFO。
  192. 至此,把filter_frame的来龙去脉搞清楚啦

5.4 filter之后,ffmpeg如何编码

当我们写了一个filter,把视频做处理后,ffmpeg是如何把它编码的呢?
通过研究,发现编码的源头函数是reap_filters(…),它会被transcode_step(…)函数调用。

  1. 5.4.1. reap_filters //ffmpeg.c
  2. static int reap_filters(int flush)
  3. {
  4.     AVFrame *filtered_frame = NULL;//该指针将存储一个经过滤镜处理后的buffer,并送给encoder
  5.     int i;

  6.     /* Reap all buffers present in the buffer sinks */
  7.     for (i = 0; i < nb_output_streams; i++) {//一路video,一路audio,那么nb_output_streams = 2
  8.         OutputStream *ost = output_streams[i];
  9.         OutputFile *of = output_files[ost->file_index];
  10.         AVFilterContext *filter;
  11.         AVCodecContext *enc = ost->enc_ctx;
  12.         int ret = 0;

  13.         if (!ost->filter)
  14.             continue;
  15.         filter = ost->filter->filter;//OutputStream的filter指针指向buffersink.c定义的AVFilterContext。也就是本文讨论的,最后一个AVFilterContext

  16.         if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
  17.             return AVERROR(ENOMEM);
  18.         }
  19.         filtered_frame = ost->filtered_frame;

  20.         while (1) {
  21.             double float_pts = AV_NOPTS_VALUE; // this is identical to filtered_frame.pts but with higher precision
  22.             //av_buffersink_get_frame_flags定义在buffersink.c,用于从FIFO读出一帧
  23.             ret = av_buffersink_get_frame_flags(filter, filtered_frame,
  24.                                                AV_BUFFERSINK_FLAG_NO_REQUEST);
  25.             if (ret < 0) {
  26.                 //省略,检查ret
  27.                 //如果ret<0,不是别的错误,那认为还没有数据,跳出循环
  28.                 break;
  29.             }
  30.             switch (filter->inputs[0]->type) {
  31.             case AVMEDIA_TYPE_VIDEO:
  32.                 //do_video_out函数将会做video编码
  33.                 do_video_out(of->ctx, ost, filtered_frame, float_pts);
  34.                 break;
  35.             case AVMEDIA_TYPE_AUDIO:
  36.                 //do_audio_out函数将会做audioo编码
  37.                 do_audio_out(of->ctx, ost, filtered_frame);
  38.                 break;
  39.             default:
  40.                 // TODO support subtitle filters
  41.                 av_assert0(0);
  42.             }

  43.             av_frame_unref(filtered_frame);
  44.         }
  45.     }

  46.     return 0;
  47. }

  48. 前一节说了,filter_frame()的最终结果是,把buffer存在了buffersink.c的FIFO里。
  49. 那么,这一节,说的其实就是一个从buffersink的FIFO读数据,并编码的过程。
  50. 从上面可知,av_buffersink_get_frame_flags函数,从buffersink读取一帧数据,放到filtered_frame。

  51. 5.4.2. do_video_out //ffmpeg.c
  52. static void do_video_out(AVFormatContext *s,
  53.                          OutputStream *ost,
  54.                          AVFrame *next_picture,
  55.                          double sync_ipts)
  56. {
  57.     int ret;
  58.     AVCodecContext *enc = ost->enc_ctx;
  59.     int nb_frames, nb0_frames, i;
  60.     //省略300字
  61.     for (i = 0; i < nb_frames; i++) {
  62.         AVFrame *in_picture;
  63.         if (i < nb0_frames && ost->last_frame) {
  64.             in_picture = ost->last_frame;
  65.         } else
  66.             in_picture = next_picture;
  67.         //省略300字
  68.         ost->frames_encoded++;
  69.         //开始编码
  70.         ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet);
  71.     }
  72.     ///省略300字
  73. }

  74. 该函数很长,做了很多杂事,但关键代码就是调用编码函数avcodec_encode_video2

5.5 函数流程图

下面给出ffmpeg使用filter时的函数流程图,主要把和filter相关的函数拉出来!

图7: ffmpeg带filter的流程图

ffmpeg常规的avcodec_register_all(…), avfilter_register_all(…), av_register_all(…)等函数用于注册各种资源

transcode_init() : 主要用于初始化前文提到的各种结构体。
transcode_step : 主要工作: 解码->送filter过滤->编码->继续解码….

choose_output() : 用于选择一个OutputStream。比如有一个audio,一个video,那要根据pts策略,比如谁的pts比较小,就挑哪个OutputStream先干活。
transcode_frome_filter() : 用于选个一个InputStream,用于下一步的process_input()。
process_input() : 主要是解码,并把解码的buffer送往filter处理。
reap_filters() : 主要是,从filter的FIFO拿出buffer,并编码。


6. 参考资料

FFmpeg官网: http://www.ffmpeg.org
FFmpeg doc : http://www.ffmpeg.org/documentation.html
FFmpeg wiki : https://trac.ffmpeg.org/wiki

阅读(1454) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册