Ruslan-B / FFmpeg.AutoGen

FFmpeg auto generated unsafe bindings for C#/.NET and Core (Linux, MacOS and Mono).
GNU Lesser General Public License v3.0
1.34k stars 315 forks source link

Display transformation matrix functions unavailable #263

Closed KaFo closed 1 year ago

KaFo commented 1 year ago

Since ffmpeg 5.X, information about rotated videos (like phone recordings) are now longer provided via a stream metadata property "rotate" (with 90, 180, 270 values), but instead vbia a side_data of type DISPLAYMATRIX. See https://stackoverflow.com/a/75526656 for how to use.

To get from matrix to rotation angle, access to 3 simple display transformation matrix functions is needed: https://ffmpeg.org/doxygen/trunk/group__lavu__video__display.html

Long story short: these functions are not yet available in FFmpeg.AutoGen and should be added.

Ruslan-B commented 1 year ago

go it but need few days - tipped over kids toy got a minor injury - dominant hand so need few days

SuRGeoNix commented 1 year ago

@KaFo I needed that function lately too, so I've just recoded from FFmpeg to C#. Here it is if you need it until it will be included in FFmpeg.Autogen:-

static double Hypot(double x, double y) => Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
static double CONVFP(double x) => x / (1 << 16);
public static double av_display_rotation_get(byte* matrixBytes)
{
    if (matrixBytes == null)
        return 0;

    var matrix = (UInt32*) matrixBytes;

    double[] scale = new double[2];

    scale[0] = Hypot(CONVFP(matrix[0]), CONVFP(matrix[3]));
    scale[1] = Hypot(CONVFP(matrix[1]), CONVFP(matrix[4]));

    if (scale[0] == 0 && scale[1] == 0)
        return 0;

    double rotation = -(Math.Atan2(CONVFP(matrix[1]) / scale[1], CONVFP(matrix[0]) / scale[0]) * 180 / Math.PI);

    return rotation < 0 ? 360 + rotation : rotation;
}

And this is how I use it:-

double rotation = av_display_rotation_get(av_stream_get_side_data(AVStream, AVPacketSideDataType.AV_PKT_DATA_DISPLAYMATRIX, null));

(didn't tested it much yet so I hope is not buggy) (from ffmpeg: The angle will be in range [-180.0, 180.0] | rescaling to [0.0 - 360.0])

P.S.: @Ruslan-B hope you feel better soon and come back with a stronger hand ;)

Ruslan-B commented 1 year ago

In which versions this needs to be added?

Ruslan-B commented 1 year ago

Available in 5.1.2.3 and 6.0.0.2

KaFo commented 1 year ago

great, thanks! And also thank you @SuRGeoNix for the workaround.