Use AVVideoScalingModeResizeAspect instead of abusing extended pixels
This not only eliminates the hand-rolled aspect ratio correction (which didn't actually handle scaling), but it also avoids us having to write to the GPU frames to add padding which improves performance.
This commit is contained in:
parent
a29d2e11ea
commit
ca041f2934
4 changed files with 12 additions and 91 deletions
|
|
@ -26,37 +26,15 @@ namespace platf {
|
|||
|
||||
av_img_t *av_img = (av_img_t *) &img;
|
||||
|
||||
size_t left_pad, right_pad, top_pad, bottom_pad;
|
||||
CVPixelBufferGetExtendedPixels(av_img->pixel_buffer, &left_pad, &right_pad, &top_pad, &bottom_pad);
|
||||
|
||||
const uint8_t *data = (const uint8_t *) CVPixelBufferGetBaseAddressOfPlane(av_img->pixel_buffer, 0) - left_pad - (top_pad * img.width);
|
||||
|
||||
int result = av_image_fill_arrays(av_frame->data, av_frame->linesize, data, (AVPixelFormat) av_frame->format, img.width, img.height, 32);
|
||||
|
||||
// We will create the black bars for the padding top/bottom or left/right here in very cheap way.
|
||||
// The luminance is 0, therefore, we simply need to set the chroma values to 128 for each pixel
|
||||
// for black bars (instead of green with chroma 0). However, this only works 100% correct, when
|
||||
// the resolution is devisable by 32. This could be improved by calculating the chroma values for
|
||||
// the outer content pixels, which should introduce only a minor performance hit.
|
||||
//
|
||||
// XXX: Improve the algorithm to take into account the outer pixels
|
||||
|
||||
size_t uv_plane_height = CVPixelBufferGetHeightOfPlane(av_img->pixel_buffer, 1);
|
||||
|
||||
if (left_pad || right_pad) {
|
||||
for (int l = 0; l < uv_plane_height + (top_pad / 2); l++) {
|
||||
int line = l * av_frame->linesize[1];
|
||||
memset((void *) &av_frame->data[1][line], 128, (size_t) left_pad);
|
||||
memset((void *) &av_frame->data[1][line + img.width - right_pad], 128, right_pad);
|
||||
}
|
||||
// Set up the data fields in the AVFrame to point into the mapped CVPixelBuffer
|
||||
int planes = CVPixelBufferGetPlaneCount(av_img->pixel_buffer->buf);
|
||||
for (int i = 0; i < planes; i++) {
|
||||
av_frame->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(av_img->pixel_buffer->buf, i);
|
||||
av_frame->data[i] = (uint8_t *) CVPixelBufferGetBaseAddressOfPlane(av_img->pixel_buffer->buf, i);
|
||||
}
|
||||
|
||||
if (top_pad || bottom_pad) {
|
||||
memset((void *) &av_frame->data[1][0], 128, (top_pad / 2) * av_frame->linesize[1]);
|
||||
memset((void *) &av_frame->data[1][((top_pad / 2) + uv_plane_height) * av_frame->linesize[1]], 128, bottom_pad / 2 * av_frame->linesize[1]);
|
||||
}
|
||||
|
||||
return result > 0 ? 0 : -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue