Support changing resolutions between passes with macroblock-tree
authorFiona Glaser <fiona@x264.com>
Sat, 9 Jun 2012 01:19:59 +0000 (18:19 -0700)
committerFiona Glaser <fiona@x264.com>
Tue, 17 Jul 2012 19:19:13 +0000 (12:19 -0700)
Implement a basic separable bilinear filter to rescale the quantizer offsets.
Structure inspired by swscale, but floating-point instead of fixed-point.
Not as optimized as it could be, but it's quite fast already.

Example compression penalties on a 720p video game recording:
First pass with 720p and second as 480p: ~-1.5% (vs. same res)
First pass with 480p and second as 720p: ~-3% (vs. same res)

encoder/ratecontrol.c

index 65b64ad..4303000 100644 (file)
@@ -136,9 +136,21 @@ struct x264_ratecontrol_t
     double lmin[3];             /* min qscale by frame type */
     double lmax[3];
     double lstep;               /* max change (multiply) in qscale per frame */
-    uint16_t *qp_buffer[2];     /* Global buffers for converting MB-tree quantizer data. */
-    int qpbuf_pos;              /* In order to handle pyramid reordering, QP buffer acts as a stack.
+    struct
+    {
+        uint16_t *qp_buffer[2]; /* Global buffers for converting MB-tree quantizer data. */
+        int qpbuf_pos;          /* In order to handle pyramid reordering, QP buffer acts as a stack.
                                  * This value is the current position (0 or 1). */
+        int src_mb_count;
+
+        /* For rescaling */
+        int rescale_enabled;
+        float *scale_buffer[2]; /* Intermediate buffers */
+        int filtersize[2];      /* filter size (H/V) */
+        float *coeffs[2];
+        int *pos[2];
+        int srcdim[2];          /* Source dimensions (W/H) */
+    } mbtree;
 
     /* MBRC stuff */
     float frame_size_estimated; /* Access to this variable must be atomic: double is
@@ -392,6 +404,130 @@ void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_off
     }
 }
 
+static int x264_macroblock_tree_rescale_init( x264_t *h, x264_ratecontrol_t *rc )
+{
+    /* Use fractional QP array dimensions to compensate for edge padding */
+    float srcdim[2] = {rc->mbtree.srcdim[0] / 16.f, rc->mbtree.srcdim[1] / 16.f};
+    float dstdim[2] = {    h->param.i_width / 16.f,    h->param.i_height / 16.f};
+    int srcdimi[2] = {ceil(srcdim[0]), ceil(srcdim[1])};
+    int dstdimi[2] = {ceil(dstdim[0]), ceil(dstdim[1])};
+    if( PARAM_INTERLACED )
+    {
+        srcdimi[1] = (srcdimi[1]+1)&~1;
+        dstdimi[1] = (dstdimi[1]+1)&~1;
+    }
+
+    rc->mbtree.src_mb_count = srcdimi[0] * srcdimi[1];
+
+    CHECKED_MALLOC( rc->mbtree.qp_buffer[0], rc->mbtree.src_mb_count * sizeof(uint16_t) );
+    if( h->param.i_bframe_pyramid && h->param.rc.b_stat_read )
+        CHECKED_MALLOC( rc->mbtree.qp_buffer[1], rc->mbtree.src_mb_count * sizeof(uint16_t) );
+    rc->mbtree.qpbuf_pos = -1;
+
+    /* No rescaling to do */
+    if( srcdimi[0] == dstdimi[0] && srcdimi[1] == dstdimi[1] )
+        return 0;
+
+    rc->mbtree.rescale_enabled = 1;
+
+    /* Allocate intermediate scaling buffers */
+    CHECKED_MALLOC( rc->mbtree.scale_buffer[0], srcdimi[0] * srcdimi[1] * sizeof(float) );
+    CHECKED_MALLOC( rc->mbtree.scale_buffer[1], dstdimi[0] * srcdimi[1] * sizeof(float) );
+
+    /* Allocate and calculate resize filter parameters and coefficients */
+    for( int i = 0; i < 2; i++ )
+    {
+        if( srcdim[i] > dstdim[i] ) // downscale
+            rc->mbtree.filtersize[i] = 1 + (2 * srcdimi[i] + dstdimi[i] - 1) / dstdimi[i];
+        else                        // upscale
+            rc->mbtree.filtersize[i] = 3;
+
+        CHECKED_MALLOC( rc->mbtree.coeffs[i], rc->mbtree.filtersize[i] * dstdimi[i] * sizeof(float) );
+        CHECKED_MALLOC( rc->mbtree.pos[i], dstdimi[i] * sizeof(int) );
+
+        /* Initialize filter coefficients */
+        float inc = srcdim[i] / dstdim[i];
+        float dmul = inc > 1.f ? dstdim[i] / srcdim[i] : 1.f;
+        float dstinsrc = 0.5f * inc - 0.5f;
+        int filtersize = rc->mbtree.filtersize[i];
+        for( int j = 0; j < dstdimi[i]; j++ )
+        {
+            int pos = dstinsrc - (filtersize - 2.f) * 0.5f;
+            float sum = 0.0;
+            rc->mbtree.pos[i][j] = pos;
+            for( int k = 0; k < filtersize; k++ )
+            {
+                float d = fabs( pos + k - dstinsrc ) * dmul;
+                float coeff = X264_MAX( 1.f - d, 0 );
+                rc->mbtree.coeffs[i][j * filtersize + k] = coeff;
+                sum += coeff;
+            }
+            sum = 1.0f / sum;
+            for( int k = 0; k < filtersize; k++ )
+                rc->mbtree.coeffs[i][j * filtersize + k] *= sum;
+            dstinsrc += inc;
+        }
+    }
+
+    /* Write back actual qp array dimensions */
+    rc->mbtree.srcdim[0] = srcdimi[0];
+    rc->mbtree.srcdim[1] = srcdimi[1];
+    return 0;
+fail:
+    return -1;
+}
+
+static void x264_macroblock_tree_rescale_destroy( x264_ratecontrol_t *rc )
+{
+    for( int i = 0; i < 2; i++ )
+    {
+        x264_free( rc->mbtree.qp_buffer[i] );
+        x264_free( rc->mbtree.scale_buffer[i] );
+        x264_free( rc->mbtree.coeffs[i] );
+        x264_free( rc->mbtree.pos[i] );
+    }
+}
+
+static ALWAYS_INLINE float tapfilter( float *src, int pos, int max, int stride, float *coeff, int filtersize )
+{
+    float sum = 0.f;
+    for( int i = 0; i < filtersize; i++, pos++ )
+        sum += src[x264_clip3( pos, 0, max-1 )*stride] * coeff[i];
+    return sum;
+}
+
+static void x264_macroblock_tree_rescale( x264_t *h, x264_ratecontrol_t *rc, float *dst )
+{
+    float *input, *output;
+    int filtersize, stride, height;
+
+    /* H scale first */
+    input = rc->mbtree.scale_buffer[0];
+    output = rc->mbtree.scale_buffer[1];
+    filtersize = rc->mbtree.filtersize[0];
+    stride = rc->mbtree.srcdim[0];
+    height = rc->mbtree.srcdim[1];
+    for( int y = 0; y < height; y++, input += stride, output += h->mb.i_mb_width )
+    {
+        float *coeff = rc->mbtree.coeffs[0];
+        for( int x = 0; x < h->mb.i_mb_width; x++, coeff+=filtersize )
+            output[x] = tapfilter( input, rc->mbtree.pos[0][x], stride, 1, coeff, filtersize );
+    }
+
+    /* V scale next */
+    input = rc->mbtree.scale_buffer[1];
+    output = dst;
+    filtersize = rc->mbtree.filtersize[1];
+    stride = h->mb.i_mb_width;
+    height = rc->mbtree.srcdim[1];
+    for( int x = 0; x < h->mb.i_mb_width; x++, input++, output++ )
+    {
+        float *coeff = rc->mbtree.coeffs[1];
+        for( int y = 0; y < h->mb.i_mb_height; y++, coeff+=filtersize )
+            output[y*stride] = tapfilter( input, rc->mbtree.pos[1][y], height, stride, coeff, filtersize );
+    }
+}
+
 int x264_macroblock_tree_read( x264_t *h, x264_frame_t *frame, float *quant_offsets )
 {
     x264_ratecontrol_t *rc = h->rc;
@@ -400,18 +536,18 @@ int x264_macroblock_tree_read( x264_t *h, x264_frame_t *frame, float *quant_offs
     if( rc->entry[frame->i_frame].kept_as_ref )
     {
         uint8_t i_type;
-        if( rc->qpbuf_pos < 0 )
+        if( rc->mbtree.qpbuf_pos < 0 )
         {
             do
             {
-                rc->qpbuf_pos++;
+                rc->mbtree.qpbuf_pos++;
 
                 if( !fread( &i_type, 1, 1, rc->p_mbtree_stat_file_in ) )
                     goto fail;
-                if( fread( rc->qp_buffer[rc->qpbuf_pos], sizeof(uint16_t), h->mb.i_mb_count, rc->p_mbtree_stat_file_in ) != h->mb.i_mb_count )
+                if( fread( rc->mbtree.qp_buffer[rc->mbtree.qpbuf_pos], sizeof(uint16_t), rc->mbtree.src_mb_count, rc->p_mbtree_stat_file_in ) != rc->mbtree.src_mb_count )
                     goto fail;
 
-                if( i_type != i_type_actual && rc->qpbuf_pos == 1 )
+                if( i_type != i_type_actual && rc->mbtree.qpbuf_pos == 1 )
                 {
                     x264_log( h, X264_LOG_ERROR, "MB-tree frametype %d doesn't match actual frametype %d.\n", i_type, i_type_actual );
                     return -1;
@@ -419,13 +555,18 @@ int x264_macroblock_tree_read( x264_t *h, x264_frame_t *frame, float *quant_offs
             } while( i_type != i_type_actual );
         }
 
-        for( int i = 0; i < h->mb.i_mb_count; i++ )
+        float *dst = rc->mbtree.rescale_enabled ? rc->mbtree.scale_buffer[0] : frame->f_qp_offset;
+        for( int i = 0; i < rc->mbtree.src_mb_count; i++ )
         {
-            frame->f_qp_offset[i] = ((float)(int16_t)endian_fix16( rc->qp_buffer[rc->qpbuf_pos][i] )) * (1/256.0);
-            if( h->frames.b_have_lowres )
-                frame->i_inv_qscale_factor[i] = x264_exp2fix8(frame->f_qp_offset[i]);
+            int16_t qp_fix8 = endian_fix16( rc->mbtree.qp_buffer[rc->mbtree.qpbuf_pos][i] );
+            dst[i] = qp_fix8 * (1.f/256.f);
         }
-        rc->qpbuf_pos--;
+        if( rc->mbtree.rescale_enabled )
+            x264_macroblock_tree_rescale( h, rc, frame->f_qp_offset );
+        if( h->frames.b_have_lowres )
+            for( int i = 0; i < h->mb.i_mb_count; i++ )
+                frame->i_inv_qscale_factor[i] = x264_exp2fix8( frame->f_qp_offset[i] );
+        rc->mbtree.qpbuf_pos--;
     }
     else
         x264_stack_align( x264_adaptive_quant_frame, h, frame, quant_offsets );
@@ -762,11 +903,10 @@ int x264_ratecontrol_new( x264_t *h )
                 x264_log( h, X264_LOG_ERROR, "resolution specified in stats file not valid\n" );
                 return -1;
             }
-            else if( h->param.rc.b_mb_tree && (i != h->param.i_width || j != h->param.i_height)  )
+            else if( h->param.rc.b_mb_tree )
             {
-                x264_log( h, X264_LOG_ERROR, "MB-tree doesn't support different resolution than 1st pass (%dx%d vs %dx%d)\n",
-                          h->param.i_width, h->param.i_height, i, j );
-                return -1;
+                rc->mbtree.srcdim[0] = i;
+                rc->mbtree.srcdim[1] = j;
             }
             res_factor = (float)h->param.i_width * h->param.i_height / (i*j);
             /* Change in bits relative to resolution isn't quite linear on typical sources,
@@ -1025,10 +1165,13 @@ parse_error:
 
     if( h->param.rc.b_mb_tree && (h->param.rc.b_stat_read || h->param.rc.b_stat_write) )
     {
-        CHECKED_MALLOC( rc->qp_buffer[0], h->mb.i_mb_count * sizeof(uint16_t) );
-        if( h->param.i_bframe_pyramid && h->param.rc.b_stat_read )
-            CHECKED_MALLOC( rc->qp_buffer[1], h->mb.i_mb_count * sizeof(uint16_t) );
-        rc->qpbuf_pos = -1;
+        if( !h->param.rc.b_stat_read )
+        {
+            rc->mbtree.srcdim[0] = h->param.i_width;
+            rc->mbtree.srcdim[1] = h->param.i_height;
+        }
+        if( x264_macroblock_tree_rescale_init( h, rc ) < 0 )
+            return -1;
     }
 
     for( int i = 0; i<h->param.i_threads; i++ )
@@ -1216,8 +1359,7 @@ void x264_ratecontrol_delete( x264_t *h )
     x264_free( rc->pred );
     x264_free( rc->pred_b_from_p );
     x264_free( rc->entry );
-    x264_free( rc->qp_buffer[0] );
-    x264_free( rc->qp_buffer[1] );
+    x264_macroblock_tree_rescale_destroy( rc );
     if( rc->zones )
     {
         x264_free( rc->zones[0].param );
@@ -1708,10 +1850,10 @@ int x264_ratecontrol_end( x264_t *h, int bits, int *filler )
             uint8_t i_type = h->sh.i_type;
             /* Values are stored as big-endian FIX8.8 */
             for( int i = 0; i < h->mb.i_mb_count; i++ )
-                rc->qp_buffer[0][i] = endian_fix16( h->fenc->f_qp_offset[i]*256.0 );
+                rc->mbtree.qp_buffer[0][i] = endian_fix16( h->fenc->f_qp_offset[i]*256.0 );
             if( fwrite( &i_type, 1, 1, rc->p_mbtree_stat_file_out ) < 1 )
                 goto fail;
-            if( fwrite( rc->qp_buffer[0], sizeof(uint16_t), h->mb.i_mb_count, rc->p_mbtree_stat_file_out ) < h->mb.i_mb_count )
+            if( fwrite( rc->mbtree.qp_buffer[0], sizeof(uint16_t), h->mb.i_mb_count, rc->p_mbtree_stat_file_out ) < h->mb.i_mb_count )
                 goto fail;
         }
     }
@@ -2527,7 +2669,7 @@ void x264_thread_sync_ratecontrol( x264_t *cur, x264_t *prev, x264_t *next )
         COPY(short_term_cplxcount);
         COPY(bframes);
         COPY(prev_zone);
-        COPY(qpbuf_pos);
+        COPY(mbtree.qpbuf_pos);
         /* these vars can be updated by x264_ratecontrol_init_reconfigurable */
         COPY(bitrate);
         COPY(buffer_size);