Skip to content

Commit 9ed2fa8

Browse files
committed
fix(toxav): remove extra copy of video frame on encode
1 parent de30cf3 commit 9ed2fa8

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

toxav/video.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -516,20 +516,30 @@ int vc_encode(VCSession *vc, uint16_t width, uint16_t height, const uint8_t *y,
516516
vc->raw_encoder_frame_allocated = false;
517517
}
518518

519-
if (!vc->raw_encoder_frame_allocated) {
520-
if (vpx_img_alloc(&vc->raw_encoder_frame, VPX_IMG_FMT_I420, width, height, 1) == nullptr) {
521-
LOGGER_ERROR(vc->log, "Could not allocate image for frame");
522-
return -1;
519+
vpx_image_t *img = nullptr;
520+
vpx_image_t img_wrapped;
521+
if (vpx_img_wrap(&img_wrapped, VPX_IMG_FMT_I420, width, height, 1, (uint8_t *)y) != nullptr) {
522+
img = &img_wrapped;
523+
// vpx_img_wrap assumes contigues memory, so we fix that
524+
img->planes[VPX_PLANE_U] = (uint8_t *)u;
525+
img->planes[VPX_PLANE_V] = (uint8_t *)v;
526+
} else {
527+
// call to wrap failed, falling back to copy
528+
if (!vc->raw_encoder_frame_allocated) {
529+
if (vpx_img_alloc(&vc->raw_encoder_frame, VPX_IMG_FMT_I420, width, height, 1) == nullptr) {
530+
LOGGER_ERROR(vc->log, "Could not allocate image for frame");
531+
return -1;
532+
}
533+
534+
vc->raw_encoder_frame_allocated = true;
523535
}
524536

525-
vc->raw_encoder_frame_allocated = true;
526-
}
537+
img = &vc->raw_encoder_frame;
527538

528-
vpx_image_t *img = &vc->raw_encoder_frame;
529-
530-
memcpy(img->planes[VPX_PLANE_Y], y, (size_t)width * height);
531-
memcpy(img->planes[VPX_PLANE_U], u, ((size_t)width / 2) * (height / 2));
532-
memcpy(img->planes[VPX_PLANE_V], v, ((size_t)width / 2) * (height / 2));
539+
memcpy(img->planes[VPX_PLANE_Y], y, (size_t)width * height);
540+
memcpy(img->planes[VPX_PLANE_U], u, ((size_t)width / 2) * (height / 2));
541+
memcpy(img->planes[VPX_PLANE_V], v, ((size_t)width / 2) * (height / 2));
542+
}
533543

534544
int vpx_flags = 0;
535545

0 commit comments

Comments
 (0)