shithub: libvpx

Download patch

ref: dadc9189ed3e6a1b5efd77333e6037d2b0e2f398
parent: f05feab7b967ffd717d9437b7803bfbcaeced9a4
parent: d02e74a0732896b632880884b3d839a316473ae0
author: John Koleszar <jkoleszar@google.com>
date: Tue Feb 21 05:09:23 EST 2012

Merge changes I0341554f,I64e110c8

* changes:
  Consolidate C version of token packing functions
  Multithreaded encoder, late sync loopfilter

--- a/vp8/encoder/bitstream.c
+++ b/vp8/encoder/bitstream.c
@@ -382,221 +382,25 @@
     int i;
     unsigned char *ptr = cx_data;
     unsigned char *ptr_end = cx_data_end;
-    unsigned int shift;
-    vp8_writer *w;
-    ptr = cx_data;
+    vp8_writer * w;
 
     for (i = 0; i < num_part; i++)
     {
+        int mb_row;
+
         w = cpi->bc + i + 1;
+
         vp8_start_encode(w, ptr, ptr_end);
-        {
-            unsigned int split;
-            int count = w->count;
-            unsigned int range = w->range;
-            unsigned int lowvalue = w->lowvalue;
-            int mb_row;
 
-            for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
-            {
-                TOKENEXTRA *p    = cpi->tplist[mb_row].start;
-                TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
-
-                while (p < stop)
-                {
-                    const int t = p->Token;
-                    vp8_token *const a = vp8_coef_encodings + t;
-                    const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
-                    int i = 0;
-                    const unsigned char *pp = p->context_tree;
-                    int v = a->value;
-                    int n = a->Len;
-
-                    if (p->skip_eob_node)
-                    {
-                        n--;
-                        i = 2;
-                    }
-
-                    do
-                    {
-                        const int bb = (v >> --n) & 1;
-                        split = 1 + (((range - 1) * pp[i>>1]) >> 8);
-                        i = vp8_coef_tree[i+bb];
-
-                        if (bb)
-                        {
-                            lowvalue += split;
-                            range = range - split;
-                        }
-                        else
-                        {
-                            range = split;
-                        }
+        for (mb_row = i; mb_row < cpi->common.mb_rows; mb_row += num_part)
+        {
+            const TOKENEXTRA *p    = cpi->tplist[mb_row].start;
+            const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
+            int tokens = stop - p;
 
-                        shift = vp8_norm[range];
-                        range <<= shift;
-                        count += shift;
-
-                        if (count >= 0)
-                        {
-                            int offset = shift - count;
-
-                            if ((lowvalue << (offset - 1)) & 0x80000000)
-                            {
-                                int x = w->pos - 1;
-
-                                while (x >= 0 && w->buffer[x] == 0xff)
-                                {
-                                    w->buffer[x] = (unsigned char)0;
-                                    x--;
-                                }
-
-                                w->buffer[x] += 1;
-                            }
-
-                            validate_buffer(w->buffer + w->pos,
-                                            1,
-                                            cx_data_end,
-                                            &cpi->common.error);
-
-                            w->buffer[w->pos++] = (lowvalue >> (24 - offset));
-
-                            lowvalue <<= offset;
-                            shift = count;
-                            lowvalue &= 0xffffff;
-                            count -= 8 ;
-                        }
+            pack_tokens_c(w, p, tokens);
+        }
 
-                        lowvalue <<= shift;
-                    }
-                    while (n);
-
-
-                    if (b->base_val)
-                    {
-                        const int e = p->Extra, L = b->Len;
-
-                        if (L)
-                        {
-                            const unsigned char *pp = b->prob;
-                            int v = e >> 1;
-                            int n = L;              /* number of bits in v, assumed nonzero */
-                            int i = 0;
-
-                            do
-                            {
-                                const int bb = (v >> --n) & 1;
-                                split = 1 + (((range - 1) * pp[i>>1]) >> 8);
-                                i = b->tree[i+bb];
-
-                                if (bb)
-                                {
-                                    lowvalue += split;
-                                    range = range - split;
-                                }
-                                else
-                                {
-                                    range = split;
-                                }
-
-                                shift = vp8_norm[range];
-                                range <<= shift;
-                                count += shift;
-
-                                if (count >= 0)
-                                {
-                                    int offset = shift - count;
-
-                                    if ((lowvalue << (offset - 1)) & 0x80000000)
-                                    {
-                                        int x = w->pos - 1;
-
-                                        while (x >= 0 && w->buffer[x] == 0xff)
-                                        {
-                                            w->buffer[x] = (unsigned char)0;
-                                            x--;
-                                        }
-
-                                        w->buffer[x] += 1;
-                                    }
-
-                                    validate_buffer(w->buffer + w->pos,
-                                                    1,
-                                                    cx_data_end,
-                                                    &cpi->common.error);
-
-                                    w->buffer[w->pos++] =
-                                        (lowvalue >> (24 - offset));
-
-                                    lowvalue <<= offset;
-                                    shift = count;
-                                    lowvalue &= 0xffffff;
-                                    count -= 8 ;
-                                }
-
-                                lowvalue <<= shift;
-                            }
-                            while (n);
-                        }
-
-                        {
-                            split = (range + 1) >> 1;
-
-                            if (e & 1)
-                            {
-                                lowvalue += split;
-                                range = range - split;
-                            }
-                            else
-                            {
-                                range = split;
-                            }
-
-                            range <<= 1;
-
-                            if ((lowvalue & 0x80000000))
-                            {
-                                int x = w->pos - 1;
-
-                                while (x >= 0 && w->buffer[x] == 0xff)
-                                {
-                                    w->buffer[x] = (unsigned char)0;
-                                    x--;
-                                }
-
-                                w->buffer[x] += 1;
-
-                            }
-
-                            lowvalue  <<= 1;
-
-                            if (!++count)
-                            {
-                                count = -8;
-                                validate_buffer(w->buffer + w->pos,
-                                                1,
-                                                cx_data_end,
-                                                &cpi->common.error);
-
-                                w->buffer[w->pos++] = (lowvalue >> 24);
-
-                                lowvalue &= 0xffffff;
-                            }
-                        }
-
-                    }
-
-                    ++p;
-                }
-            }
-
-            w->count    = count;
-            w->lowvalue = lowvalue;
-            w->range    = range;
-
-        }
-
         vp8_stop_encode(w);
         ptr += w->pos;
     }
@@ -605,208 +409,16 @@
 
 static void pack_mb_row_tokens_c(VP8_COMP *cpi, vp8_writer *w)
 {
-
-    unsigned int split;
-    int count = w->count;
-    unsigned int range = w->range;
-    unsigned int lowvalue = w->lowvalue;
-    unsigned int shift;
     int mb_row;
 
     for (mb_row = 0; mb_row < cpi->common.mb_rows; mb_row++)
     {
-        TOKENEXTRA *p    = cpi->tplist[mb_row].start;
-        TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
+        const TOKENEXTRA *p    = cpi->tplist[mb_row].start;
+        const TOKENEXTRA *stop = cpi->tplist[mb_row].stop;
+        int tokens = stop - p;
 
-        while (p < stop)
-        {
-            const int t = p->Token;
-            vp8_token *const a = vp8_coef_encodings + t;
-            const vp8_extra_bit_struct *const b = vp8_extra_bits + t;
-            int i = 0;
-            const unsigned char *pp = p->context_tree;
-            int v = a->value;
-            int n = a->Len;
-
-            if (p->skip_eob_node)
-            {
-                n--;
-                i = 2;
-            }
-
-            do
-            {
-                const int bb = (v >> --n) & 1;
-                split = 1 + (((range - 1) * pp[i>>1]) >> 8);
-                i = vp8_coef_tree[i+bb];
-
-                if (bb)
-                {
-                    lowvalue += split;
-                    range = range - split;
-                }
-                else
-                {
-                    range = split;
-                }
-
-                shift = vp8_norm[range];
-                range <<= shift;
-                count += shift;
-
-                if (count >= 0)
-                {
-                    int offset = shift - count;
-
-                    if ((lowvalue << (offset - 1)) & 0x80000000)
-                    {
-                        int x = w->pos - 1;
-
-                        while (x >= 0 && w->buffer[x] == 0xff)
-                        {
-                            w->buffer[x] = (unsigned char)0;
-                            x--;
-                        }
-
-                        w->buffer[x] += 1;
-                    }
-
-                    validate_buffer(w->buffer + w->pos,
-                                    1,
-                                    w->buffer_end,
-                                    w->error);
-
-                    w->buffer[w->pos++] = (lowvalue >> (24 - offset));
-                    lowvalue <<= offset;
-                    shift = count;
-                    lowvalue &= 0xffffff;
-                    count -= 8 ;
-                }
-
-                lowvalue <<= shift;
-            }
-            while (n);
-
-
-            if (b->base_val)
-            {
-                const int e = p->Extra, L = b->Len;
-
-                if (L)
-                {
-                    const unsigned char *pp = b->prob;
-                    int v = e >> 1;
-                    int n = L;              /* number of bits in v, assumed nonzero */
-                    int i = 0;
-
-                    do
-                    {
-                        const int bb = (v >> --n) & 1;
-                        split = 1 + (((range - 1) * pp[i>>1]) >> 8);
-                        i = b->tree[i+bb];
-
-                        if (bb)
-                        {
-                            lowvalue += split;
-                            range = range - split;
-                        }
-                        else
-                        {
-                            range = split;
-                        }
-
-                        shift = vp8_norm[range];
-                        range <<= shift;
-                        count += shift;
-
-                        if (count >= 0)
-                        {
-                            int offset = shift - count;
-
-                            if ((lowvalue << (offset - 1)) & 0x80000000)
-                            {
-                                int x = w->pos - 1;
-
-                                while (x >= 0 && w->buffer[x] == 0xff)
-                                {
-                                    w->buffer[x] = (unsigned char)0;
-                                    x--;
-                                }
-
-                                w->buffer[x] += 1;
-                            }
-
-                            validate_buffer(w->buffer + w->pos,
-                                            1,
-                                            w->buffer_end,
-                                            w->error);
-
-                            w->buffer[w->pos++] = (lowvalue >> (24 - offset));
-                            lowvalue <<= offset;
-                            shift = count;
-                            lowvalue &= 0xffffff;
-                            count -= 8 ;
-                        }
-
-                        lowvalue <<= shift;
-                    }
-                    while (n);
-                }
-
-                {
-                    split = (range + 1) >> 1;
-
-                    if (e & 1)
-                    {
-                        lowvalue += split;
-                        range = range - split;
-                    }
-                    else
-                    {
-                        range = split;
-                    }
-
-                    range <<= 1;
-
-                    if ((lowvalue & 0x80000000))
-                    {
-                        int x = w->pos - 1;
-
-                        while (x >= 0 && w->buffer[x] == 0xff)
-                        {
-                            w->buffer[x] = (unsigned char)0;
-                            x--;
-                        }
-
-                        w->buffer[x] += 1;
-
-                    }
-
-                    lowvalue  <<= 1;
-
-                    if (!++count)
-                    {
-                        count = -8;
-
-                        validate_buffer(w->buffer + w->pos,
-                                        1,
-                                        w->buffer_end,
-                                        w->error);
-
-                        w->buffer[w->pos++] = (lowvalue >> 24);
-                        lowvalue &= 0xffffff;
-                    }
-                }
-
-            }
-
-            ++p;
-        }
-    }
-
-    w->count = count;
-    w->lowvalue = lowvalue;
-    w->range = range;
+        pack_tokens_c(w, p, tokens);
+    }
 
 }
 
--- a/vp8/encoder/ethreading.c
+++ b/vp8/encoder/ethreading.c
@@ -24,9 +24,9 @@
 extern void vp8_build_block_offsets(MACROBLOCK *x);
 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
 
-extern void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
+extern void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
 
-static THREAD_FUNCTION loopfilter_thread(void *p_data)
+static THREAD_FUNCTION thread_loopfilter(void *p_data)
 {
     VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
     VP8_COMMON *cm = &cpi->common;
@@ -41,7 +41,7 @@
             if (cpi->b_multi_threaded == 0) // we're shutting down
                 break;
 
-            loopfilter_frame(cpi, cm);
+            vp8_loopfilter_frame(cpi, cm);
 
             sem_post(&cpi->h_event_end_lpf);
         }
@@ -468,6 +468,7 @@
 
     cpi->b_multi_threaded = 0;
     cpi->encoding_thread_count = 0;
+    cpi->b_lpf_running = 0;
 
     if (cm->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
     {
@@ -526,7 +527,7 @@
             sem_init(&cpi->h_event_end_lpf, 0, 0);
 
             lpfthd->ptr1 = (void *)cpi;
-            pthread_create(&cpi->h_filter_thread, 0, loopfilter_thread, lpfthd);
+            pthread_create(&cpi->h_filter_thread, 0, thread_loopfilter, lpfthd);
         }
     }
 
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -3086,7 +3086,7 @@
     }
 }
 
-void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
+void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
 {
     if (cm->no_lpf)
     {
@@ -3716,6 +3716,15 @@
             vp8_setup_key_frame(cpi);
         }
 
+#if CONFIG_MULTITHREAD
+        /*  wait for the last picture loopfilter thread done */
+        if (cpi->b_lpf_running)
+        {
+            sem_wait(&cpi->h_event_end_lpf);
+            cpi->b_lpf_running = 0;
+        }
+#endif
+
         // transform / motion compensation build reconstruction frame
         vp8_encode_frame(cpi);
 
@@ -4074,11 +4083,12 @@
     if (cpi->b_multi_threaded)
     {
         sem_post(&cpi->h_event_start_lpf); /* start loopfilter in separate thread */
+        cpi->b_lpf_running = 1;
     }
     else
 #endif
     {
-        loopfilter_frame(cpi, cm);
+        vp8_loopfilter_frame(cpi, cm);
     }
 
     update_reference_frames(cm);
@@ -4098,10 +4108,11 @@
     vp8_pack_bitstream(cpi, dest, dest_end, size);
 
 #if CONFIG_MULTITHREAD
-    /* wait for loopfilter thread done */
-    if (cpi->b_multi_threaded)
+    /* if PSNR packets are generated we have to wait for the lpf */
+    if (cpi->b_lpf_running && cpi->b_calculate_psnr)
     {
         sem_wait(&cpi->h_event_end_lpf);
+        cpi->b_lpf_running = 0;
     }
 #endif
 
@@ -5107,6 +5118,15 @@
     else
     {
         int ret;
+
+#if CONFIG_MULTITHREAD
+        if(cpi->b_lpf_running)
+        {
+            sem_wait(&cpi->h_event_end_lpf);
+            cpi->b_lpf_running = 0;
+        }
+#endif
+
 #if CONFIG_POSTPROC
         ret = vp8_post_proc_frame(&cpi->common, dest, flags);
 #else
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -525,6 +525,7 @@
     int mt_sync_range;
     int b_multi_threaded;
     int encoding_thread_count;
+    int b_lpf_running;
 
     pthread_t *h_encoding_thread;
     pthread_t h_filter_thread;
--