From 3a84713aaa5a0f83cbb3fdca62c83df609822c9c Mon Sep 17 00:00:00 2001 From: Roman Shaposhnik Date: Wed, 12 Nov 2008 17:47:23 +0000 Subject: [PATCH] Making it easier to send arbitrary structures as work orders to MT workers Originally committed as revision 15804 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/avcodec.h | 6 +++--- libavcodec/beosthread.c | 4 ++-- libavcodec/dnxhdenc.c | 14 +++++++------- libavcodec/dv.c | 8 ++++---- libavcodec/h264.c | 5 +++-- libavcodec/mpeg12.c | 4 ++-- libavcodec/mpegvideo_enc.c | 16 ++++++++-------- libavcodec/os2thread.c | 4 ++-- libavcodec/pthread.c | 9 ++++++--- libavcodec/utils.c | 4 ++-- libavcodec/w32thread.c | 4 ++-- 11 files changed, 41 insertions(+), 37 deletions(-) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 1b165ded1c..7d0cf0ed37 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -1876,7 +1876,7 @@ typedef struct AVCodecContext { * - encoding: Set by libavcodec, user can override. * - decoding: Set by libavcodec, user can override. */ - int (*execute)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg), void **arg2, int *ret, int count); + int (*execute)(struct AVCodecContext *c, int (*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size); /** * thread opaque @@ -2642,8 +2642,8 @@ enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum int avcodec_thread_init(AVCodecContext *s, int thread_count); void avcodec_thread_free(AVCodecContext *s); -int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count); -int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count); +int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size); +int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size); //FIXME func typedef /** diff --git a/libavcodec/beosthread.c b/libavcodec/beosthread.c index 4c99bc0ff5..e2fd97777d 100644 --- a/libavcodec/beosthread.c +++ b/libavcodec/beosthread.c @@ -92,7 +92,7 @@ void avcodec_thread_free(AVCodecContext *s){ av_freep(&s->thread_opaque); } -int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){ +int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ ThreadContext *c= s->thread_opaque; int i; @@ -102,7 +102,7 @@ int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, vo /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */ for(i=0; ithread[0]->qscale; @@ -499,7 +499,7 @@ static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg) static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg) { - DNXHDEncContext *ctx = arg; + DNXHDEncContext *ctx = *(void**)arg; int mb_y, mb_x; for (mb_y = ctx->m.start_mb_y; mb_y < ctx->m.end_mb_y; mb_y++) { @@ -555,7 +555,7 @@ static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx, uint8_t *buf) static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg) { - DNXHDEncContext *ctx = arg; + DNXHDEncContext *ctx = *(void**)arg; int mb_y, mb_x; for (mb_y = ctx->m.start_mb_y; mb_y < ctx->m.end_mb_y; mb_y++) { for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) { @@ -578,7 +578,7 @@ static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx) for (q = 1; q < avctx->qmax; q++) { ctx->qscale = q; - avctx->execute(avctx, dnxhd_calc_bits_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count); + avctx->execute(avctx, dnxhd_calc_bits_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count, sizeof(void*)); } up_step = down_step = 2<lambda; @@ -658,7 +658,7 @@ static int dnxhd_find_qscale(DNXHDEncContext *ctx) bits = 0; ctx->qscale = qscale; // XXX avoid recalculating bits - ctx->m.avctx->execute(ctx->m.avctx, dnxhd_calc_bits_thread, (void**)&ctx->thread[0], NULL, ctx->m.avctx->thread_count); + ctx->m.avctx->execute(ctx->m.avctx, dnxhd_calc_bits_thread, (void**)&ctx->thread[0], NULL, ctx->m.avctx->thread_count, sizeof(void*)); for (y = 0; y < ctx->m.mb_height; y++) { for (x = 0; x < ctx->m.mb_width; x++) bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits; @@ -731,7 +731,7 @@ static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx) } if (!ret) { if (RC_VARIANCE) - avctx->execute(avctx, dnxhd_mb_var_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count); + avctx->execute(avctx, dnxhd_mb_var_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count, sizeof(void*)); qsort(ctx->mb_cmp, ctx->m.mb_num, sizeof(RCEntry), dnxhd_rc_cmp); for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) { int mb = ctx->mb_cmp[x].mb; @@ -803,7 +803,7 @@ static int dnxhd_encode_picture(AVCodecContext *avctx, unsigned char *buf, int b assert(!(ctx->slice_size[i] & 3)); } - avctx->execute(avctx, dnxhd_encode_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count); + avctx->execute(avctx, dnxhd_encode_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count, sizeof(void*)); AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF diff --git a/libavcodec/dv.c b/libavcodec/dv.c index 8dec537a78..9599612e60 100644 --- a/libavcodec/dv.c +++ b/libavcodec/dv.c @@ -1013,14 +1013,14 @@ static inline void dv_encode_video_segment(DVVideoContext *s, int work_chunk) static int dv_decode_mt(AVCodecContext *avctx, void* sl) { - dv_decode_video_segment((DVVideoContext *)avctx->priv_data, (size_t)sl); + dv_decode_video_segment((DVVideoContext *)avctx->priv_data, *(size_t*)sl); return 0; } #ifdef CONFIG_DVVIDEO_ENCODER static int dv_encode_mt(AVCodecContext *avctx, void* sl) { - dv_encode_video_segment((DVVideoContext *)avctx->priv_data, (size_t)sl); + dv_encode_video_segment((DVVideoContext *)avctx->priv_data, *(size_t*)sl); return 0; } #endif @@ -1056,7 +1056,7 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, s->buf = buf; avctx->execute(avctx, dv_decode_mt, s->sys->work_chunks, NULL, - dv_work_pool_size(s->sys)); + dv_work_pool_size(s->sys), sizeof(void*)); emms_c(); @@ -1209,7 +1209,7 @@ static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, s->buf = buf; c->execute(c, dv_encode_mt, s->sys->work_chunks, NULL, - dv_work_pool_size(s->sys)); + dv_work_pool_size(s->sys), sizeof(void*)); emms_c(); diff --git a/libavcodec/h264.c b/libavcodec/h264.c index e501cb5ce9..426a3b8639 100644 --- a/libavcodec/h264.c +++ b/libavcodec/h264.c @@ -6626,7 +6626,8 @@ static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8 } } -static int decode_slice(struct AVCodecContext *avctx, H264Context *h){ +static int decode_slice(struct AVCodecContext *avctx, void *arg){ + H264Context *h = *(void**)arg; MpegEncContext * const s = &h->s; const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F; @@ -7346,7 +7347,7 @@ static void execute_decode_slices(H264Context *h, int context_count){ } avctx->execute(avctx, (void *)decode_slice, - (void **)h->thread_context, NULL, context_count); + (void **)h->thread_context, NULL, context_count, sizeof(void*)); /* pull back stuff from slices to master context */ hx = h->thread_context[context_count - 1]; diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c index 7d81863924..8b6a93ed26 100644 --- a/libavcodec/mpeg12.c +++ b/libavcodec/mpeg12.c @@ -1866,7 +1866,7 @@ eos: // end of slice } static int slice_decode_thread(AVCodecContext *c, void *arg){ - MpegEncContext *s= arg; + MpegEncContext *s= *(void**)arg; const uint8_t *buf= s->gb.buffer; int mb_y= s->start_mb_y; @@ -2299,7 +2299,7 @@ static int decode_chunks(AVCodecContext *avctx, if(avctx->thread_count > 1){ int i; - avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count); + avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count, sizeof(void*)); for(i=0; islice_count; i++) s2->error_count += s2->thread_context[i]->error_count; } diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index ae7f211454..9efc4424a7 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -1917,7 +1917,7 @@ static int sse_mb(MpegEncContext *s){ } static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){ - MpegEncContext *s= arg; + MpegEncContext *s= *(void**)arg; s->me.pre_pass=1; @@ -1936,7 +1936,7 @@ static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){ } static int estimate_motion_thread(AVCodecContext *c, void *arg){ - MpegEncContext *s= arg; + MpegEncContext *s= *(void**)arg; ff_check_alignment(); @@ -1963,7 +1963,7 @@ static int estimate_motion_thread(AVCodecContext *c, void *arg){ } static int mb_var_thread(AVCodecContext *c, void *arg){ - MpegEncContext *s= arg; + MpegEncContext *s= *(void**)arg; int mb_x, mb_y; ff_check_alignment(); @@ -2005,7 +2005,7 @@ static void write_slice_end(MpegEncContext *s){ } static int encode_thread(AVCodecContext *c, void *arg){ - MpegEncContext *s= arg; + MpegEncContext *s= *(void**)arg; int mb_x, mb_y, pdif = 0; int chr_h= 16>>s->chroma_y_shift; int i, j; @@ -2779,11 +2779,11 @@ static int encode_picture(MpegEncContext *s, int picture_number) s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8; if(s->pict_type != FF_B_TYPE && s->avctx->me_threshold==0){ if((s->avctx->pre_me && s->last_non_b_pict_type==FF_I_TYPE) || s->avctx->pre_me==2){ - s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); + s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count, sizeof(void*)); } } - s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); + s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count, sizeof(void*)); }else /* if(s->pict_type == FF_I_TYPE) */{ /* I-Frame */ for(i=0; imb_stride*s->mb_height; i++) @@ -2791,7 +2791,7 @@ static int encode_picture(MpegEncContext *s, int picture_number) if(!s->fixed_qscale){ /* finding spatial complexity for I-frame rate control */ - s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); + s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count, sizeof(void*)); } } for(i=1; iavctx->thread_count; i++){ @@ -2931,7 +2931,7 @@ static int encode_picture(MpegEncContext *s, int picture_number) for(i=1; iavctx->thread_count; i++){ update_duplicate_context_after_me(s->thread_context[i], s); } - s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count); + s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count, sizeof(void*)); for(i=1; iavctx->thread_count; i++){ merge_context_after_encode(s, s->thread_context[i]); } diff --git a/libavcodec/os2thread.c b/libavcodec/os2thread.c index 583f255ea0..edebc9a6c8 100644 --- a/libavcodec/os2thread.c +++ b/libavcodec/os2thread.c @@ -81,7 +81,7 @@ void avcodec_thread_free(AVCodecContext *s){ av_freep(&s->thread_opaque); } -int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){ +int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ ThreadContext *c= s->thread_opaque; int i; @@ -92,7 +92,7 @@ int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, vo for(i=0; icurrent_job_lock); - c->rets[our_job%c->rets_count] = c->func(avctx, c->args[our_job]); + c->rets[our_job%c->rets_count] = c->func(avctx, (char*)c->args + our_job*c->job_size); pthread_mutex_lock(&c->current_job_lock); our_job = c->current_job++; @@ -100,7 +101,7 @@ void avcodec_thread_free(AVCodecContext *avctx) av_freep(&avctx->thread_opaque); } -int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, int *ret, int job_count) +int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void *arg, int *ret, int job_count, int job_size) { ThreadContext *c= avctx->thread_opaque; int dummy_ret; @@ -112,6 +113,7 @@ int avcodec_thread_execute(AVCodecContext *avctx, action_t* func, void **arg, in c->current_job = avctx->thread_count; c->job_count = job_count; + c->job_size = job_size; c->args = arg; c->func = func; if (ret) { @@ -147,6 +149,7 @@ int avcodec_thread_init(AVCodecContext *avctx, int thread_count) avctx->thread_count = thread_count; c->current_job = 0; c->job_count = 0; + c->job_size = 0; c->done = 0; pthread_cond_init(&c->current_job_cond, NULL); pthread_cond_init(&c->last_job_cond, NULL); diff --git a/libavcodec/utils.c b/libavcodec/utils.c index b2be4b1772..2f162bcc2c 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -368,11 +368,11 @@ int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){ return 0; } -int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){ +int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ int i; for(i=0; ithread_opaque); } -int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){ +int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){ ThreadContext *c= s->thread_opaque; int i; @@ -84,7 +84,7 @@ int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, vo /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */ for(i=0; i