diff --git a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp index b427a49d40..845fe67cb1 100644 --- a/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp +++ b/src/add-ons/media/plugins/ffmpeg/AVCodecDecoder.cpp @@ -908,7 +908,9 @@ AVCodecDecoder::_UpdateMediaHeaderForVideoFrame() fHeader.start_time = fRawDecodedPicture->reordered_opaque; fHeader.u.raw_video.display_line_width = fRawDecodedPicture->width; fHeader.u.raw_video.display_line_count = fRawDecodedPicture->height; - fHeader.u.raw_video.bytes_per_row = 0; // TODO: Implement calculation + fHeader.u.raw_video.bytes_per_row + = CalculateBytesPerRowWithColorSpaceAndVideoWidth( + fOutputVideoFormat.display.format, fRawDecodedPicture->width); fHeader.u.raw_video.field_gamma = 1.0; fHeader.u.raw_video.field_sequence = fFrame; fHeader.u.raw_video.field_number = 0; diff --git a/src/add-ons/media/plugins/ffmpeg/Utilities.h b/src/add-ons/media/plugins/ffmpeg/Utilities.h index c82e85d10c..e02f076e42 100644 --- a/src/add-ons/media/plugins/ffmpeg/Utilities.h +++ b/src/add-ons/media/plugins/ffmpeg/Utilities.h @@ -7,13 +7,15 @@ #define UTILITIES_H -/*! \brief This file contains functions to convert values from FFmpeg to Media - Kit and vice versa. +/*! \brief This file contains functions to convert and calculate values from + FFmpeg to Media Kit and vice versa. */ #include +#include + extern "C" { #include "avcodec.h" } @@ -70,5 +72,37 @@ ConvertAVCodecContextToVideoAspectWidthAndHeight(AVCodecContext& contextIn, } +/*! \brief Calculates bytes per row for a video frame. + + \param colorSpace The Media Kit color space the video frame uses. + \param videoWidth The width of the video frame. + + \returns bytes per video frame row + \returns Zero, when bytes per video frame cannot be calculated. +*/ +inline uint32 +CalculateBytesPerRowWithColorSpaceAndVideoWidth(color_space colorSpace, int videoWidth) +{ + assert(videoWidth >= 0); + + const uint32 kBytesPerRowUnknown = 0; + size_t bytesPerPixel; + size_t rowAlignment; + + if (get_pixel_size_for(colorSpace, &bytesPerPixel, &rowAlignment, NULL) != B_OK) + return kBytesPerRowUnknown; + + uint32 bytesPerRow = bytesPerPixel * videoWidth; + uint32 numberOfUnalignedBytes = bytesPerRow % rowAlignment; + + if (numberOfUnalignedBytes == 0) + return bytesPerRow; + + uint32 numberOfBytesNeededForAlignment = rowAlignment - numberOfUnalignedBytes; + bytesPerRow += numberOfBytesNeededForAlignment; + + return bytesPerRow; +} + #endif // UTILITIES_H