vd_lavc: factor out libavcodec thread setup

This commit is contained in:
wm4 2013-12-04 20:58:19 +01:00
parent 0afd121ae6
commit 47c4b5c000
3 changed files with 21 additions and 15 deletions

View File

@ -21,10 +21,13 @@
#include <libavcodec/avcodec.h>
#include "mpvcore/mp_common.h"
#include "mpvcore/mp_msg.h"
#include "demux/packet.h"
#include "av_common.h"
#include "codecs.h"
#include "osdep/numcores.h"
// Copy the codec-related fields from st into avctx. This does not set the
// codec itself, only codec related header data provided by libavformat.
@ -114,6 +117,22 @@ void mp_set_av_packet(AVPacket *dst, struct demux_packet *mpkt, AVRational *tb)
dst->dts = mp_pts_to_av(mpkt ? mpkt->dts : MP_NOPTS_VALUE, tb);
}
void mp_set_avcodec_threads(AVCodecContext *avctx, int threads)
{
if (threads == 0) {
threads = default_thread_count();
if (threads < 1) {
mp_msg(MSGT_GLOBAL, MSGL_WARN, "Could not determine "
"thread count to use, defaulting to 1.\n");
threads = 1;
}
// Apparently some libavcodec versions have or had trouble with more
// than 16 threads, and/or print a warning when using > 16.
threads = MPMIN(threads, 16);
}
avctx->thread_count = threads;
}
void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type)
{
AVCodec *cur = NULL;

View File

@ -31,6 +31,7 @@ void mp_copy_lav_codec_headers(AVCodecContext *avctx, AVCodecContext *st);
void mp_set_av_packet(AVPacket *dst, struct demux_packet *mpkt, AVRational *tb);
int64_t mp_pts_to_av(double mp_pts, AVRational *tb);
double mp_pts_from_av(int64_t av_pts, AVRational *tb);
void mp_set_avcodec_threads(AVCodecContext *avctx, int threads);
void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type);
int mp_codec_to_av_codec_id(const char *codec);
const char *mp_codec_from_av_codec_id(int codec_id);

View File

@ -49,7 +49,6 @@
#include "video/decode/dec_video.h"
#include "demux/stheader.h"
#include "demux/packet.h"
#include "osdep/numcores.h"
#include "video/csputils.h"
#include "lavc.h"
@ -393,9 +392,6 @@ static void init_avctx(struct dec_video *vd, const char *decoder,
avctx->codec_type = AVMEDIA_TYPE_VIDEO;
avctx->codec_id = lavc_codec->id;
avctx->thread_count = lavc_param->threads;
#if HAVE_AVUTIL_REFCOUNTING
avctx->refcounted_frames = 1;
ctx->pic = av_frame_alloc();
@ -420,17 +416,7 @@ static void init_avctx(struct dec_video *vd, const char *decoder,
avctx->release_buffer = mp_codec_release_buffer;
}
#endif
}
if (avctx->thread_count == 0) {
int threads = default_thread_count();
if (threads < 1) {
mp_msg(MSGT_DECVIDEO, MSGL_WARN, "[VD_FFMPEG] Could not determine "
"thread count to use, defaulting to 1.\n");
threads = 1;
}
threads = FFMIN(threads, 16);
avctx->thread_count = threads;
mp_set_avcodec_threads(avctx, lavc_param->threads);
}
avctx->flags |= lavc_param->bitexact;