+ int j, k, n, off, cc_1, cc_2, cc[3], flag[3] ;
+ uint16_t s1, s2, s3, s4;
+ cc_1 = 0; cc_2 = 0;
+
+ int offset = 0;
+
+ if ( buf->flags & 16 )
+ {
+ /* Frame is progressive, be more discerning. */
+ color_diff = prog_diff;
+ color_equal = prog_equal;
+ threshold = prog_threshold;
+ }
+
+ /* One pas for Y, one pass for Cb, one pass for Cr */
+ for( k = 0; k < 3; k++ )
+ {
+ if( k == 1 )
+ {
+ /* Y has already been checked, now offset by Y's dimensions
+ and divide all the other values by 2, since Cr and Cb
+ are half-size compared to Y. */
+ offset = width * height;
+ width >>= 1;
+ height >>= 1;
+ }
+ else if ( k == 2 )
+ {
+ /* Y and Cb are done, so the offset needs to be bumped
+ so it's width*height + (width / 2) * (height / 2) */
+ offset *= 5/4;
+ }
+
+ for( j = 0; j < width; ++j )
+ {
+ off = 0;
+
+ for( n = 0; n < ( height - 4 ); n = n + 2 )
+ {
+ /* Look at groups of 4 sequential horizontal lines */
+ s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
+ s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
+ s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
+ s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
+
+ /* Note if the 1st and 2nd lines are more different in
+ color than the 1st and 3rd lines are similar in color.*/
+ if ( ( abs( s1 - s3 ) < color_equal ) &&
+ ( abs( s1 - s2 ) > color_diff ) )
+ ++cc_1;
+
+ /* Note if the 2nd and 3rd lines are more different in
+ color than the 2nd and 4th lines are similar in color.*/
+ if ( ( abs( s2 - s4 ) < color_equal ) &&
+ ( abs( s2 - s3 ) > color_diff) )
+ ++cc_2;
+
+ /* Now move down 2 horizontal lines before starting over.*/
+ off += 2 * width;
+ }
+ }
+
+ // compare results
+ /* The final cc score for a plane is the percentage of combed pixels it contains.
+ Because sensitivity goes down to hundreths of a percent, multiply by 1000
+ so it will be easy to compare against the threhold value which is an integer. */
+ cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
+ }
+
+
+ /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
+ int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
+
+ /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
+ if( average_cc > threshold )
+ {
+#if 0
+ hb_log("Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
+#endif
+ return 1;
+ }