@@ -1718,34 +1718,58 @@ namespace TwkMovie
17181718 rotation = (rotEntry) ? atoi (rotEntry->value ) : 0 ;
17191719
17201720 // If rotation metadata not in metadata, try to get it from side data
1721- if (!rotEntry && videoStream-> codecpar -> nb_coded_side_data > 0 )
1721+ if (!rotEntry)
17221722 {
17231723 double rotationFromSideData = 0 ;
1724- for (int i = 0 ; i < videoStream->codecpar ->nb_coded_side_data ; ++i)
1724+ bool foundMatrix = false ;
1725+
1726+ if (videoStream->codecpar && videoStream->codecpar ->nb_coded_side_data > 0 )
17251727 {
1726- const AVPacketSideData* sd = &videoStream->codecpar ->coded_side_data [i];
1727- if (sd->type == AV_PKT_DATA_DISPLAYMATRIX)
1728+ for (int i = 0 ; i < videoStream->codecpar ->nb_coded_side_data ; ++i)
17281729 {
1729- rotationFromSideData = av_display_rotation_get ((int32_t *)sd->data );
1730+ const AVPacketSideData* sd = &videoStream->codecpar ->coded_side_data [i];
1731+ if (sd->type == AV_PKT_DATA_DISPLAYMATRIX)
1732+ {
1733+ rotationFromSideData = av_display_rotation_get ((int32_t *)sd->data );
1734+ foundMatrix = true ;
1735+ break ;
1736+ }
17301737 }
17311738 }
17321739
1733- // Getting rid of negative rotation metadata
1734- rotation = rotationFromSideData < 0 ? lround (rotationFromSideData) + 360 : lround (rotationFromSideData);
1735-
1736- // Setting rotation
1737- char charRotation[5 ]; // Expecting a number between -360 and 360
1738- // (inclusive)
1739- sprintf (charRotation, " %d" , rotation);
1740- if (av_dict_set (&videoStream->metadata , " rotate" , charRotation, 0 ) < 0 )
1740+ if (!foundMatrix && videoCodecContext && videoCodecContext->nb_coded_side_data > 0 )
17411741 {
1742- cout << " ERROR: Unable to rotate video, unable to parse "
1743- " rotation metadata."
1744- << endl;
1742+ for (int i = 0 ; i < videoCodecContext->nb_coded_side_data ; ++i)
1743+ {
1744+ const AVPacketSideData* sd = &videoCodecContext->coded_side_data [i];
1745+ if (sd->type == AV_PKT_DATA_DISPLAYMATRIX)
1746+ {
1747+ rotationFromSideData = av_display_rotation_get ((int32_t *)sd->data );
1748+ foundMatrix = true ;
1749+ break ;
1750+ }
1751+ }
17451752 }
1746- else
1753+
1754+ if (foundMatrix)
17471755 {
1748- m_info.proxy .attribute <string>(" Rotation" ) = charRotation;
1756+ // Getting rid of negative rotation metadata
1757+ rotation = rotationFromSideData < 0 ? lround (rotationFromSideData) + 360 : lround (rotationFromSideData);
1758+
1759+ // Setting rotation
1760+ char charRotation[5 ]; // Expecting a number between -360 and 360
1761+ // (inclusive)
1762+ sprintf (charRotation, " %d" , rotation);
1763+ if (av_dict_set (&videoStream->metadata , " rotate" , charRotation, 0 ) < 0 )
1764+ {
1765+ cout << " ERROR: Unable to rotate video, unable to parse "
1766+ " rotation metadata."
1767+ << endl;
1768+ }
1769+ else
1770+ {
1771+ m_info.proxy .attribute <string>(" Rotation" ) = charRotation;
1772+ }
17491773 }
17501774 }
17511775
0 commit comments