@@ -11436,24 +11436,33 @@ static int mov_composite_tile_packet(AVFormatContext *s, AVStream *st, AVPacket
1143611436 int V = v_plane [uv_idx ];
1143711437
1143811438 // Match libheif's YUV to RGB conversion exactly
11439- // JPEG/JFIF uses full-range YCbCr with BT.601 coefficients
11440- // Using exact coefficients from ITU-R BT.601 standard
11441- int Y_val = Y ;
11442- int Cb = U - 128 ;
11443- int Cr = V - 128 ;
11439+ // Check if we should use limited range conversion
11440+ int Y_val , Cb , Cr ;
1144411441
11445- // BT.601 coefficients: Kr=0.299, Kg=0.587, Kb=0.114
11446- // Derived values: e=1.402, d=1.772
11447- // For G calculation: Kr*e/Kg = 0.299*1.402/0.587 = 0.71414
11448- // Kb*d/Kg = 0.114*1.772/0.587 = 0.34414
11449- double R_d = Y_val + 1.402 * Cr ;
11450- double G_d = Y_val - 0.34414 * Cb - 0.71414 * Cr ;
11451- double B_d = Y_val + 1.772 * Cb ;
11442+ // Check color range from the tile stream
11443+ AVStream * tile_stream = stg -> streams [i ];
1145211444
11453- // Round to nearest integer (matching libheif's rounding)
11454- uint8_t R = av_clip_uint8 ((int )(R_d + 0.5 ));
11455- uint8_t G = av_clip_uint8 ((int )(G_d + 0.5 ));
11456- uint8_t B = av_clip_uint8 ((int )(B_d + 0.5 ));
11445+ // HEIC uses full-range YUV (yuvj420p)
11446+ // Use full range conversion
11447+ Y_val = Y ;
11448+ Cb = U - 128 ;
11449+ Cr = V - 128 ;
11450+
11451+ // BT.601 coefficients for JPEG/JFIF full-range
11452+ // Using exact values to match libheif
11453+ // R = Y + 1.402 * Cr
11454+ // G = Y - 0.344136 * Cb - 0.714136 * Cr
11455+ // B = Y + 1.772 * Cb
11456+
11457+ // Use integer arithmetic for better precision matching
11458+ int R_i = Y_val + (1402 * Cr + 500 ) / 1000 ;
11459+ int G_i = Y_val - (344 * Cb + 714 * Cr + 500 ) / 1000 ;
11460+ int B_i = Y_val + (1772 * Cb + 500 ) / 1000 ;
11461+
11462+ // Clamp to valid range
11463+ uint8_t R = av_clip_uint8 (R_i );
11464+ uint8_t G = av_clip_uint8 (G_i );
11465+ uint8_t B = av_clip_uint8 (B_i );
1145711466
1145811467 // Apply color space conversion if needed
1145911468 if (need_p3_to_srgb ) {
0 commit comments