changeset 8129:a9734fe0811e libavcodec

Making it easier to send arbitrary structures as work orders to MT workers
author romansh
date Wed, 12 Nov 2008 17:47:23 +0000
parents e2241dd85c65
children c45366b01126
files avcodec.h beosthread.c dnxhdenc.c dv.c h264.c mpeg12.c mpegvideo_enc.c os2thread.c pthread.c utils.c w32thread.c
diffstat 11 files changed, 41 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/avcodec.h	Wed Nov 12 17:29:02 2008 +0000
+++ b/avcodec.h	Wed Nov 12 17:47:23 2008 +0000
@@ -1876,7 +1876,7 @@
      * - 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 @@
 
 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
 
 /**
--- a/beosthread.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/beosthread.c	Wed Nov 12 17:47:23 2008 +0000
@@ -92,7 +92,7 @@
     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 @@
     /* 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<count; i++){
-        c[i].arg= arg[i];
+        c[i].arg= (char*)arg + i*size;
         c[i].func= func;
         c[i].ret= 12345;
 
--- a/dnxhdenc.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/dnxhdenc.c	Wed Nov 12 17:47:23 2008 +0000
@@ -449,7 +449,7 @@
 
 static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg)
 {
-    DNXHDEncContext *ctx = arg;
+    DNXHDEncContext *ctx = *(void**)arg;
     int mb_y, mb_x;
     int qscale = ctx->thread[0]->qscale;
 
@@ -499,7 +499,7 @@
 
 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 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 @@
 
     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_FRAC_BITS;
     lambda = ctx->lambda;
@@ -658,7 +658,7 @@
         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 @@
     }
     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 @@
         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
 
--- a/dv.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/dv.c	Wed Nov 12 17:47:23 2008 +0000
@@ -1013,14 +1013,14 @@
 
 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 @@
 
     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 @@
 
     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();
 
--- a/h264.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/h264.c	Wed Nov 12 17:47:23 2008 +0000
@@ -6626,7 +6626,8 @@
     }
 }
 
-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 @@
         }
 
         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];
--- a/mpeg12.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/mpeg12.c	Wed Nov 12 17:47:23 2008 +0000
@@ -1866,7 +1866,7 @@
 }
 
 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 @@
                 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; i<s->slice_count; i++)
                         s2->error_count += s2->thread_context[i]->error_count;
                 }
--- a/mpegvideo_enc.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/mpegvideo_enc.c	Wed Nov 12 17:47:23 2008 +0000
@@ -1917,7 +1917,7 @@
 }
 
 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 estimate_motion_thread(AVCodecContext *c, void *arg){
-    MpegEncContext *s= arg;
+    MpegEncContext *s= *(void**)arg;
 
     ff_check_alignment();
 
@@ -1963,7 +1963,7 @@
 }
 
 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 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 @@
         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; i<s->mb_stride*s->mb_height; i++)
@@ -2791,7 +2791,7 @@
 
         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; i<s->avctx->thread_count; i++){
@@ -2931,7 +2931,7 @@
     for(i=1; i<s->avctx->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; i<s->avctx->thread_count; i++){
         merge_context_after_encode(s, s->thread_context[i]);
     }
--- a/os2thread.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/os2thread.c	Wed Nov 12 17:47:23 2008 +0000
@@ -81,7 +81,7 @@
     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 @@
 
     for(i=0; i<count; i++){
 
-        c[i].arg= arg[i];
+        c[i].arg= (char*)arg + i*size;
         c[i].func= func;
         c[i].ret= 12345;
 
--- a/pthread.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/pthread.c	Wed Nov 12 17:47:23 2008 +0000
@@ -30,10 +30,11 @@
 typedef struct ThreadContext {
     pthread_t *workers;
     action_t *func;
-    void **args;
+    void *args;
     int *rets;
     int rets_count;
     int job_count;
+    int job_size;
 
     pthread_cond_t last_job_cond;
     pthread_cond_t current_job_cond;
@@ -67,7 +68,7 @@
         }
         pthread_mutex_unlock(&c->current_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 @@
     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 @@
 
     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 @@
     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);
--- a/utils.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/utils.c	Wed Nov 12 17:47:23 2008 +0000
@@ -368,11 +368,11 @@
     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; i<count; i++){
-        int r= func(c, arg[i]);
+        int r= func(c, (char*)arg + i*size);
         if(ret) ret[i]= r;
     }
     return 0;
--- a/w32thread.c	Wed Nov 12 17:29:02 2008 +0000
+++ b/w32thread.c	Wed Nov 12 17:47:23 2008 +0000
@@ -74,7 +74,7 @@
     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;
 
@@ -84,7 +84,7 @@
     /* 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<count; i++){
-        c[i].arg= arg[i];
+        c[i].arg= (char*)arg + i*size;
         c[i].func= func;
         c[i].ret= 12345;