[FFmpeg-devel] [RFC] threading API

Reimar Döffinger Reimar.Doeffinger
Tue Oct 6 15:13:46 CEST 2009


On Tue, Oct 06, 2009 at 01:52:47PM +0200, Michael Niedermayer wrote:
> On Tue, Oct 06, 2009 at 09:17:30AM +0200, Reimar D?ffinger wrote:
> > On Tue, Oct 06, 2009 at 09:01:15AM +0200, Reimar D?ffinger wrote:
> > > The later is only a very minor optimization, and if a API change should
> > > be avoided by all means, the former can be achieved also by adding to
> > > AVCodecContext a set_thread_number callback, that if set is executed
> > > each time before the main worker function with the same void *arg, i.e.
> > > void (*set_thread_number)(struct AVCodecContext *c, void *arg, int thread)
> > > 
> > > So, what are the opinions? Other ideas?
> > 
> > Actually, a better idea: add a
> > void *(get_worker_arg)(struct AVCodecContext *c, void *arg_array, int arg_size,
> > int arg_elem_size, int jobnr, int threadnr)
> > where the arg_ stuff matches with the avctx->execute (arg2, count, size)
> > arguments.
> 
> > The default implementation would then be
> > default_get_worker_arg:
> > return (char*)arg_array + jobnr*arg_elem_size;
> > whereas dnxhdenc would use something like
> > DNXHDContext *c = ((DNXHDContext **)arg_array)[threadnr];
> > c->start_slice = jobnr;
> > return c;
> 
> I see no reason why we ever should do 
> return (char*)arg_array + jobnr*arg_elem_size
> its always better to reuse contexts

Well, reason 1: it allows to change the existing code gradually
reason 2: it might be possible that some codecs do not need
to have a separate context for each thread anyway, they can all
use the same AVCodecContext (and its priv_data), but they need some
per-task data. I think that is what the current implementation
was thought for and which seems like a useful feature to me.
Of course you can do that with get_worker_arg to generate/copy-in that per-task
data and in the main function copy it out, but that might be some
pointless copying to save a minimal amount of memory (if the per-task
data is a bit of an effort to calculate but the same for multiple
execute calls this would even waste memory and CPU).
I admit that on thinking about it there might not be any real-world use case,
but it at least leaves reason 1 for now...



More information about the ffmpeg-devel mailing list