summaryrefslogtreecommitdiffstats
path: root/libhb
diff options
context:
space:
mode:
authorjstebbins <[email protected]>2011-06-08 21:20:08 +0000
committerjstebbins <[email protected]>2011-06-08 21:20:08 +0000
commitc3c5052550c010732b5ca31acf563ea1e3349d88 (patch)
tree5bc011e81849ed9eb94e189de0a6312e99c3386d /libhb
parent925b90ee9ee13afdc8efbf1bb3e46d564a67bb7e (diff)
libhb: Enable multi-threaded decode in ffmpeg
Enable both slice and frame based mutli-threaded decode in ffmpeg. Uses cpu_count/2 + 1 threads. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4034 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'libhb')
-rw-r--r--libhb/decavcodec.c12
-rw-r--r--libhb/decomb.c2
-rw-r--r--libhb/deinterlace.c2
-rw-r--r--libhb/encavcodec.c2
-rw-r--r--libhb/encavcodecaudio.c2
-rw-r--r--libhb/hb.c12
-rw-r--r--libhb/hbffmpeg.h4
-rw-r--r--libhb/stream.c2
-rw-r--r--libhb/sync.c2
9 files changed, 27 insertions, 13 deletions
diff --git a/libhb/decavcodec.c b/libhb/decavcodec.c
index fd7d24f14..568d2c7c4 100644
--- a/libhb/decavcodec.c
+++ b/libhb/decavcodec.c
@@ -204,8 +204,9 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
pv->parser = av_parser_init( codec_id );
pv->context = avcodec_alloc_context();
+ avcodec_get_context_defaults3(pv->context, codec);
hb_ff_set_sample_fmt( pv->context, codec );
- hb_avcodec_open( pv->context, codec );
+ hb_avcodec_open( pv->context, codec, 0 );
if ( w->audio != NULL )
{
@@ -522,8 +523,9 @@ static int decavcodecBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
AVCodecParserContext *parser = av_parser_init( codec->id );
AVCodecContext *context = avcodec_alloc_context();
+ avcodec_get_context_defaults3(context, codec);
hb_ff_set_sample_fmt( context, codec );
- hb_avcodec_open( context, codec );
+ hb_avcodec_open( context, codec, 0 );
uint8_t *buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE );
int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
unsigned char *pbuffer;
@@ -1161,9 +1163,9 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
// incorrectly initialized the 1st time avcodec_open is called.
// If you close it and open a 2nd time, it finishes the job.
hb_ff_set_sample_fmt( pv->context, codec );
- hb_avcodec_open( pv->context, codec );
+ hb_avcodec_open( pv->context, codec, 0 );
hb_avcodec_close( pv->context );
- hb_avcodec_open( pv->context, codec );
+ hb_avcodec_open( pv->context, codec, HB_FFMPEG_THREADS_AUTO );
}
if( in->start >= 0 )
@@ -1282,7 +1284,7 @@ static void init_ffmpeg_context( hb_work_object_t *w )
{
AVCodec *codec = avcodec_find_decoder( pv->context->codec_id );
hb_ff_set_sample_fmt( pv->context, codec );
- hb_avcodec_open( pv->context, codec );
+ hb_avcodec_open( pv->context, codec, HB_FFMPEG_THREADS_AUTO );
}
// set up our best guess at the frame duration.
// the frame rate in the codec is usually bogus but it's sometimes
diff --git a/libhb/decomb.c b/libhb/decomb.c
index 0873d4b6f..5e020b11e 100644
--- a/libhb/decomb.c
+++ b/libhb/decomb.c
@@ -1904,7 +1904,7 @@ hb_filter_private_t * hb_decomb_init( int pix_fmt,
avctx_enc->flags |= CODEC_FLAG_QPEL;
}
- hb_avcodec_open(avctx_enc, enc);
+ hb_avcodec_open(avctx_enc, enc, 0);
}
pv->mcdeint_frame = avcodec_alloc_frame();
diff --git a/libhb/deinterlace.c b/libhb/deinterlace.c
index 78856dd13..3080caead 100644
--- a/libhb/deinterlace.c
+++ b/libhb/deinterlace.c
@@ -653,7 +653,7 @@ hb_filter_private_t * hb_deinterlace_init( int pix_fmt,
avctx_enc->flags |= CODEC_FLAG_QPEL;
}
- hb_avcodec_open(avctx_enc, enc);
+ hb_avcodec_open(avctx_enc, enc, 0);
}
pv->mcdeint_frame = avcodec_alloc_frame();
diff --git a/libhb/encavcodec.c b/libhb/encavcodec.c
index da46db8c5..0474dd612 100644
--- a/libhb/encavcodec.c
+++ b/libhb/encavcodec.c
@@ -280,7 +280,7 @@ int encavcodecInit( hb_work_object_t * w, hb_job_t * job )
}
}
- if( hb_avcodec_open( context, codec ) )
+ if( hb_avcodec_open( context, codec, 0 ) )
{
hb_log( "encavcodecInit: avcodec_open failed" );
}
diff --git a/libhb/encavcodecaudio.c b/libhb/encavcodecaudio.c
index 86ab66300..9cf9c9831 100644
--- a/libhb/encavcodecaudio.c
+++ b/libhb/encavcodecaudio.c
@@ -93,7 +93,7 @@ static int encavcodecaInit( hb_work_object_t * w, hb_job_t * job )
// Try to set format to float. Fallback to whatever is supported.
hb_ff_set_sample_fmt( context, codec );
- if( hb_avcodec_open( context, codec ) )
+ if( hb_avcodec_open( context, codec, 0 ) )
{
hb_log( "encavcodecaInit: avcodec_open failed" );
return 1;
diff --git a/libhb/hb.c b/libhb/hb.c
index 1e3686cf2..b1050e0e0 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -99,9 +99,19 @@ void hb_avcodec_init()
av_register_all();
}
-int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
+int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec, int thread_count)
{
int ret;
+
+ if ( ( thread_count == HB_FFMPEG_THREADS_AUTO || thread_count > 0 ) &&
+ ( codec->type == AVMEDIA_TYPE_VIDEO ) )
+ {
+ avctx->thread_count = ( thread_count == HB_FFMPEG_THREADS_AUTO ) ?
+ hb_get_cpu_count() / 2 + 1 : thread_count;
+ avctx->thread_type = FF_THREAD_FRAME|FF_THREAD_SLICE;
+ avctx->thread_safe_callbacks = 1;
+ }
+
ret = avcodec_open(avctx, codec);
return ret;
}
diff --git a/libhb/hbffmpeg.h b/libhb/hbffmpeg.h
index 568eb5340..21108211e 100644
--- a/libhb/hbffmpeg.h
+++ b/libhb/hbffmpeg.h
@@ -7,8 +7,10 @@
#include "libavutil/opt.h"
#include "libswscale/swscale.h"
+#define HB_FFMPEG_THREADS_AUTO (-1) // let hb_avcodec_open decide thread_count
+
void hb_avcodec_init(void);
-int hb_avcodec_open( AVCodecContext *, struct AVCodec * );
+int hb_avcodec_open( AVCodecContext *, struct AVCodec *, int thread_count );
int hb_avcodec_close( AVCodecContext * );
int hb_ff_layout_xlat(int64_t ff_layout, int channels);
struct SwsContext*
diff --git a/libhb/stream.c b/libhb/stream.c
index 3467b1c97..48fbe68a7 100644
--- a/libhb/stream.c
+++ b/libhb/stream.c
@@ -3123,7 +3123,7 @@ static void ffmpeg_add_codec( hb_stream_t *stream, int stream_index )
context->error_concealment = FF_EC_GUESS_MVS|FF_EC_DEBLOCK;
AVCodec *codec = avcodec_find_decoder( context->codec_id );
hb_ff_set_sample_fmt( context, codec );
- hb_avcodec_open( context, codec );
+ hb_avcodec_open( context, codec, HB_FFMPEG_THREADS_AUTO );
}
// The ffmpeg stream reader / parser shares a lot of state with the
diff --git a/libhb/sync.c b/libhb/sync.c
index eedaedbab..f4cb29f25 100644
--- a/libhb/sync.c
+++ b/libhb/sync.c
@@ -1119,7 +1119,7 @@ static void InitAudio( hb_job_t * job, hb_sync_common_t * common, int i )
c->channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( w->audio->config.in.channel_layout );
c->sample_fmt = AV_SAMPLE_FMT_FLT;
- if( hb_avcodec_open( c, codec ) < 0 )
+ if( hb_avcodec_open( c, codec, 0 ) < 0 )
{
hb_log( "sync: avcodec_open failed" );
return;