Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display transformation matrix functions unavailable #263

Closed
KaFo opened this issue Apr 23, 2023 · 5 comments
Closed

Display transformation matrix functions unavailable #263

KaFo opened this issue Apr 23, 2023 · 5 comments

Comments

@KaFo
Copy link

KaFo commented Apr 23, 2023

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
Copy link
Owner

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

@SuRGeoNix
Copy link

SuRGeoNix commented May 3, 2023

@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
Copy link
Owner

Ruslan-B commented May 4, 2023

In which versions this needs to be added?

Ruslan-B pushed a commit that referenced this issue May 5, 2023
Ruslan-B added a commit that referenced this issue May 5, 2023
Co-authored-by: Ruslan Balanukhin <[email protected]>
Ruslan-B added a commit that referenced this issue May 5, 2023
Co-authored-by: Ruslan Balanukhin <[email protected]>
Ruslan-B added a commit that referenced this issue May 5, 2023
* Add display.h #263 (#265)

Co-authored-by: Ruslan Balanukhin <[email protected]>

* Fix codegen parameters in workflow (#262)

---------

Co-authored-by: Ruslan Balanukhin <[email protected]>
Co-authored-by: Nick Darvey <[email protected]>
@Ruslan-B
Copy link
Owner

Ruslan-B commented May 5, 2023

Available in 5.1.2.3 and 6.0.0.2

@Ruslan-B Ruslan-B closed this as completed May 5, 2023
@KaFo
Copy link
Author

KaFo commented May 20, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants