ffmpeg多线程调研
1 解码
经调研,ffmpeg对h264, hevc多线程解码,提供配置线程数的接口,如下红色部分,AV_CODEC_CAP_SLICE_THREADS为支持片级多线程,AV_CODEC_CAP_FRAME_THREADS为支持帧级多线程。
AVCodec ff_h264_decoder = {
.name ="h264",
.long_name =NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
.type =AVMEDIA_TYPE_VIDEO,
.id =AV_CODEC_ID_H264,
.priv_data_size =sizeof(H264Context),
.init =ff_h264_decode_init,
.close =h264_decode_end,
.decode =h264_decode_frame,
.capabilities =/*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
AV_CODEC_CAP_FRAME_THREADS,
.caps_internal =FF_CODEC_CAP_INIT_THREADSAFE,
.flush =flush_dpb,
.init_thread_copy =ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
.update_thread_context =ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
.profiles =NULL_IF_CONFIG_SMALL(ff_h264_profiles),
.priv_class =&h264_class,
};
AVCodec ff_hevc_decoder = {
.name ="hevc",
.long_name =NULL_IF_CONFIG_SMALL("HEVC (High Efficiency Video Coding)"),
.type =AVMEDIA_TYPE_VIDEO,
.id =AV_CODEC_ID_HEVC,
.priv_data_size = sizeof(HEVCContext),
.priv_class =&hevc_decoder_class,
.init =hevc_decode_init,
.close =hevc_decode_free,
.decode =hevc_decode_frame,
.flush = hevc_decode_flush,
.update_thread_context = hevc_update_thread_context,
.init_thread_copy =hevc_init_thread_copy,
.capabilities =AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
.profiles =NULL_IF_CONFIG_SMALL(ff_hevc_profiles),
};
以H264解码为例的测试情况如下:
机器:Intel(R) Xeon(R) CPU E5620 @ 2.40GHz
CPU: 2CPU,1个CPU共4核,逻辑CPU的个数为16。
测试命令:
time ffmpeg -threads n -thread_type n -i /home/rec/264/s.264 -y /home/rec/264/1.yuv
thread_type为帧加速(1)的测试结果如下:
线程数
用时
0(自动16)
real 0m27.831s user 1m39.554s sys 0m7.999s
1
real 0m50.932s user 0m44.639s sys 0m3.462s
2
real 0m50.616s user 1m13.673s sys 0m6.223s
3
real 0m42.813s user 1m14.316s sys 0m6.542s
4
real 0m42.813s user 1m14.316s sys 0m6.542s
5
real 0m38.283s user 1m14.257s sys 0m6.368s
8
real 0m34.117s user 1m22.018s sys 0m6.985s
12
real 0m28.414s user 1m35.914s sys 0m7.889s
16
real 0m28.770s user 1m39.370s sys 0m8.875s
18
real 0m28.102s user 1m41.025s sys 0m8.331s
20
real 0m25.982s user 1m41.025s sys 0m8.331s
依上表有如下结论:
1 在多核的情况下,多线程解码比单线程解码用时要少。
2 线程并不是越多越好。
2编码
ffmpeg对h264, hevc多线程编码,不提供配置线程数的接口,如下红色部分,AVCodec ff_libx264_encoder = {
.name ="libx264",
.long_name =NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part10"),
.type =AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_H264,
.priv_data_size =sizeof(X264Context),
.init = X264_init,
.encode2 = X264_frame,
.close = X264_close,
.capabilities =AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
.priv_class =&x264_class,
.defaults = x264_defaults,
.init_static_data = X264_init_static,
.caps_internal =FF_CODEC_CAP_INIT_THREADSAFE |
FF_CODEC_CAP_INIT_CLEANUP,
};
AVCodec ff_libx265_encoder = {
.name ="libx265",
.long_name =NULL_IF_CONFIG_SMALL("libx265 H.265 / HEVC"),
.type =AVMEDIA_TYPE_VIDEO,
.id =AV_CODEC_ID_HEVC,
.init = libx265_encode_init,
.init_static_data = libx265_encode_init_csp,
.encode2 =libx265_encode_frame,
.close =libx265_encode_close,
.priv_data_size =sizeof(libx265Context),
.priv_class = &class,
.defaults = x265_defaults,
.capabilities =AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
};