+int check_combing_mask( hb_filter_private_t * pv )
+{
+ /* Go through the mask in X*Y blocks. If any of these windows
+ have threshold or more combed pixels, consider the whole
+ frame to be combed and send it on to be deinterlaced. */
+
+ /* Block mask threshold -- The number of pixels
+ in a block_width * block_height window of
+ he mask that need to show combing for the
+ whole frame to be seen as such. */
+ int threshold = pv->block_threshold;
+ int block_width = pv->block_width;
+ int block_height = pv->block_height;
+ int block_x, block_y;
+ int block_score = 0; int send_to_blend = 0;
+
+ int x, y, k;
+
+ for( k = 0; k < 1; k++ )
+ {
+ int ref_stride = pv->ref_stride[k];
+ for( y = 0; y < ( pv->height[k] - block_height ); y = y + block_height )
+ {
+ for( x = 0; x < ( pv->width[k] - block_width ); x = x + block_width )
+ {
+ block_score = 0;
+ for( block_y = 0; block_y < block_height; block_y++ )
+ {
+ for( block_x = 0; block_x < block_width; block_x++ )
+ {
+ int mask_y = y + block_y;
+ int mask_x = x + block_x;
+
+ /* We only want to mark a pixel in a block as combed
+ if the pixels above and below are as well. Got to
+ handle the top and bottom lines separately. */
+ if( y + block_y == 0 )
+ {
+ if( pv->mask[k][mask_y*ref_stride+mask_x ] == 255 &&
+ pv->mask[k][mask_y*ref_stride+mask_x + 1] == 255 )
+ block_score++;
+ }
+ else if( y + block_y == pv->height[k] - 1 )
+ {
+ if( pv->mask[k][mask_y*ref_stride+mask_x - 1] == 255 &&
+ pv->mask[k][mask_y*ref_stride+mask_x ] == 255 )
+ block_score++;
+ }
+ else
+ {
+ if( pv->mask[k][mask_y*ref_stride+mask_x - 1] == 255 &&
+ pv->mask[k][mask_y*ref_stride+mask_x ] == 255 &&
+ pv->mask[k][mask_y*ref_stride+mask_x + 1] == 255 )
+ block_score++;
+ }
+ }
+ }
+
+ if( block_score >= ( threshold / 2 ) )
+ {
+#if 0
+ hb_log("decomb: frame %i | score %i | type %s", pv->yadif_deinterlaced_frames + pv->blend_deinterlaced_frames + pv->unfiltered_frames + 1, block_score, pv->buf_settings->flags & 16 ? "Film" : "Video");
+#endif
+ if ( block_score <= threshold && !( pv->buf_settings->flags & 16) )
+ {
+ /* Blend video content that scores between
+ ( threshold / 2 ) and threshold. */
+ send_to_blend = 1;
+ }
+ else if( block_score > threshold )
+ {
+ if( pv->buf_settings->flags & 16 )
+ {
+ /* Blend progressive content above the threshold.*/
+ return 2;
+ }
+ else
+ {
+ /* Yadif deinterlace video content above the threshold. */
+ return 1;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if( send_to_blend )
+ {
+ return 2;
+ }
+ else
+ {
+ /* Consider this frame to be uncombed. */
+ return 0;
+ }
+}
+
+int detect_combed_segment( hb_filter_private_t * pv, int segment_start, int segment_stop )
+{
+ /* A mish-mash of various comb detection tricks
+ picked up from neuron2's Decomb plugin for
+ AviSynth and tritical's IsCombedT and
+ IsCombedTIVTC plugins. */
+
+ int x, y, k, width, height;
+
+ /* Comb scoring algorithm */
+ int spatial_metric = pv->spatial_metric;
+ /* Motion threshold */
+ int mthresh = pv->motion_threshold;
+ /* Spatial threshold */
+ int athresh = pv->spatial_threshold;
+ int athresh_squared = athresh * athresh;
+ int athresh6 = 6 *athresh;
+
+ /* One pas for Y, one pass for U, one pass for V */
+ for( k = 0; k < 1; k++ )
+ {
+ int ref_stride = pv->ref_stride[k];
+ width = pv->width[k];
+ height = pv->height[k];
+
+ /* Comb detection has to start at y = 2 and end at
+ y = height - 2, because it needs to examine
+ 2 pixels above and 2 below the current pixel. */
+ if( segment_start < 2 )
+ segment_start = 2;
+ if( segment_stop > height - 2 )
+ segment_stop = height - 2;
+
+ for( y = segment_start; y < segment_stop; y++ )
+ {
+ /* These are just to make the buffer locations easier to read. */
+ int back_2 = ( y - 2 )*ref_stride ;
+ int back_1 = ( y - 1 )*ref_stride;
+ int current = y*ref_stride;
+ int forward_1 = ( y + 1 )*ref_stride;
+ int forward_2 = ( y + 2 )*ref_stride;
+
+ /* We need to examine a column of 5 pixels
+ in the prev, cur, and next frames. */
+ uint8_t previous_frame[5];
+ uint8_t current_frame[5];
+ uint8_t next_frame[5];
+
+ for( x = 0; x < width; x++ )
+ {
+ /* Fill up the current frame array with the current pixel values.*/
+ current_frame[0] = pv->ref[1][k][back_2 + x];
+ current_frame[1] = pv->ref[1][k][back_1 + x];
+ current_frame[2] = pv->ref[1][k][current + x];
+ current_frame[3] = pv->ref[1][k][forward_1 + x];
+ current_frame[4] = pv->ref[1][k][forward_2 + x];
+
+ int up_diff = current_frame[2] - current_frame[1];
+ int down_diff = current_frame[2] - current_frame[3];
+
+ if( ( up_diff > athresh && down_diff > athresh ) ||
+ ( up_diff < -athresh && down_diff < -athresh ) )
+ {
+ /* The pixel above and below are different,
+ and they change in the same "direction" too.*/
+ int motion = 0;
+ if( mthresh > 0 )
+ {
+ /* Make sure there's sufficient motion between frame t-1 to frame t+1. */
+ previous_frame[0] = pv->ref[0][k][back_2 + x];
+ previous_frame[1] = pv->ref[0][k][back_1 + x];
+ previous_frame[2] = pv->ref[0][k][current + x];
+ previous_frame[3] = pv->ref[0][k][forward_1 + x];
+ previous_frame[4] = pv->ref[0][k][forward_2 + x];
+ next_frame[0] = pv->ref[2][k][back_2 + x];
+ next_frame[1] = pv->ref[2][k][back_1 + x];
+ next_frame[2] = pv->ref[2][k][current + x];
+ next_frame[3] = pv->ref[2][k][forward_1 + x];
+ next_frame[4] = pv->ref[2][k][forward_2 + x];
+
+ if( abs( previous_frame[2] - current_frame[2] ) > mthresh &&
+ abs( current_frame[1] - next_frame[1] ) > mthresh &&
+ abs( current_frame[3] - next_frame[3] ) > mthresh )
+ motion++;
+ if( abs( next_frame[2] - current_frame[2] ) > mthresh &&
+ abs( previous_frame[1] - current_frame[1] ) > mthresh &&
+ abs( previous_frame[3] - current_frame[3] ) > mthresh )
+ motion++;
+ }
+ else
+ {
+ /* User doesn't want to check for motion,
+ so move on to the spatial check. */
+ motion = 1;
+ }
+
+ if( motion || ( pv->yadif_deinterlaced_frames==0 && pv->blend_deinterlaced_frames==0 && pv->unfiltered_frames==0) )
+ {
+ /* That means it's time for the spatial check.
+ We've got several options here. */
+ if( spatial_metric == 0 )
+ {
+ /* Simple 32detect style comb detection */
+ if( ( abs( current_frame[2] - current_frame[4] ) < 10 ) &&
+ ( abs( current_frame[2] - current_frame[3] ) > 15 ) )
+ {
+ pv->mask[k][y*ref_stride + x] = 255;
+ }
+ else
+ {
+ pv->mask[k][y*ref_stride + x] = 0;
+ }
+ }
+ else if( spatial_metric == 1 )
+ {
+ /* This, for comparison, is what IsCombed uses.
+ It's better, but still noise senstive. */
+ int combing = ( current_frame[1] - current_frame[2] ) *
+ ( current_frame[3] - current_frame[2] );
+
+ if( combing > athresh_squared )
+ pv->mask[k][y*ref_stride + x] = 255;
+ else
+ pv->mask[k][y*ref_stride + x] = 0;
+ }
+ else if( spatial_metric == 2 )
+ {
+ /* Tritical's noise-resistant combing scorer.
+ The check is done on a bob+blur convolution. */
+ int combing = abs( current_frame[0]
+ + ( 4 * current_frame[2] )
+ + current_frame[4]
+ - ( 3 * ( current_frame[1]
+ + current_frame[3] ) ) );
+
+ /* If the frame is sufficiently combed,
+ then mark it down on the mask as 255. */
+ if( combing > athresh6 )
+ pv->mask[k][y*ref_stride + x] = 255;
+ else
+ pv->mask[k][y*ref_stride + x] = 0;
+ }
+ }
+ else
+ {
+ pv->mask[k][y*ref_stride + x] = 0;
+ }
+ }
+ else
+ {
+ pv->mask[k][y*ref_stride + x] = 0;
+ }
+ }
+ }
+ }
+}
+
+typedef struct decomb_thread_arg_s {
+ hb_filter_private_t *pv;
+ int segment;
+} decomb_thread_arg_t;
+
+/*
+ * comb detect this segment of all three planes in a single thread.
+ */
+void decomb_filter_thread( void *thread_args_v )
+{
+ decomb_arguments_t *decomb_work = NULL;
+ hb_filter_private_t * pv;
+ int run = 1;
+ int segment, segment_start, segment_stop, plane;
+ decomb_thread_arg_t *thread_args = thread_args_v;
+
+ pv = thread_args->pv;
+ segment = thread_args->segment;
+
+ hb_log("decomb thread started for segment %d", segment);
+
+ while( run )
+ {
+ /*
+ * Wait here until there is work to do. hb_lock() blocks until
+ * render releases it to say that there is more work to do.
+ */
+ hb_lock( pv->decomb_begin_lock[segment] );
+
+ decomb_work = &pv->decomb_arguments[segment];
+
+ if( decomb_work->stop )
+ {
+ /*
+ * No more work to do, exit this thread.
+ */
+ run = 0;
+ continue;
+ }
+
+ /*
+ * Process segment (for now just from luma)
+ */
+ for( plane = 0; plane < 1; plane++)
+ {
+
+ int w = pv->width[plane];
+ int h = pv->height[plane];
+ int ref_stride = pv->ref_stride[plane];
+ segment_start = ( h / pv->cpu_count ) * segment;
+ if( segment == pv->cpu_count - 1 )
+ {
+ /*
+ * Final segment
+ */
+ segment_stop = h;
+ } else {
+ segment_stop = ( h / pv->cpu_count ) * ( segment + 1 );
+ }
+
+ detect_combed_segment( pv, segment_start, segment_stop );
+ }
+ /*
+ * Finished this segment, let everyone know.
+ */
+ hb_unlock( pv->decomb_complete_lock[segment] );
+ }
+ free( thread_args_v );
+}
+
+int comb_segmenter( hb_filter_private_t * pv )
+{
+ int segment;
+
+ for( segment = 0; segment < pv->cpu_count; segment++ )
+ {
+ /*
+ * Let the thread for this plane know that we've setup work
+ * for it by releasing the begin lock (ensuring that the
+ * complete lock is already locked so that we block when
+ * we try to lock it again below).
+ */
+ hb_lock( pv->decomb_complete_lock[segment] );
+ hb_unlock( pv->decomb_begin_lock[segment] );
+ }
+
+ /*
+ * Wait until all three threads have completed by trying to get
+ * the complete lock that we locked earlier for each thread, which
+ * will block until that thread has completed the work on that
+ * plane.
+ */
+ for( segment = 0; segment < pv->cpu_count; segment++ )
+ {
+ hb_lock( pv->decomb_complete_lock[segment] );
+ hb_unlock( pv->decomb_complete_lock[segment] );
+ }
+
+ return check_combing_mask( pv );
+}
+